0

我有一个执行 bash 命令的小 C 程序(一种 shell 程序)。

当用户发送这样的命令时:

  1. ls -l | grep .[c,h]$ | wc -l

  2. ls

  3. 回声 | ls

我决定使用链表数据结构,这是执行的功能

int execute(command *c) {
//create new procces and execute command 

int numOfPipe = getNumOfPipes(c);   // gets how many commands there is seperated by | (for 1 command with no pipes. getNumOfPipes returns 1
int fds[numOfPipe][2];

pid_t pids[numOfPipe];
int i;
int status;

// create pipes
for( i = 0; i < numOfPipe; i++ ) {
    pipe(fds[i]);
}

i=0;

// p set to be the head of the list (first command)
Pipe* p = c->pl->head;  

// running throw all nodes
while(i<numOfPipe) {

    if ((pids[i] = fork()) < 0) {
            perror("fork");
            abort();
    } else if (pids[i] == 0) {
        // children processes

        // there is more then one command
        if (numOfPipe > 1) {    
            if (p==c->pl->head) // first node (will be first command)
                dup2(fds[i][1],STDOUT_FILENO);
            else if(p->next!=NULL) {    // not first / last command
                dup2(fds[i-1][0],STDIN_FILENO);
                dup2(fds[i][1],STDOUT_FILENO);

            } else if(p->next==NULL)    // last command
                dup2(fds[i-1][0],STDIN_FILENO);


            // closing all pipes
            int j;
            for( j = 0; j < numOfPipe; j++ ){
                close(fds[j][0]);
                close(fds[j][1]);
            }
        }

        // execute command
        execvp(p->params[0],p->params);

        // on execute error will print this messege and returns COMMAND_ERROR
        fprintf(stderr, "%s: command not found\n",p->params[0]);    
        destroyShell(c);
        free(c);
        exit(COMMAND_ERROR);    
    }

    // parent closing all end of pipes
    int j;
    for( j = 0; j < numOfPipe && numOfPipe > 1; j++ ){
        close(fds[j][0]);
        close(fds[j][1]);
    }

    // waiting each process
    while(wait(&status)!=pids[i]);

    // if the command is valid
    if (WIFEXITED(status) && WEXITSTATUS(status)==COMMAND_ERROR) {
        destroyShell(c);
        return WEXITSTATUS(status);
    }

    p=p->next;
    i++;


}

// closing all end of pipes (necessary here????)
int j;
for (j = 0; j < numOfPipe; j++) {
    close(fds[j][0]);
    close(fds[j][1]);
}

// returns all command's return code
if (WIFEXITED(status))
    return WEXITSTATUS(status);

return 0;
}

到目前为止,这段代码以不可预知的方式工作,对于单个命令它的工作,对于薮猫有时它工作,有时不......在这段代码上花了很多时间,谷歌根本没有帮助!

几点注意事项:

  1. 当用户发送命令时,例如:

回声|ls|123

返回码将是 COMMAND_ERROR 宏定义,并且应该是这样的错误信息:

123:找不到命令

但不知何故,我的代码会执行 'echo | ls' 并显示前 2 个命令的输出,之后将显示 '123' 命令的错误消息。同样对于一些错误的命令 - 它应该只显示它​​找到的第一个错误消息,并且它不应该继续执行下一个命令......

对于这个命令:

ls -l | grep .[c,h]$ | wc -l

它似乎在无限循环中放养......

  1. 命令 *c 保存链表,链表中的每个节点将是一个命令,每个节点将保留数组调用参数,数组调用参数保留命令的所有参数。示例:

回声 1234 | ls /家

 ___________                 ___________
|           |               |           |
| COMMAND 1 |      --->     | COMMAND 2 |
|___________|               |___________|
      |                           |
      |                           |
    ______                     _______
   |      |                   |       |
   | ECHO |                   |  LS   |
   |______|                   |_______|
   |      |                   |       |
   | 1234 |                   | /HOME |
   |______|                   |_______|

我希望我很清楚并很好地解释了我的问题。非常感谢!

4

0 回答 0