formatted with 4 spaces

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

formatted with 4 spaces

src/c2html.c file | annotate | diff | comparison | revisions
     1.1 --- a/src/c2html.c	Wed Jul 10 18:12:13 2013 +0200
     1.2 +++ b/src/c2html.c	Fri Aug 30 10:51:49 2013 +0200
     1.3 @@ -38,409 +38,413 @@
     1.4  #define WORDBUF_SIZE 64
     1.5  
     1.6  const char* ckeywords[] = {
     1.7 -  "auto", "break", "case", "char", "const", "continue", "default", "do", 
     1.8 -  "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", 
     1.9 -  "long", "register", "return", "short", "signed", "sizeof", "static",
    1.10 -  "struct", "switch", "typedef", "union", "unsigned", "void", "volatile",
    1.11 -  "while", NULL
    1.12 +    "auto", "break", "case", "char", "const", "continue", "default", "do",
    1.13 +    "double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
    1.14 +    "long", "register", "return", "short", "signed", "sizeof", "static",
    1.15 +    "struct", "switch", "typedef", "union", "unsigned", "void", "volatile",
    1.16 +    "while", NULL
    1.17  };
    1.18  
    1.19  const char* jkeywords[] = {
    1.20 -  "abstract", "continue", "for", "new", "switch", "assert", "default", "goto",
    1.21 -  "package", "synchronized", "boolean", "do", "if", "private", "this",
    1.22 -  "break", "double", "implements", "protected", "throw", "byte", "else",
    1.23 -  "import", "public", "throws", "case", "enum", "instanceof", "return",
    1.24 -  "transient", "catch", "extends", "int", "short", "try", "char", "final",
    1.25 -  "interface", "static", "void", "class", "finally", "long", "strictfp",
    1.26 -  "volatile", "const", "float", "native", "super", "while", NULL
    1.27 +    "abstract", "continue", "for", "new", "switch", "assert", "default", "goto",
    1.28 +    "package", "synchronized", "boolean", "do", "if", "private", "this",
    1.29 +    "break", "double", "implements", "protected", "throw", "byte", "else",
    1.30 +    "import", "public", "throws", "case", "enum", "instanceof", "return",
    1.31 +    "transient", "catch", "extends", "int", "short", "try", "char", "final",
    1.32 +    "interface", "static", "void", "class", "finally", "long", "strictfp",
    1.33 +    "volatile", "const", "float", "native", "super", "while", NULL
    1.34  };
    1.35  
    1.36  #define iswordcharacter(c) (isalnum(c) || c=='_' || c=='#' || c=='@')
    1.37  
    1.38  int isctype(char *word, size_t len) {
    1.39 -  return (word[len-2] == '_' && word[len-1] == 't');
    1.40 +    return (word[len-2] == '_' && word[len-1] == 't');
    1.41  }
    1.42  
    1.43  int iscdirective(char *word) {
    1.44 -  return (word[0] == '#');
    1.45 +    return (word[0] == '#');
    1.46  }
    1.47  
    1.48  int isjtype(char *word, size_t len) {
    1.49 -  return isupper(word[0]);
    1.50 +    return isupper(word[0]);
    1.51  }
    1.52  
    1.53  int isjdirective(char *word) {
    1.54 -  return word[0] == '@';
    1.55 +    return word[0] == '@';
    1.56  }
    1.57  
    1.58  typedef struct {
    1.59 -  const char** keywords;
    1.60 -  int(*istype)(char*,size_t);
    1.61 -  int(*isdirective)(char*);
    1.62 +    const char** keywords;
    1.63 +    int(*istype)(char*,size_t);
    1.64 +    int(*isdirective)(char*);
    1.65  } highlighter_t;
    1.66  
    1.67  typedef struct {
    1.68 -  char* outfilename;
    1.69 -  char* infilename;
    1.70 -  int highlight;
    1.71 +    char* outfilename;
    1.72 +    char* infilename;
    1.73 +    int highlight;
    1.74  } settings_t;
    1.75  
    1.76  typedef struct {
    1.77 -  size_t count;
    1.78 -  size_t capacity;
    1.79 -  size_t maxlinewidth;
    1.80 -  char** lines;
    1.81 +    size_t count;
    1.82 +    size_t capacity;
    1.83 +    size_t maxlinewidth;
    1.84 +    char** lines;
    1.85  } inputfile_t;
    1.86  
    1.87  inputfile_t *inputfilebuffer(size_t capacity) {
    1.88 -  inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t));
    1.89 -  inputfile->lines = (char**) malloc(capacity * sizeof(char*));
    1.90 -  inputfile->capacity = capacity;
    1.91 -  inputfile->count = 0;
    1.92 -  inputfile->maxlinewidth = 0;
    1.93 -  
    1.94 -  return inputfile;
    1.95 +    inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t));
    1.96 +    inputfile->lines = (char**) malloc(capacity * sizeof(char*));
    1.97 +    inputfile->capacity = capacity;
    1.98 +    inputfile->count = 0;
    1.99 +    inputfile->maxlinewidth = 0;
   1.100 +
   1.101 +    return inputfile;
   1.102  }
   1.103  
   1.104  void addline(inputfile_t *inputfile, char* line, size_t width) {
   1.105 -  char *l = (char*) malloc(width+1);
   1.106 -  memcpy(l, line, width);
   1.107 -  l[width] = 0;
   1.108 -  if (inputfile->count >= inputfile->capacity) {
   1.109 -    inputfile->capacity <<= 1;
   1.110 -    inputfile->lines = realloc(inputfile->lines, inputfile->capacity);
   1.111 -  }
   1.112 -  inputfile->lines[inputfile->count] = l;
   1.113 -  inputfile->maxlinewidth =
   1.114 -          width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth;
   1.115 -  inputfile->count++;
   1.116 +    char *l = (char*) malloc(width+1);
   1.117 +    memcpy(l, line, width);
   1.118 +    l[width] = 0;
   1.119 +    if (inputfile->count >= inputfile->capacity) {
   1.120 +        inputfile->capacity <<= 1;
   1.121 +        inputfile->lines = realloc(inputfile->lines, inputfile->capacity);
   1.122 +    }
   1.123 +    inputfile->lines[inputfile->count] = l;
   1.124 +    inputfile->maxlinewidth =
   1.125 +        width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth;
   1.126 +    inputfile->count++;
   1.127  }
   1.128  
   1.129  void freeinputfilebuffer(inputfile_t *inputfile) {
   1.130 -  for (int i = 0 ; i < inputfile->count ; i++) {
   1.131 -    free(inputfile->lines[i]);
   1.132 -  }
   1.133 -  free(inputfile->lines);
   1.134 -  free(inputfile);
   1.135 +    for (int i = 0 ; i < inputfile->count ; i++) {
   1.136 +        free(inputfile->lines[i]);
   1.137 +    }
   1.138 +    free(inputfile->lines);
   1.139 +    free(inputfile);
   1.140  }
   1.141  
   1.142  inputfile_t *readinput(char *filename) {
   1.143  
   1.144 -  int fd = open(filename, O_RDONLY);
   1.145 -  if (fd == -1) return NULL;
   1.146 -  
   1.147 -  inputfile_t *inputfile = inputfilebuffer(512);
   1.148 -  
   1.149 -  char buf[INPUTBUF_SIZE];
   1.150 -  ssize_t r;
   1.151 -  
   1.152 -  size_t maxlinewidth = 256;
   1.153 -  char *line = (char*) malloc(maxlinewidth);
   1.154 -  size_t col = 0;
   1.155 -  
   1.156 -  while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) {
   1.157 -    for (size_t i = 0 ; i < r ; i++) {
   1.158 -      if (col >= maxlinewidth-4) {
   1.159 -        maxlinewidth <<= 1;
   1.160 -        line = realloc(line, maxlinewidth);
   1.161 -      }
   1.162 +    int fd = open(filename, O_RDONLY);
   1.163 +    if (fd == -1) return NULL;
   1.164  
   1.165 -      if (buf[i] == '\n') {
   1.166 -        line[col++] = '\n';
   1.167 -        line[col] = 0;
   1.168 -        addline(inputfile, line, col);        
   1.169 -        col = 0;
   1.170 -      } else {
   1.171 -        line[col++] = buf[i];
   1.172 -      }
   1.173 +    inputfile_t *inputfile = inputfilebuffer(512);
   1.174 +
   1.175 +    char buf[INPUTBUF_SIZE];
   1.176 +    ssize_t r;
   1.177 +
   1.178 +    size_t maxlinewidth = 256;
   1.179 +    char *line = (char*) malloc(maxlinewidth);
   1.180 +    size_t col = 0;
   1.181 +
   1.182 +    while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) {
   1.183 +        for (size_t i = 0 ; i < r ; i++) {
   1.184 +            if (col >= maxlinewidth-4) {
   1.185 +                maxlinewidth <<= 1;
   1.186 +                line = realloc(line, maxlinewidth);
   1.187 +            }
   1.188 +
   1.189 +            if (buf[i] == '\n') {
   1.190 +                line[col++] = '\n';
   1.191 +                line[col] = 0;
   1.192 +                addline(inputfile, line, col);
   1.193 +                col = 0;
   1.194 +            } else {
   1.195 +                line[col++] = buf[i];
   1.196 +            }
   1.197 +        }
   1.198      }
   1.199 -  }
   1.200 -  
   1.201 -  free(line);
   1.202 -  
   1.203 -  close(fd);
   1.204 -  
   1.205 -  return inputfile;
   1.206 +
   1.207 +    free(line);
   1.208 +
   1.209 +    close(fd);
   1.210 +
   1.211 +    return inputfile;
   1.212  }
   1.213  
   1.214  size_t writeescapedchar(char *dest, size_t dp, char c) {
   1.215 -  if (c == '>') {
   1.216 -    dest[dp++] = '&'; dest[dp++] = 'g';
   1.217 -    dest[dp++] = 't'; dest[dp++] = ';';
   1.218 -  } else if (c == '<') {
   1.219 -    dest[dp++] = '&'; dest[dp++] = 'l';
   1.220 -    dest[dp++] = 't'; dest[dp++] = ';';
   1.221 -  } else {
   1.222 -    dest[dp++] = c;
   1.223 -  }
   1.224 -  
   1.225 -  return dp;
   1.226 +    if (c == '>') {
   1.227 +        dest[dp++] = '&'; dest[dp++] = 'g'; dest[dp++] = 't'; dest[dp++] = ';';
   1.228 +    } else if (c == '<') {
   1.229 +        dest[dp++] = '&'; dest[dp++] = 'l'; dest[dp++] = 't'; dest[dp++] = ';';
   1.230 +    } else {
   1.231 +        dest[dp++] = c;
   1.232 +    }
   1.233 +
   1.234 +    return dp;
   1.235  }
   1.236  
   1.237  int iskeyword(char *word, const char** keywords) {
   1.238 -  for (int i = 0 ; keywords[i] ; i++) {
   1.239 -    if (strncmp(keywords[i], word, WORDBUF_SIZE) == 0) {
   1.240 -      return 1;
   1.241 +    for (int i = 0 ; keywords[i] ; i++) {
   1.242 +        if (strncmp(keywords[i], word, WORDBUF_SIZE) == 0) {
   1.243 +            return 1;
   1.244 +        }
   1.245      }
   1.246 -  }
   1.247 -  return 0;
   1.248 +    return 0;
   1.249  }
   1.250  
   1.251  int iscapsonly(char *word, size_t wp) {
   1.252 -  for (size_t i = 0 ; i < wp ; i++) {
   1.253 -    if (!isupper(word[i]) && word[i] != '_') {
   1.254 -      return 0;
   1.255 +    for (size_t i = 0 ; i < wp ; i++) {
   1.256 +        if (!isupper(word[i]) && word[i] != '_') {
   1.257 +            return 0;
   1.258 +        }
   1.259      }
   1.260 -  }
   1.261 -  return 1;
   1.262 +    return 1;
   1.263  }
   1.264  
   1.265  void parseline(char *src, char *dest, highlighter_t *highlighter) {
   1.266 -  size_t sp = 0, dp = 0;
   1.267 -  /* indent */
   1.268 -  while (isspace(src[sp])) {
   1.269 -    dest[dp++] = src[sp++];
   1.270 -  }
   1.271 -  
   1.272 -  static char word[WORDBUF_SIZE];
   1.273 -  static char includefile[FILENAME_MAX];
   1.274 -  
   1.275 -  memset(word, 0, WORDBUF_SIZE);
   1.276 -  size_t wp = 0, ifp = 0;
   1.277 -  int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
   1.278 -  static int iscommentml;
   1.279 -  int isescaping = 0;
   1.280 -  
   1.281 -  if (iscommentml) {
   1.282 -    iscomment = 1;
   1.283 -    memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29);
   1.284 -    dp += 29;
   1.285 -  }
   1.286 +    size_t sp = 0, dp = 0;
   1.287 +    /* indent */
   1.288 +    while (isspace(src[sp])) {
   1.289 +        dest[dp++] = src[sp++];
   1.290 +    }
   1.291  
   1.292 -  for (char c = src[sp] ; c ; c=src[++sp]) {
   1.293 -    /* comments */
   1.294 -    if (c == '/') {
   1.295 -      if (iscommentml && sp > 0 && src[sp-1] == '*') {
   1.296 -        iscomment = 0;
   1.297 -        iscommentml = 0;
   1.298 -        memcpy(&(dest[dp]), "/</span>", 8);
   1.299 -        dp += 8;
   1.300 -        continue;
   1.301 -      } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
   1.302 +    static char word[WORDBUF_SIZE];
   1.303 +    static char includefile[FILENAME_MAX];
   1.304 +
   1.305 +    memset(word, 0, WORDBUF_SIZE);
   1.306 +    size_t wp = 0, ifp = 0;
   1.307 +    int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
   1.308 +    static int iscommentml;
   1.309 +    int isescaping = 0;
   1.310 +
   1.311 +    if (iscommentml) {
   1.312          iscomment = 1;
   1.313 -        iscommentml = (src[sp+1] == '*');
   1.314          memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29);
   1.315          dp += 29;
   1.316 -      }
   1.317      }
   1.318 -    
   1.319 -    if (iscomment) {
   1.320 -      if (c == '\n') {
   1.321 -        memcpy(&(dest[dp]), "</span>", 7);
   1.322 -        dp += 7;
   1.323 -      }
   1.324 -      dp = writeescapedchar(dest, dp, c);
   1.325 -    } else if (isinclude) {
   1.326 -      if (c == '<') {
   1.327 -        memcpy(&(dest[dp]), "<span class=\"c2html-stdinclude\">", 32);
   1.328 -        dp += 32;
   1.329 -        dp = writeescapedchar(dest, dp, c);
   1.330 -      } else if (c == '\"') {
   1.331 -        if (parseinclude) {
   1.332 -          dest[dp++] = '\"';
   1.333 -          dest[dp++] = '>';
   1.334 -          memcpy(&(dest[dp]), includefile, ifp);
   1.335 -          dp += ifp;
   1.336 -          
   1.337 -          dp = writeescapedchar(dest, dp, c);
   1.338 -          memcpy(&(dest[dp]), "</a>", 4);
   1.339 -          dp += 4;
   1.340 -          parseinclude = 0;
   1.341 +
   1.342 +    for (char c = src[sp] ; c ; c=src[++sp]) {
   1.343 +        /* comments */
   1.344 +        if (c == '/') {
   1.345 +            if (iscommentml && sp > 0 && src[sp-1] == '*') {
   1.346 +                iscomment = 0;
   1.347 +                iscommentml = 0;
   1.348 +                memcpy(&(dest[dp]), "/</span>", 8);
   1.349 +                dp += 8;
   1.350 +                continue;
   1.351 +            } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
   1.352 +                iscomment = 1;
   1.353 +                iscommentml = (src[sp+1] == '*');
   1.354 +                memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29);
   1.355 +                dp += 29;
   1.356 +            }
   1.357 +        }
   1.358 +
   1.359 +        if (iscomment) {
   1.360 +            if (c == '\n') {
   1.361 +                memcpy(&(dest[dp]), "</span>", 7);
   1.362 +                dp += 7;
   1.363 +            }
   1.364 +            dp = writeescapedchar(dest, dp, c);
   1.365 +        } else if (isinclude) {
   1.366 +            if (c == '<') {
   1.367 +                memcpy(&(dest[dp]), "<span class=\"c2html-stdinclude\">", 32);
   1.368 +                dp += 32;
   1.369 +                dp = writeescapedchar(dest, dp, c);
   1.370 +            } else if (c == '\"') {
   1.371 +                if (parseinclude) {
   1.372 +                    dest[dp++] = '\"';
   1.373 +                    dest[dp++] = '>';
   1.374 +                    memcpy(&(dest[dp]), includefile, ifp);
   1.375 +                    dp += ifp;
   1.376 +
   1.377 +                    dp = writeescapedchar(dest, dp, c);
   1.378 +                    memcpy(&(dest[dp]), "</a>", 4);
   1.379 +                    dp += 4;
   1.380 +                    parseinclude = 0;
   1.381 +                } else {
   1.382 +                    memcpy(&(dest[dp]),
   1.383 +                        "<a class=\"c2html-userinclude\" href=", 35);
   1.384 +                    dp += 35;
   1.385 +                    dp = writeescapedchar(dest, dp, c);
   1.386 +                    ifp = 0;
   1.387 +                    includefile[ifp++] = '\"';
   1.388 +                    parseinclude = 1;
   1.389 +                }
   1.390 +            } else if (c == '>') {
   1.391 +                dp = writeescapedchar(dest, dp, c);
   1.392 +                memcpy(&(dest[dp]), "</span>", 7);
   1.393 +                dp += 7;
   1.394 +            } else {
   1.395 +                if (parseinclude) {
   1.396 +                    includefile[ifp++] = c;
   1.397 +                }
   1.398 +                dp = writeescapedchar(dest, dp, c);
   1.399 +            }
   1.400          } else {
   1.401 -          memcpy(&(dest[dp]), "<a class=\"c2html-userinclude\" href=", 35);
   1.402 -          dp += 35;
   1.403 -          dp = writeescapedchar(dest, dp, c);
   1.404 -          ifp = 0;
   1.405 -          includefile[ifp++] = '\"';
   1.406 -          parseinclude = 1;
   1.407 +            /* strings */
   1.408 +            if (!isescaping && (c == '\'' || c == '\"')) {
   1.409 +                isstring ^= 1;
   1.410 +                if (isstring) {
   1.411 +                    memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28);
   1.412 +                    dp += 28;
   1.413 +                    dp = writeescapedchar(dest, dp, c);
   1.414 +                } else {
   1.415 +                    dp = writeescapedchar(dest, dp, c);
   1.416 +                    memcpy(&(dest[dp]), "</span>", 7);
   1.417 +                    dp += 7;
   1.418 +                }
   1.419 +            } else {
   1.420 +                if (isstring) {
   1.421 +                    dp = writeescapedchar(dest, dp, c);
   1.422 +                } else if (!iswordcharacter(c)) {
   1.423 +                    /* interpret word int_t */
   1.424 +                    if (wp > 0 && wp < WORDBUF_SIZE) {
   1.425 +                        int closespan = 1;
   1.426 +                        if (iskeyword(word, highlighter->keywords)) {
   1.427 +                            memcpy(&(dest[dp]),
   1.428 +                                "<span class=\"c2html-keyword\">", 29);
   1.429 +                            dp += 29;
   1.430 +                        } else if (highlighter->istype(word, wp)) {
   1.431 +                            memcpy(&(dest[dp]),
   1.432 +                                "<span class=\"c2html-type\">", 26);
   1.433 +                            dp += 26;
   1.434 +                        } else if (highlighter->isdirective(word)) {
   1.435 +                            isinclude = !strncmp(
   1.436 +                                "#include", word, WORDBUF_SIZE);
   1.437 +                            memcpy(&(dest[dp]),
   1.438 +                                "<span class=\"c2html-directive\">", 31);
   1.439 +                            dp += 31;
   1.440 +                        } else if (iscapsonly(word, wp)) {
   1.441 +                            memcpy(&(dest[dp]),
   1.442 +                                "<span class=\"c2html-macroconst\">", 32);
   1.443 +                            dp += 32;
   1.444 +                        } else {
   1.445 +                            closespan = 0;
   1.446 +                        }
   1.447 +                        for (int i = 0 ; i < wp ; i++) {
   1.448 +                            dp = writeescapedchar(dest, dp, word[i]);
   1.449 +                        }
   1.450 +                        if (closespan) {
   1.451 +                            memcpy(&(dest[dp]), "</span>", 7);
   1.452 +                            dp += 7;
   1.453 +                        }
   1.454 +                    }
   1.455 +                    memset(word, 0, WORDBUF_SIZE);
   1.456 +                    wp = 0;
   1.457 +                    dp = writeescapedchar(dest, dp, c);
   1.458 +                } else {
   1.459 +                    /* read word */
   1.460 +                    if (wp < WORDBUF_SIZE) {
   1.461 +                        word[wp++] = c;
   1.462 +                    } else if (wp == WORDBUF_SIZE) {
   1.463 +                        for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
   1.464 +                            dp = writeescapedchar(dest, dp, word[i]);
   1.465 +                        }
   1.466 +                        wp++;
   1.467 +                        dp = writeescapedchar(dest, dp, c);
   1.468 +                    } else {
   1.469 +                        dp = writeescapedchar(dest, dp, c);
   1.470 +                    }
   1.471 +                }
   1.472 +            }
   1.473 +
   1.474 +            isescaping = !isescaping & (c == '\\');
   1.475          }
   1.476 -      } else if (c == '>') {
   1.477 -        dp = writeescapedchar(dest, dp, c);
   1.478 -        memcpy(&(dest[dp]), "</span>", 7);
   1.479 -        dp += 7;
   1.480 -      } else {
   1.481 -        if (parseinclude) {
   1.482 -          includefile[ifp++] = c;
   1.483 -        }
   1.484 -        dp = writeescapedchar(dest, dp, c);
   1.485 -      }
   1.486 -    } else {
   1.487 -      /* strings */
   1.488 -      if (!isescaping && (c == '\'' || c == '\"')) {
   1.489 -        isstring ^= 1;
   1.490 -        if (isstring) {
   1.491 -          memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28);
   1.492 -          dp += 28;
   1.493 -          dp = writeescapedchar(dest, dp, c);
   1.494 -        } else {
   1.495 -          dp = writeescapedchar(dest, dp, c);
   1.496 -          memcpy(&(dest[dp]), "</span>", 7);
   1.497 -          dp += 7;
   1.498 -        }
   1.499 -      } else {
   1.500 -        if (isstring) {
   1.501 -          dp = writeescapedchar(dest, dp, c);
   1.502 -        } else if (!iswordcharacter(c)) {
   1.503 -          /* interpret word int_t */
   1.504 -          if (wp > 0 && wp < WORDBUF_SIZE) {
   1.505 -            int closespan = 1;
   1.506 -            if (iskeyword(word, highlighter->keywords)) {
   1.507 -              memcpy(&(dest[dp]), "<span class=\"c2html-keyword\">", 29);
   1.508 -              dp += 29;
   1.509 -            } else if (highlighter->istype(word, wp)) {
   1.510 -              memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26);
   1.511 -              dp += 26;
   1.512 -            } else if (highlighter->isdirective(word)) {
   1.513 -              isinclude = !strncmp("#include", word, WORDBUF_SIZE);
   1.514 -              memcpy(&(dest[dp]), "<span class=\"c2html-directive\">", 31);
   1.515 -              dp += 31;
   1.516 -            } else if (iscapsonly(word, wp)) {
   1.517 -              memcpy(&(dest[dp]), "<span class=\"c2html-macroconst\">", 32);
   1.518 -              dp += 32;
   1.519 -            } else {
   1.520 -              closespan = 0;
   1.521 -            }
   1.522 -            for (int i = 0 ; i < wp ; i++) {
   1.523 -              dp = writeescapedchar(dest, dp, word[i]);
   1.524 -            }
   1.525 -            if (closespan) {
   1.526 -              memcpy(&(dest[dp]), "</span>", 7);
   1.527 -              dp += 7;
   1.528 -            }
   1.529 -          }
   1.530 -          memset(word, 0, WORDBUF_SIZE);
   1.531 -          wp = 0;
   1.532 -          dp = writeescapedchar(dest, dp, c);
   1.533 -        } else {
   1.534 -          /* read word */
   1.535 -          if (wp < WORDBUF_SIZE) {
   1.536 -            word[wp++] = c;
   1.537 -          } else if (wp == WORDBUF_SIZE) {
   1.538 -            for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
   1.539 -              dp = writeescapedchar(dest, dp, word[i]);
   1.540 -            }
   1.541 -            wp++;
   1.542 -            dp = writeescapedchar(dest, dp, c);
   1.543 -          } else {
   1.544 -            dp = writeescapedchar(dest, dp, c);
   1.545 -          }
   1.546 -        }
   1.547 -      }
   1.548 -
   1.549 -      isescaping = !isescaping & (c == '\\');
   1.550      }
   1.551 -  }
   1.552 -  dest[dp] = 0;
   1.553 +    dest[dp] = 0;
   1.554  }
   1.555  
   1.556  void printhelp() {
   1.557 -  printf("Formats source code using HTML.\n\nUsage:\n"
   1.558 -      "  c2html [Options] FILE\n\n"
   1.559 -      " Options:\n"
   1.560 -      "  -h                    Prints this help message\n"
   1.561 -      "  -j                    Highlight Java instead of C source code\n"
   1.562 -      "  -o <output>           Output file (if not specified, stdout is used)\n"
   1.563 -      "  -p                    Disable highlighting (plain text)\n"
   1.564 -      "\n");
   1.565 -  
   1.566 -  
   1.567 +    printf("Formats source code using HTML.\n\nUsage:\n"
   1.568 +        "  c2html [Options] FILE\n\n"
   1.569 +        " Options:\n"
   1.570 +        "  -h                    Prints this help message\n"
   1.571 +        "  -j                    Highlight Java instead of C source code\n"
   1.572 +        "  -o <output>           Output file (stdout, if not specified)\n"
   1.573 +        "  -p                    Disable highlighting (plain text)\n"
   1.574 +        "\n");
   1.575 +
   1.576 +
   1.577  }
   1.578  
   1.579  int lnint(size_t lnc) {
   1.580 -  int w = 1, p = 1;
   1.581 -  while ((p*=10) < lnc) w++;
   1.582 -  return w;
   1.583 +    int w = 1, p = 1;
   1.584 +    while ((p*=10) < lnc) w++;
   1.585 +    return w;
   1.586  }
   1.587  
   1.588  int main(int argc, char** argv) {
   1.589 -  
   1.590 -  settings_t settings;
   1.591 -  settings.outfilename = NULL;
   1.592 -  settings.highlight = 1;
   1.593 -  
   1.594 -  highlighter_t highlighter;
   1.595 -  highlighter.isdirective = iscdirective;
   1.596 -  highlighter.istype = isctype;
   1.597 -  highlighter.keywords = ckeywords;
   1.598 -  
   1.599 -  char optc;
   1.600 -  while ((optc = getopt(argc, argv, "hjo:p")) != -1) {
   1.601 -    switch (optc) {
   1.602 -      case 'o':
   1.603 -        if (!(optarg[0] == '-' && optarg[1] == 0)) {
   1.604 -          settings.outfilename = optarg;
   1.605 +
   1.606 +    settings_t settings;
   1.607 +    settings.outfilename = NULL;
   1.608 +    settings.highlight = 1;
   1.609 +
   1.610 +    highlighter_t highlighter;
   1.611 +    highlighter.isdirective = iscdirective;
   1.612 +    highlighter.istype = isctype;
   1.613 +    highlighter.keywords = ckeywords;
   1.614 +
   1.615 +    char optc;
   1.616 +    while ((optc = getopt(argc, argv, "hjo:p")) != -1) {
   1.617 +        switch (optc) {
   1.618 +            case 'o':
   1.619 +                if (!(optarg[0] == '-' && optarg[1] == 0)) {
   1.620 +                    settings.outfilename = optarg;
   1.621 +                }
   1.622 +                break;
   1.623 +            case 'j':
   1.624 +                highlighter.isdirective = isjdirective;
   1.625 +                highlighter.istype = isjtype;
   1.626 +                highlighter.keywords = jkeywords;
   1.627 +                break;
   1.628 +            case 'p':
   1.629 +                settings.highlight = 0;
   1.630 +                break;
   1.631 +            case 'h':
   1.632 +                printhelp();
   1.633 +                return 0;
   1.634 +            default:
   1.635 +                return 1;
   1.636          }
   1.637 -        break;
   1.638 -      case 'j':
   1.639 -        highlighter.isdirective = isjdirective;
   1.640 -        highlighter.istype = isjtype;
   1.641 -        highlighter.keywords = jkeywords;
   1.642 -        break;
   1.643 -      case 'p':
   1.644 -        settings.highlight = 0;
   1.645 -        break;
   1.646 -      case 'h':
   1.647 +    }
   1.648 +
   1.649 +    if (optind != argc-1) {
   1.650          printhelp();
   1.651 +        return 1;
   1.652 +    } else {
   1.653 +        settings.infilename = argv[optind];
   1.654 +
   1.655 +        inputfile_t *inputfile = readinput(settings.infilename);
   1.656 +        if (inputfile) {
   1.657 +            FILE *fout;
   1.658 +            char *line;
   1.659 +            if (settings.highlight) {
   1.660 +                line = (char*) malloc(inputfile->maxlinewidth*64);
   1.661 +            } else {
   1.662 +                line = NULL;
   1.663 +            }
   1.664 +            if (settings.outfilename) {
   1.665 +                fout = fopen(settings.outfilename, "w");
   1.666 +            } else {
   1.667 +                fout = stdout;
   1.668 +            }
   1.669 +            fprintf(fout, "<pre>\n");
   1.670 +            int lnw = lnint(inputfile->count);
   1.671 +            for (int i = 0 ; i < inputfile->count ; i++) {
   1.672 +                if (settings.highlight) {
   1.673 +                    parseline(inputfile->lines[i], line, &highlighter);
   1.674 +                } else {
   1.675 +                    line = inputfile->lines[i];
   1.676 +                }
   1.677 +                fprintf(fout, "<span class=\"c2html-lineno\">%*d:</span> %s",
   1.678 +                    lnw, i+1, line);
   1.679 +            }
   1.680 +            if (settings.highlight) {
   1.681 +                free(line);
   1.682 +            }
   1.683 +            fprintf(fout, "</pre>\n");
   1.684 +
   1.685 +            if (fout != stdout) {
   1.686 +                fclose(fout);
   1.687 +            }
   1.688 +
   1.689 +            freeinputfilebuffer(inputfile);
   1.690 +        }
   1.691 +
   1.692          return 0;
   1.693 -      default:
   1.694 -        return 1;
   1.695      }
   1.696 -  }
   1.697 -
   1.698 -  if (optind != argc-1) {
   1.699 -    printhelp();
   1.700 -    return 1;
   1.701 -  } else {
   1.702 -    settings.infilename = argv[optind];
   1.703 -    
   1.704 -    inputfile_t *inputfile = readinput(settings.infilename);
   1.705 -    if (inputfile) {
   1.706 -      FILE *fout;
   1.707 -      char *line;
   1.708 -      if (settings.highlight) {
   1.709 -        line = (char*) malloc(inputfile->maxlinewidth*64);
   1.710 -      } else {
   1.711 -        line = NULL;
   1.712 -      }
   1.713 -      if (settings.outfilename) {
   1.714 -        fout = fopen(settings.outfilename, "w");
   1.715 -      } else {
   1.716 -        fout = stdout;
   1.717 -      }
   1.718 -      fprintf(fout, "<pre>\n");
   1.719 -      int lnw = lnint(inputfile->count);
   1.720 -      for (int i = 0 ; i < inputfile->count ; i++) {
   1.721 -        if (settings.highlight) {
   1.722 -          parseline(inputfile->lines[i], line, &highlighter);
   1.723 -        } else {
   1.724 -          line = inputfile->lines[i];
   1.725 -        }
   1.726 -        fprintf(fout, "<span class=\"c2html-lineno\">%*d:</span> %s",
   1.727 -            lnw, i+1, line);
   1.728 -      }
   1.729 -      if (settings.highlight) {
   1.730 -        free(line);
   1.731 -      }
   1.732 -      fprintf(fout, "</pre>\n");
   1.733 -      
   1.734 -      if (fout != stdout) {
   1.735 -        fclose(fout);
   1.736 -      }
   1.737 -      
   1.738 -      freeinputfilebuffer(inputfile);
   1.739 -    }
   1.740 -  
   1.741 -    return 0;
   1.742 -  }
   1.743  }
   1.744  

mercurial