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