1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/ccodegen.c Thu Jan 23 09:19:37 2014 +0100 1.3 @@ -0,0 +1,198 @@ 1.4 +/* 1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 1.6 + * 1.7 + * Copyright 2014 Mike Becker. All rights reserved. 1.8 + * 1.9 + * Redistribution and use in source and binary forms, with or without 1.10 + * modification, are permitted provided that the following conditions are met: 1.11 + * 1.12 + * 1. Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 1.15 + * 2. Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 1.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 1.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 1.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 1.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 1.29 + * POSSIBILITY OF SUCH DAMAGE. 1.30 + * 1.31 + */ 1.32 + 1.33 +#include "ccodegen.h" 1.34 +#include <string.h> 1.35 +#include <ctype.h> 1.36 + 1.37 +const char* ckeywords[] = { 1.38 + "auto", "break", "case", "char", "const", "continue", "default", "do", 1.39 + "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", 1.40 + "long", "register", "return", "short", "signed", "sizeof", "static", 1.41 + "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", 1.42 + "while", NULL 1.43 +}; 1.44 + 1.45 +int isctype(char *word, size_t len) { 1.46 + return (word[len-2] == '_' && word[len-1] == 't'); 1.47 +} 1.48 + 1.49 +int iscdirective(char *word) { 1.50 + return (word[0] == '#'); 1.51 +} 1.52 + 1.53 +void cparseline(char *src, char *dest, highlighter_t *hltr) { 1.54 + size_t sp = 0, dp = 0; 1.55 + /* indent */ 1.56 + while (isspace(src[sp])) { 1.57 + dest[dp++] = src[sp++]; 1.58 + } 1.59 + 1.60 + memset(hltr->word, 0, WORDBUF_SIZE); 1.61 + size_t wp = 0, ifp = 0; 1.62 + int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0; 1.63 + int isescaping = 0; 1.64 + 1.65 + if (hltr->iscommentml) { 1.66 + iscomment = 1; 1.67 + memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); 1.68 + dp += 29; 1.69 + } 1.70 + 1.71 + for (char c = src[sp] ; c ; c=src[++sp]) { 1.72 + /* comments */ 1.73 + if (c == '/') { 1.74 + if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') { 1.75 + iscomment = 0; 1.76 + hltr->iscommentml = 0; 1.77 + memcpy(&(dest[dp]), "/</span>", 8); 1.78 + dp += 8; 1.79 + continue; 1.80 + } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { 1.81 + iscomment = 1; 1.82 + hltr->iscommentml = (src[sp+1] == '*'); 1.83 + memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); 1.84 + dp += 29; 1.85 + } 1.86 + } 1.87 + 1.88 + if (iscomment) { 1.89 + if (c == '\n') { 1.90 + memcpy(&(dest[dp]), "</span>", 7); 1.91 + dp += 7; 1.92 + } 1.93 + dp = writeescapedchar(dest, dp, c); 1.94 + } else if (isinclude) { 1.95 + if (c == '<') { 1.96 + memcpy(&(dest[dp]), "<span class=\"c2html-stdinclude\">", 32); 1.97 + dp += 32; 1.98 + dp = writeescapedchar(dest, dp, c); 1.99 + } else if (c == '\"') { 1.100 + if (parseinclude) { 1.101 + dest[dp++] = '\"'; 1.102 + dest[dp++] = '>'; 1.103 + memcpy(&(dest[dp]), hltr->includefile, ifp); 1.104 + dp += ifp; 1.105 + 1.106 + dp = writeescapedchar(dest, dp, c); 1.107 + memcpy(&(dest[dp]), "</a>", 4); 1.108 + dp += 4; 1.109 + parseinclude = 0; 1.110 + } else { 1.111 + memcpy(&(dest[dp]), 1.112 + "<a class=\"c2html-userinclude\" href=", 35); 1.113 + dp += 35; 1.114 + dp = writeescapedchar(dest, dp, c); 1.115 + ifp = 0; 1.116 + hltr->includefile[ifp++] = '\"'; 1.117 + parseinclude = 1; 1.118 + } 1.119 + } else if (c == '>') { 1.120 + dp = writeescapedchar(dest, dp, c); 1.121 + memcpy(&(dest[dp]), "</span>", 7); 1.122 + dp += 7; 1.123 + } else { 1.124 + if (parseinclude) { 1.125 + hltr->includefile[ifp++] = c; 1.126 + } 1.127 + dp = writeescapedchar(dest, dp, c); 1.128 + } 1.129 + } else { 1.130 + /* strings */ 1.131 + if (!isescaping && (c == '\'' || c == '\"')) { 1.132 + isstring ^= 1; 1.133 + if (isstring) { 1.134 + memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28); 1.135 + dp += 28; 1.136 + dp = writeescapedchar(dest, dp, c); 1.137 + } else { 1.138 + dp = writeescapedchar(dest, dp, c); 1.139 + memcpy(&(dest[dp]), "</span>", 7); 1.140 + dp += 7; 1.141 + } 1.142 + } else { 1.143 + if (isstring) { 1.144 + dp = writeescapedchar(dest, dp, c); 1.145 + } else if (!iswordcharacter(c)) { 1.146 + /* interpret word int_t */ 1.147 + if (wp > 0 && wp < WORDBUF_SIZE) { 1.148 + int closespan = 1; 1.149 + if (iskeyword(hltr->word, hltr->keywords)) { 1.150 + memcpy(&(dest[dp]), 1.151 + "<span class=\"c2html-keyword\">", 29); 1.152 + dp += 29; 1.153 + } else if (hltr->istype(hltr->word, wp)) { 1.154 + memcpy(&(dest[dp]), 1.155 + "<span class=\"c2html-type\">", 26); 1.156 + dp += 26; 1.157 + } else if (hltr->isdirective(hltr->word)) { 1.158 + isinclude = !strncmp( 1.159 + "#include", hltr->word, WORDBUF_SIZE); 1.160 + memcpy(&(dest[dp]), 1.161 + "<span class=\"c2html-directive\">", 31); 1.162 + dp += 31; 1.163 + } else if (iscapsonly(hltr->word, wp)) { 1.164 + memcpy(&(dest[dp]), 1.165 + "<span class=\"c2html-macroconst\">", 32); 1.166 + dp += 32; 1.167 + } else { 1.168 + closespan = 0; 1.169 + } 1.170 + for (int i = 0 ; i < wp ; i++) { 1.171 + dp = writeescapedchar(dest, dp, hltr->word[i]); 1.172 + } 1.173 + if (closespan) { 1.174 + memcpy(&(dest[dp]), "</span>", 7); 1.175 + dp += 7; 1.176 + } 1.177 + } 1.178 + memset(hltr->word, 0, WORDBUF_SIZE); 1.179 + wp = 0; 1.180 + dp = writeescapedchar(dest, dp, c); 1.181 + } else { 1.182 + /* read word */ 1.183 + if (wp < WORDBUF_SIZE) { 1.184 + hltr->word[wp++] = c; 1.185 + } else if (wp == WORDBUF_SIZE) { 1.186 + for (int i = 0 ; i < WORDBUF_SIZE ; i++) { 1.187 + dp = writeescapedchar(dest, dp, hltr->word[i]); 1.188 + } 1.189 + wp++; 1.190 + dp = writeescapedchar(dest, dp, c); 1.191 + } else { 1.192 + dp = writeescapedchar(dest, dp, c); 1.193 + } 1.194 + } 1.195 + } 1.196 + 1.197 + isescaping = !isescaping & (c == '\\'); 1.198 + } 1.199 + } 1.200 + dest[dp] = 0; 1.201 +}