src/c2html.c

Tue, 23 Aug 2016 17:31:15 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Aug 2016 17:31:15 +0200
changeset 49
f86f0b054464
parent 48
b2724c711203
child 50
17408c3607ce
permissions
-rw-r--r--

cleans up includes

     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, int* x) {
    49     while (*src && *src != '\n') {
    50         put_htmlescaped(dest, *src);
    51         src++;
    52     }
    53     ucx_buffer_putc(dest, '\n');
    54 }
    56 int 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     /* allocate line buffer */
    69     UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND);
    70     if(!line) {
    71         return 1;
    72     }
    74     /* start monospace formatting */
    75     out("<pre>\n", 1, 6, stream);
    77     /* process lines */
    78     size_t lineno = 0;
    79     int multiline_comment = 0;
    81     UCX_FOREACH(sourceline, in) {
    82         /* increase line number and clean line buffer */
    83         lineno++;
    84         ucx_buffer_clear(line);
    86         /* write line number */
    87         if (showlineno) {
    88             ucx_bprintf(line, "<span class=\"c2html-lineno\">"
    89                     "<a name=\"l%d\" href=\"#l%d\">%*d </a></span> ",
    90                     lineno, lineno, lnw, lineno);
    91         }
    93         /* process code line */
    94         highlighter(sourceline->data, line, &multiline_comment);
    96         /* write code line and reset buffer */
    97         out(line->space, 1, line->size, stream);
    98     }
   100     /* end monospace formatting */
   101     out("</pre>\n", 1, 7, stream);
   103     /* cleanup and return */
   104     ucx_buffer_free(line);
   105     return 0;
   106 }
   108 #define FILEBUF_SIZE 4096
   110 enum source_type {
   111     SOURCE_C,
   112     SOURCE_JAVA,
   113     SOURCE_PLAIN
   114 };
   116 int main(int argc, char** argv) {
   118     /* Default settings */
   119     Settings settings;
   120     memset(&settings, 0, sizeof(settings));
   121     settings.showlinenumbers = 1;
   122     enum source_type sourcetype = SOURCE_C;
   124     /* Parse command line */
   125     char optc;
   126     while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) {
   127         switch (optc) {
   128             case 'o':
   129                 if (!(optarg[0] == '-' && optarg[1] == 0)) {
   130                     settings.outfilename = optarg;
   131                 }
   132                 break;
   133             case 'F':
   134                 settings.footerfile = optarg;
   135                 break;
   136             case 'H':
   137                 settings.headerfile = optarg;
   138                 break;
   139             case 'j':
   140                 sourcetype = SOURCE_JAVA;
   141                 break;
   142             case 'p':
   143                 sourcetype = SOURCE_PLAIN;
   144                 break;
   145             case 'l':
   146                 settings.showlinenumbers = 0;
   147                 break;
   148             case 'h':
   149                 printhelp();
   150                 return EXIT_SUCCESS;
   151             case 'v':
   152             case 'V':
   153 #ifdef VERSION_DEVELOP
   154                 printf("%d.%d (unstable)\n", VERSION_MAJOR, VERSION_MINOR);
   155 #else
   156                 printf("%d.%d\n", VERSION_MAJOR, VERSION_MINOR);
   157 #endif
   158                 return EXIT_SUCCESS;
   159             default:
   160                 return EXIT_FAILURE;
   161         }
   162     }
   164     if (optind != argc-1) {
   165         printhelp();
   166         return EXIT_FAILURE;
   167     } else {
   168         /* Choose highlighter */
   169         highlighter_func hltr = NULL;
   170         switch (sourcetype) {
   171             case SOURCE_C:
   172                 hltr = c_highlighter;
   173                 break;
   174             case SOURCE_JAVA:
   175                 hltr = java_highlighter;
   176                 break;
   177             case SOURCE_PLAIN:
   178                 hltr = plain_highlighter;
   179                 break;
   180             default: /* should be unreachable */
   181                 fprintf(stderr, "error in enum source_type\n");
   182                 return EXIT_FAILURE;
   183         }
   185         /* Open output file */
   186         settings.infilename = argv[optind];
   187         FILE *fout;
   188         if (settings.outfilename) {
   189             fout = fopen(settings.outfilename, "w");
   190             if (!fout) {
   191                 perror("Error opening output file");
   192                 return EXIT_FAILURE;
   193             }
   194         } else {
   195             fout = stdout;
   196         }
   198         /* Allocate file buffer  */
   199         char *filebuf = malloc(FILEBUF_SIZE);
   200         if (!filebuf) {
   201             perror("Error allocating file buffer");
   202             return EXIT_FAILURE;
   203         }
   205         /* Prepend header file */
   206         {
   207             FILE *headerfile = fopen(settings.headerfile, "r");
   208             if (!headerfile) {
   209                 perror("Error opening header file");
   210                 if (fout != stdout) {
   211                     fclose(fout);
   212                 }
   213                 return EXIT_FAILURE;
   214             }
   215             ucx_stream_copy(headerfile, fout,
   216                     (read_func) fread, (write_func) fwrite,
   217                     filebuf, FILEBUF_SIZE, (size_t)-1);
   218             fclose(headerfile);
   219         }
   221         /* Process input file */
   222         FILE *inputfile = fopen(settings.infilename, "r");
   223         if (inputfile) {
   224             UcxBuffer *content = ucx_buffer_new(NULL,
   225                     FILEBUF_SIZE*2, UCX_BUFFER_AUTOEXTEND);
   226             {
   227                 ucx_stream_copy(inputfile, content, (read_func) fread,
   228                         (write_func) ucx_buffer_write,
   229                         filebuf, FILEBUF_SIZE, (size_t)-1);
   230             }
   231             fclose(inputfile);
   233             UcxList *inputlines = ucx_list_append(NULL, content->space);
   234             for (size_t i = 1 ; i < content->size ; i++) {
   235                 if (content->space[i] == '\r') {
   236                     content->space[i] = '\n'; i++;
   237                 }
   238                 if (content->space[i] == '\n' && i+1 < content->size) {
   239                     ucx_list_append(inputlines, content->space+i+1);
   240                 }
   241             }
   243             formatlines(hltr, inputlines,
   244                     (write_func) fwrite, fout, settings.showlinenumbers);
   246             ucx_buffer_free(content);
   247         } else {
   248             perror("Error opening input file");
   249             if (fout != stdout) {
   250                 fclose(fout);
   251             }
   252             return EXIT_FAILURE;
   253         }
   255         /* Append footer file */
   256         {
   257             FILE *footerfile = fopen(settings.footerfile, "r");
   258             if (!footerfile) {
   259                 perror("Error opening footer file");
   260                 if (fout != stdout) {
   261                     fclose(fout);
   262                 }
   263                 return EXIT_FAILURE;
   264             }
   265             ucx_stream_copy(footerfile, fout,
   266                     (read_func) fread, (write_func) fwrite,
   267                     filebuf, FILEBUF_SIZE, (size_t)-1);
   268             fclose(footerfile);
   269         }
   272         free(filebuf);
   274         return EXIT_SUCCESS;
   275     }
   276 }

mercurial