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