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