xref: /freebsd/bin/stty/cchar.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef lint
31 #if 0
32 static char sccsid[] = "@(#)cchar.c	8.5 (Berkeley) 4/2/94";
33 #endif
34 #endif /* not lint */
35 #include <sys/cdefs.h>
36 #include <sys/types.h>
37 
38 #include <err.h>
39 #include <limits.h>
40 #include <stdlib.h>
41 #include <string.h>
42 
43 #include "stty.h"
44 #include "extern.h"
45 
46 static int c_cchar(const void *, const void *);
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 	{ "erase2",	VERASE2,	CERASE2 },
63 	{ "intr",	VINTR,		CINTR },
64 	{ "kill",	VKILL,		CKILL },
65 	{ "lnext",	VLNEXT,		CLNEXT },
66 	{ "min",	VMIN,		CMIN },
67 	{ "quit",	VQUIT,		CQUIT },
68 	{ "reprint",	VREPRINT, 	CREPRINT },
69 	{ "start",	VSTART,		CSTART },
70 	{ "status",	VSTATUS, 	CSTATUS },
71 	{ "stop",	VSTOP,		CSTOP },
72 	{ "susp",	VSUSP,		CSUSP },
73 	{ "time",	VTIME,		CTIME },
74 	{ "werase",	VWERASE,	CWERASE },
75 	{ NULL,		0,		0},
76 };
77 
78 struct cchar cchars2[] = {
79 	{ "brk",	VEOL,		CEOL },
80 	{ "flush",	VDISCARD, 	CDISCARD },
81 	{ "rprnt",	VREPRINT, 	CREPRINT },
82 	{ NULL,		0,		0 },
83 };
84 
85 static int
86 c_cchar(const void *a, const void *b)
87 {
88 
89         return (strcmp(((const struct cchar *)a)->name, ((const struct cchar *)b)->name));
90 }
91 
92 int
93 csearch(char ***argvp, struct info *ip)
94 {
95 	struct cchar *cp, tmp;
96 	long val;
97 	char *arg, *ep, *name;
98 
99 	name = **argvp;
100 
101 	tmp.name = name;
102 	if (!(cp = (struct cchar *)bsearch(&tmp, cchars1,
103 	    sizeof(cchars1)/sizeof(struct cchar) - 1, sizeof(struct cchar),
104 	    c_cchar)) && !(cp = (struct cchar *)bsearch(&tmp, cchars2,
105 	    sizeof(cchars2)/sizeof(struct cchar) - 1, sizeof(struct cchar),
106 	    c_cchar)))
107 		return (0);
108 
109 	arg = *++*argvp;
110 	if (!arg) {
111 		warnx("option requires an argument -- %s", name);
112 		usage();
113 	}
114 
115 #define CHK(s)  (*arg == s[0] && !strcmp(arg, s))
116 	if (CHK("undef") || CHK("<undef>"))
117 		ip->t.c_cc[cp->sub] = _POSIX_VDISABLE;
118 	else if (cp->sub == VMIN || cp->sub == VTIME) {
119 		val = strtol(arg, &ep, 10);
120 		if (val > UCHAR_MAX) {
121 			warnx("maximum option value is %d -- %s",
122 			    UCHAR_MAX, name);
123 			usage();
124 		}
125 		if (*ep != '\0') {
126 			warnx("option requires a numeric argument -- %s", name);
127 			usage();
128 		}
129 		ip->t.c_cc[cp->sub] = val;
130 	} else if (arg[0] == '^')
131 		ip->t.c_cc[cp->sub] = (arg[1] == '?') ? 0177 :
132 		    (arg[1] == '-') ? _POSIX_VDISABLE : arg[1] & 037;
133 	else
134 		ip->t.c_cc[cp->sub] = arg[0];
135 	ip->set = 1;
136 	return (1);
137 }
138