Home | History | Annotate | Line # | Download | only in stty
gfmt.c revision 1.7
      1 /*-
      2  * Copyright (c) 1991 The Regents of the University of California.
      3  * 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	5.4 (Berkeley) 6/10/91";*/
     36 static char rcsid[] = "$Id: gfmt.c,v 1.7 1994/03/23 05:05:30 mycroft Exp $";
     37 #endif /* not lint */
     38 
     39 #include <sys/types.h>
     40 #include <err.h>
     41 #include <stdio.h>
     42 #include <string.h>
     43 #include "stty.h"
     44 #include "extern.h"
     45 
     46 static void gerr __P((char *));
     47 
     48 void
     49 gprint(tp, wp, ldisc)
     50 	struct termios *tp;
     51 	struct winsize *wp;
     52 	int ldisc;
     53 {
     54 	register struct cchar *cp;
     55 
     56 	(void)printf("gfmt1:cflag=%x:iflag=%x:lflag=%x:oflag=%x:",
     57 	    tp->c_cflag, tp->c_iflag, tp->c_lflag, tp->c_oflag);
     58 	for (cp = cchars1; cp->name; ++cp)
     59 		(void)printf("%s=%x:", cp->name, tp->c_cc[cp->sub]);
     60 	(void)printf("ispeed=%d:ospeed=%d\n", cfgetispeed(tp), cfgetospeed(tp));
     61 }
     62 
     63 void
     64 gread(tp, s)
     65 	register struct termios *tp;
     66 	char *s;
     67 {
     68 	register char *ep, *p;
     69 	register struct cchar *cp;
     70 	long tmp;
     71 
     72 #define	CHK(s)	(*p == s[0] && !strcmp(p, s))
     73 	if (!(s = index(s, ':')))
     74 		gerr(NULL);
     75 	for (++s; s;) {
     76 		p = strsep(&s, ":\0");
     77 		if (!p || !*p)
     78 			break;
     79 		if (!(ep = index(p, '=')))
     80 			gerr(p);
     81 		*ep++ = '\0';
     82 		(void)sscanf(ep, "%lx", &tmp);
     83 		if (CHK("cflag")) {
     84 			tp->c_cflag = tmp;
     85 			continue;
     86 		}
     87 		if (CHK("iflag")) {
     88 			tp->c_iflag = tmp;
     89 			continue;
     90 		}
     91 		if (CHK("ispeed")) {
     92 			(void)sscanf(ep, "%ld", &tmp);
     93 			tp->c_ispeed = tmp;
     94 			continue;
     95 		}
     96 		if (CHK("lflag")) {
     97 			tp->c_lflag = tmp;
     98 			continue;
     99 		}
    100 		if (CHK("oflag")) {
    101 			tp->c_oflag = tmp;
    102 			continue;
    103 		}
    104 		if (CHK("ospeed")) {
    105 			(void)sscanf(ep, "%ld", &tmp);
    106 			tp->c_ospeed = tmp;
    107 			continue;
    108 		}
    109 		for (cp = cchars1; cp->name; ++cp) {
    110 			if (CHK(cp->name)) {
    111 				if (cp->sub == VMIN || cp->sub == VTIME)
    112 					(void)sscanf(ep, "%ld", &tmp);
    113 				tp->c_cc[cp->sub] = tmp;
    114 				break;
    115 			}
    116 		}
    117 		if (!cp->name)
    118 			gerr(p);
    119 	}
    120 }
    121 
    122 static void
    123 gerr(s)
    124 	char *s;
    125 {
    126 	if (s)
    127 		errx(1, "illegal gfmt1 option -- %s", s);
    128 	else
    129 		errx(1, "illegal gfmt1 option");
    130 }
    131