]>
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 | ||
ccf0a4a8 | 368 | if (!(fileTable_isDefined (ft) && fileTable_inRange (ft, fid))) |
369 | { | |
370 | llcontbug (message ("Bad file table or id: %s %d", bool_unparse (fileTable_isDefined (ft)), fid)); | |
371 | return FALSE; | |
372 | } | |
373 | else | |
374 | { | |
375 | return (ft->elements[fid]->ftype == FILE_XH); | |
376 | } | |
28bf4b0b | 377 | } |
378 | ||
616915dd | 379 | bool |
380 | fileTable_isSpecialFile (fileTable ft, fileId fid) | |
381 | { | |
382 | if (fileId_isInvalid (fid)) | |
383 | { | |
384 | return FALSE; | |
385 | } | |
ccf0a4a8 | 386 | |
616915dd | 387 | llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid)); |
388 | return (ft->elements[fid]->fspecial); | |
389 | } | |
390 | ||
391 | fileId | |
392 | fileTable_addLibraryFile (fileTable ft, cstring name) | |
393 | { | |
394 | return (fileTable_addFilePrim (ft, cstring_copy (name), | |
395 | FALSE, FILE_HEADER, fileId_invalid)); | |
396 | } | |
397 | ||
28bf4b0b | 398 | fileId |
399 | fileTable_addXHFile (fileTable ft, cstring name) | |
400 | { | |
401 | return (fileTable_addFilePrim (ft, cstring_copy (name), | |
402 | FALSE, FILE_XH, fileId_invalid)); | |
403 | } | |
404 | ||
616915dd | 405 | # ifndef NOLCL |
406 | fileId | |
407 | fileTable_addImportFile (fileTable ft, cstring name) | |
408 | { | |
409 | return (fileTable_addFilePrim (ft, cstring_copy (name), | |
410 | FALSE, FILE_HEADER, fileId_invalid)); | |
411 | } | |
412 | ||
413 | fileId | |
414 | fileTable_addLCLFile (fileTable ft, cstring name) | |
415 | { | |
416 | return (fileTable_addFilePrim (ft, cstring_copy (name), | |
417 | FALSE, FILE_HEADER, fileId_invalid)); | |
418 | } | |
419 | # endif | |
420 | ||
421 | # ifndef NOLCL | |
422 | static int tmpcounter = 0; | |
423 | # endif | |
424 | ||
425 | fileId | |
426 | fileTable_addMacrosFile (fileTable ft) | |
427 | { | |
28bf4b0b | 428 | cstring newname = |
429 | makeTempName (context_tmpdir (), cstring_makeLiteralTemp ("lmx"), | |
430 | cstring_makeLiteralTemp (".llm")); | |
616915dd | 431 | |
432 | return (fileTable_addFilePrim (ft, newname, TRUE, FILE_MACROS, fileId_invalid)); | |
433 | } | |
434 | ||
28bf4b0b | 435 | fileId |
436 | fileTable_addMetastateFile (fileTable ft, cstring name) | |
437 | { | |
438 | return (fileTable_addFilePrim (ft, cstring_copy (name), | |
439 | FALSE, FILE_METASTATE, fileId_invalid)); | |
440 | } | |
441 | ||
616915dd | 442 | fileId |
443 | fileTable_addCTempFile (fileTable ft, fileId fid) | |
444 | { | |
28bf4b0b | 445 | cstring newname = |
446 | makeTempName (context_tmpdir (), cstring_makeLiteralTemp ("cl"), | |
447 | C_EXTENSION); | |
616915dd | 448 | |
449 | llassert (fileTable_isDefined (ft)); | |
450 | ||
451 | if (!fileId_isValid (ft->elements[fid]->fder)) | |
452 | { | |
28bf4b0b | 453 | if (fileTable_isXHFile (ft, fid)) |
454 | { | |
455 | return (fileTable_addFilePrim (ft, newname, TRUE, FILE_XH, fid)); | |
456 | } | |
457 | else | |
458 | { | |
459 | return (fileTable_addFilePrim (ft, newname, TRUE, FILE_NORMAL, fid)); | |
460 | } | |
616915dd | 461 | } |
462 | else | |
463 | { | |
28bf4b0b | 464 | if (fileTable_isXHFile (ft, fid)) |
465 | { | |
466 | return (fileTable_addFilePrim (ft, newname, TRUE, FILE_XH, | |
467 | ft->elements[fid]->fder)); | |
468 | } | |
469 | else | |
470 | { | |
471 | return (fileTable_addFilePrim (ft, newname, TRUE, FILE_NORMAL, | |
472 | ft->elements[fid]->fder)); | |
473 | } | |
616915dd | 474 | } |
475 | } | |
476 | ||
477 | # ifndef NOLCL | |
478 | fileId | |
479 | fileTable_addltemp (fileTable ft) | |
480 | { | |
28bf4b0b | 481 | cstring newname = makeTempName (context_tmpdir (), |
482 | cstring_makeLiteralTemp ("ls"), | |
483 | cstring_makeLiteralTemp (".lsl")); | |
616915dd | 484 | fileId ret; |
485 | ||
28bf4b0b | 486 | if (cstring_hasNonAlphaNumBar (newname)) |
616915dd | 487 | { |
488 | char *lastpath = (char *)NULL; | |
489 | ||
490 | if (tmpcounter == 0) | |
491 | { | |
492 | lldiagmsg | |
493 | (message | |
494 | ("Operating system generates tmp filename containing invalid charater: %s", | |
28bf4b0b | 495 | newname)); |
616915dd | 496 | lldiagmsg (cstring_makeLiteral |
497 | ("Try cleaning up the tmp directory. Attempting to continue.")); | |
498 | } | |
499 | ||
28bf4b0b | 500 | /*@access cstring@*/ |
501 | llassert (cstring_isDefined (newname)); | |
616915dd | 502 | lastpath = strrchr (newname, CONNECTCHAR); /* get the directory */ |
503 | llassert (lastpath != NULL); | |
504 | *lastpath = '\0'; | |
505 | ||
28bf4b0b | 506 | newname = message ("%q%hlsl%d.lsl", |
507 | newname, | |
508 | CONNECTCHAR, | |
509 | tmpcounter); | |
510 | /*@noaccess cstring@*/ | |
616915dd | 511 | tmpcounter++; |
512 | } | |
513 | ||
514 | /* | |
515 | ** this is kind of yucky...need to make the result of cstring_fromChars | |
516 | ** refer to the same storage as its argument. Of course, this loses, | |
517 | ** since cstring is abstract. Should make it an only? | |
518 | */ | |
519 | ||
28bf4b0b | 520 | ret = fileTable_addFilePrim (ft, cstring_copy (newname), |
616915dd | 521 | TRUE, FILE_LSLTEMP, fileId_invalid); |
28bf4b0b | 522 | cstring_free (newname); |
616915dd | 523 | return (ret); |
524 | } | |
525 | # endif | |
526 | ||
527 | bool | |
528 | fileTable_exists (fileTable ft, cstring s) | |
529 | { | |
530 | int tindex = fileTable_getIndex (ft, s); | |
531 | ||
532 | if (tindex == NOT_FOUND) | |
533 | return FALSE; | |
534 | else | |
535 | return TRUE; | |
536 | } | |
537 | ||
538 | fileId | |
539 | fileTable_lookup (fileTable ft, cstring s) | |
540 | { | |
541 | int tindex = fileTable_getIndex (ft, s); | |
542 | ||
543 | if (tindex == NOT_FOUND) | |
544 | { | |
545 | return fileId_invalid; | |
546 | } | |
547 | else | |
548 | { | |
549 | return tindex; | |
550 | } | |
551 | } | |
552 | ||
28bf4b0b | 553 | /* |
554 | ** This is pretty awkward --- when we find the real path of | |
555 | ** a .xh file, we may need to change the recorded name. [Sigh] | |
556 | */ | |
557 | ||
558 | void | |
559 | fileTable_setFilePath (fileTable ft, fileId fid, cstring path) | |
560 | { | |
561 | llassert (fileId_isValid (fid)); | |
562 | llassert (fileTable_isDefined (ft)); | |
563 | /* Need to put new string in hash table */ | |
564 | cstringTable_insert (ft->htable, cstring_copy (path), fid); | |
565 | ft->elements[fid]->fname = cstring_copy (path); | |
566 | } | |
567 | ||
616915dd | 568 | fileId |
569 | fileTable_lookupBase (fileTable ft, cstring base) | |
570 | { | |
571 | int tindex = fileTable_getIndex (ft, base); | |
572 | ||
573 | if (tindex == NOT_FOUND) | |
574 | { | |
616915dd | 575 | return fileId_invalid; |
576 | } | |
577 | else | |
578 | { | |
579 | fileId der; | |
580 | ||
581 | llassert (fileTable_isDefined (ft)); | |
582 | ||
583 | der = ft->elements[tindex]->fder; | |
584 | ||
585 | if (!fileId_isValid (der)) | |
586 | { | |
587 | der = tindex; | |
588 | } | |
589 | ||
590 | return der; | |
591 | } | |
592 | } | |
593 | ||
594 | cstring | |
595 | fileTable_getName (fileTable ft, fileId fid) | |
596 | { | |
597 | if (!fileId_isValid (fid)) | |
598 | { | |
599 | llcontbug | |
600 | (message ("fileTable_getName: called with invalid type id: %d", fid)); | |
601 | return cstring_makeLiteralTemp ("<invalid>"); | |
602 | } | |
603 | ||
604 | llassert (fileTable_isDefined (ft)); | |
605 | return (ft->elements[fid]->fname); | |
606 | } | |
607 | ||
608 | cstring | |
609 | fileTable_getRootName (fileTable ft, fileId fid) | |
610 | { | |
611 | fileId fder; | |
612 | ||
613 | if (!fileId_isValid (fid)) | |
614 | { | |
615 | llcontbug (message ("fileTable_getName: called with invalid id: %d", fid)); | |
616 | return cstring_makeLiteralTemp ("<invalid>"); | |
617 | } | |
618 | ||
619 | if (!fileTable_isDefined (ft)) | |
620 | { | |
621 | return cstring_makeLiteralTemp ("<no file table>"); | |
622 | } | |
623 | ||
616915dd | 624 | fder = ft->elements[fid]->fder; |
625 | ||
626 | if (fileId_isValid (fder)) | |
627 | { | |
628 | return (ft->elements[fder]->fname); | |
629 | } | |
630 | else | |
631 | { | |
632 | return (ft->elements[fid]->fname); | |
633 | } | |
634 | } | |
635 | ||
636 | cstring | |
637 | fileTable_getNameBase (fileTable ft, fileId fid) | |
638 | { | |
639 | if (!fileId_isValid (fid)) | |
640 | { | |
641 | llcontbug (message ("fileTable_getName: called with invalid id: %d", fid)); | |
642 | return cstring_makeLiteralTemp ("<invalid>"); | |
643 | } | |
644 | ||
645 | if (!fileTable_isDefined (ft)) | |
646 | { | |
647 | return cstring_makeLiteralTemp ("<no file table>"); | |
648 | } | |
649 | ||
650 | return (ft->elements[fid]->basename); | |
651 | } | |
652 | ||
653 | bool | |
654 | fileTable_sameBase (fileTable ft, fileId f1, fileId f2) | |
655 | { | |
656 | fileId fd1, fd2; | |
657 | ||
658 | if (!fileId_isValid (f1)) | |
659 | { | |
660 | return FALSE; | |
661 | } | |
662 | ||
663 | if (!fileId_isValid (f2)) | |
664 | { | |
665 | return FALSE; | |
666 | } | |
667 | ||
668 | llassert (fileTable_isDefined (ft)); | |
669 | ||
670 | if (f1 == f2) | |
671 | { | |
672 | return TRUE; | |
673 | } | |
674 | ||
675 | fd1 = ft->elements[f1]->fder; | |
676 | ||
677 | if (!fileId_isValid (fd1)) | |
678 | { | |
679 | fd1 = f1; | |
680 | } | |
681 | ||
682 | fd2 = ft->elements[f2]->fder; | |
683 | ||
684 | ||
685 | if (!fileId_isValid (fd2)) | |
686 | { | |
687 | fd2 = f2; | |
688 | } | |
689 | ||
690 | return (fd1 == fd2); | |
691 | } | |
692 | ||
693 | void | |
694 | fileTable_cleanup (fileTable ft) | |
695 | { | |
696 | int i; | |
697 | bool msg; | |
698 | int skip; | |
699 | ||
700 | llassert (fileTable_isDefined (ft)); | |
701 | ||
702 | msg = ((ft->nentries > 40) && context_getFlag (FLG_SHOWSCAN)); | |
703 | skip = ft->nentries / 10; | |
704 | ||
705 | if (msg) | |
706 | { | |
707 | (void) fflush (g_msgstream); | |
708 | fprintf (stderr, "< cleaning"); | |
709 | } | |
710 | ||
711 | for (i = 0; i < ft->nentries; i++) | |
712 | { | |
713 | ftentry fe = ft->elements[i]; | |
714 | ||
715 | if (fe->ftemp) | |
716 | { | |
717 | /* let's be real careful now, hon! */ | |
718 | ||
719 | /* | |
720 | ** Make sure it is really a derived file | |
721 | */ | |
722 | ||
723 | if (fe->ftype == FILE_LSLTEMP || fe->ftype == FILE_NODELETE) | |
724 | { | |
725 | ; /* already removed */ | |
726 | } | |
727 | else if (fileId_isValid (fe->fder)) | |
728 | { | |
28bf4b0b | 729 | /*@i423 this should use close (fd) also... */ |
730 | (void) osd_unlink (fe->fname); | |
616915dd | 731 | } |
732 | else if (fe->ftype == FILE_MACROS) | |
733 | { | |
28bf4b0b | 734 | (void) osd_unlink (fe->fname); |
616915dd | 735 | } |
736 | else | |
737 | { | |
738 | llbug (message ("Temporary file is not derivative: %s " | |
739 | "(not deleted)", fe->fname)); | |
740 | } | |
741 | } | |
742 | else | |
743 | { | |
744 | ; | |
745 | } | |
746 | ||
747 | if (msg && ((i % skip) == 0)) | |
748 | { | |
749 | (void) fflush (g_msgstream); | |
750 | ||
751 | if (i == 0) { | |
752 | fprintf (stderr, " "); | |
753 | } else { | |
754 | fprintf (stderr, "."); | |
755 | } | |
756 | ||
757 | (void) fflush (stderr); | |
758 | } | |
759 | } | |
760 | ||
761 | if (msg) | |
762 | { | |
763 | fprintf (stderr, " >\n"); | |
764 | } | |
765 | } | |
766 | ||
767 | void | |
768 | fileTable_free (/*@only@*/ fileTable f) | |
769 | { | |
770 | int i = 0; | |
771 | ||
772 | if (f == (fileTable)NULL) | |
773 | { | |
774 | return; | |
775 | } | |
776 | ||
777 | while ( i < f->nentries ) | |
778 | { | |
779 | ftentry_free (f->elements[i]); | |
780 | i++; | |
781 | } | |
782 | ||
28bf4b0b | 783 | cstringTable_free (f->htable); |
616915dd | 784 | sfree (f->elements); |
785 | sfree (f); | |
786 | } | |
787 | ||
788 | /* | |
789 | ** unique temp filename are constructed from <dir><pre><pid><msg>.<suf> | |
790 | ** requires: <dir> must end in '/' | |
791 | */ | |
792 | ||
793 | static void nextMsg (char *msg) | |
794 | { | |
795 | /*@+charint@*/ | |
796 | if (msg[0] < 'Z') | |
797 | { | |
798 | msg[0]++; | |
799 | } | |
800 | else | |
801 | { | |
802 | msg[0] = 'A'; | |
803 | if (msg[1] < 'Z') | |
804 | { | |
805 | msg[1]++; | |
806 | } | |
807 | else | |
808 | { | |
809 | msg[1] = 'A'; | |
810 | if (msg[2] < 'Z') | |
811 | { | |
812 | msg[2]++; | |
813 | } | |
814 | else | |
815 | { | |
816 | msg[2] = 'A'; | |
817 | if (msg[3] < 'Z') | |
818 | { | |
819 | msg[3]++; | |
820 | } | |
821 | else | |
822 | { | |
823 | llassertprint (FALSE, ("nextMsg: out of unique names!!!")); | |
824 | } | |
825 | } | |
826 | } | |
827 | } | |
828 | /*@-charint@*/ | |
829 | } | |
830 | ||
28bf4b0b | 831 | static /*@only@*/ cstring makeTempName (cstring dir, cstring pre, cstring suf) |
616915dd | 832 | { |
833 | static int pid = 0; | |
834 | static /*@owned@*/ char *msg = NULL; | |
28bf4b0b | 835 | static /*@only@*/ cstring pidname = NULL; |
836 | int maxlen; | |
837 | cstring smsg; | |
616915dd | 838 | |
28bf4b0b | 839 | llassert (cstring_length (pre) <= 3); |
616915dd | 840 | |
841 | /* | |
842 | ** We limit the temp name to 8 characters: | |
843 | ** pre: 3 or less | |
844 | ** msg: 3 | |
845 | ** pid: 2 (% 100) | |
846 | */ | |
847 | ||
848 | if (msg == NULL) | |
849 | { | |
850 | msg = mstring_copy ("AAA"); /* there are 26^3 temp names */ | |
851 | } | |
852 | ||
853 | if (pid == 0) | |
854 | { | |
855 | /*@+matchanyintegral@*/ | |
856 | pid = osd_getPid (); | |
857 | /*@=matchanyintegral@*/ | |
858 | } | |
859 | ||
28bf4b0b | 860 | if (cstring_isUndefined (pidname)) |
616915dd | 861 | { |
28bf4b0b | 862 | pidname = message ("%d", pid % 100); |
616915dd | 863 | } |
864 | ||
28bf4b0b | 865 | maxlen = (cstring_length (dir) + cstring_length (pre) + mstring_length (msg) |
866 | + cstring_length (pidname) + cstring_length (suf) + 2); | |
616915dd | 867 | |
28bf4b0b | 868 | smsg = message ("%s%s%s%s%s", dir, pre, pidname, cstring_fromChars (msg), suf); |
616915dd | 869 | nextMsg (msg); |
870 | ||
28bf4b0b | 871 | while (osd_fileExists (smsg)) |
616915dd | 872 | { |
28bf4b0b | 873 | cstring_free (smsg); |
874 | smsg = message ("%s%s%s%s%s", dir, pre, pidname, cstring_fromChars (msg), suf); | |
616915dd | 875 | nextMsg (msg); |
876 | } | |
616915dd | 877 | |
878 | ||
28bf4b0b | 879 | return smsg; |
880 | } |