2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 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 splint: splint@cs.virginia.edu
21 ** To report a bug: splint-bug@cs.virginia.edu
22 ** For more information: http://www.splint.org
27 ** replaces filenamemap.c
28 ** based (loosely) on typeTable.c
30 ** entries in the fileTable are:
32 ** name - name of the file
33 ** type - kind of file (a temp file to be deleted?)
34 ** link - derived from this file
39 * - Added conditional stuff (macros OS2 and MSDOS) to make names of temporary
40 * files under Windows or OS/2 not larger than 8+3 characters to avoid
41 * trouble with FAT file systems or Novell Netware volumes.
42 * - Added include of new header file portab.h containing OS dependent stuff.
43 * - Changed occurance of '/' as path delimiter to a macro.
44 * - Added conditional stuff (#define and #include) for IBM's compiler.
47 # include "splintMacros.nf"
52 # if defined(__IBMC__) && defined(OS2)
54 # define getpid _getpid
59 static bool fileTable_inRange (fileTable ft, fileId fid) /*@*/
61 return (fileTable_isDefined (ft) && (fid >= 0) && (fid < ft->nentries));
64 static fileId fileTable_internAddEntry (fileTable p_ft, /*@only@*/ ftentry p_e)
66 static /*@only@*/ cstring makeTempName (cstring p_dir, cstring p_pre, cstring p_suf);
68 static /*@only@*/ cstring
69 fileType_unparse (fileType ft)
73 case FILE_NORMAL: return cstring_makeLiteral ("normal");
74 case FILE_NODELETE: return cstring_makeLiteral ("normal");
75 case FILE_LSLTEMP: return cstring_makeLiteral ("ltemp");
76 case FILE_HEADER: return cstring_makeLiteral ("header");
77 case FILE_XH: return cstring_makeLiteral ("xh");
78 case FILE_MACROS: return cstring_makeLiteral ("macros");
79 case FILE_METASTATE: return cstring_makeLiteral ("metastate");
86 fileTable_getIndex (fileTable ft, cstring s)
90 if (ft == NULL) return NOT_FOUND;
91 abspath = osd_absolutePath (cstring_undefined, s);
92 DPRINTF (("Absolute path: %s: %s", s, abspath));
93 res = cstringTable_lookup (ft->htable, abspath);
94 cstring_free (abspath);
99 fileTable_unparse (fileTable ft)
101 cstring s = cstring_undefined;
104 if (fileTable_isUndefined (ft))
106 return (cstring_makeLiteral ("<fileTable undefined>"));
109 for (i = 0; i < ft->nentries; i++)
111 if (fileId_isValid (ft->elements[i]->fder))
113 s = message ("%s\n[%d] %s %q %d (%s)",
115 ft->elements[i]->fname,
116 fileType_unparse (ft->elements[i]->ftype),
117 ft->elements[i]->fder,
118 ft->elements[ft->elements[i]->fder]->fname);
122 s = message ("%s\n[%d] %s %q", s, i, ft->elements[i]->fname,
123 fileType_unparse (ft->elements[i]->ftype));
130 void fileTable_printTemps (fileTable ft)
132 if (fileTable_isDefined (ft))
136 for (i = 0; i < ft->nentries; i++)
138 if (ft->elements[i]->ftemp)
140 if (fileId_isValid (ft->elements[i]->fder))
142 fprintf (stderr, " %s:1\n\t%s:1\n",
143 cstring_toCharsSafe (ft->elements[ft->elements[i]->fder]->fname),
144 cstring_toCharsSafe (ft->elements[i]->fname));
148 fprintf (stderr, "[no file]\n\t%s:1\n",
149 cstring_toCharsSafe (ft->elements[i]->fname));
157 ** loads in fileTable from fileTable_dump
160 static /*@notnull@*/ ftentry
161 ftentry_create (/*@keep@*/ cstring tn, bool temp, fileType typ, fileId der)
163 ftentry t = (ftentry) dmalloc (sizeof (*t));
165 if (cstring_isUndefined (tn))
167 llbug (cstring_makeLiteral ("Undefined filename!"));
172 t->basename = cstring_undefined;
177 /* Don't set these until the basename is needed. */
185 ftentry_free (/*@only@*/ ftentry t)
187 cstring_free (t->fname);
188 cstring_free (t->basename);
192 /*@only@*/ /*@notnull@*/ fileTable
195 fileTable ft = (fileTable) dmalloc (sizeof (*ft));
198 ft->nspace = FTBASESIZE;
199 ft->elements = (ftentry *) dmalloc (FTBASESIZE * sizeof (*ft->elements));
200 ft->htable = cstringTable_create (FTHASHSIZE);
203 ft->nopenspace = FTBASESIZE;
204 ft->openelements = (foentry *) dmalloc (FTBASESIZE * sizeof (*ft->openelements));
210 fileTable_grow (fileTable ft)
215 llassert (fileTable_isDefined (ft));
217 ft->nspace = FTBASESIZE;
219 newent = (ftentry *) dmalloc ((ft->nentries + ft->nspace) * sizeof (*newent));
221 for (i = 0; i < ft->nentries; i++)
223 newent[i] = ft->elements[i];
226 sfree (ft->elements);
227 ft->elements = newent;
231 fileTable_growOpen (fileTable ft)
236 llassert (fileTable_isDefined (ft));
238 ft->nopenspace = FTBASESIZE;
240 newent = (foentry *) dmalloc ((ft->nopen + ft->nopenspace) * sizeof (*newent));
242 for (i = 0; i < ft->nopen; i++)
244 newent[i] = ft->openelements[i];
247 sfree (ft->openelements);
248 ft->openelements = newent;
252 fileTable_internAddEntry (fileTable ft, /*@only@*/ ftentry e)
254 llassert (fileTable_isDefined (ft));
261 DPRINTF (("Adding: %s", e->fname));
262 cstringTable_insert (ft->htable, e->fname, ft->nentries);
263 ft->elements[ft->nentries] = e;
266 return (ft->nentries - 1);
269 void fileTable_noDelete (fileTable ft, cstring name)
271 fileId fid = fileTable_lookup (ft, name);
273 if (fileId_isValid (fid)) {
274 llassert (fileTable_isDefined (ft));
276 ft->elements[fid]->ftype = FILE_NODELETE;
281 fileTable_addFilePrim (fileTable ft, /*@temp@*/ cstring name,
282 bool temp, fileType typ, fileId der)
285 cstring absname = osd_absolutePath (NULL, name);
286 int tindex = fileTable_getIndex (ft, absname);
288 llassert (ft != fileTable_undefined);
290 if (tindex != NOT_FOUND)
292 llcontbug (message ("fileTable_addFilePrim: duplicate entry: %q", absname));
297 ftentry e = ftentry_create (absname, temp, typ, der);
299 if (der == fileId_invalid)
301 llassert (cstring_isUndefined (e->basename));
303 e->basename = fileLib_removePathFree (fileLib_removeAnyExtension (absname));
304 e->fsystem = context_isSystemDir (absname);
305 e->fspecial = context_isSpecialFile (absname);
309 cstring srcname = cstring_concatFree1 (fileLib_removeAnyExtension (absname),
311 fileId fid = fileTable_lookup (ft, srcname);
312 cstring_free (srcname);
314 if (fileId_isValid (fid))
316 fileId derid = ft->elements[fid]->fder;
318 ft->elements[fid]->fspecial = TRUE;
320 if (fileId_isValid (derid))
322 ft->elements[derid]->fspecial = TRUE;
329 ftentry de = ft->elements[der];
331 llassert (cstring_isUndefined (e->basename));
332 e->basename = cstring_copy (de->basename);
333 e->fsystem = de->fsystem;
334 e->fspecial = de->fspecial;
337 return (fileTable_internAddEntry (ft, e));
342 fileTable_addFile (fileTable ft, cstring name)
344 return (fileTable_addFilePrim (ft, name, FALSE, FILE_NORMAL, fileId_invalid));
348 fileTable_addFileOnly (fileTable ft, /*@only@*/ cstring name)
350 fileId res = fileTable_addFilePrim (ft, name, FALSE, FILE_NORMAL, fileId_invalid);
356 fileTable_addHeaderFile (fileTable ft, cstring name)
359 res = fileTable_addFilePrim (ft, name, FALSE, FILE_HEADER, fileId_invalid);
365 fileTable_isHeader (fileTable ft, fileId fid)
367 if (fileId_isInvalid (fid))
372 llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid));
373 return (ft->elements[fid]->ftype == FILE_HEADER);
377 fileTable_isSystemFile (fileTable ft, fileId fid)
379 if (fileId_isInvalid (fid))
384 llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid));
385 return (ft->elements[fid]->fsystem);
389 fileTable_isXHFile (fileTable ft, fileId fid)
391 if (fileId_isInvalid (fid))
396 if (!(fileTable_isDefined (ft) && fileTable_inRange (ft, fid)))
398 llcontbug (message ("Bad file table or id: %s %d", bool_unparse (fileTable_isDefined (ft)), fid));
403 return (ft->elements[fid]->ftype == FILE_XH);
408 fileTable_isSpecialFile (fileTable ft, fileId fid)
410 if (fileId_isInvalid (fid))
415 llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid));
416 return (ft->elements[fid]->fspecial);
420 fileTable_addLibraryFile (fileTable ft, cstring name)
422 return (fileTable_addFilePrim (ft, name, FALSE, FILE_HEADER, fileId_invalid));
426 fileTable_addXHFile (fileTable ft, cstring name)
428 return (fileTable_addFilePrim (ft, name, FALSE, FILE_XH, fileId_invalid));
433 fileTable_addImportFile (fileTable ft, cstring name)
435 return (fileTable_addFilePrim (ft, name, FALSE, FILE_HEADER, fileId_invalid));
439 fileTable_addLCLFile (fileTable ft, cstring name)
441 return (fileTable_addFilePrim (ft, name, FALSE, FILE_HEADER, fileId_invalid));
446 static int tmpcounter = 0;
450 fileTable_addMacrosFile (fileTable ft)
453 makeTempName (context_tmpdir (), cstring_makeLiteralTemp ("lmx"),
454 cstring_makeLiteralTemp (".llm"));
455 fileId res = fileTable_addFilePrim (ft, newname, TRUE, FILE_MACROS, fileId_invalid);
456 cstring_free (newname);
461 fileTable_addMetastateFile (fileTable ft, cstring name)
463 return (fileTable_addFilePrim (ft, name, FALSE, FILE_METASTATE, fileId_invalid));
467 fileTable_addCTempFile (fileTable ft, fileId fid)
470 makeTempName (context_tmpdir (), cstring_makeLiteralTemp ("cl"),
474 llassert (fileTable_isDefined (ft));
476 if (!fileId_isValid (ft->elements[fid]->fder))
478 if (fileTable_isXHFile (ft, fid))
480 res = fileTable_addFilePrim (ft, newname, TRUE, FILE_XH, fid);
484 res = fileTable_addFilePrim (ft, newname, TRUE, FILE_NORMAL, fid);
489 if (fileTable_isXHFile (ft, fid))
491 res = fileTable_addFilePrim (ft, newname, TRUE, FILE_XH,
492 ft->elements[fid]->fder);
496 res = fileTable_addFilePrim (ft, newname, TRUE, FILE_NORMAL,
497 ft->elements[fid]->fder);
501 cstring_free (newname);
507 fileTable_addltemp (fileTable ft)
509 cstring newname = makeTempName (context_tmpdir (),
510 cstring_makeLiteralTemp ("ls"),
511 cstring_makeLiteralTemp (".lsl"));
514 if (cstring_hasNonAlphaNumBar (newname))
516 char *lastpath = (char *)NULL;
522 ("Operating system generates tmp filename containing invalid charater: %s",
524 lldiagmsg (cstring_makeLiteral
525 ("Try cleaning up the tmp directory. Attempting to continue."));
529 llassert (cstring_isDefined (newname));
530 lastpath = strrchr (newname, CONNECTCHAR); /* get the directory */
531 llassert (lastpath != NULL);
534 newname = message ("%q%hlsl%d.lsl",
538 /*@noaccess cstring@*/
543 ** this is kind of yucky...need to make the result of cstring_fromChars
544 ** refer to the same storage as its argument. Of course, this loses,
545 ** since cstring is abstract. Should make it an only?
548 ret = fileTable_addFilePrim (ft, newname, TRUE, FILE_LSLTEMP, fileId_invalid);
549 cstring_free (newname);
555 fileTable_exists (fileTable ft, cstring s)
557 int tindex = fileTable_getIndex (ft, s);
559 if (tindex == NOT_FOUND)
561 DPRINTF (("Not found: %s", s));
571 fileTable_lookup (fileTable ft, cstring s)
573 int tindex = fileTable_getIndex (ft, s);
575 if (tindex == NOT_FOUND)
577 return fileId_invalid;
586 ** This is pretty awkward --- when we find the real path of
587 ** a .xh file, we may need to change the recorded name. [Sigh]
591 fileTable_setFilePath (fileTable ft, fileId fid, cstring path)
593 llassert (fileId_isValid (fid));
594 llassert (fileTable_isDefined (ft));
595 /* Need to put new string in hash table */
596 cstringTable_insert (ft->htable, cstring_copy (path), fid);
597 ft->elements[fid]->fname = cstring_copy (path);
601 fileTable_lookupBase (fileTable ft, cstring base)
603 int tindex = fileTable_getIndex (ft, base);
605 if (tindex == NOT_FOUND)
607 return fileId_invalid;
613 llassert (fileTable_isDefined (ft));
615 der = ft->elements[tindex]->fder;
617 if (!fileId_isValid (der))
627 fileTable_getName (fileTable ft, fileId fid)
629 if (!fileId_isValid (fid))
632 (message ("fileTable_getName: called with invalid type id: %d", fid));
633 return cstring_makeLiteralTemp ("<invalid>");
636 llassert (fileTable_isDefined (ft));
637 return (ft->elements[fid]->fname);
641 fileTable_getRootName (fileTable ft, fileId fid)
645 if (!fileId_isValid (fid))
647 llcontbug (message ("fileTable_getName: called with invalid id: %d", fid));
648 return cstring_makeLiteralTemp ("<invalid>");
651 if (!fileTable_isDefined (ft))
653 return cstring_makeLiteralTemp ("<no file table>");
656 fder = ft->elements[fid]->fder;
658 if (fileId_isValid (fder))
660 return (ft->elements[fder]->fname);
664 return (ft->elements[fid]->fname);
669 fileTable_getNameBase (fileTable ft, fileId fid)
671 if (!fileId_isValid (fid))
673 llcontbug (message ("fileTable_getName: called with invalid id: %d", fid));
674 return cstring_makeLiteralTemp ("<invalid>");
677 if (!fileTable_isDefined (ft))
679 return cstring_makeLiteralTemp ("<no file table>");
682 return (ft->elements[fid]->basename);
686 fileTable_sameBase (fileTable ft, fileId f1, fileId f2)
690 if (!fileId_isValid (f1))
695 if (!fileId_isValid (f2))
700 llassert (fileTable_isDefined (ft));
707 fd1 = ft->elements[f1]->fder;
709 if (!fileId_isValid (fd1))
714 fd2 = ft->elements[f2]->fder;
717 if (!fileId_isValid (fd2))
726 fileTable_cleanup (fileTable ft)
732 llassert (fileTable_isDefined (ft));
734 msg = ((ft->nentries > 40) && context_getFlag (FLG_SHOWSCAN));
735 skip = ft->nentries / 10;
739 (void) fflush (g_msgstream);
740 fprintf (stderr, "< cleaning");
743 for (i = 0; i < ft->nentries; i++)
745 ftentry fe = ft->elements[i];
749 /* let's be real careful now, hon! */
752 ** Make sure it is really a derived file
755 if (fe->ftype == FILE_LSLTEMP || fe->ftype == FILE_NODELETE)
757 ; /* already removed */
759 else if (fileId_isValid (fe->fder))
761 /*@i423 this should use close (fd) also... */
762 (void) osd_unlink (fe->fname);
764 else if (fe->ftype == FILE_MACROS)
766 (void) osd_unlink (fe->fname);
770 llbug (message ("Temporary file is not derivative: %s "
771 "(not deleted)", fe->fname));
779 if (msg && ((i % skip) == 0))
781 (void) fflush (g_msgstream);
784 fprintf (stderr, " ");
786 fprintf (stderr, ".");
789 (void) fflush (stderr);
795 fprintf (stderr, " >\n");
800 fileTable_free (/*@only@*/ fileTable f)
804 if (f == (fileTable)NULL)
809 while ( i < f->nentries )
811 ftentry_free (f->elements[i]);
815 cstringTable_free (f->htable);
821 ** unique temp filename are constructed from <dir><pre><pid><msg>.<suf>
822 ** requires: <dir> must end in '/'
825 static void nextMsg (char *msg)
855 llassertprint (FALSE, ("nextMsg: out of unique names!!!"));
863 static /*@only@*/ cstring makeTempName (cstring dir, cstring pre, cstring suf)
866 static /*@owned@*/ char *msg = NULL;
867 static /*@only@*/ cstring pidname = NULL;
871 llassert (cstring_length (pre) <= 3);
874 ** We limit the temp name to 8 characters:
882 msg = mstring_copy ("AAA"); /* there are 26^3 temp names */
887 /*@+matchanyintegral@*/
889 /*@=matchanyintegral@*/
892 if (cstring_isUndefined (pidname))
894 pidname = message ("%d", pid % 100);
897 maxlen = (cstring_length (dir) + cstring_length (pre) + mstring_length (msg)
898 + cstring_length (pidname) + cstring_length (suf) + 2);
900 smsg = message ("%s%s%s%s%s", dir, pre, pidname, cstring_fromChars (msg), suf);
903 while (osd_fileExists (smsg))
906 smsg = message ("%s%s%s%s%s", dir, pre, pidname, cstring_fromChars (msg), suf);
915 foentry_create (/*@exposed@*/ FILE *f, /*@only@*/ cstring fname)
917 foentry t = (foentry) dmalloc (sizeof (*t));
924 foentry_free (/*@only@*/ foentry foe)
926 cstring_free (foe->fname);
931 fileTable_addOpen (fileTable ft, /*@observer@*/ FILE *f, /*@only@*/ cstring fname)
933 llassert (fileTable_isDefined (ft));
935 if (ft->nopenspace <= 0)
937 fileTable_growOpen (ft);
941 ft->openelements[ft->nopen] = foentry_create (f, fname);
945 FILE *fileTable_openFile (fileTable ft, cstring fname, char *mode)
947 FILE *res = fopen (cstring_toCharsSafe (fname), mode);
950 fileTable_addOpen (ft, res, cstring_copy (fname));
951 DPRINTF (("Opening file: %s / %p", fname, res));
957 bool fileTable_closeFile (fileTable ft, FILE *f)
959 bool foundit = FALSE;
962 llassert (fileTable_isDefined (ft));
964 DPRINTF (("Closing file: %p", f));
966 for (i = 0; i < ft->nopen; i++)
968 if (ft->openelements[i]->f == f)
970 DPRINTF (("Closing file: %p = %s", f, ft->openelements[i]->fname));
972 if (i == ft->nopen - 1)
974 foentry_free (ft->openelements[i]);
975 ft->openelements[i] = NULL;
979 foentry_free (ft->openelements[i]);
980 ft->openelements[i] = ft->openelements[ft->nopen - 1];
981 ft->openelements[ft->nopen - 1] = NULL;
992 return (fclose (f) == 0);
995 void fileTable_closeAll (fileTable ft)
999 for (i = 0; i < ft->nopen; i++)
1002 lldiagmsg (message ("Unclosed file at exit: %s", ft->openelements[i]->fname));
1005 (void) fclose (ft->openelements[i]->f); /* No check - cleaning up after errors */
1006 ft->openelements[i]->f = NULL;
1007 foentry_free (ft->openelements[i]);
1008 ft->openelements[i] = NULL;
1011 ft->nopenspace += ft->nopen;