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;
56 if (s2[i] == 0 || s2[i] == '@')
60 if (s2[i] == 0 || s1[i] == '@')
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;
92 while (fgets(line, sizeof(line), f) != NULL) {
93 r = next_word(word, sizeof(word), line);
97 sl[c].name = strdup(word);
98 sl[c].callsites = callsites;
104 sl = realloc(sl, *alloc * sizeof(sl[0]));
105 my_assert_not(sl, NULL);
106 memset(sl + c, 0, (*alloc - c) * sizeof(sl[0]));
114 const char *sym_use(const struct sl_item *sym)
116 static char buf[256+3];
119 ret = snprintf(buf, sizeof(buf), "%s%s",
120 sym->callsites ? "" : "rm_", sym->name);
121 if (ret >= sizeof(buf)) {
122 printf("truncation detected: '%s'\n", buf);
129 int main(int argc, char *argv[])
131 struct sl_item *symlist, *sym, ssym = { NULL, };
132 int patch_callsites = 0;
133 FILE *fout, *fin, *f;
145 // -c - patch callsites
146 printf("usage:\n%s <asmf_out> <asmf_in> [[-c] <listf>]*>\n",
153 symlist = calloc(symlist_alloc, sizeof(symlist[0]));
154 my_assert_not(symlist, NULL);
156 for (i = 3; i < argc; i++) {
157 if (!strcmp(argv[i], "-c")) {
162 f = fopen(argv[i], "r");
163 my_assert_not(f, NULL);
164 read_list(&symlist, &symlist_cnt, &symlist_alloc,
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].callsites, 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);
212 if (sym != NULL && sym->callsites) {
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);
229 if (sym != NULL && sym->callsites) {
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);
246 if (sym != NULL && sym->callsites) {
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);