]> git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
removed branch
[xonotic/gmqcc.git] / main.c
1 /*
2  * Copyright (C) 2012 
3  *     Dale Weiler
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is furnished to do
10  * so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 #include "gmqcc.h"
24 // todo CLEANUP this argitem thing
25 typedef struct { char *name, type; } argitem;
26 VECTOR_MAKE(argitem, items);
27
28 /* global options */
29 int opts_debug  = 0;
30 int opts_memchk = 0;
31
32 static const int usage(const char *const app) {
33     printf("usage:\n");
34     printf("    %s -c<file> -- compile file\n" , app);
35     printf("    %s -a<file> -- assemble file\n", app);
36     printf("    additional flags:\n");
37     printf("        -debug  -- turns on compiler debug messages\n");
38     printf("        -memchk -- turns on compiler memory leak check\n");
39     
40     return -1;
41 }
42
43 int main(int argc, char **argv) {
44     size_t itr = 0;
45     char  *app = &argv[0][0];
46     FILE  *fpp = NULL;
47
48     /*
49      * Parse all command line arguments.  This is rather annoying to do
50      * because of all tiny corner cases.
51      */
52     if (argc <= 1 || (argv[1][0] != '-'))
53         return usage(app);
54
55     while ((argc > 1) && argv[1][0] == '-') {
56         switch (argv[1][1]) {
57             case 'c': items_add((argitem){util_strdup(&argv[1][2]), 0}); break; /* compile  */ 
58             case 'a': items_add((argitem){util_strdup(&argv[1][2]), 1}); break; /* assemble */
59             default:
60                 if (!strncmp(&argv[1][1], "debug" , 5)) { opts_debug  = 1; break; }
61                 if (!strncmp(&argv[1][1], "memchk", 6)) { opts_memchk = 1; break; }
62                 return usage(app);
63                 
64         }
65         ++argv;
66         --argc;
67     }
68
69     /*
70      * options could depend on another option, this is where option
71      * validity checking like that would take place.
72      */
73     if (opts_memchk && !opts_debug) 
74         printf("Warning: cannot enable -memchk, without -debug.\n");
75
76     /* multi file multi path compilation system */
77     for (; itr < items_elements; itr++) {
78         switch (items_data[itr].type) {
79             case 0:
80                 fpp = fopen(items_data[itr].name, "r");
81                 struct lex_file *lex = lex_open(fpp);
82                 parse_gen(lex);
83                 lex_close(lex);
84                 break;
85             case 1:
86                 asm_init (items_data[itr].name, &fpp);
87                 asm_parse(fpp);
88                 asm_close(fpp);
89                 break;
90         }
91     }
92     
93     /* clean list */
94     for (itr = 0; itr < items_elements; itr++)
95         mem_d(items_data[itr].name);
96     mem_d(items_data);
97
98     util_meminfo();
99     return 0;
100 }