xref: /openbsd/usr.sbin/lpd/lp_stty.c (revision 3b188dab)
1*3b188dabSeric /* $OpenBSD: lp_stty.c,v 1.1.1.1 2018/04/27 16:14:36 eric Exp $ */
2*3b188dabSeric 
3*3b188dabSeric /*
4*3b188dabSeric  * Adapted from the following files in src/usr.sbin/lpr/lpd:
5*3b188dabSeric  *
6*3b188dabSeric  * extern.h,v 1.9 2015/09/29 02:37:29
7*3b188dabSeric  * key.c,v 1.8 2015/01/16 06:40:18
8*3b188dabSeric  * modes.c,v 1.8 2015/01/16 06:40:18
9*3b188dabSeric  * printjob.c,v 1.58 2016/11/22 16:03:57
10*3b188dabSeric  */
11*3b188dabSeric 
12*3b188dabSeric static const char *printer;
13*3b188dabSeric 
14*3b188dabSeric /*-
15*3b188dabSeric  *
16*3b188dabSeric  * Copyright (c) 1989, 1993
17*3b188dabSeric  *	The Regents of the University of California.  All rights reserved.
18*3b188dabSeric  * Copyright (c) 1991, 1993, 1994
19*3b188dabSeric  *	The Regents of the University of California.  All rights reserved.
20*3b188dabSeric  *
21*3b188dabSeric  * Redistribution and use in source and binary forms, with or without
22*3b188dabSeric  * modification, are permitted provided that the following conditions
23*3b188dabSeric  * are met:
24*3b188dabSeric  * 1. Redistributions of source code must retain the above copyright
25*3b188dabSeric  *    notice, this list of conditions and the following disclaimer.
26*3b188dabSeric  * 2. Redistributions in binary form must reproduce the above copyright
27*3b188dabSeric  *    notice, this list of conditions and the following disclaimer in the
28*3b188dabSeric  *    documentation and/or other materials provided with the distribution.
29*3b188dabSeric  * 3. Neither the name of the University nor the names of its contributors
30*3b188dabSeric  *    may be used to endorse or promote products derived from this software
31*3b188dabSeric  *    without specific prior written permission.
32*3b188dabSeric  *
33*3b188dabSeric  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34*3b188dabSeric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35*3b188dabSeric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36*3b188dabSeric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37*3b188dabSeric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38*3b188dabSeric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39*3b188dabSeric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40*3b188dabSeric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41*3b188dabSeric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42*3b188dabSeric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43*3b188dabSeric  * SUCH DAMAGE.
44*3b188dabSeric  */
45*3b188dabSeric 
46*3b188dabSeric #include <sys/types.h>
47*3b188dabSeric #include <sys/ioctl.h>
48*3b188dabSeric 
49*3b188dabSeric #include <errno.h>
50*3b188dabSeric #include <signal.h>
51*3b188dabSeric #include <stdlib.h>
52*3b188dabSeric #include <stdio.h>
53*3b188dabSeric #include <string.h>
54*3b188dabSeric #include <dirent.h>
55*3b188dabSeric #include <limits.h>
56*3b188dabSeric #include <termios.h>
57*3b188dabSeric 
58*3b188dabSeric #include "lp.h"
59*3b188dabSeric #include "log.h"
60*3b188dabSeric 
61*3b188dabSeric /*
62*3b188dabSeric  * from extern.h
63*3b188dabSeric  */
64*3b188dabSeric 
65*3b188dabSeric struct info {
66*3b188dabSeric 	int fd;					/* file descriptor */
67*3b188dabSeric 	int ldisc;				/* line discipline */
68*3b188dabSeric 	int off;				/* turn off */
69*3b188dabSeric 	int set;				/* need set */
70*3b188dabSeric 	int wset;				/* need window set */
71*3b188dabSeric 	char *arg;				/* argument */
72*3b188dabSeric 	struct termios t;			/* terminal info */
73*3b188dabSeric 	struct winsize win;			/* window info */
74*3b188dabSeric };
75*3b188dabSeric 
76*3b188dabSeric 
77*3b188dabSeric /*
78*3b188dabSeric  * from key.c
79*3b188dabSeric  */
80*3b188dabSeric 
81*3b188dabSeric static int  c_key(const void *, const void *);
82*3b188dabSeric static void f_cbreak(struct info *);
83*3b188dabSeric static void f_columns(struct info *);
84*3b188dabSeric static void f_dec(struct info *);
85*3b188dabSeric static void f_extproc(struct info *);
86*3b188dabSeric static void f_ispeed(struct info *);
87*3b188dabSeric static void f_nl(struct info *);
88*3b188dabSeric static void f_ospeed(struct info *);
89*3b188dabSeric static void f_raw(struct info *);
90*3b188dabSeric static void f_rows(struct info *);
91*3b188dabSeric static void f_sane(struct info *);
92*3b188dabSeric static void f_tty(struct info *);
93*3b188dabSeric 
94*3b188dabSeric static struct key {
95*3b188dabSeric 	char *name;				/* name */
96*3b188dabSeric 	void (*f)(struct info *);		/* function */
97*3b188dabSeric #define	F_NEEDARG	0x01			/* needs an argument */
98*3b188dabSeric #define	F_OFFOK		0x02			/* can turn off */
99*3b188dabSeric 	int flags;
100*3b188dabSeric } const keys[] = {
101*3b188dabSeric 	{ "cbreak",	f_cbreak,	F_OFFOK },
102*3b188dabSeric 	{ "cols",	f_columns,	F_NEEDARG },
103*3b188dabSeric 	{ "columns",	f_columns,	F_NEEDARG },
104*3b188dabSeric 	{ "cooked", 	f_sane,		0 },
105*3b188dabSeric 	{ "dec",	f_dec,		0 },
106*3b188dabSeric 	{ "extproc",	f_extproc,	F_OFFOK },
107*3b188dabSeric 	{ "ispeed",	f_ispeed,	F_NEEDARG },
108*3b188dabSeric 	{ "new",	f_tty,		0 },
109*3b188dabSeric 	{ "nl",		f_nl,		F_OFFOK },
110*3b188dabSeric 	{ "old",	f_tty,		0 },
111*3b188dabSeric 	{ "ospeed",	f_ospeed,	F_NEEDARG },
112*3b188dabSeric 	{ "raw",	f_raw,		F_OFFOK },
113*3b188dabSeric 	{ "rows",	f_rows,		F_NEEDARG },
114*3b188dabSeric 	{ "sane",	f_sane,		0 },
115*3b188dabSeric 	{ "tty",	f_tty,		0 },
116*3b188dabSeric };
117*3b188dabSeric 
118*3b188dabSeric static int
c_key(const void * a,const void * b)119*3b188dabSeric c_key(const void *a, const void *b)
120*3b188dabSeric {
121*3b188dabSeric 
122*3b188dabSeric         return (strcmp(((const struct key *)a)->name,
123*3b188dabSeric 	    ((const struct key *)b)->name));
124*3b188dabSeric }
125*3b188dabSeric 
126*3b188dabSeric static int
ksearch(char *** argvp,struct info * ip)127*3b188dabSeric ksearch(char ***argvp, struct info *ip)
128*3b188dabSeric {
129*3b188dabSeric 	char *name;
130*3b188dabSeric 	struct key *kp, tmp;
131*3b188dabSeric 
132*3b188dabSeric 	name = **argvp;
133*3b188dabSeric 	if (*name == '-') {
134*3b188dabSeric 		ip->off = 1;
135*3b188dabSeric 		++name;
136*3b188dabSeric 	} else
137*3b188dabSeric 		ip->off = 0;
138*3b188dabSeric 
139*3b188dabSeric 	tmp.name = name;
140*3b188dabSeric 	if (!(kp = (struct key *)bsearch(&tmp, keys,
141*3b188dabSeric 	    sizeof(keys)/sizeof(struct key), sizeof(struct key), c_key)))
142*3b188dabSeric 		return (0);
143*3b188dabSeric 	if (!(kp->flags & F_OFFOK) && ip->off) {
144*3b188dabSeric 		log_warnx("%s: illegal option: %s", printer, name);
145*3b188dabSeric 		return (1);
146*3b188dabSeric 	}
147*3b188dabSeric 	if (kp->flags & F_NEEDARG && !(ip->arg = *++*argvp)) {
148*3b188dabSeric 		log_warnx("%s: option requires an argument: %s", printer, name);
149*3b188dabSeric 		return (1);
150*3b188dabSeric 	}
151*3b188dabSeric 	kp->f(ip);
152*3b188dabSeric 	return (1);
153*3b188dabSeric }
154*3b188dabSeric 
155*3b188dabSeric static void
f_cbreak(struct info * ip)156*3b188dabSeric f_cbreak(struct info *ip)
157*3b188dabSeric {
158*3b188dabSeric 
159*3b188dabSeric 	if (ip->off)
160*3b188dabSeric 		f_sane(ip);
161*3b188dabSeric 	else {
162*3b188dabSeric 		ip->t.c_iflag |= BRKINT|IXON|IMAXBEL;
163*3b188dabSeric 		ip->t.c_oflag |= OPOST;
164*3b188dabSeric 		ip->t.c_lflag |= ISIG|IEXTEN;
165*3b188dabSeric 		ip->t.c_lflag &= ~ICANON;
166*3b188dabSeric 		ip->set = 1;
167*3b188dabSeric 	}
168*3b188dabSeric }
169*3b188dabSeric 
170*3b188dabSeric static void
f_columns(struct info * ip)171*3b188dabSeric f_columns(struct info *ip)
172*3b188dabSeric {
173*3b188dabSeric 
174*3b188dabSeric 	ip->win.ws_col = atoi(ip->arg);
175*3b188dabSeric 	ip->wset = 1;
176*3b188dabSeric }
177*3b188dabSeric 
178*3b188dabSeric static void
f_dec(struct info * ip)179*3b188dabSeric f_dec(struct info *ip)
180*3b188dabSeric {
181*3b188dabSeric 
182*3b188dabSeric 	ip->t.c_cc[VERASE] = (u_char)0177;
183*3b188dabSeric 	ip->t.c_cc[VKILL] = CTRL('u');
184*3b188dabSeric 	ip->t.c_cc[VINTR] = CTRL('c');
185*3b188dabSeric 	ip->t.c_lflag &= ~ECHOPRT;
186*3b188dabSeric 	ip->t.c_lflag |= ECHOE|ECHOKE|ECHOCTL;
187*3b188dabSeric 	ip->t.c_iflag &= ~IXANY;
188*3b188dabSeric 	ip->set = 1;
189*3b188dabSeric }
190*3b188dabSeric 
191*3b188dabSeric static void
f_extproc(struct info * ip)192*3b188dabSeric f_extproc(struct info *ip)
193*3b188dabSeric {
194*3b188dabSeric 
195*3b188dabSeric 	if (ip->set) {
196*3b188dabSeric 		int tmp = 1;
197*3b188dabSeric 		(void)ioctl(ip->fd, TIOCEXT, &tmp);
198*3b188dabSeric 	} else {
199*3b188dabSeric 		int tmp = 0;
200*3b188dabSeric 		(void)ioctl(ip->fd, TIOCEXT, &tmp);
201*3b188dabSeric 	}
202*3b188dabSeric }
203*3b188dabSeric 
204*3b188dabSeric static void
f_ispeed(struct info * ip)205*3b188dabSeric f_ispeed(struct info *ip)
206*3b188dabSeric {
207*3b188dabSeric 
208*3b188dabSeric 	cfsetispeed(&ip->t, atoi(ip->arg));
209*3b188dabSeric 	ip->set = 1;
210*3b188dabSeric }
211*3b188dabSeric 
212*3b188dabSeric static void
f_nl(struct info * ip)213*3b188dabSeric f_nl(struct info *ip)
214*3b188dabSeric {
215*3b188dabSeric 
216*3b188dabSeric 	if (ip->off) {
217*3b188dabSeric 		ip->t.c_iflag |= ICRNL;
218*3b188dabSeric 		ip->t.c_oflag |= ONLCR;
219*3b188dabSeric 	} else {
220*3b188dabSeric 		ip->t.c_iflag &= ~ICRNL;
221*3b188dabSeric 		ip->t.c_oflag &= ~ONLCR;
222*3b188dabSeric 	}
223*3b188dabSeric 	ip->set = 1;
224*3b188dabSeric }
225*3b188dabSeric 
226*3b188dabSeric static void
f_ospeed(struct info * ip)227*3b188dabSeric f_ospeed(struct info *ip)
228*3b188dabSeric {
229*3b188dabSeric 
230*3b188dabSeric 	cfsetospeed(&ip->t, atoi(ip->arg));
231*3b188dabSeric 	ip->set = 1;
232*3b188dabSeric }
233*3b188dabSeric 
234*3b188dabSeric static void
f_raw(struct info * ip)235*3b188dabSeric f_raw(struct info *ip)
236*3b188dabSeric {
237*3b188dabSeric 
238*3b188dabSeric 	if (ip->off)
239*3b188dabSeric 		f_sane(ip);
240*3b188dabSeric 	else {
241*3b188dabSeric 		cfmakeraw(&ip->t);
242*3b188dabSeric 		ip->t.c_cflag &= ~(CSIZE|PARENB);
243*3b188dabSeric 		ip->t.c_cflag |= CS8;
244*3b188dabSeric 		ip->set = 1;
245*3b188dabSeric 	}
246*3b188dabSeric }
247*3b188dabSeric 
248*3b188dabSeric static void
f_rows(struct info * ip)249*3b188dabSeric f_rows(struct info *ip)
250*3b188dabSeric {
251*3b188dabSeric 
252*3b188dabSeric 	ip->win.ws_row = atoi(ip->arg);
253*3b188dabSeric 	ip->wset = 1;
254*3b188dabSeric }
255*3b188dabSeric 
256*3b188dabSeric static void
f_sane(struct info * ip)257*3b188dabSeric f_sane(struct info *ip)
258*3b188dabSeric {
259*3b188dabSeric 
260*3b188dabSeric 	ip->t.c_cflag = TTYDEF_CFLAG | (ip->t.c_cflag & (CLOCAL|CRTSCTS));
261*3b188dabSeric 	ip->t.c_iflag = TTYDEF_IFLAG;
262*3b188dabSeric 	ip->t.c_iflag |= ICRNL;
263*3b188dabSeric 	/* preserve user-preference flags in lflag */
264*3b188dabSeric #define	LKEEP	(ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH)
265*3b188dabSeric 	ip->t.c_lflag = TTYDEF_LFLAG | (ip->t.c_lflag & LKEEP);
266*3b188dabSeric 	ip->t.c_oflag = TTYDEF_OFLAG;
267*3b188dabSeric 	ip->set = 1;
268*3b188dabSeric }
269*3b188dabSeric 
270*3b188dabSeric static void
f_tty(struct info * ip)271*3b188dabSeric f_tty(struct info *ip)
272*3b188dabSeric {
273*3b188dabSeric 	int tmp;
274*3b188dabSeric 
275*3b188dabSeric 	tmp = TTYDISC;
276*3b188dabSeric 	if (ioctl(0, TIOCSETD, &tmp) == -1)
277*3b188dabSeric 		log_warn("%s: ioctl(TIOCSETD)", printer);
278*3b188dabSeric }
279*3b188dabSeric 
280*3b188dabSeric /*
281*3b188dabSeric  * from key.c
282*3b188dabSeric  */
283*3b188dabSeric 
284*3b188dabSeric struct modes {
285*3b188dabSeric 	char *name;
286*3b188dabSeric 	long set;
287*3b188dabSeric 	long unset;
288*3b188dabSeric };
289*3b188dabSeric 
290*3b188dabSeric /*
291*3b188dabSeric  * The code in optlist() depends on minus options following regular
292*3b188dabSeric  * options, i.e. "foo" must immediately precede "-foo".
293*3b188dabSeric  */
294*3b188dabSeric const struct modes cmodes[] = {
295*3b188dabSeric 	{ "cs5",	CS5, CSIZE },
296*3b188dabSeric 	{ "cs6",	CS6, CSIZE },
297*3b188dabSeric 	{ "cs7",	CS7, CSIZE },
298*3b188dabSeric 	{ "cs8",	CS8, CSIZE },
299*3b188dabSeric 	{ "cstopb",	CSTOPB, 0 },
300*3b188dabSeric 	{ "-cstopb",	0, CSTOPB },
301*3b188dabSeric 	{ "cread",	CREAD, 0 },
302*3b188dabSeric 	{ "-cread",	0, CREAD },
303*3b188dabSeric 	{ "parenb",	PARENB, 0 },
304*3b188dabSeric 	{ "-parenb",	0, PARENB },
305*3b188dabSeric 	{ "parodd",	PARODD, 0 },
306*3b188dabSeric 	{ "-parodd",	0, PARODD },
307*3b188dabSeric 	{ "parity",	PARENB | CS7, PARODD | CSIZE },
308*3b188dabSeric 	{ "-parity",	CS8, PARODD | PARENB | CSIZE },
309*3b188dabSeric 	{ "evenp",	PARENB | CS7, PARODD | CSIZE },
310*3b188dabSeric 	{ "-evenp",	CS8, PARODD | PARENB | CSIZE },
311*3b188dabSeric 	{ "oddp",	PARENB | CS7 | PARODD, CSIZE },
312*3b188dabSeric 	{ "-oddp",	CS8, PARODD | PARENB | CSIZE },
313*3b188dabSeric 	{ "pass8",	CS8, PARODD | PARENB | CSIZE },
314*3b188dabSeric 	{ "-pass8",	PARENB | CS7, PARODD | CSIZE },
315*3b188dabSeric 	{ "hupcl",	HUPCL, 0 },
316*3b188dabSeric 	{ "-hupcl",	0, HUPCL },
317*3b188dabSeric 	{ "hup",	HUPCL, 0 },
318*3b188dabSeric 	{ "-hup",	0, HUPCL },
319*3b188dabSeric 	{ "clocal",	CLOCAL, 0 },
320*3b188dabSeric 	{ "-clocal",	0, CLOCAL },
321*3b188dabSeric 	{ "crtscts",	CRTSCTS, 0 },
322*3b188dabSeric 	{ "-crtscts",	0, CRTSCTS },
323*3b188dabSeric 	{ "mdmbuf",	MDMBUF, 0 },
324*3b188dabSeric 	{ "-mdmbuf",	0, MDMBUF },
325*3b188dabSeric 	{ NULL },
326*3b188dabSeric };
327*3b188dabSeric 
328*3b188dabSeric const struct modes imodes[] = {
329*3b188dabSeric 	{ "ignbrk",	IGNBRK, 0 },
330*3b188dabSeric 	{ "-ignbrk",	0, IGNBRK },
331*3b188dabSeric 	{ "brkint",	BRKINT, 0 },
332*3b188dabSeric 	{ "-brkint",	0, BRKINT },
333*3b188dabSeric 	{ "ignpar",	IGNPAR, 0 },
334*3b188dabSeric 	{ "-ignpar",	0, IGNPAR },
335*3b188dabSeric 	{ "parmrk",	PARMRK, 0 },
336*3b188dabSeric 	{ "-parmrk",	0, PARMRK },
337*3b188dabSeric 	{ "inpck",	INPCK, 0 },
338*3b188dabSeric 	{ "-inpck",	0, INPCK },
339*3b188dabSeric 	{ "istrip",	ISTRIP, 0 },
340*3b188dabSeric 	{ "-istrip",	0, ISTRIP },
341*3b188dabSeric 	{ "inlcr",	INLCR, 0 },
342*3b188dabSeric 	{ "-inlcr",	0, INLCR },
343*3b188dabSeric 	{ "igncr",	IGNCR, 0 },
344*3b188dabSeric 	{ "-igncr",	0, IGNCR },
345*3b188dabSeric 	{ "icrnl",	ICRNL, 0 },
346*3b188dabSeric 	{ "-icrnl",	0, ICRNL },
347*3b188dabSeric 	{ "iuclc",	IUCLC, 0 },
348*3b188dabSeric 	{ "-iuclc",	0, IUCLC },
349*3b188dabSeric 	{ "ixon",	IXON, 0 },
350*3b188dabSeric 	{ "-ixon",	0, IXON },
351*3b188dabSeric 	{ "flow",	IXON, 0 },
352*3b188dabSeric 	{ "-flow",	0, IXON },
353*3b188dabSeric 	{ "ixoff",	IXOFF, 0 },
354*3b188dabSeric 	{ "-ixoff",	0, IXOFF },
355*3b188dabSeric 	{ "tandem",	IXOFF, 0 },
356*3b188dabSeric 	{ "-tandem",	0, IXOFF },
357*3b188dabSeric 	{ "ixany",	IXANY, 0 },
358*3b188dabSeric 	{ "-ixany",	0, IXANY },
359*3b188dabSeric 	{ "decctlq",	0, IXANY },
360*3b188dabSeric 	{ "-decctlq",	IXANY, 0 },
361*3b188dabSeric 	{ "imaxbel",	IMAXBEL, 0 },
362*3b188dabSeric 	{ "-imaxbel",	0, IMAXBEL },
363*3b188dabSeric 	{ NULL },
364*3b188dabSeric };
365*3b188dabSeric 
366*3b188dabSeric const struct modes lmodes[] = {
367*3b188dabSeric 	{ "echo",	ECHO, 0 },
368*3b188dabSeric 	{ "-echo",	0, ECHO },
369*3b188dabSeric 	{ "echoe",	ECHOE, 0 },
370*3b188dabSeric 	{ "-echoe",	0, ECHOE },
371*3b188dabSeric 	{ "crterase",	ECHOE, 0 },
372*3b188dabSeric 	{ "-crterase",	0, ECHOE },
373*3b188dabSeric 	{ "crtbs",	ECHOE, 0 },	/* crtbs not supported, close enough */
374*3b188dabSeric 	{ "-crtbs",	0, ECHOE },
375*3b188dabSeric 	{ "echok",	ECHOK, 0 },
376*3b188dabSeric 	{ "-echok",	0, ECHOK },
377*3b188dabSeric 	{ "echoke",	ECHOKE, 0 },
378*3b188dabSeric 	{ "-echoke",	0, ECHOKE },
379*3b188dabSeric 	{ "crtkill",	ECHOKE, 0 },
380*3b188dabSeric 	{ "-crtkill",	0, ECHOKE },
381*3b188dabSeric 	{ "altwerase",	ALTWERASE, 0 },
382*3b188dabSeric 	{ "-altwerase",	0, ALTWERASE },
383*3b188dabSeric 	{ "iexten",	IEXTEN, 0 },
384*3b188dabSeric 	{ "-iexten",	0, IEXTEN },
385*3b188dabSeric 	{ "echonl",	ECHONL, 0 },
386*3b188dabSeric 	{ "-echonl",	0, ECHONL },
387*3b188dabSeric 	{ "echoctl",	ECHOCTL, 0 },
388*3b188dabSeric 	{ "-echoctl",	0, ECHOCTL },
389*3b188dabSeric 	{ "ctlecho",	ECHOCTL, 0 },
390*3b188dabSeric 	{ "-ctlecho",	0, ECHOCTL },
391*3b188dabSeric 	{ "echoprt",	ECHOPRT, 0 },
392*3b188dabSeric 	{ "-echoprt",	0, ECHOPRT },
393*3b188dabSeric 	{ "prterase",	ECHOPRT, 0 },
394*3b188dabSeric 	{ "-prterase",	0, ECHOPRT },
395*3b188dabSeric 	{ "isig",	ISIG, 0 },
396*3b188dabSeric 	{ "-isig",	0, ISIG },
397*3b188dabSeric 	{ "icanon",	ICANON, 0 },
398*3b188dabSeric 	{ "-icanon",	0, ICANON },
399*3b188dabSeric 	{ "noflsh",	NOFLSH, 0 },
400*3b188dabSeric 	{ "-noflsh",	0, NOFLSH },
401*3b188dabSeric 	{ "tostop",	TOSTOP, 0 },
402*3b188dabSeric 	{ "-tostop",	0, TOSTOP },
403*3b188dabSeric 	{ "flusho",	FLUSHO, 0 },
404*3b188dabSeric 	{ "-flusho",	0, FLUSHO },
405*3b188dabSeric 	{ "pendin",	PENDIN, 0 },
406*3b188dabSeric 	{ "-pendin",	0, PENDIN },
407*3b188dabSeric 	{ "crt",	ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT },
408*3b188dabSeric 	{ "-crt",	ECHOK, ECHOE|ECHOKE|ECHOCTL },
409*3b188dabSeric 	{ "newcrt",	ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT },
410*3b188dabSeric 	{ "-newcrt",	ECHOK, ECHOE|ECHOKE|ECHOCTL },
411*3b188dabSeric 	{ "nokerninfo",	NOKERNINFO, 0 },
412*3b188dabSeric 	{ "-nokerninfo",0, NOKERNINFO },
413*3b188dabSeric 	{ "kerninfo",	0, NOKERNINFO },
414*3b188dabSeric 	{ "-kerninfo",	NOKERNINFO, 0 },
415*3b188dabSeric 	{ "xcase",	XCASE, 0 },
416*3b188dabSeric 	{ "-xcase",	0, XCASE },
417*3b188dabSeric 	{ NULL },
418*3b188dabSeric };
419*3b188dabSeric 
420*3b188dabSeric const struct modes omodes[] = {
421*3b188dabSeric 	{ "opost",	OPOST, 0 },
422*3b188dabSeric 	{ "-opost",	0, OPOST },
423*3b188dabSeric 	{ "litout",	0, OPOST },
424*3b188dabSeric 	{ "-litout",	OPOST, 0 },
425*3b188dabSeric 	{ "ocrnl",	OCRNL, 0 },
426*3b188dabSeric 	{ "-ocrnl",	0, OCRNL },
427*3b188dabSeric 	{ "olcuc",	OLCUC, 0 },
428*3b188dabSeric 	{ "-olcuc",	0, OLCUC },
429*3b188dabSeric 	{ "onlcr",	ONLCR, 0 },
430*3b188dabSeric 	{ "-onlcr",	0, ONLCR },
431*3b188dabSeric 	{ "onlret",	ONLRET, 0 },
432*3b188dabSeric 	{ "-onlret",	0, ONLRET },
433*3b188dabSeric 	{ "onocr",	ONOCR, 0 },
434*3b188dabSeric 	{ "-onocr",	0, ONOCR },
435*3b188dabSeric 	{ "tabs",	0, OXTABS },		/* "preserve" tabs */
436*3b188dabSeric 	{ "-tabs",	OXTABS, 0 },
437*3b188dabSeric 	{ "oxtabs",	OXTABS, 0 },
438*3b188dabSeric 	{ "-oxtabs",	0, OXTABS },
439*3b188dabSeric 	{ NULL },
440*3b188dabSeric };
441*3b188dabSeric 
442*3b188dabSeric #define	CHK(s)	(*name == s[0] && !strcmp(name, s))
443*3b188dabSeric 
444*3b188dabSeric static int
msearch(char *** argvp,struct info * ip)445*3b188dabSeric msearch(char ***argvp, struct info *ip)
446*3b188dabSeric {
447*3b188dabSeric 	const struct modes *mp;
448*3b188dabSeric 	char *name;
449*3b188dabSeric 
450*3b188dabSeric 	name = **argvp;
451*3b188dabSeric 
452*3b188dabSeric 	for (mp = cmodes; mp->name; ++mp)
453*3b188dabSeric 		if (CHK(mp->name)) {
454*3b188dabSeric 			ip->t.c_cflag &= ~mp->unset;
455*3b188dabSeric 			ip->t.c_cflag |= mp->set;
456*3b188dabSeric 			ip->set = 1;
457*3b188dabSeric 			return (1);
458*3b188dabSeric 		}
459*3b188dabSeric 	for (mp = imodes; mp->name; ++mp)
460*3b188dabSeric 		if (CHK(mp->name)) {
461*3b188dabSeric 			ip->t.c_iflag &= ~mp->unset;
462*3b188dabSeric 			ip->t.c_iflag |= mp->set;
463*3b188dabSeric 			ip->set = 1;
464*3b188dabSeric 			return (1);
465*3b188dabSeric 		}
466*3b188dabSeric 	for (mp = lmodes; mp->name; ++mp)
467*3b188dabSeric 		if (CHK(mp->name)) {
468*3b188dabSeric 			ip->t.c_lflag &= ~mp->unset;
469*3b188dabSeric 			ip->t.c_lflag |= mp->set;
470*3b188dabSeric 			ip->set = 1;
471*3b188dabSeric 			return (1);
472*3b188dabSeric 		}
473*3b188dabSeric 	for (mp = omodes; mp->name; ++mp)
474*3b188dabSeric 		if (CHK(mp->name)) {
475*3b188dabSeric 			ip->t.c_oflag &= ~mp->unset;
476*3b188dabSeric 			ip->t.c_oflag |= mp->set;
477*3b188dabSeric 			ip->set = 1;
478*3b188dabSeric 			return (1);
479*3b188dabSeric 		}
480*3b188dabSeric 	return (0);
481*3b188dabSeric }
482*3b188dabSeric 
483*3b188dabSeric /*
484*3b188dabSeric  * from prinjob.c
485*3b188dabSeric  */
486*3b188dabSeric 
487*3b188dabSeric void
lp_stty(struct lp_printer * lp,int fd)488*3b188dabSeric lp_stty(struct lp_printer *lp, int fd)
489*3b188dabSeric {
490*3b188dabSeric 	struct info i;
491*3b188dabSeric 	char **argv, **ap, **ep, *p, *val;
492*3b188dabSeric 
493*3b188dabSeric 	printer = lp->lp_name;
494*3b188dabSeric 
495*3b188dabSeric 	i.fd = fd;
496*3b188dabSeric 	i.set = i.wset = 0;
497*3b188dabSeric 	if (ioctl(i.fd, TIOCEXCL, (char *)0) == -1)
498*3b188dabSeric 		fatal("%s: ioctl(TIOCEXCL)", printer);
499*3b188dabSeric 
500*3b188dabSeric 	if (tcgetattr(i.fd, &i.t) == -1)
501*3b188dabSeric 		fatal("%s: tcgetattr", printer);
502*3b188dabSeric 
503*3b188dabSeric 	if (lp->lp_br > 0) {
504*3b188dabSeric 		cfsetspeed(&i.t, lp->lp_br);
505*3b188dabSeric 		i.set = 1;
506*3b188dabSeric 	}
507*3b188dabSeric 	if (lp->lp_ms) {
508*3b188dabSeric 		if (ioctl(i.fd, TIOCGETD, &i.ldisc) == -1)
509*3b188dabSeric 			fatal("%s: ioctl(TIOCGETD)", printer);
510*3b188dabSeric 
511*3b188dabSeric 		if (ioctl(i.fd, TIOCGWINSZ, &i.win) == -1)
512*3b188dabSeric 			log_warn("%s: ioctl(TIOCGWINSZ)", printer);
513*3b188dabSeric 
514*3b188dabSeric 		argv = calloc(256, sizeof(char *));
515*3b188dabSeric 		if (argv == NULL)
516*3b188dabSeric 			fatal("%s: malloc", printer);
517*3b188dabSeric 
518*3b188dabSeric 		p = strdup(lp->lp_ms);
519*3b188dabSeric 		ap = argv;
520*3b188dabSeric 		ep = argv + 255;
521*3b188dabSeric 		while ((val = strsep(&p, " \t,")) != NULL) {
522*3b188dabSeric 			if ((*ap++ = strdup(val)) == NULL)
523*3b188dabSeric 				fatal("%s: strdup", printer);
524*3b188dabSeric 			if (ap == ep)
525*3b188dabSeric 				fatal("%s: too many \"ms\" entries", printer);
526*3b188dabSeric 		}
527*3b188dabSeric 		*ap = NULL;
528*3b188dabSeric 
529*3b188dabSeric 		for (; *argv; ++argv) {
530*3b188dabSeric 			if (ksearch(&argv, &i))
531*3b188dabSeric 				continue;
532*3b188dabSeric 			if (msearch(&argv, &i))
533*3b188dabSeric 				continue;
534*3b188dabSeric 			log_warnx("%s: unknown stty flag: %s", printer, *argv);
535*3b188dabSeric 		}
536*3b188dabSeric 	}
537*3b188dabSeric 
538*3b188dabSeric 	if (i.set && tcsetattr(i.fd, TCSANOW, &i.t) == -1)
539*3b188dabSeric 		fatal("%s: tcsetattr", printer);
540*3b188dabSeric 
541*3b188dabSeric 	if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) == -1)
542*3b188dabSeric 		log_warn("%s: ioctl(TIOCSWINSZ)", printer);
543*3b188dabSeric }
544