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: info@splint.org
21 ** To report a bug: splint-bug@splint.org
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
60 fileTable_addOpen (fileTable p_ft, /*@observer@*/ FILE *p_f, /*@only@*/ cstring p_fname)
63 static bool fileTable_inRange (fileTable ft, fileId fid) /*@*/
65 return (fileTable_isDefined (ft) && (fid >= 0) && (fid < ft->nentries));
68 static fileId fileTable_internAddEntry (fileTable p_ft, /*@only@*/ ftentry p_e)
70 static /*@only@*/ cstring makeTempName (cstring p_dir, cstring p_pre, cstring p_suf);
72 static /*@only@*/ cstring
73 fileType_unparse (fileType ft)
77 case FILE_NORMAL: return cstring_makeLiteral ("normal");
78 case FILE_NODELETE: return cstring_makeLiteral ("normal");
79 case FILE_LSLTEMP: return cstring_makeLiteral ("ltemp");
80 case FILE_HEADER: return cstring_makeLiteral ("header");
81 case FILE_XH: return cstring_makeLiteral ("xh");
82 case FILE_MACROS: return cstring_makeLiteral ("macros");
83 case FILE_METASTATE: return cstring_makeLiteral ("metastate");
90 fileTable_getIndex (fileTable ft, cstring s)
94 if (ft == NULL) return NOT_FOUND;
95 abspath = osd_absolutePath (cstring_undefined, s);
96 DPRINTF (("Absolute path: %s: %s", s, abspath));
97 res = cstringTable_lookup (ft->htable, abspath);
98 cstring_free (abspath);
102 static cstring ftentry_unparse (fileTable ft, ftentry fte)
104 if (fileId_isValid (fte->fder))
106 llassert (fileTable_isDefined (ft));
108 return message ("%s %q %d (%s)",
110 fileType_unparse (fte->ftype),
112 ft->elements[fte->fder]->fname);
116 return message ("%s %q", fte->fname,
117 fileType_unparse (fte->ftype));
122 fileTable_unparse (fileTable ft)
124 cstring s = cstring_undefined;
127 if (fileTable_isUndefined (ft))
129 return (cstring_makeLiteral ("<fileTable undefined>"));
132 for (i = 0; i < ft->nentries; i++)
134 s = message ("%s\n[%d] %q", s, i, ftentry_unparse (ft, ft->elements[i]));
140 void fileTable_printTemps (fileTable ft)
142 if (fileTable_isDefined (ft))
146 for (i = 0; i < ft->nentries; i++)
148 if (ft->elements[i]->ftemp)
150 if (fileId_isValid (ft->elements[i]->fder))
152 fprintf (stderr, " %s:1\n\t%s:1\n",
153 cstring_toCharsSafe (ft->elements[ft->elements[i]->fder]->fname),
154 cstring_toCharsSafe (ft->elements[i]->fname));
158 fprintf (stderr, "[no file]\n\t%s:1\n",
159 cstring_toCharsSafe (ft->elements[i]->fname));
167 ** loads in fileTable from fileTable_dump
170 static /*@notnull@*/ ftentry
171 ftentry_create (/*@keep@*/ cstring tn, bool temp, fileType typ, fileId der)
173 ftentry t = (ftentry) dmalloc (sizeof (*t));
175 if (cstring_isUndefined (tn))
177 llbug (cstring_makeLiteral ("Undefined filename!"));
182 t->basename = cstring_undefined;
187 /* Don't set these until the basename is needed. */
195 ftentry_free (/*@only@*/ ftentry t)
197 cstring_free (t->fname);
198 cstring_free (t->basename);
202 /*@only@*/ /*@notnull@*/ fileTable
205 fileTable ft = (fileTable) dmalloc (sizeof (*ft));
208 ft->nspace = FTBASESIZE;
209 ft->elements = (ftentry *) dmalloc (FTBASESIZE * sizeof (*ft->elements));
210 ft->htable = cstringTable_create (FTHASHSIZE);
213 ft->nopenspace = FTBASESIZE;
214 ft->openelements = (foentry *) dmalloc (FTBASESIZE * sizeof (*ft->openelements));
220 fileTable_grow (fileTable ft)
225 llassert (fileTable_isDefined (ft));
227 ft->nspace = FTBASESIZE;
229 newent = (ftentry *) dmalloc ((ft->nentries + ft->nspace) * sizeof (*newent));
231 for (i = 0; i < ft->nentries; i++)
233 newent[i] = ft->elements[i];
236 sfree (ft->elements);
237 ft->elements = newent;
241 fileTable_growOpen (fileTable ft)
246 llassert (fileTable_isDefined (ft));
248 ft->nopenspace = FTBASESIZE;
250 newent = (foentry *) dmalloc ((ft->nopen + ft->nopenspace) * sizeof (*newent));
252 for (i = 0; i < ft->nopen; i++)
254 newent[i] = ft->openelements[i];
257 sfree (ft->openelements);
258 ft->openelements = newent;
262 fileTable_internAddEntry (fileTable ft, /*@only@*/ ftentry e)
264 llassert (fileTable_isDefined (ft));
271 DPRINTF (("Adding: %s", e->fname));
272 cstringTable_insert (ft->htable, e->fname, ft->nentries);
273 ft->elements[ft->nentries] = e;
276 return (ft->nentries - 1);
279 void fileTable_noDelete (fileTable ft, cstring name)
281 fileId fid = fileTable_lookup (ft, name);
283 if (fileId_isValid (fid))
285 llassert (fileTable_isDefined (ft));
286 ft->elements[fid]->ftype = FILE_NODELETE;
290 DPRINTF (("Invalid no delete: %s", name));
295 fileTable_addFilePrim (fileTable ft, /*@temp@*/ cstring name,
296 bool temp, fileType typ, fileId der)
299 cstring absname = osd_absolutePath (NULL, name);
300 int tindex = fileTable_getIndex (ft, absname);
302 DPRINTF (("Got abs path: %s", absname));
303 llassert (ft != fileTable_undefined);
305 if (tindex != NOT_FOUND)
307 llcontbug (message ("fileTable_addFilePrim: duplicate entry: %q", absname));
312 ftentry e = ftentry_create (absname, temp, typ, der);
314 if (der == fileId_invalid)
316 llassert (cstring_isUndefined (e->basename));
318 e->basename = fileLib_removePathFree (fileLib_removeAnyExtension (absname));
319 e->fsystem = context_isSystemDir (absname);
320 e->fspecial = context_isSpecialFile (absname);
324 cstring srcname = cstring_concatFree1 (fileLib_removeAnyExtension (absname),
326 fileId fid = fileTable_lookup (ft, srcname);
327 cstring_free (srcname);
329 if (fileId_isValid (fid))
331 fileId derid = ft->elements[fid]->fder;
333 ft->elements[fid]->fspecial = TRUE;
335 if (fileId_isValid (derid))
337 ft->elements[derid]->fspecial = TRUE;
344 ftentry de = ft->elements[der];
346 llassert (cstring_isUndefined (e->basename));
347 e->basename = cstring_copy (de->basename);
348 e->fsystem = de->fsystem;
349 e->fspecial = de->fspecial;
352 return (fileTable_internAddEntry (ft, e));
357 fileTable_addFile (fileTable ft, cstring name)
359 return (fileTable_addFilePrim (ft, name, FALSE, FILE_NORMAL, fileId_invalid));
363 fileTable_addFileOnly (fileTable ft, /*@only@*/ cstring name)
365 fileId res = fileTable_addFilePrim (ft, name, FALSE, FILE_NORMAL, fileId_invalid);
371 fileTable_addHeaderFile (fileTable ft, cstring name)
374 res = fileTable_addFilePrim (ft, name, FALSE, FILE_HEADER, fileId_invalid);
380 fileTable_addStreamFile (fileTable ft, FILE *fstream, cstring name)
382 fileTable_addOpen (ft, fstream, cstring_copy (name));
386 fileTable_isHeader (fileTable ft, fileId fid)
388 if (fileId_isInvalid (fid))
393 llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid));
394 return (ft->elements[fid]->ftype == FILE_HEADER);
398 fileTable_isSystemFile (fileTable ft, fileId fid)
400 if (fileId_isInvalid (fid))
405 llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid));
406 return (ft->elements[fid]->fsystem);
410 fileTable_isXHFile (fileTable ft, fileId fid)
412 if (fileId_isInvalid (fid))
417 if (!(fileTable_isDefined (ft) && fileTable_inRange (ft, fid)))
419 llcontbug (message ("Bad file table or id: %s %d", bool_unparse (fileTable_isDefined (ft)), fid));
424 return (ft->elements[fid]->ftype == FILE_XH);
429 fileTable_isSpecialFile (fileTable ft, fileId fid)
431 if (fileId_isInvalid (fid))
436 llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid));
437 return (ft->elements[fid]->fspecial);
441 fileTable_addLibraryFile (fileTable ft, cstring name)
443 return (fileTable_addFilePrim (ft, name, FALSE, FILE_HEADER, fileId_invalid));
447 fileTable_addXHFile (fileTable ft, cstring name)
449 return (fileTable_addFilePrim (ft, name, FALSE, FILE_XH, fileId_invalid));
454 fileTable_addImportFile (fileTable ft, cstring name)
456 return (fileTable_addFilePrim (ft, name, FALSE, FILE_HEADER, fileId_invalid));
460 fileTable_addLCLFile (fileTable ft, cstring name)
462 return (fileTable_addFilePrim (ft, name, FALSE, FILE_HEADER, fileId_invalid));
467 static int tmpcounter = 0;
471 fileTable_addMacrosFile (fileTable ft)
474 makeTempName (context_tmpdir (), cstring_makeLiteralTemp ("lmx"),
475 cstring_makeLiteralTemp (".llm"));
476 fileId res = fileTable_addFilePrim (ft, newname, TRUE, FILE_MACROS, fileId_invalid);
477 cstring_free (newname);
482 fileTable_addMetastateFile (fileTable ft, cstring name)
484 return (fileTable_addFilePrim (ft, name, FALSE, FILE_METASTATE, fileId_invalid));
488 fileTable_addCTempFile (fileTable ft, fileId fid)
491 makeTempName (context_tmpdir (), cstring_makeLiteralTemp ("cl"),
495 DPRINTF (("tmp dir: %s", context_tmpdir ()));
496 DPRINTF (("new name: %s", newname));
498 llassert (fileTable_isDefined (ft));
500 if (!fileId_isValid (ft->elements[fid]->fder))
502 if (fileTable_isXHFile (ft, fid))
504 res = fileTable_addFilePrim (ft, newname, TRUE, FILE_XH, fid);
508 res = fileTable_addFilePrim (ft, newname, TRUE, FILE_NORMAL, fid);
513 if (fileTable_isXHFile (ft, fid))
515 res = fileTable_addFilePrim (ft, newname, TRUE, FILE_XH,
516 ft->elements[fid]->fder);
520 res = fileTable_addFilePrim (ft, newname, TRUE, FILE_NORMAL,
521 ft->elements[fid]->fder);
525 DPRINTF (("Added file: %s", fileTable_fileName (res)));
526 cstring_free (newname);
532 fileTable_addltemp (fileTable ft)
534 cstring newname = makeTempName (context_tmpdir (),
535 cstring_makeLiteralTemp ("ls"),
536 cstring_makeLiteralTemp (".lsl"));
539 if (cstring_hasNonAlphaNumBar (newname))
541 char *lastpath = (char *)NULL;
547 ("Operating system generates tmp filename containing invalid charater: %s",
549 lldiagmsg (cstring_makeLiteral
550 ("Try cleaning up the tmp directory. Attempting to continue."));
554 llassert (cstring_isDefined (newname));
555 lastpath = strrchr (newname, CONNECTCHAR); /* get the directory */
556 llassert (lastpath != NULL);
559 newname = message ("%q%hlsl%d.lsl",
563 /*@noaccess cstring@*/
568 ** this is kind of yucky...need to make the result of cstring_fromChars
569 ** refer to the same storage as its argument. Of course, this loses,
570 ** since cstring is abstract. Should make it an only?
573 ret = fileTable_addFilePrim (ft, newname, TRUE, FILE_LSLTEMP, fileId_invalid);
574 cstring_free (newname);
580 fileTable_exists (fileTable ft, cstring s)
582 int tindex = fileTable_getIndex (ft, s);
584 if (tindex == NOT_FOUND)
586 DPRINTF (("Not found: %s", s));
596 fileTable_lookup (fileTable ft, cstring s)
598 int tindex = fileTable_getIndex (ft, s);
600 if (tindex == NOT_FOUND)
602 return fileId_invalid;
611 ** This is pretty awkward --- when we find the real path of
612 ** a .xh file, we may need to change the recorded name. [Sigh]
616 fileTable_setFilePath (fileTable ft, fileId fid, cstring path)
618 llassert (fileId_isValid (fid));
619 llassert (fileTable_isDefined (ft));
620 /* Need to put new string in hash table */
621 cstringTable_insert (ft->htable, cstring_copy (path), fid);
622 ft->elements[fid]->fname = cstring_copy (path);
626 fileTable_lookupBase (fileTable ft, cstring base)
628 int tindex = fileTable_getIndex (ft, base);
630 if (tindex == NOT_FOUND)
632 return fileId_invalid;
638 llassert (fileTable_isDefined (ft));
640 der = ft->elements[tindex]->fder;
642 if (!fileId_isValid (der))
652 fileTable_getName (fileTable ft, fileId fid)
654 if (!fileId_isValid (fid))
657 (message ("fileTable_getName: called with invalid type id: %d", fid));
658 return cstring_makeLiteralTemp ("<invalid>");
661 llassert (fileTable_isDefined (ft));
662 return (ft->elements[fid]->fname);
666 fileTable_getRootName (fileTable ft, fileId fid)
670 if (!fileId_isValid (fid))
672 llcontbug (message ("fileTable_getName: called with invalid id: %d", fid));
673 return cstring_makeLiteralTemp ("<invalid>");
676 if (!fileTable_isDefined (ft))
678 return cstring_makeLiteralTemp ("<no file table>");
681 fder = ft->elements[fid]->fder;
683 if (fileId_isValid (fder))
685 return (ft->elements[fder]->fname);
689 return (ft->elements[fid]->fname);
694 fileTable_getNameBase (fileTable ft, fileId fid)
696 if (!fileId_isValid (fid))
698 llcontbug (message ("fileTable_getName: called with invalid id: %d", fid));
699 return cstring_makeLiteralTemp ("<invalid>");
702 if (!fileTable_isDefined (ft))
704 return cstring_makeLiteralTemp ("<no file table>");
707 return (ft->elements[fid]->basename);
711 fileTable_sameBase (fileTable ft, fileId f1, fileId f2)
715 if (!fileId_isValid (f1))
720 if (!fileId_isValid (f2))
725 llassert (fileTable_isDefined (ft));
732 fd1 = ft->elements[f1]->fder;
734 if (!fileId_isValid (fd1))
739 fd2 = ft->elements[f2]->fder;
742 if (!fileId_isValid (fd2))
751 fileTable_cleanup (fileTable ft)
757 llassert (fileTable_isDefined (ft));
759 msg = ((ft->nentries > 40) && context_getFlag (FLG_SHOWSCAN));
760 skip = ft->nentries / 10;
764 (void) fflush (g_warningstream);
765 fprintf (stderr, "< cleaning");
768 for (i = 0; i < ft->nentries; i++)
770 ftentry fe = ft->elements[i];
774 /* let's be real careful now, hon! */
777 ** Make sure it is really a derived file
781 if (fe->ftype == FILE_LSLTEMP || fe->ftype == FILE_NODELETE)
783 ; /* already removed */
785 else if (fileId_isValid (fe->fder))
787 /*@i423 this should use close (fd) also... */
788 (void) osd_unlink (fe->fname);
790 else if (fe->ftype == FILE_MACROS)
792 (void) osd_unlink (fe->fname);
796 llbug (message ("Temporary file is not derivative: %s "
797 "(not deleted)", fe->fname));
805 if (msg && ((i % skip) == 0))
807 (void) fflush (g_warningstream);
810 fprintf (stderr, " ");
812 fprintf (stderr, ".");
815 (void) fflush (stderr);
821 fprintf (stderr, " >\n");
826 fileTable_free (/*@only@*/ fileTable f)
830 if (f == (fileTable)NULL)
835 while ( i < f->nentries )
837 ftentry_free (f->elements[i]);
841 cstringTable_free (f->htable);
847 ** unique temp filename are constructed from <dir><pre><pid><msg>.<suf>
848 ** requires: <dir> must end in '/'
851 static void nextMsg (char *msg)
881 llassertprint (FALSE, ("nextMsg: out of unique names!!!"));
889 static /*@only@*/ cstring makeTempName (cstring dir, cstring pre, cstring suf)
892 static /*@owned@*/ char *msg = NULL;
893 static /*@only@*/ cstring pidname = NULL;
897 llassert (cstring_length (pre) <= 3);
900 ** We limit the temp name to 8 characters:
908 msg = mstring_copy ("AAA"); /* there are 26^3 temp names */
913 /*@+matchanyintegral@*/
915 /*@=matchanyintegral@*/
918 if (cstring_isUndefined (pidname))
920 pidname = message ("%d", pid % 100);
923 maxlen = (cstring_length (dir) + cstring_length (pre) + mstring_length (msg)
924 + cstring_length (pidname) + cstring_length (suf) + 2);
926 DPRINTF (("Dir: %s / %s / %s / %s / %s",
927 dir, pre, pidname, msg, suf));
929 smsg = message ("%s%s%s%s%s", dir, pre, pidname, cstring_fromChars (msg), suf);
932 DPRINTF (("Trying: %s", smsg));
934 while (osd_fileExists (smsg))
937 smsg = message ("%s%s%s%s%s", dir, pre, pidname, cstring_fromChars (msg), suf);
946 foentry_create (/*@exposed@*/ FILE *f, /*@only@*/ cstring fname)
948 foentry t = (foentry) dmalloc (sizeof (*t));
955 foentry_free (/*@only@*/ foentry foe)
957 cstring_free (foe->fname);
962 fileTable_addOpen (fileTable ft, /*@observer@*/ FILE *f, /*@only@*/ cstring fname)
964 llassert (fileTable_isDefined (ft));
966 if (ft->nopenspace <= 0)
968 fileTable_growOpen (ft);
972 ft->openelements[ft->nopen] = foentry_create (f, fname);
976 FILE *fileTable_openFile (fileTable ft, cstring fname, char *mode)
978 FILE *res = fopen (cstring_toCharsSafe (fname), mode);
981 fileTable_addOpen (ft, res, cstring_copy (fname));
982 DPRINTF (("Opening file: %s / %p", fname, res));
988 bool fileTable_closeFile (fileTable ft, FILE *f)
990 bool foundit = FALSE;
993 llassert (fileTable_isDefined (ft));
995 DPRINTF (("Closing file: %p", f));
997 for (i = 0; i < ft->nopen; i++)
999 if (ft->openelements[i]->f == f)
1001 DPRINTF (("Closing file: %p = %s", f, ft->openelements[i]->fname));
1003 if (i == ft->nopen - 1)
1005 foentry_free (ft->openelements[i]);
1006 ft->openelements[i] = NULL;
1010 foentry_free (ft->openelements[i]);
1011 ft->openelements[i] = ft->openelements[ft->nopen - 1];
1012 ft->openelements[ft->nopen - 1] = NULL;
1023 return (fclose (f) == 0);
1026 void fileTable_closeAll (fileTable ft)
1030 llassert (fileTable_isDefined (ft));
1032 for (i = 0; i < ft->nopen; i++)
1035 lldiagmsg (message ("Unclosed file at exit: %s", ft->openelements[i]->fname));
1038 if (ft->openelements[i]->f != NULL)
1040 (void) fclose (ft->openelements[i]->f); /* No check - cleaning up after errors */
1043 ft->openelements[i]->f = NULL;
1044 foentry_free (ft->openelements[i]);
1045 ft->openelements[i] = NULL;
1048 ft->nopenspace += ft->nopen;