changes merged

Wed, 10 Jul 2013 13:54:15 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 10 Jul 2013 13:54:15 +0200
changeset 13
fe74bf2d5f27
parent 12
7ce5c4b51959 (diff)
parent 10
925172e535a9 (current diff)
child 14
b33629bf4b58

changes merged

src/c2html.c file | annotate | diff | comparison | revisions
     1.1 --- a/Makefile	Fri Jun 21 13:32:31 2013 +0200
     1.2 +++ b/Makefile	Wed Jul 10 13:54:15 2013 +0200
     1.3 @@ -37,7 +37,7 @@
     1.4  	$(MKDIR) build
     1.5  	
     1.6  test: compile
     1.7 -	./build/$(BIN) src/c2html.c > build/body.html
     1.8 +	./build/$(BIN) $(ARGS) src/c2html.c > build/body.html
     1.9  	cat test/header.html build/body.html test/footer.html > build/code.html
    1.10  	
    1.11  clean:
     2.1 --- a/src/c2html.c	Fri Jun 21 13:32:31 2013 +0200
     2.2 +++ b/src/c2html.c	Wed Jul 10 13:54:15 2013 +0200
     2.3 @@ -47,6 +47,11 @@
     2.4    "switch", "typedef", "union", "unsigned", "void", "volatile", "while", NULL
     2.5  };
     2.6  
     2.7 +typedef struct {
     2.8 +  char* outfilename;
     2.9 +  char* infilename;
    2.10 +  int highlight;
    2.11 +} settings_t;
    2.12  
    2.13  typedef struct {
    2.14    size_t count;
    2.15 @@ -308,7 +313,10 @@
    2.16  
    2.17  void printhelp() {
    2.18    printf("Formats source code using HTML.\n\nUsage:\n"
    2.19 -      "  c2html [FILE...]"
    2.20 +      "  c2html [Options] FILE\n\n"
    2.21 +      " Options:\n"
    2.22 +      "  -h                    Prints this help message\n"
    2.23 +      "  -o <output>           Output file (if not specified, stdout is used)\n"
    2.24        "\n");
    2.25    
    2.26    
    2.27 @@ -322,23 +330,63 @@
    2.28  
    2.29  int main(int argc, char** argv) {
    2.30    
    2.31 -  if (argc == 1) {
    2.32 +  settings_t settings;
    2.33 +  settings.outfilename = NULL;
    2.34 +  settings.highlight = 1;
    2.35 +  
    2.36 +  char optc;
    2.37 +  while ((optc = getopt(argc, argv, "ho:p")) != -1) {
    2.38 +    switch (optc) {
    2.39 +      case 'o':
    2.40 +        if (!(optarg[0] == '-' && optarg[1] == 0)) {
    2.41 +          settings.outfilename = optarg;
    2.42 +        }
    2.43 +        break;
    2.44 +      case 'p':
    2.45 +        settings.highlight = 0;
    2.46 +        break;
    2.47 +      case 'h':
    2.48 +        printhelp();
    2.49 +        return 0;
    2.50 +      default:
    2.51 +        return 1;
    2.52 +    }
    2.53 +  }
    2.54 +
    2.55 +  if (optind != argc-1) {
    2.56      printhelp();
    2.57 -    return 0;
    2.58 +    return 1;
    2.59    } else {
    2.60 +    settings.infilename = argv[optind];
    2.61      
    2.62 -    inputfile_t *inputfile = readinput(argv[1]);
    2.63 +    inputfile_t *inputfile = readinput(settings.infilename);
    2.64      if (inputfile) {
    2.65 -      printf("<pre>\n");
    2.66 -      char *line = (char*) malloc(inputfile->maxlinewidth*64);
    2.67 +      FILE *fout;
    2.68 +      if (settings.outfilename) {
    2.69 +        fout = fopen(settings.outfilename, "w");
    2.70 +      } else {
    2.71 +        fout = stdout;
    2.72 +      }
    2.73 +      fprintf(fout, "<pre>\n");
    2.74 +      char *line = (char*) malloc(inputfile->maxlinewidth
    2.75 +          * (settings.highlight?64:0));
    2.76        int lnw = lnint(inputfile->count);
    2.77        for (int i = 0 ; i < inputfile->count ; i++) {
    2.78 -        parseline(inputfile->lines[i], line);
    2.79 -        printf("<span class=\"c2html-lineno\">%*d:</span> %s",
    2.80 +        if (settings.highlight) {
    2.81 +          parseline(inputfile->lines[i], line);
    2.82 +        } else {
    2.83 +          line = inputfile->lines[i];
    2.84 +        }
    2.85 +        fprintf(fout, "<span class=\"c2html-lineno\">%*d:</span> %s",
    2.86              lnw, i+1, line);
    2.87        }
    2.88        free(line);
    2.89 -      printf("</pre>\n");
    2.90 +      fprintf(fout, "</pre>\n");
    2.91 +      
    2.92 +      if (fout != stdout) {
    2.93 +        fclose(fout);
    2.94 +      }
    2.95 +      
    2.96        freeinputfilebuffer(inputfile);
    2.97      }
    2.98    

mercurial