]> andersk Git - splint.git/blame - src/fileTable.c
Added the code for setBufferSize and setStringLegnth annotations.
[splint.git] / src / fileTable.c
CommitLineData
885824d3 1/*
2** LCLint - annotation-assisted static program checker
3** Copyright (C) 1994-2000 University of Virginia,
4** Massachusetts Institute of Technology
5**
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.
10**
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.
15**
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.
19**
20** For information on lclint: lclint-request@cs.virginia.edu
21** To report a bug: lclint-bug@cs.virginia.edu
22** For more information: http://lclint.cs.virginia.edu
23*/
24/*
25** fileTable.c
26**
27** replaces filenamemap.c
28** based (loosely) on typeTable.c
29**
30** entries in the fileTable are:
31**
32** name - name of the file
33** type - kind of file (a temp file to be deleted?)
34** link - derived from this file
35**
36*/
37/*
38 * Herbert 04/1997:
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.
45 */
46
47# include "lclintMacros.nf"
48# include "llbasic.h"
49# include "osd.h"
50# include "llmain.h"
51# include "portab.h"
52# if defined(__IBMC__) && defined(OS2)
53# include <process.h>
54# define getpid _getpid
55# endif
56
57/*@access fileId*/
58
59static bool fileTable_inRange (fileTable ft, fileId fid) /*@*/
60{
61 return (fileTable_isDefined (ft) && (fid >= 0) && (fid < ft->nentries));
62}
63
64static fileId fileTable_internAddEntry (fileTable p_ft, /*@only@*/ ftentry p_e)
65 /*@modifies p_ft@*/ ;
66static /*@only@*/ char *makeTempName (char *p_dir, char *p_pre, char *p_suf);
67
68static /*@only@*/ cstring
69fileType_unparse (fileType ft)
70{
71 switch (ft)
72 {
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_MACROS: return cstring_makeLiteral ("macros");
78 }
79
80 BADEXIT;
81}
82
83static int
84fileTable_getIndex (fileTable ft, cstring s)
85{
86 if (ft == NULL) return NOT_FOUND;
87 return (hashTable_lookup (ft->htable, s));
88}
89
90/*@only@*/ cstring
91fileTable_unparse (fileTable ft)
92{
93 cstring s = cstring_undefined;
94 int i;
95
96 if (fileTable_isUndefined (ft))
97 {
98 return (cstring_makeLiteral ("<fileTable undefined>"));
99 }
100
101 for (i = 0; i < ft->nentries; i++)
102 {
103 if (fileId_isValid (ft->elements[i]->fder))
104 {
105 s = message ("%s\n[%d] %s %q %d (%s)",
106 s, i,
107 ft->elements[i]->fname,
108 fileType_unparse (ft->elements[i]->ftype),
109 ft->elements[i]->fder,
110 ft->elements[ft->elements[i]->fder]->fname);
111 }
112 else
113 {
114 s = message ("%s\n[%d] %s %q", s, i, ft->elements[i]->fname,
115 fileType_unparse (ft->elements[i]->ftype));
116 }
117 }
118
119 return s;
120}
121
122void fileTable_printTemps (fileTable ft)
123{
124 if (fileTable_isDefined (ft))
125 {
126 int i;
127
128 for (i = 0; i < ft->nentries; i++)
129 {
130 if (ft->elements[i]->ftemp)
131 {
132 if (fileId_isValid (ft->elements[i]->fder))
133 {
134 fprintf (stderr, " %s:1\n\t%s:1\n",
135 cstring_toCharsSafe (ft->elements[ft->elements[i]->fder]->fname),
136 cstring_toCharsSafe (ft->elements[i]->fname));
137 }
138 else
139 {
140 fprintf (stderr, "[no file]\n\t%s:1\n",
141 cstring_toCharsSafe (ft->elements[i]->fname));
142 }
143 }
144 }
145 }
146}
147
148/*
149** loads in fileTable from fileTable_dump
150*/
151
152static /*@notnull@*/ ftentry
153ftentry_create (/*@keep@*/ cstring tn, bool temp, fileType typ, fileId der)
154{
155 ftentry t = (ftentry) dmalloc (sizeof (*t));
156
157 if (cstring_isUndefined (tn))
158 {
159 llbug (cstring_makeLiteral ("Undefined filename!"));
160 }
161
162 t->fname = tn;
163
164 t->basename = cstring_undefined;
165 t->ftemp = temp;
166 t->ftype = typ;
167 t->fder = der;
168
169 /* Don't set these until the basename is needed. */
170 t->fsystem = FALSE;
171 t->fspecial = FALSE;
172
173 return t;
174}
175
176static void
177ftentry_free (/*@only@*/ ftentry t)
178{
179 cstring_free (t->fname);
180 cstring_free (t->basename);
181 sfree (t);
182}
183
184/*@only@*/ /*@notnull@*/ fileTable
185fileTable_create ()
186{
187 fileTable ft = (fileTable) dmalloc (sizeof (*ft));
188
189 ft->nentries = 0;
190 ft->nspace = FTBASESIZE;
191 ft->elements = (ftentry *) dmalloc (FTBASESIZE * sizeof (*ft->elements));
192 ft->htable = hashTable_create (FTHASHSIZE);
193
194 return (ft);
195}
196
197static void
198fileTable_grow (fileTable ft)
199{
200 int i;
201 ftentry *newent;
202
203 llassert (fileTable_isDefined (ft));
204
205 ft->nspace = FTBASESIZE;
206
207 newent = (ftentry *) dmalloc ((ft->nentries + ft->nspace) * sizeof (*newent));
208
209 for (i = 0; i < ft->nentries; i++)
210 {
211 newent[i] = ft->elements[i];
212 }
213
214 sfree (ft->elements);
215 ft->elements = newent;
216}
217
218static fileId
219fileTable_internAddEntry (fileTable ft, /*@only@*/ ftentry e)
220{
221 llassert (fileTable_isDefined (ft));
222
223 if (ft->nspace <= 0)
224 fileTable_grow (ft);
225
226 ft->nspace--;
227
228 hashTable_insert (ft->htable, e->fname, ft->nentries);
229 ft->elements[ft->nentries] = e;
230
231 ft->nentries++;
232
233 return (ft->nentries - 1);
234}
235
236void fileTable_noDelete (fileTable ft, cstring name)
237{
238 fileId fid = fileTable_lookup (ft, name);
239
240 if (fileId_isValid (fid)) {
241 llassert (fileTable_isDefined (ft));
242
243 ft->elements[fid]->ftype = FILE_NODELETE;
244 }
245}
246
247static fileId
248fileTable_addFilePrim (fileTable ft, /*@only@*/ cstring name,
249 bool temp, fileType typ, fileId der)
250 /*@modifies ft@*/
251{
252 int tindex = fileTable_getIndex (ft, name);
253
254 llassert (ft != fileTable_undefined);
255
256 if (tindex != NOT_FOUND)
257 {
258 llcontbug (message ("fileTable_addFilePrim: duplicate entry: %q", name));
259
260 return tindex;
261 }
262 else
263 {
264 ftentry e = ftentry_create (name, temp, typ, der);
265
266 if (der == fileId_invalid)
267 {
268 llassert (cstring_isUndefined (e->basename));
269
270 e->basename = cstring_fromChars
271 (removePathFree (removeAnyExtension
272 (cstring_toCharsSafe (name))));
273 e->fsystem = context_isSystemDir (name);
274 e->fspecial = context_isSpecialFile (name);
275
276 if (e->fspecial)
277 {
278 cstring srcname = cstring_concatFree (cstring_fromChars (removeAnyExtension (cstring_toCharsSafe (name))), cstring_makeLiteral (".c"));
279 fileId fid = fileTable_lookup (ft, srcname);
280
281 cstring_free (srcname);
282
283 if (fileId_isValid (fid))
284 {
285 fileId derid = ft->elements[fid]->fder;
286
287 ft->elements[fid]->fspecial = TRUE;
288
289 if (fileId_isValid (derid))
290 {
291 ft->elements[derid]->fspecial = TRUE;
292 }
293 }
294 }
295 }
296 else
297 {
298 ftentry de = ft->elements[der];
299
300 llassert (cstring_isUndefined (e->basename));
301 e->basename = cstring_copy (de->basename);
302 e->fsystem = de->fsystem;
303 e->fspecial = de->fspecial;
304 }
305
306 return (fileTable_internAddEntry (ft, e));
307 }
308}
309
310fileId
311fileTable_addFile (fileTable ft, cstring name)
312{
313 /* while (*name == '.' && *(name + 1) == '/') name += 2; */
314 return (fileTable_addFilePrim (ft, cstring_copy (name),
315 FALSE, FILE_NORMAL, fileId_invalid));
316}
317
318fileId
319fileTable_addFileOnly (fileTable ft, /*@only@*/ cstring name)
320{
321 return (fileTable_addFilePrim (ft, name, FALSE, FILE_NORMAL, fileId_invalid));
322}
323
324fileId
325fileTable_addHeaderFile (fileTable ft, cstring name)
326{
327 DPRINTF (("Add header: %s", name));
328 return (fileTable_addFilePrim (ft, cstring_copy (name), FALSE,
329 FILE_HEADER, fileId_invalid));
330}
331
332bool
333fileTable_isHeader (fileTable ft, fileId fid)
334{
335 if (fileId_isInvalid (fid))
336 {
337 return FALSE;
338 }
339
340 llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid));
341 return (ft->elements[fid]->ftype == FILE_HEADER);
342}
343
344bool
345fileTable_isSystemFile (fileTable ft, fileId fid)
346{
347 if (fileId_isInvalid (fid))
348 {
349 return FALSE;
350 }
351
352 llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid));
353 return (ft->elements[fid]->fsystem);
354}
355
356bool
357fileTable_isSpecialFile (fileTable ft, fileId fid)
358{
359 if (fileId_isInvalid (fid))
360 {
361 return FALSE;
362 }
363
364 llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid));
365 return (ft->elements[fid]->fspecial);
366}
367
368fileId
369fileTable_addLibraryFile (fileTable ft, cstring name)
370{
371 return (fileTable_addFilePrim (ft, cstring_copy (name),
372 FALSE, FILE_HEADER, fileId_invalid));
373}
374
375# ifndef NOLCL
376fileId
377fileTable_addImportFile (fileTable ft, cstring name)
378{
379 return (fileTable_addFilePrim (ft, cstring_copy (name),
380 FALSE, FILE_HEADER, fileId_invalid));
381}
382
383fileId
384fileTable_addLCLFile (fileTable ft, cstring name)
385{
386 return (fileTable_addFilePrim (ft, cstring_copy (name),
387 FALSE, FILE_HEADER, fileId_invalid));
388}
389# endif
390
391# ifndef NOLCL
392static int tmpcounter = 0;
393# endif
394
395fileId
396fileTable_addMacrosFile (fileTable ft)
397{
398 cstring newname = cstring_fromChars
399 (makeTempName (cstring_toCharsSafe (context_tmpdir ()), "lmx", ".llm"));
400
401 return (fileTable_addFilePrim (ft, newname, TRUE, FILE_MACROS, fileId_invalid));
402}
403
404fileId
405fileTable_addCTempFile (fileTable ft, fileId fid)
406{
407# if FALSE
408 /* Can't control output file name for cl preprocessor */
409 cstring newname = cstring_concatChars (removeAnyExtension (fileName (fid)), ".i");
410# else
411 cstring newname = cstring_fromChars
412 (makeTempName (cstring_toCharsSafe (context_tmpdir ()), "cl", ".c"));
413# endif
414
415 llassert (fileTable_isDefined (ft));
416
417 if (!fileId_isValid (ft->elements[fid]->fder))
418 {
419 return (fileTable_addFilePrim (ft, newname, TRUE, FILE_NORMAL, fid));
420 }
421 else
422 {
423 return (fileTable_addFilePrim (ft, newname, TRUE, FILE_NORMAL,
424 ft->elements[fid]->fder));
425 }
426}
427
428# ifndef NOLCL
429fileId
430fileTable_addltemp (fileTable ft)
431{
432 char *newname = makeTempName (cstring_toCharsSafe (context_tmpdir ()),
433 "ls", ".lsl");
434 char *onewname;
435 fileId ret;
436
437 if (cstring_hasNonAlphaNumBar (cstring_fromChars (newname)))
438 {
439 char *lastpath = (char *)NULL;
440
441 if (tmpcounter == 0)
442 {
443 lldiagmsg
444 (message
445 ("Operating system generates tmp filename containing invalid charater: %s",
446 cstring_fromChars (newname)));
447 lldiagmsg (cstring_makeLiteral
448 ("Try cleaning up the tmp directory. Attempting to continue."));
449 }
450
451 lastpath = strrchr (newname, CONNECTCHAR); /* get the directory */
452 llassert (lastpath != NULL);
453 *lastpath = '\0';
454
455 onewname = newname;
456 newname = cstring_toCharsSafe (message ("%s%hlsl%d.lsl",
457 cstring_fromChars (newname),
458 CONNECTCHAR,
459 tmpcounter));
460 sfree (onewname);
461 tmpcounter++;
462 }
463
464 /*
465 ** this is kind of yucky...need to make the result of cstring_fromChars
466 ** refer to the same storage as its argument. Of course, this loses,
467 ** since cstring is abstract. Should make it an only?
468 */
469
470 ret = fileTable_addFilePrim (ft, cstring_copy (cstring_fromChars (newname)),
471 TRUE, FILE_LSLTEMP, fileId_invalid);
472 sfree (newname);
473 return (ret);
474}
475# endif
476
477bool
478fileTable_exists (fileTable ft, cstring s)
479{
480 int tindex = fileTable_getIndex (ft, s);
481
482 if (tindex == NOT_FOUND)
483 return FALSE;
484 else
485 return TRUE;
486}
487
488fileId
489fileTable_lookup (fileTable ft, cstring s)
490{
491 int tindex = fileTable_getIndex (ft, s);
492
493 if (tindex == NOT_FOUND)
494 {
495 return fileId_invalid;
496 }
497 else
498 {
499 return tindex;
500 }
501}
502
503fileId
504fileTable_lookupBase (fileTable ft, cstring base)
505{
506 int tindex = fileTable_getIndex (ft, base);
507
508 if (tindex == NOT_FOUND)
509 {
510
511 return fileId_invalid;
512 }
513 else
514 {
515 fileId der;
516
517 llassert (fileTable_isDefined (ft));
518
519 der = ft->elements[tindex]->fder;
520
521 if (!fileId_isValid (der))
522 {
523 der = tindex;
524 }
525
526 return der;
527 }
528}
529
530cstring
531fileTable_getName (fileTable ft, fileId fid)
532{
533 if (!fileId_isValid (fid))
534 {
535 llcontbug
536 (message ("fileTable_getName: called with invalid type id: %d", fid));
537 return cstring_makeLiteralTemp ("<invalid>");
538 }
539
540 llassert (fileTable_isDefined (ft));
541 return (ft->elements[fid]->fname);
542}
543
544cstring
545fileTable_getRootName (fileTable ft, fileId fid)
546{
547 fileId fder;
548
549 if (!fileId_isValid (fid))
550 {
551 llcontbug (message ("fileTable_getName: called with invalid id: %d", fid));
552 return cstring_makeLiteralTemp ("<invalid>");
553 }
554
555 if (!fileTable_isDefined (ft))
556 {
557 return cstring_makeLiteralTemp ("<no file table>");
558 }
559
560 fder = ft->elements[fid]->fder;
561
562 if (fileId_isValid (fder))
563 {
564 return (ft->elements[fder]->fname);
565 }
566 else
567 {
568 return (ft->elements[fid]->fname);
569 }
570}
571
572cstring
573fileTable_getNameBase (fileTable ft, fileId fid)
574{
575 if (!fileId_isValid (fid))
576 {
577 llcontbug (message ("fileTable_getName: called with invalid id: %d", fid));
578 return cstring_makeLiteralTemp ("<invalid>");
579 }
580
581 if (!fileTable_isDefined (ft))
582 {
583 return cstring_makeLiteralTemp ("<no file table>");
584 }
585
586 return (ft->elements[fid]->basename);
587}
588
589bool
590fileTable_sameBase (fileTable ft, fileId f1, fileId f2)
591{
592 fileId fd1, fd2;
593
594 if (!fileId_isValid (f1))
595 {
596 return FALSE;
597 }
598
599 if (!fileId_isValid (f2))
600 {
601 return FALSE;
602 }
603
604 llassert (fileTable_isDefined (ft));
605
606 if (f1 == f2)
607 {
608 return TRUE;
609 }
610
611 fd1 = ft->elements[f1]->fder;
612
613 if (!fileId_isValid (fd1))
614 {
615 fd1 = f1;
616 }
617
618 fd2 = ft->elements[f2]->fder;
619
620
621 if (!fileId_isValid (fd2))
622 {
623 fd2 = f2;
624 }
625
626 return (fd1 == fd2);
627}
628
629void
630fileTable_cleanup (fileTable ft)
631{
632 int i;
633 bool msg;
634 int skip;
635
636 llassert (fileTable_isDefined (ft));
637
638 msg = ((ft->nentries > 40) && context_getFlag (FLG_SHOWSCAN));
639 skip = ft->nentries / 10;
640
641 if (msg)
642 {
643 (void) fflush (g_msgstream);
644 fprintf (stderr, "< cleaning");
645 }
646
647 for (i = 0; i < ft->nentries; i++)
648 {
649 ftentry fe = ft->elements[i];
650
651 if (fe->ftemp)
652 {
653 /* let's be real careful now, hon! */
654
655 /*
656 ** Make sure it is really a derived file
657 */
658
659 if (fe->ftype == FILE_LSLTEMP || fe->ftype == FILE_NODELETE)
660 {
661 ; /* already removed */
662 }
663 else if (fileId_isValid (fe->fder))
664 {
665 (void) osd_unlink (cstring_toCharsSafe (fe->fname));
666 }
667 else if (fe->ftype == FILE_MACROS)
668 {
669 (void) osd_unlink (cstring_toCharsSafe (fe->fname));
670 }
671 else
672 {
673 llbug (message ("Temporary file is not derivative: %s "
674 "(not deleted)", fe->fname));
675 }
676 }
677 else
678 {
679 ;
680 }
681
682 if (msg && ((i % skip) == 0))
683 {
684 (void) fflush (g_msgstream);
685
686 if (i == 0) {
687 fprintf (stderr, " ");
688 } else {
689 fprintf (stderr, ".");
690 }
691
692 (void) fflush (stderr);
693 }
694 }
695
696 if (msg)
697 {
698 fprintf (stderr, " >\n");
699 }
700}
701
702void
703fileTable_free (/*@only@*/ fileTable f)
704{
705 int i = 0;
706
707 if (f == (fileTable)NULL)
708 {
709 return;
710 }
711
712 while ( i < f->nentries )
713 {
714 ftentry_free (f->elements[i]);
715 i++;
716 }
717
718 hashTable_free (f->htable);
719 sfree (f->elements);
720 sfree (f);
721}
722
723/*
724** unique temp filename are constructed from <dir><pre><pid><msg>.<suf>
725** requires: <dir> must end in '/'
726*/
727
728static void nextMsg (char *msg)
729{
730 /*@+charint@*/
731 if (msg[0] < 'Z')
732 {
733 msg[0]++;
734 }
735 else
736 {
737 msg[0] = 'A';
738 if (msg[1] < 'Z')
739 {
740 msg[1]++;
741 }
742 else
743 {
744 msg[1] = 'A';
745 if (msg[2] < 'Z')
746 {
747 msg[2]++;
748 }
749 else
750 {
751 msg[2] = 'A';
752 if (msg[3] < 'Z')
753 {
754 msg[3]++;
755 }
756 else
757 {
758 llassertprint (FALSE, ("nextMsg: out of unique names!!!"));
759 }
760 }
761 }
762 }
763 /*@-charint@*/
764}
765
766static /*@only@*/ char *makeTempName (char *dir, char *pre, char *suf)
767{
768 static int pid = 0;
769 static /*@owned@*/ char *msg = NULL;
770 static /*@only@*/ char *pidname = NULL;
771 size_t maxlen;
772 char *buf;
773
774 llassert (strlen (pre) <= 3);
775
776 /*
777 ** We limit the temp name to 8 characters:
778 ** pre: 3 or less
779 ** msg: 3
780 ** pid: 2 (% 100)
781 */
782
783 if (msg == NULL)
784 {
785 msg = mstring_copy ("AAA"); /* there are 26^3 temp names */
786 }
787
788 if (pid == 0)
789 {
790 /*@+matchanyintegral@*/
791 pid = osd_getPid ();
792 /*@=matchanyintegral@*/
793 }
794
795 if (pidname == NULL)
796 {
797 pidname = cstring_toCharsSafe (message ("%d", pid % 100));
798 }
799 else
800 {
801 pidname = mstring_createEmpty ();
802 }
803
804 maxlen = (strlen (dir) + strlen (pre) + strlen (msg)
805 + strlen (pidname) + strlen (suf) + 2);
806
807 buf = mstring_create (size_toInt (maxlen));
808
809 sprintf (buf, "%s%s%s%s%s", dir, pre, pidname, msg, suf);
810 nextMsg (msg);
811
812 while (osd_fileExists (buf))
813 {
814 sprintf (buf, "%s%s%s%s%s", dir, pre, pidname, msg, suf);
815 nextMsg (msg);
816 }
817
818 return buf;
819}
820
821
822
This page took 0.209595 seconds and 5 git commands to generate.