1f906263 |
1 | #include <stdio.h> |
2 | #include <stdlib.h> |
3 | #include <string.h> |
4 | |
5 | #include "my_assert.h" |
6 | #include "my_str.h" |
7 | |
8 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) |
9 | #define IS(w, y) !strcmp(w, y) |
10 | #define IS_START(w, y) !strncmp(w, y, strlen(y)) |
11 | |
12 | #include "protoparse.h" |
13 | |
14 | int main(int argc, char *argv[]) |
15 | { |
16 | const struct parsed_proto *pp; |
17 | FILE *fout, *fhdr; |
18 | char basename[256]; |
19 | char line[256]; |
20 | char fmt[256]; |
21 | char word[256]; |
aa1aa2c2 |
22 | int noname = 0; |
23 | const char *p2; |
1f906263 |
24 | char *p; |
aa1aa2c2 |
25 | int arg; |
1f906263 |
26 | int ret, ord; |
27 | int l; |
28 | |
aa1aa2c2 |
29 | for (arg = 1; arg < argc; arg++) { |
30 | if (IS(argv[arg], "-n")) |
31 | noname = 1; |
32 | else |
33 | break; |
34 | } |
35 | |
36 | if (argc != arg + 2) { |
37 | printf("usage:\n%s [-n] <.h> <.def>\n", argv[0]); |
1f906263 |
38 | return 1; |
39 | } |
40 | |
41 | hdrfn = argv[arg++]; |
42 | fhdr = fopen(hdrfn, "r"); |
43 | my_assert_not(fhdr, NULL); |
44 | |
45 | fout = fopen(argv[arg++], "w"); |
46 | my_assert_not(fout, NULL); |
47 | |
48 | p = strrchr(hdrfn, '.'); |
49 | my_assert_not(p, NULL); |
aa1aa2c2 |
50 | p2 = strrchr(hdrfn, '/'); |
51 | if (p2++ == NULL) |
52 | p2 = hdrfn; |
53 | l = p - p2; |
54 | my_assert((unsigned int)l < 256, 1); |
55 | memcpy(basename, p2, l); |
1f906263 |
56 | basename[l] = 0; |
57 | |
58 | snprintf(fmt, sizeof(fmt), "%s_%%d", basename); |
59 | |
60 | fprintf(fout, "LIBRARY %s\n", basename); |
61 | fprintf(fout, "EXPORTS\n"); |
62 | |
63 | while (fgets(line, sizeof(line), fhdr)) |
64 | { |
65 | p = sskip(line); |
66 | if (*p == 0) |
67 | continue; |
68 | |
69 | if (IS(p, "//")) |
70 | continue; |
71 | |
72 | ret = 0; |
73 | while (p != NULL && *p != 0) { |
74 | p = next_word(word, sizeof(word), p); |
75 | ret = sscanf(word, fmt, &ord); |
76 | if (ret == 1) |
77 | break; |
78 | } |
79 | if (ret != 1) { |
80 | printf("scan for '%s' failed for '%s'\n", fmt, line); |
81 | return 1; |
82 | } |
83 | |
84 | snprintf(word, sizeof(word), fmt, ord); |
85 | pp = proto_parse(fhdr, word, 0); |
86 | if (pp == NULL) |
87 | return 1; |
88 | |
89 | fprintf(fout, " %s", word); |
90 | if (pp->is_stdcall) |
91 | fprintf(fout, "@%-2d", pp->argc_stack * 4); |
92 | else |
93 | fprintf(fout, " "); |
aa1aa2c2 |
94 | fprintf(fout, " @%d", ord); |
95 | if (noname) |
96 | fprintf(fout, " NONAME"); |
97 | fprintf(fout, "\n"); |
1f906263 |
98 | } |
99 | |
100 | fclose(fhdr); |
101 | fclose(fout); |
102 | return 0; |
103 | } |
104 | |
105 | // vim:ts=2:shiftwidth=2:expandtab |