Clean up sysconf and associated headers.
[dragonfly.git] / usr.bin / tip / tip / remote.c
1 /*
2  * Copyright (c) 1992, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by the University of
17  *      California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * @(#) Copyright (c) 1992, 1993 The Regents of the University of California.  All rights reserved.
35  * @(#)remote.c 8.1 (Berkeley) 6/6/93
36  * $FreeBSD: src/usr.bin/tip/tip/remote.c,v 1.4 1999/08/28 01:06:35 peter Exp $
37  * $DragonFly: src/usr.bin/tip/tip/remote.c,v 1.4 2005/04/19 05:32:02 cpressey Exp $
38  */
39
40 #include <err.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43
44 #include "tipconf.h"
45 #include "tip.h"
46 #include "pathnames.h"
47
48 /*
49  * Attributes to be gleened from remote host description
50  *   data base.
51  */
52 static char **caps[] = {
53         &AT, &DV, &CM, &CU, &EL, &IE, &OE, &PN, &PR, &DI,
54         &ES, &EX, &FO, &RC, &RE, &PA, &LI, &LO
55 };
56
57 static char *capstrings[] = {
58         "at", "dv", "cm", "cu", "el", "ie", "oe", "pn", "pr",
59         "di", "es", "ex", "fo", "rc", "re", "pa", "li", "lo", 0
60 };
61
62 static char     *db_array[3] = { _PATH_REMOTE, 0, 0 };
63
64 #define cgetflag(f)     (cgetcap(bp, f, ':') != NULL)
65
66 static void getremcap(char *);
67
68 /*
69         Expand the start tilde sequence at the start of the
70         specified path. Optionally, free space allocated to
71         path before reinitializing it.
72 */
73 static int
74 expand_tilde (char **path, void (*free) (char *p))
75 {
76         int rc = 0;
77         char buffer [PATH_MAX];
78         char *tailp;
79         if ((tailp = strchr (*path + 1, '/')) != NULL)
80         {
81                 struct passwd *pwd;
82                 *tailp++ = '\0';
83                 if (*(*path + 1) == '\0')
84                         strcpy (buffer, getlogin ());
85                 else
86                         strcpy (buffer, *path + 1);
87                 if ((pwd = getpwnam (buffer)) != NULL)
88                 {
89                         strcpy (buffer, pwd->pw_dir);
90                         strcat (buffer, "/");
91                         strcat (buffer, tailp);
92                         if (free)
93                                 free (*path);
94                         *path = strdup (buffer);
95                         rc++;
96                 }
97                 return rc;
98         }
99         return (-1);
100 }
101
102 static void
103 getremcap(host)
104         register char *host;
105 {
106         register char **p, ***q;
107         char *bp;
108         char *rempath;
109         int   stat;
110
111         rempath = getenv("REMOTE");
112         if (rempath != NULL)
113                 if (*rempath != '/')
114                         /* we have an entry */
115                         cgetset(rempath);
116                 else {  /* we have a path */
117                         db_array[1] = rempath;
118                         db_array[2] = _PATH_REMOTE;
119                 }
120
121         if ((stat = cgetent(&bp, db_array, host)) < 0) {
122                 if (DV ||
123                     (host[0] == '/' && access(DV = host, R_OK | W_OK) == 0)) {
124                         CU = DV;
125                         HO = host;
126                         HW = 1;
127                         DU = 0;
128                         if (!BR)
129                                 BR = DEFBR;
130                         FS = DEFFS;
131                         return;
132                 }
133                 switch(stat) {
134                 case -1:
135                         warnx("unknown host %s", host);
136                         break;
137                 case -2:
138                         warnx("can't open host description file");
139                         break;
140                 case -3:
141                         warnx("possible reference loop in host description file");
142                         break;
143                 }
144                 exit(3);
145         }
146
147         for (p = capstrings, q = caps; *p != NULL; p++, q++)
148                 if (**q == NULL)
149                         cgetstr(bp, *p, *q);
150         if (!BR && (cgetnum(bp, "br", &BR) == -1))
151                 BR = DEFBR;
152         if (cgetnum(bp, "fs", &FS) == -1)
153                 FS = DEFFS;
154         if (DU < 0)
155                 DU = 0;
156         else
157                 DU = cgetflag("du");
158         if (DV == NULL) {
159                 fprintf(stderr, "%s: missing device spec\n", host);
160                 exit(3);
161         }
162         if (DU && CU == NULL)
163                 CU = DV;
164         if (DU && PN == NULL) {
165                 fprintf(stderr, "%s: missing phone number\n", host);
166                 exit(3);
167         }
168
169         HD = cgetflag("hd");
170
171         /*
172          * This effectively eliminates the "hw" attribute
173          *   from the description file
174          */
175         if (!HW)
176                 HW = (CU == NULL) || (DU && equal(DV, CU));
177         HO = host;
178
179         /*
180                 If login script, verify access
181         */
182         if (LI != NULL) {
183                 if (*LI == '~')
184                         (void) expand_tilde (&LI, NULL);
185                 if (access (LI, F_OK | X_OK) != 0) {
186                         printf("tip (warning): can't open login script \"%s\"\n", LI);
187                         LI = NULL;
188                 }
189         }
190
191         /*
192                 If logout script, verify access
193         */
194         if (LO != NULL) {
195                 if (*LO == '~')
196                         (void) expand_tilde (&LO, NULL);
197                 if (access (LO, F_OK | X_OK) != 0) {
198                         printf("tip (warning): can't open logout script \"%s\"\n", LO);
199                         LO = NULL;
200                 }
201         }
202
203         /*
204          * see if uppercase mode should be turned on initially
205          */
206         if (cgetflag("ra"))
207                 boolean(value(RAISE)) = 1;
208         if (cgetflag("ec"))
209                 boolean(value(ECHOCHECK)) = 1;
210         if (cgetflag("be"))
211                 boolean(value(BEAUTIFY)) = 1;
212         if (cgetflag("nb"))
213                 boolean(value(BEAUTIFY)) = 0;
214         if (cgetflag("sc"))
215                 boolean(value(SCRIPT)) = 1;
216         if (cgetflag("tb"))
217                 boolean(value(TABEXPAND)) = 1;
218         if (cgetflag("vb"))
219                 boolean(value(VERBOSE)) = 1;
220         if (cgetflag("nv"))
221                 boolean(value(VERBOSE)) = 0;
222         if (cgetflag("ta"))
223                 boolean(value(TAND)) = 1;
224         if (cgetflag("nt"))
225                 boolean(value(TAND)) = 0;
226         if (cgetflag("rw"))
227                 boolean(value(RAWFTP)) = 1;
228         if (cgetflag("hd"))
229                 boolean(value(HALFDUPLEX)) = 1;
230         if (RE == NULL)
231                 RE = (char *)"tip.record";
232         if (EX == NULL)
233                 EX = (char *)"\t\n\b\f";
234         if (ES != NULL)
235                 vstring("es", ES);
236         if (FO != NULL)
237                 vstring("fo", FO);
238         if (PR != NULL)
239                 vstring("pr", PR);
240         if (RC != NULL)
241                 vstring("rc", RC);
242         if (cgetnum(bp, "dl", &DL) == -1)
243                 DL = 0;
244         if (cgetnum(bp, "cl", &CL) == -1)
245                 CL = 0;
246         if (cgetnum(bp, "et", &ET) == -1)
247                 ET = 10;
248 }
249
250 char *
251 getremote(host)
252         char *host;
253 {
254         register char *cp;
255         static char *next;
256         static int lookedup = 0;
257
258         if (!lookedup) {
259                 if (host == NULL && (host = getenv("HOST")) == NULL)
260                         errx(3, "no host specified");
261                 getremcap(host);
262                 next = DV;
263                 lookedup++;
264         }
265         /*
266          * We return a new device each time we're called (to allow
267          *   a rotary action to be simulated)
268          */
269         if (next == NULL)
270                 return (NULL);
271         if ((cp = index(next, ',')) == NULL) {
272                 DV = next;
273                 next = NULL;
274         } else {
275                 *cp++ = '\0';
276                 DV = next;
277                 next = cp;
278         }
279         return (DV);
280 }