home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / sys / amiga / programm / 13071 < prev    next >
Encoding:
Text File  |  1992-09-03  |  9.1 KB  |  294 lines

  1. Path: sparky!uunet!ivgate!mtask!MsgPost.v1.00
  2. From: MsgPost.v1.00@mtask.omahug.org (MsgPost v1.00)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: [2 of 2] A serial device example (LONG)
  5. Message-ID: <21.2aa6c1d5@ivgate>
  6. Date: 03 Sep 92 08:19:34 CST
  7. Reply-To: msgpost.v1.00@mtask.omahug.org
  8. Organization: Multitasking Systems, Kansas City
  9. Sender: news@ivgate.omahug.org (UUscan 1.10)
  10. Followup-To: comp.sys.amiga.programmer
  11. Lines: 281
  12.  
  13.                 mask = 1L <<
  14. iorequest->IOSer.io_Message.mn_ReplyPort->mp_SigBit;
  15.                 mask = Wait (mask | SIGBREAKF_CTRL_C);
  16.  
  17.                 if (mask & SIGBREAKF_CTRL_C) {
  18.                         AbortIO (iorequest);
  19.                         WaitIO (iorequest);
  20.                         NNTP_in_ip = 0;
  21.  
  22.                         // PutStr ("reads: terminated outstanding IO\n");
  23.                         return -1;
  24.                 }
  25.                 WaitIO (iorequest);
  26.                 NNTP_in_ip = 0;
  27.         }
  28.  
  29.         if (errno = iorequest->IOSer.io_Error) {
  30.                 PrintFault (errno, "reads IO error");
  31.                 return -1;
  32.         }
  33.  
  34.         return iorequest->IOSer.io_Actual;
  35. }
  36.  
  37. int writes (struct IOExtSer *iorequest, char *buf, int len)
  38. {
  39.         int errno;
  40.         //
  41.         //  writes: Output 'len' bytes in 'buf' to 'iorequest'.
  42.         //
  43.         //          Return an error if one occurs.
  44.         //
  45.         //          Much simpler than reads().
  46.         //
  47.         iorequest->IOSer.io_Command = CMD_WRITE;
  48.         iorequest->IOSer.io_Data    = buf;
  49.         iorequest->IOSer.io_Length  = len;
  50.         NNTP_out_ip = 1;
  51.  
  52.         DoIO (iorequest);
  53.         NNTP_out_ip = 0;
  54.  
  55.         if (errno = iorequest->IOSer.io_Error) {
  56.                 PrintFault (errno, "writes: IO error");
  57.                 return -1;
  58.         }
  59.  
  60.         return iorequest->IOSer.io_Actual;
  61. }
  62.  
  63. #ifdef DEBUG
  64. void DebugIoRequest (struct IOExtSer * iorequest)
  65. {
  66.         int mask = iorequest->io_SerFlags;
  67.  
  68.         printf ("CtlChar    0x%lx \n", iorequest->io_CtlChar);
  69.         printf ("RBufLen    %ld   \n", iorequest->io_RBufLen);
  70.         printf ("ExtFlags   0x%lx \n", iorequest->io_ExtFlags);
  71.         printf ("Baud       %ld   \n", iorequest->io_Baud);
  72.         printf ("BrkTime    %ld   \n", iorequest->io_BrkTime);
  73.         printf ("ReadLen    %ld   \n", iorequest->io_ReadLen);
  74.         printf ("WriteLen   %ld   \n", iorequest->io_WriteLen);
  75.         printf ("StopBits   %ld   \n", iorequest->io_StopBits);
  76.         printf ("SerFlags   0x%lx \n", iorequest->io_SerFlags);
  77.         if (mask) {
  78.                 printf ("           ");
  79.                 if (mask & SERF_XDISABLED)
  80.                         printf ("XDISABLED ");
  81.                 if (mask & SERF_EOFMODE)
  82.                         printf ("EOFMODE ");
  83.                 if (mask & SERF_SHARED)
  84.                         printf ("SHARED ");
  85.                 if (mask & SERF_RAD_BOOGIE)
  86.                         printf ("RAD_BOOGIE ");
  87.                 if (mask & SERF_QUEUEDBRK)
  88.                         printf ("QUEUEDBRK ");
  89.                 if (mask & SERF_7WIRE)
  90.                         printf ("7WIRE ");
  91.                 if (mask & SERF_PARTY_ODD)
  92.                         printf ("PARTY_ODD ");
  93.                 if (mask & SERF_PARTY_ON)
  94.                         printf ("PARTY_ON");
  95.                 printf ("\n");
  96.         }
  97.         printf ("Status     0x%lx \n", iorequest->io_Status);
  98.         printf ("\n");
  99.  
  100.         return;
  101. }
  102. #endif
  103.  
  104. int done = 0;
  105. int ih_done = 0;
  106.  
  107. __geta4 void InputHandler (void)
  108. {
  109.     int ih_len;
  110.     char ih_inbuf [256];
  111.  
  112.     struct MsgPort *myport = (struct MsgPort *) CreatePort (NULL, 0);
  113.     struct MsgPort *saveport = NULL;
  114.  
  115.     // NO ONE touch NNTP_in_io after I've been entered, until I'm done.
  116.     // Under pain of GURU.
  117.  
  118.     if (!myport) {
  119.             PutStr ("InputHandler: CreatePort() failure\n");
  120.  
  121.             ih_done = 1;
  122.             done = 3;
  123.             return;
  124.     }
  125.  
  126.     saveport = NNTP_in_io->IOSer.io_Message.mn_ReplyPort;
  127.     NNTP_in_io->IOSer.io_Message.mn_ReplyPort = myport;
  128.  
  129.     while (!done) {
  130.  
  131.             ih_len = reads (NNTP_in_io, ih_inbuf, 256);
  132.  
  133.             if (ih_len <= 0)
  134.                     continue;
  135.  
  136.             ih_len = Write (Output (), ih_inbuf, ih_len);
  137.             if (ih_len < 1)
  138.                     PrintFault (IoErr (), "InputHandler Write error");
  139.             Flush (Output ());
  140.     }
  141.  
  142.     DeletePort (myport);
  143.     NNTP_in_io->IOSer.io_Message.mn_ReplyPort = saveport;
  144.  
  145.     ih_done = 1;
  146.     return;
  147. }
  148.  
  149. int main (int argc, char **argv)
  150. {
  151.         char *device;
  152.         int unit;
  153.         unsigned long pid;
  154.         char *cmd;
  155.         char chr;
  156.         char main_inbuf [128];
  157.         char outbuf [128];
  158.         int len;
  159.         int in_command = 0;
  160.         int last_was_newline = 0;
  161.  
  162.         if (argc <= 1) {
  163.                 device = "serial.device";
  164.                 unit = 0;
  165.         }
  166.         else
  167.                 if (argc != 3) {
  168.                         PutStr ("GRn-Term <device> <unit>\n");
  169.                         exit (20);
  170.                 }
  171.  
  172.         device = argv [1];
  173.         unit = atoi (argv [2]);
  174.  
  175.         SetupSerialPort (0, device, unit);
  176.  
  177.         pid = CreateNewProcTags (
  178.                 NP_Entry,       InputHandler,
  179.                 NP_Input,       Input(),
  180.                 NP_Output,      Output(),
  181.                 NP_CloseInput,  DOSFALSE,
  182.                 NP_CloseOutput, DOSFALSE,
  183.                 NP_Name,        "InputHandler",
  184.                 TAG_DONE);
  185.  
  186.         if (!pid)
  187.                 KillSerialPort (0, "CreateNewProc() failed\n");
  188.  
  189.         PutStr ("\nGRn-Term Initialized\n\n");
  190.  
  191.         cmd = outbuf;
  192.         while (!done) {
  193.  
  194.                 len = Read (Input (), main_inbuf, 1);
  195.  
  196.                 if (len < 0) {
  197.                         PrintFault (IoErr (), "CONSOLE: input error");
  198.                         continue;
  199.                 }
  200.                 if (len == 0) {
  201.                         // EOF on input
  202.                         done = 1;
  203.                         Signal ((struct Task *) pid, SIGBREAKF_CTRL_C);
  204.                         continue;
  205.                 }
  206.  
  207.                 chr = main_inbuf [0];
  208.  
  209.                 if (cmd == outbuf && chr == '\\')
  210.                         in_command = 1;
  211.                 if (!in_command)
  212.                         if (chr == '\n')
  213.                                 writes (NNTP_out_io, "\r", 1);
  214.                         else
  215.                                 writes (NNTP_out_io, main_inbuf, 1);
  216.  
  217.                 *cmd++ = main_inbuf [0];
  218.  
  219.                 if (chr == '\n') {
  220.                         last_was_newline = 1;
  221.                         *cmd = '\0';
  222.                         cmd = outbuf;
  223.  
  224.                         if (!in_command)
  225.                                 continue;
  226.  
  227.                         if (strnicmp (outbuf, "\\quit", 5) == 0) {
  228.                                 done = 2;
  229.                                 Signal ((struct Task *) pid, SIGBREAKF_CTRL_C);
  230.                         }
  231.                         else if (strncmp (outbuf, "\\dial ", 6) == 0) {
  232.                                 char *p = outbuf + 6;
  233.  
  234.                                 writes (NNTP_out_io, "\rAT\r", 4);
  235.                                 Delay (50);
  236.                                 *(p + strlen (p) - 1) = '\r';
  237.                                 writes (NNTP_out_io, p, strlen (p));
  238.                         }
  239.                         else if (strncmp (outbuf, "\\hangup", 7) == 0) {
  240.                                 Delay (110);
  241.                                 writes (NNTP_out_io, "+", 1);
  242.                                 Delay (5);
  243.                                 writes (NNTP_out_io, "+", 1);
  244.                                 Delay (5);
  245.                                 writes (NNTP_out_io, "+", 1);
  246.                                 Delay (150);
  247.                                 writes (NNTP_out_io, "ATH0\r\n", 6);
  248.                                 Delay (110);
  249.                         }
  250. #if 0
  251.                         // FIXME: get InputHandler() off the serial port
  252.                         else if (strncmp (outbuf, "\\grn", 4) == 0) {
  253.                                 int rslt;
  254.  
  255.                                 ShutDownSerialPort ();
  256.                                 rslt = SystemTagList (outbuf + 1, TAG_DONE);
  257.                                 if (rslt == -1)
  258.                                         PutStr ("GRn failed\n");
  259.                                 else
  260.                                         PutStr ("GRn complete\n");
  261.                                 SetupSerialPort (0, device, unit);
  262.                         }
  263. #endif
  264.                         else {
  265.                                 PutStr ("Unknown control command '");
  266.                                 PutStr (outbuf);
  267.                                 PutStr ("'\n");
  268.                         }
  269.                         in_command = 0;
  270.                 }
  271.                 else
  272.                         last_was_newline = 0;
  273.                 *cmd = '\0';
  274.         }
  275.  
  276.         len = 0;
  277.         while (!ih_done) {
  278.                 len++;
  279.                 if (len > 10) {
  280.                         len = 0;
  281.                         PutStr ("Waiting on InputHandler to terminate\n");
  282.                 }
  283.                 Delay (10);
  284.         }
  285.  
  286.         ShutDownSerialPort ();
  287.  
  288.         PutStr ("\nGRn-Term closing down\n\n");
  289. }
  290. --
  291.   //   Michael B. Smith
  292. \X/    mbs@adastra.cvl.va.us  -or-  uunet.uu.net!virginia.edu!adastra!mbs
  293.  
  294.