xref: /original-bsd/usr.bin/window/main.c (revision 7e5c8007)
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  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 char copyright[] =
13 "@(#) Copyright (c) 1983, 1993\n\
14 	The Regents of the University of California.  All rights reserved.\n";
15 #endif /* not lint */
16 
17 #ifndef lint
18 static char sccsid[] = "@(#)main.c	8.2 (Berkeley) 04/02/94";
19 #endif /* not lint */
20 
21 #include "defs.h"
22 #include <paths.h>
23 #include <stdio.h>
24 #include "string.h"
25 #include "char.h"
26 #include "local.h"
27 
28 #define next(a) (*++*(a) ? *(a) : (*++(a) ? *(a) : (char *)usage()))
29 
30 /*ARGSUSED*/
31 main(argc, argv)
32 char **argv;
33 {
34 	register char *p;
35 	char fflag = 0;
36 	char dflag = 0;
37 	char xflag = 0;
38 	char *cmd = 0;
39 	char tflag = 0;
40 
41 	escapec = ESCAPEC;
42 	if (p = rindex(*argv, '/'))
43 		p++;
44 	else
45 		p = *argv;
46 	debug = strcmp(p, "a.out") == 0;
47 	while (*++argv) {
48 		if (**argv == '-') {
49 			switch (*++*argv) {
50 			case 'f':
51 				fflag++;
52 				break;
53 			case 'c':
54 				if (cmd != 0) {
55 					(void) fprintf(stderr,
56 						"Only one -c allowed.\n");
57 					(void) usage();
58 				}
59 				cmd = next(argv);
60 				break;
61 			case 'e':
62 				setescape(next(argv));
63 				break;
64 			case 't':
65 				tflag++;
66 				break;
67 			case 'd':
68 				dflag++;
69 				break;
70 			case 'D':
71 				debug = !debug;
72 				break;
73 			case 'x':
74 				xflag++;
75 				break;
76 			default:
77 				(void) usage();
78 			}
79 		} else
80 			(void) usage();
81 	}
82 	if ((p = getenv("SHELL")) == 0)
83 		p = _PATH_BSHELL;
84 	if ((default_shellfile = str_cpy(p)) == 0) {
85 		(void) fprintf(stderr, "Out of memory.\n");
86 		exit(1);
87 	}
88 	if (p = rindex(default_shellfile, '/'))
89 		p++;
90 	else
91 		p = default_shellfile;
92 	default_shell[0] = p;
93 	default_shell[1] = 0;
94 	default_nline = NLINE;
95 	default_smooth = 1;
96 	(void) gettimeofday(&starttime, (struct timezone *)0);
97 	if (wwinit() < 0) {
98 		(void) fprintf(stderr, "%s.\n", wwerror());
99 		exit(1);
100 	}
101 
102 #ifdef OLD_TTY
103 	if (debug)
104 		wwnewtty.ww_tchars.t_quitc = wwoldtty.ww_tchars.t_quitc;
105 	if (xflag) {
106 		wwnewtty.ww_tchars.t_stopc = wwoldtty.ww_tchars.t_stopc;
107 		wwnewtty.ww_tchars.t_startc = wwoldtty.ww_tchars.t_startc;
108 	}
109 #else
110 	if (debug) {
111 		wwnewtty.ww_termios.c_cc[VQUIT] =
112 			wwoldtty.ww_termios.c_cc[VQUIT];
113 		wwnewtty.ww_termios.c_lflag |= ISIG;
114 	}
115 	if (xflag) {
116 		wwnewtty.ww_termios.c_cc[VSTOP] =
117 			wwoldtty.ww_termios.c_cc[VSTOP];
118 		wwnewtty.ww_termios.c_cc[VSTART] =
119 			wwoldtty.ww_termios.c_cc[VSTART];
120 		wwnewtty.ww_termios.c_iflag |= IXON;
121 	}
122 #endif
123 	if (debug || xflag)
124 		(void) wwsettty(0, &wwnewtty);
125 
126 	if ((cmdwin = wwopen(wwbaud > 2400 ? WWO_REVERSE : 0, 1, wwncol,
127 			     0, 0, 0)) == 0) {
128 		wwflush();
129 		(void) fprintf(stderr, "%s.\r\n", wwerror());
130 		goto bad;
131 	}
132 	cmdwin->ww_mapnl = 1;
133 	cmdwin->ww_nointr = 1;
134 	cmdwin->ww_noupdate = 1;
135 	cmdwin->ww_unctrl = 1;
136 	if ((framewin = wwopen(WWO_GLASS|WWO_FRAME, wwnrow, wwncol, 0, 0, 0))
137 	    == 0) {
138 		wwflush();
139 		(void) fprintf(stderr, "%s.\r\n", wwerror());
140 		goto bad;
141 	}
142 	wwadd(framewin, &wwhead);
143 	if ((boxwin = wwopen(WWO_GLASS, wwnrow, wwncol, 0, 0, 0)) == 0) {
144 		wwflush();
145 		(void) fprintf(stderr, "%s.\r\n", wwerror());
146 		goto bad;
147 	}
148 	fgwin = framewin;
149 
150 	wwupdate();
151 	wwflush();
152 	setvars();
153 
154 	setterse(tflag);
155 	setcmd(1);
156 	if (cmd != 0)
157 		(void) dolongcmd(cmd, (struct value *)0, 0);
158 	if (!fflag)
159 		if (dflag || doconfig() < 0)
160 			dodefault();
161 	if (selwin != 0)
162 		setcmd(0);
163 
164 	mloop();
165 
166 bad:
167 	wwend(1);
168 	return 0;
169 }
170 
171 usage()
172 {
173 	(void) fprintf(stderr, "Usage: window [-e escape-char] [-c command] [-t] [-f] [-d]\n");
174 	exit(1);
175 	return 0;			/* for lint */
176 }
177