Lexer actually lexes filenames now, and odats are made out of mapvariants
[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> //strndupa
18 /* Posix */
19 #include <stdlib.h> //exit
20 #include <unistd.h> //getopt
21 /* Internal */
22 #include <apc/parser.tab.h> //bison
23
24 const char* cargs['Z'] = {0};
25
26 int main(int, char*[]);
27
28 extern //bison
29 int yyparse(void);
30 extern //lexer.c
31 int lexer_init(void);
32
33 extern //apc/parser.tab.c
34 YYSTYPE yylval;
35 extern //lexer.c
36 int lexer(void);
37
38 /* Main entry from terminal
39 parses the command line and kicks off recursive scanning
40 */
41 int main
42 ( int argc,
43 char* argv[]
44 )
45 #define $($)#$ //stringifier
46 #define MAXSTR 255
47 #define MAXERR "-%c allows at most " $(MAXSTR) " input characters\n", opt
48 #define OPTS "d:o:h-"
49 #define USAGE "Usage %s [-d dir_root][-o output_file][-h]\n", argv[0]
50 #define USAGE_LONG \
51 "\tOptions:\n" \
52 "\t\t-d\tRoot directory to parse from \t[./]\n" \
53 "\t\t-o\tOutput filename \t\t[a.asspak]\n" \
54 "\t\t-h\tPrint this help\n"
55 #define DONE -1
56 { int opt;
57
58 getopt:
59 switch (opt = getopt(argc, argv, OPTS))
60 { case DONE:
61 break;
62 case 'd' :
63 case 'o' :
64 if (strnlen(optarg, MAXSTR) != MAXSTR)
65 { cargs[opt] = optarg;
66 goto getopt;
67 }
68 fprintf(stderr, MAXERR);
69 default :
70 fprintf(stderr, USAGE);
71 exit(EXIT_FAILURE);
72 case 'h' :
73 printf(USAGE);
74 printf(USAGE_LONG);
75 exit(EXIT_SUCCESS);
76 }
77 if (lexer_init())
78 { perror("lexer");
79 exit(EXIT_FAILURE);
80 }
81 yyparse();
82 exit(EXIT_SUCCESS);
83 }
84