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