src/c2html.c

Wed, 10 Jul 2013 18:12:13 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 10 Jul 2013 18:12:13 +0200
changeset 18
5085b57e3fd6
parent 17
7ea86024aef0
child 19
2e812df2b231
permissions
-rw-r--r--

fixed highlighting for java

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

mercurial