xref: /freebsd/bin/stty/gfmt.c (revision d0834e7f9dc7284bf4ba993f69fb9a836ee8ee99)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993, 1994
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
64b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
74b88c807SRodney W. Grimes  * are met:
84b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
94b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
104b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
114b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
124b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
134b88c807SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
144b88c807SRodney W. Grimes  *    must display the following acknowledgement:
154b88c807SRodney W. Grimes  *	This product includes software developed by the University of
164b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
174b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
184b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
194b88c807SRodney W. Grimes  *    without specific prior written permission.
204b88c807SRodney W. Grimes  *
214b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
224b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
234b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
244b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
254b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
264b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
274b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
284b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
294b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
304b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
314b88c807SRodney W. Grimes  * SUCH DAMAGE.
324b88c807SRodney W. Grimes  */
334b88c807SRodney W. Grimes 
344b88c807SRodney W. Grimes #ifndef lint
359ba8bd65SPhilippe Charnier #if 0
369ba8bd65SPhilippe Charnier static char sccsid[] = "@(#)gfmt.c	8.6 (Berkeley) 4/2/94";
379ba8bd65SPhilippe Charnier #endif
389ba8bd65SPhilippe Charnier static const char rcsid[] =
392a456239SPeter Wemm   "$FreeBSD$";
404b88c807SRodney W. Grimes #endif /* not lint */
414b88c807SRodney W. Grimes 
424b88c807SRodney W. Grimes #include <sys/types.h>
434b88c807SRodney W. Grimes 
444b88c807SRodney W. Grimes #include <err.h>
454b88c807SRodney W. Grimes #include <stdio.h>
464b88c807SRodney W. Grimes #include <string.h>
474b88c807SRodney W. Grimes 
484b88c807SRodney W. Grimes #include "stty.h"
494b88c807SRodney W. Grimes #include "extern.h"
504b88c807SRodney W. Grimes 
514b88c807SRodney W. Grimes static void
524b88c807SRodney W. Grimes gerr(s)
534b88c807SRodney W. Grimes 	char *s;
544b88c807SRodney W. Grimes {
554b88c807SRodney W. Grimes 	if (s)
564b88c807SRodney W. Grimes 		errx(1, "illegal gfmt1 option -- %s", s);
574b88c807SRodney W. Grimes 	else
584b88c807SRodney W. Grimes 		errx(1, "illegal gfmt1 option");
594b88c807SRodney W. Grimes }
604b88c807SRodney W. Grimes 
614b88c807SRodney W. Grimes void
62d0834e7fSAndrey A. Chernov gprint(tp, wp, ldisc, timeout)
634b88c807SRodney W. Grimes 	struct termios *tp;
644b88c807SRodney W. Grimes 	struct winsize *wp;
654b88c807SRodney W. Grimes 	int ldisc;
66d0834e7fSAndrey A. Chernov 	int timeout;
674b88c807SRodney W. Grimes {
684b88c807SRodney W. Grimes 	struct cchar *cp;
694b88c807SRodney W. Grimes 
700fd510b7SJoerg Wunsch 	(void)printf("gfmt1:cflag=%lx:iflag=%lx:lflag=%lx:oflag=%lx:",
710eb7b1caSBruce Evans 	    (u_long)tp->c_cflag, (u_long)tp->c_iflag, (u_long)tp->c_lflag,
720eb7b1caSBruce Evans 	    (u_long)tp->c_oflag);
734b88c807SRodney W. Grimes 	for (cp = cchars1; cp->name; ++cp)
744b88c807SRodney W. Grimes 		(void)printf("%s=%x:", cp->name, tp->c_cc[cp->sub]);
75d0834e7fSAndrey A. Chernov 	(void)printf("ispeed=%lu:ospeed=%lu:drainwait=%d\n",
76d0834e7fSAndrey A. Chernov 	    (u_long)cfgetispeed(tp), (u_long)cfgetospeed(tp), timeout);
774b88c807SRodney W. Grimes }
784b88c807SRodney W. Grimes 
794b88c807SRodney W. Grimes void
80d0834e7fSAndrey A. Chernov gread(tp, top, s)
814b88c807SRodney W. Grimes 	struct termios *tp;
82d0834e7fSAndrey A. Chernov 	int *top;
834b88c807SRodney W. Grimes 	char *s;
844b88c807SRodney W. Grimes {
854b88c807SRodney W. Grimes 	struct cchar *cp;
864b88c807SRodney W. Grimes 	char *ep, *p;
874b88c807SRodney W. Grimes 	long tmp;
884b88c807SRodney W. Grimes 
894b88c807SRodney W. Grimes 	if ((s = strchr(s, ':')) == NULL)
904b88c807SRodney W. Grimes 		gerr(NULL);
914b88c807SRodney W. Grimes 	for (++s; s != NULL;) {
924b88c807SRodney W. Grimes 		p = strsep(&s, ":\0");
934b88c807SRodney W. Grimes 		if (!p || !*p)
944b88c807SRodney W. Grimes 			break;
954b88c807SRodney W. Grimes 		if (!(ep = strchr(p, '=')))
964b88c807SRodney W. Grimes 			gerr(p);
974b88c807SRodney W. Grimes 		*ep++ = '\0';
984b88c807SRodney W. Grimes 		(void)sscanf(ep, "%lx", &tmp);
994b88c807SRodney W. Grimes 
1004b88c807SRodney W. Grimes #define	CHK(s)	(*p == s[0] && !strcmp(p, s))
1014b88c807SRodney W. Grimes 		if (CHK("cflag")) {
1024b88c807SRodney W. Grimes 			tp->c_cflag = tmp;
1034b88c807SRodney W. Grimes 			continue;
1044b88c807SRodney W. Grimes 		}
1054b88c807SRodney W. Grimes 		if (CHK("iflag")) {
1064b88c807SRodney W. Grimes 			tp->c_iflag = tmp;
1074b88c807SRodney W. Grimes 			continue;
1084b88c807SRodney W. Grimes 		}
1094b88c807SRodney W. Grimes 		if (CHK("ispeed")) {
1104b88c807SRodney W. Grimes 			(void)sscanf(ep, "%ld", &tmp);
1114b88c807SRodney W. Grimes 			tp->c_ispeed = tmp;
1124b88c807SRodney W. Grimes 			continue;
1134b88c807SRodney W. Grimes 		}
1144b88c807SRodney W. Grimes 		if (CHK("lflag")) {
1154b88c807SRodney W. Grimes 			tp->c_lflag = tmp;
1164b88c807SRodney W. Grimes 			continue;
1174b88c807SRodney W. Grimes 		}
1184b88c807SRodney W. Grimes 		if (CHK("oflag")) {
1194b88c807SRodney W. Grimes 			tp->c_oflag = tmp;
1204b88c807SRodney W. Grimes 			continue;
1214b88c807SRodney W. Grimes 		}
1224b88c807SRodney W. Grimes 		if (CHK("ospeed")) {
1234b88c807SRodney W. Grimes 			(void)sscanf(ep, "%ld", &tmp);
1244b88c807SRodney W. Grimes 			tp->c_ospeed = tmp;
1254b88c807SRodney W. Grimes 			continue;
1264b88c807SRodney W. Grimes 		}
127d0834e7fSAndrey A. Chernov 		if (CHK("drainwait")) {
128d0834e7fSAndrey A. Chernov 			(void)sscanf(ep, "%ld", &tmp);
129d0834e7fSAndrey A. Chernov 			*top = tmp;
130d0834e7fSAndrey A. Chernov 			continue;
131d0834e7fSAndrey A. Chernov 		}
1324b88c807SRodney W. Grimes 		for (cp = cchars1; cp->name != NULL; ++cp)
1334b88c807SRodney W. Grimes 			if (CHK(cp->name)) {
1344b88c807SRodney W. Grimes 				if (cp->sub == VMIN || cp->sub == VTIME)
1354b88c807SRodney W. Grimes 					(void)sscanf(ep, "%ld", &tmp);
1364b88c807SRodney W. Grimes 				tp->c_cc[cp->sub] = tmp;
1374b88c807SRodney W. Grimes 				break;
1384b88c807SRodney W. Grimes 			}
1394b88c807SRodney W. Grimes 		if (cp->name == NULL)
1404b88c807SRodney W. Grimes 			gerr(p);
1414b88c807SRodney W. Grimes 	}
1424b88c807SRodney W. Grimes }
143