src/c2html.c

Sun, 19 Apr 2015 10:48:00 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 19 Apr 2015 10:48:00 +0200
changeset 24
e43dee5892f4
parent 23
f44a185b678b
child 25
f82aa7afe872
permissions
-rw-r--r--

improved code structure and added option for disabling line numbers

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2015 Mike Becker. All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  *
    28  */
    29 #include "c2html.h"
    31 inputfile_t *inputfilebuffer(size_t capacity) {
    32     inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t));
    33     inputfile->lines = (char**) malloc(capacity * sizeof(char*));
    34     inputfile->capacity = capacity;
    35     inputfile->count = 0;
    36     inputfile->maxlinewidth = 0;
    38     return inputfile;
    39 }
    41 void addline(inputfile_t *inputfile, char* line, size_t width) {
    42     char *l = (char*) malloc(width+1);
    43     memcpy(l, line, width);
    44     l[width] = 0;
    45     if (inputfile->count >= inputfile->capacity) {
    46         inputfile->capacity <<= 1;
    47         inputfile->lines = realloc(inputfile->lines, inputfile->capacity);
    48     }
    49     inputfile->lines[inputfile->count] = l;
    50     inputfile->maxlinewidth =
    51         width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth;
    52     inputfile->count++;
    53 }
    55 void freeinputfilebuffer(inputfile_t *inputfile) {
    56     for (int i = 0 ; i < inputfile->count ; i++) {
    57         free(inputfile->lines[i]);
    58     }
    59     free(inputfile->lines);
    60     free(inputfile);
    61 }
    63 inputfile_t *readinput(char *filename) {
    65     int fd = open(filename, O_RDONLY);
    66     if (fd == -1) return NULL;
    68     inputfile_t *inputfile = inputfilebuffer(512);
    70     char buf[INPUTBUF_SIZE];
    71     ssize_t r;
    73     size_t maxlinewidth = 256;
    74     char *line = (char*) malloc(maxlinewidth);
    75     size_t col = 0;
    77     while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) {
    78         for (size_t i = 0 ; i < r ; i++) {
    79             if (col >= maxlinewidth-4) {
    80                 maxlinewidth <<= 1;
    81                 line = realloc(line, maxlinewidth);
    82             }
    84             if (buf[i] == '\n') {
    85                 line[col++] = '\n';
    86                 line[col] = 0;
    87                 addline(inputfile, line, col);
    88                 col = 0;
    89             } else {
    90                 line[col++] = buf[i];
    91             }
    92         }
    93     }
    95     free(line);
    97     close(fd);
    99     return inputfile;
   100 }
   102 void printhelp() {
   103     printf("Formats source code using HTML.\n\nUsage:\n"
   104         "  c2html [Options] FILE\n\n"
   105         " Options:\n"
   106         "  -h                    Prints this help message\n"
   107         "  -j                    Highlight Java instead of C source code\n"
   108         "  -o <output>           Output file (stdout, if not specified)\n"
   109         "  -H <header>           Prepend header file\n"
   110         "  -F <footer>           Append footer file\n"
   111         "  -p                    Disable highlighting (plain text)\n"
   112         "  -l                    Disable line numbers\n"
   113         "\n");
   116 }
   118 int lnint(size_t lnc) {
   119     int w = 1, p = 1;
   120     while ((p*=10) < lnc) w++;
   121     return w;
   122 }
   124 int copyfile(char *filename, FILE *dest) {
   125     if (!filename) {
   126         return 0;
   127     }
   129     FILE *src = fopen(filename, "r");
   130     if (src) {
   131         char buf[4096];
   132         int r;
   133         while ((r = fread(buf, 1, 4096, src)) > 0) {
   134             fwrite(buf, 1, r, dest);
   135         }
   136         fclose(src);
   137         return 0;
   138     } else {
   139         return -1;
   140     }
   141 }
   143 #define WRITECONST(stream, out, cstr) out(cstr, 1, sizeof(cstr)-1, stream)
   144 int formatfile(
   145         highlighter_t *highlighter,
   146         inputfile_t *in,
   147         fmt_write_func out,
   148         void *stream,
   149         _Bool showln) {
   150     // formats an input file and writes the result to out
   152     char *line = malloc(in->maxlinewidth*64);
   153     if(!line) {
   154         return 1;
   155     }
   156     WRITECONST(stream, out, "<pre>\n");
   158     int lnw = lnint(in->count);
   159     for (int i = 0 ; i < in->count ; i++) {
   160         char *ln = line;
   161         if (highlighter) {
   162             highlighter->parser(in->lines[i], line, highlighter);
   163         } else {
   164             ln = in->lines[i];
   165         }
   167         // write line number
   168         if (showln) {
   169             WRITECONST(stream, out, "<span class=\"c2html-lineno\">");
   170             char lnbuf[16];
   171             int len = snprintf(lnbuf, 16, "%*d ", lnw, i+1);
   172             out(lnbuf, 1, len, stream);
   173             WRITECONST(stream, out, "</span> ");
   174         }
   176         // write formated (or plain) code line
   177         out(ln, 1, strlen(ln), stream);
   178     }
   180     WRITECONST(stream, out, "</pre>\n");
   181     free(line);
   182     return 0;
   183 }
   185 void init_c_highlighter(highlighter_t *highlighter) {
   186     memset(highlighter, 0, sizeof(highlighter_t));
   187     highlighter->isdirective = iscdirective;
   188     highlighter->istype = isctype;
   189     highlighter->keywords = ckeywords;
   190     highlighter->parser = cparseline;
   191 }
   193 void init_java_highlighter(highlighter_t *highlighter) {
   194     memset(highlighter, 0, sizeof(highlighter_t));
   195     highlighter->isdirective = isjdirective;
   196     highlighter->istype = isjtype;
   197     highlighter->keywords = jkeywords;
   198     highlighter->parser = jparseline;
   199 }
   201 int main(int argc, char** argv) {
   202     int retcode = EXIT_SUCCESS;
   204     settings_t settings;
   205     memset(&settings, 0, sizeof(settings));
   206     settings.highlight = 1;
   207     settings.showlinenumbers = 1;
   209     int lang = C2HTML_C;
   211     char optc;
   212     while ((optc = getopt(argc, argv, "hljo:pH:F:")) != -1) {
   213         switch (optc) {
   214             case 'o':
   215                 if (!(optarg[0] == '-' && optarg[1] == 0)) {
   216                     settings.outfilename = optarg;
   217                 }
   218                 break;
   219             case 'F':
   220                 settings.footerfile = optarg;
   221                 break;
   222             case 'H':
   223                 settings.headerfile = optarg;
   224                 break;
   225             case 'j':
   226                 lang = C2HTML_JAVA;
   227                 break;
   228             case 'p':
   229                 settings.highlight = 0;
   230                 break;
   231             case 'l':
   232                 settings.showlinenumbers = 0;
   233                 break;
   234             case 'h':
   235                 printhelp();
   236                 return 0;
   237             default:
   238                 return 1;
   239         }
   240     }
   242     if (optind != argc-1) {
   243         printhelp();
   244         return 1;
   245     } else {
   246         settings.infilename = argv[optind];
   247         FILE *fout;
   248         if (settings.outfilename) {
   249             fout = fopen(settings.outfilename, "w");
   250             if (!fout) {
   251                 perror("Error opening output file");
   252                 return -1;
   253             }
   254         } else {
   255             fout = stdout;
   256         }
   258         if (copyfile(settings.headerfile, fout)) {
   259             perror("Error opening header file");
   260             retcode = -1;
   261             goto prog_end;
   262         }
   264         highlighter_t highlighter;
   265         highlighter_t *hptr = &highlighter;
   266         switch (lang) {
   267             case C2HTML_C:
   268                 init_c_highlighter(&highlighter);
   269                 break;
   270             case C2HTML_JAVA:
   271                 init_java_highlighter(&highlighter);
   272                 break;
   273             default:
   274                 hptr = NULL;
   275                 break;
   276         }
   277         if (!settings.highlight) {
   278             hptr = NULL;
   279         }
   281         inputfile_t *inputfile = readinput(settings.infilename);
   282         if (inputfile) {
   283             formatfile(
   284                     hptr,
   285                     inputfile,
   286                     (fmt_write_func)fwrite,
   287                     fout,
   288                     settings.showlinenumbers);
   289         } else {
   290             perror("Error opening input file");
   291             retcode = -1;
   292         }
   294         if (copyfile(settings.footerfile, fout)) {
   295             perror("Error opening footer file");
   296             retcode = -1;
   297         }
   299         prog_end:        
   300         if (fout != stdout) {
   301             fclose(fout);
   302         }
   304         return retcode;
   305     }
   306 }

mercurial