home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / useful / disk / misc / btntape / src / tapemon.c < prev    next >
C/C++ Source or Header  |  1994-04-03  |  6KB  |  193 lines

  1. /**
  2.  *  TapeMon:  a monitor program for the BTNtape handler.
  3.  *        Version 3.0   3/14/94
  4.  */
  5. #define VERSTAG "$VER: TapeMon/RAR 3.0 "
  6. /*
  7.  *  (c) Copyright 1990, 1994  Robert Rethemeyer
  8.  *   This software may be freely distributed and redistributed,
  9.  *   for non-commercial purposes, provided this notice is included.
  10.  *------------------------------------------------------------
  11.  *   Run TapeMon from a CLI to receive informational messages
  12.  *   from the BTN-Tape handler process.  To terminate the
  13.  *   monitor program, break the CLI using control-C or BREAK.
  14.  *
  15.  *   Compile with SAS/C v6.  To compile TapeMon, use "smake tapemon"
  16.  */
  17. #define _STRICT_ANSI
  18. #include <exec/types.h>
  19. #include <exec/nodes.h>
  20. #include <exec/lists.h>
  21. #include <exec/ports.h>
  22. #include <exec/tasks.h>
  23. #include <exec/execbase.h>
  24. #include <libraries/dos.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "tplink.h"
  29.  
  30. #include <proto/exec.h>
  31. #include <proto/dos.h>
  32. #include <pragmas/exec_pragmas.h>
  33. #include <pragmas/dos_pragmas.h>
  34.  
  35. extern struct DosLibrary *DOSBase;
  36.  
  37. void main(int,char *[]);
  38. void toUPPER(char *);
  39.  
  40. void main(int argc, char *argv[])
  41. {
  42.  struct Task    *tapeproc;
  43.  struct Task    *monproc;
  44.  struct MsgPort *monport;
  45.  struct MsgPort *tapeport;
  46.  struct Message *tmsg;
  47. #define BTNM ((struct BTNmsg*)tmsg)
  48.  static struct BTNmsg  mmsg;
  49.  struct tplink  *linktp;
  50.  char tn[32] = "BTN_";
  51.  char mn[32] = "TMON_";
  52.  UBYTE snsdata[64];
  53.  UBYTE *snsflag;
  54.  char *tapename;
  55.  char *monname;
  56.  char *xx;
  57.  char *ver;
  58.  ULONG x;
  59.  int   run = 1;
  60.  BOOL  fullsense = 0;
  61.  int   ii;
  62.  ULONG monsig;
  63.  BYTE  oldpri;
  64.  char  dbbcopy[80];
  65.  
  66.    ver = VERSTAG "(" __DATE__ ")";
  67.    monproc = FindTask(NULL);
  68.  
  69.    if(argc > 1) {
  70.       toUPPER(argv[1]);
  71.       xx = argv[1] + strlen(argv[1])-1;
  72.       if(*xx == ':') *xx = '\0';
  73.       tapename = strcat(tn,argv[1]);
  74.       monname  = strcat(mn,argv[1]);
  75.    }
  76.    else { tapename = "BTN_TAPE";  monname = "TMON_TAPE"; }
  77.  
  78.    if(argc > 2) fullsense = 1;
  79.  
  80.    monport = CreatePort(monname,0);
  81.    if(!monport)  {
  82.          printf("Unable to create tapemon port\n");
  83.          exit(34);
  84.    }
  85.    monsig = 1 << monport->mp_SigBit;
  86.  
  87.    Forbid();
  88.    tapeport = FindPort(tapename);
  89.    Permit();
  90.    if (!tapeport) {  /* tapemon may start before the handler */
  91.          printf("Waiting for %s: to mount (^C to quit)...\n",tapename+4);
  92.          x = Wait(monsig | SIGBREAKF_CTRL_C); /* Wait for wakeup from hndlr */
  93.          if(x & monsig) {
  94.              tmsg = GetMsg(monport);
  95.              tapeport = tmsg->mn_ReplyPort;
  96.              ReplyMsg(tmsg);
  97.          }
  98.          if(x & SIGBREAKF_CTRL_C) {
  99.              DeletePort(monport);
  100.              exit(0);
  101.          }
  102.    }
  103.    tapeproc = tapeport->mp_SigTask;
  104.  
  105.    /* let the handler know we are here */
  106.    mmsg.bmsg.mn_ReplyPort = monport;
  107.    mmsg.mptr = monport;
  108.    PutMsg(tapeport,&mmsg.bmsg);
  109.    WaitPort(monport);       /* wait for reply */
  110.    tmsg = GetMsg(monport);  /* which contains link to tplink block */
  111.    if(!(linktp = BTNM->mptr)) {
  112.          printf("Port %s refused connection...\n  TapeMon already running?\n",
  113.                  tapename);
  114.          DeletePort(monport);
  115.          exit(35);
  116.    }
  117.  
  118.    /* print informational items */
  119.    printf("*** BTN-Tape Handler & Monitor ***\n%s\n",linktp->cpyw);
  120.    printf("%s: Proc %X DevNode %X\n",tapeproc->tc_Node.ln_Name,
  121.                                            tapeproc,linktp->devnode);
  122.    printf("%s  Unit-%X  LU-%X\n",linktp->driver,*(linktp->unit),
  123.                                                 *(linktp->Lun)>>5);
  124.    if(linktp->inquiry[8] != '\0')
  125.         printf("Drive: %s\n",linktp->inquiry+8);
  126.    if(linktp->inquiry[0] == 0x01)
  127.         printf("Sequential Access\n");
  128.    else if (linktp->inquiry[0] == 0x10)
  129.         printf("Direct Access\n");
  130.    else if (linktp->inquiry[0] == 0x7f)
  131.         printf("BAD LOGICAL UNIT NUMBER\n");
  132.    else printf("Device type = %X\n",linktp->inquiry[0]);
  133.  
  134.    if(linktp->badparm) printf("ERROR(S) IN STARTUP\n");
  135.  
  136.    if(linktp->sense)
  137.        printf("Last sense= %s, other=%02X,%02X\n",linktp->sense,linktp->xsns1,
  138.          linktp->xsns2);
  139.  
  140.    printf("Use ^C to terminate TapeMon\n-%s\n",linktp->version+6);
  141.  
  142.    /* Set the monitor priority to the same as that of the handler */
  143.    oldpri = SetTaskPri (monproc, (long)tapeproc->tc_Node.ln_Pri);
  144.  
  145.    /* The main loop.  Wait for a message from the handler, copy the print
  146.       string, reply the message, then print the string.  Escape on ctl-C.
  147.    */
  148.  
  149.    x=0;
  150.    for(;;) {
  151.      if(x & SIGBREAKF_CTRL_C) run = 0;
  152.      if(tmsg = GetMsg(monport)) {           /* check for a BTN message */
  153.         linktp = BTNM->mptr;                    /* get link to BTN struct */
  154.         strcpy(dbbcopy, linktp->dbb);           /* make a copy of string */
  155.         snsflag = linktp->sns;                  /* make a copy of sense data */
  156.         if(fullsense)  if(snsflag)
  157.             memcpy(snsdata,snsflag,64);
  158.         ReplyMsg(tmsg);                         /* release handler */
  159.         printf(" %s",dbbcopy);                  /* print handler's message */
  160.         if(fullsense && snsflag) {              /* print extra sense info */
  161.            for(ii=0; ii<32; ii++)
  162.                printf(" %02X",snsdata[ii]);
  163.            printf("\n");
  164.         }
  165.      }
  166.      else {
  167.         if(run) x = Wait(monsig | SIGBREAKF_CTRL_C);  /* Wait for msg/ctl-c */
  168.         else break;
  169.      }
  170.    }
  171.  
  172.    /* tell handler we are going bye-bye */
  173.    mmsg.mptr = NULL;
  174.    PutMsg(tapeport,&mmsg.bmsg);
  175.    WaitPort(monport);
  176.  
  177.    /* Clean up and exit */
  178.    SetTaskPri(monproc, (long) oldpri);          /* restore priority */
  179.    DeletePort(monport);
  180.    printf("TapeMon terminated\n");
  181.    exit(0);
  182. }
  183.  
  184. /**************************************************************************/
  185. /* toUPPER: convert string to upper case */
  186.  
  187. void toUPPER(char *zz)
  188. {
  189.   for(; *zz != '\0'; zz++)  if(*zz >= 'a') *zz -= 0x20;
  190.   return;
  191. }
  192.  
  193.