Debian Codegraph @

clex/driver.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2013, 2014, 2015, 2016 The University of Utah
3 * All rights reserved.
4 *
5 * This file is distributed under the University of Illinois Open Source
6 * License. See the file COPYING for details.
7 */
8
9 #if HAVE_CONFIG_H
10 # include <config.h>
11 #endif
12
13 #include <assert.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include "defs.h"
19
20 struct tok_t {
21 char *str;
22 enum tok_kind kind;
23 int id;
24 };
25
26 static struct tok_t *tok_list;
27 static int toks;
28 static int max_toks;
29 static const int initial_length = 1;
30
31 static int add_tok(char *str, enum tok_kind kind) {
32 assert(str);
33 if (toks >= max_toks) {
34 max_toks *= 2;
35 tok_list =
36 (struct tok_t *)realloc(tok_list, max_toks * sizeof(struct tok_t));
37 assert(tok_list);
38 }
39 tok_list[toks].str = strdup(str);
40 assert(tok_list[toks].str);
41 tok_list[toks].kind = kind;
42 tok_list[toks].id = -1;
43 toks++;
44 return toks - 1;
45 }
46
47 void process_token(enum tok_kind kind) {
48 int tok = add_tok(yytext, kind);
49 count++;
50 }
51
52 enum mode_t {
53 MODE_RENAME = 1111,
54 MODE_PRINT,
55 MODE_DELETE_STRING,
56 MODE_RM_TOKS,
57 MODE_RM_TOK_PATTERN,
58 MODE_SHORTEN_STRING,
59 MODE_X_STRING,
60 MODE_DEFINE,
61 MODE_NONE,
62 };
63
64 static void print_toks(void) {
65 int i;
66 for (i = 0; i < toks; i++) {
67 printf("%s", tok_list[i].str);
68 }
69 exit(OK);
70 }
71
72 static int next_char(char *c) {
73 if (*c == 'z') {
74 *c = 'a';
75 return 1;
76 }
77 *c = 1 + *c;
78 return 0;
79 }
80
81 static void next_name(char *name) {
82 int pos = strlen(name) - 1;
83 while (1) {
84 int wrapped = next_char(&name[pos]);
85 if (!wrapped)
86 return;
87 if (pos == 0) {
88 // there's no next string at this length so prepend a character
89 int i;
90 int len = strlen(name);
91 for (i = len; i >= 0; i--)
92 name[i + 1] = name[i];
93 name[0] = 'a';
94 return;
95 }
96 pos--;
97 }
98 }
99
100 static void find_unused_name(char *name) {
101 strcpy(name, "a");
102 int clash;
103 do {
104 clash = 0;
105 int i;
106 for (i = 0; i < toks; i++) {
107 if (strcmp(tok_list[i].str, name) == 0) {
108 next_name(name);
109 clash = 1;
110 break;
111 }
112 }
113 } while (clash);
114 }
115
116 static int should_be_renamed(char *name, char *newname) {
117 int i;
118 for (i=0; i < strlen(name); i++) {
119 if (name[i] < 'a' || name[i] > 'z')
120 return 1;
121 }
122 if (strlen(newname) > strlen(name))
123 return 0;
124 return strcmp(newname, name) < 0;
125 }
126
127 static void index_toks(char ***index_ptr, int *index_size_ptr, char *newname) {
128 char **index = 0;
129 int index_size = 0;
130 int i;
131 for (i = 0; i < toks; i++) {
132 if (tok_list[i].kind != TOK_IDENT)
133 continue;
134 if (!should_be_renamed(tok_list[i].str, newname))
135 continue;
136 int matched = 0;
137 int j;
138 for (j = 0; j < index_size; j++) {
139 if (strcmp(index[j], tok_list[i].str) == 0) {
140 matched = 1;
141 tok_list[i].id = j;
142 break;
143 }
144 }
145 if (!matched) {
146 tok_list[i].id = index_size;
147 index = realloc(index, (1 + index_size) * sizeof(char *));
148 index[index_size] = tok_list[i].str;
149 index_size++;
150 }
151 }
152 *index_ptr = index;
153 *index_size_ptr = index_size;
154 }
155
156 static void print_renamed(int tok_index, char *newname) {
157 int i;
158 for (i = 0; i < toks; i++) {
159 if (tok_list[i].id == tok_index)
160 printf("%s", newname);
161 else
162 printf("%s", tok_list[i].str);
163 }
164 }
165
166 static void rename_toks(int tok_index) {
167 char newname[255];
168 find_unused_name(newname);
169 assert(tok_index >= 0);
170 char **index;
171 int index_size;
172 index_toks(&index, &index_size, newname);
173 //fprintf(stderr, "tok_index = %d, index size = %d\n", tok_index, index_size);
174 if (tok_index >= index_size) {
175 //fprintf(stderr, "rename_toks stop\n");
176 exit(STOP);
177 } else {
178 //fprintf(stderr, "rename_toks with index %d, source '%s', target '%s'\n",
179 // tok_index, index[tok_index], newname);
180 print_renamed(tok_index, newname);
181 exit(OK);
182 }
183 }
184
185 static void string_rm_chars(char *s, int i) {
186 int j;
187 for (j = 0; j < (strlen(s) - i + 1); j++) {
188 s[j] = s[j + i];
189 }
190 }
191
192 static int remove_line(char *s, int idx, int *numlines) {
193 int line = 0;
194 int lastpos = 1;
195 int ret = 0;
196 int i;
197 for (i = 0; i < strlen(s); i++) {
198 if (strncmp(s + i, "\\n", 2) == 0 || s[i + 1] == 0) {
199 if (line == idx) {
200 if (s[i + 1] == 0) {
201 // printf ("removing rest of string at %d\n", lastpos);
202 s[lastpos] = '"';
203 s[lastpos + 1] = 0;
204 } else {
205 // printf ("removing %d chars at %d\n", i - lastpos + 2, lastpos);
206 string_rm_chars(s + lastpos, i - lastpos + 2);
207 ret = 1;
208 }
209 }
210 lastpos = i + 2;
211 line++;
212 }
213 }
214 *numlines = line;
215 return ret;
216 }
217
218 static void shorten_string(int idx) {
219 int i;
220 int matched = 0;
221 int which = 0;
222 for (i = 0; i < toks; i++) {
223 if (!matched && tok_list[i].kind == TOK_STRING) {
224 char *s = tok_list[i].str;
225 int len = strlen(s) - 2;
226 if (idx >= len) {
227 idx -= len;
228 } else {
229 string_rm_chars(s + idx + 1, 1);
230 matched = 1;
231 which++;
232 }
233 }
234 printf("%s", tok_list[i].str);
235 }
236 if (matched) {
237 exit(OK);
238 } else {
239 exit(STOP);
240 }
241 }
242
243 static void x_string(int idx) {
244 int i;
245 int matched = 0;
246 int which = 0;
247 for (i = 0; i < toks; i++) {
248 if (!matched && tok_list[i].kind == TOK_STRING) {
249 char *s = tok_list[i].str;
250 int j;
251 for (j = 0; j < strlen(s); j++) {
252 if (s[j] != 'x') {
253 if (which == idx) {
254 s[j] = 'x';
255 matched = 1;
256 }
257 which++;
258 }
259 }
260 }
261 printf("%s", tok_list[i].str);
262 }
263 if (matched) {
264 exit(OK);
265 } else {
266 exit(STOP);
267 }
268 }
269
270 static void delete_string(int idx) {
271 int i;
272 int matched = 0;
273 int which = 0;
274 for (i = 0; i < toks; i++) {
275 int printed = 0;
276 if (tok_list[i].kind == TOK_STRING &&
277 strcmp(tok_list[i].str, "\"\"") != 0) {
278 if (which == idx) {
279 printf("\"\"");
280 printed = 1;
281 matched = 1;
282 }
283 which++;
284 }
285 if (!printed)
286 printf("%s", tok_list[i].str);
287 }
288 if (matched) {
289 exit(OK);
290 } else {
291 exit(STOP);
292 }
293 }
294
295 static int n_toks;
296
297 static void rm_toks(int idx) {
298 int i;
299 int matched = 0;
300 int which = 0;
301 int started = 0;
302 for (i = 0; i < toks; i++) {
303 if (tok_list[i].kind != TOK_WS &&
304 tok_list[i].kind != TOK_NEWLINE) {
305 if (which == idx) {
306 started = 1;
307 matched = 1;
308 }
309 which++;
310 }
311 if (!started || (which > (idx + n_toks)))
312 printf("%s", tok_list[i].str);
313 }
314 if (matched) {
315 exit(OK);
316 } else {
317 exit(STOP);
318 }
319 }
320
321 static void print_pattern(unsigned char c) {
322 int z;
323 for (z = 0; z < 8; z++) {
324 printf("%d", (c & 128) ? 1 : 0);
325 c <<= 1;
326 }
327 printf("\n");
328 }
329
330 static void rm_tok_pattern(int idx) {
331 int i;
332 int n_patterns = 1 << (n_toks - 1);
333
334 #ifdef _MSC_VER
335 unsigned char *patterns = calloc(n_patterns, sizeof(unsigned char));
336 #else
337 unsigned char patterns[n_patterns];
338 #endif
339
340 for (i = 0; i < n_patterns; i++) {
341 patterns[i] = 1 | ((unsigned)i << 1);
342 }
343
344 int n_pattern = idx & (n_patterns - 1);
345 unsigned char pat = patterns[n_pattern];
346
347 #ifdef _MSC_VER
348 free(patterns);
349 #endif
350
351 if (0) {
352 printf("pattern = ");
353 print_pattern(pat);
354 }
355
356 idx >>= (n_toks - 1);
357
358 int which = 0;
359 int started = 0;
360 int matched = 0;
361 int deleted = 0;
362 for (i = 0; i < toks; i++) {
363 if (tok_list[i].kind != TOK_WS &&
364 tok_list[i].kind != TOK_NEWLINE) {
365 if (which == idx) {
366 matched = 1;
367 started = 1;
368 }
369 if (which == (idx + n_toks))
370 started = 0;
371 which++;
372 }
373 int print = 0;
374 int pattern_idx = which - idx;
375 if (tok_list[i].kind == TOK_WS ||
376 tok_list[i].kind == TOK_NEWLINE) {
377 print = 1;
378 } else {
379 if (!started) {
380 print = 1;
381 } else {
382 if (pat & 1) {
383 deleted = 1;
384 // printf ("[%s]", tok_list[i].str);
385 } else {
386 print = 1;
387 }
388 pat >>= 1;
389 }
390 }
391 if (print)
392 printf("%s", tok_list[i].str);
393 }
394 if (matched && deleted) {
395 exit(OK);
396 } else {
397 exit(STOP);
398 }
399 }
400
401 // handle simple #defines
402 // todo: handle macro arguments
403 // todo: handle undefinition, redefinition, and other cases
404 // fixme: this is just extremely hacky-- partial preprocessing should be done by
405 // a separate tool that resembles unifdef
406 void replace_macro(int i) {
407 int initial = i;
408 char *macro = tok_list[i].str;
409 // printf("replacing macro '%s'\n", macro);
410 i++;
411 while (tok_list[i].kind == TOK_WS)
412 i++;
413 int end = i;
414 while (tok_list[end].kind != TOK_NEWLINE)
415 end++;
416 int x;
417 for (x = 0; x < toks; ++x) {
418 if (x != initial &&
419 strcmp(tok_list[x].str, macro) == 0) {
420 int y;
421 for (y = i; y < end; ++y)
422 printf("%s", tok_list[y].str);
423 } else {
424 printf("%s", tok_list[x].str);
425 }
426 }
427 }
428
429 void define(int tok_index) {
430 int i;
431 int found = 0;
432 for (i = 0; i < toks; ++i) {
433 if (strcmp(tok_list[i].str, "#") == 0) {
434 i++;
435 while (tok_list[i].kind == TOK_WS)
436 i++;
437 if (strcmp(tok_list[i].str, "define") != 0)
438 continue;
439 i++;
440 while (tok_list[i].kind == TOK_WS)
441 i++;
442 int j;
443 int used = 0;
444 for (j = 0; j < toks; ++j)
445 if (j != i &&
446 strcmp(tok_list[j].str, tok_list[i].str) == 0)
447 used = 1;
448 if (!used)
449 continue;
450 if (found == tok_index) {
451 replace_macro(i);
452 exit(OK);
453 }
454 found++;
455 }
456 }
457 exit(STOP);
458 }
459
460 int main(int argc, char *argv[]) {
461 if (argc != 4) {
462 printf("USAGE: %s command index file\n", argv[0]);
463 exit(STOP);
464 }
465
466 char *cmd = argv[1];
467 enum mode_t mode = MODE_NONE;
468 if (strcmp(cmd, "rename-toks") == 0) {
469 mode = MODE_RENAME;
470 } else if (strcmp(cmd, "print") == 0) {
471 mode = MODE_PRINT;
472 } else if (strcmp(cmd, "delete-string") == 0) {
473 mode = MODE_DELETE_STRING;
474 } else if (strcmp(cmd, "shorten-string") == 0) {
475 mode = MODE_SHORTEN_STRING;
476 } else if (strcmp(cmd, "x-string") == 0) {
477 mode = MODE_X_STRING;
478 } else if (strncmp(cmd, "rm-toks-", 8) == 0) {
479 mode = MODE_RM_TOKS;
480 int res = sscanf(&cmd[8], "%d", &n_toks);
481 assert(res == 1);
482 assert(n_toks > 0 && n_toks <= 1000);
483 } else if (strncmp(cmd, "rm-tok-pattern-", 15) == 0) {
484 mode = MODE_RM_TOK_PATTERN;
485 int res = sscanf(&cmd[15], "%d", &n_toks);
486 assert(res == 1);
487 assert(n_toks > 1 && n_toks <= 8);
488 } else if (strcmp(cmd, "define") == 0) {
489 mode = MODE_DEFINE;
490 } else {
491 printf("error: unknown mode '%s'\n", cmd);
492 assert(0);
493 }
494
495 int tok_index;
496 int ret = sscanf(argv[2], "%d", &tok_index);
497 assert(ret == 1);
498 // printf ("file = '%s'\n", argv[3]);
499 FILE *in = fopen(argv[3], "r");
500 assert(in);
501 yyin = in;
502
503 max_toks = initial_length;
504 tok_list = (struct tok_t *)malloc(max_toks * sizeof(struct tok_t));
505 assert(tok_list);
506
507 yylex();
508
509 // these calls all exit() at the end
510 switch (mode) {
511 case MODE_PRINT:
512 print_toks();
513 assert(0);
514 case MODE_RENAME:
515 rename_toks(tok_index);
516 assert(0);
517 case MODE_DELETE_STRING:
518 delete_string(tok_index);
519 assert(0);
520 case MODE_SHORTEN_STRING:
521 shorten_string(tok_index);
522 assert(0);
523 case MODE_X_STRING:
524 x_string(tok_index);
525 assert(0);
526 case MODE_RM_TOKS:
527 rm_toks(tok_index);
528 assert(0);
529 case MODE_RM_TOK_PATTERN:
530 rm_tok_pattern(tok_index);
531 assert(0);
532 case MODE_DEFINE:
533 define(tok_index);
534 assert(0);
535 default:
536 assert(0);
537 }
538 }