4 struct parsed_proto_arg {
7 struct parsed_proto *fptr;
14 struct parsed_proto_arg arg[16];
18 unsigned int is_func:1;
19 unsigned int is_stdcall:1;
20 unsigned int is_fptr:1;
21 unsigned int is_array:1;
24 static const char *hdrfn;
25 static int hdrfline = 0;
27 static int find_protostr(char *dst, size_t dlen, FILE *fhdr,
28 const char *fname, const char *sym_)
30 const char *sym = sym_;
31 const char *finc_name;
38 if (sym[0] == '_' && strncmp(fname, "stdc", 4) == 0)
44 while (fgets(dst, dlen, fhdr))
47 if (strncmp(dst, "//#include ", 11) == 0) {
49 p = strpbrk(finc_name, "\r\n ");
53 finc = fopen(finc_name, "r");
55 printf("%s:%d: can't open '%s'\n",
56 fname, line, finc_name);
59 ret = find_protostr(dst, dlen, finc,
66 if (strncmp(sskip(dst), "//", 2) == 0)
70 if (p != NULL && p > dst
71 && (my_isblank(p[-1]) || my_issep(p[-1]))
72 && (my_isblank(p[symlen]) || my_issep(p[symlen])))
80 p = dst + strlen(dst);
81 for (p--; p > dst && my_isblank(*p); --p)
87 static int get_regparm(char *dst, size_t dlen, char *p)
94 for (o = 0, i = 1; o < dlen; i++) {
106 static const char *known_type_mod[] = {
112 static const char *known_types[] = {
139 // returns ptr to char after type ends
140 static const char *typecmp(const char *n, const char *t)
142 for (; *t != 0; n++, t++) {
143 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
145 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
154 static char *check_type(const char *name, int *len_out)
156 const char *n = name, *n1;
162 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
163 len = strlen(known_type_mod[i]);
164 if (strncmp(n, known_type_mod[i], len) != 0)
168 while (my_isblank(*n))
173 for (i = 0; i < ARRAY_SIZE(known_types); i++) {
174 n1 = typecmp(n, known_types[i]);
178 *len_out = n1 - name;
179 return strndup(name, *len_out);
185 /* args are always expanded to 32bit */
186 static const char *map_reg(const char *reg)
188 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
189 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
190 const char *regs_b[] = { "al", "bl", "cl", "dl" };
193 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
194 if (IS(reg, regs_w[i]))
197 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
198 if (IS(reg, regs_b[i]))
204 static int parse_protostr(char *protostr, struct parsed_proto *pp)
206 struct parsed_proto_arg *arg;
217 if (p[0] == '/' && p[1] == '/') {
218 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
222 // strip unneeded stuff
223 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
224 if ((p1[0] == '/' && p1[1] == '*')
225 || (p1[0] == '*' && p1[1] == '/'))
229 if (strncmp(p, "extern ", 7) == 0)
231 if (strncmp(p, "WINBASEAPI ", 11) == 0)
234 kt = check_type(p, &ret);
236 printf("%s:%d:%ld: unhandled return in '%s'\n",
237 hdrfn, hdrfline, (p - protostr) + 1, protostr);
243 if (!strchr(p, ')')) {
244 p = next_idt(buf, sizeof(buf), p);
247 printf("%s:%d:%ld: var name missing\n",
248 hdrfn, hdrfline, (p - protostr) + 1);
251 strcpy(pp->name, buf);
268 p = next_word(cconv, sizeof(cconv), p);
271 printf("%s:%d:%ld: cconv missing\n",
272 hdrfn, hdrfline, (p - protostr) + 1);
275 if (IS(cconv, "__cdecl"))
277 else if (IS(cconv, "__stdcall"))
279 else if (IS(cconv, "__fastcall"))
281 else if (IS(cconv, "__thiscall"))
283 else if (IS(cconv, "__userpurge"))
284 pp->is_stdcall = 1; // in all cases seen..
285 else if (IS(cconv, "__usercall"))
286 pp->is_stdcall = 0; // ..or is it?
287 else if (IS(cconv, "WINAPI"))
290 printf("%s:%d:%ld: unhandled cconv: '%s'\n",
291 hdrfn, hdrfline, (p - protostr) + 1, cconv);
297 printf("%s:%d:%ld: '*' expected\n",
298 hdrfn, hdrfline, (p - protostr) + 1);
304 p = next_idt(buf, sizeof(buf), p);
307 printf("%s:%d:%ld: func name missing\n",
308 hdrfn, hdrfline, (p - protostr) + 1);
311 strcpy(pp->name, buf);
313 ret = get_regparm(regparm, sizeof(regparm), p);
315 if (!IS(regparm, "eax") && !IS(regparm, "ax")
316 && !IS(regparm, "al"))
318 printf("%s:%d:%ld: bad regparm: %s\n",
319 hdrfn, hdrfline, (p - protostr) + 1, regparm);
328 printf("%s:%d:%ld: ')' expected\n",
329 hdrfn, hdrfline, (p - protostr) + 1);
336 printf("%s:%d:%ld: '(' expected, got '%c'\n",
337 hdrfn, hdrfline, (p - protostr) + 1, *p);
344 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
345 && *sskip(p + 4) == ')')
357 arg = &pp->arg[xarg];
361 kt = check_type(p, &ret);
363 printf("%s:%d:%ld: unhandled type for arg%d\n",
364 hdrfn, hdrfline, (p - protostr) + 1, xarg);
372 arg->fptr = calloc(1, sizeof(*arg->fptr));
373 ret = parse_protostr(p1, arg->fptr);
375 printf("%s:%d:%ld: funcarg parse failed\n",
376 hdrfn, hdrfline, p1 - protostr);
379 // we'll treat it as void * for non-calls
380 arg->type = "void *";
385 p = next_idt(buf, sizeof(buf), p);
389 printf("%s:%d:%ld: idt missing for arg%d\n",
390 hdrfn, hdrfline, (p - protostr) + 1, xarg);
396 ret = get_regparm(regparm, sizeof(regparm), p);
401 arg->reg = strdup(map_reg(regparm));
405 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
406 if (pp->arg[0].reg != NULL) {
407 printf("%s:%d: %s with arg1 spec %s?\n",
408 hdrfn, hdrfline, cconv, pp->arg[0].reg);
410 pp->arg[0].reg = strdup("ecx");
413 if (xarg > 1 && IS(cconv, "__fastcall")) {
414 if (pp->arg[1].reg != NULL) {
415 printf("%s:%d: %s with arg2 spec %s?\n",
416 hdrfn, hdrfline, cconv, pp->arg[1].reg);
418 pp->arg[1].reg = strdup("edx");
423 for (i = 0; i < pp->argc; i++) {
424 if (pp->arg[i].reg == NULL)
433 static int proto_parse(FILE *fhdr, const char *sym, struct parsed_proto *pp)
438 memset(pp, 0, sizeof(*pp));
440 ret = find_protostr(protostr, sizeof(protostr), fhdr, hdrfn, sym);
442 printf("%s: sym '%s' is missing\n", hdrfn, sym);
446 return parse_protostr(protostr, pp) < 0 ? -1 : 0;
449 static void proto_release(struct parsed_proto *pp)
453 for (i = 0; i < pp->argc; i++) {
454 if (pp->arg[i].reg != NULL)
455 free(pp->arg[i].reg);
456 if (pp->arg[i].type != NULL)
457 free(pp->arg[i].type);
458 if (pp->arg[i].fptr != NULL)
459 free(pp->arg[i].fptr);
461 if (pp->ret_type != NULL)