home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / useful / disk / misc / btntape / src / btn.c next >
C/C++ Source or Header  |  1994-04-03  |  38KB  |  1,000 lines

  1. /****     BTNtape Handler for SCSI tape drives        ****/
  2. /**** Author: Bob Rethemeyer  (drBob@cup.portal.com)  ****/
  3.  
  4. #define VERSDATE "(03.14.94)"  /* (DD.MM.YY) */
  5. #define VERSNAME "BTNtape-RAR 3.0 "
  6. /* (If you recompile BTN, ^^^ put your initials here!) */
  7.  
  8. /*--------------------------------------------------------------------------
  9.  *  (c) Copyright 1990,1994  Robert Rethemeyer.
  10.  *  This software may be freely distributed and redistributed,
  11.  *  for non-commercial purposes, provided this notice is included.
  12.  *--------------------------------------------------------------------------
  13.  *  BTNtape is an AmigaDOS device handler to make a simple DOS TAPE: device.
  14.  *  It converts DOS packets for the device into I/O requests to a
  15.  *  "SCSI-direct" compatible device driver.  It is based on "my.handler"
  16.  *  by Phillip Lindsay and a SCSI-direct program by Robert Mitchell.
  17.  *  Seek() handling derived from code by Dennis J. Brueni, 8-24-91.
  18.  *  Source is ANSI C compliant.  Compile with SAS/C v6.
  19.  *
  20.  *  This handler works in conjunction with the accompanying TapeMon program.
  21.  *--------------------------------------------------------------------------
  22.  */
  23. #define _STRICT_ANSI
  24. #include <exec/types.h>
  25. #include <exec/nodes.h>
  26. #include <exec/lists.h>
  27. #include <exec/ports.h>
  28. #include <exec/tasks.h>
  29. #include <exec/libraries.h>
  30. #include <exec/io.h>
  31. #include <exec/memory.h>
  32. #include <devices/scsidisk.h>
  33. #include <intuition/intuition.h>
  34. #include <libraries/dos.h>
  35. #include <libraries/dosextens.h>
  36. #include <libraries/filehandler.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <stdlib.h>
  40. #include <stdarg.h>
  41. #include "btn.h"
  42. #include "tplink.h"
  43.  
  44. #include <proto/exec.h>
  45. #include <proto/intuition.h>
  46. #include <pragmas/exec_pragmas.h>
  47. #include <pragmas/intuition_pragmas.h>
  48.  
  49. /* sense keys of interest */
  50. #define NOS 0x00  /* no sense */
  51. #define RCV 0x01  /* recovered error */
  52. #define ILL 0x05  /* illegal request */
  53. #define UAT 0x06  /* unit attention */
  54. #define BLC 0x08  /* blank check */
  55. #define VOF 0x0d  /* volume overflow */
  56. /* pseudo sense keys returned by DoSense */
  57. #define FMK 0x10  /* filemark */
  58. #define EOM 0x11  /* end of tape */
  59. #define ILI 0x12  /* incorr leng */
  60. #define SERR 0x13 /* SCSI error  */
  61.  
  62. /*======== Global data */
  63.  
  64. struct ExecBase *SysBase;
  65. struct IntuitionBase *IntuitionBase=NULL;
  66. UBYTE           *cdb;          /* pointer to SCSI command buffer */
  67. UBYTE           *sns;          /* pointer to sense data buffer   */
  68. UBYTE           *inq;          /* pointer to inquiry data        */
  69. struct SCSICmd  *cmd;          /* pointer to scsidirect command  */
  70. struct IOStdReq *ior;          /* pointer to io request structure*/
  71. UBYTE *TapeBuff[2]={NULL,NULL};/* pointers to 2 tape buffers     */
  72. struct Process  *myproc;       /* ptr to handler's process struct*/
  73. struct DosPacket *mypkt;       /* ptr to dos packet sent         */
  74. struct tplink   *linktp;       /* pointer to link structure      */
  75. struct MsgPort  *monport=NULL; /* ptr to mon communication msg port */
  76. struct MsgPort  *btnport;      /* ptr to btn communication msg port */
  77. struct BTNmsg    monmsg;       /* message we will send to tapemon*/
  78. ULONG  blknum;                 /* block number for io operation  */
  79. ULONG  blksize = 512;          /* bytes per tape block           */
  80. ULONG  numblks = 1;            /* number of blocks per io oper   */
  81. ULONG  TBSize;                 /* bytes in a tape buffer         */
  82. ULONG  rwlen;                  /* bytes in a tape read/write     */
  83. ULONG  rdlen;                  /* bytes/blks in a read           */
  84. ULONG  expect;                 /* expect num of bytes transferred*/
  85. ULONG  bugmask = 0;            /* 2090A bug circumvention        */
  86. long   filenum = -1;           /* current file position on tape  */
  87. long   lastwrtn= -1;           /* last filenum written           */
  88. long   actual;                 /* actual num of bytes transferred*/
  89. long   res1;                   /* #bytes xfered packet return    */
  90. long   Boff;                   /* current offset in tape buffer  */
  91. long   tpsize ;                /* tape size in blocks            */
  92. long   reserved = 0;           /* number of reserved blks at BOT */
  93. int    tapenum;                /* tape volume number             */
  94. BOOL   inprog = FALSE;         /* io operation in progress flag  */
  95. BOOL   reten = FALSE;          /* retension-on-UAT flag          */
  96. BOOL   mbusy = FALSE;          /* waiting for reply from tapemon */
  97. BOOL   bswap = FALSE;          /* swap-bytes flag */
  98. BOOL   dbug = FALSE;           /* print debug info */
  99. char   *z;                     /* scratch */
  100. char   *devname, nbuf[32];     /* file name reference from Open()*/
  101. char   dbb[80];                /* buffer for monitor messages    */
  102. UBYTE  Lun = 0;                /* Logical Unit number << 5       */
  103. UBYTE  varblk = 0;             /* variable-block flag for SEQ    */
  104. UBYTE  fixedbit = 0;           /* fixed-block mode bit for SEQ   */
  105. UBYTE versiontag[] = "$VER: " VERSNAME VERSDATE;
  106. char  *cpywr = "(c) Copyright 1990-1994 R. Rethemeyer";
  107.  
  108. /***********************  Main program  ********************************/
  109. void _main(char *nu)
  110. {
  111.  struct SCSICmd scsicmd;  /* SCSIdirect command buffer */
  112.  UBYTE  snsarea[64];      /* sense data buffer */
  113.  UBYTE  cmdbuff[16];      /* SCSI command buffer */
  114.  UBYTE  inqdata[40];      /* inquiry data buffer */
  115.  struct tplink      tpl;       /* structure to link hndlr & mon     */
  116.  struct DeviceNode  *mynode;   /* ptr to devnode passed in pkt Arg3 */
  117.  struct MsgPort     *mp;       /* ptr to io message port            */
  118.  struct MsgPort     *pktport;  /* ptr to process message port       */
  119.  struct Message     *m;
  120.  struct BTNmsg      *b;
  121.  ULONG   dvnode;        /* ptr to devnode passed in pkt Arg3 */
  122.  ULONG   unit=99999;    /* device SCSI unit address          */
  123.  ULONG   bufmemtype=0;  /* type of mem for dynamic buffers   */
  124.  ULONG   gotopos=0;     /* position to open at (blk/filemark)*/
  125.  ULONG   fmarks=1;      /* # file marks for write/skip       */
  126.  ULONG   pktsig;        /* signal mask for process msg port  */
  127.  ULONG   btnsig=0;      /* signal mask for mon comm msg port */
  128.  char    *driver;       /* name of SCSI device driver        */
  129.  UBYTE   *dptr;         /* ptr to next byte in dos buffer    */
  130.  long    dflags=0;      /* device flags                      */
  131.  long    iostat;        /* status of previous read           */
  132.  long    dcnt;          /* count of dos packet bytes to move */
  133.  long    mcnt;          /* count of bytes to move            */
  134.  long    rcnt;          /* count of bytes moved for read     */
  135.  long    current;       /* current byte position in file     */
  136.  long    remain;        /* bytes remaining in tape buffer    */
  137.  long    pkcnt;         /* flag to indicate data written     */
  138.  long    x;
  139.  int     y=0;
  140.  int     opmode=0;      /* how has the handler been opened   */
  141.     #define CLOSED  0       /* not doing anything */
  142.     #define READING 1       /* reading tape */
  143.     #define WRITING 2       /* writing tape */
  144.     #define CURRPOS 3       /* read current file position */
  145.     #define RAWMODE 4       /* raw command mode */
  146.     #define UMODSEL 5       /* user mode select */
  147.  int     Bn;            /* current buffer number, 0 or 1 */
  148.  int     position;      /* how to position tape at Open  */
  149.     #define POSREW  0       /* rewind to beginning */
  150.     #define POSNREW 1       /* don't move          */
  151.     #define POSAPP  2       /* append to eod (seq) */
  152.     #define POSEND  3       /* append to eod (seq) */
  153.     #define POSSKIP 4       /* skip to file/block  */
  154.  int     nrflag=POSREW; /* rewind behavior of TAPE: */
  155.  BOOL    rewcl = FALSE; /* rewind-on-close flag */
  156.  BOOL    eject = FALSE; /* eject-on-close flag  */
  157.  BOOL    dirty = FALSE; /* buffer has unwritten data in it */
  158.  BOOL    atend;         /* flags filemark encountered */
  159.  BOOL    eot;           /* flags end-of-tape encountered */
  160.  BOOL    rderr;         /* flags error during read */
  161.  BOOL    errec = FALSE; /* attempt error recovery flag */
  162.  BOOL    aonly = FALSE; /* append-only flag */
  163.  BOOL    rdonly =FALSE; /* read-only flag */
  164.  BOOL    starting=FALSE;/* flag detects first open message */
  165.  BOOL    dos2=FALSE;    /* indicates i