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)
59 # define getpid _getpid
60 # define S_IRUSR S_IREAD
61 # define S_IWUSR S_IWRITE
62 # define S_IXUSR S_IEXEC
68 fileTable_addOpen (fileTable p_ft, /*@observer@*/ FILE *p_f, /*@only@*/ cstring p_fname)
71 static bool fileTable_inRange (fileTable ft, fileId fid) /*@*/
73 return (fileTable_isDefined (ft) && (fid >= 0) && (fid < ft->nentries));
76 static fileId fileTable_internAddEntry (fileTable p_ft, /*@only@*/ ftentry p_e)
78 static /*@only@*/ cstring makeTempName (cstring p_dir, cstring p_pre, cstring p_suf);
80 static /*@only@*/ cstring
81 fileType_unparse (fileType ft)
85 case FILE_NORMAL: return cstring_makeLiteral ("normal");
86 case FILE_NODELETE: return cstring_makeLiteral ("normal");
87 case FILE_LSLTEMP: return cstring_makeLiteral ("ltemp");
88 case FILE_HEADER: return cstring_makeLiteral ("header");
89 case FILE_XH: return cstring_makeLiteral ("xh");
90 case FILE_MACROS: return cstring_makeLiteral ("macros");
91 case FILE_METASTATE: return cstring_makeLiteral ("metastate");
98 fileTable_getIndex (fileTable ft, cstring s)
102 if (ft == NULL) return NOT_FOUND;
103 abspath = osd_absolutePath (cstring_undefined, s);
105 if (context_getFlag (FLG_CASEINSENSITIVEFILENAMES))
107 abspath = cstring_downcase (abspath);
110 DPRINTF (("Absolute path: %s: %s", s, abspath));
111 res = cstringTable_lookup (ft->htable, abspath);
112 cstring_free (abspath);
116 static cstring ftentry_unparse (fileTable ft, ftentry fte)
118 if (fileId_isValid (fte->fder))
120 llassert (fileTable_isDefined (ft));
122 return message ("%s %q %d (%s)",
124 fileType_unparse (fte->ftype),
126 ft->elements[fte->fder]->fname);
130 return message ("%s %q", fte->fname,
131 fileType_unparse (fte->ftype));
136 fileTable_unparse (fileTable ft)
138 cstring s = cstring_undefined;
141 if (fileTable_isUndefined (ft))
143 return (cstring_makeLiteral ("<fileTable undefined>"));
146 for (i = 0; i < ft->nentries; i++)
148 s = message ("%s\n[%d] %q", s, i, ftentry_unparse (ft, ft->elements[i]));
154 void fileTable_printTemps (fileTable ft)
156 if (fileTable_isDefined (ft))
160 for (i = 0; i < ft->nentries; i++)
162 if (ft->elements[i]->ftemp)
164 if (fileId_isValid (ft->elements[i]->fder))
166 fprintf (stderr, " %s:1\n\t%s:1\n",
167 cstring_toCharsSafe (ft->elements[ft->elements[i]->fder]->fname),
168 cstring_toCharsSafe (ft->elements[i]->fname));
172 fprintf (stderr, "[no file]\n\t%s:1\n",
173 cstring_toCharsSafe (ft->elements[i]->fname));
181 ** loads in fileTable from fileTable_dump
184 static /*@notnull@*/ ftentry
185 ftentry_create (/*@keep@*/ cstring tn, bool temp, fileType typ, fileId der)
187 ftentry t = (ftentry) dmalloc (sizeof (*t));
189 if (cstring_isUndefined (tn))
191 llbug (cstring_makeLiteral ("Undefined filename!"));
196 t->basename = cstring_undefined;
201 /* Don't set these until the basename is needed. */
209 ftentry_free (/*@only@*/ ftentry t)
211 cstring_free (t->fname);
212 cstring_free (t->basename);
216 /*@only@*/ /*@notnull@*/ fileTable
219 fileTable ft = (fileTable) dmalloc (sizeof (*ft));
222 ft->nspace = FTBASESIZE;
223 ft->elements = (ftentry *) dmalloc (FTBASESIZE * sizeof (*ft->elements));
224 ft->htable = cstringTable_create (FTHASHSIZE);
227 ft->nopenspace = FTBASESIZE;
228 ft->openelements = (foentry *) dmalloc (FTBASESIZE * sizeof (*ft->openelements));
234 fileTable_grow (fileTable ft)
239 llassert (fileTable_isDefined (ft));
241 ft->nspace = FTBASESIZE;
243 newent = (ftentry *) dmalloc ((ft->nentries + ft->nspace) * sizeof (*newent));
245 for (i = 0; i < ft->nentries; i++)
247 newent[i] = ft->elements[i];
250 sfree (ft->elements);
251 ft->elements = newent;
255 fileTable_growOpen (fileTable ft)
260 llassert (fileTable_isDefined (ft));
262 ft->nopenspace = FTBASESIZE;
264 newent = (foentry *) dmalloc ((ft->nopen + ft->nopenspace) * sizeof (*newent));
266 for (i = 0; i < ft->nopen; i++)
268 newent[i] = ft->openelements[i];
271 sfree (ft->openelements);
272 ft->openelements = newent;
276 fileTable_internAddEntry (fileTable ft, /*@only@*/ ftentry e)
278 llassert (fileTable_isDefined (ft));
285 DPRINTF (("Adding: %s", e->fname));
287 if (context_getFlag (FLG_CASEINSENSITIVEFILENAMES))
289 cstring sd = cstring_downcase (e->fname);
290 cstringTable_insert (ft->htable, sd, ft->nentries);
291 cstring_free (e->fname);
295 cstringTable_insert (ft->htable, e->fname, ft->nentries);
298 ft->elements[ft->nentries] = e;
301 return (ft->nentries - 1);
304 void fileTable_noDelete (fileTable ft, cstring name)
306 fileId fid = fileTable_lookup (ft, name);
308 if (fileId_isValid (fid))
310 llassert (fileTable_isDefined (ft));
311 ft->elements[fid]->ftype = FILE_NODELETE;
315 DPRINTF (("Invalid no delete: %s", name));
320 fileTable_addFilePrim (fileTable ft, /*@temp@*/ cstring name,
321 bool temp, fileType typ, fileId der)
324 cstring absname = osd_absolutePath (NULL, name);
325 int tindex = fileTable_getIndex (ft, absname);
327 /*@i534 fprintf (stderr, "Got abs path: %s", absname); */
328 llassert (ft != fileTable_undefined);
330 if (tindex != NOT_FOUND)
332 llcontbug (message ("fileTable_addFilePrim: duplicate entry: %q", absname));
337 ftentry e = ftentry_create (absname, temp, typ, der);
339 if (der == fileId_invalid)
341 llassert (cstring_isUndefined (e->basename));
343 e->basename = fileLib_removePathFree (fileLib_removeAnyExtension (absname));
344 e->fsystem = context_isSystemDir (absname);
347 ** evans 2002-03-15: change suggested by Jim Zelenka
348 ** support relative paths for system directories
353 e->fsystem = context_isSystemDir (name);
356 e->fspecial = context_isSpecialFile (absname);
360 cstring srcname = cstring_concatFree1 (fileLib_removeAnyExtension (absname),
362 fileId fid = fileTable_lookup (ft, srcname);
363 cstring_free (srcname);
365 if (fileId_isValid (fid))
367 fileId derid = ft->elements[fid]->fder;
369 ft->elements[fid]->fspecial = TRUE;
371 if (fileId_isValid (derid))
373 ft->elements[derid]->fspecial = TRUE;
380 ftentry de = ft->elements[der];
382 llassert (cstring_isUndefined (e->basename));
383 e->basename = cstring_copy (de->basename);
384 e->fsystem = de->fsystem;
385 e->fspecial = de->fspecial;
388 return (fileTable_internAddEntry (ft, e));
393 fileTable_addFile (fileTable ft, cstring name)
395 return (fileTable_addFilePrim (ft, name, FALSE, FILE_NORMAL, fileId_invalid));
399 fileTable_addFileOnly (fileTable ft, /*@only@*/ cstring name)
401 fileId res = fileTable_addFilePrim (ft, name, FALSE, FILE_NORMAL, fileId_invalid);
407 fileTable_addHeaderFile (fileTable ft, cstring name)
410 res = fileTable_addFilePrim (ft, name, FALSE, FILE_HEADER, fileId_invalid);
416 fileTable_addStreamFile (fileTable ft, FILE *fstream, cstring name)
418 fileTable_addOpen (ft, fstream, cstring_copy (name));
422 fileTable_isHeader (fileTable ft, fileId fid)
424 if (fileId_isInvalid (fid))
429 llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid));
430 return (ft->elements[fid]->ftype == FILE_HEADER);
434 fileTable_isSystemFile (fileTable ft, fileId fid)
436 if (fileId_isInvalid (fid))
441 llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid));
442 return (ft->elements[fid]->fsystem);
446 fileTable_isXHFile (fileTable ft, fileId fid)
448 if (fileId_isInvalid (fid))
453 if (!(fileTable_isDefined (ft) && fileTable_inRange (ft, fid)))
455 llcontbug (message ("Bad file table or id: %s %d", bool_unparse (fileTable_isDefined (ft)), fid));
460 return (ft->elements[fid]->ftype == FILE_XH);
465 fileTable_isSpecialFile (fileTable ft, fileId fid)
467 if (fileId_isInvalid (fid))
472 llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid));
473 return (ft->elements[fid]->fspecial);
477 fileTable_addLibraryFile (fileTable ft, cstring name)
479 return (fileTable_addFilePrim (ft, name, FALSE, FILE_HEADER, fileId_invalid));
483 fileTable_addXHFile (fileTable ft, cstring name)
485 return (fileTable_addFilePrim (ft, name, FALSE, FILE_XH, fileId_invalid));
490 fileTable_addImportFile (fileTable ft, cstring name)
492 return (fileTable_addFilePrim (ft, name, FALSE, FILE_HEADER, fileId_invalid));
496 fileTable_addLCLFile (fileTable ft, cstring name)
498 return (fileTable_addFilePrim (ft, name, FALSE, FILE_HEADER, fileId_invalid));
503 static int tmpcounter = 0;
507 fileTable_addMacrosFile (fileTable ft)
510 makeTempName (context_tmpdir (), cstring_makeLiteralTemp ("lmx"),
511 cstring_makeLiteralTemp (".llm"));
512 fileId res = fileTable_addFilePrim (ft, newname, TRUE, FILE_MACROS, fileId_invalid);
513 cstring_free (newname);
518 fileTable_addMetastateFile (fileTable ft, cstring name)
520 return (fileTable_addFilePrim (ft, name, FALSE, FILE_METASTATE, fileId_invalid));
524 fileTable_addCTempFile (fileTable ft, fileId fid)
527 makeTempName (context_tmpdir (), cstring_makeLiteralTemp ("cl"),
531 DPRINTF (("tmp dir: %s", context_tmpdir ()));
532 DPRINTF (("new name: %s", newname));
534 llassert (fileTable_isDefined (ft));
536 if (!fileId_isValid (ft->elements[fid]->fder))
538 if (fileTable_isXHFile (ft, fid))
540 res = fileTable_addFilePrim (ft, newname, TRUE, FILE_XH, fid);
544 res = fileTable_addFilePrim (ft, newname, TRUE, FILE_NORMAL, fid);
549 if (fileTable_isXHFile (ft, fid))
551 res = fileTable_addFilePrim (ft, newname, TRUE, FILE_XH,
552 ft->elements[fid]->fder);
556 res = fileTable_addFilePrim (ft, newname, TRUE, FILE_NORMAL,
557 ft->elements[fid]->fder);
561 DPRINTF (("Added file: %s", fileTable_fileName (res)));
562 cstring_free (newname);
568 fileTable_addltemp (fileTable ft)
570 cstring newname = makeTempName (context_tmpdir (),
571 cstring_makeLiteralTemp ("ls"),
572 cstring_makeLiteralTemp (".lsl"));
575 if (cstring_hasNonAlphaNumBar (newname))
577 char *lastpath = (char *)NULL;
583 ("Operating system generates tmp filename containing invalid charater: %s",
585 lldiagmsg (cstring_makeLiteral
586 ("Try cleaning up the tmp directory. Attempting to continue."));
590 llassert (cstring_isDefined (newname));
591 lastpath = strrchr (newname, CONNECTCHAR); /* get the directory */
592 llassert (lastpath != NULL);
595 newname = message ("%q%hlsl%d.lsl",
599 /*@noaccess cstring@*/
604 ** this is kind of yucky...need to make the result of cstring_fromChars
605 ** refer to the same storage as its argument. Of course, this loses,
606 ** since cstring is abstract. Should make it an only?
609 ret = fileTable_addFilePrim (ft, newname, TRUE, FILE_LSLTEMP, fileId_invalid);
610 cstring_free (newname);
616 fileTable_exists (fileTable ft, cstring s)
618 int tindex = fileTable_getIndex (ft, s);
620 if (tindex == NOT_FOUND)
622 DPRINTF (("Not found: %s", s));
632 fileTable_lookup (fileTable ft, cstring s)
634 int tindex = fileTable_getIndex (ft, s);
636 if (tindex == NOT_FOUND)
638 return fileId_invalid;
647 ** This is pretty awkward --- when we find the real path of
648 ** a .xh file, we may need to change the recorded name. [Sigh]
652 fileTable_setFilePath (fileTable ft, fileId fid, cstring path)
654 llassert (fileId_isValid (fid));
655 llassert (fileTable_isDefined (ft));
656 /* Need to put new string in hash table */
657 cstringTable_insert (ft->htable, cstring_copy (path), fid);
658 ft->elements[fid]->fname = cstring_copy (path);
662 fileTable_lookupBase (fileTable ft, cstring base)
666 if (context_getFlag (FLG_CASEINSENSITIVEFILENAMES))
668 cstring dbase = cstring_downcase (base);
669 tindex = fileTable_getIndex (ft, dbase);
670 cstring_free (dbase);
674 tindex = fileTable_getIndex (ft, base);
677 if (tindex == NOT_FOUND)
679 return fileId_invalid;
685 llassert (fileTable_isDefined (ft));
687 der = ft->elements[tindex]->fder;
689 if (!fileId_isValid (der))
699 fileTable_getName (fileTable ft, fileId fid)
701 if (!fileId_isValid (fid))
704 (message ("fileTable_getName: called with invalid type id: %d", fid));
705 return cstring_makeLiteralTemp ("<invalid>");
708 llassert (fileTable_isDefined (ft));
709 return (ft->elements[fid]->fname);
713 fileTable_getRootName (fileTable ft, fileId fid)
717 if (!fileId_isValid (fid))
719 llcontbug (message ("fileTable_getName: called with invalid id: %d", fid));
720 return cstring_makeLiteralTemp ("<invalid>");
723 if (!fileTable_isDefined (ft))
725 return cstring_makeLiteralTemp ("<no file table>");
728 fder = ft->elements[fid]->fder;
730 if (fileId_isValid (fder))
732 return (ft->elements[fder]->fname);
736 return (ft->elements[fid]->fname);
741 fileTable_getNameBase (fileTable ft, fileId fid)
743 if (!fileId_isValid (fid))
745 llcontbug (message ("fileTable_getName: called with invalid id: %d", fid));
746 return cstring_makeLiteralTemp ("<invalid>");
749 if (!fileTable_isDefined (ft))
751 return cstring_makeLiteralTemp ("<no file table>");
754 return (ft->elements[fid]->basename);
758 fileTable_sameBase (fileTable ft, fileId f1, fileId f2)
762 if (!fileId_isValid (f1))
767 if (!fileId_isValid (f2))
772 llassert (fileTable_isDefined (ft));
779 fd1 = ft->elements[f1]->fder;
781 if (!fileId_isValid (fd1))
786 fd2 = ft->elements[f2]->fder;
789 if (!fileId_isValid (fd2))
798 fileTable_cleanup (fileTable ft)
804 llassert (fileTable_isDefined (ft));
806 msg = ((ft->nentries > 40) && context_getFlag (FLG_SHOWSCAN));
807 skip = ft->nentries / 10;
811 (void) fflush (g_warningstream);
812 displayScanOpen (cstring_makeLiteral ("< cleaning"));
815 for (i = 0; i < ft->nentries; i++)
817 ftentry fe = ft->elements[i];
821 /* let's be real careful now, hon! */
824 ** Make sure it is really a derived file
828 if (fe->ftype == FILE_LSLTEMP || fe->ftype == FILE_NODELETE)
830 ; /* already removed */
832 else if (fileId_isValid (fe->fder))
834 /*@i423 this should use close (fd) also... */
835 (void) osd_unlink (fe->fname);
837 else if (fe->ftype == FILE_MACROS)
839 (void) osd_unlink (fe->fname);
843 llbug (message ("Temporary file is not derivative: %s "
844 "(not deleted)", fe->fname));
852 if (msg && ((i % skip) == 0))
854 displayScan (cstring_makeLiteral (i == 0 ? " " : "."));
865 fileTable_free (/*@only@*/ fileTable f)
869 if (f == (fileTable)NULL)
874 while ( i < f->nentries )
876 ftentry_free (f->elements[i]);
880 cstringTable_free (f->htable);
886 ** unique temp filename are constructed from <dir><pre><pid><msg>.<suf>
887 ** requires: <dir> must end in '/'
890 static void nextMsg (char *msg)
920 llassertprint (FALSE, ("nextMsg: out of unique names!!!"));
928 static /*@only@*/ cstring makeTempName (cstring dir, cstring pre, cstring suf)
931 static /*@owned@*/ char *msg = NULL;
932 static /*@only@*/ cstring pidname = NULL;
936 llassert (cstring_length (pre) <= 3);
939 ** We limit the temp name to 8 characters:
947 msg = mstring_copy ("AAA"); /* there are 26^3 temp names */
952 /*@+matchanyintegral@*/
954 /*@=matchanyintegral@*/
957 if (cstring_isUndefined (pidname))
959 pidname = message ("%d", pid % 100);
962 maxlen = (cstring_length (dir) + cstring_length (pre) + mstring_length (msg)
963 + cstring_length (pidname) + cstring_length (suf) + 2);
965 DPRINTF (("Dir: %s / %s / %s / %s / %s",
966 dir, pre, pidname, msg, suf));
968 smsg = message ("%s%s%s%s%s", dir, pre, pidname, cstring_fromChars (msg), suf);
971 DPRINTF (("Trying: %s", smsg));
973 while (osd_fileExists (smsg))
976 smsg = message ("%s%s%s%s%s", dir, pre, pidname, cstring_fromChars (msg), suf);
984 foentry_create (/*@exposed@*/ FILE *f, /*@only@*/ cstring fname)
986 foentry t = (foentry) dmalloc (sizeof (*t));
993 foentry_free (/*@only@*/ foentry foe)
995 cstring_free (foe->fname);
1000 fileTable_addOpen (fileTable ft, /*@observer@*/ FILE *f, /*@only@*/ cstring fname)
1002 llassert (fileTable_isDefined (ft));
1004 if (ft->nopenspace <= 0)
1006 fileTable_growOpen (ft);
1010 ft->openelements[ft->nopen] = foentry_create (f, fname);
1014 FILE *fileTable_createFile (fileTable ft, cstring fname)
1016 int fdesc = open (cstring_toCharsSafe (fname), O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, S_IRUSR | S_IWUSR);
1020 osd_setTempError ();
1021 llfatalerror (message ("Temporary file for "
1022 "pre-processor output already exists. Trying to "
1026 /*@notreached@*/ return NULL;
1030 FILE *res = fdopen (fdesc, "w");
1034 fileTable_addOpen (ft, res, cstring_copy (fname));
1035 DPRINTF (("Opening file: %s / %p", fname, res));
1039 DPRINTF (("Error opening: %s", fname));
1046 FILE *fileTable_createMacrosFile (fileTable ft, cstring fname)
1048 int fdesc = open (cstring_toCharsSafe (fname), O_RDWR | O_CREAT | O_TRUNC | O_EXCL, S_IRUSR | S_IWUSR);
1052 osd_setTempError ();
1053 llfatalerror (message ("Temporary file for "
1054 "pre-processor output already exists. Trying to "
1058 /*@notreached@*/ return NULL;
1062 FILE *res = fdopen (fdesc, "w+");
1066 fileTable_addOpen (ft, res, cstring_copy (fname));
1067 DPRINTF (("Opening file: %s / %p", fname, res));
1071 DPRINTF (("Error opening: %s", fname));
1078 FILE *fileTable_openReadFile (fileTable ft, cstring fname)
1080 FILE *res = fopen (cstring_toCharsSafe (fname), "r");
1084 fileTable_addOpen (ft, res, cstring_copy (fname));
1085 DPRINTF (("Opening read file: %s / %p", fname, res));
1089 DPRINTF (("Cannot open read file: %s", fname));
1096 ** Allows overwriting
1099 FILE *fileTable_openWriteFile (fileTable ft, cstring fname)
1101 FILE *res = fopen (cstring_toCharsSafe (fname), "w");
1104 fileTable_addOpen (ft, res, cstring_copy (fname));
1105 DPRINTF (("Opening file: %s / %p", fname, res));
1111 FILE *fileTable_openWriteUpdateFile (fileTable ft, cstring fname)
1113 FILE *res = fopen (cstring_toCharsSafe (fname), "w+");
1116 fileTable_addOpen (ft, res, cstring_copy (fname));
1117 DPRINTF (("Opening file: %s / %p", fname, res));
1123 bool fileTable_closeFile (fileTable ft, FILE *f)
1125 bool foundit = FALSE;
1128 llassert (fileTable_isDefined (ft));
1130 DPRINTF (("Closing file: %p", f));
1132 for (i = 0; i < ft->nopen; i++)
1134 if (ft->openelements[i]->f == f)
1136 DPRINTF (("Closing file: %p = %s", f, ft->openelements[i]->fname));
1138 if (i == ft->nopen - 1)
1140 foentry_free (ft->openelements[i]);
1141 ft->openelements[i] = NULL;
1145 foentry_free (ft->openelements[i]);
1146 ft->openelements[i] = ft->openelements[ft->nopen - 1];
1147 ft->openelements[ft->nopen - 1] = NULL;
1158 return (fclose (f) == 0);
1161 void fileTable_closeAll (fileTable ft)
1165 llassert (fileTable_isDefined (ft));
1167 for (i = 0; i < ft->nopen; i++)
1170 lldiagmsg (message ("Unclosed file at exit: %s", ft->openelements[i]->fname));
1173 if (ft->openelements[i]->f != NULL)
1175 (void) fclose (ft->openelements[i]->f); /* No check - cleaning up after errors */
1178 ft->openelements[i]->f = NULL;
1179 foentry_free (ft->openelements[i]);
1180 ft->openelements[i] = NULL;
1183 ft->nopenspace += ft->nopen;