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);
98 static cstring ftentry_unparse (fileTable ft, ftentry fte)
100 if (fileId_isValid (fte->fder))
102 return message ("%s %q %d (%s)",
104 fileType_unparse (fte->ftype),
106 ft->elements[fte->fder]->fname);
110 return message ("%s %q", fte->fname,
111 fileType_unparse (fte->ftype));
116 fileTable_unparse (fileTable ft)
118 cstring s = cstring_undefined;
121 if (fileTable_isUndefined (ft))
123 return (cstring_makeLiteral ("<fileTable undefined>"));
126 for (i = 0; i < ft->nentries; i++)
128 s = message ("%s\n[%d] %q", s, i, ftentry_unparse (ft, ft->elements[i]));
134 void fileTable_printTemps (fileTable ft)
136 if (fileTable_isDefined (ft))
140 for (i = 0; i < ft->nentries; i++)
142 if (ft->elements[i]->ftemp)
144 if (fileId_isValid (ft->elements[i]->fder))
146 fprintf (stderr, " %s:1\n\t%s:1\n",
147 cstring_toCharsSafe (ft->elements[ft->elements[i]->fder]->fname),
148 cstring_toCharsSafe (ft->elements[i]->fname));
152 fprintf (stderr, "[no file]\n\t%s:1\n",
153 cstring_toCharsSafe (ft->elements[i]->fname));
161 ** loads in fileTable from fileTable_dump
164 static /*@notnull@*/ ftentry
165 ftentry_create (/*@keep@*/ cstring tn, bool temp, fileType typ, fileId der)
167 ftentry t = (ftentry) dmalloc (sizeof (*t));
169 if (cstring_isUndefined (tn))
171 llbug (cstring_makeLiteral ("Undefined filename!"));
176 t->basename = cstring_undefined;
181 /* Don't set these until the basename is needed. */
189 ftentry_free (/*@only@*/ ftentry t)
191 cstring_free (t->fname);
192 cstring_free (t->basename);
196 /*@only@*/ /*@notnull@*/ fileTable
199 fileTable ft = (fileTable) dmalloc (sizeof (*ft));
202 ft->nspace = FTBASESIZE;
203 ft->elements = (ftentry *) dmalloc (FTBASESIZE * sizeof (*ft->elements));
204 ft->htable = cstringTable_create (FTHASHSIZE);
207 ft->nopenspace = FTBASESIZE;
208 ft->openelements = (foentry *) dmalloc (FTBASESIZE * sizeof (*ft->openelements));
214 fileTable_grow (fileTable ft)
219 llassert (fileTable_isDefined (ft));
221 ft->nspace = FTBASESIZE;
223 newent = (ftentry *) dmalloc ((ft->nentries + ft->nspace) * sizeof (*newent));
225 for (i = 0; i < ft->nentries; i++)
227 newent[i] = ft->elements[i];
230 sfree (ft->elements);
231 ft->elements = newent;
235 fileTable_growOpen (fileTable ft)
240 llassert (fileTable_isDefined (ft));
242 ft->nopenspace = FTBASESIZE;
244 newent = (foentry *) dmalloc ((ft->nopen + ft->nopenspace) * sizeof (*newent));
246 for (i = 0; i < ft->nopen; i++)
248 newent[i] = ft->openelements[i];
251 sfree (ft->openelements);
252 ft->openelements = newent;
256 fileTable_internAddEntry (fileTable ft, /*@only@*/ ftentry e)
258 llassert (fileTable_isDefined (ft));
265 DPRINTF (("Adding: %s", e->fname));
266 cstringTable_insert (ft->htable, e->fname, ft->nentries);
267 ft->elements[ft->nentries] = e;
270 return (ft->nentries - 1);
273 void fileTable_noDelete (fileTable ft, cstring name)
275 fileId fid = fileTable_lookup (ft, name);
277 if (fileId_isValid (fid))
279 llassert (fileTable_isDefined (ft));
280 ft->elements[fid]->ftype = FILE_NODELETE;
284 DPRINTF (("Invalid no delete: %s", name));
289 fileTable_addFilePrim (fileTable ft, /*@temp@*/ cstring name,
290 bool temp, fileType typ, fileId der)
293 cstring absname = osd_absolutePath (NULL, name);
294 int tindex = fileTable_getIndex (ft, absname);
296 DPRINTF (("Got abs path: %s", absname));
297 llassert (ft != fileTable_undefined);
299 if (tindex != NOT_FOUND)
301 llcontbug (message ("fileTable_addFilePrim: duplicate entry: %q", absname));
306 ftentry e = ftentry_create (absname, temp, typ, der);
308 if (der == fileId_invalid)
310 llassert (cstring_isUndefined (e->basename));
312 e->basename = fileLib_removePathFree (fileLib_removeAnyExtension (absname));
313 e->fsystem = context_isSystemDir (absname);
314 e->fspecial = context_isSpecialFile (absname);
318 cstring srcname = cstring_concatFree1 (fileLib_removeAnyExtension (absname),
320 fileId fid = fileTable_lookup (ft, srcname);
321 cstring_free (srcname);
323 if (fileId_isValid (fid))
325 fileId derid = ft->elements[fid]->fder;
327 ft->elements[fid]->fspecial = TRUE;
329 if (fileId_isValid (derid))
331 ft->elements[derid]->fspecial = TRUE;
338 ftentry de = ft->elements[der];
340 llassert (cstring_isUndefined (e->basename));
341 e->basename = cstring_copy (de->basename);
342 e->fsystem = de->fsystem;
343 e->fspecial = de->fspecial;
346 return (fileTable_internAddEntry (ft, e));
351 fileTable_addFile (fileTable ft, cstring name)
353 return (fileTable_addFilePrim (ft, name, FALSE, FILE_NORMAL, fileId_invalid));
357 fileTable_addFileOnly (fileTable ft, /*@only@*/ cstring name)
359 fileId res = fileTable_addFilePrim (ft, name, FALSE, FILE_NORMAL, fileId_invalid);
365 fileTable_addHeaderFile (fileTable ft, cstring name)
368 res = fileTable_addFilePrim (ft, name, FALSE, FILE_HEADER, fileId_invalid);
374 fileTable_isHeader (fileTable ft, fileId fid)
376 if (fileId_isInvalid (fid))
381 llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid));
382 return (ft->elements[fid]->ftype == FILE_HEADER);
386 fileTable_isSystemFile (fileTable ft, fileId fid)
388 if (fileId_isInvalid (fid))
393 llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid));
394 return (ft->elements[fid]->fsystem);
398 fileTable_isXHFile (fileTable ft, fileId fid)
400 if (fileId_isInvalid (fid))
405 if (!(fileTable_isDefined (ft) && fileTable_inRange (ft, fid)))
407 llcontbug (message ("Bad file table or id: %s %d", bool_unparse (fileTable_isDefined (ft)), fid));
412 return (ft->elements[fid]->ftype == FILE_XH);
417 fileTable_isSpecialFile (fileTable ft, fileId fid)
419 if (fileId_isInvalid (fid))
424 llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid));
425 return (ft->elements[fid]->fspecial);
429 fileTable_addLibraryFile (fileTable ft, cstring name)
431 return (fileTable_addFilePrim (ft, name, FALSE, FILE_HEADER, fileId_invalid));
435 fileTable_addXHFile (fileTable ft, cstring name)
437 return (fileTable_addFilePrim (ft, name, FALSE, FILE_XH, fileId_invalid));
442 fileTable_addImportFile (fileTable ft, cstring name)
444 return (fileTable_addFilePrim (ft, name, FALSE, FILE_HEADER, fileId_invalid));
448 fileTable_addLCLFile (fileTable ft, cstring name)
450 return (fileTable_addFilePrim (ft, name, FALSE, FILE_HEADER, fileId_invalid));
455 static int tmpcounter = 0;
459 fileTable_addMacrosFile (fileTable ft)
462 makeTempName (context_tmpdir (), cstring_makeLiteralTemp ("lmx"),
463 cstring_makeLiteralTemp (".llm"));
464 fileId res = fileTable_addFilePrim (ft, newname, TRUE, FILE_MACROS, fileId_invalid);
465 cstring_free (newname);
470 fileTable_addMetastateFile (fileTable ft, cstring name)
472 return (fileTable_addFilePrim (ft, name, FALSE, FILE_METASTATE, fileId_invalid));
476 fileTable_addCTempFile (fileTable ft, fileId fid)
479 makeTempName (context_tmpdir (), cstring_makeLiteralTemp ("cl"),
483 DPRINTF (("tmp dir: %s", context_tmpdir ()));
484 DPRINTF (("new name: %s", newname));
486 llassert (fileTable_isDefined (ft));
488 if (!fileId_isValid (ft->elements[fid]->fder))
490 if (fileTable_isXHFile (ft, fid))
492 res = fileTable_addFilePrim (ft, newname, TRUE, FILE_XH, fid);
496 res = fileTable_addFilePrim (ft, newname, TRUE, FILE_NORMAL, fid);
501 if (fileTable_isXHFile (ft, fid))
503 res = fileTable_addFilePrim (ft, newname, TRUE, FILE_XH,
504 ft->elements[fid]->fder);
508 res = fileTable_addFilePrim (ft, newname, TRUE, FILE_NORMAL,
509 ft->elements[fid]->fder);
513 DPRINTF (("Added file: %s", fileTable_fileName (res)));
514 cstring_free (newname);
520 fileTable_addltemp (fileTable ft)
522 cstring newname = makeTempName (context_tmpdir (),
523 cstring_makeLiteralTemp ("ls"),
524 cstring_makeLiteralTemp (".lsl"));
527 if (cstring_hasNonAlphaNumBar (newname))
529 char *lastpath = (char *)NULL;
535 ("Operating system generates tmp filename containing invalid charater: %s",
537 lldiagmsg (cstring_makeLiteral
538 ("Try cleaning up the tmp directory. Attempting to continue."));
542 llassert (cstring_isDefined (newname));
543 lastpath = strrchr (newname, CONNECTCHAR); /* get the directory */
544 llassert (lastpath != NULL);
547 newname = message ("%q%hlsl%d.lsl",
551 /*@noaccess cstring@*/
556 ** this is kind of yucky...need to make the result of cstring_fromChars
557 ** refer to the same storage as its argument. Of course, this loses,
558 ** since cstring is abstract. Should make it an only?
561 ret = fileTable_addFilePrim (ft, newname, TRUE, FILE_LSLTEMP, fileId_invalid);
562 cstring_free (newname);
568 fileTable_exists (fileTable ft, cstring s)
570 int tindex = fileTable_getIndex (ft, s);
572 if (tindex == NOT_FOUND)
574 DPRINTF (("Not found: %s", s));
584 fileTable_lookup (fileTable ft, cstring s)
586 int tindex = fileTable_getIndex (ft, s);
588 if (tindex == NOT_FOUND)
590 return fileId_invalid;
599 ** This is pretty awkward --- when we find the real path of
600 ** a .xh file, we may need to change the recorded name. [Sigh]
604 fileTable_setFilePath (fileTable ft, fileId fid, cstring path)
606 llassert (fileId_isValid (fid));
607 llassert (fileTable_isDefined (ft));
608 /* Need to put new string in hash table */
609 cstringTable_insert (ft->htable, cstring_copy (path), fid);
610 ft->elements[fid]->fname = cstring_copy (path);
614 fileTable_lookupBase (fileTable ft, cstring base)
616 int tindex = fileTable_getIndex (ft, base);
618 if (tindex == NOT_FOUND)
620 return fileId_invalid;
626 llassert (fileTable_isDefined (ft));
628 der = ft->elements[tindex]->fder;
630 if (!fileId_isValid (der))
640 fileTable_getName (fileTable ft, fileId fid)
642 if (!fileId_isValid (fid))
645 (message ("fileTable_getName: called with invalid type id: %d", fid));
646 return cstring_makeLiteralTemp ("<invalid>");
649 llassert (fileTable_isDefined (ft));
650 return (ft->elements[fid]->fname);
654 fileTable_getRootName (fileTable ft, fileId fid)
658 if (!fileId_isValid (fid))
660 llcontbug (message ("fileTable_getName: called with invalid id: %d", fid));
661 return cstring_makeLiteralTemp ("<invalid>");
664 if (!fileTable_isDefined (ft))
666 return cstring_makeLiteralTemp ("<no file table>");
669 fder = ft->elements[fid]->fder;
671 if (fileId_isValid (fder))
673 return (ft->elements[fder]->fname);
677 return (ft->elements[fid]->fname);
682 fileTable_getNameBase (fileTable ft, fileId fid)
684 if (!fileId_isValid (fid))
686 llcontbug (message ("fileTable_getName: called with invalid id: %d", fid));
687 return cstring_makeLiteralTemp ("<invalid>");
690 if (!fileTable_isDefined (ft))
692 return cstring_makeLiteralTemp ("<no file table>");
695 return (ft->elements[fid]->basename);
699 fileTable_sameBase (fileTable ft, fileId f1, fileId f2)
703 if (!fileId_isValid (f1))
708 if (!fileId_isValid (f2))
713 llassert (fileTable_isDefined (ft));
720 fd1 = ft->elements[f1]->fder;
722 if (!fileId_isValid (fd1))
727 fd2 = ft->elements[f2]->fder;
730 if (!fileId_isValid (fd2))
739 fileTable_cleanup (fileTable ft)
745 llassert (fileTable_isDefined (ft));
747 msg = ((ft->nentries > 40) && context_getFlag (FLG_SHOWSCAN));
748 skip = ft->nentries / 10;
752 (void) fflush (g_msgstream);
753 fprintf (stderr, "< cleaning");
756 for (i = 0; i < ft->nentries; i++)
758 ftentry fe = ft->elements[i];
762 /* let's be real careful now, hon! */
765 ** Make sure it is really a derived file
769 if (fe->ftype == FILE_LSLTEMP || fe->ftype == FILE_NODELETE)
771 ; /* already removed */
773 else if (fileId_isValid (fe->fder))
775 /*@i423 this should use close (fd) also... */
776 (void) osd_unlink (fe->fname);
778 else if (fe->ftype == FILE_MACROS)
780 (void) osd_unlink (fe->fname);
784 llbug (message ("Temporary file is not derivative: %s "
785 "(not deleted)", fe->fname));
793 if (msg && ((i % skip) == 0))
795 (void) fflush (g_msgstream);
798 fprintf (stderr, " ");
800 fprintf (stderr, ".");
803 (void) fflush (stderr);
809 fprintf (stderr, " >\n");
814 fileTable_free (/*@only@*/ fileTable f)
818 if (f == (fileTable)NULL)
823 while ( i < f->nentries )
825 ftentry_free (f->elements[i]);
829 cstringTable_free (f->htable);
835 ** unique temp filename are constructed from <dir><pre><pid><msg>.<suf>
836 ** requires: <dir> must end in '/'
839 static void nextMsg (char *msg)
869 llassertprint (FALSE, ("nextMsg: out of unique names!!!"));
877 static /*@only@*/ cstring makeTempName (cstring dir, cstring pre, cstring suf)
880 static /*@owned@*/ char *msg = NULL;
881 static /*@only@*/ cstring pidname = NULL;
885 llassert (cstring_length (pre) <= 3);
888 ** We limit the temp name to 8 characters:
896 msg = mstring_copy ("AAA"); /* there are 26^3 temp names */
901 /*@+matchanyintegral@*/
903 /*@=matchanyintegral@*/
906 if (cstring_isUndefined (pidname))
908 pidname = message ("%d", pid % 100);
911 maxlen = (cstring_length (dir) + cstring_length (pre) + mstring_length (msg)
912 + cstring_length (pidname) + cstring_length (suf) + 2);
914 DPRINTF (("Dir: %s / %s / %s / %s / %s",
915 dir, pre, pidname, msg, suf));
917 smsg = message ("%s%s%s%s%s", dir, pre, pidname, cstring_fromChars (msg), suf);
920 DPRINTF (("Trying: %s", smsg));
922 while (osd_fileExists (smsg))
925 smsg = message ("%s%s%s%s%s", dir, pre, pidname, cstring_fromChars (msg), suf);
934 foentry_create (/*@exposed@*/ FILE *f, /*@only@*/ cstring fname)
936 foentry t = (foentry) dmalloc (sizeof (*t));
943 foentry_free (/*@only@*/ foentry foe)
945 cstring_free (foe->fname);
950 fileTable_addOpen (fileTable ft, /*@observer@*/ FILE *f, /*@only@*/ cstring fname)
952 llassert (fileTable_isDefined (ft));
954 if (ft->nopenspace <= 0)
956 fileTable_growOpen (ft);
960 ft->openelements[ft->nopen] = foentry_create (f, fname);
964 FILE *fileTable_openFile (fileTable ft, cstring fname, char *mode)
966 FILE *res = fopen (cstring_toCharsSafe (fname), mode);
969 fileTable_addOpen (ft, res, cstring_copy (fname));
970 DPRINTF (("Opening file: %s / %p", fname, res));
976 bool fileTable_closeFile (fileTable ft, FILE *f)
978 bool foundit = FALSE;
981 llassert (fileTable_isDefined (ft));
983 DPRINTF (("Closing file: %p", f));
985 for (i = 0; i < ft->nopen; i++)
987 if (ft->openelements[i]->f == f)
989 DPRINTF (("Closing file: %p = %s", f, ft->openelements[i]->fname));
991 if (i == ft->nopen - 1)
993 foentry_free (ft->openelements[i]);
994 ft->openelements[i] = NULL;
998 foentry_free (ft->openelements[i]);
999 ft->openelements[i] = ft->openelements[ft->nopen - 1];
1000 ft->openelements[ft->nopen - 1] = NULL;
1011 return (fclose (f) == 0);
1014 void fileTable_closeAll (fileTable ft)
1018 for (i = 0; i < ft->nopen; i++)
1021 lldiagmsg (message ("Unclosed file at exit: %s", ft->openelements[i]->fname));
1024 (void) fclose (ft->openelements[i]->f); /* No check - cleaning up after errors */
1025 ft->openelements[i]->f = NULL;
1026 foentry_free (ft->openelements[i]);
1027 ft->openelements[i] = NULL;
1030 ft->nopenspace += ft->nopen;