Sat, 25 Apr 2015 19:14:57 +0200
introduced macro for constant string memcpy + fixed string highlight fix
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2015 Mike Becker. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
30 #include "ccodegen.h"
31 #include <string.h>
32 #include <ctype.h>
34 const char* ckeywords[] = {
35 "auto", "break", "case", "char", "const", "continue", "default", "do",
36 "double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
37 "long", "register", "return", "short", "signed", "sizeof", "static",
38 "struct", "switch", "typedef", "union", "unsigned", "void", "volatile",
39 "while", NULL
40 };
42 int isctype(char *word, size_t len) {
43 return (word[len-2] == '_' && word[len-1] == 't');
44 }
46 int iscdirective(char *word) {
47 return (word[0] == '#');
48 }
50 #define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \
51 dp += sizeof(str)-1
53 void cparseline(char *src, char *dest, highlighter_t *hltr) {
54 size_t sp = 0, dp = 0;
55 /* indent */
56 while (isspace(src[sp])) {
57 dest[dp++] = src[sp++];
58 }
60 memset(hltr->word, 0, WORDBUF_SIZE);
61 size_t wp = 0, ifp = 0;
62 int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
63 char quote = '\0';
64 int isescaping = 0;
66 if (hltr->iscommentml) {
67 iscomment = 1;
68 memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
69 }
71 for (char c = src[sp] ; c ; c=src[++sp]) {
72 /* comments */
73 if (!isstring && c == '/') {
74 if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') {
75 iscomment = 0;
76 hltr->iscommentml = 0;
77 memcpy_const(dest, dp, "/</span>");
78 continue;
79 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
80 iscomment = 1;
81 hltr->iscommentml = (src[sp+1] == '*');
82 memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
83 }
84 }
86 if (iscomment) {
87 if (c == '\n') {
88 memcpy_const(dest, dp, "</span>");
89 }
90 dp = writeescapedchar(dest, dp, c);
91 } else if (isinclude) {
92 if (c == '<') {
93 memcpy_const(dest, dp, "<span class=\"c2html-stdinclude\">");
94 dp = writeescapedchar(dest, dp, c);
95 } else if (c == '\"') {
96 if (parseinclude) {
97 dest[dp++] = '\"';
98 dest[dp++] = '>';
99 memcpy(&(dest[dp]), hltr->includefile, ifp);
100 dp += ifp;
102 dp = writeescapedchar(dest, dp, c);
103 memcpy_const(dest, dp, "</a>");
104 parseinclude = 0;
105 } else {
106 memcpy_const(dest, dp,
107 "<a class=\"c2html-userinclude\" href=");
108 dp = writeescapedchar(dest, dp, c);
109 ifp = 0;
110 hltr->includefile[ifp++] = '\"';
111 parseinclude = 1;
112 }
113 } else if (c == '>') {
114 dp = writeescapedchar(dest, dp, c);
115 memcpy_const(dest, dp, "</span>");
116 } else {
117 if (parseinclude) {
118 hltr->includefile[ifp++] = c;
119 }
120 dp = writeescapedchar(dest, dp, c);
121 }
122 } else {
123 /* strings */
124 if (!isescaping && (c == '\'' || c == '\"')) {
125 if (isstring) {
126 dp = writeescapedchar(dest, dp, c);
127 if (c == quote) {
128 isstring = 0;
129 memcpy_const(dest, dp, "</span>");
130 } else {
131 dp = writeescapedchar(dest, dp, c);
132 }
133 } else {
134 isstring = 1;
135 quote = c;
136 memcpy_const(dest, dp,
137 "<span class=\"c2html-string\">");
138 dp = writeescapedchar(dest, dp, c);
139 }
140 } else {
141 if (isstring) {
142 dp = writeescapedchar(dest, dp, c);
143 } else if (!iswordcharacter(c)) {
144 /* interpret word int_t */
145 if (wp > 0 && wp < WORDBUF_SIZE) {
146 int closespan = 1;
147 if (iskeyword(hltr->word, hltr->keywords)) {
148 memcpy_const(dest, dp,
149 "<span class=\"c2html-keyword\">");
150 } else if (hltr->istype(hltr->word, wp)) {
151 memcpy_const(dest, dp,
152 "<span class=\"c2html-type\">");
153 } else if (hltr->isdirective(hltr->word)) {
154 isinclude = !strncmp(
155 "#include", hltr->word, WORDBUF_SIZE);
156 memcpy_const(dest, dp,
157 "<span class=\"c2html-directive\">");
158 } else if (iscapsonly(hltr->word, wp)) {
159 memcpy_const(dest, dp,
160 "<span class=\"c2html-macroconst\">");
161 } else {
162 closespan = 0;
163 }
164 for (int i = 0 ; i < wp ; i++) {
165 dp = writeescapedchar(dest, dp, hltr->word[i]);
166 }
167 if (closespan) {
168 memcpy_const(dest, dp, "</span>");
169 }
170 }
171 memset(hltr->word, 0, WORDBUF_SIZE);
172 wp = 0;
173 dp = writeescapedchar(dest, dp, c);
174 } else {
175 /* read word */
176 if (wp < WORDBUF_SIZE) {
177 hltr->word[wp++] = c;
178 } else if (wp == WORDBUF_SIZE) {
179 for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
180 dp = writeescapedchar(dest, dp, hltr->word[i]);
181 }
182 wp++;
183 dp = writeescapedchar(dest, dp, c);
184 } else {
185 dp = writeescapedchar(dest, dp, c);
186 }
187 }
188 }
190 isescaping = !isescaping & (c == '\\');
191 }
192 }
193 dest[dp] = 0;
194 }