我想写一个每行有多个命令的comamnd行解释器。每行有多个命令的命令行解释器
我在C中编写了一个程序,它每行有1个comamnd,但是如果我输入更多的命令不工作,comamnds会像下面这样输入:ls -l; pwd;猫文件; LS。
首先我解析指定参数时,我把它们放到数组和我有这个功能:
pid_t pid;
pid = fork();
switch(pid) {
case -1:
printf("DEBUG:Fork Failure\n");
exit(-1);
case 0:
execvp(cmd[j], cmd);
if(execvp(cmd[j], cmd) == -1) {
printf("Command Not Found\n");
exit(0);
}
default:
wait(NULL);
printf("DEBUG:Child Finished\n");
}
我的解析器:
printf("shell> ");
fgets (input, MAX_SIZE, stdin);
if ((strlen(input)>0) && (input[strlen (input) - 1] == '\n')) {
input[strlen (input) - 1] = '\0';
}
printf("INPUT: %s\n", input);
cnd = strtok(input, " ;");
int i = 0;
while(cnd != NULL) {
cmd[i] = cnd;
i++;
cnd = strtok(NULL, ";");
我认为我必须使用管道来解决我的问题,但如何? 任何想法?
对不起坏英语
如果一个命令正确解析,但多个命令解析不正确,解析器出现问题。你不显示你的解析器... – StoryTeller