src/c2html.c

Wed, 12 Jun 2013 14:48:50 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 12 Jun 2013 14:48:50 +0200
changeset 5
412f1896874b
parent 4
323f674931fe
child 6
d10f7570add4
permissions
-rw-r--r--

support for keywords and types

     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 16
    40 const char* keywords[] = {
    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", "struct", 
    44   "switch", "typedef", "union", "unsigned", "void", "volatile", "while", NULL
    45 };
    48 typedef struct {
    49   size_t count;
    50   size_t capacity;
    51   size_t maxlinewidth;
    52   char** lines;
    53 } inputfile_t;
    55 inputfile_t *inputfilebuffer(size_t capacity) {
    56   inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t));
    57   inputfile->lines = (char**) malloc(capacity * sizeof(char*));
    58   inputfile->capacity = capacity;
    59   inputfile->count = 0;
    60   inputfile->maxlinewidth = 0;
    62   return inputfile;
    63 }
    65 void addline(inputfile_t *inputfile, char* line, size_t width) {
    66   char *l = (char*) malloc(width+1);
    67   memcpy(l, line, width);
    68   l[width] = 0;
    69   if (inputfile->count >= inputfile->capacity) {
    70     inputfile->capacity <<= 1;
    71     inputfile->lines = realloc(inputfile->lines, inputfile->capacity);
    72   }
    73   inputfile->lines[inputfile->count] = l;
    74   inputfile->maxlinewidth =
    75           width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth;
    76   inputfile->count++;
    77 }
    79 void freeinputfilebuffer(inputfile_t *inputfile) {
    80   for (int i = 0 ; i < inputfile->count ; i++) {
    81     free(inputfile->lines[i]);
    82   }
    83   free(inputfile->lines);
    84   free(inputfile);
    85 }
    87 inputfile_t *readinput(char *filename) {
    89   int fd = open(filename, O_RDONLY);
    90   if (fd == -1) return NULL;
    92   inputfile_t *inputfile = inputfilebuffer(512);
    94   char buf[INPUTBUF_SIZE];
    95   ssize_t r;
    97   size_t maxlinewidth = 256;
    98   char *line = (char*) malloc(maxlinewidth);
    99   size_t col = 0;
   101   while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) {
   102     for (size_t i = 0 ; i < r ; i++) {
   103       if (col >= maxlinewidth-4) {
   104         maxlinewidth <<= 1;
   105         line = realloc(line, maxlinewidth);
   106       }
   108       if (buf[i] == '\n') {
   109         line[col++] = '\n';
   110         line[col] = 0;
   111         addline(inputfile, line, col);        
   112         col = 0;
   113       } else {
   114         line[col++] = buf[i];
   115       }
   116     }
   117   }
   119   free(line);
   121   close(fd);
   123   return inputfile;
   124 }
   126 size_t writeescapedchar(char *dest, size_t dp, char c) {
   127   if (c == '>') {
   128     dest[dp++] = '&'; dest[dp++] = 'g';
   129     dest[dp++] = 't'; dest[dp++] = ';';
   130   } else if (c == '<') {
   131     dest[dp++] = '&'; dest[dp++] = 'l';
   132     dest[dp++] = 't'; dest[dp++] = ';';
   133   } else {
   134     dest[dp++] = c;
   135   }
   137   return dp;
   138 }
   140 int iskeyword(char *word) {
   141   for (int i = 0 ; keywords[i] ; i++) {
   142     if (strncmp(keywords[i], word, WORDBUF_SIZE) == 0) {
   143       return 1;
   144     }
   145   }
   146   return 0;
   147 }
   149 #define istype(word, len) (word[len-2] == '_' && word[len-1] == 't')
   151 void parseline(char *src, char *dest) {
   152   size_t sp = 0, dp = 0;
   153   /* indent */
   154   while (isspace(src[sp])) {
   155     dest[dp++] = src[sp++];
   156   }
   157   char word[WORDBUF_SIZE];
   158   memset(word, 0, WORDBUF_SIZE);
   159   size_t wp = 0;
   160   int closespan;
   161   for (char c = src[sp] ; c ; c=src[++sp]) {
   162     if (!isalnum(c) && c != '_') {
   163       /* interpret word int_t */
   164       if (wp > 0 && wp < WORDBUF_SIZE) {
   165         if (iskeyword(word)) {
   166           memcpy(&(dest[dp]), "<span class=\"c2html-keyword\">", 29);
   167           dp += 29;
   168           closespan = 1;
   169         } else if (istype(word, wp)) {
   170           memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26);
   171           dp += 26;
   172           closespan = 1;
   173         } else {
   174           closespan = 0;
   175         }
   176         for (int i = 0 ; i < wp ; i++) {
   177           dp = writeescapedchar(dest, dp, word[i]);
   178         }
   179         if (closespan) {
   180           memcpy(&(dest[dp]), "</span>", 7);
   181           dp += 7;
   182         }
   183       }
   184       memset(word, 0, WORDBUF_SIZE);
   185       wp = 0;
   186       dp = writeescapedchar(dest, dp, c);
   187     } else {
   188       /* read word */
   189       if (wp < WORDBUF_SIZE) {
   190         word[wp++] = c;
   191       } else if (wp == WORDBUF_SIZE) {
   192         for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
   193           dp = writeescapedchar(dest, dp, word[i]);
   194         }
   195         wp++;
   196         dp = writeescapedchar(dest, dp, c);
   197       } else {
   198         dp = writeescapedchar(dest, dp, c);
   199       }
   200     }
   201   }
   202   dest[dp] = 0;
   203 }
   205 void printhelp() {
   206   printf("Formats source code using HTML.\n\nUsage:\n"
   207       "  c2html [FILE...]"
   208       "\n");
   211 }
   213 int lnint(size_t lnc) {
   214   int w = 1, p = 1;
   215   while ((p*=10) < lnc) w++;
   216   return w;
   217 }
   219 int main(int argc, char** argv) {
   221   if (argc == 1) {
   222     printhelp();
   223     return 0;
   224   } else {
   226     inputfile_t *inputfile = readinput(argv[1]);
   227     if (inputfile) {
   228       printf("<pre>\n");
   229       char *line = (char*) malloc(inputfile->maxlinewidth*64);
   230       int lnw = lnint(inputfile->count);
   231       for (int i = 0 ; i < inputfile->count ; i++) {
   232         parseline(inputfile->lines[i], line);
   233         printf("<span class=\"c2html-lineno\">%*d:</span> %s",
   234             lnw, i, line);
   235       }
   236       free(line);
   237       printf("</pre>\n");
   238       freeinputfilebuffer(inputfile);
   239     }
   241     return 0;
   242   }
   243 }

mercurial