static void prCmd(Cmd c, int inPipeId, int outPipeId) { int i; if ( c ) { /* printf("%s%s ", c->exec == Tamp ? "BG " : "", c->args[0]); if ( c->in == Tin ) printf("<(%s) ", c->infile); if ( c->out != Tnil ) switch ( c->out ) { case Tout: printf(">(%s) ", c->outfile); break; case Tapp: printf(">>(%s) ", c->outfile); break; case ToutErr: printf(">&(%s) ", c->outfile); break; case TappErr: printf(">>&(%s) ", c->outfile); break; case Tpipe: printf("| "); break; case TpipeErr: printf("|& "); break; default: fprintf(stderr, "Shouldn't get here\n"); exit(-1); } if ( c->nargs > 1 ) { printf("["); for ( i = 1; c->args[i] != NULL; i++ ) printf("%d:%s,", i, c->args[i]); printf("\b]"); } //printf("\nThe C-In is: %d", c->in); //printf("\nThe C-Out is: %d", c->out); putchar('\n'); */ // this driver understands one command if(!isBuiltinCommand(c->args[0])) { execcommand(c, inPipeId, outPipeId); } else if(strcmp(c->args[0], "echo") == 0) { //printf("Executing echo: in and out pipes are: %d %d", inPipeId, outPipeId ); exececho(c, inPipeId, outPipeId); } else if(strcmp(c->args[0], "pwd") == 0) { //printf("Executing echo: in and out pipes are: %d %d", inPipeId, outPipeId ); execpwd(c, inPipeId, outPipeId); } else if(strcmp(c->args[0], "setenv") == 0) { //printf("Executing getenv: in and out pipes are: %d %d", inPipeId, outPipeId ); execsetenv(c, inPipeId, outPipeId); } else if(strcmp(c->args[0], "unsetenv") == 0) { //printf("Executing getenv: in and out pipes are: %d %d", inPipeId, outPipeId ); execunsetenv(c, inPipeId, outPipeId); } else if(strcmp(c->args[0], "nice") == 0) { //printf("Executing getenv: in and out pipes are: %d %d", inPipeId, outPipeId ); execnice(c, inPipeId, outPipeId); } else if(strcmp(c->args[0], "cd") == 0) { //printf("Executing getenv: in and out pipes are: %d %d", inPipeId, outPipeId ); execcd(c, inPipeId, outPipeId); } else if(strcmp(c->args[0], "where") == 0) { //printf("Executing getenv: in and out pipes are: %d %d", inPipeId, outPipeId ); execwhere(c, inPipeId, outPipeId); } else if ( !strcmp(c->args[0], "logout") ) exit(0); else if ( !strcmp(c->args[0], "end") ) exit(0); //putchar('\n'); } }
int main(int argc, char *argv[]) { //Initialize the shell init_shell(); int numofpipes[MAXCMD]; //Initialize jobs array init_jobsarray(); //Holds the command char wholecommand[MAXNUM]; char dupwholecommand[MAXNUM]; char *cmdarr[MAXCMD][MAXOP]; //The rows hold commands separated by ; //while the columns hold the command and its options separated by " " or \t char *pipecmdarr[MAXCMD][MAXCMD][MAXOP]; char maincmd[MAXNUM]; //For prompt char username[MAXNUM],sysname[MAXNUM],cwdir[MAXNUM]; char homedir[MAXNUM]; getcwd(homedir,MAXNUM); char duphomedir[MAXNUM]; duphomedir[0] = '\0'; strcat(duphomedir,homedir); //For tokenizing char *str1 = NULL; char *token = NULL; //Counters int i; //Initializing background process array for(i=0; i<(MAXNUM*MAXOP); i++) { bgrounds[i].used = 0; } //Initializing ends //Initialize to NULL initarray(cmdarr); char *hdir[MAXNUM]; //Tokenize home directory tokenize(str1,hdir,token,duphomedir); //Tokenize home directory done while(1) { if(signal(SIGINT,signal_handler)==SIG_ERR) perror("Signal not caught!!"); if(signal(SIGCHLD,signal_handler)==SIG_ERR) perror("Signal not caught!!"); for(i=0; i<MAXCMD; i++) numofpipes[i] = 0; rectifyjobsarray(); //Printing message on background process completion for(i=0; i<(MAXNUM*MAXOP); i++) { if(bgrounds[i].used == 0) continue; bgrounds[i].used = 0; if(bgrounds[i].status == 1) { //printf("Process %s with pid %d terminated normally\n",bgrounds[i].name,bgrounds[i].pid); } else { //printf("Process %s with pid %d terminated with status %d\n",bgrounds[i].name,bgrounds[i].pid,bgrounds[i].status); } } //Renitializing background process array for(i=0; i<(MAXNUM*MAXOP); i++) { bgrounds[i].used = 0; } //Renitializing ends //Initialize to NULL initarray(cmdarr); initpipecmd(pipecmdarr); //For prompt username[0] = '\0'; cwdir[0] = '\0'; strcat(username,getlogin()); gethostname(sysname,MAXNUM); getcwd(cwdir,MAXNUM); char dupcwdir[MAXNUM]; dupcwdir[0] = '\0'; strcat(dupcwdir,cwdir); directorysettings(cwdir,homedir,dupcwdir,hdir); //Prompt settings done //Take commands printf("<%s@%s:%s>",username,sysname,cwdir); wholecommand[0] = '\0'; dupwholecommand[0] = '\0'; readLine(wholecommand); wholecommand[strlen(wholecommand)-1]='\0'; strcpy(dupwholecommand,wholecommand); //Done taking commands //Tokenize maintokenize(cmdarr,dupwholecommand); //tokentest(cmdarr); pipetokenize(cmdarr,pipecmdarr,numofpipes); //pipetokenizetest(cmdarr,pipecmdarr,numofpipes); //exit(EXIT_SUCCESS); //continue; //Tokenize done //Executing each ; separated command for(i = 0; i< MAXCMD; i++) { if(cmdarr[i][0] == NULL) continue; maincmd[0] = '\0'; strcat(maincmd,cmdarr[i][0]); strcpy(infile,"\0"); strcpy(outfile,"\0"); if(numofpipes[i] != 0) { //Execute piped commands pipesexecute(cmdarr, pipecmdarr, numofpipes, i); continue; } if((strcmp(maincmd,"echo") == 0) || (strcmp(maincmd,"pwd") == 0) || (strcmp(maincmd,"cd") == 0)\ || (strcmp(maincmd,"clear") == 0) || (strcmp(maincmd,"quit") == 0) || (strcmp(maincmd,"pinfo") == 0) || strcmp(maincmd,"jobs") == 0 || strcmp(maincmd,"overkill") == 0 || strcmp(maincmd,"kjob") == 0 || strcmp(maincmd,"fg") == 0 ) { int dupstdout = dup(1); int dupstdin = dup(0); if(checkinfile(cmdarr,i) == 1) { if(strcmp(infile,"\0") != 0) { int in=open(infile,O_RDONLY); dup2(in,0); close(in); } } int outretval = checkoutfile(cmdarr,i); if(outretval == 1 || outretval == 2) { if(strcmp(outfile,"\0") != 0) { if(outretval == 1) { int out=open(outfile,O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR); dup2(out,1); close(out); } else { if(fopen(outfile,"r") == NULL) { int out=open(outfile,O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR); dup2(out,1); close(out); } else { int out=open(outfile,O_WRONLY | O_APPEND, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR); dup2(out,1); close(out); } } } } //echo execution if(strcmp (maincmd,"echo") == 0) { exececho(wholecommand,cmdarr,i); } //echo done //pwd execution if(strcmp (maincmd,"pwd") == 0) { execpwd(); } //pwd done //chdir execution if(strcmp (maincmd,"cd") == 0) { execcd(cmdarr,homedir,i); } //chdir done //clear execution if(strcmp (maincmd,"clear") == 0) { printf("\e[1;1H\e[2J"); } //clear done //terminal quit if(strcmp (maincmd,"quit") == 0) { exit(EXIT_SUCCESS); } //terminal quit done //jobs if(strcmp (maincmd,"jobs") == 0) { execjobs(); } //jobs done //overkill if(strcmp (maincmd,"overkill") == 0) { execoverkill(); } //overkill done //kjob if(strcmp (maincmd,"kjob") == 0) { execkjob(cmdarr,i); } //kjob done //fg if(strcmp (maincmd,"fg") == 0) { execfg(cmdarr,i); } //fg done //pinfo if(strcmp (maincmd,"pinfo") == 0) { pid_t proid; int flag = 0; if(cmdarr[i][1] == NULL) //pinfo of shell proid = getpid(); else { //pinfo of given pid char temps[MAXNUM]; strcpy(temps,cmdarr[i][1]); proid = atoi(temps); if(kill(proid,0) == (-1)) { printf("No such process exists\n"); continue; } flag = 1; } getpinfo(proid, flag, homedir); } //pinfo done dup2(dupstdin,0); dup2(dupstdout,1); } else {