src/c2html.c

Fri, 04 Mar 2016 15:02:22 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 04 Mar 2016 15:02:22 +0100
changeset 36
be60c22cddfe
parent 35
abaf2489c549
child 37
1a67185e5496
permissions
-rw-r--r--

fixed possible naming conflicts with is.* functions

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2016 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,
    48             sizeof(char*)*inputfile->capacity);
    49     }
    50     inputfile->lines[inputfile->count] = l;
    51     inputfile->maxlinewidth =
    52         width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth;
    53     inputfile->count++;
    54 }
    56 void freeinputfilebuffer(inputfile_t *inputfile) {
    57     for (int i = 0 ; i < inputfile->count ; i++) {
    58         free(inputfile->lines[i]);
    59     }
    60     free(inputfile->lines);
    61     free(inputfile);
    62 }
    64 inputfile_t *readinput(char *filename) {
    66     int fd = open(filename, O_RDONLY);
    67     if (fd == -1) return NULL;
    69     inputfile_t *inputfile = inputfilebuffer(512);
    71     char buf[INPUTBUF_SIZE];
    72     ssize_t r;
    74     size_t maxlinewidth = 256;
    75     char *line = (char*) malloc(maxlinewidth);
    76     size_t col = 0;
    78     while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) {
    79         for (size_t i = 0 ; i < r ; i++) {
    80             if (col >= maxlinewidth-4) {
    81                 maxlinewidth <<= 1;
    82                 line = realloc(line, maxlinewidth);
    83             }
    85             if (buf[i] == '\n') {
    86                 line[col++] = '\n';
    87                 line[col] = 0;
    88                 addline(inputfile, line, col);
    89                 col = 0;
    90             } else {
    91                 line[col++] = buf[i];
    92             }
    93         }
    94     }
    96     free(line);
    98     close(fd);
   100     return inputfile;
   101 }
   103 void printhelp() {
   104     printf("Formats source code using HTML.\n\nUsage:\n"
   105         "  c2html [Options] FILE\n\n"
   106         " Options:\n"
   107         "  -h                    Prints this help message\n"
   108         "  -j                    Highlight Java instead of C source code\n"
   109         "  -o <output>           Output file (stdout, if not specified)\n"
   110         "  -H <header>           Prepend header file\n"
   111         "  -F <footer>           Append footer file\n"
   112         "  -p                    Disable highlighting (plain text)\n"
   113         "  -l                    Disable line numbers\n"
   114         "\n");
   117 }
   119 int lnint(size_t lnc) {
   120     int w = 1, p = 1;
   121     while ((p*=10) < lnc) w++;
   122     return w;
   123 }
   125 int copyfile(char *filename, FILE *dest) {
   126     if (!filename) {
   127         return 0;
   128     }
   130     FILE *src = fopen(filename, "r");
   131     if (src) {
   132         char buf[4096];
   133         int r;
   134         while ((r = fread(buf, 1, 4096, src)) > 0) {
   135             fwrite(buf, 1, r, dest);
   136         }
   137         fclose(src);
   138         return 0;
   139     } else {
   140         return -1;
   141     }
   142 }
   144 #define WRITECONST(stream, out, cstr) out(cstr, 1, sizeof(cstr)-1, stream)
   145 int formatfile(
   146         highlighter_t *highlighter,
   147         inputfile_t *in,
   148         fmt_write_func out,
   149         void *stream,
   150         _Bool showln) {
   151     // formats an input file and writes the result to out
   153     char *line = malloc(in->maxlinewidth*64);
   154     if(!line) {
   155         return 1;
   156     }
   157     WRITECONST(stream, out, "<pre>\n");
   159     int lnw = lnint(in->count);
   160     for (int i = 0 ; i < in->count ; i++) {
   161         if (highlighter) {
   162             highlighter->parser(in->lines[i], line, highlighter);
   163         } else {
   164             char *c = in->lines[i];
   165             size_t dp = 0;
   166             while (*c) {
   167                 dp = writeescapedchar(line, dp, *c);
   168                 c++;
   169             }
   170             line[dp] = '\0';
   171         }
   173         // write line number
   174         if (showln) {
   175             WRITECONST(stream, out, "<span class=\"c2html-lineno\">");
   176             char lnbuf[128];
   177             int len;
   178             // line number link
   179             len = snprintf(lnbuf, 128, "<a name=\"l%d\" href=\"#l%d\">",
   180                 i+1, i+1);
   181             out(lnbuf, 1, len, stream);
   182             // justified line number
   183             len = snprintf(lnbuf, 128, "%*d ", lnw, i+1);
   184             out(lnbuf, 1, len, stream);
   185             WRITECONST(stream, out, "</a></span> ");
   186         }
   188         // write formated (or plain) code line
   189         out(line, 1, strlen(line), stream);
   190     }
   192     WRITECONST(stream, out, "</pre>\n");
   193     free(line);
   194     return 0;
   195 }
   197 void init_c_highlighter(highlighter_t *highlighter) {
   198     memset(highlighter, 0, sizeof(highlighter_t));
   199     highlighter->isdirective = check_cdirective;
   200     highlighter->istype = check_ctype;
   201     highlighter->keywords = ckeywords;
   202     highlighter->parser = cparseline;
   203 }
   205 void init_java_highlighter(highlighter_t *highlighter) {
   206     memset(highlighter, 0, sizeof(highlighter_t));
   207     highlighter->isdirective = check_jdirective;
   208     highlighter->istype = check_jtype;
   209     highlighter->keywords = jkeywords;
   210     highlighter->parser = jparseline;
   211 }
   213 int main(int argc, char** argv) {
   214     int retcode = EXIT_SUCCESS;
   216     settings_t settings;
   217     memset(&settings, 0, sizeof(settings));
   218     settings.highlight = 1;
   219     settings.showlinenumbers = 1;
   221     int lang = C2HTML_C;
   223     char optc;
   224     while ((optc = getopt(argc, argv, "hljo:pH:F:")) != -1) {
   225         switch (optc) {
   226             case 'o':
   227                 if (!(optarg[0] == '-' && optarg[1] == 0)) {
   228                     settings.outfilename = optarg;
   229                 }
   230                 break;
   231             case 'F':
   232                 settings.footerfile = optarg;
   233                 break;
   234             case 'H':
   235                 settings.headerfile = optarg;
   236                 break;
   237             case 'j':
   238                 lang = C2HTML_JAVA;
   239                 break;
   240             case 'p':
   241                 settings.highlight = 0;
   242                 break;
   243             case 'l':
   244                 settings.showlinenumbers = 0;
   245                 break;
   246             case 'h':
   247                 printhelp();
   248                 return 0;
   249             default:
   250                 return 1;
   251         }
   252     }
   254     if (optind != argc-1) {
   255         printhelp();
   256         return 1;
   257     } else {
   258         settings.infilename = argv[optind];
   259         FILE *fout;
   260         if (settings.outfilename) {
   261             fout = fopen(settings.outfilename, "w");
   262             if (!fout) {
   263                 perror("Error opening output file");
   264                 return -1;
   265             }
   266         } else {
   267             fout = stdout;
   268         }
   270         if (copyfile(settings.headerfile, fout)) {
   271             perror("Error opening header file");
   272             retcode = -1;
   273             goto prog_end;
   274         }
   276         highlighter_t highlighter;
   277         highlighter_t *hptr = &highlighter;
   278         switch (lang) {
   279             case C2HTML_C:
   280                 init_c_highlighter(&highlighter);
   281                 break;
   282             case C2HTML_JAVA:
   283                 init_java_highlighter(&highlighter);
   284                 break;
   285             default:
   286                 hptr = NULL;
   287                 break;
   288         }
   289         if (!settings.highlight) {
   290             hptr = NULL;
   291         }
   293         inputfile_t *inputfile = readinput(settings.infilename);
   294         if (inputfile) {
   295             formatfile(
   296                     hptr,
   297                     inputfile,
   298                     (fmt_write_func)fwrite,
   299                     fout,
   300                     settings.showlinenumbers);
   301             freeinputfilebuffer(inputfile);
   302         } else {
   303             perror("Error opening input file");
   304             retcode = -1;
   305         }
   307         if (copyfile(settings.footerfile, fout)) {
   308             perror("Error opening footer file");
   309             retcode = -1;
   310         }
   312         prog_end:        
   313         if (fout != stdout) {
   314             fclose(fout);
   315         }
   317         return retcode;
   318     }
   319 }

mercurial