xref: /freebsd/bin/stty/cchar.c (revision 5b31cc94b10d4bb7109c6b27940a0fc76a44a331)
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 #endif /* not lint */
32 #include <sys/cdefs.h>
33 #include <sys/types.h>
34 
35 #include <err.h>
36 #include <limits.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 #include "stty.h"
41 #include "extern.h"
42 
43 static int c_cchar(const void *, const void *);
44 
45 /*
46  * Special control characters.
47  *
48  * Cchars1 are the standard names, cchars2 are the old aliases.
49  * The first are displayed, but both are recognized on the
50  * command line.
51  */
52 struct cchar cchars1[] = {
53 	{ "discard",	VDISCARD, 	CDISCARD },
54 	{ "dsusp", 	VDSUSP,		CDSUSP },
55 	{ "eof",	VEOF,		CEOF },
56 	{ "eol",	VEOL,		CEOL },
57 	{ "eol2",	VEOL2,		CEOL },
58 	{ "erase",	VERASE,		CERASE },
59 	{ "erase2",	VERASE2,	CERASE2 },
60 	{ "intr",	VINTR,		CINTR },
61 	{ "kill",	VKILL,		CKILL },
62 	{ "lnext",	VLNEXT,		CLNEXT },
63 	{ "min",	VMIN,		CMIN },
64 	{ "quit",	VQUIT,		CQUIT },
65 	{ "reprint",	VREPRINT, 	CREPRINT },
66 	{ "start",	VSTART,		CSTART },
67 	{ "status",	VSTATUS, 	CSTATUS },
68 	{ "stop",	VSTOP,		CSTOP },
69 	{ "susp",	VSUSP,		CSUSP },
70 	{ "time",	VTIME,		CTIME },
71 	{ "werase",	VWERASE,	CWERASE },
72 	{ NULL,		0,		0},
73 };
74 
75 struct cchar cchars2[] = {
76 	{ "brk",	VEOL,		CEOL },
77 	{ "flush",	VDISCARD, 	CDISCARD },
78 	{ "rprnt",	VREPRINT, 	CREPRINT },
79 	{ NULL,		0,		0 },
80 };
81 
82 static int
83 c_cchar(const void *a, const void *b)
84 {
85 
86         return (strcmp(((const struct cchar *)a)->name, ((const struct cchar *)b)->name));
87 }
88 
89 int
90 csearch(char ***argvp, struct info *ip)
91 {
92 	struct cchar *cp, tmp;
93 	long val;
94 	char *arg, *ep, *name;
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, cchars2,
102 	    sizeof(cchars2)/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 > UCHAR_MAX) {
118 			warnx("maximum option value is %d -- %s",
119 			    UCHAR_MAX, name);
120 			usage();
121 		}
122 		if (*ep != '\0') {
123 			warnx("option requires a numeric argument -- %s", name);
124 			usage();
125 		}
126 		ip->t.c_cc[cp->sub] = val;
127 	} else if (arg[0] == '^')
128 		ip->t.c_cc[cp->sub] = (arg[1] == '?') ? 0177 :
129 		    (arg[1] == '-') ? _POSIX_VDISABLE : arg[1] & 037;
130 	else
131 		ip->t.c_cc[cp->sub] = arg[0];
132 	ip->set = 1;
133 	return (1);
134 }
135