xref: /illumos-gate/usr/src/cmd/tip/remote.c (revision 1e094e1b)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
58d489c7aSmuffin 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
87c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
97c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
107c478bd9Sstevel@tonic-gate  */
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate #include "tip.h"
137c478bd9Sstevel@tonic-gate 
14*1e094e1bSToomas Soome char *DV;	/* UNIX device(s) to open */
15*1e094e1bSToomas Soome char *EL;	/* chars marking an EOL */
16*1e094e1bSToomas Soome char *CM;	/* initial connection message */
17*1e094e1bSToomas Soome char *IE;	/* EOT to expect on input */
18*1e094e1bSToomas Soome char *OE;	/* EOT to send to complete FT */
19*1e094e1bSToomas Soome char *CU;	/* call unit if making a phone call */
20*1e094e1bSToomas Soome char *AT;	/* acu type */
21*1e094e1bSToomas Soome char *PN;	/* phone number(s) */
22*1e094e1bSToomas Soome char *DI;	/* disconnect string */
23*1e094e1bSToomas Soome char *PA;	/* parity to be generated */
24*1e094e1bSToomas Soome 
25*1e094e1bSToomas Soome char *PH;	/* phone number file */
26*1e094e1bSToomas Soome char *RM;	/* remote file name */
27*1e094e1bSToomas Soome char *HO;	/* host name */
28*1e094e1bSToomas Soome 
29*1e094e1bSToomas Soome int BR;		/* line speed for conversation */
30*1e094e1bSToomas Soome int FS;		/* frame size for transfers */
31*1e094e1bSToomas Soome char DU;	/* this host is dialed up */
32*1e094e1bSToomas Soome char HW;	/* this device is hardwired, see hunt.c */
33*1e094e1bSToomas Soome char *ES;	/* escape character */
34*1e094e1bSToomas Soome char *EX;	/* exceptions */
35*1e094e1bSToomas Soome char *FO;	/* force (literal next) char */
36*1e094e1bSToomas Soome char *RC;	/* raise character */
37*1e094e1bSToomas Soome char *RE;	/* script record file */
38*1e094e1bSToomas Soome char *PR;	/* remote prompt */
39*1e094e1bSToomas Soome int DL;		/* line delay for file transfers to remote */
40*1e094e1bSToomas Soome int CL;		/* char delay for file transfers to remote */
41*1e094e1bSToomas Soome int ET;		/* echocheck timeout */
42*1e094e1bSToomas Soome int DB;		/* dialback - ignore hangup */
43*1e094e1bSToomas Soome 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * Attributes to be gleened from remote host description
467c478bd9Sstevel@tonic-gate  *   data base.
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate static char **caps[] = {
497c478bd9Sstevel@tonic-gate 	&AT, &DV, &CM, &CU, &EL, &IE, &OE, &PN, &PR, &DI,
507c478bd9Sstevel@tonic-gate 	&ES, &EX, &FO, &RC, &RE, &PA
517c478bd9Sstevel@tonic-gate };
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate static char *capstrings[] = {
547c478bd9Sstevel@tonic-gate 	"at", "dv", "cm", "cu", "el", "ie", "oe", "pn", "pr",
557c478bd9Sstevel@tonic-gate 	"di", "es", "ex", "fo", "rc", "re", "pa", 0
567c478bd9Sstevel@tonic-gate };
577c478bd9Sstevel@tonic-gate 
588d489c7aSmuffin extern char *rgetstr(char *, char **);
597c478bd9Sstevel@tonic-gate 
608d489c7aSmuffin static void
getremcap(char * host)618d489c7aSmuffin getremcap(char *host)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	int stat;
647c478bd9Sstevel@tonic-gate 	char tbuf[BUFSIZ];
657c478bd9Sstevel@tonic-gate 	static char buf[BUFSIZ/2];
667c478bd9Sstevel@tonic-gate 	char *bp = buf;
678d489c7aSmuffin 	char **p, ***q;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	if ((stat = rgetent(tbuf, host, sizeof (tbuf))) <= 0) {
707c478bd9Sstevel@tonic-gate 		if (DV ||
717c478bd9Sstevel@tonic-gate 		    host[0] == '/' && access(DV = host, R_OK | W_OK) == 0) {
727c478bd9Sstevel@tonic-gate 			/*
737c478bd9Sstevel@tonic-gate 			 * If the user specifies a device on the commandline,
747c478bd9Sstevel@tonic-gate 			 * don't trust it.
757c478bd9Sstevel@tonic-gate 			 */
767c478bd9Sstevel@tonic-gate 			if (host[0] == '/')
777c478bd9Sstevel@tonic-gate 				trusted_device = 0;
787c478bd9Sstevel@tonic-gate 			CU = DV;
797c478bd9Sstevel@tonic-gate 			HO = host;
807c478bd9Sstevel@tonic-gate 			HW = 1;
817c478bd9Sstevel@tonic-gate 			DU = 0;
827c478bd9Sstevel@tonic-gate 			if (!BR)
837c478bd9Sstevel@tonic-gate 				BR = DEFBR;
847c478bd9Sstevel@tonic-gate 			FS = DEFFS;
857c478bd9Sstevel@tonic-gate 			RE = (char *)"tip.record";
867c478bd9Sstevel@tonic-gate 			EX = (char *)"\t\n\b\f";
877c478bd9Sstevel@tonic-gate 			DL = 0;
887c478bd9Sstevel@tonic-gate 			CL = 0;
897c478bd9Sstevel@tonic-gate 			ET = 10;
907c478bd9Sstevel@tonic-gate 			return;
917c478bd9Sstevel@tonic-gate 		}
928d489c7aSmuffin 		(void) fprintf(stderr, stat == 0 ?
938d489c7aSmuffin 		    "tip: unknown host %s\n" :
948d489c7aSmuffin 		    "tip: can't open host description file\n", host);
957c478bd9Sstevel@tonic-gate 		exit(3);
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	for (p = capstrings, q = caps; *p != NULL; p++, q++)
997c478bd9Sstevel@tonic-gate 		if (**q == NULL)
1007c478bd9Sstevel@tonic-gate 			**q = rgetstr(*p, &bp);
1017c478bd9Sstevel@tonic-gate 	if (!BR && (BR = rgetnum("br")) < 0)
1027c478bd9Sstevel@tonic-gate 		BR = DEFBR;
1037c478bd9Sstevel@tonic-gate 	if ((FS = rgetnum("fs")) < 0)
1047c478bd9Sstevel@tonic-gate 		FS = DEFFS;
1057c478bd9Sstevel@tonic-gate 	if (DU < 0)
1067c478bd9Sstevel@tonic-gate 		DU = 0;
1077c478bd9Sstevel@tonic-gate 	else
1087c478bd9Sstevel@tonic-gate 		DU = rgetflag("du");
1097c478bd9Sstevel@tonic-gate 	if (DV == NOSTR) {
1108d489c7aSmuffin 		(void) fprintf(stderr, "%s: missing device spec\n", host);
1117c478bd9Sstevel@tonic-gate 		exit(3);
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 	if (DU && CU == NOSTR)
1147c478bd9Sstevel@tonic-gate 		CU = DV;
1157c478bd9Sstevel@tonic-gate 	if (DU && PN == NOSTR) {
1168d489c7aSmuffin 		(void) fprintf(stderr, "%s: missing phone number\n", host);
1177c478bd9Sstevel@tonic-gate 		exit(3);
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 	DB = rgetflag("db");
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	/*
1227c478bd9Sstevel@tonic-gate 	 * This effectively eliminates the "hw" attribute
1237c478bd9Sstevel@tonic-gate 	 *   from the description file
1247c478bd9Sstevel@tonic-gate 	 */
1257c478bd9Sstevel@tonic-gate 	if (!HW)
1267c478bd9Sstevel@tonic-gate 		HW = (CU == NOSTR) || (DU && equal(DV, CU));
1277c478bd9Sstevel@tonic-gate 	HO = host;
1287c478bd9Sstevel@tonic-gate 	/*
1297c478bd9Sstevel@tonic-gate 	 * see if uppercase mode should be turned on initially
1307c478bd9Sstevel@tonic-gate 	 */
1317c478bd9Sstevel@tonic-gate 	if (rgetflag("ra"))
1327c478bd9Sstevel@tonic-gate 		boolean(value(RAISE)) = 1;
1337c478bd9Sstevel@tonic-gate 	if (rgetflag("ec"))
1347c478bd9Sstevel@tonic-gate 		boolean(value(ECHOCHECK)) = 1;
1357c478bd9Sstevel@tonic-gate 	if (rgetflag("be"))
1367c478bd9Sstevel@tonic-gate 		boolean(value(BEAUTIFY)) = 1;
1377c478bd9Sstevel@tonic-gate 	if (rgetflag("nb"))
1387c478bd9Sstevel@tonic-gate 		boolean(value(BEAUTIFY)) = 0;
1397c478bd9Sstevel@tonic-gate 	if (rgetflag("sc"))
1407c478bd9Sstevel@tonic-gate 		boolean(value(SCRIPT)) = 1;
1417c478bd9Sstevel@tonic-gate 	if (rgetflag("tb"))
1427c478bd9Sstevel@tonic-gate 		boolean(value(TABEXPAND)) = 1;
1437c478bd9Sstevel@tonic-gate 	if (rgetflag("vb"))
1447c478bd9Sstevel@tonic-gate 		boolean(value(VERBOSE)) = 1;
1457c478bd9Sstevel@tonic-gate 	if (rgetflag("nv"))
1467c478bd9Sstevel@tonic-gate 		boolean(value(VERBOSE)) = 0;
1477c478bd9Sstevel@tonic-gate 	if (rgetflag("ta"))
1487c478bd9Sstevel@tonic-gate 		boolean(value(TAND)) = 1;
1497c478bd9Sstevel@tonic-gate 	if (rgetflag("nt"))
1507c478bd9Sstevel@tonic-gate 		boolean(value(TAND)) = 0;
1517c478bd9Sstevel@tonic-gate 	if (rgetflag("rw"))
1527c478bd9Sstevel@tonic-gate 		boolean(value(RAWFTP)) = 1;
1537c478bd9Sstevel@tonic-gate 	if (rgetflag("hd"))
1547c478bd9Sstevel@tonic-gate 		boolean(value(HALFDUPLEX)) = 1;
1557c478bd9Sstevel@tonic-gate 	if (rgetflag("hf"))
1567c478bd9Sstevel@tonic-gate 		boolean(value(HARDWAREFLOW)) = 1;
1577c478bd9Sstevel@tonic-gate 	if (RE == NULL)
1587c478bd9Sstevel@tonic-gate 		RE = (char *)"tip.record";
1597c478bd9Sstevel@tonic-gate 	if (EX == NULL)
1607c478bd9Sstevel@tonic-gate 		EX = (char *)"\t\n\b\f";
1617c478bd9Sstevel@tonic-gate 	if (ES != NOSTR)
1628d489c7aSmuffin 		(void) vstring("es", ES);
1637c478bd9Sstevel@tonic-gate 	if (FO != NOSTR)
1648d489c7aSmuffin 		(void) vstring("fo", FO);
1657c478bd9Sstevel@tonic-gate 	if (PR != NOSTR)
1668d489c7aSmuffin 		(void) vstring("pr", PR);
1677c478bd9Sstevel@tonic-gate 	if (RC != NOSTR)
1688d489c7aSmuffin 		(void) vstring("rc", RC);
1697c478bd9Sstevel@tonic-gate 	if ((DL = rgetnum("dl")) < 0)
1707c478bd9Sstevel@tonic-gate 		DL = 0;
1717c478bd9Sstevel@tonic-gate 	if ((CL = rgetnum("cl")) < 0)
1727c478bd9Sstevel@tonic-gate 		CL = 0;
1737c478bd9Sstevel@tonic-gate 	if ((ET = rgetnum("et")) < 0)
1747c478bd9Sstevel@tonic-gate 		ET = 10;
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate char *
getremote(char * host)1788d489c7aSmuffin getremote(char *host)
1797c478bd9Sstevel@tonic-gate {
1808d489c7aSmuffin 	char *cp;
1817c478bd9Sstevel@tonic-gate 	static char *next;
1827c478bd9Sstevel@tonic-gate 	static int lookedup = 0;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if (!lookedup) {
1857c478bd9Sstevel@tonic-gate 		if (host == NOSTR && (host = getenv("HOST")) == NOSTR) {
1868d489c7aSmuffin 			(void) fprintf(stderr, "tip: no host specified\n");
1877c478bd9Sstevel@tonic-gate 			exit(3);
1887c478bd9Sstevel@tonic-gate 		}
1897c478bd9Sstevel@tonic-gate 		getremcap(host);
1907c478bd9Sstevel@tonic-gate 		next = DV;
1917c478bd9Sstevel@tonic-gate 		lookedup++;
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 	/*
1947c478bd9Sstevel@tonic-gate 	 * We return a new device each time we're called (to allow
1957c478bd9Sstevel@tonic-gate 	 *   a rotary action to be simulated)
1967c478bd9Sstevel@tonic-gate 	 */
1977c478bd9Sstevel@tonic-gate 	if (next == NOSTR)
1987c478bd9Sstevel@tonic-gate 		return (NOSTR);
1997c478bd9Sstevel@tonic-gate 	if ((cp = strchr(next, ',')) == NULL) {
2007c478bd9Sstevel@tonic-gate 		DV = next;
2017c478bd9Sstevel@tonic-gate 		next = NOSTR;
2027c478bd9Sstevel@tonic-gate 	} else {
2037c478bd9Sstevel@tonic-gate 		*cp++ = '\0';
2047c478bd9Sstevel@tonic-gate 		DV = next;
2057c478bd9Sstevel@tonic-gate 		next = cp;
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 	return (DV);
2087c478bd9Sstevel@tonic-gate }
209