Debian Codegraph @

src/c/pathname.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * pathname.d - pathnames
6 *
7 * Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya
8 * Copyright (c) 1990 Giuseppe Attardi
9 * Copyright (c) 2001 Juan Jose Garcia Ripoll
10 * Copyright (c) 2015 Daniel Kochmański
11 *
12 * See file 'LICENSE' for the copyright details.
13 *
14 */
15
16 /*
17 O.S. DEPENDENT
18
19 This file contains those functions that interpret namestrings.
20 */
21
22 #include <ecl/ecl.h>
23 #include <ecl/ecl-inl.h>
24 #include <ecl/internal.h>
25 #include <limits.h>
26 #include <string.h>
27 #include <ctype.h>
28
29 typedef int (*delim_fn)(int);
30
31 /*
32 * Translates a string into the host's preferred case.
33 * See CLHS 19.2.2.1.2.2 Common Case in Pathname Components.
34 */
35 /* We use UN*X conventions, so lower case is default.
36 * However, this really should be conditionalised to the OS type,
37 * and it should translate to _opposite_ of the local case.
38 */
39
40 static cl_object
41 normalize_case(cl_object path, cl_object cas)
42 {
43 if (cas == @':local') {
44 if (path->pathname.logical)
45 return @':upcase';
46 return @':downcase';
47 } else if (cas == @':common' || cas == @':downcase' || cas == @':upcase') {
48 return cas;
49 } else {
50 FEerror("Not a valid pathname case :~%~A", 1, cas);
51 }
52 }
53
54 static bool
55 in_local_case_p(cl_object str, cl_object cas)
56 {
57 if (cas == @':downcase')
58 return ecl_string_case(str) < 0;
59 return 1;
60 }
61
62 static bool
63 in_antilocal_case_p(cl_object str, cl_object cas)
64 {
65 if (cas == @':downcase')
66 return ecl_string_case(str) > 0;
67 return 0;
68 }
69
70 static cl_object
71 to_local_case(cl_object str, cl_object cas)
72 {
73 if (cas == @':downcase')
74 return cl_string_downcase(1, str);
75 return cl_string_upcase(1, str);
76 }
77
78 static cl_object
79 host_case(cl_object host)
80 {
81 if (Null(host))
82 return @':local';
83 if (ecl_logical_hostname_p(host))
84 return @':upcase';
85 return @':downcase';
86 }
87
88 static cl_object
89 to_antilocal_case(cl_object str, cl_object cas)
90 {
91 if (cas == @':downcase')
92 return cl_string_upcase(1, str);
93 return cl_string_upcase(1, str);
94 }
95
96 static cl_object
97 translate_from_common(cl_object str, cl_object tocase)
98 {
99 int string_case = ecl_string_case(str);
100 if (string_case > 0) { /* ALL_UPPER */
101 return to_local_case(str, tocase);
102 } else if (string_case < 0) { /* ALL_LOWER */
103 return to_antilocal_case(str, tocase);
104 } else { /* Mixed case goes unchanged */
105 return str;
106 }
107 }
108
109 static cl_object
110 translate_to_common(cl_object str, cl_object fromcase)
111 {
112 if (in_local_case_p(str, fromcase)) {
113 return cl_string_upcase(1, str);
114 } else if (in_antilocal_case_p(str, fromcase)) {
115 return cl_string_downcase(1, str);
116 } else {
117 return str;
118 }
119 }
120
121 static cl_object
122 translate_component_case(cl_object str, cl_object fromcase, cl_object tocase)
123 {
124 /* Pathnames may contain some other objects, such as symbols,
125 * numbers, etc, which need not be translated */
126 if (str == OBJNULL) {
127 return str;
128 } else if (!ECL_STRINGP(str)) {
129 return str;
130 } else if (tocase == fromcase) {
131 return str;
132 } else if (tocase == @':common') {
133 return translate_to_common(str, fromcase);
134 } else if (fromcase == @':common') {
135 return translate_from_common(str, tocase);
136 } else {
137 str = translate_to_common(str, fromcase);
138 return translate_from_common(str, tocase);
139 }
140 }
141
142 static cl_object
143 translate_list_case(cl_object list, cl_object fromcase, cl_object tocase)
144 {
145 /* If the argument is really a list, translate all strings in it and
146 * return this new list, else assume it is a string and translate it.
147 */
148 if (!CONSP(list)) {
149 return translate_component_case(list, fromcase, tocase);
150 } else {
151 cl_object l;
152 list = cl_copy_list(list);
153 for (l = list; !ecl_endp(l); l = CDR(l)) {
154 /* It is safe to pass anything to translate_component_case,
155 * because it will only transform strings, leaving other
156 * object (such as symbols) unchanged.*/
157 cl_object name = ECL_CONS_CAR(l);
158 name = ECL_LISTP(name)?
159 translate_list_case(name, fromcase, tocase) :
160 translate_component_case(name, fromcase, tocase);
161 ECL_RPLACA(l, name);
162 }
163 return list;
164 }
165 }
166
167 static void
168 push_substring(cl_object buffer, cl_object string, cl_index start, cl_index end)
169 {
170 string = cl_string(string);
171 while (start < end) {
172 ecl_string_push_extend(buffer, ecl_char(string, start));
173 start++;
174 }
175 }
176
177 static void
178 push_string(cl_object buffer, cl_object string)
179 {
180 push_substring(buffer, string, 0, ecl_length(string));
181 }
182
183 static cl_object
184 destructively_check_directory(cl_object directory, bool logical, bool delete_back)
185 {
186 /* This function performs two tasks
187 * 1) It ensures that the list is a valid directory list
188 * 2) It ensures that all strings in the list are valid C strings without fill pointer
189 * All strings are copied, thus avoiding problems with the user modifying the
190 * list that was passed to MAKE-PATHNAME.
191 * 3) Redundant :back are removed.
192 */
193 /* INV: directory is always a list */
194 cl_object ptr;
195 int i;
196
197 if (!LISTP(directory))
198 return @':error';
199 if (Null(directory))
200 return directory;
201 if (ECL_CONS_CAR(directory) != @':absolute' &&
202 ECL_CONS_CAR(directory) != @':relative')
203 return @':error';
204 BEGIN:
205 for (i=0, ptr=directory; CONSP(ptr); ptr = ECL_CONS_CDR(ptr), i++) {
206 cl_object item = ECL_CONS_CAR(ptr);
207 if (item == @':back') {
208 if (i == 0)
209 return @':error';
210 item = ecl_nth(i-1, directory);
211 if (item == @':absolute' || item == @':wild-inferiors')
212 return @':error';
213 if (delete_back && i >= 2) {
214 cl_object next = ECL_CONS_CDR(ptr);
215 ptr = ecl_nthcdr(i-2, directory);
216 ECL_RPLACD(ptr, next);
217 i = i-2;
218 }
219 } else if (item == @':up') {
220 if (i == 0)
221 return @':error';
222 item = ecl_nth(i-1, directory);
223 if (item == @':absolute' || item == @':wild-inferiors')
224 return @':error';
225 } else if (item == @':relative' || item == @':absolute') {
226 if (i > 0)
227 return @':error';
228 } else if (ecl_stringp(item)) {
229 cl_index l = ecl_length(item);
230 item = cl_copy_seq(item);
231 ECL_RPLACA(ptr, item);
232 if (logical)
233 continue;
234 if (l && ecl_char(item,0) == '.') {
235 if (l == 1) {
236 /* Single dot */
237 if (i == 0)
238 return @':error';
239 ECL_RPLACD(ecl_nthcdr(--i, directory),
240 ECL_CONS_CDR(ptr));
241 } else if (l == 2 && ecl_char(item,1) == '.') {
242 ECL_RPLACA(ptr, @':up');
243 goto BEGIN;
244 }
245 }
246 } else if (item != @':wild' && item != @':wild-inferiors') {
247 return @':error';
248 }
249 }
250 return directory;
251 }
252
253 cl_object
254 ecl_make_pathname(cl_object host, cl_object device, cl_object directory,
255 cl_object name, cl_object type, cl_object version,
256 cl_object fromcase)
257 {
258 cl_object x, p, component;
259
260 p = ecl_alloc_object(t_pathname);
261 if (ecl_stringp(host))
262 p->pathname.logical = ecl_logical_hostname_p(host);
263 else if (host == ECL_NIL)
264 p->pathname.logical = FALSE;
265 else {
266 x = directory;
267 component = @':host';
268 goto ERROR;
269 }
270 if (device != ECL_NIL && device != @':unspecific' &&
271 !(!p->pathname.logical && ecl_stringp(device))) {
272 x = device;
273 component = @':device';
274 goto ERROR;
275 }
276 if (name != ECL_NIL && name != @':wild' && !ecl_stringp(name)) {
277 x = name;
278 component = @':name';
279 goto ERROR;
280 }
281 if (type != ECL_NIL && type != @':unspecific' && type != @':wild' && !ecl_stringp(type)) {
282 x = type;
283 component = @':type';
284 goto ERROR;
285 }
286 if (version != @':unspecific' && version != @':newest' &&
287 version != @':wild' && version != ECL_NIL && !ECL_FIXNUMP(version))
288 {
289 x = version;
290 component = @':version';
291 ERROR: FEerror("~s is not a valid pathname-~a component", 2, x, component);
292 }
293 switch (ecl_t_of(directory)) {
294 #ifdef ECL_UNICODE
295 case t_string:
296 #endif
297 case t_base_string:
298 directory = cl_list(2, @':absolute', directory);
299 break;
300 case t_symbol:
301 if (directory == @':wild') {
302 directory = cl_list(2, @':absolute', @':wild-inferiors');
303 break;
304 }
305 x = directory;
306 component = @':directory';
307 goto ERROR;
308 case t_list:
309 directory = cl_copy_list(directory);
310 break;
311 default:
312 x = directory;
313 component = @':directory';
314 goto ERROR;
315 }
316 p->pathname.host = host;
317 {
318 cl_object tocase = normalize_case(p, @':local');
319 if (p->pathname.logical)
320 fromcase = @':common';
321 else
322 fromcase = normalize_case(p, fromcase);
323 p->pathname.host =
324 translate_component_case(host, fromcase, tocase);
325 p->pathname.device =
326 translate_component_case(device, fromcase, tocase);
327 p->pathname.directory =
328 directory =
329 translate_list_case(directory, fromcase, tocase);
330 p->pathname.name =
331 translate_component_case(name, fromcase, tocase);
332 p->pathname.type =
333 translate_component_case(type, fromcase, tocase);
334 p->pathname.version = version;
335 }
336 directory = destructively_check_directory(directory, p->pathname.logical, 0);
337 unlikely_if (directory == @':error') {
338 cl_error(3, @'file-error', @':pathname', p);
339 }
340 p->pathname.directory = directory;
341 return(p);
342 }
343
344 static cl_object
345 tilde_expand(cl_object pathname)
346 {
347 /*
348 * If the pathname is a physical one, without hostname, without device
349 * and the first element is either a tilde '~' or '~' followed by
350 * a user name, we merge the user homedir pathname with this one.
351 */
352 cl_object directory, head;
353 if (pathname->pathname.logical || pathname->pathname.host != ECL_NIL
354 || pathname->pathname.device != ECL_NIL) {
355 return pathname;
356 }
357 directory = pathname->pathname.directory;
358 if (!CONSP(directory) || ECL_CONS_CAR(directory) != @':relative'
359 || ECL_CONS_CDR(directory) == ECL_NIL) {
360 return pathname;
361 }
362 head = CADR(directory);
363 if (ecl_stringp(head) && ecl_length(head) > 0 &&
364 ecl_char(head,0) == '~') {
365 /* Remove the tilde component */
366 ECL_RPLACD(directory, CDDR(directory));
367 pathname = cl_merge_pathnames(2, pathname,
368 ecl_homedir_pathname(head));
369 }
370 return pathname;
371 }
372
373 #define WORD_INCLUDE_DELIM 1
374 #define WORD_ALLOW_ASTERISK 2
375 #define WORD_EMPTY_IS_NIL 4
376 #define WORD_LOGICAL 8
377 #define WORD_SEARCH_LAST_DOT 16
378 #define WORD_ALLOW_LEADING_DOT 32
379 #define WORD_DISALLOW_SLASH 64
380 #define WORD_DISALLOW_SEMICOLON 128
381
382 static cl_object
383 make_one(cl_object s, cl_index start, cl_index end)
384 {
385 return cl_subseq(3, s, ecl_make_fixnum(start), ecl_make_fixnum(end));
386 }
387
388 static int is_colon(int c) { return c == ':'; }
389 static int is_slash(int c) { return IS_DIR_SEPARATOR(c); }
390 static int is_semicolon(int c) { return c == ';'; }
391 static int is_dot(int c) { return c == '.'; }
392 static int is_null(int c) { return c == '\0'; }
393
394 /*
395 * Parses a word from string `S' until either:
396 * 1) character `DELIM' is found
397 * 2) end of string is reached
398 * 3) a non valid character is found
399 * Output is either
400 * 1) :error in case (3) above
401 * 2) :wild, :wild-inferiors, :up
402 * 3) "" or ECL_NIL when word has no elements
403 * 5) A non empty string
404 */
405 static cl_object
406 parse_word(cl_object s, delim_fn delim, int flags, cl_index start,
407 cl_index end, cl_index *end_of_word)
408 {
409 cl_index i, j, last_delim = end;
410 bool wild_inferiors = FALSE;
411
412 i = j = start;
413 for (; i < end; i++) {
414 bool valid_char;
415 cl_index c = ecl_char(s, i);
416 if (delim(c)) {
417 if ((i == start) && (flags & WORD_ALLOW_LEADING_DOT)) {
418 /* Leading dot is included */
419 continue;
420 }
421 last_delim = i;
422 if (!(flags & WORD_SEARCH_LAST_DOT)) {
423 break;
424 }
425 }
426 if (c == '*') {
427 if (!(flags & WORD_ALLOW_ASTERISK))
428 valid_char = FALSE; /* Asterisks not allowed in this word */
429 else {
430 wild_inferiors = (i > start && ecl_char(s, i-1) == '*');
431 valid_char = TRUE; /* single "*" */
432 }
433 } else if (c == ';' && (flags & WORD_DISALLOW_SEMICOLON)) {
434 valid_char = 0;
435 } else if (c == '/' && (flags & WORD_DISALLOW_SLASH)) {
436 valid_char = 0;
437 } else {
438 valid_char = c != 0;
439 }
440 if (!valid_char) {
441 *end_of_word = start;
442 return @':error';
443 }
444 }
445 if (i > last_delim) {
446 /* Go back to the position of the last delimiter */
447 i = last_delim;
448 }
449 if (i < end) {
450 *end_of_word = i+1;
451 } else {
452 *end_of_word = end;
453 /* We have reached the end of the string without finding
454 the proper delimiter */
455 if (flags & WORD_INCLUDE_DELIM) {
456 *end_of_word = start;
457 return ECL_NIL;
458 }
459 }
460 switch(i-j) {
461 case 0:
462 if (flags & WORD_EMPTY_IS_NIL)
463 return ECL_NIL;
464 return cl_core.null_string;
465 case 1:
466 if (ecl_char(s,j) == '*')
467 return @':wild';
468 break;
469 case 2: {
470 cl_index c0 = ecl_char(s,j);
471 cl_index c1 = ecl_char(s,j+1);
472 if (c0 == '*' && c1 == '*')
473 return @':wild-inferiors';
474 if (!(flags & WORD_LOGICAL) && c0 == '.' && c1 == '.')
475 return @':up';
476 break;
477 }
478 default:
479 if (wild_inferiors) /* '**' surrounded by other characters */
480 return @':error';
481 }
482 return make_one(s, j, i);
483 }
484
485 /*
486 * Parses a logical or physical directory tree. Output is always a
487 * list of valid directory components, which may be just NIL.
488 *
489 * INV: When parsing of directory components has failed, a valid list
490 * is also returned, and it will be later in the parsing of
491 * pathname-name or pathname-type when the same error is detected.
492 */
493
494 static cl_object
495 parse_directories(cl_object s, int flags, cl_index start, cl_index end,
496 cl_index *end_of_dir)
497 {
498 cl_index i, j;
499 cl_object path = ECL_NIL;
500 delim_fn delim = (flags & WORD_LOGICAL) ? is_semicolon : is_slash;
501
502 flags |= WORD_INCLUDE_DELIM | WORD_ALLOW_ASTERISK;
503 *end_of_dir = start;
504 for (i = j = start; i < end; j = i) {
505 cl_object part = parse_word(s, delim, flags, j, end, &i);
506 if (part == @':error' || part == ECL_NIL)
507 break;
508 if (part == cl_core.null_string) { /* "/", ";" */
509 if (j != start) {
510 if (flags & WORD_LOGICAL)
511 return @':error';
512 *end_of_dir = i;
513 continue;
514 }
515 part = (flags & WORD_LOGICAL) ? @':relative' : @':absolute';
516 }
517 *end_of_dir = i;
518 path = ecl_cons(part, path);
519 }
520 return cl_nreverse(path);
521 }
522
523 bool
524 ecl_logical_hostname_p(cl_object host)
525 {
526 if (!ecl_stringp(host))
527 return FALSE;
528 return !Null(ecl_assqlp(host, cl_core.pathname_translations));
529 }
530
531 /*
532 * Parses a lisp namestring until the whole substring is parsed or an
533 * error is found. It returns a valid pathname or NIL, plus the place
534 * where parsing ended in *END_OF_PARSING.
535 *
536 * The rules are as follows:
537 *
538 * 1) If a hostname is supplied it determines whether the namestring
539 * will be parsed as logical or as physical.
540 *
541 * 2) If no hostname is supplied, first it tries parsing using logical
542 * pathname rules and, if no logical hostname is found, then it
543 * tries the physical pathname format.
544 *
545 * 3) Logical pathname syntax:
546 * [logical-hostname:][;][logical-directory-component;][pathname-name][.pathname-type]
547 *
548 * 4) Physical pathname syntax:
549 * [device:][[//hostname]/][directory-component/]*[pathname-name][.pathname-type]
550 *
551 * logical-hostname, device, hostname = word
552 * logical-directory-component = word | wildcard-word
553 * directory-component = word | wildcard-word | '..' | '.'
554 * pathname-name, pathname-type = word | wildcard-word | ""
555 *
556 */
557 cl_object
558 ecl_parse_namestring(cl_object s, cl_index start, cl_index end, cl_index *ep,
559 cl_object default_host)
560 {
561 cl_object host, device, path, name, type, aux, version;
562 bool logical;
563
564 if (start == end) {
565 host = device = path = name = type = aux = version = @'nil';
566 logical = 0;
567 *ep = end;
568 goto make_it;
569 }
570 /* We first try parsing as logical-pathname. In case of
571 * failure, physical-pathname parsing is performed only when
572 * there is no supplied *logical* host name. All other failures
573 * result in ECL_NIL as output.
574 */
575 host = parse_word(s, is_colon, WORD_LOGICAL | WORD_INCLUDE_DELIM |
576 WORD_DISALLOW_SEMICOLON, start, end, ep);
577 if (default_host != ECL_NIL) {
578 if (host == ECL_NIL || host == @':error')
579 host = default_host;
580 }
581 if (!ecl_logical_hostname_p(host))
582 goto physical;
583 /*
584 * Logical pathname format:
585 * [logical-hostname:][;][logical-directory-component;][pathname-name][.pathname-type]
586 */
587 logical = TRUE;
588 device = @':unspecific';
589 path = parse_directories(s, WORD_LOGICAL, *ep, end, ep);
590 if (CONSP(path)) {
591 if (ECL_CONS_CAR(path) != @':relative' &&
592 ECL_CONS_CAR(path) != @':absolute')
593 path = CONS(@':absolute', path);
594 path = destructively_check_directory(path, TRUE, FALSE);
595 } else {
596 path = CONS(@':absolute', path);
597 }
598 if (path == @':error')
599 return ECL_NIL;
600 name = parse_word(s, is_dot, WORD_LOGICAL | WORD_ALLOW_ASTERISK |
601 WORD_EMPTY_IS_NIL, *ep, end, ep);
602 if (name == @':error')
603 return ECL_NIL;
604 type = ECL_NIL;
605 version = ECL_NIL;
606 if (*ep == start || ecl_char(s, *ep-1) != '.')
607 goto make_it;
608 type = parse_word(s, is_dot, WORD_LOGICAL | WORD_ALLOW_ASTERISK |
609 WORD_EMPTY_IS_NIL, *ep, end, ep);
610 if (type == @':error')
611 return ECL_NIL;
612 if (*ep == start || ecl_char(s, *ep-1) != '.')
613 goto make_it;
614 aux = parse_word(s, is_null, WORD_LOGICAL | WORD_ALLOW_ASTERISK |
615 WORD_EMPTY_IS_NIL, *ep, end, ep);
616 if (aux == @':error') {
617 return ECL_NIL;
618 } else if (ECL_SYMBOLP(aux)) {
619 version = aux;
620 } else {
621 const cl_env_ptr the_env = ecl_process_env();
622 cl_object parsed_length;
623 version = cl_parse_integer(3, aux, @':junk-allowed', ECL_T);
624 parsed_length = ecl_nth_value(the_env, 1);
625 if (ecl_fixnum(parsed_length) == ecl_length(aux) &&
626 cl_integerp(version) != ECL_NIL && ecl_plusp(version))
627 ;
628 else if (cl_string_equal(2, aux, @':newest') != ECL_NIL)
629 version = @':newest';
630 else
631 return ECL_NIL;
632 }
633 goto make_it;
634 physical:
635 /*
636 * Physical pathname format:
637 * [[device:[//hostname]]/][directory-component/]*[pathname-name][.pathname-type]
638 */
639 logical = FALSE;
640 /* We only parse a hostname when the device was present. This
641 * requisite is a bit stupid and only applies to the Unix port,
642 * where "//home/" is equivalent to "/home" However, in Windows
643 * we need "//FOO/" to be separately handled, for it is a shared
644 * resource.
645 */
646 #if defined(ECL_MS_WINDOWS_HOST)
647 if ((start+1 <= end) && is_slash(ecl_char(s, start))) {
648 device = ECL_NIL;
649 goto maybe_parse_host;
650 }
651 #endif
652 device = parse_word(s, is_colon, WORD_INCLUDE_DELIM | WORD_EMPTY_IS_NIL |
653 WORD_DISALLOW_SLASH, start, end, ep);
654 if (device == @':error' || device == ECL_NIL) {
655 device = ECL_NIL;
656 host = ECL_NIL;
657 goto done_device_and_host;
658 }
659 if (!ecl_stringp(device)) {
660 return ECL_NIL;
661 }
662 #if defined(ECL_MS_WINDOWS_HOST)
663 maybe_parse_host:
664 #endif
665 /* Files have no effective device. */
666 if (@string-equal(2, device, @':file') == ECL_T)
667 device = ECL_NIL;
668 start = *ep;
669 host = ECL_NIL;
670 if ((start+2) <= end && is_slash(ecl_char(s, start)) &&
671 is_slash(ecl_char(s, start+1)))
672 {
673 host = parse_word(s, is_slash, WORD_EMPTY_IS_NIL,
674 start+2, end, ep);
675 if (host == @':error') {
676 host = ECL_NIL;
677 } else if (host != ECL_NIL) {
678 if (!ecl_stringp(host))
679 return ECL_NIL;
680 start = *ep;
681 if (is_slash(ecl_char(s,--start)))
682 *ep = start;
683 }
684 }
685 if (ecl_length(device) == 0)
686 device = ECL_NIL;
687 done_device_and_host:
688 path = parse_directories(s, 0, *ep, end, ep);
689 if (CONSP(path)) {
690 if (ECL_CONS_CAR(path) != @':relative' &&
691 ECL_CONS_CAR(path) != @':absolute')
692 path = CONS(@':relative', path);
693 path = destructively_check_directory(path, FALSE, FALSE);
694 }
695 if (path == @':error')
696 return ECL_NIL;
697 start = *ep;
698 name = parse_word(s, is_dot,
699 WORD_ALLOW_LEADING_DOT | WORD_SEARCH_LAST_DOT |
700 WORD_ALLOW_ASTERISK | WORD_EMPTY_IS_NIL,
701 start, end, ep);
702 if (name == @':error')
703 return ECL_NIL;
704 if ((*ep - start) <= 1 || ecl_char(s, *ep-1) != '.') {
705 type = ECL_NIL;
706 } else {
707 type = parse_word(s, is_null, WORD_ALLOW_ASTERISK, *ep, end, ep);
708 if (type == @':error')
709 return ECL_NIL;
710 }
711 version = (name != ECL_NIL || type != ECL_NIL) ? @':newest' : ECL_NIL;
712 make_it:
713 if (*ep >= end) *ep = end;
714 path = ecl_make_pathname(host, device, path, name, type, version,
715 @':local');
716 path->pathname.logical = logical;
717 return tilde_expand(path);
718 }
719
720 cl_object
721 si_default_pathname_defaults(void)
722 {
723 /* This routine outputs the value of *default-pathname-defaults*
724 * coerced to type PATHNAME. Special care is taken so that we do
725 * not enter an infinite loop when using PARSE-NAMESTRING, because
726 * this routine might itself try to use the value of this variable. */
727 const cl_env_ptr the_env = ecl_process_env();
728 cl_object path = ecl_cmp_symbol_value(the_env, @'*default-pathname-defaults*');
729 unlikely_if (!ECL_PATHNAMEP(path)) {
730 ecl_bds_bind(the_env, @'*default-pathname-defaults*', si_getcwd(0));
731 FEwrong_type_key_arg(@[pathname], @[*default-pathname-defaults*],
732 path, @'pathname');
733 }
734 @(return path);
735 }
736
737 cl_object
738 cl_pathname(cl_object x)
739 {
740 L:
741 switch (ecl_t_of(x)) {
742 #ifdef ECL_UNICODE
743 case t_string:
744 #endif
745 case t_base_string:
746 x = cl_parse_namestring(1, x);
747 case t_pathname:
748 break;
749 default:
750 if (!Null(cl_streamp(x))) {
751 x = ecl_stream_pathname(x);
752 goto L;
753 } else {
754 const char *type = "(OR FILE-STREAM STRING PATHNAME)";
755 FEwrong_type_only_arg(@[pathname], x, ecl_read_from_cstring(type));
756 }
757 break;
758 }
759 @(return x);
760 }
761
762 cl_object
763 cl_logical_pathname(cl_object x)
764 {
765 x = cl_pathname(x);
766 if (!x->pathname.logical) {
767 cl_error(9, @'simple-type-error', @':format-control',
768 @"~S cannot be coerced to a logical pathname.",
769 @':format-arguments', cl_list(1, x),
770 @':expected-type', @'logical-pathname',
771 @':datum', x);
772 }
773 @(return x);
774 }
775
776 @(defun wild-pathname-p (pathname &optional component)
777 bool checked = 0;
778 @
779 pathname = cl_pathname(pathname);
780 if (component == ECL_NIL || component == @':host') {
781 if (pathname->pathname.host == @':wild') {
782 @(return ECL_T);
783 }
784 checked = 1;
785 }
786 if (component == ECL_NIL || component == @':device') {
787 if (pathname->pathname.device == @':wild') {
788 @(return ECL_T);
789 }
790 checked = 1;
791 }
792 if (component == ECL_NIL || component == @':version') {
793 if (pathname->pathname.version == @':wild') {
794 @(return ECL_T);
795 }
796 checked = 1;
797 }
798 if (component == ECL_NIL || component == @':name') {
799 cl_object name = pathname->pathname.name;
800 if (name != ECL_NIL &&
801 (name == @':wild' || ecl_wild_string_p(name))) {
802 @(return ECL_T);
803 }
804 checked = 1;
805 }
806 if (component == ECL_NIL || component == @':type') {
807 cl_object name = pathname->pathname.type;
808 if (name != ECL_NIL &&
809 (name == @':wild' || ecl_wild_string_p(name))) {
810 @(return ECL_T);
811 }
812 checked = 1;
813 }
814 if (component == ECL_NIL || component == @':directory') {
815 cl_object list = pathname->pathname.directory;
816 checked = 1;
817 loop_for_on_unsafe(list) {
818 cl_object name = ECL_CONS_CAR(list);
819 if (name != ECL_NIL &&
820 (name == @':wild' || name == @':wild-inferiors' ||
821 ecl_wild_string_p(name))) {
822 @(return ECL_T);
823 }
824 } end_loop_for_on_unsafe(list);
825 }
826 if (checked == 0) {
827 FEerror("~A is not a valid pathname component", 1, component);
828 }
829 @(return ECL_NIL);
830 @)
831
832 /*
833 * si_coerce_to_file_pathname(P) converts P to a physical pathname,
834 * for a file which is accessible in our filesystem.
835 * INV: Wildcards are allowed.
836 * INV: A fresh new copy of the pathname is created.
837 * INV: The pathname is absolute.
838 * INV: The device component is copied from getcwd if necessary.
839 */
840 cl_object
841 si_coerce_to_file_pathname(cl_object pathname)
842 {
843 pathname = si_coerce_to_physical_pathname(pathname);
844 pathname = cl_merge_pathnames(1, pathname);
845 if (
846 #ifdef ECL_MS_WINDOWS_HOST
847 pathname->pathname.device == ECL_NIL ||
848 #endif
849 pathname->pathname.directory == ECL_NIL ||
850 ECL_CONS_CAR(pathname->pathname.directory) == @':relative') {
851 pathname = cl_merge_pathnames(2, pathname, si_getcwd(0));
852 }
853 return pathname;
854 }
855
856 /*
857 * si_coerce_to_physical_pathname(P) converts P to a physical pathname,
858 * performing the appropriate transformation if P was a logical pathname.
859 */
860 cl_object
861 si_coerce_to_physical_pathname(cl_object x)
862 {
863 x = cl_pathname(x);
864 if (x->pathname.logical)
865 return cl_translate_logical_pathname(1, x);
866 return x;
867 }
868
869 /*
870 * si_coerce_to_filename(P) converts P to a physical pathname and then
871 * to a properly encoded namestring. The output is a new simple-string
872 * or vector of utf-16 characters which can be used by the C library.
873 * INV: No wildcards are allowed.
874 */
875 cl_object
876 si_coerce_to_filename(cl_object pathname_orig)
877 {
878 cl_object namestring, pathname;
879
880 /* We always go through the pathname representation and thus
881 * cl_namestring() always outputs a fresh new string */
882 pathname = si_coerce_to_file_pathname(pathname_orig);
883 if (cl_wild_pathname_p(1,pathname) != ECL_NIL)
884 cl_error(3, @'file-error', @':pathname', pathname_orig);
885 namestring = ecl_namestring(pathname,
886 ECL_NAMESTRING_TRUNCATE_IF_ERROR |
887 ECL_NAMESTRING_FORCE_BASE_STRING);
888 if (namestring == ECL_NIL) {
889 FEerror("Pathname without a physical namestring:"
890 "~% :HOST ~A"
891 "~% :DEVICE ~A"
892 "~% :DIRECTORY ~A"
893 "~% :NAME ~A"
894 "~% :TYPE ~A"
895 "~% :VERSION ~A",
896 6, pathname_orig->pathname.host,
897 pathname_orig->pathname.device,
898 pathname_orig->pathname.directory,
899 pathname_orig->pathname.name,
900 pathname_orig->pathname.type,
901 pathname_orig->pathname.version);
902 }
903 if (cl_core.path_max != -1 &&
904 ecl_length(namestring) >= cl_core.path_max - 16)
905 FEerror("Too long filename: ~S.", 1, namestring);
906 return namestring;
907 }
908
909 #define default_device(host) ECL_NIL
910
911 cl_object
912 ecl_merge_pathnames(cl_object path, cl_object defaults, cl_object default_version)
913 {
914 cl_object host, device, directory, name, type, version;
915 cl_object tocase;
916
917 defaults = cl_pathname(defaults);
918 path = cl_parse_namestring(1, path, ECL_NIL, defaults);
919 if (Null(host = path->pathname.host))
920 host = defaults->pathname.host;
921 tocase = host_case(host);
922 if (Null(path->pathname.device)) {
923 if (Null(path->pathname.host))
924 device = cl_pathname_device(3, defaults, @':case', tocase);
925 else if (path->pathname.host == defaults->pathname.host)
926 device = defaults->pathname.device;
927 else
928 device = default_device(path->pathname.host);
929 } else {
930 device = path->pathname.device;
931 }
932 if (Null(path->pathname.directory)) {
933 directory = cl_pathname_directory(3, defaults, @':case', tocase);
934 } else if (ECL_CONS_CAR(path->pathname.directory) == @':absolute') {
935 directory = path->pathname.directory;
936 } else if (!Null(defaults->pathname.directory)) {
937 directory = ecl_append(cl_pathname_directory(3, defaults,
938 @':case', tocase),
939 CDR(path->pathname.directory));
940 /* Eliminate redundant :back */
941 directory = destructively_check_directory(directory, TRUE, TRUE);
942 } else {
943 directory = path->pathname.directory;
944 }
945 if (Null(name = path->pathname.name)) {
946 name = cl_pathname_name(3, defaults, @':case', tocase);
947 }
948 if (Null(type = path->pathname.type)) {
949 type = cl_pathname_type(3, defaults, @':case', tocase);
950 }
951 version = path->pathname.version;
952 if (Null(path->pathname.name)) {
953 if (Null(version))
954 version = defaults->pathname.version;
955 }
956 if (Null(version)) {
957 version = default_version;
958 }
959 if (default_version == @':default') {
960 if (Null(name) && Null(type)) {
961 version = ECL_NIL;
962 } else {
963 version = @':newest';
964 }
965 }
966 /*
967 In this implementation, version is not considered
968 */
969 defaults = ecl_make_pathname(host, device, directory, name,
970 type, version, tocase);
971 return defaults;
972 }
973
974 /*
975 ecl_namestring(x, flag) converts a pathname to a namestring.
976 if flag is true, then the pathname may be coerced to the requirements
977 of the filesystem, removing fields that have no meaning (such as
978 version, or type, etc); otherwise, when it is not possible to
979 produce a readable representation of the pathname, NIL is returned.
980 */
981 cl_object
982 ecl_namestring(cl_object x, int flags)
983 {
984 bool logical;
985 cl_object l, y;
986 cl_object buffer_string, buffer, host;
987 bool truncate_if_unreadable = flags & ECL_NAMESTRING_TRUNCATE_IF_ERROR;
988
989 x = cl_pathname(x);
990
991 /* INV: Pathnames can only be created by merging, parsing namestrings
992 * or using ecl_make_pathname(). In all of these cases ECL will complain
993 * at creation time if the pathname has wrong components.
994 */
995 #ifdef ECL_UNICODE
996 if (flags & ECL_NAMESTRING_FORCE_BASE_STRING) {
997 # ifdef ECL_MS_WINDOWS_HOST
998 buffer_string = si_make_vector(@'ext::byte16', /* element-type */
999 ecl_make_fixnum(128), /* size */
1000 ECL_T, /* adjustable */
1001 ecl_make_fixnum(0), /* fillp */
1002 ECL_NIL, /* displaced */
1003 ECL_NIL); /* displaced-offset */
1004 buffer = si_make_sequence_output_stream(3, buffer_string,
1005 @':external-format', @':ucs-2');
1006 # else
1007 buffer_string = ecl_alloc_adjustable_base_string(128);
1008 buffer = si_make_sequence_output_stream(1, buffer_string);
1009 # endif
1010 } else {
1011 buffer_string = ecl_alloc_adjustable_extended_string(128);
1012 buffer = si_make_string_output_stream_from_string(buffer_string);
1013 }
1014 #else
1015 buffer_string = ecl_alloc_adjustable_base_string(128);
1016 buffer = si_make_string_output_stream_from_string(buffer_string);
1017 #endif
1018 logical = x->pathname.logical;
1019 host = x->pathname.host;
1020 if (logical) {
1021 if ((y = x->pathname.device) != @':unspecific' &&
1022 truncate_if_unreadable)
1023 return ECL_NIL;
1024 if (host != ECL_NIL) {
1025 si_do_write_sequence(host, buffer, ecl_make_fixnum(0), ECL_NIL);
1026 writestr_stream(":", buffer);
1027 }
1028 } else {
1029 if ((y = x->pathname.device) != ECL_NIL) {
1030 si_do_write_sequence(y, buffer, ecl_make_fixnum(0), ECL_NIL);
1031 writestr_stream(":", buffer);
1032 }
1033 if (host != ECL_NIL) {
1034 #if !defined(ECL_MS_WINDOWS_HOST)
1035 if (y == ECL_NIL) {
1036 writestr_stream("file:", buffer);
1037 }
1038 #endif
1039 writestr_stream("//", buffer);
1040 si_do_write_sequence(host, buffer, ecl_make_fixnum(0), ECL_NIL);
1041 }
1042 }
1043 l = x->pathname.directory;
1044 if (ecl_endp(l))
1045 goto NO_DIRECTORY;
1046 y = ECL_CONS_CAR(l);
1047 if (y == @':relative') {
1048 if (logical)
1049 ecl_write_char(';', buffer);
1050 } else {
1051 if (!logical)
1052 ecl_write_char(DIR_SEPARATOR, buffer);
1053 }
1054 l = ECL_CONS_CDR(l);
1055 loop_for_in(l) {
1056 y = ECL_CONS_CAR(l);
1057 if (y == @':up') {
1058 writestr_stream("..", buffer);
1059 } else if (y == @':wild') {
1060 writestr_stream("*", buffer);
1061 } else if (y == @':wild-inferiors') {
1062 writestr_stream("**", buffer);
1063 } else if (y != @':back') {
1064 si_do_write_sequence(y, buffer, ecl_make_fixnum(0), ECL_NIL);
1065 } else {
1066 /* Directory :back has no namestring representation */
1067 return ECL_NIL;
1068 }
1069 ecl_write_char(logical? ';' : DIR_SEPARATOR, buffer);
1070 } end_loop_for_in;
1071 NO_DIRECTORY:
1072 if (ecl_file_position(buffer) == ecl_make_fixnum(0)) {
1073 if ((ecl_stringp(x->pathname.name) &&
1074 ecl_member_char(':', x->pathname.name)) ||
1075 (ecl_stringp(x->pathname.type) &&
1076 ecl_member_char(':', x->pathname.type)))
1077 writestr_stream(":", buffer);
1078 }
1079 y = x->pathname.name;
1080 if (y != ECL_NIL) {
1081 if (y == @':wild') {
1082 writestr_stream("*", buffer);
1083 } else {
1084 si_do_write_sequence(y, buffer, ecl_make_fixnum(0), ECL_NIL);
1085 }
1086 } else if (!logical && !Null(x->pathname.type)) {
1087 /* #P".txt" is :NAME = ".txt" :TYPE = NIL and
1088 hence :NAME = NIL and :TYPE != NIL does not have
1089 a printed representation */
1090 return ECL_NIL;
1091 }
1092 y = x->pathname.type;
1093 if (y == @':unspecific') {
1094 return ECL_NIL;
1095 } else if (y != ECL_NIL) {
1096 if (y == @':wild') {
1097 writestr_stream(".*", buffer);
1098 } else {
1099 writestr_stream(".", buffer);
1100 si_do_write_sequence(y, buffer, ecl_make_fixnum(0), ECL_NIL);
1101 }
1102 }
1103 y = x->pathname.version;
1104 if (logical) {
1105 if (y != ECL_NIL) {
1106 writestr_stream(".", buffer);
1107 if (y == @':wild') {
1108 writestr_stream("*", buffer);
1109 } else if (y == @':newest') {
1110 si_do_write_sequence(ecl_symbol_name(y), buffer,
1111 ecl_make_fixnum(0), ECL_NIL);
1112 } else {
1113 /* Since the printer is not reentrant,
1114 * we cannot use cl_write and friends.
1115 */
1116 int n = ecl_fixnum(y), i;
1117 char b[ECL_FIXNUM_BITS / 2];
1118 for (i = 0; n; i++) {
1119 b[i] = n%10 + '0';
1120 n = n/10;
1121 }
1122 if (i == 0)
1123 b[i++] = '0';
1124 while (i--) {
1125 ecl_write_char(b[i], buffer);
1126 }
1127 }
1128 }
1129 } else if (!truncate_if_unreadable) {
1130 /* Namestrings of physical pathnames have restrictions... */
1131 if (Null(x->pathname.name) && Null(x->pathname.type)) {
1132 /* Directories cannot have a version number */
1133 if (y != ECL_NIL)
1134 return ECL_NIL;
1135 } else if (y != @':newest') {
1136 /* Filenames have an implicit version :newest */
1137 return ECL_NIL;
1138 }
1139 }
1140 #ifdef ECL_UNICODE
1141 if (flags & ECL_NAMESTRING_FORCE_BASE_STRING) {
1142 #endif
1143 /* add null terminator */
1144 ecl_write_char('\0', buffer);
1145 buffer_string->base_string.fillp--;
1146 buffer_string->base_string.dim--;
1147 #ifdef ECL_UNICODE
1148 }
1149 #endif
1150 return buffer_string;
1151 }
1152
1153 cl_object
1154 cl_namestring(cl_object x)
1155 {
1156 @(return ecl_namestring(x, ECL_NAMESTRING_TRUNCATE_IF_ERROR));
1157 }
1158
1159 @(defun parse_namestring (thing
1160 &o host (defaults si_default_pathname_defaults())
1161 &k (start ecl_make_fixnum(0)) end junk_allowed
1162 &a output)
1163 @
1164 if (host != ECL_NIL) {
1165 host = cl_string(host);
1166 }
1167 if (!ecl_stringp(thing)) {
1168 output = cl_pathname(thing);
1169 } else {
1170 cl_object default_host = host;
1171 cl_index_pair p;
1172 cl_index ee;
1173 if (default_host == ECL_NIL && defaults != ECL_NIL) {
1174 defaults = cl_pathname(defaults);
1175 default_host = defaults->pathname.host;
1176 }
1177 p = ecl_vector_start_end(@[parse-namestring], thing, start, end);
1178 output = ecl_parse_namestring(thing, p.start, p.end, &ee, default_host);
1179 start = ecl_make_fixnum(ee);
1180 if (output == ECL_NIL || ee != p.end) {
1181 if (Null(junk_allowed)) {
1182 FEparse_error("Cannot parse the namestring ~S~%"
1183 "from ~S to ~S.", ECL_NIL,
1184 3, thing, start, end);
1185 }
1186 goto OUTPUT;
1187 }
1188 }
1189 if (host != ECL_NIL && !ecl_equal(output->pathname.host, host)) {
1190 FEerror("The pathname ~S does not contain the required host ~S.",
1191 2, thing, host);
1192 }
1193 OUTPUT:
1194 @(return output start);
1195 @)
1196
1197 @(defun merge_pathnames (path
1198 &o (defaults si_default_pathname_defaults())
1199 (default_version @':newest'))
1200 @
1201 path = cl_pathname(path);
1202 defaults = cl_pathname(defaults);
1203 @(return ecl_merge_pathnames(path, defaults, default_version));
1204 @)
1205
1206 @(defun make_pathname (&key (host ECL_NIL hostp) (device ECL_NIL devicep)
1207 (directory ECL_NIL directoryp)
1208 (name ECL_NIL namep) (type ECL_NIL typep) (version ECL_NIL versionp)
1209 ((:case scase) @':local')
1210 defaults
1211 &aux x)
1212 @
1213 if (Null(defaults)) {
1214 defaults = si_default_pathname_defaults();
1215 defaults = ecl_make_pathname(defaults->pathname.host,
1216 ECL_NIL, ECL_NIL, ECL_NIL, ECL_NIL, ECL_NIL,
1217 @':local');
1218 } else {
1219 defaults = cl_pathname(defaults);
1220 }
1221 if (!hostp) host = defaults->pathname.host;
1222 x = ecl_make_pathname(host, device, directory, name, type, version, scase);
1223 if (!devicep) x->pathname.device = defaults->pathname.device;
1224 if (!directoryp) x->pathname.directory = defaults->pathname.directory;
1225 if (!namep) x->pathname.name = defaults->pathname.name;
1226 if (!typep) x->pathname.type = defaults->pathname.type;
1227 if (!versionp) x->pathname.version = defaults->pathname.version;
1228
1229 @(return x);
1230 @)
1231
1232 cl_object
1233 cl_pathnamep(cl_object pname)
1234 {
1235 @(return (ECL_PATHNAMEP(pname) ? ECL_T : ECL_NIL));
1236 }
1237
1238 cl_object
1239 si_logical_pathname_p(cl_object pname)
1240 {
1241 @(return ((ECL_PATHNAMEP(pname) && pname->pathname.logical)?
1242 ECL_T : ECL_NIL));
1243 }
1244
1245 @(defun pathname_host (pname &key ((:case scase) @':local'))
1246 @
1247 pname = cl_pathname(pname);
1248 @(return translate_component_case(pname->pathname.host,
1249 normalize_case(pname, @':local'),
1250 normalize_case(pname, scase)));
1251 @)
1252
1253 @(defun pathname_device (pname &key ((:case scase) @':local'))
1254 @
1255 pname = cl_pathname(pname);
1256 @(return translate_component_case(pname->pathname.device,
1257 normalize_case(pname, @':local'),
1258 normalize_case(pname, scase)));
1259 @)
1260
1261 @(defun pathname_directory (pname &key ((:case scase) @':local'))
1262 @
1263 pname = cl_pathname(pname);
1264 @(return translate_list_case(pname->pathname.directory,
1265 normalize_case(pname, @':local'),
1266 normalize_case(pname, scase)));
1267 @)
1268
1269 @(defun pathname_name(pname &key ((:case scase) @':local'))
1270 @
1271 pname = cl_pathname(pname);
1272 @(return translate_component_case(pname->pathname.name,
1273 normalize_case(pname, @':local'),
1274 normalize_case(pname, scase)));
1275 @)
1276
1277 @(defun pathname_type(pname &key ((:case scase) @':local'))
1278 @
1279 pname = cl_pathname(pname);
1280 @(return translate_component_case(pname->pathname.type,
1281 normalize_case(pname, @':local'),
1282 normalize_case(pname, scase)));
1283 @)
1284
1285 cl_object
1286 cl_pathname_version(cl_object pname)
1287 {
1288 pname = cl_pathname(pname);
1289 @(return pname->pathname.version);
1290 }
1291
1292 cl_object
1293 cl_file_namestring(cl_object pname)
1294 {
1295 pname = cl_pathname(pname);
1296 @(return ecl_namestring(ecl_make_pathname(ECL_NIL, ECL_NIL, ECL_NIL,
1297 pname->pathname.name,
1298 pname->pathname.type,
1299 pname->pathname.version,
1300 @':local'),
1301 ECL_NAMESTRING_TRUNCATE_IF_ERROR));
1302 }
1303
1304 cl_object
1305 cl_directory_namestring(cl_object pname)
1306 {
1307 pname = cl_pathname(pname);
1308 @(return ecl_namestring(ecl_make_pathname(ECL_NIL, ECL_NIL,
1309 pname->pathname.directory,
1310 ECL_NIL, ECL_NIL, ECL_NIL,
1311 @':local'),
1312 ECL_NAMESTRING_TRUNCATE_IF_ERROR));
1313 }
1314
1315 cl_object
1316 cl_host_namestring(cl_object pname)
1317 {
1318 pname = cl_pathname(pname);
1319 pname = pname->pathname.host;
1320 if (Null(pname) || pname == @':wild')
1321 pname = cl_core.null_string;
1322 @(return pname);
1323 }
1324
1325 #define EN_MATCH(p1,p2,el) (ecl_equalp(p1->pathname.el, p2->pathname.el)? ECL_NIL : p1->pathname.el)
1326
1327 @(defun enough_namestring (path
1328 &o (defaults si_default_pathname_defaults()))
1329 cl_object newpath, pathdir, defaultdir, fname;
1330 @
1331 defaults = cl_pathname(defaults);
1332 path = cl_pathname(path);
1333 pathdir = path->pathname.directory;
1334 defaultdir = defaults->pathname.directory;
1335 if (Null(pathdir)) {
1336 pathdir = ecl_list1(@':relative');
1337 } else if (Null(defaultdir)) {
1338 /* The defaults pathname does not have a directory. */
1339 } else if (ECL_CONS_CAR(pathdir) == @':relative') {
1340 /* The pathname is relative to the default one one, so we just output the
1341 original one */
1342 } else {
1343 /* The new pathname is an absolute one. We compare it with the defaults
1344 and if they have some common elements, we just output the remaining ones. */
1345 cl_object dir_begin = funcall(5, @'mismatch', pathdir, defaultdir,
1346 @':test', @'equal');
1347 if (dir_begin == ECL_NIL) {
1348 pathdir = ECL_NIL;
1349 } else if (dir_begin == cl_length(defaultdir)) {
1350 pathdir = funcall(3, @'subseq', pathdir, dir_begin);
1351 pathdir = CONS(@':relative', pathdir);
1352 }
1353 }
1354 fname = EN_MATCH(path, defaults, name);
1355 if (fname == ECL_NIL) fname = path->pathname.name;
1356 /* Create a path with all elements that do not match the default */
1357 newpath = ecl_make_pathname(EN_MATCH(path, defaults, host),
1358 EN_MATCH(path, defaults, device),
1359 pathdir, fname,
1360 EN_MATCH(path, defaults, type),
1361 EN_MATCH(path, defaults, version),
1362 @':local');
1363 newpath->pathname.logical = path->pathname.logical;
1364 @(return ecl_namestring(newpath, ECL_NAMESTRING_TRUNCATE_IF_ERROR));
1365 @)
1366 #undef EN_MATCH
1367
1368 /* --------------- PATHNAME MATCHING ------------------ */
1369
1370 bool
1371 ecl_wild_string_p(cl_object item)
1372 {
1373 if (ECL_STRINGP(item)) {
1374 cl_index i, l = ecl_length(item);
1375 for (i = 0; i < l; i++) {
1376 ecl_character c = ecl_char(item, i);
1377 if (c == '\\' || c == '*' || c == '?')
1378 return 1;
1379 }
1380 }
1381 return 0;
1382 }
1383
1384 /*
1385 * Take two C strings and check if the first (s) one matches against
1386 * the pattern given by the second one (p). The pattern is that of a
1387 * Unix shell except for brackets and curly braces
1388 */
1389 bool
1390 ecl_string_match(cl_object s, cl_index j, cl_index ls,
1391 cl_object p, cl_index i, cl_index lp)
1392 {
1393 while (i < lp) {
1394 cl_index cp = ecl_char(p, i);
1395 switch (cp) {
1396 case '*': {
1397 /* An asterisk in the pattern matches any
1398 * number of characters. We try the shortest
1399 * sequence that matches. */
1400 cl_index cn = 0, next;
1401 for (next = i+1;
1402 next < lp && ((cn = ecl_char(p, next)) == '*');
1403 next++)
1404 ;
1405 if (next == lp) {
1406 return TRUE;
1407 }
1408 while (j < ls) {
1409 if (ecl_string_match(s, j, ls, p, next, lp)) {
1410 return TRUE;
1411 }
1412 j++;
1413 }
1414 return FALSE;
1415 break;
1416 }
1417 case '?':
1418 /* Match any character */
1419 if (j >= ls) return FALSE;
1420 i++; j++;
1421 break;
1422 case '\\':
1423 /* Interpret a pattern character literally.
1424 Trailing slash is interpreted as a slash. */
1425 if (++i >= lp) i--;
1426 default:
1427 if ((j >= ls) || (cp != ecl_char(s, j))) {
1428 /* Either there are no characters left in "s"
1429 * or the next character does not match. */
1430 return FALSE;
1431 }
1432 i++; j++;
1433 }
1434 }
1435 /* At the end all characters should have been matched */
1436 return (j >= ls);
1437 }
1438
1439 static bool
1440 path_item_match(cl_object a, cl_object mask) {
1441 if (mask == @':wild')
1442 return TRUE;
1443 /* If a component in the tested path is a wildcard field, this
1444 can only be matched by the same wildcard field in the mask */
1445 if (!ecl_stringp(a) || mask == ECL_NIL)
1446 return (a == mask);
1447 if (!ecl_stringp(mask))
1448 FEerror("~S is not supported as mask for pathname-match-p", 1, mask);
1449 return ecl_string_match(a, 0, ecl_length(a),
1450 mask, 0, ecl_length(mask));
1451 }
1452
1453 static bool
1454 path_list_match(cl_object a, cl_object mask) {
1455 cl_object item_mask;
1456 while (!ecl_endp(mask)) {
1457 item_mask = CAR(mask);
1458 mask = CDR(mask);
1459 if (item_mask == @':wild-inferiors') {
1460 if (ecl_endp(mask))
1461 return TRUE;
1462 while (!ecl_endp(a)) {
1463 if (path_list_match(a, mask))
1464 return TRUE;
1465 a = CDR(a);
1466 }
1467 return FALSE;
1468 } else if (ecl_endp(a)) {
1469 /* A NIL directory should match against :absolute
1470 or :relative, in order to perform suitable translations. */
1471 if (item_mask != @':absolute' && item_mask != @':relative')
1472 return FALSE;
1473 } else if (!path_item_match(CAR(a), item_mask)) {
1474 return FALSE;
1475 } else {
1476 a = CDR(a);
1477 }
1478 }
1479 if (!ecl_endp(a))
1480 return FALSE;
1481 return TRUE;
1482 }
1483
1484 cl_object
1485 cl_pathname_match_p(cl_object path, cl_object mask)
1486 {
1487 cl_object output = ECL_NIL;
1488 path = cl_pathname(path);
1489 mask = cl_pathname(mask);
1490 if (path->pathname.logical != mask->pathname.logical)
1491 goto OUTPUT;
1492 #if 0
1493 /* INV: This was checked in the calling routine */
1494 if (!path_item_match(path->pathname.host, mask->pathname.host))
1495 goto OUTPUT;
1496 #endif
1497 /* Missing components default to :WILD */
1498 if (!Null(mask->pathname.directory) &&
1499 !path_list_match(path->pathname.directory, mask->pathname.directory))
1500 goto OUTPUT;
1501 if (!path_item_match(path->pathname.name, mask->pathname.name))
1502 goto OUTPUT;
1503 if (!path_item_match(path->pathname.type, mask->pathname.type))
1504 goto OUTPUT;
1505 if (Null(mask->pathname.version) ||
1506 path_item_match(path->pathname.version, mask->pathname.version))
1507 output = ECL_T;
1508 OUTPUT:
1509 @(return output);
1510 }
1511
1512 /* --------------- PATHNAME TRANSLATIONS ------------------ */
1513
1514 static cl_object
1515 coerce_to_from_pathname(cl_object x, cl_object host)
1516 {
1517 switch (ecl_t_of(x)) {
1518 #ifdef ECL_UNICODE
1519 case t_string:
1520 #endif
1521 case t_base_string:
1522 x = cl_parse_namestring(2, x, host);
1523 case t_pathname:
1524 if (x->pathname.logical)
1525 return x;
1526 default:
1527 FEerror("~S is not a valid from-pathname translation", 1, x);
1528 }
1529 }
1530
1531 @(defun si::pathname-translations (host &optional (set OBJNULL))
1532 cl_index parsed_len, len;
1533 cl_object pair, l;
1534 @
1535 /* Check that host is a valid host name */
1536 if (ecl_unlikely(!ECL_STRINGP(host)))
1537 FEwrong_type_nth_arg(@[si::pathname-translations], 1, host, @[string]);
1538 host = cl_string_upcase(1, host);
1539 len = ecl_length(host);
1540 parse_word(host, is_null, WORD_LOGICAL, 0, len, &parsed_len);
1541 if (parsed_len < len) {
1542 FEerror("Wrong host syntax ~S", 1, host);
1543 }
1544 /* Find its translation list */
1545 pair = ecl_assqlp(host, cl_core.pathname_translations);
1546 if (set == OBJNULL) {
1547 @(return ((pair == ECL_NIL)? ECL_NIL : CADR(pair)));
1548 }
1549 /* Set the new translation list */
1550 if (ecl_unlikely(!LISTP(set))) {
1551 FEwrong_type_nth_arg(@[si::pathname-translations], 2, set, @[list]);
1552 }
1553 if (pair == ECL_NIL) {
1554 pair = CONS(host, CONS(ECL_NIL, ECL_NIL));
1555 cl_core.pathname_translations = CONS(pair, cl_core.pathname_translations);
1556 }
1557 for (l = set, set = ECL_NIL; !ecl_endp(l); l = CDR(l)) {
1558 cl_object item = CAR(l);
1559 cl_object from = coerce_to_from_pathname(cl_car(item), host);
1560 cl_object to = cl_pathname(cl_cadr(item));
1561 set = CONS(CONS(from, CONS(to, ECL_NIL)), set);
1562 }
1563 set = cl_nreverse(set);
1564 ECL_RPLACA(ECL_CONS_CDR(pair), set);
1565 @(return set);
1566 @)
1567
1568 /* This function matches a single component SOURCE against a single component
1569 MATCH. The result is expected to be a list that contains all matches. It is a
1570 list accomodate partial wildcards inside the MATCH component. For example:
1571
1572 ("foo" "bar") -> :error
1573 ("foo" "foo") -> ()
1574 ("f*o" "fo") -> ("")
1575 ("f*o" "foo") -> ("o")
1576 ("f*b*q*" "foobarqux") -> ("oo" "ar" "ux")
1577 ("whatev" :wild) -> ("whatev")
1578
1579 At least that seems to be the purpose from careful reading. -- jd 2025-06-26
1580 */
1581 static cl_object
1582 find_wilds(cl_object source, cl_object match)
1583 {
1584 cl_object result = ECL_NIL;
1585 cl_index i, j, k, ls, lm;
1586
1587 if (match == @':wild')
1588 return ecl_list1(source);
1589 if (!ecl_stringp(match) || !ecl_stringp(source)) {
1590 /* i.e :ABSOLUTE vs :ABSOLUTE */
1591 if (match != source)
1592 return @':error';
1593 return ECL_NIL;
1594 }
1595 ls = ecl_length(source);
1596 lm = ecl_length(match);
1597 for(i = j = 0; i < ls && j < lm; ) {
1598 cl_index pattern_char = ecl_char(match,j);
1599 if (pattern_char == '*') {
1600 /* Find the shortest match to the next character. */
1601 pattern_char = ecl_char(match,++j);
1602 /* k = (position pattern_char source :start i) */
1603 for (k = i;
1604 k < ls && ecl_char(source,k) != pattern_char;
1605 k++)
1606 ;
1607 result = CONS(make_one(source, i, k), result);
1608 i = k;
1609 continue;
1610 }
1611 if (ecl_char(source,i) != pattern_char)
1612 return @':error';
1613 i++, j++;
1614 }
1615 if (i < ls || j < lm)
1616 return @':error';
1617 return result;
1618 }
1619
1620 static cl_object
1621 find_list_wilds(cl_object a, cl_object mask)
1622 {
1623 cl_object l = ECL_NIL, l2;
1624
1625 while (!ecl_endp(mask)) {
1626 cl_object item_mask = CAR(mask);
1627 mask = CDR(mask);
1628 if (item_mask == @':wild-inferiors') {
1629 l2 = ECL_NIL;
1630 while (!path_list_match(a, mask)) {
1631 if (ecl_endp(a))
1632 return @':error';
1633 l2 = CONS(CAR(a),l2);
1634 a = CDR(a);
1635 }
1636 l = CONS(l2, l);
1637 } else if (ecl_endp(a)) {
1638 /* A NIL directory should match against :absolute
1639 or :relative, in order to perform suitable translations. */
1640 if (item_mask != @':absolute' && item_mask != @':relative')
1641 return @':error';
1642 } else {
1643 l2 = find_wilds(CAR(a), item_mask);
1644 if (l2 == @':error')
1645 return @':error';
1646 if (!Null(l2))
1647 l = CONS(l2, l);
1648 a = CDR(a);
1649 }
1650 }
1651 return @nreverse(l);
1652 }
1653
1654 static cl_object
1655 copy_wildcards(cl_object *wilds_list, cl_object pattern)
1656 {
1657 cl_index i, l, j;
1658 bool new_string;
1659 cl_object wilds = *wilds_list, token;
1660
1661 if (pattern == @':wild') {
1662 if (ecl_endp(wilds))
1663 return @':error';
1664 pattern = CAR(wilds);
1665 if(CONSP(pattern)) {
1666 /* find_wilds constructs a list with one element */
1667 if(!Null(CDR(pattern)))
1668 return @':error';
1669 pattern = CAR(pattern);
1670 }
1671 *wilds_list = CDR(wilds);
1672 return pattern;
1673 }
1674
1675 if (pattern == @':wild-inferiors')
1676 return @':error';
1677 if (!ecl_stringp(pattern))
1678 return pattern;
1679
1680 new_string = FALSE;
1681 l = ecl_length(pattern);
1682 token = si_get_buffer_string();
1683 for (j = i = 0; i < l; ) {
1684 cl_index c = ecl_char(pattern, i);
1685 if (c != '*') {
1686 i++;
1687 continue;
1688 }
1689 if (i != j) {
1690 push_substring(token, pattern, j, i);
1691 }
1692 new_string = TRUE;
1693 if (ecl_endp(wilds)) {
1694 return @':error';
1695 }
1696 push_string(token, CAR(wilds));
1697 wilds = CDR(wilds);
1698 j = i++;
1699 }
1700 /* Only create a new string when needed */
1701 if (new_string) {
1702 pattern = cl_copy_seq(token);
1703 }
1704 si_put_buffer_string(token);
1705 *wilds_list = wilds;
1706 return pattern;
1707 }
1708
1709 static cl_object
1710 copy_list_wildcards(cl_object *wilds, cl_object to)
1711 {
1712 cl_object result = ECL_NIL;
1713 while (!ecl_endp(to)) {
1714 cl_object d, mask = CAR(to);
1715 if (mask == @':wild-inferiors') {
1716 cl_object list = *wilds;
1717 if (ecl_endp(list))
1718 return @':error';
1719 else {
1720 cl_object dirlist = CAR(list);
1721 if (CONSP(dirlist))
1722 result = ecl_append(dirlist, result);
1723 else if (!Null(dirlist))
1724 return @':error';
1725 }
1726 *wilds = CDR(list);
1727 } else {
1728 d = copy_wildcards(wilds, mask);
1729 if (d == @':error')
1730 return d;
1731 result = CONS(d, result);
1732 }
1733 to = CDR(to);
1734 }
1735 if (CONSP(result))
1736 result = @nreverse(result);
1737 return result;
1738 }
1739
1740 @(defun translate-pathname (source from to &key)
1741 cl_object wilds, d;
1742 cl_object host, device, directory, name, type, version;
1743 cl_object fromcase, tocase;
1744 @
1745 /* The pathname from which we get the data */
1746 source = cl_pathname(source);
1747 /* The mask applied to the source pathname */
1748 from = cl_pathname(from);
1749 fromcase = normalize_case(from, @':local');
1750 /* The pattern which says what the output should look like */
1751 to = cl_pathname(to);
1752 tocase = normalize_case(to, @':local');
1753
1754 if (source->pathname.logical != from->pathname.logical)
1755 goto error;
1756
1757 /* Match host names */
1758 if (cl_string_equal(2, source->pathname.host, from->pathname.host) == ECL_NIL)
1759 goto error;
1760 host = to->pathname.host;
1761
1762 /* Logical pathnames do not have devices. We just overwrite it. */
1763 device = to->pathname.device;
1764
1765 /* Match directories */
1766 wilds = find_list_wilds(source->pathname.directory,
1767 from->pathname.directory);
1768 if (wilds == @':error') goto error;
1769 if (Null(to->pathname.directory)) {
1770 /* Missing components are replaced */
1771 d = translate_list_case(source->pathname.directory, fromcase, tocase);
1772 } else {
1773 wilds = translate_list_case(wilds, fromcase, tocase);
1774 d = copy_list_wildcards(&wilds, to->pathname.directory);
1775 if (d == @':error') goto error;
1776 if (wilds != ECL_NIL) goto error2;
1777 }
1778 directory = d;
1779
1780 /* Match name */
1781 wilds = find_wilds(source->pathname.name, from->pathname.name);
1782 if (wilds == @':error') goto error2;
1783 if (Null(to->pathname.name)) {
1784 d = translate_component_case(source->pathname.name, fromcase, tocase);
1785 } else {
1786 wilds = translate_list_case(wilds, fromcase, tocase);
1787 d = copy_wildcards(&wilds, to->pathname.name);
1788 if (d == @':error') goto error;
1789 if (wilds != ECL_NIL) goto error2;
1790 }
1791 name = d;
1792
1793 /* Match type */
1794 wilds = find_wilds(source->pathname.type, from->pathname.type);
1795 if (wilds == @':error') goto error2;
1796 if (Null(to->pathname.type)) {
1797 d = translate_component_case(source->pathname.type, fromcase, tocase);
1798 } else {
1799 wilds = translate_list_case(wilds, fromcase, tocase);
1800 d = copy_wildcards(&wilds, to->pathname.type);
1801 if (d == @':error') goto error;
1802 if (wilds != ECL_NIL) goto error2;
1803 }
1804 type = d;
1805
1806 /* Match version */
1807 version = to->pathname.version;
1808 if (from->pathname.version == @':wild') {
1809 if (to->pathname.version == @':wild') {
1810 version = source->pathname.version;
1811 }
1812 }
1813 @(return ecl_make_pathname(host, device, directory, name, type,
1814 version, tocase));
1815 error:
1816 FEerror("~S is not a specialization of path ~S", 2, source, from);
1817 error2:
1818 FEerror("Number of wildcards in ~S do not match ~S", 2, from, to);
1819 @)
1820
1821 @(defun translate-logical-pathname (source &key)
1822 cl_object l, pair;
1823 cl_object pathname;
1824 @
1825 pathname = cl_pathname(source);
1826 begin:
1827 if (!pathname->pathname.logical) {
1828 @(return pathname);
1829 }
1830 l = @si::pathname-translations(1, pathname->pathname.host);
1831 for(; !ecl_endp(l); l = CDR(l)) {
1832 pair = CAR(l);
1833 if (!Null(cl_pathname_match_p(pathname, CAR(pair)))) {
1834 pathname = cl_translate_pathname(3, pathname, CAR(pair), CADR(pair));
1835 goto begin;
1836 }
1837 }
1838 FEerror("~S admits no logical pathname translations", 1, pathname);
1839 @)