+ case 'O':
+ if (!options_witharg(&argc, &argv, &argarg)) {
+ con_out("option -O requires a numerical argument, or optimization name with an optional 'no-' prefix\n");
+ return false;
+ }
+ if (isdigit(argarg[0])) {
+ opts.O = atoi(argarg);
+ opts_setoptimlevel(opts.O);
+ } else {
+ util_strtocmd(argarg, argarg, strlen(argarg)+1);
+ if (!strcmp(argarg, "HELP")) {
+ con_out("Possible optimizations:\n");
+ for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
+ util_strtononcmd(opts_opt_list[itr].name, buffer, sizeof(buffer));
+ con_out(" -O%-20s (-O%u)\n", buffer, opts_opt_oflag[itr]);
+ }
+ exit(0);
+ }
+ else if (!strcmp(argarg, "ALL"))
+ opts_setoptimlevel(opts.O = 9999);
+ else if (!strncmp(argarg, "NO_", 3)) {
+ if (!opts_setoptim(argarg+3, false)) {
+ con_out("unknown optimization: %s\n", argarg+3);
+ return false;
+ }
+ }
+ else {
+ if (!opts_setoptim(argarg, true)) {
+ con_out("unknown optimization: %s\n", argarg);
+ return false;
+ }
+ }
+ }
+ break;
+
+ case 'o':
+ if (!options_witharg(&argc, &argv, &argarg)) {
+ con_out("option -o requires an argument: the output file name\n");
+ return false;
+ }
+ opts.output = argarg;
+ opts_output_wasset = true;
+ break;
+
+ case 'a':
+ case 's':
+ item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
+ if (!options_witharg(&argc, &argv, &argarg)) {
+ con_out("option -a requires a filename %s\n",
+ (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
+ return false;
+ }
+ item.filename = argarg;
+ vec_push(items, item);
+ break;
+
+ case '-':
+ if (!argv[0][2]) {
+ /* anything following -- is considered a non-option argument */
+ argend = true;
+ break;
+ }
+ /* All long options without arguments */
+ else if (!strcmp(argv[0]+2, "help")) {
+ usage();
+ exit(0);
+ }
+ else if (!strcmp(argv[0]+2, "version")) {
+ version();
+ exit(0);
+ }
+ else if (!strcmp(argv[0]+2, "quiet")) {
+ opts.quiet = true;
+ break;
+ }
+ else {
+ /* All long options with arguments */
+ if (options_long_witharg("output", &argc, &argv, &argarg)) {
+ opts.output = argarg;
+ opts_output_wasset = true;
+ } else {
+ con_out("Unknown parameter: %s\n", argv[0]);
+ return false;
+ }
+ }
+ break;
+
+ default:
+ con_out("Unknown parameter: %s\n", argv[0]);
+ return false;
+ }
+ }
+ else
+ {
+ /* it's a QC filename */
+ item.filename = argv[0];
+ item.type = TYPE_QC;
+ vec_push(items, item);
+ }
+ }
+ opts_ini_init(config);
+ return true;
+}
+
+/* returns the line number, or -1 on error */
+static bool progs_nextline(char **out, size_t *alen,FILE *src) {
+ int len;
+ char *line;
+ char *start;
+ char *end;
+
+ line = *out;
+ len = file_getline(&line, alen, src);
+ if (len == -1)
+ return false;
+
+ /* start at first non-blank */
+ for (start = line; isspace(*start); ++start) {}
+ /* end at the first non-blank */
+ for (end = start; *end && !isspace(*end); ++end) {}
+
+ *out = line;
+ /* move the actual filename to the beginning */
+ while (start != end) {
+ *line++ = *start++;
+ }
+ *line = 0;
+ return true;
+}
+
+int main(int argc, char **argv) {
+ size_t itr;
+ int retval = 0;
+ bool opts_output_free = false;
+ bool operators_free = false;
+ bool progs_src = false;
+ FILE *outfile = NULL;
+
+ app_name = argv[0];
+ con_init ();
+ opts_init("progs.dat", COMPILER_GMQCC, (1024 << 3));
+
+ util_seed(time(0));
+
+ if (!options_parse(argc, argv)) {
+ return usage();
+ }
+
+ if (OPTS_FLAG(TRUE_EMPTY_STRINGS) && OPTS_FLAG(FALSE_EMPTY_STRINGS)) {
+ con_err("-ftrue-empty-strings and -ffalse-empty-strings are mutually exclusive");
+ exit(1);
+ }
+
+ /* the standard decides which set of operators to use */
+ if (opts.standard == COMPILER_GMQCC) {
+ operators = c_operators;
+ operator_count = c_operator_count;
+ } else if (opts.standard == COMPILER_FTEQCC) {
+ operators = fte_operators;
+ operator_count = fte_operator_count;
+ } else {
+ operators = qcc_operators;
+ operator_count = qcc_operator_count;
+ }
+
+ if (operators == fte_operators) {
+ /* fix ternary? */
+ if (OPTS_FLAG(CORRECT_TERNARY)) {
+ oper_info *newops;
+ if (operators[operator_count-2].id != opid1(',') ||
+ operators[operator_count-1].id != opid2(':','?'))
+ {
+ con_err("internal error: operator precedence table wasn't updated correctly!\n");
+ exit(1);
+ }
+ operators_free = true;
+ newops = (oper_info*)mem_a(sizeof(operators[0]) * operator_count);
+ memcpy(newops, operators, sizeof(operators[0]) * operator_count);
+ memcpy(&newops[operator_count-2], &operators[operator_count-1], sizeof(newops[0]));
+ memcpy(&newops[operator_count-1], &operators[operator_count-2], sizeof(newops[0]));
+ newops[operator_count-2].prec = newops[operator_count-1].prec+1;
+ operators = newops;
+ }
+ }
+
+ if (opts.dump) {
+ for (itr = 0; itr < COUNT_FLAGS; ++itr)
+ con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
+ for (itr = 0; itr < COUNT_WARNINGS; ++itr)
+ con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
+
+ con_out("output = %s\n", opts.output);
+ con_out("optimization level = %d\n", opts.O);
+ con_out("standard = %i\n", opts.standard);
+ }
+
+ if (opts.pp_only) {
+ if (opts_output_wasset) {
+ outfile = file_open(opts.output, "wb");
+ if (!outfile) {
+ con_err("failed to open `%s` for writing\n", opts.output);
+ retval = 1;
+ goto cleanup;
+ }
+ }
+ else {
+ outfile = con_default_out();
+ }
+ }
+
+ if (!opts.pp_only) {
+ if (!parser_init()) {
+ con_err("failed to initialize parser\n");
+ retval = 1;
+ goto cleanup;
+ }
+ }
+
+ if (opts.pp_only || OPTS_FLAG(FTEPP)) {
+ if (!ftepp_init()) {
+ con_err("failed to initialize parser\n");
+ retval = 1;
+ goto cleanup;