diff -r ebbf0776c1bc -r 537aec525835 src/c2html.c
--- a/src/c2html.c Fri Aug 30 11:23:44 2013 +0200
+++ b/src/c2html.c Thu Jan 23 09:19:37 2014 +0100
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
- * Copyright 2013 Mike Becker. All rights reserved.
+ * Copyright 2014 Mike Becker. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -34,56 +34,10 @@
#include
#include
-#define INPUTBUF_SIZE 2048
-#define WORDBUF_SIZE 64
-
-const char* ckeywords[] = {
- "auto", "break", "case", "char", "const", "continue", "default", "do",
- "double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
- "long", "register", "return", "short", "signed", "sizeof", "static",
- "struct", "switch", "typedef", "union", "unsigned", "void", "volatile",
- "while", NULL
-};
-
-const char* jkeywords[] = {
- "abstract", "continue", "for", "new", "switch", "assert", "default", "goto",
- "package", "synchronized", "boolean", "do", "if", "private", "this",
- "break", "double", "implements", "protected", "throw", "byte", "else",
- "import", "public", "throws", "case", "enum", "instanceof", "return",
- "transient", "catch", "extends", "int", "short", "try", "char", "final",
- "interface", "static", "void", "class", "finally", "long", "strictfp",
- "volatile", "const", "float", "native", "super", "while", NULL
-};
-
-#define iswordcharacter(c) (isalnum(c) || c=='_' || c=='#' || c=='@')
+#include "javacodegen.h"
+#include "ccodegen.h"
-int isctype(char *word, size_t len) {
- return (word[len-2] == '_' && word[len-1] == 't');
-}
-
-int iscdirective(char *word) {
- return (word[0] == '#');
-}
-
-int isjtype(char *word, size_t len) {
- return isupper(word[0]);
-}
-
-int isjdirective(char *word) {
- return word[0] == '@';
-}
-
-typedef struct _highlighter_t highlighter_t;
-
-struct _highlighter_t {
- const char** keywords;
- int(*istype)(char*,size_t);
- int(*isdirective)(char*);
- void(*parser)(char*,char*,highlighter_t*);
- int iscommentml;
- char word[WORDBUF_SIZE];
- char includefile[FILENAME_MAX];
-};
+#define INPUTBUF_SIZE 2048
typedef struct {
char* outfilename;
@@ -169,190 +123,6 @@
return inputfile;
}
-size_t writeescapedchar(char *dest, size_t dp, char c) {
- if (c == '>') {
- dest[dp++] = '&'; dest[dp++] = 'g'; dest[dp++] = 't'; dest[dp++] = ';';
- } else if (c == '<') {
- dest[dp++] = '&'; dest[dp++] = 'l'; dest[dp++] = 't'; dest[dp++] = ';';
- } else {
- dest[dp++] = c;
- }
-
- return dp;
-}
-
-int iskeyword(char *word, const char** keywords) {
- for (int i = 0 ; keywords[i] ; i++) {
- if (strncmp(keywords[i], word, WORDBUF_SIZE) == 0) {
- return 1;
- }
- }
- return 0;
-}
-
-int iscapsonly(char *word, size_t wp) {
- for (size_t i = 0 ; i < wp ; i++) {
- if (!isupper(word[i]) && word[i] != '_') {
- return 0;
- }
- }
- return 1;
-}
-
-void parseline(char *src, char *dest, highlighter_t *hltr) {
- hltr->parser(src, dest, hltr);
-}
-
-void cjparseline(char *src, char *dest, highlighter_t *hltr) {
- size_t sp = 0, dp = 0;
- /* indent */
- while (isspace(src[sp])) {
- dest[dp++] = src[sp++];
- }
-
- memset(hltr->word, 0, WORDBUF_SIZE);
- size_t wp = 0, ifp = 0;
- int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
- int isescaping = 0;
-
- if (hltr->iscommentml) {
- iscomment = 1;
- memcpy(&(dest[dp]), "", 8);
- dp += 8;
- continue;
- } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
- iscomment = 1;
- hltr->iscommentml = (src[sp+1] == '*');
- memcpy(&(dest[dp]), "", 7);
- dp += 7;
- }
- dp = writeescapedchar(dest, dp, c);
- } else if (isinclude) {
- if (c == '<') {
- memcpy(&(dest[dp]), "", 32);
- dp += 32;
- dp = writeescapedchar(dest, dp, c);
- } else if (c == '\"') {
- if (parseinclude) {
- dest[dp++] = '\"';
- dest[dp++] = '>';
- memcpy(&(dest[dp]), hltr->includefile, ifp);
- dp += ifp;
-
- dp = writeescapedchar(dest, dp, c);
- memcpy(&(dest[dp]), "", 4);
- dp += 4;
- parseinclude = 0;
- } else {
- memcpy(&(dest[dp]),
- "') {
- dp = writeescapedchar(dest, dp, c);
- memcpy(&(dest[dp]), "", 7);
- dp += 7;
- } else {
- if (parseinclude) {
- hltr->includefile[ifp++] = c;
- }
- dp = writeescapedchar(dest, dp, c);
- }
- } else {
- /* strings */
- if (!isescaping && (c == '\'' || c == '\"')) {
- isstring ^= 1;
- if (isstring) {
- memcpy(&(dest[dp]), "", 28);
- dp += 28;
- dp = writeescapedchar(dest, dp, c);
- } else {
- dp = writeescapedchar(dest, dp, c);
- memcpy(&(dest[dp]), "", 7);
- dp += 7;
- }
- } else {
- if (isstring) {
- dp = writeescapedchar(dest, dp, c);
- } else if (!iswordcharacter(c)) {
- /* interpret word int_t */
- if (wp > 0 && wp < WORDBUF_SIZE) {
- int closespan = 1;
- if (iskeyword(hltr->word, hltr->keywords)) {
- memcpy(&(dest[dp]),
- "", 29);
- dp += 29;
- } else if (hltr->istype(hltr->word, wp)) {
- memcpy(&(dest[dp]),
- "", 26);
- dp += 26;
- } else if (hltr->isdirective(hltr->word)) {
- isinclude = !strncmp(
- "#include", hltr->word, WORDBUF_SIZE);
- memcpy(&(dest[dp]),
- "", 31);
- dp += 31;
- } else if (iscapsonly(hltr->word, wp)) {
- memcpy(&(dest[dp]),
- "", 32);
- dp += 32;
- } else {
- closespan = 0;
- }
- for (int i = 0 ; i < wp ; i++) {
- dp = writeescapedchar(dest, dp, hltr->word[i]);
- }
- if (closespan) {
- memcpy(&(dest[dp]), "", 7);
- dp += 7;
- }
- }
- memset(hltr->word, 0, WORDBUF_SIZE);
- wp = 0;
- dp = writeescapedchar(dest, dp, c);
- } else {
- /* read word */
- if (wp < WORDBUF_SIZE) {
- hltr->word[wp++] = c;
- } else if (wp == WORDBUF_SIZE) {
- for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
- dp = writeescapedchar(dest, dp, hltr->word[i]);
- }
- wp++;
- dp = writeescapedchar(dest, dp, c);
- } else {
- dp = writeescapedchar(dest, dp, c);
- }
- }
- }
-
- isescaping = !isescaping & (c == '\\');
- }
- }
- dest[dp] = 0;
-}
-
void printhelp() {
printf("Formats source code using HTML.\n\nUsage:\n"
" c2html [Options] FILE\n\n"
@@ -382,7 +152,7 @@
highlighter.isdirective = iscdirective;
highlighter.istype = isctype;
highlighter.keywords = ckeywords;
- highlighter.parser = cjparseline;
+ highlighter.parser = cparseline;
char optc;
while ((optc = getopt(argc, argv, "hjo:p")) != -1) {
@@ -396,6 +166,7 @@
highlighter.isdirective = isjdirective;
highlighter.istype = isjtype;
highlighter.keywords = jkeywords;
+ highlighter.parser = jparseline;
break;
case 'p':
settings.highlight = 0;
@@ -432,7 +203,7 @@
int lnw = lnint(inputfile->count);
for (int i = 0 ; i < inputfile->count ; i++) {
if (settings.highlight) {
- parseline(inputfile->lines[i], line, &highlighter);
+ highlighter.parser(inputfile->lines[i], line, &highlighter);
} else {
line = inputfile->lines[i];
}