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