df4810e3b56569a732d054ea402af5de424fa957
[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 #include "ir.h"
24
25 #define DEFAULT_PAGESIZE 4096
26 const char* cargs['Z'] = {0};
27 long sys_pagesize;
28
29 int main(int, char*[]);
30
31 extern //lexer.c
32 int lexer_init(void);
33 extern //scanner.c
34 int scanner_init(void);
35 extern //scanner.c
36 void scanner_quit(void);
37 extern //scanner.c
38 int scanner_scanpath(char const*);
39 extern //ir.c
40 int ir_init(void);
41 extern //ir.c
42 int ir_linker(void);
43 extern //ir.c
44 int ir_condenser(void);
45 extern
46 void ir_test(void);
47
48 /* Main entry from terminal
49 parses the command line and kicks off recursive scanning
50 */
51 int main
52 ( int argc,
53 char* argv[]
54 )
55 #define $($)#$ //stringifier
56 #define MAXSTR 255
57 #define MAXERR "-%c allows at most " $(MAXSTR) " input characters\n", opt
58 #define OPTS "d:o:h-"
59 #define USAGE "Usage %s [-d dir_root][-o output_file][-h]\n", argv[0]
60 #define USAGE_LONG \
61 "\tOptions:\n" \
62 "\t\t-d\tRoot directory to parse from \t[./]\n" \
63 "\t\t-o\tOutput filename \t\t[a.asspak]\n" \
64 "\t\t-h\tPrint this help\n"
65 #define DONE -1
66 #define SCANPATH (cargs['d'] ? cargs['d'] : "./")
67 { int opt;
68 getopt:
69 switch (opt = getopt(argc, argv, OPTS))
70 { case 'd' :
71 case 'o' :
72 if (strnlen(optarg, MAXSTR) != MAXSTR)
73 { cargs[opt] = optarg;
74 goto getopt;
75 }
76 fprintf(stderr, MAXERR);
77 default :
78 fprintf(stderr, USAGE);
79 exit(EXIT_FAILURE);
80 case 'h' :
81 printf(USAGE);
82 printf(USAGE_LONG);
83 exit(EXIT_SUCCESS);
84 case DONE:
85 break;
86 }
87 if ((sys_pagesize = sysconf(_SC_PAGESIZE)) == 0)
88 sys_pagesize = DEFAULT_PAGESIZE;
89 if (ir_init())
90 { perror("init");
91 exit(EXIT_FAILURE);
92 }
93 #if 0
94 if (scanner_scanpath(SCANPATH))
95 { perror("scanner");
96 exit(EXIT_FAILURE);
97 }
98 #endif
99 ir_class class;
100 ir_set set;
101 int i, j;
102 char wordbuf[0xFFF];
103 for (class = ir_class_root(), i = 0, j = 0; i < 100; i++)
104 { sprintf(wordbuf,"Class%i",i);
105 class = ir_class_addchild(class,(uint8_t*)wordbuf);
106 sprintf(wordbuf,"c%i-s%i",i,j++);
107 set = ir_class_addset(class, (uint8_t*)wordbuf);
108 sprintf(wordbuf,"c%i-s%i",i,j++);
109 ir_class_addset(class, (uint8_t*)wordbuf);
110 sprintf(wordbuf,"c%i-s%i",i,j++);
111 ir_class_addset(class, (uint8_t*)wordbuf);
112 sprintf(wordbuf,"c%i-sc%i",i,j=0);
113 set = ir_set_addchild(set, (uint8_t*)wordbuf);
114 sprintf(wordbuf,"c%i-sc%i",i,++j);
115 ir_set_addchild(set, (uint8_t*)wordbuf);
116 sprintf(wordbuf,"c%i-sc%i",i,++j);
117 set = ir_set_addchild(set, (uint8_t*)wordbuf);
118 sprintf(wordbuf,"c%i-sb%i",i,j=0);
119 ir_set_addchild(set, (uint8_t*)wordbuf);
120 }
121 ir_test();
122 ir_linker();
123 ir_condenser();
124 exit(EXIT_SUCCESS);
125 }