//*****************************************************************************
//
// Program:     Shell Project -- part 1
//
// Author:      Travis Dillon - I only wrote the doline function
// Class:       CS 442
// Email:       tdillon@ace.cs.ohiou.edu
//
// Description: this is a simple bash "shell" which takes in a
//              command line and parses it, then displays the
//              values of various tokens it has parsed
//
// Date:        September 23, 2004
//
//*****************************************************************************


#include <string.h> 
#include <stdio.h>
#include <stdlib.h>
#include "parser.h"
#include "bash.h"
static int line_num = 1;

int main(int argc, char *argv[])
{
    yydebug = 1;  /* turn on ugly YACC debugging */
    yydebug = 0;  /* turn off ugly YACC debugging */

    /* parse the input file */
    yyparse();

    exit(0);
}



//*****************************************************************************
//
// Function:   doline
//
// Purpose:    Performs output operations on the token values
//             passed from the parsing
//
// Parameters: *pcmd - link lists of command structs
//
//
//*****************************************************************************



void doline(struct command *pcmd)
{
   int i = 0;
   int back_flag = pcmd -> is_back;

   printf("========== line ");
   printf("%d",line_num);
   printf(" ============\n");
   line_num = line_num + 1;

   while(pcmd != NULL)
   {  //output the command names of each pipe
      printf("Command name: '");
      printf(pcmd -> command);
      printf("'\n");

      while(i < pcmd -> argc)
      {  //output all the arguments of this pipe
         printf("     argv[");
         printf("%d",i);
         printf("]: '");
         printf(pcmd -> argv[i]);
         printf("'");
         printf("\n");
         i = i + 1;
      }
      i = 0;
      //output file redirections
      printf("  stdin:  '");
      if(pcmd -> infile != NULL)
      {  //stdin is directed
         printf(pcmd -> infile);
         printf("'");
      }
      else
      {
         printf("<undirected>'");
      }
      printf("\n  stdout: '");
      if(pcmd -> outfile != NULL)
      {  //stdout is directed
         printf(pcmd -> outfile);
         if(pcmd -> output_append)
         {
            printf("' (append)");
         }
         else
         {
            printf("'");
         }
      }
      else
      {
         printf("<undirected>'");
      }
      printf("\n  stderr: '");
      if(pcmd -> errfile != NULL)
      {  //stderr is directed
         printf(pcmd -> errfile);
         if(pcmd -> error_append)
         {
            printf("' (append)\n");
         }
         else
         {
            printf("'\n");
         }
      }
      else
      {
         printf("<undirected>'\n");
      }
      pcmd = pcmd -> nextpipe;
   }

   if(back_flag)
   {  //run command in background
      printf("  background-option: '<on>'\n");
   }
   else
   {
      printf("  background-option: '<off>'\n");
   }   
} /* end of doline(..) */