xref: /illumos-gate/usr/src/cmd/tip/remote.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 /*
6  * Copyright (c) 1983 Regents of the University of California.
7  * All rights reserved.  The Berkeley software License Agreement
8  * specifies the terms and conditions for redistribution.
9  */
10 
11 #ident	"%Z%%M%	%I%	%E% SMI"	/* from UCB 5.3 4/30/86 */
12 
13 #include "tip.h"
14 
15 /*
16  * Attributes to be gleened from remote host description
17  *   data base.
18  */
19 static char **caps[] = {
20 	&AT, &DV, &CM, &CU, &EL, &IE, &OE, &PN, &PR, &DI,
21 	&ES, &EX, &FO, &RC, &RE, &PA
22 };
23 
24 static char *capstrings[] = {
25 	"at", "dv", "cm", "cu", "el", "ie", "oe", "pn", "pr",
26 	"di", "es", "ex", "fo", "rc", "re", "pa", 0
27 };
28 
29 char *rgetstr();
30 
31 static
32 getremcap(host)
33 	register char *host;
34 {
35 	int stat;
36 	char tbuf[BUFSIZ];
37 	static char buf[BUFSIZ/2];
38 	char *bp = buf;
39 	register char **p, ***q;
40 
41 	if ((stat = rgetent(tbuf, host, sizeof (tbuf))) <= 0) {
42 		if (DV ||
43 		    host[0] == '/' && access(DV = host, R_OK | W_OK) == 0) {
44 			/*
45 			 * If the user specifies a device on the commandline,
46 			 * don't trust it.
47 			 */
48 			if (host[0] == '/')
49 				trusted_device = 0;
50 			CU = DV;
51 			HO = host;
52 			HW = 1;
53 			DU = 0;
54 			if (!BR)
55 				BR = DEFBR;
56 			FS = DEFFS;
57 			RE = (char *)"tip.record";
58 			EX = (char *)"\t\n\b\f";
59 			DL = 0;
60 			CL = 0;
61 			ET = 10;
62 			return;
63 		}
64 		fprintf(stderr, stat == 0 ?
65 			"tip: unknown host %s\n" :
66 			"tip: can't open host description file\n", host);
67 		exit(3);
68 	}
69 
70 	for (p = capstrings, q = caps; *p != NULL; p++, q++)
71 		if (**q == NULL)
72 			**q = rgetstr(*p, &bp);
73 	if (!BR && (BR = rgetnum("br")) < 0)
74 		BR = DEFBR;
75 	if ((FS = rgetnum("fs")) < 0)
76 		FS = DEFFS;
77 	if (DU < 0)
78 		DU = 0;
79 	else
80 		DU = rgetflag("du");
81 	if (DV == NOSTR) {
82 		fprintf(stderr, "%s: missing device spec\n", host);
83 		exit(3);
84 	}
85 	if (DU && CU == NOSTR)
86 		CU = DV;
87 	if (DU && PN == NOSTR) {
88 		fprintf(stderr, "%s: missing phone number\n", host);
89 		exit(3);
90 	}
91 	DB = rgetflag("db");
92 
93 	/*
94 	 * This effectively eliminates the "hw" attribute
95 	 *   from the description file
96 	 */
97 	if (!HW)
98 		HW = (CU == NOSTR) || (DU && equal(DV, CU));
99 	HO = host;
100 	/*
101 	 * see if uppercase mode should be turned on initially
102 	 */
103 	if (rgetflag("ra"))
104 		boolean(value(RAISE)) = 1;
105 	if (rgetflag("ec"))
106 		boolean(value(ECHOCHECK)) = 1;
107 	if (rgetflag("be"))
108 		boolean(value(BEAUTIFY)) = 1;
109 	if (rgetflag("nb"))
110 		boolean(value(BEAUTIFY)) = 0;
111 	if (rgetflag("sc"))
112 		boolean(value(SCRIPT)) = 1;
113 	if (rgetflag("tb"))
114 		boolean(value(TABEXPAND)) = 1;
115 	if (rgetflag("vb"))
116 		boolean(value(VERBOSE)) = 1;
117 	if (rgetflag("nv"))
118 		boolean(value(VERBOSE)) = 0;
119 	if (rgetflag("ta"))
120 		boolean(value(TAND)) = 1;
121 	if (rgetflag("nt"))
122 		boolean(value(TAND)) = 0;
123 	if (rgetflag("rw"))
124 		boolean(value(RAWFTP)) = 1;
125 	if (rgetflag("hd"))
126 		boolean(value(HALFDUPLEX)) = 1;
127 	if (rgetflag("hf"))
128 		boolean(value(HARDWAREFLOW)) = 1;
129 	if (RE == NULL)
130 		RE = (char *)"tip.record";
131 	if (EX == NULL)
132 		EX = (char *)"\t\n\b\f";
133 	if (ES != NOSTR)
134 		vstring("es", ES);
135 	if (FO != NOSTR)
136 		vstring("fo", FO);
137 	if (PR != NOSTR)
138 		vstring("pr", PR);
139 	if (RC != NOSTR)
140 		vstring("rc", RC);
141 	if ((DL = rgetnum("dl")) < 0)
142 		DL = 0;
143 	if ((CL = rgetnum("cl")) < 0)
144 		CL = 0;
145 	if ((ET = rgetnum("et")) < 0)
146 		ET = 10;
147 }
148 
149 char *
150 getremote(host)
151 	char *host;
152 {
153 	register char *cp;
154 	static char *next;
155 	static int lookedup = 0;
156 
157 	if (!lookedup) {
158 		if (host == NOSTR && (host = getenv("HOST")) == NOSTR) {
159 			fprintf(stderr, "tip: no host specified\n");
160 			exit(3);
161 		}
162 		getremcap(host);
163 		next = DV;
164 		lookedup++;
165 	}
166 	/*
167 	 * We return a new device each time we're called (to allow
168 	 *   a rotary action to be simulated)
169 	 */
170 	if (next == NOSTR)
171 		return (NOSTR);
172 	if ((cp = strchr(next, ',')) == NULL) {
173 		DV = next;
174 		next = NOSTR;
175 	} else {
176 		*cp++ = '\0';
177 		DV = next;
178 		next = cp;
179 	}
180 	return (DV);
181 }
182