Fixups for warnings on Windows (esp 64bit).
[make.git] / vmsify.c
blob178fc5153dc58e006c395ce53f5e967dd47be5d1
1 /* vmsify.c -- Module for vms <-> unix file name conversion
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 2007, 2008, 2009 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 3 of the License, or (at your option) any later
9 version.
11 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along with
16 this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Written by Klaus Kämpf (kkaempf@progis.de)
19 of proGIS Software, Aachen, Germany */
22 #include <stdio.h>
23 #include <string.h>
24 #include <ctype.h>
26 #if VMS
27 #include <unixlib.h>
28 #include <stdlib.h>
29 #include <jpidef.h>
30 #include <descrip.h>
31 #include <uaidef.h>
32 #include <ssdef.h>
33 #include <starlet.h>
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); \
44 } while (0)
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))
51 #endif
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 '_'
65 static int
66 copyto (char **to, const char **from, char upto, int as_dir)
68 const char *s;
70 s = strrchr (*from, '.');
72 while (**from)
74 if (**from == upto)
78 (*from)++;
80 while (**from == upto);
81 return 1;
83 if (**from == '.')
85 if ((as_dir == 1)
86 || (*from != s))
87 **to = '_';
88 else
89 **to = '.';
91 else
93 #ifdef HAVE_CASE_INSENSITIVE_FS
94 if (isupper ((unsigned char)**from))
95 **to = tolower ((unsigned char)**from);
96 else
97 #endif
98 **to = **from;
100 (*to)++;
101 (*from)++;
104 return 0;
109 get translation of logical name
113 static char *
114 trnlog (const char *name)
116 int stat;
117 static char reslt[1024];
118 $DESCRIPTOR (reslt_dsc, reslt);
119 short resltlen;
120 struct dsc$descriptor_s name_dsc;
121 char *s;
124 * the string isn't changed, but there is no string descriptor with
125 * "const char *dsc$a_pointer"
127 INIT_DSC_CSTRING (name_dsc, (char *)name);
129 stat = lib$sys_trnlog (&name_dsc, &resltlen, &reslt_dsc);
131 if ((stat&1) == 0)
133 return "";
135 if (stat == SS$_NOTRAN)
137 return "";
139 reslt[resltlen] = '\0';
141 s = malloc (resltlen+1);
142 if (s == 0)
143 return "";
144 strcpy (s, reslt);
145 return s;
148 static char *
149 showall (char *s)
151 static char t[512];
152 char *pt;
154 pt = t;
155 if (strchr (s, '\\') == 0)
156 return s;
157 while (*s)
159 if (*s == '\\')
161 *pt++ = *s;
163 *pt++ = *s++;
165 return pt;
169 enum namestate { N_START, N_DEVICE, N_OPEN, N_DOT, N_CLOSED, N_DONE };
172 convert unix style name to vms style
173 type = 0 -> name is a full name (directory and filename part)
174 type = 1 -> name is a directory
175 type = 2 -> name is a filename without directory
177 The following conversions are applied
178 (0) (1) (2)
179 input full name dir name file name
181 1 ./ <cwd> [] <current directory>.dir
182 2 ../ <home of cwd> <home of cwd> <home of cwd>.dir
184 3 // <dev of cwd>: <dev of cwd>:[000000] <dev of cwd>:000000.dir
185 4 //a a: a: a:
186 5 //a/ a: a: a:000000.dir
188 9 / [000000] [000000] 000000.dir
189 10 /a [000000]a [a] [000000]a
190 11 /a/ [a] [a] [000000]a.dir
191 12 /a/b [a]b [a.b] [a]b
192 13 /a/b/ [a.b] [a.b] [a]b.dir
193 14 /a/b/c [a.b]c [a.b.c] [a.b]c
194 15 /a/b/c/ [a.b.c] [a.b.c] [a.b]c.dir
196 16 a a [.a] a
197 17 a/ [.a] [.a] a.dir
198 18 a/b [.a]b [.a.b] [.a]b
199 19 a/b/ [.a.b] [.a.b] [.a]b.dir
200 20 a/b/c [.a.b]c [.a.b.c] [.a.b]c
201 21 a/b/c/ [.a.b.c] [.a.b.c] [.a.b]c.dir
203 22 a.b.c a_b.c [.a_b_c] a_b_c.dir
205 23 [x][y]z [x.y]z [x.y]z [x.y]z
206 24 [x][.y]z [x.y]z [x.y]z [x.y]z
208 25 filenames with '$' are left unchanged if they contain no '/'
209 25 filenames with ':' are left unchanged
210 26 filenames with a single pair of '[' ']' are left unchanged
212 The input string is not written to. The result is also const because
213 it's a static buffer; we don't want to change it.
216 const char *
217 vmsify (const char *name, int type)
219 /* max 255 device
220 max 39 directory
221 max 39 filename
222 max 39 filetype
223 max 5 version
225 #define MAXPATHLEN 512
227 enum namestate nstate;
228 static char vmsname[MAXPATHLEN+1];
229 const char *fptr;
230 const char *t;
231 char *vptr;
232 int as_dir;
233 int count;
234 const char *s;
235 const char *s1;
236 const char *s2;
238 if (name == 0)
239 return 0;
240 fptr = name;
241 vptr = vmsname;
242 nstate = N_START;
244 /* case 25a */
245 t = strpbrk (name, "$:");
247 if (t != 0)
249 // const char *s1;
250 // const char *s2;
252 if (type == 1)
254 s1 = strchr (t+1, '[');
255 s2 = strchr (t+1, ']');
258 if (*t == '$')
260 if (strchr (name, '/') == 0)
262 strcpy (vmsname, name);
263 if ((type == 1) && (s1 != 0) && (s2 == 0))
264 strcat (vmsname, "]");
265 return vmsname;
268 else
270 strcpy (vmsname, name);
271 if ((type == 1) && (s1 != 0) && (s2 == 0))
272 strcat (vmsname, "]");
273 return vmsname;
277 /* case 26 */
278 t = strchr (name, '[');
280 if (t != 0)
282 // const char *s;
283 // const char *s1 = strchr (t+1, '[');
284 s1 = strchr (t+1, '[');
285 if (s1 == 0)
287 strcpy (vmsname, name);
288 if ((type == 1) && (strchr (t+1, ']') == 0))
289 strcat (vmsname, "]");
290 return vmsname;
292 s1--;
293 if (*s1 != ']')
295 strcpy (vmsname, name);
296 return vmsname; /* not ][, keep unchanged */
299 /* we have ][ */
301 s = name;
303 /* s -> starting char
304 s1 -> ending ']' */
307 strncpy (vptr, s, s1-s); /* copy up to but not including ']' */
308 vptr += s1-s;
309 if (*s1 == 0)
310 break;
311 s = s1 + 1; /* s -> char behind ']' */
312 if (*s != '[') /* was '][' ? */
313 break; /* no, last ] found, exit */
314 s++;
315 if (*s != '.')
316 *vptr++ = '.';
317 s1 = strchr (s, ']');
318 if (s1 == 0) /* no closing ] */
319 s1 = s + strlen (s);
321 while (1);
323 *vptr++ = ']';
325 fptr = s;
328 else /* no [ in name */
330 int state = 0;
331 int rooted = 1; /* flag if logical is rooted, else insert [000000] */
335 switch (state)
337 case 0: /* start of loop */
338 if (*fptr == '/')
340 fptr++;
341 state = 1;
343 else if (*fptr == '.')
345 fptr++;
346 state = 10;
348 else
349 state = 2;
350 break;
352 case 1: /* '/' at start */
353 if (*fptr == '/')
355 fptr++;
356 state = 3;
358 else
359 state = 4;
360 break;
362 case 2: /* no '/' at start */
364 const char *s = strchr (fptr, '/');
365 if (s == 0) /* no '/' (16) */
367 if (type == 1)
369 strcpy (vptr, "[.");
370 vptr += 2;
372 copyto (&vptr, &fptr, 0, (type==1));
373 if (type == 1)
374 *vptr++ = ']';
375 state = -1;
377 else /* found '/' (17..21) */
379 if ((type == 2)
380 && (*(s+1) == 0)) /* 17(2) */
382 copyto (&vptr, &fptr, '/', 1);
383 state = 7;
385 else
387 strcpy (vptr, "[.");
388 vptr += 2;
389 copyto (&vptr, &fptr, '/', 1);
390 nstate = N_OPEN;
391 state = 9;
394 break;
397 case 3: /* '//' at start */
399 // const char *s;
400 // const char *s1;
401 char *vp;
402 while (*fptr == '/') /* collapse all '/' */
403 fptr++;
404 if (*fptr == 0) /* just // */
406 char cwdbuf[MAXPATHLEN+1];
408 s1 = getcwd(cwdbuf, MAXPATHLEN);
409 if (s1 == 0)
411 vmsname[0] = '\0';
412 return vmsname; /* FIXME, err getcwd */
414 s = strchr (s1, ':');
415 if (s == 0)
417 vmsname[0] = '\0';
418 return vmsname; /* FIXME, err no device */
420 strncpy (vptr, s1, s-s1+1);
421 vptr += s-s1+1;
422 state = -1;
423 break;
426 s = vptr;
428 if (copyto (&vptr, &fptr, '/', 1) == 0) /* copy device part */
430 *vptr++ = ':';
431 state = -1;
432 break;
434 *vptr = ':';
435 nstate = N_DEVICE;
436 if (*fptr == 0) /* just '//a/' */
438 strcpy (vptr+1, "[000000]");
439 vptr += 9;
440 state = -1;
441 break;
443 *vptr = 0;
444 /* check logical for [000000] insertion */
445 vp = trnlog (s);
446 if (*vp != '\0')
447 { /* found translation */
448 for (;;) /* loop over all nested logicals */
450 char *vp2 = vp + strlen (vp) - 1;
451 if (*vp2 == ':') /* translation ends in ':' */
453 vp2 = trnlog (vp);
454 free (vp);
455 if (*vp2 == 0)
457 rooted = 0;
458 break;
460 vp = vp2;
461 continue; /* next iteration */
463 if (*vp2 == ']') /* translation ends in ']' */
465 if (*(vp2-1) == '.') /* ends in '.]' */
467 if (strncmp (fptr, "000000", 6) != 0)
468 rooted = 0;
470 else
472 strcpy (vmsname, s1);
473 vp = strchr (vmsname, ']');
474 *vp = '.';
475 nstate = N_DOT;
476 vptr = vp;
479 break;
481 free (vp);
483 else
484 rooted = 0;
486 if (*vptr == 0)
488 nstate = N_DEVICE;
489 *vptr++ = ':';
491 else
492 vptr++;
494 if (rooted == 0)
496 nstate = N_DOT;
497 strcpy (vptr, "[000000.");
498 vptr += 8;
499 vp = vptr-1;
501 else
502 vp = 0;
504 /* vp-> '.' after 000000 or NULL */
506 s = strchr (fptr, '/');
507 if (s == 0)
508 { /* no next '/' */
509 if (*(vptr-1) == '.')
510 *(vptr-1) = ']';
511 else if (rooted == 0)
512 *vptr++ = ']';
513 copyto (&vptr, &fptr, 0, (type == 1));
514 state = -1;
515 break;
517 else
519 while (*(s+1) == '/') /* skip multiple '/' */
520 s++;
523 if ((rooted != 0)
524 && (*(vptr-1) != '.'))
526 *vptr++ = '[';
527 nstate = N_DOT;
529 else
530 if ((nstate == N_DOT)
531 && (vp != 0)
532 && (*(s+1) == 0))
534 if (type == 2)
536 *vp = ']';
537 nstate = N_CLOSED;
540 state = 9;
541 break;
543 case 4: /* single '/' at start (9..15) */
544 if (*fptr == 0)
545 state = 5;
546 else
547 state = 6;
548 break;
550 case 5: /* just '/' at start (9) */
551 if (type != 2)
553 *vptr++ = '[';
554 nstate = N_OPEN;
556 strcpy (vptr, "000000");
557 vptr += 6;
558 if (type == 2)
559 state = 7;
560 else
561 state = 8;
562 break;
564 case 6: /* chars following '/' at start 10..15 */
566 const char *s;
567 *vptr++ = '[';
568 nstate = N_OPEN;
569 s = strchr (fptr, '/');
570 if (s == 0) /* 10 */
572 if (type != 1)
574 strcpy (vptr, "000000]");
575 vptr += 7;
577 copyto (&vptr, &fptr, 0, (type == 1));
578 if (type == 1)
580 *vptr++ = ']';
582 state = -1;
584 else /* 11..15 */
586 if ( (type == 2)
587 && (*(s+1) == 0)) /* 11(2) */
589 strcpy (vptr, "000000]");
590 nstate = N_CLOSED;
591 vptr += 7;
593 copyto (&vptr, &fptr, '/', (*(vptr-1) != ']'));
594 state = 9;
596 break;
599 case 7: /* add '.dir' and exit */
600 if ((nstate == N_OPEN)
601 || (nstate == N_DOT))
603 char *vp = vptr-1;
604 while (vp > vmsname)
606 if (*vp == ']')
608 break;
610 if (*vp == '.')
612 *vp = ']';
613 break;
615 vp--;
618 strcpy (vptr, ".dir");
619 vptr += 4;
620 state = -1;
621 break;
623 case 8: /* add ']' and exit */
624 *vptr++ = ']';
625 state = -1;
626 break;
628 case 9: /* 17..21, fptr -> 1st '/' + 1 */
630 const char *s;
631 if (*fptr == 0)
633 if (type == 2)
635 state = 7;
637 else
638 state = 8;
639 break;
641 s = strchr (fptr, '/');
642 if (s == 0)
644 if (type != 1)
646 if (nstate == N_OPEN)
648 *vptr++ = ']';
649 nstate = N_CLOSED;
651 as_dir = 0;
653 else
655 if (nstate == N_OPEN)
657 *vptr++ = '.';
658 nstate = N_DOT;
660 as_dir = 1;
663 else
665 while (*(s+1) == '/')
666 s++;
667 if ( (type == 2)
668 && (*(s+1) == 0)) /* 19(2), 21(2)*/
670 if (nstate != N_CLOSED)
672 *vptr++ = ']';
673 nstate = N_CLOSED;
675 as_dir = 1;
677 else
679 if (nstate == N_OPEN)
681 *vptr++ = '.';
682 nstate = N_DOT;
684 as_dir = 1;
687 if ( (*fptr == '.') /* check for '..' or '../' */
688 && (*(fptr+1) == '.')
689 && ((*(fptr+2) == '/')
690 || (*(fptr+2) == 0)) )
692 char *vp;
693 fptr += 2;
694 if (*fptr == '/')
698 fptr++;
700 while (*fptr == '/');
702 else if (*fptr == 0)
703 type = 1;
704 vptr--; /* vptr -> '.' or ']' */
705 vp = vptr;
706 for (;;)
708 vp--;
709 if (*vp == '.') /* one back */
711 vptr = vp;
712 nstate = N_OPEN;
713 break;
715 if (*vp == '[') /* top level reached */
717 if (*fptr == 0)
719 strcpy (vp, "[000000]");
720 vptr = vp + 8;
721 nstate = N_CLOSED;
722 s = 0;
723 break;
725 else
727 vptr = vp+1;
728 nstate = N_OPEN;
729 break;
734 else
736 copyto (&vptr, &fptr, '/', as_dir);
737 if (nstate == N_DOT)
738 nstate = N_OPEN;
740 if (s == 0)
741 { /* 18,20 */
742 if (type == 1)
743 *vptr++ = ']';
744 state = -1;
746 else
748 if (*(s+1) == 0)
750 if (type == 2) /* 19,21 */
752 state = 7;
754 else
756 *vptr++ = ']';
757 state = -1;
761 break;
764 case 10: /* 1,2 first is '.' */
765 if (*fptr == '.')
767 fptr++;
768 state = 11;
770 else
771 state = 12;
772 break;
774 case 11: /* 2, '..' at start */
775 count = 1;
776 if (*fptr != 0)
778 if (*fptr != '/') /* got ..xxx */
780 strcpy (vmsname, name);
781 return vmsname;
783 do /* got ../ */
785 fptr++;
786 while (*fptr == '/') fptr++;
787 if (*fptr != '.')
788 break;
789 if (*(fptr+1) != '.')
790 break;
791 fptr += 2;
792 if ((*fptr == 0)
793 || (*fptr == '/'))
794 count++;
796 while (*fptr == '/');
798 { /* got '..' or '../' */
799 char *vp;
800 char cwdbuf[MAXPATHLEN+1];
802 vp = getcwd(cwdbuf, MAXPATHLEN);
803 if (vp == 0)
805 vmsname[0] = '\0';
806 return vmsname; /* FIXME, err getcwd */
808 strcpy (vptr, vp);
809 vp = strchr (vptr, ']');
810 if (vp != 0)
812 nstate = N_OPEN;
813 while (vp > vptr)
815 vp--;
816 if (*vp == '[')
818 vp++;
819 strcpy (vp, "000000]");
820 state = -1;
821 break;
823 else if (*vp == '.')
825 if (--count == 0)
827 if (*fptr == 0) /* had '..' or '../' */
829 *vp++ = ']';
830 state = -1;
832 else /* had '../xxx' */
834 state = 9;
836 *vp = '\0';
837 break;
842 vptr += strlen (vptr);
844 break;
846 case 12: /* 1, '.' at start */
847 if (*fptr != 0)
849 if (*fptr != '/')
851 strcpy (vmsname, name);
852 return vmsname;
854 while (*fptr == '/')
855 fptr++;
859 char *vp;
860 char cwdbuf[MAXPATHLEN+1];
862 vp = getcwd(cwdbuf, MAXPATHLEN);
863 if (vp == 0)
865 vmsname[0] = '\0';
866 return vmsname; /*FIXME, err getcwd */
868 strcpy (vptr, vp);
870 if (*fptr == 0)
872 state = -1;
873 break;
875 else
877 char *vp = strchr (vptr, ']');
878 if (vp == 0)
880 state = -1;
881 break;
883 *vp = '\0';
884 nstate = N_OPEN;
885 vptr += strlen (vptr);
886 state = 9;
888 break;
892 while (state > 0);
898 /* directory conversion done
899 fptr -> filename part of input string
900 vptr -> free space in vmsname
903 *vptr++ = 0;
905 return vmsname;
911 convert from vms-style to unix-style
913 dev:[dir1.dir2] //dev/dir1/dir2/
916 const char *
917 unixify (const char *name)
919 static char piece[512];
920 const char *s;
921 char *p;
923 if (strchr (name, '/') != 0) /* already in unix style */
925 strcpy (piece, name);
926 return piece;
929 p = piece;
930 *p = 0;
932 /* device part */
934 s = strchr (name, ':');
936 if (s != 0)
938 int l = s - name;
939 *p++ = '/';
940 *p++ = '/';
941 strncpy (p, name, l);
942 p += l;
945 /* directory part */
947 *p++ = '/';
948 s = strchr (name, '[');
950 if (s != 0)
952 s++;
953 switch (*s)
955 case ']': /* [] */
956 strcat (p, "./");
957 break;
958 case '-': /* [- */
959 strcat (p, "../");
960 break;
961 case '.':
962 strcat (p, "./"); /* [. */
963 break;
964 default:
965 s--;
966 break;
968 s++;
969 while (*s)
971 if (*s == '.')
972 *p++ = '/';
973 else
974 *p++ = *s;
975 s++;
976 if (*s == ']')
978 s++;
979 break;
982 if (*s != 0) /* more after ']' ?? */
984 if (*(p-1) != '/')
985 *p++ = '/';
986 strcpy (p, s); /* copy it anyway */
990 else /* no '[' anywhere */
993 *p++ = 0;
996 /* force end with '/' */
998 if (*(p-1) != '/')
999 *p++ = '/';
1000 *p = 0;
1002 return piece;
1005 /* EOF */