]> de.git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
Commandline handling first draft
[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
25 uint32_t    opt_flags[1 + (NUM_F_FLAGS / 32)];
26 uint32_t    opt_O = 1;
27 const char *opt_output = "progs.dat";
28
29 static int usage() {
30     printf("Usage:\n");
31     return -1;
32 }
33
34 static bool options_setflag(const char *name, bool on) {
35     size_t i;
36
37     for (i = 0; i < opt_flag_list_count; ++i) {
38         if (!strcmp(name, opt_flag_list[i].name)) {
39             longbit lb = opt_flag_list[i].bit;
40 #if 0
41             if (on)
42                 opt_flags[lb.idx] |= (1<<(lb.bit));
43             else
44                 opt_flags[lb.idx] &= ~(1<<(lb.bit));
45 #else
46             if (on)
47                 opt_flags[0] |= (1<<lb);
48             else
49                 opt_flags[0] &= ~(1<<(lb));
50 #endif
51             return true;
52         }
53     }
54     return false;
55 }
56
57 static bool options_witharg(int *argc_, char ***argv_, char **out) {
58     int  argc   = *argc_;
59     char **argv = *argv_;
60
61     if (argv[0][2]) {
62         *out = argv[0]+2;
63         return true;
64     }
65     /* eat up the next */
66     if (argc < 2) /* no parameter was provided */
67         return false;
68
69     *out = argv[1];
70     --*argc_;
71     ++*argv_;
72     return true;
73 }
74
75 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
76     int  argc   = *argc_;
77     char **argv = *argv_;
78
79     size_t len = strlen(optname);
80
81     if (strncmp(argv[0]+2, optname, len))
82         return false;
83
84     /* it's --optname, check how the parameter is supplied */
85     if (argv[0][2+len] == '=') {
86         /* using --opt=param */
87         *out = argv[0]+2+len+1;
88         return true;
89     }
90
91     if (argc < 2) /* no parameter was provided */
92         return false;
93
94     /* using --opt param */
95     *out = argv[1];
96     --*argc_;
97     ++*argv_;
98     return true;
99 }
100
101 static bool options_parse(int argc, char **argv) {
102     bool argend = false;
103     while (!argend && argc) {
104         char *argarg;
105         if (argv[0][0] == '-') {
106             switch (argv[0][1]) {
107                 /* -h, show usage but exit with 0 */
108                 case 'h':
109                     usage();
110                     exit(0);
111                     break;
112
113                 /* handle all -fflags */
114                 case 'f':
115                     if (!strncmp(argv[0]+2, "no-", 3)) {
116                         if (!options_setflag(argv[0]+5, false)) {
117                             printf("unknown flag: %s\n", argv[0]+2);
118                             return false;
119                         }
120                     }
121                     else if (!options_setflag(argv[0]+2, true)) {
122                         printf("unknown flag: %s\n", argv[0]+2);
123                         return false;
124                     }
125                     break;
126
127                 case 'O':
128                     if (!options_witharg(&argc, &argv, &argarg)) {
129                         printf("option -O requires a numerical argument\n");
130                         return false;
131                     }
132                     opt_O = atoi(argarg);
133                     break;
134
135                 case 'o':
136                     if (!options_witharg(&argc, &argv, &argarg)) {
137                         printf("option -o requires an argument: the output file name\n");
138                         return false;
139                     }
140                     opt_output = argarg;
141                     break;
142
143                 case '-':
144                     if (!argv[0][2]) {
145                         /* anything following -- is considered a non-option argument */
146                         argend = true;
147                         break;
148                     }
149             /* All long options without arguments */
150                     else if (!strcmp(argv[0]+2, "help")) {
151                         usage();
152                         exit(0);
153                     }
154                     else {
155             /* All long options with arguments */
156                         if (options_long_witharg("output", &argc, &argv, &argarg))
157                             opt_output = argarg;
158                         else
159                         {
160                             printf("Unknown parameter: %s\n", argv[0]);
161                             return false;
162                         }
163                     }
164                     break;
165
166                 default:
167                     break;
168             }
169         }
170         ++argv;
171         --argc;
172     }
173     return true;
174 }
175
176 int main(int argc, char **argv) {
177     /* char     *app = &argv[0][0]; */
178
179     if (!options_parse(argc-1, argv+1)) {
180         return usage();
181     }
182
183     util_debug("COM", "starting ...\n");
184
185     /* stuff */
186
187     util_debug("COM", "cleaning ...\n");
188
189     util_meminfo();
190     return 0;
191 }