Thu, 25 Aug 2016 12:16:57 +0200
replaces stack buffers with UCX buffers
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2016 Mike Becker. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * */ #include "c2html.h" #include "ucx/list.h" void printhelp() { printf("Formats source code using HTML.\n\nUsage:\n" " c2html [Options] FILE\n\n" " Options:\n" " -h Prints this help message\n" " -j Highlight Java instead of C source code\n" " -o <output> Output file (stdout, if not specified)\n" " -H <header> Prepend header file\n" " -F <footer> Append footer file\n" " -p Disable highlighting (plain text)\n" " -l Disable line numbers\n" " -V, -v Prints version and exits\n" "\n"); } static void plain_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) { while (*src && *src != '\n') { put_htmlescaped(dest, *src); src++; } ucx_buffer_putc(dest, '\n'); } void formatlines(highlighter_func highlighter, UcxList *in, write_func out, void *stream, int showlineno) { /* compute width of line numbering */ int lnw; if (showlineno) { size_t lines = ucx_list_size(in); lnw = 1; int p = 1; while ((p*=10) < lines) lnw++; } /* start monospace formatting */ out("<pre>\n", 1, 6, stream); /* process lines */ size_t lineno = 0; HighlighterData *hd = new_highlighter_data(); UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); if(!line || !hd) { perror("Error allocating buffer for output"); return; } UCX_FOREACH(sourceline, in) { /* increase line number and clean line buffer */ lineno++; ucx_buffer_clear(line); /* write line number */ if (showlineno) { ucx_bprintf(line, "<span class=\"c2html-lineno\">" "<a name=\"l%d\" href=\"#l%d\">%*d </a></span> ", lineno, lineno, lnw, lineno); } /* process code line */ highlighter(sourceline->data, line, hd); /* write code line */ out(line->space, 1, line->size, stream); } /* end monospace formatting */ out("</pre>\n", 1, 7, stream); /* cleanup and return */ free_highlighter_data(hd); ucx_buffer_free(line); } #define FILEBUF_SIZE 4096 enum source_type { SOURCE_C, SOURCE_JAVA, SOURCE_PLAIN }; int main(int argc, char** argv) { /* Default settings */ Settings settings; memset(&settings, 0, sizeof(settings)); settings.showlinenumbers = 1; enum source_type sourcetype = SOURCE_C; /* Parse command line */ char optc; while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) { switch (optc) { case 'o': if (!(optarg[0] == '-' && optarg[1] == 0)) { settings.outfilename = optarg; } break; case 'F': settings.footerfile = optarg; break; case 'H': settings.headerfile = optarg; break; case 'j': sourcetype = SOURCE_JAVA; break; case 'p': sourcetype = SOURCE_PLAIN; break; case 'l': settings.showlinenumbers = 0; break; case 'h': printhelp(); return EXIT_SUCCESS; case 'v': case 'V': #ifdef VERSION_DEVELOP printf("%d.%d (unstable)\n", VERSION_MAJOR, VERSION_MINOR); #else printf("%d.%d\n", VERSION_MAJOR, VERSION_MINOR); #endif return EXIT_SUCCESS; default: return EXIT_FAILURE; } } if (optind != argc-1) { printhelp(); return EXIT_FAILURE; } else { /* Choose highlighter */ highlighter_func hltr = NULL; switch (sourcetype) { case SOURCE_C: hltr = c_highlighter; break; case SOURCE_JAVA: hltr = java_highlighter; break; case SOURCE_PLAIN: hltr = plain_highlighter; break; default: /* should be unreachable */ fprintf(stderr, "error in enum source_type\n"); return EXIT_FAILURE; } /* Open output file */ settings.infilename = argv[optind]; FILE *fout; if (settings.outfilename) { fout = fopen(settings.outfilename, "w"); if (!fout) { perror("Error opening output file"); return EXIT_FAILURE; } } else { fout = stdout; } /* Allocate file buffer */ char *filebuf = malloc(FILEBUF_SIZE); if (!filebuf) { perror("Error allocating file buffer"); return EXIT_FAILURE; } /* Prepend header file */ { FILE *headerfile = fopen(settings.headerfile, "r"); if (!headerfile) { perror("Error opening header file"); if (fout != stdout) { fclose(fout); } return EXIT_FAILURE; } ucx_stream_copy(headerfile, fout, (read_func) fread, (write_func) fwrite, filebuf, FILEBUF_SIZE, (size_t)-1); fclose(headerfile); } /* Process input file */ FILE *inputfile = fopen(settings.infilename, "r"); if (inputfile) { UcxBuffer *content = ucx_buffer_new(NULL, FILEBUF_SIZE*2, UCX_BUFFER_AUTOEXTEND); { ucx_stream_copy(inputfile, content, (read_func) fread, (write_func) ucx_buffer_write, filebuf, FILEBUF_SIZE, (size_t)-1); } fclose(inputfile); UcxList *inputlines = ucx_list_append(NULL, content->space); for (size_t i = 1 ; i < content->size ; i++) { if (content->space[i] == '\r') { content->space[i] = '\n'; i++; } if (content->space[i] == '\n' && i+1 < content->size) { ucx_list_append(inputlines, content->space+i+1); } } formatlines(hltr, inputlines, (write_func) fwrite, fout, settings.showlinenumbers); ucx_buffer_free(content); } else { perror("Error opening input file"); if (fout != stdout) { fclose(fout); } return EXIT_FAILURE; } /* Append footer file */ { FILE *footerfile = fopen(settings.footerfile, "r"); if (!footerfile) { perror("Error opening footer file"); if (fout != stdout) { fclose(fout); } return EXIT_FAILURE; } ucx_stream_copy(footerfile, fout, (read_func) fread, (write_func) fwrite, filebuf, FILEBUF_SIZE, (size_t)-1); fclose(footerfile); } free(filebuf); return EXIT_SUCCESS; } }