* system.h (malloc, realloc, calloc, strdup, bzero, bcmp, rindex):
[official-gcc.git] / gcc / scan.c
blob4af6a43a702243f1117d15bd69f5315f00240ccb
1 /* Utility functions for scan-decls and fix-header programs.
2 Copyright (C) 1993, 1994, 1998 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 "hconfig.h"
19 #include "system.h"
20 #include "scan.h"
22 int lineno = 1;
23 int source_lineno = 1;
24 sstring source_filename;
26 void
27 make_sstring_space (str, count)
28 sstring *str;
29 int count;
31 int cur_pos = str->ptr - str->base;
32 int cur_size = str->limit - str->base;
33 int new_size = cur_pos + count + 100;
35 if (new_size <= cur_size)
36 return;
38 str->base = xrealloc (str->base, new_size);
39 str->ptr = str->base + cur_size;
40 str->limit = str->base + new_size;
43 void
44 sstring_append (dst, src)
45 sstring *dst;
46 sstring *src;
48 register char *d, *s;
49 register int count = SSTRING_LENGTH(src);
50 MAKE_SSTRING_SPACE(dst, count + 1);
51 d = dst->ptr;
52 s = src->base;
53 while (--count >= 0) *d++ = *s++;
54 dst->ptr = d;
55 *d = 0;
58 int
59 scan_ident (fp, s, c)
60 register FILE *fp;
61 register sstring *s;
62 int c;
64 s->ptr = s->base;
65 if (ISALPHA(c) || c == '_')
67 for (;;)
69 SSTRING_PUT(s, c);
70 c = getc (fp);
71 if (c == EOF || !(ISALNUM(c) || c == '_'))
72 break;
75 MAKE_SSTRING_SPACE(s, 1);
76 *s->ptr = 0;
77 return c;
80 int
81 scan_string (fp, s, init)
82 register FILE *fp;
83 register sstring *s;
84 int init;
86 int c;
87 for (;;)
89 c = getc (fp);
90 if (c == EOF || c == '\n')
91 break;
92 if (c == init)
94 c = getc (fp);
95 break;
97 if (c == '\\')
99 c = getc (fp);
100 if (c == EOF)
101 break;
102 if (c == '\n')
103 continue;
105 SSTRING_PUT(s, c);
107 MAKE_SSTRING_SPACE(s, 1);
108 *s->ptr = 0;
109 return c;
112 /* Skip horizontal white spaces (spaces, tabs, and C-style comments). */
115 skip_spaces (fp, c)
116 register FILE *fp;
117 int c;
119 for (;;)
121 if (c == ' ' || c == '\t')
122 c = getc (fp);
123 else if (c == '/')
125 c = getc (fp);
126 if (c != '*')
128 ungetc (c, fp);
129 return '/';
131 c = getc (fp);
132 for (;;)
134 if (c == EOF)
135 return EOF;
136 else if (c != '*')
138 if (c == '\n')
139 source_lineno++, lineno++;
140 c = getc (fp);
142 else if ((c = getc (fp)) == '/')
143 return getc (fp);
146 else
147 break;
149 return c;
153 read_upto (fp, str, delim)
154 FILE *fp;
155 sstring *str;
156 int delim;
158 int ch;
159 for (;;)
161 ch = getc (fp);
162 if (ch == EOF || ch == delim)
163 break;
164 SSTRING_PUT(str, ch);
166 MAKE_SSTRING_SPACE(str, 1);
167 *str->ptr = 0;
168 return ch;
172 get_token (fp, s)
173 register FILE *fp;
174 register sstring *s;
176 int c;
177 s->ptr = s->base;
178 retry:
179 c = ' ';
180 c = skip_spaces (fp, c);
181 if (c == '\n')
183 source_lineno++;
184 lineno++;
185 goto retry;
187 if (c == '#')
189 c = get_token (fp, s);
190 if (c == INT_TOKEN)
192 source_lineno = atoi (s->base) - 1; /* '\n' will add 1 */
193 get_token (fp, &source_filename);
195 for (;;)
197 c = getc (fp);
198 if (c == EOF)
199 return EOF;
200 if (c == '\n')
202 source_lineno++;
203 lineno++;
204 goto retry;
208 if (c == EOF)
209 return EOF;
210 if (ISDIGIT (c))
214 SSTRING_PUT(s, c);
215 c = getc (fp);
216 } while (c != EOF && ISDIGIT(c));
217 ungetc (c, fp);
218 c = INT_TOKEN;
219 goto done;
221 if (ISALPHA (c) || c == '_')
223 c = scan_ident (fp, s, c);
224 ungetc (c, fp);
225 return IDENTIFIER_TOKEN;
227 if (c == '\'' || c == '"')
229 c = scan_string (fp, s, c);
230 ungetc (c, fp);
231 return c == '\'' ? CHAR_TOKEN : STRING_TOKEN;
233 SSTRING_PUT(s, c);
234 done:
235 MAKE_SSTRING_SPACE(s, 1);
236 *s->ptr = 0;
237 return c;
240 unsigned int
241 hashstr (str, len)
242 const char *str;
243 unsigned int len;
245 unsigned int n = len;
246 unsigned int r = 0;
247 const unsigned char *s = (const unsigned char *)str;
250 r = r * 67 + (*s++ - 113);
251 while (--n);
252 return r + len;