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