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