cchar.c revision 127958
1185029Spjd/*-
2185029Spjd * Copyright (c) 1991, 1993, 1994
3185029Spjd *	The Regents of the University of California.  All rights reserved.
4185029Spjd *
5185029Spjd * Redistribution and use in source and binary forms, with or without
6185029Spjd * modification, are permitted provided that the following conditions
7185029Spjd * are met:
8185029Spjd * 1. Redistributions of source code must retain the above copyright
9185029Spjd *    notice, this list of conditions and the following disclaimer.
10185029Spjd * 2. Redistributions in binary form must reproduce the above copyright
11185029Spjd *    notice, this list of conditions and the following disclaimer in the
12185029Spjd *    documentation and/or other materials provided with the distribution.
13185029Spjd * 4. Neither the name of the University nor the names of its contributors
14185029Spjd *    may be used to endorse or promote products derived from this software
15185029Spjd *    without specific prior written permission.
16185029Spjd *
17185029Spjd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18185029Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19185029Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20185029Spjd * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21185029Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22219089Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23247265Smm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24185029Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25185029Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26185029Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27185029Spjd * SUCH DAMAGE.
28185029Spjd */
29185029Spjd
30185029Spjd#ifndef lint
31185029Spjd#if 0
32185029Spjdstatic char sccsid[] = "@(#)cchar.c	8.5 (Berkeley) 4/2/94";
33185029Spjd#endif
34185029Spjd#endif /* not lint */
35185029Spjd#include <sys/cdefs.h>
36185029Spjd__FBSDID("$FreeBSD: head/bin/stty/cchar.c 127958 2004-04-06 20:06:54Z markm $");
37185029Spjd
38185029Spjd#include <sys/types.h>
39185029Spjd
40185029Spjd#include <err.h>
41185029Spjd#include <limits.h>
42185029Spjd#include <stddef.h>
43185029Spjd#include <stdlib.h>
44185029Spjd#include <string.h>
45185029Spjd
46185029Spjd#include "stty.h"
47185029Spjd#include "extern.h"
48185029Spjd
49185029Spjdstatic int c_cchar(const void *, const void *);
50185029Spjd
51185029Spjd/*
52185029Spjd * Special control characters.
53185029Spjd *
54185029Spjd * Cchars1 are the standard names, cchars2 are the old aliases.
55185029Spjd * The first are displayed, but both are recognized on the
56185029Spjd * command line.
57185029Spjd */
58185029Spjdstruct cchar cchars1[] = {
59185029Spjd	{ "discard",	VDISCARD, 	CDISCARD },
60185029Spjd	{ "dsusp", 	VDSUSP,		CDSUSP },
61185029Spjd	{ "eof",	VEOF,		CEOF },
62185029Spjd	{ "eol",	VEOL,		CEOL },
63185029Spjd	{ "eol2",	VEOL2,		CEOL },
64185029Spjd	{ "erase",	VERASE,		CERASE },
65185029Spjd	{ "erase2",	VERASE2,	CERASE2 },
66185029Spjd	{ "intr",	VINTR,		CINTR },
67185029Spjd	{ "kill",	VKILL,		CKILL },
68219089Spjd	{ "lnext",	VLNEXT,		CLNEXT },
69219089Spjd	{ "min",	VMIN,		CMIN },
70219089Spjd	{ "quit",	VQUIT,		CQUIT },
71219089Spjd	{ "reprint",	VREPRINT, 	CREPRINT },
72219089Spjd	{ "start",	VSTART,		CSTART },
73219089Spjd	{ "status",	VSTATUS, 	CSTATUS },
74219089Spjd	{ "stop",	VSTOP,		CSTOP },
75219089Spjd	{ "susp",	VSUSP,		CSUSP },
76219089Spjd	{ "time",	VTIME,		CTIME },
77219089Spjd	{ "werase",	VWERASE,	CWERASE },
78219089Spjd	{ NULL,		0,		0},
79219089Spjd};
80185029Spjd
81185029Spjdstruct cchar cchars2[] = {
82185029Spjd	{ "brk",	VEOL,		CEOL },
83185029Spjd	{ "flush",	VDISCARD, 	CDISCARD },
84185029Spjd	{ "rprnt",	VREPRINT, 	CREPRINT },
85185029Spjd	{ NULL,		0,		0 },
86185029Spjd};
87219089Spjd
88185029Spjdstatic int
89185029Spjdc_cchar(const void *a, const void *b)
90185029Spjd{
91185029Spjd
92219089Spjd        return (strcmp(((const struct cchar *)a)->name, ((const struct cchar *)b)->name));
93185029Spjd}
94219089Spjd
95219089Spjdint
96185029Spjdcsearch(char ***argvp, struct info *ip)
97185029Spjd{
98185029Spjd	struct cchar *cp, tmp;
99185029Spjd	long val;
100185029Spjd	char *arg, *ep, *name;
101185029Spjd
102185029Spjd	name = **argvp;
103185029Spjd
104185029Spjd	tmp.name = name;
105185029Spjd	if (!(cp = (struct cchar *)bsearch(&tmp, cchars1,
106185029Spjd	    sizeof(cchars1)/sizeof(struct cchar) - 1, sizeof(struct cchar),
107185029Spjd	    c_cchar)) && !(cp = (struct cchar *)bsearch(&tmp, cchars2,
108185029Spjd	    sizeof(cchars2)/sizeof(struct cchar) - 1, sizeof(struct cchar),
109185029Spjd	    c_cchar)))
110185029Spjd		return (0);
111185029Spjd
112185029Spjd	arg = *++*argvp;
113185029Spjd	if (!arg) {
114185029Spjd		warnx("option requires an argument -- %s", name);
115185029Spjd		usage();
116185029Spjd	}
117185029Spjd
118185029Spjd#define CHK(s)  (*arg == s[0] && !strcmp(arg, s))
119185029Spjd	if (CHK("undef") || CHK("<undef>"))
120185029Spjd		ip->t.c_cc[cp->sub] = _POSIX_VDISABLE;
121185029Spjd	else if (cp->sub == VMIN || cp->sub == VTIME) {
122185029Spjd		val = strtol(arg, &ep, 10);
123185029Spjd		if (val > UCHAR_MAX) {
124185029Spjd			warnx("maximum option value is %d -- %s",
125185029Spjd			    UCHAR_MAX, name);
126185029Spjd			usage();
127185029Spjd		}
128185029Spjd		if (*ep != '\0') {
129185029Spjd			warnx("option requires a numeric argument -- %s", name);
130185029Spjd			usage();
131185029Spjd		}
132185029Spjd		ip->t.c_cc[cp->sub] = val;
133185029Spjd	} else if (arg[0] == '^')
134185029Spjd		ip->t.c_cc[cp->sub] = (arg[1] == '?') ? 0177 :
135185029Spjd		    (arg[1] == '-') ? _POSIX_VDISABLE : arg[1] & 037;
136185029Spjd	else
137185029Spjd		ip->t.c_cc[cp->sub] = arg[0];
138185029Spjd	ip->set = 1;
139185029Spjd	return (1);
140185029Spjd}
141185029Spjd