]>
Commit | Line | Data |
---|---|---|
add7091b | 1 | /* Part of CPP library. (include file handling) |
5e7b4e25 JL |
2 | Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998, |
3 | 1999, 2000 Free Software Foundation, Inc. | |
add7091b ZW |
4 | Written by Per Bothner, 1994. |
5 | Based on CCCP program by Paul Rubin, June 1986 | |
6 | Adapted to ANSI C, Richard Stallman, Jan 1987 | |
7 | Split out of cpplib.c, Zack Weinberg, Oct 1998 | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify it | |
10 | under the terms of the GNU General Public License as published by the | |
11 | Free Software Foundation; either version 2, or (at your option) any | |
12 | later version. | |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program; if not, write to the Free Software | |
e38992e8 | 21 | Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
add7091b ZW |
22 | |
23 | #include "config.h" | |
24 | #include "system.h" | |
e38992e8 | 25 | #include "hashtab.h" |
add7091b | 26 | #include "cpplib.h" |
88ae23e7 | 27 | #include "cpphash.h" |
c1212d2f | 28 | #include "intl.h" |
168d3732 | 29 | #include "mkdeps.h" |
add7091b | 30 | |
38b24ee2 ZW |
31 | static IHASH *redundant_include_p PARAMS ((cpp_reader *, IHASH *, |
32 | struct file_name_list *)); | |
f2d5f0cc ZW |
33 | static IHASH *make_IHASH PARAMS ((const char *, const char *, |
34 | struct file_name_list *, | |
35 | unsigned int, IHASH **)); | |
a73ac7a5 | 36 | static struct file_name_map *read_name_map |
38b24ee2 ZW |
37 | PARAMS ((cpp_reader *, const char *)); |
38 | static char *read_filename_string PARAMS ((int, FILE *)); | |
39 | static char *remap_filename PARAMS ((cpp_reader *, char *, | |
40 | struct file_name_list *)); | |
a73ac7a5 | 41 | static struct file_name_list *actual_directory |
38b24ee2 | 42 | PARAMS ((cpp_reader *, const char *)); |
d35364d1 ZW |
43 | static unsigned int hash_IHASH PARAMS ((const void *)); |
44 | static int eq_IHASH PARAMS ((const void *, const void *)); | |
168d3732 ZW |
45 | static int find_include_file PARAMS ((cpp_reader *, const char *, |
46 | struct file_name_list *, | |
47 | IHASH **, int *)); | |
48 | static int read_include_file PARAMS ((cpp_reader *, int, IHASH *)); | |
e576beb0 | 49 | static inline int open_include_file PARAMS ((cpp_reader *, const char *)); |
2c826217 | 50 | |
0b3d776a | 51 | #if 0 |
f84c2018 | 52 | static void hack_vms_include_specification PARAMS ((char *)); |
0b3d776a | 53 | #endif |
add7091b | 54 | |
d35364d1 ZW |
55 | /* Initial size of include hash table. */ |
56 | #define IHASHSIZE 50 | |
57 | ||
bb52fa7f ZW |
58 | #ifndef INCLUDE_LEN_FUDGE |
59 | #define INCLUDE_LEN_FUDGE 0 | |
60 | #endif | |
61 | ||
d35364d1 ZW |
62 | /* Calculate hash of an IHASH entry. */ |
63 | static unsigned int | |
64 | hash_IHASH (x) | |
65 | const void *x; | |
add7091b | 66 | { |
29a72a4f ZW |
67 | const IHASH *i = (const IHASH *)x; |
68 | return i->hash; | |
d35364d1 | 69 | } |
add7091b | 70 | |
d35364d1 ZW |
71 | /* Compare an existing IHASH structure with a potential one. */ |
72 | static int | |
73 | eq_IHASH (x, y) | |
74 | const void *x; | |
75 | const void *y; | |
76 | { | |
12cf91fe ZW |
77 | const char *a = ((const IHASH *)x)->nshort; |
78 | const char *b = ((const IHASH *)y)->nshort; | |
d35364d1 ZW |
79 | return !strcmp (a, b); |
80 | } | |
add7091b | 81 | |
d35364d1 ZW |
82 | /* Init the hash table. In here so it can see the hash and eq functions. */ |
83 | void | |
84 | _cpp_init_include_hash (pfile) | |
85 | cpp_reader *pfile; | |
86 | { | |
87 | pfile->all_include_files | |
88 | = htab_create (IHASHSIZE, hash_IHASH, eq_IHASH, free); | |
0b3d776a | 89 | } |
add7091b | 90 | |
0b3d776a ZW |
91 | /* Return 0 if the file pointed to by IHASH has never been included before, |
92 | -1 if it has been included before and need not be again, | |
93 | or a pointer to an IHASH entry which is the file to be reread. | |
94 | "Never before" is with respect to the position in ILIST. | |
add7091b | 95 | |
0b3d776a ZW |
96 | This will not detect redundancies involving odd uses of the |
97 | `current directory' rule for "" includes. They aren't quite | |
98 | pathological, but I think they are rare enough not to worry about. | |
99 | The simplest example is: | |
add7091b | 100 | |
0b3d776a ZW |
101 | top.c: |
102 | #include "a/a.h" | |
103 | #include "b/b.h" | |
add7091b | 104 | |
0b3d776a ZW |
105 | a/a.h: |
106 | #include "../b/b.h" | |
add7091b | 107 | |
0b3d776a ZW |
108 | and the problem is that for `current directory' includes, |
109 | ihash->foundhere is not on any of the global include chains, | |
110 | so the test below (i->foundhere == l) may be false even when | |
111 | the directories are in fact the same. */ | |
112 | ||
38b24ee2 | 113 | static IHASH * |
0b3d776a | 114 | redundant_include_p (pfile, ihash, ilist) |
add7091b | 115 | cpp_reader *pfile; |
38b24ee2 | 116 | IHASH *ihash; |
0b3d776a | 117 | struct file_name_list *ilist; |
add7091b | 118 | { |
0b3d776a | 119 | struct file_name_list *l; |
38b24ee2 | 120 | IHASH *i; |
add7091b | 121 | |
0b3d776a ZW |
122 | if (! ihash->foundhere) |
123 | return 0; | |
add7091b | 124 | |
0b3d776a ZW |
125 | for (i = ihash; i; i = i->next_this_file) |
126 | for (l = ilist; l; l = l->next) | |
127 | if (i->foundhere == l) | |
128 | /* The control_macro works like this: If it's NULL, the file | |
129 | is to be included again. If it's "", the file is never to | |
130 | be included again. If it's a string, the file is not to be | |
131 | included again if the string is the name of a defined macro. */ | |
132 | return (i->control_macro | |
133 | && (i->control_macro[0] == '\0' | |
638d694d | 134 | || cpp_defined (pfile, i->control_macro, |
12cf91fe | 135 | ustrlen (i->control_macro)))) |
38b24ee2 | 136 | ? (IHASH *)-1 : i; |
add7091b | 137 | |
0b3d776a | 138 | return 0; |
add7091b ZW |
139 | } |
140 | ||
b0699dad ZW |
141 | /* Return 1 if the file named by FNAME has been included before in |
142 | any context, 0 otherwise. */ | |
143 | int | |
144 | cpp_included (pfile, fname) | |
145 | cpp_reader *pfile; | |
146 | const char *fname; | |
147 | { | |
d35364d1 ZW |
148 | IHASH dummy, *ptr; |
149 | dummy.nshort = fname; | |
12cf91fe | 150 | dummy.hash = _cpp_calc_hash ((const U_CHAR *)fname, strlen (fname)); |
29a72a4f ZW |
151 | ptr = htab_find_with_hash (pfile->all_include_files, |
152 | (const void *)&dummy, dummy.hash); | |
b0699dad ZW |
153 | return (ptr != NULL); |
154 | } | |
155 | ||
f2d5f0cc ZW |
156 | /* Create an IHASH entry and insert it in SLOT. */ |
157 | static IHASH * | |
158 | make_IHASH (name, fname, path, hash, slot) | |
159 | const char *name, *fname; | |
160 | struct file_name_list *path; | |
161 | unsigned int hash; | |
162 | IHASH **slot; | |
163 | { | |
164 | IHASH *ih; | |
165 | if (path == ABSOLUTE_PATH) | |
166 | { | |
167 | ih = (IHASH *) xmalloc (sizeof (IHASH) + strlen (name)); | |
168 | ih->nshort = ih->name; | |
169 | } | |
170 | else | |
171 | { | |
172 | char *s; | |
173 | ||
174 | if ((s = strstr (name, fname)) != NULL) | |
175 | { | |
176 | ih = (IHASH *) xmalloc (sizeof (IHASH) + strlen (name)); | |
177 | ih->nshort = ih->name + (s - name); | |
178 | } | |
179 | else | |
180 | { | |
181 | ih = (IHASH *) xmalloc (sizeof (IHASH) + strlen (name) | |
182 | + strlen (fname) + 1); | |
183 | ih->nshort = ih->name + strlen (name) + 1; | |
184 | strcpy ((char *)ih->nshort, fname); | |
185 | } | |
186 | } | |
187 | strcpy ((char *)ih->name, name); | |
188 | ih->foundhere = path; | |
189 | ih->control_macro = NULL; | |
190 | ih->hash = hash; | |
191 | ih->next_this_file = *slot; | |
192 | *slot = ih; | |
193 | return ih; | |
194 | } | |
195 | ||
e576beb0 ZW |
196 | /* Centralize calls to open(2) here. This provides a hook for future |
197 | changes which might, e.g. look for and open a precompiled version | |
198 | of the header. It also means all the magic currently associated | |
199 | with calling open is in one place, and if we ever need more, it'll | |
200 | be in one place too. | |
201 | ||
10e56506 ZW |
202 | We used to open files in nonblocking mode, but that caused more |
203 | problems than it solved. Do take care not to acquire a controlling | |
204 | terminal by mistake (this can't happen on sane systems, but | |
205 | paranoia is a virtue). | |
e576beb0 ZW |
206 | |
207 | Use the three-argument form of open even though we aren't | |
208 | specifying O_CREAT, to defend against broken system headers. */ | |
209 | ||
210 | static inline int | |
211 | open_include_file (pfile, filename) | |
212 | cpp_reader *pfile ATTRIBUTE_UNUSED; | |
213 | const char *filename; | |
214 | { | |
10e56506 | 215 | return open (filename, O_RDONLY|O_NOCTTY, 0666); |
e576beb0 ZW |
216 | } |
217 | ||
0b3d776a ZW |
218 | /* Search for include file FNAME in the include chain starting at |
219 | SEARCH_START. Return -2 if this file doesn't need to be included | |
220 | (because it was included already and it's marked idempotent), | |
221 | -1 if an error occurred, or a file descriptor open on the file. | |
222 | *IHASH is set to point to the include hash entry for this file, and | |
d35364d1 | 223 | *BEFORE is set to 1 if the file was included before (but needs to be read |
0b3d776a | 224 | again). */ |
168d3732 ZW |
225 | static int |
226 | find_include_file (pfile, fname, search_start, ihash, before) | |
add7091b | 227 | cpp_reader *pfile; |
bcc5cac9 | 228 | const char *fname; |
0b3d776a | 229 | struct file_name_list *search_start; |
38b24ee2 | 230 | IHASH **ihash; |
0b3d776a | 231 | int *before; |
add7091b | 232 | { |
d35364d1 ZW |
233 | struct file_name_list *path; |
234 | IHASH *ih, **slot; | |
235 | IHASH dummy; | |
236 | int f; | |
0b3d776a | 237 | char *name; |
0b3d776a | 238 | |
d35364d1 | 239 | dummy.nshort = fname; |
12cf91fe | 240 | dummy.hash = _cpp_calc_hash ((const U_CHAR *)fname, strlen (fname)); |
d35364d1 | 241 | path = (fname[0] == '/') ? ABSOLUTE_PATH : search_start; |
29a72a4f | 242 | slot = (IHASH **) htab_find_slot_with_hash (pfile->all_include_files, |
e38992e8 RK |
243 | (const void *) &dummy, |
244 | dummy.hash, INSERT); | |
0b3d776a | 245 | |
d35364d1 | 246 | if (*slot && (ih = redundant_include_p (pfile, *slot, path))) |
0b3d776a | 247 | { |
d35364d1 ZW |
248 | if (ih == (IHASH *)-1) |
249 | return -2; | |
0b3d776a | 250 | |
d35364d1 ZW |
251 | *before = 1; |
252 | *ihash = ih; | |
e576beb0 | 253 | return open_include_file (pfile, ih->name); |
0b3d776a | 254 | } |
d35364d1 ZW |
255 | |
256 | if (path == ABSOLUTE_PATH) | |
0b3d776a | 257 | { |
d35364d1 | 258 | name = (char *) fname; |
e576beb0 | 259 | f = open_include_file (pfile, name); |
0b3d776a | 260 | } |
d35364d1 | 261 | else |
0b3d776a | 262 | { |
d35364d1 | 263 | /* Search directory path, trying to open the file. */ |
5f6d3823 | 264 | name = (char *) alloca (strlen (fname) + pfile->max_include_len |
d35364d1 ZW |
265 | + 2 + INCLUDE_LEN_FUDGE); |
266 | do | |
0b3d776a | 267 | { |
d35364d1 ZW |
268 | memcpy (name, path->name, path->nlen); |
269 | name[path->nlen] = '/'; | |
270 | strcpy (&name[path->nlen+1], fname); | |
271 | _cpp_simplify_pathname (name); | |
ae79697b | 272 | if (CPP_OPTION (pfile, remap)) |
d35364d1 ZW |
273 | name = remap_filename (pfile, name, path); |
274 | ||
e576beb0 | 275 | f = open_include_file (pfile, name); |
d35364d1 ZW |
276 | #ifdef EACCES |
277 | if (f == -1 && errno == EACCES) | |
278 | { | |
279 | cpp_error (pfile, | |
280 | "included file `%s' exists but is not readable", | |
281 | name); | |
282 | return -1; | |
283 | } | |
0b3d776a | 284 | #endif |
d35364d1 ZW |
285 | if (f >= 0) |
286 | break; | |
287 | path = path->next; | |
288 | } | |
289 | while (path); | |
0b3d776a | 290 | } |
d35364d1 | 291 | if (f == -1) |
0b3d776a | 292 | return -1; |
d35364d1 | 293 | |
f2d5f0cc | 294 | ih = make_IHASH (name, fname, path, dummy.hash, slot); |
d35364d1 ZW |
295 | *before = 0; |
296 | *ihash = ih; | |
297 | return f; | |
add7091b ZW |
298 | } |
299 | ||
f2d5f0cc ZW |
300 | /* Create a dummy IHASH entry for FNAME, and return its name pointer. |
301 | This is used by #line. */ | |
302 | const char * | |
303 | _cpp_fake_ihash (pfile, fname) | |
304 | cpp_reader *pfile; | |
305 | const char *fname; | |
306 | { | |
307 | IHASH *ih, **slot; | |
308 | IHASH dummy; | |
309 | ||
310 | dummy.nshort = fname; | |
12cf91fe | 311 | dummy.hash = _cpp_calc_hash ((const U_CHAR *)fname, strlen (fname)); |
f2d5f0cc | 312 | slot = (IHASH **) htab_find_slot_with_hash (pfile->all_include_files, |
e38992e8 RK |
313 | (const void *) &dummy, |
314 | dummy.hash, INSERT); | |
f2d5f0cc ZW |
315 | if (*slot) |
316 | return (*slot)->name; | |
317 | ih = make_IHASH (fname, 0, ABSOLUTE_PATH, dummy.hash, slot); | |
318 | return ih->name; | |
319 | } | |
320 | ||
321 | ||
add7091b ZW |
322 | /* The file_name_map structure holds a mapping of file names for a |
323 | particular directory. This mapping is read from the file named | |
324 | FILE_NAME_MAP_FILE in that directory. Such a file can be used to | |
325 | map filenames on a file system with severe filename restrictions, | |
326 | such as DOS. The format of the file name map file is just a series | |
327 | of lines with two tokens on each line. The first token is the name | |
328 | to map, and the second token is the actual name to use. */ | |
329 | ||
330 | struct file_name_map | |
331 | { | |
332 | struct file_name_map *map_next; | |
333 | char *map_from; | |
334 | char *map_to; | |
335 | }; | |
336 | ||
337 | #define FILE_NAME_MAP_FILE "header.gcc" | |
338 | ||
339 | /* Read a space delimited string of unlimited length from a stdio | |
340 | file. */ | |
341 | ||
342 | static char * | |
343 | read_filename_string (ch, f) | |
344 | int ch; | |
345 | FILE *f; | |
346 | { | |
347 | char *alloc, *set; | |
348 | int len; | |
349 | ||
350 | len = 20; | |
351 | set = alloc = xmalloc (len + 1); | |
a9ae4483 | 352 | if (! is_space(ch)) |
add7091b ZW |
353 | { |
354 | *set++ = ch; | |
a9ae4483 | 355 | while ((ch = getc (f)) != EOF && ! is_space(ch)) |
add7091b ZW |
356 | { |
357 | if (set - alloc == len) | |
358 | { | |
359 | len *= 2; | |
360 | alloc = xrealloc (alloc, len + 1); | |
361 | set = alloc + len / 2; | |
362 | } | |
363 | *set++ = ch; | |
364 | } | |
365 | } | |
366 | *set = '\0'; | |
367 | ungetc (ch, f); | |
368 | return alloc; | |
369 | } | |
370 | ||
371 | /* This structure holds a linked list of file name maps, one per directory. */ | |
372 | ||
373 | struct file_name_map_list | |
374 | { | |
375 | struct file_name_map_list *map_list_next; | |
376 | char *map_list_name; | |
377 | struct file_name_map *map_list_map; | |
378 | }; | |
379 | ||
380 | /* Read the file name map file for DIRNAME. */ | |
381 | ||
382 | static struct file_name_map * | |
383 | read_name_map (pfile, dirname) | |
384 | cpp_reader *pfile; | |
460ee112 | 385 | const char *dirname; |
add7091b ZW |
386 | { |
387 | register struct file_name_map_list *map_list_ptr; | |
388 | char *name; | |
389 | FILE *f; | |
390 | ||
ae79697b | 391 | for (map_list_ptr = CPP_OPTION (pfile, map_list); map_list_ptr; |
add7091b ZW |
392 | map_list_ptr = map_list_ptr->map_list_next) |
393 | if (! strcmp (map_list_ptr->map_list_name, dirname)) | |
394 | return map_list_ptr->map_list_map; | |
395 | ||
396 | map_list_ptr = ((struct file_name_map_list *) | |
397 | xmalloc (sizeof (struct file_name_map_list))); | |
c49445e0 | 398 | map_list_ptr->map_list_name = xstrdup (dirname); |
add7091b ZW |
399 | |
400 | name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2); | |
401 | strcpy (name, dirname); | |
402 | if (*dirname) | |
403 | strcat (name, "/"); | |
404 | strcat (name, FILE_NAME_MAP_FILE); | |
405 | f = fopen (name, "r"); | |
406 | if (!f) | |
0b3d776a | 407 | map_list_ptr->map_list_map = (struct file_name_map *)-1; |
add7091b ZW |
408 | else |
409 | { | |
410 | int ch; | |
411 | int dirlen = strlen (dirname); | |
412 | ||
413 | while ((ch = getc (f)) != EOF) | |
414 | { | |
415 | char *from, *to; | |
416 | struct file_name_map *ptr; | |
417 | ||
a9ae4483 | 418 | if (is_space(ch)) |
add7091b ZW |
419 | continue; |
420 | from = read_filename_string (ch, f); | |
a9ae4483 | 421 | while ((ch = getc (f)) != EOF && is_hspace(ch)) |
add7091b ZW |
422 | ; |
423 | to = read_filename_string (ch, f); | |
424 | ||
425 | ptr = ((struct file_name_map *) | |
426 | xmalloc (sizeof (struct file_name_map))); | |
427 | ptr->map_from = from; | |
428 | ||
429 | /* Make the real filename absolute. */ | |
430 | if (*to == '/') | |
431 | ptr->map_to = to; | |
432 | else | |
433 | { | |
434 | ptr->map_to = xmalloc (dirlen + strlen (to) + 2); | |
435 | strcpy (ptr->map_to, dirname); | |
436 | ptr->map_to[dirlen] = '/'; | |
437 | strcpy (ptr->map_to + dirlen + 1, to); | |
438 | free (to); | |
439 | } | |
440 | ||
441 | ptr->map_next = map_list_ptr->map_list_map; | |
442 | map_list_ptr->map_list_map = ptr; | |
443 | ||
444 | while ((ch = getc (f)) != '\n') | |
445 | if (ch == EOF) | |
446 | break; | |
447 | } | |
448 | fclose (f); | |
449 | } | |
450 | ||
ae79697b ZW |
451 | map_list_ptr->map_list_next = CPP_OPTION (pfile, map_list); |
452 | CPP_OPTION (pfile, map_list) = map_list_ptr; | |
add7091b ZW |
453 | |
454 | return map_list_ptr->map_list_map; | |
455 | } | |
456 | ||
0b3d776a | 457 | /* Remap NAME based on the file_name_map (if any) for LOC. */ |
add7091b | 458 | |
0b3d776a ZW |
459 | static char * |
460 | remap_filename (pfile, name, loc) | |
add7091b | 461 | cpp_reader *pfile; |
0b3d776a ZW |
462 | char *name; |
463 | struct file_name_list *loc; | |
add7091b | 464 | { |
0b3d776a | 465 | struct file_name_map *map; |
460ee112 | 466 | const char *from, *p, *dir; |
add7091b | 467 | |
0b3d776a ZW |
468 | if (! loc->name_map) |
469 | loc->name_map = read_name_map (pfile, | |
470 | loc->name | |
471 | ? loc->name : "."); | |
add7091b | 472 | |
0b3d776a ZW |
473 | if (loc->name_map == (struct file_name_map *)-1) |
474 | return name; | |
475 | ||
476 | from = name + strlen (loc->name) + 1; | |
477 | ||
478 | for (map = loc->name_map; map; map = map->map_next) | |
479 | if (!strcmp (map->map_from, from)) | |
480 | return map->map_to; | |
481 | ||
482 | /* Try to find a mapping file for the particular directory we are | |
483 | looking in. Thus #include <sys/types.h> will look up sys/types.h | |
484 | in /usr/include/header.gcc and look up types.h in | |
485 | /usr/include/sys/header.gcc. */ | |
7ceb3598 | 486 | p = strrchr (name, '/'); |
0b3d776a ZW |
487 | if (!p) |
488 | p = name; | |
489 | if (loc && loc->name | |
490 | && strlen (loc->name) == (size_t) (p - name) | |
491 | && !strncmp (loc->name, name, p - name)) | |
492 | /* FILENAME is in SEARCHPTR, which we've already checked. */ | |
493 | return name; | |
494 | ||
495 | if (p == name) | |
496 | { | |
497 | dir = "."; | |
498 | from = name; | |
499 | } | |
500 | else | |
501 | { | |
460ee112 | 502 | char * newdir = (char *) alloca (p - name + 1); |
7ceb3598 | 503 | memcpy (newdir, name, p - name); |
460ee112 KG |
504 | newdir[p - name] = '\0'; |
505 | dir = newdir; | |
0b3d776a | 506 | from = p + 1; |
add7091b | 507 | } |
0b3d776a ZW |
508 | |
509 | for (map = read_name_map (pfile, dir); map; map = map->map_next) | |
510 | if (! strcmp (map->map_from, name)) | |
511 | return map->map_to; | |
add7091b | 512 | |
0b3d776a | 513 | return name; |
add7091b ZW |
514 | } |
515 | ||
168d3732 ZW |
516 | |
517 | void | |
12cf91fe | 518 | _cpp_execute_include (pfile, f, len, no_reinclude, search_start) |
168d3732 | 519 | cpp_reader *pfile; |
12cf91fe | 520 | U_CHAR *f; |
168d3732 ZW |
521 | unsigned int len; |
522 | int no_reinclude; | |
523 | struct file_name_list *search_start; | |
524 | { | |
525 | IHASH *ihash; | |
12cf91fe | 526 | char *fname = (char *)f; |
168d3732 ZW |
527 | int fd; |
528 | int angle_brackets = fname[0] == '<'; | |
529 | int before; | |
530 | ||
531 | if (!search_start) | |
532 | { | |
533 | if (angle_brackets) | |
ae79697b ZW |
534 | search_start = CPP_OPTION (pfile, bracket_include); |
535 | else if (CPP_OPTION (pfile, ignore_srcdir)) | |
536 | search_start = CPP_OPTION (pfile, quote_include); | |
168d3732 ZW |
537 | else |
538 | search_start = CPP_BUFFER (pfile)->actual_dir; | |
539 | } | |
540 | ||
541 | if (!search_start) | |
542 | { | |
543 | cpp_error (pfile, "No include path in which to find %s", fname); | |
544 | return; | |
545 | } | |
546 | ||
547 | /* Remove quote marks. */ | |
548 | fname++; | |
549 | len -= 2; | |
550 | fname[len] = '\0'; | |
551 | ||
552 | fd = find_include_file (pfile, fname, search_start, &ihash, &before); | |
553 | ||
554 | if (fd == -2) | |
555 | return; | |
556 | ||
557 | if (fd == -1) | |
558 | { | |
ae79697b | 559 | if (CPP_OPTION (pfile, print_deps_missing_files) |
168d3732 ZW |
560 | && CPP_PRINT_DEPS (pfile) > (angle_brackets || |
561 | (pfile->system_include_depth > 0))) | |
562 | { | |
563 | if (!angle_brackets) | |
564 | deps_add_dep (pfile->deps, fname); | |
565 | else | |
566 | { | |
567 | char *p; | |
568 | struct file_name_list *ptr; | |
569 | /* If requested as a system header, assume it belongs in | |
570 | the first system header directory. */ | |
ae79697b ZW |
571 | if (CPP_OPTION (pfile, bracket_include)) |
572 | ptr = CPP_OPTION (pfile, bracket_include); | |
168d3732 | 573 | else |
ae79697b | 574 | ptr = CPP_OPTION (pfile, quote_include); |
168d3732 ZW |
575 | |
576 | p = (char *) alloca (strlen (ptr->name) | |
577 | + strlen (fname) + 2); | |
578 | if (*ptr->name != '\0') | |
579 | { | |
580 | strcpy (p, ptr->name); | |
581 | strcat (p, "/"); | |
582 | } | |
583 | strcat (p, fname); | |
584 | deps_add_dep (pfile->deps, p); | |
585 | } | |
586 | } | |
587 | /* If -M was specified, and this header file won't be added to | |
588 | the dependency list, then don't count this as an error, | |
589 | because we can still produce correct output. Otherwise, we | |
590 | can't produce correct output, because there may be | |
591 | dependencies we need inside the missing file, and we don't | |
592 | know what directory this missing file exists in. */ | |
593 | else if (CPP_PRINT_DEPS (pfile) | |
594 | && (CPP_PRINT_DEPS (pfile) | |
595 | <= (angle_brackets || (pfile->system_include_depth > 0)))) | |
596 | cpp_warning (pfile, "No include path in which to find %s", fname); | |
597 | else | |
598 | cpp_error_from_errno (pfile, fname); | |
599 | ||
600 | return; | |
601 | } | |
602 | ||
603 | /* For -M, add the file to the dependencies on its first inclusion. */ | |
604 | if (!before && (CPP_PRINT_DEPS (pfile) | |
605 | > (angle_brackets || (pfile->system_include_depth > 0)))) | |
606 | deps_add_dep (pfile->deps, ihash->name); | |
607 | ||
608 | /* Handle -H option. */ | |
ae79697b | 609 | if (CPP_OPTION (pfile, print_include_names)) |
168d3732 ZW |
610 | { |
611 | cpp_buffer *fp = CPP_BUFFER (pfile); | |
612 | while ((fp = CPP_PREV_BUFFER (fp)) != NULL) | |
613 | putc ('.', stderr); | |
614 | fprintf (stderr, " %s\n", ihash->name); | |
615 | } | |
616 | ||
f2d5f0cc | 617 | /* Actually process the file. */ |
168d3732 | 618 | if (no_reinclude) |
12cf91fe | 619 | ihash->control_macro = U""; |
168d3732 ZW |
620 | |
621 | if (read_include_file (pfile, fd, ihash)) | |
622 | { | |
168d3732 | 623 | if (angle_brackets) |
c56c2073 | 624 | pfile->system_include_depth++; |
168d3732 ZW |
625 | } |
626 | } | |
627 | ||
628 | ||
c45da1ca ZW |
629 | /* Push an input buffer and load it up with the contents of FNAME. |
630 | If FNAME is "" or NULL, read standard input. */ | |
631 | int | |
632 | cpp_read_file (pfile, fname) | |
633 | cpp_reader *pfile; | |
634 | const char *fname; | |
635 | { | |
d35364d1 ZW |
636 | IHASH *ih, **slot; |
637 | IHASH dummy; | |
c45da1ca ZW |
638 | int f; |
639 | ||
d35364d1 ZW |
640 | if (fname == NULL) |
641 | fname = ""; | |
642 | ||
d35364d1 | 643 | dummy.nshort = fname; |
c6b5cb46 ZW |
644 | /* _cpp_calc_hash doesn't like zero-length strings. */ |
645 | if (*fname == 0) | |
646 | dummy.hash = 0; | |
647 | else | |
12cf91fe | 648 | dummy.hash = _cpp_calc_hash ((const U_CHAR *)fname, strlen (fname)); |
29a72a4f ZW |
649 | slot = (IHASH **) htab_find_slot_with_hash (pfile->all_include_files, |
650 | (const void *) &dummy, | |
e38992e8 | 651 | dummy.hash, INSERT); |
d35364d1 | 652 | if (*slot && (ih = redundant_include_p (pfile, *slot, ABSOLUTE_PATH))) |
c45da1ca | 653 | { |
e38992e8 | 654 | if (ih == (IHASH *) -1) |
d35364d1 | 655 | return 1; /* Already included. */ |
c45da1ca | 656 | } |
d35364d1 | 657 | else |
f2d5f0cc | 658 | ih = make_IHASH (fname, 0, ABSOLUTE_PATH, dummy.hash, slot); |
c45da1ca | 659 | |
d35364d1 ZW |
660 | if (*fname == '\0') |
661 | f = 0; | |
662 | else | |
e576beb0 | 663 | f = open_include_file (pfile, fname); |
c45da1ca | 664 | |
168d3732 | 665 | return read_include_file (pfile, f, ih); |
c45da1ca ZW |
666 | } |
667 | ||
0b3d776a ZW |
668 | /* Read the contents of FD into the buffer on the top of PFILE's stack. |
669 | IHASH points to the include hash entry for the file associated with | |
670 | FD. | |
add7091b ZW |
671 | |
672 | The caller is responsible for the cpp_push_buffer. */ | |
673 | ||
168d3732 ZW |
674 | static int |
675 | read_include_file (pfile, fd, ihash) | |
add7091b | 676 | cpp_reader *pfile; |
0b3d776a | 677 | int fd; |
38b24ee2 | 678 | IHASH *ihash; |
add7091b ZW |
679 | { |
680 | struct stat st; | |
681 | size_t st_size; | |
554fbeef | 682 | long length; |
0b3d776a | 683 | cpp_buffer *fp; |
add7091b | 684 | |
d35364d1 ZW |
685 | fp = cpp_push_buffer (pfile, NULL, 0); |
686 | ||
687 | if (fp == 0) | |
688 | goto push_fail; | |
689 | ||
0b3d776a ZW |
690 | if (fstat (fd, &st) < 0) |
691 | goto perror_fail; | |
554fbeef | 692 | |
6458033d ZW |
693 | /* If fd points to a plain file, we know how big it is, so we can |
694 | allocate the buffer all at once. If fd is a pipe or terminal, we | |
695 | can't. Most C source files are 4k or less, so we guess that. If | |
10e56506 ZW |
696 | fd is something weird, like a directory, we don't want to read it |
697 | at all. | |
6458033d ZW |
698 | |
699 | Unfortunately, different systems use different st.st_mode values | |
700 | for pipes: some have S_ISFIFO, some S_ISSOCK, some are buggy and | |
10e56506 ZW |
701 | zero the entire struct stat except a couple fields. Hence we don't |
702 | even try to figure out what something is, except for plain files, | |
703 | directories, and block devices. | |
6458033d ZW |
704 | |
705 | In all cases, read_and_prescan will resize the buffer if it | |
706 | turns out there's more data than we thought. */ | |
707 | ||
0b3d776a ZW |
708 | if (S_ISREG (st.st_mode)) |
709 | { | |
554fbeef ZW |
710 | /* off_t might have a wider range than size_t - in other words, |
711 | the max size of a file might be bigger than the address | |
6458033d ZW |
712 | space. We can't handle a file that large. (Anyone with |
713 | a single source file bigger than 4GB needs to rethink | |
714 | their coding style.) */ | |
0b3d776a | 715 | st_size = (size_t) st.st_size; |
e915b770 KG |
716 | if ((unsigned HOST_WIDEST_INT) st_size |
717 | != (unsigned HOST_WIDEST_INT) st.st_size) | |
554fbeef ZW |
718 | { |
719 | cpp_error (pfile, "file `%s' is too large", ihash->name); | |
720 | goto fail; | |
721 | } | |
add7091b | 722 | } |
10e56506 | 723 | else if (S_ISBLK (st.st_mode)) |
0b3d776a | 724 | { |
10e56506 ZW |
725 | cpp_error (pfile, "%s is a block device", ihash->name); |
726 | goto fail; | |
0b3d776a | 727 | } |
10e56506 | 728 | else if (S_ISDIR (st.st_mode)) |
0b3d776a | 729 | { |
10e56506 | 730 | cpp_error (pfile, "%s is a directory", ihash->name); |
554fbeef | 731 | goto fail; |
add7091b | 732 | } |
10e56506 ZW |
733 | else |
734 | { | |
735 | /* We don't know how big this is. 4k is a decent first guess. */ | |
736 | st_size = 4096; | |
737 | } | |
add7091b | 738 | |
554fbeef ZW |
739 | /* Read the file, converting end-of-line characters and trigraphs |
740 | (if enabled). */ | |
741 | fp->ihash = ihash; | |
38b24ee2 | 742 | fp->nominal_fname = ihash->name; |
45b966db | 743 | length = _cpp_read_and_prescan (pfile, fp, fd, st_size); |
554fbeef ZW |
744 | if (length < 0) |
745 | goto fail; | |
746 | if (length == 0) | |
12cf91fe | 747 | ihash->control_macro = U""; /* never re-include */ |
add7091b | 748 | |
0b3d776a | 749 | close (fd); |
ff2b53ef | 750 | fp->rlimit = fp->buf + length; |
554fbeef | 751 | fp->cur = fp->buf; |
0b22d65c ZW |
752 | if (ihash->foundhere != ABSOLUTE_PATH) |
753 | fp->system_header_p = ihash->foundhere->sysp; | |
554fbeef | 754 | fp->lineno = 1; |
099a9dd0 | 755 | fp->line_base = fp->buf; |
554fbeef ZW |
756 | |
757 | /* The ->actual_dir field is only used when ignore_srcdir is not in effect; | |
758 | see do_include */ | |
ae79697b | 759 | if (!CPP_OPTION (pfile, ignore_srcdir)) |
38b24ee2 | 760 | fp->actual_dir = actual_directory (pfile, ihash->name); |
554fbeef | 761 | |
add7091b | 762 | pfile->input_stack_listing_current = 0; |
d35364d1 | 763 | pfile->only_seen_white = 2; |
add7091b ZW |
764 | return 1; |
765 | ||
0b3d776a | 766 | perror_fail: |
0b3d776a ZW |
767 | cpp_error_from_errno (pfile, ihash->name); |
768 | fail: | |
554fbeef | 769 | cpp_pop_buffer (pfile); |
d35364d1 | 770 | push_fail: |
0b3d776a ZW |
771 | close (fd); |
772 | return 0; | |
add7091b ZW |
773 | } |
774 | ||
6458033d ZW |
775 | /* Given a path FNAME, extract the directory component and place it |
776 | onto the actual_dirs list. Return a pointer to the allocated | |
777 | file_name_list structure. These structures are used to implement | |
778 | current-directory "" include searching. */ | |
779 | ||
f1a86df6 ZW |
780 | static struct file_name_list * |
781 | actual_directory (pfile, fname) | |
782 | cpp_reader *pfile; | |
bcc5cac9 | 783 | const char *fname; |
f1a86df6 ZW |
784 | { |
785 | char *last_slash, *dir; | |
786 | size_t dlen; | |
787 | struct file_name_list *x; | |
788 | ||
c49445e0 | 789 | dir = xstrdup (fname); |
7ceb3598 | 790 | last_slash = strrchr (dir, '/'); |
f1a86df6 ZW |
791 | if (last_slash) |
792 | { | |
793 | if (last_slash == dir) | |
794 | { | |
795 | dlen = 1; | |
796 | last_slash[1] = '\0'; | |
797 | } | |
798 | else | |
799 | { | |
800 | dlen = last_slash - dir; | |
801 | *last_slash = '\0'; | |
802 | } | |
803 | } | |
804 | else | |
805 | { | |
806 | dir[0] = '.'; | |
807 | dir[1] = '\0'; | |
808 | dlen = 1; | |
809 | } | |
810 | ||
811 | if (dlen > pfile->max_include_len) | |
812 | pfile->max_include_len = dlen; | |
813 | ||
814 | for (x = pfile->actual_dirs; x; x = x->alloc) | |
815 | if (!strcmp (x->name, dir)) | |
816 | { | |
817 | free (dir); | |
818 | return x; | |
819 | } | |
820 | ||
821 | /* Not found, make a new one. */ | |
822 | x = (struct file_name_list *) xmalloc (sizeof (struct file_name_list)); | |
823 | x->name = dir; | |
824 | x->nlen = dlen; | |
ae79697b | 825 | x->next = CPP_OPTION (pfile, quote_include); |
f1a86df6 | 826 | x->alloc = pfile->actual_dirs; |
0b22d65c | 827 | x->sysp = CPP_BUFFER (pfile)->system_header_p; |
f1a86df6 ZW |
828 | x->name_map = NULL; |
829 | ||
830 | pfile->actual_dirs = x; | |
831 | return x; | |
832 | } | |
833 | ||
0b3d776a ZW |
834 | /* Simplify a path name in place, deleting redundant components. This |
835 | reduces OS overhead and guarantees that equivalent paths compare | |
836 | the same (modulo symlinks). | |
837 | ||
838 | Transforms made: | |
839 | foo/bar/../quux foo/quux | |
840 | foo/./bar foo/bar | |
841 | foo//bar foo/bar | |
842 | /../quux /quux | |
843 | //quux //quux (POSIX allows leading // as a namespace escape) | |
844 | ||
845 | Guarantees no trailing slashes. All transforms reduce the length | |
846 | of the string. | |
847 | */ | |
0b22d65c | 848 | void |
b0699dad | 849 | _cpp_simplify_pathname (path) |
21380ab0 | 850 | char *path; |
0b3d776a ZW |
851 | { |
852 | char *from, *to; | |
853 | char *base; | |
854 | int absolute = 0; | |
855 | ||
509781a4 | 856 | #if defined (HAVE_DOS_BASED_FILE_SYSTEM) |
0b3d776a ZW |
857 | /* Convert all backslashes to slashes. */ |
858 | for (from = path; *from; from++) | |
859 | if (*from == '\\') *from = '/'; | |
860 | ||
861 | /* Skip over leading drive letter if present. */ | |
862 | if (ISALPHA (path[0]) && path[1] == ':') | |
863 | from = to = &path[2]; | |
864 | else | |
865 | from = to = path; | |
866 | #else | |
867 | from = to = path; | |
868 | #endif | |
869 | ||
870 | /* Remove redundant initial /s. */ | |
871 | if (*from == '/') | |
872 | { | |
873 | absolute = 1; | |
874 | to++; | |
875 | from++; | |
876 | if (*from == '/') | |
877 | { | |
878 | if (*++from == '/') | |
879 | /* 3 or more initial /s are equivalent to 1 /. */ | |
880 | while (*++from == '/'); | |
881 | else | |
882 | /* On some hosts // differs from /; Posix allows this. */ | |
883 | to++; | |
884 | } | |
885 | } | |
886 | base = to; | |
887 | ||
888 | for (;;) | |
889 | { | |
890 | while (*from == '/') | |
891 | from++; | |
892 | ||
893 | if (from[0] == '.' && from[1] == '/') | |
894 | from += 2; | |
895 | else if (from[0] == '.' && from[1] == '\0') | |
896 | goto done; | |
897 | else if (from[0] == '.' && from[1] == '.' && from[2] == '/') | |
898 | { | |
899 | if (base == to) | |
900 | { | |
901 | if (absolute) | |
902 | from += 3; | |
903 | else | |
904 | { | |
905 | *to++ = *from++; | |
906 | *to++ = *from++; | |
907 | *to++ = *from++; | |
908 | base = to; | |
909 | } | |
910 | } | |
911 | else | |
912 | { | |
913 | to -= 2; | |
914 | while (to > base && *to != '/') to--; | |
915 | if (*to == '/') | |
916 | to++; | |
917 | from += 3; | |
918 | } | |
919 | } | |
920 | else if (from[0] == '.' && from[1] == '.' && from[2] == '\0') | |
921 | { | |
922 | if (base == to) | |
923 | { | |
924 | if (!absolute) | |
925 | { | |
926 | *to++ = *from++; | |
927 | *to++ = *from++; | |
928 | } | |
929 | } | |
930 | else | |
931 | { | |
932 | to -= 2; | |
933 | while (to > base && *to != '/') to--; | |
934 | if (*to == '/') | |
935 | to++; | |
936 | } | |
937 | goto done; | |
938 | } | |
939 | else | |
940 | /* Copy this component and trailing /, if any. */ | |
941 | while ((*to++ = *from++) != '/') | |
942 | { | |
943 | if (!to[-1]) | |
944 | { | |
945 | to--; | |
946 | goto done; | |
947 | } | |
948 | } | |
949 | ||
950 | } | |
951 | ||
952 | done: | |
953 | /* Trim trailing slash */ | |
954 | if (to[0] == '/' && (!absolute || to > path+1)) | |
955 | to--; | |
956 | ||
957 | /* Change the empty string to "." so that stat() on the result | |
958 | will always work. */ | |
959 | if (to == path) | |
960 | *to++ = '.'; | |
961 | ||
962 | *to = '\0'; | |
963 | ||
964 | return; | |
965 | } | |
966 | ||
967 | /* It is not clear when this should be used if at all, so I've | |
968 | disabled it until someone who understands VMS can look at it. */ | |
969 | #if 0 | |
add7091b ZW |
970 | |
971 | /* Under VMS we need to fix up the "include" specification filename. | |
972 | ||
973 | Rules for possible conversions | |
974 | ||
975 | fullname tried paths | |
976 | ||
977 | name name | |
978 | ./dir/name [.dir]name | |
979 | /dir/name dir:name | |
980 | /name [000000]name, name | |
981 | dir/name dir:[000000]name, dir:name, dir/name | |
982 | dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name | |
983 | path:/name path:[000000]name, path:name | |
984 | path:/dir/name path:[000000.dir]name, path:[dir]name | |
985 | path:dir/name path:[dir]name | |
986 | [path]:[dir]name [path.dir]name | |
987 | path/[dir]name [path.dir]name | |
988 | ||
989 | The path:/name input is constructed when expanding <> includes. */ | |
990 | ||
991 | ||
992 | static void | |
993 | hack_vms_include_specification (fullname) | |
994 | char *fullname; | |
995 | { | |
996 | register char *basename, *unixname, *local_ptr, *first_slash; | |
997 | int f, check_filename_before_returning, must_revert; | |
998 | char Local[512]; | |
999 | ||
1000 | check_filename_before_returning = 0; | |
1001 | must_revert = 0; | |
1002 | /* See if we can find a 1st slash. If not, there's no path information. */ | |
7ceb3598 | 1003 | first_slash = strchr (fullname, '/'); |
add7091b ZW |
1004 | if (first_slash == 0) |
1005 | return 0; /* Nothing to do!!! */ | |
1006 | ||
1007 | /* construct device spec if none given. */ | |
1008 | ||
7ceb3598 | 1009 | if (strchr (fullname, ':') == 0) |
add7091b ZW |
1010 | { |
1011 | ||
1012 | /* If fullname has a slash, take it as device spec. */ | |
1013 | ||
1014 | if (first_slash == fullname) | |
1015 | { | |
7ceb3598 | 1016 | first_slash = strchr (fullname + 1, '/'); /* 2nd slash ? */ |
add7091b ZW |
1017 | if (first_slash) |
1018 | *first_slash = ':'; /* make device spec */ | |
1019 | for (basename = fullname; *basename != 0; basename++) | |
1020 | *basename = *(basename+1); /* remove leading slash */ | |
1021 | } | |
1022 | else if ((first_slash[-1] != '.') /* keep ':/', './' */ | |
1023 | && (first_slash[-1] != ':') | |
1024 | && (first_slash[-1] != ']')) /* or a vms path */ | |
1025 | { | |
1026 | *first_slash = ':'; | |
1027 | } | |
1028 | else if ((first_slash[1] == '[') /* skip './' in './[dir' */ | |
1029 | && (first_slash[-1] == '.')) | |
1030 | fullname += 2; | |
1031 | } | |
1032 | ||
1033 | /* Get part after first ':' (basename[-1] == ':') | |
1034 | or last '/' (basename[-1] == '/'). */ | |
1035 | ||
1036 | basename = base_name (fullname); | |
1037 | ||
1038 | local_ptr = Local; /* initialize */ | |
1039 | ||
1040 | /* We are trying to do a number of things here. First of all, we are | |
1041 | trying to hammer the filenames into a standard format, such that later | |
1042 | processing can handle them. | |
1043 | ||
1044 | If the file name contains something like [dir.], then it recognizes this | |
1045 | as a root, and strips the ".]". Later processing will add whatever is | |
1046 | needed to get things working properly. | |
1047 | ||
1048 | If no device is specified, then the first directory name is taken to be | |
1049 | a device name (or a rooted logical). */ | |
1050 | ||
1051 | /* Point to the UNIX filename part (which needs to be fixed!) | |
1052 | but skip vms path information. | |
1053 | [basename != fullname since first_slash != 0]. */ | |
1054 | ||
1055 | if ((basename[-1] == ':') /* vms path spec. */ | |
1056 | || (basename[-1] == ']') | |
1057 | || (basename[-1] == '>')) | |
1058 | unixname = basename; | |
1059 | else | |
1060 | unixname = fullname; | |
1061 | ||
1062 | if (*unixname == '/') | |
1063 | unixname++; | |
1064 | ||
1065 | /* If the directory spec is not rooted, we can just copy | |
1066 | the UNIX filename part and we are done. */ | |
1067 | ||
1068 | if (((basename - fullname) > 1) | |
1069 | && ( (basename[-1] == ']') | |
1070 | || (basename[-1] == '>'))) | |
1071 | { | |
1072 | if (basename[-2] != '.') | |
1073 | { | |
1074 | ||
1075 | /* The VMS part ends in a `]', and the preceding character is not a `.'. | |
1076 | -> PATH]:/name (basename = '/name', unixname = 'name') | |
1077 | We strip the `]', and then splice the two parts of the name in the | |
86702e31 | 1078 | usual way. Given the default locations for include files, |
add7091b ZW |
1079 | we will only use this code if the user specifies alternate locations |
1080 | with the /include (-I) switch on the command line. */ | |
1081 | ||
1082 | basename -= 1; /* Strip "]" */ | |
1083 | unixname--; /* backspace */ | |
1084 | } | |
1085 | else | |
1086 | { | |
1087 | ||
1088 | /* The VMS part has a ".]" at the end, and this will not do. Later | |
1089 | processing will add a second directory spec, and this would be a syntax | |
1090 | error. Thus we strip the ".]", and thus merge the directory specs. | |
1091 | We also backspace unixname, so that it points to a '/'. This inhibits the | |
1092 | generation of the 000000 root directory spec (which does not belong here | |
1093 | in this case). */ | |
1094 | ||
1095 | basename -= 2; /* Strip ".]" */ | |
1096 | unixname--; /* backspace */ | |
1097 | } | |
1098 | } | |
1099 | ||
1100 | else | |
1101 | ||
1102 | { | |
1103 | ||
1104 | /* We drop in here if there is no VMS style directory specification yet. | |
1105 | If there is no device specification either, we make the first dir a | |
1106 | device and try that. If we do not do this, then we will be essentially | |
1107 | searching the users default directory (as if they did a #include "asdf.h"). | |
1108 | ||
1109 | Then all we need to do is to push a '[' into the output string. Later | |
1110 | processing will fill this in, and close the bracket. */ | |
1111 | ||
1112 | if ((unixname != fullname) /* vms path spec found. */ | |
1113 | && (basename[-1] != ':')) | |
1114 | *local_ptr++ = ':'; /* dev not in spec. take first dir */ | |
1115 | ||
1116 | *local_ptr++ = '['; /* Open the directory specification */ | |
1117 | } | |
1118 | ||
1119 | if (unixname == fullname) /* no vms dir spec. */ | |
1120 | { | |
1121 | must_revert = 1; | |
1122 | if ((first_slash != 0) /* unix dir spec. */ | |
1123 | && (*unixname != '/') /* not beginning with '/' */ | |
1124 | && (*unixname != '.')) /* or './' or '../' */ | |
1125 | *local_ptr++ = '.'; /* dir is local ! */ | |
1126 | } | |
1127 | ||
1128 | /* at this point we assume that we have the device spec, and (at least | |
1129 | the opening "[" for a directory specification. We may have directories | |
1130 | specified already. | |
1131 | ||
1132 | If there are no other slashes then the filename will be | |
1133 | in the "root" directory. Otherwise, we need to add | |
1134 | directory specifications. */ | |
1135 | ||
7ceb3598 | 1136 | if (strchr (unixname, '/') == 0) |
add7091b ZW |
1137 | { |
1138 | /* if no directories specified yet and none are following. */ | |
1139 | if (local_ptr[-1] == '[') | |
1140 | { | |
1141 | /* Just add "000000]" as the directory string */ | |
1142 | strcpy (local_ptr, "000000]"); | |
1143 | local_ptr += strlen (local_ptr); | |
1144 | check_filename_before_returning = 1; /* we might need to fool with this later */ | |
1145 | } | |
1146 | } | |
1147 | else | |
1148 | { | |
1149 | ||
1150 | /* As long as there are still subdirectories to add, do them. */ | |
7ceb3598 | 1151 | while (strchr (unixname, '/') != 0) |
add7091b ZW |
1152 | { |
1153 | /* If this token is "." we can ignore it | |
1154 | if it's not at the beginning of a path. */ | |
1155 | if ((unixname[0] == '.') && (unixname[1] == '/')) | |
1156 | { | |
1157 | /* remove it at beginning of path. */ | |
1158 | if ( ((unixname == fullname) /* no device spec */ | |
1159 | && (fullname+2 != basename)) /* starts with ./ */ | |
1160 | /* or */ | |
1161 | || ((basename[-1] == ':') /* device spec */ | |
1162 | && (unixname-1 == basename))) /* and ./ afterwards */ | |
1163 | *local_ptr++ = '.'; /* make '[.' start of path. */ | |
1164 | unixname += 2; | |
1165 | continue; | |
1166 | } | |
1167 | ||
1168 | /* Add a subdirectory spec. Do not duplicate "." */ | |
1169 | if ( local_ptr[-1] != '.' | |
1170 | && local_ptr[-1] != '[' | |
1171 | && local_ptr[-1] != '<') | |
1172 | *local_ptr++ = '.'; | |
1173 | ||
1174 | /* If this is ".." then the spec becomes "-" */ | |
1175 | if ( (unixname[0] == '.') | |
1176 | && (unixname[1] == '.') | |
1177 | && (unixname[2] == '/')) | |
1178 | { | |
1179 | /* Add "-" and skip the ".." */ | |
1180 | if ((local_ptr[-1] == '.') | |
1181 | && (local_ptr[-2] == '[')) | |
1182 | local_ptr--; /* prevent [.- */ | |
1183 | *local_ptr++ = '-'; | |
1184 | unixname += 3; | |
1185 | continue; | |
1186 | } | |
1187 | ||
1188 | /* Copy the subdirectory */ | |
1189 | while (*unixname != '/') | |
1190 | *local_ptr++= *unixname++; | |
1191 | ||
1192 | unixname++; /* Skip the "/" */ | |
1193 | } | |
1194 | ||
1195 | /* Close the directory specification */ | |
1196 | if (local_ptr[-1] == '.') /* no trailing periods */ | |
1197 | local_ptr--; | |
1198 | ||
1199 | if (local_ptr[-1] == '[') /* no dir needed */ | |
1200 | local_ptr--; | |
1201 | else | |
1202 | *local_ptr++ = ']'; | |
1203 | } | |
1204 | ||
1205 | /* Now add the filename. */ | |
1206 | ||
1207 | while (*unixname) | |
1208 | *local_ptr++ = *unixname++; | |
1209 | *local_ptr = 0; | |
1210 | ||
1211 | /* Now append it to the original VMS spec. */ | |
1212 | ||
1213 | strcpy ((must_revert==1)?fullname:basename, Local); | |
1214 | ||
1215 | /* If we put a [000000] in the filename, try to open it first. If this fails, | |
1216 | remove the [000000], and return that name. This provides flexibility | |
1217 | to the user in that they can use both rooted and non-rooted logical names | |
1218 | to point to the location of the file. */ | |
1219 | ||
1220 | if (check_filename_before_returning) | |
1221 | { | |
e576beb0 | 1222 | f = open (fullname, O_RDONLY|O_NONBLOCK); |
add7091b ZW |
1223 | if (f >= 0) |
1224 | { | |
1225 | /* The file name is OK as it is, so return it as is. */ | |
1226 | close (f); | |
1227 | return 1; | |
1228 | } | |
1229 | ||
1230 | /* The filename did not work. Try to remove the [000000] from the name, | |
1231 | and return it. */ | |
1232 | ||
7ceb3598 NB |
1233 | basename = strchr (fullname, '['); |
1234 | local_ptr = strchr (fullname, ']') + 1; | |
add7091b ZW |
1235 | strcpy (basename, local_ptr); /* this gets rid of it */ |
1236 | ||
1237 | } | |
1238 | ||
1239 | return 1; | |
1240 | } | |
1241 | #endif /* VMS */ |