xref: /freebsd/bin/stty/gfmt.c (revision 51e235148a4becba94e824a44bd69687644a7f56)
1 /*-
2  * Copyright (c) 1991, 1993, 1994
3  *	The Regents of the University of California.  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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef lint
31 #if 0
32 static char sccsid[] = "@(#)gfmt.c	8.6 (Berkeley) 4/2/94";
33 #endif
34 #endif /* not lint */
35 #include <sys/cdefs.h>
36 #include <sys/types.h>
37 
38 #include <err.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 
43 #include "stty.h"
44 #include "extern.h"
45 
46 static void gerr(const char *s) __dead2;
47 
48 static void
49 gerr(const char *s)
50 {
51 	if (s)
52 		errx(1, "illegal gfmt1 option -- %s", s);
53 	else
54 		errx(1, "illegal gfmt1 option");
55 }
56 
57 void
58 gprint(struct termios *tp, struct winsize *wp __unused, int ldisc __unused)
59 {
60 	struct cchar *cp;
61 
62 	(void)printf("gfmt1:cflag=%lx:iflag=%lx:lflag=%lx:oflag=%lx:",
63 	    (u_long)tp->c_cflag, (u_long)tp->c_iflag, (u_long)tp->c_lflag,
64 	    (u_long)tp->c_oflag);
65 	for (cp = cchars1; cp->name; ++cp)
66 		(void)printf("%s=%x:", cp->name, tp->c_cc[cp->sub]);
67 	(void)printf("ispeed=%lu:ospeed=%lu\n",
68 	    (u_long)cfgetispeed(tp), (u_long)cfgetospeed(tp));
69 }
70 
71 void
72 gread(struct termios *tp, char *s)
73 {
74 	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 		tmp = strtoul(ep, NULL, 0x10);
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 			tmp = strtoul(ep, NULL, 10);
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 			tmp = strtoul(ep, NULL, 10);
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 					tmp = strtoul(ep, NULL, 10);
120 				tp->c_cc[cp->sub] = tmp;
121 				break;
122 			}
123 		if (cp->name == NULL)
124 			gerr(p);
125 	}
126 }
127