在C shell中实现管道(Unix)

5 c unix shell pipe

基本上我使用标准POSIX命令创建了一个shell,我希望能够实现管道.现在它正确处理命令,并可以使用&进行后台处理.但我需要能够管道使用| 和>>.例如,像这样:cat file1 file2 >> file3 cat file1 file2 | 更多file1 | grep的东西

这是我目前的代码.我也想避免"系统"调用.我知道你需要使用dup2,但我编写代码的方式有点奇怪,所以我希望有人能告诉我在这段代码中实现管道是否可行?谢谢!我知道dup2被使用了,但也是我的def.困惑于如何实现>>如同如|

#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <string>
#include <iostream>
#include <stdlib.h> 
#include <stdio.h>

using namespace std;


void Execute(char* command[],bool BG)
{
//Int Status is Used Purely for the waitpid, fork() is set up like normal.
    int status;
    pid_t pid = fork();


    switch(pid)
    {
        case  0:
            execvp(command[0], command);

            if(execvp(command[0], command) == -1)
            {
                cout << "Command Not Found" << endl;
                exit(0);
            }

          default:
            if(BG == 0)
            {
                    waitpid(pid, &status, 0);
//Debug             cout << "DEBUG:Child Finished" << endl;
            }


    }

}


bool ParseArg(char* prompt, char* command[], char Readin[],bool BG)
{

    fprintf(stderr, "myshell>");
        cin.getline(Readin,50);
    prompt = strtok(Readin, " ");
    int i = 0;

    while(prompt != NULL)
    {
        command[i] = prompt;
        if(strcmp(command[i], "&") == 0){
//Debug        cout << "& found";
        command[i] = NULL;
        return true;
    }
//Debug        cout << command[i] << " ";
        i++;
        prompt = strtok(NULL, " ");

    }
    return false;
}

void Clean(char* command[])
{
//Clean Array
        for(int a=0; a < 50; a++)
        {
             command[a] = NULL;
        }
}



int main()
{
   char* prompt;
   char* command[50];
   char Readin[50]; 
   bool BG = false;



   while(command[0] != NULL)
   {

        Clean(command);
       BG = ParseArg(prompt, command, Readin, BG);
       if(strcmp(command[0], "exit") == 0 || strcmp(command[0], "quit") == 0 )
       {
             break;
       }

    else
    {
            Execute(command,BG);
    }

   }

   return 1;

}
Run Code Online (Sandbox Code Playgroud)

Rom*_*aka 5

实际上,管道和重定向是不同的.要实现重定向(例如>>),您必须dup2确实使用.首先,使用适当的标志打开所需的文件(因为>>它们将是O_WRONLY|O_CREAT|O_APPEND).其次,使用dup2,使stdout(文件描述符1)成为这个新打开的fd的副本.最后,关闭新开的fd.

要创建管道,您需要一个pipe系统调用.阅读其手册页,它包含示例代码.然后,您还需要分别为一个进程和stdout为另一个进程dup2返回文件描述符pipe.


Swi*_*iss 5

您应该能够使用shell实现管道和输出重定向,但我注意到了一些事情:

  • 您将用于读取输入,解析和输出的代码混合在一起,您可能希望将此功能分开.
  • strtok作为shell命令的解析器不会很好.它适用于非常简单的命令,但您可能希望研究创建或找到更好的解析器.对于echo "hello world"当前的解析方法,命令会有问题.
  • 您可能希望创建一个用于保存已分析命令的简单结构.

这是一些让你入门的伪代码:

#define MAX_LINE 10000
#define MAX_COMMANDS 100
#define MAX_ARGS 100

// Struct to contain parsed input
struct command
{
    // Change these with IO redirection
    FILE *input; // Should default to STDIN
    FILE *output; // Should default to STDOUT

    int num_commands;
    int num_args[MAX_COMMANDS]; // Number of args for each command
    char* command_list[MAX_COMMANDS]; // Contains the programs to be run
    char* args_list[MAX_COMMANDS][MAX_ARGS]; // The args for each command
    boolean background_task;
    boolean append;
}

int main()
{
    char input[MAX_LINE];

    while (1)
    {
        struct command cmd;

        print_prompt();
        read_input(input);
        parse_input(input, &cmd);
        execute(&cmd);
    }
}
Run Code Online (Sandbox Code Playgroud)

祝这个项目好运!