xref: /original-bsd/usr.bin/tip/remote.c (revision 2bdcd748)
1 /*
2  * Copyright (c) 1992 The Regents of the University of California.
3  * All rights reserved.
4  *
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 static char copyright[] =
11 "@(#) Copyright (c) 1992 The Regents of the University of California.\n\
12  All rights reserved.\n";
13 #endif /* not lint */
14 
15 #ifndef lint
16 static char sccsid[] = "@(#)remote.c	5.10 (Berkeley) 10/17/92";
17 #endif /* not lint */
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 
22 #include "pathnames.h"
23 #include "tip.h"
24 
25 /*
26  * Attributes to be gleened from remote host description
27  *   data base.
28  */
29 static char **caps[] = {
30 	&AT, &DV, &CM, &CU, &EL, &IE, &OE, &PN, &PR, &DI,
31 	&ES, &EX, &FO, &RC, &RE, &PA
32 };
33 
34 static char *capstrings[] = {
35 	"at", "dv", "cm", "cu", "el", "ie", "oe", "pn", "pr",
36 	"di", "es", "ex", "fo", "rc", "re", "pa", 0
37 };
38 
39 static char	*db_array[3] = { _PATH_REMOTE, 0, 0 };
40 
41 #define cgetflag(f)	(cgetcap(bp, f, ':') != NULL)
42 
43 static
44 getremcap(host)
45 	register char *host;
46 {
47 	register char **p, ***q;
48 	char *bp;
49 	char *rempath;
50 	int   stat;
51 
52 	rempath = getenv("REMOTE");
53 	if (rempath != NULL)
54 		if (*rempath != '/')
55 			/* we have an entry */
56 			cgetset(rempath);
57 		else {	/* we have a path */
58 			db_array[1] = rempath;
59 			db_array[2] = _PATH_REMOTE;
60 		}
61 
62 	if ((stat = cgetent(&bp, db_array, host)) < 0) {
63 		if (DV ||
64 		    host[0] == '/' && access(DV = host, R_OK | W_OK) == 0) {
65 			CU = DV;
66 			HO = host;
67 			HW = 1;
68 			DU = 0;
69 			if (!BR)
70 				BR = DEFBR;
71 			FS = DEFFS;
72 			return;
73 		}
74 		switch(stat) {
75 		case -1:
76 			fprintf(stderr, "tip: unknown host %s\n", host);
77 			break;
78 		case -2:
79 			fprintf(stderr,
80 			    "tip: can't open host description file\n");
81 			break;
82 		case -3:
83 			fprintf(stderr,
84 			    "tip: possible reference loop in host description file\n");
85 			break;
86 		}
87 		exit(3);
88 	}
89 
90 	for (p = capstrings, q = caps; *p != NULL; p++, q++)
91 		if (**q == NULL)
92 			cgetstr(bp, *p, *q);
93 	if (!BR && (cgetnum(bp, "br", &BR) == -1))
94 		BR = DEFBR;
95 	if (cgetnum(bp, "fs", &FS) == -1)
96 		FS = DEFFS;
97 	if (DU < 0)
98 		DU = 0;
99 	else
100 		DU = cgetflag("du");
101 	if (DV == NOSTR) {
102 		fprintf(stderr, "%s: missing device spec\n", host);
103 		exit(3);
104 	}
105 	if (DU && CU == NOSTR)
106 		CU = DV;
107 	if (DU && PN == NOSTR) {
108 		fprintf(stderr, "%s: missing phone number\n", host);
109 		exit(3);
110 	}
111 
112 	HD = cgetflag("hd");
113 
114 	/*
115 	 * This effectively eliminates the "hw" attribute
116 	 *   from the description file
117 	 */
118 	if (!HW)
119 		HW = (CU == NOSTR) || (DU && equal(DV, CU));
120 	HO = host;
121 	/*
122 	 * see if uppercase mode should be turned on initially
123 	 */
124 	if (cgetflag("ra"))
125 		boolean(value(RAISE)) = 1;
126 	if (cgetflag("ec"))
127 		boolean(value(ECHOCHECK)) = 1;
128 	if (cgetflag("be"))
129 		boolean(value(BEAUTIFY)) = 1;
130 	if (cgetflag("nb"))
131 		boolean(value(BEAUTIFY)) = 0;
132 	if (cgetflag("sc"))
133 		boolean(value(SCRIPT)) = 1;
134 	if (cgetflag("tb"))
135 		boolean(value(TABEXPAND)) = 1;
136 	if (cgetflag("vb"))
137 		boolean(value(VERBOSE)) = 1;
138 	if (cgetflag("nv"))
139 		boolean(value(VERBOSE)) = 0;
140 	if (cgetflag("ta"))
141 		boolean(value(TAND)) = 1;
142 	if (cgetflag("nt"))
143 		boolean(value(TAND)) = 0;
144 	if (cgetflag("rw"))
145 		boolean(value(RAWFTP)) = 1;
146 	if (cgetflag("hd"))
147 		boolean(value(HALFDUPLEX)) = 1;
148 	if (RE == NOSTR)
149 		RE = (char *)"tip.record";
150 	if (EX == NOSTR)
151 		EX = (char *)"\t\n\b\f";
152 	if (ES != NOSTR)
153 		vstring("es", ES);
154 	if (FO != NOSTR)
155 		vstring("fo", FO);
156 	if (PR != NOSTR)
157 		vstring("pr", PR);
158 	if (RC != NOSTR)
159 		vstring("rc", RC);
160 	if (cgetnum(bp, "dl", &DL) == -1)
161 		DL = 0;
162 	if (cgetnum(bp, "cl", &CL) == -1)
163 		CL = 0;
164 	if (cgetnum(bp, "et", &ET) == -1)
165 		ET = 10;
166 }
167 
168 char *
169 getremote(host)
170 	char *host;
171 {
172 	register char *cp;
173 	static char *next;
174 	static int lookedup = 0;
175 
176 	if (!lookedup) {
177 		if (host == NOSTR && (host = getenv("HOST")) == NOSTR) {
178 			fprintf(stderr, "tip: no host specified\n");
179 			exit(3);
180 		}
181 		getremcap(host);
182 		next = DV;
183 		lookedup++;
184 	}
185 	/*
186 	 * We return a new device each time we're called (to allow
187 	 *   a rotary action to be simulated)
188 	 */
189 	if (next == NOSTR)
190 		return (NOSTR);
191 	if ((cp = index(next, ',')) == NULL) {
192 		DV = next;
193 		next = NOSTR;
194 	} else {
195 		*cp++ = '\0';
196 		DV = next;
197 		next = cp;
198 	}
199 	return (DV);
200 }
201