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