structured source code

Thu, 23 Jan 2014 09:19:37 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 23 Jan 2014 09:19:37 +0100
changeset 21
537aec525835
parent 20
ebbf0776c1bc
child 22
f463693b5eeb

structured source code

LICENSE file | annotate | diff | comparison | revisions
Makefile file | annotate | diff | comparison | revisions
conf.mk file | annotate | diff | comparison | revisions
src/Makefile file | annotate | diff | comparison | revisions
src/c2html.c file | annotate | diff | comparison | revisions
src/ccodegen.c file | annotate | diff | comparison | revisions
src/ccodegen.h file | annotate | diff | comparison | revisions
src/codegens.c file | annotate | diff | comparison | revisions
src/codegens.h file | annotate | diff | comparison | revisions
src/javacodegen.c file | annotate | diff | comparison | revisions
src/javacodegen.h file | annotate | diff | comparison | revisions
src/obj.mk file | annotate | diff | comparison | revisions
--- a/LICENSE	Fri Aug 30 11:23:44 2013 +0200
+++ b/LICENSE	Thu Jan 23 09:19:37 2014 +0100
@@ -1,4 +1,4 @@
-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:
--- a/Makefile	Fri Aug 30 11:23:44 2013 +0200
+++ b/Makefile	Thu Jan 23 09:19:37 2014 +0100
@@ -31,7 +31,7 @@
 all: clean compile
 	
 compile: build
-	cd src; $(MAKE) $(BIN)
+	cd src; $(MAKE)
 
 build:
 	$(MKDIR) build
--- a/conf.mk	Fri Aug 30 11:23:44 2013 +0200
+++ b/conf.mk	Thu Jan 23 09:19:37 2014 +0100
@@ -36,8 +36,4 @@
 CFLAGS  = -g -O2 -std=gnu99 -Wall -Werror -pedantic
 LD      = gcc
 LDFLAGS =
-OBJEXT  = o
-LIBEXT  = a
-AR      = ar
-ARFLAGS = -r
-
+OBJ_EXT = .o
--- a/src/Makefile	Fri Aug 30 11:23:44 2013 +0200
+++ b/src/Makefile	Thu Jan 23 09:19:37 2014 +0100
@@ -28,14 +28,17 @@
 
 include ../conf.mk
 
-DST = ../build
+SRC  = c2html.c
+SRC += codegens.c
+SRC += ccodegen.c
+SRC += javacodegen.c
 
-include obj.mk
+OBJ = $(SRC:%.c=../build/%$(OBJ_EXT))
 
-$(BIN): $(OBJ)
-	$(LD) -o $(DST)/$@ $^ $(LDFLAGS)
+all: $(OBJ)
+	$(LD) -o ../build/$(BIN) $^ $(LDFLAGS)
 	
 
-$(DST)/%.$(OBJEXT): %.c
+../build/%$(OBJ_EXT): %.c
 	$(CC) -o $@ $(CFLAGS) -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 <unistd.h>
 #include <ctype.h>
 
-#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]), "<span class=\"c2html-comment\">", 29);
-        dp += 29;
-    }
-
-    for (char c = src[sp] ; c ; c=src[++sp]) {
-        /* comments */
-        if (c == '/') {
-            if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') {
-                iscomment = 0;
-                hltr->iscommentml = 0;
-                memcpy(&(dest[dp]), "/</span>", 8);
-                dp += 8;
-                continue;
-            } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
-                iscomment = 1;
-                hltr->iscommentml = (src[sp+1] == '*');
-                memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29);
-                dp += 29;
-            }
-        }
-
-        if (iscomment) {
-            if (c == '\n') {
-                memcpy(&(dest[dp]), "</span>", 7);
-                dp += 7;
-            }
-            dp = writeescapedchar(dest, dp, c);
-        } else if (isinclude) {
-            if (c == '<') {
-                memcpy(&(dest[dp]), "<span class=\"c2html-stdinclude\">", 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]), "</a>", 4);
-                    dp += 4;
-                    parseinclude = 0;
-                } else {
-                    memcpy(&(dest[dp]),
-                        "<a class=\"c2html-userinclude\" href=", 35);
-                    dp += 35;
-                    dp = writeescapedchar(dest, dp, c);
-                    ifp = 0;
-                    hltr->includefile[ifp++] = '\"';
-                    parseinclude = 1;
-                }
-            } else if (c == '>') {
-                dp = writeescapedchar(dest, dp, c);
-                memcpy(&(dest[dp]), "</span>", 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]), "<span class=\"c2html-string\">", 28);
-                    dp += 28;
-                    dp = writeescapedchar(dest, dp, c);
-                } else {
-                    dp = writeescapedchar(dest, dp, c);
-                    memcpy(&(dest[dp]), "</span>", 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]),
-                                "<span class=\"c2html-keyword\">", 29);
-                            dp += 29;
-                        } else if (hltr->istype(hltr->word, wp)) {
-                            memcpy(&(dest[dp]),
-                                "<span class=\"c2html-type\">", 26);
-                            dp += 26;
-                        } else if (hltr->isdirective(hltr->word)) {
-                            isinclude = !strncmp(
-                                "#include", hltr->word, WORDBUF_SIZE);
-                            memcpy(&(dest[dp]),
-                                "<span class=\"c2html-directive\">", 31);
-                            dp += 31;
-                        } else if (iscapsonly(hltr->word, wp)) {
-                            memcpy(&(dest[dp]),
-                                "<span class=\"c2html-macroconst\">", 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]), "</span>", 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];
                 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ccodegen.c	Thu Jan 23 09:19:37 2014 +0100
@@ -0,0 +1,198 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * 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:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "ccodegen.h"
+#include <string.h>
+#include <ctype.h>
+
+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
+};
+
+int isctype(char *word, size_t len) {
+    return (word[len-2] == '_' && word[len-1] == 't');
+}
+
+int iscdirective(char *word) {
+    return (word[0] == '#');
+}
+
+void cparseline(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]), "<span class=\"c2html-comment\">", 29);
+        dp += 29;
+    }
+
+    for (char c = src[sp] ; c ; c=src[++sp]) {
+        /* comments */
+        if (c == '/') {
+            if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') {
+                iscomment = 0;
+                hltr->iscommentml = 0;
+                memcpy(&(dest[dp]), "/</span>", 8);
+                dp += 8;
+                continue;
+            } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
+                iscomment = 1;
+                hltr->iscommentml = (src[sp+1] == '*');
+                memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29);
+                dp += 29;
+            }
+        }
+
+        if (iscomment) {
+            if (c == '\n') {
+                memcpy(&(dest[dp]), "</span>", 7);
+                dp += 7;
+            }
+            dp = writeescapedchar(dest, dp, c);
+        } else if (isinclude) {
+            if (c == '<') {
+                memcpy(&(dest[dp]), "<span class=\"c2html-stdinclude\">", 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]), "</a>", 4);
+                    dp += 4;
+                    parseinclude = 0;
+                } else {
+                    memcpy(&(dest[dp]),
+                        "<a class=\"c2html-userinclude\" href=", 35);
+                    dp += 35;
+                    dp = writeescapedchar(dest, dp, c);
+                    ifp = 0;
+                    hltr->includefile[ifp++] = '\"';
+                    parseinclude = 1;
+                }
+            } else if (c == '>') {
+                dp = writeescapedchar(dest, dp, c);
+                memcpy(&(dest[dp]), "</span>", 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]), "<span class=\"c2html-string\">", 28);
+                    dp += 28;
+                    dp = writeescapedchar(dest, dp, c);
+                } else {
+                    dp = writeescapedchar(dest, dp, c);
+                    memcpy(&(dest[dp]), "</span>", 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]),
+                                "<span class=\"c2html-keyword\">", 29);
+                            dp += 29;
+                        } else if (hltr->istype(hltr->word, wp)) {
+                            memcpy(&(dest[dp]),
+                                "<span class=\"c2html-type\">", 26);
+                            dp += 26;
+                        } else if (hltr->isdirective(hltr->word)) {
+                            isinclude = !strncmp(
+                                "#include", hltr->word, WORDBUF_SIZE);
+                            memcpy(&(dest[dp]),
+                                "<span class=\"c2html-directive\">", 31);
+                            dp += 31;
+                        } else if (iscapsonly(hltr->word, wp)) {
+                            memcpy(&(dest[dp]),
+                                "<span class=\"c2html-macroconst\">", 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]), "</span>", 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;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ccodegen.h	Thu Jan 23 09:19:37 2014 +0100
@@ -0,0 +1,51 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * 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:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef CCODEGEN_H
+#define	CCODEGEN_H
+
+#include "codegens.h"
+#include <stddef.h>
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+extern const char* ckeywords[];
+
+int isctype(char *word, size_t len);
+int iscdirective(char *word);
+void cparseline(char *src, char *dest, highlighter_t *hltr);
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* CCODEGEN_H */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/codegens.c	Thu Jan 23 09:19:37 2014 +0100
@@ -0,0 +1,63 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * 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:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <ctype.h>
+#include <string.h>
+#include "codegens.h"
+
+
+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;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/codegens.h	Thu Jan 23 09:19:37 2014 +0100
@@ -0,0 +1,66 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * 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:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef CODEGENS_H
+#define	CODEGENS_H
+
+#include <stdio.h>
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+#define WORDBUF_SIZE 64
+
+#define iswordcharacter(c) (isalnum(c) || c=='_' || c=='#' || c=='@')
+    
+typedef struct _highlighter_t highlighter_t;
+typedef void(*parser_fnc)(char*,char*,highlighter_t*);
+
+struct _highlighter_t {
+    const char** keywords;
+    int(*istype)(char*,size_t);
+    int(*isdirective)(char*);
+    parser_fnc parser;
+    int iscommentml;
+    char word[WORDBUF_SIZE];
+    char includefile[FILENAME_MAX];
+};
+
+size_t writeescapedchar(char *dest, size_t dp, char c);
+int iskeyword(char *word, const char** keywords);
+int iscapsonly(char *word, size_t wp);
+
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* CODEGENS_H */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/javacodegen.c	Thu Jan 23 09:19:37 2014 +0100
@@ -0,0 +1,165 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * 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:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "javacodegen.h"
+#include <string.h>
+#include <ctype.h>
+
+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
+};
+
+int isjtype(char *word, size_t len) {
+    return isupper(word[0]);
+}
+
+int isjdirective(char *word) {
+    return word[0] == '@';
+}
+
+void jparseline(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;
+    int isstring = 0, iscomment = 0, isimport = 0;
+    int isescaping = 0;
+
+    if (hltr->iscommentml) {
+        iscomment = 1;
+        memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29);
+        dp += 29;
+    }
+
+    for (char c = src[sp] ; c ; c=src[++sp]) {
+        /* comments */
+        if (c == '/') {
+            if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') {
+                iscomment = 0;
+                hltr->iscommentml = 0;
+                memcpy(&(dest[dp]), "/</span>", 8);
+                dp += 8;
+                continue;
+            } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
+                iscomment = 1;
+                hltr->iscommentml = (src[sp+1] == '*');
+                memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29);
+                dp += 29;
+            }
+        }
+
+        if (iscomment) {
+            if (c == '\n') {
+                memcpy(&(dest[dp]), "</span>", 7);
+                dp += 7;
+            }
+            dp = writeescapedchar(dest, dp, c);
+        } else if (isimport) {
+            // TODO: local imports
+        } else {
+            /* strings */
+            if (!isescaping && (c == '\'' || c == '\"')) {
+                isstring ^= 1;
+                if (isstring) {
+                    memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28);
+                    dp += 28;
+                    dp = writeescapedchar(dest, dp, c);
+                } else {
+                    dp = writeescapedchar(dest, dp, c);
+                    memcpy(&(dest[dp]), "</span>", 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]),
+                                "<span class=\"c2html-keyword\">", 29);
+                            dp += 29;
+                        } else if (hltr->istype(hltr->word, wp)) {
+                            memcpy(&(dest[dp]),
+                                "<span class=\"c2html-type\">", 26);
+                            dp += 26;
+                        } else if (hltr->isdirective(hltr->word)) {
+                            memcpy(&(dest[dp]),
+                                "<span class=\"c2html-directive\">", 31);
+                            dp += 31;
+                        } else if (iscapsonly(hltr->word, wp)) {
+                            memcpy(&(dest[dp]),
+                                "<span class=\"c2html-macroconst\">", 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]), "</span>", 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;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/javacodegen.h	Thu Jan 23 09:19:37 2014 +0100
@@ -0,0 +1,52 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * 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:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef JAVACODEGEN_H
+#define	JAVACODEGEN_H
+
+#include "codegens.h"
+#include <stddef.h>
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+extern const char* jkeywords[];
+
+int isjtype(char *word, size_t len);
+int isjdirective(char *word);
+void jparseline(char *src, char *dest, highlighter_t *hltr);
+
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* JAVACODEGEN_H */
+
--- a/src/obj.mk	Fri Aug 30 11:23:44 2013 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,31 +0,0 @@
-#
-# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
-#
-# Copyright 2013 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:
-#
-#   1. Redistributions of source code must retain the above copyright
-#      notice, this list of conditions and the following disclaimer.
-#
-#   2. Redistributions in binary form must reproduce the above copyright
-#      notice, this list of conditions and the following disclaimer in the
-#      documentation and/or other materials provided with the distribution.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
-# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-# POSSIBILITY OF SUCH DAMAGE.
-#
-
-OBJ = $(addsuffix .$(OBJEXT),$(addprefix $(DST)/, \
-    c2html \
-))

mercurial