]>
Commit | Line | Data |
---|---|---|
616915dd | 1 | /* |
2 | ** LCLint - annotation-assisted static program checker | |
28bf4b0b | 3 | ** Copyright (C) 1994-2001 University of Virginia, |
616915dd | 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 | ||
59 | static bool fileTable_inRange (fileTable ft, fileId fid) /*@*/ | |
60 | { | |
61 | return (fileTable_isDefined (ft) && (fid >= 0) && (fid < ft->nentries)); | |
62 | } | |
63 | ||
64 | static fileId fileTable_internAddEntry (fileTable p_ft, /*@only@*/ ftentry p_e) | |
65 | /*@modifies p_ft@*/ ; | |
28bf4b0b | 66 | static /*@only@*/ cstring makeTempName (cstring p_dir, cstring p_pre, cstring p_suf); |
616915dd | 67 | |
68 | static /*@only@*/ cstring | |
69 | fileType_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"); | |
28bf4b0b | 77 | case FILE_XH: return cstring_makeLiteral ("xh"); |
616915dd | 78 | case FILE_MACROS: return cstring_makeLiteral ("macros"); |
28bf4b0b | 79 | case FILE_METASTATE: return cstring_makeLiteral ("metastate"); |
616915dd | 80 | } |
81 | ||
82 | BADEXIT; | |
83 | } | |
84 | ||
85 | static int | |
86 | fileTable_getIndex (fileTable ft, cstring s) | |
87 | { | |
88 | if (ft == NULL) return NOT_FOUND; | |
28bf4b0b | 89 | return (cstringTable_lookup (ft->htable, s)); |
616915dd | 90 | } |
91 | ||
92 | /*@only@*/ cstring | |
93 | fileTable_unparse (fileTable ft) | |
94 | { | |
95 | cstring s = cstring_undefined; | |
96 | int i; | |
97 | ||
98 | if (fileTable_isUndefined (ft)) | |
99 | { | |
100 | return (cstring_makeLiteral ("<fileTable undefined>")); | |
101 | } | |
102 | ||
103 | for (i = 0; i < ft->nentries; i++) | |
104 | { | |
105 | if (fileId_isValid (ft->elements[i]->fder)) | |
106 | { | |
107 | s = message ("%s\n[%d] %s %q %d (%s)", | |
108 | s, i, | |
109 | ft->elements[i]->fname, | |
110 | fileType_unparse (ft->elements[i]->ftype), | |
111 | ft->elements[i]->fder, | |
112 | ft->elements[ft->elements[i]->fder]->fname); | |
113 | } | |
114 | else | |
115 | { | |
116 | s = message ("%s\n[%d] %s %q", s, i, ft->elements[i]->fname, | |
117 | fileType_unparse (ft->elements[i]->ftype)); | |
118 | } | |
119 | } | |
120 | ||
121 | return s; | |
122 | } | |
123 | ||
124 | void fileTable_printTemps (fileTable ft) | |
125 | { | |
126 | if (fileTable_isDefined (ft)) | |
127 | { | |
128 | int i; | |
129 | ||
130 | for (i = 0; i < ft->nentries; i++) | |
131 | { | |
132 | if (ft->elements[i]->ftemp) | |
133 | { | |
134 | if (fileId_isValid (ft->elements[i]->fder)) | |
135 | { | |
136 | fprintf (stderr, " %s:1\n\t%s:1\n", | |
137 | cstring_toCharsSafe (ft->elements[ft->elements[i]->fder]->fname), | |
138 | cstring_toCharsSafe (ft->elements[i]->fname)); | |
139 | } | |
140 | else | |
141 | { | |
142 | fprintf (stderr, "[no file]\n\t%s:1\n", | |
143 | cstring_toCharsSafe (ft->elements[i]->fname)); | |
144 | } | |
145 | } | |
146 | } | |
147 | } | |
148 | } | |
149 | ||
150 | /* | |
151 | ** loads in fileTable from fileTable_dump | |
152 | */ | |
153 | ||
154 | static /*@notnull@*/ ftentry | |
155 | ftentry_create (/*@keep@*/ cstring tn, bool temp, fileType typ, fileId der) | |
156 | { | |
157 | ftentry t = (ftentry) dmalloc (sizeof (*t)); | |
158 | ||
159 | if (cstring_isUndefined (tn)) | |
160 | { | |
161 | llbug (cstring_makeLiteral ("Undefined filename!")); | |
162 | } | |
163 | ||
164 | t->fname = tn; | |
165 | ||
166 | t->basename = cstring_undefined; | |
167 | t->ftemp = temp; | |
168 | t->ftype = typ; | |
169 | t->fder = der; | |
170 | ||
171 | /* Don't set these until the basename is needed. */ | |
172 | t->fsystem = FALSE; | |
173 | t->fspecial = FALSE; | |
174 | ||
175 | return t; | |
176 | } | |
177 | ||
178 | static void | |
179 | ftentry_free (/*@only@*/ ftentry t) | |
180 | { | |
181 | cstring_free (t->fname); | |
182 | cstring_free (t->basename); | |
183 | sfree (t); | |
184 | } | |
185 | ||
186 | /*@only@*/ /*@notnull@*/ fileTable | |
187 | fileTable_create () | |
188 | { | |
189 | fileTable ft = (fileTable) dmalloc (sizeof (*ft)); | |
190 | ||
191 | ft->nentries = 0; | |
192 | ft->nspace = FTBASESIZE; | |
193 | ft->elements = (ftentry *) dmalloc (FTBASESIZE * sizeof (*ft->elements)); | |
28bf4b0b | 194 | ft->htable = cstringTable_create (FTHASHSIZE); |
616915dd | 195 | |
196 | return (ft); | |
197 | } | |
198 | ||
199 | static void | |
200 | fileTable_grow (fileTable ft) | |
201 | { | |
202 | int i; | |
203 | ftentry *newent; | |
204 | ||
205 | llassert (fileTable_isDefined (ft)); | |
206 | ||
207 | ft->nspace = FTBASESIZE; | |
208 | ||
209 | newent = (ftentry *) dmalloc ((ft->nentries + ft->nspace) * sizeof (*newent)); | |
210 | ||
211 | for (i = 0; i < ft->nentries; i++) | |
212 | { | |
213 | newent[i] = ft->elements[i]; | |
214 | } | |
215 | ||
216 | sfree (ft->elements); | |
217 | ft->elements = newent; | |
218 | } | |
219 | ||
220 | static fileId | |
221 | fileTable_internAddEntry (fileTable ft, /*@only@*/ ftentry e) | |
222 | { | |
223 | llassert (fileTable_isDefined (ft)); | |
224 | ||
225 | if (ft->nspace <= 0) | |
226 | fileTable_grow (ft); | |
227 | ||
228 | ft->nspace--; | |
229 | ||
28bf4b0b | 230 | cstringTable_insert (ft->htable, e->fname, ft->nentries); |
616915dd | 231 | ft->elements[ft->nentries] = e; |
232 | ||
233 | ft->nentries++; | |
234 | return (ft->nentries - 1); | |
235 | } | |
236 | ||
237 | void fileTable_noDelete (fileTable ft, cstring name) | |
238 | { | |
239 | fileId fid = fileTable_lookup (ft, name); | |
240 | ||
241 | if (fileId_isValid (fid)) { | |
242 | llassert (fileTable_isDefined (ft)); | |
243 | ||
244 | ft->elements[fid]->ftype = FILE_NODELETE; | |
245 | } | |
246 | } | |
247 | ||
248 | static fileId | |
249 | fileTable_addFilePrim (fileTable ft, /*@only@*/ cstring name, | |
250 | bool temp, fileType typ, fileId der) | |
251 | /*@modifies ft@*/ | |
252 | { | |
253 | int tindex = fileTable_getIndex (ft, name); | |
254 | ||
255 | llassert (ft != fileTable_undefined); | |
256 | ||
257 | if (tindex != NOT_FOUND) | |
258 | { | |
259 | llcontbug (message ("fileTable_addFilePrim: duplicate entry: %q", name)); | |
260 | ||
261 | return tindex; | |
262 | } | |
263 | else | |
264 | { | |
265 | ftentry e = ftentry_create (name, temp, typ, der); | |
266 | ||
267 | if (der == fileId_invalid) | |
268 | { | |
269 | llassert (cstring_isUndefined (e->basename)); | |
270 | ||
28bf4b0b | 271 | e->basename = fileLib_removePathFree (fileLib_removeAnyExtension (name)); |
616915dd | 272 | e->fsystem = context_isSystemDir (name); |
273 | e->fspecial = context_isSpecialFile (name); | |
274 | ||
275 | if (e->fspecial) | |
276 | { | |
28bf4b0b | 277 | cstring srcname = cstring_concatFree1 (fileLib_removeAnyExtension (name), |
278 | C_EXTENSION); | |
616915dd | 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 | ||
310 | fileId | |
311 | fileTable_addFile (fileTable ft, cstring name) | |
312 | { | |
313 | /* while (*name == '.' && *(name + 1) == '/') name += 2; */ | |
28bf4b0b | 314 | |
616915dd | 315 | return (fileTable_addFilePrim (ft, cstring_copy (name), |
316 | FALSE, FILE_NORMAL, fileId_invalid)); | |
317 | } | |
318 | ||
319 | fileId | |
320 | fileTable_addFileOnly (fileTable ft, /*@only@*/ cstring name) | |
321 | { | |
322 | return (fileTable_addFilePrim (ft, name, FALSE, FILE_NORMAL, fileId_invalid)); | |
323 | } | |
324 | ||
325 | fileId | |
326 | fileTable_addHeaderFile (fileTable ft, cstring name) | |
327 | { | |
28bf4b0b | 328 | fileId res; |
329 | ||
330 | res = fileTable_addFilePrim (ft, cstring_copy (name), FALSE, | |
331 | FILE_HEADER, fileId_invalid); | |
332 | return res; | |
333 | ||
616915dd | 334 | } |
335 | ||
336 | bool | |
337 | fileTable_isHeader (fileTable ft, fileId fid) | |
338 | { | |
339 | if (fileId_isInvalid (fid)) | |
340 | { | |
341 | return FALSE; | |
342 | } | |
343 | ||
344 | llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid)); | |
345 | return (ft->elements[fid]->ftype == FILE_HEADER); | |
346 | } | |
347 | ||
348 | bool | |
349 | fileTable_isSystemFile (fileTable ft, fileId fid) | |
350 | { | |
351 | if (fileId_isInvalid (fid)) | |
352 | { | |
353 | return FALSE; | |
354 | } | |
355 | ||
356 | llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid)); | |
357 | return (ft->elements[fid]->fsystem); | |
358 | } | |
359 | ||
28bf4b0b | 360 | bool |
361 | fileTable_isXHFile (fileTable ft, fileId fid) | |
362 | { | |
363 | if (fileId_isInvalid (fid)) | |
364 | { | |
365 | return FALSE; | |
366 | } | |
367 | ||
368 | llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid)); | |
369 | return (ft->elements[fid]->ftype == FILE_XH); | |
370 | } | |
371 | ||
616915dd | 372 | bool |
373 | fileTable_isSpecialFile (fileTable ft, fileId fid) | |
374 | { | |
375 | if (fileId_isInvalid (fid)) | |
376 | { | |
377 | return FALSE; | |
378 | } | |
379 | ||
380 | llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid)); | |
381 | return (ft->elements[fid]->fspecial); | |
382 | } | |
383 | ||
384 | fileId | |
385 | fileTable_addLibraryFile (fileTable ft, cstring name) | |
386 | { | |
387 | return (fileTable_addFilePrim (ft, cstring_copy (name), | |
388 | FALSE, FILE_HEADER, fileId_invalid)); | |
389 | } | |
390 | ||
28bf4b0b | 391 | fileId |
392 | fileTable_addXHFile (fileTable ft, cstring name) | |
393 | { | |
394 | return (fileTable_addFilePrim (ft, cstring_copy (name), | |
395 | FALSE, FILE_XH, fileId_invalid)); | |
396 | } | |
397 | ||
616915dd | 398 | # ifndef NOLCL |
399 | fileId | |
400 | fileTable_addImportFile (fileTable ft, cstring name) | |
401 | { | |
402 | return (fileTable_addFilePrim (ft, cstring_copy (name), | |
403 | FALSE, FILE_HEADER, fileId_invalid)); | |
404 | } | |
405 | ||
406 | fileId | |
407 | fileTable_addLCLFile (fileTable ft, cstring name) | |
408 | { | |
409 | return (fileTable_addFilePrim (ft, cstring_copy (name), | |
410 | FALSE, FILE_HEADER, fileId_invalid)); | |
411 | } | |
412 | # endif | |
413 | ||
414 | # ifndef NOLCL | |
415 | static int tmpcounter = 0; | |
416 | # endif | |
417 | ||
418 | fileId | |
419 | fileTable_addMacrosFile (fileTable ft) | |
420 | { | |
28bf4b0b | 421 | cstring newname = |
422 | makeTempName (context_tmpdir (), cstring_makeLiteralTemp ("lmx"), | |
423 | cstring_makeLiteralTemp (".llm")); | |
616915dd | 424 | |
425 | return (fileTable_addFilePrim (ft, newname, TRUE, FILE_MACROS, fileId_invalid)); | |
426 | } | |
427 | ||
28bf4b0b | 428 | fileId |
429 | fileTable_addMetastateFile (fileTable ft, cstring name) | |
430 | { | |
431 | return (fileTable_addFilePrim (ft, cstring_copy (name), | |
432 | FALSE, FILE_METASTATE, fileId_invalid)); | |
433 | } | |
434 | ||
616915dd | 435 | fileId |
436 | fileTable_addCTempFile (fileTable ft, fileId fid) | |
437 | { | |
438 | # if FALSE | |
439 | /* Can't control output file name for cl preprocessor */ | |
28bf4b0b | 440 | cstring newname = cstring_concatChars (fileLib_removeAnyExtension (fileName (fid)), ".i"); |
616915dd | 441 | # else |
28bf4b0b | 442 | cstring newname = |
443 | makeTempName (context_tmpdir (), cstring_makeLiteralTemp ("cl"), | |
444 | C_EXTENSION); | |
616915dd | 445 | # endif |
446 | ||
447 | llassert (fileTable_isDefined (ft)); | |
448 | ||
449 | if (!fileId_isValid (ft->elements[fid]->fder)) | |
450 | { | |
28bf4b0b | 451 | if (fileTable_isXHFile (ft, fid)) |
452 | { | |
453 | return (fileTable_addFilePrim (ft, newname, TRUE, FILE_XH, fid)); | |
454 | } | |
455 | else | |
456 | { | |
457 | return (fileTable_addFilePrim (ft, newname, TRUE, FILE_NORMAL, fid)); | |
458 | } | |
616915dd | 459 | } |
460 | else | |
461 | { | |
28bf4b0b | 462 | if (fileTable_isXHFile (ft, fid)) |
463 | { | |
464 | return (fileTable_addFilePrim (ft, newname, TRUE, FILE_XH, | |
465 | ft->elements[fid]->fder)); | |
466 | } | |
467 | else | |
468 | { | |
469 | return (fileTable_addFilePrim (ft, newname, TRUE, FILE_NORMAL, | |
470 | ft->elements[fid]->fder)); | |
471 | } | |
616915dd | 472 | } |
473 | } | |
474 | ||
475 | # ifndef NOLCL | |
476 | fileId | |
477 | fileTable_addltemp (fileTable ft) | |
478 | { | |
28bf4b0b | 479 | cstring newname = makeTempName (context_tmpdir (), |
480 | cstring_makeLiteralTemp ("ls"), | |
481 | cstring_makeLiteralTemp (".lsl")); | |
616915dd | 482 | fileId ret; |
483 | ||
28bf4b0b | 484 | if (cstring_hasNonAlphaNumBar (newname)) |
616915dd | 485 | { |
486 | char *lastpath = (char *)NULL; | |
487 | ||
488 | if (tmpcounter == 0) | |
489 | { | |
490 | lldiagmsg | |
491 | (message | |
492 | ("Operating system generates tmp filename containing invalid charater: %s", | |
28bf4b0b | 493 | newname)); |
616915dd | 494 | lldiagmsg (cstring_makeLiteral |
495 | ("Try cleaning up the tmp directory. Attempting to continue.")); | |
496 | } | |
497 | ||
28bf4b0b | 498 | /*@access cstring@*/ |
499 | llassert (cstring_isDefined (newname)); | |
616915dd | 500 | lastpath = strrchr (newname, CONNECTCHAR); /* get the directory */ |
501 | llassert (lastpath != NULL); | |
502 | *lastpath = '\0'; | |
503 | ||
28bf4b0b | 504 | newname = message ("%q%hlsl%d.lsl", |
505 | newname, | |
506 | CONNECTCHAR, | |
507 | tmpcounter); | |
508 | /*@noaccess cstring@*/ | |
616915dd | 509 | tmpcounter++; |
510 | } | |
511 | ||
512 | /* | |
513 | ** this is kind of yucky...need to make the result of cstring_fromChars | |
514 | ** refer to the same storage as its argument. Of course, this loses, | |
515 | ** since cstring is abstract. Should make it an only? | |
516 | */ | |
517 | ||
28bf4b0b | 518 | ret = fileTable_addFilePrim (ft, cstring_copy (newname), |
616915dd | 519 | TRUE, FILE_LSLTEMP, fileId_invalid); |
28bf4b0b | 520 | cstring_free (newname); |
616915dd | 521 | return (ret); |
522 | } | |
523 | # endif | |
524 | ||
525 | bool | |
526 | fileTable_exists (fileTable ft, cstring s) | |
527 | { | |
528 | int tindex = fileTable_getIndex (ft, s); | |
529 | ||
530 | if (tindex == NOT_FOUND) | |
531 | return FALSE; | |
532 | else | |
533 | return TRUE; | |
534 | } | |
535 | ||
536 | fileId | |
537 | fileTable_lookup (fileTable ft, cstring s) | |
538 | { | |
539 | int tindex = fileTable_getIndex (ft, s); | |
540 | ||
541 | if (tindex == NOT_FOUND) | |
542 | { | |
543 | return fileId_invalid; | |
544 | } | |
545 | else | |
546 | { | |
547 | return tindex; | |
548 | } | |
549 | } | |
550 | ||
28bf4b0b | 551 | /* |
552 | ** This is pretty awkward --- when we find the real path of | |
553 | ** a .xh file, we may need to change the recorded name. [Sigh] | |
554 | */ | |
555 | ||
556 | void | |
557 | fileTable_setFilePath (fileTable ft, fileId fid, cstring path) | |
558 | { | |
559 | llassert (fileId_isValid (fid)); | |
560 | llassert (fileTable_isDefined (ft)); | |
561 | /* Need to put new string in hash table */ | |
562 | cstringTable_insert (ft->htable, cstring_copy (path), fid); | |
563 | ft->elements[fid]->fname = cstring_copy (path); | |
564 | } | |
565 | ||
616915dd | 566 | fileId |
567 | fileTable_lookupBase (fileTable ft, cstring base) | |
568 | { | |
569 | int tindex = fileTable_getIndex (ft, base); | |
570 | ||
571 | if (tindex == NOT_FOUND) | |
572 | { | |
616915dd | 573 | return fileId_invalid; |
574 | } | |
575 | else | |
576 | { | |
577 | fileId der; | |
578 | ||
579 | llassert (fileTable_isDefined (ft)); | |
580 | ||
581 | der = ft->elements[tindex]->fder; | |
582 | ||
583 | if (!fileId_isValid (der)) | |
584 | { | |
585 | der = tindex; | |
586 | } | |
587 | ||
588 | return der; | |
589 | } | |
590 | } | |
591 | ||
592 | cstring | |
593 | fileTable_getName (fileTable ft, fileId fid) | |
594 | { | |
595 | if (!fileId_isValid (fid)) | |
596 | { | |
597 | llcontbug | |
598 | (message ("fileTable_getName: called with invalid type id: %d", fid)); | |
599 | return cstring_makeLiteralTemp ("<invalid>"); | |
600 | } | |
601 | ||
602 | llassert (fileTable_isDefined (ft)); | |
603 | return (ft->elements[fid]->fname); | |
604 | } | |
605 | ||
606 | cstring | |
607 | fileTable_getRootName (fileTable ft, fileId fid) | |
608 | { | |
609 | fileId fder; | |
610 | ||
611 | if (!fileId_isValid (fid)) | |
612 | { | |
613 | llcontbug (message ("fileTable_getName: called with invalid id: %d", fid)); | |
614 | return cstring_makeLiteralTemp ("<invalid>"); | |
615 | } | |
616 | ||
617 | if (!fileTable_isDefined (ft)) | |
618 | { | |
619 | return cstring_makeLiteralTemp ("<no file table>"); | |
620 | } | |
621 | ||
616915dd | 622 | fder = ft->elements[fid]->fder; |
623 | ||
624 | if (fileId_isValid (fder)) | |
625 | { | |
626 | return (ft->elements[fder]->fname); | |
627 | } | |
628 | else | |
629 | { | |
630 | return (ft->elements[fid]->fname); | |
631 | } | |
632 | } | |
633 | ||
634 | cstring | |
635 | fileTable_getNameBase (fileTable ft, fileId fid) | |
636 | { | |
637 | if (!fileId_isValid (fid)) | |
638 | { | |
639 | llcontbug (message ("fileTable_getName: called with invalid id: %d", fid)); | |
640 | return cstring_makeLiteralTemp ("<invalid>"); | |
641 | } | |
642 | ||
643 | if (!fileTable_isDefined (ft)) | |
644 | { | |
645 | return cstring_makeLiteralTemp ("<no file table>"); | |
646 | } | |
647 | ||
648 | return (ft->elements[fid]->basename); | |
649 | } | |
650 | ||
651 | bool | |
652 | fileTable_sameBase (fileTable ft, fileId f1, fileId f2) | |
653 | { | |
654 | fileId fd1, fd2; | |
655 | ||
656 | if (!fileId_isValid (f1)) | |
657 | { | |
658 | return FALSE; | |
659 | } | |
660 | ||
661 | if (!fileId_isValid (f2)) | |
662 | { | |
663 | return FALSE; | |
664 | } | |
665 | ||
666 | llassert (fileTable_isDefined (ft)); | |
667 | ||
668 | if (f1 == f2) | |
669 | { | |
670 | return TRUE; | |
671 | } | |
672 | ||
673 | fd1 = ft->elements[f1]->fder; | |
674 | ||
675 | if (!fileId_isValid (fd1)) | |
676 | { | |
677 | fd1 = f1; | |
678 | } | |
679 | ||
680 | fd2 = ft->elements[f2]->fder; | |
681 | ||
682 | ||
683 | if (!fileId_isValid (fd2)) | |
684 | { | |
685 | fd2 = f2; | |
686 | } | |
687 | ||
688 | return (fd1 == fd2); | |
689 | } | |
690 | ||
691 | void | |
692 | fileTable_cleanup (fileTable ft) | |
693 | { | |
694 | int i; | |
695 | bool msg; | |
696 | int skip; | |
697 | ||
698 | llassert (fileTable_isDefined (ft)); | |
699 | ||
700 | msg = ((ft->nentries > 40) && context_getFlag (FLG_SHOWSCAN)); | |
701 | skip = ft->nentries / 10; | |
702 | ||
703 | if (msg) | |
704 | { | |
705 | (void) fflush (g_msgstream); | |
706 | fprintf (stderr, "< cleaning"); | |
707 | } | |
708 | ||
709 | for (i = 0; i < ft->nentries; i++) | |
710 | { | |
711 | ftentry fe = ft->elements[i]; | |
712 | ||
713 | if (fe->ftemp) | |
714 | { | |
715 | /* let's be real careful now, hon! */ | |
716 | ||
717 | /* | |
718 | ** Make sure it is really a derived file | |
719 | */ | |
720 | ||
721 | if (fe->ftype == FILE_LSLTEMP || fe->ftype == FILE_NODELETE) | |
722 | { | |
723 | ; /* already removed */ | |
724 | } | |
725 | else if (fileId_isValid (fe->fder)) | |
726 | { | |
28bf4b0b | 727 | /*@i423 this should use close (fd) also... */ |
728 | (void) osd_unlink (fe->fname); | |
616915dd | 729 | } |
730 | else if (fe->ftype == FILE_MACROS) | |
731 | { | |
28bf4b0b | 732 | (void) osd_unlink (fe->fname); |
616915dd | 733 | } |
734 | else | |
735 | { | |
736 | llbug (message ("Temporary file is not derivative: %s " | |
737 | "(not deleted)", fe->fname)); | |
738 | } | |
739 | } | |
740 | else | |
741 | { | |
742 | ; | |
743 | } | |
744 | ||
745 | if (msg && ((i % skip) == 0)) | |
746 | { | |
747 | (void) fflush (g_msgstream); | |
748 | ||
749 | if (i == 0) { | |
750 | fprintf (stderr, " "); | |
751 | } else { | |
752 | fprintf (stderr, "."); | |
753 | } | |
754 | ||
755 | (void) fflush (stderr); | |
756 | } | |
757 | } | |
758 | ||
759 | if (msg) | |
760 | { | |
761 | fprintf (stderr, " >\n"); | |
762 | } | |
763 | } | |
764 | ||
765 | void | |
766 | fileTable_free (/*@only@*/ fileTable f) | |
767 | { | |
768 | int i = 0; | |
769 | ||
770 | if (f == (fileTable)NULL) | |
771 | { | |
772 | return; | |
773 | } | |
774 | ||
775 | while ( i < f->nentries ) | |
776 | { | |
777 | ftentry_free (f->elements[i]); | |
778 | i++; | |
779 | } | |
780 | ||
28bf4b0b | 781 | cstringTable_free (f->htable); |
616915dd | 782 | sfree (f->elements); |
783 | sfree (f); | |
784 | } | |
785 | ||
786 | /* | |
787 | ** unique temp filename are constructed from <dir><pre><pid><msg>.<suf> | |
788 | ** requires: <dir> must end in '/' | |
789 | */ | |
790 | ||
791 | static void nextMsg (char *msg) | |
792 | { | |
793 | /*@+charint@*/ | |
794 | if (msg[0] < 'Z') | |
795 | { | |
796 | msg[0]++; | |
797 | } | |
798 | else | |
799 | { | |
800 | msg[0] = 'A'; | |
801 | if (msg[1] < 'Z') | |
802 | { | |
803 | msg[1]++; | |
804 | } | |
805 | else | |
806 | { | |
807 | msg[1] = 'A'; | |
808 | if (msg[2] < 'Z') | |
809 | { | |
810 | msg[2]++; | |
811 | } | |
812 | else | |
813 | { | |
814 | msg[2] = 'A'; | |
815 | if (msg[3] < 'Z') | |
816 | { | |
817 | msg[3]++; | |
818 | } | |
819 | else | |
820 | { | |
821 | llassertprint (FALSE, ("nextMsg: out of unique names!!!")); | |
822 | } | |
823 | } | |
824 | } | |
825 | } | |
826 | /*@-charint@*/ | |
827 | } | |
828 | ||
28bf4b0b | 829 | static /*@only@*/ cstring makeTempName (cstring dir, cstring pre, cstring suf) |
616915dd | 830 | { |
831 | static int pid = 0; | |
832 | static /*@owned@*/ char *msg = NULL; | |
28bf4b0b | 833 | static /*@only@*/ cstring pidname = NULL; |
834 | int maxlen; | |
835 | cstring smsg; | |
616915dd | 836 | |
28bf4b0b | 837 | llassert (cstring_length (pre) <= 3); |
616915dd | 838 | |
839 | /* | |
840 | ** We limit the temp name to 8 characters: | |
841 | ** pre: 3 or less | |
842 | ** msg: 3 | |
843 | ** pid: 2 (% 100) | |
844 | */ | |
845 | ||
846 | if (msg == NULL) | |
847 | { | |
848 | msg = mstring_copy ("AAA"); /* there are 26^3 temp names */ | |
849 | } | |
850 | ||
851 | if (pid == 0) | |
852 | { | |
853 | /*@+matchanyintegral@*/ | |
854 | pid = osd_getPid (); | |
855 | /*@=matchanyintegral@*/ | |
856 | } | |
857 | ||
28bf4b0b | 858 | if (cstring_isUndefined (pidname)) |
616915dd | 859 | { |
28bf4b0b | 860 | pidname = message ("%d", pid % 100); |
616915dd | 861 | } |
862 | ||
28bf4b0b | 863 | maxlen = (cstring_length (dir) + cstring_length (pre) + mstring_length (msg) |
864 | + cstring_length (pidname) + cstring_length (suf) + 2); | |
616915dd | 865 | |
28bf4b0b | 866 | smsg = message ("%s%s%s%s%s", dir, pre, pidname, cstring_fromChars (msg), suf); |
616915dd | 867 | nextMsg (msg); |
868 | ||
28bf4b0b | 869 | while (osd_fileExists (smsg)) |
616915dd | 870 | { |
28bf4b0b | 871 | cstring_free (smsg); |
872 | smsg = message ("%s%s%s%s%s", dir, pre, pidname, cstring_fromChars (msg), suf); | |
616915dd | 873 | nextMsg (msg); |
874 | } | |
616915dd | 875 | |
876 | ||
28bf4b0b | 877 | return smsg; |
878 | } |