我正在创建一个shell,它将模拟Linux中shell的行为,就像执行ls、mkdir、find等命令一样,现在我使用termios来监听arrow key press和Enter key press,如果用户按下向上箭头键,则向用户显示先前执行的命令。但在执行完我的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/