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));
89 llassert (cstring_isDefined (s->name));
90 /*@i534 clean this up...*/
91 ps = strrchr (s->name, CONNECTCHAR);
97 /*@noaccess cstring@*/
99 if (strchr (ps, '.') == NULL)
101 s->name = cstring_concatFree1 (s->name, suffix);
105 s->name = fileLib_cleanName (s->name);
106 /*@i523@*/ cstring_free (oname); /* evans 2002-07-12: why no error without this?! */
112 s->fromString = FALSE;
113 s->stringSource = NULL;
114 s->stringSourceTail = NULL;
120 extern /*@only@*/ inputStream
121 inputStream_fromString (cstring name, cstring str)
123 inputStream s = (inputStream) dmalloc (sizeof (*s));
125 s->name = cstring_copy (name);
126 s->stringSource = cstring_copy (str);
127 s->stringSourceTail = s->stringSource;
130 s->fromString = TRUE;
139 extern int inputStream_nextChar (inputStream s)
143 llassert (inputStream_isDefined (s));
144 res = inputStream_peekChar (s);
148 if (res == (int) '\n')
161 DPRINTF (("Next char: %c [%d]", (char) res, res));
165 extern int inputStream_peekNChar (inputStream s, int n)
166 /* Doesn't work across lines! */
168 llassert (inputStream_isDefined (s));
169 llassert (s->curLine != NULL);
170 llassert (s->charNo + n < strlen (s->curLine));
171 return ((int) s->curLine [s->charNo + n]);
174 extern int inputStream_peekChar (inputStream s)
176 llassert (inputStream_isDefined (s));
178 if (s->curLine == NULL)
182 cur = inputStream_nextLine (s);
183 s->curLine = cur; /* split this to avoid possible undefined behavior */
187 if (s->curLine == NULL)
192 llassert (s->charNo <= strlen (s->curLine));
194 if (s->curLine[s->charNo] == '\0')
199 return ((int) s->curLine [s->charNo]);
202 extern /*@dependent@*/ /*@null@*/
203 char *inputStream_nextLine (inputStream s)
208 llassert (inputStream_isDefined (s));
209 llassert (s->curLine == NULL);
214 if (cstring_isEmpty (s->stringSourceTail))
221 char *c = strchr (s->stringSourceTail, '\n');
223 /* in case line is terminated not by newline */
226 c = strchr (s->stringSourceTail, '\0');
229 len = size_fromInt (c - s->stringSourceTail + 1);
231 if (len > size_fromInt (STUBMAXRECORDSIZE - 2))
233 len = size_fromInt (STUBMAXRECORDSIZE - 2);
236 currentLine = &(s->buffer)[0];
237 strncpy (currentLine, s->stringSourceTail, len);
238 currentLine[len] = '\0';
239 s->stringSourceTail += len;
240 /*@noaccess cstring@*/
246 llassert (s->file != NULL);
247 currentLine = fgets (&(s->buffer)[0], STUBMAXRECORDSIZE, s->file);
249 if (currentLine == 0)
251 strcpy (s->buffer, "*** End of File ***");
256 len = strlen (currentLine) - 1;
257 if (s->buffer[len] == '\n')
259 s->buffer[len] = '\0';
263 if (len >= size_fromInt (STUBMAXRECORDSIZE - 2))
265 lldiagmsg (message ("Input line too long: %s",
266 cstring_fromChars (currentLine)));
271 /* if (s->echo) slo_echoLine (currentLine); only needed in LCL */
276 inputStream_open (inputStream s)
278 llassert (inputStream_isDefined (s));
281 /* not an error: tail is dependent */
282 s->stringSourceTail = s->stringSource;
286 DPRINTF (("Opening: %s", s->name));
287 s->file = fileTable_openReadFile (context_fileTable (), s->name);
288 return (s->file != 0 || s->fromString);
295 ** *s.name == filename (*s.name) || filetype (*s.name)
296 ** *s.name consists of a file name and type only ("<filename>.<type>)
297 ** No path name is included
300 ** if filefound (*path, *s) then
301 ** result = true \and *s.name = filespec_where_file_found (*path, *s)
306 extern bool inputStream_getPath (cstring path, inputStream s)
309 filestatus status; /* return status of osd_getEnvPath.*/
310 bool rVal; /* return value of this procedure. */
312 llassert (cstring_isDefined (path));
313 llassert (inputStream_isDefined (s));
314 llassert (cstring_isDefined (s->name));
316 status = osd_getPath (path, s->name, &returnPath);
318 if (status == OSD_FILEFOUND)
319 { /* Should be majority of cases. */
322 cstring_free (s->name);
323 s->name = fileLib_cleanName (returnPath);
325 else if (status == OSD_FILENOTFOUND)
329 else if (status == OSD_PATHTOOLONG)
332 /* Directory and filename are too long. Report error. */
333 llbuglit ("soure_getPath: Filename plus directory from search path too long");
338 llbuglit ("inputStream_getPath: invalid return status");
344 /*:open:*/ FILE *inputStream_getFile (inputStream s)
346 llassert (inputStream_isDefined (s));
347 llassert (s->file != NULL);
351 cstring inputStream_fileName (inputStream s)
353 llassert (inputStream_isDefined (s));
357 bool inputStream_isOpen (inputStream s)
359 return (inputStream_isDefined (s) && (s->file != 0 || s->fromString));
362 int inputStream_thisLineNumber (inputStream s)
364 llassert (inputStream_isDefined (s));