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