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
34 #include "telnet_locl.h"
36 RCSID("$Id: commands.c,v 1.59 2000/01/08 08:04:16 assar Exp $");
38 #if defined(IPPROTO_IP) && defined(IP_TOS)
40 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
43 static char _hostname[MaxHostNameLen];
45 typedef int (*intrtn_t)(int, char**);
46 static int call(intrtn_t, ...);
49 char *name; /* command name */
50 char *help; /* help string (NULL for no help) */
51 int (*handler)(); /* routine which executes command */
52 int needconnect; /* Do we need to be connected to execute? */
55 static char line[256];
56 static char saveline[256];
58 static char *margv[20];
68 if (*cp == '!') { /* Special case shell escape */
69 /* save for shell command */
70 strlcpy(saveline, line, sizeof(saveline));
71 *argp++ = "!"; /* No room in string to get this */
83 for (cp2 = cp; c != '\0'; c = *++cp) {
91 if ((c = *++cp) == '\0')
93 } else if (c == '"') {
96 } else if (c == '\'') {
99 } else if (isspace(c))
113 * Make a character string into a number.
115 * Todo: 1. Could take random integers (12, 0x12, 012, 0b1).
128 c = b | 0x40; /* DEL */
141 * Construct a control character sequence
142 * for a special character.
149 * The only way I could get the Sun 3.5 compiler
151 * if ((unsigned int)c >= 0x80)
152 * was to assign "c" to an unsigned int variable...
155 unsigned int uic = (unsigned int)c;
159 if (c == (cc_t)_POSIX_VDISABLE) {
164 buf[1] = ((c>>6)&07) + '0';
165 buf[2] = ((c>>3)&07) + '0';
166 buf[3] = (c&07) + '0';
168 } else if (uic >= 0x20) {
182 * The following are data structures and routines for
183 * the "send" command.
188 char *name; /* How user refers to it (case independent) */
189 char *help; /* Help information (0 ==> no help) */
190 int needconnect; /* Need to be connected */
191 int narg; /* Number of arguments */
192 int (*handler)(); /* Routine to perform (for special ops) */
193 int nbyte; /* Number of bytes to send this command */
194 int what; /* Character to be sent (<0 ==> special) */
202 send_dontcmd (char *),
203 send_willcmd (char *),
204 send_wontcmd (char *);
206 static struct sendlist Sendlist[] = {
207 { "ao", "Send Telnet Abort output", 1, 0, 0, 2, AO },
208 { "ayt", "Send Telnet 'Are You There'", 1, 0, 0, 2, AYT },
209 { "brk", "Send Telnet Break", 1, 0, 0, 2, BREAK },
210 { "break", 0, 1, 0, 0, 2, BREAK },
211 { "ec", "Send Telnet Erase Character", 1, 0, 0, 2, EC },
212 { "el", "Send Telnet Erase Line", 1, 0, 0, 2, EL },
213 { "escape", "Send current escape character", 1, 0, send_esc, 1, 0 },
214 { "ga", "Send Telnet 'Go Ahead' sequence", 1, 0, 0, 2, GA },
215 { "ip", "Send Telnet Interrupt Process", 1, 0, 0, 2, IP },
216 { "intp", 0, 1, 0, 0, 2, IP },
217 { "interrupt", 0, 1, 0, 0, 2, IP },
218 { "intr", 0, 1, 0, 0, 2, IP },
219 { "nop", "Send Telnet 'No operation'", 1, 0, 0, 2, NOP },
220 { "eor", "Send Telnet 'End of Record'", 1, 0, 0, 2, EOR },
221 { "abort", "Send Telnet 'Abort Process'", 1, 0, 0, 2, ABORT },
222 { "susp", "Send Telnet 'Suspend Process'", 1, 0, 0, 2, SUSP },
223 { "eof", "Send Telnet End of File Character", 1, 0, 0, 2, xEOF },
224 { "synch", "Perform Telnet 'Synch operation'", 1, 0, dosynch, 2, 0 },
225 { "getstatus", "Send request for STATUS", 1, 0, get_status, 6, 0 },
226 { "?", "Display send options", 0, 0, send_help, 0, 0 },
227 { "help", 0, 0, 0, send_help, 0, 0 },
228 { "do", 0, 0, 1, send_docmd, 3, 0 },
229 { "dont", 0, 0, 1, send_dontcmd, 3, 0 },
230 { "will", 0, 0, 1, send_willcmd, 3, 0 },
231 { "wont", 0, 0, 1, send_wontcmd, 3, 0 },
235 #define GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
236 sizeof(struct sendlist)))
239 sendcmd(int argc, char **argv)
241 int count; /* how many bytes we are going to need to send */
243 struct sendlist *s; /* pointer to current command */
248 printf("need at least one argument for 'send' command\r\n");
249 printf("'send ?' for help\r\n");
253 * First, validate all the send arguments.
254 * In addition, we see how much space we are going to need, and
255 * whether or not we will be doing a "SYNCH" operation (which
256 * flushes the network queue).
259 for (i = 1; i < argc; i++) {
260 s = GETSEND(argv[i]);
262 printf("Unknown send argument '%s'\r\n'send ?' for help.\r\n",
265 } else if (Ambiguous(s)) {
266 printf("Ambiguous send argument '%s'\r\n'send ?' for help.\r\n",
270 if (i + s->narg >= argc) {
272 "Need %d argument%s to 'send %s' command. 'send %s ?' for help.\r\n",
273 s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
277 if (s->handler == send_help) {
283 needconnect += s->needconnect;
285 if (!connected && needconnect) {
286 printf("?Need to be connected first.\r\n");
287 printf("'send ?' for help\r\n");
290 /* Now, do we have enough room? */
291 if (NETROOM() < count) {
292 printf("There is not enough room in the buffer TO the network\r\n");
293 printf("to process your request. Nothing will be done.\r\n");
294 printf("('send synch' will throw away most data in the network\r\n");
295 printf("buffer, if this might help.)\r\n");
298 /* OK, they are all OK, now go through again and actually send */
300 for (i = 1; i < argc; i++) {
301 if ((s = GETSEND(argv[i])) == 0) {
302 fprintf(stderr, "Telnet 'send' error - argument disappeared!\r\n");
308 success += (*s->handler)((s->narg > 0) ? argv[i+1] : 0,
309 (s->narg > 1) ? argv[i+2] : 0);
312 NET2ADD(IAC, s->what);
313 printoption("SENT", IAC, s->what);
316 return (count == success);
320 send_tncmd(void (*func)(), char *cmd, char *name);
330 send_docmd(char *name)
332 return(send_tncmd(send_do, "do", name));
336 send_dontcmd(char *name)
338 return(send_tncmd(send_dont, "dont", name));
342 send_willcmd(char *name)
344 return(send_tncmd(send_will, "will", name));
348 send_wontcmd(char *name)
350 return(send_tncmd(send_wont, "wont", name));
354 send_tncmd(void (*func)(), char *cmd, char *name)
357 extern char *telopts[];
360 if (isprefix(name, "help") || isprefix(name, "?")) {
363 printf("Usage: send %s <value|option>\r\n", cmd);
364 printf("\"value\" must be from 0 to 255\r\n");
365 printf("Valid options are:\r\n\t");
368 for (cpp = telopts; *cpp; cpp++) {
369 len = strlen(*cpp) + 3;
370 if (col + len > 65) {
374 printf(" \"%s\"", *cpp);
380 cpp = genget(name, telopts, sizeof(char *));
381 if (Ambiguous(cpp)) {
382 fprintf(stderr,"'%s': ambiguous argument ('send %s ?' for help).\r\n",
391 while (*cp >= '0' && *cp <= '9') {
397 fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\r\n",
400 } else if (val < 0 || val > 255) {
401 fprintf(stderr, "'%s': bad value ('send %s ?' for help).\r\n",
407 printf("?Need to be connected first.\r\n");
417 struct sendlist *s; /* pointer to current command */
418 for (s = Sendlist; s->name; s++) {
420 printf("%-15s %s\r\n", s->name, s->help);
426 * The following are the routines and data structures referred
427 * to by the arguments to the "toggle" command.
442 (SetSockOpt(net, SOL_SOCKET, SO_DEBUG, debug)) < 0) {
443 perror("setsockopt (SO_DEBUG)");
447 if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 0, 0) < 0)
448 perror("setsockopt (SO_DEBUG)");
450 printf("Cannot turn off socket debugging\r\n");
455 #if defined(KRB4) && defined(HAVE_KRB_DISABLE_DEBUG)
473 printf("Will send carriage returns as telnet <CR><LF>.\r\n");
475 printf("Will send carriage returns as telnet <CR><NUL>.\r\n");
485 donebinarytoggle = 1;
490 if (my_want_state_is_will(TELOPT_BINARY) &&
491 my_want_state_is_do(TELOPT_BINARY)) {
493 } else if (my_want_state_is_wont(TELOPT_BINARY) &&
494 my_want_state_is_dont(TELOPT_BINARY)) {
497 val = binmode ? 0 : 1;
501 if (my_want_state_is_will(TELOPT_BINARY) &&
502 my_want_state_is_do(TELOPT_BINARY)) {
503 printf("Already operating in binary mode with remote host.\r\n");
505 printf("Negotiating binary mode with remote host.\r\n");
509 if (my_want_state_is_wont(TELOPT_BINARY) &&
510 my_want_state_is_dont(TELOPT_BINARY)) {
511 printf("Already in network ascii mode with remote host.\r\n");
513 printf("Negotiating network ascii mode with remote host.\r\n");
523 donebinarytoggle = 1;
526 val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
529 if (my_want_state_is_do(TELOPT_BINARY)) {
530 printf("Already receiving in binary mode.\r\n");
532 printf("Negotiating binary mode on input.\r\n");
536 if (my_want_state_is_dont(TELOPT_BINARY)) {
537 printf("Already receiving in network ascii mode.\r\n");
539 printf("Negotiating network ascii mode on input.\r\n");
549 donebinarytoggle = 1;
552 val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
555 if (my_want_state_is_will(TELOPT_BINARY)) {
556 printf("Already transmitting in binary mode.\r\n");
558 printf("Negotiating binary mode on output.\r\n");
562 if (my_want_state_is_wont(TELOPT_BINARY)) {
563 printf("Already transmitting in network ascii mode.\r\n");
565 printf("Negotiating network ascii mode on output.\r\n");
573 static int togglehelp (void);
574 #if defined(AUTHENTICATION)
575 extern int auth_togdebug (int);
577 #if defined(ENCRYPTION)
578 extern int EncryptAutoEnc (int);
579 extern int EncryptAutoDec (int);
580 extern int EncryptDebug (int);
581 extern int EncryptVerbose (int);
585 char *name; /* name of toggle */
586 char *help; /* help message */
587 int (*handler)(); /* routine to do actual setting */
589 char *actionexplanation;
592 static struct togglelist Togglelist[] = {
594 "flushing of output when sending interrupt characters",
597 "flush output when sending interrupt characters" },
599 "automatic sending of interrupt characters in urgent mode",
602 "send interrupt characters in urgent mode" },
603 #if defined(AUTHENTICATION)
605 "automatic sending of login and/or authentication info",
608 "send login name and/or authentication information" },
610 "Toggle authentication debugging",
613 "print authentication debugging information" },
615 #if defined(ENCRYPTION)
617 "automatic encryption of data stream",
620 "automatically encrypt output" },
622 "automatic decryption of data stream",
625 "automatically decrypt input" },
627 "Toggle verbose encryption output",
630 "print verbose encryption output" },
632 "Toggle encryption debugging",
635 "print encryption debugging information" },
638 "don't read ~/.telnetrc file",
641 "skip reading of ~/.telnetrc file" },
643 "sending and receiving of binary data",
648 "receiving of binary data",
653 "sending of binary data",
658 "sending carriage returns as telnet <CR><LF>",
663 "mapping of received carriage returns",
666 "map carriage return on output" },
668 "local recognition of certain control characters",
671 "recognize certain control characters" },
672 { " ", "", 0 }, /* empty line */
677 "turn on socket level debugging" },
678 #if defined(KRB4) && defined(HAVE_KRB_DISABLE_DEBUG)
680 "kerberos 4 debugging",
683 "turn on kerberos 4 debugging" },
686 "printing of hexadecimal network data (debugging)",
689 "print hexadecimal representation of network traffic" },
691 "output of \"netdata\" to user readable format (debugging)",
694 "print user readable output for \"netdata\"" },
696 "viewing of options processing (debugging)",
699 "show option processing" },
701 "(debugging) toggle printing of hexadecimal terminal data",
704 "print hexadecimal representation of terminal traffic" },
717 struct togglelist *c;
719 for (c = Togglelist; c->name; c++) {
722 printf("%-15s toggle %s\r\n", c->name, c->help);
728 printf("%-15s %s\r\n", "?", "display help information");
733 settogglehelp(int set)
735 struct togglelist *c;
737 for (c = Togglelist; c->name; c++) {
740 printf("%-15s %s %s\r\n", c->name, set ? "enable" : "disable",
748 #define GETTOGGLE(name) (struct togglelist *) \
749 genget(name, (char **) Togglelist, sizeof(struct togglelist))
752 toggle(int argc, char *argv[])
756 struct togglelist *c;
760 "Need an argument to 'toggle' command. 'toggle ?' for help.\r\n");
769 fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\r\n",
773 fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\r\n",
778 *c->variable = !*c->variable; /* invert it */
779 if (c->actionexplanation) {
780 printf("%s %s.\r\n", *c->variable? "Will" : "Won't",
781 c->actionexplanation);
785 retval &= (*c->handler)(-1);
793 * The following perform the "set" command.
796 struct termios new_tc = { 0 };
799 char *name; /* name */
800 char *help; /* help information */
802 cc_t *charp; /* where it is located at */
805 static struct setlist Setlist[] = {
806 #ifdef KLUDGELINEMODE
807 { "echo", "character to toggle local echoing on/off", 0, &echoc },
809 { "escape", "character to escape back to telnet command mode", 0, &escape },
810 { "rlogin", "rlogin escape character", 0, &rlogin },
811 { "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile},
813 { " ", "The following need 'localchars' to be toggled true", 0, 0 },
814 { "flushoutput", "character to cause an Abort Output", 0, &termFlushChar },
815 { "interrupt", "character to cause an Interrupt Process", 0, &termIntChar },
816 { "quit", "character to cause an Abort process", 0, &termQuitChar },
817 { "eof", "character to cause an EOF ", 0, &termEofChar },
819 { " ", "The following are for local editing in linemode", 0, 0 },
820 { "erase", "character to use to erase a character", 0, &termEraseChar },
821 { "kill", "character to use to erase a line", 0, &termKillChar },
822 { "lnext", "character to use for literal next", 0, &termLiteralNextChar },
823 { "susp", "character to cause a Suspend Process", 0, &termSuspChar },
824 { "reprint", "character to use for line reprint", 0, &termRprntChar },
825 { "worderase", "character to use to erase a word", 0, &termWerasChar },
826 { "start", "character to use for XON", 0, &termStartChar },
827 { "stop", "character to use for XOFF", 0, &termStopChar },
828 { "forw1", "alternate end of line character", 0, &termForw1Char },
829 { "forw2", "alternate end of line character", 0, &termForw2Char },
830 { "ayt", "alternate AYT character", 0, &termAytChar },
834 static struct setlist *
837 return (struct setlist *)
838 genget(name, (char **) Setlist, sizeof(struct setlist));
842 set_escape_char(char *s)
844 if (rlogin != _POSIX_VDISABLE) {
845 rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
846 printf("Telnet rlogin escape character is '%s'.\r\n",
849 escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
850 printf("Telnet escape character is '%s'.\r\n", control(escape));
855 setcmd(int argc, char *argv[])
859 struct togglelist *c;
861 if (argc < 2 || argc > 3) {
862 printf("Format is 'set Name Value'\r\n'set ?' for help.\r\n");
865 if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
866 for (ct = Setlist; ct->name; ct++)
867 printf("%-15s %s\r\n", ct->name, ct->help);
870 printf("%-15s %s\r\n", "?", "display help information");
874 ct = getset(argv[1]);
876 c = GETTOGGLE(argv[1]);
878 fprintf(stderr, "'%s': unknown argument ('set ?' for help).\r\n",
881 } else if (Ambiguous(c)) {
882 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\r\n",
887 if ((argc == 2) || (strcmp("on", argv[2]) == 0))
889 else if (strcmp("off", argv[2]) == 0)
892 printf("Format is 'set togglename [on|off]'\r\n'set ?' for help.\r\n");
895 if (c->actionexplanation) {
896 printf("%s %s.\r\n", *c->variable? "Will" : "Won't",
897 c->actionexplanation);
902 } else if (argc != 3) {
903 printf("Format is 'set Name Value'\r\n'set ?' for help.\r\n");
905 } else if (Ambiguous(ct)) {
906 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\r\n",
909 } else if (ct->handler) {
910 (*ct->handler)(argv[2]);
911 printf("%s set to \"%s\".\r\n", ct->name, (char *)ct->charp);
913 if (strcmp("off", argv[2])) {
914 value = special(argv[2]);
916 value = _POSIX_VDISABLE;
918 *(ct->charp) = (cc_t)value;
919 printf("%s character is '%s'.\r\n", ct->name, control(*(ct->charp)));
926 unsetcmd(int argc, char *argv[])
929 struct togglelist *c;
934 "Need an argument to 'unset' command. 'unset ?' for help.\r\n");
937 if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
938 for (ct = Setlist; ct->name; ct++)
939 printf("%-15s %s\r\n", ct->name, ct->help);
942 printf("%-15s %s\r\n", "?", "display help information");
954 fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\r\n",
957 } else if (Ambiguous(c)) {
958 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\r\n",
964 if (c->actionexplanation) {
965 printf("%s %s.\r\n", *c->variable? "Will" : "Won't",
966 c->actionexplanation);
971 } else if (Ambiguous(ct)) {
972 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\r\n",
975 } else if (ct->handler) {
977 printf("%s reset to \"%s\".\r\n", ct->name, (char *)ct->charp);
979 *(ct->charp) = _POSIX_VDISABLE;
980 printf("%s character is '%s'.\r\n", ct->name, control(*(ct->charp)));
987 * The following are the data structures and routines for the
990 #ifdef KLUDGELINEMODE
991 extern int kludgelinemode;
997 send_wont(TELOPT_LINEMODE, 1);
998 send_dont(TELOPT_SGA, 1);
999 send_dont(TELOPT_ECHO, 1);
1007 #ifdef KLUDGELINEMODE
1009 send_dont(TELOPT_SGA, 1);
1011 send_will(TELOPT_LINEMODE, 1);
1012 send_dont(TELOPT_ECHO, 1);
1019 #ifdef KLUDGELINEMODE
1021 send_do(TELOPT_SGA, 1);
1024 send_wont(TELOPT_LINEMODE, 1);
1025 send_do(TELOPT_ECHO, 1);
1030 dolmmode(int bit, int on)
1033 extern int linemode;
1035 if (my_want_state_is_wont(TELOPT_LINEMODE)) {
1036 printf("?Need to have LINEMODE option enabled first.\r\n");
1037 printf("'mode ?' for help.\r\n");
1042 c = (linemode | bit);
1044 c = (linemode & ~bit);
1052 return dolmmode(bit, 1);
1056 tn_clearmode(int bit)
1058 return dolmmode(bit, 0);
1062 char *name; /* command name */
1063 char *help; /* help string */
1064 int (*handler)(); /* routine which executes command */
1065 int needconnect; /* Do we need to be connected to execute? */
1069 static int modehelp(void);
1071 static struct modelist ModeList[] = {
1072 { "character", "Disable LINEMODE option", docharmode, 1 },
1073 #ifdef KLUDGELINEMODE
1074 { "", "(or disable obsolete line-by-line mode)", 0 },
1076 { "line", "Enable LINEMODE option", dolinemode, 1 },
1077 #ifdef KLUDGELINEMODE
1078 { "", "(or enable obsolete line-by-line mode)", 0 },
1081 { "", "These require the LINEMODE option to be enabled", 0 },
1082 { "isig", "Enable signal trapping", tn_setmode, 1, MODE_TRAPSIG },
1083 { "+isig", 0, tn_setmode, 1, MODE_TRAPSIG },
1084 { "-isig", "Disable signal trapping", tn_clearmode, 1, MODE_TRAPSIG },
1085 { "edit", "Enable character editing", tn_setmode, 1, MODE_EDIT },
1086 { "+edit", 0, tn_setmode, 1, MODE_EDIT },
1087 { "-edit", "Disable character editing", tn_clearmode, 1, MODE_EDIT },
1088 { "softtabs", "Enable tab expansion", tn_setmode, 1, MODE_SOFT_TAB },
1089 { "+softtabs", 0, tn_setmode, 1, MODE_SOFT_TAB },
1090 { "-softtabs", "Disable character editing", tn_clearmode, 1, MODE_SOFT_TAB },
1091 { "litecho", "Enable literal character echo", tn_setmode, 1, MODE_LIT_ECHO },
1092 { "+litecho", 0, tn_setmode, 1, MODE_LIT_ECHO },
1093 { "-litecho", "Disable literal character echo", tn_clearmode, 1, MODE_LIT_ECHO },
1094 { "help", 0, modehelp, 0 },
1095 #ifdef KLUDGELINEMODE
1096 { "kludgeline", 0, dokludgemode, 1 },
1099 { "?", "Print help information", modehelp, 0 },
1107 struct modelist *mt;
1109 printf("format is: 'mode Mode', where 'Mode' is one of:\r\n\r\n");
1110 for (mt = ModeList; mt->name; mt++) {
1113 printf("%-15s %s\r\n", mt->name, mt->help);
1121 #define GETMODECMD(name) (struct modelist *) \
1122 genget(name, (char **) ModeList, sizeof(struct modelist))
1125 modecmd(int argc, char **argv)
1127 struct modelist *mt;
1130 printf("'mode' command requires an argument\r\n");
1131 printf("'mode ?' for help.\r\n");
1132 } else if ((mt = GETMODECMD(argv[1])) == 0) {
1133 fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\r\n", argv[1]);
1134 } else if (Ambiguous(mt)) {
1135 fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\r\n", argv[1]);
1136 } else if (mt->needconnect && !connected) {
1137 printf("?Need to be connected first.\r\n");
1138 printf("'mode ?' for help.\r\n");
1139 } else if (mt->handler) {
1140 return (*mt->handler)(mt->arg1);
1146 * The following data structures and routines implement the
1147 * "display" command.
1151 display(int argc, char *argv[])
1153 struct togglelist *tl;
1156 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \
1157 if (*tl->variable) { \
1162 printf(" %s.\r\n", tl->actionexplanation); \
1165 #define doset(sl) if (sl->name && *sl->name != ' ') { \
1166 if (sl->handler == 0) \
1167 printf("%-15s [%s]\r\n", sl->name, control(*sl->charp)); \
1169 printf("%-15s \"%s\"\r\n", sl->name, (char *)sl->charp); \
1173 for (tl = Togglelist; tl->name; tl++) {
1177 for (sl = Setlist; sl->name; sl++) {
1183 for (i = 1; i < argc; i++) {
1184 sl = getset(argv[i]);
1185 tl = GETTOGGLE(argv[i]);
1186 if (Ambiguous(sl) || Ambiguous(tl)) {
1187 printf("?Ambiguous argument '%s'.\r\n", argv[i]);
1189 } else if (!sl && !tl) {
1190 printf("?Unknown argument '%s'.\r\n", argv[i]);
1202 /*@*/optionstatus();
1203 #if defined(ENCRYPTION)
1212 * The following are the data structures, and many of the routines,
1213 * relating to command processing.
1217 * Set the escape character.
1220 setescape(int argc, char *argv[])
1226 "Deprecated usage - please use 'set escape%s%s' in the future.\r\n",
1227 (argc > 2)? " ":"", (argc > 2)? argv[1]: "");
1231 printf("new escape character: ");
1232 fgets(buf, sizeof(buf), stdin);
1237 printf("Escape character is '%s'.\r\n", control(escape));
1247 printf("Deprecated usage - please use 'toggle crmod' in the future.\r\n");
1248 printf("%s map carriage return on output.\r\n", crmod ? "Will" : "Won't");
1259 long oldrows, oldcols, newrows, newcols, err;
1261 err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1264 * If we didn't get the window size before the SUSPEND, but we
1265 * can get them now (?), then send the NAWS to make sure that
1266 * we are set up for the right window size.
1268 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1269 (err || ((oldrows != newrows) || (oldcols != newcols)))) {
1273 /* reget parameters in case they were changed */
1274 TerminalSaveState();
1277 printf("Suspend is not supported. Try the '!' command instead\r\n");
1283 shell(int argc, char **argv)
1285 long oldrows, oldcols, newrows, newcols, err;
1289 err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1292 perror("Fork failed\r\n");
1298 * Fire up the shell in the child.
1300 char *shellp, *shellname;
1302 shellp = getenv("SHELL");
1305 if ((shellname = strrchr(shellp, '/')) == 0)
1310 execl(shellp, shellname, "-c", &saveline[1], 0);
1312 execl(shellp, shellname, 0);
1317 wait((int *)0); /* Wait for the shell to complete */
1319 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1320 (err || ((oldrows != newrows) || (oldcols != newcols)))) {
1329 bye(int argc, char **argv)
1331 extern int resettermname;
1335 printf("Connection closed.\r\n");
1339 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
1340 auth_encrypt_connect(connected);
1345 if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0))
1346 longjmp(toplevel, 1);
1347 return 0; /* NOTREACHED */
1353 call(bye, "bye", "fromquit", 0);
1355 return 0; /*NOTREACHED*/
1361 send_do(TELOPT_LOGOUT, 1);
1378 static void slc_help(void);
1380 struct slclist SlcList[] = {
1381 { "export", "Use local special character definitions",
1382 slc_mode_export, 0 },
1383 { "import", "Use remote special character definitions",
1384 slc_mode_import, 1 },
1385 { "check", "Verify remote special character definitions",
1386 slc_mode_import, 0 },
1387 { "help", 0, slc_help, 0 },
1388 { "?", "Print help information", slc_help, 0 },
1397 for (c = SlcList; c->name; c++) {
1400 printf("%-15s %s\r\n", c->name, c->help);
1407 static struct slclist *
1410 return (struct slclist *)
1411 genget(name, (char **) SlcList, sizeof(struct slclist));
1415 slccmd(int argc, char **argv)
1421 "Need an argument to 'slc' command. 'slc ?' for help.\r\n");
1424 c = getslc(argv[1]);
1426 fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\r\n",
1431 fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\r\n",
1435 (*c->handler)(c->arg);
1441 * The ENVIRON command.
1451 static void env_help (void);
1453 struct envlist EnvList[] = {
1454 { "define", "Define an environment variable",
1455 (void (*)())env_define, 2 },
1456 { "undefine", "Undefine an environment variable",
1458 { "export", "Mark an environment variable for automatic export",
1460 { "unexport", "Don't mark an environment variable for automatic export",
1462 { "send", "Send an environment variable", env_send, 1 },
1463 { "list", "List the current environment variables",
1465 { "help", 0, env_help, 0 },
1466 { "?", "Print help information", env_help, 0 },
1475 for (c = EnvList; c->name; c++) {
1478 printf("%-15s %s\r\n", c->name, c->help);
1485 static struct envlist *
1486 getenvcmd(char *name)
1488 return (struct envlist *)
1489 genget(name, (char **) EnvList, sizeof(struct envlist));
1493 env_cmd(int argc, char **argv)
1499 "Need an argument to 'environ' command. 'environ ?' for help.\r\n");
1502 c = getenvcmd(argv[1]);
1504 fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\r\n",
1509 fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\r\n",
1513 if (c->narg + 2 != argc) {
1515 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\r\n",
1516 c->narg < argc + 2 ? "only " : "",
1517 c->narg, c->narg == 1 ? "" : "s", c->name);
1520 (*c->handler)(argv[2], argv[3]);
1525 struct env_lst *next; /* pointer to next structure */
1526 struct env_lst *prev; /* pointer to previous structure */
1527 unsigned char *var; /* pointer to variable name */
1528 unsigned char *value; /* pointer to variable value */
1529 int export; /* 1 -> export with default list of variables */
1530 int welldefined; /* A well defined variable */
1533 struct env_lst envlisthead;
1536 env_find(unsigned char *var)
1540 for (ep = envlisthead.next; ep; ep = ep->next) {
1541 if (strcmp((char *)ep->var, (char *)var) == 0)
1548 #define environ _environ
1554 extern char **environ;
1558 for (epp = environ; *epp; epp++) {
1559 if ((cp = strchr(*epp, '='))) {
1561 ep = env_define((unsigned char *)*epp,
1562 (unsigned char *)cp+1);
1568 * Special case for DISPLAY variable. If it is ":0.0" or
1569 * "unix:0.0", we have to get rid of "unix" and insert our
1572 if ((ep = env_find("DISPLAY"))
1573 && (*ep->value == ':'
1574 || strncmp((char *)ep->value, "unix:", 5) == 0)) {
1576 char *cp2 = strchr((char *)ep->value, ':');
1578 /* XXX - should be k_gethostname? */
1579 gethostname(hbuf, 256);
1582 /* If this is not the full name, try to get it via DNS */
1583 if (strchr(hbuf, '.') == 0) {
1584 struct addrinfo hints, *ai, *a;
1587 memset (&hints, 0, sizeof(hints));
1588 hints.ai_flags = AI_CANONNAME;
1590 error = getaddrinfo (hbuf, NULL, &hints, &ai);
1592 for (a = ai; a != NULL; a = a->ai_next)
1593 if (a->ai_canonname != NULL) {
1603 asprintf (&cp, "%s%s", hbuf, cp2);
1605 ep->value = (unsigned char *)cp;
1608 * If USER is not defined, but LOGNAME is, then add
1609 * USER with the value from LOGNAME. By default, we
1610 * don't export the USER variable.
1612 if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
1613 env_define((unsigned char *)"USER", ep->value);
1614 env_unexport((unsigned char *)"USER");
1616 env_export((unsigned char *)"DISPLAY");
1617 env_export((unsigned char *)"PRINTER");
1618 env_export((unsigned char *)"XAUTHORITY");
1622 env_define(unsigned char *var, unsigned char *value)
1626 if ((ep = env_find(var))) {
1632 ep = (struct env_lst *)malloc(sizeof(struct env_lst));
1633 ep->next = envlisthead.next;
1634 envlisthead.next = ep;
1635 ep->prev = &envlisthead;
1637 ep->next->prev = ep;
1639 ep->welldefined = opt_welldefined((char *)var);
1641 ep->var = (unsigned char *)strdup((char *)var);
1642 ep->value = (unsigned char *)strdup((char *)value);
1647 env_undefine(unsigned char *var)
1651 if ((ep = env_find(var))) {
1652 ep->prev->next = ep->next;
1654 ep->next->prev = ep->prev;
1664 env_export(unsigned char *var)
1668 if ((ep = env_find(var)))
1673 env_unexport(unsigned char *var)
1677 if ((ep = env_find(var)))
1682 env_send(unsigned char *var)
1686 if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1688 && my_state_is_wont(TELOPT_OLD_ENVIRON)
1692 "Cannot send '%s': Telnet ENVIRON option not enabled\r\n",
1698 fprintf(stderr, "Cannot send '%s': variable not defined\r\n",
1702 env_opt_start_info();
1703 env_opt_add(ep->var);
1712 for (ep = envlisthead.next; ep; ep = ep->next) {
1713 printf("%c %-20s %s\r\n", ep->export ? '*' : ' ',
1714 ep->var, ep->value);
1719 env_default(int init, int welldefined)
1721 static struct env_lst *nep = NULL;
1728 while ((nep = nep->next)) {
1729 if (nep->export && (nep->welldefined == welldefined))
1737 env_getvalue(unsigned char *var)
1741 if ((ep = env_find(var)))
1747 #if defined(AUTHENTICATION)
1749 * The AUTHENTICATE command.
1762 struct authlist AuthList[] = {
1763 { "status", "Display current status of authentication information",
1765 { "disable", "Disable an authentication type ('auth disable ?' for more)",
1767 { "enable", "Enable an authentication type ('auth enable ?' for more)",
1769 { "help", 0, auth_help, 0 },
1770 { "?", "Print help information", auth_help, 0 },
1779 for (c = AuthList; c->name; c++) {
1782 printf("%-15s %s\r\n", c->name, c->help);
1791 auth_cmd(int argc, char **argv)
1797 "Need an argument to 'auth' command. 'auth ?' for help.\r\n");
1801 c = (struct authlist *)
1802 genget(argv[1], (char **) AuthList, sizeof(struct authlist));
1804 fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\r\n",
1809 fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\r\n",
1813 if (c->narg + 2 != argc) {
1815 "Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\r\n",
1816 c->narg < argc + 2 ? "only " : "",
1817 c->narg, c->narg == 1 ? "" : "s", c->name);
1820 return((*c->handler)(argv[2], argv[3]));
1825 #if defined(ENCRYPTION)
1827 * The ENCRYPT command.
1830 struct encryptlist {
1842 struct encryptlist EncryptList[] = {
1843 { "enable", "Enable encryption. ('encrypt enable ?' for more)",
1844 EncryptEnable, 1, 1, 2 },
1845 { "disable", "Disable encryption. ('encrypt enable ?' for more)",
1846 EncryptDisable, 0, 1, 2 },
1847 { "type", "Set encryptiong type. ('encrypt type ?' for more)",
1848 EncryptType, 0, 1, 1 },
1849 { "start", "Start encryption. ('encrypt start ?' for more)",
1850 EncryptStart, 1, 0, 1 },
1851 { "stop", "Stop encryption. ('encrypt stop ?' for more)",
1852 EncryptStop, 1, 0, 1 },
1853 { "input", "Start encrypting the input stream",
1854 EncryptStartInput, 1, 0, 0 },
1855 { "-input", "Stop encrypting the input stream",
1856 EncryptStopInput, 1, 0, 0 },
1857 { "output", "Start encrypting the output stream",
1858 EncryptStartOutput, 1, 0, 0 },
1859 { "-output", "Stop encrypting the output stream",
1860 EncryptStopOutput, 1, 0, 0 },
1862 { "status", "Display current status of authentication information",
1863 EncryptStatus, 0, 0, 0 },
1864 { "help", 0, EncryptHelp, 0, 0, 0 },
1865 { "?", "Print help information", EncryptHelp, 0, 0, 0 },
1872 struct encryptlist *c;
1874 for (c = EncryptList; c->name; c++) {
1877 printf("%-15s %s\r\n", c->name, c->help);
1886 encrypt_cmd(int argc, char **argv)
1888 struct encryptlist *c;
1890 c = (struct encryptlist *)
1891 genget(argv[1], (char **) EncryptList, sizeof(struct encryptlist));
1893 fprintf(stderr, "'%s': unknown argument ('encrypt ?' for help).\r\n",
1898 fprintf(stderr, "'%s': ambiguous argument ('encrypt ?' for help).\r\n",
1903 if (argc < c->minarg || argc > c->maxarg) {
1904 if (c->minarg == c->maxarg) {
1905 fprintf(stderr, "Need %s%d argument%s ",
1906 c->minarg < argc ? "only " : "", c->minarg,
1907 c->minarg == 1 ? "" : "s");
1909 fprintf(stderr, "Need %s%d-%d arguments ",
1910 c->maxarg < argc ? "only " : "", c->minarg, c->maxarg);
1912 fprintf(stderr, "to 'encrypt %s' command. 'encrypt ?' for help.\r\n",
1916 if (c->needconnect && !connected) {
1917 if (!(argc && (isprefix(argv[2], "help") || isprefix(argv[2], "?")))) {
1918 printf("?Need to be connected first.\r\n");
1922 return ((*c->handler)(argc > 0 ? argv[2] : 0,
1923 argc > 1 ? argv[3] : 0,
1924 argc > 2 ? argv[4] : 0));
1930 * Print status about the connection.
1934 status(int argc, char **argv)
1937 printf("Connected to %s.\r\n", hostname);
1938 if ((argc < 2) || strcmp(argv[1], "notmuch")) {
1939 int mode = getconnmode();
1941 if (my_want_state_is_will(TELOPT_LINEMODE)) {
1942 printf("Operating with LINEMODE option\r\n");
1943 printf("%s line editing\r\n", (mode&MODE_EDIT) ? "Local" : "No");
1944 printf("%s catching of signals\r\n",
1945 (mode&MODE_TRAPSIG) ? "Local" : "No");
1947 #ifdef KLUDGELINEMODE
1948 } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
1949 printf("Operating in obsolete linemode\r\n");
1952 printf("Operating in single character mode\r\n");
1954 printf("Catching signals locally\r\n");
1956 printf("%s character echo\r\n", (mode&MODE_ECHO) ? "Local" : "Remote");
1957 if (my_want_state_is_will(TELOPT_LFLOW))
1958 printf("%s flow control\r\n", (mode&MODE_FLOW) ? "Local" : "No");
1959 #if defined(ENCRYPTION)
1964 printf("No connection.\r\n");
1966 printf("Escape character is '%s'.\r\n", control(escape));
1973 * Function that gets called when SIGINFO is received.
1976 ayt_status(int ignore)
1978 call(status, "status", "notmuch", 0);
1982 static Command *getcmd(char *name);
1985 cmdrc(char *m1, char *m2)
1987 static char rcname[128];
1991 int l1 = strlen(m1);
1992 int l2 = strlen(m2);
1998 strlcpy(m1save, m1, sizeof(m1save));
2001 if (rcname[0] == 0) {
2002 char *home = getenv("HOME");
2004 snprintf (rcname, sizeof(rcname), "%s/.telnetrc",
2008 if ((rcfile = fopen(rcname, "r")) == 0) {
2013 if (fgets(line, sizeof(line), rcfile) == NULL)
2020 if (!isspace(line[0]))
2023 if (gotmachine == 0) {
2024 if (isspace(line[0]))
2026 if (strncasecmp(line, m1, l1) == 0)
2027 strncpy(line, &line[l1], sizeof(line) - l1);
2028 else if (strncasecmp(line, m2, l2) == 0)
2029 strncpy(line, &line[l2], sizeof(line) - l2);
2030 else if (strncasecmp(line, "DEFAULT", 7) == 0)
2031 strncpy(line, &line[7], sizeof(line) - 7);
2034 if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
2041 c = getcmd(margv[0]);
2043 printf("?Ambiguous command: %s\r\n", margv[0]);
2047 printf("?Invalid command: %s\r\n", margv[0]);
2051 * This should never happen...
2053 if (c->needconnect && !connected) {
2054 printf("?Need to be connected first for %s.\r\n", margv[0]);
2057 (*c->handler)(margc, margv);
2063 tn(int argc, char **argv)
2065 struct servent *sp = 0;
2066 extern char *inet_ntoa();
2067 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2071 char *cmd, *hostp = 0, *portp = 0;
2075 /* clear the socket address prior to use */
2078 printf("?Already connected to %s\r\n", hostname);
2083 strlcpy(line, "open ", sizeof(line));
2085 fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
2093 if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
2095 if (strcmp(*argv, "-l") == 0) {
2099 user = strdup(*argv++);
2103 if (strcmp(*argv, "-a") == 0) {
2119 printf("usage: %s [-l user] [-a] host-name [port]\r\n", cmd);
2127 if (*portp == '-') {
2134 sp = roken_getservbyname(portp, "tcp");
2138 printf("%s: bad port number\r\n", portp);
2147 sp = roken_getservbyname("telnet", "tcp");
2149 fprintf(stderr, "telnet: tcp/telnet: unknown service\r\n");
2159 struct addrinfo *ai, *a, hints;
2161 char portstr[NI_MAXSERV];
2163 memset (&hints, 0, sizeof(hints));
2164 hints.ai_socktype = SOCK_STREAM;
2165 hints.ai_protocol = IPPROTO_TCP;
2166 hints.ai_flags = AI_CANONNAME;
2168 snprintf (portstr, sizeof(portstr), "%u", ntohs(port));
2170 error = getaddrinfo (hostp, portstr, &hints, &ai);
2172 fprintf (stderr, "%s: %s\r\n", hostp, gai_strerror (error));
2176 strlcpy (_hostname, hostp, sizeof(_hostname));
2177 hostname = _hostname;
2179 for (a = ai; a != NULL && connected == 0; a = a->ai_next) {
2182 if (a->ai_canonname != NULL)
2183 strlcpy (_hostname, a->ai_canonname, sizeof(_hostname));
2185 if (getnameinfo (a->ai_addr, a->ai_addrlen,
2186 addrstr, sizeof(addrstr),
2187 NULL, 0, NI_NUMERICHOST) != 0)
2188 strlcpy (addrstr, "unknown address", sizeof(addrstr));
2190 printf("Trying %s...\r\n", addrstr);
2192 net = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
2195 warn ("telnet: socket");
2198 #if defined(IP_OPTIONS) && defined(IPPROTO_IP) && defined(HAVE_SETSOCKOPT)
2199 if (srp && setsockopt(net, IPPROTO_IP, IP_OPTIONS,
2200 (void *)srp, srlen) < 0)
2201 perror("setsockopt (IP_OPTIONS)");
2203 #if defined(IPPROTO_IP) && defined(IP_TOS)
2205 # if defined(HAVE_GETTOSBYNAME)
2207 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
2211 tos = 020; /* Low Delay bit */
2213 && (setsockopt(net, IPPROTO_IP, IP_TOS,
2214 (void *)&tos, sizeof(int)) < 0)
2215 && (errno != ENOPROTOOPT))
2216 perror("telnet: setsockopt (IP_TOS) (ignored)");
2218 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2219 if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
2220 perror("setsockopt (SO_DEBUG)");
2223 if (connect (net, a->ai_addr, a->ai_addrlen) < 0) {
2224 fprintf (stderr, "telnet: connect to address %s: %s\n",
2225 addrstr, strerror(errno));
2227 if (a->ai_next != NULL) {
2235 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
2236 auth_encrypt_connect(connected);
2240 cmdrc(hostp, hostname);
2241 if (autologin && user == NULL)
2242 user = (char *)get_default_username ();
2244 env_define((unsigned char *)"USER", (unsigned char *)user);
2245 env_export((unsigned char *)"USER");
2247 call(status, "status", "notmuch", 0);
2248 if (setjmp(peerdied) == 0)
2249 my_telnet((char *)user);
2251 ExitString("Connection closed by foreign host.\r\n",1);
2256 #define HELPINDENT ((int)sizeof ("connect"))
2259 openhelp[] = "connect to a site",
2260 closehelp[] = "close current connection",
2261 logouthelp[] = "forcibly logout remote user and close the connection",
2262 quithelp[] = "exit telnet",
2263 statushelp[] = "print status information",
2264 helphelp[] = "print help information",
2265 sendhelp[] = "transmit special characters ('send ?' for more)",
2266 sethelp[] = "set operating parameters ('set ?' for more)",
2267 unsethelp[] = "unset operating parameters ('unset ?' for more)",
2268 togglestring[] ="toggle operating parameters ('toggle ?' for more)",
2269 slchelp[] = "change state of special charaters ('slc ?' for more)",
2270 displayhelp[] = "display operating parameters",
2271 #if defined(AUTHENTICATION)
2272 authhelp[] = "turn on (off) authentication ('auth ?' for more)",
2274 #if defined(ENCRYPTION)
2275 encrypthelp[] = "turn on (off) encryption ('encrypt ?' for more)",
2277 zhelp[] = "suspend telnet",
2278 shellhelp[] = "invoke a subshell",
2279 envhelp[] = "change environment variables ('environ ?' for more)",
2280 modestring[] = "try to enter line or character mode ('mode ?' for more)";
2282 static int help(int argc, char **argv);
2284 static Command cmdtab[] = {
2285 { "close", closehelp, bye, 1 },
2286 { "logout", logouthelp, logout, 1 },
2287 { "display", displayhelp, display, 0 },
2288 { "mode", modestring, modecmd, 0 },
2289 { "open", openhelp, tn, 0 },
2290 { "quit", quithelp, quit, 0 },
2291 { "send", sendhelp, sendcmd, 0 },
2292 { "set", sethelp, setcmd, 0 },
2293 { "unset", unsethelp, unsetcmd, 0 },
2294 { "status", statushelp, status, 0 },
2295 { "toggle", togglestring, toggle, 0 },
2296 { "slc", slchelp, slccmd, 0 },
2297 #if defined(AUTHENTICATION)
2298 { "auth", authhelp, auth_cmd, 0 },
2300 #if defined(ENCRYPTION)
2301 { "encrypt", encrypthelp, encrypt_cmd, 0 },
2303 { "z", zhelp, telnetsuspend, 0 },
2304 { "!", shellhelp, shell, 0 },
2305 { "environ", envhelp, env_cmd, 0 },
2306 { "?", helphelp, help, 0 },
2310 static char crmodhelp[] = "deprecated command -- use 'toggle crmod' instead";
2311 static char escapehelp[] = "deprecated command -- use 'set escape' instead";
2313 static Command cmdtab2[] = {
2314 { "help", 0, help, 0 },
2315 { "escape", escapehelp, setescape, 0 },
2316 { "crmod", crmodhelp, togcrmod, 0 },
2322 * Call routine with argc, argv set from args (terminated by 0).
2326 call(intrtn_t routine, ...)
2332 va_start(ap, routine);
2333 while ((args[argno++] = va_arg(ap, char *)) != 0);
2335 return (*routine)(argno-1, args);
2344 if ((cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command))))
2346 return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
2350 command(int top, char *tbuf, int cnt)
2358 signal(SIGINT, SIG_DFL);
2359 signal(SIGQUIT, SIG_DFL);
2362 if (rlogin == _POSIX_VDISABLE)
2363 printf("%s> ", prompt);
2367 while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
2370 if (cp == line || *--cp != '\n' || cp == line)
2373 if (rlogin == _POSIX_VDISABLE)
2374 printf("%s\r\n", line);
2377 if (rlogin != _POSIX_VDISABLE)
2378 printf("%s> ", prompt);
2379 if (fgets(line, sizeof(line), stdin) == NULL) {
2380 if (feof(stdin) || ferror(stdin)) {
2390 if (margv[0] == 0) {
2393 c = getcmd(margv[0]);
2395 printf("?Ambiguous command\r\n");
2399 printf("?Invalid command\r\n");
2402 if (c->needconnect && !connected) {
2403 printf("?Need to be connected first.\r\n");
2406 if ((*c->handler)(margc, margv)) {
2412 longjmp(toplevel, 1);
2423 help(int argc, char **argv)
2428 printf("Commands may be abbreviated. Commands are:\r\n\r\n");
2429 for (c = cmdtab; c->name; c++)
2431 printf("%-*s\t%s\r\n", HELPINDENT, c->name,
2436 while (--argc > 0) {
2441 printf("?Ambiguous help command %s\r\n", arg);
2442 else if (c == (Command *)0)
2443 printf("?Invalid help command %s\r\n", arg);
2445 printf("%s\r\n", c->help);
2451 #if 0 /* XXX - broken */
2453 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2456 * Source route is handed in as
2457 * [!]@hop1@hop2...[@|:]dst
2458 * If the leading ! is present, it is a
2459 * strict source route, otherwise it is
2460 * assmed to be a loose source route.
2462 * We fill in the source route option as
2463 * hop1,hop2,hop3...dest
2464 * and return a pointer to hop1, which will
2465 * be the address to connect() to.
2468 * arg: pointer to route list to decipher
2470 * cpp: If *cpp is not equal to NULL, this is a
2471 * pointer to a pointer to a character array
2472 * that should be filled in with the option.
2474 * lenp: pointer to an integer that contains the
2475 * length of *cpp if *cpp != NULL.
2479 * Returns the address of the host to connect to. If the
2480 * return value is -1, there was a syntax error in the
2481 * option, either unknown characters, or too many hosts.
2482 * If the return value is 0, one of the hostnames in the
2483 * path is unknown, and *cpp is set to point to the bad
2486 * *cpp: If *cpp was equal to NULL, it will be filled
2487 * in with a pointer to our static area that has
2488 * the option filled in. This will be 32bit aligned.
2490 * *lenp: This will be filled in with how long the option
2491 * pointed to by *cpp is.
2495 sourceroute(char *arg, char **cpp, int *lenp)
2497 static char lsr[44];
2498 char *cp, *cp2, *lsrp, *lsrep;
2500 struct in_addr sin_addr;
2501 struct hostent *host = 0;
2505 * Verify the arguments, and make sure we have
2506 * at least 7 bytes for the option.
2508 if (cpp == NULL || lenp == NULL)
2509 return((unsigned long)-1);
2510 if (*cpp != NULL && *lenp < 7)
2511 return((unsigned long)-1);
2513 * Decide whether we have a buffer passed to us,
2514 * or if we need to use our own static buffer.
2518 lsrep = lsrp + *lenp;
2527 * Next, decide whether we have a loose source
2528 * route or a strict source route, and fill in
2529 * the begining of the option.
2533 *lsrp++ = IPOPT_SSRR;
2535 *lsrp++ = IPOPT_LSRR;
2538 return((unsigned long)-1);
2540 lsrp++; /* skip over length, we'll fill it in later */
2545 sin_addr.s_addr = 0;
2550 else for (cp2 = cp; (c = *cp2); cp2++) {
2555 } else if (c == '@') {
2557 } else if (c == ':') {
2566 if ((tmp = inet_addr(cp)) != -1) {
2567 sin_addr.s_addr = tmp;
2568 } else if ((host = roken_gethostbyname(cp))) {
2570 host->h_addr_list[0],
2576 memmove(lsrp, &sin_addr, 4);
2583 * Check to make sure there is space for next address
2585 if (lsrp + 4 > lsrep)
2586 return((unsigned long)-1);
2588 if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
2591 return((unsigned long)-1);
2593 *lsrp++ = IPOPT_NOP; /* 32 bit word align it */
2594 *lenp = lsrp - *cpp;
2595 return(sin_addr.s_addr);