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