commands.c revision 1.13 1 /* $NetBSD: commands.c,v 1.13 1996/02/28 21:03:53 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)commands.c 8.4 (Berkeley) 5/30/95";
39 #else
40 static char rcsid[] = "$NetBSD: commands.c,v 1.13 1996/02/28 21:03:53 thorpej Exp $";
41 #endif
42 #endif /* not lint */
43
44 #if defined(unix)
45 #include <sys/param.h>
46 #if defined(CRAY) || defined(sysV88)
47 #include <sys/types.h>
48 #endif
49 #include <sys/file.h>
50 #else
51 #include <sys/types.h>
52 #endif /* defined(unix) */
53 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #ifdef CRAY
56 #include <fcntl.h>
57 #endif /* CRAY */
58
59 #include <signal.h>
60 #include <netdb.h>
61 #include <ctype.h>
62 #include <pwd.h>
63 #include <varargs.h>
64 #include <errno.h>
65
66 #include <arpa/telnet.h>
67 #include <sys/cdefs.h>
68 #define P __P
69
70 #include "general.h"
71
72 #include "ring.h"
73
74 #include "externs.h"
75 #include "defines.h"
76 #include "types.h"
77
78 #if !defined(CRAY) && !defined(sysV88)
79 #include <netinet/in_systm.h>
80 # if (defined(vax) || defined(tahoe) || defined(hp300)) && !defined(ultrix)
81 # include <machine/endian.h>
82 # endif /* vax */
83 #endif /* !defined(CRAY) && !defined(sysV88) */
84 #include <netinet/ip.h>
85
86
87 #ifndef MAXHOSTNAMELEN
88 #define MAXHOSTNAMELEN 64
89 #endif MAXHOSTNAMELEN
90
91 #if defined(IPPROTO_IP) && defined(IP_TOS)
92 int tos = -1;
93 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
94
95 char *hostname;
96 static char _hostname[MAXHOSTNAMELEN];
97
98 extern char *getenv();
99
100 extern int isprefix();
101 extern char **genget();
102 extern int Ambiguous();
103
104 static call();
105
106 typedef struct {
107 char *name; /* command name */
108 char *help; /* help string (NULL for no help) */
109 int (*handler)(); /* routine which executes command */
110 int needconnect; /* Do we need to be connected to execute? */
111 } Command;
112
113 static char line[256];
114 static char saveline[256];
115 static int margc;
116 static char *margv[20];
117
118 static void
119 makeargv()
120 {
121 register char *cp, *cp2, c;
122 register char **argp = margv;
123
124 margc = 0;
125 cp = line;
126 if (*cp == '!') { /* Special case shell escape */
127 strcpy(saveline, line); /* save for shell command */
128 *argp++ = "!"; /* No room in string to get this */
129 margc++;
130 cp++;
131 }
132 while (c = *cp) {
133 register int inquote = 0;
134 while (isspace(c))
135 c = *++cp;
136 if (c == '\0')
137 break;
138 *argp++ = cp;
139 margc += 1;
140 for (cp2 = cp; c != '\0'; c = *++cp) {
141 if (inquote) {
142 if (c == inquote) {
143 inquote = 0;
144 continue;
145 }
146 } else {
147 if (c == '\\') {
148 if ((c = *++cp) == '\0')
149 break;
150 } else if (c == '"') {
151 inquote = '"';
152 continue;
153 } else if (c == '\'') {
154 inquote = '\'';
155 continue;
156 } else if (isspace(c))
157 break;
158 }
159 *cp2++ = c;
160 }
161 *cp2 = '\0';
162 if (c == '\0')
163 break;
164 cp++;
165 }
166 *argp++ = 0;
167 }
168
169 /*
170 * Make a character string into a number.
171 *
172 * Todo: 1. Could take random integers (12, 0x12, 012, 0b1).
173 */
174
175 static
176 special(s)
177 register char *s;
178 {
179 register char c;
180 char b;
181
182 switch (*s) {
183 case '^':
184 b = *++s;
185 if (b == '?') {
186 c = b | 0x40; /* DEL */
187 } else {
188 c = b & 0x1f;
189 }
190 break;
191 default:
192 c = *s;
193 break;
194 }
195 return c;
196 }
197
198 /*
199 * Construct a control character sequence
200 * for a special character.
201 */
202 static char *
203 control(c)
204 register cc_t c;
205 {
206 static char buf[5];
207 /*
208 * The only way I could get the Sun 3.5 compiler
209 * to shut up about
210 * if ((unsigned int)c >= 0x80)
211 * was to assign "c" to an unsigned int variable...
212 * Arggg....
213 */
214 register unsigned int uic = (unsigned int)c;
215
216 if (uic == 0x7f)
217 return ("^?");
218 if (c == (cc_t)_POSIX_VDISABLE) {
219 return "off";
220 }
221 if (uic >= 0x80) {
222 buf[0] = '\\';
223 buf[1] = ((c>>6)&07) + '0';
224 buf[2] = ((c>>3)&07) + '0';
225 buf[3] = (c&07) + '0';
226 buf[4] = 0;
227 } else if (uic >= 0x20) {
228 buf[0] = c;
229 buf[1] = 0;
230 } else {
231 buf[0] = '^';
232 buf[1] = '@'+c;
233 buf[2] = 0;
234 }
235 return (buf);
236 }
237
238
239
240 /*
241 * The following are data structures and routines for
242 * the "send" command.
243 *
244 */
245
246 struct sendlist {
247 char *name; /* How user refers to it (case independent) */
248 char *help; /* Help information (0 ==> no help) */
249 int needconnect; /* Need to be connected */
250 int narg; /* Number of arguments */
251 int (*handler)(); /* Routine to perform (for special ops) */
252 int nbyte; /* Number of bytes to send this command */
253 int what; /* Character to be sent (<0 ==> special) */
254 };
255
256
258 static int
259 send_esc P((void)),
260 send_help P((void)),
261 send_docmd P((char *)),
262 send_dontcmd P((char *)),
263 send_willcmd P((char *)),
264 send_wontcmd P((char *));
265
266 static struct sendlist Sendlist[] = {
267 { "ao", "Send Telnet Abort output", 1, 0, 0, 2, AO },
268 { "ayt", "Send Telnet 'Are You There'", 1, 0, 0, 2, AYT },
269 { "brk", "Send Telnet Break", 1, 0, 0, 2, BREAK },
270 { "break", 0, 1, 0, 0, 2, BREAK },
271 { "ec", "Send Telnet Erase Character", 1, 0, 0, 2, EC },
272 { "el", "Send Telnet Erase Line", 1, 0, 0, 2, EL },
273 { "escape", "Send current escape character", 1, 0, send_esc, 1, 0 },
274 { "ga", "Send Telnet 'Go Ahead' sequence", 1, 0, 0, 2, GA },
275 { "ip", "Send Telnet Interrupt Process", 1, 0, 0, 2, IP },
276 { "intp", 0, 1, 0, 0, 2, IP },
277 { "interrupt", 0, 1, 0, 0, 2, IP },
278 { "intr", 0, 1, 0, 0, 2, IP },
279 { "nop", "Send Telnet 'No operation'", 1, 0, 0, 2, NOP },
280 { "eor", "Send Telnet 'End of Record'", 1, 0, 0, 2, EOR },
281 { "abort", "Send Telnet 'Abort Process'", 1, 0, 0, 2, ABORT },
282 { "susp", "Send Telnet 'Suspend Process'", 1, 0, 0, 2, SUSP },
283 { "eof", "Send Telnet End of File Character", 1, 0, 0, 2, xEOF },
284 { "synch", "Perform Telnet 'Synch operation'", 1, 0, dosynch, 2, 0 },
285 { "getstatus", "Send request for STATUS", 1, 0, get_status, 6, 0 },
286 { "?", "Display send options", 0, 0, send_help, 0, 0 },
287 { "help", 0, 0, 0, send_help, 0, 0 },
288 { "do", 0, 0, 1, send_docmd, 3, 0 },
289 { "dont", 0, 0, 1, send_dontcmd, 3, 0 },
290 { "will", 0, 0, 1, send_willcmd, 3, 0 },
291 { "wont", 0, 0, 1, send_wontcmd, 3, 0 },
292 { 0 }
293 };
294
295 #define GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
296 sizeof(struct sendlist)))
297
298 static int
299 sendcmd(argc, argv)
300 int argc;
301 char **argv;
302 {
303 int count; /* how many bytes we are going to need to send */
304 int i;
305 int question = 0; /* was at least one argument a question */
306 struct sendlist *s; /* pointer to current command */
307 int success = 0;
308 int needconnect = 0;
309
310 if (argc < 2) {
311 printf("need at least one argument for 'send' command\n");
312 printf("'send ?' for help\n");
313 return 0;
314 }
315 /*
316 * First, validate all the send arguments.
317 * In addition, we see how much space we are going to need, and
318 * whether or not we will be doing a "SYNCH" operation (which
319 * flushes the network queue).
320 */
321 count = 0;
322 for (i = 1; i < argc; i++) {
323 s = GETSEND(argv[i]);
324 if (s == 0) {
325 printf("Unknown send argument '%s'\n'send ?' for help.\n",
326 argv[i]);
327 return 0;
328 } else if (Ambiguous(s)) {
329 printf("Ambiguous send argument '%s'\n'send ?' for help.\n",
330 argv[i]);
331 return 0;
332 }
333 if (i + s->narg >= argc) {
334 fprintf(stderr,
335 "Need %d argument%s to 'send %s' command. 'send %s ?' for help.\n",
336 s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
337 return 0;
338 }
339 count += s->nbyte;
340 if (s->handler == send_help) {
341 send_help();
342 return 0;
343 }
344
345 i += s->narg;
346 needconnect += s->needconnect;
347 }
348 if (!connected && needconnect) {
349 printf("?Need to be connected first.\n");
350 printf("'send ?' for help\n");
351 return 0;
352 }
353 /* Now, do we have enough room? */
354 if (NETROOM() < count) {
355 printf("There is not enough room in the buffer TO the network\n");
356 printf("to process your request. Nothing will be done.\n");
357 printf("('send synch' will throw away most data in the network\n");
358 printf("buffer, if this might help.)\n");
359 return 0;
360 }
361 /* OK, they are all OK, now go through again and actually send */
362 count = 0;
363 for (i = 1; i < argc; i++) {
364 if ((s = GETSEND(argv[i])) == 0) {
365 fprintf(stderr, "Telnet 'send' error - argument disappeared!\n");
366 (void) quit();
367 /*NOTREACHED*/
368 }
369 if (s->handler) {
370 count++;
371 success += (*s->handler)((s->narg > 0) ? argv[i+1] : 0,
372 (s->narg > 1) ? argv[i+2] : 0);
373 i += s->narg;
374 } else {
375 NET2ADD(IAC, s->what);
376 printoption("SENT", IAC, s->what);
377 }
378 }
379 return (count == success);
380 }
381
382 static int
383 send_esc()
384 {
385 NETADD(escape);
386 return 1;
387 }
388
389 static int
390 send_docmd(name)
391 char *name;
392 {
393 return(send_tncmd(send_do, "do", name));
394 }
395
396 static int
397 send_dontcmd(name)
398 char *name;
399 {
400 return(send_tncmd(send_dont, "dont", name));
401 }
402 static int
403 send_willcmd(name)
404 char *name;
405 {
406 return(send_tncmd(send_will, "will", name));
407 }
408 static int
409 send_wontcmd(name)
410 char *name;
411 {
412 return(send_tncmd(send_wont, "wont", name));
413 }
414
415 int
416 send_tncmd(func, cmd, name)
417 void (*func)();
418 char *cmd, *name;
419 {
420 char **cpp;
421 extern char *telopts[];
422 register int val = 0;
423
424 if (isprefix(name, "?")) {
425 register int col, len;
426
427 printf("Usage: send %s <value|option>\n", cmd);
428 printf("\"value\" must be from 0 to 255\n");
429 printf("Valid options are:\n\t");
430
431 col = 8;
432 for (cpp = telopts; *cpp; cpp++) {
433 len = strlen(*cpp) + 3;
434 if (col + len > 65) {
435 printf("\n\t");
436 col = 8;
437 }
438 printf(" \"%s\"", *cpp);
439 col += len;
440 }
441 printf("\n");
442 return 0;
443 }
444 cpp = (char **)genget(name, telopts, sizeof(char *));
445 if (Ambiguous(cpp)) {
446 fprintf(stderr,"'%s': ambiguous argument ('send %s ?' for help).\n",
447 name, cmd);
448 return 0;
449 }
450 if (cpp) {
451 val = cpp - telopts;
452 } else {
453 register char *cp = name;
454
455 while (*cp >= '0' && *cp <= '9') {
456 val *= 10;
457 val += *cp - '0';
458 cp++;
459 }
460 if (*cp != 0) {
461 fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\n",
462 name, cmd);
463 return 0;
464 } else if (val < 0 || val > 255) {
465 fprintf(stderr, "'%s': bad value ('send %s ?' for help).\n",
466 name, cmd);
467 return 0;
468 }
469 }
470 if (!connected) {
471 printf("?Need to be connected first.\n");
472 return 0;
473 }
474 (*func)(val, 1);
475 return 1;
476 }
477
478 static int
479 send_help()
480 {
481 struct sendlist *s; /* pointer to current command */
482 for (s = Sendlist; s->name; s++) {
483 if (s->help)
484 printf("%-15s %s\n", s->name, s->help);
485 }
486 return(0);
487 }
488
489 /*
491 * The following are the routines and data structures referred
492 * to by the arguments to the "toggle" command.
493 */
494
495 static int
496 lclchars()
497 {
498 donelclchars = 1;
499 return 1;
500 }
501
502 static int
503 togdebug()
504 {
505 #ifndef NOT43
506 if (net > 0 &&
507 (SetSockOpt(net, SOL_SOCKET, SO_DEBUG, debug)) < 0) {
508 perror("setsockopt (SO_DEBUG)");
509 }
510 #else /* NOT43 */
511 if (debug) {
512 if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0)
513 perror("setsockopt (SO_DEBUG)");
514 } else
515 printf("Cannot turn off socket debugging\n");
516 #endif /* NOT43 */
517 return 1;
518 }
519
520
521 static int
522 togcrlf()
523 {
524 if (crlf) {
525 printf("Will send carriage returns as telnet <CR><LF>.\n");
526 } else {
527 printf("Will send carriage returns as telnet <CR><NUL>.\n");
528 }
529 return 1;
530 }
531
532 int binmode;
533
534 static int
535 togbinary(val)
536 int val;
537 {
538 donebinarytoggle = 1;
539
540 if (val >= 0) {
541 binmode = val;
542 } else {
543 if (my_want_state_is_will(TELOPT_BINARY) &&
544 my_want_state_is_do(TELOPT_BINARY)) {
545 binmode = 1;
546 } else if (my_want_state_is_wont(TELOPT_BINARY) &&
547 my_want_state_is_dont(TELOPT_BINARY)) {
548 binmode = 0;
549 }
550 val = binmode ? 0 : 1;
551 }
552
553 if (val == 1) {
554 if (my_want_state_is_will(TELOPT_BINARY) &&
555 my_want_state_is_do(TELOPT_BINARY)) {
556 printf("Already operating in binary mode with remote host.\n");
557 } else {
558 printf("Negotiating binary mode with remote host.\n");
559 tel_enter_binary(3);
560 }
561 } else {
562 if (my_want_state_is_wont(TELOPT_BINARY) &&
563 my_want_state_is_dont(TELOPT_BINARY)) {
564 printf("Already in network ascii mode with remote host.\n");
565 } else {
566 printf("Negotiating network ascii mode with remote host.\n");
567 tel_leave_binary(3);
568 }
569 }
570 return 1;
571 }
572
573 static int
574 togrbinary(val)
575 int val;
576 {
577 donebinarytoggle = 1;
578
579 if (val == -1)
580 val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
581
582 if (val == 1) {
583 if (my_want_state_is_do(TELOPT_BINARY)) {
584 printf("Already receiving in binary mode.\n");
585 } else {
586 printf("Negotiating binary mode on input.\n");
587 tel_enter_binary(1);
588 }
589 } else {
590 if (my_want_state_is_dont(TELOPT_BINARY)) {
591 printf("Already receiving in network ascii mode.\n");
592 } else {
593 printf("Negotiating network ascii mode on input.\n");
594 tel_leave_binary(1);
595 }
596 }
597 return 1;
598 }
599
600 static int
601 togxbinary(val)
602 int val;
603 {
604 donebinarytoggle = 1;
605
606 if (val == -1)
607 val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
608
609 if (val == 1) {
610 if (my_want_state_is_will(TELOPT_BINARY)) {
611 printf("Already transmitting in binary mode.\n");
612 } else {
613 printf("Negotiating binary mode on output.\n");
614 tel_enter_binary(2);
615 }
616 } else {
617 if (my_want_state_is_wont(TELOPT_BINARY)) {
618 printf("Already transmitting in network ascii mode.\n");
619 } else {
620 printf("Negotiating network ascii mode on output.\n");
621 tel_leave_binary(2);
622 }
623 }
624 return 1;
625 }
626
627
628 static int togglehelp P((void));
629 #if defined(AUTHENTICATION)
630 extern int auth_togdebug P((int));
631 #endif
632
633 struct togglelist {
634 char *name; /* name of toggle */
635 char *help; /* help message */
636 int (*handler)(); /* routine to do actual setting */
637 int *variable;
638 char *actionexplanation;
639 };
640
641 static struct togglelist Togglelist[] = {
642 { "autoflush",
643 "flushing of output when sending interrupt characters",
644 0,
645 &autoflush,
646 "flush output when sending interrupt characters" },
647 { "autosynch",
648 "automatic sending of interrupt characters in urgent mode",
649 0,
650 &autosynch,
651 "send interrupt characters in urgent mode" },
652 #if defined(AUTHENTICATION)
653 { "autologin",
654 "automatic sending of login and/or authentication info",
655 0,
656 &autologin,
657 "send login name and/or authentication information" },
658 { "authdebug",
659 "Toggle authentication debugging",
660 auth_togdebug,
661 0,
662 "print authentication debugging information" },
663 #endif
664 { "skiprc",
665 "don't read ~/.telnetrc file",
666 0,
667 &skiprc,
668 "skip reading of ~/.telnetrc file" },
669 { "binary",
670 "sending and receiving of binary data",
671 togbinary,
672 0,
673 0 },
674 { "inbinary",
675 "receiving of binary data",
676 togrbinary,
677 0,
678 0 },
679 { "outbinary",
680 "sending of binary data",
681 togxbinary,
682 0,
683 0 },
684 { "crlf",
685 "sending carriage returns as telnet <CR><LF>",
686 togcrlf,
687 &crlf,
688 0 },
689 { "crmod",
690 "mapping of received carriage returns",
691 0,
692 &crmod,
693 "map carriage return on output" },
694 { "localchars",
695 "local recognition of certain control characters",
696 lclchars,
697 &localchars,
698 "recognize certain control characters" },
699 { " ", "", 0 }, /* empty line */
700 #if defined(unix) && defined(TN3270)
701 { "apitrace",
702 "(debugging) toggle tracing of API transactions",
703 0,
704 &apitrace,
705 "trace API transactions" },
706 { "cursesdata",
707 "(debugging) toggle printing of hexadecimal curses data",
708 0,
709 &cursesdata,
710 "print hexadecimal representation of curses data" },
711 #endif /* defined(unix) && defined(TN3270) */
712 { "debug",
713 "debugging",
714 togdebug,
715 &debug,
716 "turn on socket level debugging" },
717 { "netdata",
718 "printing of hexadecimal network data (debugging)",
719 0,
720 &netdata,
721 "print hexadecimal representation of network traffic" },
722 { "prettydump",
723 "output of \"netdata\" to user readable format (debugging)",
724 0,
725 &prettydump,
726 "print user readable output for \"netdata\"" },
727 { "options",
728 "viewing of options processing (debugging)",
729 0,
730 &showoptions,
731 "show option processing" },
732 #if defined(unix)
733 { "termdata",
734 "(debugging) toggle printing of hexadecimal terminal data",
735 0,
736 &termdata,
737 "print hexadecimal representation of terminal traffic" },
738 #endif /* defined(unix) */
739 { "?",
740 0,
741 togglehelp },
742 { "help",
743 0,
744 togglehelp },
745 { 0 }
746 };
747
748 static int
749 togglehelp()
750 {
751 struct togglelist *c;
752
753 for (c = Togglelist; c->name; c++) {
754 if (c->help) {
755 if (*c->help)
756 printf("%-15s toggle %s\n", c->name, c->help);
757 else
758 printf("\n");
759 }
760 }
761 printf("\n");
762 printf("%-15s %s\n", "?", "display help information");
763 return 0;
764 }
765
766 static void
767 settogglehelp(set)
768 int set;
769 {
770 struct togglelist *c;
771
772 for (c = Togglelist; c->name; c++) {
773 if (c->help) {
774 if (*c->help)
775 printf("%-15s %s %s\n", c->name, set ? "enable" : "disable",
776 c->help);
777 else
778 printf("\n");
779 }
780 }
781 }
782
783 #define GETTOGGLE(name) (struct togglelist *) \
784 genget(name, (char **) Togglelist, sizeof(struct togglelist))
785
786 static int
787 toggle(argc, argv)
788 int argc;
789 char *argv[];
790 {
791 int retval = 1;
792 char *name;
793 struct togglelist *c;
794
795 if (argc < 2) {
796 fprintf(stderr,
797 "Need an argument to 'toggle' command. 'toggle ?' for help.\n");
798 return 0;
799 }
800 argc--;
801 argv++;
802 while (argc--) {
803 name = *argv++;
804 c = GETTOGGLE(name);
805 if (Ambiguous(c)) {
806 fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\n",
807 name);
808 return 0;
809 } else if (c == 0) {
810 fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\n",
811 name);
812 return 0;
813 } else {
814 if (c->variable) {
815 *c->variable = !*c->variable; /* invert it */
816 if (c->actionexplanation) {
817 printf("%s %s.\n", *c->variable? "Will" : "Won't",
818 c->actionexplanation);
819 }
820 }
821 if (c->handler) {
822 retval &= (*c->handler)(-1);
823 }
824 }
825 }
826 return retval;
827 }
828
829 /*
831 * The following perform the "set" command.
832 */
833
834 #ifdef USE_TERMIO
835 struct termio new_tc = { 0 };
836 #endif
837
838 struct setlist {
839 char *name; /* name */
840 char *help; /* help information */
841 void (*handler)();
842 cc_t *charp; /* where it is located at */
843 };
844
845 static struct setlist Setlist[] = {
846 #ifdef KLUDGELINEMODE
847 { "echo", "character to toggle local echoing on/off", 0, &echoc },
848 #endif
849 { "escape", "character to escape back to telnet command mode", 0, &escape },
850 { "rlogin", "rlogin escape character", 0, &rlogin },
851 { "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile},
852 { " ", "" },
853 { " ", "The following need 'localchars' to be toggled true", 0, 0 },
854 { "flushoutput", "character to cause an Abort Output", 0, termFlushCharp },
855 { "interrupt", "character to cause an Interrupt Process", 0, termIntCharp },
856 { "quit", "character to cause an Abort process", 0, termQuitCharp },
857 { "eof", "character to cause an EOF ", 0, termEofCharp },
858 { " ", "" },
859 { " ", "The following are for local editing in linemode", 0, 0 },
860 { "erase", "character to use to erase a character", 0, termEraseCharp },
861 { "kill", "character to use to erase a line", 0, termKillCharp },
862 { "lnext", "character to use for literal next", 0, termLiteralNextCharp },
863 { "susp", "character to cause a Suspend Process", 0, termSuspCharp },
864 { "reprint", "character to use for line reprint", 0, termRprntCharp },
865 { "worderase", "character to use to erase a word", 0, termWerasCharp },
866 { "start", "character to use for XON", 0, termStartCharp },
867 { "stop", "character to use for XOFF", 0, termStopCharp },
868 { "forw1", "alternate end of line character", 0, termForw1Charp },
869 { "forw2", "alternate end of line character", 0, termForw2Charp },
870 { "ayt", "alternate AYT character", 0, termAytCharp },
871 { 0 }
872 };
873
874 #if defined(CRAY) && !defined(__STDC__)
875 /* Work around compiler bug in pcc 4.1.5 */
876 void
877 _setlist_init()
878 {
879 #ifndef KLUDGELINEMODE
880 #define N 5
881 #else
882 #define N 6
883 #endif
884 Setlist[N+0].charp = &termFlushChar;
885 Setlist[N+1].charp = &termIntChar;
886 Setlist[N+2].charp = &termQuitChar;
887 Setlist[N+3].charp = &termEofChar;
888 Setlist[N+6].charp = &termEraseChar;
889 Setlist[N+7].charp = &termKillChar;
890 Setlist[N+8].charp = &termLiteralNextChar;
891 Setlist[N+9].charp = &termSuspChar;
892 Setlist[N+10].charp = &termRprntChar;
893 Setlist[N+11].charp = &termWerasChar;
894 Setlist[N+12].charp = &termStartChar;
895 Setlist[N+13].charp = &termStopChar;
896 Setlist[N+14].charp = &termForw1Char;
897 Setlist[N+15].charp = &termForw2Char;
898 Setlist[N+16].charp = &termAytChar;
899 #undef N
900 }
901 #endif /* defined(CRAY) && !defined(__STDC__) */
902
903 static struct setlist *
904 getset(name)
905 char *name;
906 {
907 return (struct setlist *)
908 genget(name, (char **) Setlist, sizeof(struct setlist));
909 }
910
911 void
912 set_escape_char(s)
913 char *s;
914 {
915 if (rlogin != _POSIX_VDISABLE) {
916 rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
917 printf("Telnet rlogin escape character is '%s'.\n",
918 control(rlogin));
919 } else {
920 escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
921 printf("Telnet escape character is '%s'.\n", control(escape));
922 }
923 }
924
925 static int
926 setcmd(argc, argv)
927 int argc;
928 char *argv[];
929 {
930 int value;
931 struct setlist *ct;
932 struct togglelist *c;
933
934 if (argc < 2 || argc > 3) {
935 printf("Format is 'set Name Value'\n'set ?' for help.\n");
936 return 0;
937 }
938 if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
939 for (ct = Setlist; ct->name; ct++)
940 printf("%-15s %s\n", ct->name, ct->help);
941 printf("\n");
942 settogglehelp(1);
943 printf("%-15s %s\n", "?", "display help information");
944 return 0;
945 }
946
947 ct = getset(argv[1]);
948 if (ct == 0) {
949 c = GETTOGGLE(argv[1]);
950 if (c == 0) {
951 fprintf(stderr, "'%s': unknown argument ('set ?' for help).\n",
952 argv[1]);
953 return 0;
954 } else if (Ambiguous(c)) {
955 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
956 argv[1]);
957 return 0;
958 }
959 if (c->variable) {
960 if ((argc == 2) || (strcmp("on", argv[2]) == 0))
961 *c->variable = 1;
962 else if (strcmp("off", argv[2]) == 0)
963 *c->variable = 0;
964 else {
965 printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
966 return 0;
967 }
968 if (c->actionexplanation) {
969 printf("%s %s.\n", *c->variable? "Will" : "Won't",
970 c->actionexplanation);
971 }
972 }
973 if (c->handler)
974 (*c->handler)(1);
975 } else if (argc != 3) {
976 printf("Format is 'set Name Value'\n'set ?' for help.\n");
977 return 0;
978 } else if (Ambiguous(ct)) {
979 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
980 argv[1]);
981 return 0;
982 } else if (ct->handler) {
983 (*ct->handler)(argv[2]);
984 printf("%s set to \"%s\".\n", ct->name, (char *)ct->charp);
985 } else {
986 if (strcmp("off", argv[2])) {
987 value = special(argv[2]);
988 } else {
989 value = _POSIX_VDISABLE;
990 }
991 *(ct->charp) = (cc_t)value;
992 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
993 }
994 slc_check();
995 return 1;
996 }
997
998 static int
999 unsetcmd(argc, argv)
1000 int argc;
1001 char *argv[];
1002 {
1003 struct setlist *ct;
1004 struct togglelist *c;
1005 register char *name;
1006
1007 if (argc < 2) {
1008 fprintf(stderr,
1009 "Need an argument to 'unset' command. 'unset ?' for help.\n");
1010 return 0;
1011 }
1012 if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
1013 for (ct = Setlist; ct->name; ct++)
1014 printf("%-15s %s\n", ct->name, ct->help);
1015 printf("\n");
1016 settogglehelp(0);
1017 printf("%-15s %s\n", "?", "display help information");
1018 return 0;
1019 }
1020
1021 argc--;
1022 argv++;
1023 while (argc--) {
1024 name = *argv++;
1025 ct = getset(name);
1026 if (ct == 0) {
1027 c = GETTOGGLE(name);
1028 if (c == 0) {
1029 fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\n",
1030 name);
1031 return 0;
1032 } else if (Ambiguous(c)) {
1033 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1034 name);
1035 return 0;
1036 }
1037 if (c->variable) {
1038 *c->variable = 0;
1039 if (c->actionexplanation) {
1040 printf("%s %s.\n", *c->variable? "Will" : "Won't",
1041 c->actionexplanation);
1042 }
1043 }
1044 if (c->handler)
1045 (*c->handler)(0);
1046 } else if (Ambiguous(ct)) {
1047 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1048 name);
1049 return 0;
1050 } else if (ct->handler) {
1051 (*ct->handler)(0);
1052 printf("%s reset to \"%s\".\n", ct->name, (char *)ct->charp);
1053 } else {
1054 *(ct->charp) = _POSIX_VDISABLE;
1055 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
1056 }
1057 }
1058 return 1;
1059 }
1060
1061 /*
1063 * The following are the data structures and routines for the
1064 * 'mode' command.
1065 */
1066 #ifdef KLUDGELINEMODE
1067 extern int kludgelinemode;
1068
1069 static int
1070 dokludgemode()
1071 {
1072 kludgelinemode = 1;
1073 send_wont(TELOPT_LINEMODE, 1);
1074 send_dont(TELOPT_SGA, 1);
1075 send_dont(TELOPT_ECHO, 1);
1076 }
1077 #endif
1078
1079 static int
1080 dolinemode()
1081 {
1082 #ifdef KLUDGELINEMODE
1083 if (kludgelinemode)
1084 send_dont(TELOPT_SGA, 1);
1085 #endif
1086 send_will(TELOPT_LINEMODE, 1);
1087 send_dont(TELOPT_ECHO, 1);
1088 return 1;
1089 }
1090
1091 static int
1092 docharmode()
1093 {
1094 #ifdef KLUDGELINEMODE
1095 if (kludgelinemode)
1096 send_do(TELOPT_SGA, 1);
1097 else
1098 #endif
1099 send_wont(TELOPT_LINEMODE, 1);
1100 send_do(TELOPT_ECHO, 1);
1101 return 1;
1102 }
1103
1104 static int
1105 dolmmode(bit, on)
1106 int bit, on;
1107 {
1108 unsigned char c;
1109 extern int linemode;
1110
1111 if (my_want_state_is_wont(TELOPT_LINEMODE)) {
1112 printf("?Need to have LINEMODE option enabled first.\n");
1113 printf("'mode ?' for help.\n");
1114 return 0;
1115 }
1116
1117 if (on)
1118 c = (linemode | bit);
1119 else
1120 c = (linemode & ~bit);
1121 lm_mode(&c, 1, 1);
1122 return 1;
1123 }
1124
1125 int
1126 setmode(bit)
1127 {
1128 return dolmmode(bit, 1);
1129 }
1130
1131 int
1132 clearmode(bit)
1133 {
1134 return dolmmode(bit, 0);
1135 }
1136
1137 struct modelist {
1138 char *name; /* command name */
1139 char *help; /* help string */
1140 int (*handler)(); /* routine which executes command */
1141 int needconnect; /* Do we need to be connected to execute? */
1142 int arg1;
1143 };
1144
1145 extern int modehelp();
1146
1147 static struct modelist ModeList[] = {
1148 { "character", "Disable LINEMODE option", docharmode, 1 },
1149 #ifdef KLUDGELINEMODE
1150 { "", "(or disable obsolete line-by-line mode)", 0 },
1151 #endif
1152 { "line", "Enable LINEMODE option", dolinemode, 1 },
1153 #ifdef KLUDGELINEMODE
1154 { "", "(or enable obsolete line-by-line mode)", 0 },
1155 #endif
1156 { "", "", 0 },
1157 { "", "These require the LINEMODE option to be enabled", 0 },
1158 { "isig", "Enable signal trapping", setmode, 1, MODE_TRAPSIG },
1159 { "+isig", 0, setmode, 1, MODE_TRAPSIG },
1160 { "-isig", "Disable signal trapping", clearmode, 1, MODE_TRAPSIG },
1161 { "edit", "Enable character editing", setmode, 1, MODE_EDIT },
1162 { "+edit", 0, setmode, 1, MODE_EDIT },
1163 { "-edit", "Disable character editing", clearmode, 1, MODE_EDIT },
1164 { "softtabs", "Enable tab expansion", setmode, 1, MODE_SOFT_TAB },
1165 { "+softtabs", 0, setmode, 1, MODE_SOFT_TAB },
1166 { "-softtabs", "Disable character editing", clearmode, 1, MODE_SOFT_TAB },
1167 { "litecho", "Enable literal character echo", setmode, 1, MODE_LIT_ECHO },
1168 { "+litecho", 0, setmode, 1, MODE_LIT_ECHO },
1169 { "-litecho", "Disable literal character echo", clearmode, 1, MODE_LIT_ECHO },
1170 { "help", 0, modehelp, 0 },
1171 #ifdef KLUDGELINEMODE
1172 { "kludgeline", 0, dokludgemode, 1 },
1173 #endif
1174 { "", "", 0 },
1175 { "?", "Print help information", modehelp, 0 },
1176 { 0 },
1177 };
1178
1179
1180 int
1181 modehelp()
1182 {
1183 struct modelist *mt;
1184
1185 printf("format is: 'mode Mode', where 'Mode' is one of:\n\n");
1186 for (mt = ModeList; mt->name; mt++) {
1187 if (mt->help) {
1188 if (*mt->help)
1189 printf("%-15s %s\n", mt->name, mt->help);
1190 else
1191 printf("\n");
1192 }
1193 }
1194 return 0;
1195 }
1196
1197 #define GETMODECMD(name) (struct modelist *) \
1198 genget(name, (char **) ModeList, sizeof(struct modelist))
1199
1200 static int
1201 modecmd(argc, argv)
1202 int argc;
1203 char *argv[];
1204 {
1205 struct modelist *mt;
1206
1207 if (argc != 2) {
1208 printf("'mode' command requires an argument\n");
1209 printf("'mode ?' for help.\n");
1210 } else if ((mt = GETMODECMD(argv[1])) == 0) {
1211 fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\n", argv[1]);
1212 } else if (Ambiguous(mt)) {
1213 fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]);
1214 } else if (mt->needconnect && !connected) {
1215 printf("?Need to be connected first.\n");
1216 printf("'mode ?' for help.\n");
1217 } else if (mt->handler) {
1218 return (*mt->handler)(mt->arg1);
1219 }
1220 return 0;
1221 }
1222
1223 /*
1225 * The following data structures and routines implement the
1226 * "display" command.
1227 */
1228
1229 static int
1230 display(argc, argv)
1231 int argc;
1232 char *argv[];
1233 {
1234 struct togglelist *tl;
1235 struct setlist *sl;
1236
1237 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \
1238 if (*tl->variable) { \
1239 printf("will"); \
1240 } else { \
1241 printf("won't"); \
1242 } \
1243 printf(" %s.\n", tl->actionexplanation); \
1244 }
1245
1246 #define doset(sl) if (sl->name && *sl->name != ' ') { \
1247 if (sl->handler == 0) \
1248 printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
1249 else \
1250 printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
1251 }
1252
1253 if (argc == 1) {
1254 for (tl = Togglelist; tl->name; tl++) {
1255 dotog(tl);
1256 }
1257 printf("\n");
1258 for (sl = Setlist; sl->name; sl++) {
1259 doset(sl);
1260 }
1261 } else {
1262 int i;
1263
1264 for (i = 1; i < argc; i++) {
1265 sl = getset(argv[i]);
1266 tl = GETTOGGLE(argv[i]);
1267 if (Ambiguous(sl) || Ambiguous(tl)) {
1268 printf("?Ambiguous argument '%s'.\n", argv[i]);
1269 return 0;
1270 } else if (!sl && !tl) {
1271 printf("?Unknown argument '%s'.\n", argv[i]);
1272 return 0;
1273 } else {
1274 if (tl) {
1275 dotog(tl);
1276 }
1277 if (sl) {
1278 doset(sl);
1279 }
1280 }
1281 }
1282 }
1283 /*@*/optionstatus();
1284 return 1;
1285 #undef doset
1286 #undef dotog
1287 }
1288
1289 /*
1291 * The following are the data structures, and many of the routines,
1292 * relating to command processing.
1293 */
1294
1295 /*
1296 * Set the escape character.
1297 */
1298 static int
1299 setescape(argc, argv)
1300 int argc;
1301 char *argv[];
1302 {
1303 register char *arg;
1304 char buf[50];
1305
1306 printf(
1307 "Deprecated usage - please use 'set escape%s%s' in the future.\n",
1308 (argc > 2)? " ":"", (argc > 2)? argv[1]: "");
1309 if (argc > 2)
1310 arg = argv[1];
1311 else {
1312 printf("new escape character: ");
1313 (void) fgets(buf, sizeof(buf), stdin);
1314 arg = buf;
1315 }
1316 if (arg[0] != '\0')
1317 escape = arg[0];
1318 if (!In3270) {
1319 printf("Escape character is '%s'.\n", control(escape));
1320 }
1321 (void) fflush(stdout);
1322 return 1;
1323 }
1324
1325 /*VARARGS*/
1326 static int
1327 togcrmod()
1328 {
1329 crmod = !crmod;
1330 printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
1331 printf("%s map carriage return on output.\n", crmod ? "Will" : "Won't");
1332 (void) fflush(stdout);
1333 return 1;
1334 }
1335
1336 /*VARARGS*/
1337 int
1338 suspend()
1339 {
1340 #ifdef SIGTSTP
1341 setcommandmode();
1342 {
1343 long oldrows, oldcols, newrows, newcols, err;
1344
1345 err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1346 (void) kill(0, SIGTSTP);
1347 /*
1348 * If we didn't get the window size before the SUSPEND, but we
1349 * can get them now (?), then send the NAWS to make sure that
1350 * we are set up for the right window size.
1351 */
1352 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1353 (err || ((oldrows != newrows) || (oldcols != newcols)))) {
1354 sendnaws();
1355 }
1356 }
1357 /* reget parameters in case they were changed */
1358 TerminalSaveState();
1359 setconnmode(0);
1360 #else
1361 printf("Suspend is not supported. Try the '!' command instead\n");
1362 #endif
1363 return 1;
1364 }
1365
1366 #if !defined(TN3270)
1367 /*ARGSUSED*/
1368 int
1369 shell(argc, argv)
1370 int argc;
1371 char *argv[];
1372 {
1373 long oldrows, oldcols, newrows, newcols, err;
1374
1375 setcommandmode();
1376
1377 err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1378 switch(vfork()) {
1379 case -1:
1380 perror("Fork failed\n");
1381 break;
1382
1383 case 0:
1384 {
1385 /*
1386 * Fire up the shell in the child.
1387 */
1388 register char *shellp, *shellname;
1389 extern char *strrchr();
1390
1391 shellp = getenv("SHELL");
1392 if (shellp == NULL)
1393 shellp = "/bin/sh";
1394 if ((shellname = strrchr(shellp, '/')) == 0)
1395 shellname = shellp;
1396 else
1397 shellname++;
1398 if (argc > 1)
1399 execl(shellp, shellname, "-c", &saveline[1], 0);
1400 else
1401 execl(shellp, shellname, 0);
1402 perror("Execl");
1403 _exit(1);
1404 }
1405 default:
1406 (void)wait((int *)0); /* Wait for the shell to complete */
1407
1408 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1409 (err || ((oldrows != newrows) || (oldcols != newcols)))) {
1410 sendnaws();
1411 }
1412 break;
1413 }
1414 return 1;
1415 }
1416 #else /* !defined(TN3270) */
1417 extern int shell();
1418 #endif /* !defined(TN3270) */
1419
1420 /*VARARGS*/
1421 static
1422 bye(argc, argv)
1423 int argc; /* Number of arguments */
1424 char *argv[]; /* arguments */
1425 {
1426 extern int resettermname;
1427
1428 if (connected) {
1429 (void) shutdown(net, 2);
1430 printf("Connection closed.\n");
1431 (void) NetClose(net);
1432 connected = 0;
1433 resettermname = 1;
1434 #if defined(AUTHENTICATION)
1435 auth_encrypt_connect(connected);
1436 #endif /* defined(AUTHENTICATION) */
1437 /* reset options */
1438 tninit();
1439 #if defined(TN3270)
1440 SetIn3270(); /* Get out of 3270 mode */
1441 #endif /* defined(TN3270) */
1442 }
1443 if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
1444 longjmp(toplevel, 1);
1445 /* NOTREACHED */
1446 }
1447 return 1; /* Keep lint, etc., happy */
1448 }
1449
1450 /*VARARGS*/
1451 quit()
1452 {
1453 (void) call(bye, "bye", "fromquit", 0);
1454 Exit(0);
1455 /*NOTREACHED*/
1456 }
1457
1458 /*VARARGS*/
1459 int
1460 logout()
1461 {
1462 send_do(TELOPT_LOGOUT, 1);
1463 (void) netflush();
1464 return 1;
1465 }
1466
1467
1468 /*
1470 * The SLC command.
1471 */
1472
1473 struct slclist {
1474 char *name;
1475 char *help;
1476 void (*handler)();
1477 int arg;
1478 };
1479
1480 static void slc_help();
1481
1482 struct slclist SlcList[] = {
1483 { "export", "Use local special character definitions",
1484 slc_mode_export, 0 },
1485 { "import", "Use remote special character definitions",
1486 slc_mode_import, 1 },
1487 { "check", "Verify remote special character definitions",
1488 slc_mode_import, 0 },
1489 { "help", 0, slc_help, 0 },
1490 { "?", "Print help information", slc_help, 0 },
1491 { 0 },
1492 };
1493
1494 static void
1495 slc_help()
1496 {
1497 struct slclist *c;
1498
1499 for (c = SlcList; c->name; c++) {
1500 if (c->help) {
1501 if (*c->help)
1502 printf("%-15s %s\n", c->name, c->help);
1503 else
1504 printf("\n");
1505 }
1506 }
1507 }
1508
1509 static struct slclist *
1510 getslc(name)
1511 char *name;
1512 {
1513 return (struct slclist *)
1514 genget(name, (char **) SlcList, sizeof(struct slclist));
1515 }
1516
1517 static
1518 slccmd(argc, argv)
1519 int argc;
1520 char *argv[];
1521 {
1522 struct slclist *c;
1523
1524 if (argc != 2) {
1525 fprintf(stderr,
1526 "Need an argument to 'slc' command. 'slc ?' for help.\n");
1527 return 0;
1528 }
1529 c = getslc(argv[1]);
1530 if (c == 0) {
1531 fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\n",
1532 argv[1]);
1533 return 0;
1534 }
1535 if (Ambiguous(c)) {
1536 fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\n",
1537 argv[1]);
1538 return 0;
1539 }
1540 (*c->handler)(c->arg);
1541 slcstate();
1542 return 1;
1543 }
1544
1545 /*
1547 * The ENVIRON command.
1548 */
1549
1550 struct envlist {
1551 char *name;
1552 char *help;
1553 void (*handler)();
1554 int narg;
1555 };
1556
1557 extern struct env_lst *
1558 env_define P((unsigned char *, unsigned char *));
1559 extern void
1560 env_undefine P((unsigned char *)),
1561 env_export P((unsigned char *)),
1562 env_unexport P((unsigned char *)),
1563 env_send P((unsigned char *)),
1564 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1565 env_varval P((unsigned char *)),
1566 #endif
1567 env_list P((void));
1568 static void
1569 env_help P((void));
1570
1571 struct envlist EnvList[] = {
1572 { "define", "Define an environment variable",
1573 (void (*)())env_define, 2 },
1574 { "undefine", "Undefine an environment variable",
1575 env_undefine, 1 },
1576 { "export", "Mark an environment variable for automatic export",
1577 env_export, 1 },
1578 { "unexport", "Don't mark an environment variable for automatic export",
1579 env_unexport, 1 },
1580 { "send", "Send an environment variable", env_send, 1 },
1581 { "list", "List the current environment variables",
1582 env_list, 0 },
1583 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1584 { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
1585 env_varval, 1 },
1586 #endif
1587 { "help", 0, env_help, 0 },
1588 { "?", "Print help information", env_help, 0 },
1589 { 0 },
1590 };
1591
1592 static void
1593 env_help()
1594 {
1595 struct envlist *c;
1596
1597 for (c = EnvList; c->name; c++) {
1598 if (c->help) {
1599 if (*c->help)
1600 printf("%-15s %s\n", c->name, c->help);
1601 else
1602 printf("\n");
1603 }
1604 }
1605 }
1606
1607 static struct envlist *
1608 getenvcmd(name)
1609 char *name;
1610 {
1611 return (struct envlist *)
1612 genget(name, (char **) EnvList, sizeof(struct envlist));
1613 }
1614
1615 env_cmd(argc, argv)
1616 int argc;
1617 char *argv[];
1618 {
1619 struct envlist *c;
1620
1621 if (argc < 2) {
1622 fprintf(stderr,
1623 "Need an argument to 'environ' command. 'environ ?' for help.\n");
1624 return 0;
1625 }
1626 c = getenvcmd(argv[1]);
1627 if (c == 0) {
1628 fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\n",
1629 argv[1]);
1630 return 0;
1631 }
1632 if (Ambiguous(c)) {
1633 fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\n",
1634 argv[1]);
1635 return 0;
1636 }
1637 if (c->narg + 2 != argc) {
1638 fprintf(stderr,
1639 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\n",
1640 c->narg < argc + 2 ? "only " : "",
1641 c->narg, c->narg == 1 ? "" : "s", c->name);
1642 return 0;
1643 }
1644 (*c->handler)(argv[2], argv[3]);
1645 return 1;
1646 }
1647
1648 struct env_lst {
1649 struct env_lst *next; /* pointer to next structure */
1650 struct env_lst *prev; /* pointer to previous structure */
1651 unsigned char *var; /* pointer to variable name */
1652 unsigned char *value; /* pointer to variable value */
1653 int export; /* 1 -> export with default list of variables */
1654 int welldefined; /* A well defined variable */
1655 };
1656
1657 struct env_lst envlisthead;
1658
1659 struct env_lst *
1660 env_find(var)
1661 unsigned char *var;
1662 {
1663 register struct env_lst *ep;
1664
1665 for (ep = envlisthead.next; ep; ep = ep->next) {
1666 if (strcmp((char *)ep->var, (char *)var) == 0)
1667 return(ep);
1668 }
1669 return(NULL);
1670 }
1671
1672 void
1673 env_init()
1674 {
1675 extern char **environ;
1676 register char **epp, *cp;
1677 register struct env_lst *ep;
1678 extern char *strchr();
1679
1680 for (epp = environ; *epp; epp++) {
1681 if (cp = strchr(*epp, '=')) {
1682 *cp = '\0';
1683 ep = env_define((unsigned char *)*epp,
1684 (unsigned char *)cp+1);
1685 ep->export = 0;
1686 *cp = '=';
1687 }
1688 }
1689 /*
1690 * Special case for DISPLAY variable. If it is ":0.0" or
1691 * "unix:0.0", we have to get rid of "unix" and insert our
1692 * hostname.
1693 */
1694 if ((ep = env_find("DISPLAY"))
1695 && ((*ep->value == ':')
1696 || (strncmp((char *)ep->value, "unix:", 5) == 0))) {
1697 char hbuf[256+1];
1698 char *cp2 = strchr((char *)ep->value, ':');
1699
1700 gethostname(hbuf, 256);
1701 hbuf[256] = '\0';
1702 cp = (char *)malloc(strlen(hbuf) + strlen(cp2) + 1);
1703 sprintf((char *)cp, "%s%s", hbuf, cp2);
1704 free(ep->value);
1705 ep->value = (unsigned char *)cp;
1706 }
1707 /*
1708 * If USER is not defined, but LOGNAME is, then add
1709 * USER with the value from LOGNAME. By default, we
1710 * don't export the USER variable.
1711 */
1712 if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
1713 env_define((unsigned char *)"USER", ep->value);
1714 env_unexport((unsigned char *)"USER");
1715 }
1716 env_export((unsigned char *)"DISPLAY");
1717 env_export((unsigned char *)"PRINTER");
1718 }
1719
1720 struct env_lst *
1721 env_define(var, value)
1722 unsigned char *var, *value;
1723 {
1724 register struct env_lst *ep;
1725
1726 if (ep = env_find(var)) {
1727 if (ep->var)
1728 free(ep->var);
1729 if (ep->value)
1730 free(ep->value);
1731 } else {
1732 ep = (struct env_lst *)malloc(sizeof(struct env_lst));
1733 ep->next = envlisthead.next;
1734 envlisthead.next = ep;
1735 ep->prev = &envlisthead;
1736 if (ep->next)
1737 ep->next->prev = ep;
1738 }
1739 ep->welldefined = opt_welldefined(var);
1740 ep->export = 1;
1741 ep->var = (unsigned char *)strdup((char *)var);
1742 ep->value = (unsigned char *)strdup((char *)value);
1743 return(ep);
1744 }
1745
1746 void
1747 env_undefine(var)
1748 unsigned char *var;
1749 {
1750 register struct env_lst *ep;
1751
1752 if (ep = env_find(var)) {
1753 ep->prev->next = ep->next;
1754 if (ep->next)
1755 ep->next->prev = ep->prev;
1756 if (ep->var)
1757 free(ep->var);
1758 if (ep->value)
1759 free(ep->value);
1760 free(ep);
1761 }
1762 }
1763
1764 void
1765 env_export(var)
1766 unsigned char *var;
1767 {
1768 register struct env_lst *ep;
1769
1770 if (ep = env_find(var))
1771 ep->export = 1;
1772 }
1773
1774 void
1775 env_unexport(var)
1776 unsigned char *var;
1777 {
1778 register struct env_lst *ep;
1779
1780 if (ep = env_find(var))
1781 ep->export = 0;
1782 }
1783
1784 void
1785 env_send(var)
1786 unsigned char *var;
1787 {
1788 register struct env_lst *ep;
1789
1790 if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1791 #ifdef OLD_ENVIRON
1792 && my_state_is_wont(TELOPT_OLD_ENVIRON)
1793 #endif
1794 ) {
1795 fprintf(stderr,
1796 "Cannot send '%s': Telnet ENVIRON option not enabled\n",
1797 var);
1798 return;
1799 }
1800 ep = env_find(var);
1801 if (ep == 0) {
1802 fprintf(stderr, "Cannot send '%s': variable not defined\n",
1803 var);
1804 return;
1805 }
1806 env_opt_start_info();
1807 env_opt_add(ep->var);
1808 env_opt_end(0);
1809 }
1810
1811 void
1812 env_list()
1813 {
1814 register struct env_lst *ep;
1815
1816 for (ep = envlisthead.next; ep; ep = ep->next) {
1817 printf("%c %-20s %s\n", ep->export ? '*' : ' ',
1818 ep->var, ep->value);
1819 }
1820 }
1821
1822 unsigned char *
1823 env_default(init, welldefined)
1824 int init;
1825 {
1826 static struct env_lst *nep = NULL;
1827
1828 if (init) {
1829 nep = &envlisthead;
1830 return;
1831 }
1832 if (nep) {
1833 while (nep = nep->next) {
1834 if (nep->export && (nep->welldefined == welldefined))
1835 return(nep->var);
1836 }
1837 }
1838 return(NULL);
1839 }
1840
1841 unsigned char *
1842 env_getvalue(var)
1843 unsigned char *var;
1844 {
1845 register struct env_lst *ep;
1846
1847 if (ep = env_find(var))
1848 return(ep->value);
1849 return(NULL);
1850 }
1851
1852 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1853 void
1854 env_varval(what)
1855 unsigned char *what;
1856 {
1857 extern int old_env_var, old_env_value, env_auto;
1858 int len = strlen((char *)what);
1859
1860 if (len == 0)
1861 goto unknown;
1862
1863 if (strncasecmp((char *)what, "status", len) == 0) {
1864 if (env_auto)
1865 printf("%s%s", "VAR and VALUE are/will be ",
1866 "determined automatically\n");
1867 if (old_env_var == OLD_ENV_VAR)
1868 printf("VAR and VALUE set to correct definitions\n");
1869 else
1870 printf("VAR and VALUE definitions are reversed\n");
1871 } else if (strncasecmp((char *)what, "auto", len) == 0) {
1872 env_auto = 1;
1873 old_env_var = OLD_ENV_VALUE;
1874 old_env_value = OLD_ENV_VAR;
1875 } else if (strncasecmp((char *)what, "right", len) == 0) {
1876 env_auto = 0;
1877 old_env_var = OLD_ENV_VAR;
1878 old_env_value = OLD_ENV_VALUE;
1879 } else if (strncasecmp((char *)what, "wrong", len) == 0) {
1880 env_auto = 0;
1881 old_env_var = OLD_ENV_VALUE;
1882 old_env_value = OLD_ENV_VAR;
1883 } else {
1884 unknown:
1885 printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
1886 }
1887 }
1888 #endif
1889
1890 #if defined(AUTHENTICATION)
1891 /*
1892 * The AUTHENTICATE command.
1893 */
1894
1895 struct authlist {
1896 char *name;
1897 char *help;
1898 int (*handler)();
1899 int narg;
1900 };
1901
1902 extern int
1903 auth_enable P((char *)),
1904 auth_disable P((char *)),
1905 auth_status P((void));
1906 static int
1907 auth_help P((void));
1908
1909 struct authlist AuthList[] = {
1910 { "status", "Display current status of authentication information",
1911 auth_status, 0 },
1912 { "disable", "Disable an authentication type ('auth disable ?' for more)",
1913 auth_disable, 1 },
1914 { "enable", "Enable an authentication type ('auth enable ?' for more)",
1915 auth_enable, 1 },
1916 { "help", 0, auth_help, 0 },
1917 { "?", "Print help information", auth_help, 0 },
1918 { 0 },
1919 };
1920
1921 static int
1922 auth_help()
1923 {
1924 struct authlist *c;
1925
1926 for (c = AuthList; c->name; c++) {
1927 if (c->help) {
1928 if (*c->help)
1929 printf("%-15s %s\n", c->name, c->help);
1930 else
1931 printf("\n");
1932 }
1933 }
1934 return 0;
1935 }
1936
1937 auth_cmd(argc, argv)
1938 int argc;
1939 char *argv[];
1940 {
1941 struct authlist *c;
1942
1943 if (argc < 2) {
1944 fprintf(stderr,
1945 "Need an argument to 'auth' command. 'auth ?' for help.\n");
1946 return 0;
1947 }
1948
1949 c = (struct authlist *)
1950 genget(argv[1], (char **) AuthList, sizeof(struct authlist));
1951 if (c == 0) {
1952 fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\n",
1953 argv[1]);
1954 return 0;
1955 }
1956 if (Ambiguous(c)) {
1957 fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\n",
1958 argv[1]);
1959 return 0;
1960 }
1961 if (c->narg + 2 != argc) {
1962 fprintf(stderr,
1963 "Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\n",
1964 c->narg < argc + 2 ? "only " : "",
1965 c->narg, c->narg == 1 ? "" : "s", c->name);
1966 return 0;
1967 }
1968 return((*c->handler)(argv[2], argv[3]));
1969 }
1970 #endif
1971
1972
1973 #if defined(unix) && defined(TN3270)
1974 static void
1975 filestuff(fd)
1976 int fd;
1977 {
1978 int res;
1979
1980 #ifdef F_GETOWN
1981 setconnmode(0);
1982 res = fcntl(fd, F_GETOWN, 0);
1983 setcommandmode();
1984
1985 if (res == -1) {
1986 perror("fcntl");
1987 return;
1988 }
1989 printf("\tOwner is %d.\n", res);
1990 #endif
1991
1992 setconnmode(0);
1993 res = fcntl(fd, F_GETFL, 0);
1994 setcommandmode();
1995
1996 if (res == -1) {
1997 perror("fcntl");
1998 return;
1999 }
2000 #ifdef notdef
2001 printf("\tFlags are 0x%x: %s\n", res, decodeflags(res));
2002 #endif
2003 }
2004 #endif /* defined(unix) && defined(TN3270) */
2005
2006 /*
2007 * Print status about the connection.
2008 */
2009 /*ARGSUSED*/
2010 static
2011 status(argc, argv)
2012 int argc;
2013 char *argv[];
2014 {
2015 if (connected) {
2016 printf("Connected to %s.\n", hostname);
2017 if ((argc < 2) || strcmp(argv[1], "notmuch")) {
2018 int mode = getconnmode();
2019
2020 if (my_want_state_is_will(TELOPT_LINEMODE)) {
2021 printf("Operating with LINEMODE option\n");
2022 printf("%s line editing\n", (mode&MODE_EDIT) ? "Local" : "No");
2023 printf("%s catching of signals\n",
2024 (mode&MODE_TRAPSIG) ? "Local" : "No");
2025 slcstate();
2026 #ifdef KLUDGELINEMODE
2027 } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
2028 printf("Operating in obsolete linemode\n");
2029 #endif
2030 } else {
2031 printf("Operating in single character mode\n");
2032 if (localchars)
2033 printf("Catching signals locally\n");
2034 }
2035 printf("%s character echo\n", (mode&MODE_ECHO) ? "Local" : "Remote");
2036 if (my_want_state_is_will(TELOPT_LFLOW))
2037 printf("%s flow control\n", (mode&MODE_FLOW) ? "Local" : "No");
2038 }
2039 } else {
2040 printf("No connection.\n");
2041 }
2042 # if !defined(TN3270)
2043 printf("Escape character is '%s'.\n", control(escape));
2044 (void) fflush(stdout);
2045 # else /* !defined(TN3270) */
2046 if ((!In3270) && ((argc < 2) || strcmp(argv[1], "notmuch"))) {
2047 printf("Escape character is '%s'.\n", control(escape));
2048 }
2049 # if defined(unix)
2050 if ((argc >= 2) && !strcmp(argv[1], "everything")) {
2051 printf("SIGIO received %d time%s.\n",
2052 sigiocount, (sigiocount == 1)? "":"s");
2053 if (In3270) {
2054 printf("Process ID %d, process group %d.\n",
2055 getpid(), getpgrp(getpid()));
2056 printf("Terminal input:\n");
2057 filestuff(tin);
2058 printf("Terminal output:\n");
2059 filestuff(tout);
2060 printf("Network socket:\n");
2061 filestuff(net);
2062 }
2063 }
2064 if (In3270 && transcom) {
2065 printf("Transparent mode command is '%s'.\n", transcom);
2066 }
2067 # endif /* defined(unix) */
2068 (void) fflush(stdout);
2069 if (In3270) {
2070 return 0;
2071 }
2072 # endif /* defined(TN3270) */
2073 return 1;
2074 }
2075
2076 #ifdef SIGINFO
2077 /*
2078 * Function that gets called when SIGINFO is received.
2079 */
2080 ayt_status()
2081 {
2082 (void) call(status, "status", "notmuch", 0);
2083 }
2084 #endif
2085
2086 unsigned long inet_addr();
2087
2088 int
2089 tn(argc, argv)
2090 int argc;
2091 char *argv[];
2092 {
2093 register struct hostent *host = 0;
2094 struct sockaddr_in sin;
2095 struct servent *sp = 0;
2096 unsigned long temp;
2097 extern char *inet_ntoa();
2098 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2099 char *srp = 0, *strrchr();
2100 unsigned long sourceroute(), srlen;
2101 #endif
2102 char *cmd, *hostp = 0, *portp = 0, *user = 0;
2103
2104 /* clear the socket address prior to use */
2105 memset((char *)&sin, 0, sizeof(sin));
2106
2107 if (connected) {
2108 printf("?Already connected to %s\n", hostname);
2109 setuid(getuid());
2110 return 0;
2111 }
2112 if (argc < 2) {
2113 (void) strcpy(line, "open ");
2114 printf("(to) ");
2115 (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
2116 makeargv();
2117 argc = margc;
2118 argv = margv;
2119 }
2120 cmd = *argv;
2121 --argc; ++argv;
2122 while (argc) {
2123 if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
2124 goto usage;
2125 if (strcmp(*argv, "-l") == 0) {
2126 --argc; ++argv;
2127 if (argc == 0)
2128 goto usage;
2129 user = *argv++;
2130 --argc;
2131 continue;
2132 }
2133 if (strcmp(*argv, "-a") == 0) {
2134 --argc; ++argv;
2135 autologin = 1;
2136 continue;
2137 }
2138 if (hostp == 0) {
2139 hostp = *argv++;
2140 --argc;
2141 continue;
2142 }
2143 if (portp == 0) {
2144 portp = *argv++;
2145 --argc;
2146 continue;
2147 }
2148 usage:
2149 printf("usage: %s [-l user] [-a] host-name [port]\n", cmd);
2150 setuid(getuid());
2151 return 0;
2152 }
2153 if (hostp == 0)
2154 goto usage;
2155
2156 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2157 if (hostp[0] == '@' || hostp[0] == '!') {
2158 if ((hostname = strrchr(hostp, ':')) == NULL)
2159 hostname = strrchr(hostp, '@');
2160 hostname++;
2161 srp = 0;
2162 temp = sourceroute(hostp, &srp, &srlen);
2163 if (temp == 0) {
2164 herror(srp);
2165 setuid(getuid());
2166 return 0;
2167 } else if (temp == -1) {
2168 printf("Bad source route option: %s\n", hostp);
2169 setuid(getuid());
2170 return 0;
2171 } else {
2172 sin.sin_addr.s_addr = temp;
2173 sin.sin_family = AF_INET;
2174 }
2175 } else {
2176 #endif
2177 temp = inet_addr(hostp);
2178 if (temp != INADDR_NONE) {
2179 sin.sin_addr.s_addr = temp;
2180 sin.sin_family = AF_INET;
2181 (void) strcpy(_hostname, hostp);
2182 hostname = _hostname;
2183 } else {
2184 host = gethostbyname(hostp);
2185 if (host) {
2186 sin.sin_family = host->h_addrtype;
2187 #if defined(h_addr) /* In 4.3, this is a #define */
2188 memmove((caddr_t)&sin.sin_addr,
2189 host->h_addr_list[0], host->h_length);
2190 #else /* defined(h_addr) */
2191 memmove((caddr_t)&sin.sin_addr, host->h_addr, host->h_length);
2192 #endif /* defined(h_addr) */
2193 strncpy(_hostname, host->h_name, sizeof(_hostname));
2194 _hostname[sizeof(_hostname)-1] = '\0';
2195 hostname = _hostname;
2196 } else {
2197 herror(hostp);
2198 setuid(getuid());
2199 return 0;
2200 }
2201 }
2202 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2203 }
2204 #endif
2205 if (portp) {
2206 if (*portp == '-') {
2207 portp++;
2208 telnetport = 1;
2209 } else
2210 telnetport = 0;
2211 sin.sin_port = atoi(portp);
2212 if (sin.sin_port == 0) {
2213 sp = getservbyname(portp, "tcp");
2214 if (sp)
2215 sin.sin_port = sp->s_port;
2216 else {
2217 printf("%s: bad port number\n", portp);
2218 setuid(getuid());
2219 return 0;
2220 }
2221 } else {
2222 #if !defined(htons)
2223 u_short htons P((unsigned short));
2224 #endif /* !defined(htons) */
2225 sin.sin_port = htons(sin.sin_port);
2226 }
2227 } else {
2228 if (sp == 0) {
2229 sp = getservbyname("telnet", "tcp");
2230 if (sp == 0) {
2231 fprintf(stderr, "telnet: tcp/telnet: unknown service\n");
2232 setuid(getuid());
2233 return 0;
2234 }
2235 sin.sin_port = sp->s_port;
2236 }
2237 telnetport = 1;
2238 }
2239 printf("Trying %s...\n", inet_ntoa(sin.sin_addr));
2240 do {
2241 net = socket(AF_INET, SOCK_STREAM, 0);
2242 setuid(getuid());
2243 if (net < 0) {
2244 perror("telnet: socket");
2245 return 0;
2246 }
2247 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2248 if (srp && setsockopt(net, IPPROTO_IP, IP_OPTIONS, (char *)srp, srlen) < 0)
2249 perror("setsockopt (IP_OPTIONS)");
2250 #endif
2251 #if defined(IPPROTO_IP) && defined(IP_TOS)
2252 {
2253 # if defined(HAS_GETTOS)
2254 struct tosent *tp;
2255 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
2256 tos = tp->t_tos;
2257 # endif
2258 if (tos < 0)
2259 tos = IPTOS_LOWDELAY; /* Low Delay bit */
2260 if (tos
2261 && (setsockopt(net, IPPROTO_IP, IP_TOS,
2262 (char *)&tos, sizeof(int)) < 0)
2263 && (errno != ENOPROTOOPT))
2264 perror("telnet: setsockopt (IP_TOS) (ignored)");
2265 }
2266 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2267
2268 if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
2269 perror("setsockopt (SO_DEBUG)");
2270 }
2271
2272 if (connect(net, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
2273 #if defined(h_addr) /* In 4.3, this is a #define */
2274 if (host && host->h_addr_list[1]) {
2275 int oerrno = errno;
2276
2277 fprintf(stderr, "telnet: connect to address %s: ",
2278 inet_ntoa(sin.sin_addr));
2279 errno = oerrno;
2280 perror((char *)0);
2281 host->h_addr_list++;
2282 memmove((caddr_t)&sin.sin_addr,
2283 host->h_addr_list[0], host->h_length);
2284 (void) NetClose(net);
2285 continue;
2286 }
2287 #endif /* defined(h_addr) */
2288 perror("telnet: Unable to connect to remote host");
2289 return 0;
2290 }
2291 connected++;
2292 #if defined(AUTHENTICATION)
2293 auth_encrypt_connect(connected);
2294 #endif /* defined(AUTHENTICATION) */
2295 } while (connected == 0);
2296 cmdrc(hostp, hostname);
2297 if (autologin && user == NULL) {
2298 struct passwd *pw;
2299
2300 user = getenv("USER");
2301 if (user == NULL ||
2302 (pw = getpwnam(user)) && pw->pw_uid != getuid()) {
2303 if (pw = getpwuid(getuid()))
2304 user = pw->pw_name;
2305 else
2306 user = NULL;
2307 }
2308 }
2309 if (user) {
2310 env_define((unsigned char *)"USER", (unsigned char *)user);
2311 env_export((unsigned char *)"USER");
2312 }
2313 (void) call(status, "status", "notmuch", 0);
2314 if (setjmp(peerdied) == 0)
2315 telnet(user);
2316 (void) NetClose(net);
2317 ExitString("Connection closed by foreign host.\n",1);
2318 /*NOTREACHED*/
2319 }
2320
2321 #define HELPINDENT (sizeof ("connect"))
2322
2323 static char
2324 openhelp[] = "connect to a site",
2325 closehelp[] = "close current connection",
2326 logouthelp[] = "forcibly logout remote user and close the connection",
2327 quithelp[] = "exit telnet",
2328 statushelp[] = "print status information",
2329 helphelp[] = "print help information",
2330 sendhelp[] = "transmit special characters ('send ?' for more)",
2331 sethelp[] = "set operating parameters ('set ?' for more)",
2332 unsethelp[] = "unset operating parameters ('unset ?' for more)",
2333 togglestring[] ="toggle operating parameters ('toggle ?' for more)",
2334 slchelp[] = "change state of special charaters ('slc ?' for more)",
2335 displayhelp[] = "display operating parameters",
2336 #if defined(TN3270) && defined(unix)
2337 transcomhelp[] = "specify Unix command for transparent mode pipe",
2338 #endif /* defined(TN3270) && defined(unix) */
2339 #if defined(AUTHENTICATION)
2340 authhelp[] = "turn on (off) authentication ('auth ?' for more)",
2341 #endif
2342 #if defined(unix)
2343 zhelp[] = "suspend telnet",
2344 #endif /* defined(unix) */
2345 shellhelp[] = "invoke a subshell",
2346 envhelp[] = "change environment variables ('environ ?' for more)",
2347 modestring[] = "try to enter line or character mode ('mode ?' for more)";
2348
2349 static int help();
2350
2351 static Command cmdtab[] = {
2352 { "close", closehelp, bye, 1 },
2353 { "logout", logouthelp, logout, 1 },
2354 { "display", displayhelp, display, 0 },
2355 { "mode", modestring, modecmd, 0 },
2356 { "open", openhelp, tn, 0 },
2357 { "quit", quithelp, quit, 0 },
2358 { "send", sendhelp, sendcmd, 0 },
2359 { "set", sethelp, setcmd, 0 },
2360 { "unset", unsethelp, unsetcmd, 0 },
2361 { "status", statushelp, status, 0 },
2362 { "toggle", togglestring, toggle, 0 },
2363 { "slc", slchelp, slccmd, 0 },
2364 #if defined(TN3270) && defined(unix)
2365 { "transcom", transcomhelp, settranscom, 0 },
2366 #endif /* defined(TN3270) && defined(unix) */
2367 #if defined(AUTHENTICATION)
2368 { "auth", authhelp, auth_cmd, 0 },
2369 #endif
2370 #if defined(unix)
2371 { "z", zhelp, suspend, 0 },
2372 #endif /* defined(unix) */
2373 #if defined(TN3270)
2374 { "!", shellhelp, shell, 1 },
2375 #else
2376 { "!", shellhelp, shell, 0 },
2377 #endif
2378 { "environ", envhelp, env_cmd, 0 },
2379 { "?", helphelp, help, 0 },
2380 0
2381 };
2382
2383 static char crmodhelp[] = "deprecated command -- use 'toggle crmod' instead";
2384 static char escapehelp[] = "deprecated command -- use 'set escape' instead";
2385
2386 static Command cmdtab2[] = {
2387 { "help", 0, help, 0 },
2388 { "escape", escapehelp, setescape, 0 },
2389 { "crmod", crmodhelp, togcrmod, 0 },
2390 0
2391 };
2392
2393
2394 /*
2395 * Call routine with argc, argv set from args (terminated by 0).
2396 */
2397
2398 /*VARARGS1*/
2399 static
2400 call(va_alist)
2401 va_dcl
2402 {
2403 va_list ap;
2404 typedef int (*intrtn_t)();
2405 intrtn_t routine;
2406 char *args[100];
2407 int argno = 0;
2408
2409 va_start(ap);
2410 routine = (va_arg(ap, intrtn_t));
2411 while ((args[argno++] = va_arg(ap, char *)) != 0) {
2412 ;
2413 }
2414 va_end(ap);
2415 return (*routine)(argno-1, args);
2416 }
2417
2418
2419 static Command *
2420 getcmd(name)
2421 char *name;
2422 {
2423 Command *cm;
2424
2425 if (cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command)))
2426 return cm;
2427 return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
2428 }
2429
2430 void
2431 command(top, tbuf, cnt)
2432 int top;
2433 char *tbuf;
2434 int cnt;
2435 {
2436 register Command *c;
2437
2438 setcommandmode();
2439 if (!top) {
2440 putchar('\n');
2441 #if defined(unix)
2442 } else {
2443 (void) signal(SIGINT, SIG_DFL);
2444 (void) signal(SIGQUIT, SIG_DFL);
2445 #endif /* defined(unix) */
2446 }
2447 for (;;) {
2448 if (rlogin == _POSIX_VDISABLE)
2449 printf("%s> ", prompt);
2450 if (tbuf) {
2451 register char *cp;
2452 cp = line;
2453 while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
2454 cnt--;
2455 tbuf = 0;
2456 if (cp == line || *--cp != '\n' || cp == line)
2457 goto getline;
2458 *cp = '\0';
2459 if (rlogin == _POSIX_VDISABLE)
2460 printf("%s\n", line);
2461 } else {
2462 getline:
2463 if (rlogin != _POSIX_VDISABLE)
2464 printf("%s> ", prompt);
2465 if (fgets(line, sizeof(line), stdin) == NULL) {
2466 if (feof(stdin) || ferror(stdin)) {
2467 (void) quit();
2468 /*NOTREACHED*/
2469 }
2470 break;
2471 }
2472 }
2473 if (line[0] == 0)
2474 break;
2475 makeargv();
2476 if (margv[0] == 0) {
2477 break;
2478 }
2479 c = getcmd(margv[0]);
2480 if (Ambiguous(c)) {
2481 printf("?Ambiguous command\n");
2482 continue;
2483 }
2484 if (c == 0) {
2485 printf("?Invalid command\n");
2486 continue;
2487 }
2488 if (c->needconnect && !connected) {
2489 printf("?Need to be connected first.\n");
2490 continue;
2491 }
2492 if ((*c->handler)(margc, margv)) {
2493 break;
2494 }
2495 }
2496 if (!top) {
2497 if (!connected) {
2498 longjmp(toplevel, 1);
2499 /*NOTREACHED*/
2500 }
2501 #if defined(TN3270)
2502 if (shell_active == 0) {
2503 setconnmode(0);
2504 }
2505 #else /* defined(TN3270) */
2506 setconnmode(0);
2507 #endif /* defined(TN3270) */
2508 }
2509 }
2510
2511 /*
2513 * Help command.
2514 */
2515 static
2516 help(argc, argv)
2517 int argc;
2518 char *argv[];
2519 {
2520 register Command *c;
2521
2522 if (argc == 1) {
2523 printf("Commands may be abbreviated. Commands are:\n\n");
2524 for (c = cmdtab; c->name; c++)
2525 if (c->help) {
2526 printf("%-*s\t%s\n", HELPINDENT, c->name,
2527 c->help);
2528 }
2529 return 0;
2530 }
2531 while (--argc > 0) {
2532 register char *arg;
2533 arg = *++argv;
2534 c = getcmd(arg);
2535 if (Ambiguous(c))
2536 printf("?Ambiguous help command %s\n", arg);
2537 else if (c == (Command *)0)
2538 printf("?Invalid help command %s\n", arg);
2539 else
2540 printf("%s\n", c->help);
2541 }
2542 return 0;
2543 }
2544
2545 static char *rcname = 0;
2546 static char rcbuf[128];
2547
2548 cmdrc(m1, m2)
2549 char *m1, *m2;
2550 {
2551 register Command *c;
2552 FILE *rcfile;
2553 int gotmachine = 0;
2554 int l1 = strlen(m1);
2555 int l2 = strlen(m2);
2556 char m1save[64];
2557
2558 if (skiprc)
2559 return;
2560
2561 strcpy(m1save, m1);
2562 m1 = m1save;
2563
2564 if (rcname == 0) {
2565 rcname = getenv("HOME");
2566 if (rcname)
2567 strcpy(rcbuf, rcname);
2568 else
2569 rcbuf[0] = '\0';
2570 strcat(rcbuf, "/.telnetrc");
2571 rcname = rcbuf;
2572 }
2573
2574 if ((rcfile = fopen(rcname, "r")) == 0) {
2575 return;
2576 }
2577
2578 for (;;) {
2579 if (fgets(line, sizeof(line), rcfile) == NULL)
2580 break;
2581 if (line[0] == 0)
2582 break;
2583 if (line[0] == '#')
2584 continue;
2585 if (gotmachine) {
2586 if (!isspace(line[0]))
2587 gotmachine = 0;
2588 }
2589 if (gotmachine == 0) {
2590 if (isspace(line[0]))
2591 continue;
2592 if (strncasecmp(line, m1, l1) == 0)
2593 strncpy(line, &line[l1], sizeof(line) - l1);
2594 else if (strncasecmp(line, m2, l2) == 0)
2595 strncpy(line, &line[l2], sizeof(line) - l2);
2596 else if (strncasecmp(line, "DEFAULT", 7) == 0)
2597 strncpy(line, &line[7], sizeof(line) - 7);
2598 else
2599 continue;
2600 if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
2601 continue;
2602 gotmachine = 1;
2603 }
2604 makeargv();
2605 if (margv[0] == 0)
2606 continue;
2607 c = getcmd(margv[0]);
2608 if (Ambiguous(c)) {
2609 printf("?Ambiguous command: %s\n", margv[0]);
2610 continue;
2611 }
2612 if (c == 0) {
2613 printf("?Invalid command: %s\n", margv[0]);
2614 continue;
2615 }
2616 /*
2617 * This should never happen...
2618 */
2619 if (c->needconnect && !connected) {
2620 printf("?Need to be connected first for %s.\n", margv[0]);
2621 continue;
2622 }
2623 (*c->handler)(margc, margv);
2624 }
2625 fclose(rcfile);
2626 }
2627
2628 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2629
2630 /*
2631 * Source route is handed in as
2632 * [!]@hop1 (at) hop2...[@|:]dst
2633 * If the leading ! is present, it is a
2634 * strict source route, otherwise it is
2635 * assmed to be a loose source route.
2636 *
2637 * We fill in the source route option as
2638 * hop1,hop2,hop3...dest
2639 * and return a pointer to hop1, which will
2640 * be the address to connect() to.
2641 *
2642 * Arguments:
2643 * arg: pointer to route list to decipher
2644 *
2645 * cpp: If *cpp is not equal to NULL, this is a
2646 * pointer to a pointer to a character array
2647 * that should be filled in with the option.
2648 *
2649 * lenp: pointer to an integer that contains the
2650 * length of *cpp if *cpp != NULL.
2651 *
2652 * Return values:
2653 *
2654 * Returns the address of the host to connect to. If the
2655 * return value is -1, there was a syntax error in the
2656 * option, either unknown characters, or too many hosts.
2657 * If the return value is 0, one of the hostnames in the
2658 * path is unknown, and *cpp is set to point to the bad
2659 * hostname.
2660 *
2661 * *cpp: If *cpp was equal to NULL, it will be filled
2662 * in with a pointer to our static area that has
2663 * the option filled in. This will be 32bit aligned.
2664 *
2665 * *lenp: This will be filled in with how long the option
2666 * pointed to by *cpp is.
2667 *
2668 */
2669 unsigned long
2670 sourceroute(arg, cpp, lenp)
2671 char *arg;
2672 char **cpp;
2673 int *lenp;
2674 {
2675 static char lsr[44];
2676 #ifdef sysV88
2677 static IOPTN ipopt;
2678 #endif
2679 char *cp, *cp2, *lsrp, *lsrep;
2680 register int tmp;
2681 struct in_addr sin_addr;
2682 register struct hostent *host = 0;
2683 register char c;
2684
2685 /*
2686 * Verify the arguments, and make sure we have
2687 * at least 7 bytes for the option.
2688 */
2689 if (cpp == NULL || lenp == NULL)
2690 return((unsigned long)-1);
2691 if (*cpp != NULL && *lenp < 7)
2692 return((unsigned long)-1);
2693 /*
2694 * Decide whether we have a buffer passed to us,
2695 * or if we need to use our own static buffer.
2696 */
2697 if (*cpp) {
2698 lsrp = *cpp;
2699 lsrep = lsrp + *lenp;
2700 } else {
2701 *cpp = lsrp = lsr;
2702 lsrep = lsrp + 44;
2703 }
2704
2705 cp = arg;
2706
2707 /*
2708 * Next, decide whether we have a loose source
2709 * route or a strict source route, and fill in
2710 * the begining of the option.
2711 */
2712 #ifndef sysV88
2713 if (*cp == '!') {
2714 cp++;
2715 *lsrp++ = IPOPT_SSRR;
2716 } else
2717 *lsrp++ = IPOPT_LSRR;
2718 #else
2719 if (*cp == '!') {
2720 cp++;
2721 ipopt.io_type = IPOPT_SSRR;
2722 } else
2723 ipopt.io_type = IPOPT_LSRR;
2724 #endif
2725
2726 if (*cp != '@')
2727 return((unsigned long)-1);
2728
2729 #ifndef sysV88
2730 lsrp++; /* skip over length, we'll fill it in later */
2731 *lsrp++ = 4;
2732 #endif
2733
2734 cp++;
2735
2736 sin_addr.s_addr = 0;
2737
2738 for (c = 0;;) {
2739 if (c == ':')
2740 cp2 = 0;
2741 else for (cp2 = cp; c = *cp2; cp2++) {
2742 if (c == ',') {
2743 *cp2++ = '\0';
2744 if (*cp2 == '@')
2745 cp2++;
2746 } else if (c == '@') {
2747 *cp2++ = '\0';
2748 } else if (c == ':') {
2749 *cp2++ = '\0';
2750 } else
2751 continue;
2752 break;
2753 }
2754 if (!c)
2755 cp2 = 0;
2756
2757 if ((tmp = inet_addr(cp)) != INADDR_NONE) {
2758 sin_addr.s_addr = tmp;
2759 } else if (host = gethostbyname(cp)) {
2760 #if defined(h_addr)
2761 memmove((caddr_t)&sin_addr,
2762 host->h_addr_list[0], host->h_length);
2763 #else
2764 memmove((caddr_t)&sin_addr, host->h_addr, host->h_length);
2765 #endif
2766 } else {
2767 *cpp = cp;
2768 return(0);
2769 }
2770 memmove(lsrp, (char *)&sin_addr, 4);
2771 lsrp += 4;
2772 if (cp2)
2773 cp = cp2;
2774 else
2775 break;
2776 /*
2777 * Check to make sure there is space for next address
2778 */
2779 if (lsrp + 4 > lsrep)
2780 return((unsigned long)-1);
2781 }
2782 #ifndef sysV88
2783 if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
2784 *cpp = 0;
2785 *lenp = 0;
2786 return((unsigned long)-1);
2787 }
2788 *lsrp++ = IPOPT_NOP; /* 32 bit word align it */
2789 *lenp = lsrp - *cpp;
2790 #else
2791 ipopt.io_len = lsrp - *cpp;
2792 if (ipopt.io_len <= 5) { /* Is 3 better ? */
2793 *cpp = 0;
2794 *lenp = 0;
2795 return((unsigned long)-1);
2796 }
2797 *lenp = sizeof(ipopt);
2798 *cpp = (char *) &ipopt;
2799 #endif
2800 return(sin_addr.s_addr);
2801 }
2802 #endif
2803