remote.c revision 88276
1139804Simp/*	$OpenBSD: remote.c,v 1.10 2001/10/24 18:38:58 millert Exp $	*/
21541Srgrimes/*	$NetBSD: remote.c,v 1.5 1997/04/20 00:02:45 mellon Exp $	*/
31541Srgrimes
41541Srgrimes/*
51541Srgrimes * Copyright (c) 1992, 1993
61541Srgrimes *	The Regents of the University of California.  All rights reserved.
71541Srgrimes *
81541Srgrimes *
91541Srgrimes * Redistribution and use in source and binary forms, with or without
101541Srgrimes * modification, are permitted provided that the following conditions
111541Srgrimes * are met:
121541Srgrimes * 1. Redistributions of source code must retain the above copyright
131541Srgrimes *    notice, this list of conditions and the following disclaimer.
141541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151541Srgrimes *    notice, this list of conditions and the following disclaimer in the
161541Srgrimes *    documentation and/or other materials provided with the distribution.
171541Srgrimes * 3. All advertising materials mentioning features or use of this software
181541Srgrimes *    must display the following acknowledgement:
191541Srgrimes *	This product includes software developed by the University of
201541Srgrimes *	California, Berkeley and its contributors.
211541Srgrimes * 4. Neither the name of the University nor the names of its contributors
221541Srgrimes *    may be used to endorse or promote products derived from this software
231541Srgrimes *    without specific prior written permission.
241541Srgrimes *
251541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
261541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
271541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
281541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
291541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
301541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
311541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
321541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
331541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
341541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
351541Srgrimes * SUCH DAMAGE.
361541Srgrimes */
37116182Sobrien
38116182Sobrien#include <sys/cdefs.h>
39116182Sobrien__FBSDID("$FreeBSD: head/usr.bin/tip/tip/remote.c 88276 2001-12-20 14:25:46Z markm $");
40190759Srwatson
4113203Swollman#ifndef lint
4213203Swollmanstatic char copyright[] =
431541Srgrimes"@(#) Copyright (c) 1992, 1993\n\
442112Swollman	The Regents of the University of California.  All rights reserved.\n";
4569664Speter#endif /* not lint */
46177785Skib
47192895Sjamie#ifndef lint
4876166Smarkm#if 0
4989316Salfredstatic char sccsid[] = "@(#)remote.c	8.1 (Berkeley) 6/6/93";
501541Srgrimesstatic char rcsid[] = "$OpenBSD: remote.c,v 1.10 2001/10/24 18:38:58 millert Exp $";
511541Srgrimes#endif
521541Srgrimes#endif /* not lint */
531541Srgrimes
541541Srgrimes#include <stdio.h>
55190759Srwatson#include <stdlib.h>
56141471Sjhb
57144613Sjeff#include "pathnames.h"
581541Srgrimes#include "tip.h"
591541Srgrimes
601541Srgrimes/*
611541Srgrimes * Attributes to be gleened from remote host description
62155334Srwatson *   data base.
63163606Srwatson */
64155334Srwatsonstatic char **caps[] = {
6592751Sjeff	&AT, &DV, &CM, &CU, &EL, &IE, &OE, &PN, &PR, &DI,
6632011Sbde	&ES, &EX, &FO, &RC, &RE, &PA
67155168Sjeff};
68138345Sphk
69138345Sphkstatic char *capstrings[] = {
70190759Srwatson	"at", "dv", "cm", "cu", "el", "ie", "oe", "pn", "pr",
71190759Srwatson	"di", "es", "ex", "fo", "rc", "re", "pa", 0
72190759Srwatson};
73190759Srwatson
74190759Srwatsonstatic char	*db_array[3] = { _PATH_REMOTE, 0, 0 };
751541Srgrimes
7669664Speter#define cgetflag(f)	(cgetcap(bp, f, ':') != NULL)
7769664Speter
7892751Sjeffstatic void
79166167Skibgetremcap(host)
80166167Skib	char *host;
81166167Skib{
82166167Skib	char **p, ***q;
8369664Speter	char *bp;
8469664Speter	char *rempath;
8569664Speter	int   stat;
8669664Speter
87168138Srwatson	rempath = getenv("REMOTE");
88168138Srwatson	if (rempath != NULL) {
8992654Sjeff		if (*rempath != '/')
9092654Sjeff			/* we have an entry */
91168138Srwatson			cgetset(rempath);
92168138Srwatson		else {	/* we have a path */
93168138Srwatson			db_array[1] = rempath;
94176519Sattilio			db_array[2] = _PATH_REMOTE;
9569664Speter		}
96177253Srwatson	}
9769664Speter
98183520Sjhb	if ((stat = cgetent(&bp, db_array, host)) < 0) {
99144613Sjeff		if (DV ||
100144613Sjeff		    host[0] == '/' && access(DV = host, R_OK | W_OK) == 0) {
101183519Sjhb			CU = DV;
102144613Sjeff			HO = host;
10369664Speter			HW = 1;
104161010Srwatson			DU = 0;
1051541Srgrimes			if (!BR)
1061541Srgrimes				BR = DEFBR;
1071541Srgrimes			FS = DEFFS;
1081541Srgrimes			return;
1091541Srgrimes		}
1101541Srgrimes		switch(stat) {
1111541Srgrimes		case -1:
1121541Srgrimes			fprintf(stderr, "%s: unknown host %s\n", __progname,
1131541Srgrimes			    host);
1141541Srgrimes			break;
1151541Srgrimes		case -2:
1161541Srgrimes			fprintf(stderr,
1171541Srgrimes			    "%s: can't open host description file\n",
1181541Srgrimes			    __progname);
1191541Srgrimes			break;
1201541Srgrimes		case -3:
1211541Srgrimes			fprintf(stderr,
1221541Srgrimes			    "%s: possible reference loop in host description file\n", __progname);
1231541Srgrimes			break;
124161011Srwatson		}
1251541Srgrimes		exit(3);
126161011Srwatson	}
127161011Srwatson
128161011Srwatson	for (p = capstrings, q = caps; *p != NULL; p++, q++)
1291541Srgrimes		if (**q == NULL)
1301541Srgrimes			cgetstr(bp, *p, *q);
1311541Srgrimes	if (!BR && (cgetnum(bp, "br", &BR) == -1))
1321541Srgrimes		BR = DEFBR;
13383366Sjulian	if (cgetnum(bp, "fs", &FS) == -1)
13483366Sjulian		FS = DEFFS;
135140714Sjeff	if (DU < 0)
1361541Srgrimes		DU = 0;
137150164Scsjp	else
138150164Scsjp		DU = cgetflag("du");
13991419Sjhb	if (DV == NOSTR) {
14083366Sjulian		fprintf(stderr, "%s: missing device spec\n", host);
14142408Seivind		exit(3);
14242453Seivind	}
14342408Seivind	if (DU && CU == NOSTR)
14442453Seivind		CU = DV;
145144613Sjeff	if (DU && PN == NOSTR) {
146144613Sjeff		fprintf(stderr, "%s: missing phone number\n", host);
14783366Sjulian		exit(3);
1481541Srgrimes	}
149193028Sdes	if (DU && AT == NOSTR) {
150193028Sdes		fprintf(stderr, "%s: missing acu type\n", host);
151193028Sdes		exit(3);
1521541Srgrimes	}
1531541Srgrimes
1541541Srgrimes	HD = cgetflag("hd");
1551541Srgrimes
1561541Srgrimes	/*
157111119Simp	 * This effectively eliminates the "hw" attribute
1581541Srgrimes	 *   from the description file
1591541Srgrimes	 */
16036735Sdfr	if (!HW)
1611541Srgrimes		HW = (CU == NOSTR) || (DU && equal(DV, CU));
1621541Srgrimes	HO = host;
16336735Sdfr	/*
16420069Sbde	 * see if uppercase mode should be turned on initially
165155334Srwatson	 */
166155334Srwatson	if (cgetflag("ra"))
167155334Srwatson		setboolean(value(RAISE), 1);
168155334Srwatson	if (cgetflag("ec"))
169155334Srwatson		setboolean(value(ECHOCHECK), 1);
170155334Srwatson	if (cgetflag("be"))
17120069Sbde		setboolean(value(BEAUTIFY), 1);
17220069Sbde	if (cgetflag("nb"))
17320069Sbde		setboolean(value(BEAUTIFY), 0);
17420069Sbde	if (cgetflag("sc"))
17520069Sbde		setboolean(value(SCRIPT), 1);
17620069Sbde	if (cgetflag("tb"))
1771541Srgrimes		setboolean(value(TABEXPAND), 1);
17892751Sjeff	if (cgetflag("vb"))
179100613Srwatson		setboolean(value(VERBOSE), 1);
180100613Srwatson	if (cgetflag("nv"))
181100613Srwatson		setboolean(value(VERBOSE), 0);
182100613Srwatson	if (cgetflag("ta"))
1831541Srgrimes		setboolean(value(TAND), 1);
1841541Srgrimes	if (cgetflag("nt"))
1851541Srgrimes		setboolean(value(TAND), 0);
1861541Srgrimes	if (cgetflag("rw"))
1871541Srgrimes		setboolean(value(RAWFTP), 1);
18897994Sjhb	if (cgetflag("hd"))
18997994Sjhb		setboolean(value(HALFDUPLEX), 1);
19097994Sjhb	if (cgetflag("dc"))
19197994Sjhb		setboolean(value(DC), 1);
19297994Sjhb	if (RE == NOSTR)
1931541Srgrimes		RE = (char *)"tip.record";
1941541Srgrimes	if (EX == NOSTR)
1951541Srgrimes		EX = (char *)"\t\n\b\f";
1961541Srgrimes	if (ES != NOSTR)
197168355Srwatson		vstring("es", ES);
19833360Sdyson	if (FO != NOSTR)
19951649Sphk		vstring("fo", FO);
20033360Sdyson	if (PR != NOSTR)
201185029Spjd		vstring("pr", PR);
202185029Spjd	if (RC != NOSTR)
203185029Spjd		vstring("rc", RC);
204185029Spjd	if (cgetnum(bp, "dl", &DL) == -1)
205185029Spjd		DL = 0;
206185029Spjd	if (cgetnum(bp, "cl", &CL) == -1)
207185029Spjd		CL = 0;
208185029Spjd	if (cgetnum(bp, "et", &ET) == -1)
209185029Spjd		ET = 10;
210185029Spjd}
211185029Spjd
212185029Spjdchar *
213185029Spjdgetremote(host)
214185029Spjd	char *host;
215185029Spjd{
216177785Skib	char *cp;
217177785Skib	static char *next;
218177785Skib	static int lookedup = 0;
219177785Skib
220177785Skib	if (!lookedup) {
221177785Skib		if (host == NOSTR && (host = getenv("HOST")) == NOSTR) {
222177785Skib			fprintf(stderr, "%s: no host specified\n", __progname);
223177785Skib			exit(3);
224177785Skib		}
225185029Spjd		getremcap(host);
226185029Spjd		next = DV;
227177785Skib		lookedup++;
228177785Skib	}
229177785Skib	/*
230185029Spjd	 * We return a new device each time we're called (to allow
231185029Spjd	 *   a rotary action to be simulated)
232185029Spjd	 */
233185029Spjd	if (next == NOSTR)
234185029Spjd		return (NOSTR);
235177785Skib	if ((cp = strchr(next, ',')) == NULL) {
236190759Srwatson		DV = next;
237190759Srwatson		next = NOSTR;
238140714Sjeff	} else {
2391541Srgrimes		*cp++ = '\0';
2401541Srgrimes		DV = next;
2411541Srgrimes		next = cp;
2421541Srgrimes	}
2431541Srgrimes	return (DV);
2441541Srgrimes}
2451541Srgrimes