execvp() 输出为 ncurses 创建缩进
execvp() output creates indent for ncurses
我当前的项目遇到了一个奇怪的问题。使用 ncurses 我正在制作一个基于 lsh 的 shell,在我引入 ncurses 之前它就像人们期望的那样工作,只需写入 execvp 的输出。然而现在,输出的长度在我的提示之前做了一个缩进,这实际上也将 X 协调移动到了一边(因此缩进似乎不是该行的一部分)。
我认为这是由于分叉到一个没有 ncurses(或类似的东西)的子进程。
您可以查看完整代码 here 但这是 execvp 所在的部分 运行:
int shell_launch(char **args) {
pid_t pid;
int status;
pid = fork();
if (pid == 0) {
// Child process.
// Check if user is trying to run an allowed program.
for (int i = 0; i < arrlen(allowed_cmds); i++) {
if (strcmp(allowed_cmds[i], args[0]) == 0) {
if (execvp(args[0], args) == -1) {
perror("shell");
}
}
}
exit(EXIT_FAILURE);
} else if (pid < 0) {
// Error forking
perror("shell");
} else {
// Parent process
do {
waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
如果您已使用 ncurses 初始化屏幕,则处于 原始模式,这(除其他事项外)使换行符不再映射到 carriage-return/line-feed。
如果您要 运行 一个子 shell,那么在途中您应该恢复终端模式,然后在 return 上恢复原始模式。这些是用 reset_shell_mode 和 reset_prog_mode 完成的。
我当前的项目遇到了一个奇怪的问题。使用 ncurses 我正在制作一个基于 lsh 的 shell,在我引入 ncurses 之前它就像人们期望的那样工作,只需写入 execvp 的输出。然而现在,输出的长度在我的提示之前做了一个缩进,这实际上也将 X 协调移动到了一边(因此缩进似乎不是该行的一部分)。
我认为这是由于分叉到一个没有 ncurses(或类似的东西)的子进程。
您可以查看完整代码 here 但这是 execvp 所在的部分 运行:
int shell_launch(char **args) {
pid_t pid;
int status;
pid = fork();
if (pid == 0) {
// Child process.
// Check if user is trying to run an allowed program.
for (int i = 0; i < arrlen(allowed_cmds); i++) {
if (strcmp(allowed_cmds[i], args[0]) == 0) {
if (execvp(args[0], args) == -1) {
perror("shell");
}
}
}
exit(EXIT_FAILURE);
} else if (pid < 0) {
// Error forking
perror("shell");
} else {
// Parent process
do {
waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
如果您已使用 ncurses 初始化屏幕,则处于 原始模式,这(除其他事项外)使换行符不再映射到 carriage-return/line-feed。
如果您要 运行 一个子 shell,那么在途中您应该恢复终端模式,然后在 return 上恢复原始模式。这些是用 reset_shell_mode 和 reset_prog_mode 完成的。