1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24 #include "tool_setup.h"
25
26 #ifdef __VMS
27
28 #if defined(__DECC) && !defined(__VAX) && \
29 defined(__CRTL_VER) && (__CRTL_VER >= 70301000)
30 #include <unixlib.h>
31 #endif
32
33 #include "curlx.h"
34
35 #include "curlmsg_vms.h"
36 #include "tool_vms.h"
37
38 #include "memdebug.h" /* keep this as LAST include */
39
40 void decc$__posix_exit(int __status);
41 void decc$exit(int __status);
42
43 static int vms_shell = -1;
44
45 /* VMS has a DCL shell and also has Unix shells ported to it.
46 * When curl is running under a Unix shell, we want it to be as much
47 * like Unix as possible.
48 */
is_vms_shell(void)49 int is_vms_shell(void)
50 {
51 char *shell;
52
53 /* Have we checked the shell yet? */
54 if(vms_shell >= 0)
55 return vms_shell;
56
57 shell = getenv("SHELL");
58
59 /* No shell, means DCL */
60 if(!shell) {
61 vms_shell = 1;
62 return 1;
63 }
64
65 /* Have to make sure some one did not set shell to DCL */
66 if(strcmp(shell, "DCL") == 0) {
67 vms_shell = 1;
68 return 1;
69 }
70
71 vms_shell = 0;
72 return 0;
73 }
74
75 /*
76 * VMS has two exit() routines. When running under a Unix style shell, then
77 * Unix style and the __posix_exit() routine is used.
78 *
79 * When running under the DCL shell, then the VMS encoded codes and decc$exit()
80 * is used.
81 *
82 * We can not use exit() or return a code from main() because the actual
83 * routine called depends on both the compiler version, compile options, and
84 * feature macro settings, and one of the exit routines is hidden at compile
85 * time.
86 *
87 * Since we want Curl to work properly under the VMS DCL shell and Unix
88 * shells under VMS, this routine should compile correctly regardless of
89 * the settings.
90 */
91
vms_special_exit(int code,int vms_show)92 void vms_special_exit(int code, int vms_show)
93 {
94 int vms_code;
95
96 /* The POSIX exit mode is only available after VMS 7.0 */
97 #if __CRTL_VER >= 70000000
98 if(is_vms_shell() == 0) {
99 decc$__posix_exit(code);
100 }
101 #endif
102
103 if(code > CURL_LAST) { /* If CURL_LAST exceeded then */
104 vms_code = CURL_LAST; /* curlmsg.h is out of sync. */
105 }
106 else {
107 vms_code = vms_cond[code] | vms_show;
108 }
109 decc$exit(vms_code);
110 }
111
112 #if defined(__DECC) && !defined(__VAX) && \
113 defined(__CRTL_VER) && (__CRTL_VER >= 70301000)
114
115 /*
116 * 2004-09-19 SMS.
117 *
118 * decc_init()
119 *
120 * On non-VAX systems, use LIB$INITIALIZE to set a collection of C
121 * RTL features without using the DECC$* logical name method, nor
122 * requiring the user to define the corresponding logical names.
123 */
124
125 /* Structure to hold a DECC$* feature name and its desired value. */
126 struct decc_feat_t {
127 char *name;
128 int value;
129 };
130
131 /* Array of DECC$* feature names and their desired values. */
132 static const struct decc_feat_t decc_feat_array[] = {
133 /* Preserve command-line case with SET PROCESS/PARSE_STYLE=EXTENDED */
134 { "DECC$ARGV_PARSE_STYLE", 1 },
135 /* Preserve case for filenames on ODS5 disks. */
136 { "DECC$EFS_CASE_PRESERVE", 1 },
137 /* Enable multiple dots (and most characters) in ODS5 filenames,
138 while preserving VMS-ness of ";version". */
139 { "DECC$EFS_CHARSET", 1 },
140 /* List terminator. */
141 { (char *)NULL, 0 }
142 };
143
144 /* Flag to sense if decc_init() was called. */
145 static int decc_init_done = -1;
146
147 /* LIB$INITIALIZE initialization function. */
decc_init(void)148 static void decc_init(void)
149 {
150 int feat_index;
151 int feat_value;
152 int feat_value_max;
153 int feat_value_min;
154 int i;
155 int sts;
156
157 /* Set the global flag to indicate that LIB$INITIALIZE worked. */
158 decc_init_done = 1;
159
160 /* Loop through all items in the decc_feat_array[]. */
161 for(i = 0; decc_feat_array[i].name != NULL; i++) {
162
163 /* Get the feature index. */
164 feat_index = decc$feature_get_index(decc_feat_array[i].name);
165
166 if(feat_index >= 0) {
167 /* Valid item. Collect its properties. */
168 feat_value = decc$feature_get_value(feat_index, 1);
169 feat_value_min = decc$feature_get_value(feat_index, 2);
170 feat_value_max = decc$feature_get_value(feat_index, 3);
171
172 if((decc_feat_array[i].value >= feat_value_min) &&
173 (decc_feat_array[i].value <= feat_value_max)) {
174 /* Valid value. Set it if necessary. */
175 if(feat_value != decc_feat_array[i].value) {
176 sts = decc$feature_set_value(feat_index, 1,
177 decc_feat_array[i].value);
178 }
179 }
180 else {
181 /* Invalid DECC feature value. */
182 printf(" INVALID DECC FEATURE VALUE, %d: %d <= %s <= %d.\n",
183 feat_value,
184 feat_value_min, decc_feat_array[i].name, feat_value_max);
185 }
186 }
187 else {
188 /* Invalid DECC feature name. */
189 printf(" UNKNOWN DECC FEATURE: %s.\n", decc_feat_array[i].name);
190 }
191
192 }
193 }
194
195 /* Get "decc_init()" into a valid, loaded LIB$INITIALIZE PSECT. */
196
197 #pragma nostandard
198
199 /* Establish the LIB$INITIALIZE PSECTs, with proper alignment and
200 other attributes. Note that "nopic" is significant only on VAX. */
201 #pragma extern_model save
202 #pragma extern_model strict_refdef "LIB$INITIALIZ" 2, nopic, nowrt
203 const int spare[8] = {0};
204 #pragma extern_model strict_refdef "LIB$INITIALIZE" 2, nopic, nowrt
205 void (*const x_decc_init)() = decc_init;
206 #pragma extern_model restore
207
208 /* Fake reference to ensure loading the LIB$INITIALIZE PSECT. */
209 #pragma extern_model save
210 int LIB$INITIALIZE(void);
211 #pragma extern_model strict_refdef
212 int dmy_lib$initialize = (int) LIB$INITIALIZE;
213 #pragma extern_model restore
214
215 #pragma standard
216
217 #endif /* __DECC && !__VAX && __CRTL_VER && __CRTL_VER >= 70301000 */
218
219 #endif /* __VMS */
220