Writing your parser

For the Shell assignment you'll first need to start by writing a command line parser. The parser's job will be to break up the individual parts of the command line from the shell's input.

Remember! Your shell doesn't have to execute anything yet! It only has to parse up the command line and print out the information.

Basic Psuedocode for the Parser


/**
 *  parse.c
 */

  void init_info(parseInfo *p) {
      initialize parseInfo struct
  }

  /* parse a single command */
  void parse_command(char * command, struct commandType *comm) {

  }

  /*   parse commandline for space separated commands */
  parseInfo *parse (char *cmdline) {
    foreach cmd in cmdline {
      if (cmd == command) {
        parse_command(cmd, type)
      }
    }
  }

  /* prints out parse struct */
  void print_info (parseInfo *info) {
     foreach type in parseInfo {
       print "type_name: type"
     }
  }  

  /*  free memory used in parseInfo */
  void free_info (parseInfo *info) {
     foreach memory_block in parseInfo
        free(memory_block)
  }

----------------------------------------------------------

/**
 *  shell.c
 */
 int main(int argc, char **argv) {
    while(1) {
       cmdLine = readline(printPrompt());

       info  = parse(cmdLine);

       print_info(info);
    }

    free_info(info);
 }

Required Features

  1. Take input in from command line. You'll need to read and return the command line inside the readline function. You should print out any error in the command line length or other problems in in the main or a separate function.
  2. Print argument list. Once you've read the command line you'll want to pass it to the parse function for parsing. This function is in the parse.c file and currently prints a string to show it has run. You'll need to parse up the space separated commands and build your parseInfo struct in here.
  3. Print if there is input redirection, this symbol: '<'. Once your parseInfo struct is complete you'll call print_info and it should run through the struct printing out if there is input redirection. The command would look like this: command < file
  4. Print if there is output redirection, this symbol: '>'. Similarly print_info should declare if there is output redirection. The command would look like this: command > output_file
  5. Print if in background, this symbol: '&'. Your print_info should also show weather or not the command was passed in the background. A command being run in the background would look like this: command &

    All parsing of this information is done in the parse function, the print_info simply prints out what it finds in the parseInfo struct.

Getting Started

Examples

Here is a text file of an example run of the parser shell. This should give you an idea of how the shell and parser should look and interact.

Here is a tar file that contains a Makefile that will get you started compiling on a UNIX/Linux platform. Download the file and then execute the command tar -xf exampleShell.tar to unzip the contents. Then simply cd exampleShell into the exampleShell directory and type make. This will compile shell.c into the executable shell. You can execute shell by typing ./shell at the command prompt. You can remove the executable by typing make clean. Feel free to use this example as a base for your shell.