xref: /netbsd/usr.bin/tip/remote.c (revision bf9ec67e)
1 /*	$NetBSD: remote.c,v 1.8 1998/08/25 20:59:41 ross Exp $	*/
2 
3 /*
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\
40 	The Regents of the University of California.  All rights reserved.\n");
41 #endif /* not lint */
42 
43 #ifndef lint
44 #if 0
45 static char sccsid[] = "@(#)remote.c	8.1 (Berkeley) 6/6/93";
46 #endif
47 __RCSID("$NetBSD: remote.c,v 1.8 1998/08/25 20:59:41 ross Exp $");
48 #endif /* not lint */
49 
50 #include "pathnames.h"
51 #include "tip.h"
52 
53 /*
54  * Attributes to be gleened from remote host description
55  *   data base.
56  */
57 static char **caps[] = {
58 	&AT, &DV, &CM, &CU, &EL, &IE, &OE, &PN, &PR, &DI,
59 	&ES, &EX, &FO, &RC, &RE, &PA
60 };
61 
62 static char *capstrings[] = {
63 	"at", "dv", "cm", "cu", "el", "ie", "oe", "pn", "pr",
64 	"di", "es", "ex", "fo", "rc", "re", "pa", 0
65 };
66 
67 static char	*db_array[3] = { _PATH_REMOTE, 0, 0 };
68 
69 #define cgetflag(f)	(cgetcap(bp, f, ':') != NULL)
70 
71 static	void	getremcap __P((char *));
72 
73 static void
74 getremcap(host)
75 	char *host;
76 {
77 	char **p, ***q;
78 	char *bp;
79 	char *rempath;
80 	int   stat;
81 
82 	rempath = getenv("REMOTE");
83 	if (rempath != NULL) {
84 		if (*rempath != '/')
85 			/* we have an entry */
86 			cgetset(rempath);
87 		else {	/* we have a path */
88 			db_array[1] = rempath;
89 			db_array[2] = _PATH_REMOTE;
90 		}
91 	}
92 	if ((stat = cgetent(&bp, db_array, host)) < 0) {
93 		if (DV ||
94 		    (host[0] == '/' && access(DV = host, R_OK | W_OK) == 0)) {
95 			CU = DV;
96 			HO = host;
97 			HW = 1;
98 			DU = 0;
99 			if (!BR)
100 				BR = DEFBR;
101 			FS = DEFFS;
102 			return;
103 		}
104 		switch(stat) {
105 		case -1:
106 			fprintf(stderr, "tip: unknown host %s\n", host);
107 			break;
108 		case -2:
109 			fprintf(stderr,
110 			    "tip: can't open host description file\n");
111 			break;
112 		case -3:
113 			fprintf(stderr,
114 			    "tip: possible reference loop in host description file\n");
115 			break;
116 		}
117 		exit(3);
118 	}
119 
120 	for (p = capstrings, q = caps; *p != NULL; p++, q++)
121 		if (**q == NULL)
122 			cgetstr(bp, *p, *q);
123 	if (!BR && (cgetnum(bp, "br", &BR) == -1))
124 		BR = DEFBR;
125 	if (cgetnum(bp, "fs", &FS) == -1)
126 		FS = DEFFS;
127 	if (DU < 0)
128 		DU = 0;
129 	else
130 		DU = cgetflag("du");
131 	if (DV == NULL) {
132 		fprintf(stderr, "%s: missing device spec\n", host);
133 		exit(3);
134 	}
135 	if (DU && CU == NULL)
136 		CU = DV;
137 	if (DU && PN == NULL) {
138 		fprintf(stderr, "%s: missing phone number\n", host);
139 		exit(3);
140 	}
141 
142 	HD = cgetflag("hd");
143 
144 	/*
145 	 * This effectively eliminates the "hw" attribute
146 	 *   from the description file
147 	 */
148 	if (!HW)
149 		HW = (CU == NULL) || (DU && equal(DV, CU));
150 	HO = host;
151 	/*
152 	 * see if uppercase mode should be turned on initially
153 	 */
154 	if (cgetflag("ra"))
155 		setboolean(value(RAISE), 1);
156 	if (cgetflag("ec"))
157 		setboolean(value(ECHOCHECK), 1);
158 	if (cgetflag("be"))
159 		setboolean(value(BEAUTIFY), 1);
160 	if (cgetflag("nb"))
161 		setboolean(value(BEAUTIFY), 0);
162 	if (cgetflag("sc"))
163 		setboolean(value(SCRIPT), 1);
164 	if (cgetflag("tb"))
165 		setboolean(value(TABEXPAND), 1);
166 	if (cgetflag("vb"))
167 		setboolean(value(VERBOSE), 1);
168 	if (cgetflag("nv"))
169 		setboolean(value(VERBOSE), 0);
170 	if (cgetflag("ta"))
171 		setboolean(value(TAND), 1);
172 	if (cgetflag("nt"))
173 		setboolean(value(TAND), 0);
174 	if (cgetflag("rw"))
175 		setboolean(value(RAWFTP), 1);
176 	if (cgetflag("hd"))
177 		setboolean(value(HALFDUPLEX), 1);
178 	if (cgetflag("dc"))
179 		DC = 1;
180 	if (RE == NULL)
181 		RE = (char *)"tip.record";
182 	if (EX == NULL)
183 		EX = (char *)"\t\n\b\f";
184 	if (ES != NULL)
185 		vstring("es", ES);
186 	if (FO != NULL)
187 		vstring("fo", FO);
188 	if (PR != NULL)
189 		vstring("pr", PR);
190 	if (RC != NULL)
191 		vstring("rc", RC);
192 	if (cgetnum(bp, "dl", &DL) == -1)
193 		DL = 0;
194 	if (cgetnum(bp, "cl", &CL) == -1)
195 		CL = 0;
196 	if (cgetnum(bp, "et", &ET) == -1)
197 		ET = 10;
198 }
199 
200 char *
201 getremote(host)
202 	char *host;
203 {
204 	char *cp;
205 	static char *next;
206 	static int lookedup = 0;
207 
208 	if (!lookedup) {
209 		if (host == NULL && (host = getenv("HOST")) == NULL) {
210 			fprintf(stderr, "tip: no host specified\n");
211 			exit(3);
212 		}
213 		getremcap(host);
214 		next = DV;
215 		lookedup++;
216 	}
217 	/*
218 	 * We return a new device each time we're called (to allow
219 	 *   a rotary action to be simulated)
220 	 */
221 	if (next == NULL)
222 		return (NULL);
223 	if ((cp = strchr(next, ',')) == NULL) {
224 		DV = next;
225 		next = NULL;
226 	} else {
227 		*cp++ = '\0';
228 		DV = next;
229 		next = cp;
230 	}
231 	return (DV);
232 }
233