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 tsource_close (tsource *s)
57 check (fclose (s->file) == 0);
66 tsource_free (/*@null@*/ /*@only@*/ tsource *s)
71 sfree (s->stringSource);
76 extern /*@only@*/ tsource *
77 tsource_create (char *name, char *suffix, bool echo)
80 tsource *s = (tsource *) dmalloc (sizeof (*s));
82 s->name = (char *) dmalloc (strlen (name) + strlen (suffix) + 1);
84 strcpy (s->name, name);
86 ps = strrchr (s->name, CONNECTCHAR);
93 if (strchr (ps, '.') == NULL)
95 strcat (s->name, suffix);
102 s->fromString = FALSE;
103 s->stringSource = NULL;
104 s->stringSourceTail = NULL;
110 extern /*@only@*/ tsource *
111 tsource_fromString (char *name, char *str)
113 tsource *s = (tsource *) dmalloc (sizeof (*s));
115 s->name = mstring_copy (name);
116 s->stringSource = mstring_copy (str);
117 s->stringSourceTail = s->stringSource;
120 s->fromString = TRUE;
126 extern /*@dependent@*/ /*@null@*/
127 char *tsource_nextLine (tsource *s)
134 if (s->stringSourceTail == NULL || (strlen (s->stringSourceTail) == 0))
140 char *c = strchr (s->stringSourceTail, '\n');
143 /* in case line is terminated not by newline */
146 c = strchr (s->stringSourceTail, '\0');
149 len = c - s->stringSourceTail + 1;
151 if (len > STUBMAXRECORDSIZE - 2)
153 len = (STUBMAXRECORDSIZE - 2);
156 currentLine = &(s->buffer)[0];
157 strncpy (currentLine, s->stringSourceTail, size_fromInt (len));
158 currentLine[len] = '\0';
159 s->stringSourceTail += len;
165 llassert (s->file != NULL);
166 currentLine = fgets (&(s->buffer)[0], STUBMAXRECORDSIZE, s->file);
168 if (currentLine == 0)
170 strcpy (s->buffer, "*** End of File ***");
175 len = strlen (currentLine) - 1;
176 if (s->buffer[len] == '\n')
178 s->buffer[len] = '\0';
182 if (len >= STUBMAXRECORDSIZE - 2)
184 lldiagmsg (message ("Input line too long: %s",
185 cstring_fromChars (currentLine)));
189 /* if (s->echo) slo_echoLine (currentLine); only needed in LCL */
194 tsource_open (tsource *s)
198 /* not an error: tail is dependent */
199 s->stringSourceTail = s->stringSource;
203 DPRINTF (("Open: %s", s->name));
204 s->file = fopen (s->name, "r");
205 return (s->file != 0 || s->fromString);
212 ** *s.name == filename (*s.name) || filetype (*s.name)
213 ** *s.name consists of a file name and type only ("<filename>.<type>)
214 ** No path name is included
217 ** if filefound (*path, *s) then
218 ** result = true \and *s.name = filespec_where_file_found (*path, *s)
223 extern bool tsource_getPath (char *path, tsource *s)
226 filestatus status; /* return status of osd_getEnvPath.*/
227 bool rVal; /* return value of this procedure. */
229 /* Check if requires met. */
230 if (path == NULL || s == NULL || s->name == NULL)
232 llbugexitlit ("tsource_getPath: invalid parameter");
235 status = osd_getPath (path, s->name, &returnPath);
237 if (status == OSD_FILEFOUND)
238 { /* Should be majority of cases. */
242 s->name = returnPath;
244 else if (status == OSD_FILENOTFOUND)
248 else if (status == OSD_PATHTOOLONG)
251 /* Directory and filename are too long. Report error. */
252 llbuglit ("soure_getPath: Filename plus directory from search path too long");
257 llbuglit ("tsource_getPath: invalid return status");
263 char *specFullName (char *specfile, /*@out@*/ char **inpath)
265 /* extract the path and the specname associated with the given file */
266 char *specname = (char *) dmalloc (sizeof (*specname)
267 * (strlen (specfile) + 9));
268 char *ospecname = specname;
269 char *path = (char *) dmalloc (sizeof (*path) * (strlen (specfile)));
273 /* initialized path to empty string or may have accidental garbage */
276 /*@-mayaliasunique@*/
277 strcpy (specname, specfile);
278 /*@=mayaliasunique@*/
280 /* trim off pathnames in specfile */
281 size = strlen (specname);
283 for (i = size_toInt (size) - 1; i >= 0; i--)
285 if (specname[i] == CONNECTCHAR)
287 /* strcpy (specname, (char *)specname+i+1); */
288 for (j = 0; j <= i; j++) /* include '/' */
290 path[j] = specname[j];
300 ** also remove .lcl file extension, assume it's the last extension
304 size = strlen (specname);
306 for (i = size_toInt (size) - 1; i >= 0; i--)
308 if (specname[i] == '.')
318 ** If specname no longer points to the original char,
319 ** we need to allocate a new pointer and copy the string.
322 if (specname != ospecname) {
323 char *rspecname = (char *) dmalloc (sizeof (*rspecname) * (strlen (specname) + 1));
324 strcpy (rspecname, specname); /* evs 2000-05-16: Bug: was ospecname! */