xref: /titanic_52/usr/src/cmd/ttymon/tmttydefs.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include	<unistd.h>
34*7c478bd9Sstevel@tonic-gate #include	<stdlib.h>
35*7c478bd9Sstevel@tonic-gate #include	<stdio.h>
36*7c478bd9Sstevel@tonic-gate #include	<ctype.h>
37*7c478bd9Sstevel@tonic-gate #include	<string.h>
38*7c478bd9Sstevel@tonic-gate #include	<sys/types.h>
39*7c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
40*7c478bd9Sstevel@tonic-gate #include	<termio.h>
41*7c478bd9Sstevel@tonic-gate #include 	<sys/stermio.h>
42*7c478bd9Sstevel@tonic-gate #include 	<sys/termiox.h>
43*7c478bd9Sstevel@tonic-gate #include	"ttymon.h"
44*7c478bd9Sstevel@tonic-gate #include	"tmstruct.h"
45*7c478bd9Sstevel@tonic-gate #include	"tmextern.h"
46*7c478bd9Sstevel@tonic-gate #include	"stty.h"
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate static 	void	insert_def();
49*7c478bd9Sstevel@tonic-gate static	void	zero();
50*7c478bd9Sstevel@tonic-gate int	check_flags();
51*7c478bd9Sstevel@tonic-gate void	mkargv();
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate extern	struct	Gdef Gdef[];
54*7c478bd9Sstevel@tonic-gate extern	int	Ndefs;
55*7c478bd9Sstevel@tonic-gate char	*strsave();
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate /*
58*7c478bd9Sstevel@tonic-gate  *	read_ttydefs	- read in the /etc/ttydefs and store in Gdef array
59*7c478bd9Sstevel@tonic-gate  *			- if id is not NULL, only get entry with that id
60*7c478bd9Sstevel@tonic-gate  *			- if check is TRUE, print out the entries
61*7c478bd9Sstevel@tonic-gate  */
62*7c478bd9Sstevel@tonic-gate void
63*7c478bd9Sstevel@tonic-gate read_ttydefs(id,check)
64*7c478bd9Sstevel@tonic-gate char 	*id;
65*7c478bd9Sstevel@tonic-gate int	check;
66*7c478bd9Sstevel@tonic-gate {
67*7c478bd9Sstevel@tonic-gate 	FILE 	 *fp;
68*7c478bd9Sstevel@tonic-gate 	static 	 struct Gdef def;
69*7c478bd9Sstevel@tonic-gate 	register struct Gdef *gptr;
70*7c478bd9Sstevel@tonic-gate 	static 	 char 	line[BUFSIZ];
71*7c478bd9Sstevel@tonic-gate 	static 	 char 	dbuf[BUFSIZ];
72*7c478bd9Sstevel@tonic-gate 	register char 	*ptr;
73*7c478bd9Sstevel@tonic-gate 	int	 len;
74*7c478bd9Sstevel@tonic-gate 	int 	 input,state,size,rawc,field;
75*7c478bd9Sstevel@tonic-gate 	char 	 oldc;
76*7c478bd9Sstevel@tonic-gate 	static 	 char 	d_id[MAXID+1],
77*7c478bd9Sstevel@tonic-gate 		      	d_nextid[MAXID+1],
78*7c478bd9Sstevel@tonic-gate 		      	d_autobaud[MAXID+1],
79*7c478bd9Sstevel@tonic-gate 		      	d_if[BUFSIZ],
80*7c478bd9Sstevel@tonic-gate 		      	d_ff[BUFSIZ];
81*7c478bd9Sstevel@tonic-gate 	static 	 char *states[] = {
82*7c478bd9Sstevel@tonic-gate 	   "","tty label","Initial flags","Final flags","Autobaud","Next label"
83*7c478bd9Sstevel@tonic-gate 	};
84*7c478bd9Sstevel@tonic-gate 	extern 	 char 	*getword();
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	if ((fp = fopen(TTYDEFS,"r")) == NULL) {
87*7c478bd9Sstevel@tonic-gate 		log("can't open \"%s\".\n", TTYDEFS);
88*7c478bd9Sstevel@tonic-gate 		return;
89*7c478bd9Sstevel@tonic-gate 	}
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	if (check) {
92*7c478bd9Sstevel@tonic-gate 		for (len = 0; len < (size_t)(BUFSIZ - 1); len++)
93*7c478bd9Sstevel@tonic-gate 			dbuf[len] = '-';
94*7c478bd9Sstevel@tonic-gate 		dbuf[len] = '\0';
95*7c478bd9Sstevel@tonic-gate 	}
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	/* Start searching for the line with the proper "id". */
98*7c478bd9Sstevel@tonic-gate 	input = ACTIVE;
99*7c478bd9Sstevel@tonic-gate 	do {
100*7c478bd9Sstevel@tonic-gate 		line[0] = '\0';
101*7c478bd9Sstevel@tonic-gate 		for (ptr= line,oldc = '\0'; ptr < &line[sizeof(line)-1] &&
102*7c478bd9Sstevel@tonic-gate 		 (rawc=getc(fp))!='\n' && rawc != EOF; ptr++,oldc=(char)rawc){
103*7c478bd9Sstevel@tonic-gate 			if ((rawc == '#') && (oldc != '\\'))
104*7c478bd9Sstevel@tonic-gate 				break;
105*7c478bd9Sstevel@tonic-gate 			*ptr = (char)rawc;
106*7c478bd9Sstevel@tonic-gate 		}
107*7c478bd9Sstevel@tonic-gate 		*ptr = '\0';
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 		/* skip rest of the line */
110*7c478bd9Sstevel@tonic-gate 		if (rawc != EOF && rawc != '\n') {
111*7c478bd9Sstevel@tonic-gate 			if (check && rawc != '#')
112*7c478bd9Sstevel@tonic-gate 				log("Entry too long.");
113*7c478bd9Sstevel@tonic-gate 			while ((rawc = getc(fp)) != EOF && rawc != '\n')
114*7c478bd9Sstevel@tonic-gate 				;
115*7c478bd9Sstevel@tonic-gate 		}
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 		if (rawc == EOF) {
118*7c478bd9Sstevel@tonic-gate 			if (ptr == line) break;
119*7c478bd9Sstevel@tonic-gate 			else input = FINISHED;
120*7c478bd9Sstevel@tonic-gate 		}
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 		/* if empty line, skip */
123*7c478bd9Sstevel@tonic-gate 		for (ptr=line; *ptr != '\0' && isspace(*ptr); ptr++)
124*7c478bd9Sstevel@tonic-gate 			;
125*7c478bd9Sstevel@tonic-gate 		if (*ptr == '\0') continue;
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 		/* Now we have the complete line */
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 		/* Initialize "def" and "gptr". */
130*7c478bd9Sstevel@tonic-gate 		gptr = &def;
131*7c478bd9Sstevel@tonic-gate 		zero((char *)gptr, sizeof(struct Gdef));
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 		ptr = line;
134*7c478bd9Sstevel@tonic-gate 		state = T_TTYLABEL;
135*7c478bd9Sstevel@tonic-gate 		(void)strncpy(d_id,getword(ptr,&size,0),MAXID);
136*7c478bd9Sstevel@tonic-gate 		gptr->g_id = d_id;
137*7c478bd9Sstevel@tonic-gate 		ptr += size;
138*7c478bd9Sstevel@tonic-gate 		if (*ptr != ':') {
139*7c478bd9Sstevel@tonic-gate 			field = state;
140*7c478bd9Sstevel@tonic-gate 			state = FAILURE;
141*7c478bd9Sstevel@tonic-gate 		} else {
142*7c478bd9Sstevel@tonic-gate 			ptr++;	/* Skip the ':' */
143*7c478bd9Sstevel@tonic-gate 			state++ ;
144*7c478bd9Sstevel@tonic-gate 		}
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 		/* If "id" != NULL, and it does not match, go to next entry */
147*7c478bd9Sstevel@tonic-gate 		if ((id != NULL) && (strcmp(id,gptr->g_id) != 0))
148*7c478bd9Sstevel@tonic-gate 			continue;
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 		if (check) {
151*7c478bd9Sstevel@tonic-gate 			len = strlen(line);
152*7c478bd9Sstevel@tonic-gate 			dbuf[len] = '\0';
153*7c478bd9Sstevel@tonic-gate 			log("\n%s", dbuf);
154*7c478bd9Sstevel@tonic-gate 			log("%s", line);
155*7c478bd9Sstevel@tonic-gate 			log("%s\n", dbuf);
156*7c478bd9Sstevel@tonic-gate 			dbuf[len] = '-';
157*7c478bd9Sstevel@tonic-gate 		}
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 		for (; state != FAILURE && state != SUCCESS;) {
161*7c478bd9Sstevel@tonic-gate 			switch(state) {
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 			case T_IFLAGS:
164*7c478bd9Sstevel@tonic-gate 				(void)strncpy(d_if,getword(ptr,&size,1),BUFSIZ);
165*7c478bd9Sstevel@tonic-gate 				gptr->g_iflags = d_if;
166*7c478bd9Sstevel@tonic-gate 				ptr += size;
167*7c478bd9Sstevel@tonic-gate 				if ((*ptr != ':') || (check_flags(d_if) != 0)) {
168*7c478bd9Sstevel@tonic-gate 					field = state;
169*7c478bd9Sstevel@tonic-gate 					state = FAILURE;
170*7c478bd9Sstevel@tonic-gate 				} else {
171*7c478bd9Sstevel@tonic-gate 					ptr++;
172*7c478bd9Sstevel@tonic-gate 					state++ ;
173*7c478bd9Sstevel@tonic-gate 				}
174*7c478bd9Sstevel@tonic-gate 				break;
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 			case T_FFLAGS:
177*7c478bd9Sstevel@tonic-gate 				(void)strncpy(d_ff,getword(ptr,&size,1),BUFSIZ);
178*7c478bd9Sstevel@tonic-gate 				gptr->g_fflags = d_ff;
179*7c478bd9Sstevel@tonic-gate 				ptr += size;
180*7c478bd9Sstevel@tonic-gate 				if ((*ptr != ':') || (check_flags(d_ff) != 0)) {
181*7c478bd9Sstevel@tonic-gate 					field = state;
182*7c478bd9Sstevel@tonic-gate 					state = FAILURE;
183*7c478bd9Sstevel@tonic-gate 				} else {
184*7c478bd9Sstevel@tonic-gate 					ptr++;
185*7c478bd9Sstevel@tonic-gate 					state++ ;
186*7c478bd9Sstevel@tonic-gate 				}
187*7c478bd9Sstevel@tonic-gate 				break;
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 			case T_AUTOBAUD:
190*7c478bd9Sstevel@tonic-gate 				(void)strncpy(d_autobaud,getword(ptr,&size,0),MAXID);
191*7c478bd9Sstevel@tonic-gate 				if (size > 1) {
192*7c478bd9Sstevel@tonic-gate 					ptr += size;
193*7c478bd9Sstevel@tonic-gate 					field = state;
194*7c478bd9Sstevel@tonic-gate 					state = FAILURE;
195*7c478bd9Sstevel@tonic-gate 					break;
196*7c478bd9Sstevel@tonic-gate 				}
197*7c478bd9Sstevel@tonic-gate 				if (size == 1) {
198*7c478bd9Sstevel@tonic-gate 					if (*d_autobaud == 'A')
199*7c478bd9Sstevel@tonic-gate 						gptr->g_autobaud |= A_FLAG;
200*7c478bd9Sstevel@tonic-gate 					else {
201*7c478bd9Sstevel@tonic-gate 						ptr += size;
202*7c478bd9Sstevel@tonic-gate 						field = state;
203*7c478bd9Sstevel@tonic-gate 						state = FAILURE;
204*7c478bd9Sstevel@tonic-gate 						break;
205*7c478bd9Sstevel@tonic-gate 					}
206*7c478bd9Sstevel@tonic-gate 				}
207*7c478bd9Sstevel@tonic-gate 				ptr += size;
208*7c478bd9Sstevel@tonic-gate 				if (*ptr != ':') {
209*7c478bd9Sstevel@tonic-gate 					field = state;
210*7c478bd9Sstevel@tonic-gate 					state = FAILURE;
211*7c478bd9Sstevel@tonic-gate 				} else {
212*7c478bd9Sstevel@tonic-gate 					ptr++;	/* Skip the ':' */
213*7c478bd9Sstevel@tonic-gate 					state++ ;
214*7c478bd9Sstevel@tonic-gate 				}
215*7c478bd9Sstevel@tonic-gate 				break;
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 			case T_NEXTLABEL:
218*7c478bd9Sstevel@tonic-gate 				(void)strncpy(d_nextid,getword(ptr,&size,0),MAXID);
219*7c478bd9Sstevel@tonic-gate 				gptr->g_nextid = d_nextid;
220*7c478bd9Sstevel@tonic-gate 				ptr += size;
221*7c478bd9Sstevel@tonic-gate 				if (*ptr != '\0') {
222*7c478bd9Sstevel@tonic-gate 					field = state;
223*7c478bd9Sstevel@tonic-gate 					state = FAILURE;
224*7c478bd9Sstevel@tonic-gate 				} else state = SUCCESS;
225*7c478bd9Sstevel@tonic-gate 				break;
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 			} /* end switch */
228*7c478bd9Sstevel@tonic-gate 		} /* end for loop */
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 		if (state == SUCCESS) {
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 			if (check) {
233*7c478bd9Sstevel@tonic-gate 				log("ttylabel:\t%s", gptr->g_id);
234*7c478bd9Sstevel@tonic-gate 				log("initial flags:\t%s", gptr->g_iflags);
235*7c478bd9Sstevel@tonic-gate 				log("final flags:\t%s", gptr->g_fflags);
236*7c478bd9Sstevel@tonic-gate 				if (gptr->g_autobaud & A_FLAG)
237*7c478bd9Sstevel@tonic-gate 					log("autobaud:\tyes");
238*7c478bd9Sstevel@tonic-gate 				else
239*7c478bd9Sstevel@tonic-gate 					log("autobaud:\tno");
240*7c478bd9Sstevel@tonic-gate 				log("nextlabel:\t%s", gptr->g_nextid);
241*7c478bd9Sstevel@tonic-gate 			}
242*7c478bd9Sstevel@tonic-gate 			if (Ndefs < MAXDEFS)
243*7c478bd9Sstevel@tonic-gate 				insert_def(gptr);
244*7c478bd9Sstevel@tonic-gate 			else {
245*7c478bd9Sstevel@tonic-gate 				log("can't add more entries to ttydefs table, "
246*7c478bd9Sstevel@tonic-gate 				   " Maximum entries = %d", MAXDEFS);
247*7c478bd9Sstevel@tonic-gate 				(void)fclose(fp);
248*7c478bd9Sstevel@tonic-gate 				return;
249*7c478bd9Sstevel@tonic-gate 			}
250*7c478bd9Sstevel@tonic-gate 			if (id != NULL) {
251*7c478bd9Sstevel@tonic-gate 				return;
252*7c478bd9Sstevel@tonic-gate 			}
253*7c478bd9Sstevel@tonic-gate 		}
254*7c478bd9Sstevel@tonic-gate 		else {
255*7c478bd9Sstevel@tonic-gate 			*++ptr = '\0';
256*7c478bd9Sstevel@tonic-gate 			log("Parsing failure in the \"%s\" field\n"
257*7c478bd9Sstevel@tonic-gate 			    "%s<--error detected here\n", states[field],line);
258*7c478bd9Sstevel@tonic-gate 		}
259*7c478bd9Sstevel@tonic-gate 	} while (input == ACTIVE);
260*7c478bd9Sstevel@tonic-gate 	(void)fclose(fp);
261*7c478bd9Sstevel@tonic-gate 	return;
262*7c478bd9Sstevel@tonic-gate }
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate /*
265*7c478bd9Sstevel@tonic-gate  *	zero	- zero out the buffer
266*7c478bd9Sstevel@tonic-gate  */
267*7c478bd9Sstevel@tonic-gate static void
268*7c478bd9Sstevel@tonic-gate zero(adr,size)
269*7c478bd9Sstevel@tonic-gate register char *adr;
270*7c478bd9Sstevel@tonic-gate register int size;
271*7c478bd9Sstevel@tonic-gate {
272*7c478bd9Sstevel@tonic-gate 	if (adr != NULL)
273*7c478bd9Sstevel@tonic-gate 		while (size--) *adr++ = '\0';
274*7c478bd9Sstevel@tonic-gate }
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate /*
277*7c478bd9Sstevel@tonic-gate  * find_def(ttylabel)
278*7c478bd9Sstevel@tonic-gate  *	- scan Gdef table for an entry with requested "ttylabel".
279*7c478bd9Sstevel@tonic-gate  *	- return a Gdef ptr if entry with "ttylabel" is found
280*7c478bd9Sstevel@tonic-gate  *	- return NULL if no entry with matching "ttylabel"
281*7c478bd9Sstevel@tonic-gate  */
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate struct Gdef *
284*7c478bd9Sstevel@tonic-gate find_def(ttylabel)
285*7c478bd9Sstevel@tonic-gate char	*ttylabel;
286*7c478bd9Sstevel@tonic-gate {
287*7c478bd9Sstevel@tonic-gate 	int	i;
288*7c478bd9Sstevel@tonic-gate 	struct	Gdef	*tp;
289*7c478bd9Sstevel@tonic-gate 	tp = &Gdef[0];
290*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < Ndefs; i++,tp++) {
291*7c478bd9Sstevel@tonic-gate 		if (strcmp(ttylabel, tp->g_id) == 0) {
292*7c478bd9Sstevel@tonic-gate 			return(tp);
293*7c478bd9Sstevel@tonic-gate 		}
294*7c478bd9Sstevel@tonic-gate 	}
295*7c478bd9Sstevel@tonic-gate 	return(NULL);
296*7c478bd9Sstevel@tonic-gate }
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate /*
299*7c478bd9Sstevel@tonic-gate  *	check_flags	- check to see if the flags contains options that are
300*7c478bd9Sstevel@tonic-gate  *			  recognizable by stty
301*7c478bd9Sstevel@tonic-gate  *			- return 0 if no error. Otherwise return -1
302*7c478bd9Sstevel@tonic-gate  */
303*7c478bd9Sstevel@tonic-gate int
304*7c478bd9Sstevel@tonic-gate check_flags(flags)
305*7c478bd9Sstevel@tonic-gate char	*flags;
306*7c478bd9Sstevel@tonic-gate {
307*7c478bd9Sstevel@tonic-gate 	struct 	 termio termio;
308*7c478bd9Sstevel@tonic-gate 	struct 	 termios termios;
309*7c478bd9Sstevel@tonic-gate 	struct 	 termiox termiox;
310*7c478bd9Sstevel@tonic-gate 	struct 	 winsize winsize;
311*7c478bd9Sstevel@tonic-gate 	int	 term;
312*7c478bd9Sstevel@tonic-gate 	int	 cnt = 1;
313*7c478bd9Sstevel@tonic-gate 	char	 *argvp[MAXARGS];	/* stty args */
314*7c478bd9Sstevel@tonic-gate 	static   char	 *binstty = "/usr/bin/stty";
315*7c478bd9Sstevel@tonic-gate 	static	 char	buf[BUFSIZ];
316*7c478bd9Sstevel@tonic-gate 	extern	 char	*sttyparse();
317*7c478bd9Sstevel@tonic-gate 	char	*s_arg;		/* this will point to invalid option */
318*7c478bd9Sstevel@tonic-gate 
319*7c478bd9Sstevel@tonic-gate 	/* put flags into buf, because strtok will break up buffer */
320*7c478bd9Sstevel@tonic-gate 	(void)strcpy(buf,flags);
321*7c478bd9Sstevel@tonic-gate 	argvp[0] = binstty;	/* just a place holder */
322*7c478bd9Sstevel@tonic-gate 	mkargv(buf,&argvp[1],&cnt,MAXARGS-1);
323*7c478bd9Sstevel@tonic-gate 	argvp[cnt] = (char *)0;
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate 	/*
326*7c478bd9Sstevel@tonic-gate 	 * because we don't know what type of terminal we have now,
327*7c478bd9Sstevel@tonic-gate 	 * just set term = everything, so all possible stty options
328*7c478bd9Sstevel@tonic-gate 	 * are accepted
329*7c478bd9Sstevel@tonic-gate 	 */
330*7c478bd9Sstevel@tonic-gate 	term = ASYNC|TERMIOS|FLOW;
331*7c478bd9Sstevel@tonic-gate 	if ((s_arg = sttyparse(cnt, argvp, term, &termio, &termios,
332*7c478bd9Sstevel@tonic-gate 			&termiox, &winsize)) != NULL) {
333*7c478bd9Sstevel@tonic-gate 		log("invalid mode: %s", s_arg);
334*7c478bd9Sstevel@tonic-gate 		return(-1);
335*7c478bd9Sstevel@tonic-gate 	}
336*7c478bd9Sstevel@tonic-gate 	return(0);
337*7c478bd9Sstevel@tonic-gate }
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate /*
340*7c478bd9Sstevel@tonic-gate  *	insert_def	- insert one entry into Gdef table
341*7c478bd9Sstevel@tonic-gate  */
342*7c478bd9Sstevel@tonic-gate static void
343*7c478bd9Sstevel@tonic-gate insert_def(gptr)
344*7c478bd9Sstevel@tonic-gate struct	Gdef	*gptr;
345*7c478bd9Sstevel@tonic-gate {
346*7c478bd9Sstevel@tonic-gate 	struct	Gdef	*tp;
347*7c478bd9Sstevel@tonic-gate 	extern	struct	Gdef	*find_def();
348*7c478bd9Sstevel@tonic-gate 
349*7c478bd9Sstevel@tonic-gate 	if (find_def(gptr->g_id) != NULL) {
350*7c478bd9Sstevel@tonic-gate 		log("Warning -- duplicate entry <%s>, ignored", gptr->g_id);
351*7c478bd9Sstevel@tonic-gate 		return;
352*7c478bd9Sstevel@tonic-gate 	}
353*7c478bd9Sstevel@tonic-gate 	tp = &Gdef[Ndefs];
354*7c478bd9Sstevel@tonic-gate 	tp->g_id = strsave(gptr->g_id);
355*7c478bd9Sstevel@tonic-gate 	tp->g_iflags = strsave(gptr->g_iflags);
356*7c478bd9Sstevel@tonic-gate 	tp->g_fflags = strsave(gptr->g_fflags);
357*7c478bd9Sstevel@tonic-gate 	tp->g_autobaud = gptr->g_autobaud;
358*7c478bd9Sstevel@tonic-gate 	tp->g_nextid = strsave(gptr->g_nextid);
359*7c478bd9Sstevel@tonic-gate 	Ndefs++;
360*7c478bd9Sstevel@tonic-gate 	return;
361*7c478bd9Sstevel@tonic-gate }
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate /*
364*7c478bd9Sstevel@tonic-gate  *	mkargv	- parse the string into args, starting from args[cnt]
365*7c478bd9Sstevel@tonic-gate  */
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate void
368*7c478bd9Sstevel@tonic-gate mkargv(string,args,cnt,maxargs)
369*7c478bd9Sstevel@tonic-gate char 	*string, **args;
370*7c478bd9Sstevel@tonic-gate int 	*cnt, maxargs;
371*7c478bd9Sstevel@tonic-gate {
372*7c478bd9Sstevel@tonic-gate 	register char *ptrin,*ptrout;
373*7c478bd9Sstevel@tonic-gate 	register int i;
374*7c478bd9Sstevel@tonic-gate 	int qsize;
375*7c478bd9Sstevel@tonic-gate 	extern	char	quoted();
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate 	for (i=0; i < maxargs; i++) args[i] = (char *)NULL;
378*7c478bd9Sstevel@tonic-gate 	for (ptrin = ptrout = string,i=0; *ptrin != '\0' && i < maxargs; i++) {
379*7c478bd9Sstevel@tonic-gate 		/* Skip excess white spaces between arguments. */
380*7c478bd9Sstevel@tonic-gate 		while(*ptrin == ' ' || *ptrin == '\t') {
381*7c478bd9Sstevel@tonic-gate 			ptrin++;
382*7c478bd9Sstevel@tonic-gate 			ptrout++;
383*7c478bd9Sstevel@tonic-gate 		}
384*7c478bd9Sstevel@tonic-gate 		/* Save the address of argument if there is something there. */
385*7c478bd9Sstevel@tonic-gate 		if (*ptrin == '\0') break;
386*7c478bd9Sstevel@tonic-gate 		else args[i] = ptrout;
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate /* Span the argument itself.  The '\' character causes quoting */
389*7c478bd9Sstevel@tonic-gate /* of the next character to take place (except for '\0'). */
390*7c478bd9Sstevel@tonic-gate 		while (*ptrin != '\0') {
391*7c478bd9Sstevel@tonic-gate 			if (*ptrin == '\\') {
392*7c478bd9Sstevel@tonic-gate 				*ptrout++ = quoted(ptrin,&qsize);
393*7c478bd9Sstevel@tonic-gate 				ptrin += qsize;
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate /* Is this the end of the argument?  If so quit loop. */
396*7c478bd9Sstevel@tonic-gate 			} else if (*ptrin == ' ' || *ptrin == '\t') {
397*7c478bd9Sstevel@tonic-gate 				ptrin++;
398*7c478bd9Sstevel@tonic-gate 				break;
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate /* If this is a normal letter of the argument, save it, advancing */
401*7c478bd9Sstevel@tonic-gate /* the pointers at the same time. */
402*7c478bd9Sstevel@tonic-gate 			} else *ptrout++ = *ptrin++;
403*7c478bd9Sstevel@tonic-gate 		}
404*7c478bd9Sstevel@tonic-gate 		/* Null terminate the string. */
405*7c478bd9Sstevel@tonic-gate 		*ptrout++ = '\0';
406*7c478bd9Sstevel@tonic-gate 	}
407*7c478bd9Sstevel@tonic-gate 	(*cnt) += i;
408*7c478bd9Sstevel@tonic-gate }
409*7c478bd9Sstevel@tonic-gate 
410*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
411*7c478bd9Sstevel@tonic-gate /*
412*7c478bd9Sstevel@tonic-gate  *	dump_ttydefs	- dump Gdef table to log file
413*7c478bd9Sstevel@tonic-gate  */
414*7c478bd9Sstevel@tonic-gate void
415*7c478bd9Sstevel@tonic-gate dump_ttydefs()
416*7c478bd9Sstevel@tonic-gate {
417*7c478bd9Sstevel@tonic-gate 	int	i;
418*7c478bd9Sstevel@tonic-gate 	struct	Gdef	*gptr;
419*7c478bd9Sstevel@tonic-gate 	gptr = &Gdef[0];
420*7c478bd9Sstevel@tonic-gate 	log("********** dumping ttydefs table **********");
421*7c478bd9Sstevel@tonic-gate 	log("Ndefs = %d", Ndefs);
422*7c478bd9Sstevel@tonic-gate 	log(" ");
423*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < Ndefs; i++,gptr++) {
424*7c478bd9Sstevel@tonic-gate 		log("----------------------------------------");
425*7c478bd9Sstevel@tonic-gate 		log("ttylabel:\t%s", gptr->g_id);
426*7c478bd9Sstevel@tonic-gate 		log("initial flags:\t%s", gptr->g_iflags);
427*7c478bd9Sstevel@tonic-gate 		log("final flags:\t%s", gptr->g_fflags);
428*7c478bd9Sstevel@tonic-gate 		if (gptr->g_autobaud & A_FLAG)
429*7c478bd9Sstevel@tonic-gate 			log("autobaud:\tyes");
430*7c478bd9Sstevel@tonic-gate 		else
431*7c478bd9Sstevel@tonic-gate 			log("Autobaud:\tno");
432*7c478bd9Sstevel@tonic-gate 		log("nextlabel:\t%s", gptr->g_nextid);
433*7c478bd9Sstevel@tonic-gate 		log(" ");
434*7c478bd9Sstevel@tonic-gate 	}
435*7c478bd9Sstevel@tonic-gate 	log("********** end dumping ttydefs table **********");
436*7c478bd9Sstevel@tonic-gate 	return;
437*7c478bd9Sstevel@tonic-gate }
438*7c478bd9Sstevel@tonic-gate #endif
439*7c478bd9Sstevel@tonic-gate 
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate /*
442*7c478bd9Sstevel@tonic-gate  * this is copies from uucp/strsave.c
443*7c478bd9Sstevel@tonic-gate  * and is modified that if malloc fails, it will exit
444*7c478bd9Sstevel@tonic-gate  */
445*7c478bd9Sstevel@tonic-gate char *
446*7c478bd9Sstevel@tonic-gate strsave(str)
447*7c478bd9Sstevel@tonic-gate register char *str;
448*7c478bd9Sstevel@tonic-gate {
449*7c478bd9Sstevel@tonic-gate 	register char *rval;
450*7c478bd9Sstevel@tonic-gate 
451*7c478bd9Sstevel@tonic-gate 	if (str == NULL) {
452*7c478bd9Sstevel@tonic-gate 		if ((rval = (char *)malloc(1)) == NULL) {
453*7c478bd9Sstevel@tonic-gate 			log("strsave: malloc failed");
454*7c478bd9Sstevel@tonic-gate 			exit(1);
455*7c478bd9Sstevel@tonic-gate 		}
456*7c478bd9Sstevel@tonic-gate 		*rval = '\0';
457*7c478bd9Sstevel@tonic-gate 	}
458*7c478bd9Sstevel@tonic-gate 	else {
459*7c478bd9Sstevel@tonic-gate 		if ((rval = (char *)malloc(strlen(str) + 1)) == NULL) {
460*7c478bd9Sstevel@tonic-gate 			log("strsave: malloc failed");
461*7c478bd9Sstevel@tonic-gate 			exit(1);
462*7c478bd9Sstevel@tonic-gate 		}
463*7c478bd9Sstevel@tonic-gate 		(void)strcpy(rval, str);
464*7c478bd9Sstevel@tonic-gate 	}
465*7c478bd9Sstevel@tonic-gate 	return(rval);
466*7c478bd9Sstevel@tonic-gate }
467