2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2001 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 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
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 "lclintMacros.nf"
53 inputStream_close (inputStream s)
55 llassert (inputStream_isDefined (s));
59 check (fclose (s->file) == 0);
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 /*@i534 clean this up...*/
90 ps = strrchr (s->name, CONNECTCHAR);
96 /*@noaccess cstring@*/
98 if (strchr (ps, '.') == NULL)
100 s->name = cstring_concatFree1 (s->name, suffix);
103 s->name = fileLib_cleanName (s->name);
109 s->fromString = FALSE;
110 s->stringSource = NULL;
111 s->stringSourceTail = NULL;
113 /*@i3254@*/ return s;
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 (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 if (s->curLine == NULL)
175 cur = inputStream_nextLine (s);
176 s->curLine = cur; /* split this to avoid possible undefined behavior */
180 if (s->curLine == NULL)
185 llassert (s->charNo <= strlen (s->curLine));
187 if (s->curLine[s->charNo] == '\0')
192 return ((int) s->curLine [s->charNo]);
195 extern /*@dependent@*/ /*@null@*/
196 char *inputStream_nextLine (inputStream s)
201 llassert (s->curLine == NULL);
206 if (cstring_isEmpty (s->stringSourceTail))
213 char *c = strchr (s->stringSourceTail, '\n');
215 /* in case line is terminated not by newline */
218 c = strchr (s->stringSourceTail, '\0');
221 len = c - s->stringSourceTail + 1;
223 if (len > STUBMAXRECORDSIZE - 2)
225 len = (STUBMAXRECORDSIZE - 2);
228 currentLine = &(s->buffer)[0];
229 strncpy (currentLine, s->stringSourceTail, size_fromInt (len));
230 currentLine[len] = '\0';
231 s->stringSourceTail += len;
232 /*@noaccess cstring@*/
238 llassert (s->file != NULL);
239 currentLine = fgets (&(s->buffer)[0], STUBMAXRECORDSIZE, s->file);
241 if (currentLine == 0)
243 strcpy (s->buffer, "*** End of File ***");
248 len = strlen (currentLine) - 1;
249 if (s->buffer[len] == '\n')
251 s->buffer[len] = '\0';
255 if (len >= STUBMAXRECORDSIZE - 2)
257 lldiagmsg (message ("Input line too long: %s",
258 cstring_fromChars (currentLine)));
263 /* if (s->echo) slo_echoLine (currentLine); only needed in LCL */
268 inputStream_open (inputStream s)
272 /* not an error: tail is dependent */
273 s->stringSourceTail = s->stringSource;
277 DPRINTF (("Open: %s", s->name));
278 s->file = fopen (cstring_toCharsSafe (s->name), "r");
279 return (s->file != 0 || s->fromString);
286 ** *s.name == filename (*s.name) || filetype (*s.name)
287 ** *s.name consists of a file name and type only ("<filename>.<type>)
288 ** No path name is included
291 ** if filefound (*path, *s) then
292 ** result = true \and *s.name = filespec_where_file_found (*path, *s)
297 extern bool inputStream_getPath (cstring path, inputStream s)
300 filestatus status; /* return status of osd_getEnvPath.*/
301 bool rVal; /* return value of this procedure. */
303 llassert (cstring_isDefined (path));
304 llassert (inputStream_isDefined (s));
305 llassert (cstring_isDefined (s->name));
307 status = osd_getPath (path, s->name, &returnPath);
309 if (status == OSD_FILEFOUND)
310 { /* Should be majority of cases. */
313 cstring_free (s->name);
314 s->name = fileLib_cleanName (returnPath);
316 else if (status == OSD_FILENOTFOUND)
320 else if (status == OSD_PATHTOOLONG)
323 /* Directory and filename are too long. Report error. */
324 llbuglit ("soure_getPath: Filename plus directory from search path too long");
329 llbuglit ("inputStream_getPath: invalid return status");
335 /*@open@*/ FILE *inputStream_getFile (inputStream s)
337 llassert (inputStream_isDefined (s));
341 cstring inputStream_fileName (inputStream s)
343 llassert (inputStream_isDefined (s));
347 bool inputStream_isOpen (inputStream s)
349 return (inputStream_isDefined (s) && (s->file != 0 || s->fromString));
352 int inputStream_thisLineNumber (inputStream s)
354 llassert (inputStream_isDefined (s));