Home | History | Annotate | Line # | Download | only in telnet
commands.c revision 1.45
      1  1.45       cgd /*	$NetBSD: commands.c,v 1.45 2001/01/16 02:50:31 cgd Exp $	*/
      2  1.26    itojun 
      3  1.26    itojun /*
      4  1.26    itojun  * Copyright (C) 1997 and 1998 WIDE Project.
      5  1.26    itojun  * All rights reserved.
      6  1.26    itojun  *
      7  1.26    itojun  * Redistribution and use in source and binary forms, with or without
      8  1.26    itojun  * modification, are permitted provided that the following conditions
      9  1.26    itojun  * are met:
     10  1.26    itojun  * 1. Redistributions of source code must retain the above copyright
     11  1.26    itojun  *    notice, this list of conditions and the following disclaimer.
     12  1.26    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.26    itojun  *    notice, this list of conditions and the following disclaimer in the
     14  1.26    itojun  *    documentation and/or other materials provided with the distribution.
     15  1.26    itojun  * 3. Neither the name of the project nor the names of its contributors
     16  1.26    itojun  *    may be used to endorse or promote products derived from this software
     17  1.26    itojun  *    without specific prior written permission.
     18  1.26    itojun  *
     19  1.26    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  1.26    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.26    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.26    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  1.26    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.26    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.26    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.26    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.26    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.26    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.26    itojun  * SUCH DAMAGE.
     30  1.26    itojun  */
     31  1.13   thorpej 
     32   1.1       cgd /*
     33   1.5       cgd  * Copyright (c) 1988, 1990, 1993
     34   1.5       cgd  *	The Regents of the University of California.  All rights reserved.
     35   1.1       cgd  *
     36   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     37   1.1       cgd  * modification, are permitted provided that the following conditions
     38   1.1       cgd  * are met:
     39   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     40   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     41   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     42   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     43   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     44   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     45   1.1       cgd  *    must display the following acknowledgement:
     46   1.1       cgd  *	This product includes software developed by the University of
     47   1.1       cgd  *	California, Berkeley and its contributors.
     48   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     49   1.1       cgd  *    may be used to endorse or promote products derived from this software
     50   1.1       cgd  *    without specific prior written permission.
     51   1.1       cgd  *
     52   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62   1.1       cgd  * SUCH DAMAGE.
     63   1.1       cgd  */
     64   1.1       cgd 
     65  1.15  christos #include <sys/cdefs.h>
     66   1.1       cgd #ifndef lint
     67  1.13   thorpej #if 0
     68  1.13   thorpej static char sccsid[] = "@(#)commands.c	8.4 (Berkeley) 5/30/95";
     69  1.13   thorpej #else
     70  1.45       cgd __RCSID("$NetBSD: commands.c,v 1.45 2001/01/16 02:50:31 cgd Exp $");
     71  1.13   thorpej #endif
     72   1.1       cgd #endif /* not lint */
     73   1.1       cgd 
     74   1.1       cgd #if	defined(unix)
     75   1.1       cgd #include <sys/param.h>
     76   1.5       cgd #if	defined(CRAY) || defined(sysV88)
     77   1.1       cgd #include <sys/types.h>
     78   1.1       cgd #endif
     79   1.1       cgd #include <sys/file.h>
     80   1.1       cgd #else
     81   1.1       cgd #include <sys/types.h>
     82   1.1       cgd #endif	/* defined(unix) */
     83  1.15  christos #include <sys/wait.h>
     84   1.1       cgd #include <sys/socket.h>
     85   1.1       cgd #include <netinet/in.h>
     86  1.15  christos #include <arpa/inet.h>
     87   1.1       cgd #ifdef	CRAY
     88   1.1       cgd #include <fcntl.h>
     89   1.1       cgd #endif	/* CRAY */
     90   1.1       cgd 
     91   1.1       cgd #include <signal.h>
     92   1.1       cgd #include <netdb.h>
     93   1.1       cgd #include <ctype.h>
     94   1.1       cgd #include <pwd.h>
     95  1.15  christos #ifdef __STDC__
     96  1.15  christos #include <stdarg.h>
     97  1.15  christos #else
     98   1.1       cgd #include <varargs.h>
     99  1.15  christos #endif
    100   1.1       cgd #include <errno.h>
    101  1.15  christos #include <unistd.h>
    102   1.1       cgd 
    103   1.1       cgd #include <arpa/telnet.h>
    104  1.10       jtk #include <sys/cdefs.h>
    105  1.10       jtk #define P __P
    106   1.1       cgd 
    107   1.1       cgd #include "general.h"
    108   1.1       cgd 
    109   1.1       cgd #include "ring.h"
    110   1.1       cgd 
    111   1.1       cgd #include "externs.h"
    112   1.1       cgd #include "defines.h"
    113   1.1       cgd #include "types.h"
    114  1.15  christos #include <libtelnet/misc.h>
    115  1.43   thorpej #ifdef AUTHENTICATION
    116  1.43   thorpej #include <libtelnet/auth.h>
    117  1.43   thorpej #endif
    118  1.43   thorpej #ifdef ENCRYPTION
    119  1.43   thorpej #include <libtelnet/encrypt.h>
    120  1.43   thorpej #endif
    121   1.1       cgd 
    122   1.5       cgd #if !defined(CRAY) && !defined(sysV88)
    123   1.1       cgd #include <netinet/in_systm.h>
    124   1.1       cgd # if (defined(vax) || defined(tahoe) || defined(hp300)) && !defined(ultrix)
    125   1.1       cgd # include <machine/endian.h>
    126   1.1       cgd # endif /* vax */
    127   1.5       cgd #endif /* !defined(CRAY) && !defined(sysV88) */
    128   1.1       cgd #include <netinet/ip.h>
    129   1.1       cgd 
    130   1.1       cgd 
    131  1.10       jtk #ifndef	MAXHOSTNAMELEN
    132  1.10       jtk #define	MAXHOSTNAMELEN 64
    133  1.45       cgd #endif	/* MAXHOSTNAMELEN */
    134   1.1       cgd 
    135   1.1       cgd #if	defined(IPPROTO_IP) && defined(IP_TOS)
    136   1.1       cgd int tos = -1;
    137   1.1       cgd #endif	/* defined(IPPROTO_IP) && defined(IP_TOS) */
    138   1.1       cgd 
    139   1.1       cgd char	*hostname;
    140   1.1       cgd static char _hostname[MAXHOSTNAMELEN];
    141   1.1       cgd 
    142   1.1       cgd typedef struct {
    143   1.1       cgd 	char	*name;		/* command name */
    144   1.1       cgd 	char	*help;		/* help string (NULL for no help) */
    145  1.15  christos 	int	(*handler)	/* routine which executes command */
    146  1.15  christos 			    P((int, char *[]));
    147   1.1       cgd 	int	needconnect;	/* Do we need to be connected to execute? */
    148   1.1       cgd } Command;
    149   1.1       cgd 
    150   1.1       cgd static char line[256];
    151   1.1       cgd static char saveline[256];
    152   1.1       cgd static int margc;
    153   1.1       cgd static char *margv[20];
    154   1.1       cgd 
    155  1.15  christos static void makeargv P((void));
    156  1.20       mrg static int special P((char *));
    157  1.15  christos static char *control P((cc_t));
    158  1.15  christos static int sendcmd P((int, char **));
    159  1.15  christos static int send_esc P((char *));
    160  1.15  christos static int send_docmd P((char *));
    161  1.15  christos static int send_dontcmd P((char *));
    162  1.15  christos static int send_willcmd P((char *));
    163  1.15  christos static int send_wontcmd P((char *));
    164  1.15  christos static int send_help P((char *));
    165  1.15  christos static int lclchars P((int));
    166  1.15  christos static int togdebug P((int));
    167  1.15  christos static int togcrlf P((int));
    168  1.15  christos static int togbinary P((int));
    169  1.15  christos static int togrbinary P((int));
    170  1.15  christos static int togxbinary P((int));
    171  1.15  christos static int togglehelp P((int));
    172  1.15  christos static void settogglehelp P((int));
    173  1.15  christos static int toggle P((int, char *[]));
    174  1.15  christos static struct setlist *getset P((char *));
    175  1.15  christos static int setcmd P((int, char *[]));
    176  1.15  christos static int unsetcmd P((int, char *[]));
    177  1.15  christos static int dokludgemode P((int));
    178  1.15  christos static int dolinemode P((int));
    179  1.15  christos static int docharmode P((int));
    180  1.15  christos static int dolmmode P((int, int ));
    181  1.15  christos static int modecmd P((int, char *[]));
    182  1.15  christos static int display P((int, char *[]));
    183  1.15  christos static int setescape P((int, char *[]));
    184  1.15  christos static int togcrmod P((int, char *[]));
    185  1.20       mrg static int bye P((int, char *[]));
    186  1.15  christos static void slc_help P((int));
    187  1.15  christos static struct slclist *getslc P((char *));
    188  1.20       mrg static int slccmd P((int, char *[]));
    189  1.15  christos static struct env_lst *env_help P((unsigned char *, unsigned char *));
    190  1.15  christos static struct envlist *getenvcmd P((char *));
    191  1.15  christos #ifdef AUTHENTICATION
    192  1.25  christos static int auth_help P((char *));
    193  1.15  christos #endif
    194  1.15  christos #if	defined(unix) && defined(TN3270)
    195  1.15  christos static void filestuff P((int));
    196  1.15  christos #endif
    197  1.20       mrg static int status P((int, char *[]));
    198  1.26    itojun static const char *sockaddr_ntop __P((struct sockaddr *));
    199  1.15  christos typedef int (*intrtn_t) P((int, char **));
    200  1.15  christos static int call P((intrtn_t, ...));
    201  1.15  christos static Command *getcmd P((char *));
    202  1.20       mrg static int help P((int, char *[]));
    203  1.15  christos 
    204   1.1       cgd     static void
    205   1.1       cgd makeargv()
    206   1.1       cgd {
    207   1.1       cgd     register char *cp, *cp2, c;
    208   1.1       cgd     register char **argp = margv;
    209   1.1       cgd 
    210   1.1       cgd     margc = 0;
    211   1.1       cgd     cp = line;
    212   1.1       cgd     if (*cp == '!') {		/* Special case shell escape */
    213   1.1       cgd 	strcpy(saveline, line);	/* save for shell command */
    214   1.1       cgd 	*argp++ = "!";		/* No room in string to get this */
    215   1.1       cgd 	margc++;
    216   1.1       cgd 	cp++;
    217   1.1       cgd     }
    218  1.15  christos     while ((c = *cp) != '\0') {
    219   1.1       cgd 	register int inquote = 0;
    220  1.25  christos 	while (isspace((unsigned char)c))
    221   1.1       cgd 	    c = *++cp;
    222   1.1       cgd 	if (c == '\0')
    223   1.1       cgd 	    break;
    224   1.1       cgd 	*argp++ = cp;
    225   1.1       cgd 	margc += 1;
    226   1.1       cgd 	for (cp2 = cp; c != '\0'; c = *++cp) {
    227   1.1       cgd 	    if (inquote) {
    228   1.1       cgd 		if (c == inquote) {
    229   1.1       cgd 		    inquote = 0;
    230   1.1       cgd 		    continue;
    231   1.1       cgd 		}
    232   1.1       cgd 	    } else {
    233   1.1       cgd 		if (c == '\\') {
    234   1.1       cgd 		    if ((c = *++cp) == '\0')
    235   1.1       cgd 			break;
    236   1.1       cgd 		} else if (c == '"') {
    237   1.1       cgd 		    inquote = '"';
    238   1.1       cgd 		    continue;
    239   1.1       cgd 		} else if (c == '\'') {
    240   1.1       cgd 		    inquote = '\'';
    241   1.1       cgd 		    continue;
    242  1.25  christos 		} else if (isspace((unsigned char)c))
    243   1.1       cgd 		    break;
    244   1.1       cgd 	    }
    245   1.1       cgd 	    *cp2++ = c;
    246   1.1       cgd 	}
    247   1.1       cgd 	*cp2 = '\0';
    248   1.1       cgd 	if (c == '\0')
    249   1.1       cgd 	    break;
    250   1.1       cgd 	cp++;
    251   1.1       cgd     }
    252   1.1       cgd     *argp++ = 0;
    253   1.1       cgd }
    254   1.1       cgd 
    255   1.1       cgd /*
    256   1.1       cgd  * Make a character string into a number.
    257   1.1       cgd  *
    258   1.1       cgd  * Todo:  1.  Could take random integers (12, 0x12, 012, 0b1).
    259   1.1       cgd  */
    260   1.1       cgd 
    261  1.15  christos 	static int
    262   1.1       cgd special(s)
    263   1.1       cgd 	register char *s;
    264   1.1       cgd {
    265   1.1       cgd 	register char c;
    266   1.1       cgd 	char b;
    267   1.1       cgd 
    268   1.1       cgd 	switch (*s) {
    269   1.1       cgd 	case '^':
    270   1.1       cgd 		b = *++s;
    271   1.1       cgd 		if (b == '?') {
    272   1.1       cgd 		    c = b | 0x40;		/* DEL */
    273   1.1       cgd 		} else {
    274   1.1       cgd 		    c = b & 0x1f;
    275   1.1       cgd 		}
    276   1.1       cgd 		break;
    277   1.1       cgd 	default:
    278   1.1       cgd 		c = *s;
    279   1.1       cgd 		break;
    280   1.1       cgd 	}
    281   1.1       cgd 	return c;
    282   1.1       cgd }
    283   1.1       cgd 
    284   1.1       cgd /*
    285   1.1       cgd  * Construct a control character sequence
    286   1.1       cgd  * for a special character.
    287   1.1       cgd  */
    288   1.1       cgd 	static char *
    289   1.1       cgd control(c)
    290   1.1       cgd 	register cc_t c;
    291   1.1       cgd {
    292   1.1       cgd 	static char buf[5];
    293   1.1       cgd 	/*
    294   1.1       cgd 	 * The only way I could get the Sun 3.5 compiler
    295   1.1       cgd 	 * to shut up about
    296   1.1       cgd 	 *	if ((unsigned int)c >= 0x80)
    297   1.1       cgd 	 * was to assign "c" to an unsigned int variable...
    298   1.1       cgd 	 * Arggg....
    299   1.1       cgd 	 */
    300   1.1       cgd 	register unsigned int uic = (unsigned int)c;
    301   1.1       cgd 
    302   1.1       cgd 	if (uic == 0x7f)
    303   1.1       cgd 		return ("^?");
    304   1.1       cgd 	if (c == (cc_t)_POSIX_VDISABLE) {
    305   1.1       cgd 		return "off";
    306   1.1       cgd 	}
    307   1.1       cgd 	if (uic >= 0x80) {
    308   1.1       cgd 		buf[0] = '\\';
    309   1.1       cgd 		buf[1] = ((c>>6)&07) + '0';
    310   1.1       cgd 		buf[2] = ((c>>3)&07) + '0';
    311   1.1       cgd 		buf[3] = (c&07) + '0';
    312   1.1       cgd 		buf[4] = 0;
    313   1.1       cgd 	} else if (uic >= 0x20) {
    314   1.1       cgd 		buf[0] = c;
    315   1.1       cgd 		buf[1] = 0;
    316   1.1       cgd 	} else {
    317   1.1       cgd 		buf[0] = '^';
    318   1.1       cgd 		buf[1] = '@'+c;
    319   1.1       cgd 		buf[2] = 0;
    320   1.1       cgd 	}
    321   1.1       cgd 	return (buf);
    322   1.1       cgd }
    323   1.1       cgd 
    324   1.1       cgd 
    325   1.1       cgd 
    326   1.1       cgd /*
    327   1.1       cgd  *	The following are data structures and routines for
    328   1.1       cgd  *	the "send" command.
    329   1.1       cgd  *
    330   1.1       cgd  */
    331  1.10       jtk 
    332   1.1       cgd struct sendlist {
    333   1.1       cgd     char	*name;		/* How user refers to it (case independent) */
    334   1.1       cgd     char	*help;		/* Help information (0 ==> no help) */
    335   1.1       cgd     int		needconnect;	/* Need to be connected */
    336   1.1       cgd     int		narg;		/* Number of arguments */
    337  1.15  christos     int		(*handler)	/* Routine to perform (for special ops) */
    338  1.15  christos 			    P((char *));
    339   1.1       cgd     int		nbyte;		/* Number of bytes to send this command */
    340   1.1       cgd     int		what;		/* Character to be sent (<0 ==> special) */
    341   1.1       cgd };
    342   1.1       cgd 
    343   1.1       cgd 
    345   1.1       cgd static struct sendlist Sendlist[] = {
    346   1.1       cgd     { "ao",	"Send Telnet Abort output",		1, 0, 0, 2, AO },
    347   1.1       cgd     { "ayt",	"Send Telnet 'Are You There'",		1, 0, 0, 2, AYT },
    348   1.1       cgd     { "brk",	"Send Telnet Break",			1, 0, 0, 2, BREAK },
    349   1.1       cgd     { "break",	0,					1, 0, 0, 2, BREAK },
    350   1.1       cgd     { "ec",	"Send Telnet Erase Character",		1, 0, 0, 2, EC },
    351   1.1       cgd     { "el",	"Send Telnet Erase Line",		1, 0, 0, 2, EL },
    352   1.1       cgd     { "escape",	"Send current escape character",	1, 0, send_esc, 1, 0 },
    353   1.1       cgd     { "ga",	"Send Telnet 'Go Ahead' sequence",	1, 0, 0, 2, GA },
    354   1.1       cgd     { "ip",	"Send Telnet Interrupt Process",	1, 0, 0, 2, IP },
    355   1.1       cgd     { "intp",	0,					1, 0, 0, 2, IP },
    356   1.1       cgd     { "interrupt", 0,					1, 0, 0, 2, IP },
    357   1.1       cgd     { "intr",	0,					1, 0, 0, 2, IP },
    358   1.1       cgd     { "nop",	"Send Telnet 'No operation'",		1, 0, 0, 2, NOP },
    359   1.1       cgd     { "eor",	"Send Telnet 'End of Record'",		1, 0, 0, 2, EOR },
    360   1.1       cgd     { "abort",	"Send Telnet 'Abort Process'",		1, 0, 0, 2, ABORT },
    361   1.1       cgd     { "susp",	"Send Telnet 'Suspend Process'",	1, 0, 0, 2, SUSP },
    362   1.1       cgd     { "eof",	"Send Telnet End of File Character",	1, 0, 0, 2, xEOF },
    363   1.1       cgd     { "synch",	"Perform Telnet 'Synch operation'",	1, 0, dosynch, 2, 0 },
    364   1.1       cgd     { "getstatus", "Send request for STATUS",		1, 0, get_status, 6, 0 },
    365   1.1       cgd     { "?",	"Display send options",			0, 0, send_help, 0, 0 },
    366   1.1       cgd     { "help",	0,					0, 0, send_help, 0, 0 },
    367   1.1       cgd     { "do",	0,					0, 1, send_docmd, 3, 0 },
    368   1.1       cgd     { "dont",	0,					0, 1, send_dontcmd, 3, 0 },
    369   1.1       cgd     { "will",	0,					0, 1, send_willcmd, 3, 0 },
    370   1.1       cgd     { "wont",	0,					0, 1, send_wontcmd, 3, 0 },
    371   1.1       cgd     { 0 }
    372   1.1       cgd };
    373   1.1       cgd 
    374   1.1       cgd #define	GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
    375   1.1       cgd 				sizeof(struct sendlist)))
    376   1.1       cgd 
    377   1.1       cgd     static int
    378   1.1       cgd sendcmd(argc, argv)
    379   1.1       cgd     int  argc;
    380   1.1       cgd     char **argv;
    381   1.1       cgd {
    382   1.1       cgd     int count;		/* how many bytes we are going to need to send */
    383   1.1       cgd     int i;
    384   1.1       cgd     struct sendlist *s;	/* pointer to current command */
    385   1.1       cgd     int success = 0;
    386   1.1       cgd     int needconnect = 0;
    387   1.1       cgd 
    388   1.1       cgd     if (argc < 2) {
    389   1.1       cgd 	printf("need at least one argument for 'send' command\n");
    390   1.1       cgd 	printf("'send ?' for help\n");
    391   1.1       cgd 	return 0;
    392   1.1       cgd     }
    393   1.1       cgd     /*
    394   1.1       cgd      * First, validate all the send arguments.
    395   1.1       cgd      * In addition, we see how much space we are going to need, and
    396   1.1       cgd      * whether or not we will be doing a "SYNCH" operation (which
    397   1.1       cgd      * flushes the network queue).
    398   1.1       cgd      */
    399   1.1       cgd     count = 0;
    400   1.1       cgd     for (i = 1; i < argc; i++) {
    401   1.1       cgd 	s = GETSEND(argv[i]);
    402   1.1       cgd 	if (s == 0) {
    403   1.1       cgd 	    printf("Unknown send argument '%s'\n'send ?' for help.\n",
    404   1.1       cgd 			argv[i]);
    405   1.1       cgd 	    return 0;
    406   1.1       cgd 	} else if (Ambiguous(s)) {
    407   1.1       cgd 	    printf("Ambiguous send argument '%s'\n'send ?' for help.\n",
    408   1.1       cgd 			argv[i]);
    409   1.1       cgd 	    return 0;
    410   1.1       cgd 	}
    411   1.1       cgd 	if (i + s->narg >= argc) {
    412   1.1       cgd 	    fprintf(stderr,
    413   1.1       cgd 	    "Need %d argument%s to 'send %s' command.  'send %s ?' for help.\n",
    414   1.1       cgd 		s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
    415   1.1       cgd 	    return 0;
    416   1.1       cgd 	}
    417   1.1       cgd 	count += s->nbyte;
    418  1.15  christos 	if (s->handler == send_help) {
    419   1.1       cgd 	    send_help(NULL);
    420   1.1       cgd 	    return 0;
    421   1.1       cgd 	}
    422   1.1       cgd 
    423   1.1       cgd 	i += s->narg;
    424   1.1       cgd 	needconnect += s->needconnect;
    425   1.1       cgd     }
    426   1.1       cgd     if (!connected && needconnect) {
    427   1.1       cgd 	printf("?Need to be connected first.\n");
    428   1.1       cgd 	printf("'send ?' for help\n");
    429   1.1       cgd 	return 0;
    430   1.1       cgd     }
    431   1.1       cgd     /* Now, do we have enough room? */
    432   1.1       cgd     if (NETROOM() < count) {
    433   1.1       cgd 	printf("There is not enough room in the buffer TO the network\n");
    434   1.1       cgd 	printf("to process your request.  Nothing will be done.\n");
    435   1.1       cgd 	printf("('send synch' will throw away most data in the network\n");
    436   1.1       cgd 	printf("buffer, if this might help.)\n");
    437   1.1       cgd 	return 0;
    438   1.1       cgd     }
    439   1.1       cgd     /* OK, they are all OK, now go through again and actually send */
    440   1.1       cgd     count = 0;
    441   1.1       cgd     for (i = 1; i < argc; i++) {
    442   1.1       cgd 	if ((s = GETSEND(argv[i])) == 0) {
    443  1.15  christos 	    fprintf(stderr, "Telnet 'send' error - argument disappeared!\n");
    444   1.1       cgd 	    (void) quit(0, NULL);
    445   1.1       cgd 	    /*NOTREACHED*/
    446   1.1       cgd 	}
    447   1.1       cgd 	if (s->handler) {
    448  1.15  christos 	    count++;
    449   1.1       cgd 	    success += (*s->handler)(argv[i+1]);
    450   1.1       cgd 	    i += s->narg;
    451   1.3       cgd 	} else {
    452   1.3       cgd 	    NET2ADD(IAC, s->what);
    453   1.1       cgd 	    printoption("SENT", IAC, s->what);
    454   1.1       cgd 	}
    455   1.1       cgd     }
    456   1.1       cgd     return (count == success);
    457   1.1       cgd }
    458   1.1       cgd 
    459  1.15  christos     static int
    460  1.15  christos send_esc(s)
    461   1.1       cgd     char *s;
    462   1.1       cgd {
    463   1.1       cgd     NETADD(escape);
    464   1.1       cgd     return 1;
    465   1.1       cgd }
    466   1.1       cgd 
    467   1.1       cgd     static int
    468   1.1       cgd send_docmd(name)
    469   1.1       cgd     char *name;
    470   1.1       cgd {
    471   1.1       cgd     return(send_tncmd(send_do, "do", name));
    472   1.1       cgd }
    473   1.1       cgd 
    474   1.1       cgd     static int
    475   1.1       cgd send_dontcmd(name)
    476   1.1       cgd     char *name;
    477   1.1       cgd {
    478   1.1       cgd     return(send_tncmd(send_dont, "dont", name));
    479   1.1       cgd }
    480   1.1       cgd     static int
    481   1.1       cgd send_willcmd(name)
    482   1.1       cgd     char *name;
    483   1.1       cgd {
    484   1.1       cgd     return(send_tncmd(send_will, "will", name));
    485   1.1       cgd }
    486   1.1       cgd     static int
    487   1.1       cgd send_wontcmd(name)
    488   1.1       cgd     char *name;
    489   1.1       cgd {
    490   1.1       cgd     return(send_tncmd(send_wont, "wont", name));
    491   1.1       cgd }
    492   1.1       cgd 
    493   1.1       cgd     int
    494  1.15  christos send_tncmd(func, cmd, name)
    495   1.1       cgd     void	(*func) P((int, int));
    496   1.1       cgd     char	*cmd, *name;
    497   1.1       cgd {
    498   1.1       cgd     char **cpp;
    499   1.5       cgd     extern char *telopts[];
    500   1.1       cgd     register int val = 0;
    501  1.11       jtk 
    502   1.1       cgd     if (isprefix(name, "?")) {
    503   1.1       cgd 	register int col, len;
    504   1.5       cgd 
    505   1.5       cgd 	printf("Usage: send %s <value|option>\n", cmd);
    506   1.1       cgd 	printf("\"value\" must be from 0 to 255\n");
    507   1.1       cgd 	printf("Valid options are:\n\t");
    508   1.1       cgd 
    509   1.1       cgd 	col = 8;
    510   1.5       cgd 	for (cpp = telopts; *cpp; cpp++) {
    511   1.1       cgd 	    len = strlen(*cpp) + 3;
    512   1.1       cgd 	    if (col + len > 65) {
    513   1.1       cgd 		printf("\n\t");
    514   1.1       cgd 		col = 8;
    515   1.5       cgd 	    }
    516   1.1       cgd 	    printf(" \"%s\"", *cpp);
    517   1.1       cgd 	    col += len;
    518   1.1       cgd 	}
    519   1.1       cgd 	printf("\n");
    520   1.1       cgd 	return 0;
    521   1.1       cgd     }
    522   1.1       cgd     cpp = (char **)genget(name, telopts, sizeof(char *));
    523   1.1       cgd     if (Ambiguous(cpp)) {
    524   1.1       cgd 	fprintf(stderr,"'%s': ambiguous argument ('send %s ?' for help).\n",
    525   1.1       cgd 					name, cmd);
    526   1.1       cgd 	return 0;
    527   1.5       cgd     }
    528   1.5       cgd     if (cpp) {
    529   1.5       cgd 	val = cpp - telopts;
    530   1.5       cgd     } else {
    531   1.5       cgd 	register char *cp = name;
    532   1.5       cgd 
    533   1.5       cgd 	while (*cp >= '0' && *cp <= '9') {
    534   1.5       cgd 	    val *= 10;
    535   1.5       cgd 	    val += *cp - '0';
    536   1.5       cgd 	    cp++;
    537   1.5       cgd 	}
    538   1.5       cgd 	if (*cp != 0) {
    539   1.5       cgd 	    fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\n",
    540   1.5       cgd 					name, cmd);
    541   1.5       cgd 	    return 0;
    542   1.5       cgd 	} else if (val < 0 || val > 255) {
    543   1.1       cgd 	    fprintf(stderr, "'%s': bad value ('send %s ?' for help).\n",
    544   1.5       cgd 					name, cmd);
    545   1.5       cgd 	    return 0;
    546   1.1       cgd 	}
    547   1.1       cgd     }
    548   1.1       cgd     if (!connected) {
    549   1.1       cgd 	printf("?Need to be connected first.\n");
    550   1.1       cgd 	return 0;
    551   1.5       cgd     }
    552   1.1       cgd     (*func)(val, 1);
    553   1.1       cgd     return 1;
    554   1.1       cgd }
    555   1.1       cgd 
    556  1.15  christos     static int
    557  1.15  christos send_help(n)
    558   1.1       cgd     char *n;
    559   1.1       cgd {
    560   1.1       cgd     struct sendlist *s;	/* pointer to current command */
    561   1.1       cgd     for (s = Sendlist; s->name; s++) {
    562   1.1       cgd 	if (s->help)
    563   1.1       cgd 	    printf("%-15s %s\n", s->name, s->help);
    564   1.1       cgd     }
    565   1.1       cgd     return(0);
    566   1.1       cgd }
    567   1.1       cgd 
    568   1.1       cgd /*
    570   1.1       cgd  * The following are the routines and data structures referred
    571   1.1       cgd  * to by the arguments to the "toggle" command.
    572   1.1       cgd  */
    573  1.15  christos 
    574  1.15  christos     static int
    575   1.1       cgd lclchars(n)
    576   1.1       cgd     int n;
    577   1.1       cgd {
    578   1.1       cgd     donelclchars = 1;
    579   1.1       cgd     return 1;
    580   1.1       cgd }
    581  1.15  christos 
    582  1.15  christos     static int
    583   1.1       cgd togdebug(n)
    584   1.1       cgd     int n;
    585   1.1       cgd {
    586   1.1       cgd #ifndef	NOT43
    587   1.1       cgd     if (net > 0 &&
    588   1.1       cgd 	(SetSockOpt(net, SOL_SOCKET, SO_DEBUG, debug)) < 0) {
    589   1.1       cgd 	    perror("setsockopt (SO_DEBUG)");
    590   1.1       cgd     }
    591  1.12       jtk #else	/* NOT43 */
    592   1.1       cgd     if (debug) {
    593   1.1       cgd 	if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0)
    594   1.1       cgd 	    perror("setsockopt (SO_DEBUG)");
    595   1.1       cgd     } else
    596   1.1       cgd 	printf("Cannot turn off socket debugging\n");
    597   1.1       cgd #endif	/* NOT43 */
    598   1.1       cgd     return 1;
    599   1.1       cgd }
    600   1.1       cgd 
    601  1.15  christos 
    602  1.15  christos     static int
    603   1.1       cgd togcrlf(n)
    604   1.1       cgd     int n;
    605   1.1       cgd {
    606   1.1       cgd     if (crlf) {
    607   1.1       cgd 	printf("Will send carriage returns as telnet <CR><LF>.\n");
    608   1.1       cgd     } else {
    609   1.1       cgd 	printf("Will send carriage returns as telnet <CR><NUL>.\n");
    610   1.1       cgd     }
    611   1.1       cgd     return 1;
    612   1.1       cgd }
    613   1.1       cgd 
    614   1.1       cgd int binmode;
    615   1.1       cgd 
    616   1.1       cgd     static int
    617   1.1       cgd togbinary(val)
    618   1.1       cgd     int val;
    619   1.1       cgd {
    620   1.1       cgd     donebinarytoggle = 1;
    621   1.1       cgd 
    622   1.1       cgd     if (val >= 0) {
    623   1.1       cgd 	binmode = val;
    624   1.1       cgd     } else {
    625   1.1       cgd 	if (my_want_state_is_will(TELOPT_BINARY) &&
    626   1.1       cgd 				my_want_state_is_do(TELOPT_BINARY)) {
    627   1.1       cgd 	    binmode = 1;
    628   1.1       cgd 	} else if (my_want_state_is_wont(TELOPT_BINARY) &&
    629   1.1       cgd 				my_want_state_is_dont(TELOPT_BINARY)) {
    630   1.1       cgd 	    binmode = 0;
    631   1.1       cgd 	}
    632   1.1       cgd 	val = binmode ? 0 : 1;
    633   1.1       cgd     }
    634   1.1       cgd 
    635   1.1       cgd     if (val == 1) {
    636   1.1       cgd 	if (my_want_state_is_will(TELOPT_BINARY) &&
    637   1.1       cgd 					my_want_state_is_do(TELOPT_BINARY)) {
    638   1.1       cgd 	    printf("Already operating in binary mode with remote host.\n");
    639   1.1       cgd 	} else {
    640   1.1       cgd 	    printf("Negotiating binary mode with remote host.\n");
    641   1.1       cgd 	    tel_enter_binary(3);
    642   1.1       cgd 	}
    643   1.1       cgd     } else {
    644   1.1       cgd 	if (my_want_state_is_wont(TELOPT_BINARY) &&
    645   1.1       cgd 					my_want_state_is_dont(TELOPT_BINARY)) {
    646   1.1       cgd 	    printf("Already in network ascii mode with remote host.\n");
    647   1.1       cgd 	} else {
    648   1.1       cgd 	    printf("Negotiating network ascii mode with remote host.\n");
    649   1.1       cgd 	    tel_leave_binary(3);
    650   1.1       cgd 	}
    651   1.1       cgd     }
    652   1.1       cgd     return 1;
    653   1.1       cgd }
    654   1.1       cgd 
    655   1.1       cgd     static int
    656   1.1       cgd togrbinary(val)
    657   1.1       cgd     int val;
    658   1.1       cgd {
    659   1.1       cgd     donebinarytoggle = 1;
    660   1.1       cgd 
    661   1.1       cgd     if (val == -1)
    662   1.1       cgd 	val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
    663   1.1       cgd 
    664   1.1       cgd     if (val == 1) {
    665   1.1       cgd 	if (my_want_state_is_do(TELOPT_BINARY)) {
    666   1.1       cgd 	    printf("Already receiving in binary mode.\n");
    667   1.1       cgd 	} else {
    668   1.1       cgd 	    printf("Negotiating binary mode on input.\n");
    669   1.1       cgd 	    tel_enter_binary(1);
    670   1.1       cgd 	}
    671   1.1       cgd     } else {
    672   1.1       cgd 	if (my_want_state_is_dont(TELOPT_BINARY)) {
    673   1.1       cgd 	    printf("Already receiving in network ascii mode.\n");
    674   1.1       cgd 	} else {
    675   1.1       cgd 	    printf("Negotiating network ascii mode on input.\n");
    676   1.1       cgd 	    tel_leave_binary(1);
    677   1.1       cgd 	}
    678   1.1       cgd     }
    679   1.1       cgd     return 1;
    680   1.1       cgd }
    681   1.1       cgd 
    682   1.1       cgd     static int
    683   1.1       cgd togxbinary(val)
    684   1.1       cgd     int val;
    685   1.1       cgd {
    686   1.1       cgd     donebinarytoggle = 1;
    687   1.1       cgd 
    688   1.1       cgd     if (val == -1)
    689   1.1       cgd 	val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
    690   1.1       cgd 
    691   1.1       cgd     if (val == 1) {
    692   1.1       cgd 	if (my_want_state_is_will(TELOPT_BINARY)) {
    693   1.1       cgd 	    printf("Already transmitting in binary mode.\n");
    694   1.1       cgd 	} else {
    695   1.1       cgd 	    printf("Negotiating binary mode on output.\n");
    696   1.1       cgd 	    tel_enter_binary(2);
    697   1.1       cgd 	}
    698   1.1       cgd     } else {
    699   1.1       cgd 	if (my_want_state_is_wont(TELOPT_BINARY)) {
    700   1.1       cgd 	    printf("Already transmitting in network ascii mode.\n");
    701   1.1       cgd 	} else {
    702   1.1       cgd 	    printf("Negotiating network ascii mode on output.\n");
    703   1.1       cgd 	    tel_leave_binary(2);
    704   1.1       cgd 	}
    705   1.1       cgd     }
    706   1.1       cgd     return 1;
    707  1.43   thorpej }
    708  1.43   thorpej 
    709  1.43   thorpej #ifdef	ENCRYPTION
    710  1.43   thorpej extern int EncryptAutoEnc P((int));
    711  1.43   thorpej extern int EncryptAutoDec P((int));
    712  1.43   thorpej extern int EncryptDebug P((int));
    713   1.1       cgd extern int EncryptVerbose P((int));
    714   1.1       cgd #endif	/* ENCRYPTION */
    715   1.1       cgd 
    716   1.1       cgd struct togglelist {
    717  1.15  christos     char	*name;		/* name of toggle */
    718  1.15  christos     char	*help;		/* help message */
    719   1.1       cgd     int		(*handler)	/* routine to do actual setting */
    720   1.1       cgd 			P((int));
    721   1.1       cgd     int		*variable;
    722   1.1       cgd     char	*actionexplanation;
    723   1.1       cgd };
    724   1.1       cgd 
    725   1.1       cgd static struct togglelist Togglelist[] = {
    726   1.1       cgd     { "autoflush",
    727   1.1       cgd 	"flushing of output when sending interrupt characters",
    728   1.1       cgd 	    0,
    729   1.1       cgd 		&autoflush,
    730   1.1       cgd 		    "flush output when sending interrupt characters" },
    731   1.1       cgd     { "autosynch",
    732   1.1       cgd 	"automatic sending of interrupt characters in urgent mode",
    733   1.1       cgd 	    0,
    734   1.5       cgd 		&autosynch,
    735   1.1       cgd 		    "send interrupt characters in urgent mode" },
    736   1.1       cgd #if	defined(AUTHENTICATION)
    737   1.1       cgd     { "autologin",
    738   1.1       cgd 	"automatic sending of login and/or authentication info",
    739   1.1       cgd 	    0,
    740   1.1       cgd 		&autologin,
    741   1.1       cgd 		    "send login name and/or authentication information" },
    742   1.1       cgd     { "authdebug",
    743   1.1       cgd 	"Toggle authentication debugging",
    744   1.1       cgd 	    auth_togdebug,
    745   1.1       cgd 		0,
    746  1.43   thorpej 		     "print authentication debugging information" },
    747  1.43   thorpej #endif
    748  1.43   thorpej #ifdef	ENCRYPTION
    749  1.43   thorpej     { "autoencrypt",
    750  1.43   thorpej       "automatic encryption of data stream",
    751  1.43   thorpej 	    EncryptAutoEnc,
    752  1.43   thorpej 		0,
    753  1.43   thorpej 		     "automatically encrypt output" },
    754  1.43   thorpej     { "autodecrypt",
    755  1.43   thorpej       "automatic decryption of data stream",
    756  1.43   thorpej 	    EncryptAutoDec,
    757  1.43   thorpej 		0,
    758  1.43   thorpej 		     "automatically decrypt input" },
    759  1.43   thorpej     { "verbose_encrypt",
    760  1.43   thorpej       "Toggle verbose encryption output",
    761  1.43   thorpej 	    EncryptVerbose,
    762  1.43   thorpej 		0,
    763  1.43   thorpej 		     "print verbose encryption output" },
    764  1.43   thorpej     { "encdebug",
    765  1.43   thorpej       "Toggle encryption debugging",
    766  1.43   thorpej 	    EncryptDebug,
    767  1.43   thorpej 		0,
    768   1.1       cgd 		     "print encryption debugging information" },
    769   1.1       cgd #endif	/* ENCRYPTION */
    770   1.1       cgd     { "skiprc",
    771   1.1       cgd 	"don't read ~/.telnetrc file",
    772   1.5       cgd 	    0,
    773   1.1       cgd 		&skiprc,
    774   1.1       cgd 		    "skip reading of ~/.telnetrc file" },
    775   1.1       cgd     { "binary",
    776   1.1       cgd 	"sending and receiving of binary data",
    777   1.1       cgd 	    togbinary,
    778   1.1       cgd 		0,
    779   1.1       cgd 		    0 },
    780   1.1       cgd     { "inbinary",
    781   1.1       cgd 	"receiving of binary data",
    782   1.1       cgd 	    togrbinary,
    783   1.1       cgd 		0,
    784   1.1       cgd 		    0 },
    785   1.1       cgd     { "outbinary",
    786   1.1       cgd 	"sending of binary data",
    787   1.1       cgd 	    togxbinary,
    788   1.1       cgd 		0,
    789   1.1       cgd 		    0 },
    790  1.15  christos     { "crlf",
    791   1.1       cgd 	"sending carriage returns as telnet <CR><LF>",
    792   1.1       cgd 	   togcrlf,
    793   1.1       cgd 		&crlf,
    794   1.1       cgd 		    0 },
    795   1.1       cgd     { "crmod",
    796   1.1       cgd 	"mapping of received carriage returns",
    797   1.1       cgd 	    0,
    798   1.1       cgd 		&crmod,
    799   1.1       cgd 		    "map carriage return on output" },
    800   1.1       cgd     { "localchars",
    801   1.1       cgd 	"local recognition of certain control characters",
    802   1.1       cgd 	    lclchars,
    803   1.1       cgd 		&localchars,
    804   1.1       cgd 		    "recognize certain control characters" },
    805   1.1       cgd     { " ", "", 0 },		/* empty line */
    806   1.1       cgd #if	defined(unix) && defined(TN3270)
    807   1.1       cgd     { "apitrace",
    808   1.1       cgd 	"(debugging) toggle tracing of API transactions",
    809   1.1       cgd 	    0,
    810   1.1       cgd 		&apitrace,
    811   1.1       cgd 		    "trace API transactions" },
    812   1.1       cgd     { "cursesdata",
    813   1.1       cgd 	"(debugging) toggle printing of hexadecimal curses data",
    814   1.1       cgd 	    0,
    815   1.1       cgd 		&cursesdata,
    816   1.1       cgd 		    "print hexadecimal representation of curses data" },
    817   1.1       cgd #endif	/* defined(unix) && defined(TN3270) */
    818   1.1       cgd     { "debug",
    819   1.1       cgd 	"debugging",
    820   1.1       cgd 	    togdebug,
    821   1.1       cgd 		&debug,
    822   1.1       cgd 		    "turn on socket level debugging" },
    823   1.1       cgd     { "netdata",
    824   1.1       cgd 	"printing of hexadecimal network data (debugging)",
    825   1.1       cgd 	    0,
    826   1.1       cgd 		&netdata,
    827   1.1       cgd 		    "print hexadecimal representation of network traffic" },
    828   1.1       cgd     { "prettydump",
    829   1.1       cgd 	"output of \"netdata\" to user readable format (debugging)",
    830   1.1       cgd 	    0,
    831   1.1       cgd 		&prettydump,
    832   1.1       cgd 		    "print user readable output for \"netdata\"" },
    833   1.1       cgd     { "options",
    834   1.1       cgd 	"viewing of options processing (debugging)",
    835   1.1       cgd 	    0,
    836   1.1       cgd 		&showoptions,
    837   1.1       cgd 		    "show option processing" },
    838   1.1       cgd #if	defined(unix)
    839   1.1       cgd     { "termdata",
    840   1.1       cgd 	"(debugging) toggle printing of hexadecimal terminal data",
    841   1.1       cgd 	    0,
    842   1.1       cgd 		&termdata,
    843   1.1       cgd 		    "print hexadecimal representation of terminal traffic" },
    844   1.1       cgd #endif	/* defined(unix) */
    845   1.1       cgd     { "?",
    846   1.1       cgd 	0,
    847   1.1       cgd 	    togglehelp },
    848   1.1       cgd     { "help",
    849   1.1       cgd 	0,
    850   1.1       cgd 	    togglehelp },
    851   1.1       cgd     { 0 }
    852   1.1       cgd };
    853  1.15  christos 
    854  1.15  christos     static int
    855   1.1       cgd togglehelp(n)
    856   1.1       cgd     int n;
    857   1.1       cgd {
    858   1.1       cgd     struct togglelist *c;
    859   1.1       cgd 
    860   1.1       cgd     for (c = Togglelist; c->name; c++) {
    861   1.1       cgd 	if (c->help) {
    862   1.1       cgd 	    if (*c->help)
    863   1.1       cgd 		printf("%-15s toggle %s\n", c->name, c->help);
    864   1.1       cgd 	    else
    865   1.1       cgd 		printf("\n");
    866   1.1       cgd 	}
    867   1.1       cgd     }
    868   1.1       cgd     printf("\n");
    869   1.1       cgd     printf("%-15s %s\n", "?", "display help information");
    870   1.1       cgd     return 0;
    871   1.1       cgd }
    872   1.1       cgd 
    873   1.1       cgd     static void
    874   1.1       cgd settogglehelp(set)
    875   1.1       cgd     int set;
    876   1.1       cgd {
    877   1.1       cgd     struct togglelist *c;
    878   1.1       cgd 
    879   1.1       cgd     for (c = Togglelist; c->name; c++) {
    880   1.1       cgd 	if (c->help) {
    881   1.1       cgd 	    if (*c->help)
    882   1.1       cgd 		printf("%-15s %s %s\n", c->name, set ? "enable" : "disable",
    883   1.1       cgd 						c->help);
    884   1.1       cgd 	    else
    885   1.1       cgd 		printf("\n");
    886   1.1       cgd 	}
    887   1.1       cgd     }
    888   1.1       cgd }
    889   1.1       cgd 
    890   1.1       cgd #define	GETTOGGLE(name) (struct togglelist *) \
    891   1.1       cgd 		genget(name, (char **) Togglelist, sizeof(struct togglelist))
    892   1.1       cgd 
    893   1.1       cgd     static int
    894   1.1       cgd toggle(argc, argv)
    895   1.1       cgd     int  argc;
    896   1.1       cgd     char *argv[];
    897   1.1       cgd {
    898   1.1       cgd     int retval = 1;
    899   1.1       cgd     char *name;
    900   1.1       cgd     struct togglelist *c;
    901   1.1       cgd 
    902   1.1       cgd     if (argc < 2) {
    903   1.1       cgd 	fprintf(stderr,
    904   1.1       cgd 	    "Need an argument to 'toggle' command.  'toggle ?' for help.\n");
    905   1.1       cgd 	return 0;
    906   1.1       cgd     }
    907   1.1       cgd     argc--;
    908   1.1       cgd     argv++;
    909   1.1       cgd     while (argc--) {
    910   1.1       cgd 	name = *argv++;
    911   1.1       cgd 	c = GETTOGGLE(name);
    912   1.1       cgd 	if (Ambiguous(c)) {
    913   1.1       cgd 	    fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\n",
    914   1.1       cgd 					name);
    915   1.1       cgd 	    return 0;
    916   1.1       cgd 	} else if (c == 0) {
    917   1.1       cgd 	    fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\n",
    918   1.1       cgd 					name);
    919   1.1       cgd 	    return 0;
    920   1.1       cgd 	} else {
    921   1.1       cgd 	    if (c->variable) {
    922   1.1       cgd 		*c->variable = !*c->variable;		/* invert it */
    923   1.1       cgd 		if (c->actionexplanation) {
    924   1.1       cgd 		    printf("%s %s.\n", *c->variable? "Will" : "Won't",
    925   1.1       cgd 							c->actionexplanation);
    926   1.1       cgd 		}
    927   1.1       cgd 	    }
    928   1.1       cgd 	    if (c->handler) {
    929   1.1       cgd 		retval &= (*c->handler)(-1);
    930   1.1       cgd 	    }
    931   1.1       cgd 	}
    932   1.1       cgd     }
    933   1.1       cgd     return retval;
    934   1.1       cgd }
    935   1.1       cgd 
    936   1.1       cgd /*
    938   1.1       cgd  * The following perform the "set" command.
    939   1.1       cgd  */
    940   1.1       cgd 
    941   1.1       cgd #ifdef	USE_TERMIO
    942   1.1       cgd struct termio new_tc = { 0 };
    943   1.1       cgd #endif
    944   1.1       cgd 
    945  1.15  christos struct setlist {
    946   1.1       cgd     char *name;				/* name */
    947   1.1       cgd     char *help;				/* help information */
    948   1.1       cgd     void (*handler) P((char *));
    949   1.1       cgd     cc_t *charp;			/* where it is located at */
    950   1.1       cgd };
    951   1.1       cgd 
    952   1.1       cgd static struct setlist Setlist[] = {
    953   1.1       cgd #ifdef	KLUDGELINEMODE
    954   1.1       cgd     { "echo", 	"character to toggle local echoing on/off", 0, &echoc },
    955   1.1       cgd #endif
    956   1.1       cgd     { "escape",	"character to escape back to telnet command mode", 0, &escape },
    957   1.1       cgd     { "rlogin", "rlogin escape character", 0, &rlogin },
    958   1.1       cgd     { "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile},
    959   1.1       cgd     { " ", "" },
    960   1.1       cgd     { " ", "The following need 'localchars' to be toggled true", 0, 0 },
    961   1.1       cgd     { "flushoutput", "character to cause an Abort Output", 0, termFlushCharp },
    962   1.1       cgd     { "interrupt", "character to cause an Interrupt Process", 0, termIntCharp },
    963   1.1       cgd     { "quit",	"character to cause an Abort process", 0, termQuitCharp },
    964   1.1       cgd     { "eof",	"character to cause an EOF ", 0, termEofCharp },
    965   1.1       cgd     { " ", "" },
    966   1.1       cgd     { " ", "The following are for local editing in linemode", 0, 0 },
    967   1.1       cgd     { "erase",	"character to use to erase a character", 0, termEraseCharp },
    968   1.1       cgd     { "kill",	"character to use to erase a line", 0, termKillCharp },
    969   1.1       cgd     { "lnext",	"character to use for literal next", 0, termLiteralNextCharp },
    970   1.1       cgd     { "susp",	"character to cause a Suspend Process", 0, termSuspCharp },
    971   1.1       cgd     { "reprint", "character to use for line reprint", 0, termRprntCharp },
    972   1.1       cgd     { "worderase", "character to use to erase a word", 0, termWerasCharp },
    973   1.1       cgd     { "start",	"character to use for XON", 0, termStartCharp },
    974   1.1       cgd     { "stop",	"character to use for XOFF", 0, termStopCharp },
    975   1.1       cgd     { "forw1",	"alternate end of line character", 0, termForw1Charp },
    976   1.1       cgd     { "forw2",	"alternate end of line character", 0, termForw2Charp },
    977   1.1       cgd     { "ayt",	"alternate AYT character", 0, termAytCharp },
    978   1.1       cgd     { 0 }
    979   1.1       cgd };
    980   1.1       cgd 
    981   1.1       cgd #if	defined(CRAY) && !defined(__STDC__)
    982   1.1       cgd /* Work around compiler bug in pcc 4.1.5 */
    983   1.1       cgd     void
    984   1.1       cgd _setlist_init()
    985   1.1       cgd {
    986   1.1       cgd #ifndef	KLUDGELINEMODE
    987   1.1       cgd #define	N 5
    988   1.1       cgd #else
    989   1.1       cgd #define	N 6
    990   1.1       cgd #endif
    991   1.1       cgd 	Setlist[N+0].charp = &termFlushChar;
    992   1.1       cgd 	Setlist[N+1].charp = &termIntChar;
    993   1.1       cgd 	Setlist[N+2].charp = &termQuitChar;
    994   1.1       cgd 	Setlist[N+3].charp = &termEofChar;
    995   1.1       cgd 	Setlist[N+6].charp = &termEraseChar;
    996   1.1       cgd 	Setlist[N+7].charp = &termKillChar;
    997   1.1       cgd 	Setlist[N+8].charp = &termLiteralNextChar;
    998   1.1       cgd 	Setlist[N+9].charp = &termSuspChar;
    999   1.1       cgd 	Setlist[N+10].charp = &termRprntChar;
   1000   1.1       cgd 	Setlist[N+11].charp = &termWerasChar;
   1001   1.1       cgd 	Setlist[N+12].charp = &termStartChar;
   1002   1.1       cgd 	Setlist[N+13].charp = &termStopChar;
   1003   1.1       cgd 	Setlist[N+14].charp = &termForw1Char;
   1004   1.1       cgd 	Setlist[N+15].charp = &termForw2Char;
   1005   1.1       cgd 	Setlist[N+16].charp = &termAytChar;
   1006   1.1       cgd #undef	N
   1007   1.1       cgd }
   1008   1.1       cgd #endif	/* defined(CRAY) && !defined(__STDC__) */
   1009   1.1       cgd 
   1010   1.1       cgd     static struct setlist *
   1011   1.1       cgd getset(name)
   1012   1.1       cgd     char *name;
   1013   1.1       cgd {
   1014   1.1       cgd     return (struct setlist *)
   1015   1.1       cgd 		genget(name, (char **) Setlist, sizeof(struct setlist));
   1016   1.1       cgd }
   1017   1.1       cgd 
   1018   1.1       cgd     void
   1019   1.1       cgd set_escape_char(s)
   1020   1.1       cgd     char *s;
   1021   1.1       cgd {
   1022   1.1       cgd 	if (rlogin != _POSIX_VDISABLE) {
   1023   1.1       cgd 		rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
   1024   1.1       cgd 		printf("Telnet rlogin escape character is '%s'.\n",
   1025   1.1       cgd 					control(rlogin));
   1026   1.1       cgd 	} else {
   1027   1.1       cgd 		escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
   1028   1.1       cgd 		printf("Telnet escape character is '%s'.\n", control(escape));
   1029   1.1       cgd 	}
   1030   1.1       cgd }
   1031   1.1       cgd 
   1032   1.1       cgd     static int
   1033   1.1       cgd setcmd(argc, argv)
   1034   1.1       cgd     int  argc;
   1035   1.1       cgd     char *argv[];
   1036   1.1       cgd {
   1037   1.1       cgd     int value;
   1038   1.1       cgd     struct setlist *ct;
   1039   1.1       cgd     struct togglelist *c;
   1040   1.1       cgd 
   1041   1.1       cgd     if (argc < 2 || argc > 3) {
   1042   1.1       cgd 	printf("Format is 'set Name Value'\n'set ?' for help.\n");
   1043   1.1       cgd 	return 0;
   1044   1.1       cgd     }
   1045   1.1       cgd     if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
   1046   1.1       cgd 	for (ct = Setlist; ct->name; ct++)
   1047   1.1       cgd 	    printf("%-15s %s\n", ct->name, ct->help);
   1048   1.1       cgd 	printf("\n");
   1049   1.1       cgd 	settogglehelp(1);
   1050   1.1       cgd 	printf("%-15s %s\n", "?", "display help information");
   1051   1.1       cgd 	return 0;
   1052   1.1       cgd     }
   1053   1.1       cgd 
   1054   1.1       cgd     ct = getset(argv[1]);
   1055   1.1       cgd     if (ct == 0) {
   1056   1.1       cgd 	c = GETTOGGLE(argv[1]);
   1057   1.1       cgd 	if (c == 0) {
   1058   1.1       cgd 	    fprintf(stderr, "'%s': unknown argument ('set ?' for help).\n",
   1059   1.1       cgd 			argv[1]);
   1060   1.1       cgd 	    return 0;
   1061   1.1       cgd 	} else if (Ambiguous(c)) {
   1062   1.1       cgd 	    fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
   1063   1.1       cgd 			argv[1]);
   1064   1.1       cgd 	    return 0;
   1065   1.1       cgd 	}
   1066   1.1       cgd 	if (c->variable) {
   1067   1.1       cgd 	    if ((argc == 2) || (strcmp("on", argv[2]) == 0))
   1068   1.1       cgd 		*c->variable = 1;
   1069   1.1       cgd 	    else if (strcmp("off", argv[2]) == 0)
   1070   1.1       cgd 		*c->variable = 0;
   1071   1.1       cgd 	    else {
   1072   1.1       cgd 		printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
   1073   1.1       cgd 		return 0;
   1074   1.1       cgd 	    }
   1075   1.1       cgd 	    if (c->actionexplanation) {
   1076   1.1       cgd 		printf("%s %s.\n", *c->variable? "Will" : "Won't",
   1077   1.1       cgd 							c->actionexplanation);
   1078   1.1       cgd 	    }
   1079   1.1       cgd 	}
   1080   1.1       cgd 	if (c->handler)
   1081   1.1       cgd 	    (*c->handler)(1);
   1082   1.1       cgd     } else if (argc != 3) {
   1083   1.1       cgd 	printf("Format is 'set Name Value'\n'set ?' for help.\n");
   1084   1.1       cgd 	return 0;
   1085   1.1       cgd     } else if (Ambiguous(ct)) {
   1086   1.1       cgd 	fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
   1087   1.1       cgd 			argv[1]);
   1088   1.1       cgd 	return 0;
   1089   1.1       cgd     } else if (ct->handler) {
   1090   1.1       cgd 	(*ct->handler)(argv[2]);
   1091   1.1       cgd 	printf("%s set to \"%s\".\n", ct->name, (char *)ct->charp);
   1092   1.1       cgd     } else {
   1093   1.1       cgd 	if (strcmp("off", argv[2])) {
   1094   1.1       cgd 	    value = special(argv[2]);
   1095   1.1       cgd 	} else {
   1096   1.1       cgd 	    value = _POSIX_VDISABLE;
   1097   1.1       cgd 	}
   1098   1.1       cgd 	*(ct->charp) = (cc_t)value;
   1099   1.1       cgd 	printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
   1100   1.1       cgd     }
   1101   1.1       cgd     slc_check();
   1102   1.1       cgd     return 1;
   1103   1.1       cgd }
   1104   1.1       cgd 
   1105   1.1       cgd     static int
   1106   1.1       cgd unsetcmd(argc, argv)
   1107   1.1       cgd     int  argc;
   1108   1.1       cgd     char *argv[];
   1109   1.1       cgd {
   1110   1.1       cgd     struct setlist *ct;
   1111   1.1       cgd     struct togglelist *c;
   1112   1.1       cgd     register char *name;
   1113   1.1       cgd 
   1114   1.1       cgd     if (argc < 2) {
   1115   1.1       cgd 	fprintf(stderr,
   1116   1.1       cgd 	    "Need an argument to 'unset' command.  'unset ?' for help.\n");
   1117   1.1       cgd 	return 0;
   1118   1.1       cgd     }
   1119   1.1       cgd     if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
   1120   1.1       cgd 	for (ct = Setlist; ct->name; ct++)
   1121   1.1       cgd 	    printf("%-15s %s\n", ct->name, ct->help);
   1122   1.1       cgd 	printf("\n");
   1123   1.1       cgd 	settogglehelp(0);
   1124   1.1       cgd 	printf("%-15s %s\n", "?", "display help information");
   1125   1.1       cgd 	return 0;
   1126   1.1       cgd     }
   1127   1.1       cgd 
   1128   1.1       cgd     argc--;
   1129   1.1       cgd     argv++;
   1130   1.1       cgd     while (argc--) {
   1131   1.1       cgd 	name = *argv++;
   1132   1.1       cgd 	ct = getset(name);
   1133   1.1       cgd 	if (ct == 0) {
   1134   1.1       cgd 	    c = GETTOGGLE(name);
   1135   1.1       cgd 	    if (c == 0) {
   1136   1.1       cgd 		fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\n",
   1137   1.1       cgd 			name);
   1138   1.1       cgd 		return 0;
   1139   1.1       cgd 	    } else if (Ambiguous(c)) {
   1140   1.1       cgd 		fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
   1141   1.1       cgd 			name);
   1142   1.1       cgd 		return 0;
   1143   1.1       cgd 	    }
   1144   1.1       cgd 	    if (c->variable) {
   1145   1.1       cgd 		*c->variable = 0;
   1146   1.1       cgd 		if (c->actionexplanation) {
   1147   1.1       cgd 		    printf("%s %s.\n", *c->variable? "Will" : "Won't",
   1148   1.1       cgd 							c->actionexplanation);
   1149   1.1       cgd 		}
   1150   1.1       cgd 	    }
   1151   1.1       cgd 	    if (c->handler)
   1152   1.1       cgd 		(*c->handler)(0);
   1153   1.1       cgd 	} else if (Ambiguous(ct)) {
   1154   1.1       cgd 	    fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
   1155   1.1       cgd 			name);
   1156   1.1       cgd 	    return 0;
   1157   1.1       cgd 	} else if (ct->handler) {
   1158   1.1       cgd 	    (*ct->handler)(0);
   1159   1.1       cgd 	    printf("%s reset to \"%s\".\n", ct->name, (char *)ct->charp);
   1160   1.1       cgd 	} else {
   1161   1.1       cgd 	    *(ct->charp) = _POSIX_VDISABLE;
   1162   1.1       cgd 	    printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
   1163   1.1       cgd 	}
   1164   1.1       cgd     }
   1165   1.1       cgd     return 1;
   1166   1.1       cgd }
   1167   1.1       cgd 
   1168   1.1       cgd /*
   1170   1.1       cgd  * The following are the data structures and routines for the
   1171   1.1       cgd  * 'mode' command.
   1172   1.1       cgd  */
   1173  1.15  christos #ifdef	KLUDGELINEMODE
   1174  1.15  christos extern int kludgelinemode;
   1175   1.1       cgd 
   1176   1.1       cgd     static int
   1177   1.1       cgd dokludgemode(n)
   1178   1.1       cgd     int n;
   1179   1.1       cgd {
   1180  1.15  christos     kludgelinemode = 1;
   1181   1.1       cgd     send_wont(TELOPT_LINEMODE, 1);
   1182   1.1       cgd     send_dont(TELOPT_SGA, 1);
   1183   1.1       cgd     send_dont(TELOPT_ECHO, 1);
   1184   1.1       cgd     return 1;
   1185  1.15  christos }
   1186  1.15  christos #endif
   1187   1.1       cgd 
   1188   1.1       cgd     static int
   1189   1.1       cgd dolinemode(n)
   1190   1.1       cgd     int n;
   1191   1.1       cgd {
   1192   1.1       cgd #ifdef	KLUDGELINEMODE
   1193   1.1       cgd     if (kludgelinemode)
   1194   1.1       cgd 	send_dont(TELOPT_SGA, 1);
   1195   1.1       cgd #endif
   1196   1.1       cgd     send_will(TELOPT_LINEMODE, 1);
   1197   1.1       cgd     send_dont(TELOPT_ECHO, 1);
   1198  1.15  christos     return 1;
   1199  1.15  christos }
   1200   1.1       cgd 
   1201   1.1       cgd     static int
   1202   1.1       cgd docharmode(n)
   1203   1.1       cgd     int n;
   1204   1.1       cgd {
   1205   1.1       cgd #ifdef	KLUDGELINEMODE
   1206   1.1       cgd     if (kludgelinemode)
   1207   1.1       cgd 	send_do(TELOPT_SGA, 1);
   1208   1.1       cgd     else
   1209   1.1       cgd #endif
   1210   1.1       cgd     send_wont(TELOPT_LINEMODE, 1);
   1211   1.1       cgd     send_do(TELOPT_ECHO, 1);
   1212   1.1       cgd     return 1;
   1213   1.1       cgd }
   1214   1.1       cgd 
   1215   1.1       cgd     static int
   1216   1.1       cgd dolmmode(bit, on)
   1217   1.1       cgd     int bit, on;
   1218   1.1       cgd {
   1219   1.1       cgd     unsigned char c;
   1220   1.1       cgd     extern int linemode;
   1221   1.1       cgd 
   1222   1.1       cgd     if (my_want_state_is_wont(TELOPT_LINEMODE)) {
   1223   1.1       cgd 	printf("?Need to have LINEMODE option enabled first.\n");
   1224   1.1       cgd 	printf("'mode ?' for help.\n");
   1225   1.1       cgd 	return 0;
   1226   1.1       cgd     }
   1227   1.1       cgd 
   1228   1.1       cgd     if (on)
   1229   1.1       cgd 	c = (linemode | bit);
   1230   1.1       cgd     else
   1231   1.1       cgd 	c = (linemode & ~bit);
   1232   1.1       cgd     lm_mode(&c, 1, 1);
   1233  1.15  christos     return 1;
   1234  1.15  christos }
   1235   1.1       cgd 
   1236   1.1       cgd     int
   1237   1.1       cgd set_mode(bit)
   1238   1.1       cgd     int bit;
   1239   1.1       cgd {
   1240  1.15  christos     return dolmmode(bit, 1);
   1241  1.15  christos }
   1242   1.1       cgd 
   1243   1.1       cgd     int
   1244   1.1       cgd clear_mode(bit)
   1245   1.1       cgd     int bit;
   1246   1.1       cgd {
   1247   1.1       cgd     return dolmmode(bit, 0);
   1248   1.1       cgd }
   1249  1.15  christos 
   1250  1.15  christos struct modelist {
   1251   1.1       cgd 	char	*name;		/* command name */
   1252   1.1       cgd 	char	*help;		/* help string */
   1253   1.1       cgd 	int	(*handler)	/* routine which executes command */
   1254   1.1       cgd 			P((int));
   1255   1.1       cgd 	int	needconnect;	/* Do we need to be connected to execute? */
   1256   1.1       cgd 	int	arg1;
   1257   1.1       cgd };
   1258   1.1       cgd 
   1259   1.1       cgd static struct modelist ModeList[] = {
   1260   1.1       cgd     { "character", "Disable LINEMODE option",	docharmode, 1 },
   1261   1.1       cgd #ifdef	KLUDGELINEMODE
   1262   1.1       cgd     { "",	"(or disable obsolete line-by-line mode)", 0 },
   1263   1.1       cgd #endif
   1264   1.1       cgd     { "line",	"Enable LINEMODE option",	dolinemode, 1 },
   1265   1.1       cgd #ifdef	KLUDGELINEMODE
   1266  1.15  christos     { "",	"(or enable obsolete line-by-line mode)", 0 },
   1267  1.15  christos #endif
   1268  1.15  christos     { "", "", 0 },
   1269  1.15  christos     { "",	"These require the LINEMODE option to be enabled", 0 },
   1270  1.15  christos     { "isig",	"Enable signal trapping",	set_mode, 1, MODE_TRAPSIG },
   1271  1.15  christos     { "+isig",	0,				set_mode, 1, MODE_TRAPSIG },
   1272  1.15  christos     { "-isig",	"Disable signal trapping",	clear_mode, 1, MODE_TRAPSIG },
   1273  1.15  christos     { "edit",	"Enable character editing",	set_mode, 1, MODE_EDIT },
   1274  1.15  christos     { "+edit",	0,				set_mode, 1, MODE_EDIT },
   1275  1.15  christos     { "-edit",	"Disable character editing",	clear_mode, 1, MODE_EDIT },
   1276  1.15  christos     { "softtabs", "Enable tab expansion",	set_mode, 1, MODE_SOFT_TAB },
   1277  1.15  christos     { "+softtabs", 0,				set_mode, 1, MODE_SOFT_TAB },
   1278   1.1       cgd     { "-softtabs", "Disable character editing",	clear_mode, 1, MODE_SOFT_TAB },
   1279   1.1       cgd     { "litecho", "Enable literal character echo", set_mode, 1, MODE_LIT_ECHO },
   1280   1.1       cgd     { "+litecho", 0,				set_mode, 1, MODE_LIT_ECHO },
   1281   1.1       cgd     { "-litecho", "Disable literal character echo", clear_mode, 1, MODE_LIT_ECHO },
   1282   1.1       cgd     { "help",	0,				modehelp, 0 },
   1283   1.1       cgd #ifdef	KLUDGELINEMODE
   1284   1.1       cgd     { "kludgeline", 0,				dokludgemode, 1 },
   1285   1.1       cgd #endif
   1286   1.1       cgd     { "", "", 0 },
   1287   1.1       cgd     { "?",	"Print help information",	modehelp, 0 },
   1288   1.1       cgd     { 0 },
   1289  1.15  christos };
   1290  1.15  christos 
   1291   1.1       cgd 
   1292   1.1       cgd     int
   1293   1.1       cgd modehelp(n)
   1294   1.1       cgd     int n;
   1295   1.1       cgd {
   1296   1.1       cgd     struct modelist *mt;
   1297   1.1       cgd 
   1298   1.1       cgd     printf("format is:  'mode Mode', where 'Mode' is one of:\n\n");
   1299   1.1       cgd     for (mt = ModeList; mt->name; mt++) {
   1300   1.1       cgd 	if (mt->help) {
   1301   1.1       cgd 	    if (*mt->help)
   1302   1.1       cgd 		printf("%-15s %s\n", mt->name, mt->help);
   1303   1.1       cgd 	    else
   1304   1.1       cgd 		printf("\n");
   1305   1.1       cgd 	}
   1306   1.1       cgd     }
   1307   1.1       cgd     return 0;
   1308   1.1       cgd }
   1309   1.1       cgd 
   1310   1.1       cgd #define	GETMODECMD(name) (struct modelist *) \
   1311   1.1       cgd 		genget(name, (char **) ModeList, sizeof(struct modelist))
   1312   1.1       cgd 
   1313   1.1       cgd     static int
   1314   1.1       cgd modecmd(argc, argv)
   1315   1.1       cgd     int  argc;
   1316   1.1       cgd     char *argv[];
   1317   1.1       cgd {
   1318   1.1       cgd     struct modelist *mt;
   1319   1.1       cgd 
   1320   1.1       cgd     if (argc != 2) {
   1321   1.1       cgd 	printf("'mode' command requires an argument\n");
   1322   1.1       cgd 	printf("'mode ?' for help.\n");
   1323   1.1       cgd     } else if ((mt = GETMODECMD(argv[1])) == 0) {
   1324   1.1       cgd 	fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\n", argv[1]);
   1325   1.1       cgd     } else if (Ambiguous(mt)) {
   1326   1.1       cgd 	fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]);
   1327   1.1       cgd     } else if (mt->needconnect && !connected) {
   1328   1.1       cgd 	printf("?Need to be connected first.\n");
   1329   1.1       cgd 	printf("'mode ?' for help.\n");
   1330   1.1       cgd     } else if (mt->handler) {
   1331   1.1       cgd 	return (*mt->handler)(mt->arg1);
   1332   1.1       cgd     }
   1333   1.1       cgd     return 0;
   1334   1.1       cgd }
   1335   1.1       cgd 
   1336   1.1       cgd /*
   1338   1.1       cgd  * The following data structures and routines implement the
   1339   1.1       cgd  * "display" command.
   1340   1.1       cgd  */
   1341   1.1       cgd 
   1342   1.1       cgd     static int
   1343   1.1       cgd display(argc, argv)
   1344   1.1       cgd     int  argc;
   1345   1.1       cgd     char *argv[];
   1346   1.1       cgd {
   1347   1.1       cgd     struct togglelist *tl;
   1348   1.1       cgd     struct setlist *sl;
   1349   1.1       cgd 
   1350   1.1       cgd #define	dotog(tl)	if (tl->variable && tl->actionexplanation) { \
   1351   1.1       cgd 			    if (*tl->variable) { \
   1352   1.1       cgd 				printf("will"); \
   1353   1.1       cgd 			    } else { \
   1354   1.1       cgd 				printf("won't"); \
   1355   1.1       cgd 			    } \
   1356   1.1       cgd 			    printf(" %s.\n", tl->actionexplanation); \
   1357   1.1       cgd 			}
   1358   1.1       cgd 
   1359   1.1       cgd #define	doset(sl)   if (sl->name && *sl->name != ' ') { \
   1360   1.1       cgd 			if (sl->handler == 0) \
   1361   1.1       cgd 			    printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
   1362   1.1       cgd 			else \
   1363   1.1       cgd 			    printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
   1364   1.1       cgd 		    }
   1365   1.1       cgd 
   1366   1.1       cgd     if (argc == 1) {
   1367   1.1       cgd 	for (tl = Togglelist; tl->name; tl++) {
   1368   1.1       cgd 	    dotog(tl);
   1369   1.1       cgd 	}
   1370   1.1       cgd 	printf("\n");
   1371   1.1       cgd 	for (sl = Setlist; sl->name; sl++) {
   1372   1.1       cgd 	    doset(sl);
   1373   1.1       cgd 	}
   1374   1.1       cgd     } else {
   1375   1.1       cgd 	int i;
   1376   1.1       cgd 
   1377   1.1       cgd 	for (i = 1; i < argc; i++) {
   1378   1.1       cgd 	    sl = getset(argv[i]);
   1379   1.1       cgd 	    tl = GETTOGGLE(argv[i]);
   1380   1.1       cgd 	    if (Ambiguous(sl) || Ambiguous(tl)) {
   1381   1.1       cgd 		printf("?Ambiguous argument '%s'.\n", argv[i]);
   1382   1.1       cgd 		return 0;
   1383   1.1       cgd 	    } else if (!sl && !tl) {
   1384   1.1       cgd 		printf("?Unknown argument '%s'.\n", argv[i]);
   1385   1.1       cgd 		return 0;
   1386   1.1       cgd 	    } else {
   1387   1.1       cgd 		if (tl) {
   1388   1.1       cgd 		    dotog(tl);
   1389   1.1       cgd 		}
   1390   1.1       cgd 		if (sl) {
   1391   1.1       cgd 		    doset(sl);
   1392  1.43   thorpej 		}
   1393  1.43   thorpej 	    }
   1394  1.43   thorpej 	}
   1395   1.1       cgd     }
   1396   1.1       cgd /*@*/optionstatus();
   1397   1.1       cgd #ifdef	ENCRYPTION
   1398   1.1       cgd     EncryptStatus();
   1399   1.1       cgd #endif	/* ENCRYPTION */
   1400   1.1       cgd     return 1;
   1401   1.1       cgd #undef	doset
   1402   1.1       cgd #undef	dotog
   1403   1.1       cgd }
   1404   1.1       cgd 
   1405   1.1       cgd /*
   1407   1.1       cgd  * The following are the data structures, and many of the routines,
   1408   1.1       cgd  * relating to command processing.
   1409   1.1       cgd  */
   1410   1.1       cgd 
   1411   1.1       cgd /*
   1412   1.1       cgd  * Set the escape character.
   1413   1.1       cgd  */
   1414   1.1       cgd 	static int
   1415   1.1       cgd setescape(argc, argv)
   1416   1.1       cgd 	int argc;
   1417   1.1       cgd 	char *argv[];
   1418   1.1       cgd {
   1419   1.1       cgd 	register char *arg;
   1420   1.1       cgd 	char buf[50];
   1421   1.1       cgd 
   1422   1.1       cgd 	printf(
   1423   1.1       cgd 	    "Deprecated usage - please use 'set escape%s%s' in the future.\n",
   1424   1.1       cgd 				(argc > 2)? " ":"", (argc > 2)? argv[1]: "");
   1425   1.1       cgd 	if (argc > 2)
   1426   1.1       cgd 		arg = argv[1];
   1427   1.1       cgd 	else {
   1428   1.1       cgd 		printf("new escape character: ");
   1429   1.1       cgd 		(void) fgets(buf, sizeof(buf), stdin);
   1430   1.1       cgd 		arg = buf;
   1431   1.1       cgd 	}
   1432   1.1       cgd 	if (arg[0] != '\0')
   1433   1.1       cgd 		escape = arg[0];
   1434   1.1       cgd 	if (!In3270) {
   1435   1.1       cgd 		printf("Escape character is '%s'.\n", control(escape));
   1436   1.1       cgd 	}
   1437  1.15  christos 	(void) fflush(stdout);
   1438  1.15  christos 	return 1;
   1439  1.15  christos }
   1440   1.1       cgd 
   1441   1.1       cgd     /*VARARGS*/
   1442   1.1       cgd     static int
   1443   1.1       cgd togcrmod(argc, argv)
   1444   1.1       cgd     int argc;
   1445   1.1       cgd     char *argv[];
   1446   1.1       cgd {
   1447   1.1       cgd     crmod = !crmod;
   1448   1.1       cgd     printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
   1449   1.1       cgd     printf("%s map carriage return on output.\n", crmod ? "Will" : "Won't");
   1450  1.15  christos     (void) fflush(stdout);
   1451  1.15  christos     return 1;
   1452  1.15  christos }
   1453   1.1       cgd 
   1454   1.1       cgd     /*VARARGS*/
   1455   1.1       cgd     int
   1456   1.1       cgd suspend(argc, argv)
   1457   1.1       cgd     int argc;
   1458   1.1       cgd     char *argv[];
   1459   1.5       cgd {
   1460   1.1       cgd #ifdef	SIGTSTP
   1461   1.5       cgd     setcommandmode();
   1462   1.5       cgd     {
   1463  1.10       jtk 	long oldrows, oldcols, newrows, newcols, err;
   1464   1.5       cgd 
   1465   1.5       cgd 	err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
   1466   1.5       cgd 	(void) kill(0, SIGTSTP);
   1467   1.5       cgd 	/*
   1468   1.1       cgd 	 * If we didn't get the window size before the SUSPEND, but we
   1469   1.1       cgd 	 * can get them now (?), then send the NAWS to make sure that
   1470   1.1       cgd 	 * we are set up for the right window size.
   1471   1.1       cgd 	 */
   1472   1.1       cgd 	if (TerminalWindowSize(&newrows, &newcols) && connected &&
   1473   1.1       cgd 	    (err || ((oldrows != newrows) || (oldcols != newcols)))) {
   1474   1.1       cgd 		sendnaws();
   1475   1.1       cgd 	}
   1476   1.1       cgd     }
   1477   1.1       cgd     /* reget parameters in case they were changed */
   1478   1.1       cgd     TerminalSaveState();
   1479   1.1       cgd     setconnmode(0);
   1480   1.1       cgd #else
   1481   1.1       cgd     printf("Suspend is not supported.  Try the '!' command instead\n");
   1482   1.1       cgd #endif
   1483   1.1       cgd     return 1;
   1484   1.1       cgd }
   1485   1.1       cgd 
   1486   1.1       cgd #if	!defined(TN3270)
   1487   1.5       cgd     /*ARGSUSED*/
   1488  1.19   thorpej     int
   1489  1.19   thorpej shell(argc, argv)
   1490  1.19   thorpej     int argc;
   1491  1.19   thorpej     char *argv[];
   1492   1.5       cgd {
   1493   1.1       cgd     long oldrows, oldcols, newrows, newcols, err;
   1494   1.5       cgd 
   1495   1.5       cgd #ifdef __GNUC__
   1496   1.1       cgd     (void) &err;	/* XXX avoid GCC warning */
   1497   1.1       cgd #endif
   1498  1.32     soren 
   1499   1.1       cgd     setcommandmode();
   1500   1.1       cgd 
   1501   1.1       cgd     err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
   1502   1.1       cgd     switch(vfork()) {
   1503   1.1       cgd     case -1:
   1504   1.1       cgd 	perror("Fork failed");
   1505   1.1       cgd 	break;
   1506   1.1       cgd 
   1507   1.1       cgd     case 0:
   1508   1.1       cgd 	{
   1509   1.1       cgd 	    /*
   1510   1.1       cgd 	     * Fire up the shell in the child.
   1511  1.10       jtk 	     */
   1512   1.1       cgd 	    register char *shellp, *shellname;
   1513   1.1       cgd 
   1514   1.1       cgd 	    shellp = getenv("SHELL");
   1515   1.1       cgd 	    if (shellp == NULL)
   1516   1.1       cgd 		shellp = "/bin/sh";
   1517   1.1       cgd 	    if ((shellname = strrchr(shellp, '/')) == 0)
   1518   1.1       cgd 		shellname = shellp;
   1519  1.32     soren 	    else
   1520   1.1       cgd 		shellname++;
   1521   1.1       cgd 	    if (argc > 1)
   1522   1.1       cgd 		execl(shellp, shellname, "-c", &saveline[1], 0);
   1523   1.1       cgd 	    else
   1524   1.5       cgd 		execl(shellp, shellname, 0);
   1525   1.5       cgd 	    perror("execl");
   1526   1.5       cgd 	    _exit(1);
   1527   1.5       cgd 	}
   1528   1.5       cgd     default:
   1529   1.5       cgd 	    (void)wait((int *)0);	/* Wait for the shell to complete */
   1530   1.1       cgd 
   1531   1.1       cgd 	    if (TerminalWindowSize(&newrows, &newcols) && connected &&
   1532   1.1       cgd 		(err || ((oldrows != newrows) || (oldcols != newcols)))) {
   1533   1.1       cgd 		    sendnaws();
   1534   1.1       cgd 	    }
   1535   1.1       cgd 	    break;
   1536  1.15  christos     }
   1537   1.1       cgd     return 1;
   1538   1.1       cgd }
   1539   1.1       cgd #endif	/* !defined(TN3270) */
   1540   1.1       cgd 
   1541   1.1       cgd     /*VARARGS*/
   1542   1.1       cgd     static int
   1543   1.1       cgd bye(argc, argv)
   1544   1.1       cgd     int  argc;		/* Number of arguments */
   1545   1.1       cgd     char *argv[];	/* arguments */
   1546   1.1       cgd {
   1547   1.1       cgd     extern int resettermname;
   1548   1.1       cgd 
   1549  1.43   thorpej     if (connected) {
   1550   1.1       cgd 	(void) shutdown(net, 2);
   1551   1.5       cgd 	printf("Connection closed.\n");
   1552   1.1       cgd 	(void) NetClose(net);
   1553   1.1       cgd 	connected = 0;
   1554   1.1       cgd 	resettermname = 1;
   1555   1.1       cgd #if	defined(AUTHENTICATION) || defined(ENCRYPTION)
   1556   1.1       cgd 	auth_encrypt_connect(connected);
   1557   1.1       cgd #endif	/* defined(AUTHENTICATION) */
   1558   1.1       cgd 	/* reset options */
   1559   1.1       cgd 	tninit();
   1560   1.1       cgd #if	defined(TN3270)
   1561   1.1       cgd 	SetIn3270();		/* Get out of 3270 mode */
   1562   1.1       cgd #endif	/* defined(TN3270) */
   1563   1.1       cgd     }
   1564   1.1       cgd     if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
   1565   1.1       cgd 	longjmp(toplevel, 1);
   1566  1.15  christos 	/* NOTREACHED */
   1567  1.15  christos     }
   1568  1.15  christos     return 1;			/* Keep lint, etc., happy */
   1569  1.15  christos }
   1570   1.1       cgd 
   1571   1.1       cgd /*VARARGS*/
   1572   1.1       cgd int
   1573   1.1       cgd quit(argc, argv)
   1574   1.1       cgd 	int argc;
   1575   1.1       cgd 	char *argv[];
   1576   1.1       cgd {
   1577   1.1       cgd 	(void) call(bye, "bye", "fromquit", 0);
   1578  1.15  christos 	Exit(0);
   1579  1.15  christos 	/*NOTREACHED*/
   1580  1.15  christos }
   1581   1.1       cgd 
   1582   1.1       cgd /*VARARGS*/
   1583   1.1       cgd 	int
   1584   1.1       cgd logout(argc, argv)
   1585   1.1       cgd 	int argc;
   1586   1.1       cgd 	char *argv[];
   1587   1.1       cgd {
   1588   1.1       cgd 	send_do(TELOPT_LOGOUT, 1);
   1589   1.1       cgd 	(void) netflush();
   1590   1.1       cgd 	return 1;
   1591   1.1       cgd }
   1592   1.1       cgd 
   1593   1.1       cgd 
   1594   1.1       cgd /*
   1596   1.1       cgd  * The SLC command.
   1597   1.1       cgd  */
   1598   1.1       cgd 
   1599   1.1       cgd struct slclist {
   1600   1.1       cgd 	char	*name;
   1601   1.1       cgd 	char	*help;
   1602   1.1       cgd 	void	(*handler) P((int));
   1603   1.1       cgd 	int	arg;
   1604   1.1       cgd };
   1605   1.1       cgd 
   1606   1.1       cgd struct slclist SlcList[] = {
   1607   1.1       cgd     { "export",	"Use local special character definitions",
   1608   1.1       cgd 						slc_mode_export,	0 },
   1609   1.1       cgd     { "import",	"Use remote special character definitions",
   1610   1.1       cgd 						slc_mode_import,	1 },
   1611   1.1       cgd     { "check",	"Verify remote special character definitions",
   1612  1.15  christos 						slc_mode_import,	0 },
   1613  1.15  christos     { "help",	0,				slc_help,		0 },
   1614   1.1       cgd     { "?",	"Print help information",	slc_help,		0 },
   1615   1.1       cgd     { 0 },
   1616   1.1       cgd };
   1617   1.1       cgd 
   1618   1.1       cgd     static void
   1619   1.1       cgd slc_help(n)
   1620   1.1       cgd     int n;
   1621   1.1       cgd {
   1622   1.1       cgd     struct slclist *c;
   1623   1.1       cgd 
   1624   1.1       cgd     for (c = SlcList; c->name; c++) {
   1625   1.1       cgd 	if (c->help) {
   1626   1.1       cgd 	    if (*c->help)
   1627   1.1       cgd 		printf("%-15s %s\n", c->name, c->help);
   1628   1.1       cgd 	    else
   1629   1.1       cgd 		printf("\n");
   1630   1.1       cgd 	}
   1631   1.1       cgd     }
   1632   1.1       cgd }
   1633   1.1       cgd 
   1634   1.1       cgd     static struct slclist *
   1635  1.15  christos getslc(name)
   1636   1.1       cgd     char *name;
   1637   1.1       cgd {
   1638   1.1       cgd     return (struct slclist *)
   1639   1.1       cgd 		genget(name, (char **) SlcList, sizeof(struct slclist));
   1640   1.1       cgd }
   1641   1.1       cgd 
   1642   1.1       cgd     static int
   1643   1.1       cgd slccmd(argc, argv)
   1644   1.1       cgd     int  argc;
   1645   1.1       cgd     char *argv[];
   1646   1.1       cgd {
   1647   1.1       cgd     struct slclist *c;
   1648   1.1       cgd 
   1649  1.10       jtk     if (argc != 2) {
   1650   1.1       cgd 	fprintf(stderr,
   1651  1.10       jtk 	    "Need an argument to 'slc' command.  'slc ?' for help.\n");
   1652   1.1       cgd 	return 0;
   1653   1.1       cgd     }
   1654  1.10       jtk     c = getslc(argv[1]);
   1655   1.1       cgd     if (c == 0) {
   1656  1.10       jtk 	fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\n",
   1657   1.1       cgd     				argv[1]);
   1658   1.1       cgd 	return 0;
   1659   1.1       cgd     }
   1660   1.1       cgd     if (Ambiguous(c)) {
   1661   1.1       cgd 	fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\n",
   1662   1.1       cgd     				argv[1]);
   1663   1.1       cgd 	return 0;
   1664   1.1       cgd     }
   1665   1.1       cgd     (*c->handler)(c->arg);
   1666   1.1       cgd     slcstate();
   1667   1.1       cgd     return 1;
   1668   1.1       cgd }
   1669   1.1       cgd 
   1670  1.15  christos /*
   1672   1.1       cgd  * The ENVIRON command.
   1673   1.1       cgd  */
   1674   1.1       cgd 
   1675   1.1       cgd struct envlist {
   1676  1.15  christos 	char	*name;
   1677   1.1       cgd 	char	*help;
   1678   1.1       cgd 	struct env_lst *(*handler) P((unsigned char *, unsigned char *));
   1679   1.1       cgd 	int	narg;
   1680   1.1       cgd };
   1681   1.1       cgd 
   1682   1.1       cgd struct envlist EnvList[] = {
   1683   1.1       cgd     { "define",	"Define an environment variable",
   1684   1.1       cgd 						env_define,	2 },
   1685   1.1       cgd     { "undefine", "Undefine an environment variable",
   1686   1.5       cgd 						env_undefine,	1 },
   1687   1.5       cgd     { "export",	"Mark an environment variable for automatic export",
   1688   1.5       cgd 						env_export,	1 },
   1689   1.5       cgd     { "unexport", "Don't mark an environment variable for automatic export",
   1690   1.1       cgd 						env_unexport,	1 },
   1691   1.1       cgd     { "send",	"Send an environment variable", env_send,	1 },
   1692   1.1       cgd     { "list",	"List the current environment variables",
   1693   1.1       cgd 						env_list,	0 },
   1694   1.1       cgd #if defined(OLD_ENVIRON) && defined(ENV_HACK)
   1695  1.15  christos     { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
   1696  1.15  christos 						env_varval,    1 },
   1697  1.15  christos #endif
   1698   1.1       cgd     { "help",	0,				env_help,		0 },
   1699   1.1       cgd     { "?",	"Print help information",	env_help,		0 },
   1700   1.1       cgd     { 0 },
   1701   1.1       cgd };
   1702   1.1       cgd 
   1703   1.1       cgd     static struct env_lst *
   1704   1.1       cgd env_help(us1, us2)
   1705   1.1       cgd     unsigned char *us1, *us2;
   1706   1.1       cgd {
   1707   1.1       cgd     struct envlist *c;
   1708   1.1       cgd 
   1709  1.15  christos     for (c = EnvList; c->name; c++) {
   1710   1.1       cgd 	if (c->help) {
   1711   1.1       cgd 	    if (*c->help)
   1712   1.1       cgd 		printf("%-15s %s\n", c->name, c->help);
   1713   1.1       cgd 	    else
   1714   1.1       cgd 		printf("\n");
   1715   1.1       cgd 	}
   1716   1.1       cgd     }
   1717   1.1       cgd     return NULL;
   1718   1.1       cgd }
   1719   1.1       cgd 
   1720  1.15  christos     static struct envlist *
   1721   1.1       cgd getenvcmd(name)
   1722   1.1       cgd     char *name;
   1723   1.1       cgd {
   1724   1.1       cgd     return (struct envlist *)
   1725   1.1       cgd 		genget(name, (char **) EnvList, sizeof(struct envlist));
   1726   1.1       cgd }
   1727   1.1       cgd 
   1728   1.1       cgd     int
   1729   1.1       cgd env_cmd(argc, argv)
   1730   1.1       cgd     int  argc;
   1731   1.1       cgd     char *argv[];
   1732   1.1       cgd {
   1733   1.1       cgd     struct envlist *c;
   1734  1.10       jtk 
   1735   1.1       cgd     if (argc < 2) {
   1736  1.10       jtk 	fprintf(stderr,
   1737   1.1       cgd 	    "Need an argument to 'environ' command.  'environ ?' for help.\n");
   1738   1.1       cgd 	return 0;
   1739  1.10       jtk     }
   1740   1.1       cgd     c = getenvcmd(argv[1]);
   1741  1.10       jtk     if (c == 0) {
   1742   1.1       cgd 	fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\n",
   1743   1.1       cgd     				argv[1]);
   1744   1.1       cgd 	return 0;
   1745   1.1       cgd     }
   1746   1.1       cgd     if (Ambiguous(c)) {
   1747   1.1       cgd 	fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\n",
   1748   1.1       cgd     				argv[1]);
   1749   1.1       cgd 	return 0;
   1750   1.1       cgd     }
   1751   1.1       cgd     if (c->narg + 2 != argc) {
   1752   1.1       cgd 	fprintf(stderr,
   1753   1.1       cgd 	    "Need %s%d argument%s to 'environ %s' command.  'environ ?' for help.\n",
   1754   1.1       cgd 		c->narg < argc + 2 ? "only " : "",
   1755   1.1       cgd 		c->narg, c->narg == 1 ? "" : "s", c->name);
   1756   1.5       cgd 	return 0;
   1757   1.1       cgd     }
   1758   1.5       cgd     (*c->handler)(argv[2], argv[3]);
   1759   1.1       cgd     return 1;
   1760   1.5       cgd }
   1761   1.1       cgd 
   1762   1.1       cgd struct env_lst {
   1763   1.1       cgd 	struct env_lst *next;	/* pointer to next structure */
   1764   1.1       cgd 	struct env_lst *prev;	/* pointer to previous structure */
   1765   1.1       cgd 	unsigned char *var;	/* pointer to variable name */
   1766   1.1       cgd 	unsigned char *value;	/* pointer to variable value */
   1767   1.1       cgd 	int export;		/* 1 -> export with default list of variables */
   1768   1.1       cgd 	int welldefined;	/* A well defined variable */
   1769   1.1       cgd };
   1770   1.1       cgd 
   1771   1.1       cgd struct env_lst envlisthead;
   1772   1.1       cgd 
   1773   1.1       cgd 	struct env_lst *
   1774   1.1       cgd env_find(var)
   1775   1.1       cgd 	unsigned char *var;
   1776   1.1       cgd {
   1777   1.1       cgd 	register struct env_lst *ep;
   1778   1.1       cgd 
   1779   1.1       cgd 	for (ep = envlisthead.next; ep; ep = ep->next) {
   1780   1.1       cgd 		if (strcmp((char *)ep->var, (char *)var) == 0)
   1781   1.1       cgd 			return(ep);
   1782   1.1       cgd 	}
   1783   1.1       cgd 	return(NULL);
   1784   1.1       cgd }
   1785   1.1       cgd 
   1786  1.15  christos 	void
   1787   1.1       cgd env_init()
   1788   1.1       cgd {
   1789   1.1       cgd 	extern char **environ;
   1790   1.1       cgd 	register char **epp, *cp;
   1791   1.1       cgd 	register struct env_lst *ep;
   1792   1.1       cgd 
   1793   1.1       cgd 	for (epp = environ; *epp; epp++) {
   1794   1.1       cgd 		if ((cp = strchr(*epp, '=')) != NULL) {
   1795   1.1       cgd 			*cp = '\0';
   1796   1.1       cgd 			ep = env_define((unsigned char *)*epp,
   1797   1.1       cgd 					(unsigned char *)cp+1);
   1798   1.1       cgd 			ep->export = 0;
   1799   1.1       cgd 			*cp = '=';
   1800   1.1       cgd 		}
   1801  1.10       jtk 	}
   1802  1.21       mrg 	/*
   1803  1.10       jtk 	 * Special case for DISPLAY variable.  If it is ":0.0" or
   1804   1.1       cgd 	 * "unix:0.0", we have to get rid of "unix" and insert our
   1805  1.21       mrg 	 * hostname.
   1806  1.22      fvdl 	 */
   1807   1.1       cgd 	if ((ep = env_find("DISPLAY"))
   1808   1.1       cgd 	    && ((*ep->value == ':')
   1809   1.1       cgd 		|| (strncmp((char *)ep->value, "unix:", 5) == 0))) {
   1810   1.1       cgd 		char hbuf[MAXHOSTNAMELEN + 1];
   1811   1.1       cgd 		char *cp2 = strchr((char *)ep->value, ':');
   1812   1.1       cgd 
   1813   1.1       cgd 		gethostname(hbuf, sizeof hbuf);
   1814   1.1       cgd 		hbuf[sizeof(hbuf) - 1] = '\0';
   1815   1.1       cgd 		cp = (char *)malloc(strlen(hbuf) + strlen(cp2) + 1);
   1816   1.1       cgd 		sprintf((char *)cp, "%s%s", hbuf, cp2);
   1817   1.1       cgd 		free(ep->value);
   1818   1.1       cgd 		ep->value = (unsigned char *)cp;
   1819  1.15  christos 	}
   1820   1.1       cgd 	/*
   1821  1.15  christos 	 * If USER is not defined, but LOGNAME is, then add
   1822  1.15  christos 	 * USER with the value from LOGNAME.  By default, we
   1823   1.1       cgd 	 * don't export the USER variable.
   1824   1.1       cgd 	 */
   1825   1.1       cgd 	if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
   1826   1.1       cgd 		env_define((unsigned char *)"USER", ep->value);
   1827   1.1       cgd 		env_unexport((unsigned char *)"USER", NULL);
   1828   1.1       cgd 	}
   1829   1.1       cgd 	env_export((unsigned char *)"DISPLAY", NULL);
   1830   1.1       cgd 	env_export((unsigned char *)"PRINTER", NULL);
   1831  1.15  christos }
   1832   1.1       cgd 
   1833   1.1       cgd 	struct env_lst *
   1834   1.1       cgd env_define(var, value)
   1835   1.1       cgd 	unsigned char *var, *value;
   1836   1.1       cgd {
   1837   1.1       cgd 	register struct env_lst *ep;
   1838   1.1       cgd 
   1839   1.1       cgd 	if ((ep = env_find(var)) != NULL) {
   1840   1.1       cgd 		if (ep->var)
   1841   1.1       cgd 			free(ep->var);
   1842   1.1       cgd 		if (ep->value)
   1843   1.1       cgd 			free(ep->value);
   1844   1.5       cgd 	} else {
   1845   1.1       cgd 		ep = (struct env_lst *)malloc(sizeof(struct env_lst));
   1846   1.1       cgd 		ep->next = envlisthead.next;
   1847   1.1       cgd 		envlisthead.next = ep;
   1848   1.1       cgd 		ep->prev = &envlisthead;
   1849   1.1       cgd 		if (ep->next)
   1850   1.1       cgd 			ep->next->prev = ep;
   1851  1.15  christos 	}
   1852  1.15  christos 	ep->welldefined = opt_welldefined(var);
   1853   1.1       cgd 	ep->export = 1;
   1854  1.15  christos 	ep->var = (unsigned char *)strdup((char *)var);
   1855   1.1       cgd 	ep->value = (unsigned char *)strdup((char *)value);
   1856   1.1       cgd 	return(ep);
   1857   1.1       cgd }
   1858  1.15  christos 
   1859   1.1       cgd 	struct env_lst *
   1860   1.1       cgd env_undefine(var, d)
   1861   1.1       cgd 	unsigned char *var;
   1862   1.1       cgd 	unsigned char *d;
   1863   1.1       cgd {
   1864   1.1       cgd 	register struct env_lst *ep;
   1865   1.1       cgd 
   1866   1.1       cgd 	if ((ep = env_find(var)) != NULL) {
   1867   1.1       cgd 		ep->prev->next = ep->next;
   1868  1.15  christos 		if (ep->next)
   1869   1.1       cgd 			ep->next->prev = ep->prev;
   1870   1.1       cgd 		if (ep->var)
   1871  1.15  christos 			free(ep->var);
   1872  1.15  christos 		if (ep->value)
   1873   1.1       cgd 			free(ep->value);
   1874  1.15  christos 		free(ep);
   1875   1.1       cgd 	}
   1876   1.1       cgd 	return NULL;
   1877   1.1       cgd }
   1878  1.15  christos 
   1879   1.1       cgd 	struct env_lst *
   1880  1.15  christos env_export(var, d)
   1881   1.1       cgd 	unsigned char *var;
   1882   1.1       cgd 	unsigned char *d;
   1883  1.15  christos {
   1884  1.15  christos 	register struct env_lst *ep;
   1885   1.1       cgd 
   1886  1.15  christos 	if ((ep = env_find(var)) != NULL)
   1887   1.1       cgd 		ep->export = 1;
   1888   1.1       cgd 	return NULL;
   1889   1.1       cgd }
   1890  1.15  christos 
   1891   1.1       cgd 	struct env_lst *
   1892  1.15  christos env_unexport(var, d)
   1893   1.1       cgd 	unsigned char *var;
   1894   1.1       cgd 	unsigned char *d;
   1895  1.15  christos {
   1896  1.15  christos 	register struct env_lst *ep;
   1897   1.1       cgd 
   1898  1.15  christos 	if ((ep = env_find(var)) != NULL)
   1899   1.1       cgd 		ep->export = 0;
   1900   1.1       cgd 	return NULL;
   1901   1.1       cgd }
   1902  1.10       jtk 
   1903   1.5       cgd 	struct env_lst *
   1904   1.5       cgd env_send(var, d)
   1905   1.5       cgd 	unsigned char *var;
   1906   1.5       cgd 	unsigned char *d;
   1907   1.1       cgd {
   1908   1.1       cgd 	register struct env_lst *ep;
   1909   1.1       cgd 
   1910  1.15  christos 	if (my_state_is_wont(TELOPT_NEW_ENVIRON)
   1911   1.1       cgd #ifdef	OLD_ENVIRON
   1912   1.1       cgd 	    && my_state_is_wont(TELOPT_OLD_ENVIRON)
   1913   1.1       cgd #endif
   1914   1.1       cgd 		) {
   1915   1.1       cgd 		fprintf(stderr,
   1916  1.15  christos 		    "Cannot send '%s': Telnet ENVIRON option not enabled\n",
   1917   1.1       cgd 									var);
   1918   1.1       cgd 		return NULL;
   1919   1.1       cgd 	}
   1920   1.1       cgd 	ep = env_find(var);
   1921  1.15  christos 	if (ep == 0) {
   1922   1.1       cgd 		fprintf(stderr, "Cannot send '%s': variable not defined\n",
   1923   1.1       cgd 									var);
   1924  1.15  christos 		return NULL;
   1925  1.15  christos 	}
   1926  1.15  christos 	env_opt_start_info();
   1927   1.1       cgd 	env_opt_add(ep->var);
   1928   1.1       cgd 	env_opt_end(0);
   1929   1.1       cgd 	return NULL;
   1930   1.1       cgd }
   1931   1.1       cgd 
   1932   1.1       cgd 	struct env_lst *
   1933   1.1       cgd env_list(d1, d2)
   1934  1.15  christos 	unsigned char *d1, *d2;
   1935   1.1       cgd {
   1936   1.1       cgd 	register struct env_lst *ep;
   1937   1.1       cgd 
   1938   1.5       cgd 	for (ep = envlisthead.next; ep; ep = ep->next) {
   1939   1.1       cgd 		printf("%c %-20s %s\n", ep->export ? '*' : ' ',
   1940  1.23   msaitoh 					ep->var, ep->value);
   1941   1.1       cgd 	}
   1942   1.1       cgd 	return NULL;
   1943   1.1       cgd }
   1944   1.1       cgd 
   1945   1.1       cgd 	unsigned char *
   1946  1.15  christos env_default(init, welldefined)
   1947   1.1       cgd 	int init;
   1948   1.1       cgd 	int welldefined;
   1949  1.15  christos {
   1950   1.5       cgd 	static struct env_lst *nep = NULL;
   1951   1.1       cgd 
   1952   1.1       cgd 	if (init) {
   1953   1.1       cgd 		nep = &envlisthead;
   1954   1.1       cgd 		return NULL;
   1955   1.1       cgd 	}
   1956   1.1       cgd 	if (nep) {
   1957   1.1       cgd 		while ((nep = nep->next) != NULL) {
   1958   1.1       cgd 			if (nep->export && (nep->welldefined == welldefined))
   1959   1.1       cgd 				return(nep->var);
   1960   1.1       cgd 		}
   1961   1.1       cgd 	}
   1962   1.1       cgd 	return(NULL);
   1963  1.15  christos }
   1964   1.1       cgd 
   1965   1.1       cgd 	unsigned char *
   1966   1.1       cgd env_getvalue(var)
   1967   1.1       cgd 	unsigned char *var;
   1968   1.5       cgd {
   1969   1.5       cgd 	register struct env_lst *ep;
   1970   1.5       cgd 
   1971   1.5       cgd 	if ((ep = env_find(var)) != NULL)
   1972   1.5       cgd 		return(ep->value);
   1973   1.5       cgd 	return(NULL);
   1974   1.5       cgd }
   1975   1.5       cgd 
   1976   1.5       cgd #if defined(OLD_ENVIRON) && defined(ENV_HACK)
   1977   1.5       cgd 	void
   1978   1.5       cgd env_varval(what)
   1979   1.5       cgd 	unsigned char *what;
   1980   1.5       cgd {
   1981   1.5       cgd 	extern int old_env_var, old_env_value, env_auto;
   1982   1.5       cgd 	int len = strlen((char *)what);
   1983   1.5       cgd 
   1984   1.5       cgd 	if (len == 0)
   1985   1.5       cgd 		goto unknown;
   1986   1.5       cgd 
   1987   1.5       cgd 	if (strncasecmp((char *)what, "status", len) == 0) {
   1988   1.5       cgd 		if (env_auto)
   1989   1.5       cgd 			printf("%s%s", "VAR and VALUE are/will be ",
   1990   1.5       cgd 					"determined automatically\n");
   1991   1.5       cgd 		if (old_env_var == OLD_ENV_VAR)
   1992   1.5       cgd 			printf("VAR and VALUE set to correct definitions\n");
   1993   1.5       cgd 		else
   1994   1.5       cgd 			printf("VAR and VALUE definitions are reversed\n");
   1995   1.5       cgd 	} else if (strncasecmp((char *)what, "auto", len) == 0) {
   1996   1.5       cgd 		env_auto = 1;
   1997   1.5       cgd 		old_env_var = OLD_ENV_VALUE;
   1998   1.5       cgd 		old_env_value = OLD_ENV_VAR;
   1999   1.5       cgd 	} else if (strncasecmp((char *)what, "right", len) == 0) {
   2000   1.5       cgd 		env_auto = 0;
   2001   1.5       cgd 		old_env_var = OLD_ENV_VAR;
   2002   1.5       cgd 		old_env_value = OLD_ENV_VALUE;
   2003   1.5       cgd 	} else if (strncasecmp((char *)what, "wrong", len) == 0) {
   2004   1.5       cgd 		env_auto = 0;
   2005   1.5       cgd 		old_env_var = OLD_ENV_VALUE;
   2006   1.5       cgd 		old_env_value = OLD_ENV_VAR;
   2007   1.1       cgd 	} else {
   2008   1.1       cgd unknown:
   2009   1.1       cgd 		printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
   2010   1.1       cgd 	}
   2011   1.1       cgd }
   2012   1.1       cgd #endif
   2013   1.1       cgd 
   2014  1.25  christos #if	defined(AUTHENTICATION)
   2015   1.1       cgd /*
   2016   1.1       cgd  * The AUTHENTICATE command.
   2017   1.1       cgd  */
   2018   1.1       cgd 
   2019   1.1       cgd struct authlist {
   2020   1.1       cgd 	char	*name;
   2021   1.1       cgd 	char	*help;
   2022   1.1       cgd 	int	(*handler) P((char *));
   2023   1.1       cgd 	int	narg;
   2024   1.1       cgd };
   2025   1.1       cgd 
   2026   1.1       cgd struct authlist AuthList[] = {
   2027   1.1       cgd     { "status",	"Display current status of authentication information",
   2028   1.1       cgd 						auth_status,	0 },
   2029   1.1       cgd     { "disable", "Disable an authentication type ('auth disable ?' for more)",
   2030   1.1       cgd 						auth_disable,	1 },
   2031  1.25  christos     { "enable", "Enable an authentication type ('auth enable ?' for more)",
   2032  1.25  christos 						auth_enable,	1 },
   2033   1.1       cgd     { "help",	0,				auth_help,		0 },
   2034   1.1       cgd     { "?",	"Print help information",	auth_help,		0 },
   2035   1.1       cgd     { 0 },
   2036   1.1       cgd };
   2037   1.1       cgd 
   2038   1.1       cgd     static int
   2039   1.1       cgd auth_help(s)
   2040   1.1       cgd     char *s;
   2041   1.1       cgd {
   2042   1.1       cgd     struct authlist *c;
   2043   1.1       cgd 
   2044   1.1       cgd     for (c = AuthList; c->name; c++) {
   2045   1.1       cgd 	if (c->help) {
   2046   1.1       cgd 	    if (*c->help)
   2047  1.25  christos 		printf("%-15s %s\n", c->name, c->help);
   2048   1.1       cgd 	    else
   2049   1.1       cgd 		printf("\n");
   2050   1.1       cgd 	}
   2051   1.1       cgd     }
   2052   1.1       cgd     return 0;
   2053   1.1       cgd }
   2054  1.10       jtk 
   2055  1.10       jtk     int
   2056  1.10       jtk auth_cmd(argc, argv)
   2057  1.10       jtk     int  argc;
   2058  1.10       jtk     char *argv[];
   2059  1.10       jtk {
   2060   1.1       cgd     struct authlist *c;
   2061   1.1       cgd 
   2062   1.1       cgd     if (argc < 2) {
   2063  1.10       jtk 	fprintf(stderr,
   2064   1.1       cgd 	    "Need an argument to 'auth' command.  'auth ?' for help.\n");
   2065  1.10       jtk 	return 0;
   2066   1.1       cgd     }
   2067   1.1       cgd 
   2068  1.10       jtk     c = (struct authlist *)
   2069   1.1       cgd 		genget(argv[1], (char **) AuthList, sizeof(struct authlist));
   2070  1.10       jtk     if (c == 0) {
   2071   1.1       cgd 	fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\n",
   2072   1.1       cgd     				argv[1]);
   2073   1.1       cgd 	return 0;
   2074   1.1       cgd     }
   2075   1.1       cgd     if (Ambiguous(c)) {
   2076   1.1       cgd 	fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\n",
   2077   1.1       cgd     				argv[1]);
   2078   1.1       cgd 	return 0;
   2079  1.25  christos     }
   2080   1.1       cgd     if (c->narg + 2 != argc) {
   2081   1.1       cgd 	fprintf(stderr,
   2082   1.1       cgd 	    "Need %s%d argument%s to 'auth %s' command.  'auth ?' for help.\n",
   2083  1.43   thorpej 		c->narg < argc + 2 ? "only " : "",
   2084  1.43   thorpej 		c->narg, c->narg == 1 ? "" : "s", c->name);
   2085  1.43   thorpej 	return 0;
   2086  1.43   thorpej     }
   2087  1.43   thorpej     return((*c->handler)(argv[2]));
   2088  1.43   thorpej }
   2089  1.43   thorpej #endif
   2090  1.43   thorpej 
   2091  1.43   thorpej #ifdef	ENCRYPTION
   2092  1.43   thorpej /*
   2093  1.43   thorpej  * The ENCRYPT command.
   2094  1.43   thorpej  */
   2095  1.43   thorpej 
   2096  1.43   thorpej struct encryptlist {
   2097  1.43   thorpej 	char	*name;
   2098  1.43   thorpej 	char	*help;
   2099  1.43   thorpej 	int	(*handler) P((char *, char *));
   2100  1.43   thorpej 	int	needconnect;
   2101  1.43   thorpej 	int	minarg;
   2102  1.43   thorpej 	int	maxarg;
   2103  1.43   thorpej };
   2104  1.43   thorpej 
   2105  1.43   thorpej static int
   2106  1.43   thorpej 	EncryptHelp P((char *, char *));
   2107  1.43   thorpej typedef int (*encrypthandler) P((char *, char *));
   2108  1.43   thorpej 
   2109  1.43   thorpej struct encryptlist EncryptList[] = {
   2110  1.43   thorpej     { "enable", "Enable encryption. ('encrypt enable ?' for more)",
   2111  1.43   thorpej 						EncryptEnable, 1, 1, 2 },
   2112  1.43   thorpej     { "disable", "Disable encryption. ('encrypt enable ?' for more)",
   2113  1.43   thorpej 						EncryptDisable, 0, 1, 2 },
   2114  1.43   thorpej     { "type", "Set encryption type. ('encrypt type ?' for more)",
   2115  1.43   thorpej 						EncryptType, 0, 1, 1 },
   2116  1.43   thorpej     { "start", "Start encryption. ('encrypt start ?' for more)",
   2117  1.43   thorpej 				(encrypthandler) EncryptStart, 1, 0, 1 },
   2118  1.43   thorpej     { "stop", "Stop encryption. ('encrypt stop ?' for more)",
   2119  1.43   thorpej 				(encrypthandler) EncryptStop, 1, 0, 1 },
   2120  1.43   thorpej     { "input", "Start encrypting the input stream",
   2121  1.43   thorpej 				(encrypthandler) EncryptStartInput, 1, 0, 0 },
   2122  1.43   thorpej     { "-input", "Stop encrypting the input stream",
   2123  1.43   thorpej 				(encrypthandler) EncryptStopInput, 1, 0, 0 },
   2124  1.43   thorpej     { "output", "Start encrypting the output stream",
   2125  1.43   thorpej 				(encrypthandler) EncryptStartOutput, 1, 0, 0 },
   2126  1.43   thorpej     { "-output", "Stop encrypting the output stream",
   2127  1.43   thorpej 				(encrypthandler) EncryptStopOutput, 1, 0, 0 },
   2128  1.43   thorpej 
   2129  1.43   thorpej     { "status",       "Display current status of authentication information",
   2130  1.43   thorpej 				(encrypthandler) EncryptStatus,	0, 0, 0 },
   2131  1.43   thorpej     { "help", 0,				 EncryptHelp,	0, 0, 0 },
   2132  1.43   thorpej     { "?",    "Print help information",		 EncryptHelp,	0, 0, 0 },
   2133  1.43   thorpej     { 0 },
   2134  1.43   thorpej };
   2135  1.43   thorpej 
   2136  1.43   thorpej static int
   2137  1.43   thorpej EncryptHelp(s1, s2)
   2138  1.43   thorpej 	char *s1, *s2;
   2139  1.43   thorpej {
   2140  1.43   thorpej 	struct encryptlist *c;
   2141  1.43   thorpej 
   2142  1.43   thorpej 	for (c = EncryptList; c->name; c++) {
   2143  1.43   thorpej 		if (c->help) {
   2144  1.43   thorpej 			if (*c->help)
   2145  1.43   thorpej 				printf("%-15s %s\n", c->name, c->help);
   2146  1.43   thorpej 			else
   2147  1.43   thorpej 				printf("\n");
   2148  1.43   thorpej 		}
   2149  1.43   thorpej 	}
   2150  1.43   thorpej 	return (0);
   2151  1.43   thorpej }
   2152  1.43   thorpej 
   2153  1.43   thorpej int
   2154  1.43   thorpej encrypt_cmd(argc, argv)
   2155  1.43   thorpej 	int argc;
   2156  1.43   thorpej 	char *argv[];
   2157  1.43   thorpej {
   2158  1.43   thorpej 	struct encryptlist *c;
   2159  1.43   thorpej 
   2160  1.43   thorpej 	if (argc < 2) {
   2161  1.43   thorpej 		fprintf(stderr,
   2162  1.43   thorpej 		    "Need an argument to 'encrypt' command.  "
   2163  1.43   thorpej 		    "'encrypt ?' for help.\n");
   2164  1.43   thorpej 		return (0);
   2165  1.43   thorpej 	}
   2166  1.43   thorpej 
   2167  1.43   thorpej 	c = (struct encryptlist *)
   2168  1.43   thorpej 	    genget(argv[1], (char **) EncryptList, sizeof(struct encryptlist));
   2169  1.43   thorpej 	if (c == NULL) {
   2170  1.43   thorpej 		fprintf(stderr,
   2171  1.43   thorpej 		    "'%s': unknown argument ('encrypt ?' for help).\n",
   2172  1.43   thorpej 		    argv[1]);
   2173  1.43   thorpej 		return (0);
   2174  1.43   thorpej 	}
   2175  1.43   thorpej 	if (Ambiguous(c)) {
   2176  1.43   thorpej 		fprintf(stderr,
   2177  1.43   thorpej 		    "'%s': ambiguous argument ('encrypt ?' for help).\n",
   2178  1.43   thorpej 		    argv[1]);
   2179  1.43   thorpej 		return (0);
   2180  1.43   thorpej 	}
   2181  1.43   thorpej 	argc -= 2;
   2182  1.43   thorpej 	if (argc < c->minarg || argc > c->maxarg) {
   2183  1.43   thorpej 		if (c->minarg == c->maxarg) {
   2184  1.43   thorpej 			fprintf(stderr, "Need %s%d argument%s ",
   2185  1.43   thorpej 			    c->minarg < argc ? "only " : "", c->minarg,
   2186  1.43   thorpej 			    c->minarg == 1 ? "" : "s");
   2187  1.43   thorpej 		} else {
   2188  1.43   thorpej 			fprintf(stderr, "Need %s%d-%d arguments ",
   2189  1.43   thorpej 			    c->maxarg < argc ? "only " : "", c->minarg,
   2190  1.43   thorpej 			    c->maxarg);
   2191  1.43   thorpej 		}
   2192  1.43   thorpej 		fprintf(stderr,
   2193  1.43   thorpej 		    "to 'encrypt %s' command.  'encrypt ?' for help.\n",
   2194  1.43   thorpej 		    c->name);
   2195  1.43   thorpej 		return (0);
   2196  1.43   thorpej 	}
   2197  1.43   thorpej 	if (c->needconnect && !connected) {
   2198  1.43   thorpej 		if (!(argc && (isprefix(argv[2], "help") ||
   2199   1.1       cgd 		    isprefix(argv[2], "?")))) {
   2200   1.1       cgd 			printf("?Need to be connected first.\n");
   2201   1.1       cgd 			return (0);
   2202   1.1       cgd 		}
   2203   1.1       cgd 	}
   2204   1.1       cgd 	return ((*c->handler)(argv[2], argv[3]));
   2205   1.1       cgd }
   2206   1.1       cgd #endif	/* ENCRYPTION */
   2207   1.1       cgd 
   2208   1.1       cgd #if	defined(unix) && defined(TN3270)
   2209   1.1       cgd     static void
   2210   1.1       cgd filestuff(fd)
   2211   1.1       cgd     int fd;
   2212   1.1       cgd {
   2213   1.1       cgd     int res;
   2214   1.1       cgd 
   2215   1.1       cgd #ifdef	F_GETOWN
   2216   1.1       cgd     setconnmode(0);
   2217   1.1       cgd     res = fcntl(fd, F_GETOWN, 0);
   2218   1.1       cgd     setcommandmode();
   2219   1.1       cgd 
   2220   1.1       cgd     if (res == -1) {
   2221   1.1       cgd 	perror("fcntl");
   2222   1.1       cgd 	return;
   2223   1.1       cgd     }
   2224   1.1       cgd     printf("\tOwner is %d.\n", res);
   2225   1.1       cgd #endif
   2226   1.1       cgd 
   2227   1.5       cgd     setconnmode(0);
   2228   1.5       cgd     res = fcntl(fd, F_GETFL, 0);
   2229   1.5       cgd     setcommandmode();
   2230   1.1       cgd 
   2231   1.1       cgd     if (res == -1) {
   2232   1.1       cgd 	perror("fcntl");
   2233   1.1       cgd 	return;
   2234   1.1       cgd     }
   2235   1.1       cgd #ifdef notdef
   2236   1.1       cgd     printf("\tFlags are 0x%x: %s\n", res, decodeflags(res));
   2237  1.15  christos #endif
   2238   1.1       cgd }
   2239   1.1       cgd #endif /* defined(unix) && defined(TN3270) */
   2240   1.1       cgd 
   2241   1.1       cgd /*
   2242   1.1       cgd  * Print status about the connection.
   2243   1.1       cgd  */
   2244   1.1       cgd     /*ARGSUSED*/
   2245   1.1       cgd     static int
   2246   1.1       cgd status(argc, argv)
   2247   1.1       cgd     int	 argc;
   2248   1.1       cgd     char *argv[];
   2249   1.1       cgd {
   2250   1.1       cgd     if (connected) {
   2251   1.1       cgd 	printf("Connected to %s.\n", hostname);
   2252   1.1       cgd 	if ((argc < 2) || strcmp(argv[1], "notmuch")) {
   2253   1.1       cgd 	    int mode = getconnmode();
   2254   1.1       cgd 
   2255   1.1       cgd 	    if (my_want_state_is_will(TELOPT_LINEMODE)) {
   2256   1.1       cgd 		printf("Operating with LINEMODE option\n");
   2257   1.1       cgd 		printf("%s line editing\n", (mode&MODE_EDIT) ? "Local" : "No");
   2258   1.1       cgd 		printf("%s catching of signals\n",
   2259   1.1       cgd 					(mode&MODE_TRAPSIG) ? "Local" : "No");
   2260   1.1       cgd 		slcstate();
   2261   1.1       cgd #ifdef	KLUDGELINEMODE
   2262   1.1       cgd 	    } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
   2263   1.1       cgd 		printf("Operating in obsolete linemode\n");
   2264   1.1       cgd #endif
   2265  1.43   thorpej 	    } else {
   2266  1.43   thorpej 		printf("Operating in single character mode\n");
   2267  1.43   thorpej 		if (localchars)
   2268   1.1       cgd 		    printf("Catching signals locally\n");
   2269   1.1       cgd 	    }
   2270   1.1       cgd 	    printf("%s character echo\n", (mode&MODE_ECHO) ? "Local" : "Remote");
   2271   1.1       cgd 	    if (my_want_state_is_will(TELOPT_LFLOW))
   2272   1.1       cgd 		printf("%s flow control\n", (mode&MODE_FLOW) ? "Local" : "No");
   2273   1.1       cgd #ifdef	ENCRYPTION
   2274   1.1       cgd 	    encrypt_display();
   2275   1.1       cgd #endif	/* ENCRYPTION */
   2276   1.1       cgd 	}
   2277   1.1       cgd     } else {
   2278   1.1       cgd 	printf("No connection.\n");
   2279   1.1       cgd     }
   2280   1.1       cgd #   if !defined(TN3270)
   2281   1.1       cgd     printf("Escape character is '%s'.\n", control(escape));
   2282   1.1       cgd     (void) fflush(stdout);
   2283   1.1       cgd #   else /* !defined(TN3270) */
   2284   1.1       cgd     if ((!In3270) && ((argc < 2) || strcmp(argv[1], "notmuch"))) {
   2285  1.16     enami 	printf("Escape character is '%s'.\n", control(escape));
   2286   1.1       cgd     }
   2287   1.1       cgd #   if defined(unix)
   2288   1.1       cgd     if ((argc >= 2) && !strcmp(argv[1], "everything")) {
   2289   1.1       cgd 	printf("SIGIO received %d time%s.\n",
   2290   1.1       cgd 				sigiocount, (sigiocount == 1)? "":"s");
   2291   1.1       cgd 	if (In3270) {
   2292   1.1       cgd 	    printf("Process ID %d, process group %d.\n",
   2293   1.1       cgd 					    getpid(), getpgrp());
   2294   1.1       cgd 	    printf("Terminal input:\n");
   2295  1.36    itojun 	    filestuff(tin);
   2296   1.1       cgd 	    printf("Terminal output:\n");
   2297   1.1       cgd 	    filestuff(tout);
   2298   1.1       cgd 	    printf("Network socket:\n");
   2299   1.1       cgd 	    filestuff(net);
   2300   1.1       cgd 	}
   2301   1.1       cgd     }
   2302   1.1       cgd     if (In3270 && transcom) {
   2303   1.1       cgd 	printf("Transparent mode command is '%s'.\n", transcom);
   2304   1.1       cgd     }
   2305   1.1       cgd #   endif /* defined(unix) */
   2306   1.1       cgd     (void) fflush(stdout);
   2307   1.1       cgd     if (In3270) {
   2308   1.1       cgd 	return 0;
   2309   1.1       cgd     }
   2310  1.15  christos #   endif /* defined(TN3270) */
   2311   1.1       cgd     return 1;
   2312   1.1       cgd }
   2313  1.15  christos 
   2314   1.1       cgd #ifdef	SIGINFO
   2315   1.1       cgd /*
   2316   1.1       cgd  * Function that gets called when SIGINFO is received.
   2317  1.26    itojun  */
   2318  1.26    itojun int
   2319  1.26    itojun ayt_status()
   2320  1.26    itojun {
   2321  1.38    itojun     return call(status, "status", "notmuch", 0);
   2322  1.38    itojun }
   2323  1.38    itojun #endif
   2324  1.38    itojun 
   2325  1.38    itojun static const char *
   2326  1.38    itojun sockaddr_ntop(sa)
   2327  1.26    itojun     struct sockaddr *sa;
   2328  1.38    itojun {
   2329  1.38    itojun     static char addrbuf[NI_MAXHOST];
   2330  1.38    itojun #ifdef NI_WITHSCOPEID
   2331  1.38    itojun     const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
   2332  1.26    itojun #else
   2333  1.26    itojun     const int niflags = NI_NUMERICHOST;
   2334  1.26    itojun #endif
   2335  1.37    itojun 
   2336  1.37    itojun     if (getnameinfo(sa, sa->sa_len, addrbuf, sizeof(addrbuf),
   2337  1.37    itojun 	    NULL, 0, niflags) == 0)
   2338  1.37    itojun 	return addrbuf;
   2339  1.37    itojun     else
   2340  1.37    itojun 	return NULL;
   2341  1.37    itojun }
   2342  1.37    itojun 
   2343  1.37    itojun #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
   2344  1.37    itojun static int setpolicy __P((int, struct addrinfo *, char *));
   2345  1.37    itojun 
   2346  1.37    itojun static int
   2347  1.37    itojun setpolicy(net, res, policy)
   2348  1.37    itojun 	int net;
   2349  1.37    itojun 	struct addrinfo *res;
   2350  1.37    itojun 	char *policy;
   2351  1.37    itojun {
   2352  1.37    itojun 	char *buf;
   2353  1.37    itojun 	int level;
   2354  1.37    itojun 	int optname;
   2355  1.37    itojun 
   2356  1.37    itojun 	if (policy == NULL)
   2357  1.37    itojun 		return 0;
   2358  1.37    itojun 
   2359  1.37    itojun 	buf = ipsec_set_policy(policy, strlen(policy));
   2360  1.37    itojun 	if (buf == NULL) {
   2361  1.37    itojun 		printf("%s\n", ipsec_strerror());
   2362  1.37    itojun 		return -1;
   2363  1.37    itojun 	}
   2364  1.37    itojun 	level = res->ai_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
   2365  1.37    itojun 	optname = res->ai_family == AF_INET ? IP_IPSEC_POLICY : IPV6_IPSEC_POLICY;
   2366  1.37    itojun 	if (setsockopt(net, level, optname, buf, ipsec_get_policylen(buf)) < 0){
   2367  1.37    itojun 		perror("setsockopt");
   2368   1.1       cgd 		return -1;
   2369   1.1       cgd 	}
   2370   1.1       cgd 
   2371   1.1       cgd 	free(buf);
   2372   1.1       cgd 	return 0;
   2373  1.29    itojun }
   2374  1.29    itojun #endif
   2375  1.26    itojun 
   2376   1.1       cgd     int
   2377  1.15  christos tn(argc, argv)
   2378  1.15  christos     int argc;
   2379  1.26    itojun     char *argv[];
   2380   1.1       cgd {
   2381  1.24   mycroft     struct addrinfo hints, *res, *res0;
   2382  1.24   mycroft     char *cause = "telnet: unknown";
   2383  1.15  christos     int error;
   2384  1.15  christos #if	defined(IP_OPTIONS) && defined(IPPROTO_IP)
   2385  1.15  christos     char *srp = 0;
   2386   1.1       cgd     unsigned long srlen;
   2387   1.1       cgd     int proto, opt;
   2388   1.1       cgd #endif
   2389   1.1       cgd     char *cmd, *hostp = 0, *portp = 0;
   2390   1.1       cgd     const char *user = 0;
   2391   1.1       cgd #ifdef __GNUC__	/* Avoid vfork clobbering */
   2392   1.1       cgd     (void) &user;
   2393   1.1       cgd #endif
   2394   1.1       cgd 
   2395   1.1       cgd     if (connected) {
   2396   1.1       cgd 	printf("?Already connected to %s\n", hostname);
   2397   1.1       cgd 	return 0;
   2398   1.1       cgd     }
   2399   1.1       cgd     if (argc < 2) {
   2400   1.1       cgd 	(void) strcpy(line, "open ");
   2401   1.1       cgd 	printf("(to) ");
   2402  1.10       jtk 	(void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
   2403   1.1       cgd 	makeargv();
   2404   1.1       cgd 	argc = margc;
   2405   1.1       cgd 	argv = margv;
   2406   1.1       cgd     }
   2407   1.1       cgd     cmd = *argv;
   2408   1.1       cgd     --argc; ++argv;
   2409   1.1       cgd     while (argc) {
   2410   1.1       cgd 	if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
   2411   1.1       cgd 	    goto usage;
   2412   1.1       cgd 	if (strcmp(*argv, "-l") == 0) {
   2413   1.1       cgd 	    --argc; ++argv;
   2414   1.1       cgd 	    if (argc == 0)
   2415   1.1       cgd 		goto usage;
   2416   1.1       cgd 	    user = *argv++;
   2417   1.1       cgd 	    --argc;
   2418   1.1       cgd 	    continue;
   2419   1.1       cgd 	}
   2420   1.1       cgd 	if (strcmp(*argv, "-a") == 0) {
   2421   1.1       cgd 	    --argc; ++argv;
   2422   1.1       cgd 	    autologin = 1;
   2423   1.1       cgd 	    continue;
   2424   1.1       cgd 	}
   2425   1.1       cgd 	if (hostp == 0) {
   2426   1.1       cgd 	    hostp = *argv++;
   2427   1.1       cgd 	    --argc;
   2428   1.1       cgd 	    continue;
   2429   1.1       cgd 	}
   2430   1.1       cgd 	if (portp == 0) {
   2431   1.1       cgd 	    portp = *argv++;
   2432   1.1       cgd 	    --argc;
   2433   1.1       cgd 	    continue;
   2434  1.26    itojun 	}
   2435   1.1       cgd     usage:
   2436  1.26    itojun 	printf("usage: %s [-l user] [-a] host-name [port]\n", cmd);
   2437  1.26    itojun 	return 0;
   2438  1.26    itojun     }
   2439  1.26    itojun     if (hostp == 0)
   2440  1.26    itojun 	goto usage;
   2441  1.26    itojun 
   2442  1.26    itojun     (void) strcpy(_hostname, hostp);
   2443  1.26    itojun     if (hostp[0] == '@' || hostp[0] == '!') {
   2444   1.1       cgd 	char *p;
   2445   1.1       cgd 	hostname = NULL;
   2446  1.26    itojun 	for (p = hostp + 1; *p; p++) {
   2447  1.26    itojun 	    if (*p == ',' || *p == '@')
   2448  1.26    itojun 		hostname = p;
   2449  1.26    itojun 	}
   2450  1.26    itojun 	if (hostname == NULL) {
   2451  1.36    itojun 	    fprintf(stderr, "%s: bad source route specification\n", hostp);
   2452  1.36    itojun 	    return 0;
   2453  1.30       jtk 	}
   2454  1.36    itojun 	*hostname++ = '\0';
   2455  1.36    itojun     } else
   2456  1.36    itojun 	hostname = hostp;
   2457  1.26    itojun 
   2458  1.26    itojun     if (!portp) {
   2459  1.26    itojun 	telnetport = 1;
   2460  1.26    itojun 	portp = "telnet";
   2461  1.26    itojun     } else if (portp[0] == '-') {
   2462  1.26    itojun 	/* use telnet negotiation if port number/name preceded by minus sign */
   2463  1.36    itojun 	telnetport = 1;
   2464  1.29    itojun 	portp++;
   2465  1.26    itojun     }
   2466  1.36    itojun 
   2467  1.36    itojun     memset(&hints, 0, sizeof(hints));
   2468  1.36    itojun     hints.ai_family = AF_UNSPEC;
   2469  1.36    itojun     hints.ai_socktype = SOCK_STREAM;
   2470  1.36    itojun     hints.ai_protocol = 0;
   2471  1.36    itojun     hints.ai_flags = AI_NUMERICHOST;	/* avoid forward lookup */
   2472  1.36    itojun     error = getaddrinfo(hostname, portp, &hints, &res0);
   2473  1.36    itojun     if (!error) {
   2474  1.36    itojun 	/* numeric */
   2475   1.1       cgd 	if (doaddrlookup &&
   2476  1.36    itojun 	    getnameinfo(res0->ai_addr, res0->ai_addrlen,
   2477  1.26    itojun 		_hostname, sizeof(_hostname), NULL, 0, NI_NAMEREQD) == 0)
   2478  1.26    itojun 	    ; /* okay */
   2479  1.26    itojun 	else {
   2480  1.26    itojun 	    strncpy(_hostname, hostname, sizeof(_hostname) - 1);
   2481  1.36    itojun 	    _hostname[sizeof(_hostname) - 1] = '\0';
   2482  1.29    itojun 	}
   2483  1.40    itojun     } else {
   2484  1.40    itojun 	/* FQDN - try again with forward DNS lookup */
   2485  1.40    itojun 	memset(&hints, 0, sizeof(hints));
   2486  1.40    itojun 	hints.ai_family = AF_UNSPEC;
   2487  1.36    itojun 	hints.ai_socktype = SOCK_STREAM;
   2488  1.36    itojun 	hints.ai_protocol = 0;
   2489  1.36    itojun 	hints.ai_flags = AI_CANONNAME;
   2490  1.36    itojun 	error = getaddrinfo(hostname, portp, &hints, &res0);
   2491  1.36    itojun 	if (error == EAI_SERVICE) {
   2492  1.36    itojun 	    fprintf(stderr, "tcp/%s: unknown service\n", portp);
   2493  1.36    itojun 	    return 0;
   2494  1.36    itojun 	} else if (error) {
   2495  1.36    itojun 	    fprintf(stderr, "%s: %s\n", hostname, gai_strerror(error));
   2496  1.26    itojun 	    return 0;
   2497  1.26    itojun 	}
   2498  1.26    itojun 	if (res0->ai_canonname) {
   2499  1.29    itojun 	    (void) strncpy(_hostname, res0->ai_canonname,
   2500  1.29    itojun 	        sizeof(_hostname) - 1);
   2501  1.26    itojun 	} else
   2502  1.26    itojun 	    (void) strncpy(_hostname, hostname, sizeof(_hostname) - 1);
   2503   1.1       cgd 	_hostname[sizeof(_hostname) - 1] = '\0';
   2504  1.29    itojun     }
   2505  1.29    itojun     hostname = _hostname;
   2506   1.1       cgd 
   2507   1.1       cgd     net = -1;
   2508   1.1       cgd     for (res = res0; res; res = res->ai_next) {
   2509  1.29    itojun 	printf("Trying %s...\n", sockaddr_ntop(res->ai_addr));
   2510   1.1       cgd 	net = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
   2511  1.26    itojun 	if (net < 0) {
   2512  1.33    itojun 	    cause = "telnet: socket";
   2513  1.33    itojun 	    continue;
   2514  1.33    itojun 	}
   2515  1.26    itojun 
   2516  1.33    itojun 	if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
   2517  1.26    itojun 	    perror("setsockopt (SO_DEBUG)");
   2518  1.26    itojun 	}
   2519  1.26    itojun 	if (hostp[0] == '@' || hostp[0] == '!') {
   2520  1.37    itojun 	    if ((srlen = sourceroute(res, hostp, &srp, &proto, &opt)) < 0) {
   2521  1.26    itojun 		(void) NetClose(net);
   2522  1.37    itojun 		net = -1;
   2523  1.37    itojun 		continue;
   2524  1.37    itojun 	    }
   2525  1.37    itojun 	    if (srp && setsockopt(net, proto, opt, srp, srlen) < 0)
   2526  1.37    itojun 		perror("setsockopt (source route)");
   2527  1.37    itojun 	}
   2528  1.37    itojun 
   2529  1.37    itojun #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
   2530  1.37    itojun 	if (setpolicy(net, res, ipsec_policy_in) < 0) {
   2531  1.26    itojun 	    (void) NetClose(net);
   2532  1.26    itojun 	    net = -1;
   2533   1.1       cgd 	    continue;
   2534  1.26    itojun 	}
   2535  1.26    itojun 	if (setpolicy(net, res, ipsec_policy_out) < 0) {
   2536   1.1       cgd 	    (void) NetClose(net);
   2537   1.1       cgd 	    net = -1;
   2538   1.1       cgd 	    continue;
   2539  1.26    itojun 	}
   2540   1.1       cgd #endif
   2541   1.1       cgd 
   2542   1.1       cgd 	if (connect(net, res->ai_addr, res->ai_addrlen) < 0) {
   2543  1.29    itojun 	    if (res->ai_next) {
   2544  1.29    itojun 		int oerrno = errno;
   2545  1.29    itojun 
   2546  1.29    itojun 		fprintf(stderr, "telnet: connect to address %s: ",
   2547   1.1       cgd 						sockaddr_ntop(res->ai_addr));
   2548  1.29    itojun 		errno = oerrno;
   2549   1.1       cgd 		perror((char *)0);
   2550  1.43   thorpej 	    }
   2551   1.1       cgd 	    cause = "telnet: Unable to connect to remote host";
   2552  1.43   thorpej 	    (void) NetClose(net);
   2553  1.29    itojun 	    net = -1;
   2554  1.29    itojun 	    continue;
   2555  1.29    itojun 	}
   2556  1.29    itojun 
   2557  1.29    itojun 	connected++;
   2558  1.29    itojun #if	defined(AUTHENTICATION) || defined(ENCRYPTION)
   2559  1.29    itojun 	auth_encrypt_connect(connected);
   2560  1.29    itojun #endif	/* defined(AUTHENTICATION) || defined(ENCRYPTION) */
   2561   1.1       cgd 	break;
   2562   1.1       cgd     }
   2563   1.1       cgd     freeaddrinfo(res0);
   2564   1.1       cgd     if (net < 0 || connected == 0) {
   2565   1.1       cgd 	perror(cause);
   2566   1.1       cgd 	return 0;
   2567  1.15  christos     }
   2568  1.15  christos 
   2569   1.1       cgd     cmdrc(hostp, hostname);
   2570   1.1       cgd     if (autologin && user == NULL) {
   2571   1.1       cgd 	struct passwd *pw;
   2572   1.1       cgd 
   2573   1.1       cgd 	user = getenv("USER");
   2574   1.1       cgd 	if (user == NULL ||
   2575   1.1       cgd 	    ((pw = getpwnam(user)) && pw->pw_uid != getuid())) {
   2576  1.15  christos 		if ((pw = getpwuid(getuid())) != NULL)
   2577   1.1       cgd 			user = pw->pw_name;
   2578   1.1       cgd 		else
   2579   1.1       cgd 			user = NULL;
   2580   1.1       cgd 	}
   2581   1.1       cgd     }
   2582   1.1       cgd     if (user) {
   2583   1.1       cgd 	env_define((unsigned char *)"USER", (unsigned char *)user);
   2584   1.1       cgd 	env_export((unsigned char *)"USER", NULL);
   2585   1.1       cgd     }
   2586  1.17   thorpej     (void) call(status, "status", "notmuch", 0);
   2587   1.1       cgd     if (setjmp(peerdied) == 0)
   2588   1.1       cgd 	telnet(user);
   2589   1.1       cgd     (void) NetClose(net);
   2590   1.1       cgd     ExitString("Connection closed by foreign host.\n",1);
   2591   1.1       cgd     /*NOTREACHED*/
   2592   1.1       cgd }
   2593   1.1       cgd 
   2594   1.1       cgd #define HELPINDENT ((int)sizeof ("connect"))
   2595   1.1       cgd 
   2596   1.1       cgd static char
   2597   1.1       cgd 	openhelp[] =	"connect to a site",
   2598   1.1       cgd 	closehelp[] =	"close current connection",
   2599   1.1       cgd 	logouthelp[] =	"forcibly logout remote user and close the connection",
   2600   1.1       cgd 	quithelp[] =	"exit telnet",
   2601   1.1       cgd 	statushelp[] =	"print status information",
   2602   1.1       cgd 	helphelp[] =	"print help information",
   2603   1.1       cgd 	sendhelp[] =	"transmit special characters ('send ?' for more)",
   2604   1.5       cgd 	sethelp[] = 	"set operating parameters ('set ?' for more)",
   2605   1.1       cgd 	unsethelp[] = 	"unset operating parameters ('unset ?' for more)",
   2606   1.1       cgd 	togglestring[] ="toggle operating parameters ('toggle ?' for more)",
   2607  1.43   thorpej 	slchelp[] =	"change state of special charaters ('slc ?' for more)",
   2608  1.43   thorpej 	displayhelp[] =	"display operating parameters",
   2609  1.43   thorpej #if	defined(TN3270) && defined(unix)
   2610   1.1       cgd 	transcomhelp[] = "specify Unix command for transparent mode pipe",
   2611   1.1       cgd #endif	/* defined(TN3270) && defined(unix) */
   2612   1.1       cgd #if	defined(AUTHENTICATION)
   2613   1.1       cgd 	authhelp[] =	"turn on (off) authentication ('auth ?' for more)",
   2614   1.1       cgd #endif
   2615   1.1       cgd #ifdef	ENCRYPTION
   2616   1.1       cgd 	encrypthelp[] = "turn on (off) encryption ('encrypt ?' for more)",
   2617   1.1       cgd #endif	/* ENCRYPTION */
   2618   1.1       cgd #if	defined(unix)
   2619   1.1       cgd 	zhelp[] =	"suspend telnet",
   2620   1.1       cgd #endif	/* defined(unix) */
   2621   1.1       cgd 	shellhelp[] =	"invoke a subshell",
   2622   1.1       cgd 	envhelp[] =	"change environment variables ('environ ?' for more)",
   2623   1.1       cgd 	modestring[] = "try to enter line or character mode ('mode ?' for more)";
   2624   1.1       cgd 
   2625   1.1       cgd static Command cmdtab[] = {
   2626   1.1       cgd 	{ "close",	closehelp,	bye,		1 },
   2627   1.1       cgd 	{ "logout",	logouthelp,	logout,		1 },
   2628   1.1       cgd 	{ "display",	displayhelp,	display,	0 },
   2629   1.1       cgd 	{ "mode",	modestring,	modecmd,	0 },
   2630   1.1       cgd 	{ "open",	openhelp,	tn,		0 },
   2631   1.1       cgd 	{ "quit",	quithelp,	quit,		0 },
   2632   1.1       cgd 	{ "send",	sendhelp,	sendcmd,	0 },
   2633   1.5       cgd 	{ "set",	sethelp,	setcmd,		0 },
   2634   1.1       cgd 	{ "unset",	unsethelp,	unsetcmd,	0 },
   2635  1.43   thorpej 	{ "status",	statushelp,	status,		0 },
   2636  1.43   thorpej 	{ "toggle",	togglestring,	toggle,		0 },
   2637  1.43   thorpej 	{ "slc",	slchelp,	slccmd,		0 },
   2638   1.1       cgd #if	defined(TN3270) && defined(unix)
   2639   1.1       cgd 	{ "transcom",	transcomhelp,	settranscom,	0 },
   2640   1.1       cgd #endif	/* defined(TN3270) && defined(unix) */
   2641   1.1       cgd #if	defined(AUTHENTICATION)
   2642   1.1       cgd 	{ "auth",	authhelp,	auth_cmd,	0 },
   2643   1.1       cgd #endif
   2644   1.1       cgd #ifdef	ENCRYPTION
   2645   1.1       cgd 	{ "encrypt",	encrypthelp,	encrypt_cmd,	0 },
   2646   1.1       cgd #endif
   2647   1.1       cgd #if	defined(unix)
   2648   1.1       cgd 	{ "z",		zhelp,		suspend,	0 },
   2649  1.15  christos #endif	/* defined(unix) */
   2650   1.1       cgd #if	defined(TN3270)
   2651   1.1       cgd 	{ "!",		shellhelp,	shell,		1 },
   2652   1.1       cgd #else
   2653   1.1       cgd 	{ "!",		shellhelp,	shell,		0 },
   2654   1.1       cgd #endif
   2655   1.1       cgd 	{ "environ",	envhelp,	env_cmd,	0 },
   2656   1.1       cgd 	{ "?",		helphelp,	help,		0 },
   2657   1.1       cgd 	{ NULL,		NULL,		NULL,		0 }
   2658   1.1       cgd };
   2659  1.15  christos 
   2660   1.1       cgd static char	crmodhelp[] =	"deprecated command -- use 'toggle crmod' instead";
   2661   1.1       cgd static char	escapehelp[] =	"deprecated command -- use 'set escape' instead";
   2662   1.1       cgd 
   2663   1.1       cgd static Command cmdtab2[] = {
   2664   1.1       cgd 	{ "help",	0,		help,		0 },
   2665   1.1       cgd 	{ "escape",	escapehelp,	setescape,	0 },
   2666   1.1       cgd 	{ "crmod",	crmodhelp,	togcrmod,	0 },
   2667   1.1       cgd 	{ NULL,		NULL,		NULL,		0 }
   2668  1.15  christos };
   2669  1.15  christos 
   2670  1.15  christos 
   2671  1.15  christos /*
   2672   1.1       cgd  * Call routine with argc, argv set from args (terminated by 0).
   2673   1.1       cgd  */
   2674  1.15  christos 
   2675   1.1       cgd     /*VARARGS1*/
   2676   1.1       cgd     static int
   2677   1.1       cgd #ifdef __STDC__
   2678   1.1       cgd call(intrtn_t routine, ...)
   2679  1.15  christos #else
   2680  1.15  christos call(va_alist)
   2681   1.1       cgd     va_dcl
   2682   1.1       cgd #endif
   2683   1.1       cgd {
   2684  1.15  christos     va_list ap;
   2685  1.15  christos     char *args[100];
   2686  1.15  christos     int argno = 0;
   2687  1.15  christos #ifndef __STDC__
   2688   1.1       cgd     intrtn_t routine;
   2689   1.1       cgd 
   2690   1.1       cgd     va_start(ap);
   2691   1.1       cgd     routine = (va_arg(ap, intrtn_t));
   2692   1.1       cgd #else
   2693   1.1       cgd     va_start(ap, routine);
   2694   1.1       cgd #endif
   2695   1.1       cgd 
   2696   1.1       cgd     while ((args[argno++] = va_arg(ap, char *)) != 0) {
   2697   1.1       cgd 	;
   2698   1.1       cgd     }
   2699   1.1       cgd     va_end(ap);
   2700   1.1       cgd     return (*routine)(argno-1, args);
   2701   1.1       cgd }
   2702  1.15  christos 
   2703   1.1       cgd 
   2704   1.1       cgd     static Command *
   2705   1.1       cgd getcmd(name)
   2706   1.1       cgd     char *name;
   2707   1.1       cgd {
   2708   1.1       cgd     Command *cm;
   2709   1.1       cgd 
   2710   1.1       cgd     if ((cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command))) != NULL)
   2711   1.1       cgd 	return cm;
   2712   1.1       cgd     return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
   2713   1.1       cgd }
   2714   1.1       cgd 
   2715   1.1       cgd     void
   2716   1.1       cgd command(top, tbuf, cnt)
   2717   1.1       cgd     int top;
   2718   1.1       cgd     char *tbuf;
   2719   1.1       cgd     int cnt;
   2720   1.1       cgd {
   2721   1.1       cgd     register Command *c;
   2722   1.1       cgd 
   2723   1.1       cgd     setcommandmode();
   2724   1.1       cgd     if (!top) {
   2725   1.1       cgd 	putchar('\n');
   2726   1.1       cgd #if	defined(unix)
   2727   1.1       cgd     } else {
   2728   1.1       cgd 	(void) signal(SIGINT, SIG_DFL);
   2729   1.1       cgd 	(void) signal(SIGQUIT, SIG_DFL);
   2730   1.1       cgd #endif	/* defined(unix) */
   2731   1.1       cgd     }
   2732   1.1       cgd     for (;;) {
   2733   1.1       cgd 	if (rlogin == _POSIX_VDISABLE)
   2734   1.1       cgd 		printf("%s> ", prompt);
   2735   1.1       cgd 	if (tbuf) {
   2736   1.1       cgd 	    register char *cp;
   2737  1.10       jtk 	    cp = line;
   2738   1.1       cgd 	    while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
   2739   1.1       cgd 		cnt--;
   2740   1.1       cgd 	    tbuf = 0;
   2741   1.1       cgd 	    if (cp == line || *--cp != '\n' || cp == line)
   2742   1.1       cgd 		goto getline;
   2743   1.1       cgd 	    *cp = '\0';
   2744  1.15  christos 	    if (rlogin == _POSIX_VDISABLE)
   2745   1.1       cgd 		printf("%s\n", line);
   2746   1.1       cgd 	} else {
   2747   1.1       cgd 	getline:
   2748   1.1       cgd 	    if (rlogin != _POSIX_VDISABLE)
   2749   1.1       cgd 		printf("%s> ", prompt);
   2750   1.1       cgd 	    if (fgets(line, sizeof(line), stdin) == NULL) {
   2751   1.1       cgd 		if (feof(stdin) || ferror(stdin)) {
   2752   1.1       cgd 		    (void) quit(0, NULL);
   2753   1.1       cgd 		    /*NOTREACHED*/
   2754   1.1       cgd 		}
   2755   1.1       cgd 		break;
   2756   1.1       cgd 	    }
   2757   1.1       cgd 	}
   2758   1.1       cgd 	if (line[0] == 0)
   2759   1.1       cgd 	    break;
   2760   1.1       cgd 	makeargv();
   2761   1.1       cgd 	if (margv[0] == 0) {
   2762   1.1       cgd 	    break;
   2763   1.1       cgd 	}
   2764   1.1       cgd 	c = getcmd(margv[0]);
   2765   1.1       cgd 	if (Ambiguous(c)) {
   2766   1.1       cgd 	    printf("?Ambiguous command\n");
   2767   1.1       cgd 	    continue;
   2768   1.1       cgd 	}
   2769   1.1       cgd 	if (c == 0) {
   2770   1.1       cgd 	    printf("?Invalid command\n");
   2771   1.1       cgd 	    continue;
   2772   1.1       cgd 	}
   2773   1.1       cgd 	if (c->needconnect && !connected) {
   2774   1.1       cgd 	    printf("?Need to be connected first.\n");
   2775   1.1       cgd 	    continue;
   2776   1.1       cgd 	}
   2777   1.1       cgd 	if ((*c->handler)(margc, margv)) {
   2778   1.1       cgd 	    break;
   2779   1.1       cgd 	}
   2780   1.1       cgd     }
   2781   1.1       cgd     if (!top) {
   2782   1.1       cgd 	if (!connected) {
   2783   1.1       cgd 	    longjmp(toplevel, 1);
   2784   1.1       cgd 	    /*NOTREACHED*/
   2785   1.1       cgd 	}
   2786   1.1       cgd #if	defined(TN3270)
   2787   1.1       cgd 	if (shell_active == 0) {
   2788   1.1       cgd 	    setconnmode(0);
   2789   1.1       cgd 	}
   2790   1.1       cgd #else	/* defined(TN3270) */
   2791  1.15  christos 	setconnmode(0);
   2792   1.1       cgd #endif	/* defined(TN3270) */
   2793   1.1       cgd     }
   2794   1.1       cgd }
   2795   1.1       cgd 
   2796   1.1       cgd /*
   2798   1.1       cgd  * Help command.
   2799   1.1       cgd  */
   2800   1.1       cgd 	static int
   2801   1.1       cgd help(argc, argv)
   2802   1.1       cgd 	int argc;
   2803   1.1       cgd 	char *argv[];
   2804   1.1       cgd {
   2805   1.1       cgd 	register Command *c;
   2806   1.1       cgd 
   2807   1.1       cgd 	if (argc == 1) {
   2808   1.1       cgd 		printf("Commands may be abbreviated.  Commands are:\n\n");
   2809   1.1       cgd 		for (c = cmdtab; c->name; c++)
   2810   1.1       cgd 			if (c->help) {
   2811   1.1       cgd 				printf("%-*s\t%s\n", HELPINDENT, c->name,
   2812   1.1       cgd 								    c->help);
   2813   1.1       cgd 			}
   2814   1.1       cgd 		return 0;
   2815   1.1       cgd 	}
   2816   1.1       cgd 	while (--argc > 0) {
   2817   1.1       cgd 		register char *arg;
   2818   1.1       cgd 		arg = *++argv;
   2819   1.1       cgd 		c = getcmd(arg);
   2820   1.1       cgd 		if (Ambiguous(c))
   2821   1.1       cgd 			printf("?Ambiguous help command %s\n", arg);
   2822   1.1       cgd 		else if (c == (Command *)0)
   2823   1.1       cgd 			printf("?Invalid help command %s\n", arg);
   2824  1.15  christos 		else
   2825   1.1       cgd 			printf("%s\n", c->help);
   2826  1.24   mycroft 	}
   2827   1.1       cgd 	return 0;
   2828   1.1       cgd }
   2829   1.1       cgd 
   2830   1.1       cgd static char *rcname = 0;
   2831   1.1       cgd static char rcbuf[128];
   2832   1.1       cgd 
   2833  1.44       mjl void
   2834   1.1       cgd cmdrc(m1, m2)
   2835   1.1       cgd 	const char *m1, *m2;
   2836   1.1       cgd {
   2837   1.1       cgd     register Command *c;
   2838  1.44       mjl     FILE *rcfile;
   2839   1.1       cgd     int gotmachine = 0;
   2840   1.1       cgd     int l1 = strlen(m1);
   2841   1.1       cgd     int l2 = strlen(m2);
   2842   1.1       cgd     char m1save[MAXHOSTNAMELEN + 1];
   2843   1.1       cgd 
   2844   1.1       cgd     if (skiprc)
   2845   1.1       cgd 	return;
   2846   1.1       cgd 
   2847   1.1       cgd     strlcpy(m1save, m1, sizeof(m1save));
   2848   1.1       cgd     m1 = m1save;
   2849   1.1       cgd 
   2850   1.1       cgd     if (rcname == 0) {
   2851   1.1       cgd 	rcname = getenv("HOME");
   2852   1.1       cgd 	if (rcname)
   2853   1.1       cgd 	    strcpy(rcbuf, rcname);
   2854   1.1       cgd 	else
   2855   1.1       cgd 	    rcbuf[0] = '\0';
   2856   1.1       cgd 	strcat(rcbuf, "/.telnetrc");
   2857   1.1       cgd 	rcname = rcbuf;
   2858   1.1       cgd     }
   2859   1.1       cgd 
   2860   1.1       cgd     if ((rcfile = fopen(rcname, "r")) == 0) {
   2861   1.1       cgd 	return;
   2862   1.1       cgd     }
   2863  1.25  christos 
   2864   1.1       cgd     for (;;) {
   2865   1.1       cgd 	if (fgets(line, sizeof(line), rcfile) == NULL)
   2866   1.1       cgd 	    break;
   2867  1.25  christos 	if (line[0] == 0)
   2868   1.1       cgd 	    break;
   2869   1.1       cgd 	if (line[0] == '#')
   2870   1.1       cgd 	    continue;
   2871   1.1       cgd 	if (gotmachine) {
   2872   1.1       cgd 	    if (!isspace((unsigned char)line[0]))
   2873   1.1       cgd 		gotmachine = 0;
   2874   1.1       cgd 	}
   2875   1.1       cgd 	if (gotmachine == 0) {
   2876   1.1       cgd 	    if (isspace((unsigned char)line[0]))
   2877   1.1       cgd 		continue;
   2878   1.1       cgd 	    if (strncasecmp(line, m1, l1) == 0)
   2879   1.1       cgd 		strncpy(line, &line[l1], sizeof(line) - l1);
   2880   1.1       cgd 	    else if (strncasecmp(line, m2, l2) == 0)
   2881   1.1       cgd 		strncpy(line, &line[l2], sizeof(line) - l2);
   2882   1.1       cgd 	    else if (strncasecmp(line, "DEFAULT", 7) == 0)
   2883   1.1       cgd 		strncpy(line, &line[7], sizeof(line) - 7);
   2884   1.1       cgd 	    else
   2885   1.1       cgd 		continue;
   2886   1.1       cgd 	    if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
   2887   1.1       cgd 		continue;
   2888   1.1       cgd 	    gotmachine = 1;
   2889   1.1       cgd 	}
   2890   1.1       cgd 	makeargv();
   2891   1.1       cgd 	if (margv[0] == 0)
   2892   1.1       cgd 	    continue;
   2893   1.1       cgd 	c = getcmd(margv[0]);
   2894   1.1       cgd 	if (Ambiguous(c)) {
   2895   1.1       cgd 	    printf("?Ambiguous command: %s\n", margv[0]);
   2896   1.1       cgd 	    continue;
   2897   1.1       cgd 	}
   2898   1.1       cgd 	if (c == 0) {
   2899   1.1       cgd 	    printf("?Invalid command: %s\n", margv[0]);
   2900   1.1       cgd 	    continue;
   2901   1.1       cgd 	}
   2902   1.1       cgd 	/*
   2903   1.1       cgd 	 * This should never happen...
   2904   1.1       cgd 	 */
   2905   1.1       cgd 	if (c->needconnect && !connected) {
   2906   1.1       cgd 	    printf("?Need to be connected first for %s.\n", margv[0]);
   2907  1.26    itojun 	    continue;
   2908  1.39    itojun 	}
   2909  1.39    itojun 	(*c->handler)(margc, margv);
   2910  1.39    itojun     }
   2911  1.39    itojun     fclose(rcfile);
   2912   1.1       cgd }
   2913   1.1       cgd 
   2914   1.1       cgd /*
   2915   1.1       cgd  * Source route is handed in as
   2916   1.1       cgd  *	[!]@hop1 (at) hop2...@dst
   2917   1.1       cgd  *
   2918   1.1       cgd  * If the leading ! is present, it is a strict source route, otherwise it is
   2919  1.39    itojun  * assmed to be a loose source route.  Note that leading ! is effective
   2920  1.39    itojun  * only for IPv4 case.
   2921  1.39    itojun  *
   2922   1.1       cgd  * We fill in the source route option as
   2923  1.39    itojun  *	hop1,hop2,hop3...dest
   2924  1.39    itojun  * and return a pointer to hop1, which will
   2925   1.1       cgd  * be the address to connect() to.
   2926  1.39    itojun  *
   2927  1.39    itojun  * Arguments:
   2928   1.1       cgd  *	ai:	The address (by struct addrinfo) for the final destination.
   2929   1.1       cgd  *
   2930   1.1       cgd  *	arg:	Pointer to route list to decipher
   2931   1.1       cgd  *
   2932   1.1       cgd  *	cpp: 	Pointer to a pointer, so that sourceroute() can return
   2933  1.39    itojun  *		the address of result buffer (statically alloc'ed).
   2934   1.1       cgd  *
   2935  1.39    itojun  *	protop/optp:
   2936  1.39    itojun  *		Pointer to an integer.  The pointed variable
   2937  1.39    itojun  *	lenp:	pointer to an integer that contains the
   2938  1.39    itojun  *		length of *cpp if *cpp != NULL.
   2939  1.39    itojun  *
   2940  1.39    itojun  * Return values:
   2941  1.39    itojun  *
   2942  1.39    itojun  *	Returns the length of the option pointed to by *cpp.  If the
   2943   1.1       cgd  *	return value is -1, there was a syntax error in the
   2944  1.39    itojun  *	option, either arg contained unknown characters or too many hosts,
   2945  1.39    itojun  *	or hostname cannot be resolved.
   2946  1.10       jtk  *
   2947  1.39    itojun  *	The caller needs to pass return value (len), *cpp, *protop and *optp
   2948  1.10       jtk  *	to setsockopt(2).
   2949   1.1       cgd  *
   2950  1.26    itojun  *	*cpp:	Points to the result buffer.  The region is statically
   2951  1.26    itojun  *		allocated by the function.
   2952  1.26    itojun  *
   2953  1.24   mycroft  *	*protop:
   2954  1.26    itojun  *		protocol # to be passed to setsockopt(2).
   2955  1.26    itojun  *
   2956  1.26    itojun  *	*optp:	option # to be passed to setsockopt(2).
   2957   1.1       cgd  *
   2958   1.5       cgd  */
   2959   1.5       cgd int
   2960   1.5       cgd sourceroute(ai, arg, cpp, protop, optp)
   2961   1.1       cgd 	struct addrinfo *ai;
   2962  1.26    itojun 	char *arg;
   2963  1.26    itojun 	char **cpp;
   2964  1.26    itojun 	int *protop;
   2965   1.1       cgd 	int *optp;
   2966  1.26    itojun {
   2967  1.26    itojun #ifdef	sysV88
   2968  1.26    itojun 	static IOPTN ipopt;
   2969  1.26    itojun #endif
   2970  1.26    itojun 	char *cp, *cp2, *lsrp, *lsrep;
   2971  1.26    itojun 	struct addrinfo hints, *res;
   2972   1.1       cgd 	int len, error;
   2973   1.1       cgd 	struct sockaddr_in *sin;
   2974  1.26    itojun 	register char c;
   2975   1.1       cgd 	static char lsr[44];
   2976  1.26    itojun #ifdef INET6
   2977  1.26    itojun 	struct cmsghdr *cmsg;
   2978   1.1       cgd 	struct sockaddr_in6 *sin6;
   2979   1.1       cgd 	static char rhbuf[1024];
   2980   1.1       cgd #endif
   2981  1.26    itojun 
   2982  1.41     blymn 	/*
   2983  1.41     blymn 	 * Verify the arguments.
   2984  1.41     blymn 	 */
   2985  1.41     blymn 	if (cpp == NULL)
   2986  1.42    itojun 		return -1;
   2987  1.41     blymn 
   2988  1.42    itojun 	cp = arg;
   2989  1.41     blymn 
   2990  1.26    itojun 	*cpp = NULL;
   2991  1.26    itojun 
   2992  1.26    itojun 	  /* init these just in case.... */
   2993  1.26    itojun 	lsrp = NULL;
   2994  1.26    itojun 	lsrep = NULL;
   2995  1.26    itojun #ifdef INET6
   2996  1.26    itojun 	cmsg = NULL;
   2997  1.26    itojun #endif
   2998  1.26    itojun 
   2999  1.26    itojun 	switch (ai->ai_family) {
   3000   1.5       cgd 	case AF_INET:
   3001  1.26    itojun 		lsrp = lsr;
   3002  1.26    itojun 		lsrep = lsrp + sizeof(lsr);
   3003  1.26    itojun 
   3004  1.26    itojun 		/*
   3005  1.26    itojun 		 * Next, decide whether we have a loose source
   3006   1.5       cgd 		 * route or a strict source route, and fill in
   3007  1.26    itojun 		 * the begining of the option.
   3008  1.26    itojun 		 */
   3009  1.26    itojun #ifndef	sysV88
   3010  1.26    itojun 		if (*cp == '!') {
   3011  1.26    itojun 			cp++;
   3012   1.5       cgd 			*lsrp++ = IPOPT_SSRR;
   3013  1.26    itojun 		} else
   3014  1.26    itojun 			*lsrp++ = IPOPT_LSRR;
   3015   1.5       cgd #else
   3016  1.26    itojun 		if (*cp == '!') {
   3017  1.26    itojun 			cp++;
   3018  1.26    itojun 			ipopt.io_type = IPOPT_SSRR;
   3019  1.26    itojun 		} else
   3020  1.26    itojun 			ipopt.io_type = IPOPT_LSRR;
   3021  1.26    itojun #endif
   3022  1.26    itojun 		if (*cp != '@')
   3023  1.26    itojun 			return -1;
   3024  1.26    itojun #ifndef	sysV88
   3025  1.26    itojun 		lsrp++;		/* skip over length, we'll fill it in later */
   3026  1.26    itojun 		*lsrp++ = 4;
   3027  1.26    itojun #endif
   3028  1.26    itojun 		cp++;
   3029  1.26    itojun 		*protop = IPPROTO_IP;
   3030  1.26    itojun 		*optp = IP_OPTIONS;
   3031  1.26    itojun 		break;
   3032   1.5       cgd #ifdef INET6
   3033  1.26    itojun 	case AF_INET6:
   3034  1.26    itojun 		cmsg = inet6_rthdr_init(rhbuf, IPV6_RTHDR_TYPE_0);
   3035  1.26    itojun 		if (*cp != '@')
   3036   1.1       cgd 			return -1;
   3037  1.26    itojun 		cp++;
   3038  1.26    itojun 		*protop = IPPROTO_IPV6;
   3039  1.26    itojun 		*optp = IPV6_PKTOPTIONS;
   3040   1.1       cgd 		break;
   3041   1.1       cgd #endif
   3042   1.1       cgd 	default:
   3043   1.1       cgd 		return -1;
   3044  1.15  christos 	}
   3045   1.1       cgd 
   3046   1.1       cgd 	memset(&hints, 0, sizeof(hints));
   3047   1.1       cgd 	hints.ai_family = ai->ai_family;
   3048   1.1       cgd 	hints.ai_socktype = SOCK_STREAM;
   3049   1.1       cgd 
   3050   1.1       cgd 	for (c = 0;;) {
   3051  1.26    itojun 		if (c == ':')
   3052  1.26    itojun 			cp2 = 0;
   3053  1.26    itojun 		else for (cp2 = cp; (c = *cp2) != '\0'; cp2++) {
   3054   1.1       cgd 			if (c == ',') {
   3055  1.26    itojun 				*cp2++ = '\0';
   3056  1.26    itojun 				if (*cp2 == '@')
   3057  1.26    itojun 					cp2++;
   3058   1.1       cgd 			} else if (c == '@') {
   3059   1.1       cgd 				*cp2++ = '\0';
   3060   1.1       cgd 			}
   3061   1.1       cgd #if 0	/*colon conflicts with IPv6 address*/
   3062   1.1       cgd 			else if (c == ':') {
   3063   1.1       cgd 				*cp2++ = '\0';
   3064  1.26    itojun 			}
   3065  1.26    itojun #endif
   3066  1.26    itojun 			else
   3067  1.26    itojun 				continue;
   3068  1.26    itojun 			break;
   3069  1.26    itojun 		}
   3070  1.26    itojun 		if (!c)
   3071  1.26    itojun 			cp2 = 0;
   3072  1.26    itojun 
   3073  1.26    itojun 		error = getaddrinfo(cp, NULL, &hints, &res);
   3074  1.26    itojun 		if (error) {
   3075  1.26    itojun 			fprintf(stderr, "%s: %s\n", cp, gai_strerror(error));
   3076  1.26    itojun 			return -1;
   3077  1.26    itojun 		}
   3078  1.26    itojun 		if (ai->ai_family != res->ai_family) {
   3079  1.26    itojun 			freeaddrinfo(res);
   3080  1.26    itojun 			return -1;
   3081  1.26    itojun 		}
   3082  1.26    itojun 		if (ai->ai_family == AF_INET) {
   3083  1.26    itojun 			/*
   3084  1.26    itojun 			 * Check to make sure there is space for address
   3085  1.26    itojun 			 */
   3086  1.26    itojun 			if (lsrp + 4 > lsrep) {
   3087  1.26    itojun 				freeaddrinfo(res);
   3088  1.26    itojun 				return -1;
   3089  1.26    itojun 			}
   3090  1.26    itojun 			sin = (struct sockaddr_in *)res->ai_addr;
   3091   1.1       cgd 			memcpy(lsrp, &sin->sin_addr, sizeof(struct in_addr));
   3092  1.26    itojun 			lsrp += sizeof(struct in_addr);
   3093  1.26    itojun 		}
   3094  1.26    itojun #ifdef INET6
   3095   1.1       cgd 		else if (ai->ai_family == AF_INET6) {
   3096  1.26    itojun 			sin6 = (struct sockaddr_in6 *)res->ai_addr;
   3097   1.1       cgd 			inet6_rthdr_add(cmsg, &sin6->sin6_addr,
   3098   1.1       cgd 				IPV6_RTHDR_LOOSE);
   3099   1.1       cgd 		}
   3100   1.1       cgd #endif
   3101  1.26    itojun 		else {
   3102  1.26    itojun 			freeaddrinfo(res);
   3103  1.26    itojun 			return -1;
   3104   1.1       cgd 		}
   3105  1.26    itojun 		freeaddrinfo(res);
   3106  1.26    itojun 		if (cp2)
   3107  1.26    itojun 			cp = cp2;
   3108  1.26    itojun 		else
   3109   1.5       cgd 			break;
   3110  1.26    itojun 	}
   3111  1.26    itojun 	if (ai->ai_family == AF_INET) {
   3112  1.26    itojun 		/* record the last hop */
   3113  1.26    itojun 		if (lsrp + 4 > lsrep)
   3114  1.26    itojun 			return -1;
   3115  1.26    itojun 		sin = (struct sockaddr_in *)ai->ai_addr;
   3116  1.26    itojun 		memcpy(lsrp, &sin->sin_addr, sizeof(struct in_addr));
   3117  1.26    itojun 		lsrp += sizeof(struct in_addr);
   3118  1.26    itojun #ifndef	sysV88
   3119  1.26    itojun 		lsr[IPOPT_OLEN] = lsrp - lsr;
   3120  1.26    itojun 		if (lsr[IPOPT_OLEN] <= 7 || lsr[IPOPT_OLEN] > 40)
   3121  1.26    itojun 			return -1;
   3122   1.1       cgd 		*lsrp++ = IPOPT_NOP;	/*32bit word align*/
   3123  1.26    itojun 		len = lsrp - lsr;
   3124  1.26    itojun 		*cpp = lsr;
   3125  1.26    itojun #else
   3126  1.26    itojun 		ipopt.io_len = lsrp - lsr;
   3127  1.26    itojun 		if (ipopt.io_len <= 5)	/*is 3 better?*/
   3128   1.5       cgd 			return -1;
   3129   1.5       cgd 		*cpp = (char 8)&ipopt;
   3130  1.26    itojun #endif
   3131  1.26    itojun 	}
   3132  1.26    itojun #ifdef INET6
   3133   1.1       cgd 	else if (ai->ai_family == AF_INET6) {
   3134                 		inet6_rthdr_lasthop(cmsg, IPV6_RTHDR_LOOSE);
   3135                 		len = cmsg->cmsg_len;
   3136                 		*cpp = rhbuf;
   3137                 	}
   3138                 #endif
   3139                 	else
   3140                 		return -1;
   3141                 	return len;
   3142                 }
   3143