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"
53 inputStream_close (inputStream s)
55 llassert (inputStream_isDefined (s));
59 check (fileTable_closeFile (context_fileTable (), s->file));
68 inputStream_free (/*@null@*/ /*@only@*/ inputStream s)
70 if (inputStream_isDefined (s))
72 cstring_free (s->name);
73 cstring_free (s->stringSource);
78 extern /*@only@*/ inputStream
79 inputStream_create (cstring name, cstring suffix, bool echo)
82 inputStream s = (inputStream) dmalloc (sizeof (*s));
88 llassert (cstring_isDefined (s->name));
89 ps = strrchr (s->name, CONNECTCHAR);
95 /*@noaccess cstring@*/
97 if (strchr (ps, '.') == NULL)
99 s->name = cstring_concatFree1 (s->name, suffix);
102 s->name = fileLib_cleanName (s->name);
108 s->fromString = FALSE;
109 s->stringSource = NULL;
110 s->stringSourceTail = NULL;
116 extern /*@only@*/ inputStream
117 inputStream_fromString (cstring name, cstring str)
119 inputStream s = (inputStream) dmalloc (sizeof (*s));
121 s->name = cstring_copy (name);
122 s->stringSource = cstring_copy (str);
123 s->stringSourceTail = s->stringSource;
126 s->fromString = TRUE;
135 extern int inputStream_nextChar (inputStream s)
139 llassert (inputStream_isDefined (s));
140 res = inputStream_peekChar (s);
144 if (res == (int) '\n')
157 DPRINTF (("Next char: %c [%d]", (char) res, res));
161 extern int inputStream_peekNChar (inputStream s, int n)
162 /* Doesn't work across lines! */
164 llassert (inputStream_isDefined (s));
165 llassert (s->curLine != NULL);
166 llassert (s->charNo + n < strlen (s->curLine));
167 return ((int) s->curLine [s->charNo + n]);
170 extern int inputStream_peekChar (inputStream s)
172 llassert (inputStream_isDefined (s));
174 if (s->curLine == NULL)
178 cur = inputStream_nextLine (s);
179 s->curLine = cur; /* split this to avoid possible undefined behavior */
183 if (s->curLine == NULL)
188 llassert (s->charNo <= strlen (s->curLine));
190 if (s->curLine[s->charNo] == '\0')
195 return ((int) s->curLine [s->charNo]);
198 extern /*@dependent@*/ /*@null@*/
199 char *inputStream_nextLine (inputStream s)
204 llassert (inputStream_isDefined (s));
205 llassert (s->curLine == NULL);
210 if (cstring_isEmpty (s->stringSourceTail))
217 char *c = strchr (s->stringSourceTail, '\n');
219 /* in case line is terminated not by newline */
222 c = strchr (s->stringSourceTail, '\0');
225 len = size_fromInt (c - s->stringSourceTail + 1);
227 if (len > size_fromInt (STUBMAXRECORDSIZE - 2))
229 len = size_fromInt (STUBMAXRECORDSIZE - 2);
232 currentLine = &(s->buffer)[0];
233 strncpy (currentLine, s->stringSourceTail, len);
234 currentLine[len] = '\0';
235 s->stringSourceTail += len;
236 /*@noaccess cstring@*/
242 llassert (s->file != NULL);
243 currentLine = fgets (&(s->buffer)[0], STUBMAXRECORDSIZE, s->file);
245 if (currentLine == 0)
247 strcpy (s->buffer, "*** End of File ***");
252 len = strlen (currentLine) - 1;
253 if (s->buffer[len] == '\n')
255 s->buffer[len] = '\0';
259 if (len >= size_fromInt (STUBMAXRECORDSIZE - 2))
261 lldiagmsg (message ("Input line too long: %s",
262 cstring_fromChars (currentLine)));
267 /* if (s->echo) slo_echoLine (currentLine); only needed in LCL */
272 inputStream_open (inputStream s)
274 llassert (inputStream_isDefined (s));
277 /* not an error: tail is dependent */
278 s->stringSourceTail = s->stringSource;
282 DPRINTF (("Opening: %s", s->name));
283 s->file = fileTable_openReadFile (context_fileTable (), s->name);
284 return (s->file != 0 || s->fromString);
291 ** *s.name == filename (*s.name) || filetype (*s.name)
292 ** *s.name consists of a file name and type only ("<filename>.<type>)
293 ** No path name is included
296 ** if filefound (*path, *s) then
297 ** result = true \and *s.name = filespec_where_file_found (*path, *s)
302 extern bool inputStream_getPath (cstring path, inputStream s)
305 filestatus status; /* return status of osd_getEnvPath.*/
306 bool rVal; /* return value of this procedure. */
308 llassert (cstring_isDefined (path));
309 llassert (inputStream_isDefined (s));
310 llassert (cstring_isDefined (s->name));
312 status = osd_getPath (path, s->name, &returnPath);
314 if (status == OSD_FILEFOUND)
315 { /* Should be majority of cases. */
318 cstring_free (s->name);
319 s->name = fileLib_cleanName (returnPath);
321 else if (status == OSD_FILENOTFOUND)
325 else if (status == OSD_PATHTOOLONG)
328 /* Directory and filename are too long. Report error. */
329 llbuglit ("soure_getPath: Filename plus directory from search path too long");
334 llbuglit ("inputStream_getPath: invalid return status");
340 /*:open:*/ FILE *inputStream_getFile (inputStream s)
342 llassert (inputStream_isDefined (s));
343 llassert (s->file != NULL);
347 cstring inputStream_fileName (inputStream s)
349 llassert (inputStream_isDefined (s));
353 bool inputStream_isOpen (inputStream s)
355 return (inputStream_isDefined (s) && (s->file != 0 || s->fromString));
358 int inputStream_thisLineNumber (inputStream s)
360 llassert (inputStream_isDefined (s));