src/c2html.c

Wed, 10 Jul 2013 14:38:56 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 10 Jul 2013 14:38:56 +0200
changeset 15
398a7589297f
parent 14
b33629bf4b58
child 16
fa0bcd0444eb
permissions
-rw-r--r--

double free fix

universe@1 1 /*
universe@1 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@1 3 *
universe@1 4 * Copyright 2013 Mike Becker. All rights reserved.
universe@1 5 *
universe@1 6 * Redistribution and use in source and binary forms, with or without
universe@1 7 * modification, are permitted provided that the following conditions are met:
universe@1 8 *
universe@1 9 * 1. Redistributions of source code must retain the above copyright
universe@1 10 * notice, this list of conditions and the following disclaimer.
universe@1 11 *
universe@1 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@1 13 * notice, this list of conditions and the following disclaimer in the
universe@1 14 * documentation and/or other materials provided with the distribution.
universe@1 15 *
universe@1 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@1 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@1 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@1 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@1 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@1 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@1 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@1 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@1 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@1 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@1 26 * POSSIBILITY OF SUCH DAMAGE.
universe@1 27 *
universe@1 28 */
universe@1 29
universe@1 30 #include <stdio.h>
universe@1 31 #include <stdlib.h>
universe@1 32 #include <string.h>
universe@1 33 #include <fcntl.h>
universe@1 34 #include <unistd.h>
universe@4 35 #include <ctype.h>
universe@4 36
universe@4 37 #define INPUTBUF_SIZE 2048
universe@5 38 #define WORDBUF_SIZE 16
universe@5 39
universe@7 40 #define istype(word, len) (word[len-2] == '_' && word[len-1] == 't')
universe@7 41 #define isdirective(word) (word[0] == '#')
universe@7 42
universe@5 43 const char* keywords[] = {
universe@5 44 "auto", "break", "case", "char", "const", "continue", "default", "do",
universe@5 45 "double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
universe@5 46 "long", "register", "return", "short", "signed", "sizeof", "static", "struct",
universe@5 47 "switch", "typedef", "union", "unsigned", "void", "volatile", "while", NULL
universe@5 48 };
universe@4 49
universe@11 50 typedef struct {
universe@11 51 char* outfilename;
universe@11 52 char* infilename;
universe@12 53 int highlight;
universe@11 54 } settings_t;
universe@4 55
universe@4 56 typedef struct {
universe@4 57 size_t count;
universe@4 58 size_t capacity;
universe@4 59 size_t maxlinewidth;
universe@4 60 char** lines;
universe@4 61 } inputfile_t;
universe@1 62
universe@1 63 inputfile_t *inputfilebuffer(size_t capacity) {
universe@1 64 inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t));
universe@1 65 inputfile->lines = (char**) malloc(capacity * sizeof(char*));
universe@1 66 inputfile->capacity = capacity;
universe@1 67 inputfile->count = 0;
universe@4 68 inputfile->maxlinewidth = 0;
universe@1 69
universe@1 70 return inputfile;
universe@0 71 }
universe@0 72
universe@1 73 void addline(inputfile_t *inputfile, char* line, size_t width) {
universe@1 74 char *l = (char*) malloc(width+1);
universe@1 75 memcpy(l, line, width);
universe@1 76 l[width] = 0;
universe@1 77 if (inputfile->count >= inputfile->capacity) {
universe@1 78 inputfile->capacity <<= 1;
universe@1 79 inputfile->lines = realloc(inputfile->lines, inputfile->capacity);
universe@1 80 }
universe@1 81 inputfile->lines[inputfile->count] = l;
universe@4 82 inputfile->maxlinewidth =
universe@4 83 width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth;
universe@1 84 inputfile->count++;
universe@1 85 }
universe@1 86
universe@1 87 void freeinputfilebuffer(inputfile_t *inputfile) {
universe@1 88 for (int i = 0 ; i < inputfile->count ; i++) {
universe@1 89 free(inputfile->lines[i]);
universe@1 90 }
universe@1 91 free(inputfile->lines);
universe@1 92 free(inputfile);
universe@1 93 }
universe@1 94
universe@1 95 inputfile_t *readinput(char *filename) {
universe@1 96
universe@1 97 int fd = open(filename, O_RDONLY);
universe@1 98 if (fd == -1) return NULL;
universe@1 99
universe@1 100 inputfile_t *inputfile = inputfilebuffer(512);
universe@1 101
universe@4 102 char buf[INPUTBUF_SIZE];
universe@1 103 ssize_t r;
universe@1 104
universe@4 105 size_t maxlinewidth = 256;
universe@1 106 char *line = (char*) malloc(maxlinewidth);
universe@1 107 size_t col = 0;
universe@1 108
universe@4 109 while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) {
universe@1 110 for (size_t i = 0 ; i < r ; i++) {
universe@1 111 if (col >= maxlinewidth-4) {
universe@1 112 maxlinewidth <<= 1;
universe@1 113 line = realloc(line, maxlinewidth);
universe@1 114 }
universe@1 115
universe@1 116 if (buf[i] == '\n') {
universe@5 117 line[col++] = '\n';
universe@1 118 line[col] = 0;
universe@1 119 addline(inputfile, line, col);
universe@1 120 col = 0;
universe@1 121 } else {
universe@1 122 line[col++] = buf[i];
universe@1 123 }
universe@1 124 }
universe@1 125 }
universe@1 126
universe@1 127 free(line);
universe@1 128
universe@1 129 close(fd);
universe@1 130
universe@1 131 return inputfile;
universe@1 132 }
universe@1 133
universe@5 134 size_t writeescapedchar(char *dest, size_t dp, char c) {
universe@5 135 if (c == '>') {
universe@5 136 dest[dp++] = '&'; dest[dp++] = 'g';
universe@5 137 dest[dp++] = 't'; dest[dp++] = ';';
universe@5 138 } else if (c == '<') {
universe@5 139 dest[dp++] = '&'; dest[dp++] = 'l';
universe@5 140 dest[dp++] = 't'; dest[dp++] = ';';
universe@5 141 } else {
universe@5 142 dest[dp++] = c;
universe@5 143 }
universe@5 144
universe@5 145 return dp;
universe@5 146 }
universe@5 147
universe@5 148 int iskeyword(char *word) {
universe@5 149 for (int i = 0 ; keywords[i] ; i++) {
universe@5 150 if (strncmp(keywords[i], word, WORDBUF_SIZE) == 0) {
universe@5 151 return 1;
universe@5 152 }
universe@5 153 }
universe@5 154 return 0;
universe@5 155 }
universe@5 156
universe@9 157 int iscapsonly(char *word, size_t wp) {
universe@9 158 for (size_t i = 0 ; i < wp ; i++) {
universe@9 159 if (!isupper(word[i]) && word[i] != '_') {
universe@9 160 return 0;
universe@9 161 }
universe@9 162 }
universe@9 163 return 1;
universe@9 164 }
universe@9 165
universe@4 166 void parseline(char *src, char *dest) {
universe@4 167 size_t sp = 0, dp = 0;
universe@4 168 /* indent */
universe@4 169 while (isspace(src[sp])) {
universe@4 170 dest[dp++] = src[sp++];
universe@4 171 }
universe@10 172
universe@10 173 static char word[WORDBUF_SIZE];
universe@10 174 static char includefile[FILENAME_MAX];
universe@10 175
universe@5 176 memset(word, 0, WORDBUF_SIZE);
universe@10 177 size_t wp = 0, ifp = 0;
universe@10 178 int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
universe@8 179 static int iscommentml;
universe@7 180 int isescaping = 0;
universe@8 181
universe@8 182 if (iscommentml) {
universe@8 183 iscomment = 1;
universe@8 184 memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29);
universe@8 185 dp += 29;
universe@8 186 }
universe@9 187
universe@4 188 for (char c = src[sp] ; c ; c=src[++sp]) {
universe@8 189 /* comments */
universe@8 190 if (c == '/') {
universe@8 191 if (iscommentml && sp > 0 && src[sp-1] == '*') {
universe@8 192 iscomment = 0;
universe@8 193 iscommentml = 0;
universe@8 194 memcpy(&(dest[dp]), "/</span>", 8);
universe@8 195 dp += 8;
universe@8 196 continue;
universe@8 197 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
universe@8 198 iscomment = 1;
universe@8 199 iscommentml = (src[sp+1] == '*');
universe@8 200 memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29);
universe@8 201 dp += 29;
universe@8 202 }
universe@8 203 }
universe@8 204
universe@8 205 if (iscomment) {
universe@8 206 if (c == '\n') {
universe@7 207 memcpy(&(dest[dp]), "</span>", 7);
universe@7 208 dp += 7;
universe@7 209 }
universe@8 210 dp = writeescapedchar(dest, dp, c);
universe@10 211 } else if (isinclude) {
universe@10 212 if (c == '<') {
universe@10 213 memcpy(&(dest[dp]), "<span class=\"c2html-stdinclude\">", 32);
universe@10 214 dp += 32;
universe@10 215 dp = writeescapedchar(dest, dp, c);
universe@10 216 } else if (c == '\"') {
universe@10 217 if (parseinclude) {
universe@10 218 dest[dp++] = '\"';
universe@10 219 dest[dp++] = '>';
universe@10 220 memcpy(&(dest[dp]), includefile, ifp);
universe@10 221 dp += ifp;
universe@10 222
universe@10 223 dp = writeescapedchar(dest, dp, c);
universe@10 224 memcpy(&(dest[dp]), "</a>", 4);
universe@10 225 dp += 4;
universe@10 226 parseinclude = 0;
universe@10 227 } else {
universe@10 228 memcpy(&(dest[dp]), "<a class=\"c2html-userinclude\" href=", 35);
universe@10 229 dp += 35;
universe@10 230 dp = writeescapedchar(dest, dp, c);
universe@10 231 ifp = 0;
universe@10 232 includefile[ifp++] = '\"';
universe@10 233 parseinclude = 1;
universe@10 234 }
universe@10 235 } else if (c == '>') {
universe@10 236 dp = writeescapedchar(dest, dp, c);
universe@10 237 memcpy(&(dest[dp]), "</span>", 7);
universe@10 238 dp += 7;
universe@10 239 } else {
universe@10 240 if (parseinclude) {
universe@10 241 includefile[ifp++] = c;
universe@10 242 }
universe@10 243 dp = writeescapedchar(dest, dp, c);
universe@10 244 }
universe@7 245 } else {
universe@8 246 /* strings */
universe@8 247 if (!isescaping && (c == '\'' || c == '\"')) {
universe@8 248 isstring ^= 1;
universe@8 249 if (isstring) {
universe@8 250 memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28);
universe@8 251 dp += 28;
universe@7 252 dp = writeescapedchar(dest, dp, c);
universe@7 253 } else {
universe@7 254 dp = writeescapedchar(dest, dp, c);
universe@8 255 memcpy(&(dest[dp]), "</span>", 7);
universe@8 256 dp += 7;
universe@8 257 }
universe@8 258 } else {
universe@8 259 if (isstring) {
universe@8 260 dp = writeescapedchar(dest, dp, c);
universe@10 261 } else if (!isalnum(c) && c != '_' && c != '#' && c != '.') {
universe@8 262 /* interpret word int_t */
universe@8 263 if (wp > 0 && wp < WORDBUF_SIZE) {
universe@8 264 int closespan = 1;
universe@8 265 if (iskeyword(word)) {
universe@8 266 memcpy(&(dest[dp]), "<span class=\"c2html-keyword\">", 29);
universe@8 267 dp += 29;
universe@8 268 } else if (istype(word, wp)) {
universe@8 269 memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26);
universe@8 270 dp += 26;
universe@8 271 } else if (isdirective(word)) {
universe@10 272 isinclude = !strncmp("#include", word, WORDBUF_SIZE);
universe@8 273 memcpy(&(dest[dp]), "<span class=\"c2html-directive\">", 31);
universe@8 274 dp += 31;
universe@9 275 } else if (iscapsonly(word, wp)) {
universe@9 276 memcpy(&(dest[dp]), "<span class=\"c2html-macroconst\">", 32);
universe@9 277 dp += 32;
universe@8 278 } else {
universe@8 279 closespan = 0;
universe@8 280 }
universe@8 281 for (int i = 0 ; i < wp ; i++) {
universe@8 282 dp = writeescapedchar(dest, dp, word[i]);
universe@8 283 }
universe@8 284 if (closespan) {
universe@8 285 memcpy(&(dest[dp]), "</span>", 7);
universe@8 286 dp += 7;
universe@8 287 }
universe@8 288 }
universe@9 289 memset(word, 0, WORDBUF_SIZE);
universe@9 290 wp = 0;
universe@8 291 dp = writeescapedchar(dest, dp, c);
universe@8 292 } else {
universe@8 293 /* read word */
universe@8 294 if (wp < WORDBUF_SIZE) {
universe@8 295 word[wp++] = c;
universe@8 296 } else if (wp == WORDBUF_SIZE) {
universe@8 297 for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
universe@8 298 dp = writeescapedchar(dest, dp, word[i]);
universe@8 299 }
universe@8 300 wp++;
universe@8 301 dp = writeescapedchar(dest, dp, c);
universe@8 302 } else {
universe@8 303 dp = writeescapedchar(dest, dp, c);
universe@8 304 }
universe@7 305 }
universe@5 306 }
universe@8 307
universe@8 308 isescaping = !isescaping & (c == '\\');
universe@4 309 }
universe@4 310 }
universe@4 311 dest[dp] = 0;
universe@4 312 }
universe@4 313
universe@1 314 void printhelp() {
universe@1 315 printf("Formats source code using HTML.\n\nUsage:\n"
universe@11 316 " c2html [Options] FILE\n\n"
universe@11 317 " Options:\n"
universe@11 318 " -h Prints this help message\n"
universe@11 319 " -o <output> Output file (if not specified, stdout is used)\n"
universe@14 320 " -p Disable highlighting (plain text)\n"
universe@1 321 "\n");
universe@1 322
universe@1 323
universe@1 324 }
universe@1 325
universe@4 326 int lnint(size_t lnc) {
universe@1 327 int w = 1, p = 1;
universe@1 328 while ((p*=10) < lnc) w++;
universe@1 329 return w;
universe@1 330 }
universe@1 331
universe@1 332 int main(int argc, char** argv) {
universe@1 333
universe@11 334 settings_t settings;
universe@11 335 settings.outfilename = NULL;
universe@12 336 settings.highlight = 1;
universe@11 337
universe@11 338 char optc;
universe@12 339 while ((optc = getopt(argc, argv, "ho:p")) != -1) {
universe@11 340 switch (optc) {
universe@11 341 case 'o':
universe@11 342 if (!(optarg[0] == '-' && optarg[1] == 0)) {
universe@11 343 settings.outfilename = optarg;
universe@11 344 }
universe@11 345 break;
universe@12 346 case 'p':
universe@12 347 settings.highlight = 0;
universe@12 348 break;
universe@11 349 case 'h':
universe@11 350 printhelp();
universe@11 351 return 0;
universe@11 352 default:
universe@11 353 return 1;
universe@11 354 }
universe@11 355 }
universe@11 356
universe@11 357 if (optind != argc-1) {
universe@1 358 printhelp();
universe@11 359 return 1;
universe@1 360 } else {
universe@11 361 settings.infilename = argv[optind];
universe@1 362
universe@11 363 inputfile_t *inputfile = readinput(settings.infilename);
universe@1 364 if (inputfile) {
universe@11 365 FILE *fout;
universe@15 366 char *line;
universe@15 367 if (settings.highlight) {
universe@15 368 line = (char*) malloc(inputfile->maxlinewidth*64);
universe@15 369 } else {
universe@15 370 line = NULL;
universe@15 371 }
universe@11 372 if (settings.outfilename) {
universe@11 373 fout = fopen(settings.outfilename, "w");
universe@11 374 } else {
universe@11 375 fout = stdout;
universe@11 376 }
universe@11 377 fprintf(fout, "<pre>\n");
universe@4 378 int lnw = lnint(inputfile->count);
universe@1 379 for (int i = 0 ; i < inputfile->count ; i++) {
universe@12 380 if (settings.highlight) {
universe@12 381 parseline(inputfile->lines[i], line);
universe@12 382 } else {
universe@12 383 line = inputfile->lines[i];
universe@12 384 }
universe@11 385 fprintf(fout, "<span class=\"c2html-lineno\">%*d:</span> %s",
universe@9 386 lnw, i+1, line);
universe@1 387 }
universe@15 388 if (settings.highlight) {
universe@15 389 free(line);
universe@15 390 }
universe@11 391 fprintf(fout, "</pre>\n");
universe@11 392
universe@11 393 if (fout != stdout) {
universe@11 394 fclose(fout);
universe@11 395 }
universe@11 396
universe@1 397 freeinputfilebuffer(inputfile);
universe@1 398 }
universe@1 399
universe@1 400 return 0;
universe@1 401 }
universe@1 402 }
universe@1 403

mercurial