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