Tue, 23 Aug 2016 14:13:46 +0200
increases input buffer + adds regression tests
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@22 | 29 | #include "c2html.h" |
universe@1 | 30 | |
universe@39 | 31 | #include "ucx/buffer.h" |
universe@39 | 32 | #include "ucx/list.h" |
universe@39 | 33 | #include "ucx/utils.h" |
universe@1 | 34 | |
universe@1 | 35 | void printhelp() { |
universe@19 | 36 | printf("Formats source code using HTML.\n\nUsage:\n" |
universe@19 | 37 | " c2html [Options] FILE\n\n" |
universe@19 | 38 | " Options:\n" |
universe@19 | 39 | " -h Prints this help message\n" |
universe@19 | 40 | " -j Highlight Java instead of C source code\n" |
universe@19 | 41 | " -o <output> Output file (stdout, if not specified)\n" |
universe@22 | 42 | " -H <header> Prepend header file\n" |
universe@22 | 43 | " -F <footer> Append footer file\n" |
universe@19 | 44 | " -p Disable highlighting (plain text)\n" |
olaf@24 | 45 | " -l Disable line numbers\n" |
universe@37 | 46 | " -V, -v Prints version and exits\n" |
universe@19 | 47 | "\n"); |
universe@1 | 48 | } |
universe@1 | 49 | |
universe@22 | 50 | int copyfile(char *filename, FILE *dest) { |
universe@22 | 51 | if (!filename) { |
universe@22 | 52 | return 0; |
universe@22 | 53 | } |
universe@22 | 54 | |
universe@22 | 55 | FILE *src = fopen(filename, "r"); |
universe@22 | 56 | if (src) { |
universe@22 | 57 | char buf[4096]; |
universe@22 | 58 | int r; |
universe@22 | 59 | while ((r = fread(buf, 1, 4096, src)) > 0) { |
universe@22 | 60 | fwrite(buf, 1, r, dest); |
universe@22 | 61 | } |
universe@22 | 62 | fclose(src); |
universe@22 | 63 | return 0; |
universe@22 | 64 | } else { |
universe@39 | 65 | return 1; |
universe@22 | 66 | } |
universe@22 | 67 | } |
universe@22 | 68 | |
olaf@24 | 69 | #define WRITECONST(stream, out, cstr) out(cstr, 1, sizeof(cstr)-1, stream) |
olaf@24 | 70 | int formatfile( |
olaf@24 | 71 | highlighter_t *highlighter, |
universe@39 | 72 | UcxList *in, |
universe@39 | 73 | write_func out, |
olaf@24 | 74 | void *stream, |
universe@39 | 75 | int showlineno) { |
universe@39 | 76 | size_t lines = ucx_list_size(in); |
olaf@24 | 77 | |
universe@39 | 78 | UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); |
olaf@24 | 79 | if(!line) { |
olaf@24 | 80 | return 1; |
olaf@24 | 81 | } |
olaf@24 | 82 | WRITECONST(stream, out, "<pre>\n"); |
olaf@24 | 83 | |
universe@39 | 84 | int lnw; |
universe@39 | 85 | { |
universe@39 | 86 | lnw = 1; |
universe@39 | 87 | int p = 1; |
universe@39 | 88 | while ((p*=10) < lines) lnw++; |
universe@39 | 89 | } |
universe@39 | 90 | |
universe@39 | 91 | size_t lineno = 0; |
universe@39 | 92 | UCX_FOREACH(sourceline, in) { |
universe@39 | 93 | lineno++; |
universe@39 | 94 | /* TODO: backwards compatibility: replace line->space in all occasions |
universe@39 | 95 | * and use UcxBuffer functions |
universe@39 | 96 | */ |
olaf@24 | 97 | if (highlighter) { |
universe@39 | 98 | highlighter->parser(sourceline->data, line->space, highlighter); |
olaf@24 | 99 | } else { |
universe@39 | 100 | char *c = sourceline->data; |
universe@30 | 101 | size_t dp = 0; |
universe@40 | 102 | while (*c && *c != '\n') { |
universe@39 | 103 | dp = writeescapedchar(line->space, dp, *c); |
universe@30 | 104 | c++; |
universe@30 | 105 | } |
universe@40 | 106 | line->space[dp++] = '\n'; |
universe@39 | 107 | line->space[dp] = '\0'; |
olaf@24 | 108 | } |
universe@30 | 109 | |
olaf@24 | 110 | // write line number |
universe@39 | 111 | if (showlineno) { |
olaf@24 | 112 | WRITECONST(stream, out, "<span class=\"c2html-lineno\">"); |
universe@27 | 113 | char lnbuf[128]; |
universe@27 | 114 | int len; |
universe@27 | 115 | // line number link |
universe@27 | 116 | len = snprintf(lnbuf, 128, "<a name=\"l%d\" href=\"#l%d\">", |
universe@39 | 117 | lineno, lineno); |
olaf@24 | 118 | out(lnbuf, 1, len, stream); |
universe@27 | 119 | // justified line number |
universe@39 | 120 | len = snprintf(lnbuf, 128, "%*d ", lnw, lineno); |
universe@27 | 121 | out(lnbuf, 1, len, stream); |
universe@27 | 122 | WRITECONST(stream, out, "</a></span> "); |
olaf@24 | 123 | } |
olaf@24 | 124 | |
olaf@24 | 125 | // write formated (or plain) code line |
universe@39 | 126 | out(line->space, 1, strlen(line->space), stream); |
olaf@24 | 127 | } |
olaf@24 | 128 | |
olaf@24 | 129 | WRITECONST(stream, out, "</pre>\n"); |
universe@39 | 130 | ucx_buffer_free(line); |
olaf@24 | 131 | return 0; |
olaf@24 | 132 | } |
olaf@24 | 133 | |
olaf@24 | 134 | void init_c_highlighter(highlighter_t *highlighter) { |
olaf@24 | 135 | memset(highlighter, 0, sizeof(highlighter_t)); |
universe@36 | 136 | highlighter->isdirective = check_cdirective; |
universe@36 | 137 | highlighter->istype = check_ctype; |
olaf@24 | 138 | highlighter->keywords = ckeywords; |
olaf@24 | 139 | highlighter->parser = cparseline; |
olaf@24 | 140 | } |
olaf@24 | 141 | |
olaf@24 | 142 | void init_java_highlighter(highlighter_t *highlighter) { |
olaf@24 | 143 | memset(highlighter, 0, sizeof(highlighter_t)); |
universe@36 | 144 | highlighter->isdirective = check_jdirective; |
universe@36 | 145 | highlighter->istype = check_jtype; |
olaf@24 | 146 | highlighter->keywords = jkeywords; |
olaf@24 | 147 | highlighter->parser = jparseline; |
olaf@24 | 148 | } |
olaf@24 | 149 | |
universe@39 | 150 | enum source_type { |
universe@39 | 151 | SOURCE_C, |
universe@39 | 152 | SOURCE_JAVA, |
universe@39 | 153 | SOURCE_PLAIN |
universe@39 | 154 | }; |
universe@39 | 155 | |
universe@1 | 156 | int main(int argc, char** argv) { |
universe@22 | 157 | int retcode = EXIT_SUCCESS; |
universe@22 | 158 | |
universe@39 | 159 | Settings settings; |
universe@22 | 160 | memset(&settings, 0, sizeof(settings)); |
olaf@24 | 161 | settings.showlinenumbers = 1; |
universe@22 | 162 | |
universe@39 | 163 | enum source_type sourcetype = SOURCE_C; |
universe@19 | 164 | |
universe@19 | 165 | char optc; |
universe@37 | 166 | while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) { |
universe@19 | 167 | switch (optc) { |
universe@19 | 168 | case 'o': |
universe@19 | 169 | if (!(optarg[0] == '-' && optarg[1] == 0)) { |
universe@19 | 170 | settings.outfilename = optarg; |
universe@19 | 171 | } |
universe@19 | 172 | break; |
universe@22 | 173 | case 'F': |
universe@22 | 174 | settings.footerfile = optarg; |
universe@22 | 175 | break; |
universe@22 | 176 | case 'H': |
universe@22 | 177 | settings.headerfile = optarg; |
universe@22 | 178 | break; |
universe@19 | 179 | case 'j': |
universe@39 | 180 | sourcetype = SOURCE_JAVA; |
universe@19 | 181 | break; |
universe@19 | 182 | case 'p': |
universe@39 | 183 | sourcetype = SOURCE_PLAIN; |
universe@19 | 184 | break; |
olaf@24 | 185 | case 'l': |
olaf@24 | 186 | settings.showlinenumbers = 0; |
olaf@24 | 187 | break; |
universe@19 | 188 | case 'h': |
universe@19 | 189 | printhelp(); |
universe@38 | 190 | return EXIT_SUCCESS; |
universe@37 | 191 | case 'v': |
universe@37 | 192 | case 'V': |
universe@37 | 193 | #ifdef VERSION_DEVELOP |
universe@37 | 194 | printf("%d.%d (unstable)\n", VERSION_MAJOR, VERSION_MINOR); |
universe@37 | 195 | #else |
universe@37 | 196 | printf("%d.%d\n", VERSION_MAJOR, VERSION_MINOR); |
universe@37 | 197 | #endif |
universe@38 | 198 | return EXIT_SUCCESS; |
universe@19 | 199 | default: |
universe@38 | 200 | return EXIT_FAILURE; |
universe@11 | 201 | } |
universe@19 | 202 | } |
universe@19 | 203 | |
universe@19 | 204 | if (optind != argc-1) { |
universe@11 | 205 | printhelp(); |
universe@19 | 206 | return 1; |
universe@19 | 207 | } else { |
universe@19 | 208 | settings.infilename = argv[optind]; |
universe@22 | 209 | FILE *fout; |
universe@22 | 210 | if (settings.outfilename) { |
universe@22 | 211 | fout = fopen(settings.outfilename, "w"); |
universe@22 | 212 | if (!fout) { |
universe@22 | 213 | perror("Error opening output file"); |
universe@38 | 214 | return EXIT_FAILURE; |
universe@22 | 215 | } |
universe@22 | 216 | } else { |
universe@22 | 217 | fout = stdout; |
universe@22 | 218 | } |
universe@22 | 219 | |
universe@22 | 220 | if (copyfile(settings.headerfile, fout)) { |
universe@22 | 221 | perror("Error opening header file"); |
universe@38 | 222 | retcode = EXIT_FAILURE; |
universe@22 | 223 | goto prog_end; |
universe@22 | 224 | } |
olaf@24 | 225 | |
olaf@24 | 226 | highlighter_t highlighter; |
olaf@24 | 227 | highlighter_t *hptr = &highlighter; |
universe@39 | 228 | switch (sourcetype) { |
universe@39 | 229 | case SOURCE_C: |
olaf@24 | 230 | init_c_highlighter(&highlighter); |
olaf@24 | 231 | break; |
universe@39 | 232 | case SOURCE_JAVA: |
olaf@24 | 233 | init_java_highlighter(&highlighter); |
olaf@24 | 234 | break; |
universe@39 | 235 | case SOURCE_PLAIN: |
olaf@24 | 236 | hptr = NULL; |
olaf@24 | 237 | break; |
universe@39 | 238 | default: /* should be unreachable */ |
universe@39 | 239 | fprintf(stderr, "error in enum source_type\n"); |
universe@39 | 240 | retcode = EXIT_FAILURE; |
universe@39 | 241 | goto prog_end; |
olaf@24 | 242 | } |
universe@19 | 243 | |
universe@39 | 244 | FILE *inputfile = fopen(settings.infilename, "r"); |
universe@19 | 245 | if (inputfile) { |
universe@39 | 246 | UcxBuffer *filebuf = ucx_buffer_new(NULL, |
universe@41 | 247 | 8192, UCX_BUFFER_AUTOEXTEND); |
universe@39 | 248 | { |
universe@41 | 249 | const size_t tmpbufsize = 4096; |
universe@39 | 250 | char *tmpbuf = malloc(tmpbufsize); |
universe@39 | 251 | ucx_stream_copy(inputfile, filebuf, (read_func) fread, |
universe@39 | 252 | (write_func) ucx_buffer_write, |
universe@39 | 253 | tmpbuf, tmpbufsize, (size_t)-1); |
universe@39 | 254 | free(tmpbuf); |
universe@39 | 255 | } |
universe@39 | 256 | fclose(inputfile); |
universe@39 | 257 | |
universe@39 | 258 | UcxList *inputlines = ucx_list_append(NULL, filebuf->space); |
universe@39 | 259 | for (size_t i = 1 ; i < filebuf->size ; i++) { |
universe@39 | 260 | if (filebuf->space[i] == '\r') { |
universe@39 | 261 | filebuf->space[i] = '\n'; i++; |
universe@39 | 262 | } |
universe@39 | 263 | if (filebuf->space[i] == '\n' && i+1 < filebuf->size) { |
universe@39 | 264 | ucx_list_append(inputlines, filebuf->space+i+1); |
universe@39 | 265 | } |
universe@39 | 266 | } |
universe@39 | 267 | |
olaf@24 | 268 | formatfile( |
olaf@24 | 269 | hptr, |
universe@39 | 270 | inputlines, |
universe@39 | 271 | (write_func) fwrite, |
olaf@24 | 272 | fout, |
olaf@24 | 273 | settings.showlinenumbers); |
universe@39 | 274 | ucx_buffer_free(filebuf); |
universe@22 | 275 | } else { |
universe@22 | 276 | perror("Error opening input file"); |
universe@38 | 277 | retcode = EXIT_FAILURE; |
olaf@24 | 278 | } |
olaf@24 | 279 | |
olaf@24 | 280 | if (copyfile(settings.footerfile, fout)) { |
olaf@24 | 281 | perror("Error opening footer file"); |
universe@38 | 282 | retcode = EXIT_FAILURE; |
universe@22 | 283 | } |
universe@22 | 284 | |
universe@22 | 285 | prog_end: |
universe@22 | 286 | if (fout != stdout) { |
universe@22 | 287 | fclose(fout); |
universe@19 | 288 | } |
universe@19 | 289 | |
universe@22 | 290 | return retcode; |
universe@11 | 291 | } |
universe@1 | 292 | } |
universe@1 | 293 |