xref: /original-bsd/usr.bin/telnet/main.c (revision abb30312)
1 /*
2  * Copyright (c) 1988, 1990 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1988, 1990 Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)main.c	5.1 (Berkeley) 09/14/90";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 #if	defined(unix)
20 #include <strings.h>
21 #else	/* defined(unix) */
22 #include <string.h>
23 #endif	/* defined(unix) */
24 
25 #include "ring.h"
26 #include "externs.h"
27 #include "defines.h"
28 
29 /*
30  * Initialize variables.
31  */
32 void
33 tninit()
34 {
35     init_terminal();
36 
37     init_network();
38 
39     init_telnet();
40 
41     init_sys();
42 
43 #if defined(TN3270)
44     init_3270();
45 #endif
46 }
47 
48 int	autologin;
49 
50 /*
51  * main.  Parse arguments, invoke the protocol or command parser.
52  */
53 
54 
55 main(argc, argv)
56 	int argc;
57 	char *argv[];
58 {
59 	extern char *optarg;
60 	extern int optind;
61 	int ch;
62 	char *user, *strrchr();
63 
64 	tninit();		/* Clear out things */
65 #if	defined(CRAY) && !defined(__STDC__)
66 	_setlist_init();	/* Work around compiler bug */
67 #endif
68 
69 	TerminalSaveState();
70 
71 	if (prompt = strrchr(argv[0], '/'))
72 		++prompt;
73 	else
74 		prompt = argv[0];
75 
76 	user = NULL;
77 	autologin = 0;
78 	while ((ch = getopt(argc, argv, "ade:l:n:")) != EOF) {
79 		switch(ch) {
80 		case 'a':
81 			autologin = 1;
82 			break;
83 		case 'd':
84 			debug = 1;
85 			break;
86 		case 'e':
87 			set_escape_char(optarg);
88 			break;
89 		case 'l':
90 			autologin = 1;
91 			user = optarg;
92 			break;
93 		case 'n':
94 #if defined(TN3270) && defined(unix)
95 			/* distinguish between "-n oasynch" and "-noasynch" */
96 			if (argv[optind - 1][0] == '-' && argv[optind - 1][1]
97 			    == 'n' && argv[optind - 1][2] == 'o') {
98 				if (!strcmp(optarg, "oasynch")) {
99 					noasynchtty = 1;
100 					noasynchnet = 1;
101 				} else if (!strcmp(optarg, "oasynchtty"))
102 					noasynchtty = 1;
103 				else if (!strcmp(optarg, "oasynchnet"))
104 					noasynchnet = 1;
105 			} else
106 #endif	/* defined(TN3270) && defined(unix) */
107 				SetNetTrace(optarg);
108 			break;
109 #if defined(TN3270) && defined(unix)
110 		case 't':
111 			transcom = tline;
112 			(void)strcpy(transcom, optarg);
113 			break;
114 #endif
115 		case '?':
116 		default:
117 			usage();
118 			/* NOTREACHED */
119 		}
120 	}
121 	argc -= optind;
122 	argv += optind;
123 
124 	if (argc) {
125 		char *args[7], **argp = args;
126 
127 		if (argc > 2)
128 			usage();
129 		*argp++ = prompt;
130 		if (user) {
131 			*argp++ = "-l";
132 			*argp++ = user;
133 		}
134 		*argp++ = argv[0];		/* host */
135 		if (argc > 1)
136 			*argp++ = argv[1];	/* port */
137 		*argp = 0;
138 
139 		if (setjmp(toplevel) != 0)
140 			Exit(0);
141 		if (tn(argp - args, args) == 1)
142 			return (0);
143 		else
144 			return (1);
145 	}
146 	(void)setjmp(toplevel);
147 	for (;;) {
148 #ifdef TN3270
149 		if (shell_active)
150 			shell_continue();
151 		else
152 #endif
153 			command(1, 0, 0);
154 	}
155 }
156 
157 usage()
158 {
159 	fprintf(stderr, "usage: %s [-a] [ [-l user] host-name [port] ]\n",
160 	    prompt);
161 	exit(1);
162 }
163