我当前的项目遇到了一个奇怪的问题。使用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初始化屏幕,那就是原始模式,其中(除其他外)使换行不再映射到回车/换行。
如果您要运行子shell,那么您应该恢复终端模式,然后在返回时恢复原始模式。这些都是使用reset_shell_mode和reset_prog_mode完成的。