xref: /csrg-svn/usr.bin/tip/remote.c (revision 62315)
119807Sdist /*
2*62315Sbostic  * Copyright (c) 1992, 1993
3*62315Sbostic  *	The Regents of the University of California.  All rights reserved.
435464Sbostic  *
556117Selan  *
656259Selan  * %sccs.include.redist.c%
719807Sdist  */
819807Sdist 
913279Ssam #ifndef lint
1056271Selan static char copyright[] =
11*62315Sbostic "@(#) Copyright (c) 1992, 1993\n\
12*62315Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1335464Sbostic #endif /* not lint */
1413279Ssam 
1556259Selan #ifndef lint
16*62315Sbostic static char sccsid[] = "@(#)remote.c	8.1 (Berkeley) 06/06/93";
1756259Selan #endif /* not lint */
183695Sroot 
1956586Sbostic #include <stdio.h>
2056586Sbostic #include <stdlib.h>
2156586Sbostic 
2256586Sbostic #include "pathnames.h"
2356586Sbostic #include "tip.h"
2456586Sbostic 
253695Sroot /*
263695Sroot  * Attributes to be gleened from remote host description
273695Sroot  *   data base.
283695Sroot  */
293695Sroot static char **caps[] = {
3013139Sralph 	&AT, &DV, &CM, &CU, &EL, &IE, &OE, &PN, &PR, &DI,
3113139Sralph 	&ES, &EX, &FO, &RC, &RE, &PA
323695Sroot };
333695Sroot 
343695Sroot static char *capstrings[] = {
3513139Sralph 	"at", "dv", "cm", "cu", "el", "ie", "oe", "pn", "pr",
3613139Sralph 	"di", "es", "ex", "fo", "rc", "re", "pa", 0
373695Sroot };
383695Sroot 
3956117Selan static char	*db_array[3] = { _PATH_REMOTE, 0, 0 };
403695Sroot 
4156117Selan #define cgetflag(f)	(cgetcap(bp, f, ':') != NULL)
4256117Selan 
433695Sroot static
getremcap(host)443695Sroot getremcap(host)
453695Sroot 	register char *host;
463695Sroot {
473695Sroot 	register char **p, ***q;
4856117Selan 	char *bp;
4956117Selan 	char *rempath;
5056117Selan 	int   stat;
513695Sroot 
5256117Selan 	rempath = getenv("REMOTE");
5356117Selan 	if (rempath != NULL)
5456117Selan 		if (*rempath != '/')
5556117Selan 			/* we have an entry */
5656117Selan 			cgetset(rempath);
5756117Selan 		else {	/* we have a path */
5856117Selan 			db_array[1] = rempath;
5956117Selan 			db_array[2] = _PATH_REMOTE;
6056117Selan 		}
6156117Selan 
6256117Selan 	if ((stat = cgetent(&bp, db_array, host)) < 0) {
6325577Skarels 		if (DV ||
6425577Skarels 		    host[0] == '/' && access(DV = host, R_OK | W_OK) == 0) {
6525577Skarels 			CU = DV;
6625577Skarels 			HO = host;
6725577Skarels 			HW = 1;
6825577Skarels 			DU = 0;
6925577Skarels 			if (!BR)
7025577Skarels 				BR = DEFBR;
7125577Skarels 			FS = DEFFS;
7225577Skarels 			return;
7325577Skarels 		}
7456117Selan 		switch(stat) {
7556117Selan 		case -1:
7656117Selan 			fprintf(stderr, "tip: unknown host %s\n", host);
7756117Selan 			break;
7856117Selan 		case -2:
7956117Selan 			fprintf(stderr,
8056117Selan 			    "tip: can't open host description file\n");
8156117Selan 			break;
8256117Selan 		case -3:
8356117Selan 			fprintf(stderr,
8456117Selan 			    "tip: possible reference loop in host description file\n");
8556117Selan 			break;
8656117Selan 		}
873695Sroot 		exit(3);
883695Sroot 	}
893695Sroot 
903695Sroot 	for (p = capstrings, q = caps; *p != NULL; p++, q++)
915136Ssam 		if (**q == NULL)
9256117Selan 			cgetstr(bp, *p, *q);
9356117Selan 	if (!BR && (cgetnum(bp, "br", &BR) == -1))
943695Sroot 		BR = DEFBR;
9556117Selan 	if (cgetnum(bp, "fs", &FS) == -1)
963695Sroot 		FS = DEFFS;
975136Ssam 	if (DU < 0)
985136Ssam 		DU = 0;
995136Ssam 	else
10056117Selan 		DU = cgetflag("du");
1013695Sroot 	if (DV == NOSTR) {
1023695Sroot 		fprintf(stderr, "%s: missing device spec\n", host);
1033695Sroot 		exit(3);
1043695Sroot 	}
1053695Sroot 	if (DU && CU == NOSTR)
1063695Sroot 		CU = DV;
1073695Sroot 	if (DU && PN == NOSTR) {
1083695Sroot 		fprintf(stderr, "%s: missing phone number\n", host);
1093695Sroot 		exit(3);
1103695Sroot 	}
11113139Sralph 
11256117Selan 	HD = cgetflag("hd");
11313139Sralph 
1144011Ssam 	/*
1154011Ssam 	 * This effectively eliminates the "hw" attribute
1164011Ssam 	 *   from the description file
1174011Ssam 	 */
1184011Ssam 	if (!HW)
1194011Ssam 		HW = (CU == NOSTR) || (DU && equal(DV, CU));
1203695Sroot 	HO = host;
12113139Sralph 	/*
12213139Sralph 	 * see if uppercase mode should be turned on initially
12313139Sralph 	 */
12456117Selan 	if (cgetflag("ra"))
12513279Ssam 		boolean(value(RAISE)) = 1;
12656117Selan 	if (cgetflag("ec"))
12713279Ssam 		boolean(value(ECHOCHECK)) = 1;
12856117Selan 	if (cgetflag("be"))
12913279Ssam 		boolean(value(BEAUTIFY)) = 1;
13056117Selan 	if (cgetflag("nb"))
13113279Ssam 		boolean(value(BEAUTIFY)) = 0;
13256117Selan 	if (cgetflag("sc"))
13313279Ssam 		boolean(value(SCRIPT)) = 1;
13456117Selan 	if (cgetflag("tb"))
13513279Ssam 		boolean(value(TABEXPAND)) = 1;
13656117Selan 	if (cgetflag("vb"))
13713279Ssam 		boolean(value(VERBOSE)) = 1;
13856117Selan 	if (cgetflag("nv"))
13913279Ssam 		boolean(value(VERBOSE)) = 0;
14056117Selan 	if (cgetflag("ta"))
14113279Ssam 		boolean(value(TAND)) = 1;
14256117Selan 	if (cgetflag("nt"))
14313279Ssam 		boolean(value(TAND)) = 0;
14456117Selan 	if (cgetflag("rw"))
14513279Ssam 		boolean(value(RAWFTP)) = 1;
14656117Selan 	if (cgetflag("hd"))
14713279Ssam 		boolean(value(HALFDUPLEX)) = 1;
14827659Smckusick 	if (RE == NOSTR)
14913279Ssam 		RE = (char *)"tip.record";
15027659Smckusick 	if (EX == NOSTR)
15113279Ssam 		EX = (char *)"\t\n\b\f";
15213279Ssam 	if (ES != NOSTR)
15313279Ssam 		vstring("es", ES);
15413279Ssam 	if (FO != NOSTR)
15513279Ssam 		vstring("fo", FO);
15613279Ssam 	if (PR != NOSTR)
15713279Ssam 		vstring("pr", PR);
15813279Ssam 	if (RC != NOSTR)
15913279Ssam 		vstring("rc", RC);
16056117Selan 	if (cgetnum(bp, "dl", &DL) == -1)
16113279Ssam 		DL = 0;
16256117Selan 	if (cgetnum(bp, "cl", &CL) == -1)
16313279Ssam 		CL = 0;
16456117Selan 	if (cgetnum(bp, "et", &ET) == -1)
16513279Ssam 		ET = 10;
1663695Sroot }
1673695Sroot 
1683695Sroot char *
getremote(host)1693695Sroot getremote(host)
1703695Sroot 	char *host;
1713695Sroot {
1723695Sroot 	register char *cp;
1733695Sroot 	static char *next;
1743695Sroot 	static int lookedup = 0;
1753695Sroot 
1763695Sroot 	if (!lookedup) {
1773695Sroot 		if (host == NOSTR && (host = getenv("HOST")) == NOSTR) {
1783695Sroot 			fprintf(stderr, "tip: no host specified\n");
1793695Sroot 			exit(3);
1803695Sroot 		}
1813695Sroot 		getremcap(host);
1823695Sroot 		next = DV;
1833695Sroot 		lookedup++;
1843695Sroot 	}
1853695Sroot 	/*
1863695Sroot 	 * We return a new device each time we're called (to allow
1873695Sroot 	 *   a rotary action to be simulated)
1883695Sroot 	 */
1893695Sroot 	if (next == NOSTR)
1905136Ssam 		return (NOSTR);
1913695Sroot 	if ((cp = index(next, ',')) == NULL) {
1923695Sroot 		DV = next;
1933695Sroot 		next = NOSTR;
1943695Sroot 	} else {
1953695Sroot 		*cp++ = '\0';
1963695Sroot 		DV = next;
1973695Sroot 		next = cp;
1983695Sroot 	}
1995136Ssam 	return (DV);
2003695Sroot }
201