133685Sbostic /* 233685Sbostic * Copyright (c) 1988 Regents of the University of California. 333685Sbostic * All rights reserved. 433685Sbostic * 533685Sbostic * Redistribution and use in source and binary forms are permitted 634898Sbostic * provided that the above copyright notice and this paragraph are 734898Sbostic * duplicated in all such forms and that any documentation, 834898Sbostic * advertising materials, and other materials related to such 934898Sbostic * distribution and use acknowledge that the software was developed 1034898Sbostic * by the University of California, Berkeley. The name of the 1134898Sbostic * University may not be used to endorse or promote products derived 1234898Sbostic * from this software without specific prior written permission. 1334898Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1434898Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1534898Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1633685Sbostic */ 1733685Sbostic 1833685Sbostic #ifndef lint 19*39529Sborman static char sccsid[] = "@(#)commands.c 1.23 (Berkeley) 11/14/89"; 2033685Sbostic #endif /* not lint */ 2133685Sbostic 2232144Sminshall #include <sys/types.h> 2336274Sminshall #if defined(unix) 2436274Sminshall #include <sys/file.h> 2536274Sminshall #endif /* defined(unix) */ 2632144Sminshall #include <sys/socket.h> 2732144Sminshall #include <netinet/in.h> 2838689Sborman #ifdef CRAY 2938689Sborman #include <sys/fcntl.h> 3038810Sborman #endif /* CRAY */ 3132144Sminshall 3232144Sminshall #include <signal.h> 3332144Sminshall #include <netdb.h> 3432144Sminshall #include <ctype.h> 3535298Sminshall #include <varargs.h> 3632144Sminshall 3732144Sminshall #include <arpa/telnet.h> 3832144Sminshall 3934305Sminshall #include "general.h" 4034305Sminshall 4132381Sminshall #include "ring.h" 4232381Sminshall 4332144Sminshall #include "externs.h" 4432144Sminshall #include "defines.h" 4532144Sminshall #include "types.h" 4632144Sminshall 4738689Sborman #ifdef SRCRT 4838810Sborman # ifndef CRAY 4938810Sborman # include <netinet/in_systm.h> 50*39529Sborman # if defined(vax) || defined(tahoe) 5138810Sborman # include <machine/endian.h> 52*39529Sborman # endif /* vax */ 5338810Sborman # endif /* CRAY */ 5438689Sborman #include <netinet/ip.h> 5538810Sborman #endif /* SRCRT */ 5638689Sborman 5738689Sborman 5832144Sminshall char *hostname; 5938689Sborman extern char *getenv(); 6032144Sminshall 6136180Sminshall #define Ambiguous(s) ((char **)s == &ambiguous) 6232144Sminshall static char *ambiguous; /* special return value for command routines */ 6332144Sminshall 6432144Sminshall typedef struct { 6532144Sminshall char *name; /* command name */ 6638689Sborman char *help; /* help string (NULL for no help) */ 6732144Sminshall int (*handler)(); /* routine which executes command */ 6832144Sminshall int needconnect; /* Do we need to be connected to execute? */ 6932144Sminshall } Command; 7032144Sminshall 7138689Sborman static char line[256]; 7238689Sborman static char saveline[256]; 7332144Sminshall static int margc; 7432144Sminshall static char *margv[20]; 7532144Sminshall 7632144Sminshall /* 7732144Sminshall * Various utility routines. 7832144Sminshall */ 7932144Sminshall 8036180Sminshall #if !defined(BSD) || (BSD <= 43) 8136180Sminshall 8236180Sminshall char *h_errlist[] = { 8336180Sminshall "Error 0", 8436180Sminshall "Unknown host", /* 1 HOST_NOT_FOUND */ 8536180Sminshall "Host name lookup failure", /* 2 TRY_AGAIN */ 8636180Sminshall "Unknown server error", /* 3 NO_RECOVERY */ 8736180Sminshall "No address associated with name", /* 4 NO_ADDRESS */ 8836180Sminshall }; 8936180Sminshall int h_nerr = { sizeof(h_errlist)/sizeof(h_errlist[0]) }; 9036180Sminshall 9136274Sminshall int h_errno; /* In some version of SunOS this is necessary */ 9236180Sminshall 9336180Sminshall /* 9436180Sminshall * herror -- 9536180Sminshall * print the error indicated by the h_errno value. 9636180Sminshall */ 9736180Sminshall herror(s) 9836180Sminshall char *s; 9936180Sminshall { 10036180Sminshall if (s && *s) { 10136180Sminshall fprintf(stderr, "%s: ", s); 10236180Sminshall } 10336180Sminshall if ((h_errno < 0) || (h_errno >= h_nerr)) { 10436180Sminshall fprintf(stderr, "Unknown error\n"); 10536274Sminshall } else if (h_errno == 0) { 10636274Sminshall #if defined(sun) 10736274Sminshall fprintf(stderr, "Host unknown\n"); 10836274Sminshall #endif /* defined(sun) */ 10936180Sminshall } else { 11036180Sminshall fprintf(stderr, "%s\n", h_errlist[h_errno]); 11136180Sminshall } 11236180Sminshall } 11336180Sminshall #endif /* !define(BSD) || (BSD <= 43) */ 11436180Sminshall 11532144Sminshall static void 11632144Sminshall makeargv() 11732144Sminshall { 11832144Sminshall register char *cp; 11932144Sminshall register char **argp = margv; 12032144Sminshall 12132144Sminshall margc = 0; 12232144Sminshall cp = line; 12332144Sminshall if (*cp == '!') { /* Special case shell escape */ 12438689Sborman strcpy(saveline, line); /* save for shell command */ 12532144Sminshall *argp++ = "!"; /* No room in string to get this */ 12632144Sminshall margc++; 12732144Sminshall cp++; 12832144Sminshall } 12932144Sminshall while (*cp) { 13032144Sminshall while (isspace(*cp)) 13132144Sminshall cp++; 13232144Sminshall if (*cp == '\0') 13332144Sminshall break; 13432144Sminshall *argp++ = cp; 13532144Sminshall margc += 1; 13632144Sminshall while (*cp != '\0' && !isspace(*cp)) 13732144Sminshall cp++; 13832144Sminshall if (*cp == '\0') 13932144Sminshall break; 14032144Sminshall *cp++ = '\0'; 14132144Sminshall } 14232144Sminshall *argp++ = 0; 14332144Sminshall } 14432144Sminshall 14532144Sminshall 14632144Sminshall static char ** 14732144Sminshall genget(name, table, next) 14832144Sminshall char *name; /* name to match */ 14932144Sminshall char **table; /* name entry in table */ 15032144Sminshall char **(*next)(); /* routine to return next entry in table */ 15132144Sminshall { 15232144Sminshall register char *p, *q; 15332144Sminshall register char **c, **found; 15432144Sminshall register int nmatches, longest; 15532144Sminshall 15632144Sminshall if (name == 0) { 15732144Sminshall return 0; 15832144Sminshall } 15932144Sminshall longest = 0; 16032144Sminshall nmatches = 0; 16132144Sminshall found = 0; 16232144Sminshall for (c = table; (p = *c) != 0; c = (*next)(c)) { 16332144Sminshall for (q = name; 16432144Sminshall (*q == *p) || (isupper(*q) && tolower(*q) == *p); p++, q++) 16532144Sminshall if (*q == 0) /* exact match? */ 16632144Sminshall return (c); 16732144Sminshall if (!*q) { /* the name was a prefix */ 16832144Sminshall if (q - name > longest) { 16932144Sminshall longest = q - name; 17032144Sminshall nmatches = 1; 17132144Sminshall found = c; 17232144Sminshall } else if (q - name == longest) 17332144Sminshall nmatches++; 17432144Sminshall } 17532144Sminshall } 17632144Sminshall if (nmatches > 1) 17736180Sminshall return &ambiguous; 17832144Sminshall return (found); 17932144Sminshall } 18032144Sminshall 18132144Sminshall /* 18232144Sminshall * Make a character string into a number. 18332144Sminshall * 18432144Sminshall * Todo: 1. Could take random integers (12, 0x12, 012, 0b1). 18532144Sminshall */ 18632144Sminshall 18732144Sminshall static 18832144Sminshall special(s) 18932144Sminshall register char *s; 19032144Sminshall { 19132144Sminshall register char c; 19232144Sminshall char b; 19332144Sminshall 19432144Sminshall switch (*s) { 19532144Sminshall case '^': 19632144Sminshall b = *++s; 19732144Sminshall if (b == '?') { 19832144Sminshall c = b | 0x40; /* DEL */ 19932144Sminshall } else { 20032144Sminshall c = b & 0x1f; 20132144Sminshall } 20232144Sminshall break; 20332144Sminshall default: 20432144Sminshall c = *s; 20532144Sminshall break; 20632144Sminshall } 20732144Sminshall return c; 20832144Sminshall } 20932144Sminshall 21032144Sminshall /* 21132144Sminshall * Construct a control character sequence 21232144Sminshall * for a special character. 21332144Sminshall */ 21432144Sminshall static char * 21532144Sminshall control(c) 21632144Sminshall register int c; 21732144Sminshall { 21832144Sminshall static char buf[3]; 21932144Sminshall 22032144Sminshall if (c == 0x7f) 22132144Sminshall return ("^?"); 22232144Sminshall if (c == '\377') { 22332144Sminshall return "off"; 22432144Sminshall } 22532144Sminshall if (c >= 0x20) { 22632144Sminshall buf[0] = c; 22732144Sminshall buf[1] = 0; 22832144Sminshall } else { 22932144Sminshall buf[0] = '^'; 23032144Sminshall buf[1] = '@'+c; 23132144Sminshall buf[2] = 0; 23232144Sminshall } 23332144Sminshall return (buf); 23432144Sminshall } 23532144Sminshall 23632144Sminshall 23732144Sminshall 23832144Sminshall /* 23932144Sminshall * The following are data structures and routines for 24032144Sminshall * the "send" command. 24132144Sminshall * 24232144Sminshall */ 24332144Sminshall 24432144Sminshall struct sendlist { 24532144Sminshall char *name; /* How user refers to it (case independent) */ 24632144Sminshall char *help; /* Help information (0 ==> no help) */ 24732144Sminshall #if defined(NOT43) 24838689Sborman int (*handler)(); /* Routine to perform (for special ops) */ 24932144Sminshall #else /* defined(NOT43) */ 25038689Sborman void (*handler)(); /* Routine to perform (for special ops) */ 25132144Sminshall #endif /* defined(NOT43) */ 25238689Sborman int what; /* Character to be sent (<0 ==> special) */ 25332144Sminshall }; 25432144Sminshall 25532144Sminshall #define SENDQUESTION -1 25632144Sminshall #define SENDESCAPE -3 25732144Sminshall 25832144Sminshall static struct sendlist Sendlist[] = { 25938689Sborman { "ao", "Send Telnet Abort output", 0, AO }, 26038689Sborman { "ayt", "Send Telnet 'Are You There'", 0, AYT }, 26138689Sborman { "brk", "Send Telnet Break", 0, BREAK }, 26238689Sborman { "ec", "Send Telnet Erase Character", 0, EC }, 26338689Sborman { "el", "Send Telnet Erase Line", 0, EL }, 26438689Sborman { "escape", "Send current escape character", 0, SENDESCAPE }, 26538689Sborman { "ga", "Send Telnet 'Go Ahead' sequence", 0, GA }, 26638689Sborman { "ip", "Send Telnet Interrupt Process", 0, IP }, 26738689Sborman { "nop", "Send Telnet 'No operation'", 0, NOP }, 26838689Sborman { "eor", "Send Telnet 'End of Record'", 0, EOR }, 26938689Sborman { "abort", "Send Telnet 'Abort Process'", 0, ABORT }, 27038689Sborman { "susp", "Send Telnet 'Suspend Process'", 0, SUSP }, 27138689Sborman { "eof", "Send Telnet End of File Character", 0, xEOF }, 27238689Sborman { "synch", "Perform Telnet 'Synch operation'", dosynch, SYNCH }, 27338909Sborman { "getstatus", "Send request for STATUS", get_status, 0 }, 27438689Sborman { "?", "Display send options", 0, SENDQUESTION }, 27532144Sminshall { 0 } 27632144Sminshall }; 27732144Sminshall 27832144Sminshall static struct sendlist Sendlist2[] = { /* some synonyms */ 27938689Sborman { "break", 0, 0, BREAK }, 28032144Sminshall 28138689Sborman { "intp", 0, 0, IP }, 28238689Sborman { "interrupt", 0, 0, IP }, 28338689Sborman { "intr", 0, 0, IP }, 28432144Sminshall 28538689Sborman { "help", 0, 0, SENDQUESTION }, 28632144Sminshall 28738689Sborman { 0 } 28832144Sminshall }; 28932144Sminshall 29032144Sminshall static char ** 29132144Sminshall getnextsend(name) 29232144Sminshall char *name; 29332144Sminshall { 29432144Sminshall struct sendlist *c = (struct sendlist *) name; 29532144Sminshall 29632144Sminshall return (char **) (c+1); 29732144Sminshall } 29832144Sminshall 29932144Sminshall static struct sendlist * 30032144Sminshall getsend(name) 30132144Sminshall char *name; 30232144Sminshall { 30332144Sminshall struct sendlist *sl; 30432144Sminshall 30532144Sminshall if ((sl = (struct sendlist *) 30632144Sminshall genget(name, (char **) Sendlist, getnextsend)) != 0) { 30732144Sminshall return sl; 30832144Sminshall } else { 30932144Sminshall return (struct sendlist *) 31032144Sminshall genget(name, (char **) Sendlist2, getnextsend); 31132144Sminshall } 31232144Sminshall } 31332144Sminshall 31432144Sminshall static 31532144Sminshall sendcmd(argc, argv) 31632144Sminshall int argc; 31732144Sminshall char **argv; 31832144Sminshall { 31932144Sminshall int what; /* what we are sending this time */ 32032144Sminshall int count; /* how many bytes we are going to need to send */ 32132144Sminshall int i; 32232144Sminshall int question = 0; /* was at least one argument a question */ 32332144Sminshall struct sendlist *s; /* pointer to current command */ 32432144Sminshall 32532144Sminshall if (argc < 2) { 32632144Sminshall printf("need at least one argument for 'send' command\n"); 32732144Sminshall printf("'send ?' for help\n"); 32832144Sminshall return 0; 32932144Sminshall } 33032144Sminshall /* 33132144Sminshall * First, validate all the send arguments. 33232144Sminshall * In addition, we see how much space we are going to need, and 33332144Sminshall * whether or not we will be doing a "SYNCH" operation (which 33432144Sminshall * flushes the network queue). 33532144Sminshall */ 33632144Sminshall count = 0; 33732144Sminshall for (i = 1; i < argc; i++) { 33832144Sminshall s = getsend(argv[i]); 33932144Sminshall if (s == 0) { 34032144Sminshall printf("Unknown send argument '%s'\n'send ?' for help.\n", 34132144Sminshall argv[i]); 34232144Sminshall return 0; 34332144Sminshall } else if (Ambiguous(s)) { 34432144Sminshall printf("Ambiguous send argument '%s'\n'send ?' for help.\n", 34532144Sminshall argv[i]); 34632144Sminshall return 0; 34732144Sminshall } 34832144Sminshall switch (s->what) { 34932144Sminshall case SENDQUESTION: 35038689Sborman question = 1; 35132144Sminshall break; 35232144Sminshall case SENDESCAPE: 35332144Sminshall count += 1; 35432144Sminshall break; 35532144Sminshall case SYNCH: 35632144Sminshall count += 2; 35732144Sminshall break; 35832144Sminshall default: 35932144Sminshall count += 2; 36032144Sminshall break; 36132144Sminshall } 36232144Sminshall } 36338689Sborman if (!connected) { 36438689Sborman if (count) 36538689Sborman printf("?Need to be connected first.\n"); 36638689Sborman if (question) { 36738689Sborman for (s = Sendlist; s->name; s++) 36838689Sborman if (s->help) 36938689Sborman printf("%-15s %s\n", s->name, s->help); 37038689Sborman } else 37138689Sborman printf("'send ?' for help\n"); 37238689Sborman return !question; 37338689Sborman } 37432144Sminshall /* Now, do we have enough room? */ 37532144Sminshall if (NETROOM() < count) { 37632144Sminshall printf("There is not enough room in the buffer TO the network\n"); 37732144Sminshall printf("to process your request. Nothing will be done.\n"); 37832144Sminshall printf("('send synch' will throw away most data in the network\n"); 37932144Sminshall printf("buffer, if this might help.)\n"); 38032144Sminshall return 0; 38132144Sminshall } 38232144Sminshall /* OK, they are all OK, now go through again and actually send */ 38332144Sminshall for (i = 1; i < argc; i++) { 38432144Sminshall if ((s = getsend(argv[i])) == 0) { 38532144Sminshall fprintf(stderr, "Telnet 'send' error - argument disappeared!\n"); 38632144Sminshall quit(); 38732144Sminshall /*NOTREACHED*/ 38832144Sminshall } 38938689Sborman if (s->handler) { 39038689Sborman (*s->handler)(s); 39132144Sminshall } else { 39232144Sminshall switch (what = s->what) { 39332144Sminshall case SYNCH: 39432144Sminshall dosynch(); 39532144Sminshall break; 39632144Sminshall case SENDQUESTION: 39732144Sminshall for (s = Sendlist; s->name; s++) { 39838689Sborman if (s->help) 39938689Sborman printf("%-15s %s\n", s->name, s->help); 40032144Sminshall } 40132144Sminshall question = 1; 40232144Sminshall break; 40332144Sminshall case SENDESCAPE: 40432144Sminshall NETADD(escape); 40532144Sminshall break; 40632144Sminshall default: 40732144Sminshall NET2ADD(IAC, what); 40832144Sminshall break; 40932144Sminshall } 41032144Sminshall } 41132144Sminshall } 41232144Sminshall return !question; 41332144Sminshall } 41432144Sminshall 41532144Sminshall /* 41632144Sminshall * The following are the routines and data structures referred 41732144Sminshall * to by the arguments to the "toggle" command. 41832144Sminshall */ 41932144Sminshall 42032144Sminshall static 42132144Sminshall lclchars() 42232144Sminshall { 42332144Sminshall donelclchars = 1; 42432144Sminshall return 1; 42532144Sminshall } 42632144Sminshall 42732144Sminshall static 42832144Sminshall togdebug() 42932144Sminshall { 43032144Sminshall #ifndef NOT43 43132144Sminshall if (net > 0 && 43232144Sminshall (SetSockOpt(net, SOL_SOCKET, SO_DEBUG, debug)) < 0) { 43332144Sminshall perror("setsockopt (SO_DEBUG)"); 43432144Sminshall } 43532144Sminshall #else /* NOT43 */ 43632144Sminshall if (debug) { 43732144Sminshall if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) 43832144Sminshall perror("setsockopt (SO_DEBUG)"); 43932144Sminshall } else 44032144Sminshall printf("Cannot turn off socket debugging\n"); 44132144Sminshall #endif /* NOT43 */ 44232144Sminshall return 1; 44332144Sminshall } 44432144Sminshall 44532144Sminshall 44632144Sminshall static int 44732144Sminshall togcrlf() 44832144Sminshall { 44932144Sminshall if (crlf) { 45032144Sminshall printf("Will send carriage returns as telnet <CR><LF>.\n"); 45132144Sminshall } else { 45232144Sminshall printf("Will send carriage returns as telnet <CR><NUL>.\n"); 45332144Sminshall } 45432144Sminshall return 1; 45532144Sminshall } 45632144Sminshall 45738909Sborman int binmode; 45832144Sminshall 45932144Sminshall static int 46038689Sborman togbinary(val) 46138689Sborman int val; 46232144Sminshall { 46332144Sminshall donebinarytoggle = 1; 46432144Sminshall 46538909Sborman if (val >= 0) { 46638909Sborman binmode = val; 46738909Sborman } else { 46838909Sborman if (my_want_state_is_will(TELOPT_BINARY) && 46938909Sborman my_want_state_is_do(TELOPT_BINARY)) { 47038909Sborman binmode = 1; 47138909Sborman } else if (my_want_state_is_wont(TELOPT_BINARY) && 47238909Sborman my_want_state_is_dont(TELOPT_BINARY)) { 47338909Sborman binmode = 0; 47438909Sborman } 47538909Sborman val = binmode ? 0 : 1; 47638909Sborman } 47738909Sborman 47838909Sborman if (val == 1) { 47938909Sborman if (my_want_state_is_will(TELOPT_BINARY) && 48038909Sborman my_want_state_is_do(TELOPT_BINARY)) { 48138689Sborman printf("Already operating in binary mode with remote host.\n"); 48238909Sborman } else { 48338909Sborman printf("Negotiating binary mode with remote host.\n"); 48438909Sborman tel_enter_binary(3); 48538689Sborman } 48638909Sborman } else { 48738909Sborman if (my_want_state_is_wont(TELOPT_BINARY) && 48838909Sborman my_want_state_is_dont(TELOPT_BINARY)) { 48938689Sborman printf("Already in network ascii mode with remote host.\n"); 49038909Sborman } else { 49138909Sborman printf("Negotiating network ascii mode with remote host.\n"); 49238909Sborman tel_leave_binary(3); 49338689Sborman } 49432144Sminshall } 49532144Sminshall return 1; 49632144Sminshall } 49732144Sminshall 49838909Sborman static int 49938909Sborman togrbinary(val) 50038909Sborman int val; 50138909Sborman { 50238909Sborman donebinarytoggle = 1; 50332144Sminshall 50438909Sborman if (val == -1) 50538909Sborman val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1; 50632144Sminshall 50738909Sborman if (val == 1) { 50838909Sborman if (my_want_state_is_do(TELOPT_BINARY)) { 50938909Sborman printf("Already receiving in binary mode.\n"); 51038909Sborman } else { 51138909Sborman printf("Negotiating binary mode on input.\n"); 51238909Sborman tel_enter_binary(1); 51338909Sborman } 51438909Sborman } else { 51538909Sborman if (my_want_state_is_dont(TELOPT_BINARY)) { 51638909Sborman printf("Already receiving in network ascii mode.\n"); 51738909Sborman } else { 51838909Sborman printf("Negotiating network ascii mode on input.\n"); 51938909Sborman tel_leave_binary(1); 52038909Sborman } 52138909Sborman } 52238909Sborman return 1; 52338909Sborman } 52438909Sborman 52538909Sborman static int 52638909Sborman togxbinary(val) 52738909Sborman int val; 52838909Sborman { 52938909Sborman donebinarytoggle = 1; 53038909Sborman 53138909Sborman if (val == -1) 53238909Sborman val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1; 53338909Sborman 53438909Sborman if (val == 1) { 53538909Sborman if (my_want_state_is_will(TELOPT_BINARY)) { 53638909Sborman printf("Already transmitting in binary mode.\n"); 53738909Sborman } else { 53838909Sborman printf("Negotiating binary mode on output.\n"); 53938909Sborman tel_enter_binary(2); 54038909Sborman } 54138909Sborman } else { 54238909Sborman if (my_want_state_is_wont(TELOPT_BINARY)) { 54338909Sborman printf("Already transmitting in network ascii mode.\n"); 54438909Sborman } else { 54538909Sborman printf("Negotiating network ascii mode on output.\n"); 54638909Sborman tel_leave_binary(2); 54738909Sborman } 54838909Sborman } 54938909Sborman return 1; 55038909Sborman } 55138909Sborman 55238909Sborman 55332144Sminshall extern int togglehelp(); 55438689Sborman extern int slc_check(); 55532144Sminshall 55632144Sminshall struct togglelist { 55732144Sminshall char *name; /* name of toggle */ 55832144Sminshall char *help; /* help message */ 55932144Sminshall int (*handler)(); /* routine to do actual setting */ 56032144Sminshall int *variable; 56132144Sminshall char *actionexplanation; 56232144Sminshall }; 56332144Sminshall 56432144Sminshall static struct togglelist Togglelist[] = { 56532144Sminshall { "autoflush", 56638689Sborman "flushing of output when sending interrupt characters", 56732144Sminshall 0, 56838689Sborman &autoflush, 56938689Sborman "flush output when sending interrupt characters" }, 57032144Sminshall { "autosynch", 57138689Sborman "automatic sending of interrupt characters in urgent mode", 57232144Sminshall 0, 57338689Sborman &autosynch, 57438689Sborman "send interrupt characters in urgent mode" }, 57532144Sminshall { "binary", 57638689Sborman "sending and receiving of binary data", 57732144Sminshall togbinary, 57838689Sborman 0, 57938689Sborman 0 }, 58038909Sborman { "inbinary", 58138909Sborman "receiving of binary data", 58238909Sborman togrbinary, 58338909Sborman 0, 58438909Sborman 0 }, 58538909Sborman { "outbinary", 58638909Sborman "sending of binary data", 58738909Sborman togxbinary, 58838909Sborman 0, 58938909Sborman 0 }, 59032144Sminshall { "crlf", 59138689Sborman "sending carriage returns as telnet <CR><LF>", 59232144Sminshall togcrlf, 59338689Sborman &crlf, 59438689Sborman 0 }, 59532144Sminshall { "crmod", 59638689Sborman "mapping of received carriage returns", 59732144Sminshall 0, 59838689Sborman &crmod, 59938689Sborman "map carriage return on output" }, 60032144Sminshall { "localchars", 60138689Sborman "local recognition of certain control characters", 60232144Sminshall lclchars, 60338689Sborman &localchars, 60438689Sborman "recognize certain control characters" }, 60538689Sborman { " ", "", 0 }, /* empty line */ 60638208Sminshall #if defined(unix) && defined(TN3270) 60738920Sminshall { "apitrace", 60838920Sminshall "(debugging) toggle tracing of API transactions", 60938920Sminshall 0, 61038920Sminshall &apitrace, 61138920Sminshall "trace API transactions" }, 61238208Sminshall { "cursesdata", 61338208Sminshall "(debugging) toggle printing of hexadecimal curses data", 61438208Sminshall 0, 61538689Sborman &cursesdata, 61638689Sborman "print hexadecimal representation of curses data" }, 61738208Sminshall #endif /* defined(unix) && defined(TN3270) */ 61832144Sminshall { "debug", 61938689Sborman "debugging", 62032144Sminshall togdebug, 62138689Sborman &debug, 62238689Sborman "turn on socket level debugging" }, 62332144Sminshall { "netdata", 62438689Sborman "printing of hexadecimal network data (debugging)", 62532144Sminshall 0, 62638689Sborman &netdata, 62738689Sborman "print hexadecimal representation of network traffic" }, 62838689Sborman { "prettydump", 62938689Sborman "output of \"netdata\" to user readable format (debugging)", 63038689Sborman 0, 63138689Sborman &prettydump, 63238689Sborman "print user readable output for \"netdata\"" }, 63332144Sminshall { "options", 63438689Sborman "viewing of options processing (debugging)", 63532144Sminshall 0, 63638689Sborman &showoptions, 63738689Sborman "show option processing" }, 63838208Sminshall #if defined(unix) 63938208Sminshall { "termdata", 64038208Sminshall "(debugging) toggle printing of hexadecimal terminal data", 64138208Sminshall 0, 64238689Sborman &termdata, 64338689Sborman "print hexadecimal representation of terminal traffic" }, 64438208Sminshall #endif /* defined(unix) */ 64532144Sminshall { "?", 64638689Sborman 0, 64738689Sborman togglehelp }, 64832144Sminshall { "help", 64938689Sborman 0, 65038689Sborman togglehelp }, 65132144Sminshall { 0 } 65232144Sminshall }; 65332144Sminshall 65432144Sminshall static 65532144Sminshall togglehelp() 65632144Sminshall { 65732144Sminshall struct togglelist *c; 65832144Sminshall 65932144Sminshall for (c = Togglelist; c->name; c++) { 66038689Sborman if (c->help) { 66138689Sborman if (*c->help) 66238689Sborman printf("%-15s toggle %s\n", c->name, c->help); 66338689Sborman else 66438689Sborman printf("\n"); 66532144Sminshall } 66632144Sminshall } 66738689Sborman printf("\n"); 66838689Sborman printf("%-15s %s\n", "?", "display help information"); 66932144Sminshall return 0; 67032144Sminshall } 67132144Sminshall 67238689Sborman static 67338689Sborman settogglehelp(set) 67438689Sborman int set; 67538689Sborman { 67638689Sborman struct togglelist *c; 67738689Sborman 67838689Sborman for (c = Togglelist; c->name; c++) { 67938689Sborman if (c->help) { 68038689Sborman if (*c->help) 68138689Sborman printf("%-15s %s %s\n", c->name, set ? "enable" : "disable", 68238689Sborman c->help); 68338689Sborman else 68438689Sborman printf("\n"); 68538689Sborman } 68638689Sborman } 68738689Sborman } 68838689Sborman 68932144Sminshall static char ** 69032144Sminshall getnexttoggle(name) 69132144Sminshall char *name; 69232144Sminshall { 69332144Sminshall struct togglelist *c = (struct togglelist *) name; 69432144Sminshall 69532144Sminshall return (char **) (c+1); 69632144Sminshall } 69732144Sminshall 69832144Sminshall static struct togglelist * 69932144Sminshall gettoggle(name) 70032144Sminshall char *name; 70132144Sminshall { 70232144Sminshall return (struct togglelist *) 70332144Sminshall genget(name, (char **) Togglelist, getnexttoggle); 70432144Sminshall } 70532144Sminshall 70632144Sminshall static 70732144Sminshall toggle(argc, argv) 70832144Sminshall int argc; 70932144Sminshall char *argv[]; 71032144Sminshall { 71132144Sminshall int retval = 1; 71232144Sminshall char *name; 71332144Sminshall struct togglelist *c; 71432144Sminshall 71532144Sminshall if (argc < 2) { 71632144Sminshall fprintf(stderr, 71732144Sminshall "Need an argument to 'toggle' command. 'toggle ?' for help.\n"); 71832144Sminshall return 0; 71932144Sminshall } 72032144Sminshall argc--; 72132144Sminshall argv++; 72232144Sminshall while (argc--) { 72332144Sminshall name = *argv++; 72432144Sminshall c = gettoggle(name); 72532144Sminshall if (Ambiguous(c)) { 72632144Sminshall fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\n", 72732144Sminshall name); 72832144Sminshall return 0; 72932144Sminshall } else if (c == 0) { 73032144Sminshall fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\n", 73132144Sminshall name); 73232144Sminshall return 0; 73332144Sminshall } else { 73432144Sminshall if (c->variable) { 73532144Sminshall *c->variable = !*c->variable; /* invert it */ 73632144Sminshall if (c->actionexplanation) { 73732144Sminshall printf("%s %s.\n", *c->variable? "Will" : "Won't", 73832144Sminshall c->actionexplanation); 73932144Sminshall } 74032144Sminshall } 74132144Sminshall if (c->handler) { 74238689Sborman retval &= (*c->handler)(-1); 74332144Sminshall } 74432144Sminshall } 74532144Sminshall } 74632144Sminshall return retval; 74732144Sminshall } 74832144Sminshall 74932144Sminshall /* 75032144Sminshall * The following perform the "set" command. 75132144Sminshall */ 75232144Sminshall 75338689Sborman #ifdef USE_TERMIO 75438689Sborman struct termio new_tc = { 0 }; 75538689Sborman #endif 75638689Sborman 75732144Sminshall struct setlist { 75832144Sminshall char *name; /* name */ 75932144Sminshall char *help; /* help information */ 76038689Sborman void (*handler)(); 761*39529Sborman unsigned char *charp; /* where it is located at */ 76232144Sminshall }; 76332144Sminshall 76432144Sminshall static struct setlist Setlist[] = { 76538689Sborman { "echo", "character to toggle local echoing on/off", 0, &echoc }, 76638689Sborman { "escape", "character to escape back to telnet command mode", 0, &escape }, 76738689Sborman { "tracefile", "file to write trace intormation to", SetNetTrace, NetTraceFile}, 76832144Sminshall { " ", "" }, 76938689Sborman { " ", "The following need 'localchars' to be toggled true", 0, 0 }, 77038689Sborman { "flushoutput", "character to cause an Abort Oubput", 0, termFlushCharp }, 77138689Sborman { "interrupt", "character to cause an Interrupt Process", 0, termIntCharp }, 77238689Sborman { "quit", "character to cause an Abort process", 0, termQuitCharp }, 77338689Sborman { "eof", "character to cause an EOF ", 0, termEofCharp }, 77438689Sborman { " ", "" }, 77538689Sborman { " ", "The following are for local editing in linemode", 0, 0 }, 77638689Sborman { "erase", "character to use to erase a character", 0, termEraseCharp }, 77738689Sborman { "kill", "character to use to erase a line", 0, termKillCharp }, 77838689Sborman { "lnext", "character to use for literal next", 0, termLiteralNextCharp }, 77938689Sborman { "susp", "character to cuase a Suspend Process", 0, termSuspCharp }, 78038689Sborman { "reprint", "character to use for line reprint", 0, termRprntCharp }, 78138689Sborman { "worderase", "character to use to erase a word", 0, termWerasCharp }, 78238689Sborman { "start", "character to use for XON", 0, termStartCharp }, 78338689Sborman { "stop", "character to sue for XOFF", 0, termStopCharp }, 78432144Sminshall { 0 } 78532144Sminshall }; 78632144Sminshall 78738689Sborman #ifdef CRAY 78838689Sborman /* Work around compiler bug */ 78938689Sborman _setlist_init() 79038689Sborman { 791*39529Sborman Setlist[5].charp = &termFlushChar; 79238689Sborman Setlist[6].charp = &termIntChar; 79338689Sborman Setlist[7].charp = &termQuitChar; 79438689Sborman Setlist[8].charp = &termEofChar; 79538689Sborman Setlist[11].charp = &termEraseChar; 79638689Sborman Setlist[12].charp = &termKillChar; 797*39529Sborman Setlist[13].charp = &termLiteralNextChar; 798*39529Sborman Setlist[14].charp = &termSuspChar; 799*39529Sborman Setlist[15].charp = &termRprntChar; 800*39529Sborman Setlist[16].charp = &termWerasChar; 801*39529Sborman Setlist[17].charp = &termStartChar; 802*39529Sborman Setlist[18].charp = &termStopChar; 80338689Sborman } 80438810Sborman #endif /* CRAY */ 80538689Sborman 80632144Sminshall static char ** 80732144Sminshall getnextset(name) 80832144Sminshall char *name; 80932144Sminshall { 81032144Sminshall struct setlist *c = (struct setlist *)name; 81132144Sminshall 81232144Sminshall return (char **) (c+1); 81332144Sminshall } 81432144Sminshall 81532144Sminshall static struct setlist * 81632144Sminshall getset(name) 81732144Sminshall char *name; 81832144Sminshall { 81932144Sminshall return (struct setlist *) genget(name, (char **) Setlist, getnextset); 82032144Sminshall } 82132144Sminshall 82232144Sminshall static 82332144Sminshall setcmd(argc, argv) 82432144Sminshall int argc; 82532144Sminshall char *argv[]; 82632144Sminshall { 82732144Sminshall int value; 82832144Sminshall struct setlist *ct; 82938689Sborman struct togglelist *c; 83032144Sminshall 83138689Sborman if (argc < 2 || argc > 3) { 83238689Sborman printf("Format is 'set Name Value'\n'set ?' for help.\n"); 83332144Sminshall return 0; 83432144Sminshall } 83538689Sborman if ((argc == 2) && 83638689Sborman ((!strcmp(argv[1], "?")) || (!strcmp(argv[1], "help")))) { 83738689Sborman for (ct = Setlist; ct->name; ct++) 83838689Sborman printf("%-15s %s\n", ct->name, ct->help); 83938689Sborman printf("\n"); 84038689Sborman settogglehelp(1); 84138689Sborman printf("%-15s %s\n", "?", "display help information"); 84238689Sborman return 0; 84338689Sborman } 84432144Sminshall 84532144Sminshall ct = getset(argv[1]); 84632144Sminshall if (ct == 0) { 84738689Sborman c = gettoggle(argv[1]); 84838689Sborman if (c == 0) { 84938689Sborman fprintf(stderr, "'%s': unknown argument ('set ?' for help).\n", 85032144Sminshall argv[1]); 85138689Sborman return 0; 85238689Sborman } else if (Ambiguous(c)) { 85338689Sborman fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n", 85438689Sborman argv[1]); 85538689Sborman return 0; 85638689Sborman } 85738689Sborman if (c->variable) { 85838689Sborman if ((argc == 2) || (strcmp("on", argv[2]) == 0)) 85938689Sborman *c->variable = 1; 86038689Sborman else if (strcmp("off", argv[2]) == 0) 86138689Sborman *c->variable = 0; 86238689Sborman else { 86338689Sborman printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n"); 86438689Sborman return 0; 86538689Sborman } 86638689Sborman if (c->actionexplanation) { 86738689Sborman printf("%s %s.\n", *c->variable? "Will" : "Won't", 86838689Sborman c->actionexplanation); 86938689Sborman } 87038689Sborman } 87138689Sborman if (c->handler) 87238689Sborman (*c->handler)(1); 87338689Sborman } else if (argc != 3) { 87438689Sborman printf("Format is 'set Name Value'\n'set ?' for help.\n"); 87532144Sminshall return 0; 87632144Sminshall } else if (Ambiguous(ct)) { 87732144Sminshall fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n", 87832144Sminshall argv[1]); 87932144Sminshall return 0; 88038689Sborman } else if (ct->handler) { 88138689Sborman (*ct->handler)(argv[2]); 88238689Sborman printf("%s set to \"%s\".\n", ct->name, ct->charp); 88332144Sminshall } else { 88432144Sminshall if (strcmp("off", argv[2])) { 88532144Sminshall value = special(argv[2]); 88632144Sminshall } else { 88732144Sminshall value = -1; 88832144Sminshall } 88932144Sminshall *(ct->charp) = value; 89032144Sminshall printf("%s character is '%s'.\n", ct->name, control(*(ct->charp))); 89132144Sminshall } 89238689Sborman slc_check(); 89332144Sminshall return 1; 89432144Sminshall } 89538689Sborman 89638689Sborman static 89738689Sborman unsetcmd(argc, argv) 89838689Sborman int argc; 89938689Sborman char *argv[]; 90038689Sborman { 90138689Sborman int value; 90238689Sborman struct setlist *ct; 90338689Sborman struct togglelist *c; 90438689Sborman register char *name; 90538689Sborman 90638689Sborman if (argc < 2) { 90738689Sborman fprintf(stderr, 90838689Sborman "Need an argument to 'unset' command. 'unset ?' for help.\n"); 90938689Sborman return 0; 91038689Sborman } 91138689Sborman if ((!strcmp(argv[1], "?")) || (!strcmp(argv[1], "help"))) { 91238689Sborman for (ct = Setlist; ct->name; ct++) 91338689Sborman printf("%-15s %s\n", ct->name, ct->help); 91438689Sborman printf("\n"); 91538689Sborman settogglehelp(0); 91638689Sborman printf("%-15s %s\n", "?", "display help information"); 91738689Sborman return 0; 91838689Sborman } 91938689Sborman 92038689Sborman argc--; 92138689Sborman argv++; 92238689Sborman while (argc--) { 92338689Sborman name = *argv++; 92438689Sborman ct = getset(name); 92538689Sborman if (ct == 0) { 92638689Sborman c = gettoggle(name); 92738689Sborman if (c == 0) { 92838689Sborman fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\n", 92938689Sborman name); 93038689Sborman return 0; 93138689Sborman } else if (Ambiguous(c)) { 93238689Sborman fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n", 93338689Sborman name); 93438689Sborman return 0; 93538689Sborman } 93638689Sborman if (c->variable) { 93738689Sborman *c->variable = 0; 93838689Sborman if (c->actionexplanation) { 93938689Sborman printf("%s %s.\n", *c->variable? "Will" : "Won't", 94038689Sborman c->actionexplanation); 94138689Sborman } 94238689Sborman } 94338689Sborman if (c->handler) 94438689Sborman (*c->handler)(0); 94538689Sborman } else if (Ambiguous(ct)) { 94638689Sborman fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n", 94738689Sborman name); 94838689Sborman return 0; 94938689Sborman } else if (ct->handler) { 95038689Sborman (*ct->handler)(0); 95138689Sborman printf("%s reset to \"%s\".\n", ct->name, ct->charp); 95238689Sborman } else { 95338689Sborman value = -1; 95438689Sborman *(ct->charp) = -1; 95538689Sborman printf("%s character is '%s'.\n", ct->name, control(*(ct->charp))); 95638689Sborman } 95738689Sborman } 95838689Sborman return 1; 95938689Sborman } 96032144Sminshall 96132144Sminshall /* 96232144Sminshall * The following are the data structures and routines for the 96332144Sminshall * 'mode' command. 96432144Sminshall */ 96538689Sborman #ifdef KLUDGELINEMODE 96638689Sborman extern int kludgelinemode; 96738689Sborman #endif 96832144Sminshall 96932144Sminshall static 97032144Sminshall dolinemode() 97132144Sminshall { 97238689Sborman #ifdef KLUDGELINEMODE 97338689Sborman if (kludgelinemode) 97438689Sborman send_dont(TELOPT_SGA, 1); 97538689Sborman #endif 97638689Sborman send_will(TELOPT_LINEMODE, 1); 97738689Sborman send_dont(TELOPT_ECHO, 1); 97832144Sminshall return 1; 97932144Sminshall } 98032144Sminshall 98132144Sminshall static 98232144Sminshall docharmode() 98332144Sminshall { 98438689Sborman #ifdef KLUDGELINEMODE 98538689Sborman if (kludgelinemode) 98638689Sborman send_do(TELOPT_SGA, 1); 98738689Sborman else 98838689Sborman #endif 98938689Sborman send_wont(TELOPT_LINEMODE, 1); 99038689Sborman send_do(TELOPT_ECHO, 1); 99138689Sborman return 1; 99238689Sborman } 99338689Sborman 99438689Sborman setmode(bit) 99538689Sborman { 99638689Sborman return dolmmode(bit, 1); 99738689Sborman } 99838689Sborman 99938689Sborman clearmode(bit) 100038689Sborman { 100138689Sborman return dolmmode(bit, 0); 100238689Sborman } 100338689Sborman 100438689Sborman dolmmode(bit, on) 100538689Sborman int bit, on; 100638689Sborman { 100738689Sborman char c; 100838689Sborman extern int linemode; 100938689Sborman 101038689Sborman if (my_want_state_is_wont(TELOPT_LINEMODE)) { 101138689Sborman printf("?Need to have LINEMODE option enabled first.\n"); 101238689Sborman printf("'mode ?' for help.\n"); 101338689Sborman return 0; 101432144Sminshall } 101538689Sborman 101638689Sborman if (on) 101738689Sborman c = (linemode | bit); 101838689Sborman else 101938689Sborman c = (linemode & ~bit); 102038689Sborman lm_mode(&c, 1, 1); 102132144Sminshall return 1; 102232144Sminshall } 102332144Sminshall 102438689Sborman struct modelist { 102538689Sborman char *name; /* command name */ 102638689Sborman char *help; /* help string */ 102738689Sborman int (*handler)(); /* routine which executes command */ 102838689Sborman int needconnect; /* Do we need to be connected to execute? */ 102938689Sborman int arg1; 103038689Sborman }; 103138689Sborman 103238689Sborman extern int modehelp(); 103338689Sborman 103438689Sborman static struct modelist ModeList[] = { 103538689Sborman { "character", "Disable LINEMODE option", docharmode, 1 }, 1036*39529Sborman #ifdef KLUDGELINEMODE 1037*39529Sborman { "", "(or disable obsolete line-by-line mode)", 0 }, 103838689Sborman #endif 103938689Sborman { "line", "Enable LINEMODE option", dolinemode, 1 }, 1040*39529Sborman #ifdef KLUDGELINEMODE 1041*39529Sborman { "", "(or enable obsolete line-by-line mode)", 0 }, 104238689Sborman #endif 104338689Sborman { "", "", 0 }, 104438689Sborman { "", "These require the LINEMODE option to be enabled", 0 }, 104538689Sborman { "isig", "Enable signal trapping", setmode, 1, MODE_TRAPSIG }, 104638689Sborman { "+isig", 0, setmode, 1, MODE_TRAPSIG }, 104738689Sborman { "-isig", "Disable signal trapping", clearmode, 1, MODE_TRAPSIG }, 104838689Sborman { "edit", "Enable character editing", setmode, 1, MODE_EDIT }, 104938689Sborman { "+edit", 0, setmode, 1, MODE_EDIT }, 105038689Sborman { "-edit", "Disable character editing", clearmode, 1, MODE_EDIT }, 105138689Sborman { "help", 0, modehelp, 0 }, 105238689Sborman { "?", "Print help information", modehelp, 0 }, 105332144Sminshall { 0 }, 105432144Sminshall }; 105532144Sminshall 105632144Sminshall static char ** 105732144Sminshall getnextmode(name) 105832144Sminshall char *name; 105932144Sminshall { 106038689Sborman return (char **) (((struct modelist *)name)+1); 106132144Sminshall } 106232144Sminshall 106338689Sborman static struct modelist * 106432144Sminshall getmodecmd(name) 106532144Sminshall char *name; 106632144Sminshall { 106738689Sborman return (struct modelist *) genget(name, (char **) ModeList, getnextmode); 106832144Sminshall } 106932144Sminshall 107038689Sborman modehelp() 107138689Sborman { 107238689Sborman struct modelist *mt; 107338689Sborman 107438689Sborman printf("format is: 'mode Mode', where 'Mode' is one of:\n\n"); 107538689Sborman for (mt = ModeList; mt->name; mt++) { 107638689Sborman if (mt->help) { 107738689Sborman if (*mt->help) 107838689Sborman printf("%-15s %s\n", mt->name, mt->help); 107938689Sborman else 108038689Sborman printf("\n"); 108138689Sborman } 108238689Sborman } 108338689Sborman return 0; 108438689Sborman } 108538689Sborman 108632144Sminshall static 108732144Sminshall modecmd(argc, argv) 108832144Sminshall int argc; 108932144Sminshall char *argv[]; 109032144Sminshall { 109138689Sborman struct modelist *mt; 109232144Sminshall 109338689Sborman if (argc != 2) { 109438689Sborman printf("'mode' command requires an argument\n"); 109538689Sborman printf("'mode ?' for help.\n"); 109638689Sborman } else if ((mt = getmodecmd(argv[1])) == 0) { 109732144Sminshall fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\n", argv[1]); 109832144Sminshall } else if (Ambiguous(mt)) { 109932144Sminshall fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]); 110038689Sborman } else if (mt->needconnect && !connected) { 110138689Sborman printf("?Need to be connected first.\n"); 110238689Sborman printf("'mode ?' for help.\n"); 110338689Sborman } else if (mt->handler) { 110438689Sborman return (*mt->handler)(mt->arg1); 110532144Sminshall } 110638689Sborman return 0; 110732144Sminshall } 110832144Sminshall 110932144Sminshall /* 111032144Sminshall * The following data structures and routines implement the 111132144Sminshall * "display" command. 111232144Sminshall */ 111332144Sminshall 111432144Sminshall static 111532144Sminshall display(argc, argv) 111632144Sminshall int argc; 111732144Sminshall char *argv[]; 111832144Sminshall { 111932144Sminshall #define dotog(tl) if (tl->variable && tl->actionexplanation) { \ 112032144Sminshall if (*tl->variable) { \ 112132144Sminshall printf("will"); \ 112232144Sminshall } else { \ 112332144Sminshall printf("won't"); \ 112432144Sminshall } \ 112532144Sminshall printf(" %s.\n", tl->actionexplanation); \ 112632144Sminshall } 112732144Sminshall 112832144Sminshall #define doset(sl) if (sl->name && *sl->name != ' ') { \ 112938689Sborman if (sl->handler == 0) \ 113038689Sborman printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \ 113138689Sborman else \ 113238689Sborman printf("%-15s \"%s\"\n", sl->name, sl->charp); \ 113332144Sminshall } 113432144Sminshall 113532144Sminshall struct togglelist *tl; 113632144Sminshall struct setlist *sl; 113732144Sminshall 113832144Sminshall if (argc == 1) { 113932144Sminshall for (tl = Togglelist; tl->name; tl++) { 114032144Sminshall dotog(tl); 114132144Sminshall } 114232144Sminshall printf("\n"); 114332144Sminshall for (sl = Setlist; sl->name; sl++) { 114432144Sminshall doset(sl); 114532144Sminshall } 114632144Sminshall } else { 114732144Sminshall int i; 114832144Sminshall 114932144Sminshall for (i = 1; i < argc; i++) { 115032144Sminshall sl = getset(argv[i]); 115132144Sminshall tl = gettoggle(argv[i]); 115232144Sminshall if (Ambiguous(sl) || Ambiguous(tl)) { 115332144Sminshall printf("?Ambiguous argument '%s'.\n", argv[i]); 115432144Sminshall return 0; 115532144Sminshall } else if (!sl && !tl) { 115632144Sminshall printf("?Unknown argument '%s'.\n", argv[i]); 115732144Sminshall return 0; 115832144Sminshall } else { 115932144Sminshall if (tl) { 116032144Sminshall dotog(tl); 116132144Sminshall } 116232144Sminshall if (sl) { 116332144Sminshall doset(sl); 116432144Sminshall } 116532144Sminshall } 116632144Sminshall } 116732144Sminshall } 116838689Sborman /*@*/optionstatus(); 116932144Sminshall return 1; 117032144Sminshall #undef doset 117132144Sminshall #undef dotog 117232144Sminshall } 117332144Sminshall 117432144Sminshall /* 117532144Sminshall * The following are the data structures, and many of the routines, 117632144Sminshall * relating to command processing. 117732144Sminshall */ 117832144Sminshall 117932144Sminshall /* 118032144Sminshall * Set the escape character. 118132144Sminshall */ 118232144Sminshall static 118332144Sminshall setescape(argc, argv) 118432144Sminshall int argc; 118532144Sminshall char *argv[]; 118632144Sminshall { 118732144Sminshall register char *arg; 118832144Sminshall char buf[50]; 118932144Sminshall 119032144Sminshall printf( 119132144Sminshall "Deprecated usage - please use 'set escape%s%s' in the future.\n", 119232144Sminshall (argc > 2)? " ":"", (argc > 2)? argv[1]: ""); 119332144Sminshall if (argc > 2) 119432144Sminshall arg = argv[1]; 119532144Sminshall else { 119632144Sminshall printf("new escape character: "); 119734849Sminshall (void) gets(buf); 119832144Sminshall arg = buf; 119932144Sminshall } 120032144Sminshall if (arg[0] != '\0') 120132144Sminshall escape = arg[0]; 120232144Sminshall if (!In3270) { 120332144Sminshall printf("Escape character is '%s'.\n", control(escape)); 120432144Sminshall } 120534849Sminshall (void) fflush(stdout); 120632144Sminshall return 1; 120732144Sminshall } 120832144Sminshall 120932144Sminshall /*VARARGS*/ 121032144Sminshall static 121132144Sminshall togcrmod() 121232144Sminshall { 121332144Sminshall crmod = !crmod; 121432144Sminshall printf("Deprecated usage - please use 'toggle crmod' in the future.\n"); 121532144Sminshall printf("%s map carriage return on output.\n", crmod ? "Will" : "Won't"); 121634849Sminshall (void) fflush(stdout); 121732144Sminshall return 1; 121832144Sminshall } 121932144Sminshall 122032144Sminshall /*VARARGS*/ 122132144Sminshall suspend() 122232144Sminshall { 122338689Sborman #ifdef SIGTSTP 122437219Sminshall setcommandmode(); 122537219Sminshall { 122637219Sminshall long oldrows, oldcols, newrows, newcols; 122737219Sminshall 122837219Sminshall TerminalWindowSize(&oldrows, &oldcols); 122934849Sminshall (void) kill(0, SIGTSTP); 123037219Sminshall TerminalWindowSize(&newrows, &newcols); 123137219Sminshall if ((oldrows != newrows) || (oldcols != newcols)) { 123237219Sminshall if (connected) { 123337219Sminshall sendnaws(); 123437219Sminshall } 123537219Sminshall } 123637219Sminshall } 123737219Sminshall /* reget parameters in case they were changed */ 123837219Sminshall TerminalSaveState(); 123938689Sborman setconnmode(0); 124038689Sborman #else 124138689Sborman printf("Suspend is not supported. Try the '!' command instead\n"); 124238689Sborman #endif 124337219Sminshall return 1; 124432144Sminshall } 124532144Sminshall 124638689Sborman #if !defined(TN3270) 124738689Sborman shell(argc, argv) 124838689Sborman int argc; 124938689Sborman char *argv[]; 125038689Sborman { 125138689Sborman extern char *rindex(); 125238689Sborman char cmdbuf[256]; 125338689Sborman 125438689Sborman setcommandmode(); 125538689Sborman switch(vfork()) { 125638689Sborman case -1: 125738689Sborman perror("Fork failed\n"); 125838689Sborman break; 125938689Sborman 126038689Sborman case 0: 126138689Sborman { 126238689Sborman /* 126338689Sborman * Fire up the shell in the child. 126438689Sborman */ 126538689Sborman register char *shell, *shellname; 126638689Sborman 126738689Sborman shell = getenv("SHELL"); 126838689Sborman if (shell == NULL) 126938689Sborman shell = "/bin/sh"; 127038689Sborman if ((shellname = rindex(shell, '/')) == 0) 127138689Sborman shellname = shell; 127238689Sborman else 127338689Sborman shellname++; 127438689Sborman if (argc > 1) 127538689Sborman execl(shell, shellname, "-c", &saveline[1], 0); 127638689Sborman else 127738689Sborman execl(shell, shellname, 0); 127838689Sborman perror("Execl"); 127938689Sborman _exit(1); 128038689Sborman } 128138689Sborman default: 128238689Sborman wait((int *)0); /* Wait for the shell to complete */ 128338689Sborman } 128438689Sborman } 128538689Sborman #endif /* !defined(TN3270) */ 128638689Sborman 128732144Sminshall /*VARARGS*/ 128832144Sminshall static 128932144Sminshall bye(argc, argv) 129032144Sminshall int argc; /* Number of arguments */ 129132144Sminshall char *argv[]; /* arguments */ 129232144Sminshall { 129332144Sminshall if (connected) { 129434849Sminshall (void) shutdown(net, 2); 129532144Sminshall printf("Connection closed.\n"); 129634849Sminshall (void) NetClose(net); 129732144Sminshall connected = 0; 129832144Sminshall /* reset options */ 129932144Sminshall tninit(); 130032144Sminshall #if defined(TN3270) 130132144Sminshall SetIn3270(); /* Get out of 3270 mode */ 130232144Sminshall #endif /* defined(TN3270) */ 130332144Sminshall } 130432144Sminshall if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) { 130532144Sminshall longjmp(toplevel, 1); 130632144Sminshall /* NOTREACHED */ 130732144Sminshall } 130832144Sminshall return 1; /* Keep lint, etc., happy */ 130932144Sminshall } 131032144Sminshall 131132144Sminshall /*VARARGS*/ 131232144Sminshall quit() 131332144Sminshall { 131432144Sminshall (void) call(bye, "bye", "fromquit", 0); 131532144Sminshall Exit(0); 131632144Sminshall return 1; /* just to keep lint happy */ 131732144Sminshall } 131838689Sborman 131938689Sborman /* 132038689Sborman * The SLC command. 132138689Sborman */ 132232144Sminshall 132338689Sborman struct slclist { 132438689Sborman char *name; 132538689Sborman char *help; 132638689Sborman int (*handler)(); 132738689Sborman int arg; 132838689Sborman }; 132938689Sborman 133038689Sborman extern int slc_help(); 133138689Sborman extern int slc_mode_export(), slc_mode_import(), slcstate(); 133238689Sborman 133338689Sborman struct slclist SlcList[] = { 133438689Sborman { "export", "Use local special character definitions", 133538689Sborman slc_mode_export, 0 }, 133638689Sborman { "import", "Use remote special character definitions", 133738689Sborman slc_mode_import, 1 }, 133838689Sborman { "check", "Verify remote special character definitions", 133938689Sborman slc_mode_import, 0 }, 134038689Sborman { "help", 0, slc_help, 0 }, 134138689Sborman { "?", "Print help information", slc_help, 0 }, 134238689Sborman { 0 }, 134338689Sborman }; 134438689Sborman 134538689Sborman static 134638689Sborman slc_help() 134738689Sborman { 134838689Sborman struct slclist *c; 134938689Sborman 135038689Sborman for (c = SlcList; c->name; c++) { 135138689Sborman if (c->help) { 135238689Sborman if (*c->help) 135338689Sborman printf("%-15s %s\n", c->name, c->help); 135438689Sborman else 135538689Sborman printf("\n"); 135638689Sborman } 135738689Sborman } 135838689Sborman } 135938689Sborman 136038689Sborman static char ** 136138689Sborman getnextslc(name) 136238689Sborman char *name; 136338689Sborman { 136438689Sborman return (char **)(((struct slclist *)name)+1); 136538689Sborman } 136638689Sborman 136738689Sborman static struct slclist * 136838689Sborman getslc(name) 136938689Sborman char *name; 137038689Sborman { 137138689Sborman return (struct slclist *)genget(name, (char **) SlcList, getnextslc); 137238689Sborman } 137338689Sborman 137438689Sborman static 137538689Sborman slccmd(argc, argv) 137638689Sborman int argc; 137738689Sborman char *argv[]; 137838689Sborman { 137938689Sborman struct slclist *c; 138038689Sborman 138138689Sborman if (argc != 2) { 138238689Sborman fprintf(stderr, 138338689Sborman "Need an argument to 'slc' command. 'slc ?' for help.\n"); 138438689Sborman return 0; 138538689Sborman } 138638689Sborman c = getslc(argv[1]); 138738689Sborman if (c == 0) { 138838689Sborman fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\n", 138938689Sborman argv[1]); 139038689Sborman return 0; 139138689Sborman } 139238689Sborman if (Ambiguous(c)) { 139338689Sborman fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\n", 139438689Sborman argv[1]); 139538689Sborman return 0; 139638689Sborman } 139738689Sborman (*c->handler)(c->arg); 139838689Sborman slcstate(); 139938689Sborman return 1; 140038689Sborman } 140138689Sborman 140236274Sminshall #if defined(unix) 140332144Sminshall /* 140436274Sminshall * Some information about our file descriptors. 140536274Sminshall */ 140636274Sminshall 140736274Sminshall char * 140836274Sminshall decodeflags(mask) 140936274Sminshall int mask; 141036274Sminshall { 141136274Sminshall static char buffer[100]; 141236274Sminshall #define do(m,s) \ 141336274Sminshall if (mask&(m)) { \ 141436274Sminshall strcat(buffer, (s)); \ 141536274Sminshall } 141636274Sminshall 141736274Sminshall buffer[0] = 0; /* Terminate it */ 141836274Sminshall 141936274Sminshall #ifdef FREAD 142036274Sminshall do(FREAD, " FREAD"); 142136274Sminshall #endif 142236274Sminshall #ifdef FWRITE 142336274Sminshall do(FWRITE, " FWRITE"); 142436274Sminshall #endif 142536274Sminshall #ifdef F_DUPFP 142636274Sminshall do(F_DUPFD, " F_DUPFD"); 142736274Sminshall #endif 142836274Sminshall #ifdef FNDELAY 142936274Sminshall do(FNDELAY, " FNDELAY"); 143036274Sminshall #endif 143136274Sminshall #ifdef FAPPEND 143236274Sminshall do(FAPPEND, " FAPPEND"); 143336274Sminshall #endif 143436274Sminshall #ifdef FMARK 143536274Sminshall do(FMARK, " FMARK"); 143636274Sminshall #endif 143736274Sminshall #ifdef FDEFER 143836274Sminshall do(FDEFER, " FDEFER"); 143936274Sminshall #endif 144036274Sminshall #ifdef FASYNC 144136274Sminshall do(FASYNC, " FASYNC"); 144236274Sminshall #endif 144336274Sminshall #ifdef FSHLOCK 144436274Sminshall do(FSHLOCK, " FSHLOCK"); 144536274Sminshall #endif 144636274Sminshall #ifdef FEXLOCK 144736274Sminshall do(FEXLOCK, " FEXLOCK"); 144836274Sminshall #endif 144936274Sminshall #ifdef FCREAT 145036274Sminshall do(FCREAT, " FCREAT"); 145136274Sminshall #endif 145236274Sminshall #ifdef FTRUNC 145336274Sminshall do(FTRUNC, " FTRUNC"); 145436274Sminshall #endif 145536274Sminshall #ifdef FEXCL 145636274Sminshall do(FEXCL, " FEXCL"); 145736274Sminshall #endif 145836274Sminshall 145936274Sminshall return buffer; 146036274Sminshall } 146136274Sminshall #undef do 146236274Sminshall 146336274Sminshall static void 146436274Sminshall filestuff(fd) 146536274Sminshall int fd; 146636274Sminshall { 146736274Sminshall int res; 146836274Sminshall 146938689Sborman #ifdef F_GETOWN 147038689Sborman setconnmode(0); 147136274Sminshall res = fcntl(fd, F_GETOWN, 0); 147236274Sminshall setcommandmode(); 147336274Sminshall 147436274Sminshall if (res == -1) { 147536274Sminshall perror("fcntl"); 147636274Sminshall return; 147736274Sminshall } 147836274Sminshall printf("\tOwner is %d.\n", res); 147938689Sborman #endif 148036274Sminshall 148138689Sborman setconnmode(0); 148236274Sminshall res = fcntl(fd, F_GETFL, 0); 148336274Sminshall setcommandmode(); 148436274Sminshall 148536274Sminshall if (res == -1) { 148636274Sminshall perror("fcntl"); 148736274Sminshall return; 148836274Sminshall } 148936274Sminshall printf("\tFlags are 0x%x: %s\n", res, decodeflags(res)); 149036274Sminshall } 149136274Sminshall 149236274Sminshall 149336274Sminshall #endif /* defined(unix) */ 149436274Sminshall 149536274Sminshall /* 149632144Sminshall * Print status about the connection. 149732144Sminshall */ 149834849Sminshall /*ARGSUSED*/ 149932144Sminshall static 150032144Sminshall status(argc, argv) 150132144Sminshall int argc; 150232144Sminshall char *argv[]; 150332144Sminshall { 150432144Sminshall if (connected) { 150532144Sminshall printf("Connected to %s.\n", hostname); 150636242Sminshall if ((argc < 2) || strcmp(argv[1], "notmuch")) { 150738689Sborman int mode = getconnmode(); 150838689Sborman 150938689Sborman if (my_want_state_is_will(TELOPT_LINEMODE)) { 151038689Sborman printf("Operating with LINEMODE option\n"); 151138689Sborman printf("%s line editing\n", (mode&MODE_EDIT) ? "Local" : "No"); 151238689Sborman printf("%s catching of signals\n", 151338689Sborman (mode&MODE_TRAPSIG) ? "Local" : "No"); 151438689Sborman slcstate(); 151538689Sborman #ifdef KLUDGELINEMODE 1516*39529Sborman } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) { 151738689Sborman printf("Operating in obsolete linemode\n"); 151838689Sborman #endif 151938689Sborman } else { 152038689Sborman printf("Operating in single character mode\n"); 152138689Sborman if (localchars) 152238689Sborman printf("Catching signals locally\n"); 152332144Sminshall } 152438689Sborman printf("%s character echo\n", (mode&MODE_ECHO) ? "Local" : "Remote"); 152538689Sborman if (my_want_state_is_will(TELOPT_LFLOW)) 152638689Sborman printf("%s flow control\n", (mode&MODE_FLOW) ? "Local" : "No"); 152732144Sminshall } 152832144Sminshall } else { 152932144Sminshall printf("No connection.\n"); 153032144Sminshall } 153132144Sminshall # if !defined(TN3270) 153232144Sminshall printf("Escape character is '%s'.\n", control(escape)); 153334849Sminshall (void) fflush(stdout); 153432144Sminshall # else /* !defined(TN3270) */ 153532144Sminshall if ((!In3270) && ((argc < 2) || strcmp(argv[1], "notmuch"))) { 153632144Sminshall printf("Escape character is '%s'.\n", control(escape)); 153732144Sminshall } 153832144Sminshall # if defined(unix) 153936242Sminshall if ((argc >= 2) && !strcmp(argv[1], "everything")) { 154036242Sminshall printf("SIGIO received %d time%s.\n", 154136242Sminshall sigiocount, (sigiocount == 1)? "":"s"); 154236274Sminshall if (In3270) { 154336274Sminshall printf("Process ID %d, process group %d.\n", 154436274Sminshall getpid(), getpgrp(getpid())); 154536274Sminshall printf("Terminal input:\n"); 154636274Sminshall filestuff(tin); 154736274Sminshall printf("Terminal output:\n"); 154836274Sminshall filestuff(tout); 154936274Sminshall printf("Network socket:\n"); 155036274Sminshall filestuff(net); 155136274Sminshall } 155236242Sminshall } 155332144Sminshall if (In3270 && transcom) { 155432144Sminshall printf("Transparent mode command is '%s'.\n", transcom); 155532144Sminshall } 155632144Sminshall # endif /* defined(unix) */ 155734849Sminshall (void) fflush(stdout); 155832144Sminshall if (In3270) { 155932144Sminshall return 0; 156032144Sminshall } 156132144Sminshall # endif /* defined(TN3270) */ 156232144Sminshall return 1; 156332144Sminshall } 156432144Sminshall 156532144Sminshall 156632144Sminshall 156732144Sminshall int 156832144Sminshall tn(argc, argv) 156932144Sminshall int argc; 157032144Sminshall char *argv[]; 157132144Sminshall { 157232144Sminshall register struct hostent *host = 0; 157332144Sminshall struct sockaddr_in sin; 157432144Sminshall struct servent *sp = 0; 157532144Sminshall static char hnamebuf[32]; 157638689Sborman unsigned long temp, inet_addr(); 157737219Sminshall extern char *inet_ntoa(); 157838689Sborman #if defined(SRCRT) && defined(IPPROTO_IP) 157938689Sborman char *srp = 0, *strrchr(); 158038689Sborman unsigned long sourceroute(), srlen; 158138689Sborman #endif 158232144Sminshall 158332144Sminshall 158432144Sminshall if (connected) { 158532144Sminshall printf("?Already connected to %s\n", hostname); 158632144Sminshall return 0; 158732144Sminshall } 158832144Sminshall if (argc < 2) { 158932144Sminshall (void) strcpy(line, "Connect "); 159032144Sminshall printf("(to) "); 159134849Sminshall (void) gets(&line[strlen(line)]); 159232144Sminshall makeargv(); 159332144Sminshall argc = margc; 159432144Sminshall argv = margv; 159532144Sminshall } 159632144Sminshall if ((argc < 2) || (argc > 3)) { 159732144Sminshall printf("usage: %s host-name [port]\n", argv[0]); 159832144Sminshall return 0; 159932144Sminshall } 160038689Sborman #if defined(SRCRT) && defined(IPPROTO_IP) 160138689Sborman if (argv[1][0] == '@' || argv[1][0] == '!') { 160238689Sborman if ((hostname = strrchr(argv[1], ':')) == NULL) 160338689Sborman hostname = strrchr(argv[1], '@'); 160438689Sborman hostname++; 160538689Sborman srp = 0; 160638689Sborman temp = sourceroute(argv[1], &srp, &srlen); 160738689Sborman if (temp == 0) { 160838689Sborman herror(srp); 160938689Sborman return 0; 161038689Sborman } else if (temp == -1) { 161138689Sborman printf("Bad source route option: %s\n", argv[1]); 161238689Sborman return 0; 161338689Sborman } else { 161438689Sborman sin.sin_addr.s_addr = temp; 161538689Sborman sin.sin_family = AF_INET; 161638689Sborman } 161732144Sminshall } else { 161838689Sborman #endif 161938689Sborman temp = inet_addr(argv[1]); 162038689Sborman if (temp != (unsigned long) -1) { 162138689Sborman sin.sin_addr.s_addr = temp; 162238689Sborman sin.sin_family = AF_INET; 162338689Sborman (void) strcpy(hnamebuf, argv[1]); 162438689Sborman hostname = hnamebuf; 162538689Sborman } else { 162638689Sborman host = gethostbyname(argv[1]); 162738689Sborman if (host) { 162838689Sborman sin.sin_family = host->h_addrtype; 162932144Sminshall #if defined(h_addr) /* In 4.3, this is a #define */ 163038689Sborman memcpy((caddr_t)&sin.sin_addr, 163132144Sminshall host->h_addr_list[0], host->h_length); 163232144Sminshall #else /* defined(h_addr) */ 163338689Sborman memcpy((caddr_t)&sin.sin_addr, host->h_addr, host->h_length); 163432144Sminshall #endif /* defined(h_addr) */ 163538689Sborman hostname = host->h_name; 163638689Sborman } else { 163738689Sborman herror(argv[1]); 163838689Sborman return 0; 163938689Sborman } 164032144Sminshall } 164138689Sborman #if defined(SRCRT) && defined(IPPROTO_IP) 164232144Sminshall } 164338689Sborman #endif 164432144Sminshall if (argc == 3) { 164538689Sborman int tmp; 164638689Sborman 164738689Sborman if (*argv[2] == '-') { 164838689Sborman argv[2]++; 164938689Sborman telnetport = 1; 165038689Sborman } else 165138689Sborman telnetport = 0; 165232144Sminshall sin.sin_port = atoi(argv[2]); 165332144Sminshall if (sin.sin_port == 0) { 165432144Sminshall sp = getservbyname(argv[2], "tcp"); 165532144Sminshall if (sp) 165632144Sminshall sin.sin_port = sp->s_port; 165732144Sminshall else { 165832144Sminshall printf("%s: bad port number\n", argv[2]); 165932144Sminshall return 0; 166032144Sminshall } 166132144Sminshall } else { 166234849Sminshall #if !defined(htons) 166334849Sminshall u_short htons(); 166434849Sminshall #endif /* !defined(htons) */ 166532144Sminshall sin.sin_port = htons(sin.sin_port); 166632144Sminshall } 166732144Sminshall } else { 166832144Sminshall if (sp == 0) { 166932144Sminshall sp = getservbyname("telnet", "tcp"); 167032144Sminshall if (sp == 0) { 167134849Sminshall fprintf(stderr, "telnet: tcp/telnet: unknown service\n"); 167232144Sminshall return 0; 167332144Sminshall } 167432144Sminshall sin.sin_port = sp->s_port; 167532144Sminshall } 167632144Sminshall telnetport = 1; 167732144Sminshall } 167837219Sminshall printf("Trying %s...\n", inet_ntoa(sin.sin_addr)); 167932144Sminshall do { 168032144Sminshall net = socket(AF_INET, SOCK_STREAM, 0); 168132144Sminshall if (net < 0) { 168232144Sminshall perror("telnet: socket"); 168332144Sminshall return 0; 168432144Sminshall } 168538689Sborman #if defined(SRCRT) && defined(IPPROTO_IP) 168638689Sborman if (srp && setsockopt(net, IPPROTO_IP, IP_OPTIONS, (char *)srp, srlen) < 0) 168738689Sborman perror("setsockopt (IP_OPTIONS)"); 168838689Sborman #endif 168932144Sminshall if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) { 169032144Sminshall perror("setsockopt (SO_DEBUG)"); 169132144Sminshall } 169232144Sminshall 169332144Sminshall if (connect(net, (struct sockaddr *)&sin, sizeof (sin)) < 0) { 169432144Sminshall #if defined(h_addr) /* In 4.3, this is a #define */ 169532144Sminshall if (host && host->h_addr_list[1]) { 169632144Sminshall int oerrno = errno; 169732144Sminshall 169832144Sminshall fprintf(stderr, "telnet: connect to address %s: ", 169932144Sminshall inet_ntoa(sin.sin_addr)); 170032144Sminshall errno = oerrno; 170132144Sminshall perror((char *)0); 170232144Sminshall host->h_addr_list++; 170332144Sminshall memcpy((caddr_t)&sin.sin_addr, 170432144Sminshall host->h_addr_list[0], host->h_length); 170532144Sminshall (void) NetClose(net); 170632144Sminshall continue; 170732144Sminshall } 170832144Sminshall #endif /* defined(h_addr) */ 170932144Sminshall perror("telnet: Unable to connect to remote host"); 171032144Sminshall return 0; 171137219Sminshall } 171232144Sminshall connected++; 171332144Sminshall } while (connected == 0); 171438689Sborman cmdrc(argv[1], hostname); 171534849Sminshall (void) call(status, "status", "notmuch", 0); 171632144Sminshall if (setjmp(peerdied) == 0) 171732144Sminshall telnet(); 171834849Sminshall (void) NetClose(net); 171932381Sminshall ExitString("Connection closed by foreign host.\n",1); 172032144Sminshall /*NOTREACHED*/ 172132144Sminshall } 172232144Sminshall 172332144Sminshall 172432144Sminshall #define HELPINDENT (sizeof ("connect")) 172532144Sminshall 172632144Sminshall static char 172732144Sminshall openhelp[] = "connect to a site", 172832144Sminshall closehelp[] = "close current connection", 172932144Sminshall quithelp[] = "exit telnet", 173032144Sminshall statushelp[] = "print status information", 173132144Sminshall helphelp[] = "print help information", 173232144Sminshall sendhelp[] = "transmit special characters ('send ?' for more)", 173332144Sminshall sethelp[] = "set operating parameters ('set ?' for more)", 173438689Sborman unsethelp[] = "unset operating parameters ('unset ?' for more)", 173532144Sminshall togglestring[] ="toggle operating parameters ('toggle ?' for more)", 173638689Sborman slchelp[] = "change state of special charaters ('slc ?' for more)", 173732144Sminshall displayhelp[] = "display operating parameters", 173832144Sminshall #if defined(TN3270) && defined(unix) 173932144Sminshall transcomhelp[] = "specify Unix command for transparent mode pipe", 174032144Sminshall #endif /* defined(TN3270) && defined(unix) */ 174132144Sminshall #if defined(unix) 174232144Sminshall zhelp[] = "suspend telnet", 174332144Sminshall #endif /* defined(unix */ 174432144Sminshall shellhelp[] = "invoke a subshell", 174538689Sborman modestring[] = "try to enter line-by-line or character-at-a-time mode"; 174632144Sminshall 174732144Sminshall extern int help(), shell(); 174832144Sminshall 174932144Sminshall static Command cmdtab[] = { 175038689Sborman { "close", closehelp, bye, 1 }, 175138689Sborman { "display", displayhelp, display, 0 }, 175238689Sborman { "mode", modestring, modecmd, 0 }, 175338689Sborman { "open", openhelp, tn, 0 }, 175438689Sborman { "quit", quithelp, quit, 0 }, 175538689Sborman { "send", sendhelp, sendcmd, 0 }, 175638689Sborman { "set", sethelp, setcmd, 0 }, 175738689Sborman { "unset", unsethelp, unsetcmd, 0 }, 175838689Sborman { "status", statushelp, status, 0 }, 175938689Sborman { "toggle", togglestring, toggle, 0 }, 176038689Sborman { "slc", slchelp, slccmd, 0 }, 176132144Sminshall #if defined(TN3270) && defined(unix) 176238689Sborman { "transcom", transcomhelp, settranscom, 0 }, 176332144Sminshall #endif /* defined(TN3270) && defined(unix) */ 176432144Sminshall #if defined(unix) 176538689Sborman { "z", zhelp, suspend, 0 }, 176632144Sminshall #endif /* defined(unix) */ 176732144Sminshall #if defined(TN3270) 176838689Sborman { "!", shellhelp, shell, 1 }, 176938689Sborman #else 177038689Sborman { "!", shellhelp, shell, 0 }, 177138689Sborman #endif 177238689Sborman { "?", helphelp, help, 0 }, 177332144Sminshall 0 177432144Sminshall }; 177532144Sminshall 177632144Sminshall static char crmodhelp[] = "deprecated command -- use 'toggle crmod' instead"; 177732144Sminshall static char escapehelp[] = "deprecated command -- use 'set escape' instead"; 177832144Sminshall 177932144Sminshall static Command cmdtab2[] = { 178038689Sborman { "help", 0, help, 0 }, 178138689Sborman { "escape", escapehelp, setescape, 0 }, 178238689Sborman { "crmod", crmodhelp, togcrmod, 0 }, 178332144Sminshall 0 178432144Sminshall }; 178532144Sminshall 178635298Sminshall 178732144Sminshall /* 178832144Sminshall * Call routine with argc, argv set from args (terminated by 0). 178932144Sminshall */ 179035298Sminshall 179135417Sminshall /*VARARGS1*/ 179232144Sminshall static 179335298Sminshall call(va_alist) 179435298Sminshall va_dcl 179532144Sminshall { 179635298Sminshall va_list ap; 179735298Sminshall typedef int (*intrtn_t)(); 179835298Sminshall intrtn_t routine; 179935298Sminshall char *args[100]; 180035298Sminshall int argno = 0; 180135298Sminshall 180235298Sminshall va_start(ap); 180335298Sminshall routine = (va_arg(ap, intrtn_t)); 180435495Sminshall while ((args[argno++] = va_arg(ap, char *)) != 0) { 180535298Sminshall ; 180635495Sminshall } 180735298Sminshall va_end(ap); 180835495Sminshall return (*routine)(argno-1, args); 180932144Sminshall } 181032144Sminshall 181135298Sminshall 181232144Sminshall static char ** 181332144Sminshall getnextcmd(name) 181432144Sminshall char *name; 181532144Sminshall { 181632144Sminshall Command *c = (Command *) name; 181732144Sminshall 181832144Sminshall return (char **) (c+1); 181932144Sminshall } 182032144Sminshall 182132144Sminshall static Command * 182232144Sminshall getcmd(name) 182332144Sminshall char *name; 182432144Sminshall { 182532144Sminshall Command *cm; 182632144Sminshall 182732144Sminshall if ((cm = (Command *) genget(name, (char **) cmdtab, getnextcmd)) != 0) { 182832144Sminshall return cm; 182932144Sminshall } else { 183032144Sminshall return (Command *) genget(name, (char **) cmdtab2, getnextcmd); 183132144Sminshall } 183232144Sminshall } 183332144Sminshall 183432144Sminshall void 183538689Sborman command(top, tbuf, cnt) 183632144Sminshall int top; 183738689Sborman char *tbuf; 183838689Sborman int cnt; 183932144Sminshall { 184032144Sminshall register Command *c; 184132144Sminshall 184232144Sminshall setcommandmode(); 184332144Sminshall if (!top) { 184432144Sminshall putchar('\n'); 184537219Sminshall #if defined(unix) 184632144Sminshall } else { 184732144Sminshall signal(SIGINT, SIG_DFL); 184832144Sminshall signal(SIGQUIT, SIG_DFL); 184932144Sminshall #endif /* defined(unix) */ 185032144Sminshall } 185132144Sminshall for (;;) { 185232144Sminshall printf("%s> ", prompt); 185338689Sborman if (tbuf) { 185438689Sborman register char *cp; 185538689Sborman cp = line; 185638689Sborman while (cnt > 0 && (*cp++ = *tbuf++) != '\n') 185738689Sborman cnt--; 185838689Sborman tbuf = 0; 185938689Sborman if (cp == line || *--cp != '\n' || cp == line) 186038689Sborman goto getline; 186138689Sborman *cp = '\0'; 186238689Sborman printf("%s\n", line); 186338689Sborman } else { 186438689Sborman getline: 186538689Sborman if (gets(line) == NULL) { 186638689Sborman if (feof(stdin) || ferror(stdin)) 186738689Sborman quit(); 186838689Sborman break; 186938689Sborman } 187032144Sminshall } 187132144Sminshall if (line[0] == 0) 187232144Sminshall break; 187332144Sminshall makeargv(); 187437219Sminshall if (margv[0] == 0) { 187537219Sminshall break; 187637219Sminshall } 187732144Sminshall c = getcmd(margv[0]); 187832144Sminshall if (Ambiguous(c)) { 187932144Sminshall printf("?Ambiguous command\n"); 188032144Sminshall continue; 188132144Sminshall } 188232144Sminshall if (c == 0) { 188332144Sminshall printf("?Invalid command\n"); 188432144Sminshall continue; 188532144Sminshall } 188632144Sminshall if (c->needconnect && !connected) { 188732144Sminshall printf("?Need to be connected first.\n"); 188832144Sminshall continue; 188932144Sminshall } 189032144Sminshall if ((*c->handler)(margc, margv)) { 189132144Sminshall break; 189232144Sminshall } 189332144Sminshall } 189432144Sminshall if (!top) { 189532144Sminshall if (!connected) { 189632144Sminshall longjmp(toplevel, 1); 189732144Sminshall /*NOTREACHED*/ 189832144Sminshall } 189932144Sminshall #if defined(TN3270) 190032144Sminshall if (shell_active == 0) { 190138689Sborman setconnmode(0); 190232144Sminshall } 190332144Sminshall #else /* defined(TN3270) */ 190438689Sborman setconnmode(0); 190532144Sminshall #endif /* defined(TN3270) */ 190632144Sminshall } 190732144Sminshall } 190832144Sminshall 190932144Sminshall /* 191032144Sminshall * Help command. 191132144Sminshall */ 191232144Sminshall static 191332144Sminshall help(argc, argv) 191432144Sminshall int argc; 191532144Sminshall char *argv[]; 191632144Sminshall { 191732144Sminshall register Command *c; 191832144Sminshall 191932144Sminshall if (argc == 1) { 192032144Sminshall printf("Commands may be abbreviated. Commands are:\n\n"); 192132144Sminshall for (c = cmdtab; c->name; c++) 192238689Sborman if (c->help) { 192332144Sminshall printf("%-*s\t%s\n", HELPINDENT, c->name, 192432144Sminshall c->help); 192532144Sminshall } 192632144Sminshall return 0; 192732144Sminshall } 192832144Sminshall while (--argc > 0) { 192932144Sminshall register char *arg; 193032144Sminshall arg = *++argv; 193132144Sminshall c = getcmd(arg); 193232144Sminshall if (Ambiguous(c)) 193332144Sminshall printf("?Ambiguous help command %s\n", arg); 193432144Sminshall else if (c == (Command *)0) 193532144Sminshall printf("?Invalid help command %s\n", arg); 193632144Sminshall else 193732144Sminshall printf("%s\n", c->help); 193832144Sminshall } 193932144Sminshall return 0; 194032144Sminshall } 194138689Sborman 194238689Sborman static char *rcname = 0; 194338689Sborman static char rcbuf[128]; 194438689Sborman 194538689Sborman cmdrc(m1, m2) 194638689Sborman char *m1, *m2; 194738689Sborman { 194838689Sborman register Command *c; 194938689Sborman FILE *rcfile; 195038689Sborman int gotmachine = 0; 195138689Sborman int l1 = strlen(m1); 195238689Sborman int l2 = strlen(m2); 195338689Sborman char m1save[64]; 195438689Sborman 195538689Sborman strcpy(m1save, m1); 195638689Sborman m1 = m1save; 195738689Sborman 195838689Sborman if (rcname == 0) { 195938689Sborman rcname = getenv("HOME"); 196038689Sborman if (rcname) 196138689Sborman strcpy(rcbuf, rcname); 196238689Sborman else 196338689Sborman rcbuf[0] = '\0'; 196438689Sborman strcat(rcbuf, "/.telnetrc"); 196538689Sborman rcname = rcbuf; 196638689Sborman } 196738689Sborman 196838689Sborman if ((rcfile = fopen(rcname, "r")) == 0) { 196938689Sborman return; 197038689Sborman } 197138689Sborman 197238689Sborman for (;;) { 197338689Sborman if (fgets(line, sizeof(line), rcfile) == NULL) 197438689Sborman break; 197538689Sborman if (line[0] == 0) 197638689Sborman break; 197738689Sborman if (line[0] == '#') 197838689Sborman continue; 197938689Sborman if (gotmachine == 0) { 198038689Sborman if (isspace(line[0])) 198138689Sborman continue; 198238689Sborman if (strncasecmp(line, m1, l1) == 0) 198338689Sborman strncpy(line, &line[l1], sizeof(line) - l1); 198438689Sborman else if (strncasecmp(line, m2, l2) == 0) 198538689Sborman strncpy(line, &line[l2], sizeof(line) - l2); 198638689Sborman else 198738689Sborman continue; 198838689Sborman gotmachine = 1; 198938689Sborman } else { 199038689Sborman if (!isspace(line[0])) { 199138689Sborman gotmachine = 0; 199238689Sborman continue; 199338689Sborman } 199438689Sborman } 199538689Sborman makeargv(); 199638689Sborman if (margv[0] == 0) 199738689Sborman continue; 199838689Sborman c = getcmd(margv[0]); 199938689Sborman if (Ambiguous(c)) { 200038689Sborman printf("?Ambiguous command: %s\n", margv[0]); 200138689Sborman continue; 200238689Sborman } 200338689Sborman if (c == 0) { 200438689Sborman printf("?Invalid command: %s\n", margv[0]); 200538689Sborman continue; 200638689Sborman } 200738689Sborman /* 200838689Sborman * This should never happen... 200938689Sborman */ 201038689Sborman if (c->needconnect && !connected) { 201138689Sborman printf("?Need to be connected first for %s.\n", margv[0]); 201238689Sborman continue; 201338689Sborman } 201438689Sborman (*c->handler)(margc, margv); 201538689Sborman } 201638689Sborman fclose(rcfile); 201738689Sborman } 201838689Sborman 201938689Sborman #if defined(SRCRT) && defined(IPPROTO_IP) 202038689Sborman 202138689Sborman /* 202238689Sborman * Source route is handed in as 202338689Sborman * [!]@hop1@hop2...[@|:]dst 202438689Sborman * If the leading ! is present, it is a 202538689Sborman * strict source route, otherwise it is 202638689Sborman * assmed to be a loose source route. 202738689Sborman * 202838689Sborman * We fill in the source route option as 202938689Sborman * hop1,hop2,hop3...dest 203038689Sborman * and return a pointer to hop1, which will 203138689Sborman * be the address to connect() to. 203238689Sborman * 203338689Sborman * Arguments: 203438689Sborman * arg: pointer to route list to decipher 203538689Sborman * 203638689Sborman * cpp: If *cpp is not equal to NULL, this is a 203738689Sborman * pointer to a pointer to a character array 203838689Sborman * that should be filled in with the option. 203938689Sborman * 204038689Sborman * lenp: pointer to an integer that contains the 204138689Sborman * length of *cpp if *cpp != NULL. 204238689Sborman * 204338689Sborman * Return values: 204438689Sborman * 204538689Sborman * Returns the address of the host to connect to. If the 204638689Sborman * return value is -1, there was a syntax error in the 204738689Sborman * option, either unknown characters, or too many hosts. 204838689Sborman * If the return value is 0, one of the hostnames in the 204938689Sborman * path is unknown, and *cpp is set to point to the bad 205038689Sborman * hostname. 205138689Sborman * 205238689Sborman * *cpp: If *cpp was equal to NULL, it will be filled 205338689Sborman * in with a pointer to our static area that has 205438689Sborman * the option filled in. This will be 32bit aligned. 205538689Sborman * 205638689Sborman * *lenp: This will be filled in with how long the option 205738689Sborman * pointed to by *cpp is. 205838689Sborman * 205938689Sborman */ 206038689Sborman unsigned long 206138689Sborman sourceroute(arg, cpp, lenp) 206238689Sborman char *arg; 206338689Sborman char **cpp; 206438689Sborman int *lenp; 206538689Sborman { 206638689Sborman static char lsr[44]; 206738689Sborman char *cp, *cp2, *lsrp, *lsrep, *index(); 206838689Sborman register int tmp; 206938689Sborman struct in_addr sin_addr; 207038689Sborman register struct hostent *host = 0; 207138689Sborman register char c; 207238689Sborman 207338689Sborman /* 207438689Sborman * Verify the arguments, and make sure we have 207538689Sborman * at least 7 bytes for the option. 207638689Sborman */ 207738689Sborman if (cpp == NULL || lenp == NULL) 207838689Sborman return((unsigned long)-1); 207938689Sborman if (*cpp != NULL && *lenp < 7) 208038689Sborman return((unsigned long)-1); 208138689Sborman /* 208238689Sborman * Decide whether we have a buffer passed to us, 208338689Sborman * or if we need to use our own static buffer. 208438689Sborman */ 208538689Sborman if (*cpp) { 208638689Sborman lsrp = *cpp; 208738689Sborman lsrep = lsrp + *lenp; 208838689Sborman } else { 208938689Sborman *cpp = lsrp = lsr; 209038689Sborman lsrep = lsrp + 44; 209138689Sborman } 209238689Sborman 209338689Sborman cp = arg; 209438689Sborman 209538689Sborman /* 209638689Sborman * Next, decide whether we have a loose source 209738689Sborman * route or a strict source route, and fill in 209838689Sborman * the begining of the option. 209938689Sborman */ 210038689Sborman if (*cp == '!') { 210138689Sborman cp++; 210238689Sborman *lsrp++ = IPOPT_SSRR; 210338689Sborman } else 210438689Sborman *lsrp++ = IPOPT_LSRR; 210538689Sborman 210638689Sborman if (*cp != '@') 210738689Sborman return((unsigned long)-1); 210838689Sborman 210938689Sborman lsrp++; /* skip over length, we'll fill it in later */ 211038689Sborman *lsrp++ = 4; 211138689Sborman 211238689Sborman cp++; 211338689Sborman 211438689Sborman sin_addr.s_addr = 0; 211538689Sborman 211638689Sborman for (c = 0;;) { 211738689Sborman if (c == ':') 211838689Sborman cp2 = 0; 211938689Sborman else for (cp2 = cp; c = *cp2; cp2++) { 212038689Sborman if (c == ',') { 212138689Sborman *cp2++ = '\0'; 212238689Sborman if (*cp2 == '@') 212338689Sborman cp2++; 212438689Sborman } else if (c == '@') { 212538689Sborman *cp2++ = '\0'; 212638689Sborman } else if (c == ':') { 212738689Sborman *cp2++ = '\0'; 212838689Sborman } else 212938689Sborman continue; 213038689Sborman break; 213138689Sborman } 213238689Sborman if (!c) 213338689Sborman cp2 = 0; 213438689Sborman 213538689Sborman if ((tmp = inet_addr(cp)) != -1) { 213638689Sborman sin_addr.s_addr = tmp; 213738689Sborman } else if (host = gethostbyname(cp)) { 213838689Sborman #if defined(h_addr) 213938689Sborman memcpy((caddr_t)&sin_addr, 214038689Sborman host->h_addr_list[0], host->h_length); 214138689Sborman #else 214238689Sborman memcpy((caddr_t)&sin_addr, host->h_addr, host->h_length); 214338689Sborman #endif 214438689Sborman } else { 214538689Sborman *cpp = cp; 214638689Sborman return(0); 214738689Sborman } 214838689Sborman memcpy(lsrp, (char *)&sin_addr, 4); 214938689Sborman lsrp += 4; 215038689Sborman if (cp2) 215138689Sborman cp = cp2; 215238689Sborman else 215338689Sborman break; 215438689Sborman /* 215538689Sborman * Check to make sure there is space for next address 215638689Sborman */ 215738689Sborman if (lsrp + 4 > lsrep) 215838689Sborman return((unsigned long)-1); 215938689Sborman } 216038689Sborman if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) { 216138689Sborman *cpp = 0; 216238689Sborman *lenp = 0; 216338689Sborman return((unsigned long)-1); 216438689Sborman } 216538689Sborman *lsrp++ = IPOPT_NOP; /* 32 bit word align it */ 216638689Sborman *lenp = lsrp - *cpp; 216738689Sborman return(sin_addr.s_addr); 216838689Sborman } 216938689Sborman #endif 217038689Sborman 217138909Sborman #if defined(NOSTRNCASECMP) 217238689Sborman strncasecmp(p1, p2, len) 217338689Sborman register char *p1, *p2; 217438689Sborman int len; 217538689Sborman { 217638689Sborman while (len--) { 217738689Sborman if (tolower(*p1) != tolower(*p2)) 217838689Sborman return(tolower(*p1) - tolower(*p2)); 217938689Sborman if (*p1 == '\0') 218038689Sborman return(0); 218138689Sborman p1++, p2++; 218238689Sborman } 218338689Sborman return(0); 218438689Sborman } 218538689Sborman #endif 2186