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