Fri, 04 Mar 2016 15:02:22 +0100
fixed possible naming conflicts with is.* functions
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@1 | 31 | inputfile_t *inputfilebuffer(size_t capacity) { |
universe@19 | 32 | inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t)); |
universe@19 | 33 | inputfile->lines = (char**) malloc(capacity * sizeof(char*)); |
universe@19 | 34 | inputfile->capacity = capacity; |
universe@19 | 35 | inputfile->count = 0; |
universe@19 | 36 | inputfile->maxlinewidth = 0; |
universe@19 | 37 | |
universe@19 | 38 | return inputfile; |
universe@0 | 39 | } |
universe@0 | 40 | |
universe@1 | 41 | void addline(inputfile_t *inputfile, char* line, size_t width) { |
universe@19 | 42 | char *l = (char*) malloc(width+1); |
universe@19 | 43 | memcpy(l, line, width); |
universe@19 | 44 | l[width] = 0; |
universe@19 | 45 | if (inputfile->count >= inputfile->capacity) { |
universe@19 | 46 | inputfile->capacity <<= 1; |
universe@25 | 47 | inputfile->lines = realloc(inputfile->lines, |
universe@25 | 48 | sizeof(char*)*inputfile->capacity); |
universe@19 | 49 | } |
universe@19 | 50 | inputfile->lines[inputfile->count] = l; |
universe@19 | 51 | inputfile->maxlinewidth = |
universe@19 | 52 | width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth; |
universe@19 | 53 | inputfile->count++; |
universe@1 | 54 | } |
universe@1 | 55 | |
universe@1 | 56 | void freeinputfilebuffer(inputfile_t *inputfile) { |
universe@19 | 57 | for (int i = 0 ; i < inputfile->count ; i++) { |
universe@19 | 58 | free(inputfile->lines[i]); |
universe@19 | 59 | } |
universe@19 | 60 | free(inputfile->lines); |
universe@19 | 61 | free(inputfile); |
universe@1 | 62 | } |
universe@1 | 63 | |
universe@1 | 64 | inputfile_t *readinput(char *filename) { |
universe@1 | 65 | |
universe@19 | 66 | int fd = open(filename, O_RDONLY); |
universe@19 | 67 | if (fd == -1) return NULL; |
universe@1 | 68 | |
universe@19 | 69 | inputfile_t *inputfile = inputfilebuffer(512); |
universe@19 | 70 | |
universe@19 | 71 | char buf[INPUTBUF_SIZE]; |
universe@19 | 72 | ssize_t r; |
universe@19 | 73 | |
universe@19 | 74 | size_t maxlinewidth = 256; |
universe@19 | 75 | char *line = (char*) malloc(maxlinewidth); |
universe@19 | 76 | size_t col = 0; |
universe@19 | 77 | |
universe@19 | 78 | while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) { |
universe@19 | 79 | for (size_t i = 0 ; i < r ; i++) { |
universe@19 | 80 | if (col >= maxlinewidth-4) { |
universe@19 | 81 | maxlinewidth <<= 1; |
universe@19 | 82 | line = realloc(line, maxlinewidth); |
universe@19 | 83 | } |
universe@19 | 84 | |
universe@19 | 85 | if (buf[i] == '\n') { |
universe@19 | 86 | line[col++] = '\n'; |
universe@19 | 87 | line[col] = 0; |
universe@19 | 88 | addline(inputfile, line, col); |
universe@19 | 89 | col = 0; |
universe@19 | 90 | } else { |
universe@19 | 91 | line[col++] = buf[i]; |
universe@19 | 92 | } |
universe@19 | 93 | } |
universe@1 | 94 | } |
universe@19 | 95 | |
universe@19 | 96 | free(line); |
universe@19 | 97 | |
universe@19 | 98 | close(fd); |
universe@19 | 99 | |
universe@19 | 100 | return inputfile; |
universe@1 | 101 | } |
universe@1 | 102 | |
universe@1 | 103 | void printhelp() { |
universe@19 | 104 | printf("Formats source code using HTML.\n\nUsage:\n" |
universe@19 | 105 | " c2html [Options] FILE\n\n" |
universe@19 | 106 | " Options:\n" |
universe@19 | 107 | " -h Prints this help message\n" |
universe@19 | 108 | " -j Highlight Java instead of C source code\n" |
universe@19 | 109 | " -o <output> Output file (stdout, if not specified)\n" |
universe@22 | 110 | " -H <header> Prepend header file\n" |
universe@22 | 111 | " -F <footer> Append footer file\n" |
universe@19 | 112 | " -p Disable highlighting (plain text)\n" |
olaf@24 | 113 | " -l Disable line numbers\n" |
universe@19 | 114 | "\n"); |
universe@19 | 115 | |
universe@19 | 116 | |
universe@1 | 117 | } |
universe@1 | 118 | |
universe@4 | 119 | int lnint(size_t lnc) { |
universe@19 | 120 | int w = 1, p = 1; |
universe@19 | 121 | while ((p*=10) < lnc) w++; |
universe@19 | 122 | return w; |
universe@1 | 123 | } |
universe@1 | 124 | |
universe@22 | 125 | int copyfile(char *filename, FILE *dest) { |
universe@22 | 126 | if (!filename) { |
universe@22 | 127 | return 0; |
universe@22 | 128 | } |
universe@22 | 129 | |
universe@22 | 130 | FILE *src = fopen(filename, "r"); |
universe@22 | 131 | if (src) { |
universe@22 | 132 | char buf[4096]; |
universe@22 | 133 | int r; |
universe@22 | 134 | while ((r = fread(buf, 1, 4096, src)) > 0) { |
universe@22 | 135 | fwrite(buf, 1, r, dest); |
universe@22 | 136 | } |
universe@22 | 137 | fclose(src); |
universe@22 | 138 | return 0; |
universe@22 | 139 | } else { |
olaf@24 | 140 | return -1; |
universe@22 | 141 | } |
universe@22 | 142 | } |
universe@22 | 143 | |
olaf@24 | 144 | #define WRITECONST(stream, out, cstr) out(cstr, 1, sizeof(cstr)-1, stream) |
olaf@24 | 145 | int formatfile( |
olaf@24 | 146 | highlighter_t *highlighter, |
olaf@24 | 147 | inputfile_t *in, |
olaf@24 | 148 | fmt_write_func out, |
olaf@24 | 149 | void *stream, |
olaf@24 | 150 | _Bool showln) { |
olaf@24 | 151 | // formats an input file and writes the result to out |
olaf@24 | 152 | |
olaf@24 | 153 | char *line = malloc(in->maxlinewidth*64); |
olaf@24 | 154 | if(!line) { |
olaf@24 | 155 | return 1; |
olaf@24 | 156 | } |
olaf@24 | 157 | WRITECONST(stream, out, "<pre>\n"); |
olaf@24 | 158 | |
olaf@24 | 159 | int lnw = lnint(in->count); |
olaf@24 | 160 | for (int i = 0 ; i < in->count ; i++) { |
olaf@24 | 161 | if (highlighter) { |
olaf@24 | 162 | highlighter->parser(in->lines[i], line, highlighter); |
olaf@24 | 163 | } else { |
universe@30 | 164 | char *c = in->lines[i]; |
universe@30 | 165 | size_t dp = 0; |
universe@30 | 166 | while (*c) { |
universe@30 | 167 | dp = writeescapedchar(line, dp, *c); |
universe@30 | 168 | c++; |
universe@30 | 169 | } |
universe@30 | 170 | line[dp] = '\0'; |
olaf@24 | 171 | } |
universe@30 | 172 | |
olaf@24 | 173 | // write line number |
olaf@24 | 174 | if (showln) { |
olaf@24 | 175 | WRITECONST(stream, out, "<span class=\"c2html-lineno\">"); |
universe@27 | 176 | char lnbuf[128]; |
universe@27 | 177 | int len; |
universe@27 | 178 | // line number link |
universe@27 | 179 | len = snprintf(lnbuf, 128, "<a name=\"l%d\" href=\"#l%d\">", |
universe@27 | 180 | i+1, i+1); |
olaf@24 | 181 | out(lnbuf, 1, len, stream); |
universe@27 | 182 | // justified line number |
universe@27 | 183 | len = snprintf(lnbuf, 128, "%*d ", lnw, i+1); |
universe@27 | 184 | out(lnbuf, 1, len, stream); |
universe@27 | 185 | WRITECONST(stream, out, "</a></span> "); |
olaf@24 | 186 | } |
olaf@24 | 187 | |
olaf@24 | 188 | // write formated (or plain) code line |
universe@30 | 189 | out(line, 1, strlen(line), stream); |
olaf@24 | 190 | } |
olaf@24 | 191 | |
olaf@24 | 192 | WRITECONST(stream, out, "</pre>\n"); |
olaf@24 | 193 | free(line); |
olaf@24 | 194 | return 0; |
olaf@24 | 195 | } |
olaf@24 | 196 | |
olaf@24 | 197 | void init_c_highlighter(highlighter_t *highlighter) { |
olaf@24 | 198 | memset(highlighter, 0, sizeof(highlighter_t)); |
universe@36 | 199 | highlighter->isdirective = check_cdirective; |
universe@36 | 200 | highlighter->istype = check_ctype; |
olaf@24 | 201 | highlighter->keywords = ckeywords; |
olaf@24 | 202 | highlighter->parser = cparseline; |
olaf@24 | 203 | } |
olaf@24 | 204 | |
olaf@24 | 205 | void init_java_highlighter(highlighter_t *highlighter) { |
olaf@24 | 206 | memset(highlighter, 0, sizeof(highlighter_t)); |
universe@36 | 207 | highlighter->isdirective = check_jdirective; |
universe@36 | 208 | highlighter->istype = check_jtype; |
olaf@24 | 209 | highlighter->keywords = jkeywords; |
olaf@24 | 210 | highlighter->parser = jparseline; |
olaf@24 | 211 | } |
olaf@24 | 212 | |
universe@1 | 213 | int main(int argc, char** argv) { |
universe@22 | 214 | int retcode = EXIT_SUCCESS; |
universe@22 | 215 | |
universe@19 | 216 | settings_t settings; |
universe@22 | 217 | memset(&settings, 0, sizeof(settings)); |
universe@19 | 218 | settings.highlight = 1; |
olaf@24 | 219 | settings.showlinenumbers = 1; |
universe@22 | 220 | |
olaf@24 | 221 | int lang = C2HTML_C; |
universe@19 | 222 | |
universe@19 | 223 | char optc; |
olaf@24 | 224 | while ((optc = getopt(argc, argv, "hljo:pH:F:")) != -1) { |
universe@19 | 225 | switch (optc) { |
universe@19 | 226 | case 'o': |
universe@19 | 227 | if (!(optarg[0] == '-' && optarg[1] == 0)) { |
universe@19 | 228 | settings.outfilename = optarg; |
universe@19 | 229 | } |
universe@19 | 230 | break; |
universe@22 | 231 | case 'F': |
universe@22 | 232 | settings.footerfile = optarg; |
universe@22 | 233 | break; |
universe@22 | 234 | case 'H': |
universe@22 | 235 | settings.headerfile = optarg; |
universe@22 | 236 | break; |
universe@19 | 237 | case 'j': |
olaf@24 | 238 | lang = C2HTML_JAVA; |
universe@19 | 239 | break; |
universe@19 | 240 | case 'p': |
universe@19 | 241 | settings.highlight = 0; |
universe@19 | 242 | break; |
olaf@24 | 243 | case 'l': |
olaf@24 | 244 | settings.showlinenumbers = 0; |
olaf@24 | 245 | break; |
universe@19 | 246 | case 'h': |
universe@19 | 247 | printhelp(); |
universe@19 | 248 | return 0; |
universe@19 | 249 | default: |
universe@19 | 250 | return 1; |
universe@11 | 251 | } |
universe@19 | 252 | } |
universe@19 | 253 | |
universe@19 | 254 | if (optind != argc-1) { |
universe@11 | 255 | printhelp(); |
universe@19 | 256 | return 1; |
universe@19 | 257 | } else { |
universe@19 | 258 | settings.infilename = argv[optind]; |
universe@22 | 259 | FILE *fout; |
universe@22 | 260 | if (settings.outfilename) { |
universe@22 | 261 | fout = fopen(settings.outfilename, "w"); |
universe@22 | 262 | if (!fout) { |
universe@22 | 263 | perror("Error opening output file"); |
olaf@24 | 264 | return -1; |
universe@22 | 265 | } |
universe@22 | 266 | } else { |
universe@22 | 267 | fout = stdout; |
universe@22 | 268 | } |
universe@22 | 269 | |
universe@22 | 270 | if (copyfile(settings.headerfile, fout)) { |
universe@22 | 271 | perror("Error opening header file"); |
olaf@24 | 272 | retcode = -1; |
universe@22 | 273 | goto prog_end; |
universe@22 | 274 | } |
olaf@24 | 275 | |
olaf@24 | 276 | highlighter_t highlighter; |
olaf@24 | 277 | highlighter_t *hptr = &highlighter; |
olaf@24 | 278 | switch (lang) { |
olaf@24 | 279 | case C2HTML_C: |
olaf@24 | 280 | init_c_highlighter(&highlighter); |
olaf@24 | 281 | break; |
olaf@24 | 282 | case C2HTML_JAVA: |
olaf@24 | 283 | init_java_highlighter(&highlighter); |
olaf@24 | 284 | break; |
olaf@24 | 285 | default: |
olaf@24 | 286 | hptr = NULL; |
olaf@24 | 287 | break; |
olaf@24 | 288 | } |
olaf@24 | 289 | if (!settings.highlight) { |
olaf@24 | 290 | hptr = NULL; |
olaf@24 | 291 | } |
universe@19 | 292 | |
universe@19 | 293 | inputfile_t *inputfile = readinput(settings.infilename); |
universe@19 | 294 | if (inputfile) { |
olaf@24 | 295 | formatfile( |
olaf@24 | 296 | hptr, |
olaf@24 | 297 | inputfile, |
olaf@24 | 298 | (fmt_write_func)fwrite, |
olaf@24 | 299 | fout, |
olaf@24 | 300 | settings.showlinenumbers); |
universe@25 | 301 | freeinputfilebuffer(inputfile); |
universe@22 | 302 | } else { |
universe@22 | 303 | perror("Error opening input file"); |
olaf@24 | 304 | retcode = -1; |
olaf@24 | 305 | } |
olaf@24 | 306 | |
olaf@24 | 307 | if (copyfile(settings.footerfile, fout)) { |
olaf@24 | 308 | perror("Error opening footer file"); |
olaf@24 | 309 | retcode = -1; |
universe@22 | 310 | } |
universe@22 | 311 | |
universe@22 | 312 | prog_end: |
universe@22 | 313 | if (fout != stdout) { |
universe@22 | 314 | fclose(fout); |
universe@19 | 315 | } |
universe@19 | 316 | |
universe@22 | 317 | return retcode; |
universe@11 | 318 | } |
universe@1 | 319 | } |
universe@1 | 320 |