execvp() 输出为 ncurses 创建缩进 [英] execvp() output creates indent for ncurses
问题描述
我当前的项目有一个奇怪的问题.使用 ncurses 我正在制作一个基于 lsh 的外壳,在我引入 ncurses 之前,它就像人们所期望的那样工作,只需编写 execvp 的输出.然而,现在输出的长度在我的提示之前缩进,这实际上也将 X 坐标移到了一边(所以缩进似乎不是行的一部分).
I'm having a weird problem with my current project. Using ncurses I'm making a shell based on lsh, before I introduced ncurses it was working as one would expect with just writing the output from execvp. Now however, the length of the output makes an indent before my prompt which actually also shifts the X coordination with it to the side (so the indent doesn't seem to be part of the line).
我认为这是由于在没有 ncurses(或类似的东西)的情况下分叉到子进程.
I'm thinking it is due to forking into a child process without ncurses (or something along those lines).
您可以在此处查看完整代码,但这这是运行 execvp 的部分:
You can see the full code here but this here is the part where execvp is run:
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 初始化了屏幕,那就是在 raw 模式,这(除其他外)使得换行不再映射到回车/换行.
If you've initialized the screen with ncurses, that's in raw mode, which (among other things) makes newline no longer map to carriage-return/line-feed.
如果您要运行子shell,那么在途中您应该恢复终端模式,然后在返回时恢复原始模式.这些是通过 reset_shell_mode 和 reset_prog_mode 完成的.
If you're going to run a subshell, then on the way you should restore the terminal modes, and then reinstate the raw mode on return. Those are done with reset_shell_mode and reset_prog_mode.
这篇关于execvp() 输出为 ncurses 创建缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!