Home | History | Annotate | Line # | Download | only in tip
      1 /*	$NetBSD: remote.c,v 1.21 2024/09/20 19:27:03 andvar Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the University nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 #ifndef lint
     35 __COPYRIGHT("@(#) Copyright (c) 1992, 1993\
     36  The Regents of the University of California.  All rights reserved.");
     37 #endif /* not lint */
     38 
     39 #ifndef lint
     40 #if 0
     41 static char sccsid[] = "@(#)remote.c	8.1 (Berkeley) 6/6/93";
     42 #endif
     43 __RCSID("$NetBSD: remote.c,v 1.21 2024/09/20 19:27:03 andvar Exp $");
     44 #endif /* not lint */
     45 
     46 #include "pathnames.h"
     47 #include "tip.h"
     48 
     49 /*
     50  * Attributes to be gleaned from remote host description
     51  *   data base.
     52  */
     53 static char **caps[] = {
     54 	&AT, &DV, &CM, &CU, &EL, &IE, &OE, &PN, &PR, &DI,
     55 	&ES, &EX, &FO, &RC, &RE, &PA
     56 };
     57 
     58 static const char *capstrings[] = {
     59 	"at", "dv", "cm", "cu", "el", "ie", "oe", "pn", "pr",
     60 	"di", "es", "ex", "fo", "rc", "re", "pa", 0
     61 };
     62 
     63 static const char	*db_array[3] = { _PATH_REMOTE, 0, 0 };
     64 
     65 #define cgetflag(f)	(cgetcap(bp, f, ':') != NULL)
     66 
     67 static	void	getremcap(char *);
     68 
     69 static char tiprecord[] = "tip.record";
     70 static char wspace[] = "\t\n\b\f";
     71 
     72 static void
     73 getremcap(char *host)
     74 {
     75 	const char **p;
     76 	char ***q;
     77 	char *bp;
     78 	char *rempath;
     79 	int   status;
     80 
     81 	rempath = getenv("REMOTE");
     82 	if (rempath != NULL) {
     83 		if (*rempath != '/')
     84 			/* we have an entry */
     85 			(void)cgetset(rempath);
     86 		else {	/* we have a path */
     87 			db_array[1] = rempath;
     88 			db_array[2] = _PATH_REMOTE;
     89 		}
     90 	}
     91 	if ((status = cgetent(&bp, db_array, host)) < 0) {
     92 		if (DV ||
     93 		    (host[0] == '/' && access(DV = host, R_OK | W_OK) == 0)) {
     94 			CU = DV;
     95 			HO = host;
     96 			HW = 1;
     97 			DU = 0;
     98 			if (!BR)
     99 				BR = DEFBR;
    100 			FS = DEFFS;
    101 			return;
    102 		}
    103 		switch(status) {
    104 		case -1:
    105 			warnx("unknown host %s", host);
    106 			break;
    107 		case -2:
    108 			warnx("can't open host description file");
    109 			break;
    110 		case -3:
    111 			warnx("possible reference loop in host "
    112 			    "description file");
    113 
    114 			break;
    115 		}
    116 		exit(3);
    117 	}
    118 
    119 	for (p = capstrings, q = caps; *p != NULL; p++, q++)
    120 		if (**q == NULL)
    121 			(void)cgetstr(bp, *p, *q);
    122 	if (!BR && (cgetnum(bp, "br", &BR) == -1))
    123 		BR = DEFBR;
    124 	if (cgetnum(bp, "fs", &FS) == -1)
    125 		FS = DEFFS;
    126 	if (DU < 0)
    127 		DU = 0;
    128 	else
    129 		DU = cgetflag("du");
    130 	if (DV == NULL) {
    131 		errx(3, "%s: missing device spec", host);
    132 	}
    133 	if (DU && CU == NULL)
    134 		CU = DV;
    135 	if (DU && PN == NULL) {
    136 		errx(3, "%s: missing phone number", host);
    137 	}
    138 
    139 	HD = cgetflag("hd");
    140 
    141 	/*
    142 	 * This effectively eliminates the "hw" attribute
    143 	 *   from the description file
    144 	 */
    145 	if (!HW)
    146 		HW = (CU == NULL) || (DU && strcmp(DV, CU) == 0);
    147 	HO = host;
    148 	/*
    149 	 * see if uppercase mode should be turned on initially
    150 	 */
    151 	if (cgetflag("ra"))
    152 		setboolean(value(RAISE), 1);
    153 	if (cgetflag("ec"))
    154 		setboolean(value(ECHOCHECK), 1);
    155 	if (cgetflag("be"))
    156 		setboolean(value(BEAUTIFY), 1);
    157 	if (cgetflag("nb"))
    158 		setboolean(value(BEAUTIFY), 0);
    159 	if (cgetflag("sc"))
    160 		setboolean(value(SCRIPT), 1);
    161 	if (cgetflag("tb"))
    162 		setboolean(value(TABEXPAND), 1);
    163 	if (cgetflag("vb"))
    164 		setboolean(value(VERBOSE), 1);
    165 	if (cgetflag("nv"))
    166 		setboolean(value(VERBOSE), 0);
    167 	if (cgetflag("ta"))
    168 		setboolean(value(TAND), 1);
    169 	if (cgetflag("nt"))
    170 		setboolean(value(TAND), 0);
    171 	if (cgetflag("rw"))
    172 		setboolean(value(RAWFTP), 1);
    173 	if (cgetflag("hd"))
    174 		setboolean(value(HALFDUPLEX), 1);
    175 	if (cgetflag("dc"))
    176 		DC = 1;
    177 	if (cgetflag("hf"))
    178 		setboolean(value(HARDWAREFLOW), 1);
    179 	if (RE == NULL)
    180 		RE = tiprecord;
    181 	if (EX == NULL)
    182 		EX = wspace;
    183 	if (ES != NULL)
    184 		(void)vstring("es", ES);
    185 	if (FO != NULL)
    186 		(void)vstring("fo", FO);
    187 	if (PR != NULL)
    188 		(void)vstring("pr", PR);
    189 	if (RC != NULL)
    190 		(void)vstring("rc", RC);
    191 	if (cgetnum(bp, "dl", &DL) == -1)
    192 		DL = 0;
    193 	if (cgetnum(bp, "cl", &CL) == -1)
    194 		CL = 0;
    195 	if (cgetnum(bp, "et", &ET) == -1)
    196 		ET = 10;
    197 }
    198 
    199 char *
    200 getremote(char *host)
    201 {
    202 	char *cp;
    203 	static char *next;
    204 	static int lookedup = 0;
    205 
    206 	if (!lookedup) {
    207 		if (host == NULL && (host = getenv("HOST")) == NULL) {
    208 			errx(3, "no host specified");
    209 		}
    210 		getremcap(host);
    211 		next = DV;
    212 		lookedup++;
    213 	}
    214 	/*
    215 	 * We return a new device each time we're called (to allow
    216 	 *   a rotary action to be simulated)
    217 	 */
    218 	if (next == NULL)
    219 		return (NULL);
    220 	if ((cp = strchr(next, ',')) == NULL) {
    221 		DV = next;
    222 		next = NULL;
    223 	} else {
    224 		*cp++ = '\0';
    225 		DV = next;
    226 		next = cp;
    227 	}
    228 	return (DV);
    229 }
    230