xref: /illumos-gate/usr/src/cmd/ttymon/tmttydefs.c (revision 3bb2c156)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27*3bb2c156SToomas Soome /*	  All Rights Reserved	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include	<unistd.h>
307c478bd9Sstevel@tonic-gate #include	<stdlib.h>
317c478bd9Sstevel@tonic-gate #include	<stdio.h>
327c478bd9Sstevel@tonic-gate #include	<ctype.h>
337c478bd9Sstevel@tonic-gate #include	<string.h>
347c478bd9Sstevel@tonic-gate #include	<sys/types.h>
357c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
367c478bd9Sstevel@tonic-gate #include	<termio.h>
37*3bb2c156SToomas Soome #include	<sys/stermio.h>
38*3bb2c156SToomas Soome #include	<sys/termiox.h>
397c478bd9Sstevel@tonic-gate #include	"ttymon.h"
407c478bd9Sstevel@tonic-gate #include	"tmstruct.h"
417c478bd9Sstevel@tonic-gate #include	"tmextern.h"
427c478bd9Sstevel@tonic-gate #include	"stty.h"
437c478bd9Sstevel@tonic-gate 
44*3bb2c156SToomas Soome static	void	insert_def(struct Gdef *);
45*3bb2c156SToomas Soome static	void	zero(char *, int);
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  *	read_ttydefs	- read in the /etc/ttydefs and store in Gdef array
497c478bd9Sstevel@tonic-gate  *			- if id is not NULL, only get entry with that id
507c478bd9Sstevel@tonic-gate  *			- if check is TRUE, print out the entries
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate void
read_ttydefs(const char * id,int check)53*3bb2c156SToomas Soome read_ttydefs(const char *id, int check)
547c478bd9Sstevel@tonic-gate {
55*3bb2c156SToomas Soome 	FILE	*fp;
56*3bb2c156SToomas Soome 	static	struct Gdef def;
57*3bb2c156SToomas Soome 	struct	Gdef *gptr;
58*3bb2c156SToomas Soome 	static	char	line[BUFSIZ];
59*3bb2c156SToomas Soome 	static	char	dbuf[BUFSIZ];
60*3bb2c156SToomas Soome 	char	*ptr;
61*3bb2c156SToomas Soome 	int	len;
62*3bb2c156SToomas Soome 	int	input, state, size, rawc, field;
63*3bb2c156SToomas Soome 	char	oldc;
64*3bb2c156SToomas Soome 	static	char	d_id[MAXID+1],
65*3bb2c156SToomas Soome 	    d_nextid[MAXID+1],
66*3bb2c156SToomas Soome 	    d_autobaud[MAXID+1],
67*3bb2c156SToomas Soome 	    d_if[BUFSIZ],
68*3bb2c156SToomas Soome 	    d_ff[BUFSIZ];
69*3bb2c156SToomas Soome 	static	char *states[] = {
70*3bb2c156SToomas Soome 	    "", "tty label", "Initial flags", "Final flags", "Autobaud",
71*3bb2c156SToomas Soome 	    "Next label"
727c478bd9Sstevel@tonic-gate 	};
737c478bd9Sstevel@tonic-gate 
74*3bb2c156SToomas Soome 	if ((fp = fopen(TTYDEFS, "r")) == NULL) {
757c478bd9Sstevel@tonic-gate 		log("can't open \"%s\".\n", TTYDEFS);
767c478bd9Sstevel@tonic-gate 		return;
777c478bd9Sstevel@tonic-gate 	}
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	if (check) {
807c478bd9Sstevel@tonic-gate 		for (len = 0; len < (size_t)(BUFSIZ - 1); len++)
817c478bd9Sstevel@tonic-gate 			dbuf[len] = '-';
827c478bd9Sstevel@tonic-gate 		dbuf[len] = '\0';
837c478bd9Sstevel@tonic-gate 	}
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	/* Start searching for the line with the proper "id". */
867c478bd9Sstevel@tonic-gate 	input = ACTIVE;
87*3bb2c156SToomas Soome 	field = FAILURE;
887c478bd9Sstevel@tonic-gate 	do {
897c478bd9Sstevel@tonic-gate 		line[0] = '\0';
90*3bb2c156SToomas Soome 		for (ptr = line, oldc = '\0'; ptr < &line[sizeof (line) - 1] &&
91*3bb2c156SToomas Soome 		    (rawc = getc(fp)) != '\n' && rawc != EOF;
92*3bb2c156SToomas Soome 		    ptr++, oldc = (char)rawc) {
937c478bd9Sstevel@tonic-gate 			if ((rawc == '#') && (oldc != '\\'))
947c478bd9Sstevel@tonic-gate 				break;
957c478bd9Sstevel@tonic-gate 			*ptr = (char)rawc;
967c478bd9Sstevel@tonic-gate 		}
977c478bd9Sstevel@tonic-gate 		*ptr = '\0';
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 		/* skip rest of the line */
1007c478bd9Sstevel@tonic-gate 		if (rawc != EOF && rawc != '\n') {
101*3bb2c156SToomas Soome 			if (check && rawc != '#')
1027c478bd9Sstevel@tonic-gate 				log("Entry too long.");
103*3bb2c156SToomas Soome 			while ((rawc = getc(fp)) != EOF && rawc != '\n')
1047c478bd9Sstevel@tonic-gate 				;
1057c478bd9Sstevel@tonic-gate 		}
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 		if (rawc == EOF) {
108*3bb2c156SToomas Soome 			if (ptr == line)
109*3bb2c156SToomas Soome 				break;
110*3bb2c156SToomas Soome 			else
111*3bb2c156SToomas Soome 				input = FINISHED;
1127c478bd9Sstevel@tonic-gate 		}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 		/* if empty line, skip */
115*3bb2c156SToomas Soome 		for (ptr = line; *ptr != '\0' && isspace(*ptr); ptr++)
1167c478bd9Sstevel@tonic-gate 			;
117*3bb2c156SToomas Soome 		if (*ptr == '\0')
118*3bb2c156SToomas Soome 			continue;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 		/* Now we have the complete line */
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 		/* Initialize "def" and "gptr". */
1237c478bd9Sstevel@tonic-gate 		gptr = &def;
124*3bb2c156SToomas Soome 		zero((char *)gptr, sizeof (struct Gdef));
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 		ptr = line;
1277c478bd9Sstevel@tonic-gate 		state = T_TTYLABEL;
128*3bb2c156SToomas Soome 		(void) strncpy(d_id, getword(ptr, &size, 0), MAXID);
1297c478bd9Sstevel@tonic-gate 		gptr->g_id = d_id;
1307c478bd9Sstevel@tonic-gate 		ptr += size;
1317c478bd9Sstevel@tonic-gate 		if (*ptr != ':') {
1327c478bd9Sstevel@tonic-gate 			field = state;
1337c478bd9Sstevel@tonic-gate 			state = FAILURE;
1347c478bd9Sstevel@tonic-gate 		} else {
1357c478bd9Sstevel@tonic-gate 			ptr++;	/* Skip the ':' */
136*3bb2c156SToomas Soome 			state++;
1377c478bd9Sstevel@tonic-gate 		}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		/* If "id" != NULL, and it does not match, go to next entry */
140*3bb2c156SToomas Soome 		if ((id != NULL) && (strcmp(id, gptr->g_id) != 0))
1417c478bd9Sstevel@tonic-gate 			continue;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 		if (check) {
1447c478bd9Sstevel@tonic-gate 			len = strlen(line);
1457c478bd9Sstevel@tonic-gate 			dbuf[len] = '\0';
1467c478bd9Sstevel@tonic-gate 			log("\n%s", dbuf);
1477c478bd9Sstevel@tonic-gate 			log("%s", line);
1487c478bd9Sstevel@tonic-gate 			log("%s\n", dbuf);
1497c478bd9Sstevel@tonic-gate 			dbuf[len] = '-';
1507c478bd9Sstevel@tonic-gate 		}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 
153*3bb2c156SToomas Soome 		for (; state != FAILURE && state != SUCCESS; ) {
154*3bb2c156SToomas Soome 			switch (state) {
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 			case T_IFLAGS:
157*3bb2c156SToomas Soome 				(void) strncpy(d_if, getword(ptr, &size, 1),
158*3bb2c156SToomas Soome 				    BUFSIZ);
1597c478bd9Sstevel@tonic-gate 				gptr->g_iflags = d_if;
1607c478bd9Sstevel@tonic-gate 				ptr += size;
1617c478bd9Sstevel@tonic-gate 				if ((*ptr != ':') || (check_flags(d_if) != 0)) {
1627c478bd9Sstevel@tonic-gate 					field = state;
1637c478bd9Sstevel@tonic-gate 					state = FAILURE;
1647c478bd9Sstevel@tonic-gate 				} else {
165*3bb2c156SToomas Soome 					ptr++;
166*3bb2c156SToomas Soome 					state++;
1677c478bd9Sstevel@tonic-gate 				}
1687c478bd9Sstevel@tonic-gate 				break;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 			case T_FFLAGS:
171*3bb2c156SToomas Soome 				(void) strncpy(d_ff, getword(ptr, &size, 1),
172*3bb2c156SToomas Soome 				    BUFSIZ);
1737c478bd9Sstevel@tonic-gate 				gptr->g_fflags = d_ff;
1747c478bd9Sstevel@tonic-gate 				ptr += size;
1757c478bd9Sstevel@tonic-gate 				if ((*ptr != ':') || (check_flags(d_ff) != 0)) {
1767c478bd9Sstevel@tonic-gate 					field = state;
1777c478bd9Sstevel@tonic-gate 					state = FAILURE;
1787c478bd9Sstevel@tonic-gate 				} else {
179*3bb2c156SToomas Soome 					ptr++;
180*3bb2c156SToomas Soome 					state++;
1817c478bd9Sstevel@tonic-gate 				}
1827c478bd9Sstevel@tonic-gate 				break;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 			case T_AUTOBAUD:
185*3bb2c156SToomas Soome 				(void) strncpy(d_autobaud,
186*3bb2c156SToomas Soome 				    getword(ptr, &size, 0), MAXID);
1877c478bd9Sstevel@tonic-gate 				if (size > 1) {
1887c478bd9Sstevel@tonic-gate 					ptr += size;
1897c478bd9Sstevel@tonic-gate 					field = state;
1907c478bd9Sstevel@tonic-gate 					state = FAILURE;
1917c478bd9Sstevel@tonic-gate 					break;
1927c478bd9Sstevel@tonic-gate 				}
1937c478bd9Sstevel@tonic-gate 				if (size == 1) {
194*3bb2c156SToomas Soome 					if (*d_autobaud == 'A') {
1957c478bd9Sstevel@tonic-gate 						gptr->g_autobaud |= A_FLAG;
196*3bb2c156SToomas Soome 					} else {
1977c478bd9Sstevel@tonic-gate 						ptr += size;
1987c478bd9Sstevel@tonic-gate 						field = state;
1997c478bd9Sstevel@tonic-gate 						state = FAILURE;
2007c478bd9Sstevel@tonic-gate 						break;
2017c478bd9Sstevel@tonic-gate 					}
2027c478bd9Sstevel@tonic-gate 				}
2037c478bd9Sstevel@tonic-gate 				ptr += size;
2047c478bd9Sstevel@tonic-gate 				if (*ptr != ':') {
2057c478bd9Sstevel@tonic-gate 					field = state;
2067c478bd9Sstevel@tonic-gate 					state = FAILURE;
2077c478bd9Sstevel@tonic-gate 				} else {
2087c478bd9Sstevel@tonic-gate 					ptr++;	/* Skip the ':' */
209*3bb2c156SToomas Soome 					state++;
2107c478bd9Sstevel@tonic-gate 				}
2117c478bd9Sstevel@tonic-gate 				break;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 			case T_NEXTLABEL:
214*3bb2c156SToomas Soome 				(void) strncpy(d_nextid,
215*3bb2c156SToomas Soome 				    getword(ptr, &size, 0), MAXID);
2167c478bd9Sstevel@tonic-gate 				gptr->g_nextid = d_nextid;
2177c478bd9Sstevel@tonic-gate 				ptr += size;
2187c478bd9Sstevel@tonic-gate 				if (*ptr != '\0') {
2197c478bd9Sstevel@tonic-gate 					field = state;
2207c478bd9Sstevel@tonic-gate 					state = FAILURE;
221*3bb2c156SToomas Soome 				} else {
222*3bb2c156SToomas Soome 					state = SUCCESS;
223*3bb2c156SToomas Soome 				}
2247c478bd9Sstevel@tonic-gate 				break;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 			} /* end switch */
2277c478bd9Sstevel@tonic-gate 		} /* end for loop */
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 		if (state == SUCCESS) {
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 			if (check) {
2327c478bd9Sstevel@tonic-gate 				log("ttylabel:\t%s", gptr->g_id);
2337c478bd9Sstevel@tonic-gate 				log("initial flags:\t%s", gptr->g_iflags);
2347c478bd9Sstevel@tonic-gate 				log("final flags:\t%s", gptr->g_fflags);
235*3bb2c156SToomas Soome 				if (gptr->g_autobaud & A_FLAG)
2367c478bd9Sstevel@tonic-gate 					log("autobaud:\tyes");
2377c478bd9Sstevel@tonic-gate 				else
2387c478bd9Sstevel@tonic-gate 					log("autobaud:\tno");
2397c478bd9Sstevel@tonic-gate 				log("nextlabel:\t%s", gptr->g_nextid);
2407c478bd9Sstevel@tonic-gate 			}
241*3bb2c156SToomas Soome 			if (Ndefs < MAXDEFS) {
2427c478bd9Sstevel@tonic-gate 				insert_def(gptr);
243*3bb2c156SToomas Soome 			} else {
2447c478bd9Sstevel@tonic-gate 				log("can't add more entries to ttydefs table, "
245*3bb2c156SToomas Soome 				    " Maximum entries = %d", MAXDEFS);
246*3bb2c156SToomas Soome 				(void) fclose(fp);
2477c478bd9Sstevel@tonic-gate 				return;
2487c478bd9Sstevel@tonic-gate 			}
2497c478bd9Sstevel@tonic-gate 			if (id != NULL) {
2507c478bd9Sstevel@tonic-gate 				return;
2517c478bd9Sstevel@tonic-gate 			}
252*3bb2c156SToomas Soome 		} else {
2537c478bd9Sstevel@tonic-gate 			*++ptr = '\0';
2547c478bd9Sstevel@tonic-gate 			log("Parsing failure in the \"%s\" field\n"
255*3bb2c156SToomas Soome 			    "%s<--error detected here\n", states[field], line);
2567c478bd9Sstevel@tonic-gate 		}
2577c478bd9Sstevel@tonic-gate 	} while (input == ACTIVE);
258*3bb2c156SToomas Soome 	(void) fclose(fp);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate /*
2627c478bd9Sstevel@tonic-gate  *	zero	- zero out the buffer
2637c478bd9Sstevel@tonic-gate  */
2647c478bd9Sstevel@tonic-gate static void
zero(char * adr,int size)265*3bb2c156SToomas Soome zero(char *adr, int size)
2667c478bd9Sstevel@tonic-gate {
267*3bb2c156SToomas Soome 	if (adr != NULL) {
268*3bb2c156SToomas Soome 		while (size--)
269*3bb2c156SToomas Soome 			*adr++ = '\0';
270*3bb2c156SToomas Soome 	}
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate /*
2747c478bd9Sstevel@tonic-gate  * find_def(ttylabel)
2757c478bd9Sstevel@tonic-gate  *	- scan Gdef table for an entry with requested "ttylabel".
276*3bb2c156SToomas Soome  *	- return a Gdef ptr if entry with "ttylabel" is found
2777c478bd9Sstevel@tonic-gate  *	- return NULL if no entry with matching "ttylabel"
2787c478bd9Sstevel@tonic-gate  */
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate struct Gdef *
find_def(char * ttylabel)281*3bb2c156SToomas Soome find_def(char *ttylabel)
2827c478bd9Sstevel@tonic-gate {
2837c478bd9Sstevel@tonic-gate 	int	i;
2847c478bd9Sstevel@tonic-gate 	struct	Gdef	*tp;
285*3bb2c156SToomas Soome 
2867c478bd9Sstevel@tonic-gate 	tp = &Gdef[0];
287*3bb2c156SToomas Soome 	for (i = 0; i < Ndefs; i++, tp++) {
2887c478bd9Sstevel@tonic-gate 		if (strcmp(ttylabel, tp->g_id) == 0) {
289*3bb2c156SToomas Soome 			return (tp);
2907c478bd9Sstevel@tonic-gate 		}
2917c478bd9Sstevel@tonic-gate 	}
292*3bb2c156SToomas Soome 	return (NULL);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate /*
2967c478bd9Sstevel@tonic-gate  *	check_flags	- check to see if the flags contains options that are
2977c478bd9Sstevel@tonic-gate  *			  recognizable by stty
2987c478bd9Sstevel@tonic-gate  *			- return 0 if no error. Otherwise return -1
2997c478bd9Sstevel@tonic-gate  */
3007c478bd9Sstevel@tonic-gate int
check_flags(char * flags)301*3bb2c156SToomas Soome check_flags(char *flags)
3027c478bd9Sstevel@tonic-gate {
303*3bb2c156SToomas Soome 	struct	 termio termio;
304*3bb2c156SToomas Soome 	struct	 termios termios;
305*3bb2c156SToomas Soome 	struct	 termiox termiox;
306*3bb2c156SToomas Soome 	struct	 winsize winsize;
3077c478bd9Sstevel@tonic-gate 	int	 term;
3087c478bd9Sstevel@tonic-gate 	int	 cnt = 1;
3097c478bd9Sstevel@tonic-gate 	char	 *argvp[MAXARGS];	/* stty args */
3107c478bd9Sstevel@tonic-gate 	static   char	 *binstty = "/usr/bin/stty";
3117c478bd9Sstevel@tonic-gate 	static	 char	buf[BUFSIZ];
3127c478bd9Sstevel@tonic-gate 	char	*s_arg;		/* this will point to invalid option */
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	/* put flags into buf, because strtok will break up buffer */
315*3bb2c156SToomas Soome 	(void) strcpy(buf, flags);
3167c478bd9Sstevel@tonic-gate 	argvp[0] = binstty;	/* just a place holder */
317*3bb2c156SToomas Soome 	mkargv(buf, &argvp[1], &cnt, MAXARGS - 1);
318*3bb2c156SToomas Soome 	argvp[cnt] = NULL;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	/*
3217c478bd9Sstevel@tonic-gate 	 * because we don't know what type of terminal we have now,
3227c478bd9Sstevel@tonic-gate 	 * just set term = everything, so all possible stty options
3237c478bd9Sstevel@tonic-gate 	 * are accepted
3247c478bd9Sstevel@tonic-gate 	 */
3257c478bd9Sstevel@tonic-gate 	term = ASYNC|TERMIOS|FLOW;
326*3bb2c156SToomas Soome 	if ((s_arg = sttyparse(cnt, argvp, term, &termio, &termios,
327*3bb2c156SToomas Soome 	    &termiox, &winsize)) != NULL) {
3287c478bd9Sstevel@tonic-gate 		log("invalid mode: %s", s_arg);
329*3bb2c156SToomas Soome 		return (-1);
3307c478bd9Sstevel@tonic-gate 	}
331*3bb2c156SToomas Soome 	return (0);
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate /*
3357c478bd9Sstevel@tonic-gate  *	insert_def	- insert one entry into Gdef table
3367c478bd9Sstevel@tonic-gate  */
3377c478bd9Sstevel@tonic-gate static void
insert_def(struct Gdef * gptr)338*3bb2c156SToomas Soome insert_def(struct Gdef *gptr)
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	struct	Gdef	*tp;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	if (find_def(gptr->g_id) != NULL) {
3437c478bd9Sstevel@tonic-gate 		log("Warning -- duplicate entry <%s>, ignored", gptr->g_id);
3447c478bd9Sstevel@tonic-gate 		return;
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate 	tp = &Gdef[Ndefs];
3477c478bd9Sstevel@tonic-gate 	tp->g_id = strsave(gptr->g_id);
3487c478bd9Sstevel@tonic-gate 	tp->g_iflags = strsave(gptr->g_iflags);
3497c478bd9Sstevel@tonic-gate 	tp->g_fflags = strsave(gptr->g_fflags);
3507c478bd9Sstevel@tonic-gate 	tp->g_autobaud = gptr->g_autobaud;
3517c478bd9Sstevel@tonic-gate 	tp->g_nextid = strsave(gptr->g_nextid);
3527c478bd9Sstevel@tonic-gate 	Ndefs++;
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate /*
3567c478bd9Sstevel@tonic-gate  *	mkargv	- parse the string into args, starting from args[cnt]
3577c478bd9Sstevel@tonic-gate  */
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate void
mkargv(char * string,char ** args,int * cnt,int maxargs)360*3bb2c156SToomas Soome mkargv(char *string, char **args, int *cnt, int maxargs)
3617c478bd9Sstevel@tonic-gate {
362*3bb2c156SToomas Soome 	char *ptrin, *ptrout;
363*3bb2c156SToomas Soome 	int i;
3647c478bd9Sstevel@tonic-gate 	int qsize;
3657c478bd9Sstevel@tonic-gate 
366*3bb2c156SToomas Soome 	for (i = 0; i < maxargs; i++)
367*3bb2c156SToomas Soome 		args[i] = NULL;
368*3bb2c156SToomas Soome 
369*3bb2c156SToomas Soome 	ptrin = ptrout = string;
370*3bb2c156SToomas Soome 	for (i = 0; *ptrin != '\0' && i < maxargs; i++) {
3717c478bd9Sstevel@tonic-gate 		/* Skip excess white spaces between arguments. */
372*3bb2c156SToomas Soome 		while (*ptrin == ' ' || *ptrin == '\t') {
3737c478bd9Sstevel@tonic-gate 			ptrin++;
3747c478bd9Sstevel@tonic-gate 			ptrout++;
3757c478bd9Sstevel@tonic-gate 		}
3767c478bd9Sstevel@tonic-gate 		/* Save the address of argument if there is something there. */
377*3bb2c156SToomas Soome 		if (*ptrin == '\0')
378*3bb2c156SToomas Soome 			break;
379*3bb2c156SToomas Soome 		else
380*3bb2c156SToomas Soome 			args[i] = ptrout;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate /* Span the argument itself.  The '\' character causes quoting */
3837c478bd9Sstevel@tonic-gate /* of the next character to take place (except for '\0'). */
3847c478bd9Sstevel@tonic-gate 		while (*ptrin != '\0') {
3857c478bd9Sstevel@tonic-gate 			if (*ptrin == '\\') {
386*3bb2c156SToomas Soome 				*ptrout++ = quoted(ptrin, &qsize);
3877c478bd9Sstevel@tonic-gate 				ptrin += qsize;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate /* Is this the end of the argument?  If so quit loop. */
3907c478bd9Sstevel@tonic-gate 			} else if (*ptrin == ' ' || *ptrin == '\t') {
3917c478bd9Sstevel@tonic-gate 				ptrin++;
3927c478bd9Sstevel@tonic-gate 				break;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate /* If this is a normal letter of the argument, save it, advancing */
3957c478bd9Sstevel@tonic-gate /* the pointers at the same time. */
3967c478bd9Sstevel@tonic-gate 			} else *ptrout++ = *ptrin++;
3977c478bd9Sstevel@tonic-gate 		}
3987c478bd9Sstevel@tonic-gate 		/* Null terminate the string. */
3997c478bd9Sstevel@tonic-gate 		*ptrout++ = '\0';
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate 	(*cnt) += i;
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate #ifdef	DEBUG
4057c478bd9Sstevel@tonic-gate /*
4067c478bd9Sstevel@tonic-gate  *	dump_ttydefs	- dump Gdef table to log file
4077c478bd9Sstevel@tonic-gate  */
4087c478bd9Sstevel@tonic-gate void
dump_ttydefs(void)409*3bb2c156SToomas Soome dump_ttydefs(void)
4107c478bd9Sstevel@tonic-gate {
4117c478bd9Sstevel@tonic-gate 	int	i;
4127c478bd9Sstevel@tonic-gate 	struct	Gdef	*gptr;
413*3bb2c156SToomas Soome 
4147c478bd9Sstevel@tonic-gate 	gptr = &Gdef[0];
4157c478bd9Sstevel@tonic-gate 	log("********** dumping ttydefs table **********");
4167c478bd9Sstevel@tonic-gate 	log("Ndefs = %d", Ndefs);
4177c478bd9Sstevel@tonic-gate 	log(" ");
418*3bb2c156SToomas Soome 	for (i = 0; i < Ndefs; i++, gptr++) {
4197c478bd9Sstevel@tonic-gate 		log("----------------------------------------");
4207c478bd9Sstevel@tonic-gate 		log("ttylabel:\t%s", gptr->g_id);
4217c478bd9Sstevel@tonic-gate 		log("initial flags:\t%s", gptr->g_iflags);
4227c478bd9Sstevel@tonic-gate 		log("final flags:\t%s", gptr->g_fflags);
423*3bb2c156SToomas Soome 		if (gptr->g_autobaud & A_FLAG)
4247c478bd9Sstevel@tonic-gate 			log("autobaud:\tyes");
4257c478bd9Sstevel@tonic-gate 		else
4267c478bd9Sstevel@tonic-gate 			log("Autobaud:\tno");
4277c478bd9Sstevel@tonic-gate 		log("nextlabel:\t%s", gptr->g_nextid);
4287c478bd9Sstevel@tonic-gate 		log(" ");
4297c478bd9Sstevel@tonic-gate 	}
4307c478bd9Sstevel@tonic-gate 	log("********** end dumping ttydefs table **********");
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate #endif
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate /*
4367c478bd9Sstevel@tonic-gate  * this is copies from uucp/strsave.c
4377c478bd9Sstevel@tonic-gate  * and is modified that if malloc fails, it will exit
4387c478bd9Sstevel@tonic-gate  */
4397c478bd9Sstevel@tonic-gate char *
strsave(char * str)440*3bb2c156SToomas Soome strsave(char *str)
4417c478bd9Sstevel@tonic-gate {
442*3bb2c156SToomas Soome 	char *rval;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	if (str == NULL) {
445*3bb2c156SToomas Soome 		if ((rval = malloc(1)) == NULL) {
4467c478bd9Sstevel@tonic-gate 			log("strsave: malloc failed");
4477c478bd9Sstevel@tonic-gate 			exit(1);
4487c478bd9Sstevel@tonic-gate 		}
4497c478bd9Sstevel@tonic-gate 		*rval = '\0';
450*3bb2c156SToomas Soome 	} else {
451*3bb2c156SToomas Soome 		if ((rval = malloc(strlen(str) + 1)) == NULL) {
4527c478bd9Sstevel@tonic-gate 			log("strsave: malloc failed");
4537c478bd9Sstevel@tonic-gate 			exit(1);
4547c478bd9Sstevel@tonic-gate 		}
455*3bb2c156SToomas Soome 		(void) strcpy(rval, str);
4567c478bd9Sstevel@tonic-gate 	}
457*3bb2c156SToomas Soome 	return (rval);
4587c478bd9Sstevel@tonic-gate }
459