home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / bin / stty / key.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-09  |  6.2 KB  |  282 lines

  1. /*-
  2.  * Copyright (c) 1991 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)key.c    5.3 (Berkeley) 6/10/91";
  36. #endif /* not lint */
  37.  
  38. #include <sys/types.h>
  39. #include <errno.h>
  40. #include <stdlib.h>
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include "stty.h"
  44. #include "extern.h"
  45.  
  46. __BEGIN_DECLS
  47. void    f_all __P((struct info *));
  48. void    f_cbreak __P((struct info *));
  49. void    f_columns __P((struct info *));
  50. void    f_dec __P((struct info *));
  51. void    f_everything __P((struct info *));
  52. void    f_extproc __P((struct info *));
  53. void    f_ispeed __P((struct info *));
  54. void    f_nl __P((struct info *));
  55. void    f_ospeed __P((struct info *));
  56. void    f_raw __P((struct info *));
  57. void    f_rows __P((struct info *));
  58. void    f_sane __P((struct info *));
  59. void    f_size __P((struct info *));
  60. void    f_speed __P((struct info *));
  61. void    f_tty __P((struct info *));
  62. __END_DECLS
  63.  
  64. static struct key {
  65.     char *name;                /* name */
  66.     void (*f) __P((struct info *));        /* function */
  67. #define    F_NEEDARG    0x01            /* needs an argument */
  68. #define    F_OFFOK        0x02            /* can turn off */
  69.     int flags;
  70. } keys[] = {
  71.     "all",        f_all,        0,
  72.     "cbreak",    f_cbreak,    F_OFFOK,
  73.     "cols",        f_columns,    F_NEEDARG,
  74.     "columns",    f_columns,    F_NEEDARG,
  75.     "cooked",     f_sane,        0,
  76.     "dec",        f_dec,        0,
  77.     "everything",    f_everything,    0,
  78.     "extproc",    f_extproc,    F_OFFOK,
  79.     "ispeed",    f_ispeed,    0,
  80.     "new",        f_tty,        0,
  81.     "nl",        f_nl,        F_OFFOK,
  82.     "old",        f_tty,        0,
  83.     "ospeed",    f_ospeed,    F_NEEDARG,
  84.     "raw",        f_raw,        F_OFFOK,
  85.     "rows",        f_rows,        F_NEEDARG,
  86.     "sane",        f_sane,        0,
  87.     "size",        f_size,        0,
  88.     "speed",    f_speed,    0,
  89.     "tty",        f_tty,        0,
  90. };
  91.  
  92. ksearch(argvp, ip)
  93.     char ***argvp;
  94.     struct info *ip;
  95. {
  96.     register struct key *kp;
  97.     register char *name;
  98.     struct key tmp;
  99.     static int c_key __P((const void *, const void *));
  100.  
  101.     name = **argvp;
  102.     if (*name == '-') {
  103.         ip->off = 1;
  104.         ++name;
  105.     } else
  106.         ip->off = 0;
  107.  
  108.     tmp.name = name;
  109.     if (!(kp = (struct key *)bsearch(&tmp, keys,
  110.         sizeof(keys)/sizeof(struct key), sizeof(struct key), c_key)))
  111.         return(0);
  112.     if (!(kp->flags & F_OFFOK) && ip->off)
  113.         err("illegal option -- %s\n%s", name, usage);
  114.     if (kp->flags & F_NEEDARG && !(ip->arg = *++*argvp))
  115.         err("option requires an argument -- %s\n%s", name, usage);
  116.     kp->f(ip);
  117.     return(1);
  118. }
  119.  
  120. static
  121. c_key(a, b)
  122.         const void *a, *b;
  123. {
  124.         return(strcmp(((struct key *)a)->name, ((struct key *)b)->name));
  125. }
  126.  
  127. void
  128. f_all(ip)
  129.     struct info *ip;
  130. {
  131.     print(&ip->t, &ip->win, ip->ldisc, BSD);
  132. }
  133.  
  134. void
  135. f_cbreak(ip)
  136.     struct info *ip;
  137. {
  138.     if (ip->off)
  139.         f_sane(ip);
  140.     else {
  141.         ip->t.c_iflag |= BRKINT|IXON|IMAXBEL;
  142.         ip->t.c_oflag |= OPOST;
  143.         ip->t.c_lflag |= ISIG|IEXTEN;
  144.         ip->t.c_lflag &= ~ICANON;
  145.         ip->set = 1;
  146.     }
  147. }
  148.  
  149. void
  150. f_columns(ip)
  151.     struct info *ip;
  152. {
  153.     ip->win.ws_col = atoi(ip->arg);
  154.     ip->wset = 1;
  155. }
  156.  
  157. void
  158. f_dec(ip)
  159.     struct info *ip;
  160. {
  161.     ip->t.c_cc[VERASE] = (u_char)0177;
  162.     ip->t.c_cc[VKILL] = CTRL('u');
  163.     ip->t.c_cc[VINTR] = CTRL('c');
  164.     ip->t.c_lflag &= ~ECHOPRT;
  165.     ip->t.c_lflag |= ECHOE|ECHOKE|ECHOCTL;
  166.     ip->t.c_iflag &= ~IXANY;
  167.     ip->set = 1;
  168. }
  169.  
  170. void
  171. f_everything(ip)
  172.     struct info *ip;
  173. {
  174.     print(&ip->t, &ip->win, ip->ldisc, BSD);
  175. }
  176.  
  177. void
  178. f_extproc(ip)
  179.     struct info *ip;
  180. {
  181.     int tmp;
  182.  
  183.     if (ip->set) {
  184.         tmp = 1;
  185.         (void)ioctl(ip->fd, TIOCEXT, &tmp);
  186.     } else {
  187.         tmp = 0;
  188.         (void)ioctl(ip->fd, TIOCEXT, &tmp);
  189.     }
  190. }
  191.  
  192. void
  193. f_ispeed(ip)
  194.     struct info *ip;
  195. {
  196.     cfsetispeed(&ip->t, atoi(ip->arg));
  197.     ip->set = 1;
  198. }
  199.  
  200. void
  201. f_nl(ip)
  202.     struct info *ip;
  203. {
  204.     if (ip->off) {
  205.         ip->t.c_iflag |= ICRNL;
  206.         ip->t.c_oflag |= ONLCR;
  207.     } else {
  208.         ip->t.c_iflag &= ~ICRNL;
  209.         ip->t.c_oflag &= ~ONLCR;
  210.     }
  211.     ip->set = 1;
  212. }
  213.  
  214. void
  215. f_ospeed(ip)
  216.     struct info *ip;
  217. {
  218.     cfsetospeed(&ip->t, atoi(ip->arg));
  219.     ip->set = 1;
  220. }
  221.  
  222. void
  223. f_raw(ip)
  224.     struct info *ip;
  225. {
  226.     if (ip->off)
  227.         f_sane(ip);
  228.     else {
  229.         cfmakeraw(&ip->t);
  230.         ip->t.c_cflag &= ~(CSIZE|PARENB);
  231.         ip->t.c_cflag |= CS8;
  232.         ip->set = 1;
  233.     }
  234. }
  235.  
  236. void
  237. f_rows(ip)
  238.     struct info *ip;
  239. {
  240.     ip->win.ws_row = atoi(ip->arg);
  241.     ip->wset = 1;
  242. }
  243.  
  244. void
  245. f_sane(ip)
  246.     struct info *ip;
  247. {
  248.     ip->t.c_cflag = TTYDEF_CFLAG | (ip->t.c_cflag & CLOCAL);
  249.     ip->t.c_iflag = TTYDEF_IFLAG;
  250.     ip->t.c_iflag |= ICRNL;
  251.     /* preserve user-preference flags in lflag */
  252. #define    LKEEP    (ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH)
  253.     ip->t.c_lflag = TTYDEF_LFLAG | (ip->t.c_lflag & LKEEP);
  254.     ip->t.c_oflag = TTYDEF_OFLAG;
  255.     ip->set = 1;
  256. }
  257.  
  258. void
  259. f_size(ip)
  260.     struct info *ip;
  261. {
  262.     (void)printf("%d %d\n", ip->win.ws_row, ip->win.ws_col);
  263. }
  264.  
  265. void
  266. f_speed(ip)
  267.     struct info *ip;
  268. {
  269.     (void)printf("%d\n", cfgetospeed(&ip->t));
  270. }
  271.  
  272. void
  273. f_tty(ip)
  274.     struct info *ip;
  275. {
  276.     int tmp;
  277.  
  278.     tmp = TTYDISC;
  279.     if (ioctl(0, TIOCSETD, &tmp) < 0)
  280.         err("TIOCSETD: %s", strerror(errno));
  281. }
  282.