xref: /netbsd-src/bin/stty/cchar.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /* $NetBSD: cchar.c,v 1.14 2003/06/16 17:22:00 perry Exp $ */
2 
3 /*-
4  * Copyright (c) 1991, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)cchar.c	8.5 (Berkeley) 4/2/94";
40 #else
41 __RCSID("$NetBSD: cchar.c,v 1.14 2003/06/16 17:22:00 perry Exp $");
42 #endif
43 #endif /* not lint */
44 
45 #include <sys/types.h>
46 
47 #include <err.h>
48 #include <limits.h>
49 #include <stddef.h>
50 #include <stdlib.h>
51 #include <string.h>
52 
53 #include "stty.h"
54 #include "extern.h"
55 
56 /*
57  * Special control characters.
58  *
59  * Cchars1 are the standard names, cchars2 are the old aliases.
60  * The first are displayed, but both are recognized on the
61  * command line.
62  */
63 const struct cchar cchars1[] = {
64 	{ "discard",	VDISCARD, 	CDISCARD },
65 	{ "dsusp", 	VDSUSP,		CDSUSP },
66 	{ "eof",	VEOF,		CEOF },
67 	{ "eol",	VEOL,		CEOL },
68 	{ "eol2",	VEOL2,		CEOL },
69 	{ "erase",	VERASE,		CERASE },
70 	{ "intr",	VINTR,		CINTR },
71 	{ "kill",	VKILL,		CKILL },
72 	{ "lnext",	VLNEXT,		CLNEXT },
73 	{ "min",	VMIN,		CMIN },
74 	{ "quit",	VQUIT,		CQUIT },
75 	{ "reprint",	VREPRINT, 	CREPRINT },
76 	{ "start",	VSTART,		CSTART },
77 	{ "status",	VSTATUS, 	CSTATUS },
78 	{ "stop",	VSTOP,		CSTOP },
79 	{ "susp",	VSUSP,		CSUSP },
80 	{ "time",	VTIME,		CTIME },
81 	{ "werase",	VWERASE,	CWERASE },
82 	{ NULL },
83 };
84 
85 const struct cchar cchars2[] = {
86 	{ "brk",	VEOL,		CEOL },
87 	{ "flush",	VDISCARD, 	CDISCARD },
88 	{ "rprnt",	VREPRINT, 	CREPRINT },
89 	{ NULL },
90 };
91 
92 static int c_cchar(const void *, const void *);
93 
94 static int
95 c_cchar(const void *a, const void *b)
96 {
97         return (strcmp(((const struct cchar *)a)->name,
98 	    ((const struct cchar *)b)->name));
99 }
100 
101 int
102 csearch(char ***argvp, struct info *ip)
103 {
104 	struct cchar *cp, tmp;
105 	long val;
106 	char *arg, *ep, *name;
107 
108 	name = **argvp;
109 
110 	tmp.name = name;
111 	if (!(cp = (struct cchar *)bsearch(&tmp, cchars1,
112 	    sizeof(cchars1)/sizeof(cchars1[0]) - 1, sizeof(cchars1[0]),
113 	    c_cchar)) &&
114 	    !(cp = (struct cchar *)bsearch(&tmp, cchars2,
115 	    sizeof(cchars2)/sizeof(cchars2[0]) - 1, sizeof(cchars2[0]),
116 	    c_cchar)))
117 		return (0);
118 
119 	arg = *++*argvp;
120 	if (!arg) {
121 		warnx("option requires an argument -- %s", name);
122 		usage();
123 	}
124 
125 #define CHK(s)  (*arg == s[0] && !strcmp(arg, s))
126 	if (CHK("undef") || CHK("<undef>"))
127 		ip->t.c_cc[cp->sub] = _POSIX_VDISABLE;
128 	else if (cp->sub == VMIN || cp->sub == VTIME) {
129 		val = strtol(arg, &ep, 10);
130 		if (val == _POSIX_VDISABLE) {
131 			warnx("value of %ld would disable the option -- %s",
132 			    val, name);
133 			usage();
134 		}
135 		if (val > UCHAR_MAX) {
136 			warnx("maximum option value is %d -- %s",
137 			    UCHAR_MAX, name);
138 			usage();
139 		}
140 		if (*ep != '\0') {
141 			warnx("option requires a numeric argument -- %s", name);
142 			usage();
143 		}
144 		ip->t.c_cc[cp->sub] = (cc_t)val;
145 	} else if (arg[0] == '^')
146 		ip->t.c_cc[cp->sub] = (arg[1] == '?') ? 0177 :
147 		    (arg[1] == '-') ? _POSIX_VDISABLE : arg[1] & 037;
148 	else
149 		ip->t.c_cc[cp->sub] = arg[0];
150 	ip->set = 1;
151 	return (1);
152 }
153