Add a new variable: MAKE_RESTARTS, to count how many times make has re-exec'd.
[make.git] / vmsify.c
blobd8d3d85a83ce1c12fb22fd7078e5447aa7c2d194
1 /*
2 vmsify.c
4 Module for vms <-> unix file name conversion
6 Written by Klaus Kämpf (kkaempf@progis.de)
7 of proGIS Software, Aachen, Germany
9 */
11 #include <stdio.h>
12 #include <string.h>
13 #include <ctype.h>
15 #if VMS
16 #include <unixlib.h>
17 #include <stdlib.h>
18 #include <jpidef.h>
19 #include <descrip.h>
20 #include <uaidef.h>
21 #include <ssdef.h>
22 #include <starlet.h>
23 #include <lib$routines.h>
24 /* Initialize a string descriptor (struct dsc$descriptor_s) for an
25 arbitrary string. ADDR is a pointer to the first character
26 of the string, and LEN is the length of the string. */
28 #define INIT_DSC_S(dsc, addr, len) do { \
29 (dsc).dsc$b_dtype = DSC$K_DTYPE_T; \
30 (dsc).dsc$b_class = DSC$K_CLASS_S; \
31 (dsc).dsc$w_length = (len); \
32 (dsc).dsc$a_pointer = (addr); \
33 } while (0)
35 /* Initialize a string descriptor (struct dsc$descriptor_s) for a
36 NUL-terminated string. S is a pointer to the string; the length
37 is determined by calling strlen(). */
39 #define INIT_DSC_CSTRING(dsc, s) INIT_DSC_S(dsc, s, strlen(s))
40 #endif
43 copy 'from' to 'to' up to but not including 'upto'
44 return 0 if eos on from
45 return 1 if upto found
47 return 'to' at last char + 1
48 return 'from' at match + 1 or eos if no match
50 if as_dir == 1, change all '.' to '_'
51 else change all '.' but the last to '_'
54 static int
55 copyto (char **to, char **from, char upto, int as_dir)
57 char *s;
59 s = strrchr (*from, '.');
61 while (**from)
63 if (**from == upto)
67 (*from)++;
69 while (**from == upto);
70 return 1;
72 if (**from == '.')
74 if ((as_dir == 1)
75 || (*from != s))
76 **to = '_';
77 else
78 **to = '.';
80 else
82 if (isupper ((unsigned char)**from))
83 **to = tolower ((unsigned char)**from);
84 else
85 **to = **from;
87 (*to)++;
88 (*from)++;
91 return 0;
96 get translation of logical name
100 static char *
101 trnlog (char *name)
103 int stat;
104 static char reslt[1024];
105 $DESCRIPTOR (reslt_dsc, reslt);
106 short resltlen;
107 struct dsc$descriptor_s name_dsc;
108 char *s;
110 INIT_DSC_CSTRING (name_dsc, name);
112 stat = lib$sys_trnlog (&name_dsc, &resltlen, &reslt_dsc);
114 if ((stat&1) == 0)
116 return "";
118 if (stat == SS$_NOTRAN)
120 return "";
122 reslt[resltlen] = '\0';
124 s = (char *)malloc (resltlen+1);
125 if (s == 0)
126 return "";
127 strcpy (s, reslt);
128 return s;
131 static char *
132 showall (char *s)
134 static char t[512];
135 char *pt;
137 pt = t;
138 if (strchr (s, '\\') == 0)
139 return s;
140 while (*s)
142 if (*s == '\\')
144 *pt++ = *s;
146 *pt++ = *s++;
148 return pt;
152 enum namestate { N_START, N_DEVICE, N_OPEN, N_DOT, N_CLOSED, N_DONE };
155 convert unix style name to vms style
156 type = 0 -> name is a full name (directory and filename part)
157 type = 1 -> name is a directory
158 type = 2 -> name is a filename without directory
160 The following conversions are applied
161 (0) (1) (2)
162 input full name dir name file name
164 1 ./ <cwd> [] <current directory>.dir
165 2 ../ <home of cwd> <home of cwd> <home of cwd>.dir
167 3 // <dev of cwd>: <dev of cwd>:[000000] <dev of cwd>:000000.dir
168 4 //a a: a: a:
169 5 //a/ a: a: a:000000.dir
171 9 / [000000] [000000] 000000.dir
172 10 /a [000000]a [a] [000000]a
173 11 /a/ [a] [a] [000000]a.dir
174 12 /a/b [a]b [a.b] [a]b
175 13 /a/b/ [a.b] [a.b] [a]b.dir
176 14 /a/b/c [a.b]c [a.b.c] [a.b]c
177 15 /a/b/c/ [a.b.c] [a.b.c] [a.b]c.dir
179 16 a a [.a] a
180 17 a/ [.a] [.a] a.dir
181 18 a/b [.a]b [.a.b] [.a]b
182 19 a/b/ [.a.b] [.a.b] [.a]b.dir
183 20 a/b/c [.a.b]c [.a.b.c] [.a.b]c
184 21 a/b/c/ [.a.b.c] [.a.b.c] [.a.b]c.dir
186 22 a.b.c a_b.c [.a_b_c] a_b_c.dir
188 23 [x][y]z [x.y]z [x.y]z [x.y]z
189 24 [x][.y]z [x.y]z [x.y]z [x.y]z
191 25 filenames with '$' are left unchanged if they contain no '/'
192 25 filenames with ':' are left unchanged
193 26 filenames with a single pair of '[' ']' are left unchanged
195 the input string is not written to
198 char *
199 vmsify (char *name, int type)
201 /* max 255 device
202 max 39 directory
203 max 39 filename
204 max 39 filetype
205 max 5 version
207 #define MAXPATHLEN 512
209 enum namestate nstate;
210 static char vmsname[MAXPATHLEN+1];
211 char *fptr;
212 char *vptr;
213 char *s,*s1;
214 int as_dir;
215 int count;
217 if (name == 0)
218 return 0;
219 fptr = name;
220 vptr = vmsname;
221 nstate = N_START;
223 /* case 25a */
225 s = strpbrk (name, "$:");
226 if (s != 0)
228 char *s1;
229 char *s2;
231 if (type == 1)
233 s1 = strchr (s+1, '[');
234 s2 = strchr (s+1, ']');
237 if (*s == '$')
239 if (strchr (name, '/') == 0)
241 if ((type == 1) && (s1 != 0) && (s2 == 0))
243 strcpy (vmsname, name);
244 strcat (vmsname, "]");
245 return vmsname;
247 else
248 return name;
251 else
253 if ((type == 1) && (s1 != 0) && (s2 == 0))
255 strcpy (vmsname, name);
256 strcat (vmsname, "]");
257 return vmsname;
259 else
260 return name;
264 /* case 26 */
266 s = strchr (name, '[');
268 if (s != 0)
270 s1 = strchr (s+1, '[');
271 if (s1 == 0)
273 if ((type == 1)
274 && (strchr (s+1, ']') == 0))
276 strcpy (vmsname, name);
277 strcat (vmsname, "]");
278 return vmsname;
280 else
281 return name; /* single [, keep unchanged */
283 s1--;
284 if (*s1 != ']')
286 return name; /* not ][, keep unchanged */
289 /* we have ][ */
291 s = name;
293 /* s -> starting char
294 s1 -> ending ']' */
298 strncpy (vptr, s, s1-s); /* copy up to but not including ']' */
299 vptr += s1-s;
300 if (*s1 == 0)
301 break;
302 s = s1 + 1; /* s -> char behind ']' */
303 if (*s != '[') /* was '][' ? */
304 break; /* no, last ] found, exit */
305 s++;
306 if (*s != '.')
307 *vptr++ = '.';
308 s1 = strchr (s, ']');
309 if (s1 == 0) /* no closing ] */
310 s1 = s + strlen (s);
312 while (1);
314 *vptr++ = ']';
316 fptr = s;
320 else /* no [ in name */
324 int state;
325 int rooted = 1; /* flag if logical is rooted, else insert [000000] */
327 state = 0;
332 switch (state)
334 case 0: /* start of loop */
335 if (*fptr == '/')
337 fptr++;
338 state = 1;
340 else if (*fptr == '.')
342 fptr++;
343 state = 10;
345 else
346 state = 2;
347 break;
349 case 1: /* '/' at start */
350 if (*fptr == '/')
352 fptr++;
353 state = 3;
355 else
356 state = 4;
357 break;
359 case 2: /* no '/' at start */
360 s = strchr (fptr, '/');
361 if (s == 0) /* no '/' (16) */
363 if (type == 1)
365 strcpy (vptr, "[.");
366 vptr += 2;
368 copyto (&vptr, &fptr, 0, (type==1));
369 if (type == 1)
370 *vptr++ = ']';
371 state = -1;
373 else /* found '/' (17..21) */
375 if ((type == 2)
376 && (*(s+1) == 0)) /* 17(2) */
378 copyto (&vptr, &fptr, '/', 1);
379 state = 7;
381 else
383 strcpy (vptr, "[.");
384 vptr += 2;
385 copyto (&vptr, &fptr, '/', 1);
386 nstate = N_OPEN;
387 state = 9;
390 break;
392 case 3: /* '//' at start */
393 while (*fptr == '/') /* collapse all '/' */
394 fptr++;
395 if (*fptr == 0) /* just // */
397 char cwdbuf[MAXPATHLEN+1];
399 s1 = getcwd(cwdbuf, MAXPATHLEN);
400 if (s1 == 0)
402 return ""; /* FIXME, err getcwd */
404 s = strchr (s1, ':');
405 if (s == 0)
407 return ""; /* FIXME, err no device */
409 strncpy (vptr, s1, s-s1+1);
410 vptr += s-s1+1;
411 state = -1;
412 break;
415 s = vptr;
417 if (copyto (&vptr, &fptr, '/', 1) == 0) /* copy device part */
419 *vptr++ = ':';
420 state = -1;
421 break;
423 *vptr = ':';
424 nstate = N_DEVICE;
425 if (*fptr == 0) /* just '//a/' */
427 strcpy (vptr+1, "[000000]");
428 vptr += 9;
429 state = -1;
430 break;
432 *vptr = 0;
433 /* check logical for [000000] insertion */
434 s1 = trnlog (s);
435 if (*s1 != 0)
436 { /* found translation */
437 char *s2;
438 for (;;) /* loop over all nested logicals */
440 s2 = s1 + strlen (s1) - 1;
441 if (*s2 == ':') /* translation ends in ':' */
443 s2 = trnlog (s1);
444 free (s1);
445 if (*s2 == 0)
447 rooted = 0;
448 break;
450 s1 = s2;
451 continue; /* next iteration */
453 if (*s2 == ']') /* translation ends in ']' */
455 if (*(s2-1) == '.') /* ends in '.]' */
457 if (strncmp (fptr, "000000", 6) != 0)
458 rooted = 0;
460 else
462 strcpy (vmsname, s1);
463 s = strchr (vmsname, ']');
464 *s = '.';
465 nstate = N_DOT;
466 vptr = s;
469 break;
471 free (s1);
473 else
474 rooted = 0;
476 if (*vptr == 0)
478 nstate = N_DEVICE;
479 *vptr++ = ':';
481 else
482 vptr++;
484 if (rooted == 0)
486 strcpy (vptr, "[000000.");
487 vptr += 8;
488 s1 = vptr-1;
489 nstate = N_DOT;
491 else
492 s1 = 0;
494 /* s1-> '.' after 000000 or NULL */
496 s = strchr (fptr, '/');
497 if (s == 0)
498 { /* no next '/' */
499 if (*(vptr-1) == '.')
500 *(vptr-1) = ']';
501 else if (rooted == 0)
502 *vptr++ = ']';
503 copyto (&vptr, &fptr, 0, (type == 1));
504 state = -1;
505 break;
507 else
509 while (*(s+1) == '/') /* skip multiple '/' */
510 s++;
513 if ((rooted != 0)
514 && (*(vptr-1) != '.'))
516 *vptr++ = '[';
517 nstate = N_DOT;
519 else
520 if ((nstate == N_DOT)
521 && (s1 != 0)
522 && (*(s+1) == 0))
524 if (type == 2)
526 *s1 = ']';
527 nstate = N_CLOSED;
530 state = 9;
531 break;
533 case 4: /* single '/' at start (9..15) */
534 if (*fptr == 0)
535 state = 5;
536 else
537 state = 6;
538 break;
540 case 5: /* just '/' at start (9) */
541 if (type != 2)
543 *vptr++ = '[';
544 nstate = N_OPEN;
546 strcpy (vptr, "000000");
547 vptr += 6;
548 if (type == 2)
549 state = 7;
550 else
551 state = 8;
552 break;
554 case 6: /* chars following '/' at start 10..15 */
555 *vptr++ = '[';
556 nstate = N_OPEN;
557 s = strchr (fptr, '/');
558 if (s == 0) /* 10 */
560 if (type != 1)
562 strcpy (vptr, "000000]");
563 vptr += 7;
565 copyto (&vptr, &fptr, 0, (type == 1));
566 if (type == 1)
568 *vptr++ = ']';
570 state = -1;
572 else /* 11..15 */
574 if ( (type == 2)
575 && (*(s+1) == 0)) /* 11(2) */
577 strcpy (vptr, "000000]");
578 nstate = N_CLOSED;
579 vptr += 7;
581 copyto (&vptr, &fptr, '/', (*(vptr-1) != ']'));
582 state = 9;
584 break;
586 case 7: /* add '.dir' and exit */
587 if ((nstate == N_OPEN)
588 || (nstate == N_DOT))
590 s = vptr-1;
591 while (s > vmsname)
593 if (*s == ']')
595 break;
597 if (*s == '.')
599 *s = ']';
600 break;
602 s--;
605 strcpy (vptr, ".dir");
606 vptr += 4;
607 state = -1;
608 break;
610 case 8: /* add ']' and exit */
611 *vptr++ = ']';
612 state = -1;
613 break;
615 case 9: /* 17..21, fptr -> 1st '/' + 1 */
616 if (*fptr == 0)
618 if (type == 2)
620 state = 7;
622 else
623 state = 8;
624 break;
626 s = strchr (fptr, '/');
627 if (s == 0)
629 if (type != 1)
631 if (nstate == N_OPEN)
633 *vptr++ = ']';
634 nstate = N_CLOSED;
636 as_dir = 0;
638 else
640 if (nstate == N_OPEN)
642 *vptr++ = '.';
643 nstate = N_DOT;
645 as_dir = 1;
648 else
650 while (*(s+1) == '/')
651 s++;
652 if ( (type == 2)
653 && (*(s+1) == 0)) /* 19(2), 21(2)*/
655 if (nstate != N_CLOSED)
657 *vptr++ = ']';
658 nstate = N_CLOSED;
660 as_dir = 1;
662 else
664 if (nstate == N_OPEN)
666 *vptr++ = '.';
667 nstate = N_DOT;
669 as_dir = 1;
672 if ( (*fptr == '.') /* check for '..' or '../' */
673 && (*(fptr+1) == '.')
674 && ((*(fptr+2) == '/')
675 || (*(fptr+2) == 0)) )
677 fptr += 2;
678 if (*fptr == '/')
682 fptr++;
684 while (*fptr == '/');
686 else if (*fptr == 0)
687 type = 1;
688 vptr--; /* vptr -> '.' or ']' */
689 s1 = vptr;
690 for (;;)
692 s1--;
693 if (*s1 == '.') /* one back */
695 vptr = s1;
696 nstate = N_OPEN;
697 break;
699 if (*s1 == '[') /* top level reached */
701 if (*fptr == 0)
703 strcpy (s1, "[000000]");
704 vptr = s1 + 8;
705 nstate = N_CLOSED;
706 s = 0;
707 break;
709 else
711 vptr = s1+1;
712 nstate = N_OPEN;
713 break;
718 else
720 copyto (&vptr, &fptr, '/', as_dir);
721 if (nstate == N_DOT)
722 nstate = N_OPEN;
724 if (s == 0)
725 { /* 18,20 */
726 if (type == 1)
727 *vptr++ = ']';
728 state = -1;
730 else
732 if (*(s+1) == 0)
734 if (type == 2) /* 19,21 */
736 state = 7;
738 else
740 *vptr++ = ']';
741 state = -1;
745 break;
747 case 10: /* 1,2 first is '.' */
748 if (*fptr == '.')
750 fptr++;
751 state = 11;
753 else
754 state = 12;
755 break;
757 case 11: /* 2, '..' at start */
758 count = 1;
759 if (*fptr != 0)
761 if (*fptr != '/') /* got ..xxx */
763 return name;
765 do /* got ../ */
767 fptr++;
768 while (*fptr == '/') fptr++;
769 if (*fptr != '.')
770 break;
771 if (*(fptr+1) != '.')
772 break;
773 fptr += 2;
774 if ((*fptr == 0)
775 || (*fptr == '/'))
776 count++;
778 while (*fptr == '/');
780 { /* got '..' or '../' */
781 char cwdbuf[MAXPATHLEN+1];
783 s1 = getcwd(cwdbuf, MAXPATHLEN);
784 if (s1 == 0)
786 return ""; /* FIXME, err getcwd */
788 strcpy (vptr, s1);
789 s = strchr (vptr, ']');
790 if (s != 0)
792 nstate = N_OPEN;
793 while (s > vptr)
795 s--;
796 if (*s == '[')
798 s++;
799 strcpy (s, "000000]");
800 state = -1;
801 break;
803 else if (*s == '.')
805 if (--count == 0)
807 if (*fptr == 0) /* had '..' or '../' */
809 *s++ = ']';
810 state = -1;
812 else /* had '../xxx' */
814 state = 9;
816 *s = 0;
817 break;
822 vptr += strlen (vptr);
824 break;
826 case 12: /* 1, '.' at start */
827 if (*fptr != 0)
829 if (*fptr != '/')
831 return name;
833 while (*fptr == '/')
834 fptr++;
838 char cwdbuf[MAXPATHLEN+1];
840 s1 = getcwd(cwdbuf, MAXPATHLEN);
841 if (s1 == 0)
843 return ""; /*FIXME, err getcwd */
845 strcpy (vptr, s1);
846 if (*fptr == 0)
848 state = -1;
849 break;
851 else
853 s = strchr (vptr, ']');
854 if (s == 0)
856 state = -1;
857 break;
859 *s = 0;
860 nstate = N_OPEN;
861 vptr += strlen (vptr);
862 state = 9;
865 break;
869 while (state > 0);
875 /* directory conversion done
876 fptr -> filename part of input string
877 vptr -> free space in vmsname
880 *vptr++ = 0;
882 return vmsname;
888 convert from vms-style to unix-style
890 dev:[dir1.dir2] //dev/dir1/dir2/
893 char *
894 unixify (char *name)
896 static char piece[512];
897 char *s, *p;
899 if (strchr (name, '/') != 0) /* already in unix style */
900 return name;
902 p = piece;
903 *p = 0;
905 /* device part */
907 s = strchr (name, ':');
909 if (s != 0)
911 *s = 0;
912 *p++ = '/';
913 *p++ = '/';
914 strcpy (p, name);
915 p += strlen (p);
916 *s = ':';
919 /* directory part */
921 *p++ = '/';
922 s = strchr (name, '[');
924 if (s != 0)
926 s++;
927 switch (*s)
929 case ']': /* [] */
930 strcat (p, "./");
931 break;
932 case '-': /* [- */
933 strcat (p, "../");
934 break;
935 case '.':
936 strcat (p, "./"); /* [. */
937 break;
938 default:
939 s--;
940 break;
942 s++;
943 while (*s)
945 if (*s == '.')
946 *p++ = '/';
947 else
948 *p++ = *s;
949 s++;
950 if (*s == ']')
952 s++;
953 break;
956 if (*s != 0) /* more after ']' ?? */
958 if (*(p-1) != '/')
959 *p++ = '/';
960 strcpy (p, s); /* copy it anyway */
964 else /* no '[' anywhere */
967 *p++ = 0;
970 /* force end with '/' */
972 if (*(p-1) != '/')
973 *p++ = '/';
974 *p = 0;
976 return piece;
979 /* EOF */