]>
Commit | Line | Data |
---|---|---|
7936052f PB |
1 | /* scan.c - Utility functions for scan-decls and patch-header programs. |
2 | Copyright (C) 1993 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, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
17 | ||
18 | #include "scan.h" | |
19 | #include <ctype.h> | |
20 | ||
21 | int lineno = 1; | |
22 | int source_lineno = 1; | |
23 | sstring source_filename; | |
24 | ||
25 | void | |
26 | make_sstring_space (str, count) | |
27 | sstring *str; | |
28 | int count; | |
29 | { | |
30 | int cur_pos = str->ptr - str->base; | |
31 | int cur_size = str->limit - str->base; | |
32 | int new_size = cur_pos + count + 100; | |
33 | ||
34 | if (new_size <= cur_size) | |
35 | return; | |
36 | ||
37 | if (str->base == NULL) | |
38 | str->base = xmalloc (new_size); | |
39 | else | |
40 | str->base = xrealloc (str->base, new_size); | |
41 | str->ptr = str->base + cur_size; | |
42 | str->limit = str->base + new_size; | |
43 | } | |
44 | ||
45 | void | |
46 | sstring_append (dst, src) | |
47 | sstring *dst; | |
48 | sstring *src; | |
49 | { | |
50 | register char *d, *s; | |
51 | register count = SSTRING_LENGTH(src); | |
52 | MAKE_SSTRING_SPACE(dst, count + 1); | |
53 | d = dst->ptr; | |
54 | s = src->base; | |
55 | while (--count >= 0) *d++ = *s++; | |
56 | dst->ptr = d; | |
57 | *d = 0; | |
58 | } | |
59 | ||
60 | memory_full () | |
61 | { | |
62 | abort(); | |
63 | } | |
64 | ||
65 | char * | |
66 | xmalloc (size) | |
67 | unsigned size; | |
68 | { | |
69 | register char *ptr = (char *) malloc (size); | |
70 | if (ptr != 0) return (ptr); | |
71 | memory_full (); | |
72 | /*NOTREACHED*/ | |
73 | return 0; | |
74 | } | |
75 | ||
76 | ||
77 | char * | |
78 | xrealloc (old, size) | |
79 | char *old; | |
80 | unsigned size; | |
81 | { | |
82 | register char *ptr = (char *) realloc (old, size); | |
83 | if (ptr != 0) return (ptr); | |
84 | memory_full (); | |
85 | /*NOTREACHED*/ | |
86 | return 0; | |
87 | } | |
88 | ||
89 | int | |
90 | scan_ident (fp, s, c) | |
91 | register FILE *fp; | |
92 | register sstring *s; | |
93 | int c; | |
94 | { | |
95 | s->ptr = s->base; | |
96 | if (isalpha(c) || c == '_') | |
97 | { | |
98 | for (;;) | |
99 | { | |
100 | SSTRING_PUT(s, c); | |
101 | c = getc (fp); | |
102 | if (c == EOF || !(isalnum(c) || c == '_')) | |
103 | break; | |
104 | } | |
105 | } | |
106 | MAKE_SSTRING_SPACE(s, 1); | |
107 | *s->ptr = 0; | |
108 | return c; | |
109 | } | |
110 | ||
13ac10d7 RS |
111 | int |
112 | scan_string (fp, s, init) | |
7936052f PB |
113 | register FILE *fp; |
114 | register sstring *s; | |
115 | { | |
116 | int c; | |
117 | for (;;) | |
118 | { | |
119 | c = getc (fp); | |
120 | if (c == EOF || c == '\n') | |
121 | break; | |
122 | if (c == init) | |
123 | { | |
124 | c = getc (fp); | |
125 | break; | |
126 | } | |
127 | if (c == '\\') | |
128 | { | |
129 | c = getc (fp); | |
130 | if (c == EOF) | |
131 | break; | |
132 | if (c == '\n') | |
133 | continue; | |
134 | } | |
135 | SSTRING_PUT(s, c); | |
136 | } | |
137 | MAKE_SSTRING_SPACE(s, 1); | |
138 | *s->ptr = 0; | |
139 | return c; | |
140 | } | |
141 | ||
142 | /* Skip horizontal white spaces (spaces, tabs, and C-style comments). */ | |
143 | ||
13ac10d7 RS |
144 | int |
145 | skip_spaces (fp, c) | |
7936052f PB |
146 | register FILE *fp; |
147 | int c; | |
148 | { | |
149 | for (;;) | |
150 | { | |
151 | if (c == ' ' || c == '\t') | |
152 | c = getc (fp); | |
153 | else if (c == '/') | |
154 | { | |
155 | c = getc (fp); | |
156 | if (c != '*') | |
157 | { | |
158 | ungetc (c, fp); | |
159 | return '/'; | |
160 | } | |
161 | c = getc (fp); | |
162 | for (;;) | |
163 | { | |
164 | if (c == EOF) | |
165 | return EOF; | |
166 | else if (c != '*') | |
167 | { | |
168 | if (c == '\n') | |
169 | source_lineno++, lineno++; | |
170 | c = getc (fp); | |
171 | } | |
172 | else if ((c = getc (fp)) == '/') | |
173 | return getc (fp); | |
174 | } | |
175 | } | |
176 | else | |
177 | break; | |
178 | } | |
179 | return c; | |
180 | } | |
181 | ||
182 | int | |
183 | read_upto (fp, str, delim) | |
184 | FILE *fp; | |
185 | sstring *str; | |
186 | int delim; | |
187 | { | |
188 | int ch; | |
189 | for (;;) | |
190 | { | |
191 | ch = getc (fp); | |
192 | if (ch == EOF || ch == delim) | |
193 | break; | |
194 | SSTRING_PUT(str, ch); | |
195 | } | |
196 | MAKE_SSTRING_SPACE(str, 1); | |
197 | *str->ptr = 0; | |
198 | return ch; | |
199 | } | |
200 | ||
201 | int | |
202 | get_token (fp, s) | |
203 | register FILE *fp; | |
204 | register sstring *s; | |
205 | { | |
206 | int c; | |
207 | s->ptr = s->base; | |
208 | retry: | |
209 | c = ' '; | |
210 | again: | |
211 | c = skip_spaces (fp, c); | |
212 | if (c == '\n') | |
213 | { | |
214 | source_lineno++; | |
215 | lineno++; | |
216 | goto retry; | |
217 | } | |
218 | if (c == '#') | |
219 | { | |
220 | c = get_token (fp, s); | |
221 | if (c == INT_TOKEN) | |
222 | { | |
223 | source_lineno = atoi (s->base); | |
224 | get_token (fp, &source_filename); | |
225 | } | |
226 | for (;;) | |
227 | { | |
228 | c = getc (fp); | |
229 | if (c == EOF) | |
230 | return EOF; | |
231 | if (c == '\n') | |
232 | goto retry; | |
233 | } | |
234 | } | |
235 | if (c == EOF) | |
236 | return EOF; | |
237 | if (isdigit (c)) | |
238 | { | |
239 | do | |
240 | { | |
241 | SSTRING_PUT(s, c); | |
242 | c = getc (fp); | |
243 | } while (c != EOF && isdigit(c)); | |
244 | ungetc (c, fp); | |
245 | c = INT_TOKEN; | |
246 | goto done; | |
247 | } | |
248 | if (isalpha (c) || c == '_') | |
249 | { | |
250 | c = scan_ident (fp, s, c); | |
251 | ungetc (c, fp); | |
252 | return IDENTIFIER_TOKEN; | |
253 | } | |
254 | if (c == '\'' || c == '"') | |
255 | { | |
256 | int quote = c; | |
257 | c = scan_string (fp, s, c); | |
258 | ungetc (c, fp); | |
259 | return c == '\'' ? CHAR_TOKEN : STRING_TOKEN; | |
260 | } | |
261 | SSTRING_PUT(s, c); | |
262 | done: | |
263 | MAKE_SSTRING_SPACE(s, 1); | |
264 | *s->ptr = 0; | |
265 | return c; | |
266 | } | |
267 | ||
268 | unsigned long | |
269 | hash (str) | |
270 | char *str; | |
271 | { | |
272 | int h = 0; | |
273 | /* Replace this with something faster/better! FIXME! */ | |
274 | while (*str) h = (h << 3) + *str++; | |
275 | return h & 0x7FFFFFFF; | |
276 | } |