xref: /csrg-svn/bin/stty/cchar.c (revision 66642)
148938Sbostic /*-
2*66642Spendry  * Copyright (c) 1991, 1993, 1994
360721Sbostic  *	The Regents of the University of California.  All rights reserved.
448938Sbostic  *
548938Sbostic  * %sccs.include.redist.c%
648938Sbostic  */
748938Sbostic 
848938Sbostic #ifndef lint
9*66642Spendry static char sccsid[] = "@(#)cchar.c	8.5 (Berkeley) 04/02/94";
1048938Sbostic #endif /* not lint */
1148938Sbostic 
1248938Sbostic #include <sys/types.h>
1359518Sbostic 
1459518Sbostic #include <err.h>
1566372Sbostic #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 },
4066453Sbostic 	{ "min",	VMIN,		CMIN },
4159518Sbostic 	{ "quit",	VQUIT,		CQUIT },
4259518Sbostic 	{ "reprint",	VREPRINT, 	CREPRINT },
4359518Sbostic 	{ "start",	VSTART,		CSTART },
4459518Sbostic 	{ "status",	VSTATUS, 	CSTATUS },
4559518Sbostic 	{ "stop",	VSTOP,		CSTOP },
4659518Sbostic 	{ "susp",	VSUSP,		CSUSP },
4766453Sbostic 	{ "time",	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 
5966564Spendry static int
c_cchar(a,b)6066564Spendry c_cchar(a, b)
6166564Spendry         const void *a, *b;
6266564Spendry {
6366564Spendry 
6466564Spendry         return (strcmp(((struct cchar *)a)->name, ((struct cchar *)b)->name));
6566564Spendry }
6666564Spendry 
6759518Sbostic int
csearch(argvp,ip)6850010Sbostic csearch(argvp, ip)
6950010Sbostic 	char ***argvp;
7050010Sbostic 	struct info *ip;
7150010Sbostic {
7266564Spendry 	struct cchar *cp, tmp;
7366372Sbostic 	long val;
7466372Sbostic 	char *arg, *ep, *name;
7550010Sbostic 
7650010Sbostic 	name = **argvp;
7750010Sbostic 
7850010Sbostic 	tmp.name = name;
7950010Sbostic 	if (!(cp = (struct cchar *)bsearch(&tmp, cchars1,
8050041Sbostic 	    sizeof(cchars1)/sizeof(struct cchar) - 1, sizeof(struct cchar),
8150010Sbostic 	    c_cchar)) && !(cp = (struct cchar *)bsearch(&tmp, cchars1,
8250041Sbostic 	    sizeof(cchars1)/sizeof(struct cchar) - 1, sizeof(struct cchar),
8350010Sbostic 	    c_cchar)))
8459518Sbostic 		return (0);
8550010Sbostic 
8650010Sbostic 	arg = *++*argvp;
8759518Sbostic 	if (!arg) {
8859518Sbostic 		warnx("option requires an argument -- %s", name);
8959518Sbostic 		usage();
9059518Sbostic 	}
9150010Sbostic 
9250844Sbostic #define CHK(s)  (*arg == s[0] && !strcmp(arg, s))
9350010Sbostic 	if (CHK("undef") || CHK("<undef>"))
9450010Sbostic 		ip->t.c_cc[cp->sub] = _POSIX_VDISABLE;
9566372Sbostic 	else if (cp->sub == VMIN || cp->sub == VTIME) {
9666372Sbostic 		val = strtol(arg, &ep, 10);
9766372Sbostic 		if (val == _POSIX_VDISABLE) {
9866372Sbostic 			warnx("value of %ld would disable the option -- %s",
9966372Sbostic 			    val, name);
10066372Sbostic 			usage();
10166372Sbostic 		}
10266372Sbostic 		if (val > UCHAR_MAX) {
10366372Sbostic 			warnx("maximum option value is %d -- %s",
10466372Sbostic 			    UCHAR_MAX, name);
10566372Sbostic 			usage();
10666372Sbostic 		}
10766372Sbostic 		if (*ep != '\0') {
10866372Sbostic 			warnx("option requires a numeric argument -- %s", name);
10966372Sbostic 			usage();
11066372Sbostic 		}
11166372Sbostic 		ip->t.c_cc[cp->sub] = val;
11266372Sbostic 	} else if (arg[0] == '^')
11350010Sbostic 		ip->t.c_cc[cp->sub] = (arg[1] == '?') ? 0177 :
11450010Sbostic 		    (arg[1] == '-') ? _POSIX_VDISABLE : arg[1] & 037;
11550010Sbostic 	else
11650010Sbostic 		ip->t.c_cc[cp->sub] = arg[0];
11750010Sbostic 	ip->set = 1;
11859518Sbostic 	return (1);
11950010Sbostic }
120