static char rcsid[]="$Id: run.C,v 1.9 1995/09/28 03:19:18 wilmesj Exp $"; /* $Log: run.C,v $ * Revision 1.9 1995/09/28 03:19:18 wilmesj * Major logic rewrite (thanks tim) and recommenting, fixes, pipes now work properly, etc. * * Revision 1.8 1995/09/28 02:06:28 wilmesj * Comments galore. * * Revision 1.7 1995/09/28 01:24:24 wilmesj * Total rewrite of the running code.. Integrated pipe and redirection support. * Unfortunately, pipes are still broken. * * Revision 1.6 1995/09/27 17:31:36 wilmesj * Redirection and Piping parse routines. More commenting. * * Revision 1.5 1995/09/26 17:25:08 wilmesj * Extreme commenting, cleanup. * * Revision 1.4 1995/09/25 00:11:45 wilmesj * Merged bg and run into one function with a flag. * * Revision 1.3 1995/09/12 23:12:36 wilmesj * Switched kill sig 0 to wait with W_NOHANG for process checks * * Revision 1.2 1995/09/12 21:44:35 josh * *** empty log message *** * * Revision 1.1 1995/09/10 17:25:06 josh * Initial revision * */ #include "shell.h" #include #include #include #include #include //::::::::::::::::::::::::::::::::: Numwords ::::::::::::::::::::::::::::::::// /* Function: numwords() * Purpose: Count the number of words in a string, where a word is defined * as a string of non-spaces. * Returns: integer number of words * Uses: str* (string.h) */ int numwords(char*& orig_string) { char* string=orig_string; int result=0; if (strlen(string)) { result=1; while (string) { string=strchr(string, ' '); if (string) { string++; result++; } } } return result; } //::::::::::::::::::::::::::::::: trim ::::::::::::::::::::::::::::::::::::::// /* Function: trim * Purpose: get rid of leading and trailing blanks, tabs (and LFs) from a * normal string * Returns: n/a * Uses: n/a * Note: This function is taken from the RPI SandBox library, found in * /campus/rpi/sandbox/1.2/distrib/str/str.c * It is very nice! */ void trim(char *s) { char *from=s, *to=s; /* skip leading blanks */ while(*from == ' ' || *from == '\t') from++; /* move chars till end of string */ while(*from) *(to++) = *(from++); /* scan back for a non-blank */ while(*(--to) == ' ' || *to == '\t' || *to == '\n'); /* drop a null right after it */ *(++to) = '\0'; } //:::::::::::::::::::::::::::::::: char2argv ::::::::::::::::::::::::::::::::// /* Function: char2argv() * Purpose: Convert a flat character string to an argv style string vector. * Returns: Pointer to char** vector. * Uses: str*, c++ style new. */ char** char2argv (char* string) { char** argv; int argc=numwords(string); argv=new char* [argc+1]; if (argc==1) { argv[0]=new char [strlen(string)]; strcpy(argv[0],string); argv[1]=NULL; } else { char* token=strtok(string," "); argv[0]=new char[strlen(token)]; strcpy(argv[0],string); argv[1]=NULL; for (int i=1;i',"|<>",commandline); gazouta=extractcmd('<',"|<>",commandline); while ( pipeline[numprocs]=extractcmd('|',"|<>",commandline) ) numprocs++; trim (commandline); numpipes=numprocs-1; pipeline[0]=commandline; // Allocate the FD array. int fd[numpipes][2]; // And Pipe them. for (int i=0;i Invalid file for reading\n", gazinta); exit(1); } close(0); dup(ifd); close(ifd); } // Output Redirection // if (gazinta && i == numprocs - 1) { if ((ofd=open(gazinta,O_CREAT | O_RDWR, 0644)) == -1) { printf("Error: <%s> Invalid file for writing\n", gazouta); exit(2); } close(1); dup(ofd); close(ofd); } // Stdin Piping // if (i != 0) // (Only if not the first one) { close(0); dup2(fd[inpipe][0],0); // Hook our input to the one before us. } // Stdout Piping // if (i != numprocs - 1) // (Only if not the last one) { close(1); dup2(fd[outpipe][1],1); // Hook our output into the output pipe. } // Close the leftover file descriptors // for (z=0;z