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);
44 static int cmp_sym(const void *p1_, const void *p2_)
46 const struct sl_item *p1 = p1_, *p2 = p2_;
47 const char *s1 = p1->name, *s2 = p2->name;
55 if (s2[i] == 0 || s2[i] == '@')
59 if (s2[i] == 0 || s1[i] == '@')
68 static int cmp_sym_sort(const void *p1_, const void *p2_)
70 const struct sl_item *p1 = p1_, *p2 = p2_;
71 const char *s1 = p1->name, *s2 = p2->name;
74 ret = cmp_sym(p1_, p2_);
76 printf("%s: dupe sym: '%s' '%s'\n", __func__, s1, s2);
82 void read_list(struct sl_item **sl_in, int *cnt, int *alloc, FILE *f, int is_repl)
84 struct sl_item *sl = *sl_in;
90 while (fgets(line, sizeof(line), f) != NULL) {
91 r = next_word(word, sizeof(word), line);
95 sl[c].name = strdup(word);
96 sl[c].is_replace = is_repl;
101 sl = realloc(sl, *alloc * sizeof(sl[0]));
102 my_assert_not(sl, NULL);
110 int main(int argc, char *argv[])
112 struct sl_item *symlist, *sym, ssym;
113 FILE *fout, *fin, *f;
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",
133 symlist = malloc(symlist_alloc * sizeof(symlist[0]));
134 my_assert_not(symlist, NULL);
136 f = fopen(argv[3], "r");
137 my_assert_not(f, NULL);
138 read_list(&symlist, &symlist_cnt, &symlist_alloc, f, 1);
141 f = fopen(argv[4], "r");
142 my_assert_not(f, NULL);
143 read_list(&symlist, &symlist_cnt, &symlist_alloc, f, 0);
146 qsort(symlist, symlist_cnt, sizeof(symlist[0]), cmp_sym_sort);
148 printf("symlist:\n");
150 for (i = 0; i < symlist_cnt; i++)
151 printf("%d '%s'\n", symlist[i].is_replace, symlist[i].name);
153 fin = fopen(argv[2], "r");
154 my_assert_not(fin, NULL);
156 fout = fopen(argv[1], "w");
157 my_assert_not(fout, NULL);
159 while (fgets(line, sizeof(line), fin))
162 if (*p == 0 || *p == ';')
165 p = sskip(next_word(word, sizeof(word), p));
166 if (*p == 0 || *p == ';')
167 goto pass; // need at least 2 words
169 p = next_word(word2, sizeof(word2), p);
171 if (!strcasecmp(word2, "proc") || !strcasecmp(word2, "endp")) {
173 sym = bsearch(&ssym, symlist, symlist_cnt,
174 sizeof(symlist[0]), cmp_sym);
176 fprintf(fout, "rm_%s\t%s%s", word, word2, p);
181 if (!strcasecmp(word, "call") || !strcasecmp(word, "jmp")) {
183 sym = bsearch(&ssym, symlist, symlist_cnt,
184 sizeof(symlist[0]), cmp_sym);
186 fprintf(fout, "\t\t%s\t%s%s%s", word,
187 sym->is_replace ? "_" : "rm_", word2, p);
193 if (*p == 0 || *p == ';')
194 goto pass; // need at least 3 words
196 p = next_word(word3, sizeof(word3), p);
198 if (!strcasecmp(word, "dd") && !strcasecmp(word2, "offset")) {
200 sym = bsearch(&ssym, symlist, symlist_cnt,
201 sizeof(symlist[0]), cmp_sym);
203 fprintf(fout, "\t\tdd offset %s%s", word3, p);
209 if (*p == 0 || *p == ';')
210 goto pass; // need at least 4 words
212 p = next_word(word4, sizeof(word4), p);
214 if (!strcasecmp(word2, "dd") && !strcasecmp(word3, "offset")) {
216 sym = bsearch(&ssym, symlist, symlist_cnt,
217 sizeof(symlist[0]), cmp_sym);
219 fprintf(fout, "%s\tdd offset %s%s%s", word,
220 sym->is_replace ? "_" : "rm_", word4, p);
226 fwrite(line, 1, strlen(line), fout);