11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1991, 1993, 1994 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * Redistribution and use in source and binary forms, with or without 61556Srgrimes * modification, are permitted provided that the following conditions 71556Srgrimes * are met: 81556Srgrimes * 1. Redistributions of source code must retain the above copyright 91556Srgrimes * notice, this list of conditions and the following disclaimer. 101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111556Srgrimes * notice, this list of conditions and the following disclaimer in the 121556Srgrimes * documentation and/or other materials provided with the distribution. 131556Srgrimes * 4. Neither the name of the University nor the names of its contributors 141556Srgrimes * may be used to endorse or promote products derived from this software 151556Srgrimes * without specific prior written permission. 161556Srgrimes * 171556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271556Srgrimes * SUCH DAMAGE. 281556Srgrimes */ 291556Srgrimes 301556Srgrimes#ifndef lint 3136152Scharnier#if 0 3236152Scharnierstatic char sccsid[] = "@(#)cchar.c 8.5 (Berkeley) 4/2/94"; 3336152Scharnier#endif 341556Srgrimes#endif /* not lint */ 3599110Sobrien#include <sys/cdefs.h> 3699110Sobrien__FBSDID("$FreeBSD$"); 371556Srgrimes 381556Srgrimes#include <sys/types.h> 391556Srgrimes 401556Srgrimes#include <err.h> 411556Srgrimes#include <limits.h> 421556Srgrimes#include <stddef.h> 431556Srgrimes#include <stdlib.h> 441556Srgrimes#include <string.h> 451556Srgrimes 461556Srgrimes#include "stty.h" 471556Srgrimes#include "extern.h" 481556Srgrimes 4990111Simpstatic int c_cchar(const void *, const void *); 5076810Skris 511556Srgrimes/* 521556Srgrimes * Special control characters. 531556Srgrimes * 541556Srgrimes * Cchars1 are the standard names, cchars2 are the old aliases. 551556Srgrimes * The first are displayed, but both are recognized on the 561556Srgrimes * command line. 571556Srgrimes */ 581556Srgrimesstruct cchar cchars1[] = { 591556Srgrimes { "discard", VDISCARD, CDISCARD }, 601556Srgrimes { "dsusp", VDSUSP, CDSUSP }, 611556Srgrimes { "eof", VEOF, CEOF }, 621556Srgrimes { "eol", VEOL, CEOL }, 631556Srgrimes { "eol2", VEOL2, CEOL }, 641556Srgrimes { "erase", VERASE, CERASE }, 6569321Sjkh { "erase2", VERASE2, CERASE2 }, 661556Srgrimes { "intr", VINTR, CINTR }, 671556Srgrimes { "kill", VKILL, CKILL }, 681556Srgrimes { "lnext", VLNEXT, CLNEXT }, 691556Srgrimes { "min", VMIN, CMIN }, 701556Srgrimes { "quit", VQUIT, CQUIT }, 711556Srgrimes { "reprint", VREPRINT, CREPRINT }, 721556Srgrimes { "start", VSTART, CSTART }, 731556Srgrimes { "status", VSTATUS, CSTATUS }, 741556Srgrimes { "stop", VSTOP, CSTOP }, 751556Srgrimes { "susp", VSUSP, CSUSP }, 761556Srgrimes { "time", VTIME, CTIME }, 771556Srgrimes { "werase", VWERASE, CWERASE }, 7876810Skris { NULL, 0, 0}, 791556Srgrimes}; 801556Srgrimes 811556Srgrimesstruct cchar cchars2[] = { 821556Srgrimes { "brk", VEOL, CEOL }, 831556Srgrimes { "flush", VDISCARD, CDISCARD }, 841556Srgrimes { "rprnt", VREPRINT, CREPRINT }, 8576810Skris { NULL, 0, 0 }, 861556Srgrimes}; 871556Srgrimes 881556Srgrimesstatic int 8990111Simpc_cchar(const void *a, const void *b) 901556Srgrimes{ 911556Srgrimes 9276810Skris return (strcmp(((const struct cchar *)a)->name, ((const struct cchar *)b)->name)); 931556Srgrimes} 941556Srgrimes 951556Srgrimesint 9690111Simpcsearch(char ***argvp, struct info *ip) 971556Srgrimes{ 981556Srgrimes struct cchar *cp, tmp; 991556Srgrimes long val; 1001556Srgrimes char *arg, *ep, *name; 1018855Srgrimes 1021556Srgrimes name = **argvp; 1031556Srgrimes 1041556Srgrimes tmp.name = name; 1051556Srgrimes if (!(cp = (struct cchar *)bsearch(&tmp, cchars1, 1061556Srgrimes sizeof(cchars1)/sizeof(struct cchar) - 1, sizeof(struct cchar), 1078148Sache c_cchar)) && !(cp = (struct cchar *)bsearch(&tmp, cchars2, 1088148Sache sizeof(cchars2)/sizeof(struct cchar) - 1, sizeof(struct cchar), 1091556Srgrimes c_cchar))) 1101556Srgrimes return (0); 1111556Srgrimes 1121556Srgrimes arg = *++*argvp; 1131556Srgrimes if (!arg) { 1141556Srgrimes warnx("option requires an argument -- %s", name); 1151556Srgrimes usage(); 1161556Srgrimes } 1171556Srgrimes 1181556Srgrimes#define CHK(s) (*arg == s[0] && !strcmp(arg, s)) 1191556Srgrimes if (CHK("undef") || CHK("<undef>")) 1201556Srgrimes ip->t.c_cc[cp->sub] = _POSIX_VDISABLE; 1211556Srgrimes else if (cp->sub == VMIN || cp->sub == VTIME) { 1221556Srgrimes val = strtol(arg, &ep, 10); 1231556Srgrimes if (val > UCHAR_MAX) { 1241556Srgrimes warnx("maximum option value is %d -- %s", 1251556Srgrimes UCHAR_MAX, name); 1261556Srgrimes usage(); 1271556Srgrimes } 1281556Srgrimes if (*ep != '\0') { 1291556Srgrimes warnx("option requires a numeric argument -- %s", name); 1301556Srgrimes usage(); 1311556Srgrimes } 1321556Srgrimes ip->t.c_cc[cp->sub] = val; 1331556Srgrimes } else if (arg[0] == '^') 1341556Srgrimes ip->t.c_cc[cp->sub] = (arg[1] == '?') ? 0177 : 1351556Srgrimes (arg[1] == '-') ? _POSIX_VDISABLE : arg[1] & 037; 1361556Srgrimes else 1371556Srgrimes ip->t.c_cc[cp->sub] = arg[0]; 1381556Srgrimes ip->set = 1; 1391556Srgrimes return (1); 1401556Srgrimes} 141