remote.c revision 1.10 1 /* $NetBSD: remote.c,v 1.10 2004/04/23 22:11:44 christos 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.10 2004/04/23 22:11:44 christos 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 void
70 getremcap(host)
71 char *host;
72 {
73 const char **p;
74 char ***q;
75 char *bp;
76 char *rempath;
77 int status;
78
79 rempath = getenv("REMOTE");
80 if (rempath != NULL) {
81 if (*rempath != '/')
82 /* we have an entry */
83 cgetset(rempath);
84 else { /* we have a path */
85 db_array[1] = rempath;
86 db_array[2] = _PATH_REMOTE;
87 }
88 }
89 if ((status = cgetent(&bp, db_array, host)) < 0) {
90 if (DV ||
91 (host[0] == '/' && access(DV = host, R_OK | W_OK) == 0)) {
92 CU = DV;
93 HO = host;
94 HW = 1;
95 DU = 0;
96 if (!BR)
97 BR = DEFBR;
98 FS = DEFFS;
99 return;
100 }
101 switch(status) {
102 case -1:
103 fprintf(stderr, "tip: unknown host %s\n", host);
104 break;
105 case -2:
106 fprintf(stderr,
107 "tip: can't open host description file\n");
108 break;
109 case -3:
110 fprintf(stderr,
111 "tip: possible reference loop in host description file\n");
112 break;
113 }
114 exit(3);
115 }
116
117 for (p = capstrings, q = caps; *p != NULL; p++, q++)
118 if (**q == NULL)
119 cgetstr(bp, *p, *q);
120 if (!BR && (cgetnum(bp, "br", &BR) == -1))
121 BR = DEFBR;
122 if (cgetnum(bp, "fs", &FS) == -1)
123 FS = DEFFS;
124 if (DU < 0)
125 DU = 0;
126 else
127 DU = cgetflag("du");
128 if (DV == NULL) {
129 fprintf(stderr, "%s: missing device spec\n", host);
130 exit(3);
131 }
132 if (DU && CU == NULL)
133 CU = DV;
134 if (DU && PN == NULL) {
135 fprintf(stderr, "%s: missing phone number\n", host);
136 exit(3);
137 }
138
139 HD = cgetflag("hd");
140
141 /*
142 * This effectively eliminates the "hw" attribute
143 * from the description file
144 */
145 if (!HW)
146 HW = (CU == NULL) || (DU && equal(DV, CU));
147 HO = host;
148 /*
149 * see if uppercase mode should be turned on initially
150 */
151 if (cgetflag("ra"))
152 setboolean(value(RAISE), 1);
153 if (cgetflag("ec"))
154 setboolean(value(ECHOCHECK), 1);
155 if (cgetflag("be"))
156 setboolean(value(BEAUTIFY), 1);
157 if (cgetflag("nb"))
158 setboolean(value(BEAUTIFY), 0);
159 if (cgetflag("sc"))
160 setboolean(value(SCRIPT), 1);
161 if (cgetflag("tb"))
162 setboolean(value(TABEXPAND), 1);
163 if (cgetflag("vb"))
164 setboolean(value(VERBOSE), 1);
165 if (cgetflag("nv"))
166 setboolean(value(VERBOSE), 0);
167 if (cgetflag("ta"))
168 setboolean(value(TAND), 1);
169 if (cgetflag("nt"))
170 setboolean(value(TAND), 0);
171 if (cgetflag("rw"))
172 setboolean(value(RAWFTP), 1);
173 if (cgetflag("hd"))
174 setboolean(value(HALFDUPLEX), 1);
175 if (cgetflag("dc"))
176 DC = 1;
177 if (RE == NULL)
178 RE = strdup("tip.record");
179 if (EX == NULL)
180 EX = strdup("\t\n\b\f");
181 if (ES != NULL)
182 vstring("es", ES);
183 if (FO != NULL)
184 vstring("fo", FO);
185 if (PR != NULL)
186 vstring("pr", PR);
187 if (RC != NULL)
188 vstring("rc", RC);
189 if (cgetnum(bp, "dl", &DL) == -1)
190 DL = 0;
191 if (cgetnum(bp, "cl", &CL) == -1)
192 CL = 0;
193 if (cgetnum(bp, "et", &ET) == -1)
194 ET = 10;
195 }
196
197 char *
198 getremote(host)
199 char *host;
200 {
201 char *cp;
202 static char *next;
203 static int lookedup = 0;
204
205 if (!lookedup) {
206 if (host == NULL && (host = getenv("HOST")) == NULL) {
207 fprintf(stderr, "tip: no host specified\n");
208 exit(3);
209 }
210 getremcap(host);
211 next = DV;
212 lookedup++;
213 }
214 /*
215 * We return a new device each time we're called (to allow
216 * a rotary action to be simulated)
217 */
218 if (next == NULL)
219 return (NULL);
220 if ((cp = strchr(next, ',')) == NULL) {
221 DV = next;
222 next = NULL;
223 } else {
224 *cp++ = '\0';
225 DV = next;
226 next = cp;
227 }
228 return (DV);
229 }
230