xref: /original-bsd/bin/stty/gfmt.c (revision 3413c235)
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.5 (Berkeley) 04/01/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
22 gerr(s)
23 	char *s;
24 {
25 	if (s)
26 		errx(1, "illegal gfmt1 option -- %s", s);
27 	else
28 		errx(1, "illegal gfmt1 option");
29 }
30 
31 void
32 gprint(tp, wp, ldisc)
33 	struct termios *tp;
34 	struct winsize *wp;
35 	int ldisc;
36 {
37 	struct cchar *cp;
38 
39 	(void)printf("gfmt1:cflag=%x:iflag=%x:lflag=%x:oflag=%x:",
40 	    tp->c_cflag, tp->c_iflag, tp->c_lflag, tp->c_oflag);
41 	for (cp = cchars1; cp->name; ++cp)
42 		(void)printf("%s=%x:", cp->name, tp->c_cc[cp->sub]);
43 	(void)printf("ispeed=%d:ospeed=%d\n", cfgetispeed(tp), cfgetospeed(tp));
44 }
45 
46 void
47 gread(tp, s)
48 	struct termios *tp;
49 	char *s;
50 {
51 	struct cchar *cp;
52 	char *ep, *p;
53 	long tmp;
54 
55 	if ((s = strchr(s, ':')) == NULL)
56 		gerr(NULL);
57 	for (++s; s != NULL;) {
58 		p = strsep(&s, ":\0");
59 		if (!p || !*p)
60 			break;
61 		if (!(ep = strchr(p, '=')))
62 			gerr(p);
63 		*ep++ = '\0';
64 		(void)sscanf(ep, "%lx", &tmp);
65 
66 #define	CHK(s)	(*p == s[0] && !strcmp(p, s))
67 		if (CHK("cflag")) {
68 			tp->c_cflag = tmp;
69 			continue;
70 		}
71 		if (CHK("iflag")) {
72 			tp->c_iflag = tmp;
73 			continue;
74 		}
75 		if (CHK("ispeed")) {
76 			(void)sscanf(ep, "%ld", &tmp);
77 			tp->c_ispeed = tmp;
78 			continue;
79 		}
80 		if (CHK("lflag")) {
81 			tp->c_lflag = tmp;
82 			continue;
83 		}
84 		if (CHK("oflag")) {
85 			tp->c_oflag = tmp;
86 			continue;
87 		}
88 		if (CHK("ospeed")) {
89 			(void)sscanf(ep, "%ld", &tmp);
90 			tp->c_ospeed = tmp;
91 			continue;
92 		}
93 		for (cp = cchars1; cp->name != NULL; ++cp)
94 			if (CHK(cp->name)) {
95 				if (cp->sub == VMIN || cp->sub == VTIME)
96 					(void)sscanf(ep, "%ld", &tmp);
97 				tp->c_cc[cp->sub] = tmp;
98 				break;
99 			}
100 		if (cp->name == NULL)
101 			gerr(p);
102 	}
103 }
104