// Executes a <pipeline> rooted with cmd and returns the status of the last // command executed. int processPipeline(CMD* cmd) { assert(cmd); assert(ISPIPE(cmd->type) || cmd->type == SIMPLE || cmd->type == SUBCMD); if(ISPIPE(cmd->type)) { int pipeStatus = execPipe(cmd); updateStatusVar(pipeStatus); return pipeStatus; } else { return processStage(cmd); } }
void execOperatorCommand(command_t c) { command_t left = c->u.command[0]; command_t right = c->u.command[1]; switch(c->type) { case AND_COMMAND: execute_command(left, 0); c->status = left->status; if(left->status == 0) // if left command successful, execute right command and save status of right command { execute_command(right, 0); c->status = right->status; } else // dont execute right command { c->status = left->status; } break; case OR_COMMAND: execute_command(left, 0); c->status = left->status; if(left->status != 0) // if left command is not successful, execute right command and save status of right command { execute_command(right, 0); c->status = right->status; } else { c->status = left->status; } break; case PIPE_COMMAND: execPipe(c); break; case SEQUENCE_COMMAND: execute_command(left, 0); execute_command(right, 0); break; default: fprintf(stderr, "ERROR: INVALID COMMAND"); //printCommType(c->type); } }
main(int argc,char *argv[]) { int countline=0; if(argc==2) { //countline=countLine(argv[1]); countline=execPipe(argv[1]); if(countline!=-1) printf("Sono state contate %d linee.\n",countline); } else { usage(); return; } //execlp("find","find","-name","*.c",NULL); }
int main(int argc, char **argv) { int status; do { /* Start implementantion. It should stop when status is 0 (failure). */ /* Set prompt as the '$' character. */ printf("$"); int inputSize = INPUT_SIZE; /* Set the buffer size of input. */ int index = 0; /* Set the index of the input buffer. Initialise it to 0.*/ char *programName = malloc(sizeof(char) * inputSize); /* Allocate an array named programName of inputSize. This will contain the input from the user. */ int character; /* Error checking. If the programName array is NULL then the program should terminate showing the appropriate message to the user. */ if (programName == NULL) { fprintf(stderr, "programName allocation error!!\n"); status = 0; } while(1) { character = getchar(); /* Read characters from the user input. */ /* Check character. */ if (character == EOF || character == '\n') { programName[index] = '\0'; break; } else { programName[index] = character; } /* Set index of the programName array to the next read position. */ index += 1; } // Here the programName variable contains the name of the program // that the user wants to execute. // char delim[2] = "|"; if (programName[0] != '\0') { if (strstr(programName, "|") != NULL) { status = execPipe(programName); } else { status = executeCommand(programName); } } printf("Status:%d\n", status); }while(status); // Perform any shutdown/cleanup. return EXIT_SUCCESS; }
int main(int argc, char **argv) { int status; do { /* Start implementantion. It should stop when status is 0 (failure). */ /* Set prompt as the '$' character. */ printf("$"); int inputSize = INPUT_SIZE; /* Set the buffer size of input. */ int index = 0; /* Set the index of the input buffer. Initialise it to 0.*/ int character; char *programName = malloc(sizeof(char) * inputSize); /* Allocate an array named programName of inputSize. This will contain the input from the user. */ while(1) { character = getchar(); /* Read characters from the user input. */ /* Check character. */ if (character == EOF || character == '\n') { programName[index] = '\0'; break; } else { programName[index] = character; } /* Set index of the programName array to the next read position. */ index += 1; } // Here the programName contains // the command and its arguments that will be executed. if (programName[0] != '\0') { // Check for pipes. if (strstr(programName, "|") != NULL) { // We have to tokenize the programName using the | character // in order to get the input commands to execute the pipe. int i; int count = 0; for (i = 0; programName[i] != '\0'; i++) { if (programName[i] == '|') count++; } // In this shell only two pipes can be executed. if (count < 2) { status = execPipe(programName); } else { status = 1; } } else { status = executeCommand(programName); } } // Free allocated memory. free(programName); }while(status); // Perform any shutdown/cleanup. return EXIT_SUCCESS; }