1 /* vmsify.c -- Module for vms <-> unix file name conversion
2 Copyright (C) 1996-2013 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3 of the License, or (at your option) any later
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 this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Klaus Kämpf (kkaempf@progis.de)
18 of proGIS Software, Aachen, Germany */
33 #include <lib$routines.h>
34 /* Initialize a string descriptor (struct dsc$descriptor_s) for an
35 arbitrary string. ADDR is a pointer to the first character
36 of the string, and LEN is the length of the string. */
38 #define INIT_DSC_S(dsc, addr, len) do { \
39 (dsc).dsc$b_dtype = DSC$K_DTYPE_T; \
40 (dsc).dsc$b_class = DSC$K_CLASS_S; \
41 (dsc).dsc$w_length = (len); \
42 (dsc).dsc$a_pointer = (addr); \
45 /* Initialize a string descriptor (struct dsc$descriptor_s) for a
46 NUL-terminated string. S is a pointer to the string; the length
47 is determined by calling strlen(). */
49 #define INIT_DSC_CSTRING(dsc, s) INIT_DSC_S(dsc, s, strlen(s))
53 copy 'from' to 'to' up to but not including 'upto'
54 return 0 if eos on from
55 return 1 if upto found
57 return 'to' at last char + 1
58 return 'from' at match + 1 or eos if no match
60 if as_dir == 1, change all '.' to '_'
61 else change all '.' but the last to '_'
65 copyto (char **to, const char **from, char upto, int as_dir)
69 s = strrchr (*from, '.');
79 while (**from == upto);
92 #ifdef HAVE_CASE_INSENSITIVE_FS
93 if (isupper ((unsigned char)**from))
94 **to = tolower ((unsigned char)**from);
108 get translation of logical name
113 trnlog (const char *name)
116 static char reslt[1024];
117 $DESCRIPTOR (reslt_dsc, reslt);
119 struct dsc$descriptor_s name_dsc;
123 * the string isn't changed, but there is no string descriptor with
124 * "const char *dsc$a_pointer"
126 INIT_DSC_CSTRING (name_dsc, (char *)name);
128 stat = lib$sys_trnlog (&name_dsc, &resltlen, &reslt_dsc);
134 if (stat == SS$_NOTRAN)
138 reslt[resltlen] = '\0';
140 s = malloc (resltlen+1);
154 if (strchr (s, '\\') == 0)
168 enum namestate { N_START, N_DEVICE, N_OPEN, N_DOT, N_CLOSED, N_DONE };
171 convert unix style name to vms style
172 type = 0 -> name is a full name (directory and filename part)
173 type = 1 -> name is a directory
174 type = 2 -> name is a filename without directory
176 The following conversions are applied
178 input full name dir name file name
180 1 ./ <cwd> [] <current directory>.dir
181 2 ../ <home of cwd> <home of cwd> <home of cwd>.dir
183 3 // <dev of cwd>: <dev of cwd>:[000000] <dev of cwd>:000000.dir
185 5 //a/ a: a: a:000000.dir
187 9 / [000000] [000000] 000000.dir
188 10 /a [000000]a [a] [000000]a
189 11 /a/ [a] [a] [000000]a.dir
190 12 /a/b [a]b [a.b] [a]b
191 13 /a/b/ [a.b] [a.b] [a]b.dir
192 14 /a/b/c [a.b]c [a.b.c] [a.b]c
193 15 /a/b/c/ [a.b.c] [a.b.c] [a.b]c.dir
196 17 a/ [.a] [.a] a.dir
197 18 a/b [.a]b [.a.b] [.a]b
198 19 a/b/ [.a.b] [.a.b] [.a]b.dir
199 20 a/b/c [.a.b]c [.a.b.c] [.a.b]c
200 21 a/b/c/ [.a.b.c] [.a.b.c] [.a.b]c.dir
202 22 a.b.c a_b.c [.a_b_c] a_b_c.dir
204 23 [x][y]z [x.y]z [x.y]z [x.y]z
205 24 [x][.y]z [x.y]z [x.y]z [x.y]z
207 25 filenames with '$' are left unchanged if they contain no '/'
208 25 filenames with ':' are left unchanged
209 26 filenames with a single pair of '[' ']' are left unchanged
211 The input string is not written to. The result is also const because
212 it's a static buffer; we don't want to change it.
216 vmsify (const char *name, int type)
224 #define MAXPATHLEN 512
226 enum namestate nstate;
227 static char vmsname[MAXPATHLEN+1];
244 t = strpbrk (name, "$:");
253 s1 = strchr (t+1, '[');
254 s2 = strchr (t+1, ']');
259 if (strchr (name, '/') == 0)
261 strcpy (vmsname, name);
262 if ((type == 1) && (s1 != 0) && (s2 == 0))
263 strcat (vmsname, "]");
269 strcpy (vmsname, name);
270 if ((type == 1) && (s1 != 0) && (s2 == 0))
271 strcat (vmsname, "]");
277 t = strchr (name, '[');
282 // const char *s1 = strchr (t+1, '[');
283 s1 = strchr (t+1, '[');
286 strcpy (vmsname, name);
287 if ((type == 1) && (strchr (t+1, ']') == 0))
288 strcat (vmsname, "]");
294 strcpy (vmsname, name);
295 return vmsname; /* not ][, keep unchanged */
302 /* s -> starting char
306 strncpy (vptr, s, s1-s); /* copy up to but not including ']' */
310 s = s1 + 1; /* s -> char behind ']' */
311 if (*s != '[') /* was '][' ? */
312 break; /* no, last ] found, exit */
316 s1 = strchr (s, ']');
317 if (s1 == 0) /* no closing ] */
327 else /* no [ in name */
330 int rooted = 1; /* flag if logical is rooted, else insert [000000] */
336 case 0: /* start of loop */
342 else if (*fptr == '.')
351 case 1: /* '/' at start */
361 case 2: /* no '/' at start */
363 const char *s = strchr (fptr, '/');
364 if (s == 0) /* no '/' (16) */
371 copyto (&vptr, &fptr, 0, (type==1));
376 else /* found '/' (17..21) */
379 && (*(s+1) == 0)) /* 17(2) */
381 copyto (&vptr, &fptr, '/', 1);
388 copyto (&vptr, &fptr, '/', 1);
396 case 3: /* '//' at start */
401 while (*fptr == '/') /* collapse all '/' */
403 if (*fptr == 0) /* just // */
405 char cwdbuf[MAXPATHLEN+1];
407 s1 = getcwd(cwdbuf, MAXPATHLEN);
411 return vmsname; /* FIXME, err getcwd */
413 s = strchr (s1, ':');
417 return vmsname; /* FIXME, err no device */
419 strncpy (vptr, s1, s-s1+1);
427 if (copyto (&vptr, &fptr, '/', 1) == 0) /* copy device part */
435 if (*fptr == 0) /* just '//a/' */
437 strcpy (vptr+1, "[000000]");
443 /* check logical for [000000] insertion */
446 { /* found translation */
447 for (;;) /* loop over all nested logicals */
449 char *vp2 = vp + strlen (vp) - 1;
450 if (*vp2 == ':') /* translation ends in ':' */
460 continue; /* next iteration */
462 if (*vp2 == ']') /* translation ends in ']' */
464 if (*(vp2-1) == '.') /* ends in '.]' */
466 if (strncmp (fptr, "000000", 6) != 0)
471 strcpy (vmsname, s1);
472 vp = strchr (vmsname, ']');
496 strcpy (vptr, "[000000.");
503 /* vp-> '.' after 000000 or NULL */
505 s = strchr (fptr, '/');
508 if (*(vptr-1) == '.')
510 else if (rooted == 0)
512 copyto (&vptr, &fptr, 0, (type == 1));
518 while (*(s+1) == '/') /* skip multiple '/' */
523 && (*(vptr-1) != '.'))
529 if ((nstate == N_DOT)
542 case 4: /* single '/' at start (9..15) */
549 case 5: /* just '/' at start (9) */
555 strcpy (vptr, "000000");
563 case 6: /* chars following '/' at start 10..15 */
568 s = strchr (fptr, '/');
573 strcpy (vptr, "000000]");
576 copyto (&vptr, &fptr, 0, (type == 1));
586 && (*(s+1) == 0)) /* 11(2) */
588 strcpy (vptr, "000000]");
592 copyto (&vptr, &fptr, '/', (*(vptr-1) != ']'));
598 case 7: /* add '.dir' and exit */
599 if ((nstate == N_OPEN)
600 || (nstate == N_DOT))
617 strcpy (vptr, ".dir");
622 case 8: /* add ']' and exit */
627 case 9: /* 17..21, fptr -> 1st '/' + 1 */
640 s = strchr (fptr, '/');
645 if (nstate == N_OPEN)
654 if (nstate == N_OPEN)
664 while (*(s+1) == '/')
667 && (*(s+1) == 0)) /* 19(2), 21(2)*/
669 if (nstate != N_CLOSED)
678 if (nstate == N_OPEN)
686 if ( (*fptr == '.') /* check for '..' or '../' */
687 && (*(fptr+1) == '.')
688 && ((*(fptr+2) == '/')
689 || (*(fptr+2) == 0)) )
699 while (*fptr == '/');
703 vptr--; /* vptr -> '.' or ']' */
708 if (*vp == '.') /* one back */
714 if (*vp == '[') /* top level reached */
718 strcpy (vp, "[000000]");
735 copyto (&vptr, &fptr, '/', as_dir);
749 if (type == 2) /* 19,21 */
763 case 10: /* 1,2 first is '.' */
773 case 11: /* 2, '..' at start */
777 if (*fptr != '/') /* got ..xxx */
779 strcpy (vmsname, name);
785 while (*fptr == '/') fptr++;
788 if (*(fptr+1) != '.')
795 while (*fptr == '/');
797 { /* got '..' or '../' */
799 char cwdbuf[MAXPATHLEN+1];
801 vp = getcwd(cwdbuf, MAXPATHLEN);
805 return vmsname; /* FIXME, err getcwd */
808 vp = strchr (vptr, ']');
818 strcpy (vp, "000000]");
826 if (*fptr == 0) /* had '..' or '../' */
831 else /* had '../xxx' */
841 vptr += strlen (vptr);
845 case 12: /* 1, '.' at start */
850 strcpy (vmsname, name);
859 char cwdbuf[MAXPATHLEN+1];
861 vp = getcwd(cwdbuf, MAXPATHLEN);
865 return vmsname; /*FIXME, err getcwd */
876 char *vp = strchr (vptr, ']');
884 vptr += strlen (vptr);
897 /* directory conversion done
898 fptr -> filename part of input string
899 vptr -> free space in vmsname
910 convert from vms-style to unix-style
912 dev:[dir1.dir2] //dev/dir1/dir2/
916 unixify (const char *name)
918 static char piece[512];
922 if (strchr (name, '/') != 0) /* already in unix style */
924 strcpy (piece, name);
933 s = strchr (name, ':');
940 strncpy (p, name, l);
947 s = strchr (name, '[');
961 strcat (p, "./"); /* [. */
981 if (*s != 0) /* more after ']' ?? */
985 strcpy (p, s); /* copy it anyway */
989 else /* no '[' anywhere */
995 /* force end with '/' */