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

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

mercurial