X-Git-Url: https://git.xonotic.org/?a=blobdiff_plain;f=main.c;h=54c64b639ca4ae0103ad60ce52e2cd01f5122d1b;hb=75812d486ae21270e573300c3a5f6a0458bf0104;hp=0699c1897b96cd6cf0849dd1a7865d0b36f9bcdd;hpb=c3f08b289bff35de3f5988fa9131bb5bf2dc6001;p=xonotic%2Fgmqcc.git diff --git a/main.c b/main.c index 0699c18..54c64b6 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,6 @@ /* - * Copyright (C) 2012 - * Dale Weiler + * Copyright (C) 2012 + * Dale Weiler * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in @@ -20,55 +20,88 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#include -#include -#include #include "gmqcc.h" -//typedef int foo; +static const char *output = "progs.dat"; +static const char *input = NULL; -int usage(const char *name) { - printf("Usage: %s -f infile -o outfile\n", name); - return 0; -} +#define OptReq(opt, body) \ + case opt: \ + if (argv[0][2]) argarg = argv[0]+2; \ + else { \ + if (argc < 2) { \ + printf("option -%c requires an argument\n", opt); \ + exit(1); \ + } \ + argarg = argv[1]; \ + --argc; \ + ++argv; \ + } \ + do { body } while (0); \ + break; + +#define LongReq(opt, body) \ + if (!strcmp(argv[0], opt)) { \ + if (argc < 2) { \ + printf("option " opt " requires an argument\n"); \ + exit(1); \ + } \ + argarg = argv[1]; \ + --argc; \ + ++argv; \ + do { body } while (0); \ + break; \ + } else if (!strncmp(argv[0], opt "=", sizeof(opt "="))) \ + { \ + argarg = argv[0] + sizeof(opt "="); \ + do { body } while (0); \ + break; \ + } +bool parser_compile(const char *filename, const char *datfile); int main(int argc, char **argv) { - const char *ofile = NULL; - const char *ifile = NULL; - int i; - if (argc <= 2) { - return usage(*argv); - } - - for (i=0; i < argc; i++) { - if (argc != i + 1) { - switch(argv[i][0]) { - case '-': - switch(argv[i][1]) { - case 'f': ifile = argv[i+1]; break; - case 'o': ofile = argv[i+1]; break; - } - break; - } - } - } - - if (!ofile || !ifile) { - return usage(*argv); - } - - printf("ifile: %s\n", ifile); - printf("ofile: %s\n", ofile); - - /* Open file */ - FILE *fp = fopen(ifile, "r"); - if (!fp) { - fclose(fp); - return error(ERROR_COMPILER, "Source file: %s not found\n", ifile); - } else { - struct lex_file *lex = lex_open(fp); - parse (lex); - lex_close(lex); - } - return 0; + const char *argarg; + char opt; + + util_debug("COM", "starting ...\n"); + + --argc; + ++argv; + while (argc > 0) { + if (argv[0][0] == '-') { + opt = argv[0][1]; + switch (opt) + { + OptReq('o', output = argarg; ); + case '-': + LongReq("--output", output = argarg; ); + default: + printf("Unrecognized option: %s\n", argv[0]); + break; + } + } + else + { + if (input) { + printf("Onlyh 1 input file allowed\n"); + exit(1); + } + input = argv[0]; + } + --argc; + ++argv; + } + + if (!input) { + printf("must specify an input file\n"); + } + + if (!parser_compile(input, output)) { + printf("There were compile errors\n"); + } + + util_debug("COM", "cleaning ...\n"); + + util_meminfo(); + return 0; }