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