Thu, 23 Jan 2014 14:44:20 +0100
fixed licenses
universe@23 | 1 | /* |
universe@23 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@23 | 3 | * |
universe@23 | 4 | * Copyright 2014 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 <errno.h> |
universe@1 | 30 | |
universe@22 | 31 | #include "c2html.h" |
universe@1 | 32 | |
universe@1 | 33 | inputfile_t *inputfilebuffer(size_t capacity) { |
universe@19 | 34 | inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t)); |
universe@19 | 35 | inputfile->lines = (char**) malloc(capacity * sizeof(char*)); |
universe@19 | 36 | inputfile->capacity = capacity; |
universe@19 | 37 | inputfile->count = 0; |
universe@19 | 38 | inputfile->maxlinewidth = 0; |
universe@19 | 39 | |
universe@19 | 40 | return inputfile; |
universe@0 | 41 | } |
universe@0 | 42 | |
universe@1 | 43 | void addline(inputfile_t *inputfile, char* line, size_t width) { |
universe@19 | 44 | char *l = (char*) malloc(width+1); |
universe@19 | 45 | memcpy(l, line, width); |
universe@19 | 46 | l[width] = 0; |
universe@19 | 47 | if (inputfile->count >= inputfile->capacity) { |
universe@19 | 48 | inputfile->capacity <<= 1; |
universe@19 | 49 | inputfile->lines = realloc(inputfile->lines, inputfile->capacity); |
universe@19 | 50 | } |
universe@19 | 51 | inputfile->lines[inputfile->count] = l; |
universe@19 | 52 | inputfile->maxlinewidth = |
universe@19 | 53 | width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth; |
universe@19 | 54 | inputfile->count++; |
universe@1 | 55 | } |
universe@1 | 56 | |
universe@1 | 57 | void freeinputfilebuffer(inputfile_t *inputfile) { |
universe@19 | 58 | for (int i = 0 ; i < inputfile->count ; i++) { |
universe@19 | 59 | free(inputfile->lines[i]); |
universe@19 | 60 | } |
universe@19 | 61 | free(inputfile->lines); |
universe@19 | 62 | free(inputfile); |
universe@1 | 63 | } |
universe@1 | 64 | |
universe@1 | 65 | inputfile_t *readinput(char *filename) { |
universe@1 | 66 | |
universe@19 | 67 | int fd = open(filename, O_RDONLY); |
universe@19 | 68 | if (fd == -1) return NULL; |
universe@1 | 69 | |
universe@19 | 70 | inputfile_t *inputfile = inputfilebuffer(512); |
universe@19 | 71 | |
universe@19 | 72 | char buf[INPUTBUF_SIZE]; |
universe@19 | 73 | ssize_t r; |
universe@19 | 74 | |
universe@19 | 75 | size_t maxlinewidth = 256; |
universe@19 | 76 | char *line = (char*) malloc(maxlinewidth); |
universe@19 | 77 | size_t col = 0; |
universe@19 | 78 | |
universe@19 | 79 | while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) { |
universe@19 | 80 | for (size_t i = 0 ; i < r ; i++) { |
universe@19 | 81 | if (col >= maxlinewidth-4) { |
universe@19 | 82 | maxlinewidth <<= 1; |
universe@19 | 83 | line = realloc(line, maxlinewidth); |
universe@19 | 84 | } |
universe@19 | 85 | |
universe@19 | 86 | if (buf[i] == '\n') { |
universe@19 | 87 | line[col++] = '\n'; |
universe@19 | 88 | line[col] = 0; |
universe@19 | 89 | addline(inputfile, line, col); |
universe@19 | 90 | col = 0; |
universe@19 | 91 | } else { |
universe@19 | 92 | line[col++] = buf[i]; |
universe@19 | 93 | } |
universe@19 | 94 | } |
universe@1 | 95 | } |
universe@19 | 96 | |
universe@19 | 97 | free(line); |
universe@19 | 98 | |
universe@19 | 99 | close(fd); |
universe@19 | 100 | |
universe@19 | 101 | return inputfile; |
universe@1 | 102 | } |
universe@1 | 103 | |
universe@1 | 104 | void printhelp() { |
universe@19 | 105 | printf("Formats source code using HTML.\n\nUsage:\n" |
universe@19 | 106 | " c2html [Options] FILE\n\n" |
universe@19 | 107 | " Options:\n" |
universe@19 | 108 | " -h Prints this help message\n" |
universe@19 | 109 | " -j Highlight Java instead of C source code\n" |
universe@19 | 110 | " -o <output> Output file (stdout, if not specified)\n" |
universe@22 | 111 | " -H <header> Prepend header file\n" |
universe@22 | 112 | " -F <footer> Append footer file\n" |
universe@19 | 113 | " -p Disable highlighting (plain text)\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 { |
universe@22 | 140 | return errno; |
universe@22 | 141 | } |
universe@22 | 142 | } |
universe@22 | 143 | |
universe@1 | 144 | int main(int argc, char** argv) { |
universe@22 | 145 | int retcode = EXIT_SUCCESS; |
universe@22 | 146 | |
universe@19 | 147 | settings_t settings; |
universe@22 | 148 | memset(&settings, 0, sizeof(settings)); |
universe@19 | 149 | settings.highlight = 1; |
universe@22 | 150 | |
universe@19 | 151 | highlighter_t highlighter; |
universe@20 | 152 | memset(&highlighter, 0, sizeof(highlighter)); |
universe@19 | 153 | highlighter.isdirective = iscdirective; |
universe@19 | 154 | highlighter.istype = isctype; |
universe@19 | 155 | highlighter.keywords = ckeywords; |
universe@21 | 156 | highlighter.parser = cparseline; |
universe@19 | 157 | |
universe@19 | 158 | char optc; |
universe@22 | 159 | while ((optc = getopt(argc, argv, "hjo:pH:F:")) != -1) { |
universe@19 | 160 | switch (optc) { |
universe@19 | 161 | case 'o': |
universe@19 | 162 | if (!(optarg[0] == '-' && optarg[1] == 0)) { |
universe@19 | 163 | settings.outfilename = optarg; |
universe@19 | 164 | } |
universe@19 | 165 | break; |
universe@22 | 166 | case 'F': |
universe@22 | 167 | settings.footerfile = optarg; |
universe@22 | 168 | break; |
universe@22 | 169 | case 'H': |
universe@22 | 170 | settings.headerfile = optarg; |
universe@22 | 171 | break; |
universe@19 | 172 | case 'j': |
universe@19 | 173 | highlighter.isdirective = isjdirective; |
universe@19 | 174 | highlighter.istype = isjtype; |
universe@19 | 175 | highlighter.keywords = jkeywords; |
universe@21 | 176 | highlighter.parser = jparseline; |
universe@19 | 177 | break; |
universe@19 | 178 | case 'p': |
universe@19 | 179 | settings.highlight = 0; |
universe@19 | 180 | break; |
universe@19 | 181 | case 'h': |
universe@19 | 182 | printhelp(); |
universe@19 | 183 | return 0; |
universe@19 | 184 | default: |
universe@19 | 185 | return 1; |
universe@11 | 186 | } |
universe@19 | 187 | } |
universe@19 | 188 | |
universe@19 | 189 | if (optind != argc-1) { |
universe@11 | 190 | printhelp(); |
universe@19 | 191 | return 1; |
universe@19 | 192 | } else { |
universe@19 | 193 | settings.infilename = argv[optind]; |
universe@22 | 194 | FILE *fout; |
universe@22 | 195 | if (settings.outfilename) { |
universe@22 | 196 | fout = fopen(settings.outfilename, "w"); |
universe@22 | 197 | if (!fout) { |
universe@22 | 198 | perror("Error opening output file"); |
universe@22 | 199 | return errno; |
universe@22 | 200 | } |
universe@22 | 201 | } else { |
universe@22 | 202 | fout = stdout; |
universe@22 | 203 | } |
universe@22 | 204 | |
universe@22 | 205 | if (copyfile(settings.headerfile, fout)) { |
universe@22 | 206 | perror("Error opening header file"); |
universe@22 | 207 | retcode = errno; |
universe@22 | 208 | goto prog_end; |
universe@22 | 209 | } |
universe@19 | 210 | |
universe@19 | 211 | inputfile_t *inputfile = readinput(settings.infilename); |
universe@19 | 212 | if (inputfile) { |
universe@19 | 213 | char *line; |
universe@19 | 214 | if (settings.highlight) { |
universe@19 | 215 | line = (char*) malloc(inputfile->maxlinewidth*64); |
universe@19 | 216 | } else { |
universe@19 | 217 | line = NULL; |
universe@19 | 218 | } |
universe@19 | 219 | fprintf(fout, "<pre>\n"); |
universe@19 | 220 | int lnw = lnint(inputfile->count); |
universe@19 | 221 | for (int i = 0 ; i < inputfile->count ; i++) { |
universe@19 | 222 | if (settings.highlight) { |
universe@21 | 223 | highlighter.parser(inputfile->lines[i], line, &highlighter); |
universe@19 | 224 | } else { |
universe@19 | 225 | line = inputfile->lines[i]; |
universe@19 | 226 | } |
universe@19 | 227 | fprintf(fout, "<span class=\"c2html-lineno\">%*d:</span> %s", |
universe@19 | 228 | lnw, i+1, line); |
universe@19 | 229 | } |
universe@19 | 230 | if (settings.highlight) { |
universe@19 | 231 | free(line); |
universe@19 | 232 | } |
universe@19 | 233 | fprintf(fout, "</pre>\n"); |
universe@19 | 234 | |
universe@22 | 235 | freeinputfilebuffer(inputfile); |
universe@22 | 236 | |
universe@22 | 237 | if (copyfile(settings.footerfile, fout)) { |
universe@22 | 238 | perror("Error opening footer file"); |
universe@22 | 239 | retcode = errno; |
universe@19 | 240 | } |
universe@22 | 241 | } else { |
universe@22 | 242 | perror("Error opening input file"); |
universe@22 | 243 | retcode = errno; |
universe@22 | 244 | } |
universe@22 | 245 | |
universe@22 | 246 | prog_end: |
universe@22 | 247 | if (fout != stdout) { |
universe@22 | 248 | fclose(fout); |
universe@19 | 249 | } |
universe@19 | 250 | |
universe@22 | 251 | return retcode; |
universe@11 | 252 | } |
universe@1 | 253 | } |
universe@1 | 254 |