1/*
2 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3 * Use is subject to license terms.
4 */
5
6/*
7 * Copyright (c) 1983 Regents of the University of California.
8 * All rights reserved.  The Berkeley software License Agreement
9 * specifies the terms and conditions for redistribution.
10 */
11
12#pragma ident	"%Z%%M%	%I%	%E% SMI"
13
14#include "tip.h"
15
16/*
17 * Attributes to be gleened from remote host description
18 *   data base.
19 */
20static char **caps[] = {
21	&AT, &DV, &CM, &CU, &EL, &IE, &OE, &PN, &PR, &DI,
22	&ES, &EX, &FO, &RC, &RE, &PA
23};
24
25static char *capstrings[] = {
26	"at", "dv", "cm", "cu", "el", "ie", "oe", "pn", "pr",
27	"di", "es", "ex", "fo", "rc", "re", "pa", 0
28};
29
30extern char *rgetstr(char *, char **);
31
32static void
33getremcap(char *host)
34{
35	int stat;
36	char tbuf[BUFSIZ];
37	static char buf[BUFSIZ/2];
38	char *bp = buf;
39	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		(void) 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		(void) 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		(void) 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		(void) vstring("es", ES);
135	if (FO != NOSTR)
136		(void) vstring("fo", FO);
137	if (PR != NOSTR)
138		(void) vstring("pr", PR);
139	if (RC != NOSTR)
140		(void) 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
149char *
150getremote(char *host)
151{
152	char *cp;
153	static char *next;
154	static int lookedup = 0;
155
156	if (!lookedup) {
157		if (host == NOSTR && (host = getenv("HOST")) == NOSTR) {
158			(void) fprintf(stderr, "tip: no host specified\n");
159			exit(3);
160		}
161		getremcap(host);
162		next = DV;
163		lookedup++;
164	}
165	/*
166	 * We return a new device each time we're called (to allow
167	 *   a rotary action to be simulated)
168	 */
169	if (next == NOSTR)
170		return (NOSTR);
171	if ((cp = strchr(next, ',')) == NULL) {
172		DV = next;
173		next = NOSTR;
174	} else {
175		*cp++ = '\0';
176		DV = next;
177		next = cp;
178	}
179	return (DV);
180}
181