1 /* vmsify.c -- Module for vms <-> unix file name conversion
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006 Free Software Foundation, Inc.
4 This file is part of GNU Make.
6 GNU Make is free software; you can redistribute it and/or modify it under the
7 terms of the GNU General Public License as published by the Free Software
8 Foundation; either version 2, or (at your option) any later version.
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along with
15 GNU Make; see the file COPYING. If not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
18 /* Written by Klaus Kämpf (kkaempf@progis.de)
19 of proGIS Software, Aachen, Germany */
34 #include <lib$routines.h>
35 /* Initialize a string descriptor (struct dsc$descriptor_s) for an
36 arbitrary string. ADDR is a pointer to the first character
37 of the string, and LEN is the length of the string. */
39 #define INIT_DSC_S(dsc, addr, len) do { \
40 (dsc).dsc$b_dtype = DSC$K_DTYPE_T; \
41 (dsc).dsc$b_class = DSC$K_CLASS_S; \
42 (dsc).dsc$w_length = (len); \
43 (dsc).dsc$a_pointer = (addr); \
46 /* Initialize a string descriptor (struct dsc$descriptor_s) for a
47 NUL-terminated string. S is a pointer to the string; the length
48 is determined by calling strlen(). */
50 #define INIT_DSC_CSTRING(dsc, s) INIT_DSC_S(dsc, s, strlen(s))
54 copy 'from' to 'to' up to but not including 'upto'
55 return 0 if eos on from
56 return 1 if upto found
58 return 'to' at last char + 1
59 return 'from' at match + 1 or eos if no match
61 if as_dir == 1, change all '.' to '_'
62 else change all '.' but the last to '_'
66 copyto (char **to
, char **from
, char upto
, int as_dir
)
70 s
= strrchr (*from
, '.');
80 while (**from
== upto
);
93 #ifdef HAVE_CASE_INSENSITIVE_FS
94 if (isupper ((unsigned char)**from
))
95 **to
= tolower ((unsigned char)**from
);
109 get translation of logical name
117 static char reslt
[1024];
118 $
DESCRIPTOR (reslt_dsc
, reslt
);
120 struct dsc$descriptor_s name_dsc
;
123 INIT_DSC_CSTRING (name_dsc
, name
);
125 stat
= lib$
sys_trnlog (&name_dsc
, &resltlen
, &reslt_dsc
);
131 if (stat
== SS$_NOTRAN
)
135 reslt
[resltlen
] = '\0';
137 s
= (char *)malloc (resltlen
+1);
151 if (strchr (s
, '\\') == 0)
165 enum namestate
{ N_START
, N_DEVICE
, N_OPEN
, N_DOT
, N_CLOSED
, N_DONE
};
168 convert unix style name to vms style
169 type = 0 -> name is a full name (directory and filename part)
170 type = 1 -> name is a directory
171 type = 2 -> name is a filename without directory
173 The following conversions are applied
175 input full name dir name file name
177 1 ./ <cwd> [] <current directory>.dir
178 2 ../ <home of cwd> <home of cwd> <home of cwd>.dir
180 3 // <dev of cwd>: <dev of cwd>:[000000] <dev of cwd>:000000.dir
182 5 //a/ a: a: a:000000.dir
184 9 / [000000] [000000] 000000.dir
185 10 /a [000000]a [a] [000000]a
186 11 /a/ [a] [a] [000000]a.dir
187 12 /a/b [a]b [a.b] [a]b
188 13 /a/b/ [a.b] [a.b] [a]b.dir
189 14 /a/b/c [a.b]c [a.b.c] [a.b]c
190 15 /a/b/c/ [a.b.c] [a.b.c] [a.b]c.dir
193 17 a/ [.a] [.a] a.dir
194 18 a/b [.a]b [.a.b] [.a]b
195 19 a/b/ [.a.b] [.a.b] [.a]b.dir
196 20 a/b/c [.a.b]c [.a.b.c] [.a.b]c
197 21 a/b/c/ [.a.b.c] [.a.b.c] [.a.b]c.dir
199 22 a.b.c a_b.c [.a_b_c] a_b_c.dir
201 23 [x][y]z [x.y]z [x.y]z [x.y]z
202 24 [x][.y]z [x.y]z [x.y]z [x.y]z
204 25 filenames with '$' are left unchanged if they contain no '/'
205 25 filenames with ':' are left unchanged
206 26 filenames with a single pair of '[' ']' are left unchanged
208 the input string is not written to
212 vmsify (char *name
, int type
)
220 #define MAXPATHLEN 512
222 enum namestate nstate
;
223 static char vmsname
[MAXPATHLEN
+1];
238 s
= strpbrk (name
, "$:");
246 s1
= strchr (s
+1, '[');
247 s2
= strchr (s
+1, ']');
252 if (strchr (name
, '/') == 0)
254 if ((type
== 1) && (s1
!= 0) && (s2
== 0))
256 strcpy (vmsname
, name
);
257 strcat (vmsname
, "]");
266 if ((type
== 1) && (s1
!= 0) && (s2
== 0))
268 strcpy (vmsname
, name
);
269 strcat (vmsname
, "]");
279 s
= strchr (name
, '[');
283 s1
= strchr (s
+1, '[');
287 && (strchr (s
+1, ']') == 0))
289 strcpy (vmsname
, name
);
290 strcat (vmsname
, "]");
294 return name
; /* single [, keep unchanged */
299 return name
; /* not ][, keep unchanged */
306 /* s -> starting char
311 strncpy (vptr
, s
, s1
-s
); /* copy up to but not including ']' */
315 s
= s1
+ 1; /* s -> char behind ']' */
316 if (*s
!= '[') /* was '][' ? */
317 break; /* no, last ] found, exit */
321 s1
= strchr (s
, ']');
322 if (s1
== 0) /* no closing ] */
333 else /* no [ in name */
338 int rooted
= 1; /* flag if logical is rooted, else insert [000000] */
347 case 0: /* start of loop */
353 else if (*fptr
== '.')
362 case 1: /* '/' at start */
372 case 2: /* no '/' at start */
373 s
= strchr (fptr
, '/');
374 if (s
== 0) /* no '/' (16) */
381 copyto (&vptr
, &fptr
, 0, (type
==1));
386 else /* found '/' (17..21) */
389 && (*(s
+1) == 0)) /* 17(2) */
391 copyto (&vptr
, &fptr
, '/', 1);
398 copyto (&vptr
, &fptr
, '/', 1);
405 case 3: /* '//' at start */
406 while (*fptr
== '/') /* collapse all '/' */
408 if (*fptr
== 0) /* just // */
410 char cwdbuf
[MAXPATHLEN
+1];
412 s1
= getcwd(cwdbuf
, MAXPATHLEN
);
415 return ""; /* FIXME, err getcwd */
417 s
= strchr (s1
, ':');
420 return ""; /* FIXME, err no device */
422 strncpy (vptr
, s1
, s
-s1
+1);
430 if (copyto (&vptr
, &fptr
, '/', 1) == 0) /* copy device part */
438 if (*fptr
== 0) /* just '//a/' */
440 strcpy (vptr
+1, "[000000]");
446 /* check logical for [000000] insertion */
449 { /* found translation */
451 for (;;) /* loop over all nested logicals */
453 s2
= s1
+ strlen (s1
) - 1;
454 if (*s2
== ':') /* translation ends in ':' */
464 continue; /* next iteration */
466 if (*s2
== ']') /* translation ends in ']' */
468 if (*(s2
-1) == '.') /* ends in '.]' */
470 if (strncmp (fptr
, "000000", 6) != 0)
475 strcpy (vmsname
, s1
);
476 s
= strchr (vmsname
, ']');
499 strcpy (vptr
, "[000000.");
507 /* s1-> '.' after 000000 or NULL */
509 s
= strchr (fptr
, '/');
512 if (*(vptr
-1) == '.')
514 else if (rooted
== 0)
516 copyto (&vptr
, &fptr
, 0, (type
== 1));
522 while (*(s
+1) == '/') /* skip multiple '/' */
527 && (*(vptr
-1) != '.'))
533 if ((nstate
== N_DOT
)
546 case 4: /* single '/' at start (9..15) */
553 case 5: /* just '/' at start (9) */
559 strcpy (vptr
, "000000");
567 case 6: /* chars following '/' at start 10..15 */
570 s
= strchr (fptr
, '/');
575 strcpy (vptr
, "000000]");
578 copyto (&vptr
, &fptr
, 0, (type
== 1));
588 && (*(s
+1) == 0)) /* 11(2) */
590 strcpy (vptr
, "000000]");
594 copyto (&vptr
, &fptr
, '/', (*(vptr
-1) != ']'));
599 case 7: /* add '.dir' and exit */
600 if ((nstate
== N_OPEN
)
601 || (nstate
== N_DOT
))
618 strcpy (vptr
, ".dir");
623 case 8: /* add ']' and exit */
628 case 9: /* 17..21, fptr -> 1st '/' + 1 */
639 s
= strchr (fptr
, '/');
644 if (nstate
== N_OPEN
)
653 if (nstate
== N_OPEN
)
663 while (*(s
+1) == '/')
666 && (*(s
+1) == 0)) /* 19(2), 21(2)*/
668 if (nstate
!= N_CLOSED
)
677 if (nstate
== N_OPEN
)
685 if ( (*fptr
== '.') /* check for '..' or '../' */
686 && (*(fptr
+1) == '.')
687 && ((*(fptr
+2) == '/')
688 || (*(fptr
+2) == 0)) )
697 while (*fptr
== '/');
701 vptr
--; /* vptr -> '.' or ']' */
706 if (*s1
== '.') /* one back */
712 if (*s1
== '[') /* top level reached */
716 strcpy (s1
, "[000000]");
733 copyto (&vptr
, &fptr
, '/', as_dir
);
747 if (type
== 2) /* 19,21 */
760 case 10: /* 1,2 first is '.' */
770 case 11: /* 2, '..' at start */
774 if (*fptr
!= '/') /* got ..xxx */
781 while (*fptr
== '/') fptr
++;
784 if (*(fptr
+1) != '.')
791 while (*fptr
== '/');
793 { /* got '..' or '../' */
794 char cwdbuf
[MAXPATHLEN
+1];
796 s1
= getcwd(cwdbuf
, MAXPATHLEN
);
799 return ""; /* FIXME, err getcwd */
802 s
= strchr (vptr
, ']');
812 strcpy (s
, "000000]");
820 if (*fptr
== 0) /* had '..' or '../' */
825 else /* had '../xxx' */
835 vptr
+= strlen (vptr
);
839 case 12: /* 1, '.' at start */
851 char cwdbuf
[MAXPATHLEN
+1];
853 s1
= getcwd(cwdbuf
, MAXPATHLEN
);
856 return ""; /*FIXME, err getcwd */
866 s
= strchr (vptr
, ']');
874 vptr
+= strlen (vptr
);
888 /* directory conversion done
889 fptr -> filename part of input string
890 vptr -> free space in vmsname
901 convert from vms-style to unix-style
903 dev:[dir1.dir2] //dev/dir1/dir2/
909 static char piece
[512];
912 if (strchr (name
, '/') != 0) /* already in unix style */
920 s
= strchr (name
, ':');
935 s
= strchr (name
, '[');
949 strcat (p
, "./"); /* [. */
969 if (*s
!= 0) /* more after ']' ?? */
973 strcpy (p
, s
); /* copy it anyway */
977 else /* no '[' anywhere */
983 /* force end with '/' */