ede51255 |
1 | #include <stdio.h> |
2 | #include <stdlib.h> |
3 | #include <string.h> |
4 | |
5 | #include "my_assert.h" |
6 | |
7 | static int my_isblank(char c) |
8 | { |
9 | return c == '\t' || c == ' ' || c == '\r' || c == '\n'; |
10 | } |
11 | |
12 | static char *sskip(char *s) |
13 | { |
14 | while (my_isblank(*s)) |
15 | s++; |
16 | |
17 | return s; |
18 | } |
19 | |
20 | static char *next_word(char *w, size_t wsize, char *s) |
21 | { |
22 | size_t i; |
23 | |
24 | s = sskip(s); |
25 | |
26 | for (i = 0; i < wsize - 1; i++) { |
27 | if (*s == 0 || my_isblank(s[i])) |
28 | break; |
29 | w[i] = s[i]; |
30 | } |
31 | w[i] = 0; |
32 | |
33 | if (*s != 0 && !my_isblank(s[i])) |
34 | printf("warning: '%s' truncated\n", w); |
35 | |
36 | return s + i; |
37 | } |
38 | |
39 | struct sl_item { |
40 | char *name; |
41 | int is_replace; |
42 | }; |
43 | |
44 | static int cmp_sym(const void *p1_, const void *p2_) |
45 | { |
46 | const struct sl_item *p1 = p1_, *p2 = p2_; |
47 | const char *s1 = p1->name, *s2 = p2->name; |
48 | int i; |
49 | |
50 | for (i = 0; ; i++) { |
51 | if (s1[i] == s2[i]) |
52 | continue; |
53 | |
54 | if (s1[i] == 0) { |
55 | if (s2[i] == 0 || s2[i] == '@') |
56 | break; |
57 | } |
58 | |
59 | if (s2[i] == 0 || s1[i] == '@') |
60 | break; |
61 | |
62 | return s1[i] - s2[i]; |
63 | } |
64 | |
65 | return 0; |
66 | } |
67 | |
68 | static int cmp_sym_sort(const void *p1_, const void *p2_) |
69 | { |
70 | const struct sl_item *p1 = p1_, *p2 = p2_; |
71 | const char *s1 = p1->name, *s2 = p2->name; |
72 | int ret; |
73 | |
74 | ret = cmp_sym(p1_, p2_); |
75 | if (ret == 0) { |
76 | printf("%s: dupe sym: '%s' '%s'\n", __func__, s1, s2); |
77 | exit(1); |
78 | } |
79 | return ret; |
80 | } |
81 | |
82 | void read_list(struct sl_item **sl_in, int *cnt, int *alloc, FILE *f, int is_repl) |
83 | { |
84 | struct sl_item *sl = *sl_in; |
85 | int c = *cnt; |
86 | char line[256]; |
87 | char word[256]; |
88 | char *r; |
89 | |
90 | while (fgets(line, sizeof(line), f) != NULL) { |
91 | r = next_word(word, sizeof(word), line); |
92 | if (r == word) |
93 | continue; |
94 | |
95 | sl[c].name = strdup(word); |
96 | sl[c].is_replace = is_repl; |
97 | c++; |
98 | |
99 | if (c >= *alloc) { |
100 | *alloc *= 2; |
101 | sl = realloc(sl, *alloc * sizeof(sl[0])); |
102 | my_assert_not(sl, NULL); |
103 | } |
104 | } |
105 | |
106 | *sl_in = sl; |
107 | *cnt = c; |
108 | } |
109 | |
110 | int main(int argc, char *argv[]) |
111 | { |
112 | struct sl_item *symlist, *sym, ssym; |
113 | FILE *fout, *fin, *f; |
114 | int symlist_alloc; |
115 | int symlist_cnt; |
116 | char line[256]; |
117 | char word[256]; |
118 | char word2[256]; |
119 | char word3[256]; |
120 | char word4[256]; |
121 | char *p; |
122 | |
123 | if (argc != 5) { |
124 | // rmlist - prefix func with 'rm_', callsites with '_' |
125 | // renlist - prefix func and callsites with 'rm_' |
126 | printf("usage:\n%s <asmf_out> <asmf_in> <rmlist> <renlist>\n", |
127 | argv[0]); |
128 | return 1; |
129 | } |
130 | |
131 | symlist_alloc = 16; |
132 | symlist_cnt = 0; |
133 | symlist = malloc(symlist_alloc * sizeof(symlist[0])); |
134 | my_assert_not(symlist, NULL); |
135 | |
136 | f = fopen(argv[3], "r"); |
137 | my_assert_not(f, NULL); |
138 | read_list(&symlist, &symlist_cnt, &symlist_alloc, f, 1); |
139 | fclose(f); |
140 | |
141 | f = fopen(argv[4], "r"); |
142 | my_assert_not(f, NULL); |
143 | read_list(&symlist, &symlist_cnt, &symlist_alloc, f, 0); |
144 | fclose(f); |
145 | |
146 | qsort(symlist, symlist_cnt, sizeof(symlist[0]), cmp_sym_sort); |
147 | |
148 | printf("symlist:\n"); |
149 | int i; |
150 | for (i = 0; i < symlist_cnt; i++) |
151 | printf("%d '%s'\n", symlist[i].is_replace, symlist[i].name); |
152 | |
153 | fin = fopen(argv[2], "r"); |
154 | my_assert_not(fin, NULL); |
155 | |
156 | fout = fopen(argv[1], "w"); |
157 | my_assert_not(fout, NULL); |
158 | |
159 | while (fgets(line, sizeof(line), fin)) |
160 | { |
161 | p = sskip(line); |
162 | if (*p == 0 || *p == ';') |
163 | goto pass; |
164 | |
165 | p = sskip(next_word(word, sizeof(word), p)); |
166 | if (*p == 0 || *p == ';') |
167 | goto pass; // need at least 2 words |
168 | |
169 | p = next_word(word2, sizeof(word2), p); |
170 | |
171 | if (!strcasecmp(word2, "proc") || !strcasecmp(word2, "endp")) { |
172 | ssym.name = word; |
173 | sym = bsearch(&ssym, symlist, symlist_cnt, |
174 | sizeof(symlist[0]), cmp_sym); |
175 | if (sym != NULL) { |
176 | fprintf(fout, "rm_%s\t%s%s", word, word2, p); |
177 | continue; |
178 | } |
179 | } |
180 | |
181 | if (!strcasecmp(word, "call") || !strcasecmp(word, "jmp")) { |
182 | ssym.name = word2; |
183 | sym = bsearch(&ssym, symlist, symlist_cnt, |
184 | sizeof(symlist[0]), cmp_sym); |
185 | if (sym != NULL) { |
186 | fprintf(fout, "\t\t%s\t%s%s%s", word, |
187 | sym->is_replace ? "_" : "rm_", word2, p); |
188 | continue; |
189 | } |
190 | } |
191 | |
192 | p = sskip(p); |
193 | if (*p == 0 || *p == ';') |
194 | goto pass; // need at least 3 words |
195 | |
196 | p = next_word(word3, sizeof(word3), p); |
197 | |
198 | if (!strcasecmp(word, "dd") && !strcasecmp(word2, "offset")) { |
199 | ssym.name = word3; |
200 | sym = bsearch(&ssym, symlist, symlist_cnt, |
201 | sizeof(symlist[0]), cmp_sym); |
202 | if (sym != NULL) { |
203 | fprintf(fout, "\t\tdd offset %s%s", word3, p); |
204 | continue; |
205 | } |
206 | } |
207 | |
208 | p = sskip(p); |
209 | if (*p == 0 || *p == ';') |
210 | goto pass; // need at least 4 words |
211 | |
212 | p = next_word(word4, sizeof(word4), p); |
213 | |
214 | if (!strcasecmp(word2, "dd") && !strcasecmp(word3, "offset")) { |
215 | ssym.name = word4; |
216 | sym = bsearch(&ssym, symlist, symlist_cnt, |
217 | sizeof(symlist[0]), cmp_sym); |
218 | if (sym != NULL) { |
219 | fprintf(fout, "%s\tdd offset %s%s%s", word, |
220 | sym->is_replace ? "_" : "rm_", word4, p); |
221 | continue; |
222 | } |
223 | } |
224 | |
225 | pass: |
226 | fwrite(line, 1, strlen(line), fout); |
227 | } |
228 | |
229 | fclose(fin); |
230 | fclose(fout); |
231 | |
232 | return 0; |
233 | } |