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;
36 bool opts_forcecrc = false;
38 uint16_t opts_forced_crc;
40 static bool opts_output_wasset = false;
42 typedef struct { char *filename; int type; } argitem;
43 VECTOR_MAKE(argitem, items);
49 static const char *app_name;
52 printf("usage: %s [options] [files...]", app_name);
54 " -h, --help show this help message\n"
55 " -debug turns on compiler debug messages\n"
56 " -memchk turns on compiler memory leak check\n");
57 printf(" -o, --output=file output file, defaults to progs.dat\n"
58 " -a filename add an asm file to be assembled\n"
59 " -s filename add a progs.src file to be used\n");
60 printf(" -f<flag> enable a flag\n"
61 " -fno-<flag> disable a flag\n"
62 " -std standard select one of the following standards\n"
63 " -std=qcc original QuakeC\n"
64 " -std=fteqcc fteqcc QuakeC\n"
65 " -std=gmqcc this compiler (default)\n");
66 printf(" -W<warning> enable a warning\n"
67 " -Wno-<warning> disable a warning\n"
68 " -Wall enable all warnings\n");
69 printf(" -force-crc=num force a specific checksum into the header\n");
72 " -fdarkplaces-string-table-bug\n"
73 " patch the string table to work with some bugged darkplaces versions\n"
75 " omits certain null-bytes for a smaller output - requires a patched engine\n"
80 static bool options_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) {
83 for (i = 0; i < listsize; ++i) {
84 if (!strcmp(name, list[i].name)) {
85 longbit lb = list[i].bit;
88 flags[lb.idx] |= (1<<(lb.bit));
90 flags[lb.idx] &= ~(1<<(lb.bit));
95 flags[0] &= ~(1<<(lb));
102 static bool options_setflag(const char *name, bool on) {
103 return options_setflag_all(name, on, opts_flags, opts_flag_list, COUNT_FLAGS);
105 static bool options_setwarn(const char *name, bool on) {
106 return options_setflag_all(name, on, opts_warn, opts_warn_list, COUNT_WARNINGS);
109 static bool options_witharg(int *argc_, char ***argv_, char **out) {
111 char **argv = *argv_;
117 /* eat up the next */
118 if (argc < 2) /* no parameter was provided */
127 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
129 char **argv = *argv_;
131 size_t len = strlen(optname);
133 if (strncmp(argv[0]+ds, optname, len))
136 /* it's --optname, check how the parameter is supplied */
137 if (argv[0][ds+len] == '=') {
138 /* using --opt=param */
139 *out = argv[0]+ds+len+1;
143 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
146 /* using --opt param */
152 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
153 return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
155 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
156 return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
159 static bool options_parse(int argc, char **argv) {
163 while (!argend && argc > 1) {
170 if (argv[0][0] == '-') {
171 /* All gcc-type long options */
172 if (options_long_gcc("std", &argc, &argv, &argarg)) {
173 if (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default"))
174 opts_standard = COMPILER_GMQCC;
175 else if (!strcmp(argarg, "qcc"))
176 opts_standard = COMPILER_QCC;
177 else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc"))
178 opts_standard = COMPILER_FTEQCC;
179 else if (!strcmp(argarg, "qccx"))
180 opts_standard = COMPILER_QCCX;
182 printf("Unknown standard: %s\n", argarg);
187 if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
188 opts_forcecrc = true;
189 opts_forced_crc = strtol(argarg, NULL, 0);
192 if (!strcmp(argv[0]+1, "debug")) {
196 if (!strcmp(argv[0]+1, "dump")) {
200 if (!strcmp(argv[0]+1, "memchk")) {
205 switch (argv[0][1]) {
206 /* -h, show usage but exit with 0 */
212 /* handle all -fflags */
214 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
215 if (!strcmp(argv[0]+2, "HELP")) {
216 printf("Possible flags:\n");
217 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
218 util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
219 printf(" -f%s\n", buffer);
223 else if (!strncmp(argv[0]+2, "NO_", 3)) {
224 if (!options_setflag(argv[0]+5, false)) {
225 printf("unknown flag: %s\n", argv[0]+2);
229 else if (!options_setflag(argv[0]+2, true)) {
230 printf("unknown flag: %s\n", argv[0]+2);
235 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
236 if (!strcmp(argv[0]+2, "HELP")) {
237 printf("Possible warnings:\n");
238 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
239 util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
240 printf(" -W%s\n", buffer);
244 else if (!strcmp(argv[0]+2, "NO_ERROR")) {
248 else if (!strcmp(argv[0]+2, "ERROR")) {
252 else if (!strcmp(argv[0]+2, "ALL")) {
253 for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
254 opts_warn[itr] = 0xFFFFFFFFL;
257 if (!strncmp(argv[0]+2, "NO_", 3)) {
258 if (!options_setwarn(argv[0]+5, false)) {
259 printf("unknown warning: %s\n", argv[0]+2);
263 else if (!options_setwarn(argv[0]+2, true)) {
264 printf("unknown warning: %s\n", argv[0]+2);
270 if (!options_witharg(&argc, &argv, &argarg)) {
271 printf("option -O requires a numerical argument\n");
274 opts_O = atoi(argarg);
278 if (!options_witharg(&argc, &argv, &argarg)) {
279 printf("option -o requires an argument: the output file name\n");
282 opts_output = argarg;
283 opts_output_wasset = true;
288 item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
289 if (!options_witharg(&argc, &argv, &argarg)) {
290 printf("option -a requires a filename %s\n",
291 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
294 item.filename = argarg;
300 /* anything following -- is considered a non-option argument */
304 /* All long options without arguments */
305 else if (!strcmp(argv[0]+2, "help")) {
310 /* All long options with arguments */
311 if (options_long_witharg("output", &argc, &argv, &argarg)) {
312 opts_output = argarg;
313 opts_output_wasset = true;
315 printf("Unknown parameter: %s\n", argv[0]);
322 printf("Unknown parameter: %s\n", argv[0]);
328 /* it's a QC filename */
330 item.filename = argv[0];
338 static void options_set(uint32_t *flags, size_t idx, bool on)
340 longbit lb = LONGBIT(idx);
343 flags[lb.idx] |= (1<<(lb.bit));
345 flags[lb.idx] &= ~(1<<(lb.bit));
348 flags[0] |= (1<<(lb));
350 flags[0] &= ~(1<<(lb));
354 /* returns the line number, or -1 on error */
355 static bool progs_nextline(char **out, size_t *alen,FILE *src)
363 len = util_getline(&line, alen, src);
367 /* start at first non-blank */
368 for (start = line; isspace(*start); ++start) {}
369 /* end at the first non-blank */
370 for (end = start; *end && !isspace(*end); ++end) {}
373 /* move the actual filename to the beginning */
374 while (start != end) {
381 int main(int argc, char **argv) {
384 bool opts_output_free = false;
388 /* default options / warn flags */
389 options_set(opts_warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
390 options_set(opts_warn, WARN_EXTENSIONS, true);
391 options_set(opts_warn, WARN_FIELD_REDECLARED, true);
392 options_set(opts_warn, WARN_TOO_FEW_PARAMETERS, true);
393 options_set(opts_warn, WARN_MISSING_RETURN_VALUES, true);
394 options_set(opts_warn, WARN_USED_UNINITIALIZED, true);
395 options_set(opts_warn, WARN_LOCAL_CONSTANTS, true);
396 options_set(opts_warn, WARN_VOID_VARIABLES, true);
397 options_set(opts_warn, WARN_IMPLICIT_FUNCTION_POINTER, true);
399 if (!options_parse(argc, argv)) {
404 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
405 printf("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
407 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
408 printf("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
410 printf("output = %s\n", opts_output);
411 printf("optimization level = %i\n", (int)opts_O);
412 printf("standard = %i\n", opts_standard);
415 if (!parser_init()) {
416 printf("failed to initialize parser\n");
421 util_debug("COM", "starting ...\n");
423 if (items_elements) {
424 printf("Mode: manual\n");
425 printf("There are %lu items to compile:\n", (unsigned long)items_elements);
426 for (itr = 0; itr < items_elements; ++itr) {
427 printf(" item: %s (%s)\n",
428 items_data[itr].filename,
429 ( (items_data[itr].type == TYPE_QC ? "qc" :
430 (items_data[itr].type == TYPE_ASM ? "asm" :
431 (items_data[itr].type == TYPE_SRC ? "progs.src" :
434 if (!parser_compile(items_data[itr].filename)) {
440 parser_finish(opts_output);
446 printf("Mode: progs.src\n");
447 src = util_fopen("progs.src", "rb");
449 printf("failed to open `progs.src` for reading\n");
455 if (!progs_nextline(&line, &linelen, src) || !line[0]) {
456 printf("illformatted progs.src file: expected output filename in first line\n");
461 if (!opts_output_wasset) {
462 opts_output = util_strdup(line);
463 opts_output_free = true;
466 while (progs_nextline(&line, &linelen, src)) {
467 if (!line[0] || (line[0] == '/' && line[1] == '/'))
469 printf(" src: %s\n", line);
470 if (!parser_compile(line)) {
476 parser_finish(opts_output);
486 util_debug("COM", "cleaning ...\n");
491 if (opts_output_free)
492 mem_d((char*)opts_output);