xref: /original-bsd/usr.bin/telnet/main.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1988, 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1988, 1990, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 06/06/93";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 
20 #include "ring.h"
21 #include "externs.h"
22 #include "defines.h"
23 
24 /*
25  * Initialize variables.
26  */
27     void
28 tninit()
29 {
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
41 }
42 
43 	void
44 usage()
45 {
46 	fprintf(stderr, "Usage: %s %s%s%s%s\n",
47 	    prompt,
48 #ifdef	AUTHENTICATION
49 	    "[-8] [-E] [-K] [-L] [-S tos] [-X atype] [-a] [-c] [-d] [-e char]",
50 	    "\n\t[-k realm] [-l user] [-n tracefile] ",
51 #else
52 	    "[-8] [-E] [-L] [-S tos] [-a] [-c] [-d] [-e char] [-l user]",
53 	    "\n\t[-n tracefile]",
54 #endif
55 #if defined(TN3270) && defined(unix)
56 # ifdef AUTHENTICATION
57 	    "[-noasynch] [-noasynctty]\n\t[-noasyncnet] [-r] [-t transcom] ",
58 # else
59 	    "[-noasynch] [-noasynctty] [-noasyncnet] [-r]\n\t[-t transcom]",
60 # endif
61 #else
62 	    "[-r] ",
63 #endif
64 #ifdef	ENCRYPTION
65 	    "[-x] [host-name [port]]"
66 #else	/* ENCRYPTION */
67 	    "[host-name [port]]"
68 #endif	/* ENCRYPTION */
69 	);
70 	exit(1);
71 }
72 
73 /*
74  * main.  Parse arguments, invoke the protocol or command parser.
75  */
76 
77 
78 main(argc, argv)
79 	int argc;
80 	char *argv[];
81 {
82 	extern char *optarg;
83 	extern int optind;
84 	int ch;
85 	char *user, *strrchr();
86 
87 	tninit();		/* Clear out things */
88 #if	defined(CRAY) && !defined(__STDC__)
89 	_setlist_init();	/* Work around compiler bug */
90 #endif
91 
92 	TerminalSaveState();
93 
94 	if (prompt = strrchr(argv[0], '/'))
95 		++prompt;
96 	else
97 		prompt = argv[0];
98 
99 	user = NULL;
100 
101 	rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
102 	autologin = -1;
103 
104 	while ((ch = getopt(argc, argv, "8EKLS:X:acde:k:l:n:rt:x")) != EOF) {
105 		switch(ch) {
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	AUTHENTICATION
114 			autologin = 0;
115 #endif
116 			break;
117 		case 'L':
118 			eight |= 2;	/* binary output only */
119 			break;
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;
137 		case 'X':
138 #ifdef	AUTHENTICATION
139 			auth_disable_name(optarg);
140 #endif
141 			break;
142 		case 'a':
143 			autologin = 1;
144 			break;
145 		case 'c':
146 			skiprc = 1;
147 			break;
148 		case 'd':
149 			debug = 1;
150 			break;
151 		case 'e':
152 			set_escape_char(optarg);
153 			break;
154 		case 'k':
155 #if defined(AUTHENTICATION) && 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;
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;
181 				else if (!strcmp(optarg, "oasynchnet"))
182 					noasynchnet = 1;
183 			} else
184 #endif	/* defined(TN3270) && defined(unix) */
185 				SetNetTrace(optarg);
186 			break;
187 		case 'r':
188 			rlogin = '~';
189 			break;
190 		case 't':
191 #if defined(TN3270) && defined(unix)
192 			transcom = tline;
193 			(void)strcpy(transcom, optarg);
194 #else
195 			fprintf(stderr,
196 			   "%s: Warning: -t ignored, no TN3270 support.\n",
197 								prompt);
198 #endif
199 			break;
200 		case 'x':
201 #ifdef	ENCRYPTION
202 			encrypt_auto(1);
203 			decrypt_auto(1);
204 #else	/* ENCRYPTION */
205 			fprintf(stderr,
206 			    "%s: Warning: -x ignored, no ENCRYPT support.\n",
207 								prompt);
208 #endif	/* ENCRYPTION */
209 			break;
210 		case '?':
211 		default:
212 			usage();
213 			/* NOTREACHED */
214 		}
215 	}
216 	if (autologin == -1)
217 		autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
218 
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);
243 	}
244 	(void)setjmp(toplevel);
245 	for (;;) {
246 #ifdef TN3270
247 		if (shell_active)
248 			shell_continue();
249 		else
250 #endif
251 			command(1, 0, 0);
252 	}
253 }
254