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