# HG changeset patch # User Mike Becker # Date 1373457255 -7200 # Node ID fe74bf2d5f27c76b1ff2ddb0203644b8f7e32342 # Parent 7ce5c4b51959d729b34b78955338865f82176ac5# Parent 925172e535a9fffaaf4e341cb7ee276698062af0 changes merged diff -r 925172e535a9 -r fe74bf2d5f27 Makefile --- a/Makefile Fri Jun 21 13:32:31 2013 +0200 +++ b/Makefile Wed Jul 10 13:54:15 2013 +0200 @@ -37,7 +37,7 @@ $(MKDIR) build test: compile - ./build/$(BIN) src/c2html.c > build/body.html + ./build/$(BIN) $(ARGS) src/c2html.c > build/body.html cat test/header.html build/body.html test/footer.html > build/code.html clean: diff -r 925172e535a9 -r fe74bf2d5f27 src/c2html.c --- a/src/c2html.c Fri Jun 21 13:32:31 2013 +0200 +++ b/src/c2html.c Wed Jul 10 13:54:15 2013 +0200 @@ -47,6 +47,11 @@ "switch", "typedef", "union", "unsigned", "void", "volatile", "while", NULL }; +typedef struct { + char* outfilename; + char* infilename; + int highlight; +} settings_t; typedef struct { size_t count; @@ -308,7 +313,10 @@ void printhelp() { printf("Formats source code using HTML.\n\nUsage:\n" - " c2html [FILE...]" + " c2html [Options] FILE\n\n" + " Options:\n" + " -h Prints this help message\n" + " -o Output file (if not specified, stdout is used)\n" "\n"); @@ -322,23 +330,63 @@ int main(int argc, char** argv) { - if (argc == 1) { + settings_t settings; + settings.outfilename = NULL; + settings.highlight = 1; + + char optc; + while ((optc = getopt(argc, argv, "ho:p")) != -1) { + switch (optc) { + case 'o': + if (!(optarg[0] == '-' && optarg[1] == 0)) { + settings.outfilename = optarg; + } + break; + case 'p': + settings.highlight = 0; + break; + case 'h': + printhelp(); + return 0; + default: + return 1; + } + } + + if (optind != argc-1) { printhelp(); - return 0; + return 1; } else { + settings.infilename = argv[optind]; - inputfile_t *inputfile = readinput(argv[1]); + inputfile_t *inputfile = readinput(settings.infilename); if (inputfile) { - printf("
\n");
-      char *line = (char*) malloc(inputfile->maxlinewidth*64);
+      FILE *fout;
+      if (settings.outfilename) {
+        fout = fopen(settings.outfilename, "w");
+      } else {
+        fout = stdout;
+      }
+      fprintf(fout, "
\n");
+      char *line = (char*) malloc(inputfile->maxlinewidth
+          * (settings.highlight?64:0));
       int lnw = lnint(inputfile->count);
       for (int i = 0 ; i < inputfile->count ; i++) {
-        parseline(inputfile->lines[i], line);
-        printf("%*d: %s",
+        if (settings.highlight) {
+          parseline(inputfile->lines[i], line);
+        } else {
+          line = inputfile->lines[i];
+        }
+        fprintf(fout, "%*d: %s",
             lnw, i+1, line);
       }
       free(line);
-      printf("
\n"); + fprintf(fout, "
\n"); + + if (fout != stdout) { + fclose(fout); + } + freeinputfilebuffer(inputfile); }