home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d03xx / d0326.lha / MicroTerm / console.c < prev    next >
C/C++ Source or Header  |  1990-03-05  |  6KB  |  204 lines

  1. /** console.c
  2.  
  3.             Written by Stephen Vermeulen (403) 282-7990
  4.  
  5.             PO Box 3295, Station B,
  6.             Calgary, Alberta, CANADA, T2M 4L8
  7.  
  8.     this set of routines is all that is required to talk across
  9.     the serial line, including a console device attached to an
  10.     intuition window for user IO.
  11.  **/
  12.  
  13. /** opens up the console device window so that we can talk
  14.     to the modem...
  15.  **/
  16.  
  17. struct Window *cw;                  /* console talk window */
  18. struct IOStdReq *cwread, *cwwrite;  /** messages for console window **/
  19. struct MsgPort *cwport;             /** message port for console window **/
  20.  
  21. open_con()
  22. {
  23.   cw = OpenWindow(&new_cw);
  24.   if (cw)
  25.   {
  26.     SetMenuStrip(cw, &term_menu);
  27.     cwport = CreatePort(NULL, 0L);
  28.     if (cwport)
  29.     {
  30.       cwread = CreateStdIO(cwport);
  31.       if (cwread)
  32.       {
  33.         cwwrite = CreateStdIO(cwport);
  34.         if (cwwrite)
  35.         {
  36.           cwwrite->io_Data = (APTR) cw;
  37.           cwwrite->io_Length = 4L;
  38.           if (OpenDevice("console.device", 0L, cwwrite, 0L))
  39.           {
  40.             /** failure **/
  41.             DeleteStdIO(cwwrite);
  42.           }
  43.           else
  44.           {
  45.             /** success **/
  46.             cwread->io_Device = cwwrite->io_Device;
  47.             CopyMem(cwwrite, cwread, (long) sizeof(struct IOStdReq));
  48.             return(TRUE);
  49.           }
  50.         }
  51.         DeleteStdIO(cwread);
  52.       }
  53.       DeletePort(cwport);
  54.     }
  55.     CloseWindow(cw);
  56.   }
  57.   return(FALSE);
  58. }
  59.  
  60. close_con()
  61. {
  62.   CloseDevice(cwwrite);
  63.   DeleteStdIO(cwwrite);
  64.   DeleteStdIO(cwread);
  65.   DeletePort(cwport);
  66.   ClearMenuStrip(cw);
  67.   CloseWindow(cw);
  68. }
  69.  
  70. /** some buffers to hold data...
  71.  **/
  72.  
  73. UBYTE conbuf[256];
  74.  
  75. con_talk(ms)
  76. struct MySer *ms;
  77. {
  78.   short num, i, more;
  79.   long len;
  80.   struct Message *emsg;
  81.   struct IntuiMessage *msg;
  82.  
  83.   if (open_con());
  84.   {
  85.     /** do our intial reads from the console and serial devices to
  86.         get the ball rolling...
  87.      **/
  88.     cwread->io_Command = CMD_READ;
  89.     cwread->io_Flags = 0;
  90.     cwread->io_Length = 255;
  91.     cwread->io_Data = (APTR) conbuf;
  92.     SendIO(cwread);
  93.     ms->readio->IOSer.io_Command = CMD_READ;
  94.     ms->readio->IOSer.io_Flags = 0;
  95.     ms->readio->IOSer.io_Length = 1;
  96.     ms->readio->IOSer.io_Data = (APTR) ms->readdata;
  97.     SendIO(ms->readio);
  98.     more = TRUE;
  99.     while(more)
  100.     {
  101.       Wait( (1L << cw->UserPort->mp_SigBit)
  102.           | (1L << cwport->mp_SigBit)
  103.           | (1L << ms->readport->mp_SigBit) );
  104.       if (emsg = GetMsg(cwport))  /** key(s) pressed, so send to ser **/
  105.       {
  106.         ms->writeio->IOSer.io_Command = CMD_WRITE;
  107.         ms->writeio->IOSer.io_Flags = 0;
  108.         ms->writeio->IOSer.io_Length = cwread->io_Actual;
  109.         CopyMem(conbuf, ms->writedata, (long) cwread->io_Actual);
  110.         ms->writeio->IOSer.io_Data = (APTR) ms->writedata;
  111.         DoIO(ms->writeio);  /** wait till serial device has sent it **/
  112.  
  113.         cwread->io_Command = CMD_READ;
  114.         cwread->io_Flags = 0;
  115.         cwread->io_Length = 255;
  116.         cwread->io_Data = (APTR) conbuf;
  117.         SendIO(cwread);
  118.       }
  119.       if (emsg = GetMsg(ms->readport))  /** serial info received, print it **/
  120.       {
  121.         /** first send the one lone byte we requested ages ago,
  122.             then request any additional stuff that may be queued up.
  123.          **/
  124.  
  125.         cwwrite->io_Command = CMD_WRITE;
  126.         cwwrite->io_Flags = 0;
  127.         cwwrite->io_Length = 1;
  128.         cwwrite->io_Data = (APTR) ms->readdata;
  129.         DoIO(cwwrite);
  130.         /** ask for number of serial bytes queued up...
  131.          **/
  132.  
  133.         ms->readio->IOSer.io_Command = SDCMD_QUERY;
  134.         ms->readio->IOSer.io_Length = 0;
  135.         DoIO(ms->readio);
  136.         if (ms->readio->IOSer.io_Actual)
  137.         {
  138.           /** there were some bytes queued up at the serial device,
  139.               so now ask for these...
  140.            **/
  141.  
  142.           ms->readio->IOSer.io_Command = CMD_READ;
  143.           len = ms->readio->IOSer.io_Actual;
  144.           len = min(256L, len);
  145.           ms->readio->IOSer.io_Length = len;
  146.           ms->readio->IOSer.io_Flags = 0;
  147.           ms->readio->IOSer.io_Data = (APTR) ms->readdata;
  148.           ms->readio->IOSer.io_Error = 0;
  149.           DoIO(ms->readio);
  150.           cwwrite->io_Command = CMD_WRITE;
  151.           cwwrite->io_Flags = 0;
  152.           cwwrite->io_Length = len;
  153.           cwwrite->io_Data = (APTR) ms->readdata;
  154.           DoIO(cwwrite);
  155.         }
  156.  
  157.         /** finally ask for another lone byte... **/
  158.  
  159.         ms->readio->IOSer.io_Command = CMD_READ;
  160.         ms->readio->IOSer.io_Flags = 0;
  161.         ms->readio->IOSer.io_Length = 1;
  162.         ms->readio->IOSer.io_Data = (APTR) ms->readdata;
  163.         SendIO(ms->readio);
  164.       }
  165.       while (msg = (struct IntuiMessage *) GetMsg(cw->UserPort))
  166.       {
  167.         if (msg->Class == MENUPICK)
  168.         {
  169.           num = msg->Code;
  170.           while (num != MENUNULL)
  171.           {
  172.             switch (MENUNUM(num))
  173.             {
  174.               case 0:
  175.               switch (ITEMNUM(num))
  176.               {
  177.                 case 0: /** does nothing **/
  178.                   break;
  179.                 case 1: /** about **/
  180.                   break;
  181.                 case 2: /** help **/
  182.                   break;
  183.                 case 3: /** quit **/
  184.                   more = FALSE;
  185.                   break;
  186.               }
  187.               default:
  188.                 break;
  189.             }
  190.             num = ItemAddress(&term_menu, (long) num)->NextSelect;
  191.           }
  192.         }
  193.         ReplyMsg(msg);
  194.       }
  195.     } /** end while(more) do **/
  196. done_term:
  197.     AbortIO(cwread);
  198.     AbortIO(ms->readio);
  199.     WaitIO(cwread);
  200.     WaitIO(ms->readio);
  201.     close_con();
  202.   }
  203. }
  204.