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