#include #include #include #define MAX_INPUT_LENGTH 80 #define MAX_TOKENS 20 #define DEFAULT_PROMPT "osh> " #define NULL_CHAR '\0' typedef char * String; typedef char ** StringArray; /* Enumerated list for valid commands. Add to list as language grows */ enum Commands { INVALID, EXIT, FILES }; /* Returns numeric code corresponding to string containing command name */ enum Commands getCommand(String comName) { if (!strcmp("exit",comName)) { return EXIT; } else if (!strcmp("files",comName)) { return FILES; } else /* expand as command language grows */ return INVALID; } /* Separate input into its component words - assume space(s) as delimiter * * First param is String (char array) containing input. * Second param is array of String (array of char arrays). * This function will extract each word from input, copying it into next * element of array of Strings, starting at element 0. * Since both parameters are arrays, they alias the corresponding argument * and any changes made here also affect the caller's argument. * * Return value is word count. */ int inputParse(String line, StringArray words) { int count = 0; int i=0; int position=0; int done = 0; while (!done) { words[count] = (String) malloc((strlen(line)+1) * sizeof(char)); while (i < strlen(line) && line[i] != ' ' && line[i] != '\t') { words[count][position] = line[i]; position++; i++; } words[count][position] = NULL_CHAR; count++; position=0; while (i < strlen(line) && (line[i] == ' ' || line[i] == '\t')) { i++; } if (i >= strlen(line)) { done = 1; } } return count; } /* handy utility to deallocate space allocated in inputParse */ void freeTheTokens(int count, StringArray theTokens) { int i; for (i=0; i 0) { switch (getCommand(tokens[0])) { /* first word contains command */ case EXIT : finished = 1; break; case FILES : files(numTokens, tokens); /* do files command */ break; case INVALID: printf("Invalid command: %s\n", tokens[0]); break; } freeTheTokens(numTokens, tokens); } } return 0; }