remote.c revision 1.13 1 /* $NetBSD: remote.c,v 1.13 2006/04/02 06:11:45 tls 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.13 2006/04/02 06:11:45 tls 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 const 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 const 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 char tiprecord[] = "tip.record";
70 static char wspace[] = "\t\n\b\f";
71
72 static void
73 getremcap(host)
74 char *host;
75 {
76 const char **p;
77 char ***q;
78 char *bp;
79 char *rempath;
80 int status;
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 ((status = 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(status) {
105 case -1:
106 warnx("unknown host %s", host);
107 break;
108 case -2:
109 warnx("can't open host description file");
110 break;
111 case -3:
112 warnx("possible reference loop in host "
113 "description file");
114
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 errx(3, "%s: missing device spec\n", host);
133 }
134 if (DU && CU == NULL)
135 CU = DV;
136 if (DU && PN == NULL) {
137 errx(3, "%s: missing phone number\n", host);
138 }
139
140 HD = cgetflag("hd");
141
142 /*
143 * This effectively eliminates the "hw" attribute
144 * from the description file
145 */
146 if (!HW)
147 HW = (CU == NULL) || (DU && equal(DV, CU));
148 HO = host;
149 /*
150 * see if uppercase mode should be turned on initially
151 */
152 if (cgetflag("ra"))
153 setboolean(value(RAISE), 1);
154 if (cgetflag("ec"))
155 setboolean(value(ECHOCHECK), 1);
156 if (cgetflag("be"))
157 setboolean(value(BEAUTIFY), 1);
158 if (cgetflag("nb"))
159 setboolean(value(BEAUTIFY), 0);
160 if (cgetflag("sc"))
161 setboolean(value(SCRIPT), 1);
162 if (cgetflag("tb"))
163 setboolean(value(TABEXPAND), 1);
164 if (cgetflag("vb"))
165 setboolean(value(VERBOSE), 1);
166 if (cgetflag("nv"))
167 setboolean(value(VERBOSE), 0);
168 if (cgetflag("ta"))
169 setboolean(value(TAND), 1);
170 if (cgetflag("nt"))
171 setboolean(value(TAND), 0);
172 if (cgetflag("rw"))
173 setboolean(value(RAWFTP), 1);
174 if (cgetflag("hd"))
175 setboolean(value(HALFDUPLEX), 1);
176 if (cgetflag("dc"))
177 DC = 1;
178 if (cgetflag("hf"))
179 setboolean(value(HARDWAREFLOW), 1);
180 if (RE == NULL)
181 RE = tiprecord;
182 if (EX == NULL)
183 EX = wspace;
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 errx(3, "no host specified\n");
211 }
212 getremcap(host);
213 next = DV;
214 lookedup++;
215 }
216 /*
217 * We return a new device each time we're called (to allow
218 * a rotary action to be simulated)
219 */
220 if (next == NULL)
221 return (NULL);
222 if ((cp = strchr(next, ',')) == NULL) {
223 DV = next;
224 next = NULL;
225 } else {
226 *cp++ = '\0';
227 DV = next;
228 next = cp;
229 }
230 return (DV);
231 }
232