home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / utilities / safemem2.lha / SafeMem_parallel.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-05  |  2.9 KB  |  144 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <Exec/ports.h>
  4. #include <devices/parallel.h>
  5. #include <proto/exec.h>
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9.  
  10. #define   SAFEMEM
  11. #include "safemem.h"
  12. #include "safemem_parallel.h"
  13.  
  14. /*
  15.  * Structures required to talk to the parallel device
  16.  */
  17. static struct MsgPort  *st_ParallelMP;
  18. static struct IOExtPar *st_ParallelIO;    
  19. static ULONG            st_ParallelOpen = 0;
  20.  
  21. /*
  22.  * FUNCTION:
  23.  *   ULONG OpenSafeMemParallel()
  24.  *
  25.  * DESCRIPTION:
  26.  *     Opens the parallel device for use by the SafeMem functions. It is opened
  27.  *   in shared mode, so multiple safemem tasks can exist simulataneously, as
  28.  *   well as Enforcer and MungWall, etc.
  29.  *     If gbl_SafeMem == 0, this function has no effect.
  30.  *     This function changes gbl_SafeMemOutput.
  31.  *
  32.  * RETURNS:
  33.  *   SUCCESS: 1
  34.  *   FAILURE: 0
  35.  *
  36.  * WRITTEN:
  37.  *   Saturday 05-Dec-92 16:28:
  38.  */
  39. ULONG OpenSafeMemParallel()
  40. {
  41.   if(gbl_SafeMem)
  42.   {
  43.     st_ParallelMP=CreatePort(0,0);
  44.     st_ParallelIO=(struct IOExtPar *)CreateExtIO(st_ParallelMP,sizeof(struct IOExtPar));
  45.     st_ParallelIO->io_ParFlags=PARF_SHARED;
  46.  
  47.     if(OpenDevice("parallel.device", 0, (struct IOStdReq *)st_ParallelIO,0))
  48.     {
  49.       CloseSafeMemParallel();
  50.       return(0);
  51.     }
  52.  
  53.     st_ParallelOpen = 1;
  54.     gbl_SafeMemOutput = WriteSafeMemParallel;
  55.   }
  56.  
  57.   return(1);
  58. }
  59.  
  60. /*
  61.  * FUNCTION:
  62.  *   void CloseSafeMemParallel()
  63.  *
  64.  * DESCRIPTION:
  65.  *   Closes the parallel port. gbl_SafeMemOutput now reverts to _writes().
  66.  *
  67.  * WRITTEN:
  68.  *   Saturday 05-Dec-92 16:30:
  69.  */
  70. void CloseSafeMemParallel()
  71. {
  72.   if(st_ParallelOpen)
  73.   {
  74.     CloseDevice((struct IOStdReq *)st_ParallelIO);
  75.     st_ParallelOpen = 0;
  76.   }
  77.   
  78.   if(st_ParallelIO)
  79.   {
  80.     DeleteExtIO((struct IOExtReq *)st_ParallelIO);
  81.     st_ParallelIO = 0;
  82.   }
  83.   
  84.   if(st_ParallelMP)
  85.   {
  86.     DeletePort(st_ParallelMP);
  87.     st_ParallelMP = 0;
  88.   }
  89.   
  90.   if(gbl_SafeMem)
  91.     gbl_SafeMemOutput = (void (*)())_writes;
  92.  
  93.   return;
  94. }
  95.  
  96. /*
  97.  * FUNCTION:
  98.  *   void WriteSafeMemParallel(char *data)
  99.  *
  100.  * DESCRIPTION:
  101.  *     Puts text to the parallel port. Assumes the parallel port has been
  102.  *   opened with OpenSafeMemParallel().
  103.  *
  104.  * ARGUMENTS:
  105.  *   char *data - The text to put
  106.  *
  107.  * WRITTEN:
  108.  *   Saturday 05-Dec-92 16:32:
  109.  */
  110. void WriteSafeMemParallel(char *data)
  111. {
  112.   struct MessagePort *mp;
  113.   struct IOExtPar     req;
  114.  
  115.   if(st_ParallelOpen)
  116.   {
  117.    /*
  118.     * Create our own IO block & port (since we mightn't be the same task
  119.     * that opened the parallel port in the first place).
  120.     */
  121.     req = *st_ParallelIO;
  122.     mp  = CreatePort(0,0);
  123.  
  124.    /*
  125.     * Feed the data for a write
  126.     */
  127.     req.IOPar.io_Command  = CMD_WRITE;
  128.     req.IOPar.io_Length   = -1;
  129.     req.IOPar.io_Data     = (APTR)data;
  130.     req.IOPar.io_Message.mn_ReplyPort = mp;
  131.  
  132.    /*
  133.     * Execute the write & wait for it to finish
  134.     */
  135.     DoIO((struct IOStdReq *)&req);         
  136.  
  137.    /*
  138.     * Free the port & that's it.
  139.     */
  140.     DeletePort(mp);
  141.   }
  142. }
  143.  
  144.