xref: /openbsd/bin/stty/gfmt.c (revision 2076f375)
1 /*	$OpenBSD: gfmt.c,v 1.9 2016/03/23 14:52:42 mmcc 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 #include <sys/types.h>
34 
35 #include <err.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <termios.h>
39 
40 #include "stty.h"
41 #include "extern.h"
42 
43 static void
gerr(char * s)44 gerr(char *s)
45 {
46 	if (s)
47 		errx(1, "illegal gfmt1 option -- %s", s);
48 	else
49 		errx(1, "illegal gfmt1 option");
50 }
51 
52 void
gprint(struct termios * tp,struct winsize * wp,int ldisc)53 gprint(struct termios *tp, struct winsize *wp, int ldisc)
54 {
55 	const struct cchar *cp;
56 
57 	(void)printf("gfmt1:cflag=%x:iflag=%x:lflag=%x:oflag=%x:",
58 	    tp->c_cflag, tp->c_iflag, tp->c_lflag, tp->c_oflag);
59 	for (cp = cchars1; cp->name; ++cp)
60 		(void)printf("%s=%x:", cp->name, tp->c_cc[cp->sub]);
61 	(void)printf("ispeed=%d:ospeed=%d\n", cfgetispeed(tp), cfgetospeed(tp));
62 }
63 
64 void
gread(struct termios * tp,char * s)65 gread(struct termios *tp, char *s)
66 {
67 	const struct cchar *cp;
68 	char *ep, *p;
69 	long tmp;
70 
71 	if ((s = strchr(s, ':')) == NULL)
72 		gerr(NULL);
73 	for (++s; s != NULL;) {
74 		p = strsep(&s, ":\0");
75 		if (!p || !*p)
76 			break;
77 		if (!(ep = strchr(p, '=')))
78 			gerr(p);
79 		*ep++ = '\0';
80 		(void)sscanf(ep, "%lx", &tmp);
81 
82 #define	CHK(s)	(*p == s[0] && !strcmp(p, s))
83 		if (CHK("cflag")) {
84 			tp->c_cflag = tmp;
85 			continue;
86 		}
87 		if (CHK("iflag")) {
88 			tp->c_iflag = tmp;
89 			continue;
90 		}
91 		if (CHK("ispeed")) {
92 			(void)sscanf(ep, "%ld", &tmp);
93 			tp->c_ispeed = tmp;
94 			continue;
95 		}
96 		if (CHK("lflag")) {
97 			tp->c_lflag = tmp;
98 			continue;
99 		}
100 		if (CHK("oflag")) {
101 			tp->c_oflag = tmp;
102 			continue;
103 		}
104 		if (CHK("ospeed")) {
105 			(void)sscanf(ep, "%ld", &tmp);
106 			tp->c_ospeed = tmp;
107 			continue;
108 		}
109 		for (cp = cchars1; cp->name != NULL; ++cp)
110 			if (CHK(cp->name)) {
111 				tp->c_cc[cp->sub] = tmp;
112 				break;
113 			}
114 		if (cp->name == NULL)
115 			gerr(p);
116 	}
117 }
118