خرید بک لینک

Vote count: 0

I've built a microshell that takes commands from standard input (up to 256 characters). Parses the input from user and organizes it into a "Command" structure. the structure is as follows:

struct Command {
    vector<string> args;    //program name and all parameters
    string inFn;        //what comes after "<"; redirect std input
    string outFn;       //what comes after ">";  redirect std output
    bool            //flags for what kind of redirection
        inRedir,    //"<"
        outRedirTrunc,  //">"
        outRedirAppend, //">>"
        outPipe;    //"|"
    int         //descriptors for input and output
        inFd,       //initially 1; set to 0 after | or ;
        outFd,      //initially 0; set to 1 after | or ;
        pid;
};

These Command structs are filled and pushed back onto a vector of these structures(the pus_back being called once a pipe | or a semicolon ; is seen). Then these vectors of commands are push onto another vector (in order to chain pipelines together.) I have done some testing and my parsing seems to build these structs and vectors fine.

Now I have built an execute function to actually use what I have built along with some system calls (i.e. execvp, dup2...) to actually carry out user commands. The problem is the function retus nothing. It compiles fine. So my error must be logical but I am unsure what is the issue (I am new to all these low level system calls so I suspect my issue lies there)

Here is my execute function:

typedef std::vector<char> CharArray;
typedef std::vector<CharArray> ArgumentVector;


void executeChain(void){
    exte vector< vector<Command> > pipelineChains; //vector containing vector of "command structs"

    int     rc,
        status,         //wait staus
        pfd[2];         //pipe


//loop to set up pipes
    for (int i=0; i < pipelineChains.size(); i++){
        int ivSz = pipelineChains[i].size();    //size of each cmdsV vector w/i pipelineChains
        for (int j = 0; j < ivSz-1; j++){

            //open pipe
            if (pipe(pfd) == -1) {  
                printf("ERROR opening pipen");
                exit(1);
                }

            //set descriptors to pipe
            pipelineChains[i][j].outFd = pfd[1];
            pipelineChains[i][j+1].inFd = pfd[0];
        }
    }

//loop to launch commands
    for (int i=0; i < pipelineChains.size(); i++){
        int ivSz = pipelineChains[i].size();
        for (int j = 0; j < ivSz; j++){
        Command *c = &pipelineChains[i][j];
        if (c->pid = fork()){

            //inRedir "<"
            if (c->inRedir) 
                c->inFd = open((c->inFn).c_str(), O_RDONLY);

            //outRedirTrunc ">"
            if (c->outRedirTrunc) 
                c->outFd = open((c->outFn).c_str(),O_CREAT|O_TRUNC|O_WRONLY,0666);

            //outRedirAppend ">>"
            if (c->outRedirAppend)  
                c->outFd = open((c->outFn).c_str(),O_CREAT|O_APPEND|O_WRONLY,0666);

            if (c->inFd != 0){
                dup2(c->inFd,0);
                close(c->inFd);
            }

            if (c->outFd != 1){
                dup2(c->outFd,1);
                close(c->inFd);
            }


            //tu argument to cstrings
            ArgumentVector argVec;
            std::vector<char *>argV;
            for (int index = 0;  index < c->args.size();  ++index){
               argVec.push_back(CharArray(c->args[index].begin(), c->args[index].end()));
               argV.push_back(argVec.back().data());
            }
            argV.push_back(NULL);

            rc = execvp(argV[0], argV.data());  
            if (rc == -1){
                perror("ERROR exec");
                exit (1);
            }else
                exit (0);

        }


        }
    }

//loop for parent to wait for children
    for (int i=0; i < pipelineChains.size(); i++){
        int ivSz = pipelineChains[i].size();
        for (int j = 0; j < ivSz; j++){
        Command *c = &pipelineChains[i][j];
        waitpid(c->pid, &status, 0);

        if(c->inFd != 0){
            close(c->inFd);
        }
        if(c->outFd != 1){
            close(c->outFd);
        }
        }

    }


}

asked 21 secs ago

برچسب: نویسنده: استخدام کار تاريخ: يکشنبه 29 فروردين 1395 ساعت: 7:48

صفحه بندی