]> git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
Language type selection at command line.
[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 int opts_compiler = COMPILER_GMQCC;
32
33 static const int usage(const char *const app) {
34     printf("usage:\n");
35     printf("    %s -c<file>          -oprog.dat -- compile file\n"     , app);
36     printf("    %s -a<file>          -oprog.dat -- assemble file\n"    , app);
37     printf("    %s -c<file> -i<file> -oprog.dat -- compile together (allowed multiple -i<file>)\n" , app);
38     printf("    %s -a<file> -i<file> -oprog.dat -- assemble together(allowed multiple -i<file>)\n", app);
39     printf("    example:\n");
40     printf("    %s -cfoo.qc -ibar.qc -oqc.dat -afoo.qs -ibar.qs -oqs.dat\n", app);
41     printf("    additional flags:\n");
42     printf("        -debug           -- turns on compiler debug messages\n");
43     printf("        -memchk          -- turns on compiler memory leak check\n");
44     printf("        -help            -- prints this help/usage text\n");
45     printf("        -std             -- select the QuakeC compile type (types below):\n");
46     printf("            -std=qcc     -- original QuakeC\n");
47     printf("            -std=ftqecc  -- fteqcc QuakeC\n");
48     printf("            -std=qccx    -- qccx QuakeC\n");
49     printf("            -std=gmqcc   -- this compiler QuakeC (default selection)\n");
50     return -1;
51 }
52
53 int main(int argc, char **argv) {
54     size_t itr = 0;
55     char  *app = &argv[0][0];
56     FILE  *fpp = NULL;
57
58     /*
59      * Parse all command line arguments.  This is rather annoying to do
60      * because of all tiny corner cases.
61      */
62     if (argc <= 1 || (argv[1][0] != '-'))
63         return usage(app);
64
65     while ((argc > 1) && argv[1][0] == '-') {
66         switch (argv[1][1]) {
67             case 'c': items_add((argitem){util_strdup(&argv[1][2]), 0}); break; /* compile  */ 
68             case 'a': items_add((argitem){util_strdup(&argv[1][2]), 1}); break; /* assemble */
69             case 'i': items_add((argitem){util_strdup(&argv[1][2]), 2}); break; /* includes */
70             default:
71                 if (!strncmp(&argv[1][1], "debug" , 5)) { opts_debug  = 1; break; }
72                 if (!strncmp(&argv[1][1], "memchk", 6)) { opts_memchk = 1; break; }
73                 if (!strncmp(&argv[1][1], "help",   4)) {
74                     return usage(app);
75                     break;
76                 }
77                 /* compiler type selection */
78                 if (!strncmp(&argv[1][1], "std=qcc"   , 7 )) { opts_compiler = COMPILER_QCC;    break; }
79                 if (!strncmp(&argv[1][1], "std=fteqcc", 10)) { opts_compiler = COMPILER_FTEQCC; break; }
80                 if (!strncmp(&argv[1][1], "std=qccx",   8 )) { opts_compiler = COMPILER_QCCX;   break; }
81                 if (!strncmp(&argv[1][1], "std=gmqcc",  9 )) { opts_compiler = COMPILER_GMQCC;  break; }
82                 if (!strncmp(&argv[1][1], "std=",       4 )) {
83                     printf("invalid std selection, supported types:\n");
84                     printf("    -std=qcc     -- original QuakeC\n");
85                     printf("    -std=ftqecc  -- fteqcc QuakeC\n");
86                     printf("    -std=qccx    -- qccx QuakeC\n");
87                     printf("    -std=gmqcc   -- this compiler QuakeC (default selection)\n");
88                     return 0;
89                 }
90                 return usage(app);
91                 
92         }
93         ++argv;
94         --argc;
95     }
96
97     /*
98      * options could depend on another option, this is where option
99      * validity checking like that would take place.
100      */
101     if (opts_memchk && !opts_debug) 
102         printf("Warning: cannot enable -memchk, without -debug.\n");
103
104     util_debug("COM", "starting ...\n");
105     /* multi file multi path compilation system */
106     for (; itr < items_elements; itr++) {
107         switch (items_data[itr].type) {
108             case 0:
109                 fpp = fopen(items_data[itr].name, "r");
110                 struct lex_file *lex = lex_open(fpp);
111                 parse_gen(lex);
112                 lex_close(lex);
113                 break;
114             case 1:
115                 asm_init (items_data[itr].name, &fpp);
116                 asm_parse(fpp);
117                 asm_close(fpp);
118                 break;
119         }
120     }
121
122     util_debug("COM", "cleaning ...\n"); 
123     /* clean list */
124     for (itr = 0; itr < items_elements; itr++)
125         mem_d(items_data[itr].name);
126     mem_d(items_data);
127
128     util_meminfo();
129     return 0;
130 }