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