/* PURPOSE: * The MAIN function: * Open an input stream. * Call the Parser. * Close and exit. * HISTORY: * Ian D. Allen idallen@freenet.carleton.ca * ALGORITHM: * Initialize a file name buffer * LOOP: * Clear the file name buffer * CALL a function to prompt for a good file name and open the file * UNTIL we have an open stream, or no file name, or hit EOF * IF we have no open stream, quit the program * CALL the parser. * Close streams and tidy up buffers * END */ #include #include #include "misc.h" #include "cover.h" #include "buffer.h" #include "my.h" #include "parser.h" #define FNAME_INIT 20 /* initial size of file name buffer */ #define FNAME_INCR 10 /* incremental increase for file name buffer */ char *cmdname = "UNKNOWN"; /* set by main; used by eprintf */ int main(int argc, char **argv) { FILE *fd; /* descriptor of input stream */ Buffer *fbuf; /* file name buffer */ if( argc >= 1) cmdname = argv[0];/* save command name for eprintf */ mem_init(); /* ... you may put atexit(mem_term) here */ /* Note the order of operations here is (1), (2) * * (1) Allocate a File Name Buffer. * LOOP: * Clear the file name buffer. * Prompt for an input file and try (2) to open the input file. * (Note that my_open() may return stdin itself as the open fd.) * UNTIL we have an open stream, or no file name, or hit EOF * IF no stream is open, simply say goodbye and exit. */ fbuf = bf_alloc(FNAME_INIT, FNAME_INCR); /* (1) */ do { bf_clear(fbuf); fd = my_open(fbuf, NULL, MY_READONLY, TRUE); /* (2) */ } while ( fd == NULL && ! BF_EMPTY(fbuf) && ! feof(stdin) ); if( fd == NULL ){ eprintf("No file name given. Goodbye."); return EXIT_SUCCESS;/* not an error */ } printf("Successfully opened input stream '%s'\n", fbuf->buf); /* Input is from the open stream. * Output will be on stdout. */ parser(fd, fbuf->buf, stdout, "STDOUT"); printf("Returned from parsing '%s'\n", fbuf->buf); /* Note the order of operations following is the exact reverse * of the complementary operations above: (2), (1). * * (2) Close or flush the input stream. * (1) Free the file name buffer. */ my_close(fd, fbuf->buf); /* (2) */ bf_free(fbuf); /* (1) */ mem_term(); /* check for memory problems before exit */ return EXIT_SUCCESS; }