xref: /titanic_52/usr/src/lib/libnsl/dial/conn.c (revision e8031f0a8ed0e45c6d8847c5e09424e66fd34a4b)
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  */
2261961e0fSrobinson 
237c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
247c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
27*e8031f0aSraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
33*e8031f0aSraf #include "mt.h"
347c478bd9Sstevel@tonic-gate #include "uucp.h"
357c478bd9Sstevel@tonic-gate static char _ProtoSys[40];	/* protocol string from Systems file entry */
367c478bd9Sstevel@tonic-gate static char _ProtoDev[40];	/* protocol string from Devices file entry */
3761961e0fSrobinson static char _ProtoCfg[];	/* protocol string from Config  file entry */
387c478bd9Sstevel@tonic-gate 
3961961e0fSrobinson static jmp_buf Sjbuf;
4061961e0fSrobinson static unsigned expecttime;
417c478bd9Sstevel@tonic-gate 
4261961e0fSrobinson static int	Modemctrl;
437c478bd9Sstevel@tonic-gate 
4461961e0fSrobinson static void alarmtr(int);
4561961e0fSrobinson static void getProto(char *, char *);
4661961e0fSrobinson static int finds(char *, char *[], int);
4761961e0fSrobinson static int getto(char *[]);	/* make this static when ct uses altconn() */
4861961e0fSrobinson static int chat(int, char *[], int, char *, char *);
4961961e0fSrobinson static int rddev(char *, char *[], char *, int);
5061961e0fSrobinson static int expect(char *, int);
5161961e0fSrobinson static int wrstr(int, char *, int, int);
5261961e0fSrobinson static int wrchr(int, char *, int);
5361961e0fSrobinson static int processdev(char *[], char *[]);
5461961e0fSrobinson static int getdevline(char *, int);
5561961e0fSrobinson static int getsysline(char *, int);
5661961e0fSrobinson static int sysaccess(int);
5761961e0fSrobinson static int clear_hup(int);
5861961e0fSrobinson #ifndef SMALL
5961961e0fSrobinson static char *currsys(void);
6061961e0fSrobinson static char *currdev(void);
6161961e0fSrobinson #endif
6261961e0fSrobinson static int wait_for_hangup(int);
6361961e0fSrobinson static int expect_str(char *, int);
647c478bd9Sstevel@tonic-gate 
6561961e0fSrobinson static void sendthem(char *, int, char *, char *);
6661961e0fSrobinson static void nap(unsigned int);
6761961e0fSrobinson static int notin(char *, char *);
6861961e0fSrobinson static int ifdate(char *);
6961961e0fSrobinson static int classmatch(char *[], char *[]);
707c478bd9Sstevel@tonic-gate 
7161961e0fSrobinson static char *Myline = CNULL;	/* to force which line will be used */
7261961e0fSrobinson static char *Mytype = CNULL;	/* to force selection of specific device type */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * conn - place a telephone call to system and login, etc.
767c478bd9Sstevel@tonic-gate  *
777c478bd9Sstevel@tonic-gate  * return codes:
787c478bd9Sstevel@tonic-gate  *	FAIL - connection failed
797c478bd9Sstevel@tonic-gate  *	>0  - file no.  -  connect ok
807c478bd9Sstevel@tonic-gate  * When a failure occurs, Uerror is set.
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate 
8361961e0fSrobinson static int
8461961e0fSrobinson conn(char *system)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	int nf, fn = FAIL;
877c478bd9Sstevel@tonic-gate 	char *flds[F_MAX+1];
8861961e0fSrobinson 	static void sysreset(void);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	CDEBUG(4, "conn(%s)\n", system);
917c478bd9Sstevel@tonic-gate 	Uerror = 0;
927c478bd9Sstevel@tonic-gate 	while ((nf = finds(system, flds, F_MAX)) > 0) {
937c478bd9Sstevel@tonic-gate 		fn = getto(flds);
947c478bd9Sstevel@tonic-gate 		CDEBUG(4, "getto ret %d\n", fn);
957c478bd9Sstevel@tonic-gate 		if (fn < 0)
967c478bd9Sstevel@tonic-gate 			continue;
977c478bd9Sstevel@tonic-gate 		if (EQUALS(Progname, "uucico")) {
9861961e0fSrobinson 			if (chat(nf - F_LOGIN, flds + F_LOGIN, fn, "", "") ==
9961961e0fSrobinson 								SUCCESS) {
1007c478bd9Sstevel@tonic-gate 				sysreset();
1017c478bd9Sstevel@tonic-gate 				return (fn); /* successful return */
1027c478bd9Sstevel@tonic-gate 			}
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 			/* login failed */
1057c478bd9Sstevel@tonic-gate 			DEBUG(6, "close caller (%d)\n", fn);
1067c478bd9Sstevel@tonic-gate 			fd_rmlock(fn);
10761961e0fSrobinson 			(void) close(fn);
1087c478bd9Sstevel@tonic-gate 			if (Dc[0] != NULLCHAR) {
1097c478bd9Sstevel@tonic-gate 				/*EMPTY*/
1107c478bd9Sstevel@tonic-gate 				DEBUG(6, "delock line (%s)\n", Dc);
1117c478bd9Sstevel@tonic-gate 			}
1127c478bd9Sstevel@tonic-gate 		} else {
1137c478bd9Sstevel@tonic-gate 			sysreset();
1147c478bd9Sstevel@tonic-gate 			return (fn);
1157c478bd9Sstevel@tonic-gate 		}
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	/* finds or getto failed */
1197c478bd9Sstevel@tonic-gate 	sysreset();
1207c478bd9Sstevel@tonic-gate 	CDEBUG(1, "Call Failed: %s\n", UERRORTEXT);
1217c478bd9Sstevel@tonic-gate 	return (FAIL);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  * getto - connect to remote machine
1267c478bd9Sstevel@tonic-gate  *
1277c478bd9Sstevel@tonic-gate  * return codes:
1287c478bd9Sstevel@tonic-gate  *	>0  -  file number - ok
1297c478bd9Sstevel@tonic-gate  *	FAIL  -  failed
1307c478bd9Sstevel@tonic-gate  */
1317c478bd9Sstevel@tonic-gate 
13261961e0fSrobinson static int
13361961e0fSrobinson getto(char *flds[])
1347c478bd9Sstevel@tonic-gate {
1357c478bd9Sstevel@tonic-gate 	char *dev[D_MAX+2], devbuf[BUFSIZ];
13661961e0fSrobinson 	int status;
13761961e0fSrobinson 	int dcf = -1;
1387c478bd9Sstevel@tonic-gate 	int reread = 0;
1397c478bd9Sstevel@tonic-gate 	int tries = 0;	/* count of call attempts - for limit purposes */
14061961e0fSrobinson 	static void devreset(void);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	CDEBUG(1, "Device Type %s wanted\n", flds[F_TYPE]);
1437c478bd9Sstevel@tonic-gate 	Uerror = 0;
1447c478bd9Sstevel@tonic-gate 	while (tries < TRYCALLS) {
14561961e0fSrobinson 		if ((status = rddev(flds[F_TYPE], dev, devbuf, D_MAX)) ==
14661961e0fSrobinson 								FAIL) {
1477c478bd9Sstevel@tonic-gate 			if (tries == 0 || ++reread >= TRYCALLS)
1487c478bd9Sstevel@tonic-gate 				break;
1497c478bd9Sstevel@tonic-gate 			devreset();
1507c478bd9Sstevel@tonic-gate 			continue;
1517c478bd9Sstevel@tonic-gate 		}
1527c478bd9Sstevel@tonic-gate 		/* check class, check (and possibly set) speed */
1537c478bd9Sstevel@tonic-gate 		if (classmatch(flds, dev) != SUCCESS) {
1547c478bd9Sstevel@tonic-gate 			DEBUG(7, "Skipping entry in '%s'", currdev());
1557c478bd9Sstevel@tonic-gate 			DEBUG(7, " - class (%s) not wanted.\n", dev[D_CLASS]);
1567c478bd9Sstevel@tonic-gate 			continue;
1577c478bd9Sstevel@tonic-gate 		}
1587c478bd9Sstevel@tonic-gate 		DEBUG(5, "Trying device entry from '%s'.\n", currdev());
1597c478bd9Sstevel@tonic-gate 		if ((dcf = processdev(flds, dev)) >= 0)
1607c478bd9Sstevel@tonic-gate 			break;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 		switch (Uerror) {
1637c478bd9Sstevel@tonic-gate 		case SS_CANT_ACCESS_DEVICE:
1647c478bd9Sstevel@tonic-gate 		case SS_DEVICE_FAILED:
1657c478bd9Sstevel@tonic-gate 		case SS_LOCKED_DEVICE:
1667c478bd9Sstevel@tonic-gate 			break;
1677c478bd9Sstevel@tonic-gate 		default:
1687c478bd9Sstevel@tonic-gate 			tries++;
1697c478bd9Sstevel@tonic-gate 			break;
1707c478bd9Sstevel@tonic-gate 		}
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 	devreset();	/* reset devices file(s) */
1737c478bd9Sstevel@tonic-gate 	if (status == FAIL && !Uerror) {
1747c478bd9Sstevel@tonic-gate 		CDEBUG(1, "Requested Device Type Not Found\n%s", "");
1757c478bd9Sstevel@tonic-gate 		Uerror = SS_NO_DEVICE;
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 	return (dcf);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate  * classmatch - process 'Any' in Devices and Systems and
1827c478bd9Sstevel@tonic-gate  *	determine the correct speed, or match for ==
1837c478bd9Sstevel@tonic-gate  */
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate static int
18661961e0fSrobinson classmatch(char *flds[], char *dev[])
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	/* check class, check (and possibly set) speed */
18961961e0fSrobinson 	if (EQUALS(flds[F_CLASS], "Any") && EQUALS(dev[D_CLASS], "Any")) {
1907c478bd9Sstevel@tonic-gate 		dev[D_CLASS] = DEFAULT_BAUDRATE;
1917c478bd9Sstevel@tonic-gate 		return (SUCCESS);
1927c478bd9Sstevel@tonic-gate 	} else if (EQUALS(dev[D_CLASS], "Any")) {
1937c478bd9Sstevel@tonic-gate 		dev[D_CLASS] = flds[F_CLASS];
1947c478bd9Sstevel@tonic-gate 		return (SUCCESS);
1957c478bd9Sstevel@tonic-gate 	} else if (EQUALS(flds[F_CLASS], "Any") ||
19661961e0fSrobinson 					EQUALS(flds[F_CLASS], dev[D_CLASS]))
1977c478bd9Sstevel@tonic-gate 		return (SUCCESS);
19861961e0fSrobinson 	else
1997c478bd9Sstevel@tonic-gate 		return (FAIL);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate /*
2047c478bd9Sstevel@tonic-gate  *	rddev - find and unpack a line from device file for this caller type
2057c478bd9Sstevel@tonic-gate  *	lines starting with whitespace of '#' are comments
2067c478bd9Sstevel@tonic-gate  *
2077c478bd9Sstevel@tonic-gate  *	return codes:
2087c478bd9Sstevel@tonic-gate  *		>0  -  number of arguments in vector - succeeded
2097c478bd9Sstevel@tonic-gate  *		FAIL - EOF
2107c478bd9Sstevel@tonic-gate  */
2117c478bd9Sstevel@tonic-gate 
21261961e0fSrobinson static int
2137c478bd9Sstevel@tonic-gate rddev(char *type, char *dev[], char *buf, int devcount)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	char *commap, d_type[BUFSIZ];
2167c478bd9Sstevel@tonic-gate 	int na;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	while (getdevline(buf, BUFSIZ)) {
21961961e0fSrobinson 		if (buf[0] == ' ' || buf[0] == '\t' || buf[0] == '\n' ||
22061961e0fSrobinson 					buf[0] == '\0' || buf[0] == '#')
2217c478bd9Sstevel@tonic-gate 			continue;
2227c478bd9Sstevel@tonic-gate 		na = getargs(buf, dev, devcount);
2237c478bd9Sstevel@tonic-gate 		ASSERT(na >= D_CALLER, "BAD LINE", buf, na);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 		if (strncmp(dev[D_LINE], "/dev/", 5) == 0) {
2267c478bd9Sstevel@tonic-gate 			/* since cu (altconn()) strips off leading */
2277c478bd9Sstevel@tonic-gate 			/* "/dev/",  do the same here.  */
22861961e0fSrobinson 			(void) strcpy(dev[D_LINE], &(dev[D_LINE][5]));
2297c478bd9Sstevel@tonic-gate 		}
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 		/* may have ",M" subfield in D_LINE */
2327c478bd9Sstevel@tonic-gate 		Modemctrl = FALSE;
23361961e0fSrobinson 		if ((commap = strchr(dev[D_LINE], ',')) != NULL) {
2347c478bd9Sstevel@tonic-gate 			if (strcmp(commap, ",M") == SAME)
2357c478bd9Sstevel@tonic-gate 				Modemctrl = TRUE;
2367c478bd9Sstevel@tonic-gate 			*commap = '\0';
2377c478bd9Sstevel@tonic-gate 		}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 		/*
2407c478bd9Sstevel@tonic-gate 		 * D_TYPE field may have protocol subfield, which
2417c478bd9Sstevel@tonic-gate 		 * must be pulled off before comparing to desired type.
2427c478bd9Sstevel@tonic-gate 		 */
2437c478bd9Sstevel@tonic-gate 		(void) strcpy(d_type, dev[D_TYPE]);
24461961e0fSrobinson 		if ((commap = strchr(d_type, ',')) != NULL)
2457c478bd9Sstevel@tonic-gate 			*commap = '\0';
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 		/* to force the requested device type to be used. */
2487c478bd9Sstevel@tonic-gate 		if ((Mytype != NULL) && (!EQUALS(Mytype, d_type)))
2497c478bd9Sstevel@tonic-gate 			continue;
2507c478bd9Sstevel@tonic-gate 		/* to force the requested line to be used */
2517c478bd9Sstevel@tonic-gate 		if ((Myline != NULL) && (!EQUALS(Myline, dev[D_LINE])))
2527c478bd9Sstevel@tonic-gate 			continue;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 		bsfix(dev);	/* replace \X fields */
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 		if (EQUALS(d_type, type)) {
2577c478bd9Sstevel@tonic-gate 			getProto(_ProtoDev, dev[D_TYPE]);
2587c478bd9Sstevel@tonic-gate 			return (na);
2597c478bd9Sstevel@tonic-gate 		}
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 	return (FAIL);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate /*
2667c478bd9Sstevel@tonic-gate  * finds	- set system attribute vector
2677c478bd9Sstevel@tonic-gate  *
2687c478bd9Sstevel@tonic-gate  * input:
2697c478bd9Sstevel@tonic-gate  *	fsys - open Systems file descriptor
2707c478bd9Sstevel@tonic-gate  *	sysnam - system name to find
2717c478bd9Sstevel@tonic-gate  * output:
2727c478bd9Sstevel@tonic-gate  *	flds - attibute vector from Systems file
2737c478bd9Sstevel@tonic-gate  *	fldcount - number of fields in flds
2747c478bd9Sstevel@tonic-gate  * return codes:
2757c478bd9Sstevel@tonic-gate  *	>0  -  number of arguments in vector - succeeded
2767c478bd9Sstevel@tonic-gate  *	FAIL - failed
2777c478bd9Sstevel@tonic-gate  * Uerror set:
2787c478bd9Sstevel@tonic-gate  *	0 - found a line in Systems file
2797c478bd9Sstevel@tonic-gate  *	SS_BADSYSTEM - no line found in Systems file
2807c478bd9Sstevel@tonic-gate  *	SS_TIME_WRONG - wrong time to call
2817c478bd9Sstevel@tonic-gate  */
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate static int
2847c478bd9Sstevel@tonic-gate finds(char *sysnam, char *flds[], int fldcount)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate 	static char *info;	/* dynamically allocated BUFSIZ */
2877c478bd9Sstevel@tonic-gate 	int na;
2887c478bd9Sstevel@tonic-gate 
28961961e0fSrobinson 	/*
29061961e0fSrobinson 	 * format of fields
2917c478bd9Sstevel@tonic-gate 	 *	0 name;
2927c478bd9Sstevel@tonic-gate 	 *	1 time
2937c478bd9Sstevel@tonic-gate 	 *	2 acu/hardwired
2947c478bd9Sstevel@tonic-gate 	 *	3 speed
2957c478bd9Sstevel@tonic-gate 	 *	etc
2967c478bd9Sstevel@tonic-gate 	 */
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	if (sysnam == 0 || *sysnam == 0) {
2997c478bd9Sstevel@tonic-gate 		Uerror = SS_BADSYSTEM;
3007c478bd9Sstevel@tonic-gate 		return (FAIL);
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	if (info == NULL) {
30461961e0fSrobinson 		info = malloc(BUFSIZ);
3057c478bd9Sstevel@tonic-gate 		if (info == NULL) {
3067c478bd9Sstevel@tonic-gate 			DEBUG(1, "malloc failed for info in finds\n", 0);
3077c478bd9Sstevel@tonic-gate 			return (0);
3087c478bd9Sstevel@tonic-gate 		}
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate 	while (getsysline(info, BUFSIZ)) {
3117c478bd9Sstevel@tonic-gate 		na = getargs(info, flds, fldcount);
3127c478bd9Sstevel@tonic-gate 		bsfix(flds);	/* replace \X fields */
3137c478bd9Sstevel@tonic-gate 		if (!EQUALSN(sysnam, flds[F_NAME], MAXBASENAME))
3147c478bd9Sstevel@tonic-gate 			continue;
3157c478bd9Sstevel@tonic-gate 		/* check if requested Mytype device type */
31661961e0fSrobinson 		if ((Mytype != CNULL) &&
31761961e0fSrobinson 			    (!EQUALSN(flds[F_TYPE], Mytype, strlen(Mytype)))) {
3187c478bd9Sstevel@tonic-gate 			DEBUG(7, "Skipping entry in '%s'", currsys());
3197c478bd9Sstevel@tonic-gate 			DEBUG(7, " - type (%s) not wanted.\n", flds[F_TYPE]);
3207c478bd9Sstevel@tonic-gate 			continue;
3217c478bd9Sstevel@tonic-gate 		} else {
3227c478bd9Sstevel@tonic-gate 			/*EMPTY*/
3237c478bd9Sstevel@tonic-gate 			DEBUG(5, "Trying entry from '%s'", currsys());
3247c478bd9Sstevel@tonic-gate 			DEBUG(5, " - device type %s.\n", flds[F_TYPE]);
3257c478bd9Sstevel@tonic-gate 		}
3267c478bd9Sstevel@tonic-gate 		/* OK if not uucico (ie. ct or cu) or the time is right */
3277c478bd9Sstevel@tonic-gate 		if (!EQUALS(Progname, "uucico") || ifdate(flds[F_TIME])) {
3287c478bd9Sstevel@tonic-gate 			/*  found a good entry  */
3297c478bd9Sstevel@tonic-gate 			getProto(_ProtoSys, flds[F_TYPE]);
3307c478bd9Sstevel@tonic-gate 			Uerror = 0;
3317c478bd9Sstevel@tonic-gate 			return (na);	/* FOUND OK LINE */
3327c478bd9Sstevel@tonic-gate 		}
3337c478bd9Sstevel@tonic-gate 		CDEBUG(1, "Wrong Time To Call: %s\n", flds[F_TIME]);
3347c478bd9Sstevel@tonic-gate 		Uerror = SS_TIME_WRONG;
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 	if (!Uerror)
3377c478bd9Sstevel@tonic-gate 		Uerror = SS_BADSYSTEM;
3387c478bd9Sstevel@tonic-gate 	return (FAIL);
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate /*
3427c478bd9Sstevel@tonic-gate  * getProto - get the protocol letters from the input string.
3437c478bd9Sstevel@tonic-gate  * input:
3447c478bd9Sstevel@tonic-gate  *	str - string from Systems/Devices/Config file,
3457c478bd9Sstevel@tonic-gate  *		a ',' delimits the protocol string
3467c478bd9Sstevel@tonic-gate  *		e.g. ACU,g or DK,d
3477c478bd9Sstevel@tonic-gate  * output:
3487c478bd9Sstevel@tonic-gate  *	str - the , (if present) will be replaced with NULLCHAR
3497c478bd9Sstevel@tonic-gate  *
3507c478bd9Sstevel@tonic-gate  * return:  none
3517c478bd9Sstevel@tonic-gate  */
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate static void
35461961e0fSrobinson getProto(char *save, char *str)
3557c478bd9Sstevel@tonic-gate {
35661961e0fSrobinson 	char *p;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	*save = NULLCHAR;
3597c478bd9Sstevel@tonic-gate 	if ((p = strchr(str, ',')) != NULL) {
3607c478bd9Sstevel@tonic-gate 		*p = NULLCHAR;
3617c478bd9Sstevel@tonic-gate 		(void) strcpy(save, p+1);
3627c478bd9Sstevel@tonic-gate 		DEBUG(7, "Protocol = %s\n", save);
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate }
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate /*
3677c478bd9Sstevel@tonic-gate  * chat -	do conversation
3687c478bd9Sstevel@tonic-gate  * input:
3697c478bd9Sstevel@tonic-gate  *	nf - number of fields in flds array
3707c478bd9Sstevel@tonic-gate  *	flds - fields from Systems file
3717c478bd9Sstevel@tonic-gate  *	fn - write file number
3727c478bd9Sstevel@tonic-gate  *	phstr1 - phone number to replace \D
3737c478bd9Sstevel@tonic-gate  *	phstr2 - phone number to replace \T
3747c478bd9Sstevel@tonic-gate  *
3757c478bd9Sstevel@tonic-gate  *	return codes:  0  |  FAIL
3767c478bd9Sstevel@tonic-gate  */
3777c478bd9Sstevel@tonic-gate 
37861961e0fSrobinson static int
37961961e0fSrobinson chat(int nf, char *flds[], int fn, char *phstr1, char *phstr2)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	char *want, *altern;
3827c478bd9Sstevel@tonic-gate 	int k, ok;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	for (k = 0; k < nf; k += 2) {
3857c478bd9Sstevel@tonic-gate 		want = flds[k];
3867c478bd9Sstevel@tonic-gate 		ok = FAIL;
3877c478bd9Sstevel@tonic-gate 		while (ok != 0) {
3887c478bd9Sstevel@tonic-gate 			altern = index(want, '-');
3897c478bd9Sstevel@tonic-gate 			if (altern != NULL)
3907c478bd9Sstevel@tonic-gate 				*altern++ = NULLCHAR;
3917c478bd9Sstevel@tonic-gate 			ok = expect(want, fn);
3927c478bd9Sstevel@tonic-gate 			if (ok == 0)
3937c478bd9Sstevel@tonic-gate 				break;
3947c478bd9Sstevel@tonic-gate 			if (altern == NULL) {
3957c478bd9Sstevel@tonic-gate 				Uerror = SS_LOGIN_FAILED;
3967c478bd9Sstevel@tonic-gate 				logent(UERRORTEXT, "FAILED");
3977c478bd9Sstevel@tonic-gate 				return (FAIL);
3987c478bd9Sstevel@tonic-gate 			}
3997c478bd9Sstevel@tonic-gate 			want = index(altern, '-');
4007c478bd9Sstevel@tonic-gate 			if (want != NULL)
4017c478bd9Sstevel@tonic-gate 				*want++ = NULLCHAR;
4027c478bd9Sstevel@tonic-gate 			sendthem(altern, fn, phstr1, phstr2);
4037c478bd9Sstevel@tonic-gate 		}
40461961e0fSrobinson 		(void) sleep(2);
4057c478bd9Sstevel@tonic-gate 		if (flds[k+1])
4067c478bd9Sstevel@tonic-gate 			sendthem(flds[k+1], fn, phstr1, phstr2);
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate 	return (0);
4097c478bd9Sstevel@tonic-gate }
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate #define	MR 1000
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate /*
4147c478bd9Sstevel@tonic-gate  *	expect(str, fn)	look for expected string w/ possible special chars
4157c478bd9Sstevel@tonic-gate  *	char *str;
4167c478bd9Sstevel@tonic-gate  *
4177c478bd9Sstevel@tonic-gate  *	return codes:
4187c478bd9Sstevel@tonic-gate  *		0  -  found
4197c478bd9Sstevel@tonic-gate  *		FAIL  -  lost line or too many characters read
4207c478bd9Sstevel@tonic-gate  *		some character  -  timed out
4217c478bd9Sstevel@tonic-gate  */
4227c478bd9Sstevel@tonic-gate 
42361961e0fSrobinson static int
42461961e0fSrobinson expect(char *str, int fn)
4257c478bd9Sstevel@tonic-gate {
42661961e0fSrobinson 	char *bptr, *sptr;
4277c478bd9Sstevel@tonic-gate 	char    buf[BUFSIZ];
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	bptr = buf;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	for (sptr = str; *sptr; sptr++) {
4327c478bd9Sstevel@tonic-gate 		if (*sptr == '\\') {
4337c478bd9Sstevel@tonic-gate 			switch (*++sptr) {
4347c478bd9Sstevel@tonic-gate 			case 'H':
4357c478bd9Sstevel@tonic-gate 				*bptr++ = '\0';
4367c478bd9Sstevel@tonic-gate 				if (expect_str(buf, fn) == FAIL) {
4377c478bd9Sstevel@tonic-gate 					return (FAIL);
4387c478bd9Sstevel@tonic-gate 				}
4397c478bd9Sstevel@tonic-gate 				if (wait_for_hangup(fn) == FAIL) {
4407c478bd9Sstevel@tonic-gate 					return (FAIL);
4417c478bd9Sstevel@tonic-gate 				}
4427c478bd9Sstevel@tonic-gate 				bptr = buf;
4437c478bd9Sstevel@tonic-gate 				continue;
4447c478bd9Sstevel@tonic-gate 			case '\\':
4457c478bd9Sstevel@tonic-gate 				*bptr++ = '\\';
4467c478bd9Sstevel@tonic-gate 				continue;
4477c478bd9Sstevel@tonic-gate 			default:
4487c478bd9Sstevel@tonic-gate 				*bptr++ = '\\';
4497c478bd9Sstevel@tonic-gate 				*bptr++ = *sptr;
4507c478bd9Sstevel@tonic-gate 				continue;
4517c478bd9Sstevel@tonic-gate 			}
4527c478bd9Sstevel@tonic-gate 		} else
4537c478bd9Sstevel@tonic-gate 			*bptr++ = *sptr;
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate 	*bptr = '\0';
4567c478bd9Sstevel@tonic-gate 	if (expect_str(buf, fn) == FAIL) {
4577c478bd9Sstevel@tonic-gate 		return (FAIL);
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 	return (0);
4607c478bd9Sstevel@tonic-gate }
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate /*
4637c478bd9Sstevel@tonic-gate  *	expect_str(str, fn)	look for expected string, w/ no special chars
4647c478bd9Sstevel@tonic-gate  *
4657c478bd9Sstevel@tonic-gate  *	return codes:
4667c478bd9Sstevel@tonic-gate  *		0  -  found
4677c478bd9Sstevel@tonic-gate  *		FAIL  -  too many characters read
4687c478bd9Sstevel@tonic-gate  *		some character  -  timed out
4697c478bd9Sstevel@tonic-gate  */
4707c478bd9Sstevel@tonic-gate 
47161961e0fSrobinson static int
47261961e0fSrobinson expect_str(char *str, int fn)
4737c478bd9Sstevel@tonic-gate {
4747c478bd9Sstevel@tonic-gate 	static char rdvec[MR];
4757c478bd9Sstevel@tonic-gate 	char *rp = rdvec;
47661961e0fSrobinson 	int kr, c;
4777c478bd9Sstevel@tonic-gate 	char nextch;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	*rp = 0;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	CDEBUG(4, "expect: (%s", "");
4827c478bd9Sstevel@tonic-gate 	for (c = 0; (kr = str[c]) != 0; c++)
4837c478bd9Sstevel@tonic-gate 		if (kr < 040) {
4847c478bd9Sstevel@tonic-gate 			/*EMPTY*/
4857c478bd9Sstevel@tonic-gate 			CDEBUG(4, "^%c", kr | 0100);
4867c478bd9Sstevel@tonic-gate 		} else {
4877c478bd9Sstevel@tonic-gate 			/*EMPTY*/
4887c478bd9Sstevel@tonic-gate 			CDEBUG(4, "%c", kr);
4897c478bd9Sstevel@tonic-gate 		}
4907c478bd9Sstevel@tonic-gate 	CDEBUG(4, ")\n%s", "");
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	if (EQUALS(str, "\"\"")) {
4937c478bd9Sstevel@tonic-gate 		CDEBUG(4, "got it\n%s", "");
4947c478bd9Sstevel@tonic-gate 		return (0);
4957c478bd9Sstevel@tonic-gate 	}
49661961e0fSrobinson 	if (*str == '\0')
4977c478bd9Sstevel@tonic-gate 		return (0);
49861961e0fSrobinson 	if (setjmp(Sjbuf))
4997c478bd9Sstevel@tonic-gate 		return (FAIL);
5007c478bd9Sstevel@tonic-gate 	(void) signal(SIGALRM, alarmtr);
50161961e0fSrobinson 	(void) alarm(expecttime);
5027c478bd9Sstevel@tonic-gate 	while (notin(str, rdvec)) {
5037c478bd9Sstevel@tonic-gate 		errno = 0;
5047c478bd9Sstevel@tonic-gate 		kr = (*Read)(fn, &nextch, 1);
5057c478bd9Sstevel@tonic-gate 		if (kr <= 0) {
50661961e0fSrobinson 			(void) alarm(0);
5077c478bd9Sstevel@tonic-gate 			CDEBUG(4, "lost line errno - %d\n", errno);
5087c478bd9Sstevel@tonic-gate 			logent("LOGIN", "LOST LINE");
5097c478bd9Sstevel@tonic-gate 			return (FAIL);
5107c478bd9Sstevel@tonic-gate 		}
5117c478bd9Sstevel@tonic-gate 		c = nextch & 0177;
5127c478bd9Sstevel@tonic-gate 		CDEBUG(4, "%s", c < 040 ? "^" : "");
5137c478bd9Sstevel@tonic-gate 		CDEBUG(4, "%c", c < 040 ? c | 0100 : c);
5147c478bd9Sstevel@tonic-gate 		if ((*rp = nextch & 0177) != NULLCHAR)
5157c478bd9Sstevel@tonic-gate 			rp++;
5167c478bd9Sstevel@tonic-gate 		if (rp >= rdvec + MR) {
5177c478bd9Sstevel@tonic-gate 			CDEBUG(4, "enough already\n%s", "");
51861961e0fSrobinson 			(void) alarm(0);
5197c478bd9Sstevel@tonic-gate 			return (FAIL);
5207c478bd9Sstevel@tonic-gate 		}
5217c478bd9Sstevel@tonic-gate 		*rp = NULLCHAR;
5227c478bd9Sstevel@tonic-gate 	}
52361961e0fSrobinson 	(void) alarm(0);
5247c478bd9Sstevel@tonic-gate 	CDEBUG(4, "got it\n%s", "");
5257c478bd9Sstevel@tonic-gate 	return (0);
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate /*
5307c478bd9Sstevel@tonic-gate  *	alarmtr()  -  catch alarm routine for "expect".
5317c478bd9Sstevel@tonic-gate  */
5327c478bd9Sstevel@tonic-gate /*ARGSUSED*/
53361961e0fSrobinson static void
53461961e0fSrobinson alarmtr(int sig)
5357c478bd9Sstevel@tonic-gate {
5367c478bd9Sstevel@tonic-gate 	CDEBUG(6, "timed out\n%s", "");
5377c478bd9Sstevel@tonic-gate 	longjmp(Sjbuf, 1);
5387c478bd9Sstevel@tonic-gate }
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate /*
5417c478bd9Sstevel@tonic-gate  *	wait_for_hangup() - wait for a hangup to occur on the given device
5427c478bd9Sstevel@tonic-gate  */
5437c478bd9Sstevel@tonic-gate int
54461961e0fSrobinson wait_for_hangup(int dcf)
5457c478bd9Sstevel@tonic-gate {
5467c478bd9Sstevel@tonic-gate 	int rval;
5477c478bd9Sstevel@tonic-gate 	char buff[BUFSIZ];
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	CDEBUG(4, "Waiting for hangup\n%s", "");
55061961e0fSrobinson 	while ((rval = read(dcf, buff, BUFSIZ)) > 0)
55161961e0fSrobinson 		;
5527c478bd9Sstevel@tonic-gate 
55361961e0fSrobinson 	if (rval < 0)
5547c478bd9Sstevel@tonic-gate 		return (FAIL);
5557c478bd9Sstevel@tonic-gate 	CDEBUG(4, "Received hangup\n%s", "");
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	if (clear_hup(dcf) != SUCCESS) {
5587c478bd9Sstevel@tonic-gate 	    CDEBUG(4, "Unable to clear hup on device\n%s", "");
5597c478bd9Sstevel@tonic-gate 	    return (FAIL);
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 	return (SUCCESS);
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate /*
5657c478bd9Sstevel@tonic-gate  *	sendthem(str, fn, phstr1, phstr2)	send line of chat sequence
5667c478bd9Sstevel@tonic-gate  *	char *str, *phstr;
5677c478bd9Sstevel@tonic-gate  *
5687c478bd9Sstevel@tonic-gate  *	return codes:  none
5697c478bd9Sstevel@tonic-gate  */
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate #define	FLUSH() { \
5727c478bd9Sstevel@tonic-gate 	if ((bptr - buf) > 0) \
5737c478bd9Sstevel@tonic-gate 		if (wrstr(fn, buf, bptr - buf, echocheck) != SUCCESS) \
5747c478bd9Sstevel@tonic-gate 			goto err; \
5757c478bd9Sstevel@tonic-gate 	bptr = buf; \
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate 
57861961e0fSrobinson static void
57961961e0fSrobinson sendthem(char *str, int fn, char *phstr1, char *phstr2)
5807c478bd9Sstevel@tonic-gate {
5817c478bd9Sstevel@tonic-gate 	int sendcr = 1, echocheck = 0;
58261961e0fSrobinson 	char	*sptr, *bptr;
5837c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ];
5847c478bd9Sstevel@tonic-gate 	struct termio	ttybuf;
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	/* should be EQUALS, but previous versions had BREAK n for integer n */
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	if (PREFIX("BREAK", str)) {
5897c478bd9Sstevel@tonic-gate 		/* send break */
5907c478bd9Sstevel@tonic-gate 		CDEBUG(5, "BREAK\n%s", "");
5917c478bd9Sstevel@tonic-gate 		(*genbrk)(fn);
5927c478bd9Sstevel@tonic-gate 		return;
5937c478bd9Sstevel@tonic-gate 	}
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	if (EQUALS(str, "EOT")) {
5967c478bd9Sstevel@tonic-gate 		CDEBUG(5, "EOT\n%s", "");
5977c478bd9Sstevel@tonic-gate 		(void) (*Write)(fn, EOTMSG, strlen(EOTMSG));
5987c478bd9Sstevel@tonic-gate 		return;
5997c478bd9Sstevel@tonic-gate 	}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	if (EQUALS(str, "\"\"")) {
6027c478bd9Sstevel@tonic-gate 		CDEBUG(5, "\"\"\n%s", "");
6037c478bd9Sstevel@tonic-gate 		str += 2;
6047c478bd9Sstevel@tonic-gate 	}
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	bptr = buf;
6077c478bd9Sstevel@tonic-gate 	CDEBUG(5, "sendthem (%s", "");
6087c478bd9Sstevel@tonic-gate 	for (sptr = str; *sptr; sptr++) {
6097c478bd9Sstevel@tonic-gate 		if (*sptr == '\\') {
6107c478bd9Sstevel@tonic-gate 			switch (*++sptr) {
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 			/* adjust switches */
6137c478bd9Sstevel@tonic-gate 			case 'c':	/* no CR after string */
6147c478bd9Sstevel@tonic-gate 				FLUSH();
6157c478bd9Sstevel@tonic-gate 				if (sptr[1] == NULLCHAR) {
6167c478bd9Sstevel@tonic-gate 					CDEBUG(5, "<NO CR>%s", "");
6177c478bd9Sstevel@tonic-gate 					sendcr = 0;
6187c478bd9Sstevel@tonic-gate 				} else {
6197c478bd9Sstevel@tonic-gate 					/*EMPTY*/
6207c478bd9Sstevel@tonic-gate 					CDEBUG(5, "<NO CR IGNORED>\n%s", "");
6217c478bd9Sstevel@tonic-gate 				}
6227c478bd9Sstevel@tonic-gate 				continue;
6237c478bd9Sstevel@tonic-gate 			}
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 			/* stash in buf and continue */
6267c478bd9Sstevel@tonic-gate 			switch (*sptr) {
6277c478bd9Sstevel@tonic-gate 			case 'D':	/* raw phnum */
62861961e0fSrobinson 				(void) strcpy(bptr, phstr1);
6297c478bd9Sstevel@tonic-gate 				bptr += strlen(bptr);
6307c478bd9Sstevel@tonic-gate 				continue;
6317c478bd9Sstevel@tonic-gate 			case 'T':	/* translated phnum */
63261961e0fSrobinson 				(void) strcpy(bptr, phstr2);
6337c478bd9Sstevel@tonic-gate 				bptr += strlen(bptr);
6347c478bd9Sstevel@tonic-gate 				continue;
6357c478bd9Sstevel@tonic-gate 			case 'N':	/* null */
6367c478bd9Sstevel@tonic-gate 				*bptr++ = 0;
6377c478bd9Sstevel@tonic-gate 				continue;
6387c478bd9Sstevel@tonic-gate 			case 's':	/* space */
6397c478bd9Sstevel@tonic-gate 				*bptr++ = ' ';
6407c478bd9Sstevel@tonic-gate 				continue;
6417c478bd9Sstevel@tonic-gate 			case '\\':	/* backslash escapes itself */
6427c478bd9Sstevel@tonic-gate 				*bptr++ = *sptr;
6437c478bd9Sstevel@tonic-gate 				continue;
6447c478bd9Sstevel@tonic-gate 			default:	/* send the backslash */
6457c478bd9Sstevel@tonic-gate 				*bptr++ = '\\';
6467c478bd9Sstevel@tonic-gate 				*bptr++ = *sptr;
6477c478bd9Sstevel@tonic-gate 				continue;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 			/* flush buf, perform action, and continue */
6507c478bd9Sstevel@tonic-gate 			case 'E':	/* echo check on */
6517c478bd9Sstevel@tonic-gate 				FLUSH();
6527c478bd9Sstevel@tonic-gate 				CDEBUG(5, "ECHO CHECK ON\n%s", "");
6537c478bd9Sstevel@tonic-gate 				echocheck = 1;
6547c478bd9Sstevel@tonic-gate 				continue;
6557c478bd9Sstevel@tonic-gate 			case 'e':	/* echo check off */
6567c478bd9Sstevel@tonic-gate 				FLUSH();
6577c478bd9Sstevel@tonic-gate 				CDEBUG(5, "ECHO CHECK OFF\n%s", "");
6587c478bd9Sstevel@tonic-gate 				echocheck = 0;
6597c478bd9Sstevel@tonic-gate 				continue;
6607c478bd9Sstevel@tonic-gate 			case 'd':	/* sleep briefly */
6617c478bd9Sstevel@tonic-gate 				FLUSH();
6627c478bd9Sstevel@tonic-gate 				CDEBUG(5, "DELAY\n%s", "");
66361961e0fSrobinson 				(void) sleep(2);
6647c478bd9Sstevel@tonic-gate 				continue;
6657c478bd9Sstevel@tonic-gate 			case 'p':	/* pause momentarily */
6667c478bd9Sstevel@tonic-gate 				FLUSH();
6677c478bd9Sstevel@tonic-gate 				CDEBUG(5, "PAUSE\n%s", "");
6687c478bd9Sstevel@tonic-gate 				nap(HZ/4);	/* approximately 1/4 second */
6697c478bd9Sstevel@tonic-gate 				continue;
6707c478bd9Sstevel@tonic-gate 			case 'K':	/* inline break */
6717c478bd9Sstevel@tonic-gate 				FLUSH();
6727c478bd9Sstevel@tonic-gate 				CDEBUG(5, "BREAK\n%s", "");
6737c478bd9Sstevel@tonic-gate 				(*genbrk)(fn);
6747c478bd9Sstevel@tonic-gate 				continue;
6757c478bd9Sstevel@tonic-gate 			case 'M':	/* modem control - set CLOCAL */
6767c478bd9Sstevel@tonic-gate 			case 'm':	/* no modem control - clear CLOCAL */
6777c478bd9Sstevel@tonic-gate 				FLUSH();
6787c478bd9Sstevel@tonic-gate 				CDEBUG(5, ")\n%s CLOCAL ",
6797c478bd9Sstevel@tonic-gate 					(*sptr == 'M' ? "set" : "clear"));
6807c478bd9Sstevel@tonic-gate 				if ((*Ioctl)(fn, TCGETA, &ttybuf) != 0) {
6817c478bd9Sstevel@tonic-gate 					/*EMPTY*/
68261961e0fSrobinson 					CDEBUG(5,
68361961e0fSrobinson 					    "ignored. TCGETA failed, errno %d",
68461961e0fSrobinson 					    errno);
6857c478bd9Sstevel@tonic-gate 				} else {
6867c478bd9Sstevel@tonic-gate 					if (*sptr == 'M')
6877c478bd9Sstevel@tonic-gate 					ttybuf.c_cflag |= CLOCAL;
6887c478bd9Sstevel@tonic-gate 					else
6897c478bd9Sstevel@tonic-gate 					ttybuf.c_cflag &= ~CLOCAL;
6907c478bd9Sstevel@tonic-gate 					if ((*Ioctl)(fn, TCSETAW, &ttybuf) != 0)
6917c478bd9Sstevel@tonic-gate 						/*EMPTY*/
69261961e0fSrobinson 					CDEBUG(5,
69361961e0fSrobinson 					    "failed. TCSETAW failed, errno %d",
69461961e0fSrobinson 					    errno);
6957c478bd9Sstevel@tonic-gate 				}
6967c478bd9Sstevel@tonic-gate 				CDEBUG(5, "\n%s", "");
6977c478bd9Sstevel@tonic-gate 				continue;
6987c478bd9Sstevel@tonic-gate 			}
6997c478bd9Sstevel@tonic-gate 		} else
7007c478bd9Sstevel@tonic-gate 			*bptr++ = *sptr;
7017c478bd9Sstevel@tonic-gate 	}
7027c478bd9Sstevel@tonic-gate 	if (sendcr)
7037c478bd9Sstevel@tonic-gate 		*bptr++ = '\r';
7047c478bd9Sstevel@tonic-gate 	if ((bptr - buf) > 0)
7057c478bd9Sstevel@tonic-gate 		(void) wrstr(fn, buf, bptr - buf, echocheck);
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate err:
7087c478bd9Sstevel@tonic-gate 	CDEBUG(5, ")\n%s", "");
7097c478bd9Sstevel@tonic-gate }
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate #undef FLUSH
7127c478bd9Sstevel@tonic-gate 
71361961e0fSrobinson static int
7147c478bd9Sstevel@tonic-gate wrstr(int fn, char *buf, int len, int echocheck)
7157c478bd9Sstevel@tonic-gate {
7167c478bd9Sstevel@tonic-gate 	int	i;
7177c478bd9Sstevel@tonic-gate 	char dbuf[BUFSIZ], *dbptr = dbuf;
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	buf[len] = 0;
7207c478bd9Sstevel@tonic-gate 
72161961e0fSrobinson 	if (echocheck)
72261961e0fSrobinson 		return (wrchr(fn, buf, len));
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	if (Debug >= 5) {
72561961e0fSrobinson 		if (sysaccess(ACCESS_SYSTEMS) == 0) {
72661961e0fSrobinson 			/* Systems file access ok */
7277c478bd9Sstevel@tonic-gate 			for (i = 0; i < len; i++) {
7287c478bd9Sstevel@tonic-gate 				*dbptr = buf[i];
7297c478bd9Sstevel@tonic-gate 				if (*dbptr < 040) {
7307c478bd9Sstevel@tonic-gate 					*dbptr++ = '^';
7317c478bd9Sstevel@tonic-gate 					*dbptr = buf[i] | 0100;
7327c478bd9Sstevel@tonic-gate 				}
7337c478bd9Sstevel@tonic-gate 				dbptr++;
7347c478bd9Sstevel@tonic-gate 			}
7357c478bd9Sstevel@tonic-gate 			*dbptr = 0;
7367c478bd9Sstevel@tonic-gate 		} else
73761961e0fSrobinson 			(void) strcpy(dbuf, "????????");
7387c478bd9Sstevel@tonic-gate 		CDEBUG(5, "%s", dbuf);
7397c478bd9Sstevel@tonic-gate 	}
74061961e0fSrobinson 	if ((*Write)(fn, buf, len) != len)
7417c478bd9Sstevel@tonic-gate 		return (FAIL);
7427c478bd9Sstevel@tonic-gate 	return (SUCCESS);
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate 
74561961e0fSrobinson static int
7467c478bd9Sstevel@tonic-gate wrchr(int fn, char *buf, int len)
7477c478bd9Sstevel@tonic-gate {
7487c478bd9Sstevel@tonic-gate 	int	i, saccess;
7497c478bd9Sstevel@tonic-gate 	char	cin, cout;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	saccess = (sysaccess(ACCESS_SYSTEMS) == 0); /* protect Systems file */
75261961e0fSrobinson 	if (setjmp(Sjbuf))
7537c478bd9Sstevel@tonic-gate 		return (FAIL);
7547c478bd9Sstevel@tonic-gate 	(void) signal(SIGALRM, alarmtr);
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++) {
7577c478bd9Sstevel@tonic-gate 		cout = buf[i];
7587c478bd9Sstevel@tonic-gate 		if (saccess) {
7597c478bd9Sstevel@tonic-gate 			/*EMPTY*/
7607c478bd9Sstevel@tonic-gate 			CDEBUG(5, "%s", cout < 040 ? "^" : "");
7617c478bd9Sstevel@tonic-gate 			CDEBUG(5, "%c", cout < 040 ? cout | 0100 : cout);
7627c478bd9Sstevel@tonic-gate 		} else {
7637c478bd9Sstevel@tonic-gate 			/*EMPTY*/
7647c478bd9Sstevel@tonic-gate 			CDEBUG(5, "?%s", "");
7657c478bd9Sstevel@tonic-gate 		}
76661961e0fSrobinson 		if (((*Write)(fn, &cout, 1)) != 1)
7677c478bd9Sstevel@tonic-gate 			return (FAIL);
7687c478bd9Sstevel@tonic-gate 		do {
7697c478bd9Sstevel@tonic-gate 			(void) alarm(expecttime);
77061961e0fSrobinson 			if ((*Read)(fn, &cin, 1) != 1)
7717c478bd9Sstevel@tonic-gate 				return (FAIL);
7727c478bd9Sstevel@tonic-gate 			(void) alarm(0);
7737c478bd9Sstevel@tonic-gate 			cin &= 0177;
7747c478bd9Sstevel@tonic-gate 			if (saccess) {
7757c478bd9Sstevel@tonic-gate 				/*EMPTY*/
7767c478bd9Sstevel@tonic-gate 				CDEBUG(5, "%s", cin < 040 ? "^" : "");
7777c478bd9Sstevel@tonic-gate 				CDEBUG(5, "%c", cin < 040 ? cin | 0100 : cin);
7787c478bd9Sstevel@tonic-gate 			} else {
7797c478bd9Sstevel@tonic-gate 				/*EMPTY*/
7807c478bd9Sstevel@tonic-gate 				CDEBUG(5, "?%s", "");
7817c478bd9Sstevel@tonic-gate 			}
7827c478bd9Sstevel@tonic-gate 		} while (cout != (cin & 0177));
7837c478bd9Sstevel@tonic-gate 	}
7847c478bd9Sstevel@tonic-gate 	return (SUCCESS);
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate /*
7897c478bd9Sstevel@tonic-gate  *	notin(sh, lg)	check for occurrence of substring "sh"
7907c478bd9Sstevel@tonic-gate  *	char *sh, *lg;
7917c478bd9Sstevel@tonic-gate  *
7927c478bd9Sstevel@tonic-gate  *	return codes:
7937c478bd9Sstevel@tonic-gate  *		0  -  found the string
7947c478bd9Sstevel@tonic-gate  *		1  -  not in the string
7957c478bd9Sstevel@tonic-gate  */
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate static int
79861961e0fSrobinson notin(char *sh, char *lg)
7997c478bd9Sstevel@tonic-gate {
8007c478bd9Sstevel@tonic-gate 	while (*lg != NULLCHAR) {
80161961e0fSrobinson 		if (PREFIX(sh, lg))
8027c478bd9Sstevel@tonic-gate 			return (0);
8037c478bd9Sstevel@tonic-gate 		else
8047c478bd9Sstevel@tonic-gate 			lg++;
8057c478bd9Sstevel@tonic-gate 	}
8067c478bd9Sstevel@tonic-gate 	return (1);
8077c478bd9Sstevel@tonic-gate }
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate /*
8117c478bd9Sstevel@tonic-gate  *	ifdate(s)
8127c478bd9Sstevel@tonic-gate  *	char *s;
8137c478bd9Sstevel@tonic-gate  *
8147c478bd9Sstevel@tonic-gate  *	ifdate  -  this routine will check a string (s)
8157c478bd9Sstevel@tonic-gate  *	like "MoTu0800-1730" to see if the present
8167c478bd9Sstevel@tonic-gate  *	time is within the given limits.
8177c478bd9Sstevel@tonic-gate  *	SIDE EFFECT - Retrytime is set to number following ";"
8187c478bd9Sstevel@tonic-gate  *
8197c478bd9Sstevel@tonic-gate  *	String alternatives:
8207c478bd9Sstevel@tonic-gate  *		Wk - Mo thru Fr
8217c478bd9Sstevel@tonic-gate  *		zero or one time means all day
8227c478bd9Sstevel@tonic-gate  *		Any - any day
8237c478bd9Sstevel@tonic-gate  *
8247c478bd9Sstevel@tonic-gate  *	return codes:
8257c478bd9Sstevel@tonic-gate  *		0  -  not within limits
8267c478bd9Sstevel@tonic-gate  *		1  -  within limits
8277c478bd9Sstevel@tonic-gate  */
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate static int
83061961e0fSrobinson ifdate(char *s)
8317c478bd9Sstevel@tonic-gate {
8327c478bd9Sstevel@tonic-gate 	static char *days[] = {
8337c478bd9Sstevel@tonic-gate 		"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", 0
8347c478bd9Sstevel@tonic-gate 	};
8357c478bd9Sstevel@tonic-gate 	time_t	clock;
8367c478bd9Sstevel@tonic-gate 	int	t__now;
8377c478bd9Sstevel@tonic-gate 	char	*p;
8387c478bd9Sstevel@tonic-gate 	struct tm	*tp;
8397c478bd9Sstevel@tonic-gate 
84061961e0fSrobinson 	(void) time(&clock);
8417c478bd9Sstevel@tonic-gate 	tp = localtime(&clock);
8427c478bd9Sstevel@tonic-gate 	t__now = tp->tm_hour * 100 + tp->tm_min;	/* "navy" time */
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	/*
8457c478bd9Sstevel@tonic-gate 	 *	pick up retry time for failures
8467c478bd9Sstevel@tonic-gate 	 *	global variable Retrytime is set here
8477c478bd9Sstevel@tonic-gate 	 */
8487c478bd9Sstevel@tonic-gate 	if ((p = rindex(s, ';')) != NULL)
8497c478bd9Sstevel@tonic-gate 		if (isdigit(p[1])) {
8507c478bd9Sstevel@tonic-gate 		if (sscanf(p+1, "%ld", &Retrytime) < 1)
8517c478bd9Sstevel@tonic-gate 			Retrytime = 5;	/* 5 minutes is error default */
8527c478bd9Sstevel@tonic-gate 		Retrytime  *= 60;
8537c478bd9Sstevel@tonic-gate 		*p = NULLCHAR;
8547c478bd9Sstevel@tonic-gate 		}
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	while (*s) {
8577c478bd9Sstevel@tonic-gate 		int	i, dayok;
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 		for (dayok = 0; (!dayok) && isalpha(*s); s++) {
8607c478bd9Sstevel@tonic-gate 			if (PREFIX("Any", s))
8617c478bd9Sstevel@tonic-gate 				dayok = 1;
8627c478bd9Sstevel@tonic-gate 			else if (PREFIX("Wk", s)) {
8637c478bd9Sstevel@tonic-gate 				if (tp->tm_wday >= 1 && tp->tm_wday <= 5)
8647c478bd9Sstevel@tonic-gate 					dayok = 1;
8657c478bd9Sstevel@tonic-gate 			} else
8667c478bd9Sstevel@tonic-gate 				for (i = 0; days[i]; i++)
8677c478bd9Sstevel@tonic-gate 					if (PREFIX(days[i], s))
8687c478bd9Sstevel@tonic-gate 						if (tp->tm_wday == i)
8697c478bd9Sstevel@tonic-gate 							dayok = 1;
8707c478bd9Sstevel@tonic-gate 		}
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 		if (dayok) {
8737c478bd9Sstevel@tonic-gate 			int	t__low, t__high;
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 			while (isalpha(*s))	/* flush remaining day stuff */
8767c478bd9Sstevel@tonic-gate 				s++;
8777c478bd9Sstevel@tonic-gate 
87861961e0fSrobinson 			if ((sscanf(s, "%d-%d", &t__low, &t__high) < 2) ||
87961961e0fSrobinson 							(t__low == t__high))
8807c478bd9Sstevel@tonic-gate 				return (1);
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 			/* 0000 crossover? */
8837c478bd9Sstevel@tonic-gate 			if (t__low < t__high) {
88461961e0fSrobinson 				if (t__low <= t__now && t__now <= t__high)
8857c478bd9Sstevel@tonic-gate 					return (1);
88661961e0fSrobinson 			} else if (t__low <= t__now || t__now <= t__high)
8877c478bd9Sstevel@tonic-gate 				return (1);
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 			/* aim at next time slot */
8907c478bd9Sstevel@tonic-gate 			if ((s = index(s, ',')) == NULL)
8917c478bd9Sstevel@tonic-gate 				break;
8927c478bd9Sstevel@tonic-gate 		}
8937c478bd9Sstevel@tonic-gate 		if (*s)
8947c478bd9Sstevel@tonic-gate 			s++;
8957c478bd9Sstevel@tonic-gate 	}
8967c478bd9Sstevel@tonic-gate 	return (0);
8977c478bd9Sstevel@tonic-gate }
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate /*
9007c478bd9Sstevel@tonic-gate  *	char *
9017c478bd9Sstevel@tonic-gate  *	fdig(cp)	find first digit in string
9027c478bd9Sstevel@tonic-gate  *
9037c478bd9Sstevel@tonic-gate  *	return - pointer to first digit in string or end of string
9047c478bd9Sstevel@tonic-gate  */
9057c478bd9Sstevel@tonic-gate 
90661961e0fSrobinson static char *
90761961e0fSrobinson fdig(char *cp)
9087c478bd9Sstevel@tonic-gate {
9097c478bd9Sstevel@tonic-gate 	char *c;
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	for (c = cp; *c; c++)
9127c478bd9Sstevel@tonic-gate 		if (*c >= '0' && *c <= '9')
9137c478bd9Sstevel@tonic-gate 			break;
9147c478bd9Sstevel@tonic-gate 	return (c);
9157c478bd9Sstevel@tonic-gate }
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	/* nap(n) -- sleep for 'n' ticks of 1/60th sec each. */
9187c478bd9Sstevel@tonic-gate 	/* This version uses the select system call */
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 
92161961e0fSrobinson static void
92261961e0fSrobinson nap(unsigned int n)
9237c478bd9Sstevel@tonic-gate {
9247c478bd9Sstevel@tonic-gate 	struct timeval tv;
9257c478bd9Sstevel@tonic-gate 
92661961e0fSrobinson 	if (n == 0)
9277c478bd9Sstevel@tonic-gate 		return;
9287c478bd9Sstevel@tonic-gate 	tv.tv_sec = n/60;
9297c478bd9Sstevel@tonic-gate 	tv.tv_usec = ((n%60)*1000000L)/60;
93061961e0fSrobinson 	(void) select(32, 0, 0, 0, &tv);
9317c478bd9Sstevel@tonic-gate }
932