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