Upstream update.
[shishi.git] / extra / inetutils / telnet / commands.c
blob4062f3817f2eae9989f209285edad9375082ba1c
1 /*
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
7 * are met:
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
30 #ifndef lint
31 static char sccsid[] = "@(#)commands.c 8.4 (Berkeley) 5/30/95";
32 #endif /* not lint */
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
38 #ifdef HAVE_UNISTD_H
39 # include <unistd.h>
40 #endif
41 #ifdef HAVE_SYS_PARAM_H
42 # include <sys/param.h>
43 #endif
44 #ifdef HAVE_SYS_TYPES_H
45 # include <sys/types.h>
46 #endif
48 #include <sys/file.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
53 #ifdef HAVE_FCNTL_H
54 # include <fcntl.h>
55 #endif /* CRAY */
57 #include <signal.h>
58 #include <netdb.h>
59 #include <ctype.h>
60 #include <pwd.h>
61 #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
62 #include <stdarg.h>
63 #else
64 #include <varargs.h>
65 #endif
66 #include <errno.h>
68 #if defined(STDC_HEADERS) || defined(HAVE_STDLIB_H)
69 #include <stdlib.h>
70 #endif
71 #ifdef HAVE_MALLOC_H
72 #include <malloc.h>
73 #endif
75 #include <arpa/telnet.h>
77 #include "general.h"
79 #include "ring.h"
81 #include "externs.h"
82 #include "defines.h"
83 #include "types.h"
85 #if !defined(CRAY) && !defined(sysV88)
86 #ifdef HAVE_NETINET_IN_SYSTM_H
87 #include <netinet/in_systm.h>
88 #endif
89 # if (defined(vax) || defined(tahoe) || defined(hp300)) && !defined(ultrix)
90 # include <machine/endian.h>
91 # endif /* vax */
92 #endif /* !defined(CRAY) && !defined(sysV88) */
94 #ifdef HAVE_NETINET_IP_H
95 #include <netinet/ip.h>
96 #endif
98 #if defined(IPPROTO_IP) && defined(IP_TOS)
99 int tos = -1;
100 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
102 char *hostname;
103 static char *_hostname = 0;
105 extern char *getenv __P((const char *));
107 extern int isprefix __P((register char *s1, register char *s2));
108 extern char **genget __P((char *, char **, int ));
109 extern int Ambiguous __P(());
111 typedef int (*intrtn_t) __P((int, char *[]));
112 static int call __P((intrtn_t, ...));
114 typedef struct {
115 const char *name; /* command name */
116 const char *help; /* help string (NULL for no help) */
117 int (*handler) ();/* routine which executes command */
118 int needconnect; /* Do we need to be connected to execute? */
119 } Command;
121 static char line[256];
122 static char saveline[256];
123 static int margc;
124 static char *margv[20];
126 static void
127 makeargv(void)
129 register char *cp, *cp2, c;
130 register char **argp = margv;
132 margc = 0;
133 cp = line;
134 if (*cp == '!') { /* Special case shell escape */
135 strcpy(saveline, line); /* save for shell command */
136 *argp++ = "!"; /* No room in string to get this */
137 margc++;
138 cp++;
140 while (c = *cp) {
141 register int inquote = 0;
142 while (isspace(c))
143 c = *++cp;
144 if (c == '\0')
145 break;
146 *argp++ = cp;
147 margc += 1;
148 for (cp2 = cp; c != '\0'; c = *++cp) {
149 if (inquote) {
150 if (c == inquote) {
151 inquote = 0;
152 continue;
154 } else {
155 if (c == '\\') {
156 if ((c = *++cp) == '\0')
157 break;
158 } else if (c == '"') {
159 inquote = '"';
160 continue;
161 } else if (c == '\'') {
162 inquote = '\'';
163 continue;
164 } else if (isspace(c))
165 break;
167 *cp2++ = c;
169 *cp2 = '\0';
170 if (c == '\0')
171 break;
172 cp++;
174 *argp++ = 0;
178 * Make a character string into a number.
180 * Todo: 1. Could take random integers (12, 0x12, 012, 0b1).
183 static int
184 special(register char *s)
186 register char c;
187 char b;
189 switch (*s) {
190 case '^':
191 b = *++s;
192 if (b == '?') {
193 c = b | 0x40; /* DEL */
194 } else {
195 c = b & 0x1f;
197 break;
198 default:
199 c = *s;
200 break;
202 return c;
206 * Construct a control character sequence
207 * for a special character.
209 static const char *
210 control(register cc_t c)
212 static char buf[5];
214 * The only way I could get the Sun 3.5 compiler
215 * to shut up about
216 * if ((unsigned int)c >= 0x80)
217 * was to assign "c" to an unsigned int variable...
218 * Arggg....
220 register unsigned int uic = (unsigned int)c;
222 if (uic == 0x7f)
223 return ("^?");
224 if (c == (cc_t)_POSIX_VDISABLE) {
225 return "off";
227 if (uic >= 0x80) {
228 buf[0] = '\\';
229 buf[1] = ((c>>6)&07) + '0';
230 buf[2] = ((c>>3)&07) + '0';
231 buf[3] = (c&07) + '0';
232 buf[4] = 0;
233 } else if (uic >= 0x20) {
234 buf[0] = c;
235 buf[1] = 0;
236 } else {
237 buf[0] = '^';
238 buf[1] = '@'+c;
239 buf[2] = 0;
241 return (buf);
247 * The following are data structures and routines for
248 * the "send" command.
252 struct sendlist {
253 const char *name; /* How user refers to it (case independent) */
254 const char *help; /* Help information (0 ==> no help) */
255 int needconnect; /* Need to be connected */
256 int narg; /* Number of arguments */
257 int (*handler)(); /* Routine to perform (for special ops) */
258 int nbyte; /* Number of bytes to send this command */
259 int what; /* Character to be sent (<0 ==> special) */
263 static int
264 send_esc P((void)),
265 send_help P((void)),
266 send_docmd P((char *)),
267 send_dontcmd P((char *)),
268 send_willcmd P((char *)),
269 send_wontcmd P((char *));
271 static struct sendlist Sendlist[] = {
272 { "ao", "Send Telnet Abort output", 1, 0, 0, 2, AO },
273 { "ayt", "Send Telnet 'Are You There'", 1, 0, 0, 2, AYT },
274 { "brk", "Send Telnet Break", 1, 0, 0, 2, BREAK },
275 { "break", 0, 1, 0, 0, 2, BREAK },
276 { "ec", "Send Telnet Erase Character", 1, 0, 0, 2, EC },
277 { "el", "Send Telnet Erase Line", 1, 0, 0, 2, EL },
278 { "escape", "Send current escape character", 1, 0, send_esc, 1, 0 },
279 { "ga", "Send Telnet 'Go Ahead' sequence", 1, 0, 0, 2, GA },
280 { "ip", "Send Telnet Interrupt Process", 1, 0, 0, 2, IP },
281 { "intp", 0, 1, 0, 0, 2, IP },
282 { "interrupt", 0, 1, 0, 0, 2, IP },
283 { "intr", 0, 1, 0, 0, 2, IP },
284 { "nop", "Send Telnet 'No operation'", 1, 0, 0, 2, NOP },
285 { "eor", "Send Telnet 'End of Record'", 1, 0, 0, 2, EOR },
286 { "abort", "Send Telnet 'Abort Process'", 1, 0, 0, 2, ABORT },
287 { "susp", "Send Telnet 'Suspend Process'", 1, 0, 0, 2, SUSP },
288 { "eof", "Send Telnet End of File Character", 1, 0, 0, 2, xEOF },
289 { "synch", "Perform Telnet 'Synch operation'", 1, 0, dosynch, 2, 0 },
290 { "getstatus", "Send request for STATUS", 1, 0, get_status, 6, 0 },
291 { "?", "Display send options", 0, 0, send_help, 0, 0 },
292 { "help", 0, 0, 0, send_help, 0, 0 },
293 { "do", 0, 0, 1, send_docmd, 3, 0 },
294 { "dont", 0, 0, 1, send_dontcmd, 3, 0 },
295 { "will", 0, 0, 1, send_willcmd, 3, 0 },
296 { "wont", 0, 0, 1, send_wontcmd, 3, 0 },
297 { NULL, 0, 0, 0, NULL, 0, 0 }
300 #define GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
301 sizeof(struct sendlist)))
303 static int
304 sendcmd(int argc, char **argv)
306 int count; /* how many bytes we are going to need to send */
307 int i;
308 int question = 0; /* was at least one argument a question */
309 struct sendlist *s; /* pointer to current command */
310 int success = 0;
311 int needconnect = 0;
313 if (argc < 2) {
314 printf("need at least one argument for 'send' command\n");
315 printf("'send ?' for help\n");
316 return 0;
319 * First, validate all the send arguments.
320 * In addition, we see how much space we are going to need, and
321 * whether or not we will be doing a "SYNCH" operation (which
322 * flushes the network queue).
324 count = 0;
325 for (i = 1; i < argc; i++) {
326 s = GETSEND(argv[i]);
327 if (s == 0) {
328 printf("Unknown send argument '%s'\n'send ?' for help.\n",
329 argv[i]);
330 return 0;
331 } else if (Ambiguous(s)) {
332 printf("Ambiguous send argument '%s'\n'send ?' for help.\n",
333 argv[i]);
334 return 0;
336 if (i + s->narg >= argc) {
337 fprintf(stderr,
338 "Need %d argument%s to 'send %s' command. 'send %s ?' for help.\n",
339 s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
340 return 0;
342 count += s->nbyte;
343 if (s->handler == send_help) {
344 send_help();
345 return 0;
348 i += s->narg;
349 needconnect += s->needconnect;
351 if (!connected && needconnect) {
352 printf("?Need to be connected first.\n");
353 printf("'send ?' for help\n");
354 return 0;
356 /* Now, do we have enough room? */
357 if (NETROOM() < count) {
358 printf("There is not enough room in the buffer TO the network\n");
359 printf("to process your request. Nothing will be done.\n");
360 printf("('send synch' will throw away most data in the network\n");
361 printf("buffer, if this might help.)\n");
362 return 0;
364 /* OK, they are all OK, now go through again and actually send */
365 count = 0;
366 for (i = 1; i < argc; i++) {
367 if ((s = GETSEND(argv[i])) == 0) {
368 fprintf(stderr, "Telnet 'send' error - argument disappeared!\n");
369 (void) quit();
370 /*NOTREACHED*/
372 if (s->handler) {
373 count++;
374 success += (*s->handler)((s->narg > 0) ? argv[i+1] : 0,
375 (s->narg > 1) ? argv[i+2] : 0);
376 i += s->narg;
377 } else {
378 NET2ADD(IAC, s->what);
379 printoption("SENT", IAC, s->what);
382 return (count == success);
385 static int
386 send_esc()
388 NETADD(escape);
389 return 1;
392 static int
393 send_docmd(char *name)
395 return(send_tncmd(send_do, "do", name));
398 static int
399 send_dontcmd(char *name)
401 return(send_tncmd(send_dont, "dont", name));
404 static int
405 send_willcmd(char *name)
407 return(send_tncmd(send_will, "will", name));
410 static int
411 send_wontcmd(char *name)
413 return(send_tncmd(send_wont, "wont", name));
417 send_tncmd(void (*func)(), char *cmd, char *name)
419 char **cpp;
420 extern char *telopts[];
421 register int val = 0;
423 if (isprefix(name, "help") || isprefix(name, "?")) {
424 register int col, len;
426 printf("Usage: send %s <value|option>\n", cmd);
427 printf("\"value\" must be from 0 to 255\n");
428 printf("Valid options are:\n\t");
430 col = 8;
431 for (cpp = telopts; *cpp; cpp++) {
432 len = strlen(*cpp) + 3;
433 if (col + len > 65) {
434 printf("\n\t");
435 col = 8;
437 printf(" \"%s\"", *cpp);
438 col += len;
440 printf("\n");
441 return 0;
443 cpp = (char **)genget(name, telopts, sizeof(char *));
444 if (Ambiguous(cpp)) {
445 fprintf(stderr,"'%s': ambiguous argument ('send %s ?' for help).\n",
446 name, cmd);
447 return 0;
449 if (cpp) {
450 val = cpp - telopts;
451 } else {
452 register char *cp = name;
454 while (*cp >= '0' && *cp <= '9') {
455 val *= 10;
456 val += *cp - '0';
457 cp++;
459 if (*cp != 0) {
460 fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\n",
461 name, cmd);
462 return 0;
463 } else if (val < 0 || val > 255) {
464 fprintf(stderr, "'%s': bad value ('send %s ?' for help).\n",
465 name, cmd);
466 return 0;
469 if (!connected) {
470 printf("?Need to be connected first.\n");
471 return 0;
473 (*func)(val, 1);
474 return 1;
477 static int
478 send_help()
480 struct sendlist *s; /* pointer to current command */
481 for (s = Sendlist; s->name; s++) {
482 if (s->help)
483 printf("%-15s %s\n", s->name, s->help);
485 return(0);
489 * The following are the routines and data structures referred
490 * to by the arguments to the "toggle" command.
493 static int
494 lclchars()
496 donelclchars = 1;
497 return 1;
500 static int
501 togdebug()
503 #ifndef NOT43
504 if (net > 0 &&
505 (SetSockOpt(net, SOL_SOCKET, SO_DEBUG, debug)) < 0) {
506 perror("setsockopt (SO_DEBUG)");
508 #else /* NOT43 */
509 if (debug) {
510 if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 0, 0) < 0)
511 perror("setsockopt (SO_DEBUG)");
512 } else
513 printf("Cannot turn off socket debugging\n");
514 #endif /* NOT43 */
515 return 1;
519 static int
520 togcrlf()
522 if (crlf) {
523 printf("Will send carriage returns as telnet <CR><LF>.\n");
524 } else {
525 printf("Will send carriage returns as telnet <CR><NUL>.\n");
527 return 1;
530 int binmode;
532 static int
533 togbinary(int val)
535 donebinarytoggle = 1;
537 if (val >= 0) {
538 binmode = val;
539 } else {
540 if (my_want_state_is_will(TELOPT_BINARY) &&
541 my_want_state_is_do(TELOPT_BINARY)) {
542 binmode = 1;
543 } else if (my_want_state_is_wont(TELOPT_BINARY) &&
544 my_want_state_is_dont(TELOPT_BINARY)) {
545 binmode = 0;
547 val = binmode ? 0 : 1;
550 if (val == 1) {
551 if (my_want_state_is_will(TELOPT_BINARY) &&
552 my_want_state_is_do(TELOPT_BINARY)) {
553 printf("Already operating in binary mode with remote host.\n");
554 } else {
555 printf("Negotiating binary mode with remote host.\n");
556 tel_enter_binary(3);
558 } else {
559 if (my_want_state_is_wont(TELOPT_BINARY) &&
560 my_want_state_is_dont(TELOPT_BINARY)) {
561 printf("Already in network ascii mode with remote host.\n");
562 } else {
563 printf("Negotiating network ascii mode with remote host.\n");
564 tel_leave_binary(3);
567 return 1;
570 static int
571 togrbinary(int val)
573 donebinarytoggle = 1;
575 if (val == -1)
576 val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
578 if (val == 1) {
579 if (my_want_state_is_do(TELOPT_BINARY)) {
580 printf("Already receiving in binary mode.\n");
581 } else {
582 printf("Negotiating binary mode on input.\n");
583 tel_enter_binary(1);
585 } else {
586 if (my_want_state_is_dont(TELOPT_BINARY)) {
587 printf("Already receiving in network ascii mode.\n");
588 } else {
589 printf("Negotiating network ascii mode on input.\n");
590 tel_leave_binary(1);
593 return 1;
596 static int
597 togxbinary(int val)
599 donebinarytoggle = 1;
601 if (val == -1)
602 val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
604 if (val == 1) {
605 if (my_want_state_is_will(TELOPT_BINARY)) {
606 printf("Already transmitting in binary mode.\n");
607 } else {
608 printf("Negotiating binary mode on output.\n");
609 tel_enter_binary(2);
611 } else {
612 if (my_want_state_is_wont(TELOPT_BINARY)) {
613 printf("Already transmitting in network ascii mode.\n");
614 } else {
615 printf("Negotiating network ascii mode on output.\n");
616 tel_leave_binary(2);
619 return 1;
623 static int togglehelp P((void));
624 #if defined(AUTHENTICATION)
625 extern int auth_togdebug P((int));
626 #endif
627 #ifdef ENCRYPTION
628 extern int EncryptAutoEnc P((int));
629 extern int EncryptAutoDec P((int));
630 extern int EncryptDebug P((int));
631 extern int EncryptVerbose P((int));
632 #endif /* ENCRYPTION */
634 struct togglelist {
635 const char *name; /* name of toggle */
636 const char *help; /* help message */
637 int (*handler)(); /* routine to do actual setting */
638 int *variable;
639 const char *actionexplanation;
642 static struct togglelist Togglelist[] = {
643 { "autoflush",
644 "flushing of output when sending interrupt characters",
646 &autoflush,
647 "flush output when sending interrupt characters" },
648 { "autosynch",
649 "automatic sending of interrupt characters in urgent mode",
651 &autosynch,
652 "send interrupt characters in urgent mode" },
653 #if defined(AUTHENTICATION)
654 { "autologin",
655 "automatic sending of login and/or authentication info",
657 &autologin,
658 "send login name and/or authentication information" },
659 { "authdebug",
660 "Toggle authentication debugging",
661 auth_togdebug,
663 "print authentication debugging information" },
664 #endif
665 #ifdef ENCRYPTION
666 { "autoencrypt",
667 "automatic encryption of data stream",
668 EncryptAutoEnc,
670 "automatically encrypt output" },
671 { "autodecrypt",
672 "automatic decryption of data stream",
673 EncryptAutoDec,
675 "automatically decrypt input" },
676 { "verbose_encrypt",
677 "Toggle verbose encryption output",
678 EncryptVerbose,
680 "print verbose encryption output" },
681 { "encdebug",
682 "Toggle encryption debugging",
683 EncryptDebug,
685 "print encryption debugging information" },
686 #endif /* ENCRYPTION */
687 { "skiprc",
688 "don't read ~/.telnetrc file",
690 &skiprc,
691 "skip reading of ~/.telnetrc file" },
692 { "binary",
693 "sending and receiving of binary data",
694 togbinary,
696 0 },
697 { "inbinary",
698 "receiving of binary data",
699 togrbinary,
701 0 },
702 { "outbinary",
703 "sending of binary data",
704 togxbinary,
706 0 },
707 { "crlf",
708 "sending carriage returns as telnet <CR><LF>",
709 togcrlf,
710 &crlf,
711 0 },
712 { "crmod",
713 "mapping of received carriage returns",
715 &crmod,
716 "map carriage return on output" },
717 { "localchars",
718 "local recognition of certain control characters",
719 lclchars,
720 &localchars,
721 "recognize certain control characters" },
722 { " ", "", 0 }, /* empty line */
723 #if defined(unix) && defined(TN3270)
724 { "apitrace",
725 "(debugging) toggle tracing of API transactions",
727 &apitrace,
728 "trace API transactions" },
729 { "cursesdata",
730 "(debugging) toggle printing of hexadecimal curses data",
732 &cursesdata,
733 "print hexadecimal representation of curses data" },
734 #endif /* defined(unix) && defined(TN3270) */
735 { "debug",
736 "debugging",
737 togdebug,
738 &debug,
739 "turn on socket level debugging" },
740 { "netdata",
741 "printing of hexadecimal network data (debugging)",
743 &netdata,
744 "print hexadecimal representation of network traffic" },
745 { "prettydump",
746 "output of \"netdata\" to user readable format (debugging)",
748 &prettydump,
749 "print user readable output for \"netdata\"" },
750 { "options",
751 "viewing of options processing (debugging)",
753 &showoptions,
754 "show option processing" },
755 { "termdata",
756 "(debugging) toggle printing of hexadecimal terminal data",
758 &termdata,
759 "print hexadecimal representation of terminal traffic" },
760 { "?",
762 togglehelp },
763 { "help",
765 togglehelp },
766 { 0 }
769 static int
770 togglehelp()
772 struct togglelist *c;
774 for (c = Togglelist; c->name; c++) {
775 if (c->help) {
776 if (*c->help)
777 printf("%-15s toggle %s\n", c->name, c->help);
778 else
779 printf("\n");
782 printf("\n");
783 printf("%-15s %s\n", "?", "display help information");
784 return 0;
787 static void
788 settogglehelp(int set)
790 struct togglelist *c;
792 for (c = Togglelist; c->name; c++) {
793 if (c->help) {
794 if (*c->help)
795 printf("%-15s %s %s\n", c->name, set ? "enable" : "disable",
796 c->help);
797 else
798 printf("\n");
803 #define GETTOGGLE(name) (struct togglelist *) \
804 genget(name, (char **) Togglelist, sizeof(struct togglelist))
806 static int
807 toggle(int argc, char *argv[])
809 int retval = 1;
810 char *name;
811 struct togglelist *c;
813 if (argc < 2) {
814 fprintf(stderr,
815 "Need an argument to 'toggle' command. 'toggle ?' for help.\n");
816 return 0;
818 argc--;
819 argv++;
820 while (argc--) {
821 name = *argv++;
822 c = GETTOGGLE(name);
823 if (Ambiguous(c)) {
824 fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\n",
825 name);
826 return 0;
827 } else if (c == 0) {
828 fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\n",
829 name);
830 return 0;
831 } else {
832 if (c->variable) {
833 *c->variable = !*c->variable; /* invert it */
834 if (c->actionexplanation) {
835 printf("%s %s.\n", *c->variable? "Will" : "Won't",
836 c->actionexplanation);
839 if (c->handler) {
840 retval &= (*c->handler)(-1);
844 return retval;
848 * The following perform the "set" command.
851 #ifdef USE_TERMIO
852 struct termio new_tc = { 0 };
853 #endif
855 struct setlist {
856 const char *name; /* name */
857 const char *help; /* help information */
858 void (*handler)();
859 cc_t *charp; /* where it is located at */
862 static struct setlist Setlist[] = {
863 #ifdef KLUDGELINEMODE
864 { "echo", "character to toggle local echoing on/off", 0, &echoc },
865 #endif
866 { "escape", "character to escape back to telnet command mode", 0, &escape },
867 { "rlogin", "rlogin escape character", 0, &rlogin },
868 { "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile},
869 { " ", "" },
870 { " ", "The following need 'localchars' to be toggled true", 0, 0 },
871 { "flushoutput", "character to cause an Abort Output", 0, termFlushCharp },
872 { "interrupt", "character to cause an Interrupt Process", 0, termIntCharp },
873 { "quit", "character to cause an Abort process", 0, termQuitCharp },
874 { "eof", "character to cause an EOF ", 0, termEofCharp },
875 { " ", "" },
876 { " ", "The following are for local editing in linemode", 0, 0 },
877 { "erase", "character to use to erase a character", 0, termEraseCharp },
878 { "kill", "character to use to erase a line", 0, termKillCharp },
879 { "lnext", "character to use for literal next", 0, termLiteralNextCharp },
880 { "susp", "character to cause a Suspend Process", 0, termSuspCharp },
881 { "reprint", "character to use for line reprint", 0, termRprntCharp },
882 { "worderase", "character to use to erase a word", 0, termWerasCharp },
883 { "start", "character to use for XON", 0, termStartCharp },
884 { "stop", "character to use for XOFF", 0, termStopCharp },
885 { "forw1", "alternate end of line character", 0, termForw1Charp },
886 { "forw2", "alternate end of line character", 0, termForw2Charp },
887 { "ayt", "alternate AYT character", 0, termAytCharp },
888 { 0 }
891 #if defined(CRAY) && !defined(__STDC__)
892 /* Work around compiler bug in pcc 4.1.5 */
893 void
894 _setlist_init()
896 #ifndef KLUDGELINEMODE
897 #define N 5
898 #else
899 #define N 6
900 #endif
901 Setlist[N+0].charp = &termFlushChar;
902 Setlist[N+1].charp = &termIntChar;
903 Setlist[N+2].charp = &termQuitChar;
904 Setlist[N+3].charp = &termEofChar;
905 Setlist[N+6].charp = &termEraseChar;
906 Setlist[N+7].charp = &termKillChar;
907 Setlist[N+8].charp = &termLiteralNextChar;
908 Setlist[N+9].charp = &termSuspChar;
909 Setlist[N+10].charp = &termRprntChar;
910 Setlist[N+11].charp = &termWerasChar;
911 Setlist[N+12].charp = &termStartChar;
912 Setlist[N+13].charp = &termStopChar;
913 Setlist[N+14].charp = &termForw1Char;
914 Setlist[N+15].charp = &termForw2Char;
915 Setlist[N+16].charp = &termAytChar;
916 #undef N
918 #endif /* defined(CRAY) && !defined(__STDC__) */
920 static struct setlist *
921 getset(char *name)
923 return (struct setlist *)
924 genget(name, (char **) Setlist, sizeof(struct setlist));
927 void
928 set_escape_char(char *s)
930 if (rlogin != _POSIX_VDISABLE) {
931 rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
932 printf("Telnet rlogin escape character is '%s'.\n",
933 control(rlogin));
934 } else {
935 escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
936 printf("Telnet escape character is '%s'.\n", control(escape));
940 static int
941 setcmd(int argc, char *argv[])
943 int value;
944 struct setlist *ct;
945 struct togglelist *c;
947 if (argc < 2 || argc > 3) {
948 printf("Format is 'set Name Value'\n'set ?' for help.\n");
949 return 0;
951 if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
952 for (ct = Setlist; ct->name; ct++)
953 printf("%-15s %s\n", ct->name, ct->help);
954 printf("\n");
955 settogglehelp(1);
956 printf("%-15s %s\n", "?", "display help information");
957 return 0;
960 ct = getset(argv[1]);
961 if (ct == 0) {
962 c = GETTOGGLE(argv[1]);
963 if (c == 0) {
964 fprintf(stderr, "'%s': unknown argument ('set ?' for help).\n",
965 argv[1]);
966 return 0;
967 } else if (Ambiguous(c)) {
968 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
969 argv[1]);
970 return 0;
972 if (c->variable) {
973 if ((argc == 2) || (strcmp("on", argv[2]) == 0))
974 *c->variable = 1;
975 else if (strcmp("off", argv[2]) == 0)
976 *c->variable = 0;
977 else {
978 printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
979 return 0;
981 if (c->actionexplanation) {
982 printf("%s %s.\n", *c->variable? "Will" : "Won't",
983 c->actionexplanation);
986 if (c->handler)
987 (*c->handler)(1);
988 } else if (argc != 3) {
989 printf("Format is 'set Name Value'\n'set ?' for help.\n");
990 return 0;
991 } else if (Ambiguous(ct)) {
992 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
993 argv[1]);
994 return 0;
995 } else if (ct->handler) {
996 (*ct->handler)(argv[2]);
997 printf("%s set to \"%s\".\n", ct->name, (char *)ct->charp);
998 } else {
999 if (strcmp("off", argv[2])) {
1000 value = special(argv[2]);
1001 } else {
1002 value = _POSIX_VDISABLE;
1004 *(ct->charp) = (cc_t)value;
1005 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
1007 slc_check();
1008 return 1;
1011 static int
1012 unsetcmd(int argc, char *argv[])
1014 struct setlist *ct;
1015 struct togglelist *c;
1016 register char *name;
1018 if (argc < 2) {
1019 fprintf(stderr,
1020 "Need an argument to 'unset' command. 'unset ?' for help.\n");
1021 return 0;
1023 if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
1024 for (ct = Setlist; ct->name; ct++)
1025 printf("%-15s %s\n", ct->name, ct->help);
1026 printf("\n");
1027 settogglehelp(0);
1028 printf("%-15s %s\n", "?", "display help information");
1029 return 0;
1032 argc--;
1033 argv++;
1034 while (argc--) {
1035 name = *argv++;
1036 ct = getset(name);
1037 if (ct == 0) {
1038 c = GETTOGGLE(name);
1039 if (c == 0) {
1040 fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\n",
1041 name);
1042 return 0;
1043 } else if (Ambiguous(c)) {
1044 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1045 name);
1046 return 0;
1048 if (c->variable) {
1049 *c->variable = 0;
1050 if (c->actionexplanation) {
1051 printf("%s %s.\n", *c->variable? "Will" : "Won't",
1052 c->actionexplanation);
1055 if (c->handler)
1056 (*c->handler)(0);
1057 } else if (Ambiguous(ct)) {
1058 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1059 name);
1060 return 0;
1061 } else if (ct->handler) {
1062 (*ct->handler)(0);
1063 printf("%s reset to \"%s\".\n", ct->name, (char *)ct->charp);
1064 } else {
1065 *(ct->charp) = _POSIX_VDISABLE;
1066 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
1069 return 1;
1073 * The following are the data structures and routines for the
1074 * 'mode' command.
1076 #ifdef KLUDGELINEMODE
1077 extern int kludgelinemode;
1079 static int
1080 dokludgemode()
1082 kludgelinemode = 1;
1083 send_wont(TELOPT_LINEMODE, 1);
1084 send_dont(TELOPT_SGA, 1);
1085 send_dont(TELOPT_ECHO, 1);
1087 #endif
1089 static int
1090 dolinemode()
1092 #ifdef KLUDGELINEMODE
1093 if (kludgelinemode)
1094 send_dont(TELOPT_SGA, 1);
1095 #endif
1096 send_will(TELOPT_LINEMODE, 1);
1097 send_dont(TELOPT_ECHO, 1);
1098 return 1;
1101 static int
1102 docharmode()
1104 #ifdef KLUDGELINEMODE
1105 if (kludgelinemode)
1106 send_do(TELOPT_SGA, 1);
1107 else
1108 #endif
1109 send_wont(TELOPT_LINEMODE, 1);
1110 send_do(TELOPT_ECHO, 1);
1111 return 1;
1114 static int
1115 dolmmode(bit, on)
1116 int bit, on;
1118 unsigned char c;
1119 extern int linemode;
1121 if (my_want_state_is_wont(TELOPT_LINEMODE)) {
1122 printf("?Need to have LINEMODE option enabled first.\n");
1123 printf("'mode ?' for help.\n");
1124 return 0;
1127 if (on)
1128 c = (linemode | bit);
1129 else
1130 c = (linemode & ~bit);
1131 lm_mode(&c, 1, 1);
1132 return 1;
1136 set_mode(int bit)
1138 return dolmmode(bit, 1);
1142 clear_mode(int bit)
1144 return dolmmode(bit, 0);
1147 struct modelist {
1148 const char *name; /* command name */
1149 const char *help; /* help string */
1150 int (*handler)(); /* routine which executes command */
1151 int needconnect; /* Do we need to be connected to execute? */
1152 int arg1;
1155 extern int modehelp();
1157 static struct modelist ModeList[] = {
1158 { "character", "Disable LINEMODE option", docharmode, 1 },
1159 #ifdef KLUDGELINEMODE
1160 { "", "(or disable obsolete line-by-line mode)", 0 },
1161 #endif
1162 { "line", "Enable LINEMODE option", dolinemode, 1 },
1163 #ifdef KLUDGELINEMODE
1164 { "", "(or enable obsolete line-by-line mode)", 0 },
1165 #endif
1166 { "", "", 0 },
1167 { "", "These require the LINEMODE option to be enabled", 0 },
1168 { "isig", "Enable signal trapping", set_mode, 1, MODE_TRAPSIG },
1169 { "+isig", 0, set_mode, 1, MODE_TRAPSIG },
1170 { "-isig", "Disable signal trapping", clear_mode, 1, MODE_TRAPSIG },
1171 { "edit", "Enable character editing", set_mode, 1, MODE_EDIT },
1172 { "+edit", 0, set_mode, 1, MODE_EDIT },
1173 { "-edit", "Disable character editing", clear_mode, 1, MODE_EDIT },
1174 { "softtabs", "Enable tab expansion", set_mode, 1, MODE_SOFT_TAB },
1175 { "+softtabs", 0, set_mode, 1, MODE_SOFT_TAB },
1176 { "-softtabs", "Disable character editing", clear_mode, 1, MODE_SOFT_TAB },
1177 { "litecho", "Enable literal character echo", set_mode, 1, MODE_LIT_ECHO },
1178 { "+litecho", 0, set_mode, 1, MODE_LIT_ECHO },
1179 { "-litecho", "Disable literal character echo", clear_mode, 1, MODE_LIT_ECHO },
1180 { "help", 0, modehelp, 0 },
1181 #ifdef KLUDGELINEMODE
1182 { "kludgeline", 0, dokludgemode, 1 },
1183 #endif
1184 { "", "", 0 },
1185 { "?", "Print help information", modehelp, 0 },
1186 { 0 },
1191 modehelp()
1193 struct modelist *mt;
1195 printf("format is: 'mode Mode', where 'Mode' is one of:\n\n");
1196 for (mt = ModeList; mt->name; mt++) {
1197 if (mt->help) {
1198 if (*mt->help)
1199 printf("%-15s %s\n", mt->name, mt->help);
1200 else
1201 printf("\n");
1204 return 0;
1207 #define GETMODECMD(name) (struct modelist *) \
1208 genget(name, (char **) ModeList, sizeof(struct modelist))
1210 static int
1211 modecmd(int argc, char *argv[])
1213 struct modelist *mt;
1215 if (argc != 2) {
1216 printf("'mode' command requires an argument\n");
1217 printf("'mode ?' for help.\n");
1218 } else if ((mt = GETMODECMD(argv[1])) == 0) {
1219 fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\n", argv[1]);
1220 } else if (Ambiguous(mt)) {
1221 fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]);
1222 } else if (mt->needconnect && !connected) {
1223 printf("?Need to be connected first.\n");
1224 printf("'mode ?' for help.\n");
1225 } else if (mt->handler) {
1226 return (*mt->handler)(mt->arg1);
1228 return 0;
1232 * The following data structures and routines implement the
1233 * "display" command.
1236 static int
1237 display(int argc, char *argv[])
1239 struct togglelist *tl;
1240 struct setlist *sl;
1242 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \
1243 if (*tl->variable) { \
1244 printf("will"); \
1245 } else { \
1246 printf("won't"); \
1248 printf(" %s.\n", tl->actionexplanation); \
1251 #define doset(sl) if (sl->name && *sl->name != ' ') { \
1252 if (sl->handler == 0) \
1253 printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
1254 else \
1255 printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
1258 if (argc == 1) {
1259 for (tl = Togglelist; tl->name; tl++) {
1260 dotog(tl);
1262 printf("\n");
1263 for (sl = Setlist; sl->name; sl++) {
1264 doset(sl);
1266 } else {
1267 int i;
1269 for (i = 1; i < argc; i++) {
1270 sl = getset(argv[i]);
1271 tl = GETTOGGLE(argv[i]);
1272 if (Ambiguous(sl) || Ambiguous(tl)) {
1273 printf("?Ambiguous argument '%s'.\n", argv[i]);
1274 return 0;
1275 } else if (!sl && !tl) {
1276 printf("?Unknown argument '%s'.\n", argv[i]);
1277 return 0;
1278 } else {
1279 if (tl) {
1280 dotog(tl);
1282 if (sl) {
1283 doset(sl);
1288 /*@*/optionstatus();
1289 #ifdef ENCRYPTION
1290 EncryptStatus();
1291 #endif /* ENCRYPTION */
1292 return 1;
1293 #undef doset
1294 #undef dotog
1298 * The following are the data structures, and many of the routines,
1299 * relating to command processing.
1303 * Set the escape character.
1305 static int
1306 setescape(int argc, char *argv[])
1308 register char *arg;
1309 char buf[50];
1311 printf(
1312 "Deprecated usage - please use 'set escape%s%s' in the future.\n",
1313 (argc > 2)? " ":"", (argc > 2)? argv[1]: "");
1314 if (argc > 2)
1315 arg = argv[1];
1316 else {
1317 printf("new escape character: ");
1318 (void) fgets(buf, sizeof(buf), stdin);
1319 arg = buf;
1321 if (arg[0] != '\0')
1322 escape = arg[0];
1323 if (!In3270) {
1324 printf("Escape character is '%s'.\n", control(escape));
1326 (void) fflush(stdout);
1327 return 1;
1330 /*VARARGS*/
1331 static int
1332 togcrmod()
1334 crmod = !crmod;
1335 printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
1336 printf("%s map carriage return on output.\n", crmod ? "Will" : "Won't");
1337 (void) fflush(stdout);
1338 return 1;
1341 /*VARARGS*/
1343 suspend()
1345 #ifdef SIGTSTP
1346 setcommandmode();
1348 long oldrows, oldcols, newrows, newcols, err;
1350 err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1351 (void) kill(0, SIGTSTP);
1353 * If we didn't get the window size before the SUSPEND, but we
1354 * can get them now (?), then send the NAWS to make sure that
1355 * we are set up for the right window size.
1357 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1358 (err || ((oldrows != newrows) || (oldcols != newcols)))) {
1359 sendnaws();
1362 /* reget parameters in case they were changed */
1363 TerminalSaveState();
1364 setconnmode(0);
1365 #else
1366 printf("Suspend is not supported. Try the '!' command instead\n");
1367 #endif
1368 return 1;
1371 #if !defined(TN3270)
1372 /*ARGSUSED*/
1374 shell(int argc, char *argv[])
1376 long oldrows, oldcols, newrows, newcols, err;
1378 setcommandmode();
1380 err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1381 switch(vfork()) {
1382 case -1:
1383 perror("Fork failed\n");
1384 break;
1386 case 0:
1389 * Fire up the shell in the child.
1391 register char *shellp, *shellname;
1392 #ifndef strrchr
1393 extern char *strrchr();
1394 #endif
1396 shellp = getenv("SHELL");
1397 if (shellp == NULL)
1398 shellp = "/bin/sh";
1399 if ((shellname = strrchr(shellp, '/')) == 0)
1400 shellname = shellp;
1401 else
1402 shellname++;
1403 if (argc > 1)
1404 execl(shellp, shellname, "-c", &saveline[1], 0);
1405 else
1406 execl(shellp, shellname, 0);
1407 perror("Execl");
1408 _exit(1);
1410 default:
1411 (void)wait((int *)0); /* Wait for the shell to complete */
1413 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1414 (err || ((oldrows != newrows) || (oldcols != newcols)))) {
1415 sendnaws();
1417 break;
1419 return 1;
1421 #else /* !defined(TN3270) */
1422 extern int shell();
1423 #endif /* !defined(TN3270) */
1425 /*VARARGS*/
1426 /* int argc; Number of arguments */
1427 /* char *argv[]; arguments */
1428 static
1429 bye(int argc, char *argv[])
1431 extern int resettermname;
1433 if (connected) {
1434 (void) shutdown(net, 2);
1435 printf("Connection closed.\n");
1436 (void) NetClose(net);
1437 connected = 0;
1438 resettermname = 1;
1439 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
1440 auth_encrypt_connect(connected);
1441 #endif /* defined(AUTHENTICATION) || defined(ENCRYPTION) */
1442 /* reset options */
1443 tninit();
1444 #if defined(TN3270)
1445 SetIn3270(); /* Get out of 3270 mode */
1446 #endif /* defined(TN3270) */
1448 if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
1449 longjmp(toplevel, 1);
1450 /* NOTREACHED */
1452 return 1; /* Keep lint, etc., happy */
1455 /*VARARGS*/
1457 quit()
1459 (void) call(bye, "bye", "fromquit", 0);
1460 Exit(0);
1461 /*NOTREACHED*/
1464 /*VARARGS*/
1466 logout()
1468 send_do(TELOPT_LOGOUT, 1);
1469 (void) netflush();
1470 return 1;
1475 * The SLC command.
1478 struct slclist {
1479 const char *name;
1480 const char *help;
1481 void (*handler)();
1482 int arg;
1485 static void slc_help();
1487 struct slclist SlcList[] = {
1488 { "export", "Use local special character definitions",
1489 slc_mode_export, 0 },
1490 { "import", "Use remote special character definitions",
1491 slc_mode_import, 1 },
1492 { "check", "Verify remote special character definitions",
1493 slc_mode_import, 0 },
1494 { "help", 0, slc_help, 0 },
1495 { "?", "Print help information", slc_help, 0 },
1496 { 0 },
1499 static void
1500 slc_help()
1502 struct slclist *c;
1504 for (c = SlcList; c->name; c++) {
1505 if (c->help) {
1506 if (*c->help)
1507 printf("%-15s %s\n", c->name, c->help);
1508 else
1509 printf("\n");
1514 static struct slclist *
1515 getslc(name)
1516 char *name;
1518 return (struct slclist *)
1519 genget(name, (char **) SlcList, sizeof(struct slclist));
1522 static
1523 slccmd(argc, argv)
1524 int argc;
1525 char *argv[];
1527 struct slclist *c;
1529 if (argc != 2) {
1530 fprintf(stderr,
1531 "Need an argument to 'slc' command. 'slc ?' for help.\n");
1532 return 0;
1534 c = getslc(argv[1]);
1535 if (c == 0) {
1536 fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\n",
1537 argv[1]);
1538 return 0;
1540 if (Ambiguous(c)) {
1541 fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\n",
1542 argv[1]);
1543 return 0;
1545 (*c->handler)(c->arg);
1546 slcstate();
1547 return 1;
1551 * The ENVIRON command.
1554 struct envlist {
1555 const char *name;
1556 const char *help;
1557 void (*handler)();
1558 int narg;
1561 extern struct env_lst *
1562 env_define P((unsigned char *, unsigned char *));
1563 extern void
1564 env_undefine P((unsigned char *)),
1565 env_export P((unsigned char *)),
1566 env_unexport P((unsigned char *)),
1567 env_send P((unsigned char *)),
1568 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1569 env_varval P((unsigned char *)),
1570 #endif
1571 env_list P((void));
1572 static void
1573 env_help P((void));
1575 struct envlist EnvList[] = {
1576 { "define", "Define an environment variable",
1577 (void (*)())env_define, 2 },
1578 { "undefine", "Undefine an environment variable",
1579 env_undefine, 1 },
1580 { "export", "Mark an environment variable for automatic export",
1581 env_export, 1 },
1582 { "unexport", "Don't mark an environment variable for automatic export",
1583 env_unexport, 1 },
1584 { "send", "Send an environment variable", env_send, 1 },
1585 { "list", "List the current environment variables",
1586 env_list, 0 },
1587 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1588 { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
1589 env_varval, 1 },
1590 #endif
1591 { "help", 0, env_help, 0 },
1592 { "?", "Print help information", env_help, 0 },
1593 { 0 },
1596 static void
1597 env_help()
1599 struct envlist *c;
1601 for (c = EnvList; c->name; c++) {
1602 if (c->help) {
1603 if (*c->help)
1604 printf("%-15s %s\n", c->name, c->help);
1605 else
1606 printf("\n");
1611 static struct envlist *
1612 getenvcmd(char *name)
1614 return (struct envlist *)
1615 genget(name, (char **) EnvList, sizeof(struct envlist));
1619 env_cmd(int argc, char *argv[])
1621 struct envlist *c;
1623 if (argc < 2) {
1624 fprintf(stderr,
1625 "Need an argument to 'environ' command. 'environ ?' for help.\n");
1626 return 0;
1628 c = getenvcmd(argv[1]);
1629 if (c == 0) {
1630 fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\n",
1631 argv[1]);
1632 return 0;
1634 if (Ambiguous(c)) {
1635 fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\n",
1636 argv[1]);
1637 return 0;
1639 if (c->narg + 2 != argc) {
1640 fprintf(stderr,
1641 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\n",
1642 c->narg < argc + 2 ? "only " : "",
1643 c->narg, c->narg == 1 ? "" : "s", c->name);
1644 return 0;
1646 (*c->handler)(argv[2], argv[3]);
1647 return 1;
1650 struct env_lst {
1651 struct env_lst *next; /* pointer to next structure */
1652 struct env_lst *prev; /* pointer to previous structure */
1653 unsigned char *var; /* pointer to variable name */
1654 unsigned char *value; /* pointer to variable value */
1655 int export; /* 1 -> export with default list of variables */
1656 int welldefined; /* A well defined variable */
1659 struct env_lst envlisthead;
1661 struct env_lst *
1662 env_find(unsigned char *var)
1664 register struct env_lst *ep;
1666 for (ep = envlisthead.next; ep; ep = ep->next) {
1667 if (strcmp((char *)ep->var, (char *)var) == 0)
1668 return(ep);
1670 return(NULL);
1673 void
1674 env_init()
1676 extern char **environ;
1677 register char **epp, *cp;
1678 register struct env_lst *ep;
1679 #ifndef strchr
1680 extern char *strchr();
1681 #endif
1683 for (epp = environ; *epp; epp++) {
1684 if (cp = strchr(*epp, '=')) {
1685 *cp = '\0';
1686 ep = env_define((unsigned char *)*epp,
1687 (unsigned char *)cp+1);
1688 ep->export = 0;
1689 *cp = '=';
1693 * Special case for DISPLAY variable. If it is ":0.0" or
1694 * "unix:0.0", we have to get rid of "unix" and insert our
1695 * hostname.
1697 if ((ep = env_find("DISPLAY"))
1698 && ((*ep->value == ':')
1699 || (strncmp((char *)ep->value, "unix:", 5) == 0))) {
1700 extern char *localhost ();
1701 char *hostname = localhost ();
1702 char *cp2 = strchr((char *)ep->value, ':');
1704 cp = malloc(strlen(hostname) + strlen(cp2) + 1);
1705 sprintf(cp, "%s%s", hostname, cp2);
1707 free(ep->value);
1708 ep->value = (unsigned char *)cp;
1710 free (hostname);
1713 * If USER is not defined, but LOGNAME is, then add
1714 * USER with the value from LOGNAME. By default, we
1715 * don't export the USER variable.
1717 if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
1718 env_define((unsigned char *)"USER", ep->value);
1719 env_unexport((unsigned char *)"USER");
1721 env_export((unsigned char *)"DISPLAY");
1722 env_export((unsigned char *)"PRINTER");
1725 struct env_lst *
1726 env_define(unsigned char *var, unsigned char *value)
1728 register struct env_lst *ep;
1730 if (ep = env_find(var)) {
1731 if (ep->var)
1732 free(ep->var);
1733 if (ep->value)
1734 free(ep->value);
1735 } else {
1736 ep = (struct env_lst *)malloc(sizeof(struct env_lst));
1737 ep->next = envlisthead.next;
1738 envlisthead.next = ep;
1739 ep->prev = &envlisthead;
1740 if (ep->next)
1741 ep->next->prev = ep;
1743 ep->welldefined = opt_welldefined(var);
1744 ep->export = 1;
1745 ep->var = (unsigned char *)strdup((char *)var);
1746 ep->value = (unsigned char *)strdup((char *)value);
1747 return(ep);
1750 void
1751 env_undefine(unsigned char *var)
1753 register struct env_lst *ep;
1755 if (ep = env_find(var)) {
1756 ep->prev->next = ep->next;
1757 if (ep->next)
1758 ep->next->prev = ep->prev;
1759 if (ep->var)
1760 free(ep->var);
1761 if (ep->value)
1762 free(ep->value);
1763 free(ep);
1767 void
1768 env_export(unsigned char *var)
1770 register struct env_lst *ep;
1772 if (ep = env_find(var))
1773 ep->export = 1;
1776 void
1777 env_unexport(unsigned char *var)
1779 register struct env_lst *ep;
1781 if (ep = env_find(var))
1782 ep->export = 0;
1785 void
1786 env_send(unsigned char *var)
1788 register struct env_lst *ep;
1790 if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1791 #ifdef OLD_ENVIRON
1792 && my_state_is_wont(TELOPT_OLD_ENVIRON)
1793 #endif
1795 fprintf(stderr,
1796 "Cannot send '%s': Telnet ENVIRON option not enabled\n",
1797 var);
1798 return;
1800 ep = env_find(var);
1801 if (ep == 0) {
1802 fprintf(stderr, "Cannot send '%s': variable not defined\n",
1803 var);
1804 return;
1806 env_opt_start_info();
1807 env_opt_add(ep->var);
1808 env_opt_end(0);
1811 void
1812 env_list()
1814 register struct env_lst *ep;
1816 for (ep = envlisthead.next; ep; ep = ep->next) {
1817 printf("%c %-20s %s\n", ep->export ? '*' : ' ',
1818 ep->var, ep->value);
1822 unsigned char *
1823 env_default(int init, int welldefined)
1825 static struct env_lst *nep = NULL;
1827 if (init) {
1828 nep = &envlisthead;
1829 return;
1831 if (nep) {
1832 while (nep = nep->next) {
1833 if (nep->export && (nep->welldefined == welldefined))
1834 return(nep->var);
1837 return(NULL);
1840 unsigned char *
1841 env_getvalue(unsigned char *var)
1843 register struct env_lst *ep;
1845 if (ep = env_find(var))
1846 return(ep->value);
1847 return(NULL);
1850 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1851 void
1852 env_varval(unsigned char *what)
1854 extern int old_env_var, old_env_value, env_auto;
1855 int len = strlen((char *)what);
1857 if (len == 0)
1858 goto unknown;
1860 if (strncasecmp((char *)what, "status", len) == 0) {
1861 if (env_auto)
1862 printf("%s%s", "VAR and VALUE are/will be ",
1863 "determined automatically\n");
1864 if (old_env_var == OLD_ENV_VAR)
1865 printf("VAR and VALUE set to correct definitions\n");
1866 else
1867 printf("VAR and VALUE definitions are reversed\n");
1868 } else if (strncasecmp((char *)what, "auto", len) == 0) {
1869 env_auto = 1;
1870 old_env_var = OLD_ENV_VALUE;
1871 old_env_value = OLD_ENV_VAR;
1872 } else if (strncasecmp((char *)what, "right", len) == 0) {
1873 env_auto = 0;
1874 old_env_var = OLD_ENV_VAR;
1875 old_env_value = OLD_ENV_VALUE;
1876 } else if (strncasecmp((char *)what, "wrong", len) == 0) {
1877 env_auto = 0;
1878 old_env_var = OLD_ENV_VALUE;
1879 old_env_value = OLD_ENV_VAR;
1880 } else {
1881 unknown:
1882 printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
1885 #endif
1887 #if defined(AUTHENTICATION)
1889 * The AUTHENTICATE command.
1892 struct authlist {
1893 const char *name;
1894 char *help;
1895 int (*handler)();
1896 int narg;
1899 extern int
1900 auth_enable P((char *)),
1901 auth_disable P((char *)),
1902 auth_status P((void));
1903 static int
1904 auth_help P((void));
1906 struct authlist AuthList[] = {
1907 { "status", "Display current status of authentication information",
1908 auth_status, 0 },
1909 { "disable", "Disable an authentication type ('auth disable ?' for more)",
1910 auth_disable, 1 },
1911 { "enable", "Enable an authentication type ('auth enable ?' for more)",
1912 auth_enable, 1 },
1913 { "help", 0, auth_help, 0 },
1914 { "?", "Print help information", auth_help, 0 },
1915 { 0 },
1918 static int
1919 auth_help()
1921 struct authlist *c;
1923 for (c = AuthList; c->name; c++) {
1924 if (c->help) {
1925 if (*c->help)
1926 printf("%-15s %s\n", c->name, c->help);
1927 else
1928 printf("\n");
1931 return 0;
1935 auth_cmd(int argc, char *argv[])
1937 struct authlist *c;
1939 if (argc < 2) {
1940 fprintf(stderr,
1941 "Need an argument to 'auth' command. 'auth ?' for help.\n");
1942 return 0;
1945 c = (struct authlist *)
1946 genget(argv[1], (char **) AuthList, sizeof(struct authlist));
1947 if (c == 0) {
1948 fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\n",
1949 argv[1]);
1950 return 0;
1952 if (Ambiguous(c)) {
1953 fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\n",
1954 argv[1]);
1955 return 0;
1957 if (c->narg + 2 != argc) {
1958 fprintf(stderr,
1959 "Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\n",
1960 c->narg < argc + 2 ? "only " : "",
1961 c->narg, c->narg == 1 ? "" : "s", c->name);
1962 return 0;
1964 return((*c->handler)(argv[2], argv[3]));
1966 #endif
1968 #ifdef ENCRYPTION
1970 * The ENCRYPT command.
1973 struct encryptlist {
1974 const char *name;
1975 const char *help;
1976 int (*handler)();
1977 int needconnect;
1978 int minarg;
1979 int maxarg;
1982 extern int
1983 EncryptEnable P((char *, char *)),
1984 EncryptDisable P((char *, char *)),
1985 EncryptType P((char *, char *)),
1986 EncryptStart P((char *)),
1987 EncryptStartInput P((void)),
1988 EncryptStartOutput P((void)),
1989 EncryptStop P((char *)),
1990 EncryptStopInput P((void)),
1991 EncryptStopOutput P((void)),
1992 EncryptStatus P((void));
1993 static int
1994 EncryptHelp P((void));
1996 struct encryptlist EncryptList[] = {
1997 { "enable", "Enable encryption. ('encrypt enable ?' for more)",
1998 EncryptEnable, 1, 1, 2 },
1999 { "disable", "Disable encryption. ('encrypt enable ?' for more)",
2000 EncryptDisable, 0, 1, 2 },
2001 { "type", "Set encryption type. ('encrypt type ?' for more)",
2002 EncryptType, 0, 1, 1 },
2003 { "start", "Start encryption. ('encrypt start ?' for more)",
2004 EncryptStart, 1, 0, 1 },
2005 { "stop", "Stop encryption. ('encrypt stop ?' for more)",
2006 EncryptStop, 1, 0, 1 },
2007 { "input", "Start encrypting the input stream",
2008 EncryptStartInput, 1, 0, 0 },
2009 { "-input", "Stop encrypting the input stream",
2010 EncryptStopInput, 1, 0, 0 },
2011 { "output", "Start encrypting the output stream",
2012 EncryptStartOutput, 1, 0, 0 },
2013 { "-output", "Stop encrypting the output stream",
2014 EncryptStopOutput, 1, 0, 0 },
2016 { "status", "Display current status of authentication information",
2017 EncryptStatus, 0, 0, 0 },
2018 { "help", 0, EncryptHelp, 0, 0, 0 },
2019 { "?", "Print help information", EncryptHelp, 0, 0, 0 },
2020 { 0 },
2023 static int
2024 EncryptHelp()
2026 struct encryptlist *c;
2028 for (c = EncryptList; c->name; c++) {
2029 if (c->help) {
2030 if (*c->help)
2031 printf("%-15s %s\n", c->name, c->help);
2032 else
2033 printf("\n");
2036 return 0;
2040 encrypt_cmd(int argc, char *argv[])
2042 struct encryptlist *c;
2044 if (argc < 2) {
2045 fprintf(stderr,
2046 "Need an argument to 'encrypt' command. 'encrypt ?' for help.\n");
2047 return 0;
2050 c = (struct encryptlist *)
2051 genget(argv[1], (char **) EncryptList, sizeof(struct encryptlist));
2052 if (c == 0) {
2053 fprintf(stderr, "'%s': unknown argument ('encrypt ?' for help).\n",
2054 argv[1]);
2055 return 0;
2057 if (Ambiguous(c)) {
2058 fprintf(stderr, "'%s': ambiguous argument ('encrypt ?' for help).\n",
2059 argv[1]);
2060 return 0;
2062 argc -= 2;
2063 if (argc < c->minarg || argc > c->maxarg) {
2064 if (c->minarg == c->maxarg) {
2065 fprintf(stderr, "Need %s%d argument%s ",
2066 c->minarg < argc ? "only " : "", c->minarg,
2067 c->minarg == 1 ? "" : "s");
2068 } else {
2069 fprintf(stderr, "Need %s%d-%d arguments ",
2070 c->maxarg < argc ? "only " : "", c->minarg, c->maxarg);
2072 fprintf(stderr, "to 'encrypt %s' command. 'encrypt ?' for help.\n",
2073 c->name);
2074 return 0;
2076 if (c->needconnect && !connected) {
2077 if (!(argc && (isprefix(argv[2], "help") || isprefix(argv[2], "?")))) {
2078 printf("?Need to be connected first.\n");
2079 return 0;
2082 return ((*c->handler)(argc > 0 ? argv[2] : 0,
2083 argc > 1 ? argv[3] : 0,
2084 argc > 2 ? argv[4] : 0));
2086 #endif /* ENCRYPTION */
2088 #if defined(unix) && defined(TN3270)
2089 static void
2090 filestuff(int fd)
2092 int res;
2094 #ifdef F_GETOWN
2095 setconnmode(0);
2096 res = fcntl(fd, F_GETOWN, 0);
2097 setcommandmode();
2099 if (res == -1) {
2100 perror("fcntl");
2101 return;
2103 printf("\tOwner is %d.\n", res);
2104 #endif
2106 setconnmode(0);
2107 res = fcntl(fd, F_GETFL, 0);
2108 setcommandmode();
2110 if (res == -1) {
2111 perror("fcntl");
2112 return;
2114 #ifdef notdef
2115 printf("\tFlags are 0x%x: %s\n", res, decodeflags(res));
2116 #endif
2118 #endif /* defined(unix) && defined(TN3270) */
2121 * Print status about the connection.
2123 /*ARGSUSED*/
2124 static int
2125 status(int argc, char *argv[])
2127 if (connected) {
2128 printf("Connected to %s.\n", hostname);
2129 if ((argc < 2) || strcmp(argv[1], "notmuch")) {
2130 int mode = getconnmode();
2132 if (my_want_state_is_will(TELOPT_LINEMODE)) {
2133 printf("Operating with LINEMODE option\n");
2134 printf("%s line editing\n", (mode&MODE_EDIT) ? "Local" : "No");
2135 printf("%s catching of signals\n",
2136 (mode&MODE_TRAPSIG) ? "Local" : "No");
2137 slcstate();
2138 #ifdef KLUDGELINEMODE
2139 } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
2140 printf("Operating in obsolete linemode\n");
2141 #endif
2142 } else {
2143 printf("Operating in single character mode\n");
2144 if (localchars)
2145 printf("Catching signals locally\n");
2147 printf("%s character echo\n", (mode&MODE_ECHO) ? "Local" : "Remote");
2148 if (my_want_state_is_will(TELOPT_LFLOW))
2149 printf("%s flow control\n", (mode&MODE_FLOW) ? "Local" : "No");
2150 #ifdef ENCRYPTION
2151 encrypt_display();
2152 #endif /* ENCRYPTION */
2154 } else {
2155 printf("No connection.\n");
2157 # if !defined(TN3270)
2158 printf("Escape character is '%s'.\n", control(escape));
2159 (void) fflush(stdout);
2160 # else /* !defined(TN3270) */
2161 if ((!In3270) && ((argc < 2) || strcmp(argv[1], "notmuch"))) {
2162 printf("Escape character is '%s'.\n", control(escape));
2164 # if defined(unix)
2165 if ((argc >= 2) && !strcmp(argv[1], "everything")) {
2166 printf("SIGIO received %d time%s.\n",
2167 sigiocount, (sigiocount == 1)? "":"s");
2168 if (In3270) {
2169 printf("Process ID %d, process group %d.\n",
2170 getpid(), getpgrp(getpid()));
2171 printf("Terminal input:\n");
2172 filestuff(tin);
2173 printf("Terminal output:\n");
2174 filestuff(tout);
2175 printf("Network socket:\n");
2176 filestuff(net);
2179 if (In3270 && transcom) {
2180 printf("Transparent mode command is '%s'.\n", transcom);
2182 # endif /* defined(unix) */
2183 (void) fflush(stdout);
2184 if (In3270) {
2185 return 0;
2187 # endif /* defined(TN3270) */
2188 return 1;
2191 #ifdef SIGINFO
2193 * Function that gets called when SIGINFO is received.
2196 ayt_status()
2198 (void) call(status, "status", "notmuch", 0);
2200 #endif
2202 unsigned long inet_addr();
2205 tn(int argc, char *argv[])
2207 register struct hostent *host = 0;
2208 struct sockaddr_in sin;
2209 struct servent *sp = 0;
2210 unsigned long temp;
2211 extern char *inet_ntoa();
2212 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2213 char *srp = 0;
2214 #ifndef strrchr
2215 char *strrchr();
2216 #endif
2217 unsigned long sourceroute(), srlen;
2218 #endif
2219 char *cmd, *hostp = 0, *portp = 0, *user = 0;
2221 /* clear the socket address prior to use */
2222 memset((char *)&sin, 0, sizeof(sin));
2224 if (connected) {
2225 printf("?Already connected to %s\n", hostname);
2226 setuid(getuid());
2227 return 0;
2229 if (argc < 2) {
2230 (void) strcpy(line, "open ");
2231 printf("(to) ");
2232 (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
2233 makeargv();
2234 argc = margc;
2235 argv = margv;
2237 cmd = *argv;
2238 --argc; ++argv;
2239 while (argc) {
2240 if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
2241 goto usage;
2242 if (strcmp(*argv, "-l") == 0) {
2243 --argc; ++argv;
2244 if (argc == 0)
2245 goto usage;
2246 user = *argv++;
2247 --argc;
2248 continue;
2250 if (strcmp(*argv, "-a") == 0) {
2251 --argc; ++argv;
2252 autologin = 1;
2253 continue;
2255 if (hostp == 0) {
2256 hostp = *argv++;
2257 --argc;
2258 continue;
2260 if (portp == 0) {
2261 portp = *argv++;
2262 --argc;
2263 continue;
2265 usage:
2266 printf("usage: %s [-l user] [-a] host-name [port]\n", cmd);
2267 setuid(getuid());
2268 return 0;
2270 if (hostp == 0)
2271 goto usage;
2273 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2274 if (hostp[0] == '@' || hostp[0] == '!') {
2275 if ((hostname = strrchr(hostp, ':')) == NULL)
2276 hostname = strrchr(hostp, '@');
2277 hostname++;
2278 srp = 0;
2279 temp = sourceroute(hostp, &srp, &srlen);
2280 if (temp == 0) {
2281 herror(srp);
2282 setuid(getuid());
2283 return 0;
2284 } else if (temp == -1) {
2285 printf("Bad source route option: %s\n", hostp);
2286 setuid(getuid());
2287 return 0;
2288 } else {
2289 sin.sin_addr.s_addr = temp;
2290 sin.sin_family = AF_INET;
2292 } else {
2293 #endif
2294 temp = inet_addr(hostp);
2295 if (temp != (unsigned long) -1) {
2296 sin.sin_addr.s_addr = temp;
2297 sin.sin_family = AF_INET;
2299 if (_hostname)
2300 free (_hostname);
2301 _hostname = malloc (strlen (hostp) + 1);
2302 if (_hostname) {
2303 strcpy (_hostname, hostp);
2304 hostname = _hostname;
2305 } else {
2306 printf ("Can't allocate memory to copy hostname\n");
2307 setuid(getuid());
2308 return 0;
2310 } else {
2311 host = gethostbyname(hostp);
2312 if (host) {
2313 sin.sin_family = host->h_addrtype;
2314 #if defined(h_addr) /* In 4.3, this is a #define */
2315 memmove((caddr_t)&sin.sin_addr,
2316 host->h_addr_list[0], host->h_length);
2317 #else /* defined(h_addr) */
2318 memmove((caddr_t)&sin.sin_addr, host->h_addr, host->h_length);
2319 #endif /* defined(h_addr) */
2321 if (_hostname)
2322 free (_hostname);
2323 _hostname = malloc (strlen (host->h_name) + 1);
2324 if (_hostname) {
2325 strcpy (_hostname, host->h_name);
2326 hostname = _hostname;
2327 } else {
2328 printf ("Can't allocate memory to copy hostname\n");
2329 setuid(getuid());
2330 return 0;
2332 } else {
2333 herror(hostp);
2334 setuid(getuid());
2335 return 0;
2338 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2340 #endif
2341 if (portp) {
2342 if (*portp == '-') {
2343 portp++;
2344 telnetport = 1;
2345 } else
2346 telnetport = 0;
2347 sin.sin_port = atoi(portp);
2348 if (sin.sin_port == 0) {
2349 sp = getservbyname(portp, "tcp");
2350 if (sp)
2351 sin.sin_port = sp->s_port;
2352 else {
2353 printf("%s: bad port number\n", portp);
2354 setuid(getuid());
2355 return 0;
2357 } else {
2358 #if !HAVE_DECL_HTONS
2359 #ifndef htons
2360 u_short htons __P((unsigned short));
2361 #endif
2362 #endif
2363 sin.sin_port = htons (sin.sin_port);
2365 } else {
2366 if (sp == 0) {
2367 sp = getservbyname("telnet", "tcp");
2368 if (sp == 0) {
2369 fprintf(stderr, "telnet: tcp/telnet: unknown service\n");
2370 setuid(getuid());
2371 return 0;
2373 sin.sin_port = sp->s_port;
2375 telnetport = 1;
2377 printf("Trying %s...\n", inet_ntoa(sin.sin_addr));
2378 do {
2379 net = socket(AF_INET, SOCK_STREAM, 0);
2380 setuid(getuid());
2381 if (net < 0) {
2382 perror("telnet: socket");
2383 return 0;
2385 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2386 if (srp && setsockopt(net, IPPROTO_IP, IP_OPTIONS, (char *)srp, srlen) < 0)
2387 perror("setsockopt (IP_OPTIONS)");
2388 #endif
2389 #if defined(IPPROTO_IP) && defined(IP_TOS)
2391 # if defined(HAS_GETTOS)
2392 struct tosent *tp;
2393 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
2394 tos = tp->t_tos;
2395 # endif
2396 if (tos < 0)
2397 tos = 020; /* Low Delay bit */
2398 if (tos
2399 && (setsockopt(net, IPPROTO_IP, IP_TOS,
2400 (char *)&tos, sizeof(int)) < 0)
2401 && (errno != ENOPROTOOPT))
2402 perror("telnet: setsockopt (IP_TOS) (ignored)");
2404 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2406 if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
2407 perror("setsockopt (SO_DEBUG)");
2410 if (connect(net, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
2411 #if defined(h_addr) /* In 4.3, this is a #define */
2412 if (host && host->h_addr_list[1]) {
2413 int oerrno = errno;
2415 fprintf(stderr, "telnet: connect to address %s: ",
2416 inet_ntoa(sin.sin_addr));
2417 errno = oerrno;
2418 perror((char *)0);
2419 host->h_addr_list++;
2420 memmove((caddr_t)&sin.sin_addr,
2421 host->h_addr_list[0], host->h_length);
2422 (void) NetClose(net);
2423 continue;
2425 #endif /* defined(h_addr) */
2426 perror("telnet: Unable to connect to remote host");
2427 return 0;
2429 connected++;
2430 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
2431 auth_encrypt_connect(connected);
2432 #endif /* defined(AUTHENTICATION) || defined(ENCRYPTION) */
2433 } while (connected == 0);
2434 cmdrc(hostp, hostname);
2435 if (autologin && user == NULL) {
2436 struct passwd *pw;
2438 user = getenv("USER");
2439 if (user == NULL ||
2440 (pw = getpwnam(user)) && pw->pw_uid != getuid()) {
2441 if (pw = getpwuid(getuid()))
2442 user = pw->pw_name;
2443 else
2444 user = NULL;
2447 if (user) {
2448 env_define((unsigned char *)"USER", (unsigned char *)user);
2449 env_export((unsigned char *)"USER");
2451 (void) call(status, "status", "notmuch", 0);
2452 if (setjmp(peerdied) == 0)
2453 telnet(user);
2454 (void) NetClose(net);
2455 ExitString("Connection closed by foreign host.\n",1);
2456 /*NOTREACHED*/
2459 #define HELPINDENT (sizeof ("connect"))
2461 static char
2462 openhelp[] = "connect to a site",
2463 closehelp[] = "close current connection",
2464 logouthelp[] = "forcibly logout remote user and close the connection",
2465 quithelp[] = "exit telnet",
2466 statushelp[] = "print status information",
2467 helphelp[] = "print help information",
2468 sendhelp[] = "transmit special characters ('send ?' for more)",
2469 sethelp[] = "set operating parameters ('set ?' for more)",
2470 unsethelp[] = "unset operating parameters ('unset ?' for more)",
2471 togglestring[] ="toggle operating parameters ('toggle ?' for more)",
2472 slchelp[] = "change state of special charaters ('slc ?' for more)",
2473 displayhelp[] = "display operating parameters",
2474 #if defined(TN3270) && defined(unix)
2475 transcomhelp[] = "specify Unix command for transparent mode pipe",
2476 #endif /* defined(TN3270) && defined(unix) */
2477 #if defined(AUTHENTICATION)
2478 authhelp[] = "turn on (off) authentication ('auth ?' for more)",
2479 #endif
2480 #ifdef ENCRYPTION
2481 encrypthelp[] = "turn on (off) encryption ('encrypt ?' for more)",
2482 #endif /* ENCRYPTION */
2483 #if defined(unix)
2484 zhelp[] = "suspend telnet",
2485 #endif /* defined(unix) */
2486 shellhelp[] = "invoke a subshell",
2487 envhelp[] = "change environment variables ('environ ?' for more)",
2488 modestring[] = "try to enter line or character mode ('mode ?' for more)";
2490 static int help();
2492 static Command cmdtab[] = {
2493 { "close", closehelp, bye, 1 },
2494 { "logout", logouthelp, logout, 1 },
2495 { "display", displayhelp, display, 0 },
2496 { "mode", modestring, modecmd, 0 },
2497 { "open", openhelp, tn, 0 },
2498 { "quit", quithelp, quit, 0 },
2499 { "send", sendhelp, sendcmd, 0 },
2500 { "set", sethelp, setcmd, 0 },
2501 { "unset", unsethelp, unsetcmd, 0 },
2502 { "status", statushelp, status, 0 },
2503 { "toggle", togglestring, toggle, 0 },
2504 { "slc", slchelp, slccmd, 0 },
2505 #if defined(TN3270) && defined(unix)
2506 { "transcom", transcomhelp, settranscom, 0 },
2507 #endif /* defined(TN3270) && defined(unix) */
2508 #if defined(AUTHENTICATION)
2509 { "auth", authhelp, auth_cmd, 0 },
2510 #endif
2511 #ifdef ENCRYPTION
2512 { "encrypt", encrypthelp, encrypt_cmd, 0 },
2513 #endif /* ENCRYPTION */
2514 #if defined(unix)
2515 { "z", zhelp, suspend, 0 },
2516 #endif /* defined(unix) */
2517 #if defined(TN3270)
2518 { "!", shellhelp, shell, 1 },
2519 #else
2520 { "!", shellhelp, shell, 0 },
2521 #endif
2522 { "environ", envhelp, env_cmd, 0 },
2523 { "?", helphelp, help, 0 },
2527 static char crmodhelp[] = "deprecated command -- use 'toggle crmod' instead";
2528 static char escapehelp[] = "deprecated command -- use 'set escape' instead";
2530 static Command cmdtab2[] = {
2531 { "help", 0, help, 0 },
2532 { "escape", escapehelp, setescape, 0 },
2533 { "crmod", crmodhelp, togcrmod, 0 },
2539 * Call routine with argc, argv set from args (terminated by 0).
2542 /*VARARGS1*/
2543 static
2544 #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
2545 call(intrtn_t routine, ...)
2546 #else
2547 call(va_alist)
2548 va_dcl
2549 #endif
2551 va_list ap;
2552 #if !(defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__)
2553 intrtn_t routine;
2554 #endif
2555 char *args[100];
2556 int argno = 0;
2558 #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
2559 va_start(ap, routine);
2560 #else
2561 va_start(ap);
2562 routine = (va_arg(ap, intrtn_t));
2563 #endif
2564 while ((args[argno++] = va_arg(ap, char *)) != 0) {
2567 va_end(ap);
2568 return (*routine)(argno-1, args);
2572 static Command *
2573 getcmd(char *name)
2575 Command *cm;
2577 if (cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command)))
2578 return cm;
2579 return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
2582 void
2583 command(int top, char *tbuf, int cnt)
2585 register Command *c;
2587 setcommandmode();
2588 if (!top) {
2589 putchar('\n');
2590 #if defined(unix)
2591 } else {
2592 (void) signal(SIGINT, SIG_DFL);
2593 (void) signal(SIGQUIT, SIG_DFL);
2594 #endif /* defined(unix) */
2596 for (;;) {
2597 if (rlogin == _POSIX_VDISABLE)
2598 printf("%s> ", prompt);
2599 if (tbuf) {
2600 register char *cp;
2601 cp = line;
2602 while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
2603 cnt--;
2604 tbuf = 0;
2605 if (cp == line || *--cp != '\n' || cp == line)
2606 goto getline;
2607 *cp = '\0';
2608 if (rlogin == _POSIX_VDISABLE)
2609 printf("%s\n", line);
2610 } else {
2611 getline:
2612 if (rlogin != _POSIX_VDISABLE)
2613 printf("%s> ", prompt);
2614 if (fgets(line, sizeof(line), stdin) == NULL) {
2615 if (feof(stdin) || ferror(stdin)) {
2616 (void) quit();
2617 /*NOTREACHED*/
2619 break;
2622 if (line[0] == 0)
2623 break;
2624 makeargv();
2625 if (margv[0] == 0) {
2626 break;
2628 c = getcmd(margv[0]);
2629 if (Ambiguous(c)) {
2630 printf("?Ambiguous command\n");
2631 continue;
2633 if (c == 0) {
2634 printf("?Invalid command\n");
2635 continue;
2637 if (c->needconnect && !connected) {
2638 printf("?Need to be connected first.\n");
2639 continue;
2641 if ((*c->handler)(margc, margv)) {
2642 break;
2645 if (!top) {
2646 if (!connected) {
2647 longjmp(toplevel, 1);
2648 /*NOTREACHED*/
2650 #if defined(TN3270)
2651 if (shell_active == 0) {
2652 setconnmode(0);
2654 #else /* defined(TN3270) */
2655 setconnmode(0);
2656 #endif /* defined(TN3270) */
2661 * Help command.
2663 static int
2664 help(int argc, char *argv[])
2666 register Command *c;
2668 if (argc == 1) {
2669 printf("Commands may be abbreviated. Commands are:\n\n");
2670 for (c = cmdtab; c->name; c++)
2671 if (c->help) {
2672 printf("%-*s\t%s\n", HELPINDENT, c->name,
2673 c->help);
2675 return 0;
2677 while (--argc > 0) {
2678 register char *arg;
2679 arg = *++argv;
2680 c = getcmd(arg);
2681 if (Ambiguous(c))
2682 printf("?Ambiguous help command %s\n", arg);
2683 else if (c == (Command *)0)
2684 printf("?Invalid help command %s\n", arg);
2685 else
2686 printf("%s\n", c->help);
2688 return 0;
2691 static char *rcname = 0;
2692 static char rcbuf[128];
2695 cmdrc(char *m1, char *m2)
2697 register Command *c;
2698 FILE *rcfile;
2699 int gotmachine = 0;
2700 int l1 = strlen(m1);
2701 int l2 = strlen(m2);
2702 char m1save[64];
2704 if (skiprc)
2705 return;
2707 strcpy(m1save, m1);
2708 m1 = m1save;
2710 if (rcname == 0) {
2711 rcname = getenv("HOME");
2712 if (rcname)
2713 strcpy(rcbuf, rcname);
2714 else
2715 rcbuf[0] = '\0';
2716 strcat(rcbuf, "/.telnetrc");
2717 rcname = rcbuf;
2720 if ((rcfile = fopen(rcname, "r")) == 0) {
2721 return;
2724 for (;;) {
2725 if (fgets(line, sizeof(line), rcfile) == NULL)
2726 break;
2727 if (line[0] == 0)
2728 break;
2729 if (line[0] == '#')
2730 continue;
2731 if (gotmachine) {
2732 if (!isspace(line[0]))
2733 gotmachine = 0;
2735 if (gotmachine == 0) {
2736 if (isspace(line[0]))
2737 continue;
2738 if (strncasecmp(line, m1, l1) == 0)
2739 strncpy(line, &line[l1], sizeof(line) - l1);
2740 else if (strncasecmp(line, m2, l2) == 0)
2741 strncpy(line, &line[l2], sizeof(line) - l2);
2742 else if (strncasecmp(line, "DEFAULT", 7) == 0)
2743 strncpy(line, &line[7], sizeof(line) - 7);
2744 else
2745 continue;
2746 if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
2747 continue;
2748 gotmachine = 1;
2750 makeargv();
2751 if (margv[0] == 0)
2752 continue;
2753 c = getcmd(margv[0]);
2754 if (Ambiguous(c)) {
2755 printf("?Ambiguous command: %s\n", margv[0]);
2756 continue;
2758 if (c == 0) {
2759 printf("?Invalid command: %s\n", margv[0]);
2760 continue;
2763 * This should never happen...
2765 if (c->needconnect && !connected) {
2766 printf("?Need to be connected first for %s.\n", margv[0]);
2767 continue;
2769 (*c->handler)(margc, margv);
2771 fclose(rcfile);
2774 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2777 * Source route is handed in as
2778 * [!]@hop1@hop2...[@|:]dst
2779 * If the leading ! is present, it is a
2780 * strict source route, otherwise it is
2781 * assmed to be a loose source route.
2783 * We fill in the source route option as
2784 * hop1,hop2,hop3...dest
2785 * and return a pointer to hop1, which will
2786 * be the address to connect() to.
2788 * Arguments:
2789 * arg: pointer to route list to decipher
2791 * cpp: If *cpp is not equal to NULL, this is a
2792 * pointer to a pointer to a character array
2793 * that should be filled in with the option.
2795 * lenp: pointer to an integer that contains the
2796 * length of *cpp if *cpp != NULL.
2798 * Return values:
2800 * Returns the address of the host to connect to. If the
2801 * return value is -1, there was a syntax error in the
2802 * option, either unknown characters, or too many hosts.
2803 * If the return value is 0, one of the hostnames in the
2804 * path is unknown, and *cpp is set to point to the bad
2805 * hostname.
2807 * *cpp: If *cpp was equal to NULL, it will be filled
2808 * in with a pointer to our static area that has
2809 * the option filled in. This will be 32bit aligned.
2811 * *lenp: This will be filled in with how long the option
2812 * pointed to by *cpp is.
2815 unsigned long
2816 sourceroute(char *arg, char **cpp, int *lenp)
2818 static char lsr[44];
2819 #ifdef sysV88
2820 static IOPTN ipopt;
2821 #endif
2822 char *cp, *cp2, *lsrp, *lsrep;
2823 register int tmp;
2824 struct in_addr sin_addr;
2825 register struct hostent *host = 0;
2826 register char c;
2829 * Verify the arguments, and make sure we have
2830 * at least 7 bytes for the option.
2832 if (cpp == NULL || lenp == NULL)
2833 return((unsigned long)-1);
2834 if (*cpp != NULL && *lenp < 7)
2835 return((unsigned long)-1);
2837 * Decide whether we have a buffer passed to us,
2838 * or if we need to use our own static buffer.
2840 if (*cpp) {
2841 lsrp = *cpp;
2842 lsrep = lsrp + *lenp;
2843 } else {
2844 *cpp = lsrp = lsr;
2845 lsrep = lsrp + 44;
2848 cp = arg;
2851 * Next, decide whether we have a loose source
2852 * route or a strict source route, and fill in
2853 * the begining of the option.
2855 #ifndef sysV88
2856 if (*cp == '!') {
2857 cp++;
2858 *lsrp++ = IPOPT_SSRR;
2859 } else
2860 *lsrp++ = IPOPT_LSRR;
2861 #else
2862 if (*cp == '!') {
2863 cp++;
2864 ipopt.io_type = IPOPT_SSRR;
2865 } else
2866 ipopt.io_type = IPOPT_LSRR;
2867 #endif
2869 if (*cp != '@')
2870 return((unsigned long)-1);
2872 #ifndef sysV88
2873 lsrp++; /* skip over length, we'll fill it in later */
2874 *lsrp++ = 4;
2875 #endif
2877 cp++;
2879 sin_addr.s_addr = 0;
2881 for (c = 0;;) {
2882 if (c == ':')
2883 cp2 = 0;
2884 else for (cp2 = cp; c = *cp2; cp2++) {
2885 if (c == ',') {
2886 *cp2++ = '\0';
2887 if (*cp2 == '@')
2888 cp2++;
2889 } else if (c == '@') {
2890 *cp2++ = '\0';
2891 } else if (c == ':') {
2892 *cp2++ = '\0';
2893 } else
2894 continue;
2895 break;
2897 if (!c)
2898 cp2 = 0;
2900 if ((tmp = inet_addr(cp)) != -1) {
2901 sin_addr.s_addr = tmp;
2902 } else if (host = gethostbyname(cp)) {
2903 #if defined(h_addr)
2904 memmove((caddr_t)&sin_addr,
2905 host->h_addr_list[0], host->h_length);
2906 #else
2907 memmove((caddr_t)&sin_addr, host->h_addr, host->h_length);
2908 #endif
2909 } else {
2910 *cpp = cp;
2911 return(0);
2913 memmove(lsrp, (char *)&sin_addr, 4);
2914 lsrp += 4;
2915 if (cp2)
2916 cp = cp2;
2917 else
2918 break;
2920 * Check to make sure there is space for next address
2922 if (lsrp + 4 > lsrep)
2923 return((unsigned long)-1);
2925 #ifndef sysV88
2926 if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
2927 *cpp = 0;
2928 *lenp = 0;
2929 return((unsigned long)-1);
2931 *lsrp++ = IPOPT_NOP; /* 32 bit word align it */
2932 *lenp = lsrp - *cpp;
2933 #else
2934 ipopt.io_len = lsrp - *cpp;
2935 if (ipopt.io_len <= 5) { /* Is 3 better ? */
2936 *cpp = 0;
2937 *lenp = 0;
2938 return((unsigned long)-1);
2940 *lenp = sizeof(ipopt);
2941 *cpp = (char *) &ipopt;
2942 #endif
2943 return(sin_addr.s_addr);
2945 #endif