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 /* set by the standard */
43 const oper_info *operators = NULL;
44 size_t operator_count = 0;
46 typedef struct { char *filename; int type; } argitem;
47 VECTOR_MAKE(argitem, items);
53 static const char *app_name;
56 printf("usage: %s [options] [files...]", app_name);
58 " -h, --help show this help message\n"
59 " -debug turns on compiler debug messages\n"
60 " -memchk turns on compiler memory leak check\n");
61 printf(" -o, --output=file output file, defaults to progs.dat\n"
62 " -a filename add an asm file to be assembled\n"
63 " -s filename add a progs.src file to be used\n");
64 printf(" -f<flag> enable a flag\n"
65 " -fno-<flag> disable a flag\n"
66 " -std standard select one of the following standards\n"
67 " -std=qcc original QuakeC\n"
68 " -std=fteqcc fteqcc QuakeC\n"
69 " -std=gmqcc this compiler (default)\n");
70 printf(" -W<warning> enable a warning\n"
71 " -Wno-<warning> disable a warning\n"
72 " -Wall enable all warnings\n");
73 printf(" -force-crc=num force a specific checksum into the header\n");
76 " -fdarkplaces-string-table-bug\n"
77 " patch the string table to work with some bugged darkplaces versions\n"
79 " omits certain null-bytes for a smaller output - requires a patched engine\n"
84 static bool options_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) {
87 for (i = 0; i < listsize; ++i) {
88 if (!strcmp(name, list[i].name)) {
89 longbit lb = list[i].bit;
92 flags[lb.idx] |= (1<<(lb.bit));
94 flags[lb.idx] &= ~(1<<(lb.bit));
99 flags[0] &= ~(1<<(lb));
106 static bool options_setflag(const char *name, bool on) {
107 return options_setflag_all(name, on, opts_flags, opts_flag_list, COUNT_FLAGS);
109 static bool options_setwarn(const char *name, bool on) {
110 return options_setflag_all(name, on, opts_warn, opts_warn_list, COUNT_WARNINGS);
113 static bool options_witharg(int *argc_, char ***argv_, char **out) {
115 char **argv = *argv_;
121 /* eat up the next */
122 if (argc < 2) /* no parameter was provided */
131 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
133 char **argv = *argv_;
135 size_t len = strlen(optname);
137 if (strncmp(argv[0]+ds, optname, len))
140 /* it's --optname, check how the parameter is supplied */
141 if (argv[0][ds+len] == '=') {
142 /* using --opt=param */
143 *out = argv[0]+ds+len+1;
147 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
150 /* using --opt param */
156 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
157 return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
159 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
160 return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
163 static bool options_parse(int argc, char **argv) {
167 while (!argend && argc > 1) {
174 if (argv[0][0] == '-') {
175 /* All gcc-type long options */
176 if (options_long_gcc("std", &argc, &argv, &argarg)) {
177 if (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default"))
178 opts_standard = COMPILER_GMQCC;
179 else if (!strcmp(argarg, "qcc"))
180 opts_standard = COMPILER_QCC;
181 else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc"))
182 opts_standard = COMPILER_FTEQCC;
183 else if (!strcmp(argarg, "qccx"))
184 opts_standard = COMPILER_QCCX;
186 printf("Unknown standard: %s\n", argarg);
191 if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
192 opts_forcecrc = true;
193 opts_forced_crc = strtol(argarg, NULL, 0);
196 if (!strcmp(argv[0]+1, "debug")) {
200 if (!strcmp(argv[0]+1, "dump")) {
204 if (!strcmp(argv[0]+1, "memchk")) {
209 switch (argv[0][1]) {
210 /* -h, show usage but exit with 0 */
216 /* handle all -fflags */
218 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
219 if (!strcmp(argv[0]+2, "HELP")) {
220 printf("Possible flags:\n");
221 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
222 util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
223 printf(" -f%s\n", buffer);
227 else if (!strncmp(argv[0]+2, "NO_", 3)) {
228 if (!options_setflag(argv[0]+5, false)) {
229 printf("unknown flag: %s\n", argv[0]+2);
233 else if (!options_setflag(argv[0]+2, true)) {
234 printf("unknown flag: %s\n", argv[0]+2);
239 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
240 if (!strcmp(argv[0]+2, "HELP")) {
241 printf("Possible warnings:\n");
242 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
243 util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
244 printf(" -W%s\n", buffer);
248 else if (!strcmp(argv[0]+2, "NO_ERROR")) {
252 else if (!strcmp(argv[0]+2, "ERROR")) {
256 else if (!strcmp(argv[0]+2, "ALL")) {
257 for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
258 opts_warn[itr] = 0xFFFFFFFFL;
261 if (!strncmp(argv[0]+2, "NO_", 3)) {
262 if (!options_setwarn(argv[0]+5, false)) {
263 printf("unknown warning: %s\n", argv[0]+2);
267 else if (!options_setwarn(argv[0]+2, true)) {
268 printf("unknown warning: %s\n", argv[0]+2);
274 if (!options_witharg(&argc, &argv, &argarg)) {
275 printf("option -O requires a numerical argument\n");
278 opts_O = atoi(argarg);
282 if (!options_witharg(&argc, &argv, &argarg)) {
283 printf("option -o requires an argument: the output file name\n");
286 opts_output = argarg;
287 opts_output_wasset = true;
292 item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
293 if (!options_witharg(&argc, &argv, &argarg)) {
294 printf("option -a requires a filename %s\n",
295 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
298 item.filename = argarg;
304 /* anything following -- is considered a non-option argument */
308 /* All long options without arguments */
309 else if (!strcmp(argv[0]+2, "help")) {
314 /* All long options with arguments */
315 if (options_long_witharg("output", &argc, &argv, &argarg)) {
316 opts_output = argarg;
317 opts_output_wasset = true;
319 printf("Unknown parameter: %s\n", argv[0]);
326 printf("Unknown parameter: %s\n", argv[0]);
332 /* it's a QC filename */
334 item.filename = argv[0];
342 static void options_set(uint32_t *flags, size_t idx, bool on)
344 longbit lb = LONGBIT(idx);
347 flags[lb.idx] |= (1<<(lb.bit));
349 flags[lb.idx] &= ~(1<<(lb.bit));
352 flags[0] |= (1<<(lb));
354 flags[0] &= ~(1<<(lb));
358 /* returns the line number, or -1 on error */
359 static bool progs_nextline(char **out, size_t *alen,FILE *src)
367 len = util_getline(&line, alen, src);
371 /* start at first non-blank */
372 for (start = line; isspace(*start); ++start) {}
373 /* end at the first non-blank */
374 for (end = start; *end && !isspace(*end); ++end) {}
377 /* move the actual filename to the beginning */
378 while (start != end) {
385 int main(int argc, char **argv) {
388 bool opts_output_free = false;
392 /* default options / warn flags */
393 options_set(opts_warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
394 options_set(opts_warn, WARN_EXTENSIONS, true);
395 options_set(opts_warn, WARN_FIELD_REDECLARED, true);
396 options_set(opts_warn, WARN_TOO_FEW_PARAMETERS, true);
397 options_set(opts_warn, WARN_MISSING_RETURN_VALUES, true);
398 options_set(opts_warn, WARN_USED_UNINITIALIZED, true);
399 options_set(opts_warn, WARN_LOCAL_CONSTANTS, true);
400 options_set(opts_warn, WARN_VOID_VARIABLES, true);
401 options_set(opts_warn, WARN_IMPLICIT_FUNCTION_POINTER, true);
402 options_set(opts_warn, WARN_VARIADIC_FUNCTION, true);
403 options_set(opts_warn, WARN_FRAME_MACROS, true);
405 if (!options_parse(argc, argv)) {
409 /* the standard decides which set of operators to use */
410 if (opts_standard == COMPILER_GMQCC) {
411 operators = c_operators;
412 operator_count = c_operator_count;
414 operators = qcc_operators;
415 operator_count = qcc_operator_count;
419 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
420 printf("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
422 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
423 printf("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
425 printf("output = %s\n", opts_output);
426 printf("optimization level = %i\n", (int)opts_O);
427 printf("standard = %i\n", opts_standard);
430 if (!parser_init()) {
431 printf("failed to initialize parser\n");
436 util_debug("COM", "starting ...\n");
438 if (items_elements) {
439 printf("Mode: manual\n");
440 printf("There are %lu items to compile:\n", (unsigned long)items_elements);
441 for (itr = 0; itr < items_elements; ++itr) {
442 printf(" item: %s (%s)\n",
443 items_data[itr].filename,
444 ( (items_data[itr].type == TYPE_QC ? "qc" :
445 (items_data[itr].type == TYPE_ASM ? "asm" :
446 (items_data[itr].type == TYPE_SRC ? "progs.src" :
449 if (!parser_compile(items_data[itr].filename)) {
455 parser_finish(opts_output);
461 printf("Mode: progs.src\n");
462 src = util_fopen("progs.src", "rb");
464 printf("failed to open `progs.src` for reading\n");
470 if (!progs_nextline(&line, &linelen, src) || !line[0]) {
471 printf("illformatted progs.src file: expected output filename in first line\n");
476 if (!opts_output_wasset) {
477 opts_output = util_strdup(line);
478 opts_output_free = true;
481 while (progs_nextline(&line, &linelen, src)) {
482 if (!line[0] || (line[0] == '/' && line[1] == '/'))
484 printf(" src: %s\n", line);
485 if (!parser_compile(line)) {
491 parser_finish(opts_output);
501 util_debug("COM", "cleaning ...\n");
506 if (opts_output_free)
507 mem_d((char*)opts_output);