xref: /original-bsd/bin/stty/cchar.c (revision 4f00418d)
1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)cchar.c	8.1 (Berkeley) 05/31/93";
10 #endif /* not lint */
11 
12 #include <sys/types.h>
13 
14 #include <err.h>
15 #include <stddef.h>
16 #include <stdlib.h>
17 #include <string.h>
18 
19 #include "stty.h"
20 #include "extern.h"
21 
22 /*
23  * Special control characters.
24  *
25  * Cchars1 are the standard names, cchars2 are the old aliases.
26  * The first are displayed, but both are recognized on the
27  * command line.
28  */
29 struct cchar cchars1[] = {
30 	{ "discard",	VDISCARD, 	CDISCARD },
31 	{ "dsusp", 	VDSUSP,		CDSUSP },
32 	{ "eof",	VEOF,		CEOF },
33 	{ "eol",	VEOL,		CEOL },
34 	{ "eol2",	VEOL2,		CEOL },
35 	{ "erase",	VERASE,		CERASE },
36 	{ "intr",	VINTR,		CINTR },
37 	{ "kill",	VKILL,		CKILL },
38 	{ "lnext",	VLNEXT,		CLNEXT },
39 	{ "quit",	VQUIT,		CQUIT },
40 	{ "reprint",	VREPRINT, 	CREPRINT },
41 	{ "start",	VSTART,		CSTART },
42 	{ "status",	VSTATUS, 	CSTATUS },
43 	{ "stop",	VSTOP,		CSTOP },
44 	{ "susp",	VSUSP,		CSUSP },
45 	{ "werase",	VWERASE,	CWERASE },
46 	{ NULL },
47 };
48 
49 struct cchar cchars2[] = {
50 	{ "brk",	VEOL,		CEOL },
51 	{ "flush",	VDISCARD, 	CDISCARD },
52 	{ "rprnt",	VREPRINT, 	CREPRINT },
53 	{ NULL },
54 };
55 
56 int
57 csearch(argvp, ip)
58 	char ***argvp;
59 	struct info *ip;
60 {
61 	register struct cchar *cp;
62 	struct cchar tmp;
63 	char *arg, *name;
64 	static int c_cchar __P((const void *, const void *));
65 
66 	name = **argvp;
67 
68 	tmp.name = name;
69 	if (!(cp = (struct cchar *)bsearch(&tmp, cchars1,
70 	    sizeof(cchars1)/sizeof(struct cchar) - 1, sizeof(struct cchar),
71 	    c_cchar)) && !(cp = (struct cchar *)bsearch(&tmp, cchars1,
72 	    sizeof(cchars1)/sizeof(struct cchar) - 1, sizeof(struct cchar),
73 	    c_cchar)))
74 		return (0);
75 
76 	arg = *++*argvp;
77 	if (!arg) {
78 		warnx("option requires an argument -- %s", name);
79 		usage();
80 	}
81 
82 #define CHK(s)  (*arg == s[0] && !strcmp(arg, s))
83 	if (CHK("undef") || CHK("<undef>"))
84 		ip->t.c_cc[cp->sub] = _POSIX_VDISABLE;
85 	else if (arg[0] == '^')
86 		ip->t.c_cc[cp->sub] = (arg[1] == '?') ? 0177 :
87 		    (arg[1] == '-') ? _POSIX_VDISABLE : arg[1] & 037;
88 	else
89 		ip->t.c_cc[cp->sub] = arg[0];
90 	ip->set = 1;
91 	return (1);
92 }
93 
94 static int
95 c_cchar(a, b)
96         const void *a, *b;
97 {
98         return (strcmp(((struct cchar *)a)->name, ((struct cchar *)b)->name));
99 }
100