src/c2html.c

Fri, 30 Aug 2013 10:51:49 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 30 Aug 2013 10:51:49 +0200
changeset 19
2e812df2b231
parent 18
5085b57e3fd6
child 20
ebbf0776c1bc
permissions
-rw-r--r--

formatted with 4 spaces

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@18 38 #define WORDBUF_SIZE 64
universe@5 39
universe@16 40 const char* ckeywords[] = {
universe@19 41 "auto", "break", "case", "char", "const", "continue", "default", "do",
universe@19 42 "double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
universe@19 43 "long", "register", "return", "short", "signed", "sizeof", "static",
universe@19 44 "struct", "switch", "typedef", "union", "unsigned", "void", "volatile",
universe@19 45 "while", NULL
universe@5 46 };
universe@4 47
universe@17 48 const char* jkeywords[] = {
universe@19 49 "abstract", "continue", "for", "new", "switch", "assert", "default", "goto",
universe@19 50 "package", "synchronized", "boolean", "do", "if", "private", "this",
universe@19 51 "break", "double", "implements", "protected", "throw", "byte", "else",
universe@19 52 "import", "public", "throws", "case", "enum", "instanceof", "return",
universe@19 53 "transient", "catch", "extends", "int", "short", "try", "char", "final",
universe@19 54 "interface", "static", "void", "class", "finally", "long", "strictfp",
universe@19 55 "volatile", "const", "float", "native", "super", "while", NULL
universe@17 56 };
universe@17 57
universe@18 58 #define iswordcharacter(c) (isalnum(c) || c=='_' || c=='#' || c=='@')
universe@18 59
universe@17 60 int isctype(char *word, size_t len) {
universe@19 61 return (word[len-2] == '_' && word[len-1] == 't');
universe@16 62 }
universe@16 63
universe@17 64 int iscdirective(char *word) {
universe@19 65 return (word[0] == '#');
universe@16 66 }
universe@16 67
universe@17 68 int isjtype(char *word, size_t len) {
universe@19 69 return isupper(word[0]);
universe@16 70 }
universe@16 71
universe@17 72 int isjdirective(char *word) {
universe@19 73 return word[0] == '@';
universe@16 74 }
universe@16 75
universe@16 76 typedef struct {
universe@19 77 const char** keywords;
universe@19 78 int(*istype)(char*,size_t);
universe@19 79 int(*isdirective)(char*);
universe@16 80 } highlighter_t;
universe@16 81
universe@11 82 typedef struct {
universe@19 83 char* outfilename;
universe@19 84 char* infilename;
universe@19 85 int highlight;
universe@11 86 } settings_t;
universe@4 87
universe@4 88 typedef struct {
universe@19 89 size_t count;
universe@19 90 size_t capacity;
universe@19 91 size_t maxlinewidth;
universe@19 92 char** lines;
universe@4 93 } inputfile_t;
universe@1 94
universe@1 95 inputfile_t *inputfilebuffer(size_t capacity) {
universe@19 96 inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t));
universe@19 97 inputfile->lines = (char**) malloc(capacity * sizeof(char*));
universe@19 98 inputfile->capacity = capacity;
universe@19 99 inputfile->count = 0;
universe@19 100 inputfile->maxlinewidth = 0;
universe@19 101
universe@19 102 return inputfile;
universe@0 103 }
universe@0 104
universe@1 105 void addline(inputfile_t *inputfile, char* line, size_t width) {
universe@19 106 char *l = (char*) malloc(width+1);
universe@19 107 memcpy(l, line, width);
universe@19 108 l[width] = 0;
universe@19 109 if (inputfile->count >= inputfile->capacity) {
universe@19 110 inputfile->capacity <<= 1;
universe@19 111 inputfile->lines = realloc(inputfile->lines, inputfile->capacity);
universe@19 112 }
universe@19 113 inputfile->lines[inputfile->count] = l;
universe@19 114 inputfile->maxlinewidth =
universe@19 115 width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth;
universe@19 116 inputfile->count++;
universe@1 117 }
universe@1 118
universe@1 119 void freeinputfilebuffer(inputfile_t *inputfile) {
universe@19 120 for (int i = 0 ; i < inputfile->count ; i++) {
universe@19 121 free(inputfile->lines[i]);
universe@19 122 }
universe@19 123 free(inputfile->lines);
universe@19 124 free(inputfile);
universe@1 125 }
universe@1 126
universe@1 127 inputfile_t *readinput(char *filename) {
universe@1 128
universe@19 129 int fd = open(filename, O_RDONLY);
universe@19 130 if (fd == -1) return NULL;
universe@1 131
universe@19 132 inputfile_t *inputfile = inputfilebuffer(512);
universe@19 133
universe@19 134 char buf[INPUTBUF_SIZE];
universe@19 135 ssize_t r;
universe@19 136
universe@19 137 size_t maxlinewidth = 256;
universe@19 138 char *line = (char*) malloc(maxlinewidth);
universe@19 139 size_t col = 0;
universe@19 140
universe@19 141 while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) {
universe@19 142 for (size_t i = 0 ; i < r ; i++) {
universe@19 143 if (col >= maxlinewidth-4) {
universe@19 144 maxlinewidth <<= 1;
universe@19 145 line = realloc(line, maxlinewidth);
universe@19 146 }
universe@19 147
universe@19 148 if (buf[i] == '\n') {
universe@19 149 line[col++] = '\n';
universe@19 150 line[col] = 0;
universe@19 151 addline(inputfile, line, col);
universe@19 152 col = 0;
universe@19 153 } else {
universe@19 154 line[col++] = buf[i];
universe@19 155 }
universe@19 156 }
universe@1 157 }
universe@19 158
universe@19 159 free(line);
universe@19 160
universe@19 161 close(fd);
universe@19 162
universe@19 163 return inputfile;
universe@1 164 }
universe@1 165
universe@5 166 size_t writeescapedchar(char *dest, size_t dp, char c) {
universe@19 167 if (c == '>') {
universe@19 168 dest[dp++] = '&'; dest[dp++] = 'g'; dest[dp++] = 't'; dest[dp++] = ';';
universe@19 169 } else if (c == '<') {
universe@19 170 dest[dp++] = '&'; dest[dp++] = 'l'; dest[dp++] = 't'; dest[dp++] = ';';
universe@19 171 } else {
universe@19 172 dest[dp++] = c;
universe@19 173 }
universe@19 174
universe@19 175 return dp;
universe@5 176 }
universe@5 177
universe@16 178 int iskeyword(char *word, const char** keywords) {
universe@19 179 for (int i = 0 ; keywords[i] ; i++) {
universe@19 180 if (strncmp(keywords[i], word, WORDBUF_SIZE) == 0) {
universe@19 181 return 1;
universe@19 182 }
universe@5 183 }
universe@19 184 return 0;
universe@5 185 }
universe@5 186
universe@9 187 int iscapsonly(char *word, size_t wp) {
universe@19 188 for (size_t i = 0 ; i < wp ; i++) {
universe@19 189 if (!isupper(word[i]) && word[i] != '_') {
universe@19 190 return 0;
universe@19 191 }
universe@9 192 }
universe@19 193 return 1;
universe@9 194 }
universe@9 195
universe@16 196 void parseline(char *src, char *dest, highlighter_t *highlighter) {
universe@19 197 size_t sp = 0, dp = 0;
universe@19 198 /* indent */
universe@19 199 while (isspace(src[sp])) {
universe@19 200 dest[dp++] = src[sp++];
universe@19 201 }
universe@9 202
universe@19 203 static char word[WORDBUF_SIZE];
universe@19 204 static char includefile[FILENAME_MAX];
universe@19 205
universe@19 206 memset(word, 0, WORDBUF_SIZE);
universe@19 207 size_t wp = 0, ifp = 0;
universe@19 208 int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
universe@19 209 static int iscommentml;
universe@19 210 int isescaping = 0;
universe@19 211
universe@19 212 if (iscommentml) {
universe@8 213 iscomment = 1;
universe@8 214 memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29);
universe@8 215 dp += 29;
universe@8 216 }
universe@19 217
universe@19 218 for (char c = src[sp] ; c ; c=src[++sp]) {
universe@19 219 /* comments */
universe@19 220 if (c == '/') {
universe@19 221 if (iscommentml && sp > 0 && src[sp-1] == '*') {
universe@19 222 iscomment = 0;
universe@19 223 iscommentml = 0;
universe@19 224 memcpy(&(dest[dp]), "/</span>", 8);
universe@19 225 dp += 8;
universe@19 226 continue;
universe@19 227 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
universe@19 228 iscomment = 1;
universe@19 229 iscommentml = (src[sp+1] == '*');
universe@19 230 memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29);
universe@19 231 dp += 29;
universe@19 232 }
universe@19 233 }
universe@19 234
universe@19 235 if (iscomment) {
universe@19 236 if (c == '\n') {
universe@19 237 memcpy(&(dest[dp]), "</span>", 7);
universe@19 238 dp += 7;
universe@19 239 }
universe@19 240 dp = writeescapedchar(dest, dp, c);
universe@19 241 } else if (isinclude) {
universe@19 242 if (c == '<') {
universe@19 243 memcpy(&(dest[dp]), "<span class=\"c2html-stdinclude\">", 32);
universe@19 244 dp += 32;
universe@19 245 dp = writeescapedchar(dest, dp, c);
universe@19 246 } else if (c == '\"') {
universe@19 247 if (parseinclude) {
universe@19 248 dest[dp++] = '\"';
universe@19 249 dest[dp++] = '>';
universe@19 250 memcpy(&(dest[dp]), includefile, ifp);
universe@19 251 dp += ifp;
universe@19 252
universe@19 253 dp = writeescapedchar(dest, dp, c);
universe@19 254 memcpy(&(dest[dp]), "</a>", 4);
universe@19 255 dp += 4;
universe@19 256 parseinclude = 0;
universe@19 257 } else {
universe@19 258 memcpy(&(dest[dp]),
universe@19 259 "<a class=\"c2html-userinclude\" href=", 35);
universe@19 260 dp += 35;
universe@19 261 dp = writeescapedchar(dest, dp, c);
universe@19 262 ifp = 0;
universe@19 263 includefile[ifp++] = '\"';
universe@19 264 parseinclude = 1;
universe@19 265 }
universe@19 266 } else if (c == '>') {
universe@19 267 dp = writeescapedchar(dest, dp, c);
universe@19 268 memcpy(&(dest[dp]), "</span>", 7);
universe@19 269 dp += 7;
universe@19 270 } else {
universe@19 271 if (parseinclude) {
universe@19 272 includefile[ifp++] = c;
universe@19 273 }
universe@19 274 dp = writeescapedchar(dest, dp, c);
universe@19 275 }
universe@10 276 } else {
universe@19 277 /* strings */
universe@19 278 if (!isescaping && (c == '\'' || c == '\"')) {
universe@19 279 isstring ^= 1;
universe@19 280 if (isstring) {
universe@19 281 memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28);
universe@19 282 dp += 28;
universe@19 283 dp = writeescapedchar(dest, dp, c);
universe@19 284 } else {
universe@19 285 dp = writeescapedchar(dest, dp, c);
universe@19 286 memcpy(&(dest[dp]), "</span>", 7);
universe@19 287 dp += 7;
universe@19 288 }
universe@19 289 } else {
universe@19 290 if (isstring) {
universe@19 291 dp = writeescapedchar(dest, dp, c);
universe@19 292 } else if (!iswordcharacter(c)) {
universe@19 293 /* interpret word int_t */
universe@19 294 if (wp > 0 && wp < WORDBUF_SIZE) {
universe@19 295 int closespan = 1;
universe@19 296 if (iskeyword(word, highlighter->keywords)) {
universe@19 297 memcpy(&(dest[dp]),
universe@19 298 "<span class=\"c2html-keyword\">", 29);
universe@19 299 dp += 29;
universe@19 300 } else if (highlighter->istype(word, wp)) {
universe@19 301 memcpy(&(dest[dp]),
universe@19 302 "<span class=\"c2html-type\">", 26);
universe@19 303 dp += 26;
universe@19 304 } else if (highlighter->isdirective(word)) {
universe@19 305 isinclude = !strncmp(
universe@19 306 "#include", word, WORDBUF_SIZE);
universe@19 307 memcpy(&(dest[dp]),
universe@19 308 "<span class=\"c2html-directive\">", 31);
universe@19 309 dp += 31;
universe@19 310 } else if (iscapsonly(word, wp)) {
universe@19 311 memcpy(&(dest[dp]),
universe@19 312 "<span class=\"c2html-macroconst\">", 32);
universe@19 313 dp += 32;
universe@19 314 } else {
universe@19 315 closespan = 0;
universe@19 316 }
universe@19 317 for (int i = 0 ; i < wp ; i++) {
universe@19 318 dp = writeescapedchar(dest, dp, word[i]);
universe@19 319 }
universe@19 320 if (closespan) {
universe@19 321 memcpy(&(dest[dp]), "</span>", 7);
universe@19 322 dp += 7;
universe@19 323 }
universe@19 324 }
universe@19 325 memset(word, 0, WORDBUF_SIZE);
universe@19 326 wp = 0;
universe@19 327 dp = writeescapedchar(dest, dp, c);
universe@19 328 } else {
universe@19 329 /* read word */
universe@19 330 if (wp < WORDBUF_SIZE) {
universe@19 331 word[wp++] = c;
universe@19 332 } else if (wp == WORDBUF_SIZE) {
universe@19 333 for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
universe@19 334 dp = writeescapedchar(dest, dp, word[i]);
universe@19 335 }
universe@19 336 wp++;
universe@19 337 dp = writeescapedchar(dest, dp, c);
universe@19 338 } else {
universe@19 339 dp = writeescapedchar(dest, dp, c);
universe@19 340 }
universe@19 341 }
universe@19 342 }
universe@19 343
universe@19 344 isescaping = !isescaping & (c == '\\');
universe@10 345 }
universe@4 346 }
universe@19 347 dest[dp] = 0;
universe@4 348 }
universe@4 349
universe@1 350 void printhelp() {
universe@19 351 printf("Formats source code using HTML.\n\nUsage:\n"
universe@19 352 " c2html [Options] FILE\n\n"
universe@19 353 " Options:\n"
universe@19 354 " -h Prints this help message\n"
universe@19 355 " -j Highlight Java instead of C source code\n"
universe@19 356 " -o <output> Output file (stdout, if not specified)\n"
universe@19 357 " -p Disable highlighting (plain text)\n"
universe@19 358 "\n");
universe@19 359
universe@19 360
universe@1 361 }
universe@1 362
universe@4 363 int lnint(size_t lnc) {
universe@19 364 int w = 1, p = 1;
universe@19 365 while ((p*=10) < lnc) w++;
universe@19 366 return w;
universe@1 367 }
universe@1 368
universe@1 369 int main(int argc, char** argv) {
universe@19 370
universe@19 371 settings_t settings;
universe@19 372 settings.outfilename = NULL;
universe@19 373 settings.highlight = 1;
universe@19 374
universe@19 375 highlighter_t highlighter;
universe@19 376 highlighter.isdirective = iscdirective;
universe@19 377 highlighter.istype = isctype;
universe@19 378 highlighter.keywords = ckeywords;
universe@19 379
universe@19 380 char optc;
universe@19 381 while ((optc = getopt(argc, argv, "hjo:p")) != -1) {
universe@19 382 switch (optc) {
universe@19 383 case 'o':
universe@19 384 if (!(optarg[0] == '-' && optarg[1] == 0)) {
universe@19 385 settings.outfilename = optarg;
universe@19 386 }
universe@19 387 break;
universe@19 388 case 'j':
universe@19 389 highlighter.isdirective = isjdirective;
universe@19 390 highlighter.istype = isjtype;
universe@19 391 highlighter.keywords = jkeywords;
universe@19 392 break;
universe@19 393 case 'p':
universe@19 394 settings.highlight = 0;
universe@19 395 break;
universe@19 396 case 'h':
universe@19 397 printhelp();
universe@19 398 return 0;
universe@19 399 default:
universe@19 400 return 1;
universe@11 401 }
universe@19 402 }
universe@19 403
universe@19 404 if (optind != argc-1) {
universe@11 405 printhelp();
universe@19 406 return 1;
universe@19 407 } else {
universe@19 408 settings.infilename = argv[optind];
universe@19 409
universe@19 410 inputfile_t *inputfile = readinput(settings.infilename);
universe@19 411 if (inputfile) {
universe@19 412 FILE *fout;
universe@19 413 char *line;
universe@19 414 if (settings.highlight) {
universe@19 415 line = (char*) malloc(inputfile->maxlinewidth*64);
universe@19 416 } else {
universe@19 417 line = NULL;
universe@19 418 }
universe@19 419 if (settings.outfilename) {
universe@19 420 fout = fopen(settings.outfilename, "w");
universe@19 421 } else {
universe@19 422 fout = stdout;
universe@19 423 }
universe@19 424 fprintf(fout, "<pre>\n");
universe@19 425 int lnw = lnint(inputfile->count);
universe@19 426 for (int i = 0 ; i < inputfile->count ; i++) {
universe@19 427 if (settings.highlight) {
universe@19 428 parseline(inputfile->lines[i], line, &highlighter);
universe@19 429 } else {
universe@19 430 line = inputfile->lines[i];
universe@19 431 }
universe@19 432 fprintf(fout, "<span class=\"c2html-lineno\">%*d:</span> %s",
universe@19 433 lnw, i+1, line);
universe@19 434 }
universe@19 435 if (settings.highlight) {
universe@19 436 free(line);
universe@19 437 }
universe@19 438 fprintf(fout, "</pre>\n");
universe@19 439
universe@19 440 if (fout != stdout) {
universe@19 441 fclose(fout);
universe@19 442 }
universe@19 443
universe@19 444 freeinputfilebuffer(inputfile);
universe@19 445 }
universe@19 446
universe@11 447 return 0;
universe@11 448 }
universe@1 449 }
universe@1 450

mercurial