Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / window / main.c
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: src/usr.bin/window/main.c,v 1.5.6.2 2001/05/17 09:45:00 obrien Exp $";
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 *)usage()))
59
60 /*ARGSUSED*/
61 main(argc, argv)
62 char **argv;
63 {
64         register 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 = rindex(*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 = rindex(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         (void) setlocale(LC_CTYPE, "");
128         if (wwinit() < 0) {
129                 (void) fprintf(stderr, "%s.\n", wwerror());
130                 exit(1);
131         }
132
133 #ifdef OLD_TTY
134         if (debug)
135                 wwnewtty.ww_tchars.t_quitc = wwoldtty.ww_tchars.t_quitc;
136         if (xflag) {
137                 wwnewtty.ww_tchars.t_stopc = wwoldtty.ww_tchars.t_stopc;
138                 wwnewtty.ww_tchars.t_startc = wwoldtty.ww_tchars.t_startc;
139         }
140 #else
141         if (debug) {
142                 wwnewtty.ww_termios.c_cc[VQUIT] =
143                         wwoldtty.ww_termios.c_cc[VQUIT];
144                 wwnewtty.ww_termios.c_lflag |= ISIG;
145         }
146         if (xflag) {
147                 wwnewtty.ww_termios.c_cc[VSTOP] =
148                         wwoldtty.ww_termios.c_cc[VSTOP];
149                 wwnewtty.ww_termios.c_cc[VSTART] =
150                         wwoldtty.ww_termios.c_cc[VSTART];
151                 wwnewtty.ww_termios.c_iflag |= IXON;
152         }
153 #endif
154         if (debug || xflag)
155                 (void) wwsettty(0, &wwnewtty);
156
157         if ((cmdwin = wwopen(wwbaud > 2400 ? WWO_REVERSE : 0, 1, wwncol,
158                              0, 0, 0)) == 0) {
159                 wwflush();
160                 (void) fprintf(stderr, "%s.\r\n", wwerror());
161                 goto bad;
162         }
163         cmdwin->ww_mapnl = 1;
164         cmdwin->ww_nointr = 1;
165         cmdwin->ww_noupdate = 1;
166         cmdwin->ww_unctrl = 1;
167         if ((framewin = wwopen(WWO_GLASS|WWO_FRAME, wwnrow, wwncol, 0, 0, 0))
168             == 0) {
169                 wwflush();
170                 (void) fprintf(stderr, "%s.\r\n", wwerror());
171                 goto bad;
172         }
173         wwadd(framewin, &wwhead);
174         if ((boxwin = wwopen(WWO_GLASS, wwnrow, wwncol, 0, 0, 0)) == 0) {
175                 wwflush();
176                 (void) fprintf(stderr, "%s.\r\n", wwerror());
177                 goto bad;
178         }
179         fgwin = framewin;
180
181         wwupdate();
182         wwflush();
183         setvars();
184
185         setterse(tflag);
186         setcmd(1);
187         if (cmd != 0)
188                 (void) dolongcmd(cmd, (struct value *)0, 0);
189         if (!fflag)
190                 if (dflag || doconfig() < 0)
191                         dodefault();
192         if (selwin != 0)
193                 setcmd(0);
194
195         mloop();
196
197 bad:
198         wwend(1);
199         return 0;
200 }
201
202 usage()
203 {
204         (void) fprintf(stderr, "usage: window [-e escape-char] [-c command] [-t] [-f] [-d]\n");
205         exit(1);
206         return 0;                       /* for lint */
207 }