telnet.c revision 1.40       1  1.40      maya /*	$NetBSD: telnet.c,v 1.40 2018/12/14 06:08:18 maya Exp $	*/
      2   1.7   thorpej 
      3   1.1       cgd /*
      4   1.3       cgd  * Copyright (c) 1988, 1990, 1993
      5   1.3       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.26       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32   1.9  christos #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34   1.7   thorpej #if 0
     35   1.7   thorpej static char sccsid[] = "@(#)telnet.c	8.4 (Berkeley) 5/30/95";
     36   1.7   thorpej #else
     37  1.40      maya __RCSID("$NetBSD: telnet.c,v 1.40 2018/12/14 06:08:18 maya Exp $");
     38   1.7   thorpej #endif
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41  1.16   thorpej #include <sys/param.h>
     42   1.1       cgd 
     43   1.1       cgd #include <signal.h>
     44  1.32       roy #include <term.h>
     45  1.16   thorpej #include <unistd.h>
     46   1.1       cgd /* By the way, we need to include curses.h before telnet.h since,
     47   1.1       cgd  * among other things, telnet.h #defines 'DO', which is a variable
     48   1.1       cgd  * declared in curses.h.
     49   1.1       cgd  */
     50   1.1       cgd 
     51   1.1       cgd #include <arpa/telnet.h>
     52   1.1       cgd 
     53   1.1       cgd #include <ctype.h>
     54   1.1       cgd 
     55   1.1       cgd #include "ring.h"
     56   1.1       cgd #include "defines.h"
     57   1.1       cgd #include "externs.h"
     58   1.1       cgd #include "types.h"
     59   1.1       cgd #include "general.h"
     60   1.1       cgd 
     61  1.16   thorpej #include <libtelnet/misc.h>
     62  1.16   thorpej #ifdef AUTHENTICATION
     63  1.16   thorpej #include <libtelnet/auth.h>
     64  1.16   thorpej #endif
     65  1.16   thorpej #ifdef ENCRYPTION
     66  1.16   thorpej #include <libtelnet/encrypt.h>
     67  1.16   thorpej #endif
     68  1.16   thorpej 
     69   1.5       jtk #define	strip(x) ((my_want_state_is_wont(TELOPT_BINARY)) ? ((x)&0x7f) : (x))
     70   1.1       cgd 
     71   1.1       cgd static unsigned char	subbuffer[SUBBUFSIZE],
     72   1.1       cgd 			*subpointer, *subend;	 /* buffer for sub-options */
     73   1.1       cgd #define	SB_CLEAR()	subpointer = subbuffer;
     74   1.1       cgd #define	SB_TERM()	{ subend = subpointer; SB_CLEAR(); }
     75   1.1       cgd #define	SB_ACCUM(c)	if (subpointer < (subbuffer+sizeof subbuffer)) { \
     76   1.1       cgd 				*subpointer++ = (c); \
     77   1.1       cgd 			}
     78   1.1       cgd 
     79   1.1       cgd #define	SB_GET()	((*subpointer++)&0xff)
     80   1.1       cgd #define	SB_PEEK()	((*subpointer)&0xff)
     81   1.1       cgd #define	SB_EOF()	(subpointer >= subend)
     82   1.1       cgd #define	SB_LEN()	(subend - subpointer)
     83   1.1       cgd 
     84   1.1       cgd char	options[256];		/* The combined options */
     85   1.1       cgd char	do_dont_resp[256];
     86   1.1       cgd char	will_wont_resp[256];
     87   1.1       cgd 
     88   1.1       cgd int
     89   1.1       cgd 	eight = 0,
     90   1.1       cgd 	autologin = 0,	/* Autologin anyone? */
     91   1.1       cgd 	skiprc = 0,
     92   1.1       cgd 	connected,
     93   1.1       cgd 	showoptions,
     94   1.1       cgd 	ISend,		/* trying to send network data in */
     95  1.31        he 	telnet_debug = 0,
     96   1.1       cgd 	crmod,
     97   1.1       cgd 	netdata,	/* Print out network data flow */
     98   1.1       cgd 	crlf,		/* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
     99   1.1       cgd 	telnetport,
    100   1.1       cgd 	SYNCHing,	/* we are in TELNET SYNCH mode */
    101   1.1       cgd 	flushout,	/* flush output */
    102   1.1       cgd 	autoflush = 0,	/* flush output when interrupting? */
    103   1.1       cgd 	autosynch,	/* send interrupt characters with SYNCH? */
    104   1.1       cgd 	localflow,	/* we handle flow control locally */
    105   1.3       cgd 	restartany,	/* if flow control enabled, restart on any character */
    106   1.1       cgd 	localchars,	/* we recognize interrupt/quit */
    107   1.1       cgd 	donelclchars,	/* the user has set "localchars" */
    108   1.1       cgd 	donebinarytoggle,	/* the user has put us in binary */
    109   1.1       cgd 	dontlecho,	/* do we suppress local echoing right now? */
    110   1.6       jtk 	globalmode,
    111  1.13       abs 	doaddrlookup = 1, /* do a reverse address lookup? */
    112   1.6       jtk 	clienteof = 0;
    113   1.1       cgd 
    114   1.1       cgd char *prompt = 0;
    115   1.1       cgd 
    116   1.1       cgd cc_t escape;
    117   1.1       cgd cc_t rlogin;
    118   1.1       cgd #ifdef	KLUDGELINEMODE
    119   1.1       cgd cc_t echoc;
    120   1.1       cgd #endif
    121   1.1       cgd 
    122   1.1       cgd /*
    123   1.1       cgd  * Telnet receiver states for fsm
    124   1.1       cgd  */
    125   1.1       cgd #define	TS_DATA		0
    126   1.1       cgd #define	TS_IAC		1
    127   1.1       cgd #define	TS_WILL		2
    128   1.1       cgd #define	TS_WONT		3
    129   1.1       cgd #define	TS_DO		4
    130   1.1       cgd #define	TS_DONT		5
    131   1.1       cgd #define	TS_CR		6
    132   1.1       cgd #define	TS_SB		7		/* sub-option collection */
    133   1.1       cgd #define	TS_SE		8		/* looking for sub-option end */
    134   1.1       cgd 
    135   1.1       cgd static int	telrcv_state;
    136   1.3       cgd #ifdef	OLD_ENVIRON
    137   1.3       cgd unsigned char telopt_environ = TELOPT_NEW_ENVIRON;
    138   1.3       cgd #else
    139   1.3       cgd # define telopt_environ TELOPT_NEW_ENVIRON
    140   1.3       cgd #endif
    141   1.1       cgd 
    142   1.1       cgd jmp_buf	toplevel = { 0 };
    143   1.1       cgd 
    144   1.1       cgd int	flushline;
    145   1.1       cgd int	linemode;
    146   1.1       cgd 
    147   1.1       cgd #ifdef	KLUDGELINEMODE
    148   1.1       cgd int	kludgelinemode = 1;
    149   1.1       cgd #endif
    150   1.1       cgd 
    151  1.21       wiz static void dooption(int);
    152  1.21       wiz static void dontoption(int);
    153  1.21       wiz static void suboption(void);
    154  1.21       wiz static int telsnd(void);
    155  1.21       wiz static void netclear(void);
    156  1.21       wiz static void doflush(void);
    157   1.9  christos 
    158   1.1       cgd /*
    159   1.1       cgd  * The following are some clocks used to decide how to interpret
    160   1.1       cgd  * the relationship between various variables.
    161   1.1       cgd  */
    162   1.1       cgd 
    163   1.1       cgd Clocks clocks;
    164   1.1       cgd 
    165   1.1       cgd 
    166   1.1       cgd /*
    168   1.1       cgd  * Initialize telnet environment.
    169   1.1       cgd  */
    170  1.22  christos 
    171  1.22  christos void
    172   1.1       cgd init_telnet(void)
    173   1.1       cgd {
    174   1.1       cgd     env_init();
    175   1.1       cgd 
    176   1.1       cgd     SB_CLEAR();
    177   1.1       cgd     ClearArray(options);
    178  1.40      maya 
    179  1.16   thorpej     connected = ISend = localflow = donebinarytoggle = 0;
    180   1.1       cgd #if	defined(AUTHENTICATION) || defined(ENCRYPTION)
    181  1.16   thorpej     auth_encrypt_connect(connected);
    182   1.3       cgd #endif	/* defined(AUTHENTICATION) || defined(ENCRYPTION) */
    183   1.1       cgd     restartany = -1;
    184   1.1       cgd 
    185   1.1       cgd     SYNCHing = 0;
    186   1.1       cgd 
    187   1.1       cgd     /* Don't change NetTrace */
    188   1.1       cgd 
    189   1.1       cgd     escape = CONTROL(']');
    190   1.1       cgd     rlogin = _POSIX_VDISABLE;
    191   1.1       cgd #ifdef	KLUDGELINEMODE
    192   1.1       cgd     echoc = CONTROL('E');
    193   1.1       cgd #endif
    194   1.1       cgd 
    195   1.1       cgd     flushline = 1;
    196   1.1       cgd     telrcv_state = TS_DATA;
    197   1.1       cgd }
    198   1.1       cgd 
    199   1.1       cgd 
    201   1.1       cgd #ifdef	notdef
    202  1.22  christos #include <stdarg.h>
    203  1.22  christos 
    204  1.22  christos /*VARARGS*/
    205   1.1       cgd static void
    206   1.1       cgd printring(Ring *ring, char *format, ...)
    207   1.1       cgd     va_dcl
    208   1.1       cgd {
    209   1.1       cgd     va_list ap;
    210   1.1       cgd     char buffer[100];		/* where things go */
    211   1.1       cgd     char *ptr;
    212   1.1       cgd     char *string;
    213  1.22  christos     int i;
    214   1.1       cgd 
    215   1.1       cgd     va_start(ap, format);
    216   1.1       cgd 
    217   1.1       cgd     ptr = buffer;
    218   1.1       cgd 
    219   1.1       cgd     while ((i = *format++) != 0) {
    220   1.1       cgd 	if (i == '%') {
    221   1.1       cgd 	    i = *format++;
    222   1.1       cgd 	    switch (i) {
    223   1.1       cgd 	    case 'c':
    224   1.1       cgd 		*ptr++ = va_arg(ap, int);
    225   1.1       cgd 		break;
    226   1.1       cgd 	    case 's':
    227   1.1       cgd 		string = va_arg(ap, char *);
    228   1.1       cgd 		ring_supply_data(ring, buffer, ptr-buffer);
    229   1.1       cgd 		ring_supply_data(ring, string, strlen(string));
    230   1.1       cgd 		ptr = buffer;
    231   1.1       cgd 		break;
    232   1.1       cgd 	    case 0:
    233   1.1       cgd 		ExitString("printring: trailing %%.\n", 1);
    234   1.1       cgd 		/*NOTREACHED*/
    235   1.1       cgd 	    default:
    236   1.1       cgd 		ExitString("printring: unknown format character.\n", 1);
    237   1.1       cgd 		/*NOTREACHED*/
    238   1.1       cgd 	    }
    239   1.1       cgd 	} else {
    240   1.1       cgd 	    *ptr++ = i;
    241  1.19       wiz 	}
    242   1.1       cgd     }
    243   1.1       cgd     va_end(ap);
    244   1.1       cgd     ring_supply_data(ring, buffer, ptr-buffer);
    245   1.1       cgd }
    246   1.1       cgd #endif
    247   1.1       cgd 
    248   1.1       cgd /*
    249   1.1       cgd  * These routines are in charge of sending option negotiations
    250   1.1       cgd  * to the other side.
    251   1.1       cgd  *
    252   1.1       cgd  * The basic idea is that we send the negotiation if either side
    253   1.1       cgd  * is in disagreement as to what the current state should be.
    254  1.22  christos  */
    255  1.22  christos 
    256   1.1       cgd void
    257   1.1       cgd send_do(int c, int init)
    258   1.1       cgd {
    259   1.1       cgd     if (init) {
    260   1.1       cgd 	if (((do_dont_resp[c] == 0) && my_state_is_do(c)) ||
    261   1.1       cgd 				my_want_state_is_do(c))
    262   1.1       cgd 	    return;
    263   1.1       cgd 	set_my_want_state_do(c);
    264   1.1       cgd 	do_dont_resp[c]++;
    265   1.1       cgd     }
    266   1.1       cgd     NET2ADD(IAC, DO);
    267   1.1       cgd     NETADD(c);
    268   1.1       cgd     printoption("SENT", DO, c);
    269  1.22  christos }
    270  1.22  christos 
    271   1.1       cgd void
    272   1.1       cgd send_dont(int c, int init)
    273   1.1       cgd {
    274   1.1       cgd     if (init) {
    275   1.1       cgd 	if (((do_dont_resp[c] == 0) && my_state_is_dont(c)) ||
    276   1.1       cgd 				my_want_state_is_dont(c))
    277   1.1       cgd 	    return;
    278   1.1       cgd 	set_my_want_state_dont(c);
    279   1.1       cgd 	do_dont_resp[c]++;
    280   1.1       cgd     }
    281   1.1       cgd     NET2ADD(IAC, DONT);
    282   1.1       cgd     NETADD(c);
    283   1.1       cgd     printoption("SENT", DONT, c);
    284  1.22  christos }
    285  1.22  christos 
    286   1.1       cgd void
    287   1.1       cgd send_will(int c, int init)
    288   1.1       cgd {
    289   1.1       cgd     if (init) {
    290   1.1       cgd 	if (((will_wont_resp[c] == 0) && my_state_is_will(c)) ||
    291   1.1       cgd 				my_want_state_is_will(c))
    292   1.1       cgd 	    return;
    293   1.1       cgd 	set_my_want_state_will(c);
    294   1.1       cgd 	will_wont_resp[c]++;
    295   1.1       cgd     }
    296   1.1       cgd     NET2ADD(IAC, WILL);
    297   1.1       cgd     NETADD(c);
    298   1.1       cgd     printoption("SENT", WILL, c);
    299  1.22  christos }
    300  1.22  christos 
    301   1.1       cgd void
    302   1.1       cgd send_wont(int c, int init)
    303   1.1       cgd {
    304   1.1       cgd     if (init) {
    305   1.1       cgd 	if (((will_wont_resp[c] == 0) && my_state_is_wont(c)) ||
    306   1.1       cgd 				my_want_state_is_wont(c))
    307   1.1       cgd 	    return;
    308   1.1       cgd 	set_my_want_state_wont(c);
    309   1.1       cgd 	will_wont_resp[c]++;
    310   1.1       cgd     }
    311   1.1       cgd     NET2ADD(IAC, WONT);
    312   1.1       cgd     NETADD(c);
    313   1.1       cgd     printoption("SENT", WONT, c);
    314   1.1       cgd }
    315  1.22  christos 
    316  1.22  christos 
    317   1.1       cgd void
    318   1.1       cgd willoption(int option)
    319   1.1       cgd {
    320   1.1       cgd 	int new_state_ok = 0;
    321   1.1       cgd 
    322   1.1       cgd 	if (do_dont_resp[option]) {
    323   1.1       cgd 	    --do_dont_resp[option];
    324   1.1       cgd 	    if (do_dont_resp[option] && my_state_is_do(option))
    325   1.1       cgd 		--do_dont_resp[option];
    326   1.1       cgd 	}
    327   1.1       cgd 
    328   1.1       cgd 	if ((do_dont_resp[option] == 0) && my_want_state_is_dont(option)) {
    329   1.1       cgd 
    330   1.1       cgd 	    switch (option) {
    331   1.1       cgd 
    332   1.1       cgd 	    case TELOPT_ECHO:
    333   1.1       cgd 	    case TELOPT_BINARY:
    334   1.1       cgd 	    case TELOPT_SGA:
    335   1.1       cgd 		settimer(modenegotiated);
    336  1.24    itojun 		/* FALL THROUGH */
    337   1.1       cgd 	    case TELOPT_STATUS:
    338  1.16   thorpej #ifdef AUTHENTICATION
    339  1.16   thorpej 	    case TELOPT_AUTHENTICATION:
    340  1.16   thorpej #ifdef	ENCRYPTION
    341   1.1       cgd 	    case TELOPT_ENCRYPT:
    342   1.1       cgd #endif	/* ENCRYPTION */
    343   1.1       cgd #endif
    344   1.1       cgd 		new_state_ok = 1;
    345   1.1       cgd 		break;
    346   1.1       cgd 
    347   1.1       cgd 	    case TELOPT_TM:
    348   1.1       cgd 		if (flushout)
    349   1.1       cgd 		    flushout = 0;
    350   1.1       cgd 		/*
    351   1.1       cgd 		 * Special case for TM.  If we get back a WILL,
    352   1.1       cgd 		 * pretend we got back a WONT.
    353   1.1       cgd 		 */
    354   1.1       cgd 		set_my_want_state_dont(option);
    355   1.1       cgd 		set_my_state_dont(option);
    356   1.1       cgd 		return;			/* Never reply to TM will's/wont's */
    357   1.1       cgd 
    358   1.1       cgd 	    case TELOPT_LINEMODE:
    359   1.1       cgd 	    default:
    360   1.1       cgd 		break;
    361   1.1       cgd 	    }
    362   1.1       cgd 
    363   1.1       cgd 	    if (new_state_ok) {
    364   1.1       cgd 		set_my_want_state_do(option);
    365   1.1       cgd 		send_do(option, 0);
    366   1.1       cgd 		setconnmode(0);		/* possibly set new tty mode */
    367   1.1       cgd 	    } else {
    368   1.1       cgd 		do_dont_resp[option]++;
    369   1.1       cgd 		send_dont(option, 0);
    370   1.1       cgd 	    }
    371  1.17     assar 	}
    372  1.16   thorpej 	set_my_state_do(option);
    373  1.16   thorpej #ifdef	ENCRYPTION
    374  1.17     assar 	if (option == TELOPT_ENCRYPT)
    375   1.1       cgd 		encrypt_send_support();
    376   1.1       cgd #endif	/* ENCRYPTION */
    377  1.22  christos }
    378  1.22  christos 
    379   1.1       cgd void
    380   1.1       cgd wontoption(int option)
    381   1.1       cgd {
    382   1.1       cgd 	if (do_dont_resp[option]) {
    383   1.1       cgd 	    --do_dont_resp[option];
    384   1.1       cgd 	    if (do_dont_resp[option] && my_state_is_dont(option))
    385   1.1       cgd 		--do_dont_resp[option];
    386   1.1       cgd 	}
    387   1.1       cgd 
    388   1.1       cgd 	if ((do_dont_resp[option] == 0) && my_want_state_is_do(option)) {
    389   1.1       cgd 
    390   1.1       cgd 	    switch (option) {
    391   1.1       cgd 
    392   1.1       cgd #ifdef	KLUDGELINEMODE
    393   1.1       cgd 	    case TELOPT_SGA:
    394   1.1       cgd 		if (!kludgelinemode)
    395   1.1       cgd 		    break;
    396   1.1       cgd 		/* FALL THROUGH */
    397   1.1       cgd #endif
    398   1.1       cgd 	    case TELOPT_ECHO:
    399   1.1       cgd 		settimer(modenegotiated);
    400   1.1       cgd 		break;
    401   1.1       cgd 
    402   1.1       cgd 	    case TELOPT_TM:
    403   1.1       cgd 		if (flushout)
    404   1.1       cgd 		    flushout = 0;
    405   1.1       cgd 		set_my_want_state_dont(option);
    406   1.1       cgd 		set_my_state_dont(option);
    407   1.1       cgd 		return;		/* Never reply to TM will's/wont's */
    408   1.1       cgd 
    409   1.1       cgd 	    default:
    410   1.1       cgd 		break;
    411   1.1       cgd 	    }
    412   1.1       cgd 	    set_my_want_state_dont(option);
    413   1.1       cgd 	    if (my_state_is_do(option))
    414   1.1       cgd 		send_dont(option, 0);
    415   1.1       cgd 	    setconnmode(0);			/* Set new tty mode */
    416   1.1       cgd 	} else if (option == TELOPT_TM) {
    417   1.1       cgd 	    /*
    418   1.1       cgd 	     * Special case for TM.
    419   1.1       cgd 	     */
    420   1.1       cgd 	    if (flushout)
    421   1.1       cgd 		flushout = 0;
    422   1.1       cgd 	    set_my_want_state_dont(option);
    423   1.1       cgd 	}
    424   1.1       cgd 	set_my_state_dont(option);
    425  1.22  christos }
    426  1.22  christos 
    427   1.1       cgd static void
    428   1.1       cgd dooption(int option)
    429   1.1       cgd {
    430   1.1       cgd 	int new_state_ok = 0;
    431   1.1       cgd 
    432   1.1       cgd 	if (will_wont_resp[option]) {
    433   1.1       cgd 	    --will_wont_resp[option];
    434   1.1       cgd 	    if (will_wont_resp[option] && my_state_is_will(option))
    435   1.1       cgd 		--will_wont_resp[option];
    436   1.1       cgd 	}
    437   1.1       cgd 
    438   1.1       cgd 	if (will_wont_resp[option] == 0) {
    439   1.1       cgd 	  if (my_want_state_is_wont(option)) {
    440   1.1       cgd 
    441   1.1       cgd 	    switch (option) {
    442   1.1       cgd 
    443   1.1       cgd 	    case TELOPT_TM:
    444   1.1       cgd 		/*
    445   1.1       cgd 		 * Special case for TM.  We send a WILL, but pretend
    446   1.1       cgd 		 * we sent WONT.
    447   1.1       cgd 		 */
    448   1.1       cgd 		send_will(option, 0);
    449   1.1       cgd 		set_my_want_state_wont(TELOPT_TM);
    450   1.1       cgd 		set_my_state_wont(TELOPT_TM);
    451   1.1       cgd 		return;
    452   1.1       cgd 
    453   1.1       cgd 	    case TELOPT_BINARY:		/* binary mode */
    454   1.1       cgd 	    case TELOPT_NAWS:		/* window size */
    455   1.1       cgd 	    case TELOPT_TSPEED:		/* terminal speed */
    456   1.1       cgd 	    case TELOPT_LFLOW:		/* local flow control */
    457  1.16   thorpej 	    case TELOPT_TTYPE:		/* terminal type option */
    458  1.16   thorpej 	    case TELOPT_SGA:		/* no big deal */
    459  1.16   thorpej #ifdef	ENCRYPTION
    460   1.3       cgd 	    case TELOPT_ENCRYPT:	/* encryption variable option */
    461   1.3       cgd #endif	/* ENCRYPTION */
    462   1.3       cgd 		new_state_ok = 1;
    463   1.3       cgd 		break;
    464   1.3       cgd 
    465   1.3       cgd 	    case TELOPT_NEW_ENVIRON:	/* New environment variable option */
    466   1.3       cgd #ifdef	OLD_ENVIRON
    467   1.3       cgd 		if (my_state_is_will(TELOPT_OLD_ENVIRON))
    468   1.3       cgd 			send_wont(TELOPT_OLD_ENVIRON, 1); /* turn off the old */
    469   1.3       cgd 		goto env_common;
    470   1.3       cgd 	    case TELOPT_OLD_ENVIRON:	/* Old environment variable option */
    471   1.3       cgd 		if (my_state_is_will(TELOPT_NEW_ENVIRON))
    472   1.3       cgd 			break;		/* Don't enable if new one is in use! */
    473   1.1       cgd 	    env_common:
    474   1.1       cgd 		telopt_environ = option;
    475   1.1       cgd #endif
    476   1.3       cgd 		new_state_ok = 1;
    477  1.24    itojun 		break;
    478   1.1       cgd 
    479   1.1       cgd #ifdef AUTHENTICATION
    480   1.1       cgd 	    case TELOPT_AUTHENTICATION:
    481   1.1       cgd 		if (autologin)
    482   1.1       cgd 			new_state_ok = 1;
    483   1.1       cgd 		break;
    484   1.1       cgd #endif
    485  1.35  christos 
    486   1.1       cgd 	    case TELOPT_XDISPLOC:	/* X Display location */
    487   1.1       cgd 		if (env_getvalue((const unsigned char *)"DISPLAY"))
    488   1.1       cgd 		    new_state_ok = 1;
    489   1.1       cgd 		break;
    490   1.1       cgd 
    491   1.1       cgd 	    case TELOPT_LINEMODE:
    492   1.1       cgd #ifdef	KLUDGELINEMODE
    493   1.1       cgd 		kludgelinemode = 0;
    494   1.1       cgd 		send_do(TELOPT_SGA, 1);
    495   1.1       cgd #endif
    496   1.1       cgd 		set_my_want_state_will(TELOPT_LINEMODE);
    497   1.1       cgd 		send_will(option, 0);
    498   1.1       cgd 		set_my_state_will(TELOPT_LINEMODE);
    499   1.1       cgd 		slc_init();
    500   1.1       cgd 		return;
    501   1.1       cgd 
    502   1.1       cgd 	    case TELOPT_ECHO:		/* We're never going to echo... */
    503   1.1       cgd 	    default:
    504   1.1       cgd 		break;
    505   1.1       cgd 	    }
    506   1.1       cgd 
    507   1.1       cgd 	    if (new_state_ok) {
    508   1.1       cgd 		set_my_want_state_will(option);
    509   1.1       cgd 		send_will(option, 0);
    510   1.1       cgd 		setconnmode(0);			/* Set new tty mode */
    511   1.1       cgd 	    } else {
    512   1.1       cgd 		will_wont_resp[option]++;
    513   1.1       cgd 		send_wont(option, 0);
    514   1.1       cgd 	    }
    515   1.1       cgd 	  } else {
    516   1.1       cgd 	    /*
    517   1.1       cgd 	     * Handle options that need more things done after the
    518   1.1       cgd 	     * other side has acknowledged the option.
    519   1.1       cgd 	     */
    520   1.1       cgd 	    switch (option) {
    521   1.1       cgd 	    case TELOPT_LINEMODE:
    522   1.1       cgd #ifdef	KLUDGELINEMODE
    523   1.1       cgd 		kludgelinemode = 0;
    524   1.1       cgd 		send_do(TELOPT_SGA, 1);
    525   1.1       cgd #endif
    526   1.1       cgd 		set_my_state_will(option);
    527   1.1       cgd 		slc_init();
    528   1.1       cgd 		send_do(TELOPT_SGA, 0);
    529   1.1       cgd 		return;
    530   1.1       cgd 	    }
    531   1.1       cgd 	  }
    532   1.1       cgd 	}
    533   1.1       cgd 	set_my_state_will(option);
    534  1.22  christos }
    535  1.22  christos 
    536   1.1       cgd static void
    537   1.1       cgd dontoption(int option)
    538   1.1       cgd {
    539   1.1       cgd 
    540   1.1       cgd 	if (will_wont_resp[option]) {
    541   1.1       cgd 	    --will_wont_resp[option];
    542   1.1       cgd 	    if (will_wont_resp[option] && my_state_is_wont(option))
    543   1.1       cgd 		--will_wont_resp[option];
    544   1.1       cgd 	}
    545   1.1       cgd 
    546   1.1       cgd 	if ((will_wont_resp[option] == 0) && my_want_state_is_will(option)) {
    547   1.1       cgd 	    switch (option) {
    548   1.1       cgd 	    case TELOPT_LINEMODE:
    549   1.3       cgd 		linemode = 0;	/* put us back to the default state */
    550   1.3       cgd 		break;
    551   1.3       cgd #ifdef	OLD_ENVIRON
    552   1.3       cgd 	    case TELOPT_NEW_ENVIRON:
    553   1.3       cgd 		/*
    554   1.3       cgd 		 * The new environ option wasn't recognized, try
    555   1.3       cgd 		 * the old one.
    556   1.3       cgd 		 */
    557   1.3       cgd 		send_will(TELOPT_OLD_ENVIRON, 1);
    558   1.3       cgd 		telopt_environ = TELOPT_OLD_ENVIRON;
    559   1.1       cgd 		break;
    560   1.1       cgd #endif
    561   1.1       cgd 	    }
    562   1.1       cgd 	    /* we always accept a DONT */
    563   1.1       cgd 	    set_my_want_state_wont(option);
    564   1.1       cgd 	    if (my_state_is_will(option))
    565   1.1       cgd 		send_wont(option, 0);
    566   1.1       cgd 	    setconnmode(0);			/* Set new tty mode */
    567   1.1       cgd 	}
    568   1.1       cgd 	set_my_state_wont(option);
    569   1.1       cgd }
    570   1.1       cgd 
    571  1.18       wiz /*
    572   1.1       cgd  * Given a buffer returned by tgetent(), this routine will turn
    573   1.1       cgd  * the pipe separated list of names in the buffer into an array
    574   1.1       cgd  * of pointers to null terminated names.  We toss out any bad,
    575   1.1       cgd  * duplicate, or verbose names (names with spaces).
    576  1.35  christos  */
    577   1.1       cgd 
    578   1.1       cgd static char name_unknown[] = "UNKNOWN";
    579  1.22  christos static char *unknown[] = { 0, 0 };
    580  1.22  christos 
    581   1.1       cgd char **
    582  1.21       wiz mklist(char *buf, char *name)
    583  1.21       wiz {
    584   1.1       cgd 	int n;
    585   1.1       cgd 	char c, *cp, **argvp, *cp2, **argv, **avt;
    586   1.5       jtk 
    587   1.1       cgd 	if (name) {
    588   1.1       cgd 		if ((int)strlen(name) > 40) {
    589   1.1       cgd 			name = 0;
    590   1.1       cgd 			unknown[0] = name_unknown;
    591   1.1       cgd 		} else {
    592   1.1       cgd 			unknown[0] = name;
    593   1.1       cgd 			upcase(name);
    594   1.1       cgd 		}
    595   1.1       cgd 	} else
    596   1.1       cgd 		unknown[0] = name_unknown;
    597   1.1       cgd 	/*
    598   1.1       cgd 	 * Count up the number of names.
    599   1.1       cgd 	 */
    600   1.1       cgd 	for (n = 1, cp = buf; *cp && *cp != ':'; cp++) {
    601   1.1       cgd 		if (*cp == '|')
    602   1.1       cgd 			n++;
    603   1.1       cgd 	}
    604   1.1       cgd 	/*
    605   1.1       cgd 	 * Allocate an array to put the name pointers into
    606   1.1       cgd 	 */
    607   1.1       cgd 	argv = (char **)malloc((n+3)*sizeof(char *));
    608   1.1       cgd 	if (argv == 0)
    609   1.1       cgd 		return(unknown);
    610   1.1       cgd 
    611   1.1       cgd 	/*
    612   1.1       cgd 	 * Fill up the array of pointers to names.
    613   1.1       cgd 	 */
    614   1.1       cgd 	*argv = 0;
    615   1.1       cgd 	argvp = argv+1;
    616   1.1       cgd 	n = 0;
    617   1.1       cgd 	for (cp = cp2 = buf; (c = *cp);  cp++) {
    618   1.1       cgd 		if (c == '|' || c == ':') {
    619   1.1       cgd 			*cp++ = '\0';
    620   1.1       cgd 			/*
    621   1.1       cgd 			 * Skip entries that have spaces or are over 40
    622   1.1       cgd 			 * characters long.  If this is our environment
    623   1.1       cgd 			 * name, then put it up front.  Otherwise, as
    624   1.1       cgd 			 * long as this is not a duplicate name (case
    625   1.1       cgd 			 * insensitive) add it to the list.
    626   1.1       cgd 			 */
    627   1.1       cgd 			if (n || (cp - cp2 > 41))
    628   1.1       cgd 				;
    629   1.1       cgd 			else if (name && (strncasecmp(name, cp2, cp-cp2) == 0))
    630   1.1       cgd 				*argv = cp2;
    631   1.1       cgd 			else if (is_unique(cp2, argv+1, argvp))
    632   1.1       cgd 				*argvp++ = cp2;
    633   1.1       cgd 			if (c == ':')
    634   1.1       cgd 				break;
    635   1.1       cgd 			/*
    636   1.1       cgd 			 * Skip multiple delimiters. Reset cp2 to
    637   1.1       cgd 			 * the beginning of the next name. Reset n,
    638   1.1       cgd 			 * the flag for names with spaces.
    639   1.1       cgd 			 */
    640   1.1       cgd 			while ((c = *cp) == '|')
    641   1.1       cgd 				cp++;
    642   1.1       cgd 			cp2 = cp;
    643   1.1       cgd 			n = 0;
    644   1.1       cgd 		}
    645   1.1       cgd 		/*
    646   1.1       cgd 		 * Skip entries with spaces or non-ascii values.
    647   1.1       cgd 		 * Convert lower case letters to upper case.
    648   1.1       cgd 		 */
    649  1.12  christos 		if ((c == ' ') || !isascii(c))
    650  1.29       dsl 			n = 1;
    651   1.1       cgd 		else if (islower((unsigned char)c))
    652   1.5       jtk 			*cp = toupper((unsigned char)c);
    653   1.1       cgd 	}
    654   1.1       cgd 
    655   1.1       cgd 	/*
    656   1.1       cgd 	 * Check for an old V6 2 character name.  If the second
    657   1.1       cgd 	 * name points to the beginning of the buffer, and is
    658   1.1       cgd 	 * only 2 characters long, move it to the end of the array.
    659   1.1       cgd 	 */
    660   1.1       cgd 	if ((argv[1] == buf) && (strlen(argv[1]) == 2)) {
    661   1.1       cgd 		--argvp;
    662   1.1       cgd 		for (avt = &argv[1]; avt < argvp; avt++)
    663   1.1       cgd 			*avt = *(avt+1);
    664   1.1       cgd 		*argvp++ = buf;
    665   1.1       cgd 	}
    666   1.1       cgd 
    667   1.1       cgd 	/*
    668   1.1       cgd 	 * Duplicate last name, for TTYPE option, and null
    669   1.1       cgd 	 * terminate the array.  If we didn't find a match on
    670   1.1       cgd 	 * our terminal name, put that name at the beginning.
    671   1.1       cgd 	 */
    672   1.1       cgd 	cp = *(argvp-1);
    673   1.1       cgd 	*argvp++ = cp;
    674   1.1       cgd 	*argvp = 0;
    675   1.1       cgd 
    676   1.1       cgd 	if (*argv == 0) {
    677   1.1       cgd 		if (name)
    678   1.1       cgd 			*argv = name;
    679   1.1       cgd 		else {
    680   1.1       cgd 			--argvp;
    681   1.1       cgd 			for (avt = argv; avt < argvp; avt++)
    682   1.1       cgd 				*avt = *(avt+1);
    683   1.1       cgd 		}
    684   1.1       cgd 	}
    685   1.1       cgd 	if (*argv)
    686   1.1       cgd 		return(argv);
    687   1.1       cgd 	else
    688   1.1       cgd 		return(unknown);
    689  1.22  christos }
    690  1.22  christos 
    691   1.1       cgd int
    692  1.21       wiz is_unique(char *name, char **as, char **ae)
    693  1.21       wiz {
    694   1.1       cgd 	char **ap;
    695   1.1       cgd 	int n;
    696   1.1       cgd 
    697   1.1       cgd 	n = strlen(name) + 1;
    698   1.1       cgd 	for (ap = as; ap < ae; ap++)
    699   1.1       cgd 		if (strncasecmp(*ap, name, n) == 0)
    700   1.1       cgd 			return(0);
    701   1.1       cgd 	return (1);
    702   1.1       cgd }
    703  1.15     blymn 
    704   1.1       cgd #ifdef	TERMCAP
    705  1.22  christos char *termbuf;
    706  1.22  christos 
    707  1.32       roy /*ARGSUSED*/
    708   1.1       cgd int
    709  1.15     blymn setupterm(char *tname, int fd, int *errp)
    710  1.15     blymn {
    711  1.33     joerg 	char zz[1024], *zz_ptr;
    712  1.15     blymn 	char *ext_tc, *newptr;
    713  1.34  christos 	size_t len;
    714  1.15     blymn 
    715  1.15     blymn 	if ((termbuf = malloc(1024)) == NULL)
    716   1.1       cgd 		goto error;
    717  1.34  christos 
    718  1.15     blymn 	if (tgetent(termbuf, tname) == 1) {
    719  1.15     blymn 		/* check for ZZ capability, indicating termcap truncated */
    720  1.34  christos 		zz_ptr = zz;
    721  1.15     blymn 		if (tgetstr("ZZ", &zz_ptr) != NULL) {
    722  1.33     joerg 			/* it was, fish back the full termcap */
    723  1.34  christos 			sscanf(zz, "%p", &ext_tc);
    724  1.15     blymn 			len = strlen(ext_tc) + 1;
    725  1.15     blymn 			if ((newptr = realloc(termbuf, len)) == NULL)
    726  1.33     joerg 				goto error;
    727  1.15     blymn 
    728  1.15     blymn 			memcpy(newptr, ext_tc, len);
    729  1.15     blymn 			termbuf = newptr;
    730   1.1       cgd 		}
    731   1.1       cgd 
    732   1.1       cgd 		if (errp)
    733   1.1       cgd 			*errp = 1;
    734  1.15     blymn 		return(0);
    735   1.1       cgd 	}
    736   1.1       cgd   error:
    737   1.1       cgd 	if (errp)
    738   1.1       cgd 		*errp = 0;
    739   1.1       cgd 	return(-1);
    740   1.1       cgd }
    741   1.1       cgd #else
    742   1.1       cgd #define	termbuf	ttytype
    743   1.1       cgd extern char ttytype[];
    744   1.1       cgd #endif
    745   1.1       cgd 
    746  1.22  christos int resettermname = 1;
    747  1.22  christos 
    748   1.1       cgd char *
    749   1.1       cgd gettermname(void)
    750   1.1       cgd {
    751   1.1       cgd 	char *tname;
    752   1.1       cgd 	static char **tnamep = 0;
    753   1.1       cgd 	static char **next;
    754   1.1       cgd 	int err;
    755   1.1       cgd 
    756   1.1       cgd 	if (resettermname) {
    757   1.1       cgd 		resettermname = 0;
    758  1.35  christos 		if (tnamep && tnamep != unknown)
    759  1.32       roy 			free(tnamep);
    760   1.1       cgd 		if ((tname = (char *)env_getvalue((const unsigned char *)"TERM")) &&
    761   1.1       cgd 				(setupterm(tname, 1, &err) == 0)) {
    762   1.5       jtk 			tnamep = mklist(termbuf, tname);
    763   1.1       cgd 		} else {
    764   1.1       cgd 			if (tname && ((int)strlen(tname) <= 40)) {
    765   1.1       cgd 				unknown[0] = tname;
    766   1.1       cgd 				upcase(tname);
    767   1.1       cgd 			} else
    768   1.1       cgd 				unknown[0] = name_unknown;
    769   1.1       cgd 			tnamep = unknown;
    770   1.1       cgd 		}
    771   1.1       cgd 		next = tnamep;
    772   1.1       cgd 	}
    773   1.1       cgd 	if (*next == 0)
    774   1.1       cgd 		next = tnamep;
    775   1.1       cgd 	return(*next++);
    776   1.1       cgd }
    777   1.1       cgd /*
    778   1.1       cgd  * suboption()
    779   1.1       cgd  *
    780   1.1       cgd  *	Look at the sub-option buffer, and try to be helpful to the other
    781   1.1       cgd  * side.
    782   1.1       cgd  *
    783   1.1       cgd  *	Currently we recognize:
    784   1.1       cgd  *
    785   1.1       cgd  *		Terminal type, send request.
    786   1.1       cgd  *		Terminal speed (send request).
    787   1.1       cgd  *		Local flow control (is request).
    788   1.1       cgd  *		Linemode
    789  1.22  christos  */
    790  1.22  christos 
    791   1.1       cgd static void
    792   1.3       cgd suboption(void)
    793   1.3       cgd {
    794   1.1       cgd     unsigned char subchar;
    795   1.3       cgd 
    796   1.1       cgd     printsub('<', subbuffer, SB_LEN()+2);
    797   1.1       cgd     switch (subchar = SB_GET()) {
    798   1.1       cgd     case TELOPT_TTYPE:
    799   1.1       cgd 	if (my_want_state_is_wont(TELOPT_TTYPE))
    800   1.1       cgd 	    return;
    801   1.1       cgd 	if (SB_EOF() || SB_GET() != TELQUAL_SEND) {
    802   1.1       cgd 	    return;
    803   1.1       cgd 	} else {
    804   1.1       cgd 	    char *name;
    805   1.1       cgd 	    unsigned char temp[50];
    806   1.1       cgd 	    int len;
    807   1.1       cgd 
    808   1.1       cgd 	    name = gettermname();
    809  1.37      maya 	    len = strlen(name) + 4 + 2;
    810  1.37      maya 	    if (len < NETROOM()) {
    811   1.1       cgd 		snprintf((char *)temp, sizeof(temp), "%c%c%c%c%s%c%c", IAC, SB,
    812   1.1       cgd 				TELOPT_TTYPE, TELQUAL_IS, name, IAC, SE);
    813   1.1       cgd 		ring_supply_data(&netoring, temp, len);
    814   1.1       cgd 		printsub('>', &temp[2], len-2);
    815   1.1       cgd 	    } else {
    816   1.1       cgd 		ExitString("No room in buffer for terminal type.\n", 1);
    817   1.1       cgd 		/*NOTREACHED*/
    818   1.1       cgd 	    }
    819   1.1       cgd 	}
    820   1.1       cgd 	break;
    821   1.1       cgd     case TELOPT_TSPEED:
    822   1.1       cgd 	if (my_want_state_is_wont(TELOPT_TSPEED))
    823   1.1       cgd 	    return;
    824   1.1       cgd 	if (SB_EOF())
    825  1.35  christos 	    return;
    826   1.1       cgd 	if (SB_GET() == TELQUAL_SEND) {
    827   1.1       cgd 	    long osp, isp;
    828   1.1       cgd 	    unsigned char temp[50];
    829  1.35  christos 	    int len;
    830   1.1       cgd 
    831  1.37      maya 	    TerminalSpeeds(&isp, &osp);
    832  1.37      maya 
    833   1.1       cgd 	    snprintf((char *)temp, sizeof(temp), "%c%c%c%c%ld,%ld%c%c", IAC, SB,
    834   1.1       cgd 		    TELOPT_TSPEED, TELQUAL_IS, osp, isp, IAC, SE);
    835   1.1       cgd 	    len = strlen((char *)temp+4) + 4;	/* temp[3] is 0 ... */
    836   1.1       cgd 
    837   1.1       cgd 	    if (len < NETROOM()) {
    838   1.1       cgd 		ring_supply_data(&netoring, temp, len);
    839   1.1       cgd 		printsub('>', temp+2, len - 2);
    840   1.1       cgd 	    }
    841   1.1       cgd /*@*/	    else printf("lm_will: not enough room in buffer\n");
    842   1.1       cgd 	}
    843   1.1       cgd 	break;
    844   1.1       cgd     case TELOPT_LFLOW:
    845   1.1       cgd 	if (my_want_state_is_wont(TELOPT_LFLOW))
    846   1.1       cgd 	    return;
    847   1.1       cgd 	if (SB_EOF())
    848   1.3       cgd 	    return;
    849   1.3       cgd 	switch(SB_GET()) {
    850   1.3       cgd 	case LFLOW_RESTART_ANY:
    851   1.3       cgd 	    restartany = 1;
    852   1.3       cgd 	    break;
    853   1.3       cgd 	case LFLOW_RESTART_XON:
    854   1.3       cgd 	    restartany = 0;
    855   1.1       cgd 	    break;
    856   1.1       cgd 	case LFLOW_ON:
    857   1.3       cgd 	    localflow = 1;
    858   1.1       cgd 	    break;
    859   1.1       cgd 	case LFLOW_OFF:
    860   1.1       cgd 	    localflow = 0;
    861   1.1       cgd 	    break;
    862   1.1       cgd 	default:
    863   1.1       cgd 	    return;
    864   1.1       cgd 	}
    865   1.1       cgd 	setcommandmode();
    866   1.1       cgd 	setconnmode(0);
    867   1.1       cgd 	break;
    868   1.1       cgd 
    869   1.1       cgd     case TELOPT_LINEMODE:
    870   1.1       cgd 	if (my_want_state_is_wont(TELOPT_LINEMODE))
    871   1.1       cgd 	    return;
    872   1.1       cgd 	if (SB_EOF())
    873   1.1       cgd 	    return;
    874   1.1       cgd 	switch (SB_GET()) {
    875   1.1       cgd 	case WILL:
    876   1.1       cgd 	    lm_will(subpointer, SB_LEN());
    877   1.1       cgd 	    break;
    878   1.1       cgd 	case WONT:
    879   1.1       cgd 	    lm_wont(subpointer, SB_LEN());
    880   1.1       cgd 	    break;
    881   1.1       cgd 	case DO:
    882   1.1       cgd 	    lm_do(subpointer, SB_LEN());
    883   1.1       cgd 	    break;
    884   1.1       cgd 	case DONT:
    885   1.1       cgd 	    lm_dont(subpointer, SB_LEN());
    886   1.1       cgd 	    break;
    887   1.1       cgd 	case LM_SLC:
    888   1.1       cgd 	    slc(subpointer, SB_LEN());
    889   1.1       cgd 	    break;
    890   1.1       cgd 	case LM_MODE:
    891   1.1       cgd 	    lm_mode(subpointer, SB_LEN(), 0);
    892   1.1       cgd 	    break;
    893   1.1       cgd 	default:
    894   1.1       cgd 	    break;
    895   1.1       cgd 	}
    896   1.3       cgd 	break;
    897   1.3       cgd 
    898   1.3       cgd #ifdef	OLD_ENVIRON
    899   1.3       cgd     case TELOPT_OLD_ENVIRON:
    900   1.1       cgd #endif
    901   1.1       cgd     case TELOPT_NEW_ENVIRON:
    902   1.1       cgd 	if (SB_EOF())
    903   1.1       cgd 	    return;
    904   1.1       cgd 	switch(SB_PEEK()) {
    905   1.3       cgd 	case TELQUAL_IS:
    906   1.1       cgd 	case TELQUAL_INFO:
    907   1.1       cgd 	    if (my_want_state_is_dont(subchar))
    908   1.1       cgd 		return;
    909   1.3       cgd 	    break;
    910   1.1       cgd 	case TELQUAL_SEND:
    911   1.1       cgd 	    if (my_want_state_is_wont(subchar)) {
    912   1.1       cgd 		return;
    913   1.1       cgd 	    }
    914   1.1       cgd 	    break;
    915   1.1       cgd 	default:
    916   1.1       cgd 	    return;
    917   1.1       cgd 	}
    918   1.1       cgd 	env_opt(subpointer, SB_LEN());
    919   1.1       cgd 	break;
    920   1.1       cgd 
    921   1.1       cgd     case TELOPT_XDISPLOC:
    922   1.1       cgd 	if (my_want_state_is_wont(TELOPT_XDISPLOC))
    923   1.1       cgd 	    return;
    924   1.1       cgd 	if (SB_EOF())
    925   1.1       cgd 	    return;
    926   1.1       cgd 	if (SB_GET() == TELQUAL_SEND) {
    927   1.1       cgd 	    unsigned char temp[50], *dp;
    928  1.35  christos 	    int len;
    929   1.1       cgd 
    930   1.1       cgd 	    if ((dp = env_getvalue((const unsigned char *)"DISPLAY")) == NULL) {
    931   1.1       cgd 		/*
    932   1.1       cgd 		 * Something happened, we no longer have a DISPLAY
    933   1.1       cgd 		 * variable.  So, turn off the option.
    934   1.1       cgd 		 */
    935   1.1       cgd 		send_wont(TELOPT_XDISPLOC, 1);
    936  1.37      maya 		break;
    937  1.37      maya 	    }
    938   1.1       cgd 	    snprintf((char *)temp, sizeof(temp), "%c%c%c%c%s%c%c", IAC, SB,
    939   1.1       cgd 		    TELOPT_XDISPLOC, TELQUAL_IS, dp, IAC, SE);
    940   1.1       cgd 	    len = strlen((char *)temp+4) + 4;	/* temp[3] is 0 ... */
    941   1.1       cgd 
    942   1.1       cgd 	    if (len < NETROOM()) {
    943   1.1       cgd 		ring_supply_data(&netoring, temp, len);
    944   1.1       cgd 		printsub('>', temp+2, len - 2);
    945   1.1       cgd 	    }
    946   1.1       cgd /*@*/	    else printf("lm_will: not enough room in buffer\n");
    947   1.1       cgd 	}
    948  1.24    itojun 	break;
    949   1.1       cgd 
    950   1.1       cgd #ifdef AUTHENTICATION
    951   1.1       cgd 	case TELOPT_AUTHENTICATION: {
    952   1.1       cgd 		if (!autologin)
    953   1.1       cgd 			break;
    954   1.1       cgd 		if (SB_EOF())
    955   1.1       cgd 			return;
    956   1.1       cgd 		switch(SB_GET()) {
    957   1.1       cgd 		case TELQUAL_IS:
    958   1.1       cgd 			if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
    959   1.1       cgd 				return;
    960   1.1       cgd 			auth_is(subpointer, SB_LEN());
    961   1.1       cgd 			break;
    962   1.1       cgd 		case TELQUAL_SEND:
    963   1.1       cgd 			if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
    964   1.1       cgd 				return;
    965   1.1       cgd 			auth_send(subpointer, SB_LEN());
    966   1.1       cgd 			break;
    967   1.1       cgd 		case TELQUAL_REPLY:
    968   1.1       cgd 			if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
    969   1.1       cgd 				return;
    970   1.1       cgd 			auth_reply(subpointer, SB_LEN());
    971   1.1       cgd 			break;
    972   1.1       cgd 		case TELQUAL_NAME:
    973   1.1       cgd 			if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
    974   1.1       cgd 				return;
    975   1.1       cgd 			auth_name(subpointer, SB_LEN());
    976   1.1       cgd 			break;
    977   1.1       cgd 		}
    978   1.1       cgd 	}
    979  1.16   thorpej 	break;
    980  1.16   thorpej #endif
    981  1.16   thorpej #ifdef	ENCRYPTION
    982  1.16   thorpej     case TELOPT_ENCRYPT:
    983  1.16   thorpej 	if (SB_EOF())
    984  1.16   thorpej 		return;
    985  1.16   thorpej 	switch(SB_GET()) {
    986  1.16   thorpej 	case ENCRYPT_START:
    987  1.16   thorpej 		if (my_want_state_is_dont(TELOPT_ENCRYPT))
    988  1.16   thorpej 			return;
    989  1.16   thorpej 		encrypt_start(subpointer, SB_LEN());
    990  1.16   thorpej 		break;
    991  1.16   thorpej 	case ENCRYPT_END:
    992  1.16   thorpej 		if (my_want_state_is_dont(TELOPT_ENCRYPT))
    993  1.16   thorpej 			return;
    994  1.16   thorpej 		encrypt_end();
    995  1.16   thorpej 		break;
    996  1.16   thorpej 	case ENCRYPT_SUPPORT:
    997  1.16   thorpej 		if (my_want_state_is_wont(TELOPT_ENCRYPT))
    998  1.16   thorpej 			return;
    999  1.16   thorpej 		encrypt_support(subpointer, SB_LEN());
   1000  1.16   thorpej 		break;
   1001  1.16   thorpej 	case ENCRYPT_REQSTART:
   1002  1.16   thorpej 		if (my_want_state_is_wont(TELOPT_ENCRYPT))
   1003  1.16   thorpej 			return;
   1004  1.16   thorpej 		encrypt_request_start(subpointer, SB_LEN());
   1005  1.16   thorpej 		break;
   1006  1.16   thorpej 	case ENCRYPT_REQEND:
   1007  1.16   thorpej 		if (my_want_state_is_wont(TELOPT_ENCRYPT))
   1008  1.16   thorpej 			return;
   1009  1.16   thorpej 		/*
   1010  1.16   thorpej 		 * We can always send an REQEND so that we cannot
   1011  1.16   thorpej 		 * get stuck encrypting.  We should only get this
   1012  1.16   thorpej 		 * if we have been able to get in the correct mode
   1013  1.16   thorpej 		 * anyhow.
   1014  1.16   thorpej 		 */
   1015  1.16   thorpej 		encrypt_request_end();
   1016  1.16   thorpej 		break;
   1017  1.16   thorpej 	case ENCRYPT_IS:
   1018  1.16   thorpej 		if (my_want_state_is_dont(TELOPT_ENCRYPT))
   1019  1.16   thorpej 			return;
   1020  1.16   thorpej 		encrypt_is(subpointer, SB_LEN());
   1021  1.16   thorpej 		break;
   1022  1.16   thorpej 	case ENCRYPT_REPLY:
   1023  1.16   thorpej 		if (my_want_state_is_wont(TELOPT_ENCRYPT))
   1024  1.16   thorpej 			return;
   1025  1.16   thorpej 		encrypt_reply(subpointer, SB_LEN());
   1026  1.16   thorpej 		break;
   1027  1.16   thorpej 	case ENCRYPT_ENC_KEYID:
   1028  1.16   thorpej 		if (my_want_state_is_dont(TELOPT_ENCRYPT))
   1029  1.16   thorpej 			return;
   1030  1.16   thorpej 		encrypt_enc_keyid(subpointer, SB_LEN());
   1031  1.16   thorpej 		break;
   1032  1.16   thorpej 	case ENCRYPT_DEC_KEYID:
   1033  1.16   thorpej 		if (my_want_state_is_wont(TELOPT_ENCRYPT))
   1034  1.16   thorpej 			return;
   1035  1.16   thorpej 		encrypt_dec_keyid(subpointer, SB_LEN());
   1036  1.16   thorpej 		break;
   1037  1.16   thorpej 	default:
   1038  1.16   thorpej 		break;
   1039  1.16   thorpej 	}
   1040   1.1       cgd 	break;
   1041   1.1       cgd #endif	/* ENCRYPTION */
   1042   1.1       cgd     default:
   1043   1.1       cgd 	break;
   1044   1.1       cgd     }
   1045   1.1       cgd }
   1046   1.1       cgd 
   1047  1.22  christos static unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE };
   1048  1.22  christos 
   1049   1.1       cgd void
   1050   1.1       cgd lm_will(unsigned char *cmd, int len)
   1051   1.1       cgd {
   1052   1.1       cgd     if (len < 1) {
   1053   1.1       cgd /*@*/	printf("lm_will: no command!!!\n");	/* Should not happen... */
   1054   1.1       cgd 	return;
   1055   1.1       cgd     }
   1056   1.1       cgd     switch(cmd[0]) {
   1057   1.1       cgd     case LM_FORWARDMASK:	/* We shouldn't ever get this... */
   1058   1.1       cgd     default:
   1059  1.35  christos 	str_lm[3] = DONT;
   1060   1.1       cgd 	str_lm[4] = cmd[0];
   1061   1.1       cgd 	if ((size_t)NETROOM() > sizeof(str_lm)) {
   1062   1.1       cgd 	    ring_supply_data(&netoring, str_lm, sizeof(str_lm));
   1063   1.1       cgd 	    printsub('>', &str_lm[2], sizeof(str_lm)-2);
   1064   1.1       cgd 	}
   1065   1.1       cgd /*@*/	else printf("lm_will: not enough room in buffer\n");
   1066   1.1       cgd 	break;
   1067   1.1       cgd     }
   1068  1.22  christos }
   1069  1.22  christos 
   1070   1.1       cgd void
   1071   1.1       cgd lm_wont(unsigned char *cmd, int len)
   1072   1.1       cgd {
   1073   1.1       cgd     if (len < 1) {
   1074   1.1       cgd /*@*/	printf("lm_wont: no command!!!\n");	/* Should not happen... */
   1075   1.1       cgd 	return;
   1076   1.1       cgd     }
   1077   1.1       cgd     switch(cmd[0]) {
   1078   1.1       cgd     case LM_FORWARDMASK:	/* We shouldn't ever get this... */
   1079   1.1       cgd     default:
   1080   1.1       cgd 	/* We are always DONT, so don't respond */
   1081   1.1       cgd 	return;
   1082   1.1       cgd     }
   1083  1.22  christos }
   1084  1.22  christos 
   1085   1.1       cgd void
   1086   1.1       cgd lm_do(unsigned char *cmd, int len)
   1087   1.1       cgd {
   1088   1.1       cgd     if (len < 1) {
   1089   1.1       cgd /*@*/	printf("lm_do: no command!!!\n");	/* Should not happen... */
   1090   1.1       cgd 	return;
   1091   1.1       cgd     }
   1092   1.1       cgd     switch(cmd[0]) {
   1093   1.1       cgd     case LM_FORWARDMASK:
   1094   1.1       cgd     default:
   1095  1.35  christos 	str_lm[3] = WONT;
   1096   1.1       cgd 	str_lm[4] = cmd[0];
   1097   1.1       cgd 	if ((size_t)NETROOM() > sizeof(str_lm)) {
   1098   1.1       cgd 	    ring_supply_data(&netoring, str_lm, sizeof(str_lm));
   1099   1.1       cgd 	    printsub('>', &str_lm[2], sizeof(str_lm)-2);
   1100   1.1       cgd 	}
   1101   1.1       cgd /*@*/	else printf("lm_do: not enough room in buffer\n");
   1102   1.1       cgd 	break;
   1103   1.1       cgd     }
   1104  1.22  christos }
   1105  1.22  christos 
   1106   1.1       cgd void
   1107   1.1       cgd lm_dont(unsigned char *cmd, int len)
   1108   1.1       cgd {
   1109   1.1       cgd     if (len < 1) {
   1110   1.1       cgd /*@*/	printf("lm_dont: no command!!!\n");	/* Should not happen... */
   1111   1.1       cgd 	return;
   1112   1.1       cgd     }
   1113   1.1       cgd     switch(cmd[0]) {
   1114   1.1       cgd     case LM_FORWARDMASK:
   1115   1.1       cgd     default:
   1116   1.1       cgd 	/* we are always WONT, so don't respond */
   1117   1.1       cgd 	break;
   1118   1.1       cgd     }
   1119   1.1       cgd }
   1120   1.1       cgd 
   1121   1.1       cgd static unsigned char str_lm_mode[] = {
   1122   1.1       cgd 	IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE
   1123  1.22  christos };
   1124  1.22  christos 
   1125   1.1       cgd void
   1126   1.1       cgd lm_mode(unsigned char *cmd, int len, int init)
   1127   1.1       cgd {
   1128   1.1       cgd 	if (len != 1)
   1129   1.1       cgd 		return;
   1130   1.1       cgd 	if ((linemode&MODE_MASK&~MODE_ACK) == *cmd)
   1131   1.1       cgd 		return;
   1132   1.1       cgd 	if (*cmd&MODE_ACK)
   1133   1.1       cgd 		return;
   1134   1.1       cgd 	linemode = *cmd&(MODE_MASK&~MODE_ACK);
   1135   1.1       cgd 	str_lm_mode[4] = linemode;
   1136  1.35  christos 	if (!init)
   1137   1.1       cgd 	    str_lm_mode[4] |= MODE_ACK;
   1138   1.1       cgd 	if ((size_t)NETROOM() > sizeof(str_lm_mode)) {
   1139   1.1       cgd 	    ring_supply_data(&netoring, str_lm_mode, sizeof(str_lm_mode));
   1140   1.1       cgd 	    printsub('>', &str_lm_mode[2], sizeof(str_lm_mode)-2);
   1141   1.1       cgd 	}
   1142   1.1       cgd /*@*/	else printf("lm_mode: not enough room in buffer\n");
   1143   1.1       cgd 	setconnmode(0);	/* set changed mode */
   1144   1.1       cgd }
   1145   1.1       cgd 
   1146   1.1       cgd 
   1147   1.1       cgd 
   1149   1.1       cgd /*
   1150   1.1       cgd  * slc()
   1151   1.1       cgd  * Handle special character suboption of LINEMODE.
   1152   1.1       cgd  */
   1153   1.1       cgd 
   1154   1.1       cgd struct spc {
   1155   1.1       cgd 	cc_t val;
   1156   1.1       cgd 	cc_t *valp;
   1157   1.1       cgd 	char flags;	/* Current flags & level */
   1158   1.1       cgd 	char mylevel;	/* Maximum level & flags */
   1159   1.1       cgd } spc_data[NSLC+1];
   1160   1.1       cgd 
   1161   1.1       cgd #define SLC_IMPORT	0
   1162   1.1       cgd #define	SLC_EXPORT	1
   1163  1.22  christos #define SLC_RVALUE	2
   1164  1.22  christos static int slc_mode = SLC_EXPORT;
   1165   1.1       cgd 
   1166  1.21       wiz void
   1167   1.1       cgd slc_init(void)
   1168   1.1       cgd {
   1169   1.1       cgd 	struct spc *spcp;
   1170   1.1       cgd 
   1171   1.1       cgd 	localchars = 1;
   1172   1.1       cgd 	for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) {
   1173   1.1       cgd 		spcp->val = 0;
   1174   1.1       cgd 		spcp->valp = 0;
   1175   1.1       cgd 		spcp->flags = spcp->mylevel = SLC_NOSUPPORT;
   1176   1.1       cgd 	}
   1177   1.9  christos 
   1178   1.1       cgd #define	initfunc(func, flags) { \
   1179   1.1       cgd 					spcp = &spc_data[func]; \
   1180   1.1       cgd 					if ((spcp->valp = tcval(func)) != NULL){ \
   1181   1.1       cgd 					    spcp->val = *spcp->valp; \
   1182   1.1       cgd 					    spcp->mylevel = SLC_VARIABLE|flags; \
   1183   1.1       cgd 					} else { \
   1184   1.1       cgd 					    spcp->val = 0; \
   1185   1.1       cgd 					    spcp->mylevel = SLC_DEFAULT; \
   1186   1.1       cgd 					} \
   1187   1.1       cgd 				    }
   1188   1.1       cgd 
   1189   1.1       cgd 	initfunc(SLC_SYNCH, 0);
   1190   1.1       cgd 	/* No BRK */
   1191   1.1       cgd 	initfunc(SLC_AO, 0);
   1192   1.1       cgd 	initfunc(SLC_AYT, 0);
   1193   1.1       cgd 	/* No EOR */
   1194   1.1       cgd 	initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT);
   1195   1.1       cgd 	initfunc(SLC_EOF, 0);
   1196   1.1       cgd 	initfunc(SLC_SUSP, SLC_FLUSHIN);
   1197   1.1       cgd 	initfunc(SLC_EC, 0);
   1198   1.1       cgd 	initfunc(SLC_EL, 0);
   1199   1.1       cgd 	initfunc(SLC_EW, 0);
   1200   1.1       cgd 	initfunc(SLC_RP, 0);
   1201   1.1       cgd 	initfunc(SLC_LNEXT, 0);
   1202   1.1       cgd 	initfunc(SLC_XON, 0);
   1203   1.1       cgd 	initfunc(SLC_XOFF, 0);
   1204   1.1       cgd 	initfunc(SLC_FORW1, 0);
   1205   1.1       cgd 	initfunc(SLC_FORW2, 0);
   1206   1.1       cgd 	/* No FORW2 */
   1207   1.1       cgd 
   1208   1.1       cgd 	initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT);
   1209   1.1       cgd #undef	initfunc
   1210   1.1       cgd 
   1211   1.1       cgd 	if (slc_mode == SLC_EXPORT)
   1212   1.1       cgd 		slc_export();
   1213   1.1       cgd 	else
   1214   1.1       cgd 		slc_import(1);
   1215  1.22  christos 
   1216  1.22  christos }
   1217   1.1       cgd 
   1218   1.1       cgd void
   1219   1.1       cgd slcstate(void)
   1220   1.1       cgd {
   1221   1.1       cgd     printf("Special characters are %s values\n",
   1222   1.1       cgd 		slc_mode == SLC_IMPORT ? "remote default" :
   1223   1.1       cgd 		slc_mode == SLC_EXPORT ? "local" :
   1224  1.22  christos 					 "remote");
   1225  1.22  christos }
   1226   1.1       cgd 
   1227   1.1       cgd void
   1228   1.1       cgd slc_mode_export(int n)
   1229   1.1       cgd {
   1230   1.1       cgd     slc_mode = SLC_EXPORT;
   1231   1.1       cgd     if (my_state_is_will(TELOPT_LINEMODE))
   1232  1.22  christos 	slc_export();
   1233  1.22  christos }
   1234   1.1       cgd 
   1235   1.1       cgd void
   1236   1.1       cgd slc_mode_import(int def)
   1237   1.1       cgd {
   1238   1.1       cgd     slc_mode = def ? SLC_IMPORT : SLC_RVALUE;
   1239   1.1       cgd     if (my_state_is_will(TELOPT_LINEMODE))
   1240   1.1       cgd 	slc_import(def);
   1241   1.1       cgd }
   1242   1.1       cgd 
   1243   1.1       cgd unsigned char slc_import_val[] = {
   1244   1.1       cgd 	IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_VARIABLE, 0, IAC, SE
   1245   1.1       cgd };
   1246   1.1       cgd unsigned char slc_import_def[] = {
   1247  1.22  christos 	IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_DEFAULT, 0, IAC, SE
   1248  1.22  christos };
   1249   1.1       cgd 
   1250  1.35  christos void
   1251   1.1       cgd slc_import(int def)
   1252   1.1       cgd {
   1253   1.1       cgd     if ((size_t)NETROOM() > sizeof(slc_import_val)) {
   1254   1.1       cgd 	if (def) {
   1255   1.1       cgd 	    ring_supply_data(&netoring, slc_import_def, sizeof(slc_import_def));
   1256   1.1       cgd 	    printsub('>', &slc_import_def[2], sizeof(slc_import_def)-2);
   1257   1.1       cgd 	} else {
   1258   1.1       cgd 	    ring_supply_data(&netoring, slc_import_val, sizeof(slc_import_val));
   1259   1.1       cgd 	    printsub('>', &slc_import_val[2], sizeof(slc_import_val)-2);
   1260   1.1       cgd 	}
   1261   1.1       cgd     }
   1262  1.22  christos /*@*/ else printf("slc_import: not enough room\n");
   1263  1.22  christos }
   1264   1.1       cgd 
   1265  1.21       wiz void
   1266   1.1       cgd slc_export(void)
   1267   1.1       cgd {
   1268   1.1       cgd     struct spc *spcp;
   1269   1.1       cgd 
   1270   1.1       cgd     TerminalDefaultChars();
   1271   1.1       cgd 
   1272   1.1       cgd     slc_start_reply();
   1273   1.1       cgd     for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
   1274   1.1       cgd 	if (spcp->mylevel != SLC_NOSUPPORT) {
   1275   1.1       cgd 	    if (spcp->val == (cc_t)(_POSIX_VDISABLE))
   1276   1.1       cgd 		spcp->flags = SLC_NOSUPPORT;
   1277   1.1       cgd 	    else
   1278   1.1       cgd 		spcp->flags = spcp->mylevel;
   1279   1.1       cgd 	    if (spcp->valp)
   1280   1.1       cgd 		spcp->val = *spcp->valp;
   1281   1.1       cgd 	    slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
   1282   1.1       cgd 	}
   1283   1.1       cgd     }
   1284   1.1       cgd     slc_end_reply();
   1285   1.1       cgd     (void)slc_update();
   1286  1.22  christos     setconnmode(1);	/* Make sure the character values are set */
   1287  1.22  christos }
   1288   1.1       cgd 
   1289  1.21       wiz void
   1290  1.21       wiz slc(unsigned char *cp, int len)
   1291   1.1       cgd {
   1292   1.1       cgd 	struct spc *spcp;
   1293   1.1       cgd 	int func,level;
   1294   1.1       cgd 
   1295   1.1       cgd 	slc_start_reply();
   1296   1.1       cgd 
   1297   1.1       cgd 	for (; len >= 3; len -=3, cp +=3) {
   1298   1.1       cgd 
   1299   1.1       cgd 		func = cp[SLC_FUNC];
   1300   1.1       cgd 
   1301   1.1       cgd 		if (func == 0) {
   1302   1.1       cgd 			/*
   1303   1.1       cgd 			 * Client side: always ignore 0 function.
   1304   1.1       cgd 			 */
   1305   1.1       cgd 			continue;
   1306   1.1       cgd 		}
   1307   1.1       cgd 		if (func > NSLC) {
   1308   1.1       cgd 			if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT)
   1309   1.1       cgd 				slc_add_reply(func, SLC_NOSUPPORT, 0);
   1310   1.1       cgd 			continue;
   1311   1.1       cgd 		}
   1312   1.1       cgd 
   1313   1.1       cgd 		spcp = &spc_data[func];
   1314   1.1       cgd 
   1315   1.1       cgd 		level = cp[SLC_FLAGS]&(SLC_LEVELBITS|SLC_ACK);
   1316   1.1       cgd 
   1317   1.1       cgd 		if ((cp[SLC_VALUE] == (unsigned char)spcp->val) &&
   1318   1.1       cgd 		    ((level&SLC_LEVELBITS) == (spcp->flags&SLC_LEVELBITS))) {
   1319   1.1       cgd 			continue;
   1320   1.1       cgd 		}
   1321   1.1       cgd 
   1322   1.1       cgd 		if (level == (SLC_DEFAULT|SLC_ACK)) {
   1323   1.1       cgd 			/*
   1324   1.1       cgd 			 * This is an error condition, the SLC_ACK
   1325   1.1       cgd 			 * bit should never be set for the SLC_DEFAULT
   1326   1.1       cgd 			 * level.  Our best guess to recover is to
   1327   1.1       cgd 			 * ignore the SLC_ACK bit.
   1328   1.1       cgd 			 */
   1329   1.1       cgd 			cp[SLC_FLAGS] &= ~SLC_ACK;
   1330   1.1       cgd 		}
   1331   1.1       cgd 
   1332   1.1       cgd 		if (level == ((spcp->flags&SLC_LEVELBITS)|SLC_ACK)) {
   1333   1.1       cgd 			spcp->val = (cc_t)cp[SLC_VALUE];
   1334   1.1       cgd 			spcp->flags = cp[SLC_FLAGS];	/* include SLC_ACK */
   1335   1.1       cgd 			continue;
   1336   1.1       cgd 		}
   1337   1.1       cgd 
   1338   1.1       cgd 		level &= ~SLC_ACK;
   1339   1.1       cgd 
   1340   1.1       cgd 		if (level <= (spcp->mylevel&SLC_LEVELBITS)) {
   1341   1.1       cgd 			spcp->flags = cp[SLC_FLAGS]|SLC_ACK;
   1342   1.1       cgd 			spcp->val = (cc_t)cp[SLC_VALUE];
   1343   1.1       cgd 		}
   1344   1.1       cgd 		if (level == SLC_DEFAULT) {
   1345   1.1       cgd 			if ((spcp->mylevel&SLC_LEVELBITS) != SLC_DEFAULT)
   1346   1.1       cgd 				spcp->flags = spcp->mylevel;
   1347   1.1       cgd 			else
   1348   1.1       cgd 				spcp->flags = SLC_NOSUPPORT;
   1349   1.1       cgd 		}
   1350   1.1       cgd 		slc_add_reply(func, spcp->flags, spcp->val);
   1351   1.1       cgd 	}
   1352   1.1       cgd 	slc_end_reply();
   1353   1.1       cgd 	if (slc_update())
   1354  1.22  christos 		setconnmode(1);	/* set the  new character values */
   1355  1.22  christos }
   1356   1.1       cgd 
   1357  1.21       wiz void
   1358   1.1       cgd slc_check(void)
   1359   1.1       cgd {
   1360   1.1       cgd     struct spc *spcp;
   1361   1.1       cgd 
   1362   1.1       cgd     slc_start_reply();
   1363   1.1       cgd     for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
   1364   1.1       cgd 	if (spcp->valp && spcp->val != *spcp->valp) {
   1365   1.1       cgd 	    spcp->val = *spcp->valp;
   1366   1.1       cgd 	    if (spcp->val == (cc_t)(_POSIX_VDISABLE))
   1367   1.1       cgd 		spcp->flags = SLC_NOSUPPORT;
   1368   1.1       cgd 	    else
   1369   1.1       cgd 		spcp->flags = spcp->mylevel;
   1370   1.1       cgd 	    slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
   1371   1.1       cgd 	}
   1372   1.1       cgd     }
   1373   1.1       cgd     slc_end_reply();
   1374   1.1       cgd     setconnmode(1);
   1375   1.1       cgd }
   1376   1.1       cgd 
   1377   1.1       cgd 
   1378  1.22  christos unsigned char slc_reply[128];
   1379  1.22  christos unsigned char *slc_replyp;
   1380   1.1       cgd 
   1381   1.1       cgd void
   1382   1.1       cgd slc_start_reply(void)
   1383   1.1       cgd {
   1384   1.1       cgd 	slc_replyp = slc_reply;
   1385   1.1       cgd 	*slc_replyp++ = IAC;
   1386   1.1       cgd 	*slc_replyp++ = SB;
   1387   1.1       cgd 	*slc_replyp++ = TELOPT_LINEMODE;
   1388  1.22  christos 	*slc_replyp++ = LM_SLC;
   1389  1.22  christos }
   1390   1.1       cgd 
   1391  1.35  christos void
   1392  1.30  drochner slc_add_reply(unsigned int func, unsigned int flags, cc_t value)
   1393   1.1       cgd {
   1394   1.1       cgd 	if ((size_t)(slc_replyp - slc_reply) + 6 > sizeof(slc_reply))
   1395   1.1       cgd 		return;
   1396   1.1       cgd 	if ((*slc_replyp++ = func) == IAC)
   1397   1.1       cgd 		*slc_replyp++ = IAC;
   1398   1.1       cgd 	if ((*slc_replyp++ = flags) == IAC)
   1399   1.1       cgd 		*slc_replyp++ = IAC;
   1400   1.1       cgd 	if ((*slc_replyp++ = (unsigned char)value) == IAC)
   1401  1.22  christos 		*slc_replyp++ = IAC;
   1402  1.22  christos }
   1403   1.1       cgd 
   1404  1.21       wiz void
   1405   1.1       cgd slc_end_reply(void)
   1406  1.30  drochner {
   1407  1.35  christos     int len;
   1408  1.30  drochner 
   1409   1.1       cgd     len = slc_replyp - slc_reply;
   1410   1.1       cgd     if (len <= 4 || ((size_t)len + 2 > sizeof(slc_reply)))
   1411  1.30  drochner 	return;
   1412   1.1       cgd     *slc_replyp++ = IAC;
   1413   1.1       cgd     *slc_replyp++ = SE;
   1414   1.1       cgd     len += 2;
   1415   1.1       cgd     if (NETROOM() > len) {
   1416   1.1       cgd 	ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
   1417   1.1       cgd 	printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
   1418   1.1       cgd     }
   1419  1.22  christos /*@*/else printf("slc_end_reply: not enough room\n");
   1420  1.22  christos }
   1421   1.1       cgd 
   1422  1.21       wiz int
   1423   1.1       cgd slc_update(void)
   1424   1.1       cgd {
   1425   1.1       cgd 	struct spc *spcp;
   1426   1.1       cgd 	int need_update = 0;
   1427   1.1       cgd 
   1428   1.1       cgd 	for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
   1429   1.1       cgd 		if (!(spcp->flags&SLC_ACK))
   1430   1.1       cgd 			continue;
   1431   1.1       cgd 		spcp->flags &= ~SLC_ACK;
   1432   1.1       cgd 		if (spcp->valp && (*spcp->valp != spcp->val)) {
   1433   1.1       cgd 			*spcp->valp = spcp->val;
   1434   1.1       cgd 			need_update = 1;
   1435   1.1       cgd 		}
   1436   1.1       cgd 	}
   1437   1.3       cgd 	return(need_update);
   1438   1.3       cgd }
   1439   1.3       cgd 
   1440   1.3       cgd #ifdef	OLD_ENVIRON
   1441   1.3       cgd # ifdef	ENV_HACK
   1442   1.3       cgd /*
   1443   1.3       cgd  * Earlier version of telnet/telnetd from the BSD code had
   1444   1.3       cgd  * the definitions of VALUE and VAR reversed.  To ensure
   1445   1.3       cgd  * maximum interoperability, we assume that the server is
   1446   1.3       cgd  * an older BSD server, until proven otherwise.  The newer
   1447   1.3       cgd  * BSD servers should be able to handle either definition,
   1448   1.3       cgd  * so it is better to use the wrong values if we don't
   1449   1.3       cgd  * know what type of server it is.
   1450   1.3       cgd  */
   1451   1.3       cgd int env_auto = 1;
   1452   1.3       cgd int old_env_var = OLD_ENV_VAR;
   1453   1.3       cgd int old_env_value = OLD_ENV_VALUE;
   1454   1.3       cgd # else
   1455   1.3       cgd #  define old_env_var OLD_ENV_VAR
   1456   1.3       cgd #  define old_env_value OLD_ENV_VALUE
   1457  1.22  christos # endif
   1458  1.22  christos #endif
   1459   1.1       cgd 
   1460  1.21       wiz void
   1461  1.21       wiz env_opt(unsigned char *buf, int len)
   1462   1.1       cgd {
   1463   1.1       cgd 	unsigned char *ep = 0, *epc = 0;
   1464   1.1       cgd 	int i;
   1465   1.1       cgd 
   1466   1.1       cgd 	switch(buf[0]&0xff) {
   1467   1.1       cgd 	case TELQUAL_SEND:
   1468   1.1       cgd 		env_opt_start();
   1469   1.1       cgd 		if (len == 1) {
   1470   1.3       cgd 			env_opt_add(NULL);
   1471   1.3       cgd 		} else for (i = 1; i < len; i++) {
   1472   1.3       cgd 			switch (buf[i]&0xff) {
   1473   1.3       cgd #ifdef	OLD_ENVIRON
   1474   1.3       cgd 			case OLD_ENV_VAR:
   1475   1.3       cgd # ifdef	ENV_HACK
   1476   1.3       cgd 				if (telopt_environ == TELOPT_OLD_ENVIRON
   1477   1.3       cgd 				    && env_auto) {
   1478   1.3       cgd 					/* Server has the same definitions */
   1479   1.3       cgd 					old_env_var = OLD_ENV_VAR;
   1480   1.3       cgd 					old_env_value = OLD_ENV_VALUE;
   1481   1.3       cgd 				}
   1482   1.3       cgd 				/* FALL THROUGH */
   1483   1.3       cgd # endif
   1484   1.3       cgd 			case OLD_ENV_VALUE:
   1485   1.3       cgd 				/*
   1486   1.3       cgd 				 * Although OLD_ENV_VALUE is not legal, we will
   1487   1.3       cgd 				 * still recognize it, just in case it is an
   1488   1.3       cgd 				 * old server that has VAR & VALUE mixed up...
   1489   1.3       cgd 				 */
   1490   1.3       cgd 				/* FALL THROUGH */
   1491   1.3       cgd #else
   1492   1.1       cgd 			case NEW_ENV_VAR:
   1493   1.1       cgd #endif
   1494   1.1       cgd 			case ENV_USERVAR:
   1495   1.1       cgd 				if (ep) {
   1496   1.1       cgd 					*epc = 0;
   1497   1.1       cgd 					env_opt_add(ep);
   1498   1.1       cgd 				}
   1499   1.1       cgd 				ep = epc = &buf[i+1];
   1500   1.1       cgd 				break;
   1501   1.1       cgd 			case ENV_ESC:
   1502   1.1       cgd 				i++;
   1503   1.1       cgd 				/*FALL THROUGH*/
   1504   1.1       cgd 			default:
   1505   1.1       cgd 				if (epc)
   1506   1.3       cgd 					*epc++ = buf[i];
   1507   1.3       cgd 				break;
   1508   1.3       cgd 			}
   1509   1.3       cgd 		}
   1510   1.1       cgd 		if (ep) {
   1511   1.1       cgd 			*epc = 0;
   1512   1.1       cgd 			env_opt_add(ep);
   1513   1.1       cgd 		}
   1514   1.1       cgd 		env_opt_end(1);
   1515   1.1       cgd 		break;
   1516   1.1       cgd 
   1517   1.1       cgd 	case TELQUAL_IS:
   1518   1.1       cgd 	case TELQUAL_INFO:
   1519   1.1       cgd 		/* Ignore for now.  We shouldn't get it anyway. */
   1520   1.1       cgd 		break;
   1521   1.1       cgd 
   1522   1.1       cgd 	default:
   1523   1.1       cgd 		break;
   1524   1.1       cgd 	}
   1525   1.1       cgd }
   1526   1.1       cgd 
   1527   1.1       cgd #define	OPT_REPLY_SIZE	256
   1528   1.1       cgd unsigned char *opt_reply;
   1529  1.22  christos unsigned char *opt_replyp;
   1530  1.22  christos unsigned char *opt_replyend;
   1531   1.1       cgd 
   1532  1.14    itojun void
   1533  1.14    itojun env_opt_start(void)
   1534  1.14    itojun {
   1535  1.14    itojun 	unsigned char *p;
   1536  1.14    itojun 
   1537  1.14    itojun 	if (opt_reply) {
   1538  1.14    itojun 		p = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
   1539  1.14    itojun 		if (p == NULL)
   1540  1.14    itojun 			free(opt_reply);
   1541   1.1       cgd 	} else
   1542   1.1       cgd 		p = (unsigned char *)malloc(OPT_REPLY_SIZE);
   1543   1.1       cgd 	opt_reply = p;
   1544   1.1       cgd 	if (opt_reply == NULL) {
   1545   1.1       cgd /*@*/		printf("env_opt_start: malloc()/realloc() failed!!!\n");
   1546   1.1       cgd 		opt_reply = opt_replyp = opt_replyend = NULL;
   1547   1.1       cgd 		return;
   1548   1.1       cgd 	}
   1549   1.1       cgd 	opt_replyp = opt_reply;
   1550   1.3       cgd 	opt_replyend = opt_reply + OPT_REPLY_SIZE;
   1551   1.1       cgd 	*opt_replyp++ = IAC;
   1552   1.1       cgd 	*opt_replyp++ = SB;
   1553   1.1       cgd 	*opt_replyp++ = telopt_environ;
   1554  1.22  christos 	*opt_replyp++ = TELQUAL_IS;
   1555  1.22  christos }
   1556   1.1       cgd 
   1557   1.1       cgd void
   1558   1.1       cgd env_opt_start_info(void)
   1559   1.1       cgd {
   1560   1.1       cgd 	env_opt_start();
   1561   1.1       cgd 	if (opt_replyp)
   1562  1.22  christos 	    opt_replyp[-1] = TELQUAL_INFO;
   1563  1.22  christos }
   1564   1.1       cgd 
   1565  1.21       wiz void
   1566  1.30  drochner env_opt_add(unsigned char *ep)
   1567   1.1       cgd {
   1568   1.1       cgd 	unsigned char *vp, c;
   1569   1.1       cgd 	unsigned int len, olen, elen;
   1570   1.1       cgd 
   1571   1.1       cgd 	if (opt_reply == NULL)		/*XXX*/
   1572   1.3       cgd 		return;			/*XXX*/
   1573   1.3       cgd 
   1574   1.9  christos 	if (ep == NULL || *ep == '\0') {
   1575   1.3       cgd 		/* Send user defined variables first. */
   1576   1.3       cgd 		env_default(1, 0);
   1577   1.3       cgd 		while ((ep = env_default(0, 0)) != NULL)
   1578   1.3       cgd 			env_opt_add(ep);
   1579   1.9  christos 
   1580   1.1       cgd 		/* Now add the list of well know variables.  */
   1581   1.1       cgd 		env_default(1, 1);
   1582   1.1       cgd 		while ((ep = env_default(0, 1)) != NULL)
   1583   1.1       cgd 			env_opt_add(ep);
   1584  1.30  drochner 		return;
   1585  1.30  drochner 	}
   1586  1.36  christos 	vp = env_getvalue(ep);
   1587   1.1       cgd 	elen = 2 * (vp ? strlen((char *)vp) : 0) +
   1588  1.14    itojun 		2 * strlen((char *)ep) + 6;
   1589  1.30  drochner 	if ((unsigned int)(opt_replyend - opt_replyp) < elen)
   1590  1.30  drochner 	{
   1591  1.14    itojun 		unsigned char *p;
   1592  1.14    itojun 		len = opt_replyend - opt_reply + elen;
   1593  1.14    itojun 		olen = opt_replyp - opt_reply;
   1594  1.14    itojun 		p = (unsigned char *)realloc(opt_reply, len);
   1595   1.1       cgd 		if (p == NULL)
   1596   1.1       cgd 			free(opt_reply);
   1597   1.1       cgd 		opt_reply = p;
   1598   1.1       cgd 		if (opt_reply == NULL) {
   1599   1.1       cgd /*@*/			printf("env_opt_add: realloc() failed!!!\n");
   1600  1.30  drochner 			opt_reply = opt_replyp = opt_replyend = NULL;
   1601   1.1       cgd 			return;
   1602   1.1       cgd 		}
   1603   1.3       cgd 		opt_replyp = opt_reply + olen;
   1604   1.3       cgd 		opt_replyend = opt_reply + len;
   1605   1.3       cgd 	}
   1606   1.3       cgd 	if (opt_welldefined(ep))
   1607   1.3       cgd #ifdef	OLD_ENVIRON
   1608   1.3       cgd 		if (telopt_environ == TELOPT_OLD_ENVIRON)
   1609   1.3       cgd 			*opt_replyp++ = old_env_var;
   1610   1.3       cgd 		else
   1611   1.3       cgd #endif
   1612   1.1       cgd 			*opt_replyp++ = NEW_ENV_VAR;
   1613   1.9  christos 	else
   1614   1.1       cgd 		*opt_replyp++ = ENV_USERVAR;
   1615   1.1       cgd 	for (;;) {
   1616   1.1       cgd 		while ((c = *ep++) != '\0') {
   1617   1.1       cgd 			switch(c&0xff) {
   1618   1.3       cgd 			case IAC:
   1619   1.3       cgd 				*opt_replyp++ = IAC;
   1620   1.1       cgd 				break;
   1621   1.3       cgd 			case NEW_ENV_VAR:
   1622   1.1       cgd 			case NEW_ENV_VALUE:
   1623   1.1       cgd 			case ENV_ESC:
   1624   1.1       cgd 			case ENV_USERVAR:
   1625   1.1       cgd 				*opt_replyp++ = ENV_ESC;
   1626   1.1       cgd 				break;
   1627   1.9  christos 			}
   1628   1.3       cgd 			*opt_replyp++ = c;
   1629   1.3       cgd 		}
   1630   1.3       cgd 		if ((ep = vp) != NULL) {
   1631   1.3       cgd #ifdef	OLD_ENVIRON
   1632   1.3       cgd 			if (telopt_environ == TELOPT_OLD_ENVIRON)
   1633   1.3       cgd 				*opt_replyp++ = old_env_value;
   1634   1.1       cgd 			else
   1635   1.1       cgd #endif
   1636   1.1       cgd 				*opt_replyp++ = NEW_ENV_VALUE;
   1637   1.1       cgd 			vp = NULL;
   1638   1.1       cgd 		} else
   1639   1.1       cgd 			break;
   1640  1.22  christos 	}
   1641  1.35  christos }
   1642   1.3       cgd 
   1643   1.3       cgd int
   1644   1.3       cgd opt_welldefined(const char *ep)
   1645   1.3       cgd {
   1646   1.3       cgd 	if ((strcmp(ep, "USER") == 0) ||
   1647   1.3       cgd 	    (strcmp(ep, "DISPLAY") == 0) ||
   1648   1.3       cgd 	    (strcmp(ep, "PRINTER") == 0) ||
   1649   1.3       cgd 	    (strcmp(ep, "SYSTEMTYPE") == 0) ||
   1650   1.3       cgd 	    (strcmp(ep, "JOB") == 0) ||
   1651   1.3       cgd 	    (strcmp(ep, "ACCT") == 0))
   1652  1.22  christos 		return(1);
   1653  1.22  christos 	return(0);
   1654   1.1       cgd }
   1655  1.21       wiz void
   1656   1.1       cgd env_opt_end(int emptyok)
   1657   1.1       cgd {
   1658   1.1       cgd 	int len;
   1659   1.1       cgd 
   1660   1.1       cgd 	len = opt_replyp - opt_reply + 2;
   1661   1.1       cgd 	if (emptyok || len > 6) {
   1662   1.1       cgd 		*opt_replyp++ = IAC;
   1663   1.1       cgd 		*opt_replyp++ = SE;
   1664   1.1       cgd 		if (NETROOM() > len) {
   1665   1.1       cgd 			ring_supply_data(&netoring, opt_reply, len);
   1666   1.1       cgd 			printsub('>', &opt_reply[2], len - 2);
   1667   1.1       cgd 		}
   1668   1.1       cgd /*@*/		else printf("slc_end_reply: not enough room\n");
   1669   1.1       cgd 	}
   1670   1.1       cgd 	if (opt_reply) {
   1671   1.1       cgd 		free(opt_reply);
   1672   1.1       cgd 		opt_reply = opt_replyp = opt_replyend = NULL;
   1673   1.1       cgd 	}
   1674   1.1       cgd }
   1675  1.22  christos 
   1676  1.22  christos 
   1677   1.1       cgd 
   1679  1.21       wiz int
   1680  1.21       wiz telrcv(void)
   1681   1.1       cgd {
   1682   1.1       cgd     int c;
   1683   1.1       cgd     int scc;
   1684   1.1       cgd     unsigned char *sbp = NULL;
   1685   1.1       cgd     int count;
   1686   1.1       cgd     int returnValue = 0;
   1687   1.1       cgd 
   1688   1.1       cgd     scc = 0;
   1689   1.1       cgd     count = 0;
   1690   1.1       cgd     while (TTYROOM() > 2) {
   1691   1.1       cgd 	if (scc == 0) {
   1692   1.1       cgd 	    if (count) {
   1693   1.1       cgd 		ring_consumed(&netiring, count);
   1694   1.1       cgd 		returnValue = 1;
   1695   1.1       cgd 		count = 0;
   1696   1.1       cgd 	    }
   1697   1.1       cgd 	    sbp = netiring.consume;
   1698   1.1       cgd 	    scc = ring_full_consecutive(&netiring);
   1699   1.1       cgd 	    if (scc == 0) {
   1700   1.1       cgd 		/* No more data coming in */
   1701   1.1       cgd 		break;
   1702  1.16   thorpej 	    }
   1703  1.16   thorpej 	}
   1704  1.16   thorpej 
   1705  1.16   thorpej 	c = *sbp++ & 0xff, scc--; count++;
   1706   1.1       cgd #ifdef	ENCRYPTION
   1707   1.1       cgd 	if (decrypt_input)
   1708   1.1       cgd 		c = (*decrypt_input)(c);
   1709   1.1       cgd #endif	/* ENCRYPTION */
   1710   1.1       cgd 
   1711   1.1       cgd 	switch (telrcv_state) {
   1712   1.1       cgd 
   1713   1.1       cgd 	case TS_CR:
   1714   1.1       cgd 	    telrcv_state = TS_DATA;
   1715   1.1       cgd 	    if (c == '\0') {
   1716   1.1       cgd 		break;	/* Ignore \0 after CR */
   1717   1.1       cgd 	    }
   1718   1.1       cgd 	    else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
   1719   1.1       cgd 		TTYADD(c);
   1720   1.1       cgd 		break;
   1721   1.1       cgd 	    }
   1722   1.1       cgd 	    /* Else, fall through */
   1723   1.1       cgd 
   1724   1.1       cgd 	case TS_DATA:
   1725   1.1       cgd 	    if (c == IAC) {
   1726   1.1       cgd 		telrcv_state = TS_IAC;
   1727   1.1       cgd 		break;
   1728   1.1       cgd 	    }
   1729   1.1       cgd 		    /*
   1730   1.1       cgd 		     * The 'crmod' hack (see following) is needed
   1731   1.1       cgd 		     * since we can't * set CRMOD on output only.
   1732   1.1       cgd 		     * Machines like MULTICS like to send \r without
   1733   1.1       cgd 		     * \n; since we must turn off CRMOD to get proper
   1734   1.1       cgd 		     * input, the mapping is done here (sigh).
   1735  1.16   thorpej 		     */
   1736  1.16   thorpej 	    if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) {
   1737  1.16   thorpej 		if (scc > 0) {
   1738  1.16   thorpej 		    c = *sbp&0xff;
   1739   1.1       cgd #ifdef	ENCRYPTION
   1740   1.1       cgd 		    if (decrypt_input)
   1741   1.1       cgd 			c = (*decrypt_input)(c);
   1742   1.1       cgd #endif	/* ENCRYPTION */
   1743   1.1       cgd 		    if (c == 0) {
   1744   1.1       cgd 			sbp++, scc--; count++;
   1745   1.1       cgd 			/* a "true" CR */
   1746   1.1       cgd 			TTYADD('\r');
   1747   1.1       cgd 		    } else if (my_want_state_is_dont(TELOPT_ECHO) &&
   1748  1.16   thorpej 					(c == '\n')) {
   1749  1.16   thorpej 			sbp++, scc--; count++;
   1750  1.16   thorpej 			TTYADD('\n');
   1751  1.16   thorpej 		    } else {
   1752   1.1       cgd #ifdef	ENCRYPTION
   1753   1.1       cgd 			if (decrypt_input)
   1754   1.1       cgd 			    (*decrypt_input)(-1);
   1755   1.1       cgd #endif	/* ENCRYPTION */
   1756   1.1       cgd 
   1757   1.1       cgd 			TTYADD('\r');
   1758   1.1       cgd 			if (crmod) {
   1759   1.1       cgd 				TTYADD('\n');
   1760   1.1       cgd 			}
   1761   1.1       cgd 		    }
   1762   1.1       cgd 		} else {
   1763   1.1       cgd 		    telrcv_state = TS_CR;
   1764   1.1       cgd 		    TTYADD('\r');
   1765   1.1       cgd 		    if (crmod) {
   1766   1.1       cgd 			    TTYADD('\n');
   1767   1.1       cgd 		    }
   1768   1.1       cgd 		}
   1769   1.1       cgd 	    } else {
   1770   1.1       cgd 		TTYADD(c);
   1771   1.1       cgd 	    }
   1772   1.1       cgd 	    continue;
   1773   1.5       jtk 
   1774   1.1       cgd 	case TS_IAC:
   1775   1.1       cgd process_iac:
   1776   1.1       cgd 	    switch (c) {
   1777   1.1       cgd 
   1778   1.1       cgd 	    case WILL:
   1779   1.1       cgd 		telrcv_state = TS_WILL;
   1780   1.1       cgd 		continue;
   1781   1.1       cgd 
   1782   1.1       cgd 	    case WONT:
   1783   1.1       cgd 		telrcv_state = TS_WONT;
   1784   1.1       cgd 		continue;
   1785   1.1       cgd 
   1786   1.1       cgd 	    case DO:
   1787   1.1       cgd 		telrcv_state = TS_DO;
   1788   1.1       cgd 		continue;
   1789   1.1       cgd 
   1790   1.1       cgd 	    case DONT:
   1791   1.1       cgd 		telrcv_state = TS_DONT;
   1792   1.1       cgd 		continue;
   1793   1.1       cgd 
   1794   1.1       cgd 	    case DM:
   1795   1.1       cgd 		    /*
   1796   1.1       cgd 		     * We may have missed an urgent notification,
   1797   1.1       cgd 		     * so make sure we flush whatever is in the
   1798   1.1       cgd 		     * buffer currently.
   1799   1.1       cgd 		     */
   1800   1.1       cgd 		printoption("RCVD", IAC, DM);
   1801   1.1       cgd 		SYNCHing = 1;
   1802   1.1       cgd 		(void) ttyflush(1);
   1803   1.1       cgd 		SYNCHing = stilloob();
   1804   1.1       cgd 		settimer(gotDM);
   1805   1.1       cgd 		break;
   1806   1.1       cgd 
   1807   1.1       cgd 	    case SB:
   1808   1.1       cgd 		SB_CLEAR();
   1809   1.1       cgd 		telrcv_state = TS_SB;
   1810   1.1       cgd 		continue;
   1811   1.1       cgd 
   1812   1.1       cgd 
   1813   1.1       cgd 	    case IAC:
   1814   1.1       cgd 		TTYADD(IAC);
   1815   1.1       cgd 		break;
   1816   1.1       cgd 
   1817   1.1       cgd 	    case NOP:
   1818   1.1       cgd 	    case GA:
   1819   1.1       cgd 	    default:
   1820   1.1       cgd 		printoption("RCVD", IAC, c);
   1821   1.1       cgd 		break;
   1822   1.1       cgd 	    }
   1823   1.1       cgd 	    telrcv_state = TS_DATA;
   1824   1.1       cgd 	    continue;
   1825   1.1       cgd 
   1826   1.1       cgd 	case TS_WILL:
   1827   1.1       cgd 	    printoption("RCVD", WILL, c);
   1828   1.1       cgd 	    willoption(c);
   1829   1.1       cgd 	    telrcv_state = TS_DATA;
   1830   1.1       cgd 	    continue;
   1831   1.1       cgd 
   1832   1.1       cgd 	case TS_WONT:
   1833   1.1       cgd 	    printoption("RCVD", WONT, c);
   1834   1.1       cgd 	    wontoption(c);
   1835   1.1       cgd 	    telrcv_state = TS_DATA;
   1836   1.1       cgd 	    continue;
   1837   1.1       cgd 
   1838   1.1       cgd 	case TS_DO:
   1839   1.1       cgd 	    printoption("RCVD", DO, c);
   1840   1.1       cgd 	    dooption(c);
   1841   1.1       cgd 	    if (c == TELOPT_NAWS) {
   1842   1.1       cgd 		sendnaws();
   1843   1.1       cgd 	    } else if (c == TELOPT_LFLOW) {
   1844   1.1       cgd 		localflow = 1;
   1845   1.1       cgd 		setcommandmode();
   1846   1.1       cgd 		setconnmode(0);
   1847   1.1       cgd 	    }
   1848   1.1       cgd 	    telrcv_state = TS_DATA;
   1849   1.1       cgd 	    continue;
   1850   1.1       cgd 
   1851   1.1       cgd 	case TS_DONT:
   1852   1.1       cgd 	    printoption("RCVD", DONT, c);
   1853   1.1       cgd 	    dontoption(c);
   1854   1.1       cgd 	    flushline = 1;
   1855   1.1       cgd 	    setconnmode(0);	/* set new tty mode (maybe) */
   1856   1.1       cgd 	    telrcv_state = TS_DATA;
   1857   1.1       cgd 	    continue;
   1858   1.1       cgd 
   1859   1.1       cgd 	case TS_SB:
   1860   1.1       cgd 	    if (c == IAC) {
   1861   1.1       cgd 		telrcv_state = TS_SE;
   1862   1.1       cgd 	    } else {
   1863   1.1       cgd 		SB_ACCUM(c);
   1864   1.1       cgd 	    }
   1865   1.1       cgd 	    continue;
   1866   1.1       cgd 
   1867   1.1       cgd 	case TS_SE:
   1868   1.1       cgd 	    if (c != SE) {
   1869  1.27       wiz 		if (c != IAC) {
   1870   1.1       cgd 		    /*
   1871   1.1       cgd 		     * This is an error.  We only expect to get
   1872   1.1       cgd 		     * "IAC IAC" or "IAC SE".  Several things may
   1873   1.1       cgd 		     * have happened.  An IAC was not doubled, the
   1874  1.20       wiz 		     * IAC SE was left off, or another option got
   1875   1.1       cgd 		     * inserted into the suboption are all possibilities.
   1876   1.1       cgd 		     * If we assume that the IAC was not doubled,
   1877   1.1       cgd 		     * and really the IAC SE was left off, we could
   1878   1.1       cgd 		     * get into an infinite loop here.  So, instead,
   1879   1.1       cgd 		     * we terminate the suboption, and process the
   1880   1.1       cgd 		     * partial suboption if we can.
   1881   1.1       cgd 		     */
   1882   1.1       cgd 		    SB_ACCUM(IAC);
   1883   1.1       cgd 		    SB_ACCUM(c);
   1884   1.1       cgd 		    subpointer -= 2;
   1885   1.1       cgd 		    SB_TERM();
   1886   1.1       cgd 
   1887   1.1       cgd 		    printoption("In SUBOPTION processing, RCVD", IAC, c);
   1888   1.1       cgd 		    suboption();	/* handle sub-option */
   1889   1.1       cgd 		    telrcv_state = TS_IAC;
   1890   1.1       cgd 		    goto process_iac;
   1891   1.1       cgd 		}
   1892   1.1       cgd 		SB_ACCUM(c);
   1893   1.1       cgd 		telrcv_state = TS_SB;
   1894   1.1       cgd 	    } else {
   1895   1.1       cgd 		SB_ACCUM(IAC);
   1896   1.1       cgd 		SB_ACCUM(SE);
   1897   1.1       cgd 		subpointer -= 2;
   1898   1.1       cgd 		SB_TERM();
   1899   1.1       cgd 		suboption();	/* handle sub-option */
   1900   1.1       cgd 		telrcv_state = TS_DATA;
   1901   1.1       cgd 	    }
   1902   1.1       cgd 	}
   1903   1.1       cgd     }
   1904   1.1       cgd     if (count)
   1905   1.1       cgd 	ring_consumed(&netiring, count);
   1906   1.1       cgd     return returnValue||count;
   1907  1.22  christos }
   1908  1.22  christos 
   1909   1.1       cgd static int bol = 1, local = 0;
   1910   1.1       cgd 
   1911   1.1       cgd int
   1912   1.1       cgd rlogin_susp(void)
   1913   1.1       cgd {
   1914   1.1       cgd     if (local) {
   1915   1.1       cgd 	local = 0;
   1916   1.1       cgd 	bol = 1;
   1917   1.1       cgd 	command(0, "z\n", 2);
   1918   1.1       cgd 	return(1);
   1919  1.22  christos     }
   1920  1.22  christos     return(0);
   1921   1.1       cgd }
   1922   1.1       cgd 
   1923   1.1       cgd static int
   1924   1.1       cgd telsnd(void)
   1925   1.9  christos {
   1926   1.1       cgd     int tcc;
   1927   1.1       cgd     int count;
   1928   1.1       cgd     int returnValue = 0;
   1929   1.1       cgd     unsigned char *tbp = NULL;
   1930  1.21       wiz 
   1931  1.21       wiz     tcc = 0;
   1932   1.1       cgd     count = 0;
   1933   1.1       cgd     while (NETROOM() > 2) {
   1934   1.1       cgd 	int sc;
   1935   1.1       cgd 	int c;
   1936   1.1       cgd 
   1937   1.1       cgd 	if (tcc == 0) {
   1938   1.1       cgd 	    if (count) {
   1939   1.1       cgd 		ring_consumed(&ttyiring, count);
   1940   1.1       cgd 		returnValue = 1;
   1941   1.1       cgd 		count = 0;
   1942   1.1       cgd 	    }
   1943   1.1       cgd 	    tbp = ttyiring.consume;
   1944   1.1       cgd 	    tcc = ring_full_consecutive(&ttyiring);
   1945   1.1       cgd 	    if (tcc == 0) {
   1946   1.1       cgd 		break;
   1947   1.1       cgd 	    }
   1948   1.1       cgd 	}
   1949   1.1       cgd 	c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
   1950   1.1       cgd 	if (rlogin != _POSIX_VDISABLE) {
   1951   1.1       cgd 		if (bol) {
   1952   1.1       cgd 			bol = 0;
   1953   1.1       cgd 			if (sc == rlogin) {
   1954   1.1       cgd 				local = 1;
   1955   1.1       cgd 				continue;
   1956   1.1       cgd 			}
   1957   1.1       cgd 		} else if (local) {
   1958   1.1       cgd 			local = 0;
   1959   1.1       cgd 			if (sc == '.' || c == termEofChar) {
   1960   1.1       cgd 				bol = 1;
   1961   1.1       cgd 				command(0, "close\n", 6);
   1962   1.1       cgd 				continue;
   1963   1.1       cgd 			}
   1964   1.1       cgd 			if (sc == termSuspChar) {
   1965   1.1       cgd 				bol = 1;
   1966   1.1       cgd 				command(0, "z\n", 2);
   1967   1.1       cgd 				continue;
   1968   1.1       cgd 			}
   1969   1.1       cgd 			if (sc == escape) {
   1970   1.1       cgd 				command(0, (char *)tbp, tcc);
   1971   1.1       cgd 				bol = 1;
   1972   1.1       cgd 				count += tcc;
   1973   1.1       cgd 				tcc = 0;
   1974   1.1       cgd 				flushline = 1;
   1975   1.1       cgd 				break;
   1976   1.1       cgd 			}
   1977   1.1       cgd 			if (sc != rlogin) {
   1978   1.1       cgd 				++tcc;
   1979   1.1       cgd 				--tbp;
   1980   1.1       cgd 				--count;
   1981   1.1       cgd 				c = sc = rlogin;
   1982  1.13       abs 			}
   1983   1.1       cgd 		}
   1984   1.1       cgd 		if ((sc == '\n') || (sc == '\r'))
   1985   1.1       cgd 			bol = 1;
   1986   1.1       cgd 	} else if (sc == escape && escape != _POSIX_VDISABLE) {
   1987   1.1       cgd 	    /*
   1988   1.1       cgd 	     * Double escape is a pass through of a single escape character.
   1989   1.1       cgd 	     */
   1990   1.1       cgd 	    if (tcc && strip(*tbp) == escape) {
   1991   1.1       cgd 		tbp++;
   1992   1.1       cgd 		tcc--;
   1993   1.1       cgd 		count++;
   1994   1.1       cgd 		bol = 0;
   1995   1.1       cgd 	    } else {
   1996   1.1       cgd 		command(0, (char *)tbp, tcc);
   1997   1.1       cgd 		bol = 1;
   1998   1.1       cgd 		count += tcc;
   1999   1.1       cgd 		tcc = 0;
   2000   1.1       cgd 		flushline = 1;
   2001   1.1       cgd 		break;
   2002   1.1       cgd 	    }
   2003   1.1       cgd 	} else
   2004   1.1       cgd 	    bol = 0;
   2005   1.1       cgd #ifdef	KLUDGELINEMODE
   2006   1.1       cgd 	if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
   2007   1.1       cgd 	    if (tcc > 0 && strip(*tbp) == echoc) {
   2008   1.1       cgd 		tcc--; tbp++; count++;
   2009   1.1       cgd 	    } else {
   2010   1.1       cgd 		dontlecho = !dontlecho;
   2011   1.1       cgd 		settimer(echotoggle);
   2012   1.1       cgd 		setconnmode(0);
   2013   1.1       cgd 		flushline = 1;
   2014   1.8   mycroft 		break;
   2015   1.1       cgd 	    }
   2016   1.1       cgd 	}
   2017   1.1       cgd #endif
   2018   1.1       cgd 	if (sc != _POSIX_VDISABLE && MODE_LOCAL_CHARS(globalmode)) {
   2019   1.1       cgd 	    if (TerminalSpecialChars(sc) == 0) {
   2020   1.1       cgd 		bol = 1;
   2021   1.1       cgd 		break;
   2022   1.1       cgd 	    }
   2023   1.1       cgd 	}
   2024   1.1       cgd 	if (my_want_state_is_wont(TELOPT_BINARY)) {
   2025   1.1       cgd 	    switch (c) {
   2026   1.1       cgd 	    case '\n':
   2027   1.1       cgd 		    /*
   2028   1.1       cgd 		     * If we are in CRMOD mode (\r ==> \n)
   2029   1.1       cgd 		     * on our local machine, then probably
   2030   1.1       cgd 		     * a newline (unix) is CRLF (TELNET).
   2031   1.1       cgd 		     */
   2032   1.1       cgd 		if (MODE_LOCAL_CHARS(globalmode)) {
   2033   1.1       cgd 		    NETADD('\r');
   2034   1.1       cgd 		}
   2035   1.1       cgd 		NETADD('\n');
   2036   1.1       cgd 		bol = flushline = 1;
   2037   1.1       cgd 		break;
   2038   1.1       cgd 	    case '\r':
   2039   1.1       cgd 		if (!crlf) {
   2040   1.1       cgd 		    NET2ADD('\r', '\0');
   2041   1.1       cgd 		} else {
   2042   1.1       cgd 		    NET2ADD('\r', '\n');
   2043   1.1       cgd 		}
   2044   1.1       cgd 		bol = flushline = 1;
   2045   1.1       cgd 		break;
   2046   1.1       cgd 	    case IAC:
   2047   1.1       cgd 		NET2ADD(IAC, IAC);
   2048   1.1       cgd 		break;
   2049   1.1       cgd 	    default:
   2050   1.1       cgd 		NETADD(c);
   2051   1.1       cgd 		break;
   2052   1.1       cgd 	    }
   2053   1.1       cgd 	} else if (c == IAC) {
   2054   1.1       cgd 	    NET2ADD(IAC, IAC);
   2055   1.1       cgd 	} else {
   2056   1.1       cgd 	    NETADD(c);
   2057   1.1       cgd 	}
   2058   1.1       cgd     }
   2059   1.1       cgd     if (count)
   2060   1.1       cgd 	ring_consumed(&ttyiring, count);
   2061   1.1       cgd     return returnValue||count;		/* Non-zero if we did anything */
   2062   1.1       cgd }
   2063   1.1       cgd 
   2064   1.1       cgd /*
   2066   1.1       cgd  * Scheduler()
   2067   1.1       cgd  *
   2068   1.1       cgd  * Try to do something.
   2069   1.1       cgd  *
   2070  1.22  christos  * If we do something useful, return 1; else return 0.
   2071  1.22  christos  *
   2072   1.1       cgd  */
   2073   1.1       cgd 
   2074   1.1       cgd 
   2075   1.1       cgd int
   2076   1.1       cgd Scheduler(int block)			/* should we block in the select ? */
   2077   1.1       cgd {
   2078   1.1       cgd 		/* One wants to be a bit careful about setting returnValue
   2079   1.1       cgd 		 * to one, since a one implies we did some useful work,
   2080   1.1       cgd 		 * and therefore probably won't be called to block next
   2081   1.1       cgd 		 * time (TN3270 mode only).
   2082   1.1       cgd 		 */
   2083   1.1       cgd     int returnValue;
   2084   1.1       cgd     int netin, netout, netex, ttyin, ttyout;
   2085   1.1       cgd 
   2086   1.1       cgd     /* Decide which rings should be processed */
   2087   1.1       cgd 
   2088   1.1       cgd     netout = ring_full_count(&netoring) &&
   2089   1.1       cgd 	    (flushline ||
   2090   1.1       cgd 		(my_want_state_is_wont(TELOPT_LINEMODE)
   2091   1.1       cgd #ifdef	KLUDGELINEMODE
   2092   1.1       cgd 			&& (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
   2093   1.6       jtk #endif
   2094   1.1       cgd 		) ||
   2095   1.1       cgd 			my_want_state_is_will(TELOPT_BINARY));
   2096   1.1       cgd     ttyout = ring_full_count(&ttyoring);
   2097   1.1       cgd 
   2098   1.1       cgd     ttyin = ring_empty_count(&ttyiring) && (clienteof == 0);
   2099   1.1       cgd 
   2100   1.1       cgd     netin = !ISend && ring_empty_count(&netiring);
   2101   1.1       cgd 
   2102   1.1       cgd     netex = !SYNCHing;
   2103   1.1       cgd 
   2104   1.1       cgd     /* If we have seen a signal recently, reset things */
   2105   1.1       cgd 
   2106   1.1       cgd     /* Call to system code to process rings */
   2107   1.1       cgd 
   2108   1.1       cgd     returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block);
   2109   1.1       cgd 
   2110   1.1       cgd     /* Now, look at the input rings, looking for work to do. */
   2111   1.1       cgd 
   2112   1.1       cgd     if (ring_full_count(&ttyiring)) {
   2113   1.1       cgd 	    returnValue |= telsnd();
   2114   1.1       cgd     }
   2115   1.1       cgd 
   2116   1.1       cgd     if (ring_full_count(&netiring)) {
   2117   1.1       cgd 	returnValue |= telrcv();
   2118   1.1       cgd     }
   2119   1.1       cgd     return returnValue;
   2120  1.22  christos }
   2121  1.22  christos 
   2122   1.1       cgd /*
   2124   1.1       cgd  * Select from tty and network...
   2125  1.16   thorpej  */
   2126   1.1       cgd void
   2127  1.10       mrg telnet(const char *user)
   2128   1.1       cgd {
   2129   1.1       cgd     sys_telnet_init();
   2130   1.3       cgd 
   2131   1.1       cgd #if	defined(AUTHENTICATION) || defined(ENCRYPTION)
   2132   1.1       cgd     {
   2133   1.1       cgd 	static char local_host[MAXHOSTNAMELEN + 1] = { 0 };
   2134   1.1       cgd 
   2135   1.1       cgd 	if (!local_host[0]) {
   2136  1.16   thorpej 		gethostname(local_host, sizeof(local_host));
   2137   1.1       cgd 		local_host[sizeof(local_host)-1] = 0;
   2138  1.24    itojun 	}
   2139   1.1       cgd 	auth_encrypt_init(local_host, hostname, "TELNET", 0);
   2140   1.1       cgd 	auth_encrypt_user(user);
   2141   1.1       cgd     }
   2142  1.16   thorpej #endif	/* defined(AUTHENTICATION) || defined(ENCRYPTION) */
   2143  1.16   thorpej     if (telnetport) {
   2144  1.16   thorpej #ifdef AUTHENTICATION
   2145  1.16   thorpej 	if (autologin)
   2146   1.1       cgd 		send_will(TELOPT_AUTHENTICATION, 1);
   2147   1.1       cgd #endif
   2148   1.1       cgd #ifdef	ENCRYPTION
   2149   1.1       cgd 	send_do(TELOPT_ENCRYPT, 1);
   2150   1.1       cgd 	send_will(TELOPT_ENCRYPT, 1);
   2151   1.1       cgd #endif	/* ENCRYPTION */
   2152   1.3       cgd 	send_do(TELOPT_SGA, 1);
   2153   1.1       cgd 	send_will(TELOPT_TTYPE, 1);
   2154  1.35  christos 	send_will(TELOPT_NAWS, 1);
   2155   1.1       cgd 	send_will(TELOPT_TSPEED, 1);
   2156   1.1       cgd 	send_will(TELOPT_LFLOW, 1);
   2157   1.1       cgd 	send_will(TELOPT_LINEMODE, 1);
   2158   1.1       cgd 	send_will(TELOPT_NEW_ENVIRON, 1);
   2159   1.1       cgd 	send_do(TELOPT_STATUS, 1);
   2160   1.1       cgd 	if (env_getvalue((const unsigned char *)"DISPLAY"))
   2161   1.1       cgd 	    send_will(TELOPT_XDISPLOC, 1);
   2162   1.1       cgd 	if (eight)
   2163   1.1       cgd 	    tel_enter_binary(eight);
   2164   1.1       cgd     }
   2165   1.1       cgd 
   2166   1.1       cgd     for (;;) {
   2167   1.1       cgd 	int schedValue;
   2168   1.1       cgd 
   2169   1.1       cgd 	while ((schedValue = Scheduler(0)) != 0) {
   2170   1.1       cgd 	    if (schedValue == -1) {
   2171   1.1       cgd 		setcommandmode();
   2172   1.1       cgd 		return;
   2173   1.1       cgd 	    }
   2174   1.1       cgd 	}
   2175   1.1       cgd 
   2176   1.1       cgd 	if (Scheduler(1) == -1) {
   2177   1.1       cgd 	    setcommandmode();
   2178   1.1       cgd 	    return;
   2179   1.1       cgd 	}
   2180   1.1       cgd     }
   2181   1.1       cgd }
   2182   1.1       cgd 
   2183   1.1       cgd #if	0	/* XXX - this not being in is a bug */
   2185   1.1       cgd /*
   2186   1.1       cgd  * nextitem()
   2187   1.1       cgd  *
   2188   1.1       cgd  *	Return the address of the next "item" in the TELNET data
   2189  1.22  christos  * stream.  This will be the address of the next character if
   2190  1.22  christos  * the current address is a user data character, or it will
   2191   1.1       cgd  * be the address of the character following the TELNET command
   2192   1.1       cgd  * if the current address is a TELNET IAC ("I Am a Command")
   2193   1.1       cgd  * character.
   2194   1.1       cgd  */
   2195   1.1       cgd 
   2196   1.1       cgd static char *
   2197   1.1       cgd nextitem(char *current)
   2198   1.1       cgd {
   2199   1.1       cgd     if ((*current&0xff) != IAC) {
   2200   1.1       cgd 	return current+1;
   2201   1.1       cgd     }
   2202   1.1       cgd     switch (*(current+1)&0xff) {
   2203  1.21       wiz     case DO:
   2204   1.1       cgd     case DONT:
   2205   1.1       cgd     case WILL:
   2206   1.1       cgd     case WONT:
   2207   1.1       cgd 	return current+3;
   2208   1.1       cgd     case SB:		/* loop forever looking for the SE */
   2209   1.1       cgd 	{
   2210   1.1       cgd 	    char *look = current+2;
   2211   1.1       cgd 
   2212   1.1       cgd 	    for (;;) {
   2213   1.1       cgd 		if ((*look++&0xff) == IAC) {
   2214   1.1       cgd 		    if ((*look++&0xff) == SE) {
   2215   1.1       cgd 			return look;
   2216   1.1       cgd 		    }
   2217   1.1       cgd 		}
   2218   1.1       cgd 	    }
   2219   1.1       cgd 	}
   2220   1.1       cgd     default:
   2221   1.1       cgd 	return current+2;
   2222   1.1       cgd     }
   2223   1.1       cgd }
   2224   1.1       cgd #endif	/* 0 */
   2225   1.1       cgd 
   2226   1.1       cgd /*
   2227   1.1       cgd  * netclear()
   2228   1.1       cgd  *
   2229   1.1       cgd  *	We are about to do a TELNET SYNCH operation.  Clear
   2230   1.1       cgd  * the path to the network.
   2231   1.1       cgd  *
   2232   1.1       cgd  *	Things are a bit tricky since we may have sent the first
   2233   1.1       cgd  * byte or so of a previous TELNET command into the network.
   2234   1.1       cgd  * So, we have to scan the network buffer from the beginning
   2235   1.1       cgd  * until we are up to where we want to be.
   2236  1.22  christos  *
   2237  1.22  christos  *	A side effect of what we do, just to keep things
   2238   1.1       cgd  * simple, is to clear the urgent data pointer.  The principal
   2239   1.1       cgd  * caller should be setting the urgent data pointer AFTER calling
   2240  1.21       wiz  * us in any case.
   2241   1.1       cgd  */
   2242   1.1       cgd 
   2243   1.1       cgd static void
   2244   1.1       cgd netclear(void)
   2245   1.1       cgd {
   2246   1.1       cgd #if	0	/* XXX */
   2247   1.1       cgd     char *thisitem, *next;
   2248   1.1       cgd     char *good;
   2249   1.1       cgd #define	wewant(p)	((nfrontp > p) && ((*p&0xff) == IAC) && \
   2250   1.1       cgd 				((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
   2251   1.1       cgd 
   2252   1.1       cgd     thisitem = netobuf;
   2253   1.1       cgd 
   2254   1.1       cgd     while ((next = nextitem(thisitem)) <= netobuf.send) {
   2255   1.1       cgd 	thisitem = next;
   2256   1.1       cgd     }
   2257   1.1       cgd 
   2258   1.1       cgd     /* Now, thisitem is first before/at boundary. */
   2259   1.1       cgd 
   2260   1.1       cgd     good = netobuf;	/* where the good bytes go */
   2261   1.1       cgd 
   2262   1.1       cgd     while (netoring.add > thisitem) {
   2263   1.1       cgd 	if (wewant(thisitem)) {
   2264   1.5       jtk 	    int length;
   2265   1.1       cgd 
   2266   1.1       cgd 	    next = thisitem;
   2267   1.1       cgd 	    do {
   2268   1.1       cgd 		next = nextitem(next);
   2269   1.1       cgd 	    } while (wewant(next) && (nfrontp > next));
   2270   1.1       cgd 	    length = next-thisitem;
   2271   1.1       cgd 	    memmove(good, thisitem, length);
   2272   1.1       cgd 	    good += length;
   2273   1.1       cgd 	    thisitem = next;
   2274   1.1       cgd 	} else {
   2275   1.1       cgd 	    thisitem = nextitem(thisitem);
   2276   1.1       cgd 	}
   2277   1.1       cgd     }
   2278   1.1       cgd 
   2279  1.22  christos #endif	/* 0 */
   2280  1.22  christos }
   2281   1.1       cgd 
   2282   1.1       cgd /*
   2284   1.1       cgd  * These routines add various telnet commands to the data stream.
   2285   1.1       cgd  */
   2286   1.1       cgd 
   2287   1.1       cgd static void
   2288   1.1       cgd doflush(void)
   2289   1.1       cgd {
   2290   1.1       cgd     NET2ADD(IAC, DO);
   2291  1.22  christos     NETADD(TELOPT_TM);
   2292  1.22  christos     flushline = 1;
   2293   1.1       cgd     flushout = 1;
   2294   1.1       cgd     (void) ttyflush(1);			/* Flush/drop output */
   2295   1.1       cgd     /* do printoption AFTER flush, otherwise the output gets tossed... */
   2296   1.1       cgd     printoption("SENT", DO, TELOPT_TM);
   2297   1.1       cgd }
   2298   1.1       cgd 
   2299   1.1       cgd void
   2300   1.1       cgd xmitAO(void)
   2301   1.1       cgd {
   2302  1.22  christos     NET2ADD(IAC, AO);
   2303  1.22  christos     printoption("SENT", IAC, AO);
   2304   1.1       cgd     if (autoflush) {
   2305   1.1       cgd 	doflush();
   2306   1.1       cgd     }
   2307   1.1       cgd }
   2308   1.1       cgd 
   2309  1.22  christos 
   2310  1.22  christos void
   2311   1.1       cgd xmitEL(void)
   2312   1.1       cgd {
   2313   1.1       cgd     NET2ADD(IAC, EL);
   2314   1.1       cgd     printoption("SENT", IAC, EL);
   2315   1.1       cgd }
   2316   1.1       cgd 
   2317  1.22  christos void
   2318  1.22  christos xmitEC(void)
   2319   1.1       cgd {
   2320   1.1       cgd     NET2ADD(IAC, EC);
   2321   1.1       cgd     printoption("SENT", IAC, EC);
   2322   1.1       cgd }
   2323   1.1       cgd 
   2324   1.1       cgd 
   2325   1.1       cgd int
   2326   1.1       cgd dosynch(char *s)
   2327   1.1       cgd {
   2328   1.1       cgd     netclear();			/* clear the path to the network */
   2329   1.1       cgd     NETADD(IAC);
   2330  1.22  christos     setneturg();
   2331  1.22  christos     NETADD(DM);
   2332   1.1       cgd     printoption("SENT", IAC, DM);
   2333   1.1       cgd     return 1;
   2334  1.21       wiz }
   2335   1.1       cgd 
   2336   1.1       cgd int want_status_response = 0;
   2337   1.1       cgd 
   2338   1.1       cgd int
   2339   1.1       cgd get_status(char *s)
   2340   1.1       cgd {
   2341   1.1       cgd     unsigned char tmp[16];
   2342   1.1       cgd     unsigned char *cp;
   2343   1.1       cgd 
   2344   1.1       cgd     if (my_want_state_is_dont(TELOPT_STATUS)) {
   2345   1.1       cgd 	printf("Remote side does not support STATUS option\n");
   2346   1.1       cgd 	return 0;
   2347   1.1       cgd     }
   2348   1.1       cgd     cp = tmp;
   2349   1.1       cgd 
   2350   1.1       cgd     *cp++ = IAC;
   2351   1.1       cgd     *cp++ = SB;
   2352   1.1       cgd     *cp++ = TELOPT_STATUS;
   2353   1.1       cgd     *cp++ = TELQUAL_SEND;
   2354   1.1       cgd     *cp++ = IAC;
   2355   1.1       cgd     *cp++ = SE;
   2356  1.22  christos     if (NETROOM() >= cp - tmp) {
   2357  1.22  christos 	ring_supply_data(&netoring, tmp, cp-tmp);
   2358   1.1       cgd 	printsub('>', tmp+2, cp - tmp - 2);
   2359   1.1       cgd     }
   2360   1.1       cgd     ++want_status_response;
   2361   1.1       cgd     return 1;
   2362   1.1       cgd }
   2363   1.1       cgd 
   2364   1.1       cgd void
   2365   1.1       cgd intp(void)
   2366   1.9  christos {
   2367   1.1       cgd     NET2ADD(IAC, IP);
   2368   1.1       cgd     printoption("SENT", IAC, IP);
   2369   1.1       cgd     flushline = 1;
   2370  1.22  christos     if (autoflush) {
   2371  1.22  christos 	doflush();
   2372   1.1       cgd     }
   2373   1.1       cgd     if (autosynch) {
   2374   1.1       cgd 	dosynch(NULL);
   2375   1.1       cgd     }
   2376   1.1       cgd }
   2377   1.1       cgd 
   2378   1.1       cgd void
   2379   1.1       cgd sendbrk(void)
   2380   1.9  christos {
   2381   1.1       cgd     NET2ADD(IAC, BREAK);
   2382   1.1       cgd     printoption("SENT", IAC, BREAK);
   2383   1.1       cgd     flushline = 1;
   2384  1.22  christos     if (autoflush) {
   2385  1.22  christos 	doflush();
   2386   1.1       cgd     }
   2387   1.1       cgd     if (autosynch) {
   2388   1.1       cgd 	dosynch(NULL);
   2389   1.1       cgd     }
   2390   1.1       cgd }
   2391   1.1       cgd 
   2392   1.1       cgd void
   2393   1.1       cgd sendabort(void)
   2394   1.9  christos {
   2395   1.1       cgd     NET2ADD(IAC, ABORT);
   2396   1.1       cgd     printoption("SENT", IAC, ABORT);
   2397   1.1       cgd     flushline = 1;
   2398  1.22  christos     if (autoflush) {
   2399  1.22  christos 	doflush();
   2400   1.1       cgd     }
   2401   1.1       cgd     if (autosynch) {
   2402   1.1       cgd 	dosynch(NULL);
   2403   1.1       cgd     }
   2404   1.1       cgd }
   2405   1.1       cgd 
   2406   1.1       cgd void
   2407   1.1       cgd sendsusp(void)
   2408   1.9  christos {
   2409   1.1       cgd     NET2ADD(IAC, SUSP);
   2410   1.1       cgd     printoption("SENT", IAC, SUSP);
   2411   1.1       cgd     flushline = 1;
   2412  1.22  christos     if (autoflush) {
   2413  1.22  christos 	doflush();
   2414   1.1       cgd     }
   2415   1.1       cgd     if (autosynch) {
   2416   1.1       cgd 	dosynch(NULL);
   2417   1.1       cgd     }
   2418   1.1       cgd }
   2419  1.22  christos 
   2420  1.22  christos void
   2421   1.1       cgd sendeof(void)
   2422   1.1       cgd {
   2423   1.1       cgd     NET2ADD(IAC, xEOF);
   2424   1.1       cgd     printoption("SENT", IAC, xEOF);
   2425   1.1       cgd }
   2426   1.1       cgd 
   2427   1.1       cgd void
   2428   1.1       cgd sendayt(void)
   2429   1.1       cgd {
   2430  1.22  christos     NET2ADD(IAC, AYT);
   2431  1.22  christos     printoption("SENT", IAC, AYT);
   2432   1.1       cgd }
   2433   1.1       cgd 
   2434   1.1       cgd /*
   2435  1.21       wiz  * Send a window size update to the remote system.
   2436   1.1       cgd  */
   2437   1.1       cgd 
   2438   1.1       cgd void
   2439   1.1       cgd sendnaws(void)
   2440   1.1       cgd {
   2441   1.1       cgd     long rows, cols;
   2442   1.1       cgd     unsigned char tmp[16];
   2443   1.1       cgd     unsigned char *cp;
   2444   1.1       cgd 
   2445   1.1       cgd     if (my_state_is_wont(TELOPT_NAWS))
   2446   1.1       cgd 	return;
   2447   1.1       cgd 
   2448   1.1       cgd #define	PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
   2449   1.1       cgd 			    if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
   2450   1.1       cgd 
   2451   1.1       cgd     if (TerminalWindowSize(&rows, &cols) == 0) {	/* Failed */
   2452   1.1       cgd 	return;
   2453   1.1       cgd     }
   2454   1.1       cgd 
   2455   1.1       cgd     cp = tmp;
   2456   1.1       cgd 
   2457   1.1       cgd     *cp++ = IAC;
   2458   1.1       cgd     *cp++ = SB;
   2459   1.1       cgd     *cp++ = TELOPT_NAWS;
   2460   1.1       cgd     PUTSHORT(cp, cols);
   2461   1.1       cgd     PUTSHORT(cp, rows);
   2462  1.22  christos     *cp++ = IAC;
   2463  1.22  christos     *cp++ = SE;
   2464   1.1       cgd     if (NETROOM() >= cp - tmp) {
   2465   1.1       cgd 	ring_supply_data(&netoring, tmp, cp-tmp);
   2466   1.1       cgd 	printsub('>', tmp+2, cp - tmp - 2);
   2467   1.1       cgd     }
   2468   1.1       cgd }
   2469   1.1       cgd 
   2470   1.1       cgd void
   2471  1.22  christos tel_enter_binary(int rw)
   2472  1.22  christos {
   2473   1.1       cgd     if (rw&1)
   2474   1.1       cgd 	send_do(TELOPT_BINARY, 1);
   2475   1.1       cgd     if (rw&2)
   2476   1.1       cgd 	send_will(TELOPT_BINARY, 1);
   2477   1.1       cgd }
   2478   1.1       cgd 
   2479                 void
   2480                 tel_leave_binary(int rw)
   2481                 {
   2482                     if (rw&1)
   2483                 	send_dont(TELOPT_BINARY, 1);
   2484                     if (rw&2)
   2485                 	send_wont(TELOPT_BINARY, 1);
   2486                 }
   2487