home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 421.lha / BTNtape_v1.0 / tapemon / tapemon.c < prev    next >
C/C++ Source or Header  |  1990-09-29  |  4KB  |  120 lines

  1. /**
  2.  *  TapeMon:  a monitor program for the BTNtape handler.
  3.  */
  4. #define MVERSION  "TapeMon V1.0"
  5. /*
  6.  *  (c) Copyright 1990  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 main(int argc, char *argv[])
  37. {
  38.  struct Task    *tapeproc;
  39.  struct tplink  *linktp;
  40.  char *tapename;
  41.  ULONG x;
  42.  short run = 1;
  43.  BYTE  signum;
  44.  BYTE  oldpri;
  45.  
  46.    printf("*** BTN-Tape Handler & Monitor ***\n© Copyright 1990 Robert Rethemeyer\n");
  47.  
  48.    if(argc > 1) tapename = argv[1];
  49.    else         tapename = "TAPE";
  50.    if (!(tapeproc = FindTask(tapename)))  {
  51.          printf("Process %s not found...\n (be sure to use caps, no ':')\n",tapename);
  52.          exit(30);
  53.    }
  54.    if (!(linktp = (struct tplink *) tapeproc->tc_UserData))  {
  55.          printf("No linkage structure...\n wrong process, or\n handler not active yet?\n");
  56.          exit(31);
  57.    }
  58.    printf("%s: Proc %X DevNode %X\n",tapename,tapeproc,linktp->devnode);
  59.  
  60.    if (strcmp(linktp->keyword,"TapeHandler"))  {
  61.          printf("Process %s doesn't look like BTN-Tape\n",tapename);
  62.          exit(32);
  63.    }
  64.  
  65.    if (linktp->montask)  {
  66.          printf("There is already a TapeMon running\n");
  67.          exit(33);
  68.    }
  69.  
  70.    if (!linktp->handsig) {
  71.           printf("Handler has no signal\n");
  72.           exit(34);
  73.    }
  74.  
  75.    signum = AllocSignal(-1);
  76.    if (signum == -1)  {
  77.          printf("Unable to allocate signal for TapeMon\n");
  78.          exit(35);
  79.    }
  80.    linktp->monsig  = 1UL << signum;      /* give mask to tape handler */
  81.    linktp->montask = FindTask(0);        /* tell it where I am also   */
  82.    printf("%s Unit %X\n",linktp->driver,*(linktp->unit));
  83.    if(linktp->sense)
  84.        printf("Last sense= %X,%02X\n",linktp->sense,linktp->xsense);
  85.    printf("Use ^C to terminate TapeMon\n%s\n",linktp->version);
  86.  
  87.    /* Set the monitor priority to the same as that of the handler */
  88.    oldpri = SetTaskPri (linktp->montask, (long)tapeproc->tc_Node.ln_Pri);
  89.  
  90.    /* The main loop.  Wait for a signal from the handler, then print the
  91.       contents of dbb.  Some extra handshaking must then be done to sync
  92.       the two processes and prevent races.  If a control-C occurs during
  93.       a Wait, then finish whatever was in progress before ending the loop.
  94.    */
  95.  
  96.    while(run)  {
  97.      x=Wait(linktp->monsig | SIGBREAKF_CTRL_C);   /* Wait for req from hndlr */
  98.      if  (x & SIGBREAKF_CTRL_C) run = 0;
  99.      if(!(x & linktp->monsig) ) continue;
  100.  
  101.      printf(" %s",linktp->dbb);                   /* print handler's message */
  102.      Signal(tapeproc, linktp->handsig);           /* tell it we are 'done'   */
  103.  
  104.      x=Wait(linktp->monsig | SIGBREAKF_CTRL_C);   /* Wait for ack of 'done'  */
  105.      if  (x & SIGBREAKF_CTRL_C) run = 0;
  106.      if(!(x & linktp->monsig) ) Wait(linktp->monsig);
  107.  
  108.      Signal(tapeproc, linktp->handsig);           /* Ack his ack */
  109.    }
  110.  
  111.    /* Clean up and exit */
  112.  
  113.    SetTaskPri(linktp->montask, (long) oldpri);   /* restore priority */
  114.    linktp->montask = NULL;                      /* break link with handler */
  115.    linktp->monsig  = 0;
  116.    FreeSignal((ULONG)signum);
  117.    printf("TapeMon terminated\n");
  118.    exit(0);
  119. }
  120.