Home | History | Annotate | Line # | Download | only in stty
cchar.c revision 1.9
      1 /*	$NetBSD: cchar.c,v 1.9 1995/03/21 09:11:15 cgd 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 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)cchar.c	8.5 (Berkeley) 4/2/94";
     39 #else
     40 static char rcsid[] = "$NetBSD: cchar.c,v 1.9 1995/03/21 09:11:15 cgd Exp $";
     41 #endif
     42 #endif /* not lint */
     43 
     44 #include <sys/types.h>
     45 
     46 #include <err.h>
     47 #include <limits.h>
     48 #include <stddef.h>
     49 #include <stdlib.h>
     50 #include <string.h>
     51 
     52 #include "stty.h"
     53 #include "extern.h"
     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 	{ "intr",	VINTR,		CINTR },
     70 	{ "kill",	VKILL,		CKILL },
     71 	{ "lnext",	VLNEXT,		CLNEXT },
     72 	{ "min",	VMIN,		CMIN },
     73 	{ "quit",	VQUIT,		CQUIT },
     74 	{ "reprint",	VREPRINT, 	CREPRINT },
     75 	{ "start",	VSTART,		CSTART },
     76 	{ "status",	VSTATUS, 	CSTATUS },
     77 	{ "stop",	VSTOP,		CSTOP },
     78 	{ "susp",	VSUSP,		CSUSP },
     79 	{ "time",	VTIME,		CTIME },
     80 	{ "werase",	VWERASE,	CWERASE },
     81 	{ NULL },
     82 };
     83 
     84 struct cchar cchars2[] = {
     85 	{ "brk",	VEOL,		CEOL },
     86 	{ "flush",	VDISCARD, 	CDISCARD },
     87 	{ "rprnt",	VREPRINT, 	CREPRINT },
     88 	{ NULL },
     89 };
     90 
     91 static int
     92 c_cchar(a, b)
     93         const void *a, *b;
     94 {
     95 
     96         return (strcmp(((struct cchar *)a)->name, ((struct cchar *)b)->name));
     97 }
     98 
     99 int
    100 csearch(argvp, ip)
    101 	char ***argvp;
    102 	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, cchars1,
    114 	    sizeof(cchars1)/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 		if (val == _POSIX_VDISABLE) {
    130 			warnx("value of %ld would disable the option -- %s",
    131 			    val, name);
    132 			usage();
    133 		}
    134 		if (val > UCHAR_MAX) {
    135 			warnx("maximum option value is %d -- %s",
    136 			    UCHAR_MAX, name);
    137 			usage();
    138 		}
    139 		if (*ep != '\0') {
    140 			warnx("option requires a numeric argument -- %s", name);
    141 			usage();
    142 		}
    143 		ip->t.c_cc[cp->sub] = val;
    144 	} else if (arg[0] == '^')
    145 		ip->t.c_cc[cp->sub] = (arg[1] == '?') ? 0177 :
    146 		    (arg[1] == '-') ? _POSIX_VDISABLE : arg[1] & 037;
    147 	else
    148 		ip->t.c_cc[cp->sub] = arg[0];
    149 	ip->set = 1;
    150 	return (1);
    151 }
    152