xref: /netbsd-src/usr.bin/tip/remote.c (revision 4d7e773266e3c3f48566c86c0ad52d51c6454fd1)
1 /*	$NetBSD: remote.c,v 1.6 1997/05/14 00:20:03 mellon 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 #ifndef lint
38 static char copyright[] =
39 "@(#) 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 static char rcsid[] = "$NetBSD: remote.c,v 1.6 1997/05/14 00:20:03 mellon Exp $";
48 #endif /* not lint */
49 
50 #include <stdio.h>
51 #include <stdlib.h>
52 
53 #include "pathnames.h"
54 #include "tip.h"
55 
56 /*
57  * Attributes to be gleened from remote host description
58  *   data base.
59  */
60 static char **caps[] = {
61 	&AT, &DV, &CM, &CU, &EL, &IE, &OE, &PN, &PR, &DI,
62 	&ES, &EX, &FO, &RC, &RE, &PA
63 };
64 
65 static char *capstrings[] = {
66 	"at", "dv", "cm", "cu", "el", "ie", "oe", "pn", "pr",
67 	"di", "es", "ex", "fo", "rc", "re", "pa", 0
68 };
69 
70 static char	*db_array[3] = { _PATH_REMOTE, 0, 0 };
71 
72 #define cgetflag(f)	(cgetcap(bp, f, ':') != NULL)
73 
74 static
75 getremcap(host)
76 	register char *host;
77 {
78 	register char **p, ***q;
79 	char *bp;
80 	char *rempath;
81 	int   stat;
82 
83 	rempath = getenv("REMOTE");
84 	if (rempath != NULL)
85 		if (*rempath != '/')
86 			/* we have an entry */
87 			cgetset(rempath);
88 		else {	/* we have a path */
89 			db_array[1] = rempath;
90 			db_array[2] = _PATH_REMOTE;
91 		}
92 
93 	if ((stat = cgetent(&bp, db_array, host)) < 0) {
94 		if (DV ||
95 		    host[0] == '/' && access(DV = host, R_OK | W_OK) == 0) {
96 			CU = DV;
97 			HO = host;
98 			HW = 1;
99 			DU = 0;
100 			if (!BR)
101 				BR = DEFBR;
102 			FS = DEFFS;
103 			return;
104 		}
105 		switch(stat) {
106 		case -1:
107 			fprintf(stderr, "tip: unknown host %s\n", host);
108 			break;
109 		case -2:
110 			fprintf(stderr,
111 			    "tip: can't open host description file\n");
112 			break;
113 		case -3:
114 			fprintf(stderr,
115 			    "tip: possible reference loop in host description file\n");
116 			break;
117 		}
118 		exit(3);
119 	}
120 
121 	for (p = capstrings, q = caps; *p != NULL; p++, q++)
122 		if (**q == NULL)
123 			cgetstr(bp, *p, *q);
124 	if (!BR && (cgetnum(bp, "br", &BR) == -1))
125 		BR = DEFBR;
126 	if (cgetnum(bp, "fs", &FS) == -1)
127 		FS = DEFFS;
128 	if (DU < 0)
129 		DU = 0;
130 	else
131 		DU = cgetflag("du");
132 	if (DV == NOSTR) {
133 		fprintf(stderr, "%s: missing device spec\n", host);
134 		exit(3);
135 	}
136 	if (DU && CU == NOSTR)
137 		CU = DV;
138 	if (DU && PN == NOSTR) {
139 		fprintf(stderr, "%s: missing phone number\n", host);
140 		exit(3);
141 	}
142 
143 	HD = cgetflag("hd");
144 
145 	/*
146 	 * This effectively eliminates the "hw" attribute
147 	 *   from the description file
148 	 */
149 	if (!HW)
150 		HW = (CU == NOSTR) || (DU && equal(DV, CU));
151 	HO = host;
152 	/*
153 	 * see if uppercase mode should be turned on initially
154 	 */
155 	if (cgetflag("ra"))
156 		setboolean(value(RAISE), 1);
157 	if (cgetflag("ec"))
158 		setboolean(value(ECHOCHECK), 1);
159 	if (cgetflag("be"))
160 		setboolean(value(BEAUTIFY), 1);
161 	if (cgetflag("nb"))
162 		setboolean(value(BEAUTIFY), 0);
163 	if (cgetflag("sc"))
164 		setboolean(value(SCRIPT), 1);
165 	if (cgetflag("tb"))
166 		setboolean(value(TABEXPAND), 1);
167 	if (cgetflag("vb"))
168 		setboolean(value(VERBOSE), 1);
169 	if (cgetflag("nv"))
170 		setboolean(value(VERBOSE), 0);
171 	if (cgetflag("ta"))
172 		setboolean(value(TAND), 1);
173 	if (cgetflag("nt"))
174 		setboolean(value(TAND), 0);
175 	if (cgetflag("rw"))
176 		setboolean(value(RAWFTP), 1);
177 	if (cgetflag("hd"))
178 		setboolean(value(HALFDUPLEX), 1);
179 	if (cgetflag("dc"))
180 		DC = 1;
181 	if (RE == NOSTR)
182 		RE = (char *)"tip.record";
183 	if (EX == NOSTR)
184 		EX = (char *)"\t\n\b\f";
185 	if (ES != NOSTR)
186 		vstring("es", ES);
187 	if (FO != NOSTR)
188 		vstring("fo", FO);
189 	if (PR != NOSTR)
190 		vstring("pr", PR);
191 	if (RC != NOSTR)
192 		vstring("rc", RC);
193 	if (cgetnum(bp, "dl", &DL) == -1)
194 		DL = 0;
195 	if (cgetnum(bp, "cl", &CL) == -1)
196 		CL = 0;
197 	if (cgetnum(bp, "et", &ET) == -1)
198 		ET = 10;
199 }
200 
201 char *
202 getremote(host)
203 	char *host;
204 {
205 	register char *cp;
206 	static char *next;
207 	static int lookedup = 0;
208 
209 	if (!lookedup) {
210 		if (host == NOSTR && (host = getenv("HOST")) == NOSTR) {
211 			fprintf(stderr, "tip: no host specified\n");
212 			exit(3);
213 		}
214 		getremcap(host);
215 		next = DV;
216 		lookedup++;
217 	}
218 	/*
219 	 * We return a new device each time we're called (to allow
220 	 *   a rotary action to be simulated)
221 	 */
222 	if (next == NOSTR)
223 		return (NOSTR);
224 	if ((cp = index(next, ',')) == NULL) {
225 		DV = next;
226 		next = NOSTR;
227 	} else {
228 		*cp++ = '\0';
229 		DV = next;
230 		next = cp;
231 	}
232 	return (DV);
233 }
234