FSF GCC merge 02/23/03
[official-gcc.git] / gcc / scan.c
blob9e2476f92ac2ae7ad673b9eb83355c189987c7c8
1 /* Utility functions for scan-decls and fix-header programs.
2 Copyright (C) 1993, 1994, 1998, 2002 Free Software Foundation, Inc.
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.
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.
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. */
18 #include "bconfig.h"
19 #include "system.h"
20 #include "coretypes.h"
21 #include "tm.h"
22 #include "scan.h"
24 int lineno = 1;
25 int source_lineno = 1;
26 sstring source_filename;
28 void
29 make_sstring_space (str, count)
30 sstring *str;
31 int count;
33 int cur_pos = str->ptr - str->base;
34 int cur_size = str->limit - str->base;
35 int new_size = cur_pos + count + 100;
37 if (new_size <= cur_size)
38 return;
40 str->base = xrealloc (str->base, new_size);
41 str->ptr = str->base + cur_size;
42 str->limit = str->base + new_size;
45 void
46 sstring_append (dst, src)
47 sstring *dst;
48 sstring *src;
50 char *d, *s;
51 int count = SSTRING_LENGTH (src);
53 MAKE_SSTRING_SPACE (dst, count + 1);
54 d = dst->ptr;
55 s = src->base;
56 while (--count >= 0) *d++ = *s++;
57 dst->ptr = d;
58 *d = 0;
61 int
62 scan_ident (fp, s, c)
63 FILE *fp;
64 sstring *s;
65 int c;
67 s->ptr = s->base;
68 if (ISIDST (c))
70 for (;;)
72 SSTRING_PUT (s, c);
73 c = getc (fp);
74 if (c == EOF || ! ISIDNUM (c))
75 break;
78 MAKE_SSTRING_SPACE (s, 1);
79 *s->ptr = 0;
80 return c;
83 int
84 scan_string (fp, s, init)
85 FILE *fp;
86 sstring *s;
87 int init;
89 int c;
91 for (;;)
93 c = getc (fp);
94 if (c == EOF || c == '\n')
95 break;
96 if (c == init)
98 c = getc (fp);
99 break;
101 if (c == '\\')
103 c = getc (fp);
104 if (c == EOF)
105 break;
106 if (c == '\n')
107 continue;
109 SSTRING_PUT (s, c);
111 MAKE_SSTRING_SPACE (s, 1);
112 *s->ptr = 0;
113 return c;
116 /* Skip horizontal white spaces (spaces, tabs, and C-style comments). */
119 skip_spaces (fp, c)
120 FILE *fp;
121 int c;
123 for (;;)
125 if (c == ' ' || c == '\t')
126 c = getc (fp);
127 else if (c == '/')
129 c = getc (fp);
130 if (c != '*')
132 ungetc (c, fp);
133 return '/';
135 c = getc (fp);
136 for (;;)
138 if (c == EOF)
139 return EOF;
140 else if (c != '*')
142 if (c == '\n')
143 source_lineno++, lineno++;
144 c = getc (fp);
146 else if ((c = getc (fp)) == '/')
147 return getc (fp);
150 else
151 break;
153 return c;
157 read_upto (fp, str, delim)
158 FILE *fp;
159 sstring *str;
160 int delim;
162 int ch;
164 for (;;)
166 ch = getc (fp);
167 if (ch == EOF || ch == delim)
168 break;
169 SSTRING_PUT (str, ch);
171 MAKE_SSTRING_SPACE (str, 1);
172 *str->ptr = 0;
173 return ch;
177 get_token (fp, s)
178 FILE *fp;
179 sstring *s;
181 int c;
183 s->ptr = s->base;
184 retry:
185 c = ' ';
186 c = skip_spaces (fp, c);
187 if (c == '\n')
189 source_lineno++;
190 lineno++;
191 goto retry;
193 if (c == '#')
195 c = get_token (fp, s);
196 if (c == INT_TOKEN)
198 source_lineno = atoi (s->base) - 1; /* '\n' will add 1 */
199 get_token (fp, &source_filename);
201 for (;;)
203 c = getc (fp);
204 if (c == EOF)
205 return EOF;
206 if (c == '\n')
208 source_lineno++;
209 lineno++;
210 goto retry;
214 if (c == EOF)
215 return EOF;
216 if (ISDIGIT (c))
220 SSTRING_PUT (s, c);
221 c = getc (fp);
222 } while (c != EOF && ISDIGIT (c));
223 ungetc (c, fp);
224 c = INT_TOKEN;
225 goto done;
227 if (ISIDST (c))
229 c = scan_ident (fp, s, c);
230 ungetc (c, fp);
231 return IDENTIFIER_TOKEN;
233 if (c == '\'' || c == '"')
235 c = scan_string (fp, s, c);
236 ungetc (c, fp);
237 return c == '\'' ? CHAR_TOKEN : STRING_TOKEN;
239 SSTRING_PUT (s, c);
240 done:
241 MAKE_SSTRING_SPACE (s, 1);
242 *s->ptr = 0;
243 return c;
246 unsigned int
247 hashstr (str, len)
248 const char *str;
249 unsigned int len;
251 unsigned int n = len;
252 unsigned int r = 0;
253 const unsigned char *s = (const unsigned char *) str;
256 r = r * 67 + (*s++ - 113);
257 while (--n);
258 return r + len;