]> andersk Git - splint.git/blame - src/inputStream.c
Removed /bin/csh from tainted/Makefile
[splint.git] / src / inputStream.c
CommitLineData
28bf4b0b 1/*
11db3170 2** Splint - annotation-assisted static program checker
77d37419 3** Copyright (C) 1994-2002 University of Virginia,
28bf4b0b 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.
19**
1b8ae690 20** For information on splint: splint@cs.virginia.edu
21** To report a bug: splint-bug@cs.virginia.edu
11db3170 22** For more information: http://www.splint.org
28bf4b0b 23*/
24/*
25** source.c
26**
27** Interface to source file abstraction
28**
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.
32**
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.
39**
40** AUTHORS:
41**
42** Steve Garland,
43** Massachusetts Institute of Technology
44** Joe Wild, Technical Languages and Environments, DECspec project
45*/
46
1b8ae690 47# include "splintMacros.nf"
28bf4b0b 48# include "llbasic.h"
49# include "osd.h"
50# include "portab.h"
51
52extern bool
53inputStream_close (inputStream s)
54{
55 llassert (inputStream_isDefined (s));
56
57 if (s->file != NULL)
58 {
dfd82dce 59 check (fileTable_closeFile (context_fileTable (), s->file));
28bf4b0b 60 s->file = NULL;
61 return TRUE;
62 }
63
64 return FALSE;
65}
66
67extern void
68inputStream_free (/*@null@*/ /*@only@*/ inputStream s)
69{
70 if (inputStream_isDefined (s))
71 {
72 cstring_free (s->name);
73 cstring_free (s->stringSource);
74 sfree (s);
75 }
76}
77
78extern /*@only@*/ inputStream
79 inputStream_create (cstring name, cstring suffix, bool echo)
80{
81 char *ps;
82 inputStream s = (inputStream) dmalloc (sizeof (*s));
83
84 s->name = name;
85 s->file = NULL;
86
87 /*@access cstring@*/
88 llassert (cstring_isDefined (s->name));
89 /*@i534 clean this up...*/
90 ps = strrchr (s->name, CONNECTCHAR);
91
92 if (ps == NULL)
93 {
94 ps = s->name;
95 }
96 /*@noaccess cstring@*/
97
98 if (strchr (ps, '.') == NULL)
99 {
100 s->name = cstring_concatFree1 (s->name, suffix);
101 }
102
103 s->name = fileLib_cleanName (s->name);
104
105 s->lineNo = 0;
106 s->charNo = 0;
107 s->curLine = NULL;
108 s->echo = echo;
109 s->fromString = FALSE;
110 s->stringSource = NULL;
111 s->stringSourceTail = NULL;
112
113 /*@i3254@*/ return s;
114}
115
116extern /*@only@*/ inputStream
117inputStream_fromString (cstring name, cstring str)
118{
119 inputStream s = (inputStream) dmalloc (sizeof (*s));
120
121 s->name = cstring_copy (name);
122 s->stringSource = cstring_copy (str);
123 s->stringSourceTail = s->stringSource;
124 s->file = 0;
125 s->echo = FALSE;
126 s->fromString = TRUE;
127 s->lineNo = 0;
128 s->charNo = 0;
129 s->curLine = NULL;
3120b462 130 s->buffer[0] = '\0';
28bf4b0b 131
132 return s;
133}
134
135extern int inputStream_nextChar (inputStream s)
136{
137 int res;
138
139 llassert (inputStream_isDefined (s));
140 res = inputStream_peekChar (s);
141
142 if (res != EOF)
143 {
144 if (res == (int) '\n')
145 {
146 s->curLine = NULL;
147 s->charNo = 0;
148 incLine ();
149 }
150 else
151 {
152 s->charNo++;
153 incColumn ();
154 }
155 }
156
157 DPRINTF (("Next char: %c [%d]", (char) res, res));
158 return res;
159}
160
161extern int inputStream_peekNChar (inputStream s, int n)
162 /* Doesn't work across lines! */
163{
164 llassert (s->curLine != NULL);
165 llassert (s->charNo + n < strlen (s->curLine));
166 return ((int) s->curLine [s->charNo + n]);
167}
168
169extern int inputStream_peekChar (inputStream s)
170{
171 if (s->curLine == NULL)
172 {
173 char *cur;
174 s->curLine = NULL;
175 cur = inputStream_nextLine (s);
176 s->curLine = cur; /* split this to avoid possible undefined behavior */
177 s->charNo = 0;
178 }
179
180 if (s->curLine == NULL)
181 {
182 return EOF;
183 }
184
185 llassert (s->charNo <= strlen (s->curLine));
186
187 if (s->curLine[s->charNo] == '\0')
188 {
189 return (int) '\n';
190 }
191
192 return ((int) s->curLine [s->charNo]);
193}
194
195extern /*@dependent@*/ /*@null@*/
196char *inputStream_nextLine (inputStream s)
197{
198 char *currentLine;
199 int len;
200
201 llassert (s->curLine == NULL);
202 s->charNo = 0;
203
204 if (s->fromString)
205 {
206 if (cstring_isEmpty (s->stringSourceTail))
207 {
208 currentLine = 0;
209 }
210 else
211 {
212 /*@access cstring@*/
213 char *c = strchr (s->stringSourceTail, '\n');
214
215 /* in case line is terminated not by newline */
216 if (c == 0)
217 {
218 c = strchr (s->stringSourceTail, '\0');
219 }
220
221 len = c - s->stringSourceTail + 1;
222
223 if (len > STUBMAXRECORDSIZE - 2)
224 {
225 len = (STUBMAXRECORDSIZE - 2);
226 }
227
228 currentLine = &(s->buffer)[0];
229 strncpy (currentLine, s->stringSourceTail, size_fromInt (len));
230 currentLine[len] = '\0';
231 s->stringSourceTail += len;
232 /*@noaccess cstring@*/
233 }
234
235 }
236 else
237 {
238 llassert (s->file != NULL);
239 currentLine = fgets (&(s->buffer)[0], STUBMAXRECORDSIZE, s->file);
240 }
241 if (currentLine == 0)
242 {
243 strcpy (s->buffer, "*** End of File ***");
244 }
245 else
246 {
247 s->lineNo++;
248 len = strlen (currentLine) - 1;
249 if (s->buffer[len] == '\n')
250 {
251 s->buffer[len] = '\0';
252 }
253 else
254 {
255 if (len >= STUBMAXRECORDSIZE - 2)
256 {
257 lldiagmsg (message ("Input line too long: %s",
258 cstring_fromChars (currentLine)));
259 }
260 }
261 }
262
263 /* if (s->echo) slo_echoLine (currentLine); only needed in LCL */
264 return currentLine;
265}
266
267extern bool
268inputStream_open (inputStream s)
269{
270 if (s->fromString)
271 {
272 /* not an error: tail is dependent */
273 s->stringSourceTail = s->stringSource;
274 return TRUE;
275 }
276
277 DPRINTF (("Open: %s", s->name));
dfd82dce 278 s->file = fileTable_openFile (context_fileTable (), s->name, "r");
28bf4b0b 279 return (s->file != 0 || s->fromString);
280}
281
282/*
283** requires
284** path != NULL \and
285** s != NULL \and
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
289**
290** ensures
291** if filefound (*path, *s) then
292** result = true \and *s.name = filespec_where_file_found (*path, *s)
293** else
294** result = false
295*/
296
297extern bool inputStream_getPath (cstring path, inputStream s)
298{
299 cstring returnPath;
300 filestatus status; /* return status of osd_getEnvPath.*/
301 bool rVal; /* return value of this procedure. */
302
303 llassert (cstring_isDefined (path));
304 llassert (inputStream_isDefined (s));
305 llassert (cstring_isDefined (s->name));
306
307 status = osd_getPath (path, s->name, &returnPath);
308
309 if (status == OSD_FILEFOUND)
310 { /* Should be majority of cases. */
311 rVal = TRUE;
312
313 cstring_free (s->name);
314 s->name = fileLib_cleanName (returnPath);
315 }
316 else if (status == OSD_FILENOTFOUND)
317 {
318 rVal = FALSE;
319 }
320 else if (status == OSD_PATHTOOLONG)
321 {
322 rVal = FALSE;
323 /* Directory and filename are too long. Report error. */
324 llbuglit ("soure_getPath: Filename plus directory from search path too long");
325 }
326 else
327 {
328 rVal = FALSE;
329 llbuglit ("inputStream_getPath: invalid return status");
330 }
331
332 return rVal;
333}
334
15b3d2b2 335/*:open:*/ FILE *inputStream_getFile (inputStream s)
28bf4b0b 336{
337 llassert (inputStream_isDefined (s));
338 return s->file;
339}
340
341cstring inputStream_fileName (inputStream s)
342{
343 llassert (inputStream_isDefined (s));
344 return s->name;
345}
346
347bool inputStream_isOpen (inputStream s)
348{
349 return (inputStream_isDefined (s) && (s->file != 0 || s->fromString));
350}
351
352int inputStream_thisLineNumber (inputStream s)
353{
354 llassert (inputStream_isDefined (s));
355 return s->lineNo;
356}
357
358
359
360
This page took 0.098445 seconds and 5 git commands to generate.