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 callsites: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;
52 if ((s1[i] | s2[i]) == 0)
58 if (s1[i] == 0 && s2[i] == '@')
60 if (s1[i] == '@' && s2[i] == 0)
69 static int cmp_sym_sort(const void *p1_, const void *p2_)
71 const struct sl_item *p1 = p1_, *p2 = p2_;
72 const char *s1 = p1->name, *s2 = p2->name;
75 ret = cmp_sym(p1_, p2_);
77 printf("%s: dupe sym: '%s' '%s'\n", __func__, s1, s2);
83 void read_list(struct sl_item **sl_in, int *cnt, int *alloc,
84 FILE *f, int callsites)
86 struct sl_item *sl = *sl_in;
91 while (fgets(line, sizeof(line), f) != NULL) {
92 next_word(word, sizeof(word), line);
93 if (word[0] == 0 || word[0] == ';' || word[0] == '#')
96 sl[c].name = strdup(word);
97 sl[c].callsites = callsites;
103 sl = realloc(sl, *alloc * sizeof(sl[0]));
104 my_assert_not(sl, NULL);
105 memset(sl + c, 0, (*alloc - c) * sizeof(sl[0]));
113 const char *sym_use(const struct sl_item *sym)
115 static char buf[256+3];
118 ret = snprintf(buf, sizeof(buf), "rm_%s", sym->name);
119 if (ret >= sizeof(buf)) {
120 printf("truncation detected: '%s'\n", buf);
127 int main(int argc, char *argv[])
129 struct sl_item *symlist, *sym, ssym = { NULL, };
130 int patch_callsites = 0;
131 FILE *fout, *fin, *f;
143 // -c - patch callsites
144 printf("usage:\n%s <asmf_out> <asmf_in> [[-c] <listf>]*>\n",
151 symlist = calloc(symlist_alloc, sizeof(symlist[0]));
152 my_assert_not(symlist, NULL);
154 for (i = 3; i < argc; i++) {
155 if (strcmp(argv[i], "-c") == 0) {
160 f = fopen(argv[i], "r");
161 my_assert_not(f, NULL);
162 read_list(&symlist, &symlist_cnt, &symlist_alloc,
169 qsort(symlist, symlist_cnt, sizeof(symlist[0]), cmp_sym_sort);
172 printf("symlist:\n");
173 for (i = 0; i < symlist_cnt; i++)
174 printf("%d '%s'\n", symlist[i].callsites, symlist[i].name);
177 fin = fopen(argv[2], "r");
178 my_assert_not(fin, NULL);
180 fout = fopen(argv[1], "w");
181 my_assert_not(fout, NULL);
183 while (fgets(line, sizeof(line), fin))
186 if (*p == 0 || *p == ';')
189 p = sskip(next_word(word, sizeof(word), p));
190 if (*p == 0 || *p == ';')
191 goto pass; // need at least 2 words
193 p = next_word(word2, sizeof(word2), p);
195 if (!strcasecmp(word2, "proc") || !strcasecmp(word2, "endp")) {
197 sym = bsearch(&ssym, symlist, symlist_cnt,
198 sizeof(symlist[0]), cmp_sym);
201 fprintf(fout, "rm_%s\t%s%s", word, word2, p);
206 if (!strcasecmp(word, "call") || !strcasecmp(word, "jmp")) {
208 sym = bsearch(&ssym, symlist, symlist_cnt,
209 sizeof(symlist[0]), cmp_sym);
210 if (sym != NULL && sym->callsites) {
211 fprintf(fout, "\t\t%s\t%s%s", word,
218 if (*p == 0 || *p == ';')
219 goto pass; // need at least 3 words
221 p = next_word(word3, sizeof(word3), p);
223 if (!strcasecmp(word, "dd") && !strcasecmp(word2, "offset")) {
225 sym = bsearch(&ssym, symlist, symlist_cnt,
226 sizeof(symlist[0]), cmp_sym);
227 if (sym != NULL && sym->callsites) {
228 fprintf(fout, "\t\tdd offset %s%s",
235 if (*p == 0 || *p == ';')
236 goto pass; // need at least 4 words
238 p = next_word(word4, sizeof(word4), p);
240 if (!strcasecmp(word2, "dd") && !strcasecmp(word3, "offset")) {
242 sym = bsearch(&ssym, symlist, symlist_cnt,
243 sizeof(symlist[0]), cmp_sym);
244 if (sym != NULL && sym->callsites) {
245 fprintf(fout, "%s\tdd offset %s%s", word,
252 fwrite(line, 1, strlen(line), fout);
255 for (i = 0; i < symlist_cnt; i++) {
256 if (!symlist[i].found)
257 printf("warning: sym '%s' not found\n", symlist[i].name);