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