c - 如何使用termios将控制权返回给C中的终端

标签 c linux termios

我正在创建一个 shell,它将模仿 Linux 中 shell 的行为,例如执行 ls、mkdir、find 等命令,现在我已经使用了 termios监听箭头键按下和 Enter 键按下,如果用户按下向上箭头键,则向用户显示先前执行的命令。但是在执行我的 shell 程序后,在输入第一个命令后,例如:ls 命令的输出将被显示,但之后我无法执行另一个命令,因为在终端中键入并按 Enter 只是打印文本回到新行并且不执行它。

我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <term.h>
#include <curses.h>
#include <unistd.h>

static struct termios initial_settings, new_settings;
static int peek_character = -1;
void init_keyboard();
void close_keyboard();
int kbhit();
int readch();


int main() {

    int ch;
    char str[1000][200];
    init_keyboard();
    int i = 0;
    int j = 0;

    while(ch != 'q') {

    if(kbhit()) {

        ch = readch();

        if (ch == 10) {
            system(str[i]);
            i++;
        } else {
            str[i][j] = ch;

            j++;
        }

    }

}
    close_keyboard();
    exit(0);
}

void init_keyboard() {
    tcgetattr(0, &initial_settings);
    new_settings = initial_settings;
    // new_settings.c_iflag &= ~BRKINT;
    // new_settings.c_iflag &= ICRNL;
    new_settings.c_lflag &= ~ICANON;
    new_settings.c_lflag &= ECHO;
    new_settings.c_lflag &= ~ISIG;
    new_settings.c_cc[VMIN] = 1;
    new_settings.c_cc[VMIN] = 0;
    tcsetattr(0, TCSANOW, &new_settings);

}

void close_keyboard() {

    tcsetattr(0, TCSANOW, &initial_settings);

}

int kbhit() {
    char ch;
    int nread;

    if (peek_character != -1) {
        return 1;
    }

    new_settings.c_cc[VMIN] = 0;
    tcsetattr(0, TCSANOW, &new_settings);
    nread = read(0, &ch,1);
    new_settings.c_cc[VMIN]=1;
    tcsetattr(0, TCSANOW, &new_settings);

    if (nread == 1) {
        peek_character = ch;
        return 1;
    }
    return 0;
}


int readch() {
    char ch;

    if (peek_character != -1) {
        ch = peek_character;
        peek_character = -1;
        return ch;
    }

    read(0, &ch,1);
    return ch;
}

最佳答案

您需要 fork() 来创建新进程,因为 system() 执行您的命令并退出 ... 试试这个代码:

int main() {

int ch;
char str[1000][200];
init_keyboard();
int i = 0;
int j = 0;
pid_t father;
father = 0;

while(ch != 'q') {

if(kbhit()) {

    ch = readch();

    father = fork();
    if (father == 0)
    {
        if (ch == 10) {
          system(str[i]);
          i++;
        } else {
        str[i][j] = ch;
        j++;
        }
    }

}

}

关于c - 如何使用termios将控制权返回给C中的终端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58170913/

相关文章:

c - 关于C语言中的qsort(),**buf和buf[][]的区别

java - 如何从 JNI 中的 GetDirectBufferAddress 调用多种数据类型?

mingw - 从哪里获得 termios.h

c - 使用 C 从路由器转储闪存

c - 指针的大小,C中指向指针的指针

c++ - y=x++ 中有多少原始步骤?

linux - 为什么 gethostbyaddr 不适用于 www.google.com

c - 快速比较 IP 地址的最智能方法?

linux - 我需要找到如何区分 usb 已连接和读卡器也已连接到 linux 中的目标

objective-c - 为什么在与 USB 转串行适配器通信时此串行通信代码会挂起?