cea384743f0377d264eaa7145ddc7bf50fefdb6d
[henge/apc.git] / src / 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 "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 extern //ir.c
33 int ir_init(void);
34
35 extern //apc/parser.tab.c
36 YYSTYPE yylval;
37 extern //lexer.c
38 int lexer(void);
39
40 /* Main entry from terminal
41 parses the command line and kicks off recursive scanning
42 */
43 int main
44 ( int argc,
45 char* argv[]
46 )
47 #define $($)#$ //stringifier
48 #define MAXSTR 255
49 #define MAXERR "-%c allows at most " $(MAXSTR) " input characters\n", opt
50 #define OPTS "d:o:h-"
51 #define USAGE "Usage %s [-d dir_root][-o output_file][-h]\n", argv[0]
52 #define USAGE_LONG \
53 "\tOptions:\n" \
54 "\t\t-d\tRoot directory to parse from \t[./]\n" \
55 "\t\t-o\tOutput filename \t\t[a.asspak]\n" \
56 "\t\t-h\tPrint this help\n"
57 #define DONE -1
58 { int opt;
59
60 getopt:
61 switch (opt = getopt(argc, argv, OPTS))
62 { case DONE:
63 break;
64 case 'd' :
65 case 'o' :
66 if (strnlen(optarg, MAXSTR) != MAXSTR)
67 { cargs[opt] = optarg;
68 goto getopt;
69 }
70 fprintf(stderr, MAXERR);
71 default :
72 fprintf(stderr, USAGE);
73 exit(EXIT_FAILURE);
74 case 'h' :
75 printf(USAGE);
76 printf(USAGE_LONG);
77 exit(EXIT_SUCCESS);
78 }
79 if (lexer_init() || ir_init())
80 { perror("init");
81 exit(EXIT_FAILURE);
82 }
83 yyparse();
84 exit(EXIT_SUCCESS);
85 }
86