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.56 1999/09/16 20:41:35 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 hostent *he = roken_gethostbyname(hbuf);
1586 strlcpy(hbuf, he->h_name, 256);
1589 asprintf (&cp, "%s%s", hbuf, cp2);
1591 ep->value = (unsigned char *)cp;
1594 * If USER is not defined, but LOGNAME is, then add
1595 * USER with the value from LOGNAME. By default, we
1596 * don't export the USER variable.
1598 if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
1599 env_define((unsigned char *)"USER", ep->value);
1600 env_unexport((unsigned char *)"USER");
1602 env_export((unsigned char *)"DISPLAY");
1603 env_export((unsigned char *)"PRINTER");
1604 env_export((unsigned char *)"XAUTHORITY");
1608 env_define(unsigned char *var, unsigned char *value)
1612 if ((ep = env_find(var))) {
1618 ep = (struct env_lst *)malloc(sizeof(struct env_lst));
1619 ep->next = envlisthead.next;
1620 envlisthead.next = ep;
1621 ep->prev = &envlisthead;
1623 ep->next->prev = ep;
1625 ep->welldefined = opt_welldefined((char *)var);
1627 ep->var = (unsigned char *)strdup((char *)var);
1628 ep->value = (unsigned char *)strdup((char *)value);
1633 env_undefine(unsigned char *var)
1637 if ((ep = env_find(var))) {
1638 ep->prev->next = ep->next;
1640 ep->next->prev = ep->prev;
1650 env_export(unsigned char *var)
1654 if ((ep = env_find(var)))
1659 env_unexport(unsigned char *var)
1663 if ((ep = env_find(var)))
1668 env_send(unsigned char *var)
1672 if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1674 && my_state_is_wont(TELOPT_OLD_ENVIRON)
1678 "Cannot send '%s': Telnet ENVIRON option not enabled\r\n",
1684 fprintf(stderr, "Cannot send '%s': variable not defined\r\n",
1688 env_opt_start_info();
1689 env_opt_add(ep->var);
1698 for (ep = envlisthead.next; ep; ep = ep->next) {
1699 printf("%c %-20s %s\r\n", ep->export ? '*' : ' ',
1700 ep->var, ep->value);
1705 env_default(int init, int welldefined)
1707 static struct env_lst *nep = NULL;
1714 while ((nep = nep->next)) {
1715 if (nep->export && (nep->welldefined == welldefined))
1723 env_getvalue(unsigned char *var)
1727 if ((ep = env_find(var)))
1733 #if defined(AUTHENTICATION)
1735 * The AUTHENTICATE command.
1748 struct authlist AuthList[] = {
1749 { "status", "Display current status of authentication information",
1751 { "disable", "Disable an authentication type ('auth disable ?' for more)",
1753 { "enable", "Enable an authentication type ('auth enable ?' for more)",
1755 { "help", 0, auth_help, 0 },
1756 { "?", "Print help information", auth_help, 0 },
1765 for (c = AuthList; c->name; c++) {
1768 printf("%-15s %s\r\n", c->name, c->help);
1777 auth_cmd(int argc, char **argv)
1783 "Need an argument to 'auth' command. 'auth ?' for help.\r\n");
1787 c = (struct authlist *)
1788 genget(argv[1], (char **) AuthList, sizeof(struct authlist));
1790 fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\r\n",
1795 fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\r\n",
1799 if (c->narg + 2 != argc) {
1801 "Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\r\n",
1802 c->narg < argc + 2 ? "only " : "",
1803 c->narg, c->narg == 1 ? "" : "s", c->name);
1806 return((*c->handler)(argv[2], argv[3]));
1811 #if defined(ENCRYPTION)
1813 * The ENCRYPT command.
1816 struct encryptlist {
1828 struct encryptlist EncryptList[] = {
1829 { "enable", "Enable encryption. ('encrypt enable ?' for more)",
1830 EncryptEnable, 1, 1, 2 },
1831 { "disable", "Disable encryption. ('encrypt enable ?' for more)",
1832 EncryptDisable, 0, 1, 2 },
1833 { "type", "Set encryptiong type. ('encrypt type ?' for more)",
1834 EncryptType, 0, 1, 1 },
1835 { "start", "Start encryption. ('encrypt start ?' for more)",
1836 EncryptStart, 1, 0, 1 },
1837 { "stop", "Stop encryption. ('encrypt stop ?' for more)",
1838 EncryptStop, 1, 0, 1 },
1839 { "input", "Start encrypting the input stream",
1840 EncryptStartInput, 1, 0, 0 },
1841 { "-input", "Stop encrypting the input stream",
1842 EncryptStopInput, 1, 0, 0 },
1843 { "output", "Start encrypting the output stream",
1844 EncryptStartOutput, 1, 0, 0 },
1845 { "-output", "Stop encrypting the output stream",
1846 EncryptStopOutput, 1, 0, 0 },
1848 { "status", "Display current status of authentication information",
1849 EncryptStatus, 0, 0, 0 },
1850 { "help", 0, EncryptHelp, 0, 0, 0 },
1851 { "?", "Print help information", EncryptHelp, 0, 0, 0 },
1858 struct encryptlist *c;
1860 for (c = EncryptList; c->name; c++) {
1863 printf("%-15s %s\r\n", c->name, c->help);
1872 encrypt_cmd(int argc, char **argv)
1874 struct encryptlist *c;
1876 c = (struct encryptlist *)
1877 genget(argv[1], (char **) EncryptList, sizeof(struct encryptlist));
1879 fprintf(stderr, "'%s': unknown argument ('encrypt ?' for help).\r\n",
1884 fprintf(stderr, "'%s': ambiguous argument ('encrypt ?' for help).\r\n",
1889 if (argc < c->minarg || argc > c->maxarg) {
1890 if (c->minarg == c->maxarg) {
1891 fprintf(stderr, "Need %s%d argument%s ",
1892 c->minarg < argc ? "only " : "", c->minarg,
1893 c->minarg == 1 ? "" : "s");
1895 fprintf(stderr, "Need %s%d-%d arguments ",
1896 c->maxarg < argc ? "only " : "", c->minarg, c->maxarg);
1898 fprintf(stderr, "to 'encrypt %s' command. 'encrypt ?' for help.\r\n",
1902 if (c->needconnect && !connected) {
1903 if (!(argc && (isprefix(argv[2], "help") || isprefix(argv[2], "?")))) {
1904 printf("?Need to be connected first.\r\n");
1908 return ((*c->handler)(argc > 0 ? argv[2] : 0,
1909 argc > 1 ? argv[3] : 0,
1910 argc > 2 ? argv[4] : 0));
1916 * Print status about the connection.
1920 status(int argc, char **argv)
1923 printf("Connected to %s.\r\n", hostname);
1924 if ((argc < 2) || strcmp(argv[1], "notmuch")) {
1925 int mode = getconnmode();
1927 if (my_want_state_is_will(TELOPT_LINEMODE)) {
1928 printf("Operating with LINEMODE option\r\n");
1929 printf("%s line editing\r\n", (mode&MODE_EDIT) ? "Local" : "No");
1930 printf("%s catching of signals\r\n",
1931 (mode&MODE_TRAPSIG) ? "Local" : "No");
1933 #ifdef KLUDGELINEMODE
1934 } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
1935 printf("Operating in obsolete linemode\r\n");
1938 printf("Operating in single character mode\r\n");
1940 printf("Catching signals locally\r\n");
1942 printf("%s character echo\r\n", (mode&MODE_ECHO) ? "Local" : "Remote");
1943 if (my_want_state_is_will(TELOPT_LFLOW))
1944 printf("%s flow control\r\n", (mode&MODE_FLOW) ? "Local" : "No");
1945 #if defined(ENCRYPTION)
1950 printf("No connection.\r\n");
1952 printf("Escape character is '%s'.\r\n", control(escape));
1959 * Function that gets called when SIGINFO is received.
1962 ayt_status(int ignore)
1964 call(status, "status", "notmuch", 0);
1968 static Command *getcmd(char *name);
1971 cmdrc(char *m1, char *m2)
1973 static char rcname[128];
1977 int l1 = strlen(m1);
1978 int l2 = strlen(m2);
1984 strlcpy(m1save, m1, sizeof(m1save));
1987 if (rcname[0] == 0) {
1988 char *home = getenv("HOME");
1990 snprintf (rcname, sizeof(rcname), "%s/.telnetrc",
1994 if ((rcfile = fopen(rcname, "r")) == 0) {
1999 if (fgets(line, sizeof(line), rcfile) == NULL)
2006 if (!isspace(line[0]))
2009 if (gotmachine == 0) {
2010 if (isspace(line[0]))
2012 if (strncasecmp(line, m1, l1) == 0)
2013 strncpy(line, &line[l1], sizeof(line) - l1);
2014 else if (strncasecmp(line, m2, l2) == 0)
2015 strncpy(line, &line[l2], sizeof(line) - l2);
2016 else if (strncasecmp(line, "DEFAULT", 7) == 0)
2017 strncpy(line, &line[7], sizeof(line) - 7);
2020 if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
2027 c = getcmd(margv[0]);
2029 printf("?Ambiguous command: %s\r\n", margv[0]);
2033 printf("?Invalid command: %s\r\n", margv[0]);
2037 * This should never happen...
2039 if (c->needconnect && !connected) {
2040 printf("?Need to be connected first for %s.\r\n", margv[0]);
2043 (*c->handler)(margc, margv);
2049 tn(int argc, char **argv)
2051 struct hostent *host = 0;
2053 struct sockaddr_in6 sin6;
2055 struct sockaddr_in sin;
2056 struct sockaddr *sa = NULL;
2058 struct servent *sp = 0;
2060 extern char *inet_ntoa();
2061 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2065 char *cmd, *hostp = 0, *portp = 0;
2067 int family, port = 0;
2070 /* clear the socket address prior to use */
2073 printf("?Already connected to %s\r\n", hostname);
2078 strlcpy(line, "open ", sizeof(line));
2080 fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
2088 if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
2090 if (strcmp(*argv, "-l") == 0) {
2094 user = strdup(*argv++);
2098 if (strcmp(*argv, "-a") == 0) {
2114 printf("usage: %s [-l user] [-a] host-name [port]\r\n", cmd);
2121 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2122 if (hostp[0] == '@' || hostp[0] == '!') {
2123 if ((hostname = strrchr(hostp, ':')) == NULL)
2124 hostname = strrchr(hostp, '@');
2127 temp = sourceroute(hostp, &srp, &srlen);
2129 fprintf (stderr, "%s: %s\r\n", srp ? srp : "", hstrerror(h_errno));
2132 } else if (temp == -1) {
2133 printf("Bad source route option: %s\r\n", hostp);
2141 memset (&sin, 0, sizeof(sin));
2143 memset (&sin6, 0, sizeof(sin6));
2145 if(inet_pton(AF_INET6, hostp, &sin6.sin6_addr)) {
2146 sin6.sin6_family = family = AF_INET6;
2147 sa = (struct sockaddr *)&sin6;
2148 sa_size = sizeof(sin6);
2149 strlcpy(_hostname, hostp, sizeof(_hostname));
2150 hostname =_hostname;
2153 if(inet_aton(hostp, &sin.sin_addr)){
2154 sin.sin_family = family = AF_INET;
2155 sa = (struct sockaddr *)&sin;
2156 sa_size = sizeof(sin);
2157 strlcpy(_hostname, hostp, sizeof(_hostname));
2158 hostname = _hostname;
2160 #ifdef HAVE_GETHOSTBYNAME2
2162 host = gethostbyname2(hostp, AF_INET6);
2165 host = gethostbyname2(hostp, AF_INET);
2167 host = roken_gethostbyname(hostp);
2170 strlcpy(_hostname, host->h_name, sizeof(_hostname));
2171 family = host->h_addrtype;
2172 addr_list = host->h_addr_list;
2176 memset(&sin, 0, sizeof(sin));
2177 sa_size = sizeof(sin);
2178 sa = (struct sockaddr *)&sin;
2179 sin.sin_family = family;
2180 sin.sin_addr = *((struct in_addr *)(*addr_list));
2184 memset(&sin6, 0, sizeof(sin6));
2185 sa_size = sizeof(sin6);
2186 sa = (struct sockaddr *)&sin6;
2187 sin6.sin6_family = family;
2188 sin6.sin6_addr = *((struct in6_addr *)(*addr_list));
2192 fprintf(stderr, "Bad address family: %d\n", family);
2196 _hostname[sizeof(_hostname)-1] = '\0';
2197 hostname = _hostname;
2199 fprintf (stderr, "%s: %s\r\n", hostp ? hostp : "",
2200 hstrerror(h_errno));
2205 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2209 if (*portp == '-') {
2216 sp = roken_getservbyname(portp, "tcp");
2220 printf("%s: bad port number\r\n", portp);
2229 sp = roken_getservbyname("telnet", "tcp");
2231 fprintf(stderr, "telnet: tcp/telnet: unknown service\r\n");
2242 sin.sin_port = port;
2243 printf("Trying %s...\r\n", inet_ntoa(sin.sin_addr));
2247 #ifndef INET6_ADDRSTRLEN
2248 #define INET6_ADDRSTRLEN 46
2251 char buf[INET6_ADDRSTRLEN];
2253 sin6.sin6_port = port;
2254 #ifdef HAVE_INET_NTOP
2255 printf("Trying %s...\r\n", inet_ntop(AF_INET6,
2268 net = socket(family, SOCK_STREAM, 0);
2271 perror("telnet: socket");
2274 #if defined(IP_OPTIONS) && defined(IPPROTO_IP) && defined(HAVE_SETSOCKOPT)
2275 if (srp && setsockopt(net, IPPROTO_IP, IP_OPTIONS, (void *)srp,
2277 perror("setsockopt (IP_OPTIONS)");
2279 #if defined(IPPROTO_IP) && defined(IP_TOS)
2281 # if defined(HAVE_GETTOSBYNAME)
2283 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
2287 tos = 020; /* Low Delay bit */
2289 && (setsockopt(net, IPPROTO_IP, IP_TOS,
2290 (void *)&tos, sizeof(int)) < 0)
2291 && (errno != ENOPROTOOPT))
2292 perror("telnet: setsockopt (IP_TOS) (ignored)");
2294 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2296 if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
2297 perror("setsockopt (SO_DEBUG)");
2300 if (connect(net, sa, sa_size) < 0) {
2301 if (host && addr_list[1]) {
2306 fprintf(stderr, "telnet: connect to address %s: ",
2307 inet_ntoa(sin.sin_addr));
2308 sin.sin_addr = *((struct in_addr *)(*++addr_list));
2312 char buf[INET6_ADDRSTRLEN];
2314 fprintf(stderr, "telnet: connect to address %s: ",
2315 inet_ntop(AF_INET6, &sin6.sin6_addr, buf,
2317 sin6.sin6_addr = *((struct in6_addr *)(*++addr_list));
2330 perror("telnet: Unable to connect to remote host");
2334 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
2335 auth_encrypt_connect(connected);
2337 } while (connected == 0);
2338 cmdrc(hostp, hostname);
2339 if (autologin && user == NULL)
2340 user = (char *)get_default_username ();
2342 env_define((unsigned char *)"USER", (unsigned char *)user);
2343 env_export((unsigned char *)"USER");
2345 call(status, "status", "notmuch", 0);
2346 if (setjmp(peerdied) == 0)
2347 my_telnet((char *)user);
2349 ExitString("Connection closed by foreign host.\r\n",1);
2354 #define HELPINDENT ((int)sizeof ("connect"))
2357 openhelp[] = "connect to a site",
2358 closehelp[] = "close current connection",
2359 logouthelp[] = "forcibly logout remote user and close the connection",
2360 quithelp[] = "exit telnet",
2361 statushelp[] = "print status information",
2362 helphelp[] = "print help information",
2363 sendhelp[] = "transmit special characters ('send ?' for more)",
2364 sethelp[] = "set operating parameters ('set ?' for more)",
2365 unsethelp[] = "unset operating parameters ('unset ?' for more)",
2366 togglestring[] ="toggle operating parameters ('toggle ?' for more)",
2367 slchelp[] = "change state of special charaters ('slc ?' for more)",
2368 displayhelp[] = "display operating parameters",
2369 #if defined(AUTHENTICATION)
2370 authhelp[] = "turn on (off) authentication ('auth ?' for more)",
2372 #if defined(ENCRYPTION)
2373 encrypthelp[] = "turn on (off) encryption ('encrypt ?' for more)",
2375 zhelp[] = "suspend telnet",
2376 shellhelp[] = "invoke a subshell",
2377 envhelp[] = "change environment variables ('environ ?' for more)",
2378 modestring[] = "try to enter line or character mode ('mode ?' for more)";
2380 static int help(int argc, char **argv);
2382 static Command cmdtab[] = {
2383 { "close", closehelp, bye, 1 },
2384 { "logout", logouthelp, logout, 1 },
2385 { "display", displayhelp, display, 0 },
2386 { "mode", modestring, modecmd, 0 },
2387 { "open", openhelp, tn, 0 },
2388 { "quit", quithelp, quit, 0 },
2389 { "send", sendhelp, sendcmd, 0 },
2390 { "set", sethelp, setcmd, 0 },
2391 { "unset", unsethelp, unsetcmd, 0 },
2392 { "status", statushelp, status, 0 },
2393 { "toggle", togglestring, toggle, 0 },
2394 { "slc", slchelp, slccmd, 0 },
2395 #if defined(AUTHENTICATION)
2396 { "auth", authhelp, auth_cmd, 0 },
2398 #if defined(ENCRYPTION)
2399 { "encrypt", encrypthelp, encrypt_cmd, 0 },
2401 { "z", zhelp, telnetsuspend, 0 },
2402 { "!", shellhelp, shell, 0 },
2403 { "environ", envhelp, env_cmd, 0 },
2404 { "?", helphelp, help, 0 },
2408 static char crmodhelp[] = "deprecated command -- use 'toggle crmod' instead";
2409 static char escapehelp[] = "deprecated command -- use 'set escape' instead";
2411 static Command cmdtab2[] = {
2412 { "help", 0, help, 0 },
2413 { "escape", escapehelp, setescape, 0 },
2414 { "crmod", crmodhelp, togcrmod, 0 },
2420 * Call routine with argc, argv set from args (terminated by 0).
2424 call(intrtn_t routine, ...)
2430 va_start(ap, routine);
2431 while ((args[argno++] = va_arg(ap, char *)) != 0);
2433 return (*routine)(argno-1, args);
2442 if ((cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command))))
2444 return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
2448 command(int top, char *tbuf, int cnt)
2456 signal(SIGINT, SIG_DFL);
2457 signal(SIGQUIT, SIG_DFL);
2460 if (rlogin == _POSIX_VDISABLE)
2461 printf("%s> ", prompt);
2465 while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
2468 if (cp == line || *--cp != '\n' || cp == line)
2471 if (rlogin == _POSIX_VDISABLE)
2472 printf("%s\r\n", line);
2475 if (rlogin != _POSIX_VDISABLE)
2476 printf("%s> ", prompt);
2477 if (fgets(line, sizeof(line), stdin) == NULL) {
2478 if (feof(stdin) || ferror(stdin)) {
2488 if (margv[0] == 0) {
2491 c = getcmd(margv[0]);
2493 printf("?Ambiguous command\r\n");
2497 printf("?Invalid command\r\n");
2500 if (c->needconnect && !connected) {
2501 printf("?Need to be connected first.\r\n");
2504 if ((*c->handler)(margc, margv)) {
2510 longjmp(toplevel, 1);
2521 help(int argc, char **argv)
2526 printf("Commands may be abbreviated. Commands are:\r\n\r\n");
2527 for (c = cmdtab; c->name; c++)
2529 printf("%-*s\t%s\r\n", HELPINDENT, c->name,
2534 while (--argc > 0) {
2539 printf("?Ambiguous help command %s\r\n", arg);
2540 else if (c == (Command *)0)
2541 printf("?Invalid help command %s\r\n", arg);
2543 printf("%s\r\n", c->help);
2549 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2552 * Source route is handed in as
2553 * [!]@hop1@hop2...[@|:]dst
2554 * If the leading ! is present, it is a
2555 * strict source route, otherwise it is
2556 * assmed to be a loose source route.
2558 * We fill in the source route option as
2559 * hop1,hop2,hop3...dest
2560 * and return a pointer to hop1, which will
2561 * be the address to connect() to.
2564 * arg: pointer to route list to decipher
2566 * cpp: If *cpp is not equal to NULL, this is a
2567 * pointer to a pointer to a character array
2568 * that should be filled in with the option.
2570 * lenp: pointer to an integer that contains the
2571 * length of *cpp if *cpp != NULL.
2575 * Returns the address of the host to connect to. If the
2576 * return value is -1, there was a syntax error in the
2577 * option, either unknown characters, or too many hosts.
2578 * If the return value is 0, one of the hostnames in the
2579 * path is unknown, and *cpp is set to point to the bad
2582 * *cpp: If *cpp was equal to NULL, it will be filled
2583 * in with a pointer to our static area that has
2584 * the option filled in. This will be 32bit aligned.
2586 * *lenp: This will be filled in with how long the option
2587 * pointed to by *cpp is.
2591 sourceroute(char *arg, char **cpp, int *lenp)
2593 static char lsr[44];
2594 char *cp, *cp2, *lsrp, *lsrep;
2596 struct in_addr sin_addr;
2597 struct hostent *host = 0;
2601 * Verify the arguments, and make sure we have
2602 * at least 7 bytes for the option.
2604 if (cpp == NULL || lenp == NULL)
2605 return((unsigned long)-1);
2606 if (*cpp != NULL && *lenp < 7)
2607 return((unsigned long)-1);
2609 * Decide whether we have a buffer passed to us,
2610 * or if we need to use our own static buffer.
2614 lsrep = lsrp + *lenp;
2623 * Next, decide whether we have a loose source
2624 * route or a strict source route, and fill in
2625 * the begining of the option.
2629 *lsrp++ = IPOPT_SSRR;
2631 *lsrp++ = IPOPT_LSRR;
2634 return((unsigned long)-1);
2636 lsrp++; /* skip over length, we'll fill it in later */
2641 sin_addr.s_addr = 0;
2646 else for (cp2 = cp; (c = *cp2); cp2++) {
2651 } else if (c == '@') {
2653 } else if (c == ':') {
2662 if ((tmp = inet_addr(cp)) != -1) {
2663 sin_addr.s_addr = tmp;
2664 } else if ((host = roken_gethostbyname(cp))) {
2666 host->h_addr_list[0],
2672 memmove(lsrp, &sin_addr, 4);
2679 * Check to make sure there is space for next address
2681 if (lsrp + 4 > lsrep)
2682 return((unsigned long)-1);
2684 if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
2687 return((unsigned long)-1);
2689 *lsrp++ = IPOPT_NOP; /* 32 bit word align it */
2690 *lenp = lsrp - *cpp;
2691 return(sin_addr.s_addr);