build (and test!) VMS::Stdio on VMS
[p5sagit/p5-mst-13.2.git] / vms / ext / Stdio / Stdio.xs
index 0a7b47e..64bd750 100644 (file)
@@ -1,8 +1,8 @@
 /* VMS::Stdio - VMS extensions to stdio routines 
  *
- * Version:  2.1
- * Author:   Charles Bailey  bailey@genetics.upenn.edu
- * Revised:  24-Mar-1998
+ * Version:  2.2
+ * Author:   Charles Bailey  bailey@newman.upenn.edu
+ * Revised:  18-Jul-1998
  *
  */
 
@@ -81,7 +81,7 @@ IV *pval;
 
 
 static SV *
-newFH(FILE *fp, char type) {
+newFH(PerlIO *fp, char type) {
     SV *rv;
     GV **stashp, *gv = (GV *)NEWSV(0,0);
     HV *stash;
@@ -92,11 +92,11 @@ newFH(FILE *fp, char type) {
      * 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(defstash,"VMS::",5,TRUE);
-    if (!stashp || *stashp == (GV *)&sv_undef) return Nullsv;
+    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 *)&sv_undef) return Nullsv;
+    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 */
@@ -121,38 +121,99 @@ constant(name)
        if (constant(name, &i))
            ST(0) = sv_2mortal(newSViv(i));
        else
-           ST(0) = &sv_undef;
+           ST(0) = &PL_sv_undef;
+
+void
+binmode(fh)
+       SV *    fh
+       PROTOTYPE: $
+       CODE:
+           SV *name;
+          IO *io;
+          char iotype;
+          char filespec[NAM$C_MAXRSS], *acmode, *s, *colon, *dirend = Nullch;
+          int ret = 0, saverrno = errno, savevmserrno = vaxc$errno;
+           SV pos;
+           PerlIO *fp;
+          io = sv_2io(fh);
+           fp = io ? IoOFP(io) : NULL;
+          iotype = io ? IoTYPE(io) : '\0';
+           if (fp == NULL || strchr(">was+-|",iotype) == Nullch) {
+             set_errno(EBADF); set_vaxc_errno(SS$_IVCHAN); XSRETURN_UNDEF;
+           }
+           if (!PerlIO_getname(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 = PerlIO_getpos(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 = PerlIO_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+";
+           }
+           /* appearances to the contrary, this is an freopen substitute */
+           name = sv_2mortal(newSVpvn(filespec,strlen(filespec)));
+           if (PerlIO_openn(Nullch,acmode,-1,0,0,fp,1,&name) == Nullfp) XSRETURN_UNDEF;
+           if (iotype != '-' && ret != -1 && PerlIO_setpos(fp,&pos) == -1) XSRETURN_UNDEF;
+           if (ret == -1) { set_errno(saverrno); set_vaxc_errno(savevmserrno); }
+           XSRETURN_YES;
+
 
 void
 flush(fp)
-       FILE *  fp
+       PerlIO * fp
        PROTOTYPE: $
        CODE:
-           if (fflush(fp)) { ST(0) = &sv_undef; }
-           else            { clearerr(fp); ST(0) = &sv_yes; }
+           FILE *stdio = PerlIO_exportFILE(fp,0);
+           if (fflush(stdio)) { ST(0) = &PL_sv_undef; }
+           else            { clearerr(stdio); ST(0) = &PL_sv_yes; }
+           PerlIO_releaseFILE(fp,stdio);
 
 char *
 getname(fp)
-       FILE *  fp
+       PerlIO * fp
        PROTOTYPE: $
        CODE:
            char fname[NAM$C_MAXRSS+1];
            ST(0) = sv_newmortal();
-           if (fgetname(fp,fname) != NULL) sv_setpv(ST(0),fname);
+           if (PerlIO_getname(fp,fname) != NULL) sv_setpv(ST(0),fname);
 
 void
 rewind(fp)
-       FILE *  fp
+       PerlIO * fp
        PROTOTYPE: $
        CODE:
-           ST(0) = rewind(fp) ? &sv_undef : &sv_yes;
+           FILE *stdio = PerlIO_exportFILE(fp,0);
+           ST(0) = rewind(stdio) ? &PL_sv_undef : &PL_sv_yes;
+           PerlIO_releaseFILE(fp,stdio);
 
 void
 remove(name)
        char *name
        PROTOTYPE: $
        CODE:
-           ST(0) = remove(name) ? &sv_undef : &sv_yes;
+           ST(0) = remove(name) ? &PL_sv_undef : &PL_sv_yes;
 
 void
 setdef(...)
@@ -163,16 +224,17 @@ setdef(...)
            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) = &sv_undef;
+               ST(0) = &PL_sv_undef;
                if (!SvPOK(defsv)) { SETERRNO(EINVAL,LIB$_INVARG); XSRETURN(1); }
-               if (tovmsspec(SvPV(defsv,na),vmsdef) == NULL) { 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) = &sv_undef;
+               EXTEND(sp,1);  ST(0) = &PL_sv_undef;
            }
            defnam.nam$l_esa = es;  defnam.nam$b_ess = sizeof es;
            deffab.fab$l_nam = &defnam;
@@ -203,17 +265,19 @@ setdef(...)
            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) = &sv_yes;
+           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
+       PerlIO * fp
        PROTOTYPE: $
        CODE:
-           if (fsync(fileno(fp))) { ST(0) = &sv_undef; }
-           else                   { clearerr(fp); ST(0) = &sv_yes; }
+           FILE *stdio = PerlIO_exportFILE(fp,0);
+           if (fsync(fileno(stdio))) { ST(0) = &PL_sv_undef; }
+           else                   { clearerr(stdio); ST(0) = &PL_sv_yes; }
+           PerlIO_releaseFILE(fp,stdio);
 
 char *
 tmpnam()
@@ -231,6 +295,9 @@ vmsopen(spec,...)
            char *args[8],mode[3] = {'r','\0','\0'}, type = '<';
            register int i, myargc;
            FILE *fp;
+            SV *fh;
+           PerlIO *pio_fp;
+           STRLEN n_a;
        
            if (!spec || !*spec) {
               SETERRNO(EINVAL,LIB$_INVARG);
@@ -249,7 +316,7 @@ vmsopen(spec,...)
            }
            else if (*spec == '<') spec++;
            myargc = items - 1;
-           for (i = 0; i < myargc; i++) args[i] = SvPV(ST(i+1),na);
+           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:
@@ -280,11 +347,12 @@ vmsopen(spec,...)
                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) : &sv_undef);
+           if (fp != Null(FILE*)) {
+             pio_fp = PerlIO_importFILE(fp,0);
+             fh = newFH(pio_fp,(mode[1] ? '+' : (mode[0] == 'r' ? '<' : (mode[0] == 'a' ? 'a' : '>'))));
+            ST(0) = (fh ? sv_2mortal(fh) : &PL_sv_undef);
            }
-           else { ST(0) = &sv_undef; }
+           else { ST(0) = &PL_sv_undef; }
 
 void
 vmssysopen(spec,mode,perm,...)
@@ -296,14 +364,16 @@ vmssysopen(spec,mode,perm,...)
            char *args[8];
            int i, myargc, fd;
            FILE *fp;
+           PerlIO *pio_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),na);
+           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) {
@@ -337,18 +407,21 @@ vmssysopen(spec,mode,perm,...)
            }
            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) : &sv_undef);
+              ((fp = fdopen(fd, &("r\000w\000r+"[2*i]))) != Null(FILE*))) {
+             pio_fp = PerlIO_importFILE(fp,0);
+             fh = newFH(pio_fp,"<>++"[i]);
+            ST(0) = (fh ? sv_2mortal(fh) : &PL_sv_undef);
            }
-           else { ST(0) = &sv_undef; }
+           else { ST(0) = &PL_sv_undef; }
 
 void
 waitfh(fp)
-       FILE *  fp
+       PerlIO * fp
        PROTOTYPE: $
        CODE:
-           ST(0) = fwait(fp) ? &sv_undef : &sv_yes;
+           FILE *stdio = PerlIO_exportFILE(fp,0);
+           ST(0) = fwait(stdio) ? &PL_sv_undef : &PL_sv_yes;
+           PerlIO_releaseFILE(fp,stdio);
 
 void
 writeof(mysv)
@@ -359,12 +432,11 @@ writeof(mysv)
            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;
+           PerlIO *fp = io ? IoOFP(io) : NULL;
            if (fp == NULL || strchr(">was+-|",IoTYPE(io)) == Nullch) {
-             set_errno(EBADF); set_vaxc_errno(SS$_IVCHAN);
-             ST(0) = &sv_undef;  XSRETURN(1);
+             set_errno(EBADF); set_vaxc_errno(SS$_IVCHAN); XSRETURN_UNDEF;
            }
-           if (fgetname(fp,devnam) == Nullch) { ST(0) = &sv_undef; XSRETURN(1); }
+           if (PerlIO_getname(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);
@@ -372,7 +444,7 @@ writeof(mysv)
            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) = &sv_yes; }
+           if (retsts & 1) { ST(0) = &PL_sv_yes; }
            else {
              set_vaxc_errno(retsts);
              switch (retsts) {
@@ -387,5 +459,5 @@ writeof(mysv)
                default:  /* Includes "shouldn't happen" cases that might map */
                  set_errno(EVMSERR); break;         /* to other errno values */
              }
-             ST(0) = &sv_undef;
+             ST(0) = &PL_sv_undef;
            }