xref: /netbsd-src/bin/stty/gfmt.c (revision cda4f8f6ee55684e8d311b86c99ea59191e6b74f)
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 /* from: static char sccsid[] = "@(#)gfmt.c	5.4 (Berkeley) 6/10/91"; */
36 static char rcsid[] = "$Id: gfmt.c,v 1.4 1993/06/01 14:42:12 cgd Exp $";
37 #endif /* not lint */
38 
39 #include <sys/types.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include "stty.h"
43 #include "extern.h"
44 
45 static void gerr __P((char *));
46 
47 void
48 gprint(tp, wp, ldisc)
49 	struct termios *tp;
50 	struct winsize *wp;
51 	int ldisc;
52 {
53 	register struct cchar *cp;
54 
55 	(void)printf("gfmt1:cflag=%x:iflag=%x:lflag=%x:oflag=%x:",
56 	    tp->c_cflag, tp->c_iflag, tp->c_lflag, tp->c_oflag);
57 	for (cp = cchars1; cp->name; ++cp)
58 		(void)printf("%s=%x:", cp->name, tp->c_cc[cp->sub]);
59 	(void)printf("ispeed=%d:ospeed=%d\n", cfgetispeed(tp), cfgetospeed(tp));
60 }
61 
62 void
63 gread(tp, s)
64 	register struct termios *tp;
65 	char *s;
66 {
67 	register char *ep, *p;
68 	long tmp;
69 
70 #define	CHK(s)	(*p == s[0] && !strcmp(p, s))
71 	if (!(s = index(s, ':')))
72 		gerr(NULL);
73 	for (++s; s;) {
74 		p = strsep(&s, ":\0");
75 		if (!p || !*p)
76 			break;
77 		if (!(ep = index(p, '=')))
78 			gerr(p);
79 		*ep++ = '\0';
80 		(void)sscanf(ep, "%lx", &tmp);
81 		if (CHK("cflag")) {
82 			tp->c_cflag = tmp;
83 			continue;
84 		}
85 		if (CHK("discard")) {
86 			tp->c_cc[VDISCARD] = tmp;
87 			continue;
88 		}
89 		if (CHK("dsusp")) {
90 			tp->c_cc[VDSUSP] = tmp;
91 			continue;
92 		}
93 		if (CHK("eof")) {
94 			tp->c_cc[VEOF] = tmp;
95 			continue;
96 		}
97 		if (CHK("eol")) {
98 			tp->c_cc[VEOL] = tmp;
99 			continue;
100 		}
101 		if (CHK("eol2")) {
102 			tp->c_cc[VEOL2] = tmp;
103 			continue;
104 		}
105 		if (CHK("erase")) {
106 			tp->c_cc[VERASE] = tmp;
107 			continue;
108 		}
109 		if (CHK("iflag")) {
110 			tp->c_iflag = tmp;
111 			continue;
112 		}
113 		if (CHK("intr")) {
114 			tp->c_cc[VINTR] = tmp;
115 			continue;
116 		}
117 		if (CHK("ispeed")) {
118 			(void)sscanf(ep, "%ld", &tmp);
119 			tp->c_ispeed = tmp;
120 			continue;
121 		}
122 		if (CHK("kill")) {
123 			tp->c_cc[VKILL] = tmp;
124 			continue;
125 		}
126 		if (CHK("lflag")) {
127 			tp->c_lflag = tmp;
128 			continue;
129 		}
130 		if (CHK("lnext")) {
131 			tp->c_cc[VLNEXT] = tmp;
132 			continue;
133 		}
134 		if (CHK("oflag")) {
135 			tp->c_oflag = tmp;
136 			continue;
137 		}
138 		if (CHK("ospeed")) {
139 			(void)sscanf(ep, "%ld", &tmp);
140 			tp->c_ospeed = tmp;
141 			continue;
142 		}
143 		if (CHK("quit")) {
144 			tp->c_cc[VQUIT] = tmp;
145 			continue;
146 		}
147 		if (CHK("reprint")) {
148 			tp->c_cc[VREPRINT] = tmp;
149 			continue;
150 		}
151 		if (CHK("start")) {
152 			tp->c_cc[VSTART] = tmp;
153 			continue;
154 		}
155 		if (CHK("status")) {
156 			tp->c_cc[VSTATUS] = tmp;
157 			continue;
158 		}
159 		if (CHK("stop")) {
160 			tp->c_cc[VSTOP] = tmp;
161 			continue;
162 		}
163 		if (CHK("susp")) {
164 			tp->c_cc[VSUSP] = tmp;
165 			continue;
166 		}
167 		if (CHK("werase")) {
168 			tp->c_cc[VWERASE] = tmp;
169 			continue;
170 		}
171 		gerr(p);
172 	}
173 }
174 
175 static void
176 gerr(s)
177 	char *s;
178 {
179 	if (s)
180 		err("illegal gfmt1 option -- %s", s);
181 	else
182 		err("illegal gfmt1 option");
183 }
184