home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / devices / modm0dev.zoo / stty.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-08  |  6.5 KB  |  324 lines

  1. /*
  2.  * stty.c: a simple implementation of stty(1)
  3.  *
  4.  * Original version by Eric R. Smith, slightly extended by Thierry Bousch
  5.  * to support the additional modem ioctls.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <ioctl.h>
  10. #include <ctype.h>
  11.  
  12. #ifndef B0
  13. #define B0 0
  14. #endif
  15. #ifndef B134
  16. #define B134 B135
  17. #endif
  18.  
  19. /* bummer, why isn't this one in ioctl.h? */
  20. #ifndef RTSCTS
  21. #define RTSCTS    0x2000
  22. #endif
  23.  
  24. /* these ones may change, look in modm0dev.h */
  25. #define TIOCGHUPCL    (('T'<< 8) | 98)
  26. #define TIOCSHUPCL    (('T'<< 8) | 99)
  27. #define TIOCGSOFTCAR    (('T'<< 8) | 100)
  28. #define TIOCSSOFTCAR    (('T'<< 8) | 101)
  29.  
  30. struct {
  31.     char *name;
  32.     short bits;
  33. } modes[] = {
  34.     { "crmod", CRMOD },
  35.     { "cbreak", CBREAK },
  36.     { "echo", ECHO },
  37.     { "even", EVENP },
  38.     { "odd", ODDP },
  39.     { "rtscts", RTSCTS },
  40.     { "raw", RAW },
  41.     { "tandem", TANDEM },
  42.     { "tostop", TOSTOP },
  43.     { "xkey", XKEY }
  44. };
  45.  
  46. #define NMODES (sizeof(modes) / sizeof(modes[0]))
  47.  
  48. struct {
  49.     int num;
  50.     int baud;
  51. } bauds[] = {
  52.     {     B0,     0 },
  53.     {    B50,    50 },
  54.     {    B75,    75 },
  55.     {   B110,   110 },
  56.     {   B134,   134 },
  57.     {   B150,   150 },
  58.     {   B200,   200 },
  59.      {   B300,   300 },
  60.     {   B600,   600 },
  61.     {  B1200,  1200 },
  62.     {  B1800,  1800 },
  63.     {  B2400,  2400 },
  64.      {  B4800,  4800 },
  65.     {  B9600,  9600 },
  66.     { B19200, 19200 },
  67.     { B38400, 38400 }
  68. };
  69.  
  70. #define NBAUDS (sizeof(bauds) / sizeof(bauds[0]))
  71.  
  72. void prchar(s,c)
  73. char *s;
  74. char c;
  75. {
  76.     printf("%s ",s);
  77.     if (c < ' ') printf("^%c ",c + '@');
  78.     else if (c == 127) printf("^? ");
  79.     else printf("%c ",c);
  80. }
  81.  
  82. void prmode(s,f)
  83. char *s;
  84. int f;
  85. {
  86.     printf("%s%s ",(f ? "" : "-"),s);
  87. }
  88.  
  89. int speed(sp)
  90. int sp;
  91. {
  92.   int i;
  93.  
  94.   for (i = 0; i<NBAUDS; i++) {
  95.     if (bauds[i].num == sp)
  96.       return (bauds[i].baud);
  97.   }
  98.   return -1;
  99. }
  100.  
  101. int baudnum(bs)
  102. char *bs;
  103. {
  104.   int bv;
  105.   int i;
  106.  
  107.   if (!(isdigit(*bs)))
  108.     return -1;
  109.   bv = atoi(bs);  
  110.   for (i = 0; i<NBAUDS; i++) {
  111.      if (bauds[i].baud == bv)
  112.       return (bauds[i].num);
  113.    }
  114.   return -1;
  115. }
  116.  
  117. /*
  118.  * atok: turns a string into an ASCII value.  ^x yields control-x
  119.  * for X in @A-Za-z[\]^_ as long as there are only two chars.
  120.  * ^? yields DEL (127) and other single chars yield themselves.
  121.  */
  122.  
  123. int atok(s)
  124. char *s;
  125. {
  126.     char c;
  127.  
  128.     if (!s || !*s) return -1;
  129.     c = *s++;
  130.     if (c == '^') {
  131.     c = *s++;
  132.     if (*s) return -1;
  133.     if (c == '?') return 127;
  134.     else if (c >= '@' && c <= '_') return (c - '@');
  135.     else if (c >= 'a' && c <= 'z') return (c - '`');
  136.     else return -1;
  137.     }
  138.     else if (*s) return -1;
  139.     else return c;
  140. }
  141.  
  142. main(argc,argv)
  143. int argc;
  144. char *argv[];
  145. {
  146.     int fd;
  147.     int i;
  148.     char *kp;
  149.     long err;
  150.     struct sgttyb sg;
  151.     struct tchars tc;
  152.     struct ltchars ltc;
  153.     short hupcl, softcar;
  154.     int b;
  155.  
  156.     fd = 0;
  157.     if (!(isatty(fd))) {
  158.         if ((fd = open("U:\\dev\\tty",0)) < 0) {
  159.             perror("can't open tty");
  160.             exit(1);
  161.         }
  162.     }
  163.  
  164.     if (ioctl(fd,TIOCGETP,&sg) ||
  165.         ioctl(fd,TIOCGETC,&tc) ||
  166.         ioctl(fd,TIOCGLTC,<c)) {
  167.         perror("can't do ioctl");
  168.         exit(1);
  169.     }
  170.     hupcl = 0;    /* default is "no hangup-on-close" */
  171.     softcar = 1;    /* default is "local"              */
  172.     ioctl(fd,TIOCGHUPCL,&hupcl);
  173.     ioctl(fd,TIOCGSOFTCAR,&softcar);
  174.  
  175.     --argc, ++argv;
  176.  
  177.     if (!argc) {
  178.         /* print current state */
  179.         prchar("intr",tc.t_intrc);
  180.         prchar("quit",tc.t_quitc);
  181.         prchar("start",tc.t_startc);
  182.         prchar("stop",tc.t_stopc);
  183.         prchar("eof",tc.t_eofc);
  184.         prchar("brk",tc.t_brkc);
  185.         putchar('\n');
  186.  
  187.         prchar("susp",ltc.t_suspc);
  188.         prchar("dsusp",ltc.t_dsuspc);
  189.         prchar("rprnt",ltc.t_rprntc);
  190.         prchar("flush",ltc.t_flushc);
  191.         prchar("werase",ltc.t_werasc);
  192.         prchar("lnext",ltc.t_lnextc);
  193.         putchar('\n');
  194.  
  195.         printf("ispeed %d ospeed %d ",
  196.             speed(sg.sg_ispeed),
  197.             speed(sg.sg_ospeed));
  198.         prchar("erase",sg.sg_erase);
  199.         prchar("kill",sg.sg_kill);
  200.         prmode("hupcl",hupcl);
  201.         prmode("local",softcar);
  202.         putchar('\n');
  203.  
  204.         for (i=0; i<NMODES; i++) {
  205.             prmode(modes[i].name,sg.sg_flags & modes[i].bits);
  206.         }
  207.         putchar('\n');
  208.         exit(0);
  209.         }
  210.     while (argc) {
  211.         if ((b = baudnum(argv[0])) != -1) {
  212.         sg.sg_ispeed = sg.sg_ospeed = b;
  213.         }
  214.         else if (strcmp("intr",argv[0]) == 0) {
  215.         kp = &tc.t_intrc;
  216.         goto key;
  217.         }
  218.         else if (strcmp("quit",argv[0]) == 0) {
  219.         kp = &tc.t_quitc;
  220.         goto key;
  221.         }
  222.         else if (strcmp("start",argv[0]) == 0) {
  223.         kp = &tc.t_startc;
  224.         goto key;
  225.         }
  226.         else if (strcmp("stop",argv[0]) == 0) {
  227.         kp = &tc.t_stopc;
  228.         goto key;
  229.         }
  230.         else if (strcmp("eof",argv[0]) == 0) {
  231.         kp = &tc.t_eofc;
  232.         goto key;
  233.         }
  234.         else if (strcmp("brk",argv[0]) == 0) {
  235.         kp = &tc.t_brkc;
  236.         goto key;
  237.         }
  238.         else if (strcmp("susp",argv[0]) == 0) {
  239.         kp = <c.t_suspc;
  240.         goto key;
  241.         }
  242.         else if (strcmp("dsusp",argv[0]) == 0) {
  243.         kp = <c.t_dsuspc;
  244.         goto key;
  245.         }
  246.         else if (strcmp("rprnt",argv[0]) == 0) {
  247.         kp = <c.t_rprntc;
  248.         goto key;
  249.         }
  250.         else if (strcmp("flush",argv[0]) == 0) {
  251.         kp = <c.t_flushc;
  252.         goto key;
  253.         }
  254.         else if (strcmp("werase",argv[0]) == 0) {
  255.         kp = <c.t_werasc;
  256.         goto key;
  257.         }
  258.         else if (strcmp("lnext",argv[0]) == 0) {
  259.         kp = <c.t_lnextc;
  260. key:
  261.         if (argc == 1) {
  262.             printf("%s requires a key-name argument\n",argv[0]);
  263.             exit(1);
  264.         }
  265.         if ((*kp = atok(argv[1])) == -1) {
  266.             printf("Invalid key name for %s: %s\n",argv[0],argv[1]);
  267.             exit(1);
  268.         }
  269.         ++argv, --argc;
  270.         }
  271.         else if (**argv == '-') {
  272.         /* check against each mode */
  273.         if (strcmp("hupcl",1+argv[0]) == 0) {
  274.             hupcl = 0;
  275.             ioctl(fd,TIOCSHUPCL,&hupcl);
  276.             goto ok1;
  277.         }
  278.         if (strcmp("local",1+argv[0]) == 0) {
  279.             softcar = 0;
  280.             ioctl(fd,TIOCSSOFTCAR,&softcar);
  281.             goto ok1;
  282.         }
  283.         for (i=0; i<NMODES; i++) {
  284.             if (strcmp(modes[i].name,&argv[0][1]) == 0) {
  285.             sg.sg_flags &= ~modes[i].bits;
  286.             goto ok1;
  287.             }
  288.         }
  289.         printf("No such mode: %s\n",&argv[0][1]);
  290.         exit(1);
  291. ok1:        ;
  292.         }
  293.         else {
  294.             if (strcmp("hupcl",argv[0]) == 0) {
  295.                 hupcl = 1;
  296.                 ioctl(fd,TIOCSHUPCL,&hupcl);
  297.                 goto ok2;
  298.             }
  299.             if (strcmp("local",argv[0]) == 0) {
  300.                 softcar = 1;
  301.                 ioctl(fd,TIOCSSOFTCAR,&softcar);
  302.                 goto ok2;
  303.             }
  304.         for (i=0; i<NMODES; i++) {
  305.             if (strcmp(modes[i].name,*argv) == 0) {
  306.             sg.sg_flags |= modes[i].bits;
  307.             goto ok2;
  308.             }
  309.         }
  310.         printf("No such mode: %s\n",*argv);
  311.         exit(1);
  312. ok2:        ;
  313.         }
  314.         ++argv, --argc;
  315.     } /* end of "while (argc)" */
  316.  
  317.     if (ioctl(fd,TIOCSETP,&sg) ||
  318.         ioctl(fd,TIOCSETC,&tc) ||
  319.         ioctl(fd,TIOCSLTC,<c)) {
  320.         perror("Can not set tty modes");
  321.     }
  322.     exit(0);
  323. }
  324.