7 static int my_isblank(char c)
9 return c == '\t' || c == ' ' || c == '\r' || c == '\n';
12 static char *sskip(char *s)
14 while (my_isblank(*s))
20 static char *next_word(char *w, size_t wsize, char *s)
26 for (i = 0; i < wsize - 1; i++) {
27 if (*s == 0 || my_isblank(s[i]))
33 if (*s != 0 && !my_isblank(s[i]))
34 printf("warning: '%s' truncated\n", w);
41 unsigned int is_replace:1;
45 static int cmp_sym(const void *p1_, const void *p2_)
47 const struct sl_item *p1 = p1_, *p2 = p2_;
48 const char *s1 = p1->name, *s2 = p2->name;
51 // replace is with syms from C with '_' prepended
62 if (s2[i] == 0 || s2[i] == '@')
66 if (s2[i] == 0 || s1[i] == '@')
75 static int cmp_sym_sort(const void *p1_, const void *p2_)
77 const struct sl_item *p1 = p1_, *p2 = p2_;
78 const char *s1 = p1->name, *s2 = p2->name;
81 ret = cmp_sym(p1_, p2_);
83 printf("%s: dupe sym: '%s' '%s'\n", __func__, s1, s2);
89 void read_list(struct sl_item **sl_in, int *cnt, int *alloc, FILE *f, int is_repl)
91 struct sl_item *sl = *sl_in;
97 while (fgets(line, sizeof(line), f) != NULL) {
98 r = next_word(word, sizeof(word), line);
102 sl[c].name = strdup(word);
103 sl[c].is_replace = is_repl;
109 sl = realloc(sl, *alloc * sizeof(sl[0]));
110 my_assert_not(sl, NULL);
111 memset(sl + c, 0, (*alloc - c) * sizeof(sl[0]));
119 const char *sym_use(const struct sl_item *sym)
121 static char buf[256+3];
124 ret = snprintf(buf, sizeof(buf), "%s%s",
125 sym->is_replace ? "" : "rm_", sym->name);
126 if (ret >= sizeof(buf)) {
127 printf("truncation detected: '%s'\n", buf);
134 int main(int argc, char *argv[])
136 struct sl_item *symlist, *sym, ssym = { NULL, };
137 FILE *fout, *fin, *f;
149 // rmlist - prefix func with 'rm_', callsites with '_'
150 // renlist - prefix func and callsites with 'rm_'
151 printf("usage:\n%s <asmf_out> <asmf_in> <rmlist> <renlist>\n",
158 symlist = calloc(symlist_alloc, sizeof(symlist[0]));
159 my_assert_not(symlist, NULL);
161 f = fopen(argv[3], "r");
162 my_assert_not(f, NULL);
163 read_list(&symlist, &symlist_cnt, &symlist_alloc, f, 1);
166 f = fopen(argv[4], "r");
167 my_assert_not(f, NULL);
168 read_list(&symlist, &symlist_cnt, &symlist_alloc, f, 0);
171 qsort(symlist, symlist_cnt, sizeof(symlist[0]), cmp_sym_sort);
174 printf("symlist:\n");
175 for (i = 0; i < symlist_cnt; i++)
176 printf("%d '%s'\n", symlist[i].is_replace, symlist[i].name);
179 fin = fopen(argv[2], "r");
180 my_assert_not(fin, NULL);
182 fout = fopen(argv[1], "w");
183 my_assert_not(fout, NULL);
185 while (fgets(line, sizeof(line), fin))
188 if (*p == 0 || *p == ';')
191 p = sskip(next_word(word, sizeof(word), p));
192 if (*p == 0 || *p == ';')
193 goto pass; // need at least 2 words
195 p = next_word(word2, sizeof(word2), p);
197 if (!strcasecmp(word2, "proc") || !strcasecmp(word2, "endp")) {
199 sym = bsearch(&ssym, symlist, symlist_cnt,
200 sizeof(symlist[0]), cmp_sym);
203 fprintf(fout, "rm_%s\t%s%s", word, word2, p);
208 if (!strcasecmp(word, "call") || !strcasecmp(word, "jmp")) {
210 sym = bsearch(&ssym, symlist, symlist_cnt,
211 sizeof(symlist[0]), cmp_sym);
213 fprintf(fout, "\t\t%s\t%s%s", word,
220 if (*p == 0 || *p == ';')
221 goto pass; // need at least 3 words
223 p = next_word(word3, sizeof(word3), p);
225 if (!strcasecmp(word, "dd") && !strcasecmp(word2, "offset")) {
227 sym = bsearch(&ssym, symlist, symlist_cnt,
228 sizeof(symlist[0]), cmp_sym);
230 fprintf(fout, "\t\tdd offset %s%s",
237 if (*p == 0 || *p == ';')
238 goto pass; // need at least 4 words
240 p = next_word(word4, sizeof(word4), p);
242 if (!strcasecmp(word2, "dd") && !strcasecmp(word3, "offset")) {
244 sym = bsearch(&ssym, symlist, symlist_cnt,
245 sizeof(symlist[0]), cmp_sym);
247 fprintf(fout, "%s\tdd offset %s%s", word,
254 fwrite(line, 1, strlen(line), fout);
257 for (i = 0; i < symlist_cnt; i++) {
258 if (!symlist[i].found)
259 printf("warning: sym '%s' not found\n", symlist[i].name);