xref: /netbsd-src/usr.bin/tip/remote.c (revision d9158b13b5dfe46201430699a3f7a235ecf28df3)
1 /*
2  * Copyright (c) 1983 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 /*static char sccsid[] = "from: @(#)remote.c	5.5 (Berkeley) 6/1/90";*/
36 static char rcsid[] = "$Id: remote.c,v 1.2 1993/08/01 18:06:36 mycroft Exp $";
37 #endif /* not lint */
38 
39 # include "tip.h"
40 
41 /*
42  * Attributes to be gleened from remote host description
43  *   data base.
44  */
45 static char **caps[] = {
46 	&AT, &DV, &CM, &CU, &EL, &IE, &OE, &PN, &PR, &DI,
47 	&ES, &EX, &FO, &RC, &RE, &PA
48 };
49 
50 static char *capstrings[] = {
51 	"at", "dv", "cm", "cu", "el", "ie", "oe", "pn", "pr",
52 	"di", "es", "ex", "fo", "rc", "re", "pa", 0
53 };
54 
55 char *rgetstr();
56 
57 static
58 getremcap(host)
59 	register char *host;
60 {
61 	int stat;
62 	char tbuf[BUFSIZ];
63 	static char buf[BUFSIZ/2];
64 	char *bp = buf;
65 	register char **p, ***q;
66 
67 	if ((stat = rgetent(tbuf, host)) <= 0) {
68 		if (DV ||
69 		    host[0] == '/' && access(DV = host, R_OK | W_OK) == 0) {
70 			CU = DV;
71 			HO = host;
72 			HW = 1;
73 			DU = 0;
74 			if (!BR)
75 				BR = DEFBR;
76 			FS = DEFFS;
77 			return;
78 		}
79 		fprintf(stderr, stat == 0 ?
80 			"tip: unknown host %s\n" :
81 			"tip: can't open host description file\n", host);
82 		exit(3);
83 	}
84 
85 	for (p = capstrings, q = caps; *p != NULL; p++, q++)
86 		if (**q == NULL)
87 			**q = rgetstr(*p, &bp);
88 	if (!BR && (BR = rgetnum("br")) < 0)
89 		BR = DEFBR;
90 	if ((FS = rgetnum("fs")) < 0)
91 		FS = DEFFS;
92 	if (DU < 0)
93 		DU = 0;
94 	else
95 		DU = rgetflag("du");
96 	if (DV == NOSTR) {
97 		fprintf(stderr, "%s: missing device spec\n", host);
98 		exit(3);
99 	}
100 	if (DU && CU == NOSTR)
101 		CU = DV;
102 	if (DU && PN == NOSTR) {
103 		fprintf(stderr, "%s: missing phone number\n", host);
104 		exit(3);
105 	}
106 
107 	HD = rgetflag("hd");
108 
109 	/*
110 	 * This effectively eliminates the "hw" attribute
111 	 *   from the description file
112 	 */
113 	if (!HW)
114 		HW = (CU == NOSTR) || (DU && equal(DV, CU));
115 	HO = host;
116 	/*
117 	 * see if uppercase mode should be turned on initially
118 	 */
119 	if (rgetflag("ra"))
120 		boolean(value(RAISE)) = 1;
121 	if (rgetflag("ec"))
122 		boolean(value(ECHOCHECK)) = 1;
123 	if (rgetflag("be"))
124 		boolean(value(BEAUTIFY)) = 1;
125 	if (rgetflag("nb"))
126 		boolean(value(BEAUTIFY)) = 0;
127 	if (rgetflag("sc"))
128 		boolean(value(SCRIPT)) = 1;
129 	if (rgetflag("tb"))
130 		boolean(value(TABEXPAND)) = 1;
131 	if (rgetflag("vb"))
132 		boolean(value(VERBOSE)) = 1;
133 	if (rgetflag("nv"))
134 		boolean(value(VERBOSE)) = 0;
135 	if (rgetflag("ta"))
136 		boolean(value(TAND)) = 1;
137 	if (rgetflag("nt"))
138 		boolean(value(TAND)) = 0;
139 	if (rgetflag("rw"))
140 		boolean(value(RAWFTP)) = 1;
141 	if (rgetflag("hd"))
142 		boolean(value(HALFDUPLEX)) = 1;
143 	if (RE == NOSTR)
144 		RE = (char *)"tip.record";
145 	if (EX == NOSTR)
146 		EX = (char *)"\t\n\b\f";
147 	if (ES != NOSTR)
148 		vstring("es", ES);
149 	if (FO != NOSTR)
150 		vstring("fo", FO);
151 	if (PR != NOSTR)
152 		vstring("pr", PR);
153 	if (RC != NOSTR)
154 		vstring("rc", RC);
155 	if ((DL = rgetnum("dl")) < 0)
156 		DL = 0;
157 	if ((CL = rgetnum("cl")) < 0)
158 		CL = 0;
159 	if ((ET = rgetnum("et")) < 0)
160 		ET = 10;
161 }
162 
163 char *
164 getremote(host)
165 	char *host;
166 {
167 	register char *cp;
168 	static char *next;
169 	static int lookedup = 0;
170 
171 	if (!lookedup) {
172 		if (host == NOSTR && (host = getenv("HOST")) == NOSTR) {
173 			fprintf(stderr, "tip: no host specified\n");
174 			exit(3);
175 		}
176 		getremcap(host);
177 		next = DV;
178 		lookedup++;
179 	}
180 	/*
181 	 * We return a new device each time we're called (to allow
182 	 *   a rotary action to be simulated)
183 	 */
184 	if (next == NOSTR)
185 		return (NOSTR);
186 	if ((cp = index(next, ',')) == NULL) {
187 		DV = next;
188 		next = NOSTR;
189 	} else {
190 		*cp++ = '\0';
191 		DV = next;
192 		next = cp;
193 	}
194 	return (DV);
195 }
196