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