Home | History | Annotate | Line # | Download | only in stty
gfmt.c revision 1.8
      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. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 /*static char sccsid[] = "from: @(#)gfmt.c	8.6 (Berkeley) 4/2/94";*/
     36 static char *rcsid = "$Id: gfmt.c,v 1.8 1994/09/20 04:52:05 mycroft Exp $";
     37 #endif /* not lint */
     38 
     39 #include <sys/types.h>
     40 
     41 #include <err.h>
     42 #include <stdio.h>
     43 #include <string.h>
     44 
     45 #include "stty.h"
     46 #include "extern.h"
     47 
     48 static void
     49 gerr(s)
     50 	char *s;
     51 {
     52 	if (s)
     53 		errx(1, "illegal gfmt1 option -- %s", s);
     54 	else
     55 		errx(1, "illegal gfmt1 option");
     56 }
     57 
     58 void
     59 gprint(tp, wp, ldisc)
     60 	struct termios *tp;
     61 	struct winsize *wp;
     62 	int ldisc;
     63 {
     64 	struct cchar *cp;
     65 
     66 	(void)printf("gfmt1:cflag=%x:iflag=%x:lflag=%x:oflag=%x:",
     67 	    tp->c_cflag, tp->c_iflag, tp->c_lflag, tp->c_oflag);
     68 	for (cp = cchars1; cp->name; ++cp)
     69 		(void)printf("%s=%x:", cp->name, tp->c_cc[cp->sub]);
     70 	(void)printf("ispeed=%d:ospeed=%d\n", cfgetispeed(tp), cfgetospeed(tp));
     71 }
     72 
     73 void
     74 gread(tp, s)
     75 	struct termios *tp;
     76 	char *s;
     77 {
     78 	struct cchar *cp;
     79 	char *ep, *p;
     80 	long tmp;
     81 
     82 	if ((s = strchr(s, ':')) == NULL)
     83 		gerr(NULL);
     84 	for (++s; s != NULL;) {
     85 		p = strsep(&s, ":\0");
     86 		if (!p || !*p)
     87 			break;
     88 		if (!(ep = strchr(p, '=')))
     89 			gerr(p);
     90 		*ep++ = '\0';
     91 		(void)sscanf(ep, "%lx", &tmp);
     92 
     93 #define	CHK(s)	(*p == s[0] && !strcmp(p, s))
     94 		if (CHK("cflag")) {
     95 			tp->c_cflag = tmp;
     96 			continue;
     97 		}
     98 		if (CHK("iflag")) {
     99 			tp->c_iflag = tmp;
    100 			continue;
    101 		}
    102 		if (CHK("ispeed")) {
    103 			(void)sscanf(ep, "%ld", &tmp);
    104 			tp->c_ispeed = tmp;
    105 			continue;
    106 		}
    107 		if (CHK("lflag")) {
    108 			tp->c_lflag = tmp;
    109 			continue;
    110 		}
    111 		if (CHK("oflag")) {
    112 			tp->c_oflag = tmp;
    113 			continue;
    114 		}
    115 		if (CHK("ospeed")) {
    116 			(void)sscanf(ep, "%ld", &tmp);
    117 			tp->c_ospeed = tmp;
    118 			continue;
    119 		}
    120 		for (cp = cchars1; cp->name != NULL; ++cp)
    121 			if (CHK(cp->name)) {
    122 				if (cp->sub == VMIN || cp->sub == VTIME)
    123 					(void)sscanf(ep, "%ld", &tmp);
    124 				tp->c_cc[cp->sub] = tmp;
    125 				break;
    126 			}
    127 		if (cp->name == NULL)
    128 			gerr(p);
    129 	}
    130 }
    131