2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 University of Virginia,
4 ** Massachusetts Institute of Technology
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.
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.
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.
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
27 ** Interface to source file abstraction
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
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.
43 ** Massachusetts Institute of Technology
44 ** Joe Wild, Technical Languages and Environments, DECspec project
47 # include "splintMacros.nf"
52 inputStream_close (inputStream s)
54 llassert (inputStream_isDefined (s));
58 check (fileTable_closeFile (context_fileTable (), s->file));
67 inputStream_free (/*@null@*/ /*@only@*/ inputStream s)
69 if (inputStream_isDefined (s))
71 cstring_free (s->name);
72 cstring_free (s->stringSource);
77 extern /*@only@*/ inputStream
78 inputStream_create (cstring name, cstring suffix, bool echo)
81 inputStream s = (inputStream) dmalloc (sizeof (*s));
87 llassert (cstring_isDefined (s->name));
88 ps = strrchr (s->name, CONNECTCHAR);
94 /*@noaccess cstring@*/
96 if (strchr (ps, '.') == NULL)
98 s->name = cstring_concatFree1 (s->name, suffix);
101 s->name = fileLib_cleanName (s->name);
107 s->fromString = FALSE;
108 s->stringSource = NULL;
109 s->stringSourceTail = NULL;
115 extern /*@only@*/ inputStream
116 inputStream_fromString (cstring name, cstring str)
118 inputStream s = (inputStream) dmalloc (sizeof (*s));
120 s->name = cstring_copy (name);
121 s->stringSource = cstring_copy (str);
122 s->stringSourceTail = s->stringSource;
125 s->fromString = TRUE;
134 extern int inputStream_nextChar (inputStream s)
138 llassert (inputStream_isDefined (s));
139 res = inputStream_peekChar (s);
143 if (res == (int) '\n')
156 DPRINTF (("Next char: %c [%d]", (char) res, res));
160 extern int inputStream_peekNChar (inputStream s, int n)
161 /* Doesn't work across lines! */
163 llassert (inputStream_isDefined (s));
164 llassert (s->curLine != NULL);
165 llassert (s->charNo + n < strlen (s->curLine));
166 return ((int) s->curLine [s->charNo + n]);
169 extern int inputStream_peekChar (inputStream s)
171 llassert (inputStream_isDefined (s));
173 if (s->curLine == NULL)
177 cur = inputStream_nextLine (s);
178 s->curLine = cur; /* split this to avoid possible undefined behavior */
182 if (s->curLine == NULL)
187 llassert (s->charNo <= strlen (s->curLine));
189 if (s->curLine[s->charNo] == '\0')
194 return ((int) s->curLine [s->charNo]);
197 extern /*@dependent@*/ /*@null@*/
198 char *inputStream_nextLine (inputStream s)
203 llassert (inputStream_isDefined (s));
204 llassert (s->curLine == NULL);
209 if (cstring_isEmpty (s->stringSourceTail))
216 char *c = strchr (s->stringSourceTail, '\n');
218 /* in case line is terminated not by newline */
221 c = strchr (s->stringSourceTail, '\0');
224 len = size_fromInt (c - s->stringSourceTail + 1);
226 if (len > size_fromInt (STUBMAXRECORDSIZE - 2))
228 len = size_fromInt (STUBMAXRECORDSIZE - 2);
231 currentLine = &(s->buffer)[0];
232 strncpy (currentLine, s->stringSourceTail, len);
233 currentLine[len] = '\0';
234 s->stringSourceTail += len;
235 /*@noaccess cstring@*/
241 llassert (s->file != NULL);
242 currentLine = fgets (&(s->buffer)[0], STUBMAXRECORDSIZE, s->file);
244 if (currentLine == 0)
246 strcpy (s->buffer, "*** End of File ***");
251 len = strlen (currentLine) - 1;
252 if (s->buffer[len] == '\n')
254 s->buffer[len] = '\0';
258 if (len >= size_fromInt (STUBMAXRECORDSIZE - 2))
260 lldiagmsg (message ("Input line too long: %s",
261 cstring_fromChars (currentLine)));
266 /* if (s->echo) slo_echoLine (currentLine); only needed in LCL */
271 inputStream_open (inputStream s)
273 llassert (inputStream_isDefined (s));
276 /* not an error: tail is dependent */
277 s->stringSourceTail = s->stringSource;
281 DPRINTF (("Opening: %s", s->name));
282 s->file = fileTable_openReadFile (context_fileTable (), s->name);
283 return (s->file != 0 || s->fromString);
290 ** *s.name == filename (*s.name) || filetype (*s.name)
291 ** *s.name consists of a file name and type only ("<filename>.<type>)
292 ** No path name is included
295 ** if filefound (*path, *s) then
296 ** result = true \and *s.name = filespec_where_file_found (*path, *s)
301 extern bool inputStream_getPath (cstring path, inputStream s)
304 filestatus status; /* return status of osd_getEnvPath.*/
305 bool rVal; /* return value of this procedure. */
307 llassert (cstring_isDefined (path));
308 llassert (inputStream_isDefined (s));
309 llassert (cstring_isDefined (s->name));
311 status = osd_getPath (path, s->name, &returnPath);
313 if (status == OSD_FILEFOUND)
314 { /* Should be majority of cases. */
317 cstring_free (s->name);
318 s->name = fileLib_cleanName (returnPath);
320 else if (status == OSD_FILENOTFOUND)
324 else if (status == OSD_PATHTOOLONG)
327 /* Directory and filename are too long. Report error. */
328 llbuglit ("soure_getPath: Filename plus directory from search path too long");
333 llbuglit ("inputStream_getPath: invalid return status");
339 /*:open:*/ FILE *inputStream_getFile (inputStream s)
341 llassert (inputStream_isDefined (s));
342 llassert (s->file != NULL);
346 cstring inputStream_fileName (inputStream s)
348 llassert (inputStream_isDefined (s));
352 bool inputStream_isOpen (inputStream s)
354 return (inputStream_isDefined (s) && (s->file != 0 || s->fromString));
357 int inputStream_thisLineNumber (inputStream s)
359 llassert (inputStream_isDefined (s));