]>
Commit | Line | Data |
---|---|---|
28bf4b0b | 1 | /* |
2 | ** LCLint - annotation-assisted static program checker | |
3 | ** Copyright (C) 1994-2001 University of Virginia, | |
4 | ** Massachusetts Institute of Technology | |
5 | ** | |
6 | ** This program is free software; you can redistribute it and/or modify it | |
7 | ** under the terms of the GNU General Public License as published by the | |
8 | ** Free Software Foundation; either version 2 of the License, or (at your | |
9 | ** option) any later version. | |
10 | ** | |
11 | ** This program is distributed in the hope that it will be useful, but | |
12 | ** WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | ** General Public License for more details. | |
15 | ** | |
16 | ** The GNU General Public License is available from http://www.gnu.org/ or | |
17 | ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, | |
18 | ** MA 02111-1307, USA. | |
19 | ** | |
20 | ** For information on lclint: lclint-request@cs.virginia.edu | |
21 | ** To report a bug: lclint-bug@cs.virginia.edu | |
22 | ** For more information: http://lclint.cs.virginia.edu | |
23 | */ | |
24 | /* | |
25 | ** source.c | |
26 | ** | |
27 | ** Interface to source file abstraction | |
28 | ** | |
29 | ** NOTE: This module is almost identical to the one for LCL. The | |
30 | ** only difference is that a couple of source lines have been | |
31 | ** commented out. | |
32 | ** | |
33 | ** This module has too many dependencies to be in the common | |
34 | ** source area. Any of the solutions that would allow this | |
35 | ** module to be common had its own set of compromises. It | |
36 | ** seemed best and most straightforward to just keep separte | |
37 | ** copies for LSL and LCL. We should examine this again if we | |
38 | ** ever reorganize the module structure. | |
39 | ** | |
40 | ** AUTHORS: | |
41 | ** | |
42 | ** Steve Garland, | |
43 | ** Massachusetts Institute of Technology | |
44 | ** Joe Wild, Technical Languages and Environments, DECspec project | |
45 | */ | |
46 | ||
47 | # include "lclintMacros.nf" | |
48 | # include "llbasic.h" | |
49 | # include "osd.h" | |
50 | # include "portab.h" | |
51 | ||
52 | extern bool | |
53 | inputStream_close (inputStream s) | |
54 | { | |
55 | llassert (inputStream_isDefined (s)); | |
56 | ||
57 | if (s->file != NULL) | |
58 | { | |
59 | check (fclose (s->file) == 0); | |
60 | s->file = NULL; | |
61 | return TRUE; | |
62 | } | |
63 | ||
64 | return FALSE; | |
65 | } | |
66 | ||
67 | extern void | |
68 | inputStream_free (/*@null@*/ /*@only@*/ inputStream s) | |
69 | { | |
70 | if (inputStream_isDefined (s)) | |
71 | { | |
72 | cstring_free (s->name); | |
73 | cstring_free (s->stringSource); | |
74 | sfree (s); | |
75 | } | |
76 | } | |
77 | ||
78 | extern /*@only@*/ inputStream | |
79 | inputStream_create (cstring name, cstring suffix, bool echo) | |
80 | { | |
81 | char *ps; | |
82 | inputStream s = (inputStream) dmalloc (sizeof (*s)); | |
83 | ||
84 | s->name = name; | |
85 | s->file = NULL; | |
86 | ||
87 | /*@access cstring@*/ | |
88 | llassert (cstring_isDefined (s->name)); | |
89 | /*@i534 clean this up...*/ | |
90 | ps = strrchr (s->name, CONNECTCHAR); | |
91 | ||
92 | if (ps == NULL) | |
93 | { | |
94 | ps = s->name; | |
95 | } | |
96 | /*@noaccess cstring@*/ | |
97 | ||
98 | if (strchr (ps, '.') == NULL) | |
99 | { | |
100 | s->name = cstring_concatFree1 (s->name, suffix); | |
101 | } | |
102 | ||
103 | s->name = fileLib_cleanName (s->name); | |
104 | ||
105 | s->lineNo = 0; | |
106 | s->charNo = 0; | |
107 | s->curLine = NULL; | |
108 | s->echo = echo; | |
109 | s->fromString = FALSE; | |
110 | s->stringSource = NULL; | |
111 | s->stringSourceTail = NULL; | |
112 | ||
113 | /*@i3254@*/ return s; | |
114 | } | |
115 | ||
116 | extern /*@only@*/ inputStream | |
117 | inputStream_fromString (cstring name, cstring str) | |
118 | { | |
119 | inputStream s = (inputStream) dmalloc (sizeof (*s)); | |
120 | ||
121 | s->name = cstring_copy (name); | |
122 | s->stringSource = cstring_copy (str); | |
123 | s->stringSourceTail = s->stringSource; | |
124 | s->file = 0; | |
125 | s->echo = FALSE; | |
126 | s->fromString = TRUE; | |
127 | s->lineNo = 0; | |
128 | s->charNo = 0; | |
129 | s->curLine = NULL; | |
130 | ||
131 | return s; | |
132 | } | |
133 | ||
134 | extern int inputStream_nextChar (inputStream s) | |
135 | { | |
136 | int res; | |
137 | ||
138 | llassert (inputStream_isDefined (s)); | |
139 | res = inputStream_peekChar (s); | |
140 | ||
141 | if (res != EOF) | |
142 | { | |
143 | if (res == (int) '\n') | |
144 | { | |
145 | s->curLine = NULL; | |
146 | s->charNo = 0; | |
147 | incLine (); | |
148 | } | |
149 | else | |
150 | { | |
151 | s->charNo++; | |
152 | incColumn (); | |
153 | } | |
154 | } | |
155 | ||
156 | DPRINTF (("Next char: %c [%d]", (char) res, res)); | |
157 | return res; | |
158 | } | |
159 | ||
160 | extern int inputStream_peekNChar (inputStream s, int n) | |
161 | /* Doesn't work across lines! */ | |
162 | { | |
163 | llassert (s->curLine != NULL); | |
164 | llassert (s->charNo + n < strlen (s->curLine)); | |
165 | return ((int) s->curLine [s->charNo + n]); | |
166 | } | |
167 | ||
168 | extern int inputStream_peekChar (inputStream s) | |
169 | { | |
170 | if (s->curLine == NULL) | |
171 | { | |
172 | char *cur; | |
173 | s->curLine = NULL; | |
174 | cur = inputStream_nextLine (s); | |
175 | s->curLine = cur; /* split this to avoid possible undefined behavior */ | |
176 | s->charNo = 0; | |
177 | } | |
178 | ||
179 | if (s->curLine == NULL) | |
180 | { | |
181 | return EOF; | |
182 | } | |
183 | ||
184 | llassert (s->charNo <= strlen (s->curLine)); | |
185 | ||
186 | if (s->curLine[s->charNo] == '\0') | |
187 | { | |
188 | return (int) '\n'; | |
189 | } | |
190 | ||
191 | return ((int) s->curLine [s->charNo]); | |
192 | } | |
193 | ||
194 | extern /*@dependent@*/ /*@null@*/ | |
195 | char *inputStream_nextLine (inputStream s) | |
196 | { | |
197 | char *currentLine; | |
198 | int len; | |
199 | ||
200 | llassert (s->curLine == NULL); | |
201 | s->charNo = 0; | |
202 | ||
203 | if (s->fromString) | |
204 | { | |
205 | if (cstring_isEmpty (s->stringSourceTail)) | |
206 | { | |
207 | currentLine = 0; | |
208 | } | |
209 | else | |
210 | { | |
211 | /*@access cstring@*/ | |
212 | char *c = strchr (s->stringSourceTail, '\n'); | |
213 | ||
214 | /* in case line is terminated not by newline */ | |
215 | if (c == 0) | |
216 | { | |
217 | c = strchr (s->stringSourceTail, '\0'); | |
218 | } | |
219 | ||
220 | len = c - s->stringSourceTail + 1; | |
221 | ||
222 | if (len > STUBMAXRECORDSIZE - 2) | |
223 | { | |
224 | len = (STUBMAXRECORDSIZE - 2); | |
225 | } | |
226 | ||
227 | currentLine = &(s->buffer)[0]; | |
228 | strncpy (currentLine, s->stringSourceTail, size_fromInt (len)); | |
229 | currentLine[len] = '\0'; | |
230 | s->stringSourceTail += len; | |
231 | /*@noaccess cstring@*/ | |
232 | } | |
233 | ||
234 | } | |
235 | else | |
236 | { | |
237 | llassert (s->file != NULL); | |
238 | currentLine = fgets (&(s->buffer)[0], STUBMAXRECORDSIZE, s->file); | |
239 | } | |
240 | if (currentLine == 0) | |
241 | { | |
242 | strcpy (s->buffer, "*** End of File ***"); | |
243 | } | |
244 | else | |
245 | { | |
246 | s->lineNo++; | |
247 | len = strlen (currentLine) - 1; | |
248 | if (s->buffer[len] == '\n') | |
249 | { | |
250 | s->buffer[len] = '\0'; | |
251 | } | |
252 | else | |
253 | { | |
254 | if (len >= STUBMAXRECORDSIZE - 2) | |
255 | { | |
256 | lldiagmsg (message ("Input line too long: %s", | |
257 | cstring_fromChars (currentLine))); | |
258 | } | |
259 | } | |
260 | } | |
261 | ||
262 | /* if (s->echo) slo_echoLine (currentLine); only needed in LCL */ | |
263 | return currentLine; | |
264 | } | |
265 | ||
266 | extern bool | |
267 | inputStream_open (inputStream s) | |
268 | { | |
269 | if (s->fromString) | |
270 | { | |
271 | /* not an error: tail is dependent */ | |
272 | s->stringSourceTail = s->stringSource; | |
273 | return TRUE; | |
274 | } | |
275 | ||
276 | DPRINTF (("Open: %s", s->name)); | |
277 | s->file = fopen (cstring_toCharsSafe (s->name), "r"); | |
278 | return (s->file != 0 || s->fromString); | |
279 | } | |
280 | ||
281 | /* | |
282 | ** requires | |
283 | ** path != NULL \and | |
284 | ** s != NULL \and | |
285 | ** *s.name == filename (*s.name) || filetype (*s.name) | |
286 | ** *s.name consists of a file name and type only ("<filename>.<type>) | |
287 | ** No path name is included | |
288 | ** | |
289 | ** ensures | |
290 | ** if filefound (*path, *s) then | |
291 | ** result = true \and *s.name = filespec_where_file_found (*path, *s) | |
292 | ** else | |
293 | ** result = false | |
294 | */ | |
295 | ||
296 | extern bool inputStream_getPath (cstring path, inputStream s) | |
297 | { | |
298 | cstring returnPath; | |
299 | filestatus status; /* return status of osd_getEnvPath.*/ | |
300 | bool rVal; /* return value of this procedure. */ | |
301 | ||
302 | llassert (cstring_isDefined (path)); | |
303 | llassert (inputStream_isDefined (s)); | |
304 | llassert (cstring_isDefined (s->name)); | |
305 | ||
306 | status = osd_getPath (path, s->name, &returnPath); | |
307 | ||
308 | if (status == OSD_FILEFOUND) | |
309 | { /* Should be majority of cases. */ | |
310 | rVal = TRUE; | |
311 | ||
312 | cstring_free (s->name); | |
313 | s->name = fileLib_cleanName (returnPath); | |
314 | } | |
315 | else if (status == OSD_FILENOTFOUND) | |
316 | { | |
317 | rVal = FALSE; | |
318 | } | |
319 | else if (status == OSD_PATHTOOLONG) | |
320 | { | |
321 | rVal = FALSE; | |
322 | /* Directory and filename are too long. Report error. */ | |
323 | llbuglit ("soure_getPath: Filename plus directory from search path too long"); | |
324 | } | |
325 | else | |
326 | { | |
327 | rVal = FALSE; | |
328 | llbuglit ("inputStream_getPath: invalid return status"); | |
329 | } | |
330 | ||
331 | return rVal; | |
332 | } | |
333 | ||
334 | /*@open@*/ FILE *inputStream_getFile (inputStream s) | |
335 | { | |
336 | llassert (inputStream_isDefined (s)); | |
337 | return s->file; | |
338 | } | |
339 | ||
340 | cstring inputStream_fileName (inputStream s) | |
341 | { | |
342 | llassert (inputStream_isDefined (s)); | |
343 | return s->name; | |
344 | } | |
345 | ||
346 | bool inputStream_isOpen (inputStream s) | |
347 | { | |
348 | return (inputStream_isDefined (s) && (s->file != 0 || s->fromString)); | |
349 | } | |
350 | ||
351 | int inputStream_thisLineNumber (inputStream s) | |
352 | { | |
353 | llassert (inputStream_isDefined (s)); | |
354 | return s->lineNo; | |
355 | } | |
356 | ||
357 | ||
358 | ||
359 |