home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / mint / mntutl93 / stty.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-03  |  4.3 KB  |  216 lines

  1. #include <stdio.h>
  2. #include <ioctl.h>
  3.  
  4. #define TOSTOP 0x0100
  5. #define XKEY 0x0200
  6.  
  7. struct {
  8.     char *name;
  9.     short bits;
  10. } modes[] = {
  11.     { "crmod", CRMOD },
  12.     { "cbreak", CBREAK },
  13.     { "echo", ECHO },
  14.     { "raw", RAW },
  15.     { "tostop", TOSTOP },
  16.     { "xkey", XKEY }
  17. };
  18.  
  19. #define NMODES (sizeof(modes) / sizeof(modes[0]))
  20.  
  21. void prchar(s,c)
  22. char *s;
  23. char c;
  24. {
  25.     printf("%s ",s);
  26.     if (c < ' ') printf("^%c ",c + '@');
  27.     else if (c == 127) printf("^? ");
  28.     else printf("%c ",c);
  29. }
  30.  
  31. void prmode(s,f)
  32. char *s;
  33. int f;
  34. {
  35.     printf("%s%s ",(f ? "" : "-"),s);
  36. }
  37.  
  38. int speed(sp)
  39. int sp;
  40. {
  41.     return sp;
  42. }
  43.  
  44. /*
  45.  * atok: turns a string into an ASCII value.  ^x yields control-x
  46.  * for X in @A-Za-z[\]^_ as long as there are only two chars.
  47.  * ^? yields DEL (127) and other single chars yield themselves.
  48.  */
  49.  
  50. int atok(s)
  51. char *s;
  52. {
  53.     char c;
  54.  
  55.     if (!s || !*s) return -1;
  56.     c = *s++;
  57.     if (c == '^') {
  58.     c = *s++;
  59.     if (*s) return -1;
  60.     if (c == '?') return 127;
  61.     else if (c >= '@' && c <= '_') return (c - '@');
  62.     else if (c >= 'a' && c <= 'z') return (c - '`');
  63.     else return -1;
  64.     }
  65.     else if (*s) return -1;
  66.     else return c;
  67. }
  68.  
  69. main(argc,argv)
  70. int argc;
  71. char *argv[];
  72. {
  73.     int fd;
  74.     int i;
  75.     char *kp;
  76.     long err;
  77.     struct sgttyb sg;
  78.     struct tchars tc;
  79.     struct ltchars ltc;
  80.  
  81.     if ((fd = open("U:\\dev\\tty",0)) < 0) {
  82.         perror("can't open tty");
  83.         exit(1);
  84.     }
  85.     if (ioctl(fd,TIOCGETP,&sg) ||
  86.         ioctl(fd,TIOCGETC,&tc) ||
  87.         ioctl(fd,TIOCGLTC,<c)) {
  88.         perror("can't do ioctl");
  89.         exit(1);
  90.     }
  91.  
  92.     --argc, ++argv;
  93.  
  94.     if (!argc) {
  95.         /* print current state */
  96.         prchar("intr",tc.t_intrc);
  97.         prchar("quit",tc.t_quitc);
  98.         prchar("start",tc.t_startc);
  99.         prchar("stop",tc.t_stopc);
  100.         prchar("eof",tc.t_eofc);
  101.         prchar("brk",tc.t_brkc);
  102.         putchar('\n');
  103.  
  104.         prchar("susp",ltc.t_suspc);
  105.         prchar("dsusp",ltc.t_dsuspc);
  106.         prchar("rprnt",ltc.t_rprntc);
  107.         prchar("flush",ltc.t_flushc);
  108.         prchar("werase",ltc.t_werasc);
  109.         prchar("lnext",ltc.t_lnextc);
  110.         putchar('\n');
  111.  
  112.         printf("ispeed %d ospeed %d ",
  113.             speed(sg.sg_ispeed),
  114.             speed(sg.sg_ospeed));
  115.         prchar("erase",sg.sg_erase);
  116.         prchar("kill",sg.sg_kill);
  117.         putchar('\n');
  118.  
  119.         for (i=0; i<NMODES; i++) {
  120.             prmode(modes[i].name,sg.sg_flags & modes[i].bits);
  121.         }
  122.         putchar('\n');
  123.         exit(0);
  124.         }
  125.     while (argc) {
  126.         if (strcmp("intr",argv[0]) == 0) {
  127.         kp = &tc.t_intrc;
  128.         goto key;
  129.         }
  130.         else if (strcmp("quit",argv[0]) == 0) {
  131.         kp = &tc.t_quitc;
  132.         goto key;
  133.         }
  134.         else if (strcmp("start",argv[0]) == 0) {
  135.         kp = &tc.t_startc;
  136.         goto key;
  137.         }
  138.         else if (strcmp("stop",argv[0]) == 0) {
  139.         kp = &tc.t_stopc;
  140.         goto key;
  141.         }
  142.         else if (strcmp("eof",argv[0]) == 0) {
  143.         kp = &tc.t_eofc;
  144.         goto key;
  145.         }
  146.         else if (strcmp("brk",argv[0]) == 0) {
  147.         kp = &tc.t_brkc;
  148.         goto key;
  149.         }
  150.         else if (strcmp("susp",argv[0]) == 0) {
  151.         kp = <c.t_suspc;
  152.         goto key;
  153.         }
  154.         else if (strcmp("dsusp",argv[0]) == 0) {
  155.         kp = <c.t_dsuspc;
  156.         goto key;
  157.         }
  158.         else if (strcmp("rprnt",argv[0]) == 0) {
  159.         kp = <c.t_rprntc;
  160.         goto key;
  161.         }
  162.         else if (strcmp("flush",argv[0]) == 0) {
  163.         kp = <c.t_flushc;
  164.         goto key;
  165.         }
  166.         else if (strcmp("werase",argv[0]) == 0) {
  167.         kp = <c.t_werasc;
  168.         goto key;
  169.         }
  170.         else if (strcmp("lnext",argv[0]) == 0) {
  171.         kp = <c.t_lnextc;
  172. key:
  173.         if (argc == 1) {
  174.             printf("%s requires a key-name argument\n",argv[0]);
  175.             exit(1);
  176.         }
  177.         if ((*kp = atok(argv[1])) == -1) {
  178.             printf("Invalid key name for %s: %s\n",argv[0],argv[1]);
  179.             exit(1);
  180.         }
  181.         ++argv, --argc;
  182.         }
  183.         else if (**argv == '-') {
  184.         /* check against each mode */
  185.         for (i=0; i<NMODES; i++) {
  186.             if (strcmp(modes[i].name,&argv[0][1]) == 0) {
  187.             sg.sg_flags &= ~modes[i].bits;
  188.             goto ok1;
  189.             }
  190.         }
  191.         printf("No such mode: %s\n",argv[0][1]);
  192.         exit(1);
  193. ok1:        ;
  194.         }
  195.         else {
  196.         for (i=0; i<NMODES; i++) {
  197.             if (strcmp(modes[i].name,*argv) == 0) {
  198.             sg.sg_flags |= modes[i].bits;
  199.             goto ok2;
  200.             }
  201.         }
  202.         printf("No such mode: %s\n",*argv);
  203.         exit(1);
  204. ok2:        ;
  205.         }
  206.         ++argv, --argc;
  207.     } /* end of "while (argc)" */
  208.  
  209.     if (ioctl(fd,TIOCSETP,&sg) ||
  210.         ioctl(fd,TIOCSETC,&tc) ||
  211.         ioctl(fd,TIOCSLTC,<c)) {
  212.         perror("Can not set tty modes");
  213.     }
  214.     exit(0);
  215. }
  216.