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