//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); } }
void freePipe(Pipe p) { if ( p == NULL ) return; freeCmd(p->head); freePipe(p->next); free(p); } /*---------- End of freePipe ----------------------------------------------*/
//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); } }