commands.c revision 1.24 1 /* $NetBSD: commands.c,v 1.24 1998/07/26 22:35:48 mycroft 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.24 1998/07/26 22:35:48 mycroft 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 hbuf[sizeof(hbuf) - 1] = '\0';
1748 cp = (char *)malloc(strlen(hbuf) + strlen(cp2) + 1);
1749 sprintf((char *)cp, "%s%s", hbuf, cp2);
1750 free(ep->value);
1751 ep->value = (unsigned char *)cp;
1752 }
1753 /*
1754 * If USER is not defined, but LOGNAME is, then add
1755 * USER with the value from LOGNAME. By default, we
1756 * don't export the USER variable.
1757 */
1758 if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
1759 env_define((unsigned char *)"USER", ep->value);
1760 env_unexport((unsigned char *)"USER", NULL);
1761 }
1762 env_export((unsigned char *)"DISPLAY", NULL);
1763 env_export((unsigned char *)"PRINTER", NULL);
1764 }
1765
1766 struct env_lst *
1767 env_define(var, value)
1768 unsigned char *var, *value;
1769 {
1770 register struct env_lst *ep;
1771
1772 if ((ep = env_find(var)) != NULL) {
1773 if (ep->var)
1774 free(ep->var);
1775 if (ep->value)
1776 free(ep->value);
1777 } else {
1778 ep = (struct env_lst *)malloc(sizeof(struct env_lst));
1779 ep->next = envlisthead.next;
1780 envlisthead.next = ep;
1781 ep->prev = &envlisthead;
1782 if (ep->next)
1783 ep->next->prev = ep;
1784 }
1785 ep->welldefined = opt_welldefined(var);
1786 ep->export = 1;
1787 ep->var = (unsigned char *)strdup((char *)var);
1788 ep->value = (unsigned char *)strdup((char *)value);
1789 return(ep);
1790 }
1791
1792 struct env_lst *
1793 env_undefine(var, d)
1794 unsigned char *var;
1795 unsigned char *d;
1796 {
1797 register struct env_lst *ep;
1798
1799 if ((ep = env_find(var)) != NULL) {
1800 ep->prev->next = ep->next;
1801 if (ep->next)
1802 ep->next->prev = ep->prev;
1803 if (ep->var)
1804 free(ep->var);
1805 if (ep->value)
1806 free(ep->value);
1807 free(ep);
1808 }
1809 return NULL;
1810 }
1811
1812 struct env_lst *
1813 env_export(var, d)
1814 unsigned char *var;
1815 unsigned char *d;
1816 {
1817 register struct env_lst *ep;
1818
1819 if ((ep = env_find(var)) != NULL)
1820 ep->export = 1;
1821 return NULL;
1822 }
1823
1824 struct env_lst *
1825 env_unexport(var, d)
1826 unsigned char *var;
1827 unsigned char *d;
1828 {
1829 register struct env_lst *ep;
1830
1831 if ((ep = env_find(var)) != NULL)
1832 ep->export = 0;
1833 return NULL;
1834 }
1835
1836 struct env_lst *
1837 env_send(var, d)
1838 unsigned char *var;
1839 unsigned char *d;
1840 {
1841 register struct env_lst *ep;
1842
1843 if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1844 #ifdef OLD_ENVIRON
1845 && my_state_is_wont(TELOPT_OLD_ENVIRON)
1846 #endif
1847 ) {
1848 fprintf(stderr,
1849 "Cannot send '%s': Telnet ENVIRON option not enabled\n",
1850 var);
1851 return NULL;
1852 }
1853 ep = env_find(var);
1854 if (ep == 0) {
1855 fprintf(stderr, "Cannot send '%s': variable not defined\n",
1856 var);
1857 return NULL;
1858 }
1859 env_opt_start_info();
1860 env_opt_add(ep->var);
1861 env_opt_end(0);
1862 return NULL;
1863 }
1864
1865 struct env_lst *
1866 env_list(d1, d2)
1867 unsigned char *d1, *d2;
1868 {
1869 register struct env_lst *ep;
1870
1871 for (ep = envlisthead.next; ep; ep = ep->next) {
1872 printf("%c %-20s %s\n", ep->export ? '*' : ' ',
1873 ep->var, ep->value);
1874 }
1875 return NULL;
1876 }
1877
1878 unsigned char *
1879 env_default(init, welldefined)
1880 int init;
1881 int welldefined;
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;
2158 const char *user = 0;
2159 #ifdef __GNUC__ /* Avoid vfork clobbering */
2160 (void) &user;
2161 #endif
2162
2163 /* clear the socket address prior to use */
2164 memset((char *)&sin, 0, sizeof(sin));
2165
2166 if (connected) {
2167 printf("?Already connected to %s\n", hostname);
2168 setuid(getuid());
2169 return 0;
2170 }
2171 if (argc < 2) {
2172 (void) strcpy(line, "open ");
2173 printf("(to) ");
2174 (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
2175 makeargv();
2176 argc = margc;
2177 argv = margv;
2178 }
2179 cmd = *argv;
2180 --argc; ++argv;
2181 while (argc) {
2182 if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
2183 goto usage;
2184 if (strcmp(*argv, "-l") == 0) {
2185 --argc; ++argv;
2186 if (argc == 0)
2187 goto usage;
2188 user = *argv++;
2189 --argc;
2190 continue;
2191 }
2192 if (strcmp(*argv, "-a") == 0) {
2193 --argc; ++argv;
2194 autologin = 1;
2195 continue;
2196 }
2197 if (hostp == 0) {
2198 hostp = *argv++;
2199 --argc;
2200 continue;
2201 }
2202 if (portp == 0) {
2203 portp = *argv++;
2204 --argc;
2205 continue;
2206 }
2207 usage:
2208 printf("usage: %s [-l user] [-a] host-name [port]\n", cmd);
2209 setuid(getuid());
2210 return 0;
2211 }
2212 if (hostp == 0)
2213 goto usage;
2214
2215 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2216 if (hostp[0] == '@' || hostp[0] == '!') {
2217 if ((hostname = strrchr(hostp, ':')) == NULL)
2218 hostname = strrchr(hostp, '@');
2219 hostname++;
2220 srp = 0;
2221 temp = sourceroute(hostp, &srp, &srlen);
2222 if (temp == 0) {
2223 herror(srp);
2224 setuid(getuid());
2225 return 0;
2226 } else if (temp == -1) {
2227 printf("Bad source route option: %s\n", hostp);
2228 setuid(getuid());
2229 return 0;
2230 } else {
2231 sin.sin_addr.s_addr = temp;
2232 sin.sin_family = AF_INET;
2233 }
2234 } else {
2235 #endif
2236 temp = inet_addr(hostp);
2237 if (temp != INADDR_NONE) {
2238 sin.sin_addr.s_addr = temp;
2239 sin.sin_family = AF_INET;
2240 host = gethostbyaddr((char *)&temp, sizeof(temp), AF_INET);
2241 if (host)
2242 (void) strcpy(_hostname, host->h_name);
2243 else
2244 (void) strcpy(_hostname, hostp);
2245 hostname = _hostname;
2246 } else {
2247 host = gethostbyname(hostp);
2248 if (host) {
2249 sin.sin_family = host->h_addrtype;
2250 #if defined(h_addr) /* In 4.3, this is a #define */
2251 memmove((caddr_t)&sin.sin_addr,
2252 host->h_addr_list[0], host->h_length);
2253 #else /* defined(h_addr) */
2254 memmove((caddr_t)&sin.sin_addr, host->h_addr, host->h_length);
2255 #endif /* defined(h_addr) */
2256 strncpy(_hostname, host->h_name, sizeof(_hostname));
2257 _hostname[sizeof(_hostname)-1] = '\0';
2258 hostname = _hostname;
2259 } else {
2260 herror(hostp);
2261 setuid(getuid());
2262 return 0;
2263 }
2264 }
2265 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2266 }
2267 #endif
2268 if (portp) {
2269 if (*portp == '-') {
2270 portp++;
2271 telnetport = 1;
2272 } else
2273 telnetport = 0;
2274 sin.sin_port = atoi(portp);
2275 if (sin.sin_port == 0) {
2276 sp = getservbyname(portp, "tcp");
2277 if (sp)
2278 sin.sin_port = sp->s_port;
2279 else {
2280 printf("%s: bad port number\n", portp);
2281 setuid(getuid());
2282 return 0;
2283 }
2284 } else {
2285 #if !defined(htons)
2286 u_short htons P((unsigned short));
2287 #endif /* !defined(htons) */
2288 sin.sin_port = htons(sin.sin_port);
2289 }
2290 } else {
2291 if (sp == 0) {
2292 sp = getservbyname("telnet", "tcp");
2293 if (sp == 0) {
2294 fprintf(stderr, "telnet: tcp/telnet: unknown service\n");
2295 setuid(getuid());
2296 return 0;
2297 }
2298 sin.sin_port = sp->s_port;
2299 }
2300 telnetport = 1;
2301 }
2302 printf("Trying %s...\n", inet_ntoa(sin.sin_addr));
2303 do {
2304 net = socket(AF_INET, SOCK_STREAM, 0);
2305 setuid(getuid());
2306 if (net < 0) {
2307 perror("telnet: socket");
2308 return 0;
2309 }
2310 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2311 if (srp && setsockopt(net, IPPROTO_IP, IP_OPTIONS, (char *)srp, srlen) < 0)
2312 perror("setsockopt (IP_OPTIONS)");
2313 #endif
2314 #if defined(IPPROTO_IP) && defined(IP_TOS)
2315 {
2316 # if defined(HAS_GETTOS)
2317 struct tosent *tp;
2318 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
2319 tos = tp->t_tos;
2320 # endif
2321 if (tos < 0)
2322 tos = IPTOS_LOWDELAY; /* Low Delay bit */
2323 if (tos
2324 && (setsockopt(net, IPPROTO_IP, IP_TOS,
2325 (char *)&tos, sizeof(int)) < 0)
2326 && (errno != ENOPROTOOPT))
2327 perror("telnet: setsockopt (IP_TOS) (ignored)");
2328 }
2329 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2330
2331 if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
2332 perror("setsockopt (SO_DEBUG)");
2333 }
2334
2335 if (connect(net, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
2336 #if defined(h_addr) /* In 4.3, this is a #define */
2337 if (host && host->h_addr_list[1]) {
2338 int oerrno = errno;
2339
2340 fprintf(stderr, "telnet: connect to address %s: ",
2341 inet_ntoa(sin.sin_addr));
2342 errno = oerrno;
2343 perror((char *)0);
2344 host->h_addr_list++;
2345 memmove((caddr_t)&sin.sin_addr,
2346 host->h_addr_list[0], host->h_length);
2347 (void) NetClose(net);
2348 continue;
2349 }
2350 #endif /* defined(h_addr) */
2351 perror("telnet: Unable to connect to remote host");
2352 return 0;
2353 }
2354 connected++;
2355 #if defined(AUTHENTICATION)
2356 auth_encrypt_connect(connected);
2357 #endif /* defined(AUTHENTICATION) */
2358 } while (connected == 0);
2359 cmdrc(hostp, hostname);
2360 if (autologin && user == NULL) {
2361 struct passwd *pw;
2362
2363 user = getenv("USER");
2364 if (user == NULL ||
2365 ((pw = getpwnam(user)) && pw->pw_uid != getuid())) {
2366 if ((pw = getpwuid(getuid())) != NULL)
2367 user = pw->pw_name;
2368 else
2369 user = NULL;
2370 }
2371 }
2372 if (user) {
2373 env_define((unsigned char *)"USER", (unsigned char *)user);
2374 env_export((unsigned char *)"USER", NULL);
2375 }
2376 (void) call(status, "status", "notmuch", 0);
2377 if (setjmp(peerdied) == 0)
2378 telnet(user);
2379 (void) NetClose(net);
2380 ExitString("Connection closed by foreign host.\n",1);
2381 /*NOTREACHED*/
2382 }
2383
2384 #define HELPINDENT ((int)sizeof ("connect"))
2385
2386 static char
2387 openhelp[] = "connect to a site",
2388 closehelp[] = "close current connection",
2389 logouthelp[] = "forcibly logout remote user and close the connection",
2390 quithelp[] = "exit telnet",
2391 statushelp[] = "print status information",
2392 helphelp[] = "print help information",
2393 sendhelp[] = "transmit special characters ('send ?' for more)",
2394 sethelp[] = "set operating parameters ('set ?' for more)",
2395 unsethelp[] = "unset operating parameters ('unset ?' for more)",
2396 togglestring[] ="toggle operating parameters ('toggle ?' for more)",
2397 slchelp[] = "change state of special charaters ('slc ?' for more)",
2398 displayhelp[] = "display operating parameters",
2399 #if defined(TN3270) && defined(unix)
2400 transcomhelp[] = "specify Unix command for transparent mode pipe",
2401 #endif /* defined(TN3270) && defined(unix) */
2402 #if defined(AUTHENTICATION)
2403 authhelp[] = "turn on (off) authentication ('auth ?' for more)",
2404 #endif
2405 #if defined(unix)
2406 zhelp[] = "suspend telnet",
2407 #endif /* defined(unix) */
2408 shellhelp[] = "invoke a subshell",
2409 envhelp[] = "change environment variables ('environ ?' for more)",
2410 modestring[] = "try to enter line or character mode ('mode ?' for more)";
2411
2412 static Command cmdtab[] = {
2413 { "close", closehelp, bye, 1 },
2414 { "logout", logouthelp, logout, 1 },
2415 { "display", displayhelp, display, 0 },
2416 { "mode", modestring, modecmd, 0 },
2417 { "open", openhelp, tn, 0 },
2418 { "quit", quithelp, quit, 0 },
2419 { "send", sendhelp, sendcmd, 0 },
2420 { "set", sethelp, setcmd, 0 },
2421 { "unset", unsethelp, unsetcmd, 0 },
2422 { "status", statushelp, status, 0 },
2423 { "toggle", togglestring, toggle, 0 },
2424 { "slc", slchelp, slccmd, 0 },
2425 #if defined(TN3270) && defined(unix)
2426 { "transcom", transcomhelp, settranscom, 0 },
2427 #endif /* defined(TN3270) && defined(unix) */
2428 #if defined(AUTHENTICATION)
2429 { "auth", authhelp, auth_cmd, 0 },
2430 #endif
2431 #if defined(unix)
2432 { "z", zhelp, suspend, 0 },
2433 #endif /* defined(unix) */
2434 #if defined(TN3270)
2435 { "!", shellhelp, shell, 1 },
2436 #else
2437 { "!", shellhelp, shell, 0 },
2438 #endif
2439 { "environ", envhelp, env_cmd, 0 },
2440 { "?", helphelp, help, 0 },
2441 { NULL, NULL, NULL, 0 }
2442 };
2443
2444 static char crmodhelp[] = "deprecated command -- use 'toggle crmod' instead";
2445 static char escapehelp[] = "deprecated command -- use 'set escape' instead";
2446
2447 static Command cmdtab2[] = {
2448 { "help", 0, help, 0 },
2449 { "escape", escapehelp, setescape, 0 },
2450 { "crmod", crmodhelp, togcrmod, 0 },
2451 { NULL, NULL, NULL, 0 }
2452 };
2453
2454
2455 /*
2456 * Call routine with argc, argv set from args (terminated by 0).
2457 */
2458
2459 /*VARARGS1*/
2460 static int
2461 #ifdef __STDC__
2462 call(intrtn_t routine, ...)
2463 #else
2464 call(va_alist)
2465 va_dcl
2466 #endif
2467 {
2468 va_list ap;
2469 char *args[100];
2470 int argno = 0;
2471 #ifndef __STDC__
2472 intrtn_t routine;
2473
2474 va_start(ap);
2475 routine = (va_arg(ap, intrtn_t));
2476 #else
2477 va_start(ap, routine);
2478 #endif
2479
2480 while ((args[argno++] = va_arg(ap, char *)) != 0) {
2481 ;
2482 }
2483 va_end(ap);
2484 return (*routine)(argno-1, args);
2485 }
2486
2487
2488 static Command *
2489 getcmd(name)
2490 char *name;
2491 {
2492 Command *cm;
2493
2494 if ((cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command))) != NULL)
2495 return cm;
2496 return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
2497 }
2498
2499 void
2500 command(top, tbuf, cnt)
2501 int top;
2502 char *tbuf;
2503 int cnt;
2504 {
2505 register Command *c;
2506
2507 setcommandmode();
2508 if (!top) {
2509 putchar('\n');
2510 #if defined(unix)
2511 } else {
2512 (void) signal(SIGINT, SIG_DFL);
2513 (void) signal(SIGQUIT, SIG_DFL);
2514 #endif /* defined(unix) */
2515 }
2516 for (;;) {
2517 if (rlogin == _POSIX_VDISABLE)
2518 printf("%s> ", prompt);
2519 if (tbuf) {
2520 register char *cp;
2521 cp = line;
2522 while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
2523 cnt--;
2524 tbuf = 0;
2525 if (cp == line || *--cp != '\n' || cp == line)
2526 goto getline;
2527 *cp = '\0';
2528 if (rlogin == _POSIX_VDISABLE)
2529 printf("%s\n", line);
2530 } else {
2531 getline:
2532 if (rlogin != _POSIX_VDISABLE)
2533 printf("%s> ", prompt);
2534 if (fgets(line, sizeof(line), stdin) == NULL) {
2535 if (feof(stdin) || ferror(stdin)) {
2536 (void) quit(0, NULL);
2537 /*NOTREACHED*/
2538 }
2539 break;
2540 }
2541 }
2542 if (line[0] == 0)
2543 break;
2544 makeargv();
2545 if (margv[0] == 0) {
2546 break;
2547 }
2548 c = getcmd(margv[0]);
2549 if (Ambiguous(c)) {
2550 printf("?Ambiguous command\n");
2551 continue;
2552 }
2553 if (c == 0) {
2554 printf("?Invalid command\n");
2555 continue;
2556 }
2557 if (c->needconnect && !connected) {
2558 printf("?Need to be connected first.\n");
2559 continue;
2560 }
2561 if ((*c->handler)(margc, margv)) {
2562 break;
2563 }
2564 }
2565 if (!top) {
2566 if (!connected) {
2567 longjmp(toplevel, 1);
2568 /*NOTREACHED*/
2569 }
2570 #if defined(TN3270)
2571 if (shell_active == 0) {
2572 setconnmode(0);
2573 }
2574 #else /* defined(TN3270) */
2575 setconnmode(0);
2576 #endif /* defined(TN3270) */
2577 }
2578 }
2579
2580 /*
2582 * Help command.
2583 */
2584 static int
2585 help(argc, argv)
2586 int argc;
2587 char *argv[];
2588 {
2589 register Command *c;
2590
2591 if (argc == 1) {
2592 printf("Commands may be abbreviated. Commands are:\n\n");
2593 for (c = cmdtab; c->name; c++)
2594 if (c->help) {
2595 printf("%-*s\t%s\n", HELPINDENT, c->name,
2596 c->help);
2597 }
2598 return 0;
2599 }
2600 while (--argc > 0) {
2601 register char *arg;
2602 arg = *++argv;
2603 c = getcmd(arg);
2604 if (Ambiguous(c))
2605 printf("?Ambiguous help command %s\n", arg);
2606 else if (c == (Command *)0)
2607 printf("?Invalid help command %s\n", arg);
2608 else
2609 printf("%s\n", c->help);
2610 }
2611 return 0;
2612 }
2613
2614 static char *rcname = 0;
2615 static char rcbuf[128];
2616
2617 void
2618 cmdrc(m1, m2)
2619 const char *m1, *m2;
2620 {
2621 register Command *c;
2622 FILE *rcfile;
2623 int gotmachine = 0;
2624 int l1 = strlen(m1);
2625 int l2 = strlen(m2);
2626 char m1save[64];
2627
2628 if (skiprc)
2629 return;
2630
2631 strcpy(m1save, m1);
2632 m1 = m1save;
2633
2634 if (rcname == 0) {
2635 rcname = getenv("HOME");
2636 if (rcname)
2637 strcpy(rcbuf, rcname);
2638 else
2639 rcbuf[0] = '\0';
2640 strcat(rcbuf, "/.telnetrc");
2641 rcname = rcbuf;
2642 }
2643
2644 if ((rcfile = fopen(rcname, "r")) == 0) {
2645 return;
2646 }
2647
2648 for (;;) {
2649 if (fgets(line, sizeof(line), rcfile) == NULL)
2650 break;
2651 if (line[0] == 0)
2652 break;
2653 if (line[0] == '#')
2654 continue;
2655 if (gotmachine) {
2656 if (!isspace(line[0]))
2657 gotmachine = 0;
2658 }
2659 if (gotmachine == 0) {
2660 if (isspace(line[0]))
2661 continue;
2662 if (strncasecmp(line, m1, l1) == 0)
2663 strncpy(line, &line[l1], sizeof(line) - l1);
2664 else if (strncasecmp(line, m2, l2) == 0)
2665 strncpy(line, &line[l2], sizeof(line) - l2);
2666 else if (strncasecmp(line, "DEFAULT", 7) == 0)
2667 strncpy(line, &line[7], sizeof(line) - 7);
2668 else
2669 continue;
2670 if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
2671 continue;
2672 gotmachine = 1;
2673 }
2674 makeargv();
2675 if (margv[0] == 0)
2676 continue;
2677 c = getcmd(margv[0]);
2678 if (Ambiguous(c)) {
2679 printf("?Ambiguous command: %s\n", margv[0]);
2680 continue;
2681 }
2682 if (c == 0) {
2683 printf("?Invalid command: %s\n", margv[0]);
2684 continue;
2685 }
2686 /*
2687 * This should never happen...
2688 */
2689 if (c->needconnect && !connected) {
2690 printf("?Need to be connected first for %s.\n", margv[0]);
2691 continue;
2692 }
2693 (*c->handler)(margc, margv);
2694 }
2695 fclose(rcfile);
2696 }
2697
2698 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2699
2700 /*
2701 * Source route is handed in as
2702 * [!]@hop1 (at) hop2...[@|:]dst
2703 * If the leading ! is present, it is a
2704 * strict source route, otherwise it is
2705 * assmed to be a loose source route.
2706 *
2707 * We fill in the source route option as
2708 * hop1,hop2,hop3...dest
2709 * and return a pointer to hop1, which will
2710 * be the address to connect() to.
2711 *
2712 * Arguments:
2713 * arg: pointer to route list to decipher
2714 *
2715 * cpp: If *cpp is not equal to NULL, this is a
2716 * pointer to a pointer to a character array
2717 * that should be filled in with the option.
2718 *
2719 * lenp: pointer to an integer that contains the
2720 * length of *cpp if *cpp != NULL.
2721 *
2722 * Return values:
2723 *
2724 * Returns the address of the host to connect to. If the
2725 * return value is -1, there was a syntax error in the
2726 * option, either unknown characters, or too many hosts.
2727 * If the return value is 0, one of the hostnames in the
2728 * path is unknown, and *cpp is set to point to the bad
2729 * hostname.
2730 *
2731 * *cpp: If *cpp was equal to NULL, it will be filled
2732 * in with a pointer to our static area that has
2733 * the option filled in. This will be 32bit aligned.
2734 *
2735 * *lenp: This will be filled in with how long the option
2736 * pointed to by *cpp is.
2737 *
2738 */
2739 unsigned long
2740 sourceroute(arg, cpp, lenp)
2741 char *arg;
2742 char **cpp;
2743 unsigned long *lenp;
2744 {
2745 static char lsr[44];
2746 #ifdef sysV88
2747 static IOPTN ipopt;
2748 #endif
2749 char *cp, *cp2, *lsrp, *lsrep;
2750 register int tmp;
2751 struct in_addr sin_addr;
2752 register struct hostent *host = 0;
2753 register char c;
2754
2755 /*
2756 * Verify the arguments, and make sure we have
2757 * at least 7 bytes for the option.
2758 */
2759 if (cpp == NULL || lenp == NULL)
2760 return((unsigned long)-1);
2761 if (*cpp != NULL && *lenp < 7)
2762 return((unsigned long)-1);
2763 /*
2764 * Decide whether we have a buffer passed to us,
2765 * or if we need to use our own static buffer.
2766 */
2767 if (*cpp) {
2768 lsrp = *cpp;
2769 lsrep = lsrp + *lenp;
2770 } else {
2771 *cpp = lsrp = lsr;
2772 lsrep = lsrp + 44;
2773 }
2774
2775 cp = arg;
2776
2777 /*
2778 * Next, decide whether we have a loose source
2779 * route or a strict source route, and fill in
2780 * the begining of the option.
2781 */
2782 #ifndef sysV88
2783 if (*cp == '!') {
2784 cp++;
2785 *lsrp++ = IPOPT_SSRR;
2786 } else
2787 *lsrp++ = IPOPT_LSRR;
2788 #else
2789 if (*cp == '!') {
2790 cp++;
2791 ipopt.io_type = IPOPT_SSRR;
2792 } else
2793 ipopt.io_type = IPOPT_LSRR;
2794 #endif
2795
2796 if (*cp != '@')
2797 return((unsigned long)-1);
2798
2799 #ifndef sysV88
2800 lsrp++; /* skip over length, we'll fill it in later */
2801 *lsrp++ = 4;
2802 #endif
2803
2804 cp++;
2805
2806 sin_addr.s_addr = 0;
2807
2808 for (c = 0;;) {
2809 if (c == ':')
2810 cp2 = 0;
2811 else for (cp2 = cp; (c = *cp2) != '\0'; cp2++) {
2812 if (c == ',') {
2813 *cp2++ = '\0';
2814 if (*cp2 == '@')
2815 cp2++;
2816 } else if (c == '@') {
2817 *cp2++ = '\0';
2818 } else if (c == ':') {
2819 *cp2++ = '\0';
2820 } else
2821 continue;
2822 break;
2823 }
2824 if (!c)
2825 cp2 = 0;
2826
2827 if ((tmp = inet_addr(cp)) != INADDR_NONE) {
2828 sin_addr.s_addr = tmp;
2829 } else if ((host = gethostbyname(cp)) != NULL) {
2830 #if defined(h_addr)
2831 memmove((caddr_t)&sin_addr,
2832 host->h_addr_list[0], host->h_length);
2833 #else
2834 memmove((caddr_t)&sin_addr, host->h_addr, host->h_length);
2835 #endif
2836 } else {
2837 *cpp = cp;
2838 return(0);
2839 }
2840 memmove(lsrp, (char *)&sin_addr, 4);
2841 lsrp += 4;
2842 if (cp2)
2843 cp = cp2;
2844 else
2845 break;
2846 /*
2847 * Check to make sure there is space for next address
2848 */
2849 if (lsrp + 4 > lsrep)
2850 return((unsigned long)-1);
2851 }
2852 #ifndef sysV88
2853 if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
2854 *cpp = 0;
2855 *lenp = 0;
2856 return((unsigned long)-1);
2857 }
2858 *lsrp++ = IPOPT_NOP; /* 32 bit word align it */
2859 *lenp = lsrp - *cpp;
2860 #else
2861 ipopt.io_len = lsrp - *cpp;
2862 if (ipopt.io_len <= 5) { /* Is 3 better ? */
2863 *cpp = 0;
2864 *lenp = 0;
2865 return((unsigned long)-1);
2866 }
2867 *lenp = sizeof(ipopt);
2868 *cpp = (char *) &ipopt;
2869 #endif
2870 return(sin_addr.s_addr);
2871 }
2872 #endif
2873