我当前的项目有一个奇怪的问题。使用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;
}
答案 0 :(得分:0)
如果您使用ncurses初始化了屏幕,则该屏幕处于原始模式,这使换行符不再映射到回车/换行。
如果要运行子外壳程序,则应恢复终端模式,然后在返回时恢复原始模式。这些是通过reset_shell_mode和reset_prog_mode完成的。