make source match the man page, handle -number/+number more cleanly
[unix-history] / usr / src / usr.bin / telnet / main.c
CommitLineData
897ce52e 1/*
33b2a598 2 * Copyright (c) 1988, 1990 Regents of the University of California.
897ce52e
KB
3 * All rights reserved.
4 *
cb956e54 5 * %sccs.include.redist.c%
897ce52e
KB
6 */
7
8#ifndef lint
9char copyright[] =
33b2a598 10"@(#) Copyright (c) 1988, 1990 Regents of the University of California.\n\
897ce52e
KB
11 All rights reserved.\n";
12#endif /* not lint */
13
14#ifndef lint
b7c8f459 15static char sccsid[] = "@(#)main.c 5.4 (Berkeley) %G%";
897ce52e
KB
16#endif /* not lint */
17
115a5494
GM
18#include <sys/types.h>
19
20#include "ring.h"
6f6e51bd
GM
21#include "externs.h"
22#include "defines.h"
23
24/*
25 * Initialize variables.
26 */
15d31b7e 27 void
6f6e51bd
GM
28tninit()
29{
7a26b73d
PB
30 init_terminal();
31
32 init_network();
33
34 init_telnet();
35
36 init_sys();
37
38#if defined(TN3270)
39 init_3270();
40#endif
6f6e51bd
GM
41}
42
15d31b7e
DB
43 void
44usage()
45{
46 fprintf(stderr, "Usage: %s %s%s%s%s\n",
47 prompt,
48#ifdef AUTHENTICATE
49 " [-8] [-E] [-K] [-L] [-X atype] [-a] [-d] [-e char] [-k realm]",
50 "\n\t[-l user] [-n tracefile] ",
51#else
52 " [-8] [-E] [-L] [-a] [-d] [-e char] [-l user] [-n tracefile]",
53 "\n\t",
54#endif
55#if defined(TN3270) && defined(unix)
56# ifdef AUTHENTICATE
57 "[-noasynch] [-noasynctty] [-noasyncnet]\n\t[-r] [-t transcom] ",
58# else
59 "[-noasynch] [-noasynctty] [-noasyncnet] [-r] [-t transcom]\n\t",
60# endif
61#else
62 "[-r] ",
63#endif
64#ifdef ENCRYPT
65 "[-x] [host-name [port]]"
66#else
67 "[host-name [port]]"
68#endif
69 );
70 exit(1);
71}
6f6e51bd
GM
72
73/*
74 * main. Parse arguments, invoke the protocol or command parser.
75 */
7a26b73d
PB
76
77
6f6e51bd
GM
78main(argc, argv)
79 int argc;
80 char *argv[];
81{
33b2a598
MK
82 extern char *optarg;
83 extern int optind;
84 int ch;
7a26b73d 85 char *user, *strrchr();
33b2a598
MK
86
87 tninit(); /* Clear out things */
7a26b73d 88#if defined(CRAY) && !defined(__STDC__)
33b2a598 89 _setlist_init(); /* Work around compiler bug */
6055a9f6 90#endif
7a26b73d 91
33b2a598
MK
92 TerminalSaveState();
93
7a26b73d 94 if (prompt = strrchr(argv[0], '/'))
33b2a598
MK
95 ++prompt;
96 else
97 prompt = argv[0];
98
99 user = NULL;
15d31b7e
DB
100
101 rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
102 autologin = -1;
103
a187e70e 104 while ((ch = getopt(argc, argv, "8EKLS:X:ade:k:l:n:rt:x")) != EOF) {
33b2a598 105 switch(ch) {
15d31b7e
DB
106 case '8':
107 eight = 3; /* binary output and input */
108 break;
109 case 'E':
110 rlogin = escape = _POSIX_VDISABLE;
111 break;
112 case 'K':
113#ifdef AUTHENTICATE
114 autologin = 0;
115#endif
116 break;
117 case 'L':
118 eight |= 2; /* binary output only */
119 break;
a187e70e
DB
120 case 'S':
121 {
122#ifdef HAS_GETTOS
123 extern int tos;
124
125 if ((tos = parsetos(optarg, "tcp")) < 0)
126 fprintf(stderr, "%s%s%s%s\n",
127 prompt, ": Bad TOS argument '",
128 optarg,
129 "; will try to use default TOS");
130#else
131 fprintf(stderr,
132 "%s: Warning: -S ignored, no parsetos() support.\n",
133 prompt);
134#endif
135 }
136 break;
15d31b7e
DB
137 case 'X':
138#ifdef AUTHENTICATE
139 auth_disable_name(optarg);
140#endif
141 break;
33b2a598
MK
142 case 'a':
143 autologin = 1;
144 break;
b7c8f459
DB
145 case 'c':
146 skiprc = 1;
147 break;
33b2a598
MK
148 case 'd':
149 debug = 1;
150 break;
151 case 'e':
152 set_escape_char(optarg);
153 break;
15d31b7e
DB
154 case 'k':
155#if defined(AUTHENTICATE) && defined(KRB4)
156 {
157 extern char *dest_realm, dst_realm_buf[], dst_realm_sz;
158 dest_realm = dst_realm_buf;
159 (void)strncpy(dest_realm, optarg, dst_realm_sz);
160 }
161#else
162 fprintf(stderr,
163 "%s: Warning: -k ignored, no Kerberos V4 support.\n",
164 prompt);
165#endif
166 break;
33b2a598
MK
167 case 'l':
168 autologin = 1;
169 user = optarg;
170 break;
171 case 'n':
172#if defined(TN3270) && defined(unix)
173 /* distinguish between "-n oasynch" and "-noasynch" */
174 if (argv[optind - 1][0] == '-' && argv[optind - 1][1]
175 == 'n' && argv[optind - 1][2] == 'o') {
176 if (!strcmp(optarg, "oasynch")) {
177 noasynchtty = 1;
178 noasynchnet = 1;
179 } else if (!strcmp(optarg, "oasynchtty"))
180 noasynchtty = 1;
7a26b73d 181 else if (!strcmp(optarg, "oasynchnet"))
33b2a598 182 noasynchnet = 1;
33b2a598 183 } else
6f6e51bd 184#endif /* defined(TN3270) && defined(unix) */
33b2a598
MK
185 SetNetTrace(optarg);
186 break;
15d31b7e
DB
187 case 'r':
188 rlogin = '~';
189 break;
33b2a598 190 case 't':
15d31b7e 191#if defined(TN3270) && defined(unix)
33b2a598
MK
192 transcom = tline;
193 (void)strcpy(transcom, optarg);
15d31b7e
DB
194#else
195 fprintf(stderr,
196 "%s: Warning: -t ignored, no TN3270 support.\n",
197 prompt);
198#endif
33b2a598 199 break;
15d31b7e
DB
200 case 'x':
201#ifdef ENCRYPT
b7c8f459
DB
202 encrypt_auto(1);
203 decrypt_auto(1);
15d31b7e
DB
204#else
205 fprintf(stderr,
206 "%s: Warning: -x ignored, no ENCRYPT support.\n",
207 prompt);
33b2a598 208#endif
15d31b7e 209 break;
33b2a598
MK
210 case '?':
211 default:
212 usage();
213 /* NOTREACHED */
214 }
7a26b73d 215 }
15d31b7e
DB
216 if (autologin == -1)
217 autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
218
33b2a598
MK
219 argc -= optind;
220 argv += optind;
221
222 if (argc) {
223 char *args[7], **argp = args;
224
225 if (argc > 2)
226 usage();
227 *argp++ = prompt;
228 if (user) {
229 *argp++ = "-l";
230 *argp++ = user;
231 }
232 *argp++ = argv[0]; /* host */
233 if (argc > 1)
234 *argp++ = argv[1]; /* port */
235 *argp = 0;
236
237 if (setjmp(toplevel) != 0)
238 Exit(0);
239 if (tn(argp - args, args) == 1)
240 return (0);
241 else
242 return (1);
6f6e51bd 243 }
33b2a598 244 (void)setjmp(toplevel);
7a26b73d 245 for (;;) {
33b2a598
MK
246#ifdef TN3270
247 if (shell_active)
248 shell_continue();
249 else
250#endif
251 command(1, 0, 0);
7a26b73d 252 }
33b2a598 253}