1 /*	$OpenBSD: main.c,v 1.10 2005/04/02 22:59:41 jmc Exp $	*/
2 /*	$NetBSD: main.c,v 1.6 1996/02/08 20:45:01 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1983, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Edward Wang at The University of California, Berkeley.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 char copyright[] =
38 "@(#) Copyright (c) 1983, 1993\n\
39 	The Regents of the University of California.  All rights reserved.\n";
40 #endif /* not lint */
41 
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)main.c	8.2 (Berkeley) 4/2/94";
45 #else
46 static char rcsid[] = "$OpenBSD: main.c,v 1.10 2005/04/02 22:59:41 jmc Exp $";
47 #endif
48 #endif /* not lint */
49 
50 #include "defs.h"
51 #include <paths.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include "string.h"
56 #include "char.h"
57 #include "local.h"
58 
59 #define next(a) (*++*(a) ? *(a) : (*++(a) ? *(a) : (char *)(long)usage()))
60 
61 /*ARGSUSED*/
main(int argc,char * argv[])62 main(int argc, char *argv[])
63 {
64 	char *p;
65 	char fflag = 0;
66 	char dflag = 0;
67 	char xflag = 0;
68 	char *cmd = 0;
69 	char tflag = 0;
70 
71 	escapec = ESCAPEC;
72 	if (p = strrchr(*argv, '/'))
73 		p++;
74 	else
75 		p = *argv;
76 	debug = strcmp(p, "a.out") == 0;
77 	while (*++argv) {
78 		if (**argv == '-') {
79 			switch (*++*argv) {
80 			case 'f':
81 				fflag++;
82 				break;
83 			case 'c':
84 				if (cmd != 0) {
85 					(void) fprintf(stderr,
86 						"Only one -c allowed.\n");
87 					(void) usage();
88 				}
89 				cmd = next(argv);
90 				break;
91 			case 'e':
92 				setescape(next(argv));
93 				break;
94 			case 't':
95 				tflag++;
96 				break;
97 			case 'd':
98 				dflag++;
99 				break;
100 			case 'D':
101 				debug = !debug;
102 				break;
103 			case 'x':
104 				xflag++;
105 				break;
106 			default:
107 				(void) usage();
108 			}
109 		} else
110 			(void) usage();
111 	}
112 	if ((p = getenv("SHELL")) == 0)
113 		p = _PATH_BSHELL;
114 	if ((default_shellfile = str_cpy(p)) == 0) {
115 		(void) fprintf(stderr, "Out of memory.\n");
116 		exit(1);
117 	}
118 	if (p = strrchr(default_shellfile, '/'))
119 		p++;
120 	else
121 		p = default_shellfile;
122 	default_shell[0] = p;
123 	default_shell[1] = 0;
124 	default_nline = NLINE;
125 	default_smooth = 1;
126 	(void) gettimeofday(&starttime, (struct timezone *)0);
127 	if (wwinit() < 0) {
128 		(void) fprintf(stderr, "%s.\n", wwerror());
129 		exit(1);
130 	}
131 
132 #ifdef OLD_TTY
133 	if (debug)
134 		wwnewtty.ww_tchars.t_quitc = wwoldtty.ww_tchars.t_quitc;
135 	if (xflag) {
136 		wwnewtty.ww_tchars.t_stopc = wwoldtty.ww_tchars.t_stopc;
137 		wwnewtty.ww_tchars.t_startc = wwoldtty.ww_tchars.t_startc;
138 	}
139 #else
140 	if (debug) {
141 		wwnewtty.ww_termios.c_cc[VQUIT] =
142 			wwoldtty.ww_termios.c_cc[VQUIT];
143 		wwnewtty.ww_termios.c_lflag |= ISIG;
144 	}
145 	if (xflag) {
146 		wwnewtty.ww_termios.c_cc[VSTOP] =
147 			wwoldtty.ww_termios.c_cc[VSTOP];
148 		wwnewtty.ww_termios.c_cc[VSTART] =
149 			wwoldtty.ww_termios.c_cc[VSTART];
150 		wwnewtty.ww_termios.c_iflag |= IXON;
151 	}
152 #endif
153 	if (debug || xflag)
154 		(void) wwsettty(0, &wwnewtty);
155 
156 	if ((cmdwin = wwopen(WWT_INTERNAL, wwbaud > 2400 ? WWO_REVERSE : 0, 1,
157 			     wwncol, 0, 0, 0)) == 0) {
158 		wwflush();
159 		(void) fprintf(stderr, "%s.\r\n", wwerror());
160 		goto bad;
161 	}
162 	SET(cmdwin->ww_wflags,
163 	    WWW_MAPNL | WWW_NOINTR | WWW_NOUPDATE | WWW_UNCTRL);
164 	if ((framewin = wwopen(WWT_INTERNAL, WWO_GLASS|WWO_FRAME, wwnrow,
165 			       wwncol, 0, 0, 0)) == 0) {
166 		wwflush();
167 		(void) fprintf(stderr, "%s.\r\n", wwerror());
168 		goto bad;
169 	}
170 	wwadd(framewin, &wwhead);
171 	if ((boxwin = wwopen(WWT_INTERNAL, WWO_GLASS, wwnrow, wwncol, 0, 0, 0))
172 	    == 0) {
173 		wwflush();
174 		(void) fprintf(stderr, "%s.\r\n", wwerror());
175 		goto bad;
176 	}
177 	fgwin = framewin;
178 
179 	wwupdate();
180 	wwflush();
181 	setvars();
182 
183 	setterse(tflag);
184 	setcmd(1);
185 	if (cmd != 0)
186 		(void) dolongcmd(cmd, (struct value *)0, 0);
187 	if (!fflag)
188 		if (dflag || doconfig() < 0)
189 			dodefault();
190 	if (selwin != 0)
191 		setcmd(0);
192 
193 	mloop();
194 
195 bad:
196 	wwend(1);
197 	return 0;
198 }
199 
usage(void)200 usage(void)
201 {
202 	fprintf(stderr, "Usage: window [-dft] [-c command] [-e escape-char]\n");
203 	exit(1);
204 	return 0;			/* for lint */
205 }
206