(main): Change argv[i] to argv[0][i].
[platform/upstream/gcc.git] / gcc / gen-protos.c
1 /* gen-protos.c - massages a list of prototypes, for use by fixproto.
2    Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
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
16 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 #include <stdio.h>
19 #include <ctype.h>
20 #include "hconfig.h"
21 #include "scan.h"
22 #include "cpplib.h"
23 #include "cpphash.h"
24
25 #define HASH_SIZE 2503 /* a prime */
26
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
41 int hash_tab[HASH_SIZE];
42 int verbose = 0;
43 char *progname;
44
45 sstring linebuf;
46
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
57 int
58 main (argc, argv)
59      int argc;
60      char** argv;
61 {
62   FILE *inf = stdin;
63   FILE *outf = stdout;
64   int next_index = 0;
65   int i, i0;
66
67   i = strlen (argv[0]);
68   while (i > 0 && argv[0][i-1] != '/') --i;
69   progname = &argv[0][i];
70
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         {
119           if (verbose)
120             fprintf (stderr, "%s: Can't handle this complex prototype: %s\n",
121                      argv[0], linebuf.base);
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,
142          also edit lookup_std_proto in fix-header.c !! */
143       i = hashf (name_start, name_end - name_start, HASH_SIZE);
144       i0 = i;
145       if (hash_tab[i] != 0)
146         {
147           for (;;)
148             {
149               i = (i+1) % HASH_SIZE;
150               if (i == i0)
151                 abort ();
152               if (hash_tab[i] == 0)
153                 break;
154             }
155         }
156       hash_tab[i] = next_index;
157
158       fprintf (outf, "  {\"%s\", \"%s\", \"%s\" },\n",
159                name_start, decl_start, param_start);
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 }
177
178 void
179 fatal (s)
180      char *s;
181 {
182   fprintf (stderr, "%s: %s\n", "gen-protos", s);
183   exit (FATAL_EXIT_CODE);
184 }