X-Git-Url: https://git.xonotic.org/?a=blobdiff_plain;f=main.c;h=2256dc444ff70179f5c7191e1bd93b1c0fe41c9e;hb=375cfd24b379d3b84e364707422a12cd3cd42410;hp=006fdea863722c2d5b9c56666eb816c41b976c00;hpb=63b6abb46368f1f4757926a1bfbe8eb6770c2619;p=xonotic%2Fgmqcc.git diff --git a/main.c b/main.c index 006fdea..2256dc4 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2012 + * Copyright (C) 2012 * Dale Weiler * * Permission is hereby granted, free of charge, to any person obtaining a copy of @@ -21,17 +21,9 @@ * SOFTWARE. */ #include "gmqcc.h" -// todo CLEANUP this argitem thing typedef struct { char *name, type; } argitem; VECTOR_MAKE(argitem, items); -/* global options */ -bool opts_debug = false; -bool opts_memchk = false; -bool opts_darkplaces_stringtablebug = false; -bool opts_omit_nullcode = false; -int opts_compiler = COMPILER_GMQCC; - static const int usage(const char *const app) { printf("usage:\n" " %s -c -oprog.dat -- compile file\n" @@ -39,28 +31,30 @@ static const int usage(const char *const app) { " %s -c -i -oprog.dat -- compile together (allowed multiple -i)\n" " %s -a -i -oprog.dat -- assemble together(allowed multiple -i)\n" " example:\n" - " %s -cfoo.qc -ibar.qc -oqc.dat -afoo.qs -ibar.qs -oqs.dat\n" - " additional flags:\n" + " %s -cfoo.qc -ibar.qc -oqc.dat -afoo.qs -ibar.qs -oqs.dat\n", app, app, app, app, app); + + printf(" additional flags:\n" " -debug -- turns on compiler debug messages\n" " -memchk -- turns on compiler memory leak check\n" " -help -- prints this help/usage text\n" - " -std -- select the QuakeC compile type (types below):\n" - " -std=qcc -- original QuakeC\n" + " -std -- select the QuakeC compile type (types below):\n"); + + printf(" -std=qcc -- original QuakeC\n" " -std=ftqecc -- fteqcc QuakeC\n" " -std=qccx -- qccx QuakeC\n" - " -std=gmqcc -- this compiler QuakeC (default selection)\n" - " codegen flags:\n" + " -std=gmqcc -- this compiler QuakeC (default selection)\n"); + + printf(" codegen flags:\n" " -fdarkplaces-string-table-bug -- patches the string table to work with bugged versions of darkplaces\n" - " -fomit-nullcode -- omits the generation of null code (will break everywhere see propsal.txt)\n", - app,app,app,app,app - ); + " -fomit-nullcode -- omits the generation of null code (will break everywhere see propsal.txt)\n"); return -1; } int main(int argc, char **argv) { - size_t itr = 0; - char *app = &argv[0][0]; - FILE *fpp = NULL; + size_t itr = 0; + char *app = &argv[0][0]; + FILE *fpp = NULL; + lex_file *lex = NULL; /* * Parse all command line arguments. This is rather annoying to do @@ -85,9 +79,27 @@ int main(int argc, char **argv) { ); return 0; } - case 'c': items_add((argitem){util_strdup(&argv[1][2]), 0}); break; /* compile */ - case 'a': items_add((argitem){util_strdup(&argv[1][2]), 1}); break; /* assemble */ - case 'i': items_add((argitem){util_strdup(&argv[1][2]), 2}); break; /* includes */ + #define param_argument(argtype) do { \ + argitem item; \ + if (argv[1][2]) { \ + item.name = util_strdup(&argv[1][2]); \ + item.type = argtype; \ + items_add(item); \ + } else { \ + ++argv; \ + --argc; \ + if (argc <= 1) \ + goto clean_params_usage; \ + item.name = util_strdup(argv[1]); \ + item.type = argtype; \ + items_add(item); \ + } \ + } while (0) + + case 'c': { param_argument(0); break; } /* compile */ + case 'a': { param_argument(1); break; } /* assemble */ + case 'i': { param_argument(2); break; } /* includes */ + #undef parm_argument default: if (!strncmp(&argv[1][1], "debug" , 5)) { opts_debug = true; break; } if (!strncmp(&argv[1][1], "memchk", 6)) { opts_memchk = true; break; } @@ -119,7 +131,7 @@ int main(int argc, char **argv) { break; } return usage(app); - + } ++argv; --argc; @@ -129,7 +141,7 @@ int main(int argc, char **argv) { * options could depend on another option, this is where option * validity checking like that would take place. */ - if (opts_memchk && !opts_debug) + if (opts_memchk && !opts_debug) printf("Warning: cannot enable -memchk, without -debug.\n"); util_debug("COM", "starting ...\n"); @@ -137,20 +149,23 @@ int main(int argc, char **argv) { for (; itr < items_elements; itr++) { switch (items_data[itr].type) { case 0: - fpp = fopen(items_data[itr].name, "r"); - struct lex_file *lex = lex_open(fpp); - parse_gen(lex); - lex_close(lex); + lex_init (items_data[itr].name, &lex); + if (lex) { + lex_parse(lex); + lex_close(lex); + } break; case 1: asm_init (items_data[itr].name, &fpp); - asm_parse(fpp); - asm_close(fpp); + if (fpp) { + asm_parse(fpp); + asm_close(fpp); + } break; } } - util_debug("COM", "cleaning ...\n"); + util_debug("COM", "cleaning ...\n"); /* clean list */ for (itr = 0; itr < items_elements; itr++) mem_d(items_data[itr].name); @@ -158,4 +173,10 @@ int main(int argc, char **argv) { util_meminfo(); return 0; + +clean_params_usage: + for (itr = 0; itr < items_elements; itr++) + mem_d(items_data[itr].name); + mem_d(items_data); + return usage(app); }