remote.c revision 1.12 1 /* $NetBSD: remote.c,v 1.12 2006/03/29 12:37:59 yamt 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.12 2006/03/29 12:37:59 yamt 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 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 (cgetflag("hf"))
181 setboolean(value(HARDWAREFLOW), 1);
182 if (RE == NULL)
183 RE = tiprecord;
184 if (EX == NULL)
185 EX = wspace;
186 if (ES != NULL)
187 vstring("es", ES);
188 if (FO != NULL)
189 vstring("fo", FO);
190 if (PR != NULL)
191 vstring("pr", PR);
192 if (RC != NULL)
193 vstring("rc", RC);
194 if (cgetnum(bp, "dl", &DL) == -1)
195 DL = 0;
196 if (cgetnum(bp, "cl", &CL) == -1)
197 CL = 0;
198 if (cgetnum(bp, "et", &ET) == -1)
199 ET = 10;
200 }
201
202 char *
203 getremote(host)
204 char *host;
205 {
206 char *cp;
207 static char *next;
208 static int lookedup = 0;
209
210 if (!lookedup) {
211 if (host == NULL && (host = getenv("HOST")) == NULL) {
212 fprintf(stderr, "tip: no host specified\n");
213 exit(3);
214 }
215 getremcap(host);
216 next = DV;
217 lookedup++;
218 }
219 /*
220 * We return a new device each time we're called (to allow
221 * a rotary action to be simulated)
222 */
223 if (next == NULL)
224 return (NULL);
225 if ((cp = strchr(next, ',')) == NULL) {
226 DV = next;
227 next = NULL;
228 } else {
229 *cp++ = '\0';
230 DV = next;
231 next = cp;
232 }
233 return (DV);
234 }
235