build system 3
[henge/webcc.git] / src / bin / tools / apc.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 <stdio.h> //print
16 #include <errno.h> //errors
17 #include <string.h> //strnlen
18 /* Posix */
19 #include <stdlib.h> //exit
20 #include <unistd.h> //getopt
21
22 const char* cargs['Z'] = {0};
23
24 int main(int, char*[]);
25
26 extern //bison
27 void yyparse(void);
28 extern //lexer.c
29 int lexer_init(void);
30
31 /* Main entry from terminal
32 parses the command line and kicks off recursive scanning
33 */
34 int main
35 ( int argc,
36 char* argv[]
37 )
38 #define S(S)#S //stringifier
39 #define MAXSTR 255
40 #define MAXERR "-%c allows at most " S(MAXSTR) " input characters\n", opt
41 #define USAGE "Usage: %s [-r root]\n", argv[0]
42 #define DONE -1
43 { int opt;
44
45 getopt:
46 switch (opt = getopt(argc, argv, "r:o:"))
47 { case DONE:
48 break;
49 case 'r' :
50 case 'o' :
51 if (strnlen(optarg, MAXSTR) != MAXSTR)
52 { cargs[opt] = optarg;
53 goto getopt;
54 }
55 fprintf(stderr, MAXERR);
56 default :
57 fprintf(stderr, USAGE);
58 exit(EXIT_FAILURE);
59 }
60 if (lexer_init())
61 { perror("lexer");
62 exit(EXIT_FAILURE);
63 }
64 yyparse();
65 exit(EXIT_SUCCESS);
66 }
67