File: vms.c

package info (click to toggle)
unixodbc 2.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,524 kB
  • ctags: 7,656
  • sloc: ansic: 89,405; sh: 15,975; makefile: 1,574; yacc: 969; sql: 1
file content (422 lines) | stat: -rw-r--r-- 11,723 bytes parent folder | download | duplicates (6)
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
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <descrip.h>
#include <starlet.h>
#include <ssdef.h>
#include <stsdef.h>
#include <lnmdef.h>
#include <libdef.h>
#include <libfisdef.h>
#include <lib$routines>
#include <psldef.h>
#include <rmsdef.h>
#include <fabdef.h>
#include <namdef.h>
#include <unixlib.h>

static int	error_status = SS$_NORMAL;
static char	error_buffer[256];
static char	getenv_buffer[256];

typedef struct 
{
    struct dsc$descriptor_s	fnmdes;
    struct dsc$descriptor_s	imgdes;
    struct dsc$descriptor_s	symdes;

    char			filename[NAM$C_MAXRSS];
} vms_dl;


int vms_dlsym	    (vms_dl	*, void	**, int);
void * lt_dlsym	    (void *, const char *);

int lt_dlinit (void)
{
    return 0;
}

char translate_buffer[NAM$C_MAXRSS+1];

int to_vms_callback(char *name, int type)
{
	strcpy(translate_buffer, name);
	return 1;
}

void * lt_dlopen (const char *filename)
{

   /* 
    * Locates and validates a shareable image.  If the caller supplies a 
    * path as part of the image name, that's where we look.  Note that this
    * includes cases where the image name is a logical name pointing to a full
    * file spec including path.  If there is no path,  we look at wherever the
    * logical name LTDL_LIBRARY_PATH points (if it exists); it'd normally be a
    * search list logical defined in lt_dladdsearchdir, so it could point to a
    * number of places.   As a last resort we look in SYS$SHARE. 
    */

    vms_dl	*dh;
    int		status;  
    struct FAB	imgfab;  
    struct NAM  imgnam;
    char defimg[NAM$C_MAXRSS+1];
    char *defpath;
	char local_fspec[NAM$C_MAXRSS+1];

    if (filename == NULL) 
    {
	error_status = SS$_UNSUPPORTED;
	return NULL;
    }

	strcpy(local_fspec, filename);

    /* 
     * The driver manager will handle a hard-coded path from a .ini file, but
     * only if it's an absolute path in unix syntax.  To make those acceptable
     * to lib$find_image_symbol, we have to convert to VMS syntax.  N.B. Because
     * of the static buffer, this is not thread-safe, but copying immediately to
     * local storage should limit the damage.
     */
	
    if (filename[0] == '/') {
        int num_translated = decc$to_vms(local_fspec, to_vms_callback, 0, 1);
        if (num_translated == 1) strcpy(local_fspec, translate_buffer);
    }

    dh = (vms_dl *)malloc (sizeof (vms_dl));  
    memset( dh, 0, sizeof(vms_dl) );
    if (dh == NULL) 
    {
	error_status = SS$_INSFMEM;
	return NULL;
    }

    imgfab = cc$rms_fab;
	imgfab.fab$l_fna = local_fspec;
	imgfab.fab$b_fns = (int) strlen (local_fspec);
    imgfab.fab$w_ifi = 0;  

   /* If the logical name LTDL_LIBRARY_PATH does not exist, we'll depend
    * on the image name being a logical name or on the image residing in
    * SYS$SHARE.
    */
    if ( getvmsenv("LTDL_LIBRARY_PATH") == NULL )
    {
        strcpy( defimg, ".EXE" );
    }
    else 
    {
        strcpy( defimg, "LTDL_LIBRARY_PATH:.EXE" );
    }
    imgfab.fab$l_dna = defimg;
    imgfab.fab$b_dns = strlen(defimg);
    imgfab.fab$l_fop = FAB$M_NAM;
    imgfab.fab$l_nam = &imgnam;  
    imgnam = cc$rms_nam;
    imgnam.nam$l_esa = dh->filename;  
    imgnam.nam$b_ess = NAM$C_MAXRSS;
  
    status = sys$parse (&imgfab);  
    if (!($VMS_STATUS_SUCCESS(status)))
    {
        /* No luck with LTDL_LIBRARY_PATH; try SYS$SHARE */
        strcpy( defimg, "SYS$SHARE:.EXE" );
        imgfab.fab$b_dns = strlen(defimg);

        status = sys$parse (&imgfab);
        if (!($VMS_STATUS_SUCCESS(status)))
        {
	error_status = status;
	return NULL;
    }
    }

    dh->fnmdes.dsc$b_dtype = DSC$K_DTYPE_T;
    dh->fnmdes.dsc$b_class = DSC$K_CLASS_S;
    dh->fnmdes.dsc$a_pointer = imgnam.nam$l_name;
    dh->fnmdes.dsc$w_length = imgnam.nam$b_name;
    dh->imgdes.dsc$b_dtype = DSC$K_DTYPE_T;
    dh->imgdes.dsc$b_class = DSC$K_CLASS_S;
    dh->imgdes.dsc$a_pointer = dh->filename;
    dh->imgdes.dsc$w_length = imgnam.nam$b_esl;  

    /*
    ** Attempt to load a symbol at this stage to
    ** validate that the shared file can be loaded
    */
    lt_dlsym (dh, "Fake_Symbol_Name");
    if (!((error_status ^ LIB$_KEYNOTFOU) & ~7)) error_status = SS$_NORMAL;

    if (!($VMS_STATUS_SUCCESS(error_status)))
    {
	free (dh);
	return NULL;
    }
 
    return dh;
}

int lt_dlclose (void *handle)
{
    free (handle);
    return 0;
}

void * lt_dlsym (void *handle, const char *name)
{
    vms_dl			*dh;
    void			*ptr;
    int				status, flags;

    dh = (vms_dl *)handle;
    if (!dh) return NULL;

    dh->symdes.dsc$b_dtype = DSC$K_DTYPE_T;  
    dh->symdes.dsc$b_class = DSC$K_CLASS_S;
    dh->symdes.dsc$a_pointer = (char *) name;
    dh->symdes.dsc$w_length = strlen (name);

    /* firstly attempt with flags set to 0 case insensitive */
    flags = 0;
    status = vms_dlsym (dh, &ptr, flags);
    if (!($VMS_STATUS_SUCCESS(status)))
    {
	/*
	** Try again with mixed case flag set 
	*/
        flags = LIB$M_FIS_MIXEDCASE;

	status = vms_dlsym (dh, &ptr, flags);
	if (!($VMS_STATUS_SUCCESS(status)))
	{
	    error_status = status;
	    return NULL;
	}
    }

    return ptr;
}

int vms_dlsym (
    vms_dl	*dh,
    void	**ptr,
    int		flags)
{
    LIB$ESTABLISH (LIB$SIG_TO_RET);
    return LIB$FIND_IMAGE_SYMBOL (&dh->fnmdes, &dh->symdes, ptr, &dh->imgdes, flags);
}


const char *lt_dlerror (void)
{
    struct dsc$descriptor   desc;
    short		    outlen;
    int			    status;

    if (($VMS_STATUS_SUCCESS(error_status))) return NULL;
    
    desc.dsc$b_dtype = DSC$K_DTYPE_T;
    desc.dsc$b_class = DSC$K_CLASS_S;  
    desc.dsc$a_pointer = error_buffer;
    desc.dsc$w_length = sizeof (error_buffer);

    status = sys$getmsg (error_status, &outlen, &desc, 15, 0);  
    if ($VMS_STATUS_SUCCESS(status)) error_buffer[outlen] = '\0';    
    else sprintf (error_buffer, "OpenVMS exit status %8X", error_status);

    error_status = SS$_NORMAL;  

    return (error_buffer);
}

struct itemlist3 {
    unsigned short	buflen;
    unsigned short	item;
    void		*buf;
    unsigned short	*retlen;
};

int lt_dladdsearchdir (const char *search_dir)
{
    /*
     * Adds a path to the list of paths where lt_dlopen will look for shareable images.
     * We do this via a user-mode search list logical, adding one more item to the end of
     * the list whenever called.
     */

    $DESCRIPTOR(lib_path_d, "LTDL_LIBRARY_PATH");
    $DESCRIPTOR( proc_table_d, "LNM$PROCESS_TABLE" );
    unsigned int status = 0;
    unsigned char accmode = 0, lnm_exists = 1;
    int index = 0, max_index = 0;
    struct itemlist3 trnlnmlst[4] = {{sizeof(accmode), LNM$_ACMODE, &accmode, 0},
                                     {sizeof(max_index), LNM$_MAX_INDEX, &max_index, 0},
                                     {0, 0, 0, 0},
                                     {0, 0, 0, 0}};
    struct itemlist3 *crelnmlst;
    struct eqvarray {
      char eqvname[256];
    };
    struct eqvarray *eqvlist;

    /* First just check to see whether the logical name exists and how many equivalence
     * names there are.
     */
    status = SYS$TRNLNM ( NULL,
                          &proc_table_d,
                          &lib_path_d,
                          NULL,
                          trnlnmlst);

    /* If the logical name doesn't exist or exists at a privilege mode or table I
     * can't even look at, then I'll want to proceed with creating my user-mode logical.
     */
    if ( status == SS$_NOLOGNAM || status == SS$_NOPRIV ) {
       status = SS$_NORMAL;
       lnm_exists = 0;        /* skip further translation attempts */
       max_index = 0;
    }
    if (!$VMS_STATUS_SUCCESS(status)) {
       error_status = status;
       return -1;
    }

    /* Also skip further translation if the first translation exists in any mode other
     * than user mode; we want to stick user mode logicals in front of these.
     */
    if ( accmode != PSL$C_USER ) {
       lnm_exists = 0;
       max_index = 0;
    }

    /* Allocate an item list for logical name creation and an array of equivalence
     * name buffers.
     */
    crelnmlst = (struct itemlist3 *) malloc( sizeof(struct itemlist3) * (max_index + 3) );
    if (crelnmlst == NULL) { error_status = SS$_INSFMEM; return -1; }

    eqvlist = (struct eqvarray *) malloc( sizeof(struct eqvarray) * (max_index + 2) );
    if (eqvlist == NULL) { error_status = SS$_INSFMEM; return -1; }

    trnlnmlst[1].buflen = sizeof(index);
    trnlnmlst[1].item = LNM$_INDEX;
    trnlnmlst[1].buf = &index;
    trnlnmlst[1].retlen = NULL;

    trnlnmlst[2].buflen = sizeof(eqvlist[0].eqvname);
    trnlnmlst[2].item = LNM$_STRING;

    /* The loops iterates over the search list index, getting the translation
     * for each index and storing it in the item list for re-creation.
     */

    for (index = 0; index <= max_index && lnm_exists; index++ ) {

        /* Wire the input buffer for translation to the output buffer for creation */
        trnlnmlst[2].buf = &eqvlist[index].eqvname;
        crelnmlst[index].buf = &eqvlist[index].eqvname;
        trnlnmlst[2].retlen = &crelnmlst[index].buflen;

        status = SYS$TRNLNM ( NULL,
                              &proc_table_d,
                              &lib_path_d,
                              NULL,
                              trnlnmlst);

        if (!$VMS_STATUS_SUCCESS(status)) {
           error_status = status;
           free(crelnmlst);
           free(eqvlist);
           return -1;
        }

        /* If we come across a non-user-mode translation, back up and get out
         * because we don't want to recreate those in user mode.
         */
        if ( accmode != PSL$C_USER ) {
            index--;
            break;
        }

        crelnmlst[index].item = LNM$_STRING;
        crelnmlst[index].retlen = NULL;
    }

    /* At this point we have captured all the existing translations (if
     * any) and stored them in the item list for re-creation of the logical
     * name.  All that's left is to add the new item to the end of the list
     * and terminate the list.
     */

    crelnmlst[index].buflen = strlen(search_dir);
    crelnmlst[index].item = LNM$_STRING;
    crelnmlst[index].buf = (char *) search_dir;
    crelnmlst[index].retlen = NULL;

    crelnmlst[index+1].buflen = 0;
    crelnmlst[index+1].item = 0;
    crelnmlst[index+1].buf = NULL;
    crelnmlst[index+1].retlen = NULL;

    accmode = PSL$C_USER;

    status = SYS$CRELNM( NULL,
                         &proc_table_d,
                         &lib_path_d,
                         &accmode,
                         crelnmlst );

    if (!$VMS_STATUS_SUCCESS(status)) {
       error_status = status;
       free(crelnmlst);
       free(eqvlist);
       return -1;
    }

    free(crelnmlst);
    free(eqvlist);
    error_status = SS$_NORMAL;
    return 0;
}


char * getvmsenv (char *symbol)
{
    int			    status;
    unsigned short	    cbvalue;
    $DESCRIPTOR		    (logicalnametable, "LNM$FILE_DEV");
    struct dsc$descriptor_s logicalname;
    struct itemlist3	    itemlist[2];

    logicalname.dsc$w_length = strlen (symbol);
    logicalname.dsc$b_dtype = DSC$K_DTYPE_T;
    logicalname.dsc$b_class = DSC$K_CLASS_S;
    logicalname.dsc$a_pointer = symbol;

    itemlist[0].buflen = sizeof (getenv_buffer) -1;
    itemlist[0].item = LNM$_STRING;
    itemlist[0].buf = getenv_buffer;
    itemlist[0].retlen = &cbvalue;

    itemlist[1].buflen = 0;
    itemlist[1].item = 0;
    itemlist[1].buf = 0;
    itemlist[1].retlen = 0;

    status = SYS$TRNLNM (0, &logicalnametable, &logicalname, 0, itemlist);
    if (!($VMS_STATUS_SUCCESS(status))) return NULL;

    if (cbvalue > 0)
    {
	getenv_buffer[cbvalue] = '\0';
	return getenv_buffer;
    }

    return NULL;
}