home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d558 / btntape.lha / BTNtape / src / tapemon.c < prev    next >
C/C++ Source or Header  |  1991-10-28  |  5KB  |  175 lines

  1. /**
  2.  *  TapeMon:  the monitor program for the BTNtape handler.
  3.  */
  4. #define VERSTAG "$VER: TapeMon/RAR 2.1 "
  5. /*
  6.  *  (c) Copyright 1990, 1991  Robert Rethemeyer
  7.  *   This software may be freely distributed and redistributed,
  8.  *   for non-commercial purposes, provided this notice is included.
  9.  *------------------------------------------------------------
  10.  *   Run TapeMon from a CLI to receive informational messages
  11.  *   from the BTN-Tape handler process.  To terminate the
  12.  *   monitor program, break the CLI using control-C or BREAK.
  13.  */
  14. #include <exec/types.h>
  15. #include <exec/nodes.h>
  16. #include <exec/lists.h>
  17. #include <exec/ports.h>
  18. #include <exec/tasks.h>
  19. #include <exec/execbase.h>
  20. #include <libraries/dos.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "/tplink.h"
  25.  
  26. #if defined AZTEC_C
  27.   #include <functions.h>
  28. #elif defined LATTICE
  29.   #include <proto/exec.h>
  30.   #include <proto/dos.h>
  31. #endif
  32.  
  33. extern struct DosLibrary *DOSBase;
  34.  
  35. void main(int,char *[]);
  36. void toUPPER(char *);
  37.  
  38. void main(int argc, char *argv[])
  39. {
  40.  struct Task    *tapeproc;
  41.  struct Task    *monproc;
  42.  struct tplink  *linktp;
  43.  char *tapename;
  44.  char *xx;
  45.  char *ver;
  46.  ULONG x;
  47.  int   run = 1;
  48.  int   fullsense = 0;
  49.  int   ii;
  50.  BYTE  signum;
  51.  BYTE  oldpri;
  52.  
  53.    ver = VERSTAG "(" __DATE__ ")";
  54.    printf("*** BTN-Tape Handler & Monitor ***\n"
  55.           "(c) Copyright 1990,1991 R. Rethemeyer\n");
  56.  
  57.    if(argc > 1) {
  58.       tapename = argv[1];
  59.       toUPPER(tapename);
  60.       xx = &tapename[strlen(tapename)-1];
  61.       if(*xx == ':') *xx = '\0';
  62.    }
  63.    else tapename = "TAPE";
  64.  
  65.    if(argc > 2) fullsense = 1;
  66.  
  67.    if (!(tapeproc = FindTask(tapename)))  {
  68.          printf("Process %s not found.\n",tapename);
  69.          exit(30);
  70.    }
  71.    if (!(linktp = (struct tplink *) tapeproc->tc_UserData))  {
  72.          printf("No linkage structure...\n wrong process, or\n handler not active yet?\n");
  73.          exit(31);
  74.    }
  75.    printf("%s: Proc %X DevNode %X\n",tapename,tapeproc,linktp->devnode);
  76.  
  77.    if (strcmp(linktp->keyword,"TapeHandler"))  {
  78.          printf("Process %s doesn't look like BTN-Tape\n",tapename);
  79.          exit(32);
  80.    }
  81.  
  82.    if (linktp->montask)  {
  83.          printf("There is already a TapeMon running\n");
  84.          exit(33);
  85.    }
  86.  
  87.    if (!linktp->handsig) {
  88.           printf("Handler has no signal\n");
  89.           exit(34);
  90.    }
  91.  
  92.    signum = AllocSignal(-1);
  93.    if (signum == -1)  {
  94.          printf("Unable to allocate signal for TapeMon\n");
  95.          exit(35);
  96.    }
  97.    linktp->monsig  = 1UL << signum;      /* give mask to tape handler */
  98.    monproc = linktp->montask = FindTask(0);
  99.  
  100.    printf("%s  Unit-%X  LU-%X\n",linktp->driver,*(linktp->unit),
  101.                                                 *(linktp->Lun)>>5);
  102.  
  103.    if(linktp->inquiry[8] != '\0')  printf("Drive: %s\n",linktp->inquiry+8);
  104.    if(linktp->inquiry[0] == 0x01)
  105.         printf("Sequential Access\n");
  106.    else if (linktp->inquiry[0] == 0x10)
  107.         printf("Direct Access\n");
  108.    else if (linktp->inquiry[0] == 0x7f)
  109.         printf("BAD LOGICAL UNIT NUMBER\n");
  110.    else printf("Device type = %X\n",linktp->inquiry[0]);
  111.  
  112.    if(linktp->badparm) printf("ERROR(S) IN STARTUP\n");
  113.  
  114.    if(linktp->sense)
  115.        printf("Last sense= %s, other=%02X,%02X\n",linktp->sense,linktp->xsns1,
  116.          linktp->xsns2);
  117.    printf("Use ^C to terminate TapeMon\n-%s\n",linktp->version+6);
  118.  
  119.    /* Set the monitor priority to the same as that of the handler */
  120.    oldpri = SetTaskPri (monproc, (long)tapeproc->tc_Node.ln_Pri);
  121.  
  122.    /* The main loop.  Wait for a signal from the handler, then print the
  123.       contents of dbb.  Some extra handshaking must then be done to sync
  124.       the two processes and prevent races.  If a control-C occurs during
  125.       a Wait, then finish whatever was in progress before ending the loop.
  126.    */
  127.  
  128.    while(run)  {
  129.      x=Wait(linktp->monsig | SIGBREAKF_CTRL_C);   /* Wait for req from hndlr */
  130.      if  (x & SIGBREAKF_CTRL_C) run = 0;
  131.      if(!(x & linktp->monsig) ) {                 /* detect control-c */
  132.         linktp->montask = NULL;                   /* break link with handler */
  133.         if(SetSignal(0,0) & linktp->monsig) {     /* detect terminate race */
  134.             linktp->montask = monproc;
  135.             SetSignal(SIGBREAKF_CTRL_C,SIGBREAKF_CTRL_C);
  136.             run = 1;
  137.         }
  138.         continue;
  139.      }
  140.  
  141.      printf(" %s",linktp->dbb);                   /* print handler's message */
  142.      if(fullsense && linktp->sns) {               /* If sense info           */
  143.         for(ii=0; ii<32; ii++)                    /*   print all sense info  */
  144.             printf(" %02X",linktp->sns[ii]);
  145.         printf("\n");
  146.      }
  147.  
  148.      Signal(tapeproc, linktp->handsig);           /* tell it we are 'done' */
  149.  
  150.      x=Wait(linktp->monsig | SIGBREAKF_CTRL_C);   /* Wait for ack of 'done' */
  151.      if  (x & SIGBREAKF_CTRL_C) run = 0;
  152.      if(!(x & linktp->monsig) ) Wait(linktp->monsig);
  153.  
  154.      if(!run) linktp->montask = NULL;
  155.      Signal(tapeproc, linktp->handsig);           /* Ack his ack */
  156.    }
  157.  
  158.    /* Clean up and exit */
  159.  
  160.    SetTaskPri(monproc, (long) oldpri);          /* restore priority */
  161.    linktp->monsig  = 0;
  162.    FreeSignal((ULONG)signum);
  163.    printf("TapeMon terminated\n");
  164.    exit(0);
  165. }
  166.  
  167. /**************************************************************************/
  168. /* toUPPER: convert string to upper case */
  169.  
  170. void toUPPER(char *zz)
  171. {
  172.   for(; *zz != '\0'; zz++)  if(*zz >= 'a') *zz -= 0x20;
  173.   return;
  174. }
  175.