xref: /netbsd-src/bin/stty/cchar.c (revision cda4f8f6ee55684e8d311b86c99ea59191e6b74f)
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * 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 /* from: static char sccsid[] = "@(#)cchar.c	5.4 (Berkeley) 6/10/91"; */
36 static char rcsid[] = "$Id: cchar.c,v 1.4 1993/06/01 14:42:10 cgd Exp $";
37 #endif /* not lint */
38 
39 #include <sys/types.h>
40 #include <stddef.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include "stty.h"
44 #include "extern.h"
45 
46 /*
47  * Special control characters.
48  *
49  * Cchars1 are the standard names, cchars2 are the old aliases.
50  * The first are displayed, but both are recognized on the
51  * command line.
52  */
53 struct cchar cchars1[] = {
54 	"discard",	VDISCARD, 	CDISCARD,
55 	"dsusp", 	VDSUSP,		CDSUSP,
56 	"eof",		VEOF,		CEOF,
57 	"eol",		VEOL,		CEOL,
58 	"eol2",		VEOL2,		CEOL,
59 	"erase",	VERASE,		CERASE,
60 	"intr",		VINTR,		CINTR,
61 	"kill",		VKILL,		CKILL,
62 	"lnext",	VLNEXT,		CLNEXT,
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 	"werase",	VWERASE,	CWERASE,
70 	NULL,
71 };
72 
73 struct cchar cchars2[] = {
74 	"brk",		VEOL,		CEOL,
75 	"flush",	VDISCARD, 	CDISCARD,
76 	"rprnt",	VREPRINT, 	CREPRINT,
77 	"xoff",		VSTOP,		CSTOP,
78 	"xon",		VSTART,		CSTART,
79 	NULL,
80 };
81 
82 csearch(argvp, ip)
83 	char ***argvp;
84 	struct info *ip;
85 {
86 	extern char *usage;
87 	register struct cchar *cp;
88 	struct cchar tmp;
89 	char *arg, *name;
90 	static int c_cchar __P((const void *, const void *));
91 
92 	name = **argvp;
93 
94 	tmp.name = name;
95 	if (!(cp = (struct cchar *)bsearch(&tmp, cchars1,
96 	    sizeof(cchars1)/sizeof(struct cchar) - 1, sizeof(struct cchar),
97 	    c_cchar)) && !(cp = (struct cchar *)bsearch(&tmp, cchars1,
98 	    sizeof(cchars1)/sizeof(struct cchar) - 1, sizeof(struct cchar),
99 	    c_cchar)))
100 		return(0);
101 
102 	arg = *++*argvp;
103 	if (!arg)
104 		err("option requires an argument -- %s\n%s", name, usage);
105 
106 #define CHK(s)  (*name == s[0] && !strcmp(name, s))
107 	if (CHK("undef") || CHK("<undef>"))
108 		ip->t.c_cc[cp->sub] = _POSIX_VDISABLE;
109 	else if (arg[0] == '^')
110 		ip->t.c_cc[cp->sub] = (arg[1] == '?') ? 0177 :
111 		    (arg[1] == '-') ? _POSIX_VDISABLE : arg[1] & 037;
112 	else
113 		ip->t.c_cc[cp->sub] = arg[0];
114 	ip->set = 1;
115 	return(1);
116 }
117 
118 static
119 c_cchar(a, b)
120         const void *a, *b;
121 {
122         return(strcmp(((struct cchar *)a)->name, ((struct cchar *)b)->name));
123 }
124