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