xref: /csrg-svn/bin/stty/cchar.c (revision 66372)
148938Sbostic /*-
260721Sbostic  * Copyright (c) 1991, 1993
360721Sbostic  *	The Regents of the University of California.  All rights reserved.
448938Sbostic  *
548938Sbostic  * %sccs.include.redist.c%
648938Sbostic  */
748938Sbostic 
848938Sbostic #ifndef lint
9*66372Sbostic static char sccsid[] = "@(#)cchar.c	8.2 (Berkeley) 03/17/94";
1048938Sbostic #endif /* not lint */
1148938Sbostic 
1248938Sbostic #include <sys/types.h>
1359518Sbostic 
1459518Sbostic #include <err.h>
15*66372Sbostic #include <limits.h>
1648938Sbostic #include <stddef.h>
1750010Sbostic #include <stdlib.h>
1850010Sbostic #include <string.h>
1959518Sbostic 
2048938Sbostic #include "stty.h"
2150010Sbostic #include "extern.h"
2248938Sbostic 
2348938Sbostic /*
2448938Sbostic  * Special control characters.
2548938Sbostic  *
2648938Sbostic  * Cchars1 are the standard names, cchars2 are the old aliases.
2748938Sbostic  * The first are displayed, but both are recognized on the
2848938Sbostic  * command line.
2948938Sbostic  */
3048938Sbostic struct cchar cchars1[] = {
3159518Sbostic 	{ "discard",	VDISCARD, 	CDISCARD },
3259518Sbostic 	{ "dsusp", 	VDSUSP,		CDSUSP },
3359518Sbostic 	{ "eof",	VEOF,		CEOF },
3459518Sbostic 	{ "eol",	VEOL,		CEOL },
3559518Sbostic 	{ "eol2",	VEOL2,		CEOL },
3659518Sbostic 	{ "erase",	VERASE,		CERASE },
3759518Sbostic 	{ "intr",	VINTR,		CINTR },
3859518Sbostic 	{ "kill",	VKILL,		CKILL },
3959518Sbostic 	{ "lnext",	VLNEXT,		CLNEXT },
4059518Sbostic 	{ "quit",	VQUIT,		CQUIT },
4159518Sbostic 	{ "reprint",	VREPRINT, 	CREPRINT },
4259518Sbostic 	{ "start",	VSTART,		CSTART },
4359518Sbostic 	{ "status",	VSTATUS, 	CSTATUS },
4459518Sbostic 	{ "stop",	VSTOP,		CSTOP },
4559518Sbostic 	{ "susp",	VSUSP,		CSUSP },
46*66372Sbostic 	{ "vmin",	VMIN,		CMIN },
47*66372Sbostic 	{ "vtime",	VTIME,		CTIME },
4859518Sbostic 	{ "werase",	VWERASE,	CWERASE },
4959518Sbostic 	{ NULL },
5048938Sbostic };
5148938Sbostic 
5248938Sbostic struct cchar cchars2[] = {
5359518Sbostic 	{ "brk",	VEOL,		CEOL },
5459518Sbostic 	{ "flush",	VDISCARD, 	CDISCARD },
5559518Sbostic 	{ "rprnt",	VREPRINT, 	CREPRINT },
5659518Sbostic 	{ NULL },
5748938Sbostic };
5850010Sbostic 
5959518Sbostic int
6050010Sbostic csearch(argvp, ip)
6150010Sbostic 	char ***argvp;
6250010Sbostic 	struct info *ip;
6350010Sbostic {
6450010Sbostic 	register struct cchar *cp;
6550010Sbostic 	struct cchar tmp;
66*66372Sbostic 	long val;
67*66372Sbostic 	char *arg, *ep, *name;
6850010Sbostic 	static int c_cchar __P((const void *, const void *));
6950010Sbostic 
7050010Sbostic 	name = **argvp;
7150010Sbostic 
7250010Sbostic 	tmp.name = name;
7350010Sbostic 	if (!(cp = (struct cchar *)bsearch(&tmp, cchars1,
7450041Sbostic 	    sizeof(cchars1)/sizeof(struct cchar) - 1, sizeof(struct cchar),
7550010Sbostic 	    c_cchar)) && !(cp = (struct cchar *)bsearch(&tmp, cchars1,
7650041Sbostic 	    sizeof(cchars1)/sizeof(struct cchar) - 1, sizeof(struct cchar),
7750010Sbostic 	    c_cchar)))
7859518Sbostic 		return (0);
7950010Sbostic 
8050010Sbostic 	arg = *++*argvp;
8159518Sbostic 	if (!arg) {
8259518Sbostic 		warnx("option requires an argument -- %s", name);
8359518Sbostic 		usage();
8459518Sbostic 	}
8550010Sbostic 
8650844Sbostic #define CHK(s)  (*arg == s[0] && !strcmp(arg, s))
8750010Sbostic 	if (CHK("undef") || CHK("<undef>"))
8850010Sbostic 		ip->t.c_cc[cp->sub] = _POSIX_VDISABLE;
89*66372Sbostic 	else if (cp->sub == VMIN || cp->sub == VTIME) {
90*66372Sbostic 		val = strtol(arg, &ep, 10);
91*66372Sbostic 		if (val == _POSIX_VDISABLE) {
92*66372Sbostic 			warnx("value of %ld would disable the option -- %s",
93*66372Sbostic 			    val, name);
94*66372Sbostic 			usage();
95*66372Sbostic 		}
96*66372Sbostic 		if (val > UCHAR_MAX) {
97*66372Sbostic 			warnx("maximum option value is %d -- %s",
98*66372Sbostic 			    UCHAR_MAX, name);
99*66372Sbostic 			usage();
100*66372Sbostic 		}
101*66372Sbostic 		if (*ep != '\0') {
102*66372Sbostic 			warnx("option requires a numeric argument -- %s", name);
103*66372Sbostic 			usage();
104*66372Sbostic 		}
105*66372Sbostic 		ip->t.c_cc[cp->sub] = val;
106*66372Sbostic 	} else if (arg[0] == '^')
10750010Sbostic 		ip->t.c_cc[cp->sub] = (arg[1] == '?') ? 0177 :
10850010Sbostic 		    (arg[1] == '-') ? _POSIX_VDISABLE : arg[1] & 037;
10950010Sbostic 	else
11050010Sbostic 		ip->t.c_cc[cp->sub] = arg[0];
11150010Sbostic 	ip->set = 1;
11259518Sbostic 	return (1);
11350010Sbostic }
11450010Sbostic 
11559518Sbostic static int
11650010Sbostic c_cchar(a, b)
11750010Sbostic         const void *a, *b;
11850010Sbostic {
11959518Sbostic         return (strcmp(((struct cchar *)a)->name, ((struct cchar *)b)->name));
12050010Sbostic }
121