xref: /freebsd/crypto/openssl/apps/vms_decc_init.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2010-2016 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert #if defined( __VMS) && !defined( OPENSSL_NO_DECC_INIT) && \
11*e0c4386eSCy Schubert  defined( __DECC) && !defined( __VAX) && (__CRTL_VER >= 70301000)
12*e0c4386eSCy Schubert # define USE_DECC_INIT 1
13*e0c4386eSCy Schubert #endif
14*e0c4386eSCy Schubert 
15*e0c4386eSCy Schubert #ifdef USE_DECC_INIT
16*e0c4386eSCy Schubert 
17*e0c4386eSCy Schubert /*
18*e0c4386eSCy Schubert  * ----------------------------------------------------------------------
19*e0c4386eSCy Schubert  * decc_init() On non-VAX systems, uses LIB$INITIALIZE to set a collection
20*e0c4386eSCy Schubert  * of C RTL features without using the DECC$* logical name method.
21*e0c4386eSCy Schubert  * ----------------------------------------------------------------------
22*e0c4386eSCy Schubert  */
23*e0c4386eSCy Schubert 
24*e0c4386eSCy Schubert # include <stdio.h>
25*e0c4386eSCy Schubert # include <stdlib.h>
26*e0c4386eSCy Schubert # include <unixlib.h>
27*e0c4386eSCy Schubert 
28*e0c4386eSCy Schubert /* Global storage. */
29*e0c4386eSCy Schubert 
30*e0c4386eSCy Schubert /* Flag to sense if decc_init() was called. */
31*e0c4386eSCy Schubert 
32*e0c4386eSCy Schubert int decc_init_done = -1;
33*e0c4386eSCy Schubert 
34*e0c4386eSCy Schubert /* Structure to hold a DECC$* feature name and its desired value. */
35*e0c4386eSCy Schubert 
36*e0c4386eSCy Schubert typedef struct {
37*e0c4386eSCy Schubert     char *name;
38*e0c4386eSCy Schubert     int value;
39*e0c4386eSCy Schubert } decc_feat_t;
40*e0c4386eSCy Schubert 
41*e0c4386eSCy Schubert /*
42*e0c4386eSCy Schubert  * Array of DECC$* feature names and their desired values. Note:
43*e0c4386eSCy Schubert  * DECC$ARGV_PARSE_STYLE is the urgent one.
44*e0c4386eSCy Schubert  */
45*e0c4386eSCy Schubert 
46*e0c4386eSCy Schubert decc_feat_t decc_feat_array[] = {
47*e0c4386eSCy Schubert     /* Preserve command-line case with SET PROCESS/PARSE_STYLE=EXTENDED */
48*e0c4386eSCy Schubert     {"DECC$ARGV_PARSE_STYLE", 1},
49*e0c4386eSCy Schubert 
50*e0c4386eSCy Schubert     /* Preserve case for file names on ODS5 disks. */
51*e0c4386eSCy Schubert     {"DECC$EFS_CASE_PRESERVE", 1},
52*e0c4386eSCy Schubert 
53*e0c4386eSCy Schubert     /*
54*e0c4386eSCy Schubert      * Enable multiple dots (and most characters) in ODS5 file names, while
55*e0c4386eSCy Schubert      * preserving VMS-ness of ";version".
56*e0c4386eSCy Schubert      */
57*e0c4386eSCy Schubert     {"DECC$EFS_CHARSET", 1},
58*e0c4386eSCy Schubert 
59*e0c4386eSCy Schubert     /* List terminator. */
60*e0c4386eSCy Schubert     {(char *)NULL, 0}
61*e0c4386eSCy Schubert };
62*e0c4386eSCy Schubert 
63*e0c4386eSCy Schubert 
64*e0c4386eSCy Schubert /* LIB$INITIALIZE initialization function. */
65*e0c4386eSCy Schubert 
decc_init(void)66*e0c4386eSCy Schubert static void decc_init(void)
67*e0c4386eSCy Schubert {
68*e0c4386eSCy Schubert     char *openssl_debug_decc_init;
69*e0c4386eSCy Schubert     int verbose = 0;
70*e0c4386eSCy Schubert     int feat_index;
71*e0c4386eSCy Schubert     int feat_value;
72*e0c4386eSCy Schubert     int feat_value_max;
73*e0c4386eSCy Schubert     int feat_value_min;
74*e0c4386eSCy Schubert     int i;
75*e0c4386eSCy Schubert     int sts;
76*e0c4386eSCy Schubert 
77*e0c4386eSCy Schubert     /* Get debug option. */
78*e0c4386eSCy Schubert     openssl_debug_decc_init = getenv("OPENSSL_DEBUG_DECC_INIT");
79*e0c4386eSCy Schubert     if (openssl_debug_decc_init != NULL) {
80*e0c4386eSCy Schubert         verbose = strtol(openssl_debug_decc_init, NULL, 10);
81*e0c4386eSCy Schubert         if (verbose <= 0) {
82*e0c4386eSCy Schubert             verbose = 1;
83*e0c4386eSCy Schubert         }
84*e0c4386eSCy Schubert     }
85*e0c4386eSCy Schubert 
86*e0c4386eSCy Schubert     /* Set the global flag to indicate that LIB$INITIALIZE worked. */
87*e0c4386eSCy Schubert     decc_init_done = 1;
88*e0c4386eSCy Schubert 
89*e0c4386eSCy Schubert     /* Loop through all items in the decc_feat_array[]. */
90*e0c4386eSCy Schubert 
91*e0c4386eSCy Schubert     for (i = 0; decc_feat_array[i].name != NULL; i++) {
92*e0c4386eSCy Schubert         /* Get the feature index. */
93*e0c4386eSCy Schubert         feat_index = decc$feature_get_index(decc_feat_array[i].name);
94*e0c4386eSCy Schubert         if (feat_index >= 0) {
95*e0c4386eSCy Schubert             /* Valid item.  Collect its properties. */
96*e0c4386eSCy Schubert             feat_value = decc$feature_get_value(feat_index, 1);
97*e0c4386eSCy Schubert             feat_value_min = decc$feature_get_value(feat_index, 2);
98*e0c4386eSCy Schubert             feat_value_max = decc$feature_get_value(feat_index, 3);
99*e0c4386eSCy Schubert 
100*e0c4386eSCy Schubert             /* Check the validity of our desired value. */
101*e0c4386eSCy Schubert             if ((decc_feat_array[i].value >= feat_value_min) &&
102*e0c4386eSCy Schubert                 (decc_feat_array[i].value <= feat_value_max)) {
103*e0c4386eSCy Schubert                 /* Valid value.  Set it if necessary. */
104*e0c4386eSCy Schubert                 if (feat_value != decc_feat_array[i].value) {
105*e0c4386eSCy Schubert                     sts = decc$feature_set_value(feat_index,
106*e0c4386eSCy Schubert                                                  1, decc_feat_array[i].value);
107*e0c4386eSCy Schubert 
108*e0c4386eSCy Schubert                     if (verbose > 1) {
109*e0c4386eSCy Schubert                         fprintf(stderr, " %s = %d, sts = %d.\n",
110*e0c4386eSCy Schubert                                 decc_feat_array[i].name,
111*e0c4386eSCy Schubert                                 decc_feat_array[i].value, sts);
112*e0c4386eSCy Schubert                     }
113*e0c4386eSCy Schubert                 }
114*e0c4386eSCy Schubert             } else {
115*e0c4386eSCy Schubert                 /* Invalid DECC feature value. */
116*e0c4386eSCy Schubert                 fprintf(stderr,
117*e0c4386eSCy Schubert                         " INVALID DECC$FEATURE VALUE, %d: %d <= %s <= %d.\n",
118*e0c4386eSCy Schubert                         feat_value,
119*e0c4386eSCy Schubert                         feat_value_min, decc_feat_array[i].name,
120*e0c4386eSCy Schubert                         feat_value_max);
121*e0c4386eSCy Schubert             }
122*e0c4386eSCy Schubert         } else {
123*e0c4386eSCy Schubert             /* Invalid DECC feature name. */
124*e0c4386eSCy Schubert             fprintf(stderr,
125*e0c4386eSCy Schubert                     " UNKNOWN DECC$FEATURE: %s.\n", decc_feat_array[i].name);
126*e0c4386eSCy Schubert         }
127*e0c4386eSCy Schubert     }
128*e0c4386eSCy Schubert 
129*e0c4386eSCy Schubert     if (verbose > 0) {
130*e0c4386eSCy Schubert         fprintf(stderr, " DECC_INIT complete.\n");
131*e0c4386eSCy Schubert     }
132*e0c4386eSCy Schubert }
133*e0c4386eSCy Schubert 
134*e0c4386eSCy Schubert /* Get "decc_init()" into a valid, loaded LIB$INITIALIZE PSECT. */
135*e0c4386eSCy Schubert 
136*e0c4386eSCy Schubert # pragma nostandard
137*e0c4386eSCy Schubert 
138*e0c4386eSCy Schubert /*
139*e0c4386eSCy Schubert  * Establish the LIB$INITIALIZE PSECTs, with proper alignment and other
140*e0c4386eSCy Schubert  * attributes.  Note that "nopic" is significant only on VAX.
141*e0c4386eSCy Schubert  */
142*e0c4386eSCy Schubert # pragma extern_model save
143*e0c4386eSCy Schubert 
144*e0c4386eSCy Schubert # if __INITIAL_POINTER_SIZE == 64
145*e0c4386eSCy Schubert #  define PSECT_ALIGN 3
146*e0c4386eSCy Schubert # else
147*e0c4386eSCy Schubert #  define PSECT_ALIGN 2
148*e0c4386eSCy Schubert # endif
149*e0c4386eSCy Schubert 
150*e0c4386eSCy Schubert # pragma extern_model strict_refdef "LIB$INITIALIZ" PSECT_ALIGN, nopic, nowrt
151*e0c4386eSCy Schubert const int spare[8] = { 0 };
152*e0c4386eSCy Schubert 
153*e0c4386eSCy Schubert # pragma extern_model strict_refdef "LIB$INITIALIZE" PSECT_ALIGN, nopic, nowrt
154*e0c4386eSCy Schubert void (*const x_decc_init) () = decc_init;
155*e0c4386eSCy Schubert 
156*e0c4386eSCy Schubert # pragma extern_model restore
157*e0c4386eSCy Schubert 
158*e0c4386eSCy Schubert /* Fake reference to ensure loading the LIB$INITIALIZE PSECT. */
159*e0c4386eSCy Schubert 
160*e0c4386eSCy Schubert # pragma extern_model save
161*e0c4386eSCy Schubert 
162*e0c4386eSCy Schubert int LIB$INITIALIZE(void);
163*e0c4386eSCy Schubert 
164*e0c4386eSCy Schubert # pragma extern_model strict_refdef
165*e0c4386eSCy Schubert int dmy_lib$initialize = (int)LIB$INITIALIZE;
166*e0c4386eSCy Schubert 
167*e0c4386eSCy Schubert # pragma extern_model restore
168*e0c4386eSCy Schubert 
169*e0c4386eSCy Schubert # pragma standard
170*e0c4386eSCy Schubert 
171*e0c4386eSCy Schubert #else                           /* def USE_DECC_INIT */
172*e0c4386eSCy Schubert 
173*e0c4386eSCy Schubert /* Dummy code to avoid a %CC-W-EMPTYFILE complaint. */
174*e0c4386eSCy Schubert int decc_init_dummy(void);
175*e0c4386eSCy Schubert 
176*e0c4386eSCy Schubert #endif                          /* def USE_DECC_INIT */
177