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 <sys/types.h>
48 # include <sys/stat.h>
50 # include "splintMacros.nf"
56 # if defined(__IBMC__) && defined(OS2)
58 # define getpid _getpid
64 fileTable_addOpen (fileTable p_ft, /*@observer@*/ FILE *p_f, /*@only@*/ cstring p_fname)
67 static bool fileTable_inRange (fileTable ft, fileId fid) /*@*/
69 return (fileTable_isDefined (ft) && (fid >= 0) && (fid < ft->nentries));
72 static fileId fileTable_internAddEntry (fileTable p_ft, /*@only@*/ ftentry p_e)
74 static /*@only@*/ cstring makeTempName (cstring p_dir, cstring p_pre, cstring p_suf);
76 static /*@only@*/ cstring
77 fileType_unparse (fileType ft)
81 case FILE_NORMAL: return cstring_makeLiteral ("normal");
82 case FILE_NODELETE: return cstring_makeLiteral ("normal");
83 case FILE_LSLTEMP: return cstring_makeLiteral ("ltemp");
84 case FILE_HEADER: return cstring_makeLiteral ("header");
85 case FILE_XH: return cstring_makeLiteral ("xh");
86 case FILE_MACROS: return cstring_makeLiteral ("macros");
87 case FILE_METASTATE: return cstring_makeLiteral ("metastate");
94 fileTable_getIndex (fileTable ft, cstring s)
98 if (ft == NULL) return NOT_FOUND;
99 abspath = osd_absolutePath (cstring_undefined, s);
100 DPRINTF (("Absolute path: %s: %s", s, abspath));
101 res = cstringTable_lookup (ft->htable, abspath);
102 cstring_free (abspath);
106 static cstring ftentry_unparse (fileTable ft, ftentry fte)
108 if (fileId_isValid (fte->fder))
110 llassert (fileTable_isDefined (ft));
112 return message ("%s %q %d (%s)",
114 fileType_unparse (fte->ftype),
116 ft->elements[fte->fder]->fname);
120 return message ("%s %q", fte->fname,
121 fileType_unparse (fte->ftype));
126 fileTable_unparse (fileTable ft)
128 cstring s = cstring_undefined;
131 if (fileTable_isUndefined (ft))
133 return (cstring_makeLiteral ("<fileTable undefined>"));
136 for (i = 0; i < ft->nentries; i++)
138 s = message ("%s\n[%d] %q", s, i, ftentry_unparse (ft, ft->elements[i]));
144 void fileTable_printTemps (fileTable ft)
146 if (fileTable_isDefined (ft))
150 for (i = 0; i < ft->nentries; i++)
152 if (ft->elements[i]->ftemp)
154 if (fileId_isValid (ft->elements[i]->fder))
156 fprintf (stderr, " %s:1\n\t%s:1\n",
157 cstring_toCharsSafe (ft->elements[ft->elements[i]->fder]->fname),
158 cstring_toCharsSafe (ft->elements[i]->fname));
162 fprintf (stderr, "[no file]\n\t%s:1\n",
163 cstring_toCharsSafe (ft->elements[i]->fname));
171 ** loads in fileTable from fileTable_dump
174 static /*@notnull@*/ ftentry
175 ftentry_create (/*@keep@*/ cstring tn, bool temp, fileType typ, fileId der)
177 ftentry t = (ftentry) dmalloc (sizeof (*t));
179 if (cstring_isUndefined (tn))
181 llbug (cstring_makeLiteral ("Undefined filename!"));
186 t->basename = cstring_undefined;
191 /* Don't set these until the basename is needed. */
199 ftentry_free (/*@only@*/ ftentry t)
201 cstring_free (t->fname);
202 cstring_free (t->basename);
206 /*@only@*/ /*@notnull@*/ fileTable
209 fileTable ft = (fileTable) dmalloc (sizeof (*ft));
212 ft->nspace = FTBASESIZE;
213 ft->elements = (ftentry *) dmalloc (FTBASESIZE * sizeof (*ft->elements));
214 ft->htable = cstringTable_create (FTHASHSIZE);
217 ft->nopenspace = FTBASESIZE;
218 ft->openelements = (foentry *) dmalloc (FTBASESIZE * sizeof (*ft->openelements));
224 fileTable_grow (fileTable ft)
229 llassert (fileTable_isDefined (ft));
231 ft->nspace = FTBASESIZE;
233 newent = (ftentry *) dmalloc ((ft->nentries + ft->nspace) * sizeof (*newent));
235 for (i = 0; i < ft->nentries; i++)
237 newent[i] = ft->elements[i];
240 sfree (ft->elements);
241 ft->elements = newent;
245 fileTable_growOpen (fileTable ft)
250 llassert (fileTable_isDefined (ft));
252 ft->nopenspace = FTBASESIZE;
254 newent = (foentry *) dmalloc ((ft->nopen + ft->nopenspace) * sizeof (*newent));
256 for (i = 0; i < ft->nopen; i++)
258 newent[i] = ft->openelements[i];
261 sfree (ft->openelements);
262 ft->openelements = newent;
266 fileTable_internAddEntry (fileTable ft, /*@only@*/ ftentry e)
268 llassert (fileTable_isDefined (ft));
275 DPRINTF (("Adding: %s", e->fname));
276 cstringTable_insert (ft->htable, e->fname, ft->nentries);
277 ft->elements[ft->nentries] = e;
280 return (ft->nentries - 1);
283 void fileTable_noDelete (fileTable ft, cstring name)
285 fileId fid = fileTable_lookup (ft, name);
287 if (fileId_isValid (fid))
289 llassert (fileTable_isDefined (ft));
290 ft->elements[fid]->ftype = FILE_NODELETE;
294 DPRINTF (("Invalid no delete: %s", name));
299 fileTable_addFilePrim (fileTable ft, /*@temp@*/ cstring name,
300 bool temp, fileType typ, fileId der)
303 cstring absname = osd_absolutePath (NULL, name);
304 int tindex = fileTable_getIndex (ft, absname);
306 DPRINTF (("Got abs path: %s", absname));
307 llassert (ft != fileTable_undefined);
309 if (tindex != NOT_FOUND)
311 llcontbug (message ("fileTable_addFilePrim: duplicate entry: %q", absname));
316 ftentry e = ftentry_create (absname, temp, typ, der);
318 if (der == fileId_invalid)
320 llassert (cstring_isUndefined (e->basename));
322 e->basename = fileLib_removePathFree (fileLib_removeAnyExtension (absname));
323 e->fsystem = context_isSystemDir (absname);
326 ** evans 2002-03-15: change suggested by Jim Zelenka
327 ** support relative paths for system directories
332 e->fsystem = context_isSystemDir (name);
335 e->fspecial = context_isSpecialFile (absname);
339 cstring srcname = cstring_concatFree1 (fileLib_removeAnyExtension (absname),
341 fileId fid = fileTable_lookup (ft, srcname);
342 cstring_free (srcname);
344 if (fileId_isValid (fid))
346 fileId derid = ft->elements[fid]->fder;
348 ft->elements[fid]->fspecial = TRUE;
350 if (fileId_isValid (derid))
352 ft->elements[derid]->fspecial = TRUE;
359 ftentry de = ft->elements[der];
361 llassert (cstring_isUndefined (e->basename));
362 e->basename = cstring_copy (de->basename);
363 e->fsystem = de->fsystem;
364 e->fspecial = de->fspecial;
367 return (fileTable_internAddEntry (ft, e));
372 fileTable_addFile (fileTable ft, cstring name)
374 return (fileTable_addFilePrim (ft, name, FALSE, FILE_NORMAL, fileId_invalid));
378 fileTable_addFileOnly (fileTable ft, /*@only@*/ cstring name)
380 fileId res = fileTable_addFilePrim (ft, name, FALSE, FILE_NORMAL, fileId_invalid);
386 fileTable_addHeaderFile (fileTable ft, cstring name)
389 res = fileTable_addFilePrim (ft, name, FALSE, FILE_HEADER, fileId_invalid);
395 fileTable_addStreamFile (fileTable ft, FILE *fstream, cstring name)
397 fileTable_addOpen (ft, fstream, cstring_copy (name));
401 fileTable_isHeader (fileTable ft, fileId fid)
403 if (fileId_isInvalid (fid))
408 llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid));
409 return (ft->elements[fid]->ftype == FILE_HEADER);
413 fileTable_isSystemFile (fileTable ft, fileId fid)
415 if (fileId_isInvalid (fid))
420 llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid));
421 return (ft->elements[fid]->fsystem);
425 fileTable_isXHFile (fileTable ft, fileId fid)
427 if (fileId_isInvalid (fid))
432 if (!(fileTable_isDefined (ft) && fileTable_inRange (ft, fid)))
434 llcontbug (message ("Bad file table or id: %s %d", bool_unparse (fileTable_isDefined (ft)), fid));
439 return (ft->elements[fid]->ftype == FILE_XH);
444 fileTable_isSpecialFile (fileTable ft, fileId fid)
446 if (fileId_isInvalid (fid))
451 llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid));
452 return (ft->elements[fid]->fspecial);
456 fileTable_addLibraryFile (fileTable ft, cstring name)
458 return (fileTable_addFilePrim (ft, name, FALSE, FILE_HEADER, fileId_invalid));
462 fileTable_addXHFile (fileTable ft, cstring name)
464 return (fileTable_addFilePrim (ft, name, FALSE, FILE_XH, fileId_invalid));
469 fileTable_addImportFile (fileTable ft, cstring name)
471 return (fileTable_addFilePrim (ft, name, FALSE, FILE_HEADER, fileId_invalid));
475 fileTable_addLCLFile (fileTable ft, cstring name)
477 return (fileTable_addFilePrim (ft, name, FALSE, FILE_HEADER, fileId_invalid));
482 static int tmpcounter = 0;
486 fileTable_addMacrosFile (fileTable ft)
489 makeTempName (context_tmpdir (), cstring_makeLiteralTemp ("lmx"),
490 cstring_makeLiteralTemp (".llm"));
491 fileId res = fileTable_addFilePrim (ft, newname, TRUE, FILE_MACROS, fileId_invalid);
492 cstring_free (newname);
497 fileTable_addMetastateFile (fileTable ft, cstring name)
499 return (fileTable_addFilePrim (ft, name, FALSE, FILE_METASTATE, fileId_invalid));
503 fileTable_addCTempFile (fileTable ft, fileId fid)
506 makeTempName (context_tmpdir (), cstring_makeLiteralTemp ("cl"),
510 DPRINTF (("tmp dir: %s", context_tmpdir ()));
511 DPRINTF (("new name: %s", newname));
513 llassert (fileTable_isDefined (ft));
515 if (!fileId_isValid (ft->elements[fid]->fder))
517 if (fileTable_isXHFile (ft, fid))
519 res = fileTable_addFilePrim (ft, newname, TRUE, FILE_XH, fid);
523 res = fileTable_addFilePrim (ft, newname, TRUE, FILE_NORMAL, fid);
528 if (fileTable_isXHFile (ft, fid))
530 res = fileTable_addFilePrim (ft, newname, TRUE, FILE_XH,
531 ft->elements[fid]->fder);
535 res = fileTable_addFilePrim (ft, newname, TRUE, FILE_NORMAL,
536 ft->elements[fid]->fder);
540 DPRINTF (("Added file: %s", fileTable_fileName (res)));
541 cstring_free (newname);
547 fileTable_addltemp (fileTable ft)
549 cstring newname = makeTempName (context_tmpdir (),
550 cstring_makeLiteralTemp ("ls"),
551 cstring_makeLiteralTemp (".lsl"));
554 if (cstring_hasNonAlphaNumBar (newname))
556 char *lastpath = (char *)NULL;
562 ("Operating system generates tmp filename containing invalid charater: %s",
564 lldiagmsg (cstring_makeLiteral
565 ("Try cleaning up the tmp directory. Attempting to continue."));
569 llassert (cstring_isDefined (newname));
570 lastpath = strrchr (newname, CONNECTCHAR); /* get the directory */
571 llassert (lastpath != NULL);
574 newname = message ("%q%hlsl%d.lsl",
578 /*@noaccess cstring@*/
583 ** this is kind of yucky...need to make the result of cstring_fromChars
584 ** refer to the same storage as its argument. Of course, this loses,
585 ** since cstring is abstract. Should make it an only?
588 ret = fileTable_addFilePrim (ft, newname, TRUE, FILE_LSLTEMP, fileId_invalid);
589 cstring_free (newname);
595 fileTable_exists (fileTable ft, cstring s)
597 int tindex = fileTable_getIndex (ft, s);
599 if (tindex == NOT_FOUND)
601 DPRINTF (("Not found: %s", s));
611 fileTable_lookup (fileTable ft, cstring s)
613 int tindex = fileTable_getIndex (ft, s);
615 if (tindex == NOT_FOUND)
617 return fileId_invalid;
626 ** This is pretty awkward --- when we find the real path of
627 ** a .xh file, we may need to change the recorded name. [Sigh]
631 fileTable_setFilePath (fileTable ft, fileId fid, cstring path)
633 llassert (fileId_isValid (fid));
634 llassert (fileTable_isDefined (ft));
635 /* Need to put new string in hash table */
636 cstringTable_insert (ft->htable, cstring_copy (path), fid);
637 ft->elements[fid]->fname = cstring_copy (path);
641 fileTable_lookupBase (fileTable ft, cstring base)
643 int tindex = fileTable_getIndex (ft, base);
645 if (tindex == NOT_FOUND)
647 return fileId_invalid;
653 llassert (fileTable_isDefined (ft));
655 der = ft->elements[tindex]->fder;
657 if (!fileId_isValid (der))
667 fileTable_getName (fileTable ft, fileId fid)
669 if (!fileId_isValid (fid))
672 (message ("fileTable_getName: called with invalid type id: %d", fid));
673 return cstring_makeLiteralTemp ("<invalid>");
676 llassert (fileTable_isDefined (ft));
677 return (ft->elements[fid]->fname);
681 fileTable_getRootName (fileTable ft, fileId fid)
685 if (!fileId_isValid (fid))
687 llcontbug (message ("fileTable_getName: called with invalid id: %d", fid));
688 return cstring_makeLiteralTemp ("<invalid>");
691 if (!fileTable_isDefined (ft))
693 return cstring_makeLiteralTemp ("<no file table>");
696 fder = ft->elements[fid]->fder;
698 if (fileId_isValid (fder))
700 return (ft->elements[fder]->fname);
704 return (ft->elements[fid]->fname);
709 fileTable_getNameBase (fileTable ft, fileId fid)
711 if (!fileId_isValid (fid))
713 llcontbug (message ("fileTable_getName: called with invalid id: %d", fid));
714 return cstring_makeLiteralTemp ("<invalid>");
717 if (!fileTable_isDefined (ft))
719 return cstring_makeLiteralTemp ("<no file table>");
722 return (ft->elements[fid]->basename);
726 fileTable_sameBase (fileTable ft, fileId f1, fileId f2)
730 if (!fileId_isValid (f1))
735 if (!fileId_isValid (f2))
740 llassert (fileTable_isDefined (ft));
747 fd1 = ft->elements[f1]->fder;
749 if (!fileId_isValid (fd1))
754 fd2 = ft->elements[f2]->fder;
757 if (!fileId_isValid (fd2))
766 fileTable_cleanup (fileTable ft)
772 llassert (fileTable_isDefined (ft));
774 msg = ((ft->nentries > 40) && context_getFlag (FLG_SHOWSCAN));
775 skip = ft->nentries / 10;
779 (void) fflush (g_warningstream);
780 fprintf (stderr, "< cleaning");
783 for (i = 0; i < ft->nentries; i++)
785 ftentry fe = ft->elements[i];
789 /* let's be real careful now, hon! */
792 ** Make sure it is really a derived file
796 if (fe->ftype == FILE_LSLTEMP || fe->ftype == FILE_NODELETE)
798 ; /* already removed */
800 else if (fileId_isValid (fe->fder))
802 /*@i423 this should use close (fd) also... */
803 (void) osd_unlink (fe->fname);
805 else if (fe->ftype == FILE_MACROS)
807 (void) osd_unlink (fe->fname);
811 llbug (message ("Temporary file is not derivative: %s "
812 "(not deleted)", fe->fname));
820 if (msg && ((i % skip) == 0))
822 (void) fflush (g_warningstream);
825 fprintf (stderr, " ");
827 fprintf (stderr, ".");
830 (void) fflush (stderr);
836 fprintf (stderr, " >\n");
841 fileTable_free (/*@only@*/ fileTable f)
845 if (f == (fileTable)NULL)
850 while ( i < f->nentries )
852 ftentry_free (f->elements[i]);
856 cstringTable_free (f->htable);
862 ** unique temp filename are constructed from <dir><pre><pid><msg>.<suf>
863 ** requires: <dir> must end in '/'
866 static void nextMsg (char *msg)
896 llassertprint (FALSE, ("nextMsg: out of unique names!!!"));
904 static /*@only@*/ cstring makeTempName (cstring dir, cstring pre, cstring suf)
907 static /*@owned@*/ char *msg = NULL;
908 static /*@only@*/ cstring pidname = NULL;
912 llassert (cstring_length (pre) <= 3);
915 ** We limit the temp name to 8 characters:
923 msg = mstring_copy ("AAA"); /* there are 26^3 temp names */
928 /*@+matchanyintegral@*/
930 /*@=matchanyintegral@*/
933 if (cstring_isUndefined (pidname))
935 pidname = message ("%d", pid % 100);
938 maxlen = (cstring_length (dir) + cstring_length (pre) + mstring_length (msg)
939 + cstring_length (pidname) + cstring_length (suf) + 2);
941 DPRINTF (("Dir: %s / %s / %s / %s / %s",
942 dir, pre, pidname, msg, suf));
944 smsg = message ("%s%s%s%s%s", dir, pre, pidname, cstring_fromChars (msg), suf);
947 DPRINTF (("Trying: %s", smsg));
949 while (osd_fileExists (smsg))
952 smsg = message ("%s%s%s%s%s", dir, pre, pidname, cstring_fromChars (msg), suf);
960 foentry_create (/*@exposed@*/ FILE *f, /*@only@*/ cstring fname)
962 foentry t = (foentry) dmalloc (sizeof (*t));
969 foentry_free (/*@only@*/ foentry foe)
971 cstring_free (foe->fname);
976 fileTable_addOpen (fileTable ft, /*@observer@*/ FILE *f, /*@only@*/ cstring fname)
978 llassert (fileTable_isDefined (ft));
980 if (ft->nopenspace <= 0)
982 fileTable_growOpen (ft);
986 ft->openelements[ft->nopen] = foentry_create (f, fname);
990 FILE *fileTable_createFile (fileTable ft, cstring fname)
992 int fdesc = open (cstring_toCharsSafe (fname), O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, S_IRUSR | S_IWUSR);
997 llfatalerror (message ("Temporary file for "
998 "pre-processor output already exists. Trying to "
1002 /*@notreached@*/ return NULL;
1006 FILE *res = fdopen (fdesc, "w");
1010 fileTable_addOpen (ft, res, cstring_copy (fname));
1011 DPRINTF (("Opening file: %s / %p", fname, res));
1015 DPRINTF (("Error opening: %s", fname));
1022 FILE *fileTable_createMacrosFile (fileTable ft, cstring fname)
1024 int fdesc = open (cstring_toCharsSafe (fname), O_RDWR | O_CREAT | O_TRUNC | O_EXCL, S_IRUSR | S_IWUSR);
1028 osd_setTempError ();
1029 llfatalerror (message ("Temporary file for "
1030 "pre-processor output already exists. Trying to "
1034 /*@notreached@*/ return NULL;
1038 FILE *res = fdopen (fdesc, "w+");
1042 fileTable_addOpen (ft, res, cstring_copy (fname));
1043 DPRINTF (("Opening file: %s / %p", fname, res));
1047 DPRINTF (("Error opening: %s", fname));
1054 FILE *fileTable_openReadFile (fileTable ft, cstring fname)
1056 FILE *res = fopen (cstring_toCharsSafe (fname), "r");
1060 fileTable_addOpen (ft, res, cstring_copy (fname));
1061 DPRINTF (("Opening read file: %s / %p", fname, res));
1065 DPRINTF (("Cannot open read file: %s", fname));
1072 ** Allows overwriting
1075 FILE *fileTable_openWriteFile (fileTable ft, cstring fname)
1077 FILE *res = fopen (cstring_toCharsSafe (fname), "w");
1080 fileTable_addOpen (ft, res, cstring_copy (fname));
1081 DPRINTF (("Opening file: %s / %p", fname, res));
1087 FILE *fileTable_openWriteUpdateFile (fileTable ft, cstring fname)
1089 FILE *res = fopen (cstring_toCharsSafe (fname), "w+");
1092 fileTable_addOpen (ft, res, cstring_copy (fname));
1093 DPRINTF (("Opening file: %s / %p", fname, res));
1099 bool fileTable_closeFile (fileTable ft, FILE *f)
1101 bool foundit = FALSE;
1104 llassert (fileTable_isDefined (ft));
1106 DPRINTF (("Closing file: %p", f));
1108 for (i = 0; i < ft->nopen; i++)
1110 if (ft->openelements[i]->f == f)
1112 DPRINTF (("Closing file: %p = %s", f, ft->openelements[i]->fname));
1114 if (i == ft->nopen - 1)
1116 foentry_free (ft->openelements[i]);
1117 ft->openelements[i] = NULL;
1121 foentry_free (ft->openelements[i]);
1122 ft->openelements[i] = ft->openelements[ft->nopen - 1];
1123 ft->openelements[ft->nopen - 1] = NULL;
1134 return (fclose (f) == 0);
1137 void fileTable_closeAll (fileTable ft)
1141 llassert (fileTable_isDefined (ft));
1143 for (i = 0; i < ft->nopen; i++)
1146 lldiagmsg (message ("Unclosed file at exit: %s", ft->openelements[i]->fname));
1149 if (ft->openelements[i]->f != NULL)
1151 (void) fclose (ft->openelements[i]->f); /* No check - cleaning up after errors */
1154 ft->openelements[i]->f = NULL;
1155 foentry_free (ft->openelements[i]);
1156 ft->openelements[i] = NULL;
1159 ft->nopenspace += ft->nopen;