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