| 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]; |
| 22 | char *p; |
| 23 | int arg = 1; |
| 24 | int ret, ord; |
| 25 | int l; |
| 26 | |
| 27 | if (argc != 3) { |
| 28 | printf("usage:\n%s <.h> <.def>\n", argv[0]); |
| 29 | return 1; |
| 30 | } |
| 31 | |
| 32 | hdrfn = argv[arg++]; |
| 33 | fhdr = fopen(hdrfn, "r"); |
| 34 | my_assert_not(fhdr, NULL); |
| 35 | |
| 36 | fout = fopen(argv[arg++], "w"); |
| 37 | my_assert_not(fout, NULL); |
| 38 | |
| 39 | p = strrchr(hdrfn, '.'); |
| 40 | my_assert_not(p, NULL); |
| 41 | l = p - hdrfn; |
| 42 | my_assert(l < 256, 1); |
| 43 | memcpy(basename, hdrfn, l); |
| 44 | basename[l] = 0; |
| 45 | |
| 46 | snprintf(fmt, sizeof(fmt), "%s_%%d", basename); |
| 47 | |
| 48 | fprintf(fout, "LIBRARY %s\n", basename); |
| 49 | fprintf(fout, "EXPORTS\n"); |
| 50 | |
| 51 | while (fgets(line, sizeof(line), fhdr)) |
| 52 | { |
| 53 | p = sskip(line); |
| 54 | if (*p == 0) |
| 55 | continue; |
| 56 | |
| 57 | if (IS(p, "//")) |
| 58 | continue; |
| 59 | |
| 60 | ret = 0; |
| 61 | while (p != NULL && *p != 0) { |
| 62 | p = next_word(word, sizeof(word), p); |
| 63 | ret = sscanf(word, fmt, &ord); |
| 64 | if (ret == 1) |
| 65 | break; |
| 66 | } |
| 67 | if (ret != 1) { |
| 68 | printf("scan for '%s' failed for '%s'\n", fmt, line); |
| 69 | return 1; |
| 70 | } |
| 71 | |
| 72 | snprintf(word, sizeof(word), fmt, ord); |
| 73 | pp = proto_parse(fhdr, word, 0); |
| 74 | if (pp == NULL) |
| 75 | return 1; |
| 76 | |
| 77 | fprintf(fout, " %s", word); |
| 78 | if (pp->is_stdcall) |
| 79 | fprintf(fout, "@%-2d", pp->argc_stack * 4); |
| 80 | else |
| 81 | fprintf(fout, " "); |
| 82 | fprintf(fout, " @%d\n", ord); |
| 83 | } |
| 84 | |
| 85 | fclose(fhdr); |
| 86 | fclose(fout); |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | // vim:ts=2:shiftwidth=2:expandtab |