Tue, 23 Aug 2016 13:49:38 +0200
adds UCX + changes how the input file is read (uses an consecutive memory area now)
/* * 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/buffer.h" #include "ucx/list.h" #include "ucx/utils.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"); } int copyfile(char *filename, FILE *dest) { if (!filename) { return 0; } FILE *src = fopen(filename, "r"); if (src) { char buf[4096]; int r; while ((r = fread(buf, 1, 4096, src)) > 0) { fwrite(buf, 1, r, dest); } fclose(src); return 0; } else { return 1; } } #define WRITECONST(stream, out, cstr) out(cstr, 1, sizeof(cstr)-1, stream) int formatfile( highlighter_t *highlighter, UcxList *in, write_func out, void *stream, int showlineno) { size_t lines = ucx_list_size(in); UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); if(!line) { return 1; } WRITECONST(stream, out, "<pre>\n"); int lnw; { lnw = 1; int p = 1; while ((p*=10) < lines) lnw++; } size_t lineno = 0; UCX_FOREACH(sourceline, in) { lineno++; /* TODO: backwards compatibility: replace line->space in all occasions * and use UcxBuffer functions */ if (highlighter) { highlighter->parser(sourceline->data, line->space, highlighter); } else { char *c = sourceline->data; size_t dp = 0; while (*c) { dp = writeescapedchar(line->space, dp, *c); c++; } line->space[dp] = '\0'; } // write line number if (showlineno) { WRITECONST(stream, out, "<span class=\"c2html-lineno\">"); char lnbuf[128]; int len; // line number link len = snprintf(lnbuf, 128, "<a name=\"l%d\" href=\"#l%d\">", lineno, lineno); out(lnbuf, 1, len, stream); // justified line number len = snprintf(lnbuf, 128, "%*d ", lnw, lineno); out(lnbuf, 1, len, stream); WRITECONST(stream, out, "</a></span> "); } // write formated (or plain) code line out(line->space, 1, strlen(line->space), stream); } WRITECONST(stream, out, "</pre>\n"); ucx_buffer_free(line); return 0; } void init_c_highlighter(highlighter_t *highlighter) { memset(highlighter, 0, sizeof(highlighter_t)); highlighter->isdirective = check_cdirective; highlighter->istype = check_ctype; highlighter->keywords = ckeywords; highlighter->parser = cparseline; } void init_java_highlighter(highlighter_t *highlighter) { memset(highlighter, 0, sizeof(highlighter_t)); highlighter->isdirective = check_jdirective; highlighter->istype = check_jtype; highlighter->keywords = jkeywords; highlighter->parser = jparseline; } enum source_type { SOURCE_C, SOURCE_JAVA, SOURCE_PLAIN }; int main(int argc, char** argv) { int retcode = EXIT_SUCCESS; Settings settings; memset(&settings, 0, sizeof(settings)); settings.showlinenumbers = 1; enum source_type sourcetype = SOURCE_C; 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 1; } else { 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; } if (copyfile(settings.headerfile, fout)) { perror("Error opening header file"); retcode = EXIT_FAILURE; goto prog_end; } highlighter_t highlighter; highlighter_t *hptr = &highlighter; switch (sourcetype) { case SOURCE_C: init_c_highlighter(&highlighter); break; case SOURCE_JAVA: init_java_highlighter(&highlighter); break; case SOURCE_PLAIN: hptr = NULL; break; default: /* should be unreachable */ fprintf(stderr, "error in enum source_type\n"); retcode = EXIT_FAILURE; goto prog_end; } FILE *inputfile = fopen(settings.infilename, "r"); if (inputfile) { UcxBuffer *filebuf = ucx_buffer_new(NULL, 2048, UCX_BUFFER_AUTOEXTEND); { const size_t tmpbufsize = 512; char *tmpbuf = malloc(tmpbufsize); ucx_stream_copy(inputfile, filebuf, (read_func) fread, (write_func) ucx_buffer_write, tmpbuf, tmpbufsize, (size_t)-1); free(tmpbuf); } fclose(inputfile); UcxList *inputlines = ucx_list_append(NULL, filebuf->space); for (size_t i = 1 ; i < filebuf->size ; i++) { if (filebuf->space[i] == '\r') { filebuf->space[i] = '\n'; i++; } if (filebuf->space[i] == '\n' && i+1 < filebuf->size) { ucx_list_append(inputlines, filebuf->space+i+1); } } formatfile( hptr, inputlines, (write_func) fwrite, fout, settings.showlinenumbers); ucx_buffer_free(filebuf); } else { perror("Error opening input file"); retcode = EXIT_FAILURE; } if (copyfile(settings.footerfile, fout)) { perror("Error opening footer file"); retcode = EXIT_FAILURE; } prog_end: if (fout != stdout) { fclose(fout); } return retcode; } }