/* main.c * Main program for a Minimally Usable SHell (MUSH) * modified by Douglas Jones to complete MP4, Mar 27, 2018 * modified by Douglas Jones to start MP4, Mar 2, 2018 * extracted from mush.c, Feb 25, 2018. * rewritten by Douglas Jones for MP2 in CS:3620, Feb 13, 2018 * originally by Douglas Jones */ #include /* needed for fopen(), fputs(stderr), feof(stdin) */ #include /* needed for exit(EXIT_FAILURE) */ #include "globals.h" /* global variables of mush, including infile */ int main( int argc, char * argv[] ) /* repeatedly reads commands, splits them up, and executes them */ { infile = stdin; /* default */ if (argc >= 2) { /* argv[1] exists */ /* first, open the input file */ infile = fopen( argv[1], "r" ); if (infile == NULL) { fputs( argv[0], stderr ); fputs( ": cannot read ", stderr ); fputs( argv[1], stderr ); fputs( "\n", stderr ); exit( EXIT_FAILURE ); } /* then transcribe the arguments into the environment */ /* argv[1] is script name, argv[2] and up are args to script */ int i; char argname[2]; /* needed so each arg is a string */ argname[1] = '\0'; /* arg names ends with NUL char */ for( i = 1; i < argc; i++ ) { argname[0] = '0' + (i - 1); /* suffix on arg name */ /* BUG: the above creates bad arg names above "9" */ setenv( argname, argv[i], 1 ); } } while (!feof( infile )) { getcommand(); splitargv(); launch(); } }