diff options
author | David Schleef <ds@schleef.org> | 2009-09-19 11:53:39 -0700 |
---|---|---|
committer | David Schleef <ds@schleef.org> | 2009-09-19 11:53:39 -0700 |
commit | 9a308fa498f52603b7719fd2434f6842249f4c42 (patch) | |
tree | eb75016e1381af6ccdc8f8a9dce74b350511c6f2 /tools | |
parent | ec985edfe455584b7797eba2f7182b8e66389e2c (diff) |
orcc: Add handling of command line options
Diffstat (limited to 'tools')
-rw-r--r-- | tools/orcc.c | 212 |
1 files changed, 143 insertions, 69 deletions
diff --git a/tools/orcc.c b/tools/orcc.c index b7006fd..33f8f98 100644 --- a/tools/orcc.c +++ b/tools/orcc.c @@ -1,10 +1,13 @@ +#include "config.h" + #include <orc/orc.h> #include <orc-test/orctest.h> #include <orc/orcparse.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> static char * read_file (const char *filename); void output_code (OrcProgram *p, FILE *output); @@ -15,6 +18,34 @@ void output_code_no_orc (OrcProgram *p, FILE *output); static void print_defines (FILE *output); static void print_exec_header (FILE *output); +int verbose = 0; + +enum { + MODE_IMPL, + MODE_HEADER, + MODE_TEST +}; +int mode = MODE_IMPL; + +void help (void) +{ + printf("Usage:\n"); + printf(" orcc [OPTION...] INPUT_FILE\n"); + printf("\n"); + printf("Help Options:\n"); + printf(" -?, --help Show help options\n"); + printf("\n"); + printf("Application Options:\n"); + printf(" -v, --verbose Output more information\n"); + printf(" -o, --output=FILE Write output to FILE\n"); + printf(" --implementation Produce C code implementing functions\n"); + printf(" --header Produce C header for functions\n"); + printf(" --test Produce test code for functions\n"); + printf("\n"); + + exit (0); +} + int main (int argc, char *argv[]) { @@ -22,94 +53,137 @@ main (int argc, char *argv[]) int n; int i; OrcProgram **programs; - const char *filename = "test.orc"; + char *output_file = NULL; + char *input_file = NULL; FILE *output; orc_init (); orc_test_init (); - if (argc >= 2) { - filename = argv[1]; + for(i=1;i<argc;i++) { + if (strcmp(argv[i], "--header") == 0) { + mode = MODE_HEADER; + } else if (strcmp(argv[i], "--implementation") == 0) { + mode = MODE_IMPL; + } else if (strcmp(argv[i], "--test") == 0) { + mode = MODE_TEST; + } else if (strcmp (argv[i], "--output") == 0 || + strcmp(argv[i], "-o") == 0) { + if (i+1 < argc) { + output_file = argv[i+1]; + i++; + } else { + help(); + } + } else if (strcmp(argv[i], "--help") == 0 || + strcmp(argv[i], "-h") == 0) { + help (); + } else if (strcmp(argv[i], "--verbose") == 0 || + strcmp(argv[i], "-v") == 0) { + verbose = 1; + } else if (strcmp(argv[i], "--version") == 0) { + printf("Orc Compiler " PACKAGE_VERSION "\n"); + exit (0); + } else if (strncmp(argv[i], "-", 1) == 0) { + printf("Unknown option: %s\n", argv[i]); + exit (1); + } else { + if (input_file == NULL) { + input_file = argv[i]; + } else { + printf("More than one input file specified: %s\n", argv[i]); + exit (1); + } + } + } + + if (input_file == NULL) { + printf("No input file specified\n"); + exit (1); } - code = read_file (filename); + + if (output_file == NULL) { + if (mode == MODE_IMPL) { + output_file = "out.c"; + } else if (mode == MODE_HEADER) { + output_file = "out.h"; + } else if (mode == MODE_TEST) { + output_file = "out_test.c"; + } + } + + code = read_file (input_file); if (!code) { - printf("orcc <file.orc>\n"); + printf("Could not read input file: %s\n", input_file); exit(1); } n = orc_parse (code, &programs); - output = fopen ("out.c", "w"); - fprintf(output, "\n"); - fprintf(output, "/* autogenerated from %s */\n", filename); - fprintf(output, "\n"); - fprintf(output, "#ifndef DISABLE_ORC\n"); - fprintf(output, "#include <orc/orc.h>\n"); - fprintf(output, "#else\n"); - fprintf(output, "#include <stdint.h>\n"); - print_exec_header (output); - fprintf(output, "#endif\n"); - fprintf(output, "\n"); - print_defines (output); - fprintf(output, "\n"); - for(i=0;i<n;i++){ - output_code (programs[i], output); + output = fopen (output_file, "w"); + if (!output) { + printf("Could not write output file: %s\n", output_file); + exit(1); } - fclose (output); - - output = fopen ("out.h", "w"); - - fprintf(output, "\n"); - fprintf(output, "/* autogenerated from %s */\n", filename); fprintf(output, "\n"); - fprintf(output, "#ifndef _ORC_OUT_H_\n"); - fprintf(output, "#define _ORC_OUT_H_\n"); + fprintf(output, "/* autogenerated from %s */\n", input_file); fprintf(output, "\n"); - for(i=0;i<n;i++){ - output_code_header (programs[i], output); - } - fprintf(output, "\n"); - fprintf(output, "#endif\n"); - fprintf(output, "\n"); - - fclose (output); - /* test program */ - output = fopen ("test_out.c", "w"); - - fprintf(output, "\n"); - fprintf(output, "/* autogenerated from %s */\n", filename); - fprintf(output, "\n"); - fprintf(output, "#include <orc/orc.h>\n"); - fprintf(output, "#include <orc-test/orctest.h>\n"); - fprintf(output, "#include <stdio.h>\n"); - fprintf(output, "#include <stdlib.h>\n"); - fprintf(output, "\n"); - print_defines (output); - fprintf(output, "\n"); - for(i=0;i<n;i++){ - fprintf(output, "/* %s */\n", programs[i]->name); - output_code_backup (programs[i], output); - } - fprintf(output, "\n"); - fprintf(output, "int\n"); - fprintf(output, "main (int argc, char *argv[])\n"); - fprintf(output, "{\n"); - fprintf(output, " int error = FALSE;\n"); - fprintf(output, "\n"); - fprintf(output, " orc_test_init ();\n"); - fprintf(output, "\n"); - for(i=0;i<n;i++){ - output_code_test (programs[i], output); + if (mode == MODE_IMPL) { + fprintf(output, "#ifndef DISABLE_ORC\n"); + fprintf(output, "#include <orc/orc.h>\n"); + fprintf(output, "#else\n"); + fprintf(output, "#include <stdint.h>\n"); + print_exec_header (output); + fprintf(output, "#endif\n"); + fprintf(output, "\n"); + print_defines (output); + fprintf(output, "\n"); + for(i=0;i<n;i++){ + output_code (programs[i], output); + } + } else if (mode == MODE_HEADER) { + fprintf(output, "#ifndef _ORC_OUT_H_\n"); + fprintf(output, "#define _ORC_OUT_H_\n"); + fprintf(output, "\n"); + for(i=0;i<n;i++){ + output_code_header (programs[i], output); + } + fprintf(output, "\n"); + fprintf(output, "#endif\n"); + fprintf(output, "\n"); + } else if (mode == MODE_TEST) { + fprintf(output, "#include <orc/orc.h>\n"); + fprintf(output, "#include <orc-test/orctest.h>\n"); + fprintf(output, "#include <stdio.h>\n"); + fprintf(output, "#include <stdlib.h>\n"); + fprintf(output, "\n"); + print_defines (output); + fprintf(output, "\n"); + for(i=0;i<n;i++){ + fprintf(output, "/* %s */\n", programs[i]->name); + output_code_backup (programs[i], output); + } + fprintf(output, "\n"); + fprintf(output, "int\n"); + fprintf(output, "main (int argc, char *argv[])\n"); + fprintf(output, "{\n"); + fprintf(output, " int error = FALSE;\n"); + fprintf(output, "\n"); + fprintf(output, " orc_test_init ();\n"); + fprintf(output, "\n"); + for(i=0;i<n;i++){ + output_code_test (programs[i], output); + } + fprintf(output, "\n"); + fprintf(output, " if (error) {\n"); + fprintf(output, " return 1;\n"); + fprintf(output, " };\n"); + fprintf(output, " return 0;\n"); + fprintf(output, "}\n"); } - fprintf(output, "\n"); - fprintf(output, " if (error) {\n"); - fprintf(output, " return 1;\n"); - fprintf(output, " };\n"); - fprintf(output, " return 0;\n"); - fprintf(output, "}\n"); fclose (output); |