src/c2html.c

Thu, 25 Aug 2016 12:16:57 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 25 Aug 2016 12:16:57 +0200
changeset 51
f25ba6fd7a08
parent 50
17408c3607ce
child 52
33ded421c512
permissions
-rw-r--r--

replaces stack buffers with UCX buffers

     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 #include "ucx/list.h"
    33 void printhelp() {
    34     printf("Formats source code using HTML.\n\nUsage:\n"
    35         "  c2html [Options] FILE\n\n"
    36         " Options:\n"
    37         "  -h                    Prints this help message\n"
    38         "  -j                    Highlight Java instead of C source code\n"
    39         "  -o <output>           Output file (stdout, if not specified)\n"
    40         "  -H <header>           Prepend header file\n"
    41         "  -F <footer>           Append footer file\n"
    42         "  -p                    Disable highlighting (plain text)\n"
    43         "  -l                    Disable line numbers\n"
    44         "  -V, -v                Prints version and exits\n"
    45         "\n");
    46 }
    48 static void plain_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) {
    49     while (*src && *src != '\n') {
    50         put_htmlescaped(dest, *src);
    51         src++;
    52     }
    53     ucx_buffer_putc(dest, '\n');
    54 }
    56 void formatlines(highlighter_func highlighter,
    57         UcxList *in, write_func out, void *stream, int showlineno) {
    59     /* compute width of line numbering */
    60     int lnw;
    61     if (showlineno) {
    62         size_t lines = ucx_list_size(in);
    63         lnw = 1;
    64         int p = 1;
    65         while ((p*=10) < lines) lnw++;
    66     }
    68     /* start monospace formatting */
    69     out("<pre>\n", 1, 6, stream);
    71     /* process lines */
    72     size_t lineno = 0;
    73     HighlighterData *hd = new_highlighter_data();
    74     UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND);
    75     if(!line || !hd) {
    76         perror("Error allocating buffer for output");
    77         return;
    78     }
    80     UCX_FOREACH(sourceline, in) {
    81         /* increase line number and clean line buffer */
    82         lineno++;
    83         ucx_buffer_clear(line);
    85         /* write line number */
    86         if (showlineno) {
    87             ucx_bprintf(line, "<span class=\"c2html-lineno\">"
    88                     "<a name=\"l%d\" href=\"#l%d\">%*d </a></span> ",
    89                     lineno, lineno, lnw, lineno);
    90         }
    92         /* process code line */
    93         highlighter(sourceline->data, line, hd);
    95         /* write code line */
    96         out(line->space, 1, line->size, stream);
    97     }
    99     /* end monospace formatting */
   100     out("</pre>\n", 1, 7, stream);
   102     /* cleanup and return */
   103     free_highlighter_data(hd);
   104     ucx_buffer_free(line);
   105 }
   107 #define FILEBUF_SIZE 4096
   109 enum source_type {
   110     SOURCE_C,
   111     SOURCE_JAVA,
   112     SOURCE_PLAIN
   113 };
   115 int main(int argc, char** argv) {
   117     /* Default settings */
   118     Settings settings;
   119     memset(&settings, 0, sizeof(settings));
   120     settings.showlinenumbers = 1;
   121     enum source_type sourcetype = SOURCE_C;
   123     /* Parse command line */
   124     char optc;
   125     while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) {
   126         switch (optc) {
   127             case 'o':
   128                 if (!(optarg[0] == '-' && optarg[1] == 0)) {
   129                     settings.outfilename = optarg;
   130                 }
   131                 break;
   132             case 'F':
   133                 settings.footerfile = optarg;
   134                 break;
   135             case 'H':
   136                 settings.headerfile = optarg;
   137                 break;
   138             case 'j':
   139                 sourcetype = SOURCE_JAVA;
   140                 break;
   141             case 'p':
   142                 sourcetype = SOURCE_PLAIN;
   143                 break;
   144             case 'l':
   145                 settings.showlinenumbers = 0;
   146                 break;
   147             case 'h':
   148                 printhelp();
   149                 return EXIT_SUCCESS;
   150             case 'v':
   151             case 'V':
   152 #ifdef VERSION_DEVELOP
   153                 printf("%d.%d (unstable)\n", VERSION_MAJOR, VERSION_MINOR);
   154 #else
   155                 printf("%d.%d\n", VERSION_MAJOR, VERSION_MINOR);
   156 #endif
   157                 return EXIT_SUCCESS;
   158             default:
   159                 return EXIT_FAILURE;
   160         }
   161     }
   163     if (optind != argc-1) {
   164         printhelp();
   165         return EXIT_FAILURE;
   166     } else {
   167         /* Choose highlighter */
   168         highlighter_func hltr = NULL;
   169         switch (sourcetype) {
   170             case SOURCE_C:
   171                 hltr = c_highlighter;
   172                 break;
   173             case SOURCE_JAVA:
   174                 hltr = java_highlighter;
   175                 break;
   176             case SOURCE_PLAIN:
   177                 hltr = plain_highlighter;
   178                 break;
   179             default: /* should be unreachable */
   180                 fprintf(stderr, "error in enum source_type\n");
   181                 return EXIT_FAILURE;
   182         }
   184         /* Open output file */
   185         settings.infilename = argv[optind];
   186         FILE *fout;
   187         if (settings.outfilename) {
   188             fout = fopen(settings.outfilename, "w");
   189             if (!fout) {
   190                 perror("Error opening output file");
   191                 return EXIT_FAILURE;
   192             }
   193         } else {
   194             fout = stdout;
   195         }
   197         /* Allocate file buffer  */
   198         char *filebuf = malloc(FILEBUF_SIZE);
   199         if (!filebuf) {
   200             perror("Error allocating file buffer");
   201             return EXIT_FAILURE;
   202         }
   204         /* Prepend header file */
   205         {
   206             FILE *headerfile = fopen(settings.headerfile, "r");
   207             if (!headerfile) {
   208                 perror("Error opening header file");
   209                 if (fout != stdout) {
   210                     fclose(fout);
   211                 }
   212                 return EXIT_FAILURE;
   213             }
   214             ucx_stream_copy(headerfile, fout,
   215                     (read_func) fread, (write_func) fwrite,
   216                     filebuf, FILEBUF_SIZE, (size_t)-1);
   217             fclose(headerfile);
   218         }
   220         /* Process input file */
   221         FILE *inputfile = fopen(settings.infilename, "r");
   222         if (inputfile) {
   223             UcxBuffer *content = ucx_buffer_new(NULL,
   224                     FILEBUF_SIZE*2, UCX_BUFFER_AUTOEXTEND);
   225             {
   226                 ucx_stream_copy(inputfile, content, (read_func) fread,
   227                         (write_func) ucx_buffer_write,
   228                         filebuf, FILEBUF_SIZE, (size_t)-1);
   229             }
   230             fclose(inputfile);
   232             UcxList *inputlines = ucx_list_append(NULL, content->space);
   233             for (size_t i = 1 ; i < content->size ; i++) {
   234                 if (content->space[i] == '\r') {
   235                     content->space[i] = '\n'; i++;
   236                 }
   237                 if (content->space[i] == '\n' && i+1 < content->size) {
   238                     ucx_list_append(inputlines, content->space+i+1);
   239                 }
   240             }
   242             formatlines(hltr, inputlines,
   243                     (write_func) fwrite, fout, settings.showlinenumbers);
   245             ucx_buffer_free(content);
   246         } else {
   247             perror("Error opening input file");
   248             if (fout != stdout) {
   249                 fclose(fout);
   250             }
   251             return EXIT_FAILURE;
   252         }
   254         /* Append footer file */
   255         {
   256             FILE *footerfile = fopen(settings.footerfile, "r");
   257             if (!footerfile) {
   258                 perror("Error opening footer file");
   259                 if (fout != stdout) {
   260                     fclose(fout);
   261                 }
   262                 return EXIT_FAILURE;
   263             }
   264             ucx_stream_copy(footerfile, fout,
   265                     (read_func) fread, (write_func) fwrite,
   266                     filebuf, FILEBUF_SIZE, (size_t)-1);
   267             fclose(footerfile);
   268         }
   271         free(filebuf);
   273         return EXIT_SUCCESS;
   274     }
   275 }

mercurial