Wed, 31 Aug 2016 12:58:48 +0200
highlighter can now handle files which do not end with a blank line
universe@23 | 1 | /* |
universe@23 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@23 | 3 | * |
universe@35 | 4 | * Copyright 2016 Mike Becker. All rights reserved. |
universe@23 | 5 | * |
universe@23 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@23 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@23 | 8 | * |
universe@23 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@23 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@23 | 11 | * |
universe@23 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@23 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@23 | 14 | * documentation and/or other materials provided with the distribution. |
universe@23 | 15 | * |
universe@23 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@23 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@23 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@23 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@23 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@23 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@23 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@23 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@23 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@23 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@23 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@23 | 27 | * |
universe@23 | 28 | */ |
universe@52 | 29 | |
universe@52 | 30 | #include <unistd.h> |
universe@52 | 31 | |
universe@22 | 32 | #include "c2html.h" |
universe@52 | 33 | #include "highlighter.h" |
universe@1 | 34 | |
universe@39 | 35 | #include "ucx/list.h" |
universe@1 | 36 | |
universe@53 | 37 | static int appendfile(const char *filename, FILE *fout, |
universe@53 | 38 | char *copybuf, size_t copybuflen, const char *errmsg) { |
universe@53 | 39 | FILE *headerfile = fopen(filename, "r"); |
universe@53 | 40 | if (!headerfile) { |
universe@53 | 41 | perror(errmsg); |
universe@53 | 42 | if (fout != stdout) { |
universe@53 | 43 | fclose(fout); |
universe@53 | 44 | } |
universe@53 | 45 | return 1; |
universe@53 | 46 | } |
universe@53 | 47 | ucx_stream_copy(headerfile, fout, |
universe@53 | 48 | (read_func) fread, (write_func) fwrite, |
universe@53 | 49 | copybuf, copybuflen, (size_t)-1); |
universe@53 | 50 | fclose(headerfile); |
universe@53 | 51 | return 0; |
universe@53 | 52 | } |
universe@53 | 53 | |
universe@53 | 54 | static void printhelp() { |
universe@19 | 55 | printf("Formats source code using HTML.\n\nUsage:\n" |
universe@19 | 56 | " c2html [Options] FILE\n\n" |
universe@19 | 57 | " Options:\n" |
universe@19 | 58 | " -h Prints this help message\n" |
universe@19 | 59 | " -j Highlight Java instead of C source code\n" |
universe@19 | 60 | " -o <output> Output file (stdout, if not specified)\n" |
universe@22 | 61 | " -H <header> Prepend header file\n" |
universe@22 | 62 | " -F <footer> Append footer file\n" |
universe@19 | 63 | " -p Disable highlighting (plain text)\n" |
olaf@24 | 64 | " -l Disable line numbers\n" |
universe@37 | 65 | " -V, -v Prints version and exits\n" |
universe@19 | 66 | "\n"); |
universe@1 | 67 | } |
universe@1 | 68 | |
universe@53 | 69 | static void formatlines(highlighter_func highlighter, |
universe@46 | 70 | UcxList *in, write_func out, void *stream, int showlineno) { |
olaf@24 | 71 | |
universe@44 | 72 | /* compute width of line numbering */ |
universe@53 | 73 | int lnw = 0; |
universe@44 | 74 | if (showlineno) { |
universe@44 | 75 | size_t lines = ucx_list_size(in); |
universe@53 | 76 | for (size_t p = 1; p < lines ; p*=10) lnw++; |
universe@44 | 77 | } |
universe@44 | 78 | |
universe@44 | 79 | /* start monospace formatting */ |
universe@44 | 80 | out("<pre>\n", 1, 6, stream); |
universe@39 | 81 | |
universe@44 | 82 | /* process lines */ |
universe@39 | 83 | size_t lineno = 0; |
universe@51 | 84 | HighlighterData *hd = new_highlighter_data(); |
universe@51 | 85 | UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); |
universe@51 | 86 | if(!line || !hd) { |
universe@51 | 87 | perror("Error allocating buffer for output"); |
universe@51 | 88 | return; |
universe@51 | 89 | } |
universe@46 | 90 | |
universe@39 | 91 | UCX_FOREACH(sourceline, in) { |
universe@44 | 92 | /* increase line number and clean line buffer */ |
universe@39 | 93 | lineno++; |
universe@44 | 94 | ucx_buffer_clear(line); |
universe@44 | 95 | |
universe@44 | 96 | /* write line number */ |
universe@44 | 97 | if (showlineno) { |
universe@44 | 98 | ucx_bprintf(line, "<span class=\"c2html-lineno\">" |
universe@44 | 99 | "<a name=\"l%d\" href=\"#l%d\">%*d </a></span> ", |
universe@44 | 100 | lineno, lineno, lnw, lineno); |
universe@44 | 101 | } |
universe@44 | 102 | |
universe@45 | 103 | /* process code line */ |
universe@51 | 104 | highlighter(sourceline->data, line, hd); |
olaf@24 | 105 | |
universe@50 | 106 | /* write code line */ |
universe@44 | 107 | out(line->space, 1, line->size, stream); |
olaf@24 | 108 | } |
olaf@24 | 109 | |
universe@44 | 110 | /* end monospace formatting */ |
universe@44 | 111 | out("</pre>\n", 1, 7, stream); |
universe@44 | 112 | |
universe@44 | 113 | /* cleanup and return */ |
universe@51 | 114 | free_highlighter_data(hd); |
universe@39 | 115 | ucx_buffer_free(line); |
olaf@24 | 116 | } |
olaf@24 | 117 | |
universe@42 | 118 | #define FILEBUF_SIZE 4096 |
universe@42 | 119 | |
universe@39 | 120 | enum source_type { |
universe@39 | 121 | SOURCE_C, |
universe@39 | 122 | SOURCE_JAVA, |
universe@39 | 123 | SOURCE_PLAIN |
universe@39 | 124 | }; |
universe@39 | 125 | |
universe@1 | 126 | int main(int argc, char** argv) { |
universe@43 | 127 | |
universe@43 | 128 | /* Default settings */ |
universe@39 | 129 | Settings settings; |
universe@22 | 130 | memset(&settings, 0, sizeof(settings)); |
olaf@24 | 131 | settings.showlinenumbers = 1; |
universe@39 | 132 | enum source_type sourcetype = SOURCE_C; |
universe@19 | 133 | |
universe@43 | 134 | /* Parse command line */ |
universe@19 | 135 | char optc; |
universe@37 | 136 | while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) { |
universe@19 | 137 | switch (optc) { |
universe@19 | 138 | case 'o': |
universe@19 | 139 | if (!(optarg[0] == '-' && optarg[1] == 0)) { |
universe@19 | 140 | settings.outfilename = optarg; |
universe@19 | 141 | } |
universe@19 | 142 | break; |
universe@22 | 143 | case 'F': |
universe@22 | 144 | settings.footerfile = optarg; |
universe@22 | 145 | break; |
universe@22 | 146 | case 'H': |
universe@22 | 147 | settings.headerfile = optarg; |
universe@22 | 148 | break; |
universe@19 | 149 | case 'j': |
universe@39 | 150 | sourcetype = SOURCE_JAVA; |
universe@19 | 151 | break; |
universe@19 | 152 | case 'p': |
universe@39 | 153 | sourcetype = SOURCE_PLAIN; |
universe@19 | 154 | break; |
olaf@24 | 155 | case 'l': |
olaf@24 | 156 | settings.showlinenumbers = 0; |
olaf@24 | 157 | break; |
universe@19 | 158 | case 'h': |
universe@19 | 159 | printhelp(); |
universe@38 | 160 | return EXIT_SUCCESS; |
universe@37 | 161 | case 'v': |
universe@37 | 162 | case 'V': |
universe@37 | 163 | #ifdef VERSION_DEVELOP |
universe@37 | 164 | printf("%d.%d (unstable)\n", VERSION_MAJOR, VERSION_MINOR); |
universe@37 | 165 | #else |
universe@37 | 166 | printf("%d.%d\n", VERSION_MAJOR, VERSION_MINOR); |
universe@37 | 167 | #endif |
universe@38 | 168 | return EXIT_SUCCESS; |
universe@19 | 169 | default: |
universe@38 | 170 | return EXIT_FAILURE; |
universe@11 | 171 | } |
universe@19 | 172 | } |
universe@19 | 173 | |
universe@19 | 174 | if (optind != argc-1) { |
universe@11 | 175 | printhelp(); |
universe@43 | 176 | return EXIT_FAILURE; |
universe@19 | 177 | } else { |
universe@47 | 178 | /* Choose highlighter */ |
universe@46 | 179 | highlighter_func hltr = NULL; |
universe@39 | 180 | switch (sourcetype) { |
universe@39 | 181 | case SOURCE_C: |
universe@48 | 182 | hltr = c_highlighter; |
olaf@24 | 183 | break; |
universe@39 | 184 | case SOURCE_JAVA: |
universe@48 | 185 | hltr = java_highlighter; |
olaf@24 | 186 | break; |
universe@39 | 187 | case SOURCE_PLAIN: |
universe@48 | 188 | hltr = plain_highlighter; |
olaf@24 | 189 | break; |
universe@39 | 190 | default: /* should be unreachable */ |
universe@39 | 191 | fprintf(stderr, "error in enum source_type\n"); |
universe@43 | 192 | return EXIT_FAILURE; |
olaf@24 | 193 | } |
universe@42 | 194 | |
universe@43 | 195 | /* Open output file */ |
universe@43 | 196 | settings.infilename = argv[optind]; |
universe@43 | 197 | FILE *fout; |
universe@43 | 198 | if (settings.outfilename) { |
universe@43 | 199 | fout = fopen(settings.outfilename, "w"); |
universe@43 | 200 | if (!fout) { |
universe@43 | 201 | perror("Error opening output file"); |
universe@43 | 202 | return EXIT_FAILURE; |
universe@43 | 203 | } |
universe@43 | 204 | } else { |
universe@43 | 205 | fout = stdout; |
universe@43 | 206 | } |
universe@43 | 207 | |
universe@43 | 208 | /* Allocate file buffer */ |
universe@43 | 209 | char *filebuf = malloc(FILEBUF_SIZE); |
universe@43 | 210 | if (!filebuf) { |
universe@43 | 211 | perror("Error allocating file buffer"); |
universe@43 | 212 | return EXIT_FAILURE; |
universe@43 | 213 | } |
universe@43 | 214 | |
universe@43 | 215 | /* Prepend header file */ |
universe@53 | 216 | if (appendfile(settings.headerfile, fout, filebuf, FILEBUF_SIZE, |
universe@53 | 217 | "Error opening header file")) { |
universe@53 | 218 | return EXIT_FAILURE; |
universe@42 | 219 | } |
universe@19 | 220 | |
universe@43 | 221 | /* Process input file */ |
universe@39 | 222 | FILE *inputfile = fopen(settings.infilename, "r"); |
universe@19 | 223 | if (inputfile) { |
universe@42 | 224 | UcxBuffer *content = ucx_buffer_new(NULL, |
universe@42 | 225 | FILEBUF_SIZE*2, UCX_BUFFER_AUTOEXTEND); |
universe@39 | 226 | { |
universe@42 | 227 | ucx_stream_copy(inputfile, content, (read_func) fread, |
universe@39 | 228 | (write_func) ucx_buffer_write, |
universe@42 | 229 | filebuf, FILEBUF_SIZE, (size_t)-1); |
universe@39 | 230 | } |
universe@39 | 231 | fclose(inputfile); |
universe@39 | 232 | |
universe@42 | 233 | UcxList *inputlines = ucx_list_append(NULL, content->space); |
universe@42 | 234 | for (size_t i = 1 ; i < content->size ; i++) { |
universe@42 | 235 | if (content->space[i] == '\r') { |
universe@42 | 236 | content->space[i] = '\n'; i++; |
universe@39 | 237 | } |
universe@42 | 238 | if (content->space[i] == '\n' && i+1 < content->size) { |
universe@42 | 239 | ucx_list_append(inputlines, content->space+i+1); |
universe@39 | 240 | } |
universe@39 | 241 | } |
universe@39 | 242 | |
universe@46 | 243 | formatlines(hltr, inputlines, |
universe@46 | 244 | (write_func) fwrite, fout, settings.showlinenumbers); |
universe@45 | 245 | |
universe@42 | 246 | ucx_buffer_free(content); |
universe@22 | 247 | } else { |
universe@22 | 248 | perror("Error opening input file"); |
universe@43 | 249 | if (fout != stdout) { |
universe@43 | 250 | fclose(fout); |
universe@43 | 251 | } |
universe@43 | 252 | return EXIT_FAILURE; |
olaf@24 | 253 | } |
olaf@24 | 254 | |
universe@43 | 255 | /* Append footer file */ |
universe@53 | 256 | if (appendfile(settings.footerfile, fout, filebuf, FILEBUF_SIZE, |
universe@53 | 257 | "Error opening footer file")) { |
universe@53 | 258 | return EXIT_FAILURE; |
universe@22 | 259 | } |
universe@22 | 260 | |
universe@42 | 261 | free(filebuf); |
universe@19 | 262 | |
universe@43 | 263 | return EXIT_SUCCESS; |
universe@11 | 264 | } |
universe@1 | 265 | } |
universe@1 | 266 |