src/c2html.c

Fri, 30 Aug 2013 11:23:44 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 30 Aug 2013 11:23:44 +0200
changeset 20
ebbf0776c1bc
parent 19
2e812df2b231
child 21
537aec525835
permissions
-rw-r--r--

replaced function static variables with struct members

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

mercurial