2 * Copyright (c) 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)commands.c 8.2 (Berkeley) 12/15/93";
39 #include <sys/param.h>
40 #if defined(CRAY) || defined(sysV88)
41 #include <sys/types.h>
45 #include <sys/types.h>
46 #endif /* defined(unix) */
47 #include <sys/socket.h>
48 #include <netinet/in.h>
65 #include <arpa/telnet.h>
66 #include <arpa/inet.h>
76 #if !defined(CRAY) && !defined(sysV88)
77 #include <netinet/in_systm.h>
78 # if (defined(vax) || defined(tahoe) || defined(hp300)) && !defined(ultrix)
79 # include <machine/endian.h>
81 #endif /* !defined(CRAY) && !defined(sysV88) */
82 #include <netinet/ip.h>
85 #ifndef MAXHOSTNAMELEN
86 #define MAXHOSTNAMELEN 64
89 #if defined(IPPROTO_IP) && defined(IP_TOS)
91 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
94 static char _hostname[MAXHOSTNAMELEN];
96 extern char *getenv();
98 extern int isprefix();
99 extern char **genget();
100 extern int Ambiguous();
101 static int call(int (*routine)(int argc, char *argv[]), ...);
104 char *name; /* command name */
105 char *help; /* help string (NULL for no help) */
106 int (*handler)(); /* routine which executes command */
107 int needconnect; /* Do we need to be connected to execute? */
110 static char line[256];
111 static char saveline[256];
113 static char *margv[20];
116 #include <sys/wait.h>
117 #define PATH_SKEY "/usr/bin/key"
119 skey_calc(argc, argv)
126 printf("%s sequence challenge\n", argv[0]);
132 execv(PATH_SKEY, argv);
138 (void) wait(&status);
139 if (WIFEXITED(status))
140 return (WEXITSTATUS(status));
149 register char *cp, *cp2, c;
150 register char **argp = margv;
154 if (*cp == '!') { /* Special case shell escape */
155 strcpy(saveline, line); /* save for shell command */
156 *argp++ = "!"; /* No room in string to get this */
161 register int inquote = 0;
168 for (cp2 = cp; c != '\0'; c = *++cp) {
176 if ((c = *++cp) == '\0')
178 } else if (c == '"') {
181 } else if (c == '\'') {
184 } else if (isspace(c))
198 * Make a character string into a number.
200 * Todo: 1. Could take random integers (12, 0x12, 012, 0b1).
214 c = b | 0x40; /* DEL */
227 * Construct a control character sequence
228 * for a special character.
236 * The only way I could get the Sun 3.5 compiler
238 * if ((unsigned int)c >= 0x80)
239 * was to assign "c" to an unsigned int variable...
242 register unsigned int uic = (unsigned int)c;
246 if (c == (cc_t)_POSIX_VDISABLE) {
251 buf[1] = ((c>>6)&07) + '0';
252 buf[2] = ((c>>3)&07) + '0';
253 buf[3] = (c&07) + '0';
255 } else if (uic >= 0x20) {
269 * The following are data structures and routines for
270 * the "send" command.
275 char *name; /* How user refers to it (case independent) */
276 char *help; /* Help information (0 ==> no help) */
277 int needconnect; /* Need to be connected */
278 int narg; /* Number of arguments */
279 int (*handler)(); /* Routine to perform (for special ops) */
280 int nbyte; /* Number of bytes to send this command */
281 int what; /* Character to be sent (<0 ==> special) */
288 send_docmd P((char *)),
289 send_dontcmd P((char *)),
290 send_willcmd P((char *)),
291 send_wontcmd P((char *));
293 static struct sendlist Sendlist[] = {
294 { "ao", "Send Telnet Abort output", 1, 0, 0, 2, AO },
295 { "ayt", "Send Telnet 'Are You There'", 1, 0, 0, 2, AYT },
296 { "brk", "Send Telnet Break", 1, 0, 0, 2, BREAK },
297 { "break", 0, 1, 0, 0, 2, BREAK },
298 { "ec", "Send Telnet Erase Character", 1, 0, 0, 2, EC },
299 { "el", "Send Telnet Erase Line", 1, 0, 0, 2, EL },
300 { "escape", "Send current escape character", 1, 0, send_esc, 1, 0 },
301 { "ga", "Send Telnet 'Go Ahead' sequence", 1, 0, 0, 2, GA },
302 { "ip", "Send Telnet Interrupt Process", 1, 0, 0, 2, IP },
303 { "intp", 0, 1, 0, 0, 2, IP },
304 { "interrupt", 0, 1, 0, 0, 2, IP },
305 { "intr", 0, 1, 0, 0, 2, IP },
306 { "nop", "Send Telnet 'No operation'", 1, 0, 0, 2, NOP },
307 { "eor", "Send Telnet 'End of Record'", 1, 0, 0, 2, EOR },
308 { "abort", "Send Telnet 'Abort Process'", 1, 0, 0, 2, ABORT },
309 { "susp", "Send Telnet 'Suspend Process'", 1, 0, 0, 2, SUSP },
310 { "eof", "Send Telnet End of File Character", 1, 0, 0, 2, xEOF },
311 { "synch", "Perform Telnet 'Synch operation'", 1, 0, dosynch, 2, 0 },
312 { "getstatus", "Send request for STATUS", 1, 0, get_status, 6, 0 },
313 { "?", "Display send options", 0, 0, send_help, 0, 0 },
314 { "help", 0, 0, 0, send_help, 0, 0 },
315 { "do", 0, 0, 1, send_docmd, 3, 0 },
316 { "dont", 0, 0, 1, send_dontcmd, 3, 0 },
317 { "will", 0, 0, 1, send_willcmd, 3, 0 },
318 { "wont", 0, 0, 1, send_wontcmd, 3, 0 },
322 #define GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
323 sizeof(struct sendlist)))
330 int count; /* how many bytes we are going to need to send */
332 int question = 0; /* was at least one argument a question */
333 struct sendlist *s; /* pointer to current command */
338 printf("need at least one argument for 'send' command\n");
339 printf("'send ?' for help\n");
343 * First, validate all the send arguments.
344 * In addition, we see how much space we are going to need, and
345 * whether or not we will be doing a "SYNCH" operation (which
346 * flushes the network queue).
349 for (i = 1; i < argc; i++) {
350 s = GETSEND(argv[i]);
352 printf("Unknown send argument '%s'\n'send ?' for help.\n",
355 } else if (Ambiguous(s)) {
356 printf("Ambiguous send argument '%s'\n'send ?' for help.\n",
360 if (i + s->narg >= argc) {
362 "Need %d argument%s to 'send %s' command. 'send %s ?' for help.\n",
363 s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
367 if (s->handler == send_help) {
373 needconnect += s->needconnect;
375 if (!connected && needconnect) {
376 printf("?Need to be connected first.\n");
377 printf("'send ?' for help\n");
380 /* Now, do we have enough room? */
381 if (NETROOM() < count) {
382 printf("There is not enough room in the buffer TO the network\n");
383 printf("to process your request. Nothing will be done.\n");
384 printf("('send synch' will throw away most data in the network\n");
385 printf("buffer, if this might help.)\n");
388 /* OK, they are all OK, now go through again and actually send */
390 for (i = 1; i < argc; i++) {
391 if ((s = GETSEND(argv[i])) == 0) {
392 fprintf(stderr, "Telnet 'send' error - argument disappeared!\n");
398 success += (*s->handler)((s->narg > 0) ? argv[i+1] : 0,
399 (s->narg > 1) ? argv[i+2] : 0);
402 NET2ADD(IAC, s->what);
403 printoption("SENT", IAC, s->what);
406 return (count == success);
420 return(send_tncmd(send_do, "do", name));
427 return(send_tncmd(send_dont, "dont", name));
433 return(send_tncmd(send_will, "will", name));
439 return(send_tncmd(send_wont, "wont", name));
443 send_tncmd(func, cmd, name)
448 extern char *telopts[];
449 register int val = 0;
451 if (isprefix(name, "help") || isprefix(name, "?")) {
452 register int col, len;
454 printf("Usage: send %s <value|option>\n", cmd);
455 printf("\"value\" must be from 0 to 255\n");
456 printf("Valid options are:\n\t");
459 for (cpp = telopts; *cpp; cpp++) {
460 len = strlen(*cpp) + 3;
461 if (col + len > 65) {
465 printf(" \"%s\"", *cpp);
471 cpp = (char **)genget(name, telopts, sizeof(char *));
472 if (Ambiguous(cpp)) {
473 fprintf(stderr,"'%s': ambiguous argument ('send %s ?' for help).\n",
480 register char *cp = name;
482 while (*cp >= '0' && *cp <= '9') {
488 fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\n",
491 } else if (val < 0 || val > 255) {
492 fprintf(stderr, "'%s': bad value ('send %s ?' for help).\n",
498 printf("?Need to be connected first.\n");
508 struct sendlist *s; /* pointer to current command */
509 for (s = Sendlist; s->name; s++) {
511 printf("%-15s %s\n", s->name, s->help);
517 * The following are the routines and data structures referred
518 * to by the arguments to the "toggle" command.
533 (SetSockOpt(net, SOL_SOCKET, SO_DEBUG, debug)) < 0) {
534 perror("setsockopt (SO_DEBUG)");
538 if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0)
539 perror("setsockopt (SO_DEBUG)");
541 printf("Cannot turn off socket debugging\n");
551 printf("Will send carriage returns as telnet <CR><LF>.\n");
553 printf("Will send carriage returns as telnet <CR><NUL>.\n");
564 donebinarytoggle = 1;
569 if (my_want_state_is_will(TELOPT_BINARY) &&
570 my_want_state_is_do(TELOPT_BINARY)) {
572 } else if (my_want_state_is_wont(TELOPT_BINARY) &&
573 my_want_state_is_dont(TELOPT_BINARY)) {
576 val = binmode ? 0 : 1;
580 if (my_want_state_is_will(TELOPT_BINARY) &&
581 my_want_state_is_do(TELOPT_BINARY)) {
582 printf("Already operating in binary mode with remote host.\n");
584 printf("Negotiating binary mode with remote host.\n");
588 if (my_want_state_is_wont(TELOPT_BINARY) &&
589 my_want_state_is_dont(TELOPT_BINARY)) {
590 printf("Already in network ascii mode with remote host.\n");
592 printf("Negotiating network ascii mode with remote host.\n");
603 donebinarytoggle = 1;
606 val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
609 if (my_want_state_is_do(TELOPT_BINARY)) {
610 printf("Already receiving in binary mode.\n");
612 printf("Negotiating binary mode on input.\n");
616 if (my_want_state_is_dont(TELOPT_BINARY)) {
617 printf("Already receiving in network ascii mode.\n");
619 printf("Negotiating network ascii mode on input.\n");
630 donebinarytoggle = 1;
633 val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
636 if (my_want_state_is_will(TELOPT_BINARY)) {
637 printf("Already transmitting in binary mode.\n");
639 printf("Negotiating binary mode on output.\n");
643 if (my_want_state_is_wont(TELOPT_BINARY)) {
644 printf("Already transmitting in network ascii mode.\n");
646 printf("Negotiating network ascii mode on output.\n");
654 static int togglehelp P((void));
655 #if defined(AUTHENTICATION)
656 extern int auth_togdebug P((int));
660 char *name; /* name of toggle */
661 char *help; /* help message */
662 int (*handler)(); /* routine to do actual setting */
664 char *actionexplanation;
667 static struct togglelist Togglelist[] = {
669 "flushing of output when sending interrupt characters",
672 "flush output when sending interrupt characters" },
674 "automatic sending of interrupt characters in urgent mode",
677 "send interrupt characters in urgent mode" },
678 #if defined(AUTHENTICATION)
680 "automatic sending of login and/or authentication info",
683 "send login name and/or authentication information" },
685 "Toggle authentication debugging",
688 "print authentication debugging information" },
691 "don't read ~/.telnetrc file",
694 "skip reading of ~/.telnetrc file" },
696 "sending and receiving of binary data",
701 "receiving of binary data",
706 "sending of binary data",
711 "sending carriage returns as telnet <CR><LF>",
716 "mapping of received carriage returns",
719 "map carriage return on output" },
721 "local recognition of certain control characters",
724 "recognize certain control characters" },
725 { " ", "", 0 }, /* empty line */
726 #if defined(unix) && defined(TN3270)
728 "(debugging) toggle tracing of API transactions",
731 "trace API transactions" },
733 "(debugging) toggle printing of hexadecimal curses data",
736 "print hexadecimal representation of curses data" },
737 #endif /* defined(unix) && defined(TN3270) */
742 "turn on socket level debugging" },
744 "printing of hexadecimal network data (debugging)",
747 "print hexadecimal representation of network traffic" },
749 "output of \"netdata\" to user readable format (debugging)",
752 "print user readable output for \"netdata\"" },
754 "viewing of options processing (debugging)",
757 "show option processing" },
760 "(debugging) toggle printing of hexadecimal terminal data",
763 "print hexadecimal representation of terminal traffic" },
764 #endif /* defined(unix) */
777 struct togglelist *c;
779 for (c = Togglelist; c->name; c++) {
782 printf("%-15s toggle %s\n", c->name, c->help);
788 printf("%-15s %s\n", "?", "display help information");
796 struct togglelist *c;
798 for (c = Togglelist; c->name; c++) {
801 printf("%-15s %s %s\n", c->name, set ? "enable" : "disable",
809 #define GETTOGGLE(name) (struct togglelist *) \
810 genget(name, (char **) Togglelist, sizeof(struct togglelist))
819 struct togglelist *c;
823 "Need an argument to 'toggle' command. 'toggle ?' for help.\n");
832 fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\n",
836 fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\n",
841 *c->variable = !*c->variable; /* invert it */
842 if (c->actionexplanation) {
843 printf("%s %s.\n", *c->variable? "Will" : "Won't",
844 c->actionexplanation);
848 retval &= (*c->handler)(-1);
856 * The following perform the "set" command.
860 char *name; /* name */
861 char *help; /* help information */
863 cc_t *charp; /* where it is located at */
866 static struct setlist Setlist[] = {
867 #ifdef KLUDGELINEMODE
868 { "echo", "character to toggle local echoing on/off", 0, &echoc },
870 { "escape", "character to escape back to telnet command mode", 0, &escape },
871 { "rlogin", "rlogin escape character", 0, &rlogin },
872 { "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile},
874 { " ", "The following need 'localchars' to be toggled true", 0, 0 },
875 { "flushoutput", "character to cause an Abort Output", 0, termFlushCharp },
876 { "interrupt", "character to cause an Interrupt Process", 0, termIntCharp },
877 { "quit", "character to cause an Abort process", 0, termQuitCharp },
878 { "eof", "character to cause an EOF ", 0, termEofCharp },
880 { " ", "The following are for local editing in linemode", 0, 0 },
881 { "erase", "character to use to erase a character", 0, termEraseCharp },
882 { "kill", "character to use to erase a line", 0, termKillCharp },
883 { "lnext", "character to use for literal next", 0, termLiteralNextCharp },
884 { "susp", "character to cause a Suspend Process", 0, termSuspCharp },
885 { "reprint", "character to use for line reprint", 0, termRprntCharp },
886 { "worderase", "character to use to erase a word", 0, termWerasCharp },
887 { "start", "character to use for XON", 0, termStartCharp },
888 { "stop", "character to use for XOFF", 0, termStopCharp },
889 { "forw1", "alternate end of line character", 0, termForw1Charp },
890 { "forw2", "alternate end of line character", 0, termForw2Charp },
891 { "ayt", "alternate AYT character", 0, termAytCharp },
895 #if defined(CRAY) && !defined(__STDC__)
896 /* Work around compiler bug in pcc 4.1.5 */
900 #ifndef KLUDGELINEMODE
905 Setlist[N+0].charp = &termFlushChar;
906 Setlist[N+1].charp = &termIntChar;
907 Setlist[N+2].charp = &termQuitChar;
908 Setlist[N+3].charp = &termEofChar;
909 Setlist[N+6].charp = &termEraseChar;
910 Setlist[N+7].charp = &termKillChar;
911 Setlist[N+8].charp = &termLiteralNextChar;
912 Setlist[N+9].charp = &termSuspChar;
913 Setlist[N+10].charp = &termRprntChar;
914 Setlist[N+11].charp = &termWerasChar;
915 Setlist[N+12].charp = &termStartChar;
916 Setlist[N+13].charp = &termStopChar;
917 Setlist[N+14].charp = &termForw1Char;
918 Setlist[N+15].charp = &termForw2Char;
919 Setlist[N+16].charp = &termAytChar;
922 #endif /* defined(CRAY) && !defined(__STDC__) */
924 static struct setlist *
928 return (struct setlist *)
929 genget(name, (char **) Setlist, sizeof(struct setlist));
936 if (rlogin != _POSIX_VDISABLE) {
937 rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
938 printf("Telnet rlogin escape character is '%s'.\n",
941 escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
942 printf("Telnet escape character is '%s'.\n", control(escape));
953 struct togglelist *c;
955 if (argc < 2 || argc > 3) {
956 printf("Format is 'set Name Value'\n'set ?' for help.\n");
959 if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
960 for (ct = Setlist; ct->name; ct++)
961 printf("%-15s %s\n", ct->name, ct->help);
964 printf("%-15s %s\n", "?", "display help information");
968 ct = getset(argv[1]);
970 c = GETTOGGLE(argv[1]);
972 fprintf(stderr, "'%s': unknown argument ('set ?' for help).\n",
975 } else if (Ambiguous(c)) {
976 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
981 if ((argc == 2) || (strcmp("on", argv[2]) == 0))
983 else if (strcmp("off", argv[2]) == 0)
986 printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
989 if (c->actionexplanation) {
990 printf("%s %s.\n", *c->variable? "Will" : "Won't",
991 c->actionexplanation);
996 } else if (argc != 3) {
997 printf("Format is 'set Name Value'\n'set ?' for help.\n");
999 } else if (Ambiguous(ct)) {
1000 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
1003 } else if (ct->handler) {
1004 (*ct->handler)(argv[2]);
1005 printf("%s set to \"%s\".\n", ct->name, (char *)ct->charp);
1007 if (strcmp("off", argv[2])) {
1008 value = special(argv[2]);
1010 value = _POSIX_VDISABLE;
1012 *(ct->charp) = (cc_t)value;
1013 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
1020 unsetcmd(argc, argv)
1025 struct togglelist *c;
1026 register char *name;
1030 "Need an argument to 'unset' command. 'unset ?' for help.\n");
1033 if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
1034 for (ct = Setlist; ct->name; ct++)
1035 printf("%-15s %s\n", ct->name, ct->help);
1038 printf("%-15s %s\n", "?", "display help information");
1048 c = GETTOGGLE(name);
1050 fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\n",
1053 } else if (Ambiguous(c)) {
1054 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1060 if (c->actionexplanation) {
1061 printf("%s %s.\n", *c->variable? "Will" : "Won't",
1062 c->actionexplanation);
1067 } else if (Ambiguous(ct)) {
1068 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1071 } else if (ct->handler) {
1073 printf("%s reset to \"%s\".\n", ct->name, (char *)ct->charp);
1075 *(ct->charp) = _POSIX_VDISABLE;
1076 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
1083 * The following are the data structures and routines for the
1086 #ifdef KLUDGELINEMODE
1087 extern int kludgelinemode;
1093 send_wont(TELOPT_LINEMODE, 1);
1094 send_dont(TELOPT_SGA, 1);
1095 send_dont(TELOPT_ECHO, 1);
1102 #ifdef KLUDGELINEMODE
1104 send_dont(TELOPT_SGA, 1);
1106 send_will(TELOPT_LINEMODE, 1);
1107 send_dont(TELOPT_ECHO, 1);
1114 #ifdef KLUDGELINEMODE
1116 send_do(TELOPT_SGA, 1);
1119 send_wont(TELOPT_LINEMODE, 1);
1120 send_do(TELOPT_ECHO, 1);
1129 extern int linemode;
1131 if (my_want_state_is_wont(TELOPT_LINEMODE)) {
1132 printf("?Need to have LINEMODE option enabled first.\n");
1133 printf("'mode ?' for help.\n");
1138 c = (linemode | bit);
1140 c = (linemode & ~bit);
1148 return dolmmode(bit, 1);
1154 return dolmmode(bit, 0);
1158 char *name; /* command name */
1159 char *help; /* help string */
1160 int (*handler)(); /* routine which executes command */
1161 int needconnect; /* Do we need to be connected to execute? */
1165 extern int modehelp();
1167 static struct modelist ModeList[] = {
1168 { "character", "Disable LINEMODE option", docharmode, 1 },
1169 #ifdef KLUDGELINEMODE
1170 { "", "(or disable obsolete line-by-line mode)", 0 },
1172 { "line", "Enable LINEMODE option", dolinemode, 1 },
1173 #ifdef KLUDGELINEMODE
1174 { "", "(or enable obsolete line-by-line mode)", 0 },
1177 { "", "These require the LINEMODE option to be enabled", 0 },
1178 { "isig", "Enable signal trapping", setmode, 1, MODE_TRAPSIG },
1179 { "+isig", 0, setmode, 1, MODE_TRAPSIG },
1180 { "-isig", "Disable signal trapping", clearmode, 1, MODE_TRAPSIG },
1181 { "edit", "Enable character editing", setmode, 1, MODE_EDIT },
1182 { "+edit", 0, setmode, 1, MODE_EDIT },
1183 { "-edit", "Disable character editing", clearmode, 1, MODE_EDIT },
1184 { "softtabs", "Enable tab expansion", setmode, 1, MODE_SOFT_TAB },
1185 { "+softtabs", 0, setmode, 1, MODE_SOFT_TAB },
1186 { "-softtabs", "Disable character editing", clearmode, 1, MODE_SOFT_TAB },
1187 { "litecho", "Enable literal character echo", setmode, 1, MODE_LIT_ECHO },
1188 { "+litecho", 0, setmode, 1, MODE_LIT_ECHO },
1189 { "-litecho", "Disable literal character echo", clearmode, 1, MODE_LIT_ECHO },
1190 { "help", 0, modehelp, 0 },
1191 #ifdef KLUDGELINEMODE
1192 { "kludgeline", 0, dokludgemode, 1 },
1195 { "?", "Print help information", modehelp, 0 },
1203 struct modelist *mt;
1205 printf("format is: 'mode Mode', where 'Mode' is one of:\n\n");
1206 for (mt = ModeList; mt->name; mt++) {
1209 printf("%-15s %s\n", mt->name, mt->help);
1217 #define GETMODECMD(name) (struct modelist *) \
1218 genget(name, (char **) ModeList, sizeof(struct modelist))
1225 struct modelist *mt;
1228 printf("'mode' command requires an argument\n");
1229 printf("'mode ?' for help.\n");
1230 } else if ((mt = GETMODECMD(argv[1])) == 0) {
1231 fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\n", argv[1]);
1232 } else if (Ambiguous(mt)) {
1233 fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]);
1234 } else if (mt->needconnect && !connected) {
1235 printf("?Need to be connected first.\n");
1236 printf("'mode ?' for help.\n");
1237 } else if (mt->handler) {
1238 return (*mt->handler)(mt->arg1);
1244 * The following data structures and routines implement the
1245 * "display" command.
1253 struct togglelist *tl;
1256 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \
1257 if (*tl->variable) { \
1262 printf(" %s.\n", tl->actionexplanation); \
1265 #define doset(sl) if (sl->name && *sl->name != ' ') { \
1266 if (sl->handler == 0) \
1267 printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
1269 printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
1273 for (tl = Togglelist; tl->name; tl++) {
1277 for (sl = Setlist; sl->name; sl++) {
1283 for (i = 1; i < argc; i++) {
1284 sl = getset(argv[i]);
1285 tl = GETTOGGLE(argv[i]);
1286 if (Ambiguous(sl) || Ambiguous(tl)) {
1287 printf("?Ambiguous argument '%s'.\n", argv[i]);
1289 } else if (!sl && !tl) {
1290 printf("?Unknown argument '%s'.\n", argv[i]);
1302 /*@*/optionstatus();
1309 * The following are the data structures, and many of the routines,
1310 * relating to command processing.
1314 * Set the escape character.
1317 setescape(argc, argv)
1325 "Deprecated usage - please use 'set escape%s%s' in the future.\n",
1326 (argc > 2)? " ":"", (argc > 2)? argv[1]: "");
1330 printf("new escape character: ");
1331 (void) fgets(buf, sizeof(buf), stdin);
1337 printf("Escape character is '%s'.\n", control(escape));
1339 (void) fflush(stdout);
1348 printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
1349 printf("%s map carriage return on output.\n", crmod ? "Will" : "Won't");
1350 (void) fflush(stdout);
1361 long oldrows, oldcols, newrows, newcols, err;
1363 err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1364 (void) kill(0, SIGTSTP);
1366 * If we didn't get the window size before the SUSPEND, but we
1367 * can get them now (???), then send the NAWS to make sure that
1368 * we are set up for the right window size.
1370 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1371 (err || ((oldrows != newrows) || (oldcols != newcols)))) {
1375 /* reget parameters in case they were changed */
1376 TerminalSaveState();
1379 printf("Suspend is not supported. Try the '!' command instead\n");
1384 #if !defined(TN3270)
1391 long oldrows, oldcols, newrows, newcols, err;
1395 err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1398 perror("Fork failed\n");
1404 * Fire up the shell in the child.
1406 register char *shellp, *shellname;
1408 shellp = getenv("SHELL");
1411 if ((shellname = rindex(shellp, '/')) == 0)
1416 execl(shellp, shellname, "-c", &saveline[1], 0);
1418 execl(shellp, shellname, 0);
1423 (void)wait((int *)0); /* Wait for the shell to complete */
1425 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1426 (err || ((oldrows != newrows) || (oldcols != newcols)))) {
1433 #else /* !defined(TN3270) */
1435 #endif /* !defined(TN3270) */
1440 int argc; /* Number of arguments */
1441 char *argv[]; /* arguments */
1443 extern int resettermname;
1446 (void) shutdown(net, 2);
1447 printf("Connection closed.\n");
1448 (void) NetClose(net);
1451 #if defined(AUTHENTICATION)
1452 auth_encrypt_connect(connected);
1453 #endif /* defined(AUTHENTICATION) */
1457 SetIn3270(); /* Get out of 3270 mode */
1458 #endif /* defined(TN3270) */
1460 if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
1461 longjmp(toplevel, 1);
1464 return 1; /* Keep lint, etc., happy */
1470 (void) call(bye, "bye", "fromquit", 0);
1479 send_do(TELOPT_LOGOUT, 1);
1496 static void slc_help();
1498 struct slclist SlcList[] = {
1499 { "export", "Use local special character definitions",
1500 slc_mode_export, 0 },
1501 { "import", "Use remote special character definitions",
1502 slc_mode_import, 1 },
1503 { "check", "Verify remote special character definitions",
1504 slc_mode_import, 0 },
1505 { "help", 0, slc_help, 0 },
1506 { "?", "Print help information", slc_help, 0 },
1515 for (c = SlcList; c->name; c++) {
1518 printf("%-15s %s\n", c->name, c->help);
1525 static struct slclist *
1529 return (struct slclist *)
1530 genget(name, (char **) SlcList, sizeof(struct slclist));
1542 "Need an argument to 'slc' command. 'slc ?' for help.\n");
1545 c = getslc(argv[1]);
1547 fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\n",
1552 fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\n",
1556 (*c->handler)(c->arg);
1562 * The ENVIRON command.
1572 extern struct env_lst *
1573 env_define P((unsigned char *, unsigned char *));
1575 env_undefine P((unsigned char *)),
1576 env_export P((unsigned char *)),
1577 env_unexport P((unsigned char *)),
1578 env_send P((unsigned char *)),
1579 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1580 env_varval P((unsigned char *)),
1586 struct envlist EnvList[] = {
1587 { "define", "Define an environment variable",
1588 (void (*)())env_define, 2 },
1589 { "undefine", "Undefine an environment variable",
1591 { "export", "Mark an environment variable for automatic export",
1593 { "unexport", "Don't mark an environment variable for automatic export",
1595 { "send", "Send an environment variable", env_send, 1 },
1596 { "list", "List the current environment variables",
1598 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1599 { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
1602 { "help", 0, env_help, 0 },
1603 { "?", "Print help information", env_help, 0 },
1612 for (c = EnvList; c->name; c++) {
1615 printf("%-15s %s\n", c->name, c->help);
1622 static struct envlist *
1626 return (struct envlist *)
1627 genget(name, (char **) EnvList, sizeof(struct envlist));
1638 "Need an argument to 'environ' command. 'environ ?' for help.\n");
1641 c = getenvcmd(argv[1]);
1643 fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\n",
1648 fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\n",
1652 if (c->narg + 2 != argc) {
1654 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\n",
1655 c->narg < argc + 2 ? "only " : "",
1656 c->narg, c->narg == 1 ? "" : "s", c->name);
1659 (*c->handler)(argv[2], argv[3]);
1664 struct env_lst *next; /* pointer to next structure */
1665 struct env_lst *prev; /* pointer to previous structure */
1666 unsigned char *var; /* pointer to variable name */
1667 unsigned char *value; /* pointer to variable value */
1668 int export; /* 1 -> export with default list of variables */
1669 int welldefined; /* A well defined variable */
1672 struct env_lst envlisthead;
1678 register struct env_lst *ep;
1680 for (ep = envlisthead.next; ep; ep = ep->next) {
1681 if (strcmp((char *)ep->var, (char *)var) == 0)
1690 extern char **environ;
1691 register char **epp, *cp;
1692 register struct env_lst *ep;
1694 for (epp = environ; *epp; epp++) {
1695 if (cp = index(*epp, '=')) {
1697 ep = env_define((unsigned char *)*epp,
1698 (unsigned char *)cp+1);
1704 * Special case for DISPLAY variable. If it is ":0.0" or
1705 * "unix:0.0", we have to get rid of "unix" and insert our
1708 if ((ep = env_find("DISPLAY"))
1709 && ((*ep->value == ':')
1710 || (strncmp((char *)ep->value, "unix:", 5) == 0))) {
1712 char *cp2 = index((char *)ep->value, ':');
1714 gethostname(hbuf, 256);
1716 cp = (char *)malloc(strlen(hbuf) + strlen(cp2) + 1);
1717 sprintf((char *)cp, "%s%s", hbuf, cp2);
1719 ep->value = (unsigned char *)cp;
1722 * If USER is not defined, but LOGNAME is, then add
1723 * USER with the value from LOGNAME. By default, we
1724 * don't export the USER variable.
1726 if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
1727 env_define((unsigned char *)"USER", ep->value);
1728 env_unexport((unsigned char *)"USER");
1730 env_export((unsigned char *)"DISPLAY");
1731 env_export((unsigned char *)"PRINTER");
1735 env_define(var, value)
1736 unsigned char *var, *value;
1738 register struct env_lst *ep;
1740 if (ep = env_find(var)) {
1746 ep = (struct env_lst *)malloc(sizeof(struct env_lst));
1747 ep->next = envlisthead.next;
1748 envlisthead.next = ep;
1749 ep->prev = &envlisthead;
1751 ep->next->prev = ep;
1753 ep->welldefined = opt_welldefined(var);
1755 ep->var = (unsigned char *)strdup((char *)var);
1756 ep->value = (unsigned char *)strdup((char *)value);
1764 register struct env_lst *ep;
1766 if (ep = env_find(var)) {
1767 ep->prev->next = ep->next;
1769 ep->next->prev = ep->prev;
1782 register struct env_lst *ep;
1784 if (ep = env_find(var))
1792 register struct env_lst *ep;
1794 if (ep = env_find(var))
1802 register struct env_lst *ep;
1804 if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1806 && my_state_is_wont(TELOPT_OLD_ENVIRON)
1810 "Cannot send '%s': Telnet ENVIRON option not enabled\n",
1816 fprintf(stderr, "Cannot send '%s': variable not defined\n",
1820 env_opt_start_info();
1821 env_opt_add(ep->var);
1828 register struct env_lst *ep;
1830 for (ep = envlisthead.next; ep; ep = ep->next) {
1831 printf("%c %-20s %s\n", ep->export ? '*' : ' ',
1832 ep->var, ep->value);
1837 env_default(init, welldefined)
1840 static struct env_lst *nep = NULL;
1847 while (nep = nep->next) {
1848 if (nep->export && (nep->welldefined == welldefined))
1859 register struct env_lst *ep;
1861 if (ep = env_find(var))
1866 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1869 unsigned char *what;
1871 extern int old_env_var, old_env_value, env_auto;
1872 int len = strlen((char *)what);
1877 if (strncasecmp((char *)what, "status", len) == 0) {
1879 printf("%s%s", "VAR and VALUE are/will be ",
1880 "determined automatically\n");
1881 if (old_env_var == OLD_ENV_VAR)
1882 printf("VAR and VALUE set to correct definitions\n");
1884 printf("VAR and VALUE definitions are reversed\n");
1885 } else if (strncasecmp((char *)what, "auto", len) == 0) {
1887 old_env_var = OLD_ENV_VALUE;
1888 old_env_value = OLD_ENV_VAR;
1889 } else if (strncasecmp((char *)what, "right", len) == 0) {
1891 old_env_var = OLD_ENV_VAR;
1892 old_env_value = OLD_ENV_VALUE;
1893 } else if (strncasecmp((char *)what, "wrong", len) == 0) {
1895 old_env_var = OLD_ENV_VALUE;
1896 old_env_value = OLD_ENV_VAR;
1899 printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
1904 #if defined(AUTHENTICATION)
1906 * The AUTHENTICATE command.
1917 auth_enable P((int)),
1918 auth_disable P((int)),
1919 auth_status P((void));
1921 auth_help P((void));
1923 struct authlist AuthList[] = {
1924 { "status", "Display current status of authentication information",
1926 { "disable", "Disable an authentication type ('auth disable ?' for more)",
1928 { "enable", "Enable an authentication type ('auth enable ?' for more)",
1930 { "help", 0, auth_help, 0 },
1931 { "?", "Print help information", auth_help, 0 },
1940 for (c = AuthList; c->name; c++) {
1943 printf("%-15s %s\n", c->name, c->help);
1951 auth_cmd(argc, argv)
1957 c = (struct authlist *)
1958 genget(argv[1], (char **) AuthList, sizeof(struct authlist));
1960 fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\n",
1965 fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\n",
1969 if (c->narg + 2 != argc) {
1971 "Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\n",
1972 c->narg < argc + 2 ? "only " : "",
1973 c->narg, c->narg == 1 ? "" : "s", c->name);
1976 return((*c->handler)(argv[2], argv[3]));
1981 #if defined(unix) && defined(TN3270)
1990 res = fcntl(fd, F_GETOWN, 0);
1997 printf("\tOwner is %d.\n", res);
2001 res = fcntl(fd, F_GETFL, 0);
2009 printf("\tFlags are 0x%x: %s\n", res, decodeflags(res));
2012 #endif /* defined(unix) && defined(TN3270) */
2015 * Print status about the connection.
2024 printf("Connected to %s.\n", hostname);
2025 if ((argc < 2) || strcmp(argv[1], "notmuch")) {
2026 int mode = getconnmode();
2028 if (my_want_state_is_will(TELOPT_LINEMODE)) {
2029 printf("Operating with LINEMODE option\n");
2030 printf("%s line editing\n", (mode&MODE_EDIT) ? "Local" : "No");
2031 printf("%s catching of signals\n",
2032 (mode&MODE_TRAPSIG) ? "Local" : "No");
2034 #ifdef KLUDGELINEMODE
2035 } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
2036 printf("Operating in obsolete linemode\n");
2039 printf("Operating in single character mode\n");
2041 printf("Catching signals locally\n");
2043 printf("%s character echo\n", (mode&MODE_ECHO) ? "Local" : "Remote");
2044 if (my_want_state_is_will(TELOPT_LFLOW))
2045 printf("%s flow control\n", (mode&MODE_FLOW) ? "Local" : "No");
2048 printf("No connection.\n");
2050 # if !defined(TN3270)
2051 printf("Escape character is '%s'.\n", control(escape));
2052 (void) fflush(stdout);
2053 # else /* !defined(TN3270) */
2054 if ((!In3270) && ((argc < 2) || strcmp(argv[1], "notmuch"))) {
2055 printf("Escape character is '%s'.\n", control(escape));
2058 if ((argc >= 2) && !strcmp(argv[1], "everything")) {
2059 printf("SIGIO received %d time%s.\n",
2060 sigiocount, (sigiocount == 1)? "":"s");
2062 printf("Process ID %d, process group %d.\n",
2063 getpid(), getpgrp(getpid()));
2064 printf("Terminal input:\n");
2066 printf("Terminal output:\n");
2068 printf("Network socket:\n");
2072 if (In3270 && transcom) {
2073 printf("Transparent mode command is '%s'.\n", transcom);
2075 # endif /* defined(unix) */
2076 (void) fflush(stdout);
2080 # endif /* defined(TN3270) */
2086 * Function that gets called when SIGINFO is received.
2090 (void) call(status, "status", "notmuch", 0);
2099 register struct hostent *host = 0;
2100 struct sockaddr_in sin;
2101 struct servent *sp = 0;
2103 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2104 char *srp = 0, *strrchr();
2105 unsigned long sourceroute(), srlen;
2107 char *cmd, *hostp = 0, *portp = 0, *user = 0;
2109 /* clear the socket address prior to use */
2110 bzero((char *)&sin, sizeof(sin));
2113 printf("?Already connected to %s\n", hostname);
2118 (void) strcpy(line, "open ");
2120 (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
2128 if (isprefix(*argv, "help") || isprefix(*argv, "?"))
2130 if (strcmp(*argv, "-l") == 0) {
2138 if (strcmp(*argv, "-a") == 0) {
2154 printf("usage: telnet [-l user] [-a] host-name [port]\n");
2161 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2162 if (hostp[0] == '@' || hostp[0] == '!') {
2163 if ((hostname = strrchr(hostp, ':')) == NULL)
2164 hostname = strrchr(hostp, '@');
2167 temp = sourceroute(hostp, &srp, &srlen);
2172 } else if (temp == -1) {
2173 printf("Bad source route option: %s\n", hostp);
2177 sin.sin_addr.s_addr = temp;
2178 sin.sin_family = AF_INET;
2182 temp = inet_addr(hostp);
2183 if (temp != INADDR_NONE) {
2184 sin.sin_addr.s_addr = temp;
2185 sin.sin_family = AF_INET;
2187 host = gethostbyaddr((char *)&temp, sizeof(temp), AF_INET);
2189 (void) strncpy(_hostname, host->h_name, sizeof(_hostname));
2191 (void) strncpy(_hostname, hostp, sizeof(_hostname));
2192 _hostname[sizeof(_hostname)-1] = '\0';
2193 hostname = _hostname;
2195 host = gethostbyname(hostp);
2197 sin.sin_family = host->h_addrtype;
2198 #if defined(h_addr) /* In 4.3, this is a #define */
2199 memmove((caddr_t)&sin.sin_addr,
2200 host->h_addr_list[0],
2201 MIN(host->h_length, sizeof(sin.sin_addr)));
2202 #else /* defined(h_addr) */
2203 memmove((caddr_t)&sin.sin_addr, host->h_addr,
2204 MIN(host->h_length, sizeof(sin.sin_addr)));
2205 #endif /* defined(h_addr) */
2206 strncpy(_hostname, host->h_name, sizeof(_hostname));
2207 _hostname[sizeof(_hostname)-1] = '\0';
2208 hostname = _hostname;
2215 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2219 if (*portp == '-') {
2224 sin.sin_port = atoi(portp);
2225 if (sin.sin_port == 0) {
2226 sp = getservbyname(portp, "tcp");
2228 sin.sin_port = sp->s_port;
2230 printf("%s: bad port number\n", portp);
2236 u_short htons P((unsigned short));
2237 #endif /* !defined(htons) */
2238 sin.sin_port = htons(sin.sin_port);
2242 sp = getservbyname("telnet", "tcp");
2244 fprintf(stderr, "telnet: tcp/telnet: unknown service\n");
2248 sin.sin_port = sp->s_port;
2252 printf("Trying %s(%d)...\n", inet_ntoa(sin.sin_addr), sin.sin_port);
2254 net = socket(AF_INET, SOCK_STREAM, 0);
2257 perror("telnet: socket");
2260 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2261 if (srp && setsockopt(net, IPPROTO_IP, IP_OPTIONS, (char *)srp, srlen) < 0)
2262 perror("setsockopt (IP_OPTIONS)");
2264 #if defined(IPPROTO_IP) && defined(IP_TOS)
2266 # if defined(HAS_GETTOS)
2268 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
2272 tos = IPTOS_LOWDELAY;
2274 && (setsockopt(net, IPPROTO_IP, IP_TOS,
2275 (char *)&tos, sizeof(int)) < 0)
2276 && (errno != ENOPROTOOPT))
2277 perror("telnet: setsockopt (IP_TOS) (ignored)");
2279 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2281 if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
2282 perror("setsockopt (SO_DEBUG)");
2285 if (connect(net, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
2286 #if defined(h_addr) /* In 4.3, this is a #define */
2287 if (host && host->h_addr_list[1]) {
2290 fprintf(stderr, "telnet: connect to address %s: ",
2291 inet_ntoa(sin.sin_addr));
2294 host->h_addr_list++;
2295 memcpy((caddr_t)&sin.sin_addr, host->h_addr_list[0],
2296 MIN(host->h_length, sizeof(sin.sin_addr)));
2297 (void) NetClose(net);
2300 #endif /* defined(h_addr) */
2301 perror("telnet: Unable to connect to remote host");
2305 #if defined(AUTHENTICATION)
2306 auth_encrypt_connect(connected);
2307 #endif /* defined(AUTHENTICATION) */
2308 } while (connected == 0);
2309 cmdrc(hostp, hostname);
2310 if (autologin && user == NULL) {
2313 user = getenv("USER");
2315 (pw = getpwnam(user)) && pw->pw_uid != getuid()) {
2316 if (pw = getpwuid(getuid()))
2323 env_define((unsigned char *)"USER", (unsigned char *)user);
2324 env_export((unsigned char *)"USER");
2326 (void) call(status, "status", "notmuch", 0);
2327 if (setjmp(peerdied) == 0)
2329 (void) NetClose(net);
2330 ExitString("Connection closed by foreign host.\n",1);
2334 #define HELPINDENT (sizeof ("connect"))
2337 openhelp[] = "connect to a site",
2338 closehelp[] = "close current connection",
2339 logouthelp[] = "forcibly logout remote user and close the connection",
2340 quithelp[] = "exit telnet",
2341 statushelp[] = "print status information",
2342 helphelp[] = "print help information",
2343 sendhelp[] = "transmit special characters ('send ?' for more)",
2344 sethelp[] = "set operating parameters ('set ?' for more)",
2345 unsethelp[] = "unset operating parameters ('unset ?' for more)",
2346 togglestring[] ="toggle operating parameters ('toggle ?' for more)",
2347 slchelp[] = "change state of special charaters ('slc ?' for more)",
2348 displayhelp[] = "display operating parameters",
2349 #if defined(TN3270) && defined(unix)
2350 transcomhelp[] = "specify Unix command for transparent mode pipe",
2351 #endif /* defined(TN3270) && defined(unix) */
2352 #if defined(AUTHENTICATION)
2353 authhelp[] = "turn on (off) authentication ('auth ?' for more)",
2356 zhelp[] = "suspend telnet",
2357 #endif /* defined(unix) */
2359 skeyhelp[] = "compute response to s/key challenge",
2361 shellhelp[] = "invoke a subshell",
2362 envhelp[] = "change environment variables ('environ ?' for more)",
2363 modestring[] = "try to enter line or character mode ('mode ?' for more)";
2367 static Command cmdtab[] = {
2368 { "close", closehelp, bye, 1 },
2369 { "logout", logouthelp, logout, 1 },
2370 { "display", displayhelp, display, 0 },
2371 { "mode", modestring, modecmd, 0 },
2372 { "telnet", openhelp, tn, 0 },
2373 { "open", openhelp, tn, 0 },
2374 { "quit", quithelp, quit, 0 },
2375 { "send", sendhelp, sendcmd, 0 },
2376 { "set", sethelp, setcmd, 0 },
2377 { "unset", unsethelp, unsetcmd, 0 },
2378 { "status", statushelp, status, 0 },
2379 { "toggle", togglestring, toggle, 0 },
2380 { "slc", slchelp, slccmd, 0 },
2381 #if defined(TN3270) && defined(unix)
2382 { "transcom", transcomhelp, settranscom, 0 },
2383 #endif /* defined(TN3270) && defined(unix) */
2384 #if defined(AUTHENTICATION)
2385 { "auth", authhelp, auth_cmd, 0 },
2388 { "z", zhelp, suspend, 0 },
2389 #endif /* defined(unix) */
2391 { "!", shellhelp, shell, 1 },
2393 { "!", shellhelp, shell, 0 },
2395 { "environ", envhelp, env_cmd, 0 },
2396 { "?", helphelp, help, 0 },
2398 { "skey", skeyhelp, skey_calc, 0 },
2403 static char crmodhelp[] = "deprecated command -- use 'toggle crmod' instead";
2404 static char escapehelp[] = "deprecated command -- use 'set escape' instead";
2406 static Command cmdtab2[] = {
2407 { "help", 0, help, 0 },
2408 { "escape", escapehelp, setescape, 0 },
2409 { "crmod", crmodhelp, togcrmod, 0 },
2415 * Call routine with argc, argv set from args (terminated by 0).
2420 call(int (*routine)(int argc, char *argv[]), ...)
2426 va_start(ap, routine);
2427 while ((args[argno++] = va_arg(ap, char *)) != 0) {
2431 return (*routine)(argno-1, args);
2441 if (cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command)))
2443 return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
2447 command(top, tbuf, cnt)
2452 register Command *c;
2459 (void) signal(SIGINT, SIG_DFL);
2460 (void) signal(SIGQUIT, SIG_DFL);
2461 #endif /* defined(unix) */
2464 if (rlogin == _POSIX_VDISABLE)
2465 printf("%s> ", prompt);
2469 while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
2472 if (cp == line || *--cp != '\n' || cp == line)
2475 if (rlogin == _POSIX_VDISABLE)
2476 printf("%s\n", line);
2479 if (rlogin != _POSIX_VDISABLE)
2480 printf("%s> ", prompt);
2481 if (fgets(line, sizeof(line), stdin) == NULL) {
2482 if (feof(stdin) || ferror(stdin)) {
2492 if (margv[0] == 0) {
2495 c = getcmd(margv[0]);
2497 printf("?Ambiguous command\n");
2501 printf("?Invalid command\n");
2504 if (c->needconnect && !connected) {
2505 printf("?Need to be connected first.\n");
2508 if ((*c->handler)(margc, margv)) {
2514 longjmp(toplevel, 1);
2518 if (shell_active == 0) {
2521 #else /* defined(TN3270) */
2523 #endif /* defined(TN3270) */
2535 register Command *c;
2538 printf("Commands may be abbreviated. Commands are:\n\n");
2539 for (c = cmdtab; c->name; c++)
2541 printf("%-*s\t%s\n", HELPINDENT, c->name,
2546 while (--argc > 0) {
2551 printf("?Ambiguous help command %s\n", arg);
2552 else if (c == (Command *)0)
2553 printf("?Invalid help command %s\n", arg);
2555 printf("%s\n", c->help);
2560 static char *rcname = 0;
2561 static char rcbuf[128];
2566 register Command *c;
2569 int l1 = strlen(m1);
2570 int l2 = strlen(m2);
2580 rcname = getenv("HOME");
2581 if (rcname && (strlen(rcname) + 10) < sizeof(rcbuf))
2582 strcpy(rcbuf, rcname);
2585 strcat(rcbuf, "/.telnetrc");
2589 if ((rcfile = fopen(rcname, "r")) == 0) {
2594 if (fgets(line, sizeof(line), rcfile) == NULL)
2601 if (!isspace(line[0]))
2604 if (gotmachine == 0) {
2605 if (isspace(line[0]))
2607 if (strncasecmp(line, m1, l1) == 0)
2608 strncpy(line, &line[l1], sizeof(line) - l1);
2609 else if (strncasecmp(line, m2, l2) == 0)
2610 strncpy(line, &line[l2], sizeof(line) - l2);
2611 else if (strncasecmp(line, "DEFAULT", 7) == 0)
2612 strncpy(line, &line[7], sizeof(line) - 7);
2615 if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
2622 c = getcmd(margv[0]);
2624 printf("?Ambiguous command: %s\n", margv[0]);
2628 printf("?Invalid command: %s\n", margv[0]);
2632 * This should never happen...
2634 if (c->needconnect && !connected) {
2635 printf("?Need to be connected first for %s.\n", margv[0]);
2638 (*c->handler)(margc, margv);
2643 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2646 * Source route is handed in as
2647 * [!]@hop1@hop2...[@|:]dst
2648 * If the leading ! is present, it is a
2649 * strict source route, otherwise it is
2650 * assmed to be a loose source route.
2652 * We fill in the source route option as
2653 * hop1,hop2,hop3...dest
2654 * and return a pointer to hop1, which will
2655 * be the address to connect() to.
2658 * arg: pointer to route list to decipher
2660 * cpp: If *cpp is not equal to NULL, this is a
2661 * pointer to a pointer to a character array
2662 * that should be filled in with the option.
2664 * lenp: pointer to an integer that contains the
2665 * length of *cpp if *cpp != NULL.
2669 * Returns the address of the host to connect to. If the
2670 * return value is -1, there was a syntax error in the
2671 * option, either unknown characters, or too many hosts.
2672 * If the return value is 0, one of the hostnames in the
2673 * path is unknown, and *cpp is set to point to the bad
2676 * *cpp: If *cpp was equal to NULL, it will be filled
2677 * in with a pointer to our static area that has
2678 * the option filled in. This will be 32bit aligned.
2680 * *lenp: This will be filled in with how long the option
2681 * pointed to by *cpp is.
2685 sourceroute(arg, cpp, lenp)
2690 static char lsr[44];
2694 char *cp, *cp2, *lsrp, *lsrep;
2696 struct in_addr sin_addr;
2697 register struct hostent *host = 0;
2701 * Verify the arguments, and make sure we have
2702 * at least 7 bytes for the option.
2704 if (cpp == NULL || lenp == NULL)
2705 return((unsigned long)-1);
2706 if (*cpp != NULL && *lenp < 7)
2707 return((unsigned long)-1);
2709 * Decide whether we have a buffer passed to us,
2710 * or if we need to use our own static buffer.
2714 lsrep = lsrp + *lenp;
2723 * Next, decide whether we have a loose source
2724 * route or a strict source route, and fill in
2725 * the begining of the option.
2730 *lsrp++ = IPOPT_SSRR;
2732 *lsrp++ = IPOPT_LSRR;
2736 ipopt.io_type = IPOPT_SSRR;
2738 ipopt.io_type = IPOPT_LSRR;
2742 return((unsigned long)-1);
2745 lsrp++; /* skip over length, we'll fill it in later */
2751 sin_addr.s_addr = 0;
2756 else for (cp2 = cp; c = *cp2; cp2++) {
2761 } else if (c == '@') {
2763 } else if (c == ':') {
2772 if ((tmp = inet_addr(cp)) != -1) {
2773 sin_addr.s_addr = tmp;
2774 } else if (host = gethostbyname(cp)) {
2776 memcpy((caddr_t)&sin_addr, host->h_addr_list[0],
2777 MIN(host->h_length,sizeof(sin_addr)));
2779 memcpy((caddr_t)&sin_addr, host->h_addr,
2780 MIN(host->h_length,sizeof(sin_addr)));
2786 memcpy(lsrp, (char *)&sin_addr, 4);
2793 * Check to make sure there is space for next address
2795 if (lsrp + 4 > lsrep)
2796 return((unsigned long)-1);
2799 if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
2802 return((unsigned long)-1);
2804 *lsrp++ = IPOPT_NOP; /* 32 bit word align it */
2805 *lenp = lsrp - *cpp;
2807 ipopt.io_len = lsrp - *cpp;
2808 if (ipopt.io_len <= 5) { /* Is 3 better ? */
2811 return((unsigned long)-1);
2813 *lenp = sizeof(ipopt);
2814 *cpp = (char *) &ipopt;
2816 return(sin_addr.s_addr);