11.7Schristos/*	$NetBSD: key.c,v 1.7 2005/11/28 03:26:06 christos Exp $	*/
21.1Shpeyerl
31.1Shpeyerl/*-
41.1Shpeyerl * Copyright (c) 1991, 1993, 1994
51.1Shpeyerl *	The Regents of the University of California.  All rights reserved.
61.1Shpeyerl *
71.1Shpeyerl * Redistribution and use in source and binary forms, with or without
81.1Shpeyerl * modification, are permitted provided that the following conditions
91.1Shpeyerl * are met:
101.1Shpeyerl * 1. Redistributions of source code must retain the above copyright
111.1Shpeyerl *    notice, this list of conditions and the following disclaimer.
121.1Shpeyerl * 2. Redistributions in binary form must reproduce the above copyright
131.1Shpeyerl *    notice, this list of conditions and the following disclaimer in the
141.1Shpeyerl *    documentation and/or other materials provided with the distribution.
151.6Sagc * 3. Neither the name of the University nor the names of its contributors
161.1Shpeyerl *    may be used to endorse or promote products derived from this software
171.1Shpeyerl *    without specific prior written permission.
181.1Shpeyerl *
191.1Shpeyerl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
201.1Shpeyerl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
211.1Shpeyerl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221.1Shpeyerl * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
231.1Shpeyerl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
241.1Shpeyerl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
251.1Shpeyerl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
261.1Shpeyerl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271.1Shpeyerl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
281.1Shpeyerl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
291.1Shpeyerl * SUCH DAMAGE.
301.1Shpeyerl */
311.1Shpeyerl
321.2Smikel#include <sys/cdefs.h>
331.1Shpeyerl#ifndef lint
341.1Shpeyerl#if 0
351.1Shpeyerlstatic char sccsid[] = "@(#)key.c	8.3 (Berkeley) 4/2/94";
361.1Shpeyerl#else
371.7Schristos__RCSID("$NetBSD: key.c,v 1.7 2005/11/28 03:26:06 christos Exp $");
381.1Shpeyerl#endif
391.1Shpeyerl#endif /* not lint */
401.1Shpeyerl
411.1Shpeyerl#include <sys/param.h>
421.1Shpeyerl#include <sys/types.h>
431.1Shpeyerl
441.1Shpeyerl#include <errno.h>
451.1Shpeyerl#include <stdlib.h>
461.1Shpeyerl#include <stdio.h>
471.1Shpeyerl#include <string.h>
481.1Shpeyerl#include <syslog.h>
491.1Shpeyerl#include <dirent.h>
501.1Shpeyerl#include <termios.h>
511.1Shpeyerl
521.1Shpeyerl#include "lp.h"
531.1Shpeyerl#include "extern.h"
541.1Shpeyerl
551.1Shpeyerl__BEGIN_DECLS
561.2Smikelstatic int
571.5Swiz	c_key(const void *, const void *);
581.5Swizvoid	f_cbreak(struct info *);
591.5Swizvoid	f_columns(struct info *);
601.5Swizvoid	f_dec(struct info *);
611.5Swizvoid	f_extproc(struct info *);
621.5Swizvoid	f_ispeed(struct info *);
631.5Swizvoid	f_nl(struct info *);
641.5Swizvoid	f_ospeed(struct info *);
651.5Swizvoid	f_raw(struct info *);
661.5Swizvoid	f_rows(struct info *);
671.5Swizvoid	f_sane(struct info *);
681.5Swizvoid	f_tty(struct info *);
691.1Shpeyerl__END_DECLS
701.1Shpeyerl
711.1Shpeyerlstatic struct key {
721.7Schristos	const char *name;			/* name */
731.5Swiz	void (*f)(struct info *);		/* function */
741.1Shpeyerl#define	F_NEEDARG	0x01			/* needs an argument */
751.1Shpeyerl#define	F_OFFOK		0x02			/* can turn off */
761.1Shpeyerl	int flags;
771.4Smjl} const keys[] = {
781.1Shpeyerl	{ "cbreak",	f_cbreak,	F_OFFOK },
791.1Shpeyerl	{ "cols",	f_columns,	F_NEEDARG },
801.1Shpeyerl	{ "columns",	f_columns,	F_NEEDARG },
811.1Shpeyerl	{ "cooked", 	f_sane,		0 },
821.1Shpeyerl	{ "dec",	f_dec,		0 },
831.1Shpeyerl	{ "extproc",	f_extproc,	F_OFFOK },
841.1Shpeyerl	{ "ispeed",	f_ispeed,	F_NEEDARG },
851.1Shpeyerl	{ "new",	f_tty,		0 },
861.1Shpeyerl	{ "nl",		f_nl,		F_OFFOK },
871.1Shpeyerl	{ "old",	f_tty,		0 },
881.1Shpeyerl	{ "ospeed",	f_ospeed,	F_NEEDARG },
891.1Shpeyerl	{ "raw",	f_raw,		F_OFFOK },
901.1Shpeyerl	{ "rows",	f_rows,		F_NEEDARG },
911.1Shpeyerl	{ "sane",	f_sane,		0 },
921.1Shpeyerl	{ "tty",	f_tty,		0 },
931.1Shpeyerl};
941.1Shpeyerl
951.1Shpeyerlstatic int
961.4Smjlc_key(const void *a, const void *b)
971.1Shpeyerl{
981.1Shpeyerl
991.7Schristos        return (strcmp(((const struct key *)a)->name,
1001.7Schristos	    ((const struct key *)b)->name));
1011.1Shpeyerl}
1021.1Shpeyerl
1031.1Shpeyerlint
1041.4Smjlksearch(char ***argvp, struct info *ip)
1051.1Shpeyerl{
1061.1Shpeyerl	char *name;
1071.1Shpeyerl	struct key *kp, tmp;
1081.1Shpeyerl
1091.1Shpeyerl	name = **argvp;
1101.1Shpeyerl	if (*name == '-') {
1111.1Shpeyerl		ip->off = 1;
1121.1Shpeyerl		++name;
1131.1Shpeyerl	} else
1141.1Shpeyerl		ip->off = 0;
1151.1Shpeyerl
1161.1Shpeyerl	tmp.name = name;
1171.1Shpeyerl	if (!(kp = (struct key *)bsearch(&tmp, keys,
1181.1Shpeyerl	    sizeof(keys)/sizeof(struct key), sizeof(struct key), c_key)))
1191.1Shpeyerl		return (0);
1201.1Shpeyerl	if (!(kp->flags & F_OFFOK) && ip->off) {
1211.1Shpeyerl		syslog(LOG_INFO, "%s: illegal option: %s", printer, name);
1221.1Shpeyerl		return (1);
1231.1Shpeyerl	}
1241.1Shpeyerl	if (kp->flags & F_NEEDARG && !(ip->arg = *++*argvp)) {
1251.1Shpeyerl		syslog(LOG_INFO, "%s: option requires an argument: %s",
1261.1Shpeyerl		       printer, name);
1271.1Shpeyerl		return (1);
1281.1Shpeyerl	}
1291.1Shpeyerl	kp->f(ip);
1301.1Shpeyerl	return (1);
1311.1Shpeyerl}
1321.1Shpeyerl
1331.1Shpeyerlvoid
1341.4Smjlf_cbreak(struct info *ip)
1351.1Shpeyerl{
1361.1Shpeyerl
1371.1Shpeyerl	if (ip->off)
1381.1Shpeyerl		f_sane(ip);
1391.1Shpeyerl	else {
1401.1Shpeyerl		ip->t.c_iflag |= BRKINT|IXON|IMAXBEL;
1411.1Shpeyerl		ip->t.c_oflag |= OPOST;
1421.1Shpeyerl		ip->t.c_lflag |= ISIG|IEXTEN;
1431.1Shpeyerl		ip->t.c_lflag &= ~ICANON;
1441.1Shpeyerl		ip->set = 1;
1451.1Shpeyerl	}
1461.1Shpeyerl}
1471.1Shpeyerl
1481.1Shpeyerlvoid
1491.4Smjlf_columns(struct info *ip)
1501.1Shpeyerl{
1511.1Shpeyerl
1521.1Shpeyerl	ip->win.ws_col = atoi(ip->arg);
1531.1Shpeyerl	ip->wset = 1;
1541.1Shpeyerl}
1551.1Shpeyerl
1561.1Shpeyerlvoid
1571.4Smjlf_dec(struct info *ip)
1581.1Shpeyerl{
1591.1Shpeyerl
1601.1Shpeyerl	ip->t.c_cc[VERASE] = (u_char)0177;
1611.1Shpeyerl	ip->t.c_cc[VKILL] = CTRL('u');
1621.1Shpeyerl	ip->t.c_cc[VINTR] = CTRL('c');
1631.1Shpeyerl	ip->t.c_lflag &= ~ECHOPRT;
1641.1Shpeyerl	ip->t.c_lflag |= ECHOE|ECHOKE|ECHOCTL;
1651.1Shpeyerl	ip->t.c_iflag &= ~IXANY;
1661.1Shpeyerl	ip->set = 1;
1671.1Shpeyerl}
1681.1Shpeyerl
1691.1Shpeyerlvoid
1701.4Smjlf_extproc(struct info *ip)
1711.1Shpeyerl{
1721.1Shpeyerl
1731.1Shpeyerl	if (ip->set) {
1741.1Shpeyerl		int tmp = 1;
1751.1Shpeyerl		(void)ioctl(ip->fd, TIOCEXT, &tmp);
1761.1Shpeyerl	} else {
1771.1Shpeyerl		int tmp = 0;
1781.1Shpeyerl		(void)ioctl(ip->fd, TIOCEXT, &tmp);
1791.1Shpeyerl	}
1801.1Shpeyerl}
1811.1Shpeyerl
1821.1Shpeyerlvoid
1831.4Smjlf_ispeed(struct info *ip)
1841.1Shpeyerl{
1851.1Shpeyerl
1861.1Shpeyerl	cfsetispeed(&ip->t, atoi(ip->arg));
1871.1Shpeyerl	ip->set = 1;
1881.1Shpeyerl}
1891.1Shpeyerl
1901.1Shpeyerlvoid
1911.4Smjlf_nl(struct info *ip)
1921.1Shpeyerl{
1931.1Shpeyerl
1941.1Shpeyerl	if (ip->off) {
1951.1Shpeyerl		ip->t.c_iflag |= ICRNL;
1961.1Shpeyerl		ip->t.c_oflag |= ONLCR;
1971.1Shpeyerl	} else {
1981.1Shpeyerl		ip->t.c_iflag &= ~ICRNL;
1991.1Shpeyerl		ip->t.c_oflag &= ~ONLCR;
2001.1Shpeyerl	}
2011.1Shpeyerl	ip->set = 1;
2021.1Shpeyerl}
2031.1Shpeyerl
2041.1Shpeyerlvoid
2051.4Smjlf_ospeed(struct info *ip)
2061.1Shpeyerl{
2071.1Shpeyerl
2081.1Shpeyerl	cfsetospeed(&ip->t, atoi(ip->arg));
2091.1Shpeyerl	ip->set = 1;
2101.1Shpeyerl}
2111.1Shpeyerl
2121.1Shpeyerlvoid
2131.4Smjlf_raw(struct info *ip)
2141.1Shpeyerl{
2151.1Shpeyerl
2161.1Shpeyerl	if (ip->off)
2171.1Shpeyerl		f_sane(ip);
2181.1Shpeyerl	else {
2191.1Shpeyerl		cfmakeraw(&ip->t);
2201.1Shpeyerl		ip->t.c_cflag &= ~(CSIZE|PARENB);
2211.1Shpeyerl		ip->t.c_cflag |= CS8;
2221.1Shpeyerl		ip->set = 1;
2231.1Shpeyerl	}
2241.1Shpeyerl}
2251.1Shpeyerl
2261.1Shpeyerlvoid
2271.4Smjlf_rows(struct info *ip)
2281.1Shpeyerl{
2291.1Shpeyerl
2301.1Shpeyerl	ip->win.ws_row = atoi(ip->arg);
2311.1Shpeyerl	ip->wset = 1;
2321.1Shpeyerl}
2331.1Shpeyerl
2341.1Shpeyerlvoid
2351.4Smjlf_sane(struct info *ip)
2361.1Shpeyerl{
2371.1Shpeyerl
2381.3Sscottr	ip->t.c_cflag = TTYDEF_CFLAG | (ip->t.c_cflag & (CLOCAL|CRTSCTS|CDTRCTS));
2391.1Shpeyerl	ip->t.c_iflag = TTYDEF_IFLAG;
2401.1Shpeyerl	ip->t.c_iflag |= ICRNL;
2411.1Shpeyerl	/* preserve user-preference flags in lflag */
2421.1Shpeyerl#define	LKEEP	(ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH)
2431.1Shpeyerl	ip->t.c_lflag = TTYDEF_LFLAG | (ip->t.c_lflag & LKEEP);
2441.1Shpeyerl	ip->t.c_oflag = TTYDEF_OFLAG;
2451.1Shpeyerl	ip->set = 1;
2461.1Shpeyerl}
2471.1Shpeyerl
2481.1Shpeyerlvoid
2491.4Smjlf_tty(struct info *ip)
2501.1Shpeyerl{
2511.1Shpeyerl	int tmp;
2521.1Shpeyerl
2531.1Shpeyerl	tmp = TTYDISC;
2541.1Shpeyerl	if (ioctl(0, TIOCSETD, &tmp) < 0)
2551.1Shpeyerl		syslog(LOG_ERR, "%s: ioctl(TIOCSETD): %m", printer);
2561.1Shpeyerl}
257