Generate apps/progs.h on the fly
[openssl.git] / apps / vms_decc_init.c
1 /*
2  * Written by sms and contributed to the OpenSSL project.
3  */
4 /* ====================================================================
5  * Copyright (c) 2010 The OpenSSL Project.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. All advertising materials mentioning features or use of this
20  *    software must display the following acknowledgment:
21  *    "This product includes software developed by the OpenSSL Project
22  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
23  *
24  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25  *    endorse or promote products derived from this software without
26  *    prior written permission. For written permission, please contact
27  *    licensing@OpenSSL.org.
28  *
29  * 5. Products derived from this software may not be called "OpenSSL"
30  *    nor may "OpenSSL" appear in their names without prior written
31  *    permission of the OpenSSL Project.
32  *
33  * 6. Redistributions of any form whatsoever must retain the following
34  *    acknowledgment:
35  *    "This product includes software developed by the OpenSSL Project
36  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49  * OF THE POSSIBILITY OF SUCH DAMAGE.
50  * ====================================================================
51  */
52
53 #if defined( __VMS) && !defined( OPENSSL_NO_DECC_INIT) && \
54  defined( __DECC) && !defined( __VAX) && (__CRTL_VER >= 70301000)
55 # define USE_DECC_INIT 1
56 #endif
57
58 #ifdef USE_DECC_INIT
59
60 /*
61  * ----------------------------------------------------------------------
62  * decc_init() On non-VAX systems, uses LIB$INITIALIZE to set a collection
63  * of C RTL features without using the DECC$* logical name method.
64  * ----------------------------------------------------------------------
65  */
66
67 # include <stdio.h>
68 # include <stdlib.h>
69 # include <unixlib.h>
70
71 # include "apps.h"
72
73 /* Global storage. */
74
75 /* Flag to sense if decc_init() was called. */
76
77 int decc_init_done = -1;
78
79 /* Structure to hold a DECC$* feature name and its desired value. */
80
81 typedef struct {
82     char *name;
83     int value;
84 } decc_feat_t;
85
86 /*
87  * Array of DECC$* feature names and their desired values. Note:
88  * DECC$ARGV_PARSE_STYLE is the urgent one.
89  */
90
91 decc_feat_t decc_feat_array[] = {
92     /* Preserve command-line case with SET PROCESS/PARSE_STYLE=EXTENDED */
93     {"DECC$ARGV_PARSE_STYLE", 1},
94
95     /* Preserve case for file names on ODS5 disks. */
96     {"DECC$EFS_CASE_PRESERVE", 1},
97
98     /*
99      * Enable multiple dots (and most characters) in ODS5 file names, while
100      * preserving VMS-ness of ";version".
101      */
102     {"DECC$EFS_CHARSET", 1},
103
104     /* List terminator. */
105     {(char *)NULL, 0}
106 };
107
108 char **copy_argv(int *argc, char *argv[])
109 {
110     /*-
111      * The note below is for historical purpose.  On VMS now we always
112      * copy argv "safely."
113      *
114      * 2011-03-22 SMS.
115      * If we have 32-bit pointers everywhere, then we're safe, and
116      * we bypass this mess, as on non-VMS systems.
117      * Problem 1: Compaq/HP C before V7.3 always used 32-bit
118      * pointers for argv[].
119      * Fix 1: For a 32-bit argv[], when we're using 64-bit pointers
120      * everywhere else, we always allocate and use a 64-bit
121      * duplicate of argv[].
122      * Problem 2: Compaq/HP C V7.3 (Alpha, IA64) before ECO1 failed
123      * to NULL-terminate a 64-bit argv[].  (As this was written, the
124      * compiler ECO was available only on IA64.)
125      * Fix 2: Unless advised not to (VMS_TRUST_ARGV), we test a
126      * 64-bit argv[argc] for NULL, and, if necessary, use a
127      * (properly) NULL-terminated (64-bit) duplicate of argv[].
128      * The same code is used in either case to duplicate argv[].
129      * Some of these decisions could be handled in preprocessing,
130      * but the code tends to get even uglier, and the penalty for
131      * deciding at compile- or run-time is tiny.
132      */
133
134     int i, count = *argc;
135     char **newargv = app_malloc(sizeof(*newargv) * (count + 1), "argv copy");
136
137     for (i = 0; i < count; i++)
138         newargv[i] = argv[i];
139     newargv[i] = NULL;
140     *argc = i;
141     return newargv;
142 }
143
144 /* LIB$INITIALIZE initialization function. */
145
146 static void decc_init(void)
147 {
148     char *openssl_debug_decc_init;
149     int verbose = 0;
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     /* Get debug option. */
158     openssl_debug_decc_init = getenv("OPENSSL_DEBUG_DECC_INIT");
159     if (openssl_debug_decc_init != NULL) {
160         verbose = strtol(openssl_debug_decc_init, NULL, 10);
161         if (verbose <= 0) {
162             verbose = 1;
163         }
164     }
165
166     /* Set the global flag to indicate that LIB$INITIALIZE worked. */
167     decc_init_done = 1;
168
169     /* Loop through all items in the decc_feat_array[]. */
170
171     for (i = 0; decc_feat_array[i].name != NULL; i++) {
172         /* Get the feature index. */
173         feat_index = decc$feature_get_index(decc_feat_array[i].name);
174         if (feat_index >= 0) {
175             /* Valid item.  Collect its properties. */
176             feat_value = decc$feature_get_value(feat_index, 1);
177             feat_value_min = decc$feature_get_value(feat_index, 2);
178             feat_value_max = decc$feature_get_value(feat_index, 3);
179
180             /* Check the validity of our desired value. */
181             if ((decc_feat_array[i].value >= feat_value_min) &&
182                 (decc_feat_array[i].value <= feat_value_max)) {
183                 /* Valid value.  Set it if necessary. */
184                 if (feat_value != decc_feat_array[i].value) {
185                     sts = decc$feature_set_value(feat_index,
186                                                  1, decc_feat_array[i].value);
187
188                     if (verbose > 1) {
189                         fprintf(stderr, " %s = %d, sts = %d.\n",
190                                 decc_feat_array[i].name,
191                                 decc_feat_array[i].value, sts);
192                     }
193                 }
194             } else {
195                 /* Invalid DECC feature value. */
196                 fprintf(stderr,
197                         " INVALID DECC$FEATURE VALUE, %d: %d <= %s <= %d.\n",
198                         feat_value,
199                         feat_value_min, decc_feat_array[i].name,
200                         feat_value_max);
201             }
202         } else {
203             /* Invalid DECC feature name. */
204             fprintf(stderr,
205                     " UNKNOWN DECC$FEATURE: %s.\n", decc_feat_array[i].name);
206         }
207     }
208
209     if (verbose > 0) {
210         fprintf(stderr, " DECC_INIT complete.\n");
211     }
212 }
213
214 /* Get "decc_init()" into a valid, loaded LIB$INITIALIZE PSECT. */
215
216 # pragma nostandard
217
218 /*
219  * Establish the LIB$INITIALIZE PSECTs, with proper alignment and other
220  * attributes.  Note that "nopic" is significant only on VAX.
221  */
222 # pragma extern_model save
223
224 # if __INITIAL_POINTER_SIZE == 64
225 #  define PSECT_ALIGN 3
226 # else
227 #  define PSECT_ALIGN 2
228 # endif
229
230 # pragma extern_model strict_refdef "LIB$INITIALIZ" PSECT_ALIGN, nopic, nowrt
231 const int spare[8] = { 0 };
232
233 # pragma extern_model strict_refdef "LIB$INITIALIZE" PSECT_ALIGN, nopic, nowrt
234 void (*const x_decc_init) () = decc_init;
235
236 # pragma extern_model restore
237
238 /* Fake reference to ensure loading the LIB$INITIALIZE PSECT. */
239
240 # pragma extern_model save
241
242 int LIB$INITIALIZE(void);
243
244 # pragma extern_model strict_refdef
245 int dmy_lib$initialize = (int)LIB$INITIALIZE;
246
247 # pragma extern_model restore
248
249 # pragma standard
250
251 #else                           /* def USE_DECC_INIT */
252
253 /* Dummy code to avoid a %CC-W-EMPTYFILE complaint. */
254 int decc_init_dummy(void);
255
256 #endif                          /* def USE_DECC_INIT */