src/c2html.c

changeset 22
f463693b5eeb
parent 21
537aec525835
child 23
f44a185b678b
     1.1 --- a/src/c2html.c	Thu Jan 23 09:19:37 2014 +0100
     1.2 +++ b/src/c2html.c	Thu Jan 23 14:17:06 2014 +0100
     1.3 @@ -1,56 +1,6 @@
     1.4 -/*
     1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 - *
     1.7 - * Copyright 2014 Mike Becker. All rights reserved.
     1.8 - *
     1.9 - * Redistribution and use in source and binary forms, with or without
    1.10 - * modification, are permitted provided that the following conditions are met:
    1.11 - *
    1.12 - *   1. Redistributions of source code must retain the above copyright
    1.13 - *      notice, this list of conditions and the following disclaimer.
    1.14 - *
    1.15 - *   2. Redistributions in binary form must reproduce the above copyright
    1.16 - *      notice, this list of conditions and the following disclaimer in the
    1.17 - *      documentation and/or other materials provided with the distribution.
    1.18 - *
    1.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.29 - * POSSIBILITY OF SUCH DAMAGE.
    1.30 - *
    1.31 - */
    1.32 +#include <errno.h>
    1.33  
    1.34 -#include <stdio.h>
    1.35 -#include <stdlib.h>
    1.36 -#include <string.h>
    1.37 -#include <fcntl.h>
    1.38 -#include <unistd.h>
    1.39 -#include <ctype.h>
    1.40 -
    1.41 -#include "javacodegen.h"
    1.42 -#include "ccodegen.h"
    1.43 -
    1.44 -#define INPUTBUF_SIZE 2048
    1.45 -
    1.46 -typedef struct {
    1.47 -    char* outfilename;
    1.48 -    char* infilename;
    1.49 -    int highlight;
    1.50 -} settings_t;
    1.51 -
    1.52 -typedef struct {
    1.53 -    size_t count;
    1.54 -    size_t capacity;
    1.55 -    size_t maxlinewidth;
    1.56 -    char** lines;
    1.57 -} inputfile_t;
    1.58 +#include "c2html.h"
    1.59  
    1.60  inputfile_t *inputfilebuffer(size_t capacity) {
    1.61      inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t));
    1.62 @@ -130,6 +80,8 @@
    1.63          "  -h                    Prints this help message\n"
    1.64          "  -j                    Highlight Java instead of C source code\n"
    1.65          "  -o <output>           Output file (stdout, if not specified)\n"
    1.66 +        "  -H <header>           Prepend header file\n"
    1.67 +        "  -F <footer>           Append footer file\n"
    1.68          "  -p                    Disable highlighting (plain text)\n"
    1.69          "\n");
    1.70  
    1.71 @@ -142,11 +94,32 @@
    1.72      return w;
    1.73  }
    1.74  
    1.75 +int copyfile(char *filename, FILE *dest) {
    1.76 +    if (!filename) {
    1.77 +        return 0;
    1.78 +    }
    1.79 +    
    1.80 +    FILE *src = fopen(filename, "r");
    1.81 +    if (src) {
    1.82 +        char buf[4096];
    1.83 +        int r;
    1.84 +        while ((r = fread(buf, 1, 4096, src)) > 0) {
    1.85 +            fwrite(buf, 1, r, dest);
    1.86 +        }
    1.87 +        fclose(src);
    1.88 +        return 0;
    1.89 +    } else {
    1.90 +        return errno;
    1.91 +    }
    1.92 +}
    1.93 +
    1.94  int main(int argc, char** argv) {
    1.95 +    int retcode = EXIT_SUCCESS;
    1.96 +    
    1.97      settings_t settings;
    1.98 -    settings.outfilename = NULL;
    1.99 +    memset(&settings, 0, sizeof(settings));
   1.100      settings.highlight = 1;
   1.101 -
   1.102 +    
   1.103      highlighter_t highlighter;
   1.104      memset(&highlighter, 0, sizeof(highlighter));
   1.105      highlighter.isdirective = iscdirective;
   1.106 @@ -155,13 +128,19 @@
   1.107      highlighter.parser = cparseline;
   1.108  
   1.109      char optc;
   1.110 -    while ((optc = getopt(argc, argv, "hjo:p")) != -1) {
   1.111 +    while ((optc = getopt(argc, argv, "hjo:pH:F:")) != -1) {
   1.112          switch (optc) {
   1.113              case 'o':
   1.114                  if (!(optarg[0] == '-' && optarg[1] == 0)) {
   1.115                      settings.outfilename = optarg;
   1.116                  }
   1.117                  break;
   1.118 +            case 'F':
   1.119 +                settings.footerfile = optarg;
   1.120 +                break;
   1.121 +            case 'H':
   1.122 +                settings.headerfile = optarg;
   1.123 +                break;
   1.124              case 'j':
   1.125                  highlighter.isdirective = isjdirective;
   1.126                  highlighter.istype = isjtype;
   1.127 @@ -184,21 +163,31 @@
   1.128          return 1;
   1.129      } else {
   1.130          settings.infilename = argv[optind];
   1.131 +        FILE *fout;
   1.132 +        if (settings.outfilename) {
   1.133 +            fout = fopen(settings.outfilename, "w");
   1.134 +            if (!fout) {
   1.135 +                perror("Error opening output file");
   1.136 +                return errno;
   1.137 +            }
   1.138 +        } else {
   1.139 +            fout = stdout;
   1.140 +        }
   1.141 +        
   1.142 +        if (copyfile(settings.headerfile, fout)) {
   1.143 +            perror("Error opening header file");
   1.144 +            retcode = errno;
   1.145 +            goto prog_end;
   1.146 +        }
   1.147  
   1.148          inputfile_t *inputfile = readinput(settings.infilename);
   1.149          if (inputfile) {
   1.150 -            FILE *fout;
   1.151              char *line;
   1.152              if (settings.highlight) {
   1.153                  line = (char*) malloc(inputfile->maxlinewidth*64);
   1.154              } else {
   1.155                  line = NULL;
   1.156              }
   1.157 -            if (settings.outfilename) {
   1.158 -                fout = fopen(settings.outfilename, "w");
   1.159 -            } else {
   1.160 -                fout = stdout;
   1.161 -            }
   1.162              fprintf(fout, "<pre>\n");
   1.163              int lnw = lnint(inputfile->count);
   1.164              for (int i = 0 ; i < inputfile->count ; i++) {
   1.165 @@ -215,14 +204,23 @@
   1.166              }
   1.167              fprintf(fout, "</pre>\n");
   1.168  
   1.169 -            if (fout != stdout) {
   1.170 -                fclose(fout);
   1.171 +            freeinputfilebuffer(inputfile);
   1.172 +            
   1.173 +            if (copyfile(settings.footerfile, fout)) {
   1.174 +                perror("Error opening footer file");
   1.175 +                retcode = errno;
   1.176              }
   1.177 -
   1.178 -            freeinputfilebuffer(inputfile);
   1.179 +        } else {
   1.180 +            perror("Error opening input file");
   1.181 +            retcode = errno;
   1.182 +        }
   1.183 +        
   1.184 +        prog_end:        
   1.185 +        if (fout != stdout) {
   1.186 +            fclose(fout);
   1.187          }
   1.188  
   1.189 -        return 0;
   1.190 +        return retcode;
   1.191      }
   1.192  }
   1.193  

mercurial