src/c2html.c

changeset 5
412f1896874b
parent 4
323f674931fe
child 6
d10f7570add4
equal deleted inserted replaced
4:323f674931fe 5:412f1896874b
33 #include <fcntl.h> 33 #include <fcntl.h>
34 #include <unistd.h> 34 #include <unistd.h>
35 #include <ctype.h> 35 #include <ctype.h>
36 36
37 #define INPUTBUF_SIZE 2048 37 #define INPUTBUF_SIZE 2048
38 #define WORDBUF_SIZE 16
39
40 const char* keywords[] = {
41 "auto", "break", "case", "char", "const", "continue", "default", "do",
42 "double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
43 "long", "register", "return", "short", "signed", "sizeof", "static", "struct",
44 "switch", "typedef", "union", "unsigned", "void", "volatile", "while", NULL
45 };
38 46
39 47
40 typedef struct { 48 typedef struct {
41 size_t count; 49 size_t count;
42 size_t capacity; 50 size_t capacity;
96 maxlinewidth <<= 1; 104 maxlinewidth <<= 1;
97 line = realloc(line, maxlinewidth); 105 line = realloc(line, maxlinewidth);
98 } 106 }
99 107
100 if (buf[i] == '\n') { 108 if (buf[i] == '\n') {
109 line[col++] = '\n';
101 line[col] = 0; 110 line[col] = 0;
102 addline(inputfile, line, col); 111 addline(inputfile, line, col);
103 col = 0; 112 col = 0;
104 } else { 113 } else {
105 line[col++] = buf[i]; 114 line[col++] = buf[i];
112 close(fd); 121 close(fd);
113 122
114 return inputfile; 123 return inputfile;
115 } 124 }
116 125
126 size_t writeescapedchar(char *dest, size_t dp, char c) {
127 if (c == '>') {
128 dest[dp++] = '&'; dest[dp++] = 'g';
129 dest[dp++] = 't'; dest[dp++] = ';';
130 } else if (c == '<') {
131 dest[dp++] = '&'; dest[dp++] = 'l';
132 dest[dp++] = 't'; dest[dp++] = ';';
133 } else {
134 dest[dp++] = c;
135 }
136
137 return dp;
138 }
139
140 int iskeyword(char *word) {
141 for (int i = 0 ; keywords[i] ; i++) {
142 if (strncmp(keywords[i], word, WORDBUF_SIZE) == 0) {
143 return 1;
144 }
145 }
146 return 0;
147 }
148
149 #define istype(word, len) (word[len-2] == '_' && word[len-1] == 't')
150
117 void parseline(char *src, char *dest) { 151 void parseline(char *src, char *dest) {
118 size_t sp = 0, dp = 0; 152 size_t sp = 0, dp = 0;
119 /* indent */ 153 /* indent */
120 while (isspace(src[sp])) { 154 while (isspace(src[sp])) {
121 dest[dp++] = src[sp++]; 155 dest[dp++] = src[sp++];
122 } 156 }
157 char word[WORDBUF_SIZE];
158 memset(word, 0, WORDBUF_SIZE);
159 size_t wp = 0;
160 int closespan;
123 for (char c = src[sp] ; c ; c=src[++sp]) { 161 for (char c = src[sp] ; c ; c=src[++sp]) {
124 switch (c) { 162 if (!isalnum(c) && c != '_') {
125 case '<': 163 /* interpret word int_t */
126 memcpy(&(dest[dp]), "&lt;", 4); 164 if (wp > 0 && wp < WORDBUF_SIZE) {
127 dp += 4; 165 if (iskeyword(word)) {
128 break; 166 memcpy(&(dest[dp]), "<span class=\"c2html-keyword\">", 29);
129 case '>': 167 dp += 29;
130 memcpy(&(dest[dp]), "&gt;", 4); 168 closespan = 1;
131 dp += 4; 169 } else if (istype(word, wp)) {
132 break; 170 memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26);
133 default: 171 dp += 26;
134 dest[dp++] = c; 172 closespan = 1;
173 } else {
174 closespan = 0;
175 }
176 for (int i = 0 ; i < wp ; i++) {
177 dp = writeescapedchar(dest, dp, word[i]);
178 }
179 if (closespan) {
180 memcpy(&(dest[dp]), "</span>", 7);
181 dp += 7;
182 }
183 }
184 memset(word, 0, WORDBUF_SIZE);
185 wp = 0;
186 dp = writeescapedchar(dest, dp, c);
187 } else {
188 /* read word */
189 if (wp < WORDBUF_SIZE) {
190 word[wp++] = c;
191 } else if (wp == WORDBUF_SIZE) {
192 for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
193 dp = writeescapedchar(dest, dp, word[i]);
194 }
195 wp++;
196 dp = writeescapedchar(dest, dp, c);
197 } else {
198 dp = writeescapedchar(dest, dp, c);
199 }
135 } 200 }
136 } 201 }
137 dest[dp] = 0; 202 dest[dp] = 0;
138 } 203 }
139 204
163 printf("<pre>\n"); 228 printf("<pre>\n");
164 char *line = (char*) malloc(inputfile->maxlinewidth*64); 229 char *line = (char*) malloc(inputfile->maxlinewidth*64);
165 int lnw = lnint(inputfile->count); 230 int lnw = lnint(inputfile->count);
166 for (int i = 0 ; i < inputfile->count ; i++) { 231 for (int i = 0 ; i < inputfile->count ; i++) {
167 parseline(inputfile->lines[i], line); 232 parseline(inputfile->lines[i], line);
168 printf("<span class=\"c2html-lineno\">%*d:</span> %s\n", 233 printf("<span class=\"c2html-lineno\">%*d:</span> %s",
169 lnw, i, line); 234 lnw, i, line);
170 } 235 }
171 free(line); 236 free(line);
172 printf("</pre>\n"); 237 printf("</pre>\n");
173 freeinputfilebuffer(inputfile); 238 freeinputfilebuffer(inputfile);

mercurial