2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2000 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)
57 check (fclose (s->file) == 0);
66 inputStream_free (/*@null@*/ /*@only@*/ inputStream s)
71 sfree (s->stringSource);
77 extern /*@only@*/ inputStream
78 inputStream_create (char *name, char *suffix, bool echo)
81 inputStream s = (inputStream) dmalloc (sizeof (*s));
83 s->name = (char *) dmalloc (strlen (name) + strlen (suffix) + 1);
85 strcpy (s->name, name);
87 ps = strrchr (s->name, CONNECTCHAR);
94 if (strchr (ps, '.') == NULL)
96 strcat (s->name, suffix);
103 s->fromString = FALSE;
104 s->stringSource = NULL;
105 s->stringSourceTail = NULL;
110 extern /*@only@*/ inputStream
111 inputStream_fromString (char *name, char *str)
113 inputStream s = (inputStream) dmalloc (sizeof (*s));
115 s->name = mstring_copy (name);
116 s->stringSource = mstring_copy (str);
117 s->stringSourceTail = s->stringSource;
120 s->fromString = TRUE;
128 extern int inputStream_nextChar (inputStream s)
130 int res = inputStream_peekChar (s);
145 DPRINTF (("Next char: %c [%d]", res, res));
149 extern int inputStream_peekNChar (inputStream s, int n)
150 /* Doesn't work across lines! */
152 llassert (s->curLine != NULL);
153 llassert (s->charNo + n < strlen (s->curLine));
154 return ((int) s->curLine [s->charNo + n]);
157 extern int inputStream_peekChar (inputStream s)
159 if (s->curLine == NULL)
162 s->curLine = inputStream_nextLine (s);
166 if (s->curLine == NULL)
171 llassert (s->charNo <= strlen (s->curLine));
173 if (s->curLine[s->charNo] == '\0')
178 return ((int) s->curLine [s->charNo]);
181 extern /*@dependent@*/ /*@null@*/
182 char *inputStream_nextLine (inputStream s)
187 llassert (s->curLine == NULL);
192 if (s->stringSourceTail == NULL || (strlen (s->stringSourceTail) == 0))
198 char *c = strchr (s->stringSourceTail, '\n');
201 /* in case line is terminated not by newline */
204 c = strchr (s->stringSourceTail, '\0');
207 len = c - s->stringSourceTail + 1;
209 if (len > STUBMAXRECORDSIZE - 2)
211 len = (STUBMAXRECORDSIZE - 2);
214 currentLine = &(s->buffer)[0];
215 strncpy (currentLine, s->stringSourceTail, size_fromInt (len));
216 currentLine[len] = '\0';
217 s->stringSourceTail += len;
223 llassert (s->file != NULL);
224 currentLine = fgets (&(s->buffer)[0], STUBMAXRECORDSIZE, s->file);
226 if (currentLine == 0)
228 strcpy (s->buffer, "*** End of File ***");
233 len = strlen (currentLine) - 1;
234 if (s->buffer[len] == '\n')
236 s->buffer[len] = '\0';
240 if (len >= STUBMAXRECORDSIZE - 2)
242 lldiagmsg (message ("Input line too long: %s",
243 cstring_fromChars (currentLine)));
248 /* if (s->echo) slo_echoLine (currentLine); only needed in LCL */
253 inputStream_open (inputStream s)
257 /* not an error: tail is dependent */
258 s->stringSourceTail = s->stringSource;
262 DPRINTF (("Open: %s", s->name));
263 s->file = fopen (s->name, "r");
264 return (s->file != 0 || s->fromString);
271 ** *s.name == filename (*s.name) || filetype (*s.name)
272 ** *s.name consists of a file name and type only ("<filename>.<type>)
273 ** No path name is included
276 ** if filefound (*path, *s) then
277 ** result = true \and *s.name = filespec_where_file_found (*path, *s)
282 extern bool inputStream_getPath (char *path, inputStream s)
285 filestatus status; /* return status of osd_getEnvPath.*/
286 bool rVal; /* return value of this procedure. */
288 /* Check if requires met. */
289 if (path == NULL || s == NULL || s->name == NULL)
291 llbugexitlit ("inputStream_getPath: invalid parameter");
294 status = osd_getPath (path, s->name, &returnPath);
296 if (status == OSD_FILEFOUND)
297 { /* Should be majority of cases. */
301 s->name = returnPath;
303 else if (status == OSD_FILENOTFOUND)
307 else if (status == OSD_PATHTOOLONG)
310 /* Directory and filename are too long. Report error. */
311 llbuglit ("soure_getPath: Filename plus directory from search path too long");
316 llbuglit ("inputStream_getPath: invalid return status");
322 char *specFullName (char *specfile, /*@out@*/ char **inpath)
324 /* extract the path and the specname associated with the given file */
325 char *specname = (char *) dmalloc (sizeof (*specname)
326 * (strlen (specfile) + 9));
327 char *ospecname = specname;
328 char *path = (char *) dmalloc (sizeof (*path) * (strlen (specfile)));
332 /* initialized path to empty string or may have accidental garbage */
335 /*@-mayaliasunique@*/
336 strcpy (specname, specfile);
337 /*@=mayaliasunique@*/
339 /* trim off pathnames in specfile */
340 size = strlen (specname);
342 for (i = size_toInt (size) - 1; i >= 0; i--)
344 if (specname[i] == CONNECTCHAR)
346 /* strcpy (specname, (char *)specname+i+1); */
347 for (j = 0; j <= i; j++) /* include '/' */
349 path[j] = specname[j];
359 ** also remove .lcl file extension, assume it's the last extension
363 size = strlen (specname);
365 for (i = size_toInt (size) - 1; i >= 0; i--)
367 if (specname[i] == '.')
377 ** If specname no longer points to the original char,
378 ** we need to allocate a new pointer and copy the string.
381 if (specname != ospecname) {
382 char *rspecname = (char *) dmalloc (sizeof (*rspecname) * (strlen (specname) + 1));
383 strcpy (rspecname, specname); /* evs 2000-05-16: Bug: was ospecname! */