]> git.cameronkatri.com Git - apple_cmds.git/blob - adv_cmds/stty/cchar.c
shell_cmds: All compiled
[apple_cmds.git] / adv_cmds / stty / cchar.c
1 /*-
2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)cchar.c 8.5 (Berkeley) 4/2/94";
37 #endif
38 #endif /* not lint */
39 #include <sys/cdefs.h>
40 __RCSID("$FreeBSD: src/bin/stty/cchar.c,v 1.13 2002/06/30 05:15:04 obrien Exp $");
41
42 #include <sys/types.h>
43
44 #include <err.h>
45 #include <limits.h>
46 #include <stddef.h>
47 #include <stdlib.h>
48 #include <string.h>
49
50 #include "stty.h"
51 #include "extern.h"
52
53 static int c_cchar(const void *, const void *);
54
55 /*
56 * Special control characters.
57 *
58 * Cchars1 are the standard names, cchars2 are the old aliases.
59 * The first are displayed, but both are recognized on the
60 * command line.
61 */
62 struct cchar cchars1[] = {
63 { "discard", VDISCARD, CDISCARD },
64 { "dsusp", VDSUSP, CDSUSP },
65 { "eof", VEOF, CEOF },
66 { "eol", VEOL, CEOL },
67 { "eol2", VEOL2, CEOL },
68 { "erase", VERASE, CERASE },
69 #ifndef __APPLE__
70 { "erase2", VERASE2, CERASE2 },
71 #endif
72 { "intr", VINTR, CINTR },
73 { "kill", VKILL, CKILL },
74 { "lnext", VLNEXT, CLNEXT },
75 { "min", VMIN, CMIN },
76 { "quit", VQUIT, CQUIT },
77 { "reprint", VREPRINT, CREPRINT },
78 { "start", VSTART, CSTART },
79 { "status", VSTATUS, CSTATUS },
80 { "stop", VSTOP, CSTOP },
81 { "susp", VSUSP, CSUSP },
82 { "time", VTIME, CTIME },
83 { "werase", VWERASE, CWERASE },
84 { NULL, 0, 0},
85 };
86
87 struct cchar cchars2[] = {
88 { "brk", VEOL, CEOL },
89 { "flush", VDISCARD, CDISCARD },
90 { "rprnt", VREPRINT, CREPRINT },
91 { NULL, 0, 0 },
92 };
93
94 static int
95 c_cchar(const void *a, const void *b)
96 {
97
98 return (strcmp(((const struct cchar *)a)->name, ((const struct cchar *)b)->name));
99 }
100
101 int
102 csearch(char ***argvp, struct info *ip)
103 {
104 struct cchar *cp, tmp;
105 long val;
106 char *arg, *ep, *name;
107
108 name = **argvp;
109
110 tmp.name = name;
111 if (!(cp = (struct cchar *)bsearch(&tmp, cchars1,
112 sizeof(cchars1)/sizeof(struct cchar) - 1, sizeof(struct cchar),
113 c_cchar)) && !(cp = (struct cchar *)bsearch(&tmp, cchars2,
114 sizeof(cchars2)/sizeof(struct cchar) - 1, sizeof(struct cchar),
115 c_cchar)))
116 return (0);
117
118 arg = *++*argvp;
119 if (!arg) {
120 warnx("option requires an argument -- %s", name);
121 usage();
122 }
123
124 #define CHK(s) (*arg == s[0] && !strcmp(arg, s))
125 if (CHK("undef") || CHK("<undef>"))
126 ip->t.c_cc[cp->sub] = _POSIX_VDISABLE;
127 else if (cp->sub == VMIN || cp->sub == VTIME) {
128 val = strtol(arg, &ep, 10);
129 #ifdef __APPLE__
130 if (val == _POSIX_VDISABLE) {
131 warnx("value of %ld would disable the option -- %s",
132 val, name);
133 usage();
134 }
135 #endif
136 if (val > UCHAR_MAX) {
137 warnx("maximum option value is %d -- %s",
138 UCHAR_MAX, name);
139 usage();
140 }
141 if (*ep != '\0') {
142 warnx("option requires a numeric argument -- %s", name);
143 usage();
144 }
145 ip->t.c_cc[cp->sub] = val;
146 } else if (arg[0] == '^')
147 ip->t.c_cc[cp->sub] = (arg[1] == '?') ? 0177 :
148 (arg[1] == '-') ? _POSIX_VDISABLE : arg[1] & 037;
149 else
150 ip->t.c_cc[cp->sub] = arg[0];
151 ip->set = 1;
152 return (1);
153 }