//Entry point of the P2- microshell implementation int main(int argc, char *argv[]) { Pipe p; ctrlCFlag = 1; signalHandling(); ushrcProcessing(); char *hostName = (char *)malloc(1000); gethostname(hostName,1024); while(1){ fflush(stdin); if(isatty(fileno(stdin)) && ctrlCFlag == 1){ printf("%s%%", hostName); fflush(stdout); } ctrlCFlag = 1; p = parse(); if(p == NULL) { continue; } if(p!=NULL && strcmp(p->head->args[0], "end")) { prPipe(p); } else if(isatty(fileno(stdin)) && !strcmp(p->head->args[0], "end")) { exit(0); } else if(!isatty(fileno(stdin)) && !strcmp(p->head->args[0], "end")){ break; } freePipe(p); } }
int main(int argc, char *argv[]) { Pipe p; char host[25]; gethostname(host, 25); char* home=malloc(100); int fdin; backupIO(); home = strdup(getenv("HOME")); strcat(home, "/.ushrc"); if((fdin=open(home, O_RDONLY))==-1) perror("OPEN USHRC"); else { dup2(fdin, 0); close(fdin); printf("%s%% ", host); fflush(stdout); p = parse(); currPipe = 0; prPipe(p); //freePipe(p); } while ( 1 ) { restoreIO(); flushall(); printf("%s%% ", host); fflush(stdout); p = parse(); currPipe = 0; prPipe(p); //freePipe(p); } }
//ushrc execution handling void ushrcProcessing() { char *rcpath = (char *)malloc(1000); strcpy(rcpath, getenv("HOME")); rcpath = strcat(rcpath, "/.ushrc"); if((rcFile = open(rcpath,O_RDONLY)) != -1) { oldRCStdIn = dup(fileno(stdin)); if(oldRCStdIn == -1) { printf("dup rcfile failed"); return; } if(dup2(rcFile,fileno(stdin))==-1) { printf("dup rcfile failed"); return; } } else { printf("No .ushrc file found or file permission denied\n"); return; } Pipe p; char *host = (char *)malloc(1000); gethostname(host,1024); while(1){ fflush(stdin); if(isatty(fileno(stdin))){ printf("%s%%", host); } p = parse(); if(p == NULL) { continue; } if(p!=NULL && strcmp(p->head->args[0], "end")) { prPipe(p); } else if(isatty(fileno(stdin)) && !strcmp(p->head->args[0], "end")) { exit(0); } else if(!isatty(fileno(stdin)) && !strcmp(p->head->args[0], "end")){ break; } freePipe(p); } dup2(oldRCStdIn,fileno(stdin)); close(rcFile); close(oldRCStdIn); }
int main(int argc, char *argv[]) { Pipe p,q; size_t len; char *host_n=malloc(100),*host_p; int fil; gethostname(host1,20); host_n=getenv("HOME"); strcat(host_n,"/.ushrc"); if((fil=open(host_n, O_RDONLY))==-1) perror("OPEN USHRC"); int oldStdIn=dup(STDIN_FILENO); dup2(fil,STDIN_FILENO); close(fil); lockA=0; lockB=1; if(pipe(pipeA)==-1)perror("PIPE:"); if(pipe(pipeB)==-1)perror("PIPE:"); p = parse(); prPipe(p); freePipe(p); dup2(oldStdIn,STDIN_FILENO); //restore STDIN close(oldStdIn); //don't need old STDIN handle while ( 1 ) { lockA=0; lockB=1; if(pipe(pipeA)==-1)perror("PIPE:"); if(pipe(pipeB)==-1)perror("PIPE:"); printf("%s%% ", host1); fflush(stdout); //fflush(stdin); p = parse(); prPipe(p); freePipe(p); } }
int main(int argc, char *argv[]) { Pipe p; char *host = "armadillo"; while ( 1 ) { printf("%s%% ", host); p = parse(); prPipe(p); freePipe(p); } }
//Command handler starting point after main static void prPipe(Pipe p) { Cmd c; if (p == NULL) return; headCommand = 1; for ( c = p->head; c != NULL; c = c->next ) { checkExitCommands(c); shellHandler(c); } i = 0; pipeHandle = 0; prPipe(p->next); }
static void prPipe(Pipe p) { int i = 0; Cmd c; if ( p == NULL ) return; printf("Begin pipe%s\n", p->type == Pout ? "" : " Error"); for ( c = p->head; c != NULL; c = c->next ) { printf(" Cmd #%d: ", ++i); prCmd(c); } printf("End pipe\n"); prPipe(p->next); }
static void prPipe(Pipe p) { int i = 0; Cmd c; if ( p == NULL ) return; //printf("Begin pipe%s\n", p->type == Pout ? "" : " Error"); for ( c = p->head; c != NULL; c = c->next ) { //printf(" Cmd #%d: ", ++i); Execute_cmd(c); //prCmd(c); } // printf("End pipe\n"); lockA=0; lockB=1; if(pipe(pipeA)==-1)perror("PIPE:"); if(pipe(pipeB)==-1)perror("PIPE:"); prPipe(p->next); }
static void prPipe(Pipe p) { int i = 0; Cmd c; int inPipeId, outPipeId; currPipe = 0; outPipeId = 0; inPipeId = 0; if ( p == NULL ) return; //printf("Begin pipe%s\n", p->type == Pout ? "" : " Error"); for ( c = p->head; c != NULL; c = c->next ) { //printf(" Cmd #%d: ", ++i); if(c->in == Tpipe || c->in == TpipeErr) { inPipeId = currPipe-1; //printf("Setting In Pipe %d for command %s", inPipeId, c->args[0]); } if(c->out == Tpipe || c->out == TpipeErr) { outPipeId = currPipe; pipe(pipes+(2*currPipe)); currPipe++; //printf("Setting out Pipe %d for command %s", outPipeId, c->args[0]); } //fprintf(stdout,"\nInitiating command: %s\n", c->args[0]); prCmd(c, inPipeId, outPipeId); //fprintf(stdout,"\nReturning from command: %s\n", c->args[0]); } //printf("End pipe\n"); prPipe(p->next); }