Пример #1
0
/* --------------------------------------------------
     This function processes the input.  It will check for special cases:


   -------------------------------------------------- */
void processInput(){

     int i;

     //Check for &.  If present, it should be run in the bg.  Flip BG flag, then flush that array element to NULL
     if(strcmp(CARGV[CARGC - 1], "&") == 0){
          BG = true;
          CARGV[CARGC - 1] = NULL;
          CARGC--;
          //printf("background = true\n");
     }
     //Check for <.  If present, we will need to change stdin to the input file.  Keep track of that in INPF
     for(i = 0; i < CARGC; ++i){
          if(strcmp(CARGV[i], "<") == 0){
               INP = true;                             //Flip flag
               INPF = malloc(sizeof(char) * 25);       //Make memory for INPF
               INPF = CARGV[i + 1];                    //Put the file name in INPF
               CARGV[i] = NULL;                        //Wipe these array locations since we have the info.
               CARGV[i+1] = NULL;
               CARGC = CARGC -2;                       //New number of arguments.

          }
     }
     //Check for >.  If present, we will need to change stdout to the output file.  Keep track of that in OUTPF
     for(i = 0; i < CARGC; ++i){
          if(strcmp(CARGV[i], ">") == 0){
               OUTP = true;                            //Flip flag
               OUTPF = malloc(sizeof(char) * 25);      //Make memory for OUTPF
               OUTPF = CARGV[i + 1];                   //Put the file name in OUTPF
               CARGV[i] = NULL;                        //Wipe these array locations since we have the info.
               CARGV[i+1] = NULL;
               CARGC = CARGC -2;                       //Lower the number of command arguments.
          }
     }
     //Check to see if the command that remains is one of the built in.
     if(checkBuiltIn() == 0){

          callNative();            //If its not, try calling a native command.
     }
     return;
}
Пример #2
0
int main(){
	/* The raw input is stored in a buffer. The locations of the first character
	 * of the tokens and the pipes is given by a pointer*/
	char input[INPUT_BUFFER];
	char* tokens[MAX_TOKENS];
	char* pipes[MAX_TOKENS];
	int numTokens;
	int numPipes;
	
	/* Assign names and function pointers to the global array above*/
	buildFunctionTable();

	while(1){

		/* See header for more info. This function prompts the user for input.
		 * a return of -1 means no non-whitespace input*/
		if(getInput(input)==-1){
			continue;
		}
		
		/* Break the input into tokens and pipes. Return an error code for
		 * different types of invalid input (or 0 on good input) */
		int valid = parseInput(input, tokens, pipes, &numTokens, &numPipes);
		if(valid<0){
			switch(valid){
				case LINE_ENDING_INVALID:
					fprintf(stderr, "Error: There was an issue with line endings\n");
					break;
				case MAX_ARGS_ERROR:
					fprintf(stderr, "Error: Too many arguments\n");
					break;
				case QUOTE_ERROR:
					fprintf(stderr, "Error: Unmatched Quotes\n");
					break;
				default:
					fprintf(stderr, "Error: Invalid Input\n");
					break;
			}
			continue;
		}
		
		/* If there's more than one command, check for built-in commands first
		 * and then set up to execute an external command */
		if(numTokens > 0){
			
			if(checkBuiltIn(tokens,numTokens)!=0){
				int pid;
				pid	= fork();
				
				if(pid==0){
					/* The child runs the command */
					runCommands(tokens,pipes,numTokens,numPipes);
					exit(0);
				}else if(pid == -1){
					perror("fork");
				}else{
					/* The parent gives the return message */
					waitAround(pid);
				}
			}
		}
	}
	
	return 0;
}