]>
Commit | Line | Data |
---|---|---|
7936052f | 1 | /* gen-protos.c - massages a list of prototypes, for use by fixproto. |
3efba298 | 2 | Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc. |
7936052f PB |
3 | |
4 | This program is free software; you can redistribute it and/or modify it | |
5 | under the terms of the GNU General Public License as published by the | |
6 | Free Software Foundation; either version 2, or (at your option) any | |
7 | later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this program; if not, write to the Free Software | |
a35311b0 | 16 | Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
7936052f PB |
17 | |
18 | #include <stdio.h> | |
794c765c | 19 | #include <ctype.h> |
7f77b42c | 20 | #include "hconfig.h" |
7936052f | 21 | #include "scan.h" |
b3ca463c PB |
22 | #include "cpplib.h" |
23 | #include "cpphash.h" | |
7936052f PB |
24 | |
25 | #define HASH_SIZE 2503 /* a prime */ | |
26 | ||
b3ca463c PB |
27 | int |
28 | hashf (name, len, hashsize) | |
29 | register U_CHAR *name; | |
30 | register int len; | |
31 | int hashsize; | |
32 | { | |
33 | register int r = 0; | |
34 | ||
35 | while (len--) | |
36 | r = HASHSTEP (r, *name++); | |
37 | ||
38 | return MAKE_POS (r) % hashsize; | |
39 | } | |
40 | ||
7936052f | 41 | int hash_tab[HASH_SIZE]; |
cefd15ce | 42 | int verbose = 0; |
03cad97d | 43 | char *progname; |
7936052f PB |
44 | |
45 | sstring linebuf; | |
46 | ||
629b20e2 RS |
47 | /* Avoid error if config defines abort as fancy_abort. |
48 | It's not worth "really" implementing this because ordinary | |
49 | compiler users never run fix-header. */ | |
50 | ||
51 | void | |
52 | fancy_abort () | |
53 | { | |
54 | abort (); | |
55 | } | |
56 | ||
7936052f PB |
57 | int |
58 | main (argc, argv) | |
59 | int argc; | |
0f41302f | 60 | char **argv; |
7936052f PB |
61 | { |
62 | FILE *inf = stdin; | |
63 | FILE *outf = stdout; | |
64 | int next_index = 0; | |
65 | int i, i0; | |
66 | ||
03cad97d | 67 | i = strlen (argv[0]); |
a36cf2bb JW |
68 | while (i > 0 && argv[0][i-1] != '/') --i; |
69 | progname = &argv[0][i]; | |
03cad97d | 70 | |
7936052f PB |
71 | fprintf (outf, "struct fn_decl std_protos[] = {\n"); |
72 | ||
73 | for (;;) | |
74 | { | |
75 | int c = skip_spaces (inf, ' '); | |
76 | int param_nesting = 1; | |
77 | char *param_start, *param_end, *decl_start, | |
78 | *name_start, *name_end; | |
79 | register char *ptr; | |
80 | if (c == EOF) | |
81 | break; | |
82 | linebuf.ptr = linebuf.base; | |
83 | ungetc (c, inf); | |
84 | c = read_upto (inf, &linebuf, '\n'); | |
85 | if (linebuf.base[0] == '#') /* skip cpp command */ | |
86 | continue; | |
87 | if (linebuf.base[0] == '\0') /* skip empty line */ | |
88 | continue; | |
89 | ||
90 | ptr = linebuf.ptr - 1; | |
91 | while (*ptr == ' ' || *ptr == '\t') ptr--; | |
92 | if (*ptr-- != ';') | |
93 | { | |
94 | fprintf (stderr, "Funny input line: %s\n", linebuf.base); | |
95 | continue; | |
96 | } | |
97 | while (*ptr == ' ' || *ptr == '\t') ptr--; | |
98 | if (*ptr != ')') | |
99 | { | |
100 | fprintf (stderr, "Funny input line: %s\n", linebuf.base); | |
101 | continue; | |
102 | } | |
103 | param_end = ptr; | |
104 | for (;;) | |
105 | { | |
106 | int c = *--ptr; | |
107 | if (c == '(' && --param_nesting == 0) | |
108 | break; | |
109 | else if (c == ')') | |
110 | param_nesting++; | |
111 | } | |
112 | param_start = ptr+1; | |
113 | ||
114 | ptr--; | |
115 | while (*ptr == ' ' || *ptr == '\t') ptr--; | |
116 | ||
117 | if (!isalnum (*ptr)) | |
118 | { | |
cefd15ce PB |
119 | if (verbose) |
120 | fprintf (stderr, "%s: Can't handle this complex prototype: %s\n", | |
121 | argv[0], linebuf.base); | |
7936052f PB |
122 | continue; |
123 | } | |
124 | name_end = ptr+1; | |
125 | ||
126 | while (isalnum (*ptr) || *ptr == '_') --ptr; | |
127 | name_start = ptr+1; | |
128 | while (*ptr == ' ' || *ptr == '\t') ptr--; | |
129 | ptr[1] = 0; | |
130 | *name_end = 0; | |
131 | *param_end = 0; | |
132 | *name_end = 0; | |
133 | ||
134 | decl_start = linebuf.base; | |
135 | if (strncmp (decl_start, "typedef ", 8) == 0) | |
136 | continue; | |
137 | if (strncmp (decl_start, "extern ", 7) == 0) | |
138 | decl_start += 7; | |
139 | ||
140 | ||
141 | /* NOTE: If you edit this, | |
82020a12 | 142 | also edit lookup_std_proto in fix-header.c !! */ |
b3ca463c | 143 | i = hashf (name_start, name_end - name_start, HASH_SIZE); |
7936052f PB |
144 | i0 = i; |
145 | if (hash_tab[i] != 0) | |
146 | { | |
147 | for (;;) | |
148 | { | |
149 | i = (i+1) % HASH_SIZE; | |
150 | if (i == i0) | |
629b20e2 | 151 | abort (); |
7936052f PB |
152 | if (hash_tab[i] == 0) |
153 | break; | |
154 | } | |
155 | } | |
156 | hash_tab[i] = next_index; | |
157 | ||
633b477b | 158 | fprintf (outf, " {\"%s\", \"%s\", \"%s\" },\n", |
1e83f71a | 159 | name_start, decl_start, param_start); |
7936052f PB |
160 | |
161 | next_index++; | |
162 | ||
163 | if (c == EOF) | |
164 | break; | |
165 | } | |
166 | fprintf (outf, "{0, 0, 0}\n};\n"); | |
167 | ||
168 | ||
169 | fprintf (outf, "#define HASH_SIZE %d\n", HASH_SIZE); | |
170 | fprintf (outf, "short hash_tab[HASH_SIZE] = {\n"); | |
171 | for (i = 0; i < HASH_SIZE; i++) | |
172 | fprintf (outf, " %d,\n", hash_tab[i]); | |
173 | fprintf (outf, "};\n"); | |
174 | ||
175 | return 0; | |
176 | } | |
b3ca463c PB |
177 | |
178 | void | |
179 | fatal (s) | |
180 | char *s; | |
181 | { | |
182 | fprintf (stderr, "%s: %s\n", "gen-protos", s); | |
3efba298 | 183 | exit (FATAL_EXIT_CODE); |
b3ca463c | 184 | } |