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