APC main driver
[henge/webcc.git] / src / apc / main.c
1 /*!@file
2 \brief APC main driver
3 \details The driver assumes the existence of a bison-generated parser,
4 referenced by the external function 'yyparse'.
5 It also assumes the existence of a lexer which must be initialized
6 before parsing, referenced by the external function 'lexer_init'
7 which assumes standard error handling.
8 All input arguments are made available through the exposed (that is,
9 non-static) array of character pointers 'cargs', which point
10 to the non-duplicated strings in 'argv' directly from the system.
11 \author Jordan Lavatai
12 \date Aug 2016
13 ----------------------------------------------------------------------------*/
14 /* Standard */
15 #include <errno.h>
16 /* Posix */
17 #include <unistd.h> //getopt
18
19 int main(int, char*[]);
20
21 const char* cargs['Z'] = {0};
22
23 extern //bison
24 void yyparse(void);
25 extern //lexer.c
26 int lexer_init(void);
27
28 /* Main entry from terminal
29 parses the command line and kicks off recursive scanning
30 */
31 int main
32 ( int argc,
33 char* argv[]
34 )
35 #define MAXERR "-%c allows at most " #MAX_STR_LEN " input characters", opt
36 #define USAGE "Usage: %s [-r root]\n", argv[0]
37 #define DONE -1
38 { int opt;
39
40 getopt:
41 switch (opt = getopt(argc, argv, "r:o:"))
42 { case DONE:
43 break;
44 case 'r' :
45 case 'o' :
46 if (strnlen(optarg, MAX_STR_LEN) != MAX_STR_LEN)
47 { cargs[opt] = optarg;
48 goto getopt;
49 }
50 fprintf(stderr, MAXERR);
51 default :
52 fprintf(stderr, USAGE);
53 exit(EXIT_FAILURE);
54 }
55 if (lexer_init())
56 { perror("lexer");
57 exit(EXIT_FAILURE);
58 }
59 yyparse();
60 exit(EXIT_SUCCESS);
61 }
62