xref: /titanic_44/usr/src/lib/libnsl/dial/conn.c (revision d67944fbe3fa0b31893a7116a09b0718eecf6078)
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
5*d67944fbSScott Rotondo  * Common Development and Distribution License (the "License").
6*d67944fbSScott Rotondo  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2161961e0fSrobinson 
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
26*d67944fbSScott Rotondo  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
277c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
30e8031f0aSraf #include "mt.h"
317c478bd9Sstevel@tonic-gate #include "uucp.h"
327c478bd9Sstevel@tonic-gate static char _ProtoSys[40];	/* protocol string from Systems file entry */
337c478bd9Sstevel@tonic-gate static char _ProtoDev[40];	/* protocol string from Devices file entry */
3461961e0fSrobinson static char _ProtoCfg[];	/* protocol string from Config  file entry */
357c478bd9Sstevel@tonic-gate 
3661961e0fSrobinson static jmp_buf Sjbuf;
3761961e0fSrobinson static unsigned expecttime;
387c478bd9Sstevel@tonic-gate 
3961961e0fSrobinson static int	Modemctrl;
407c478bd9Sstevel@tonic-gate 
4161961e0fSrobinson static void alarmtr(int);
4261961e0fSrobinson static void getProto(char *, char *);
4361961e0fSrobinson static int finds(char *, char *[], int);
4461961e0fSrobinson static int getto(char *[]);	/* make this static when ct uses altconn() */
4561961e0fSrobinson static int chat(int, char *[], int, char *, char *);
4661961e0fSrobinson static int rddev(char *, char *[], char *, int);
4761961e0fSrobinson static int expect(char *, int);
4861961e0fSrobinson static int wrstr(int, char *, int, int);
4961961e0fSrobinson static int wrchr(int, char *, int);
5061961e0fSrobinson static int processdev(char *[], char *[]);
5161961e0fSrobinson static int getdevline(char *, int);
5261961e0fSrobinson static int getsysline(char *, int);
5361961e0fSrobinson static int sysaccess(int);
5461961e0fSrobinson static int clear_hup(int);
5561961e0fSrobinson #ifndef SMALL
5661961e0fSrobinson static char *currsys(void);
5761961e0fSrobinson static char *currdev(void);
5861961e0fSrobinson #endif
5961961e0fSrobinson static int wait_for_hangup(int);
6061961e0fSrobinson static int expect_str(char *, int);
617c478bd9Sstevel@tonic-gate 
6261961e0fSrobinson static void sendthem(char *, int, char *, char *);
6361961e0fSrobinson static void nap(unsigned int);
6461961e0fSrobinson static int notin(char *, char *);
6561961e0fSrobinson static int ifdate(char *);
6661961e0fSrobinson static int classmatch(char *[], char *[]);
677c478bd9Sstevel@tonic-gate 
6861961e0fSrobinson static char *Myline = CNULL;	/* to force which line will be used */
6961961e0fSrobinson static char *Mytype = CNULL;	/* to force selection of specific device type */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  * conn - place a telephone call to system and login, etc.
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  * return codes:
757c478bd9Sstevel@tonic-gate  *	FAIL - connection failed
767c478bd9Sstevel@tonic-gate  *	>0  - file no.  -  connect ok
777c478bd9Sstevel@tonic-gate  * When a failure occurs, Uerror is set.
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate 
80*d67944fbSScott Rotondo static void sysreset(void);
8161961e0fSrobinson static int
conn(char * system)8261961e0fSrobinson conn(char *system)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	int nf, fn = FAIL;
857c478bd9Sstevel@tonic-gate 	char *flds[F_MAX+1];
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	CDEBUG(4, "conn(%s)\n", system);
887c478bd9Sstevel@tonic-gate 	Uerror = 0;
897c478bd9Sstevel@tonic-gate 	while ((nf = finds(system, flds, F_MAX)) > 0) {
907c478bd9Sstevel@tonic-gate 		fn = getto(flds);
917c478bd9Sstevel@tonic-gate 		CDEBUG(4, "getto ret %d\n", fn);
927c478bd9Sstevel@tonic-gate 		if (fn < 0)
937c478bd9Sstevel@tonic-gate 			continue;
947c478bd9Sstevel@tonic-gate 		if (EQUALS(Progname, "uucico")) {
9561961e0fSrobinson 			if (chat(nf - F_LOGIN, flds + F_LOGIN, fn, "", "") ==
9661961e0fSrobinson 			    SUCCESS) {
977c478bd9Sstevel@tonic-gate 				sysreset();
987c478bd9Sstevel@tonic-gate 				return (fn); /* successful return */
997c478bd9Sstevel@tonic-gate 			}
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 			/* login failed */
1027c478bd9Sstevel@tonic-gate 			DEBUG(6, "close caller (%d)\n", fn);
1037c478bd9Sstevel@tonic-gate 			fd_rmlock(fn);
10461961e0fSrobinson 			(void) close(fn);
1057c478bd9Sstevel@tonic-gate 			if (Dc[0] != NULLCHAR) {
1067c478bd9Sstevel@tonic-gate 				/*EMPTY*/
1077c478bd9Sstevel@tonic-gate 				DEBUG(6, "delock line (%s)\n", Dc);
1087c478bd9Sstevel@tonic-gate 			}
1097c478bd9Sstevel@tonic-gate 		} else {
1107c478bd9Sstevel@tonic-gate 			sysreset();
1117c478bd9Sstevel@tonic-gate 			return (fn);
1127c478bd9Sstevel@tonic-gate 		}
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	/* finds or getto failed */
1167c478bd9Sstevel@tonic-gate 	sysreset();
1177c478bd9Sstevel@tonic-gate 	CDEBUG(1, "Call Failed: %s\n", UERRORTEXT);
1187c478bd9Sstevel@tonic-gate 	return (FAIL);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
121*d67944fbSScott Rotondo static void devreset(void);
122*d67944fbSScott Rotondo 
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate  * getto - connect to remote machine
1257c478bd9Sstevel@tonic-gate  *
1267c478bd9Sstevel@tonic-gate  * return codes:
1277c478bd9Sstevel@tonic-gate  *	>0  -  file number - ok
1287c478bd9Sstevel@tonic-gate  *	FAIL  -  failed
1297c478bd9Sstevel@tonic-gate  */
1307c478bd9Sstevel@tonic-gate 
13161961e0fSrobinson static int
getto(char * flds[])13261961e0fSrobinson getto(char *flds[])
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 	char *dev[D_MAX+2], devbuf[BUFSIZ];
13561961e0fSrobinson 	int status;
13661961e0fSrobinson 	int dcf = -1;
1377c478bd9Sstevel@tonic-gate 	int reread = 0;
1387c478bd9Sstevel@tonic-gate 	int tries = 0;	/* count of call attempts - for limit purposes */
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	CDEBUG(1, "Device Type %s wanted\n", flds[F_TYPE]);
1417c478bd9Sstevel@tonic-gate 	Uerror = 0;
1427c478bd9Sstevel@tonic-gate 	while (tries < TRYCALLS) {
14361961e0fSrobinson 		if ((status = rddev(flds[F_TYPE], dev, devbuf, D_MAX)) ==
14461961e0fSrobinson 		    FAIL) {
1457c478bd9Sstevel@tonic-gate 			if (tries == 0 || ++reread >= TRYCALLS)
1467c478bd9Sstevel@tonic-gate 				break;
1477c478bd9Sstevel@tonic-gate 			devreset();
1487c478bd9Sstevel@tonic-gate 			continue;
1497c478bd9Sstevel@tonic-gate 		}
1507c478bd9Sstevel@tonic-gate 		/* check class, check (and possibly set) speed */
1517c478bd9Sstevel@tonic-gate 		if (classmatch(flds, dev) != SUCCESS) {
1527c478bd9Sstevel@tonic-gate 			DEBUG(7, "Skipping entry in '%s'", currdev());
1537c478bd9Sstevel@tonic-gate 			DEBUG(7, " - class (%s) not wanted.\n", dev[D_CLASS]);
1547c478bd9Sstevel@tonic-gate 			continue;
1557c478bd9Sstevel@tonic-gate 		}
1567c478bd9Sstevel@tonic-gate 		DEBUG(5, "Trying device entry from '%s'.\n", currdev());
1577c478bd9Sstevel@tonic-gate 		if ((dcf = processdev(flds, dev)) >= 0)
1587c478bd9Sstevel@tonic-gate 			break;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 		switch (Uerror) {
1617c478bd9Sstevel@tonic-gate 		case SS_CANT_ACCESS_DEVICE:
1627c478bd9Sstevel@tonic-gate 		case SS_DEVICE_FAILED:
1637c478bd9Sstevel@tonic-gate 		case SS_LOCKED_DEVICE:
1647c478bd9Sstevel@tonic-gate 			break;
1657c478bd9Sstevel@tonic-gate 		default:
1667c478bd9Sstevel@tonic-gate 			tries++;
1677c478bd9Sstevel@tonic-gate 			break;
1687c478bd9Sstevel@tonic-gate 		}
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 	devreset();	/* reset devices file(s) */
1717c478bd9Sstevel@tonic-gate 	if (status == FAIL && !Uerror) {
1727c478bd9Sstevel@tonic-gate 		CDEBUG(1, "Requested Device Type Not Found\n%s", "");
1737c478bd9Sstevel@tonic-gate 		Uerror = SS_NO_DEVICE;
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 	return (dcf);
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate /*
1797c478bd9Sstevel@tonic-gate  * classmatch - process 'Any' in Devices and Systems and
1807c478bd9Sstevel@tonic-gate  *	determine the correct speed, or match for ==
1817c478bd9Sstevel@tonic-gate  */
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate static int
classmatch(char * flds[],char * dev[])18461961e0fSrobinson classmatch(char *flds[], char *dev[])
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate 	/* check class, check (and possibly set) speed */
18761961e0fSrobinson 	if (EQUALS(flds[F_CLASS], "Any") && EQUALS(dev[D_CLASS], "Any")) {
1887c478bd9Sstevel@tonic-gate 		dev[D_CLASS] = DEFAULT_BAUDRATE;
1897c478bd9Sstevel@tonic-gate 		return (SUCCESS);
1907c478bd9Sstevel@tonic-gate 	} else if (EQUALS(dev[D_CLASS], "Any")) {
1917c478bd9Sstevel@tonic-gate 		dev[D_CLASS] = flds[F_CLASS];
1927c478bd9Sstevel@tonic-gate 		return (SUCCESS);
1937c478bd9Sstevel@tonic-gate 	} else if (EQUALS(flds[F_CLASS], "Any") ||
19461961e0fSrobinson 	    EQUALS(flds[F_CLASS], dev[D_CLASS]))
1957c478bd9Sstevel@tonic-gate 		return (SUCCESS);
19661961e0fSrobinson 	else
1977c478bd9Sstevel@tonic-gate 		return (FAIL);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate /*
2027c478bd9Sstevel@tonic-gate  *	rddev - find and unpack a line from device file for this caller type
2037c478bd9Sstevel@tonic-gate  *	lines starting with whitespace of '#' are comments
2047c478bd9Sstevel@tonic-gate  *
2057c478bd9Sstevel@tonic-gate  *	return codes:
2067c478bd9Sstevel@tonic-gate  *		>0  -  number of arguments in vector - succeeded
2077c478bd9Sstevel@tonic-gate  *		FAIL - EOF
2087c478bd9Sstevel@tonic-gate  */
2097c478bd9Sstevel@tonic-gate 
21061961e0fSrobinson static int
rddev(char * type,char * dev[],char * buf,int devcount)2117c478bd9Sstevel@tonic-gate rddev(char *type, char *dev[], char *buf, int devcount)
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	char *commap, d_type[BUFSIZ];
2147c478bd9Sstevel@tonic-gate 	int na;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	while (getdevline(buf, BUFSIZ)) {
21761961e0fSrobinson 		if (buf[0] == ' ' || buf[0] == '\t' || buf[0] == '\n' ||
21861961e0fSrobinson 		    buf[0] == '\0' || buf[0] == '#')
2197c478bd9Sstevel@tonic-gate 			continue;
2207c478bd9Sstevel@tonic-gate 		na = getargs(buf, dev, devcount);
2217c478bd9Sstevel@tonic-gate 		ASSERT(na >= D_CALLER, "BAD LINE", buf, na);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 		if (strncmp(dev[D_LINE], "/dev/", 5) == 0) {
2247c478bd9Sstevel@tonic-gate 			/* since cu (altconn()) strips off leading */
2257c478bd9Sstevel@tonic-gate 			/* "/dev/",  do the same here.  */
22661961e0fSrobinson 			(void) strcpy(dev[D_LINE], &(dev[D_LINE][5]));
2277c478bd9Sstevel@tonic-gate 		}
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 		/* may have ",M" subfield in D_LINE */
2307c478bd9Sstevel@tonic-gate 		Modemctrl = FALSE;
23161961e0fSrobinson 		if ((commap = strchr(dev[D_LINE], ',')) != NULL) {
2327c478bd9Sstevel@tonic-gate 			if (strcmp(commap, ",M") == SAME)
2337c478bd9Sstevel@tonic-gate 				Modemctrl = TRUE;
2347c478bd9Sstevel@tonic-gate 			*commap = '\0';
2357c478bd9Sstevel@tonic-gate 		}
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 		/*
2387c478bd9Sstevel@tonic-gate 		 * D_TYPE field may have protocol subfield, which
2397c478bd9Sstevel@tonic-gate 		 * must be pulled off before comparing to desired type.
2407c478bd9Sstevel@tonic-gate 		 */
2417c478bd9Sstevel@tonic-gate 		(void) strcpy(d_type, dev[D_TYPE]);
24261961e0fSrobinson 		if ((commap = strchr(d_type, ',')) != NULL)
2437c478bd9Sstevel@tonic-gate 			*commap = '\0';
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 		/* to force the requested device type to be used. */
2467c478bd9Sstevel@tonic-gate 		if ((Mytype != NULL) && (!EQUALS(Mytype, d_type)))
2477c478bd9Sstevel@tonic-gate 			continue;
2487c478bd9Sstevel@tonic-gate 		/* to force the requested line to be used */
2497c478bd9Sstevel@tonic-gate 		if ((Myline != NULL) && (!EQUALS(Myline, dev[D_LINE])))
2507c478bd9Sstevel@tonic-gate 			continue;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 		bsfix(dev);	/* replace \X fields */
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 		if (EQUALS(d_type, type)) {
2557c478bd9Sstevel@tonic-gate 			getProto(_ProtoDev, dev[D_TYPE]);
2567c478bd9Sstevel@tonic-gate 			return (na);
2577c478bd9Sstevel@tonic-gate 		}
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 	return (FAIL);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate /*
2647c478bd9Sstevel@tonic-gate  * finds	- set system attribute vector
2657c478bd9Sstevel@tonic-gate  *
2667c478bd9Sstevel@tonic-gate  * input:
2677c478bd9Sstevel@tonic-gate  *	fsys - open Systems file descriptor
2687c478bd9Sstevel@tonic-gate  *	sysnam - system name to find
2697c478bd9Sstevel@tonic-gate  * output:
2707c478bd9Sstevel@tonic-gate  *	flds - attibute vector from Systems file
2717c478bd9Sstevel@tonic-gate  *	fldcount - number of fields in flds
2727c478bd9Sstevel@tonic-gate  * return codes:
2737c478bd9Sstevel@tonic-gate  *	>0  -  number of arguments in vector - succeeded
2747c478bd9Sstevel@tonic-gate  *	FAIL - failed
2757c478bd9Sstevel@tonic-gate  * Uerror set:
2767c478bd9Sstevel@tonic-gate  *	0 - found a line in Systems file
2777c478bd9Sstevel@tonic-gate  *	SS_BADSYSTEM - no line found in Systems file
2787c478bd9Sstevel@tonic-gate  *	SS_TIME_WRONG - wrong time to call
2797c478bd9Sstevel@tonic-gate  */
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate static int
finds(char * sysnam,char * flds[],int fldcount)2827c478bd9Sstevel@tonic-gate finds(char *sysnam, char *flds[], int fldcount)
2837c478bd9Sstevel@tonic-gate {
2847c478bd9Sstevel@tonic-gate 	static char *info;	/* dynamically allocated BUFSIZ */
2857c478bd9Sstevel@tonic-gate 	int na;
2867c478bd9Sstevel@tonic-gate 
28761961e0fSrobinson 	/*
28861961e0fSrobinson 	 * format of fields
2897c478bd9Sstevel@tonic-gate 	 *	0 name;
2907c478bd9Sstevel@tonic-gate 	 *	1 time
2917c478bd9Sstevel@tonic-gate 	 *	2 acu/hardwired
2927c478bd9Sstevel@tonic-gate 	 *	3 speed
2937c478bd9Sstevel@tonic-gate 	 *	etc
2947c478bd9Sstevel@tonic-gate 	 */
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	if (sysnam == 0 || *sysnam == 0) {
2977c478bd9Sstevel@tonic-gate 		Uerror = SS_BADSYSTEM;
2987c478bd9Sstevel@tonic-gate 		return (FAIL);
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	if (info == NULL) {
30261961e0fSrobinson 		info = malloc(BUFSIZ);
3037c478bd9Sstevel@tonic-gate 		if (info == NULL) {
3047c478bd9Sstevel@tonic-gate 			DEBUG(1, "malloc failed for info in finds\n", 0);
3057c478bd9Sstevel@tonic-gate 			return (0);
3067c478bd9Sstevel@tonic-gate 		}
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 	while (getsysline(info, BUFSIZ)) {
3097c478bd9Sstevel@tonic-gate 		na = getargs(info, flds, fldcount);
3107c478bd9Sstevel@tonic-gate 		bsfix(flds);	/* replace \X fields */
3117c478bd9Sstevel@tonic-gate 		if (!EQUALSN(sysnam, flds[F_NAME], MAXBASENAME))
3127c478bd9Sstevel@tonic-gate 			continue;
3137c478bd9Sstevel@tonic-gate 		/* check if requested Mytype device type */
31461961e0fSrobinson 		if ((Mytype != CNULL) &&
31561961e0fSrobinson 		    (!EQUALSN(flds[F_TYPE], Mytype, strlen(Mytype)))) {
3167c478bd9Sstevel@tonic-gate 			DEBUG(7, "Skipping entry in '%s'", currsys());
3177c478bd9Sstevel@tonic-gate 			DEBUG(7, " - type (%s) not wanted.\n", flds[F_TYPE]);
3187c478bd9Sstevel@tonic-gate 			continue;
3197c478bd9Sstevel@tonic-gate 		} else {
3207c478bd9Sstevel@tonic-gate 			/*EMPTY*/
3217c478bd9Sstevel@tonic-gate 			DEBUG(5, "Trying entry from '%s'", currsys());
3227c478bd9Sstevel@tonic-gate 			DEBUG(5, " - device type %s.\n", flds[F_TYPE]);
3237c478bd9Sstevel@tonic-gate 		}
3247c478bd9Sstevel@tonic-gate 		/* OK if not uucico (ie. ct or cu) or the time is right */
3257c478bd9Sstevel@tonic-gate 		if (!EQUALS(Progname, "uucico") || ifdate(flds[F_TIME])) {
3267c478bd9Sstevel@tonic-gate 			/*  found a good entry  */
3277c478bd9Sstevel@tonic-gate 			getProto(_ProtoSys, flds[F_TYPE]);
3287c478bd9Sstevel@tonic-gate 			Uerror = 0;
3297c478bd9Sstevel@tonic-gate 			return (na);	/* FOUND OK LINE */
3307c478bd9Sstevel@tonic-gate 		}
3317c478bd9Sstevel@tonic-gate 		CDEBUG(1, "Wrong Time To Call: %s\n", flds[F_TIME]);
3327c478bd9Sstevel@tonic-gate 		Uerror = SS_TIME_WRONG;
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 	if (!Uerror)
3357c478bd9Sstevel@tonic-gate 		Uerror = SS_BADSYSTEM;
3367c478bd9Sstevel@tonic-gate 	return (FAIL);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate /*
3407c478bd9Sstevel@tonic-gate  * getProto - get the protocol letters from the input string.
3417c478bd9Sstevel@tonic-gate  * input:
3427c478bd9Sstevel@tonic-gate  *	str - string from Systems/Devices/Config file,
3437c478bd9Sstevel@tonic-gate  *		a ',' delimits the protocol string
3447c478bd9Sstevel@tonic-gate  *		e.g. ACU,g or DK,d
3457c478bd9Sstevel@tonic-gate  * output:
3467c478bd9Sstevel@tonic-gate  *	str - the , (if present) will be replaced with NULLCHAR
3477c478bd9Sstevel@tonic-gate  *
3487c478bd9Sstevel@tonic-gate  * return:  none
3497c478bd9Sstevel@tonic-gate  */
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate static void
getProto(char * save,char * str)35261961e0fSrobinson getProto(char *save, char *str)
3537c478bd9Sstevel@tonic-gate {
35461961e0fSrobinson 	char *p;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	*save = NULLCHAR;
3577c478bd9Sstevel@tonic-gate 	if ((p = strchr(str, ',')) != NULL) {
3587c478bd9Sstevel@tonic-gate 		*p = NULLCHAR;
3597c478bd9Sstevel@tonic-gate 		(void) strcpy(save, p+1);
3607c478bd9Sstevel@tonic-gate 		DEBUG(7, "Protocol = %s\n", save);
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate  * chat -	do conversation
3667c478bd9Sstevel@tonic-gate  * input:
3677c478bd9Sstevel@tonic-gate  *	nf - number of fields in flds array
3687c478bd9Sstevel@tonic-gate  *	flds - fields from Systems file
3697c478bd9Sstevel@tonic-gate  *	fn - write file number
3707c478bd9Sstevel@tonic-gate  *	phstr1 - phone number to replace \D
3717c478bd9Sstevel@tonic-gate  *	phstr2 - phone number to replace \T
3727c478bd9Sstevel@tonic-gate  *
3737c478bd9Sstevel@tonic-gate  *	return codes:  0  |  FAIL
3747c478bd9Sstevel@tonic-gate  */
3757c478bd9Sstevel@tonic-gate 
37661961e0fSrobinson static int
chat(int nf,char * flds[],int fn,char * phstr1,char * phstr2)37761961e0fSrobinson chat(int nf, char *flds[], int fn, char *phstr1, char *phstr2)
3787c478bd9Sstevel@tonic-gate {
3797c478bd9Sstevel@tonic-gate 	char *want, *altern;
3807c478bd9Sstevel@tonic-gate 	int k, ok;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	for (k = 0; k < nf; k += 2) {
3837c478bd9Sstevel@tonic-gate 		want = flds[k];
3847c478bd9Sstevel@tonic-gate 		ok = FAIL;
3857c478bd9Sstevel@tonic-gate 		while (ok != 0) {
3867c478bd9Sstevel@tonic-gate 			altern = index(want, '-');
3877c478bd9Sstevel@tonic-gate 			if (altern != NULL)
3887c478bd9Sstevel@tonic-gate 				*altern++ = NULLCHAR;
3897c478bd9Sstevel@tonic-gate 			ok = expect(want, fn);
3907c478bd9Sstevel@tonic-gate 			if (ok == 0)
3917c478bd9Sstevel@tonic-gate 				break;
3927c478bd9Sstevel@tonic-gate 			if (altern == NULL) {
3937c478bd9Sstevel@tonic-gate 				Uerror = SS_LOGIN_FAILED;
3947c478bd9Sstevel@tonic-gate 				logent(UERRORTEXT, "FAILED");
3957c478bd9Sstevel@tonic-gate 				return (FAIL);
3967c478bd9Sstevel@tonic-gate 			}
3977c478bd9Sstevel@tonic-gate 			want = index(altern, '-');
3987c478bd9Sstevel@tonic-gate 			if (want != NULL)
3997c478bd9Sstevel@tonic-gate 				*want++ = NULLCHAR;
4007c478bd9Sstevel@tonic-gate 			sendthem(altern, fn, phstr1, phstr2);
4017c478bd9Sstevel@tonic-gate 		}
40261961e0fSrobinson 		(void) sleep(2);
4037c478bd9Sstevel@tonic-gate 		if (flds[k+1])
4047c478bd9Sstevel@tonic-gate 			sendthem(flds[k+1], fn, phstr1, phstr2);
4057c478bd9Sstevel@tonic-gate 	}
4067c478bd9Sstevel@tonic-gate 	return (0);
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate #define	MR 1000
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate /*
4127c478bd9Sstevel@tonic-gate  *	expect(str, fn)	look for expected string w/ possible special chars
4137c478bd9Sstevel@tonic-gate  *	char *str;
4147c478bd9Sstevel@tonic-gate  *
4157c478bd9Sstevel@tonic-gate  *	return codes:
4167c478bd9Sstevel@tonic-gate  *		0  -  found
4177c478bd9Sstevel@tonic-gate  *		FAIL  -  lost line or too many characters read
4187c478bd9Sstevel@tonic-gate  *		some character  -  timed out
4197c478bd9Sstevel@tonic-gate  */
4207c478bd9Sstevel@tonic-gate 
42161961e0fSrobinson static int
expect(char * str,int fn)42261961e0fSrobinson expect(char *str, int fn)
4237c478bd9Sstevel@tonic-gate {
42461961e0fSrobinson 	char *bptr, *sptr;
4257c478bd9Sstevel@tonic-gate 	char    buf[BUFSIZ];
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	bptr = buf;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	for (sptr = str; *sptr; sptr++) {
4307c478bd9Sstevel@tonic-gate 		if (*sptr == '\\') {
4317c478bd9Sstevel@tonic-gate 			switch (*++sptr) {
4327c478bd9Sstevel@tonic-gate 			case 'H':
4337c478bd9Sstevel@tonic-gate 				*bptr++ = '\0';
4347c478bd9Sstevel@tonic-gate 				if (expect_str(buf, fn) == FAIL) {
4357c478bd9Sstevel@tonic-gate 					return (FAIL);
4367c478bd9Sstevel@tonic-gate 				}
4377c478bd9Sstevel@tonic-gate 				if (wait_for_hangup(fn) == FAIL) {
4387c478bd9Sstevel@tonic-gate 					return (FAIL);
4397c478bd9Sstevel@tonic-gate 				}
4407c478bd9Sstevel@tonic-gate 				bptr = buf;
4417c478bd9Sstevel@tonic-gate 				continue;
4427c478bd9Sstevel@tonic-gate 			case '\\':
4437c478bd9Sstevel@tonic-gate 				*bptr++ = '\\';
4447c478bd9Sstevel@tonic-gate 				continue;
4457c478bd9Sstevel@tonic-gate 			default:
4467c478bd9Sstevel@tonic-gate 				*bptr++ = '\\';
4477c478bd9Sstevel@tonic-gate 				*bptr++ = *sptr;
4487c478bd9Sstevel@tonic-gate 				continue;
4497c478bd9Sstevel@tonic-gate 			}
4507c478bd9Sstevel@tonic-gate 		} else
4517c478bd9Sstevel@tonic-gate 			*bptr++ = *sptr;
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate 	*bptr = '\0';
4547c478bd9Sstevel@tonic-gate 	if (expect_str(buf, fn) == FAIL) {
4557c478bd9Sstevel@tonic-gate 		return (FAIL);
4567c478bd9Sstevel@tonic-gate 	}
4577c478bd9Sstevel@tonic-gate 	return (0);
4587c478bd9Sstevel@tonic-gate }
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate /*
4617c478bd9Sstevel@tonic-gate  *	expect_str(str, fn)	look for expected string, w/ no special chars
4627c478bd9Sstevel@tonic-gate  *
4637c478bd9Sstevel@tonic-gate  *	return codes:
4647c478bd9Sstevel@tonic-gate  *		0  -  found
4657c478bd9Sstevel@tonic-gate  *		FAIL  -  too many characters read
4667c478bd9Sstevel@tonic-gate  *		some character  -  timed out
4677c478bd9Sstevel@tonic-gate  */
4687c478bd9Sstevel@tonic-gate 
46961961e0fSrobinson static int
expect_str(char * str,int fn)47061961e0fSrobinson expect_str(char *str, int fn)
4717c478bd9Sstevel@tonic-gate {
4727c478bd9Sstevel@tonic-gate 	static char rdvec[MR];
4737c478bd9Sstevel@tonic-gate 	char *rp = rdvec;
47461961e0fSrobinson 	int kr, c;
4757c478bd9Sstevel@tonic-gate 	char nextch;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	*rp = 0;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	CDEBUG(4, "expect: (%s", "");
4807c478bd9Sstevel@tonic-gate 	for (c = 0; (kr = str[c]) != 0; c++)
4817c478bd9Sstevel@tonic-gate 		if (kr < 040) {
4827c478bd9Sstevel@tonic-gate 			/*EMPTY*/
4837c478bd9Sstevel@tonic-gate 			CDEBUG(4, "^%c", kr | 0100);
4847c478bd9Sstevel@tonic-gate 		} else {
4857c478bd9Sstevel@tonic-gate 			/*EMPTY*/
4867c478bd9Sstevel@tonic-gate 			CDEBUG(4, "%c", kr);
4877c478bd9Sstevel@tonic-gate 		}
4887c478bd9Sstevel@tonic-gate 	CDEBUG(4, ")\n%s", "");
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	if (EQUALS(str, "\"\"")) {
4917c478bd9Sstevel@tonic-gate 		CDEBUG(4, "got it\n%s", "");
4927c478bd9Sstevel@tonic-gate 		return (0);
4937c478bd9Sstevel@tonic-gate 	}
49461961e0fSrobinson 	if (*str == '\0')
4957c478bd9Sstevel@tonic-gate 		return (0);
49661961e0fSrobinson 	if (setjmp(Sjbuf))
4977c478bd9Sstevel@tonic-gate 		return (FAIL);
4987c478bd9Sstevel@tonic-gate 	(void) signal(SIGALRM, alarmtr);
49961961e0fSrobinson 	(void) alarm(expecttime);
5007c478bd9Sstevel@tonic-gate 	while (notin(str, rdvec)) {
5017c478bd9Sstevel@tonic-gate 		errno = 0;
5027c478bd9Sstevel@tonic-gate 		kr = (*Read)(fn, &nextch, 1);
5037c478bd9Sstevel@tonic-gate 		if (kr <= 0) {
50461961e0fSrobinson 			(void) alarm(0);
5057c478bd9Sstevel@tonic-gate 			CDEBUG(4, "lost line errno - %d\n", errno);
5067c478bd9Sstevel@tonic-gate 			logent("LOGIN", "LOST LINE");
5077c478bd9Sstevel@tonic-gate 			return (FAIL);
5087c478bd9Sstevel@tonic-gate 		}
5097c478bd9Sstevel@tonic-gate 		c = nextch & 0177;
5107c478bd9Sstevel@tonic-gate 		CDEBUG(4, "%s", c < 040 ? "^" : "");
5117c478bd9Sstevel@tonic-gate 		CDEBUG(4, "%c", c < 040 ? c | 0100 : c);
5127c478bd9Sstevel@tonic-gate 		if ((*rp = nextch & 0177) != NULLCHAR)
5137c478bd9Sstevel@tonic-gate 			rp++;
5147c478bd9Sstevel@tonic-gate 		if (rp >= rdvec + MR) {
5157c478bd9Sstevel@tonic-gate 			CDEBUG(4, "enough already\n%s", "");
51661961e0fSrobinson 			(void) alarm(0);
5177c478bd9Sstevel@tonic-gate 			return (FAIL);
5187c478bd9Sstevel@tonic-gate 		}
5197c478bd9Sstevel@tonic-gate 		*rp = NULLCHAR;
5207c478bd9Sstevel@tonic-gate 	}
52161961e0fSrobinson 	(void) alarm(0);
5227c478bd9Sstevel@tonic-gate 	CDEBUG(4, "got it\n%s", "");
5237c478bd9Sstevel@tonic-gate 	return (0);
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate /*
5287c478bd9Sstevel@tonic-gate  *	alarmtr()  -  catch alarm routine for "expect".
5297c478bd9Sstevel@tonic-gate  */
5307c478bd9Sstevel@tonic-gate /*ARGSUSED*/
53161961e0fSrobinson static void
alarmtr(int sig)53261961e0fSrobinson alarmtr(int sig)
5337c478bd9Sstevel@tonic-gate {
5347c478bd9Sstevel@tonic-gate 	CDEBUG(6, "timed out\n%s", "");
5357c478bd9Sstevel@tonic-gate 	longjmp(Sjbuf, 1);
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate /*
5397c478bd9Sstevel@tonic-gate  *	wait_for_hangup() - wait for a hangup to occur on the given device
5407c478bd9Sstevel@tonic-gate  */
5417c478bd9Sstevel@tonic-gate int
wait_for_hangup(int dcf)54261961e0fSrobinson wait_for_hangup(int dcf)
5437c478bd9Sstevel@tonic-gate {
5447c478bd9Sstevel@tonic-gate 	int rval;
5457c478bd9Sstevel@tonic-gate 	char buff[BUFSIZ];
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	CDEBUG(4, "Waiting for hangup\n%s", "");
54861961e0fSrobinson 	while ((rval = read(dcf, buff, BUFSIZ)) > 0)
54961961e0fSrobinson 		;
5507c478bd9Sstevel@tonic-gate 
55161961e0fSrobinson 	if (rval < 0)
5527c478bd9Sstevel@tonic-gate 		return (FAIL);
5537c478bd9Sstevel@tonic-gate 	CDEBUG(4, "Received hangup\n%s", "");
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	if (clear_hup(dcf) != SUCCESS) {
5567c478bd9Sstevel@tonic-gate 		CDEBUG(4, "Unable to clear hup on device\n%s", "");
5577c478bd9Sstevel@tonic-gate 		return (FAIL);
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 	return (SUCCESS);
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate /*
5637c478bd9Sstevel@tonic-gate  *	sendthem(str, fn, phstr1, phstr2)	send line of chat sequence
5647c478bd9Sstevel@tonic-gate  *	char *str, *phstr;
5657c478bd9Sstevel@tonic-gate  *
5667c478bd9Sstevel@tonic-gate  *	return codes:  none
5677c478bd9Sstevel@tonic-gate  */
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate #define	FLUSH() { \
5707c478bd9Sstevel@tonic-gate 	if ((bptr - buf) > 0) \
5717c478bd9Sstevel@tonic-gate 		if (wrstr(fn, buf, bptr - buf, echocheck) != SUCCESS) \
5727c478bd9Sstevel@tonic-gate 			goto err; \
5737c478bd9Sstevel@tonic-gate 	bptr = buf; \
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate 
57661961e0fSrobinson static void
sendthem(char * str,int fn,char * phstr1,char * phstr2)57761961e0fSrobinson sendthem(char *str, int fn, char *phstr1, char *phstr2)
5787c478bd9Sstevel@tonic-gate {
5797c478bd9Sstevel@tonic-gate 	int sendcr = 1, echocheck = 0;
58061961e0fSrobinson 	char	*sptr, *bptr;
5817c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ];
5827c478bd9Sstevel@tonic-gate 	struct termio	ttybuf;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	/* should be EQUALS, but previous versions had BREAK n for integer n */
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	if (PREFIX("BREAK", str)) {
5877c478bd9Sstevel@tonic-gate 		/* send break */
5887c478bd9Sstevel@tonic-gate 		CDEBUG(5, "BREAK\n%s", "");
5897c478bd9Sstevel@tonic-gate 		(*genbrk)(fn);
5907c478bd9Sstevel@tonic-gate 		return;
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	if (EQUALS(str, "EOT")) {
5947c478bd9Sstevel@tonic-gate 		CDEBUG(5, "EOT\n%s", "");
5957c478bd9Sstevel@tonic-gate 		(void) (*Write)(fn, EOTMSG, strlen(EOTMSG));
5967c478bd9Sstevel@tonic-gate 		return;
5977c478bd9Sstevel@tonic-gate 	}
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	if (EQUALS(str, "\"\"")) {
6007c478bd9Sstevel@tonic-gate 		CDEBUG(5, "\"\"\n%s", "");
6017c478bd9Sstevel@tonic-gate 		str += 2;
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	bptr = buf;
6057c478bd9Sstevel@tonic-gate 	CDEBUG(5, "sendthem (%s", "");
6067c478bd9Sstevel@tonic-gate 	for (sptr = str; *sptr; sptr++) {
6077c478bd9Sstevel@tonic-gate 		if (*sptr == '\\') {
6087c478bd9Sstevel@tonic-gate 			switch (*++sptr) {
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 			/* adjust switches */
6117c478bd9Sstevel@tonic-gate 			case 'c':	/* no CR after string */
6127c478bd9Sstevel@tonic-gate 				FLUSH();
6137c478bd9Sstevel@tonic-gate 				if (sptr[1] == NULLCHAR) {
6147c478bd9Sstevel@tonic-gate 					CDEBUG(5, "<NO CR>%s", "");
6157c478bd9Sstevel@tonic-gate 					sendcr = 0;
6167c478bd9Sstevel@tonic-gate 				} else {
6177c478bd9Sstevel@tonic-gate 					/*EMPTY*/
6187c478bd9Sstevel@tonic-gate 					CDEBUG(5, "<NO CR IGNORED>\n%s", "");
6197c478bd9Sstevel@tonic-gate 				}
6207c478bd9Sstevel@tonic-gate 				continue;
6217c478bd9Sstevel@tonic-gate 			}
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 			/* stash in buf and continue */
6247c478bd9Sstevel@tonic-gate 			switch (*sptr) {
6257c478bd9Sstevel@tonic-gate 			case 'D':	/* raw phnum */
62661961e0fSrobinson 				(void) strcpy(bptr, phstr1);
6277c478bd9Sstevel@tonic-gate 				bptr += strlen(bptr);
6287c478bd9Sstevel@tonic-gate 				continue;
6297c478bd9Sstevel@tonic-gate 			case 'T':	/* translated phnum */
63061961e0fSrobinson 				(void) strcpy(bptr, phstr2);
6317c478bd9Sstevel@tonic-gate 				bptr += strlen(bptr);
6327c478bd9Sstevel@tonic-gate 				continue;
6337c478bd9Sstevel@tonic-gate 			case 'N':	/* null */
6347c478bd9Sstevel@tonic-gate 				*bptr++ = 0;
6357c478bd9Sstevel@tonic-gate 				continue;
6367c478bd9Sstevel@tonic-gate 			case 's':	/* space */
6377c478bd9Sstevel@tonic-gate 				*bptr++ = ' ';
6387c478bd9Sstevel@tonic-gate 				continue;
6397c478bd9Sstevel@tonic-gate 			case '\\':	/* backslash escapes itself */
6407c478bd9Sstevel@tonic-gate 				*bptr++ = *sptr;
6417c478bd9Sstevel@tonic-gate 				continue;
6427c478bd9Sstevel@tonic-gate 			default:	/* send the backslash */
6437c478bd9Sstevel@tonic-gate 				*bptr++ = '\\';
6447c478bd9Sstevel@tonic-gate 				*bptr++ = *sptr;
6457c478bd9Sstevel@tonic-gate 				continue;
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 			/* flush buf, perform action, and continue */
6487c478bd9Sstevel@tonic-gate 			case 'E':	/* echo check on */
6497c478bd9Sstevel@tonic-gate 				FLUSH();
6507c478bd9Sstevel@tonic-gate 				CDEBUG(5, "ECHO CHECK ON\n%s", "");
6517c478bd9Sstevel@tonic-gate 				echocheck = 1;
6527c478bd9Sstevel@tonic-gate 				continue;
6537c478bd9Sstevel@tonic-gate 			case 'e':	/* echo check off */
6547c478bd9Sstevel@tonic-gate 				FLUSH();
6557c478bd9Sstevel@tonic-gate 				CDEBUG(5, "ECHO CHECK OFF\n%s", "");
6567c478bd9Sstevel@tonic-gate 				echocheck = 0;
6577c478bd9Sstevel@tonic-gate 				continue;
6587c478bd9Sstevel@tonic-gate 			case 'd':	/* sleep briefly */
6597c478bd9Sstevel@tonic-gate 				FLUSH();
6607c478bd9Sstevel@tonic-gate 				CDEBUG(5, "DELAY\n%s", "");
66161961e0fSrobinson 				(void) sleep(2);
6627c478bd9Sstevel@tonic-gate 				continue;
6637c478bd9Sstevel@tonic-gate 			case 'p':	/* pause momentarily */
6647c478bd9Sstevel@tonic-gate 				FLUSH();
6657c478bd9Sstevel@tonic-gate 				CDEBUG(5, "PAUSE\n%s", "");
6667c478bd9Sstevel@tonic-gate 				nap(HZ/4);	/* approximately 1/4 second */
6677c478bd9Sstevel@tonic-gate 				continue;
6687c478bd9Sstevel@tonic-gate 			case 'K':	/* inline break */
6697c478bd9Sstevel@tonic-gate 				FLUSH();
6707c478bd9Sstevel@tonic-gate 				CDEBUG(5, "BREAK\n%s", "");
6717c478bd9Sstevel@tonic-gate 				(*genbrk)(fn);
6727c478bd9Sstevel@tonic-gate 				continue;
6737c478bd9Sstevel@tonic-gate 			case 'M':	/* modem control - set CLOCAL */
6747c478bd9Sstevel@tonic-gate 			case 'm':	/* no modem control - clear CLOCAL */
6757c478bd9Sstevel@tonic-gate 				FLUSH();
6767c478bd9Sstevel@tonic-gate 				CDEBUG(5, ")\n%s CLOCAL ",
6777c478bd9Sstevel@tonic-gate 				    (*sptr == 'M' ? "set" : "clear"));
6787c478bd9Sstevel@tonic-gate 				if ((*Ioctl)(fn, TCGETA, &ttybuf) != 0) {
6797c478bd9Sstevel@tonic-gate 					/*EMPTY*/
68061961e0fSrobinson 					CDEBUG(5,
68161961e0fSrobinson 					    "ignored. TCGETA failed, errno %d",
68261961e0fSrobinson 					    errno);
6837c478bd9Sstevel@tonic-gate 				} else {
6847c478bd9Sstevel@tonic-gate 					if (*sptr == 'M')
6857c478bd9Sstevel@tonic-gate 					ttybuf.c_cflag |= CLOCAL;
6867c478bd9Sstevel@tonic-gate 					else
6877c478bd9Sstevel@tonic-gate 					ttybuf.c_cflag &= ~CLOCAL;
6887c478bd9Sstevel@tonic-gate 					if ((*Ioctl)(fn, TCSETAW, &ttybuf) != 0)
6897c478bd9Sstevel@tonic-gate 						/*EMPTY*/
69061961e0fSrobinson 					CDEBUG(5,
69161961e0fSrobinson 					    "failed. TCSETAW failed, errno %d",
69261961e0fSrobinson 					    errno);
6937c478bd9Sstevel@tonic-gate 				}
6947c478bd9Sstevel@tonic-gate 				CDEBUG(5, "\n%s", "");
6957c478bd9Sstevel@tonic-gate 				continue;
6967c478bd9Sstevel@tonic-gate 			}
6977c478bd9Sstevel@tonic-gate 		} else
6987c478bd9Sstevel@tonic-gate 			*bptr++ = *sptr;
6997c478bd9Sstevel@tonic-gate 	}
7007c478bd9Sstevel@tonic-gate 	if (sendcr)
7017c478bd9Sstevel@tonic-gate 		*bptr++ = '\r';
7027c478bd9Sstevel@tonic-gate 	if ((bptr - buf) > 0)
7037c478bd9Sstevel@tonic-gate 		(void) wrstr(fn, buf, bptr - buf, echocheck);
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate err:
7067c478bd9Sstevel@tonic-gate 	CDEBUG(5, ")\n%s", "");
7077c478bd9Sstevel@tonic-gate }
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate #undef FLUSH
7107c478bd9Sstevel@tonic-gate 
71161961e0fSrobinson static int
wrstr(int fn,char * buf,int len,int echocheck)7127c478bd9Sstevel@tonic-gate wrstr(int fn, char *buf, int len, int echocheck)
7137c478bd9Sstevel@tonic-gate {
7147c478bd9Sstevel@tonic-gate 	int	i;
7157c478bd9Sstevel@tonic-gate 	char dbuf[BUFSIZ], *dbptr = dbuf;
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	buf[len] = 0;
7187c478bd9Sstevel@tonic-gate 
71961961e0fSrobinson 	if (echocheck)
72061961e0fSrobinson 		return (wrchr(fn, buf, len));
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	if (Debug >= 5) {
72361961e0fSrobinson 		if (sysaccess(ACCESS_SYSTEMS) == 0) {
72461961e0fSrobinson 			/* Systems file access ok */
7257c478bd9Sstevel@tonic-gate 			for (i = 0; i < len; i++) {
7267c478bd9Sstevel@tonic-gate 				*dbptr = buf[i];
7277c478bd9Sstevel@tonic-gate 				if (*dbptr < 040) {
7287c478bd9Sstevel@tonic-gate 					*dbptr++ = '^';
7297c478bd9Sstevel@tonic-gate 					*dbptr = buf[i] | 0100;
7307c478bd9Sstevel@tonic-gate 				}
7317c478bd9Sstevel@tonic-gate 				dbptr++;
7327c478bd9Sstevel@tonic-gate 			}
7337c478bd9Sstevel@tonic-gate 			*dbptr = 0;
7347c478bd9Sstevel@tonic-gate 		} else
73561961e0fSrobinson 			(void) strcpy(dbuf, "????????");
7367c478bd9Sstevel@tonic-gate 		CDEBUG(5, "%s", dbuf);
7377c478bd9Sstevel@tonic-gate 	}
73861961e0fSrobinson 	if ((*Write)(fn, buf, len) != len)
7397c478bd9Sstevel@tonic-gate 		return (FAIL);
7407c478bd9Sstevel@tonic-gate 	return (SUCCESS);
7417c478bd9Sstevel@tonic-gate }
7427c478bd9Sstevel@tonic-gate 
74361961e0fSrobinson static int
wrchr(int fn,char * buf,int len)7447c478bd9Sstevel@tonic-gate wrchr(int fn, char *buf, int len)
7457c478bd9Sstevel@tonic-gate {
7467c478bd9Sstevel@tonic-gate 	int	i, saccess;
7477c478bd9Sstevel@tonic-gate 	char	cin, cout;
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	saccess = (sysaccess(ACCESS_SYSTEMS) == 0); /* protect Systems file */
75061961e0fSrobinson 	if (setjmp(Sjbuf))
7517c478bd9Sstevel@tonic-gate 		return (FAIL);
7527c478bd9Sstevel@tonic-gate 	(void) signal(SIGALRM, alarmtr);
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++) {
7557c478bd9Sstevel@tonic-gate 		cout = buf[i];
7567c478bd9Sstevel@tonic-gate 		if (saccess) {
7577c478bd9Sstevel@tonic-gate 			/*EMPTY*/
7587c478bd9Sstevel@tonic-gate 			CDEBUG(5, "%s", cout < 040 ? "^" : "");
7597c478bd9Sstevel@tonic-gate 			CDEBUG(5, "%c", cout < 040 ? cout | 0100 : cout);
7607c478bd9Sstevel@tonic-gate 		} else {
7617c478bd9Sstevel@tonic-gate 			/*EMPTY*/
7627c478bd9Sstevel@tonic-gate 			CDEBUG(5, "?%s", "");
7637c478bd9Sstevel@tonic-gate 		}
76461961e0fSrobinson 		if (((*Write)(fn, &cout, 1)) != 1)
7657c478bd9Sstevel@tonic-gate 			return (FAIL);
7667c478bd9Sstevel@tonic-gate 		do {
7677c478bd9Sstevel@tonic-gate 			(void) alarm(expecttime);
76861961e0fSrobinson 			if ((*Read)(fn, &cin, 1) != 1)
7697c478bd9Sstevel@tonic-gate 				return (FAIL);
7707c478bd9Sstevel@tonic-gate 			(void) alarm(0);
7717c478bd9Sstevel@tonic-gate 			cin &= 0177;
7727c478bd9Sstevel@tonic-gate 			if (saccess) {
7737c478bd9Sstevel@tonic-gate 				/*EMPTY*/
7747c478bd9Sstevel@tonic-gate 				CDEBUG(5, "%s", cin < 040 ? "^" : "");
7757c478bd9Sstevel@tonic-gate 				CDEBUG(5, "%c", cin < 040 ? cin | 0100 : cin);
7767c478bd9Sstevel@tonic-gate 			} else {
7777c478bd9Sstevel@tonic-gate 				/*EMPTY*/
7787c478bd9Sstevel@tonic-gate 				CDEBUG(5, "?%s", "");
7797c478bd9Sstevel@tonic-gate 			}
7807c478bd9Sstevel@tonic-gate 		} while (cout != (cin & 0177));
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 	return (SUCCESS);
7837c478bd9Sstevel@tonic-gate }
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate /*
7877c478bd9Sstevel@tonic-gate  *	notin(sh, lg)	check for occurrence of substring "sh"
7887c478bd9Sstevel@tonic-gate  *	char *sh, *lg;
7897c478bd9Sstevel@tonic-gate  *
7907c478bd9Sstevel@tonic-gate  *	return codes:
7917c478bd9Sstevel@tonic-gate  *		0  -  found the string
7927c478bd9Sstevel@tonic-gate  *		1  -  not in the string
7937c478bd9Sstevel@tonic-gate  */
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate static int
notin(char * sh,char * lg)79661961e0fSrobinson notin(char *sh, char *lg)
7977c478bd9Sstevel@tonic-gate {
7987c478bd9Sstevel@tonic-gate 	while (*lg != NULLCHAR) {
79961961e0fSrobinson 		if (PREFIX(sh, lg))
8007c478bd9Sstevel@tonic-gate 			return (0);
8017c478bd9Sstevel@tonic-gate 		else
8027c478bd9Sstevel@tonic-gate 			lg++;
8037c478bd9Sstevel@tonic-gate 	}
8047c478bd9Sstevel@tonic-gate 	return (1);
8057c478bd9Sstevel@tonic-gate }
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate /*
8097c478bd9Sstevel@tonic-gate  *	ifdate(s)
8107c478bd9Sstevel@tonic-gate  *	char *s;
8117c478bd9Sstevel@tonic-gate  *
8127c478bd9Sstevel@tonic-gate  *	ifdate  -  this routine will check a string (s)
8137c478bd9Sstevel@tonic-gate  *	like "MoTu0800-1730" to see if the present
8147c478bd9Sstevel@tonic-gate  *	time is within the given limits.
8157c478bd9Sstevel@tonic-gate  *	SIDE EFFECT - Retrytime is set to number following ";"
8167c478bd9Sstevel@tonic-gate  *
8177c478bd9Sstevel@tonic-gate  *	String alternatives:
8187c478bd9Sstevel@tonic-gate  *		Wk - Mo thru Fr
8197c478bd9Sstevel@tonic-gate  *		zero or one time means all day
8207c478bd9Sstevel@tonic-gate  *		Any - any day
8217c478bd9Sstevel@tonic-gate  *
8227c478bd9Sstevel@tonic-gate  *	return codes:
8237c478bd9Sstevel@tonic-gate  *		0  -  not within limits
8247c478bd9Sstevel@tonic-gate  *		1  -  within limits
8257c478bd9Sstevel@tonic-gate  */
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate static int
ifdate(char * s)82861961e0fSrobinson ifdate(char *s)
8297c478bd9Sstevel@tonic-gate {
8307c478bd9Sstevel@tonic-gate 	static char *days[] = {
8317c478bd9Sstevel@tonic-gate 		"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", 0
8327c478bd9Sstevel@tonic-gate 	};
8337c478bd9Sstevel@tonic-gate 	time_t	clock;
8347c478bd9Sstevel@tonic-gate 	int	t__now;
8357c478bd9Sstevel@tonic-gate 	char	*p;
8367c478bd9Sstevel@tonic-gate 	struct tm	*tp;
8377c478bd9Sstevel@tonic-gate 
83861961e0fSrobinson 	(void) time(&clock);
8397c478bd9Sstevel@tonic-gate 	tp = localtime(&clock);
8407c478bd9Sstevel@tonic-gate 	t__now = tp->tm_hour * 100 + tp->tm_min;	/* "navy" time */
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	/*
8437c478bd9Sstevel@tonic-gate 	 *	pick up retry time for failures
8447c478bd9Sstevel@tonic-gate 	 *	global variable Retrytime is set here
8457c478bd9Sstevel@tonic-gate 	 */
8467c478bd9Sstevel@tonic-gate 	if ((p = rindex(s, ';')) != NULL)
8477c478bd9Sstevel@tonic-gate 		if (isdigit(p[1])) {
8487c478bd9Sstevel@tonic-gate 		if (sscanf(p+1, "%ld", &Retrytime) < 1)
8497c478bd9Sstevel@tonic-gate 			Retrytime = 5;	/* 5 minutes is error default */
8507c478bd9Sstevel@tonic-gate 		Retrytime  *= 60;
8517c478bd9Sstevel@tonic-gate 		*p = NULLCHAR;
8527c478bd9Sstevel@tonic-gate 		}
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	while (*s) {
8557c478bd9Sstevel@tonic-gate 		int	i, dayok;
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 		for (dayok = 0; (!dayok) && isalpha(*s); s++) {
8587c478bd9Sstevel@tonic-gate 			if (PREFIX("Any", s))
8597c478bd9Sstevel@tonic-gate 				dayok = 1;
8607c478bd9Sstevel@tonic-gate 			else if (PREFIX("Wk", s)) {
8617c478bd9Sstevel@tonic-gate 				if (tp->tm_wday >= 1 && tp->tm_wday <= 5)
8627c478bd9Sstevel@tonic-gate 					dayok = 1;
8637c478bd9Sstevel@tonic-gate 			} else
8647c478bd9Sstevel@tonic-gate 				for (i = 0; days[i]; i++)
8657c478bd9Sstevel@tonic-gate 					if (PREFIX(days[i], s))
8667c478bd9Sstevel@tonic-gate 						if (tp->tm_wday == i)
8677c478bd9Sstevel@tonic-gate 							dayok = 1;
8687c478bd9Sstevel@tonic-gate 		}
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 		if (dayok) {
8717c478bd9Sstevel@tonic-gate 			int	t__low, t__high;
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 			while (isalpha(*s))	/* flush remaining day stuff */
8747c478bd9Sstevel@tonic-gate 				s++;
8757c478bd9Sstevel@tonic-gate 
87661961e0fSrobinson 			if ((sscanf(s, "%d-%d", &t__low, &t__high) < 2) ||
87761961e0fSrobinson 			    (t__low == t__high))
8787c478bd9Sstevel@tonic-gate 				return (1);
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 			/* 0000 crossover? */
8817c478bd9Sstevel@tonic-gate 			if (t__low < t__high) {
88261961e0fSrobinson 				if (t__low <= t__now && t__now <= t__high)
8837c478bd9Sstevel@tonic-gate 					return (1);
88461961e0fSrobinson 			} else if (t__low <= t__now || t__now <= t__high)
8857c478bd9Sstevel@tonic-gate 				return (1);
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 			/* aim at next time slot */
8887c478bd9Sstevel@tonic-gate 			if ((s = index(s, ',')) == NULL)
8897c478bd9Sstevel@tonic-gate 				break;
8907c478bd9Sstevel@tonic-gate 		}
8917c478bd9Sstevel@tonic-gate 		if (*s)
8927c478bd9Sstevel@tonic-gate 			s++;
8937c478bd9Sstevel@tonic-gate 	}
8947c478bd9Sstevel@tonic-gate 	return (0);
8957c478bd9Sstevel@tonic-gate }
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate /*
8987c478bd9Sstevel@tonic-gate  *	char *
8997c478bd9Sstevel@tonic-gate  *	fdig(cp)	find first digit in string
9007c478bd9Sstevel@tonic-gate  *
9017c478bd9Sstevel@tonic-gate  *	return - pointer to first digit in string or end of string
9027c478bd9Sstevel@tonic-gate  */
9037c478bd9Sstevel@tonic-gate 
90461961e0fSrobinson static char *
fdig(char * cp)90561961e0fSrobinson fdig(char *cp)
9067c478bd9Sstevel@tonic-gate {
9077c478bd9Sstevel@tonic-gate 	char *c;
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	for (c = cp; *c; c++)
9107c478bd9Sstevel@tonic-gate 		if (*c >= '0' && *c <= '9')
9117c478bd9Sstevel@tonic-gate 			break;
9127c478bd9Sstevel@tonic-gate 	return (c);
9137c478bd9Sstevel@tonic-gate }
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	/* nap(n) -- sleep for 'n' ticks of 1/60th sec each. */
9167c478bd9Sstevel@tonic-gate 	/* This version uses the select system call */
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 
91961961e0fSrobinson static void
nap(unsigned int n)92061961e0fSrobinson nap(unsigned int n)
9217c478bd9Sstevel@tonic-gate {
9227c478bd9Sstevel@tonic-gate 	struct timeval tv;
9237c478bd9Sstevel@tonic-gate 
92461961e0fSrobinson 	if (n == 0)
9257c478bd9Sstevel@tonic-gate 		return;
9267c478bd9Sstevel@tonic-gate 	tv.tv_sec = n/60;
9277c478bd9Sstevel@tonic-gate 	tv.tv_usec = ((n%60)*1000000L)/60;
92861961e0fSrobinson 	(void) select(32, 0, 0, 0, &tv);
9297c478bd9Sstevel@tonic-gate }
930