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