【问题标题】:execvp() output creates indent for ncursesexecvp() 输出为 ncurses 创建缩进
【发布时间】:2018-09-05 08:57:10
【问题描述】:

我当前的项目有一个奇怪的问题。使用 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 输出它

标签: c ncurses


【解决方案1】:

如果您已使用 ncurses 初始化屏幕,则处于 原始模式,这(除其他外)使换行不再映射到回车/换行。

如果你要运行一个子shell,那么你应该在途中恢复终端模式,然后在返回时恢复原始模式。这些是通过reset_shell_mode 和reset_prog_mode 完成的。

【讨论】:

  • 终于让它工作了,不知道你需要这样做,简单的修复被忽略了。非常感谢!
猜你喜欢
  • 2021-06-06
  • 1970-01-01
  • 2022-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多