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