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