Two useless/empty bison reductions removed.
[splint-patched.git] / src / inputStream.c
blobd2d341ece79b0c6fee3bb7fc400c06d8080bb2bd
1 /*
2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 University of Virginia,
4 ** Massachusetts Institute of Technology
5 **
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.
10 **
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.
15 **
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
25 ** source.c
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
31 ** commented out.
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.
40 ** AUTHORS:
42 ** Steve Garland,
43 ** Massachusetts Institute of Technology
44 ** Joe Wild, Technical Languages and Environments, DECspec project
47 # include "splintMacros.nf"
48 # include "basic.h"
49 # include "osd.h"
51 extern bool
52 inputStream_close (inputStream s)
54 llassert (inputStream_isDefined (s));
56 if (s->file != NULL)
58 check (fileTable_closeFile (context_fileTable (), s->file));
59 s->file = NULL;
60 return TRUE;
63 return FALSE;
66 extern void
67 inputStream_free (/*@null@*/ /*@only@*/ inputStream s)
69 if (inputStream_isDefined (s))
71 cstring_free (s->name);
72 cstring_free (s->stringSource);
73 sfree (s);
77 extern /*@only@*/ inputStream
78 inputStream_create (cstring name, cstring suffix, bool echo)
80 char *ps;
81 inputStream s = (inputStream) dmalloc (sizeof (*s));
83 s->name = name;
84 s->file = NULL;
86 /*@access cstring@*/
87 llassert (cstring_isDefined (s->name));
88 ps = strrchr (s->name, CONNECTCHAR);
90 if (ps == NULL)
92 ps = s->name;
94 /*@noaccess cstring@*/
96 if (strchr (ps, '.') == NULL)
98 s->name = cstring_concatFree1 (s->name, suffix);
101 s->name = fileLib_cleanName (s->name);
103 s->lineNo = 0;
104 s->charNo = 0;
105 s->curLine = NULL;
106 s->echo = echo;
107 s->fromString = FALSE;
108 s->stringSource = NULL;
109 s->stringSourceTail = NULL;
110 s->buffer[0] = '\0';
112 return s;
115 extern /*@only@*/ inputStream
116 inputStream_fromString (cstring name, cstring str)
118 inputStream s = (inputStream) dmalloc (sizeof (*s));
120 s->name = cstring_copy (name);
121 s->stringSource = cstring_copy (str);
122 s->stringSourceTail = s->stringSource;
123 s->file = 0;
124 s->echo = FALSE;
125 s->fromString = TRUE;
126 s->lineNo = 0;
127 s->charNo = 0;
128 s->curLine = NULL;
129 s->buffer[0] = '\0';
131 return s;
134 extern int inputStream_nextChar (inputStream s)
136 int res;
138 llassert (inputStream_isDefined (s));
139 res = inputStream_peekChar (s);
141 if (res != EOF)
143 if (res == (int) '\n')
145 s->curLine = NULL;
146 s->charNo = 0;
147 incLine ();
149 else
151 s->charNo++;
152 incColumn ();
156 DPRINTF (("Next char: %c [%d]", (char) res, res));
157 return res;
160 extern int inputStream_peekNChar (inputStream s, int n)
161 /* Doesn't work across lines! */
163 llassert (inputStream_isDefined (s));
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 llassert (inputStream_isDefined (s));
173 if (s->curLine == NULL)
175 char *cur;
176 s->curLine = NULL;
177 cur = inputStream_nextLine (s);
178 s->curLine = cur; /* split this to avoid possible undefined behavior */
179 s->charNo = 0;
182 if (s->curLine == NULL)
184 return EOF;
187 llassert (s->charNo <= strlen (s->curLine));
189 if (s->curLine[s->charNo] == '\0')
191 return (int) '\n';
194 return ((int) s->curLine [s->charNo]);
197 extern /*@dependent@*/ /*@null@*/
198 char *inputStream_nextLine (inputStream s)
200 char *currentLine;
201 size_t len;
203 llassert (inputStream_isDefined (s));
204 llassert (s->curLine == NULL);
205 s->charNo = 0;
207 if (s->fromString)
209 if (cstring_isEmpty (s->stringSourceTail))
211 currentLine = 0;
213 else
215 /*@access cstring@*/
216 char *c = strchr (s->stringSourceTail, '\n');
218 /* in case line is terminated not by newline */
219 if (c == 0)
221 c = strchr (s->stringSourceTail, '\0');
224 len = size_fromInt (c - s->stringSourceTail + 1);
226 if (len > size_fromInt (STUBMAXRECORDSIZE - 2))
228 len = size_fromInt (STUBMAXRECORDSIZE - 2);
231 currentLine = &(s->buffer)[0];
232 strncpy (currentLine, s->stringSourceTail, len);
233 currentLine[len] = '\0';
234 s->stringSourceTail += len;
235 /*@noaccess cstring@*/
239 else
241 llassert (s->file != NULL);
242 currentLine = fgets (&(s->buffer)[0], STUBMAXRECORDSIZE, s->file);
244 if (currentLine == 0)
246 strcpy (s->buffer, "*** End of File ***");
248 else
250 s->lineNo++;
251 len = strlen (currentLine) - 1;
252 if (s->buffer[len] == '\n')
254 s->buffer[len] = '\0';
256 else
258 if (len >= size_fromInt (STUBMAXRECORDSIZE - 2))
260 lldiagmsg (message ("Input line too long: %s",
261 cstring_fromChars (currentLine)));
266 /* if (s->echo) slo_echoLine (currentLine); only needed in LCL */
267 return currentLine;
270 extern bool
271 inputStream_open (inputStream s)
273 llassert (inputStream_isDefined (s));
274 if (s->fromString)
276 /* not an error: tail is dependent */
277 s->stringSourceTail = s->stringSource;
278 return TRUE;
281 DPRINTF (("Opening: %s", s->name));
282 s->file = fileTable_openReadFile (context_fileTable (), s->name);
283 return (s->file != 0 || s->fromString);
287 ** requires
288 ** path != NULL \and
289 ** s != NULL \and
290 ** *s.name == filename (*s.name) || filetype (*s.name)
291 ** *s.name consists of a file name and type only ("<filename>.<type>)
292 ** No path name is included
294 ** ensures
295 ** if filefound (*path, *s) then
296 ** result = true \and *s.name = filespec_where_file_found (*path, *s)
297 ** else
298 ** result = false
301 extern bool inputStream_getPath (cstring path, inputStream s)
303 cstring returnPath;
304 filestatus status; /* return status of osd_getEnvPath.*/
305 bool rVal; /* return value of this procedure. */
307 llassert (cstring_isDefined (path));
308 llassert (inputStream_isDefined (s));
309 llassert (cstring_isDefined (s->name));
311 status = osd_getPath (path, s->name, &returnPath);
313 if (status == OSD_FILEFOUND)
314 { /* Should be majority of cases. */
315 rVal = TRUE;
317 cstring_free (s->name);
318 s->name = fileLib_cleanName (returnPath);
320 else if (status == OSD_FILENOTFOUND)
322 rVal = FALSE;
324 else if (status == OSD_PATHTOOLONG)
326 rVal = FALSE;
327 /* Directory and filename are too long. Report error. */
328 llbuglit ("soure_getPath: Filename plus directory from search path too long");
330 else
332 rVal = FALSE;
333 llbuglit ("inputStream_getPath: invalid return status");
336 return rVal;
339 /*:open:*/ FILE *inputStream_getFile (inputStream s)
341 llassert (inputStream_isDefined (s));
342 llassert (s->file != NULL);
343 return s->file;
346 cstring inputStream_fileName (inputStream s)
348 llassert (inputStream_isDefined (s));
349 return s->name;
352 bool inputStream_isOpen (inputStream s)
354 return (inputStream_isDefined (s) && (s->file != 0 || s->fromString));
357 int inputStream_thisLineNumber (inputStream s)
359 llassert (inputStream_isDefined (s));
360 return s->lineNo;