forked from Perl/perl5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvms.c
14100 lines (12547 loc) · 405 KB
/
vms.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vms.c
*
* VMS-specific routines for perl5
*
* Copyright (C) 1993-2015 by Charles Bailey and others.
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*/
/*
* Yet small as was their hunted band
* still fell and fearless was each hand,
* and strong deeds they wrought yet oft,
* and loved the woods, whose ways more soft
* them seemed than thralls of that black throne
* to live and languish in halls of stone.
* "The Lay of Leithian", Canto II, lines 135-40
*
* [p.162 of _The Lays of Beleriand_]
*/
#include <acedef.h>
#include <acldef.h>
#include <armdef.h>
#include <chpdef.h>
#include <clidef.h>
#include <climsgdef.h>
#include <dcdef.h>
#include <descrip.h>
#include <devdef.h>
#include <dvidef.h>
#include <float.h>
#include <fscndef.h>
#include <iodef.h>
#include <jpidef.h>
#include <kgbdef.h>
#include <libclidef.h>
#include <libdef.h>
#include <lib$routines.h>
#include <lnmdef.h>
#include <ossdef.h>
#include <ppropdef.h>
#include <prvdef.h>
#include <pscandef.h>
#include <psldef.h>
#include <rms.h>
#include <shrdef.h>
#include <ssdef.h>
#include <starlet.h>
#include <strdef.h>
#include <str$routines.h>
#include <syidef.h>
#include <uaidef.h>
#include <uicdef.h>
#include <stsdef.h>
#include <efndef.h>
#define NO_EFN EFN$C_ENF
#include <unixlib.h>
#pragma member_alignment save
#pragma nomember_alignment longword
struct item_list_3 {
unsigned short len;
unsigned short code;
void * bufadr;
unsigned short * retadr;
};
#pragma member_alignment restore
/* Older versions of ssdef.h don't have these */
#ifndef SS$_INVFILFOROP
# define SS$_INVFILFOROP 3930
#endif
#ifndef SS$_NOSUCHOBJECT
# define SS$_NOSUCHOBJECT 2696
#endif
/* We implement I/O here, so we will be mixing PerlIO and stdio calls. */
#define PERLIO_NOT_STDIO 0
/* Don't replace system definitions of vfork, getenv, lstat, and stat,
* code below needs to get to the underlying CRTL routines. */
#define DONT_MASK_RTL_CALLS
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
/* Anticipating future expansion in lexical warnings . . . */
#ifndef WARN_INTERNAL
# define WARN_INTERNAL WARN_MISC
#endif
#ifdef VMS_LONGNAME_SUPPORT
#include <libfildef.h>
#endif
#if __CRTL_VER >= 80200000
#ifdef lstat
#undef lstat
#endif
#else
#ifdef lstat
#undef lstat
#endif
#define lstat(_x, _y) stat(_x, _y)
#endif
/* Routine to create a decterm for use with the Perl debugger */
/* No headers, this information was found in the Programming Concepts Manual */
static int (*decw_term_port)
(const struct dsc$descriptor_s * display,
const struct dsc$descriptor_s * setup_file,
const struct dsc$descriptor_s * customization,
struct dsc$descriptor_s * result_device_name,
unsigned short * result_device_name_length,
void * controller,
void * char_buffer,
void * char_change_buffer) = 0;
#if defined(NEED_AN_H_ERRNO)
dEXT int h_errno;
#endif
#if defined(__DECC) || defined(__DECCXX)
#pragma member_alignment save
#pragma nomember_alignment longword
#pragma message save
#pragma message disable misalgndmem
#endif
struct itmlst_3 {
unsigned short int buflen;
unsigned short int itmcode;
void *bufadr;
unsigned short int *retlen;
};
struct filescan_itmlst_2 {
unsigned short length;
unsigned short itmcode;
char * component;
};
struct vs_str_st {
unsigned short length;
char str[VMS_MAXRSS];
unsigned short pad; /* for longword struct alignment */
};
#if defined(__DECC) || defined(__DECCXX)
#pragma message restore
#pragma member_alignment restore
#endif
#define do_fileify_dirspec(a,b,c,d) mp_do_fileify_dirspec(aTHX_ a,b,c,d)
#define do_pathify_dirspec(a,b,c,d) mp_do_pathify_dirspec(aTHX_ a,b,c,d)
#define do_tovmsspec(a,b,c,d) mp_do_tovmsspec(aTHX_ a,b,c,0,d)
#define do_tovmspath(a,b,c,d) mp_do_tovmspath(aTHX_ a,b,c,d)
#define do_rmsexpand(a,b,c,d,e,f,g) mp_do_rmsexpand(aTHX_ a,b,c,d,e,f,g)
#define do_vms_realpath(a,b,c) mp_do_vms_realpath(aTHX_ a,b,c)
#define do_vms_realname(a,b,c) mp_do_vms_realname(aTHX_ a,b,c)
#define do_tounixspec(a,b,c,d) mp_do_tounixspec(aTHX_ a,b,c,d)
#define do_tounixpath(a,b,c,d) mp_do_tounixpath(aTHX_ a,b,c,d)
#define do_vms_case_tolerant(a) mp_do_vms_case_tolerant(a)
#define expand_wild_cards(a,b,c,d) mp_expand_wild_cards(aTHX_ a,b,c,d)
#define getredirection(a,b) mp_getredirection(aTHX_ a,b)
static char *mp_do_tovmspath(pTHX_ const char *path, char *buf, int ts, int *);
static char *mp_do_tounixpath(pTHX_ const char *path, char *buf, int ts, int *);
static char *mp_do_tounixspec(pTHX_ const char *, char *, int, int *);
static char *mp_do_pathify_dirspec(pTHX_ const char *dir,char *buf, int ts, int *);
static char * int_rmsexpand_vms(
const char * filespec, char * outbuf, unsigned opts);
static char * int_rmsexpand_tovms(
const char * filespec, char * outbuf, unsigned opts);
static char *int_tovmsspec
(const char *path, char *buf, int dir_flag, int * utf8_flag);
static char * int_fileify_dirspec(const char *dir, char *buf, int *utf8_fl);
static char * int_tounixspec(const char *spec, char *buf, int * utf8_fl);
static char * int_tovmspath(const char *path, char *buf, int * utf8_fl);
/* see system service docs for $TRNLNM -- NOT the same as LNM$_MAX_INDEX */
#define PERL_LNM_MAX_ALLOWED_INDEX 127
/* OpenVMS User's Guide says at least 9 iterative translations will be performed,
* depending on the facility. SHOW LOGICAL does 10, so we'll imitate that for
* the Perl facility.
*/
#define PERL_LNM_MAX_ITER 10
/* New values with 7.3-2*/ /* why does max DCL have 4 byte subtracted from it? */
#define MAX_DCL_SYMBOL (8192)
#define MAX_DCL_LINE_LENGTH (4096 - 4)
static char *__mystrtolower(char *str)
{
if (str) for (; *str; ++str) *str= toLOWER_L1(*str);
return str;
}
static struct dsc$descriptor_s fildevdsc =
{ 12, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$FILE_DEV" };
static struct dsc$descriptor_s crtlenvdsc =
{ 8, DSC$K_DTYPE_T, DSC$K_CLASS_S, "CRTL_ENV" };
static struct dsc$descriptor_s *fildev[] = { &fildevdsc, NULL };
static struct dsc$descriptor_s *defenv[] = { &fildevdsc, &crtlenvdsc, NULL };
static struct dsc$descriptor_s **env_tables = defenv;
static bool will_taint = FALSE; /* tainting active, but no PL_curinterp yet */
/* True if we shouldn't treat barewords as logicals during directory */
/* munching */
static int no_translate_barewords;
/* DECC feature indexes. We grab the indexes at start-up
* time for later use with decc$feature_get_value.
*/
static int disable_to_vms_logname_translation_index = -1;
static int disable_posix_root_index = -1;
static int efs_case_preserve_index = -1;
static int efs_charset_index = -1;
static int filename_unix_no_version_index = -1;
static int filename_unix_only_index = -1;
static int filename_unix_report_index = -1;
static int posix_compliant_pathnames_index = -1;
static int readdir_dropdotnotype_index = -1;
#define DECC_DISABLE_TO_VMS_LOGNAME_TRANSLATION \
(decc$feature_get_value(disable_to_vms_logname_translation_index,__FEATURE_MODE_CURVAL)>0)
#define DECC_DISABLE_POSIX_ROOT \
(decc$feature_get_value(disable_posix_root_index,__FEATURE_MODE_CURVAL)>0)
#define DECC_EFS_CASE_PRESERVE \
(decc$feature_get_value(efs_case_preserve_index,__FEATURE_MODE_CURVAL)>0)
#define DECC_EFS_CHARSET \
(decc$feature_get_value(efs_charset_index,__FEATURE_MODE_CURVAL)>0)
#define DECC_FILENAME_UNIX_NO_VERSION \
(decc$feature_get_value(filename_unix_no_version_index,__FEATURE_MODE_CURVAL)>0)
#define DECC_FILENAME_UNIX_ONLY \
(decc$feature_get_value(filename_unix_only_index,__FEATURE_MODE_CURVAL)>0)
#define DECC_FILENAME_UNIX_REPORT \
(decc$feature_get_value(filename_unix_report_index,__FEATURE_MODE_CURVAL)>0)
#define DECC_POSIX_COMPLIANT_PATHNAMES \
(decc$feature_get_value(posix_compliant_pathnames_index,__FEATURE_MODE_CURVAL)>0)
#define DECC_READDIR_DROPDOTNOTYPE \
(decc$feature_get_value(readdir_dropdotnotype_index,__FEATURE_MODE_CURVAL)>0)
static int vms_process_case_tolerant = 1;
int vms_vtf7_filenames = 0;
int gnv_unix_shell = 0;
static int vms_unlink_all_versions = 0;
static int vms_posix_exit = 0;
/* bug workarounds if needed */
int decc_bug_devnull = 1;
int vms_bug_stat_filename = 0;
static int vms_debug_on_exception = 0;
static int vms_debug_fileify = 0;
/* Simple logical name translation */
static int
simple_trnlnm(const char * logname, char * value, int value_len)
{
const $DESCRIPTOR(table_dsc, "LNM$FILE_DEV");
const unsigned long attr = LNM$M_CASE_BLIND;
struct dsc$descriptor_s name_dsc;
int status;
unsigned short result;
struct itmlst_3 itlst[2] = {{value_len, LNM$_STRING, value, &result},
{0, 0, 0, 0}};
name_dsc.dsc$w_length = strlen(logname);
name_dsc.dsc$a_pointer = (char *)logname;
name_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
name_dsc.dsc$b_class = DSC$K_CLASS_S;
status = sys$trnlnm(&attr, &table_dsc, &name_dsc, 0, itlst);
if ($VMS_STATUS_SUCCESS(status)) {
/* Null terminate and return the string */
/*--------------------------------------*/
value[result] = 0;
return result;
}
return 0;
}
/* Is this a UNIX file specification?
* No longer a simple check with EFS file specs
* For now, not a full check, but need to
* handle POSIX ^UP^ specifications
* Fixing to handle ^/ cases would require
* changes to many other conversion routines.
*/
static int
is_unix_filespec(const char *path)
{
int ret_val;
const char * pch1;
ret_val = 0;
if (! strBEGINs(path,"\"^UP^")) {
pch1 = strchr(path, '/');
if (pch1 != NULL)
ret_val = 1;
else {
/* If the user wants UNIX files, "." needs to be treated as in UNIX */
if (DECC_FILENAME_UNIX_REPORT || DECC_FILENAME_UNIX_ONLY) {
if (strEQ(path,"."))
ret_val = 1;
}
}
}
return ret_val;
}
/* This routine converts a UCS-2 character to be VTF-7 encoded.
*/
static void
ucs2_to_vtf7(char *outspec, unsigned long ucs2_char, int * output_cnt)
{
unsigned char * ucs_ptr;
int hex;
ucs_ptr = (unsigned char *)&ucs2_char;
outspec[0] = '^';
outspec[1] = 'U';
hex = (ucs_ptr[1] >> 4) & 0xf;
if (hex < 0xA)
outspec[2] = hex + '0';
else
outspec[2] = (hex - 9) + 'A';
hex = ucs_ptr[1] & 0xF;
if (hex < 0xA)
outspec[3] = hex + '0';
else {
outspec[3] = (hex - 9) + 'A';
}
hex = (ucs_ptr[0] >> 4) & 0xf;
if (hex < 0xA)
outspec[4] = hex + '0';
else
outspec[4] = (hex - 9) + 'A';
hex = ucs_ptr[1] & 0xF;
if (hex < 0xA)
outspec[5] = hex + '0';
else {
outspec[5] = (hex - 9) + 'A';
}
*output_cnt = 6;
}
/* This handles the conversion of a UNIX extended character set to a ^
* escaped VMS character.
* in a UNIX file specification.
*
* The output count variable contains the number of characters added
* to the output string.
*
* The return value is the number of characters read from the input string
*/
static int
copy_expand_unix_filename_escape(char *outspec, const char *inspec, int *output_cnt, const int * utf8_fl)
{
int count;
int utf8_flag;
utf8_flag = 0;
if (utf8_fl)
utf8_flag = *utf8_fl;
count = 0;
*output_cnt = 0;
if (*inspec >= 0x80) {
if (utf8_fl && vms_vtf7_filenames) {
unsigned long ucs_char;
ucs_char = 0;
if ((*inspec & 0xE0) == 0xC0) {
/* 2 byte Unicode */
ucs_char = ((inspec[0] & 0x1F) << 6) + (inspec[1] & 0x3f);
if (ucs_char >= 0x80) {
ucs2_to_vtf7(outspec, ucs_char, output_cnt);
return 2;
}
} else if ((*inspec & 0xF0) == 0xE0) {
/* 3 byte Unicode */
ucs_char = ((inspec[0] & 0xF) << 12) +
((inspec[1] & 0x3f) << 6) +
(inspec[2] & 0x3f);
if (ucs_char >= 0x800) {
ucs2_to_vtf7(outspec, ucs_char, output_cnt);
return 3;
}
#if 0 /* I do not see longer sequences supported by OpenVMS */
/* Maybe some one can fix this later */
} else if ((*inspec & 0xF8) == 0xF0) {
/* 4 byte Unicode */
/* UCS-4 to UCS-2 */
} else if ((*inspec & 0xFC) == 0xF8) {
/* 5 byte Unicode */
/* UCS-4 to UCS-2 */
} else if ((*inspec & 0xFE) == 0xFC) {
/* 6 byte Unicode */
/* UCS-4 to UCS-2 */
#endif
}
}
/* High bit set, but not a Unicode character! */
/* Non printing DECMCS or ISO Latin-1 character? */
if ((unsigned char)*inspec <= 0x9F) {
int hex;
outspec[0] = '^';
outspec++;
hex = (*inspec >> 4) & 0xF;
if (hex < 0xA)
outspec[1] = hex + '0';
else {
outspec[1] = (hex - 9) + 'A';
}
hex = *inspec & 0xF;
if (hex < 0xA)
outspec[2] = hex + '0';
else {
outspec[2] = (hex - 9) + 'A';
}
*output_cnt = 3;
return 1;
} else if ((unsigned char)*inspec == 0xA0) {
outspec[0] = '^';
outspec[1] = 'A';
outspec[2] = '0';
*output_cnt = 3;
return 1;
} else if ((unsigned char)*inspec == 0xFF) {
outspec[0] = '^';
outspec[1] = 'F';
outspec[2] = 'F';
*output_cnt = 3;
return 1;
}
*outspec = *inspec;
*output_cnt = 1;
return 1;
}
/* Is this a macro that needs to be passed through?
* Macros start with $( and an alpha character, followed
* by a string of alpha numeric characters ending with a )
* If this does not match, then encode it as ODS-5.
*/
if ((inspec[0] == '$') && (inspec[1] == '(')) {
int tcnt;
if (isALPHA_L1(inspec[2]) || (inspec[2] == '.') || (inspec[2] == '_')) {
tcnt = 3;
outspec[0] = inspec[0];
outspec[1] = inspec[1];
outspec[2] = inspec[2];
while(isALPHA_L1(inspec[tcnt]) ||
(inspec[2] == '.') || (inspec[2] == '_')) {
outspec[tcnt] = inspec[tcnt];
tcnt++;
}
if (inspec[tcnt] == ')') {
outspec[tcnt] = inspec[tcnt];
tcnt++;
*output_cnt = tcnt;
return tcnt;
}
}
}
switch (*inspec) {
case 0x7f:
outspec[0] = '^';
outspec[1] = '7';
outspec[2] = 'F';
*output_cnt = 3;
return 1;
break;
case '?':
if (!DECC_EFS_CHARSET)
outspec[0] = '%';
else
outspec[0] = '?';
*output_cnt = 1;
return 1;
break;
case '.':
case '!':
case '#':
case '&':
case '\'':
case '`':
case '(':
case ')':
case '+':
case '@':
case '{':
case '}':
case ',':
case ';':
case '[':
case ']':
case '%':
case '^':
case '\\':
/* Don't escape again if following character is
* already something we escape.
*/
if (strchr(".!#&\'`()+@{},;[]%^=_\\", *(inspec+1))) {
*outspec = *inspec;
*output_cnt = 1;
return 1;
break;
}
/* But otherwise fall through and escape it. */
case '=':
/* Assume that this is to be escaped */
outspec[0] = '^';
outspec[1] = *inspec;
*output_cnt = 2;
return 1;
break;
case ' ': /* space */
/* Assume that this is to be escaped */
outspec[0] = '^';
outspec[1] = '_';
*output_cnt = 2;
return 1;
break;
default:
*outspec = *inspec;
*output_cnt = 1;
return 1;
break;
}
return 0;
}
/* This handles the expansion of a '^' prefix to the proper character
* in a UNIX file specification.
*
* The output count variable contains the number of characters added
* to the output string.
*
* The return value is the number of characters read from the input
* string
*/
static int
copy_expand_vms_filename_escape(char *outspec, const char *inspec, int *output_cnt)
{
int count;
int scnt;
count = 0;
*output_cnt = 0;
if (*inspec == '^') {
inspec++;
switch (*inspec) {
/* Spaces and non-trailing dots should just be passed through,
* but eat the escape character.
*/
case '.':
*outspec = *inspec;
count += 2;
(*output_cnt)++;
break;
case '_': /* space */
*outspec = ' ';
count += 2;
(*output_cnt)++;
break;
case '^':
/* Hmm. Better leave the escape escaped. */
outspec[0] = '^';
outspec[1] = '^';
count += 2;
(*output_cnt) += 2;
break;
case 'U': /* Unicode - FIX-ME this is wrong. */
inspec++;
count++;
scnt = strspn(inspec, "0123456789ABCDEFabcdef");
if (scnt == 4) {
unsigned int c1, c2;
scnt = sscanf(inspec, "%2x%2x", &c1, &c2);
outspec[0] = c1 & 0xff;
outspec[1] = c2 & 0xff;
if (scnt > 1) {
(*output_cnt) += 2;
count += 4;
}
}
else {
/* Error - do best we can to continue */
*outspec = 'U';
outspec++;
(*output_cnt++);
*outspec = *inspec;
count++;
(*output_cnt++);
}
break;
default:
scnt = strspn(inspec, "0123456789ABCDEFabcdef");
if (scnt == 2) {
/* Hex encoded */
unsigned int c1;
scnt = sscanf(inspec, "%2x", &c1);
outspec[0] = c1 & 0xff;
if (scnt > 0) {
(*output_cnt++);
count += 2;
}
}
else {
*outspec = *inspec;
count++;
(*output_cnt++);
}
}
}
else {
*outspec = *inspec;
count++;
(*output_cnt)++;
}
return count;
}
/* vms_split_path - Verify that the input file specification is a
* VMS format file specification, and provide pointers to the components of
* it. With EFS format filenames, this is virtually the only way to
* parse a VMS path specification into components.
*
* If the sum of the components do not add up to the length of the
* string, then the passed file specification is probably a UNIX style
* path.
*/
static int
vms_split_path(const char * path, char * * volume, int * vol_len, char * * root, int * root_len,
char * * dir, int * dir_len, char * * name, int * name_len,
char * * ext, int * ext_len, char * * version, int * ver_len)
{
struct dsc$descriptor path_desc;
int status;
unsigned long flags;
int ret_stat;
struct filescan_itmlst_2 item_list[9];
const int filespec = 0;
const int nodespec = 1;
const int devspec = 2;
const int rootspec = 3;
const int dirspec = 4;
const int namespec = 5;
const int typespec = 6;
const int verspec = 7;
/* Assume the worst for an easy exit */
ret_stat = -1;
*volume = NULL;
*vol_len = 0;
*root = NULL;
*root_len = 0;
*dir = NULL;
*name = NULL;
*name_len = 0;
*ext = NULL;
*ext_len = 0;
*version = NULL;
*ver_len = 0;
path_desc.dsc$a_pointer = (char *)path; /* cast ok */
path_desc.dsc$w_length = strlen(path);
path_desc.dsc$b_dtype = DSC$K_DTYPE_T;
path_desc.dsc$b_class = DSC$K_CLASS_S;
/* Get the total length, if it is shorter than the string passed
* then this was probably not a VMS formatted file specification
*/
item_list[filespec].itmcode = FSCN$_FILESPEC;
item_list[filespec].length = 0;
item_list[filespec].component = NULL;
/* If the node is present, then it gets considered as part of the
* volume name to hopefully make things simple.
*/
item_list[nodespec].itmcode = FSCN$_NODE;
item_list[nodespec].length = 0;
item_list[nodespec].component = NULL;
item_list[devspec].itmcode = FSCN$_DEVICE;
item_list[devspec].length = 0;
item_list[devspec].component = NULL;
/* root is a special case, adding it to either the directory or
* the device components will probably complicate things for the
* callers of this routine, so leave it separate.
*/
item_list[rootspec].itmcode = FSCN$_ROOT;
item_list[rootspec].length = 0;
item_list[rootspec].component = NULL;
item_list[dirspec].itmcode = FSCN$_DIRECTORY;
item_list[dirspec].length = 0;
item_list[dirspec].component = NULL;
item_list[namespec].itmcode = FSCN$_NAME;
item_list[namespec].length = 0;
item_list[namespec].component = NULL;
item_list[typespec].itmcode = FSCN$_TYPE;
item_list[typespec].length = 0;
item_list[typespec].component = NULL;
item_list[verspec].itmcode = FSCN$_VERSION;
item_list[verspec].length = 0;
item_list[verspec].component = NULL;
item_list[8].itmcode = 0;
item_list[8].length = 0;
item_list[8].component = NULL;
status = sys$filescan
((const struct dsc$descriptor_s *)&path_desc, item_list,
&flags, NULL, NULL);
_ckvmssts_noperl(status); /* All failure status values indicate a coding error */
/* If we parsed it successfully these two lengths should be the same */
if (path_desc.dsc$w_length != item_list[filespec].length)
return ret_stat;
/* If we got here, then it is a VMS file specification */
ret_stat = 0;
/* set the volume name */
if (item_list[nodespec].length > 0) {
*volume = item_list[nodespec].component;
*vol_len = item_list[nodespec].length + item_list[devspec].length;
}
else {
*volume = item_list[devspec].component;
*vol_len = item_list[devspec].length;
}
*root = item_list[rootspec].component;
*root_len = item_list[rootspec].length;
*dir = item_list[dirspec].component;
*dir_len = item_list[dirspec].length;
/* Now fun with versions and EFS file specifications
* The parser can not tell the difference when a "." is a version
* delimiter or a part of the file specification.
*/
if ((DECC_EFS_CHARSET) &&
(item_list[verspec].length > 0) &&
(item_list[verspec].component[0] == '.')) {
*name = item_list[namespec].component;
*name_len = item_list[namespec].length + item_list[typespec].length;
*ext = item_list[verspec].component;
*ext_len = item_list[verspec].length;
*version = NULL;
*ver_len = 0;
}
else {
*name = item_list[namespec].component;
*name_len = item_list[namespec].length;
*ext = item_list[typespec].component;
*ext_len = item_list[typespec].length;
*version = item_list[verspec].component;
*ver_len = item_list[verspec].length;
}
return ret_stat;
}
/* Routine to determine if the file specification ends with .dir */
static int
is_dir_ext(char * e_spec, int e_len, char * vs_spec, int vs_len)
{
/* e_len must be 4, and version must be <= 2 characters */
if (e_len != 4 || vs_len > 2)
return 0;
/* If a version number is present, it needs to be one */
if ((vs_len == 2) && (vs_spec[1] != '1'))
return 0;
/* Look for the DIR on the extension */
if (vms_process_case_tolerant) {
if ((toUPPER_A(e_spec[1]) == 'D') &&
(toUPPER_A(e_spec[2]) == 'I') &&
(toUPPER_A(e_spec[3]) == 'R')) {
return 1;
}
} else {
/* Directory extensions are supposed to be in upper case only */
/* I would not be surprised if this rule can not be enforced */
/* if and when someone fully debugs the case sensitive mode */
if ((e_spec[1] == 'D') &&
(e_spec[2] == 'I') &&
(e_spec[3] == 'R')) {
return 1;
}
}
return 0;
}
/* my_maxidx
* Routine to retrieve the maximum equivalence index for an input
* logical name. Some calls to this routine have no knowledge if
* the variable is a logical or not. So on error we return a max
* index of zero.
*/
/*{{{int my_maxidx(const char *lnm) */
static int
my_maxidx(const char *lnm)
{
int status;
int midx;
int attr = LNM$M_CASE_BLIND;
struct dsc$descriptor lnmdsc;
struct itmlst_3 itlst[2] = {{sizeof(midx), LNM$_MAX_INDEX, &midx, 0},
{0, 0, 0, 0}};
lnmdsc.dsc$w_length = strlen(lnm);
lnmdsc.dsc$b_dtype = DSC$K_DTYPE_T;
lnmdsc.dsc$b_class = DSC$K_CLASS_S;
lnmdsc.dsc$a_pointer = (char *) lnm; /* Cast ok for read only parameter */
status = sys$trnlnm(&attr, &fildevdsc, &lnmdsc, 0, itlst);
if ((status & 1) == 0)
midx = 0;
return (midx);
}
/*}}}*/
/* Routine to remove the 2-byte prefix from the translation of a
* process-permanent file (PPF).
*/
static inline unsigned short int
S_remove_ppf_prefix(const char *lnm, char *eqv, unsigned short int eqvlen)
{
if (*((int *)lnm) == *((int *)"SYS$") &&
eqvlen >= 4 && eqv[0] == 0x1b && eqv[1] == 0x00 &&
( (lnm[4] == 'O' && strEQ(lnm,"SYS$OUTPUT")) ||
(lnm[4] == 'I' && strEQ(lnm,"SYS$INPUT")) ||
(lnm[4] == 'E' && strEQ(lnm,"SYS$ERROR")) ||
(lnm[4] == 'C' && strEQ(lnm,"SYS$COMMAND")) ) ) {
memmove(eqv, eqv+4, eqvlen-4);
eqvlen -= 4;
}
return eqvlen;
}
/*{{{int vmstrnenv(const char *lnm, char *eqv, unsigned long int idx, struct dsc$descriptor_s **tabvec, unsigned long int flags) */
int
Perl_vmstrnenv(const char *lnm, char *eqv, unsigned long int idx,
struct dsc$descriptor_s **tabvec, unsigned long int flags)
{
const char *cp1;
char uplnm[LNM$C_NAMLENGTH+1], *cp2;
unsigned short int eqvlen, curtab, ivlnm = 0, ivsym = 0, ivenv = 0, secure;
bool found_in_crtlenv = 0, found_in_clisym = 0;
unsigned long int retsts, attr = LNM$M_CASE_BLIND;
int midx;
unsigned char acmode;
struct dsc$descriptor_s lnmdsc = {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0},
tmpdsc = {6,DSC$K_DTYPE_T,DSC$K_CLASS_S,0};
struct itmlst_3 lnmlst[3] = {{sizeof idx, LNM$_INDEX, &idx, 0},
{LNM$C_NAMLENGTH, LNM$_STRING, eqv, &eqvlen},
{0, 0, 0, 0}};
$DESCRIPTOR(crtlenv,"CRTL_ENV"); $DESCRIPTOR(clisym,"CLISYM");
#if defined(PERL_IMPLICIT_CONTEXT)
pTHX = NULL;
if (PL_curinterp) {
aTHX = PERL_GET_INTERP;
} else {
aTHX = NULL;
}
#endif
if (!lnm || !eqv || ((idx != 0) && ((idx-1) > PERL_LNM_MAX_ALLOWED_INDEX))) {
set_errno(EINVAL); set_vaxc_errno(SS$_BADPARAM); return 0;
}
for (cp1 = lnm, cp2 = uplnm; *cp1; cp1++, cp2++) {
*cp2 = toUPPER_A(*cp1);
if (cp1 - lnm > LNM$C_NAMLENGTH) {
set_errno(EINVAL); set_vaxc_errno(SS$_IVLOGNAM);
return 0;
}
}
lnmdsc.dsc$w_length = cp1 - lnm;
lnmdsc.dsc$a_pointer = uplnm;
uplnm[lnmdsc.dsc$w_length] = '\0';
secure = flags & PERL__TRNENV_SECURE;
acmode = secure ? PSL$C_EXEC : PSL$C_USER;
if (!tabvec || !*tabvec) tabvec = env_tables;
for (curtab = 0; tabvec[curtab]; curtab++) {
if (!str$case_blind_compare(tabvec[curtab],&crtlenv)) {
if (!ivenv && !secure) {
char *eq;
int i;
if (!environ) {
ivenv = 1;
#if defined(PERL_IMPLICIT_CONTEXT)
if (aTHX == NULL) {
fprintf(stderr,
"Can't read CRTL environ\n");
} else
#endif
Perl_warn(aTHX_ "Can't read CRTL environ\n");
continue;
}
retsts = SS$_NOLOGNAM;
for (i = 0; environ[i]; i++) {
if ((eq = strchr(environ[i],'=')) &&
lnmdsc.dsc$w_length == (eq - environ[i]) &&
strnEQ(environ[i],lnm,eq - environ[i])) {
eq++;
for (eqvlen = 0; eq[eqvlen]; eqvlen++) eqv[eqvlen] = eq[eqvlen];
if (!eqvlen) continue;
retsts = SS$_NORMAL;
break;
}
}
if (retsts != SS$_NOLOGNAM) {
found_in_crtlenv = 1;
break;
}
}
}
else if ((tmpdsc.dsc$a_pointer = tabvec[curtab]->dsc$a_pointer) &&
!str$case_blind_compare(&tmpdsc,&clisym)) {
if (!ivsym && !secure) {
unsigned short int deflen = LNM$C_NAMLENGTH;
struct dsc$descriptor_d eqvdsc = {0,DSC$K_DTYPE_T,DSC$K_CLASS_D,0};
/* dynamic dsc to accommodate possible long value */
_ckvmssts_noperl(lib$sget1_dd(&deflen,&eqvdsc));
retsts = lib$get_symbol(&lnmdsc,&eqvdsc,&eqvlen,0);
if (retsts & 1) {
if (eqvlen > MAX_DCL_SYMBOL) {
set_errno(EVMSERR); set_vaxc_errno(LIB$_STRTRU);
eqvlen = MAX_DCL_SYMBOL;
/* Special hack--we might be called before the interpreter's */
/* fully initialized, in which case either thr or PL_curcop */
/* might be bogus. We have to check, since ckWARN needs them */
/* both to be valid if running threaded */
#if defined(PERL_IMPLICIT_CONTEXT)
if (aTHX == NULL) {
fprintf(stderr,
"Value of CLI symbol \"%s\" too long",lnm);
} else
#endif
if (ckWARN(WARN_MISC)) {
Perl_warner(aTHX_ packWARN(WARN_MISC),"Value of CLI symbol \"%s\" too long",lnm);
}
}
strncpy(eqv,eqvdsc.dsc$a_pointer,eqvlen);
}
_ckvmssts_noperl(lib$sfree1_dd(&eqvdsc));
if (retsts == LIB$_INVSYMNAM) { ivsym = 1; continue; }
if (retsts == LIB$_NOSUCHSYM) continue;
found_in_clisym = 1;
break;
}
}
else if (!ivlnm) {
if ( (idx == 0) && (flags & PERL__TRNENV_JOIN_SEARCHLIST) ) {
midx = my_maxidx(lnm);
for (idx = 0, cp2 = eqv; idx <= midx; idx++) {
lnmlst[1].bufadr = cp2;
eqvlen = 0;
retsts = sys$trnlnm(&attr,tabvec[curtab],&lnmdsc,&acmode,lnmlst);
if (retsts == SS$_IVLOGNAM) { ivlnm = 1; break; }
if (retsts == SS$_NOLOGNAM) break;
eqvlen = S_remove_ppf_prefix(uplnm, eqv, eqvlen);
cp2 += eqvlen;