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