src/c2html.c

Wed, 10 Jul 2013 17:57:03 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 10 Jul 2013 17:57:03 +0200
changeset 17
7ea86024aef0
parent 16
fa0bcd0444eb
child 18
5085b57e3fd6
permissions
-rw-r--r--

implemented java highlighting

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

mercurial