home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / unix / emx / test / termio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-28  |  6.7 KB  |  261 lines

  1. /* termio.c (emx+gcc) */
  2.  
  3. /* Test termio ioctl() and signals */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <io.h>
  9. #include <errno.h>
  10. #include <signal.h>
  11. #include <process.h>
  12. #include <setjmp.h>
  13. #include <fcntl.h>
  14. #include <sys/types.h>
  15. #include <sys/time.h>
  16. #include <sys/ioctl.h>
  17. #include <sys/termio.h>
  18.  
  19. #define FALSE 0
  20. #define TRUE  1
  21.  
  22. static int jump_flag = FALSE;
  23. static int nread_flag = FALSE;
  24. static int hex_flag = FALSE;
  25. static int select_flag = FALSE;
  26.  
  27. static jmp_buf jump;
  28.  
  29. static void handler (int sig)
  30. {
  31.   if (sig == SIGINT)
  32.     {
  33.       printf ("(SIGINT)");
  34.       signal (sig, SIG_ACK);
  35.     }
  36.   else if (sig == SIGALRM)
  37.     {
  38.       printf ("(SIGALRM)");
  39.       signal (sig, SIG_ACK);
  40.     }
  41.   else
  42.     {
  43.       printf ("Signal %d received. Process stopped.\n", sig);
  44.       exit (1);
  45.     }
  46.   if (jump_flag)
  47.     longjmp (jump, 1);
  48. }
  49.  
  50.  
  51. static void hexb (unsigned char c)
  52. {
  53.   static char const hexd[] = "0123456789ABCDEF";
  54.   char buf[2];
  55.  
  56.   buf[0] = hexd[(c >> 4) & 0x0f];
  57.   buf[1] = hexd[c & 0x0f];
  58.   write (1, buf, 2);
  59. }
  60.  
  61.  
  62. static void usage (void)
  63. {
  64.   puts ("Usage: termio [-jnh] [-s[#]]");
  65.   exit (1);
  66. }
  67.  
  68.  
  69. int main (int argc, char *argv[])
  70. {
  71.   char buf[100], *p;
  72.   int i, n, req;
  73.   struct termio tio;
  74.   fd_set rfds;
  75.   struct timeval tv, *tvp;
  76.  
  77.   for (i = 1; i < argc; ++i)
  78.     if (strcmp (argv[i], "-j") == 0)
  79.       jump_flag = TRUE;
  80.     else if (strcmp (argv[i], "-n") == 0)
  81.       nread_flag = TRUE;
  82.     else if (strcmp (argv[i], "-h") == 0)
  83.       hex_flag = TRUE;
  84.     else if (memcmp (argv[i], "-s", 2) == 0)
  85.       {
  86.         select_flag = TRUE;
  87.         if (argv[i][2] == 0)
  88.           tvp = NULL;
  89.         else
  90.           {
  91.             errno  = 0;
  92.             tv.tv_sec = strtol (argv[i]+2, &p, 10);
  93.             if (errno != 0 || *p != 0)
  94.               usage ();
  95.             tv.tv_usec = 0;
  96.             tvp = &tv;
  97.           }
  98.         }
  99.     else
  100.       usage ();
  101.   if (setjmp (jump) != 0)
  102.     puts ("jumped");
  103.   signal (SIGINT, handler);
  104.   signal (SIGBREAK, handler);
  105.   signal (SIGQUIT, handler);
  106.   signal (SIGALRM, handler);
  107.   ioctl (0, TCGETA, &tio);
  108.   tio.c_lflag &= ~IDEFAULT;
  109.   ioctl (0, TCSETA, &tio);
  110.   req = sizeof (buf) - 1;
  111.   for (;;)
  112.     {
  113.       if (nread_flag)
  114.         {
  115.           if (ioctl (0, FIONREAD, &n) == -1)
  116.             perror ("ioctl FIONREAD");
  117.           else
  118.             printf ("%d> ", n);
  119.         }
  120.       if (select_flag)
  121.         {
  122.           FD_ZERO (&rfds);
  123.           FD_SET (0, &rfds);
  124.           i = select (FD_SETSIZE, &rfds, NULL, NULL, tvp);
  125.           if (i < 0)
  126.             perror ("select");
  127.           else if (i == 0)
  128.             printf ("0> ");
  129.           else
  130.             {
  131.               printf ("%d:", i);
  132.               for (i = 7; i >= 0; --i)
  133.                 printf ("%d", (FD_ISSET (i, &rfds) ? 1 : 0));
  134.               printf ("> ");
  135.             }
  136.         }
  137.       n = read (0, buf, req);
  138.       if (n < 0)
  139.         {
  140.           if (errno == EAGAIN)
  141.             {
  142.               puts ("No data available -- sleeping for 2 seconds");
  143.               sleep (2);
  144.             }
  145.           else
  146.             perror ("read");
  147.         }
  148.       if (n >= 0)
  149.         {
  150.           write (1, "<", 1);
  151.           if (hex_flag)
  152.             for (i = 0; i < n; ++i)
  153.               hexb (buf[i]);
  154.           else
  155.             write (1, buf, n);
  156.           write (1, ">\n", 2);
  157.         }
  158.       if (n > 0)
  159.         {
  160.           buf[n] = 0;
  161.           p = strchr (buf, '\n');
  162.           if (p != NULL) *p = 0;
  163.           if (buf[0] == '?')
  164.             {
  165.               puts ("?          Help");
  166.               puts ("$          Quit");
  167.               puts ("!CMD       Shell escape");
  168.               puts ("+          Toggle ICANON");
  169.               puts (":          Toggle IDEFAULT");
  170.               puts ("|          Toggle IDELETE");
  171.               puts (";          Toggle ECHO");
  172.               puts ("-          Toggle O_NDELAY");
  173.               puts ("%SEC       Set alarm clock");
  174.               puts ("#SEC       Sleep (append 'f' to flush buffer)");
  175.               puts ("=CHARS     Set number of characters to read");
  176.               puts (">CHARS     Set VMIN");
  177.               puts ("<TENTHS    Set VTIME");
  178.               puts (".SIG       Ignore signal");
  179.               puts ("*SIG       Handle signal");
  180.               puts ("^SIG       Set default processing for signal");
  181.               puts ("~SIG       Raise signal");
  182.             }
  183.           else if (buf[0] == '$')
  184.             break;
  185.           else if (buf[0] == '!')
  186.             system (buf+1);
  187.           else if (buf[0] == '%')
  188.             printf ("alarm=%u\n", alarm (atoi (buf+1)));
  189.           else if (buf[0] == '#')
  190.             {
  191.               n = atoi (buf+1);
  192.               if (n > 0)
  193.                 sleep (n);
  194.               if (strchr (buf+1, 'f'))
  195.                 ioctl (0, TCFLSH, 0);
  196.             }
  197.           else if (buf[0] == '+')
  198.             {
  199.               tio.c_lflag ^= ICANON;
  200.               ioctl (0, TCSETA, &tio);
  201.             }
  202.           else if (buf[0] == ':')
  203.             {
  204.               tio.c_lflag ^= IDEFAULT;
  205.               ioctl (0, TCSETA, &tio);
  206.             }
  207.           else if (buf[0] == ';')
  208.             {
  209.               tio.c_lflag ^= ECHO;
  210.               ioctl (0, TCSETA, &tio);
  211.             }
  212.           else if (buf[0] == '|')
  213.             {
  214.               tio.c_iflag ^= IDELETE;
  215.               ioctl (0, TCSETA, &tio);
  216.             }
  217.           else if (buf[0] == '>')
  218.             {
  219.               tio.c_cc[VMIN] = (unsigned char)atoi (buf+1);
  220.               ioctl (0, TCSETA, &tio);
  221.             }
  222.           else if (buf[0] == '<')
  223.             {
  224.               tio.c_cc[VTIME] = (unsigned char)atoi (buf+1);
  225.               ioctl (0, TCSETA, &tio);
  226.             }
  227.           else if (buf[0] == '.')
  228.             signal (atoi (buf+1), SIG_IGN);
  229.           else if (buf[0] == '*')
  230.             signal (atoi (buf+1), handler);
  231.           else if (buf[0] == '^')
  232.             signal (atoi (buf+1), SIG_DFL);
  233.           else if (buf[0] == '~')
  234.             {
  235.               if (buf[1] == 'a')
  236.                 abort ();
  237.               else
  238.                 raise (atoi (buf+1));
  239.             }
  240.           else if (buf[0] == '=')
  241.             {
  242.               n = atoi (buf+1);
  243.               if (n >= 1 && n <= sizeof (buf) - 1)
  244.                 req = n;
  245.               else
  246.                 req = sizeof (buf) - 1;
  247.             }
  248.           else if (buf[0] == '-')
  249.             {
  250.               n = fcntl (0, F_GETFL, 0);
  251.               if (n >= 0)
  252.                 {
  253.                   n ^= O_NDELAY;
  254.                   fcntl (0, F_SETFL, n);
  255.                 }
  256.             }
  257.         }
  258.     }
  259.   return (0);
  260. }
  261.