xref: /original-bsd/bin/stty/gfmt.c (revision 5e598c08)
1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)gfmt.c	8.2 (Berkeley) 03/17/94";
10 #endif /* not lint */
11 
12 #include <sys/types.h>
13 
14 #include <err.h>
15 #include <stdio.h>
16 #include <string.h>
17 
18 #include "stty.h"
19 #include "extern.h"
20 
21 static void gerr __P((char *));
22 
23 void
24 gprint(tp, wp, ldisc)
25 	struct termios *tp;
26 	struct winsize *wp;
27 	int ldisc;
28 {
29 	register struct cchar *cp;
30 
31 	(void)printf("gfmt1:cflag=%x:iflag=%x:lflag=%x:oflag=%x:",
32 	    tp->c_cflag, tp->c_iflag, tp->c_lflag, tp->c_oflag);
33 	for (cp = cchars1; cp->name; ++cp)
34 		(void)printf("%s=%x:", cp->name, tp->c_cc[cp->sub]);
35 	(void)printf("ispeed=%d:ospeed=%d\n", cfgetispeed(tp), cfgetospeed(tp));
36 }
37 
38 void
39 gread(tp, s)
40 	register struct termios *tp;
41 	char *s;
42 {
43 	register char *ep, *p;
44 	long tmp;
45 
46 #define	CHK(s)	(*p == s[0] && !strcmp(p, s))
47 	if (!(s = strchr(s, ':')))
48 		gerr(NULL);
49 	for (++s; s;) {
50 		p = strsep(&s, ":\0");
51 		if (!p || !*p)
52 			break;
53 		if (!(ep = strchr(p, '=')))
54 			gerr(p);
55 		*ep++ = '\0';
56 		(void)sscanf(ep, "%lx", &tmp);
57 		if (CHK("cflag")) {
58 			tp->c_cflag = tmp;
59 			continue;
60 		}
61 		if (CHK("discard")) {
62 			tp->c_cc[VDISCARD] = tmp;
63 			continue;
64 		}
65 		if (CHK("dsusp")) {
66 			tp->c_cc[VDSUSP] = tmp;
67 			continue;
68 		}
69 		if (CHK("eof")) {
70 			tp->c_cc[VEOF] = tmp;
71 			continue;
72 		}
73 		if (CHK("eol")) {
74 			tp->c_cc[VEOL] = tmp;
75 			continue;
76 		}
77 		if (CHK("eol2")) {
78 			tp->c_cc[VEOL2] = tmp;
79 			continue;
80 		}
81 		if (CHK("erase")) {
82 			tp->c_cc[VERASE] = tmp;
83 			continue;
84 		}
85 		if (CHK("iflag")) {
86 			tp->c_iflag = tmp;
87 			continue;
88 		}
89 		if (CHK("intr")) {
90 			tp->c_cc[VINTR] = tmp;
91 			continue;
92 		}
93 		if (CHK("ispeed")) {
94 			(void)sscanf(ep, "%ld", &tmp);
95 			tp->c_ispeed = tmp;
96 			continue;
97 		}
98 		if (CHK("kill")) {
99 			tp->c_cc[VKILL] = tmp;
100 			continue;
101 		}
102 		if (CHK("lflag")) {
103 			tp->c_lflag = tmp;
104 			continue;
105 		}
106 		if (CHK("lnext")) {
107 			tp->c_cc[VLNEXT] = tmp;
108 			continue;
109 		}
110 		if (CHK("oflag")) {
111 			tp->c_oflag = tmp;
112 			continue;
113 		}
114 		if (CHK("ospeed")) {
115 			(void)sscanf(ep, "%ld", &tmp);
116 			tp->c_ospeed = tmp;
117 			continue;
118 		}
119 		if (CHK("quit")) {
120 			tp->c_cc[VQUIT] = tmp;
121 			continue;
122 		}
123 		if (CHK("reprint")) {
124 			tp->c_cc[VREPRINT] = tmp;
125 			continue;
126 		}
127 		if (CHK("start")) {
128 			tp->c_cc[VSTART] = tmp;
129 			continue;
130 		}
131 		if (CHK("status")) {
132 			tp->c_cc[VSTATUS] = tmp;
133 			continue;
134 		}
135 		if (CHK("stop")) {
136 			tp->c_cc[VSTOP] = tmp;
137 			continue;
138 		}
139 		if (CHK("susp")) {
140 			tp->c_cc[VSUSP] = tmp;
141 			continue;
142 		}
143 		if (CHK("vmin")) {
144 			(void)sscanf(ep, "%ld", &tmp);
145 			tp->c_cc[VMIN] = tmp;
146 			continue;
147 		}
148 		if (CHK("vtime")) {
149 			(void)sscanf(ep, "%ld", &tmp);
150 			tp->c_cc[VTIME] = tmp;
151 			continue;
152 		}
153 		if (CHK("werase")) {
154 			tp->c_cc[VWERASE] = tmp;
155 			continue;
156 		}
157 		gerr(p);
158 	}
159 }
160 
161 static void
162 gerr(s)
163 	char *s;
164 {
165 	if (s)
166 		errx(1, "illegal gfmt1 option -- %s", s);
167 	else
168 		errx(1, "illegal gfmt1 option");
169 }
170