1 /*	$OpenBSD: gfmt.c,v 1.6 2003/06/11 23:42:12 deraadt Exp $	*/
2 /*	$NetBSD: gfmt.c,v 1.10 1996/05/07 18:20:08 jtc Exp $	*/
3 
4 /*-
5  * Copyright (c) 1991, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)gfmt.c	8.6 (Berkeley) 4/2/94";
36 #else
37 static char rcsid[] = "$OpenBSD: gfmt.c,v 1.6 2003/06/11 23:42:12 deraadt Exp $";
38 #endif
39 #endif /* not lint */
40 
41 #include <sys/types.h>
42 
43 #include <err.h>
44 #include <stdio.h>
45 #include <string.h>
46 
47 #include "stty.h"
48 #include "extern.h"
49 
50 static void
gerr(char * s)51 gerr(char *s)
52 {
53 	if (s)
54 		errx(1, "illegal gfmt1 option -- %s", s);
55 	else
56 		errx(1, "illegal gfmt1 option");
57 }
58 
59 void
gprint(struct termios * tp,struct winsize * wp,int ldisc)60 gprint(struct termios *tp, struct winsize *wp, int ldisc)
61 {
62 	const struct cchar *cp;
63 
64 	(void)printf("gfmt1:cflag=%x:iflag=%x:lflag=%x:oflag=%x:",
65 	    tp->c_cflag, tp->c_iflag, tp->c_lflag, tp->c_oflag);
66 	for (cp = cchars1; cp->name; ++cp)
67 		(void)printf("%s=%x:", cp->name, tp->c_cc[cp->sub]);
68 	(void)printf("ispeed=%d:ospeed=%d\n", cfgetispeed(tp), cfgetospeed(tp));
69 }
70 
71 void
gread(struct termios * tp,char * s)72 gread(struct termios *tp, char *s)
73 {
74 	const struct cchar *cp;
75 	char *ep, *p;
76 	long tmp;
77 
78 	if ((s = strchr(s, ':')) == NULL)
79 		gerr(NULL);
80 	for (++s; s != NULL;) {
81 		p = strsep(&s, ":\0");
82 		if (!p || !*p)
83 			break;
84 		if (!(ep = strchr(p, '=')))
85 			gerr(p);
86 		*ep++ = '\0';
87 		(void)sscanf(ep, "%lx", &tmp);
88 
89 #define	CHK(s)	(*p == s[0] && !strcmp(p, s))
90 		if (CHK("cflag")) {
91 			tp->c_cflag = tmp;
92 			continue;
93 		}
94 		if (CHK("iflag")) {
95 			tp->c_iflag = tmp;
96 			continue;
97 		}
98 		if (CHK("ispeed")) {
99 			(void)sscanf(ep, "%ld", &tmp);
100 			tp->c_ispeed = tmp;
101 			continue;
102 		}
103 		if (CHK("lflag")) {
104 			tp->c_lflag = tmp;
105 			continue;
106 		}
107 		if (CHK("oflag")) {
108 			tp->c_oflag = tmp;
109 			continue;
110 		}
111 		if (CHK("ospeed")) {
112 			(void)sscanf(ep, "%ld", &tmp);
113 			tp->c_ospeed = tmp;
114 			continue;
115 		}
116 		for (cp = cchars1; cp->name != NULL; ++cp)
117 			if (CHK(cp->name)) {
118 				if (cp->sub == VMIN || cp->sub == VTIME)
119 					(void)sscanf(ep, "%ld", &tmp);
120 				tp->c_cc[cp->sub] = tmp;
121 				break;
122 			}
123 		if (cp->name == NULL)
124 			gerr(p);
125 	}
126 }
127