home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / dirs / dcmd_408.lzh / DCmd / dcmd-handler.c < prev    next >
C/C++ Source or Header  |  1990-11-21  |  3KB  |  127 lines

  1.  
  2. /*
  3.  *  DCMD-HANDLER.C
  4.  *
  5.  *  DICE COMPILATION
  6.  */
  7.  
  8. #include "defs.h"
  9.  
  10. void
  11. _main()
  12. {
  13.     XMsgPort *port;
  14.     MsgPort *privatePort;
  15.     long fh;
  16.  
  17.     {
  18.     Process *proc = FindTask(NULL);
  19.     port = FindPort(proc->pr_Task.tc_Node.ln_Name);
  20.     }
  21.     if (port == NULL)       /*  fall through    */
  22.     return;
  23.     privatePort = CreatePort(NULL, 0);
  24.  
  25.     /*
  26.      *    The RAM disk has the unique ability that when writing to a file
  27.      *    with a shared lock (1005), another process can load the file at
  28.      *    any time getting the most up to date information.
  29.      */
  30.  
  31.     if ((fh = Open(port->FileName, 1005)) == NULL) {
  32.     if (fh = Open(port->FileName, 1006)) {
  33.         Close(fh);
  34.         fh = Open(port->FileName, 1005);
  35.     }
  36.     }
  37.  
  38.     if (fh) {
  39.     Seek(fh, 0L, 1);
  40.     Write(fh, "HANDLER-RUNNING\n", 16);
  41.     }
  42.  
  43.     /*
  44.      *    set-up
  45.      */
  46.  
  47.     {
  48.     Process *proc = port->Proc;    /*  CLI process     */
  49.  
  50.     port->Port.mp_Flags = PA_SIGNAL;
  51.     port->Port.mp_SigBit= SIGBREAKB_CTRL_E;
  52.     port->Port.mp_SigTask = FindTask(NULL);
  53.     }
  54.  
  55.     for (;;) {
  56.     Message *msg = GetMsg(port);
  57.     DosPacket *packet;
  58.  
  59.     if (msg == NULL) {
  60.         long mask = Wait((1 << port->Port.mp_SigBit) | SIGBREAKF_CTRL_F | SIGBREAKF_CTRL_D);
  61.         if (mask & SIGBREAKF_CTRL_F)
  62.         break;
  63.         if (mask & SIGBREAKF_CTRL_D) {      /*  close-re-open   */
  64.         if (fh)
  65.             Close(fh);
  66.         if (fh = Open(port->FileName, 1005))
  67.             Seek(fh, 0L, 1);
  68.         continue;
  69.         }
  70.         continue;
  71.     }
  72.     packet = (DosPacket *)msg->mn_Node.ln_Name;
  73.  
  74.     switch(packet->dp_Type) {
  75.     case ACTION_WRITE:
  76.         if (fh)
  77.         Write(fh, packet->dp_Arg2, packet->dp_Arg3);
  78.         break;
  79.     case ACTION_READ:    /*  re-arrange return path  */
  80.         {
  81.         MsgPort *replyPort = packet->dp_Port;    /*  return port */
  82.  
  83.         packet->dp_Port = privatePort;        /*  redirect    */
  84.         PutMsg(port->OldConsoleTask, msg);
  85.         WaitPort(privatePort);
  86.         GetMsg(privatePort);
  87.         if (packet->dp_Res2 == 0 && packet->dp_Res1 > 0) {
  88.             if (fh)
  89.             Write(fh, packet->dp_Arg2, packet->dp_Res1);
  90.         }
  91.         PutMsg(replyPort, packet->dp_Link);     /*  -> original */
  92.         packet = NULL;
  93.         }
  94.         break;
  95.     }
  96.     if (packet)
  97.         PutMsg(port->OldConsoleTask, msg);
  98.     }
  99.  
  100.     if (fh)
  101.     Close(fh);
  102.  
  103.     /*
  104.      *    Until receive SIGBREAKF_CTRL_F, then handshake/exit
  105.      */
  106.  
  107.     port->Proc->pr_ConsoleTask = port->OldConsoleTask;
  108.     {
  109.     FileHandle *fh;
  110.     CLI *cli = BTOC(port->Proc->pr_CLI);
  111.  
  112.     if ((fh = BTOC(cli->cli_CurrentOutput)) && fh->fh_Type == (MsgPort *)port)
  113.         fh->fh_Type = port->OldConsoleTask;
  114.     if ((fh = BTOC(cli->cli_CurrentInput)) && fh->fh_Type == (MsgPort *)port)
  115.         fh->fh_Type = port->OldConsoleTask;
  116.     }
  117.  
  118.     DeletePort(privatePort);
  119.     Forbid();
  120.     {
  121.     Task *task = port->Port.mp_SigTask;
  122.     port->Port.mp_SigTask = NULL;
  123.     Signal(task, SIGBREAKF_CTRL_E);
  124.     }
  125. }
  126.  
  127.