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