
/********* CS 414 Assignment 2 **********
 ********* Omar Khan    - ohk2 **********
 ********* Murali Kumar - mk262**********
 ********* header file for our shell ****
 */


/* MACROS */
#define FREE(arg1) if ((arg1)!=NULL) free((arg1))
#define MIN(x,y) (((x) < (y))? (x) : (y))

/* constants */
#define OK 0
#define FAIL -1
#define MAX_ARGS 255
#define MAX_HIST 5
#define BUFSIZE 4096

/* define an enumeration for the type of commands  
 * NO_SUCH_BUILTIN is the same as an external command */
enum
BUILTIN_COMMANDS { NO_SUCH_BUILTIN=0, EXIT, JOBS, CD, HISTORY, KILL, HELP};

/* a structure for storing information about a command entered at the shell prompt */
struct cmdInfo_t{
  enum BUILTIN_COMMANDS p;   // the type of command entered
  char * cmd;                // the basic command itself, sans arguments
  char * myStdin;            // the standard input file for the command (if any)
  char * myStdout;           // the standard output file for the command (if any)
  char* args[MAX_ARGS];      // the arguments of the command. args[0] is the command itself
  int isFG;                  // 1 if it is a foreground process, 0 otherwise
  int builtInArg;            // the job number passed in with the KILL command
};

/* a linked list for recording information about background processes */
struct backgrndList_t{
#ifdef UNIX
  int pid;                        // process id of the process
#endif
#ifdef WINDOWS
  PROCESS_INFORMATION pi;         // PROCESS_INFORMATION related with the process
#endif
  int jobNum;                     // the job number of this process in the shell
  int isDone;                     // 1 if the background process has terminated, 0 otherwise
  char * cmdLine;                 // the command line entered for this process
  struct backgrndList_t *next;    // a pointer to the next element in the list
};

/* function definitions */

int isBuiltInCommand(char * cmdLine);
int Parser(char *,struct cmdInfo_t*);
int executeBuiltInCommand(struct cmdInfo_t * cmdInfo);
void jobHandlerAndCleanup(struct cmdInfo_t * cmdInfo);
void addBackgrndJob(struct backgrndList_t * backgrndJob);
char * buildPrompt();


#ifdef UNIX
void childHandler(int signum);
#endif

#ifdef WINDOWS
void childHandler();
#endif
