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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
/* VMS::Stdio - VMS extensions to stdio routines
*
* Version: 2.2
* Author: Charles Bailey bailey@newman.upenn.edu
* Revised: 18-Jul-1998
*
*/
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <file.h>
#include <iodef.h>
#include <rms.h>
#include <starlet.h>
static bool
constant(name, pval)
char *name;
IV *pval;
{
if (strnNE(name, "O_", 2)) return FALSE;
if (strEQ(name, "O_APPEND"))
#ifdef O_APPEND
{ *pval = O_APPEND; return TRUE; }
#else
return FALSE;
#endif
if (strEQ(name, "O_CREAT"))
#ifdef O_CREAT
{ *pval = O_CREAT; return TRUE; }
#else
return FALSE;
#endif
if (strEQ(name, "O_EXCL"))
#ifdef O_EXCL
{ *pval = O_EXCL; return TRUE; }
#else
return FALSE;
#endif
if (strEQ(name, "O_NDELAY"))
#ifdef O_NDELAY
{ *pval = O_NDELAY; return TRUE; }
#else
return FALSE;
#endif
if (strEQ(name, "O_NOWAIT"))
#ifdef O_NOWAIT
{ *pval = O_NOWAIT; return TRUE; }
#else
return FALSE;
#endif
if (strEQ(name, "O_RDONLY"))
#ifdef O_RDONLY
{ *pval = O_RDONLY; return TRUE; }
#else
return FALSE;
#endif
if (strEQ(name, "O_RDWR"))
#ifdef O_RDWR
{ *pval = O_RDWR; return TRUE; }
#else
return FALSE;
#endif
if (strEQ(name, "O_TRUNC"))
#ifdef O_TRUNC
{ *pval = O_TRUNC; return TRUE; }
#else
return FALSE;
#endif
if (strEQ(name, "O_WRONLY"))
#ifdef O_WRONLY
{ *pval = O_WRONLY; return TRUE; }
#else
return FALSE;
#endif
return FALSE;
}
static SV *
newFH(FILE *fp, char type) {
SV *rv;
GV **stashp, *gv = (GV *)NEWSV(0,0);
HV *stash;
IO *io;
/* Find stash for VMS::Stdio. We don't do this once at boot
* to allow for possibility of threaded Perl with per-thread
* symbol tables. This code (through io = ...) is really
* equivalent to gv_fetchpv("VMS::Stdio::__FH__",TRUE,SVt_PVIO),
* with a little less overhead, and good exercise for me. :-) */
stashp = (GV **)hv_fetch(PL_defstash,"VMS::",5,TRUE);
if (!stashp || *stashp == (GV *)&PL_sv_undef) return Nullsv;
if (!(stash = GvHV(*stashp))) stash = GvHV(*stashp) = newHV();
stashp = (GV **)hv_fetch(GvHV(*stashp),"Stdio::",7,TRUE);
if (!stashp || *stashp == (GV *)&PL_sv_undef) return Nullsv;
if (!(stash = GvHV(*stashp))) stash = GvHV(*stashp) = newHV();
/* Set up GV to point to IO, and then take reference */
gv_init(gv,stash,"__FH__",6,0);
io = GvIOp(gv) = newIO();
IoIFP(io) = fp;
if (type != '<') IoOFP(io) = fp;
IoTYPE(io) = type;
rv = newRV((SV *)gv);
SvREFCNT_dec(gv);
return sv_bless(rv,stash);
}
MODULE = VMS::Stdio PACKAGE = VMS::Stdio
void
constant(name)
char * name
PROTOTYPE: $
CODE:
IV i;
if (constant(name, &i))
ST(0) = sv_2mortal(newSViv(i));
else
ST(0) = &PL_sv_undef;
void
binmode(fh)
SV * fh
PROTOTYPE: $
CODE:
IO *io = sv_2io(fh);
FILE *fp = io ? IoOFP(io) : NULL;
char iotype = io ? IoTYPE(io) : '\0';
char filespec[NAM$C_MAXRSS], *acmode, *s, *colon, *dirend = Nullch;
int ret = 0, saverrno = errno, savevmserrno = vaxc$errno;
fpos_t pos;
if (fp == NULL || strchr(">was+-|",iotype) == Nullch) {
set_errno(EBADF); set_vaxc_errno(SS$_IVCHAN); XSRETURN_UNDEF;
}
if (!fgetname(fp,filespec)) XSRETURN_UNDEF;
for (s = filespec; *s; s++) {
if (*s == ':') colon = s;
else if (*s == ']' || *s == '>') dirend = s;
}
/* Looks like a tmpfile, which will go away if reopened */
if (s == dirend + 3) {
set_errno(EBADF); set_vaxc_errno(RMS$_IOP); XSRETURN_UNDEF;
}
/* If we've got a non-file-structured device, clip off the trailing
* junk, and don't lose sleep if we can't get a stream position. */
if (dirend == Nullch) *(colon+1) = '\0';
if (iotype != '-' && (ret = fgetpos(fp, &pos)) == -1 && dirend)
XSRETURN_UNDEF;
switch (iotype) {
case '<': case 'r': acmode = "rb"; break;
case '>': case 'w': case '|':
/* use 'a' instead of 'w' to avoid creating new file;
fsetpos below will take care of restoring file position */
case 'a': acmode = "ab"; break;
case '+': case 's': acmode = "rb+"; break;
case '-': acmode = fileno(fp) ? "ab" : "rb"; break;
/* iotype'll be null for the SYS$INPUT:/SYS$OUTPUT:/SYS$ERROR: files */
/* since we didn't really open them and can't really */
/* reopen them */
case 0: XSRETURN_UNDEF;
default:
if (PL_dowarn) warn("Unrecognized iotype %c for %s in binmode",
iotype, filespec);
acmode = "rb+";
}
if (freopen(filespec,acmode,fp) == NULL) XSRETURN_UNDEF;
if (iotype != '-' && ret != -1 && fsetpos(fp,&pos) == -1) XSRETURN_UNDEF;
if (ret == -1) { set_errno(saverrno); set_vaxc_errno(savevmserrno); }
XSRETURN_YES;
void
flush(fp)
FILE * fp
PROTOTYPE: $
CODE:
if (fflush(fp)) { ST(0) = &PL_sv_undef; }
else { clearerr(fp); ST(0) = &PL_sv_yes; }
char *
getname(fp)
FILE * fp
PROTOTYPE: $
CODE:
char fname[NAM$C_MAXRSS+1];
ST(0) = sv_newmortal();
if (fgetname(fp,fname) != NULL) sv_setpv(ST(0),fname);
void
rewind(fp)
FILE * fp
PROTOTYPE: $
CODE:
ST(0) = rewind(fp) ? &PL_sv_undef : &PL_sv_yes;
void
remove(name)
char *name
PROTOTYPE: $
CODE:
ST(0) = remove(name) ? &PL_sv_undef : &PL_sv_yes;
void
setdef(...)
PROTOTYPE: @
CODE:
char vmsdef[NAM$C_MAXRSS+1], es[NAM$C_MAXRSS], sep;
unsigned long int retsts;
struct FAB deffab = cc$rms_fab;
struct NAM defnam = cc$rms_nam;
struct dsc$descriptor_s dirdsc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
STRLEN n_a;
if (items) {
SV *defsv = ST(items-1); /* mimic chdir() */
ST(0) = &PL_sv_undef;
if (!SvPOK(defsv)) { SETERRNO(EINVAL,LIB$_INVARG); XSRETURN(1); }
if (tovmsspec(SvPV(defsv,n_a),vmsdef) == NULL) { XSRETURN(1); }
deffab.fab$l_fna = vmsdef; deffab.fab$b_fns = strlen(vmsdef);
}
else {
deffab.fab$l_fna = "SYS$LOGIN"; deffab.fab$b_fns = 9;
EXTEND(sp,1); ST(0) = &PL_sv_undef;
}
defnam.nam$l_esa = es; defnam.nam$b_ess = sizeof es;
deffab.fab$l_nam = &defnam;
retsts = sys$parse(&deffab,0,0);
if (retsts & 1) {
if (defnam.nam$v_wildcard) retsts = RMS$_WLD;
else if (defnam.nam$b_name || defnam.nam$b_type > 1 ||
defnam.nam$b_ver > 1) retsts = RMS$_DIR;
}
defnam.nam$b_nop |= NAM$M_SYNCHK; defnam.nam$l_rlf = NULL; deffab.fab$b_dns = 0;
if (!(retsts & 1)) {
set_vaxc_errno(retsts);
switch (retsts) {
case RMS$_DNF:
set_errno(ENOENT); break;
case RMS$_SYN: case RMS$_DIR: case RMS$_DEV:
set_errno(EINVAL); break;
case RMS$_PRV:
set_errno(EACCES); break;
default:
set_errno(EVMSERR); break;
}
(void) sys$parse(&deffab,0,0); /* free up context */
XSRETURN(1);
}
sep = *defnam.nam$l_dir;
*defnam.nam$l_dir = '\0';
my_setenv("SYS$DISK",defnam.nam$b_node ? defnam.nam$l_node : defnam.nam$l_dev);
*defnam.nam$l_dir = sep;
dirdsc.dsc$a_pointer = defnam.nam$l_dir; dirdsc.dsc$w_length = defnam.nam$b_dir;
if ((retsts = sys$setddir(&dirdsc,0,0)) & 1) ST(0) = &PL_sv_yes;
else { set_errno(EVMSERR); set_vaxc_errno(retsts); }
(void) sys$parse(&deffab,0,0); /* free up context */
void
sync(fp)
FILE * fp
PROTOTYPE: $
CODE:
if (fsync(fileno(fp))) { ST(0) = &PL_sv_undef; }
else { clearerr(fp); ST(0) = &PL_sv_yes; }
char *
tmpnam()
PROTOTYPE:
CODE:
char fname[L_tmpnam];
ST(0) = sv_newmortal();
if (tmpnam(fname) != NULL) sv_setpv(ST(0),fname);
void
vmsopen(spec,...)
char * spec
PROTOTYPE: @
CODE:
char *args[8],mode[3] = {'r','\0','\0'}, type = '<';
register int i, myargc;
FILE *fp;
STRLEN n_a;
if (!spec || !*spec) {
SETERRNO(EINVAL,LIB$_INVARG);
XSRETURN_UNDEF;
}
if (items > 9) croak("too many args");
/* First, set up name and mode args from perl's string */
if (*spec == '+') {
mode[1] = '+';
spec++;
}
if (*spec == '>') {
if (*(spec+1) == '>') *mode = 'a', spec += 2;
else *mode = 'w', spec++;
}
else if (*spec == '<') spec++;
myargc = items - 1;
for (i = 0; i < myargc; i++) args[i] = SvPV(ST(i+1),n_a);
/* This hack brought to you by C's opaque arglist management */
switch (myargc) {
case 0:
fp = fopen(spec,mode);
break;
case 1:
fp = fopen(spec,mode,args[0]);
break;
case 2:
fp = fopen(spec,mode,args[0],args[1]);
break;
case 3:
fp = fopen(spec,mode,args[0],args[1],args[2]);
break;
case 4:
fp = fopen(spec,mode,args[0],args[1],args[2],args[3]);
break;
case 5:
fp = fopen(spec,mode,args[0],args[1],args[2],args[3],args[4]);
break;
case 6:
fp = fopen(spec,mode,args[0],args[1],args[2],args[3],args[4],args[5]);
break;
case 7:
fp = fopen(spec,mode,args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
break;
case 8:
fp = fopen(spec,mode,args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
break;
}
if (fp != Nullfp) {
SV *fh = newFH(fp,(mode[1] ? '+' : (mode[0] == 'r' ? '<' : (mode[0] == 'a' ? 'a' : '>'))));
ST(0) = (fh ? sv_2mortal(fh) : &PL_sv_undef);
}
else { ST(0) = &PL_sv_undef; }
void
vmssysopen(spec,mode,perm,...)
char * spec
int mode
int perm
PROTOTYPE: @
CODE:
char *args[8];
int i, myargc, fd;
FILE *fp;
SV *fh;
STRLEN n_a;
if (!spec || !*spec) {
SETERRNO(EINVAL,LIB$_INVARG);
XSRETURN_UNDEF;
}
if (items > 11) croak("too many args");
myargc = items - 3;
for (i = 0; i < myargc; i++) args[i] = SvPV(ST(i+3),n_a);
/* More fun with C calls; can't combine with above because
args 2,3 of different types in fopen() and open() */
switch (myargc) {
case 0:
fd = open(spec,mode,perm);
break;
case 1:
fd = open(spec,mode,perm,args[0]);
break;
case 2:
fd = open(spec,mode,perm,args[0],args[1]);
break;
case 3:
fd = open(spec,mode,perm,args[0],args[1],args[2]);
break;
case 4:
fd = open(spec,mode,perm,args[0],args[1],args[2],args[3]);
break;
case 5:
fd = open(spec,mode,perm,args[0],args[1],args[2],args[3],args[4]);
break;
case 6:
fd = open(spec,mode,perm,args[0],args[1],args[2],args[3],args[4],args[5]);
break;
case 7:
fd = open(spec,mode,perm,args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
break;
case 8:
fd = open(spec,mode,perm,args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
break;
}
i = mode & 3;
if (fd >= 0 &&
((fp = fdopen(fd, &("r\000w\000r+"[2*i]))) != Nullfp)) {
SV *fh = newFH(fp,"<>++"[i]);
ST(0) = (fh ? sv_2mortal(fh) : &PL_sv_undef);
}
else { ST(0) = &PL_sv_undef; }
void
waitfh(fp)
FILE * fp
PROTOTYPE: $
CODE:
ST(0) = fwait(fp) ? &PL_sv_undef : &PL_sv_yes;
void
writeof(mysv)
SV * mysv
PROTOTYPE: $
CODE:
char devnam[257], *cp;
unsigned long int chan, iosb[2], retsts, retsts2;
struct dsc$descriptor devdsc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, devnam};
IO *io = sv_2io(mysv);
FILE *fp = io ? IoOFP(io) : NULL;
if (fp == NULL || strchr(">was+-|",IoTYPE(io)) == Nullch) {
set_errno(EBADF); set_vaxc_errno(SS$_IVCHAN); XSRETURN_UNDEF;
}
if (fgetname(fp,devnam) == Nullch) { ST(0) = &PL_sv_undef; XSRETURN(1); }
if ((cp = strrchr(devnam,':')) != NULL) *(cp+1) = '\0';
devdsc.dsc$w_length = strlen(devnam);
retsts = sys$assign(&devdsc,&chan,0,0);
if (retsts & 1) retsts = sys$qiow(0,chan,IO$_WRITEOF,iosb,0,0,0,0,0,0,0,0);
if (retsts & 1) retsts = iosb[0];
retsts2 = sys$dassgn(chan); /* Be sure to deassign the channel */
if (retsts & 1) retsts = retsts2;
if (retsts & 1) { ST(0) = &PL_sv_yes; }
else {
set_vaxc_errno(retsts);
switch (retsts) {
case SS$_EXQUOTA: case SS$_INSFMEM: case SS$_MBFULL:
case SS$_MBTOOSML: case SS$_NOIOCHAN: case SS$_NOLINKS:
case SS$_BUFFEROVF:
set_errno(ENOSPC); break;
case SS$_ILLIOFUNC: case SS$_DEVOFFLINE: case SS$_NOSUCHDEV:
set_errno(EBADF); break;
case SS$_NOPRIV:
set_errno(EACCES); break;
default: /* Includes "shouldn't happen" cases that might map */
set_errno(EVMSERR); break; /* to other errno values */
}
ST(0) = &PL_sv_undef;
}
|