]>
Commit | Line | Data |
---|---|---|
d6924eff | 1 | /* Utility functions for scan-decls and fix-header programs. |
cf403648 | 2 | Copyright (C) 1993, 1994, 1998, 2002 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 | |
e9fa0c7c | 16 | Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
7936052f | 17 | |
7f77b42c | 18 | #include "hconfig.h" |
b04cd507 KG |
19 | #include "system.h" |
20 | #include "scan.h" | |
7936052f PB |
21 | |
22 | int lineno = 1; | |
23 | int source_lineno = 1; | |
24 | sstring source_filename; | |
25 | ||
26 | void | |
27 | make_sstring_space (str, count) | |
28 | sstring *str; | |
29 | int count; | |
30 | { | |
31 | int cur_pos = str->ptr - str->base; | |
32 | int cur_size = str->limit - str->base; | |
33 | int new_size = cur_pos + count + 100; | |
34 | ||
35 | if (new_size <= cur_size) | |
36 | return; | |
786de7eb | 37 | |
d7db6646 | 38 | str->base = xrealloc (str->base, new_size); |
7936052f PB |
39 | str->ptr = str->base + cur_size; |
40 | str->limit = str->base + new_size; | |
41 | } | |
42 | ||
43 | void | |
44 | sstring_append (dst, src) | |
45 | sstring *dst; | |
46 | sstring *src; | |
47 | { | |
b3694847 | 48 | char *d, *s; |
cf403648 | 49 | int count = SSTRING_LENGTH (src); |
b3694847 | 50 | |
cf403648 | 51 | MAKE_SSTRING_SPACE (dst, count + 1); |
7936052f PB |
52 | d = dst->ptr; |
53 | s = src->base; | |
54 | while (--count >= 0) *d++ = *s++; | |
55 | dst->ptr = d; | |
786de7eb | 56 | *d = 0; |
7936052f PB |
57 | } |
58 | ||
7936052f PB |
59 | int |
60 | scan_ident (fp, s, c) | |
b3694847 SS |
61 | FILE *fp; |
62 | sstring *s; | |
7936052f PB |
63 | int c; |
64 | { | |
65 | s->ptr = s->base; | |
cf403648 | 66 | if (ISIDST (c)) |
7936052f PB |
67 | { |
68 | for (;;) | |
69 | { | |
cf403648 | 70 | SSTRING_PUT (s, c); |
7936052f | 71 | c = getc (fp); |
cf403648 | 72 | if (c == EOF || ! ISIDNUM (c)) |
7936052f PB |
73 | break; |
74 | } | |
75 | } | |
cf403648 | 76 | MAKE_SSTRING_SPACE (s, 1); |
7936052f PB |
77 | *s->ptr = 0; |
78 | return c; | |
79 | } | |
80 | ||
13ac10d7 RS |
81 | int |
82 | scan_string (fp, s, init) | |
b3694847 SS |
83 | FILE *fp; |
84 | sstring *s; | |
9870475c | 85 | int init; |
7936052f PB |
86 | { |
87 | int c; | |
b3694847 | 88 | |
7936052f PB |
89 | for (;;) |
90 | { | |
91 | c = getc (fp); | |
92 | if (c == EOF || c == '\n') | |
93 | break; | |
94 | if (c == init) | |
95 | { | |
96 | c = getc (fp); | |
97 | break; | |
98 | } | |
99 | if (c == '\\') | |
100 | { | |
101 | c = getc (fp); | |
102 | if (c == EOF) | |
103 | break; | |
104 | if (c == '\n') | |
105 | continue; | |
106 | } | |
cf403648 | 107 | SSTRING_PUT (s, c); |
7936052f | 108 | } |
cf403648 | 109 | MAKE_SSTRING_SPACE (s, 1); |
7936052f PB |
110 | *s->ptr = 0; |
111 | return c; | |
112 | } | |
113 | ||
0f41302f | 114 | /* Skip horizontal white spaces (spaces, tabs, and C-style comments). */ |
7936052f | 115 | |
13ac10d7 RS |
116 | int |
117 | skip_spaces (fp, c) | |
b3694847 | 118 | FILE *fp; |
7936052f PB |
119 | int c; |
120 | { | |
121 | for (;;) | |
122 | { | |
123 | if (c == ' ' || c == '\t') | |
124 | c = getc (fp); | |
125 | else if (c == '/') | |
126 | { | |
127 | c = getc (fp); | |
128 | if (c != '*') | |
129 | { | |
130 | ungetc (c, fp); | |
131 | return '/'; | |
132 | } | |
133 | c = getc (fp); | |
134 | for (;;) | |
135 | { | |
136 | if (c == EOF) | |
137 | return EOF; | |
138 | else if (c != '*') | |
139 | { | |
140 | if (c == '\n') | |
141 | source_lineno++, lineno++; | |
142 | c = getc (fp); | |
143 | } | |
144 | else if ((c = getc (fp)) == '/') | |
145 | return getc (fp); | |
146 | } | |
147 | } | |
148 | else | |
149 | break; | |
150 | } | |
151 | return c; | |
152 | } | |
153 | ||
154 | int | |
155 | read_upto (fp, str, delim) | |
156 | FILE *fp; | |
157 | sstring *str; | |
158 | int delim; | |
159 | { | |
160 | int ch; | |
b3694847 | 161 | |
7936052f PB |
162 | for (;;) |
163 | { | |
164 | ch = getc (fp); | |
165 | if (ch == EOF || ch == delim) | |
166 | break; | |
cf403648 | 167 | SSTRING_PUT (str, ch); |
7936052f | 168 | } |
cf403648 | 169 | MAKE_SSTRING_SPACE (str, 1); |
7936052f PB |
170 | *str->ptr = 0; |
171 | return ch; | |
172 | } | |
173 | ||
174 | int | |
175 | get_token (fp, s) | |
b3694847 SS |
176 | FILE *fp; |
177 | sstring *s; | |
7936052f PB |
178 | { |
179 | int c; | |
b3694847 | 180 | |
7936052f PB |
181 | s->ptr = s->base; |
182 | retry: | |
183 | c = ' '; | |
7936052f PB |
184 | c = skip_spaces (fp, c); |
185 | if (c == '\n') | |
186 | { | |
187 | source_lineno++; | |
188 | lineno++; | |
189 | goto retry; | |
190 | } | |
191 | if (c == '#') | |
192 | { | |
193 | c = get_token (fp, s); | |
194 | if (c == INT_TOKEN) | |
195 | { | |
a6e8021e | 196 | source_lineno = atoi (s->base) - 1; /* '\n' will add 1 */ |
7936052f PB |
197 | get_token (fp, &source_filename); |
198 | } | |
199 | for (;;) | |
200 | { | |
201 | c = getc (fp); | |
202 | if (c == EOF) | |
203 | return EOF; | |
204 | if (c == '\n') | |
a6e8021e PB |
205 | { |
206 | source_lineno++; | |
207 | lineno++; | |
7936052f | 208 | goto retry; |
a6e8021e | 209 | } |
7936052f PB |
210 | } |
211 | } | |
212 | if (c == EOF) | |
213 | return EOF; | |
e9a780ec | 214 | if (ISDIGIT (c)) |
7936052f PB |
215 | { |
216 | do | |
217 | { | |
cf403648 | 218 | SSTRING_PUT (s, c); |
7936052f | 219 | c = getc (fp); |
cf403648 | 220 | } while (c != EOF && ISDIGIT (c)); |
7936052f PB |
221 | ungetc (c, fp); |
222 | c = INT_TOKEN; | |
223 | goto done; | |
224 | } | |
0df6c2c7 | 225 | if (ISIDST (c)) |
7936052f PB |
226 | { |
227 | c = scan_ident (fp, s, c); | |
228 | ungetc (c, fp); | |
229 | return IDENTIFIER_TOKEN; | |
230 | } | |
231 | if (c == '\'' || c == '"') | |
232 | { | |
7936052f PB |
233 | c = scan_string (fp, s, c); |
234 | ungetc (c, fp); | |
235 | return c == '\'' ? CHAR_TOKEN : STRING_TOKEN; | |
236 | } | |
cf403648 | 237 | SSTRING_PUT (s, c); |
7936052f | 238 | done: |
cf403648 | 239 | MAKE_SSTRING_SPACE (s, 1); |
7936052f PB |
240 | *s->ptr = 0; |
241 | return c; | |
242 | } | |
5fa7f88c ZW |
243 | |
244 | unsigned int | |
245 | hashstr (str, len) | |
246 | const char *str; | |
247 | unsigned int len; | |
248 | { | |
249 | unsigned int n = len; | |
250 | unsigned int r = 0; | |
cf403648 | 251 | const unsigned char *s = (const unsigned char *) str; |
5fa7f88c ZW |
252 | |
253 | do | |
254 | r = r * 67 + (*s++ - 113); | |
255 | while (--n); | |
256 | return r + len; | |
257 | } |