universe@1: /* universe@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@1: * universe@1: * Copyright 2013 Mike Becker. All rights reserved. universe@1: * universe@1: * Redistribution and use in source and binary forms, with or without universe@1: * modification, are permitted provided that the following conditions are met: universe@1: * universe@1: * 1. Redistributions of source code must retain the above copyright universe@1: * notice, this list of conditions and the following disclaimer. universe@1: * universe@1: * 2. Redistributions in binary form must reproduce the above copyright universe@1: * notice, this list of conditions and the following disclaimer in the universe@1: * documentation and/or other materials provided with the distribution. universe@1: * universe@1: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@1: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@1: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@1: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@1: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@1: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@1: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@1: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@1: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@1: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@1: * POSSIBILITY OF SUCH DAMAGE. universe@1: * universe@1: */ universe@1: universe@1: #include universe@1: #include universe@1: #include universe@1: #include universe@1: #include universe@4: #include universe@4: universe@4: #define INPUTBUF_SIZE 2048 universe@5: #define WORDBUF_SIZE 16 universe@5: universe@7: #define istype(word, len) (word[len-2] == '_' && word[len-1] == 't') universe@7: #define isdirective(word) (word[0] == '#') universe@7: universe@5: const char* keywords[] = { universe@5: "auto", "break", "case", "char", "const", "continue", "default", "do", universe@5: "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", universe@5: "long", "register", "return", "short", "signed", "sizeof", "static", "struct", universe@5: "switch", "typedef", "union", "unsigned", "void", "volatile", "while", NULL universe@5: }; universe@4: universe@4: universe@4: typedef struct { universe@4: size_t count; universe@4: size_t capacity; universe@4: size_t maxlinewidth; universe@4: char** lines; universe@4: } inputfile_t; universe@1: universe@1: inputfile_t *inputfilebuffer(size_t capacity) { universe@1: inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t)); universe@1: inputfile->lines = (char**) malloc(capacity * sizeof(char*)); universe@1: inputfile->capacity = capacity; universe@1: inputfile->count = 0; universe@4: inputfile->maxlinewidth = 0; universe@1: universe@1: return inputfile; universe@0: } universe@0: universe@1: void addline(inputfile_t *inputfile, char* line, size_t width) { universe@1: char *l = (char*) malloc(width+1); universe@1: memcpy(l, line, width); universe@1: l[width] = 0; universe@1: if (inputfile->count >= inputfile->capacity) { universe@1: inputfile->capacity <<= 1; universe@1: inputfile->lines = realloc(inputfile->lines, inputfile->capacity); universe@1: } universe@1: inputfile->lines[inputfile->count] = l; universe@4: inputfile->maxlinewidth = universe@4: width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth; universe@1: inputfile->count++; universe@1: } universe@1: universe@1: void freeinputfilebuffer(inputfile_t *inputfile) { universe@1: for (int i = 0 ; i < inputfile->count ; i++) { universe@1: free(inputfile->lines[i]); universe@1: } universe@1: free(inputfile->lines); universe@1: free(inputfile); universe@1: } universe@1: universe@1: inputfile_t *readinput(char *filename) { universe@1: universe@1: int fd = open(filename, O_RDONLY); universe@1: if (fd == -1) return NULL; universe@1: universe@1: inputfile_t *inputfile = inputfilebuffer(512); universe@1: universe@4: char buf[INPUTBUF_SIZE]; universe@1: ssize_t r; universe@1: universe@4: size_t maxlinewidth = 256; universe@1: char *line = (char*) malloc(maxlinewidth); universe@1: size_t col = 0; universe@1: universe@4: while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) { universe@1: for (size_t i = 0 ; i < r ; i++) { universe@1: if (col >= maxlinewidth-4) { universe@1: maxlinewidth <<= 1; universe@1: line = realloc(line, maxlinewidth); universe@1: } universe@1: universe@1: if (buf[i] == '\n') { universe@5: line[col++] = '\n'; universe@1: line[col] = 0; universe@1: addline(inputfile, line, col); universe@1: col = 0; universe@1: } else { universe@1: line[col++] = buf[i]; universe@1: } universe@1: } universe@1: } universe@1: universe@1: free(line); universe@1: universe@1: close(fd); universe@1: universe@1: return inputfile; universe@1: } universe@1: universe@5: size_t writeescapedchar(char *dest, size_t dp, char c) { universe@5: if (c == '>') { universe@5: dest[dp++] = '&'; dest[dp++] = 'g'; universe@5: dest[dp++] = 't'; dest[dp++] = ';'; universe@5: } else if (c == '<') { universe@5: dest[dp++] = '&'; dest[dp++] = 'l'; universe@5: dest[dp++] = 't'; dest[dp++] = ';'; universe@5: } else { universe@5: dest[dp++] = c; universe@5: } universe@5: universe@5: return dp; universe@5: } universe@5: universe@5: int iskeyword(char *word) { universe@5: for (int i = 0 ; keywords[i] ; i++) { universe@5: if (strncmp(keywords[i], word, WORDBUF_SIZE) == 0) { universe@5: return 1; universe@5: } universe@5: } universe@5: return 0; universe@5: } universe@5: universe@9: int iscapsonly(char *word, size_t wp) { universe@9: for (size_t i = 0 ; i < wp ; i++) { universe@9: if (!isupper(word[i]) && word[i] != '_') { universe@9: return 0; universe@9: } universe@9: } universe@9: return 1; universe@9: } universe@9: universe@4: void parseline(char *src, char *dest) { universe@4: size_t sp = 0, dp = 0; universe@4: /* indent */ universe@4: while (isspace(src[sp])) { universe@4: dest[dp++] = src[sp++]; universe@4: } universe@10: universe@10: static char word[WORDBUF_SIZE]; universe@10: static char includefile[FILENAME_MAX]; universe@10: universe@5: memset(word, 0, WORDBUF_SIZE); universe@10: size_t wp = 0, ifp = 0; universe@10: int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0; universe@8: static int iscommentml; universe@7: int isescaping = 0; universe@8: universe@8: if (iscommentml) { universe@8: iscomment = 1; universe@8: memcpy(&(dest[dp]), "", 29); universe@8: dp += 29; universe@8: } universe@9: universe@4: for (char c = src[sp] ; c ; c=src[++sp]) { universe@8: /* comments */ universe@8: if (c == '/') { universe@8: if (iscommentml && sp > 0 && src[sp-1] == '*') { universe@8: iscomment = 0; universe@8: iscommentml = 0; universe@8: memcpy(&(dest[dp]), "/", 8); universe@8: dp += 8; universe@8: continue; universe@8: } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { universe@8: iscomment = 1; universe@8: iscommentml = (src[sp+1] == '*'); universe@8: memcpy(&(dest[dp]), "", 29); universe@8: dp += 29; universe@8: } universe@8: } universe@8: universe@8: if (iscomment) { universe@8: if (c == '\n') { universe@7: memcpy(&(dest[dp]), "", 7); universe@7: dp += 7; universe@7: } universe@8: dp = writeescapedchar(dest, dp, c); universe@10: } else if (isinclude) { universe@10: if (c == '<') { universe@10: memcpy(&(dest[dp]), "", 32); universe@10: dp += 32; universe@10: dp = writeescapedchar(dest, dp, c); universe@10: } else if (c == '\"') { universe@10: if (parseinclude) { universe@10: dest[dp++] = '\"'; universe@10: dest[dp++] = '>'; universe@10: memcpy(&(dest[dp]), includefile, ifp); universe@10: dp += ifp; universe@10: universe@10: dp = writeescapedchar(dest, dp, c); universe@10: memcpy(&(dest[dp]), "", 4); universe@10: dp += 4; universe@10: parseinclude = 0; universe@10: } else { universe@10: memcpy(&(dest[dp]), "') { universe@10: dp = writeescapedchar(dest, dp, c); universe@10: memcpy(&(dest[dp]), "", 7); universe@10: dp += 7; universe@10: } else { universe@10: if (parseinclude) { universe@10: includefile[ifp++] = c; universe@10: } universe@10: dp = writeescapedchar(dest, dp, c); universe@10: } universe@7: } else { universe@8: /* strings */ universe@8: if (!isescaping && (c == '\'' || c == '\"')) { universe@8: isstring ^= 1; universe@8: if (isstring) { universe@8: memcpy(&(dest[dp]), "", 28); universe@8: dp += 28; universe@7: dp = writeescapedchar(dest, dp, c); universe@7: } else { universe@7: dp = writeescapedchar(dest, dp, c); universe@8: memcpy(&(dest[dp]), "", 7); universe@8: dp += 7; universe@8: } universe@8: } else { universe@8: if (isstring) { universe@8: dp = writeescapedchar(dest, dp, c); universe@10: } else if (!isalnum(c) && c != '_' && c != '#' && c != '.') { universe@8: /* interpret word int_t */ universe@8: if (wp > 0 && wp < WORDBUF_SIZE) { universe@8: int closespan = 1; universe@8: if (iskeyword(word)) { universe@8: memcpy(&(dest[dp]), "", 29); universe@8: dp += 29; universe@8: } else if (istype(word, wp)) { universe@8: memcpy(&(dest[dp]), "", 26); universe@8: dp += 26; universe@8: } else if (isdirective(word)) { universe@10: isinclude = !strncmp("#include", word, WORDBUF_SIZE); universe@8: memcpy(&(dest[dp]), "", 31); universe@8: dp += 31; universe@9: } else if (iscapsonly(word, wp)) { universe@9: memcpy(&(dest[dp]), "", 32); universe@9: dp += 32; universe@8: } else { universe@8: closespan = 0; universe@8: } universe@8: for (int i = 0 ; i < wp ; i++) { universe@8: dp = writeescapedchar(dest, dp, word[i]); universe@8: } universe@8: if (closespan) { universe@8: memcpy(&(dest[dp]), "", 7); universe@8: dp += 7; universe@8: } universe@8: } universe@9: memset(word, 0, WORDBUF_SIZE); universe@9: wp = 0; universe@8: dp = writeescapedchar(dest, dp, c); universe@8: } else { universe@8: /* read word */ universe@8: if (wp < WORDBUF_SIZE) { universe@8: word[wp++] = c; universe@8: } else if (wp == WORDBUF_SIZE) { universe@8: for (int i = 0 ; i < WORDBUF_SIZE ; i++) { universe@8: dp = writeescapedchar(dest, dp, word[i]); universe@8: } universe@8: wp++; universe@8: dp = writeescapedchar(dest, dp, c); universe@8: } else { universe@8: dp = writeescapedchar(dest, dp, c); universe@8: } universe@7: } universe@5: } universe@8: universe@8: isescaping = !isescaping & (c == '\\'); universe@4: } universe@4: } universe@4: dest[dp] = 0; universe@4: } universe@4: universe@1: void printhelp() { universe@1: printf("Formats source code using HTML.\n\nUsage:\n" universe@1: " c2html [FILE...]" universe@1: "\n"); universe@1: universe@1: universe@1: } universe@1: universe@4: int lnint(size_t lnc) { universe@1: int w = 1, p = 1; universe@1: while ((p*=10) < lnc) w++; universe@1: return w; universe@1: } universe@1: universe@1: int main(int argc, char** argv) { universe@1: universe@1: if (argc == 1) { universe@1: printhelp(); universe@1: return 0; universe@1: } else { universe@1: universe@1: inputfile_t *inputfile = readinput(argv[1]); universe@1: if (inputfile) { universe@1: printf("
\n");
universe@4:       char *line = (char*) malloc(inputfile->maxlinewidth*64);
universe@4:       int lnw = lnint(inputfile->count);
universe@1:       for (int i = 0 ; i < inputfile->count ; i++) {
universe@4:         parseline(inputfile->lines[i], line);
universe@5:         printf("%*d: %s",
universe@9:             lnw, i+1, line);
universe@1:       }
universe@4:       free(line);
universe@1:       printf("
\n"); universe@1: freeinputfilebuffer(inputfile); universe@1: } universe@1: universe@1: return 0; universe@1: } universe@1: } universe@1: