bcdddabc6fb964ce0037e28ae90e42aa7b5cd2b7
[dragonfly.git] / bin / stty / cchar.c
1 /*-
2  * Copyright (c) 1991, 1993, 1994
3  *      The Regents of the University of California.  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  * @(#)cchar.c  8.5 (Berkeley) 4/2/94
34  * $FreeBSD: src/bin/stty/cchar.c,v 1.9.2.2 2001/07/04 22:40:00 kris Exp $
35  * $DragonFly: src/bin/stty/cchar.c,v 1.2 2003/06/17 04:22:50 dillon Exp $
36  */
37
38 #include <sys/types.h>
39
40 #include <err.h>
41 #include <limits.h>
42 #include <stddef.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include "stty.h"
47 #include "extern.h"
48
49 static int c_cchar __P((const void *, const void *));
50
51 /*
52  * Special control characters.
53  *
54  * Cchars1 are the standard names, cchars2 are the old aliases.
55  * The first are displayed, but both are recognized on the
56  * command line.
57  */
58 struct cchar cchars1[] = {
59         { "discard",    VDISCARD,       CDISCARD },
60         { "dsusp",      VDSUSP,         CDSUSP },
61         { "eof",        VEOF,           CEOF },
62         { "eol",        VEOL,           CEOL },
63         { "eol2",       VEOL2,          CEOL },
64         { "erase",      VERASE,         CERASE },
65         { "erase2",     VERASE2,        CERASE2 },
66         { "intr",       VINTR,          CINTR },
67         { "kill",       VKILL,          CKILL },
68         { "lnext",      VLNEXT,         CLNEXT },
69         { "min",        VMIN,           CMIN },
70         { "quit",       VQUIT,          CQUIT },
71         { "reprint",    VREPRINT,       CREPRINT },
72         { "start",      VSTART,         CSTART },
73         { "status",     VSTATUS,        CSTATUS },
74         { "stop",       VSTOP,          CSTOP },
75         { "susp",       VSUSP,          CSUSP },
76         { "time",       VTIME,          CTIME },
77         { "werase",     VWERASE,        CWERASE },
78         { NULL,         0,              0},
79 };
80
81 struct cchar cchars2[] = {
82         { "brk",        VEOL,           CEOL },
83         { "flush",      VDISCARD,       CDISCARD },
84         { "rprnt",      VREPRINT,       CREPRINT },
85         { NULL,         0,              0 },
86 };
87
88 static int
89 c_cchar(a, b)
90         const void *a, *b;
91 {
92
93         return (strcmp(((const struct cchar *)a)->name, ((const struct cchar *)b)->name));
94 }
95
96 int
97 csearch(argvp, ip)
98         char ***argvp;
99         struct info *ip;
100 {
101         struct cchar *cp, tmp;
102         long val;
103         char *arg, *ep, *name;
104
105         name = **argvp;
106
107         tmp.name = name;
108         if (!(cp = (struct cchar *)bsearch(&tmp, cchars1,
109             sizeof(cchars1)/sizeof(struct cchar) - 1, sizeof(struct cchar),
110             c_cchar)) && !(cp = (struct cchar *)bsearch(&tmp, cchars2,
111             sizeof(cchars2)/sizeof(struct cchar) - 1, sizeof(struct cchar),
112             c_cchar)))
113                 return (0);
114
115         arg = *++*argvp;
116         if (!arg) {
117                 warnx("option requires an argument -- %s", name);
118                 usage();
119         }
120
121 #define CHK(s)  (*arg == s[0] && !strcmp(arg, s))
122         if (CHK("undef") || CHK("<undef>"))
123                 ip->t.c_cc[cp->sub] = _POSIX_VDISABLE;
124         else if (cp->sub == VMIN || cp->sub == VTIME) {
125                 val = strtol(arg, &ep, 10);
126                 if (val > UCHAR_MAX) {
127                         warnx("maximum option value is %d -- %s",
128                             UCHAR_MAX, name);
129                         usage();
130                 }
131                 if (*ep != '\0') {
132                         warnx("option requires a numeric argument -- %s", name);
133                         usage();
134                 }
135                 ip->t.c_cc[cp->sub] = val;
136         } else if (arg[0] == '^')
137                 ip->t.c_cc[cp->sub] = (arg[1] == '?') ? 0177 :
138                     (arg[1] == '-') ? _POSIX_VDISABLE : arg[1] & 037;
139         else
140                 ip->t.c_cc[cp->sub] = arg[0];
141         ip->set = 1;
142         return (1);
143 }