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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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
26 uint32_t opts_flags[1 + (COUNT_FLAGS / 32)];
27 uint32_t opts_warn [1 + (COUNT_WARNINGS / 32)];
30 const char *opts_output = "progs.dat";
31 int opts_standard = COMPILER_GMQCC;
32 bool opts_debug = false;
33 bool opts_memchk = false;
34 bool opts_dump = false;
35 bool opts_werror = false;
37 static bool opts_output_wasset = false;
39 typedef struct { char *filename; int type; } argitem;
40 VECTOR_MAKE(argitem, items);
46 static const char *app_name;
49 printf("usage: %s [options] [files...]", app_name);
51 " -h, --help show this help message\n"
52 " -debug turns on compiler debug messages\n"
53 " -memchk turns on compiler memory leak check\n");
54 printf(" -o, --output=file output file, defaults to progs.dat\n"
55 " -a filename add an asm file to be assembled\n"
56 " -s filename add a progs.src file to be used\n");
57 printf(" -f<flag> enable a flag\n"
58 " -fno-<flag> disable a flag\n"
59 " -std standard select one of the following standards\n"
60 " -std=qcc original QuakeC\n"
61 " -std=fteqcc fteqcc QuakeC\n"
62 " -std=gmqcc this compiler (default)\n");
63 printf(" -W<warning> enable a warning\n"
64 " -Wno-<warning> disable a warning\n"
65 " -Wall enable all warnings\n");
68 " -fdarkplaces-string-table-bug\n"
69 " patch the string table to work with some bugged darkplaces versions\n"
71 " omits certain null-bytes for a smaller output - requires a patched engine\n"
76 static bool options_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) {
79 for (i = 0; i < listsize; ++i) {
80 if (!strcmp(name, list[i].name)) {
81 longbit lb = list[i].bit;
84 flags[lb.idx] |= (1<<(lb.bit));
86 flags[lb.idx] &= ~(1<<(lb.bit));
91 flags[0] &= ~(1<<(lb));
98 static bool options_setflag(const char *name, bool on) {
99 return options_setflag_all(name, on, opts_flags, opts_flag_list, COUNT_FLAGS);
101 static bool options_setwarn(const char *name, bool on) {
102 return options_setflag_all(name, on, opts_warn, opts_warn_list, COUNT_WARNINGS);
105 static bool options_witharg(int *argc_, char ***argv_, char **out) {
107 char **argv = *argv_;
113 /* eat up the next */
114 if (argc < 2) /* no parameter was provided */
123 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
125 char **argv = *argv_;
127 size_t len = strlen(optname);
129 if (strncmp(argv[0]+ds, optname, len))
132 /* it's --optname, check how the parameter is supplied */
133 if (argv[0][ds+len] == '=') {
134 /* using --opt=param */
135 *out = argv[0]+ds+len+1;
139 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
142 /* using --opt param */
148 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
149 return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
151 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
152 return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
155 static bool options_parse(int argc, char **argv) {
159 while (!argend && argc > 1) {
166 if (argv[0][0] == '-') {
167 /* All gcc-type long options */
168 if (options_long_gcc("std", &argc, &argv, &argarg)) {
169 if (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default"))
170 opts_standard = COMPILER_GMQCC;
171 else if (!strcmp(argarg, "qcc"))
172 opts_standard = COMPILER_QCC;
173 else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc"))
174 opts_standard = COMPILER_FTEQCC;
175 else if (!strcmp(argarg, "qccx"))
176 opts_standard = COMPILER_QCCX;
178 printf("Unknown standard: %s\n", argarg);
183 if (!strcmp(argv[0]+1, "debug")) {
187 if (!strcmp(argv[0]+1, "dump")) {
191 if (!strcmp(argv[0]+1, "memchk")) {
196 switch (argv[0][1]) {
197 /* -h, show usage but exit with 0 */
203 /* handle all -fflags */
205 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
206 if (!strcmp(argv[0]+2, "HELP")) {
207 printf("Possible flags:\n");
208 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
209 util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
210 printf(" -f%s\n", buffer);
214 else if (!strncmp(argv[0]+2, "NO_", 3)) {
215 if (!options_setflag(argv[0]+5, false)) {
216 printf("unknown flag: %s\n", argv[0]+2);
220 else if (!options_setflag(argv[0]+2, true)) {
221 printf("unknown flag: %s\n", argv[0]+2);
226 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
227 if (!strcmp(argv[0]+2, "HELP")) {
228 printf("Possible warnings:\n");
229 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
230 util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
231 printf(" -W%s\n", buffer);
235 else if (!strcmp(argv[0]+2, "NO_ERROR")) {
239 else if (!strcmp(argv[0]+2, "ERROR")) {
243 else if (!strcmp(argv[0]+2, "ALL")) {
244 for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
245 opts_warn[itr] = 0xFFFFFFFFL;
248 if (!strncmp(argv[0]+2, "NO_", 3)) {
249 if (!options_setwarn(argv[0]+5, false)) {
250 printf("unknown warning: %s\n", argv[0]+2);
254 else if (!options_setwarn(argv[0]+2, true)) {
255 printf("unknown warning: %s\n", argv[0]+2);
261 if (!options_witharg(&argc, &argv, &argarg)) {
262 printf("option -O requires a numerical argument\n");
265 opts_O = atoi(argarg);
269 if (!options_witharg(&argc, &argv, &argarg)) {
270 printf("option -o requires an argument: the output file name\n");
273 opts_output = argarg;
274 opts_output_wasset = true;
279 item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
280 if (!options_witharg(&argc, &argv, &argarg)) {
281 printf("option -a requires a filename %s\n",
282 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
285 item.filename = argarg;
291 /* anything following -- is considered a non-option argument */
295 /* All long options without arguments */
296 else if (!strcmp(argv[0]+2, "help")) {
301 /* All long options with arguments */
302 if (options_long_witharg("output", &argc, &argv, &argarg)) {
303 opts_output = argarg;
304 opts_output_wasset = true;
306 printf("Unknown parameter: %s\n", argv[0]);
313 printf("Unknown parameter: %s\n", argv[0]);
319 /* it's a QC filename */
321 item.filename = argv[0];
329 static void options_set(uint32_t *flags, size_t idx, bool on)
331 longbit lb = LONGBIT(idx);
334 flags[lb.idx] |= (1<<(lb.bit));
336 flags[lb.idx] &= ~(1<<(lb.bit));
339 flags[0] |= (1<<(lb));
341 flags[0] &= ~(1<<(lb));
345 /* returns the line number, or -1 on error */
346 static bool progs_nextline(char **out, FILE *src)
355 len = util_getline(&line, &alen, src);
359 /* start at first non-blank */
360 for (start = line; isspace(*start); ++start) {}
361 /* end at the first non-blank */
362 for (end = start; *end && !isspace(*end); ++end) {}
365 /* move the actual filename to the beginning */
366 while (start != end) {
373 int main(int argc, char **argv) {
376 bool opts_output_free = false;
380 /* default options / warn flags */
381 options_set(opts_warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
382 options_set(opts_warn, WARN_EXTENSIONS, true);
383 options_set(opts_warn, WARN_FIELD_REDECLARED, true);
385 if (!options_parse(argc, argv)) {
390 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
391 printf("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
393 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
394 printf("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
396 printf("output = %s\n", opts_output);
397 printf("optimization level = %i\n", (int)opts_O);
398 printf("standard = %i\n", opts_standard);
401 if (!parser_init()) {
402 printf("failed to initialize parser\n");
407 util_debug("COM", "starting ...\n");
409 if (items_elements) {
410 printf("Mode: manual\n");
411 printf("There are %lu items to compile:\n", (unsigned long)items_elements);
412 for (itr = 0; itr < items_elements; ++itr) {
413 printf(" item: %s (%s)\n",
414 items_data[itr].filename,
415 ( (items_data[itr].type == TYPE_QC ? "qc" :
416 (items_data[itr].type == TYPE_ASM ? "asm" :
417 (items_data[itr].type == TYPE_SRC ? "progs.src" :
420 if (!parser_compile(items_data[itr].filename)) {
426 parser_finish(opts_output);
431 printf("Mode: progs.src\n");
432 src = util_fopen("progs.src", "rb");
434 printf("failed to open `progs.src` for reading\n");
440 if (!progs_nextline(&line, src) || !line[0]) {
441 printf("illformatted progs.src file: expected output filename in first line\n");
446 if (!opts_output_wasset) {
447 opts_output = util_strdup(line);
448 opts_output_free = true;
451 while (progs_nextline(&line, src)) {
452 if (!line[0] || (line[0] == '/' && line[1] == '/'))
454 printf(" src: %s\n", line);
455 if (!parser_compile(line)) {
461 parser_finish(opts_output);
471 util_debug("COM", "cleaning ...\n");
476 if (opts_output_free)
477 mem_d((char*)opts_output);