Merge branch 'master' into amd64
[dragonfly.git] / usr.bin / window / main.c
1 /*      $NetBSD: main.c,v 1.15 2009/04/14 08:50:06 lukem 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
43 static char sccsid[] = "@(#)main.c      8.2 (Berkeley) 4/2/94";
44 #else
45 __RCSID("$NetBSD: main.c,v 1.15 2009/04/14 08:50:06 lukem 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 int     main(int, char **);
62 void    usage(void);
63
64 int
65 main(int argc, char **argv)
66 {
67         char *p;
68         const char *kp;
69         char fflag = 0;
70         char dflag = 0;
71         char xflag = 0;
72         char *cmd = 0;
73         char tflag = 0;
74         int ch;
75
76         escapec = ESCAPEC;
77         debug = strcmp(getprogname(), "a.out") == 0;
78         while ((ch = getopt(argc, argv, "fc:e:tdDx")) != -1) {
79                 switch (ch) {
80                 case 'f':
81                         fflag++;
82                         break;
83                 case 'c':
84                         if (cmd != 0) {
85                                 warnx("Only one -c allowed");
86                                 usage();
87                         }
88                         cmd = optarg;
89                         break;
90                 case 'e':
91                         setescape(optarg);
92                         break;
93                 case 't':
94                         tflag++;
95                         break;
96                 case 'd':
97                         dflag++;
98                         break;
99                 case 'D':
100                         debug = !debug;
101                         break;
102                 case 'x':
103                         xflag++;
104                         break;
105                 default:
106                         usage();
107                 }
108         }
109         if ((kp = getenv("SHELL")) == 0)
110                 kp = _PATH_BSHELL;
111         if ((default_shellfile = str_cpy(kp)) == 0)
112                 errx(1, "Out of memory.");
113         if ((p = strrchr(default_shellfile, '/')))
114                 p++;
115         else
116                 p = default_shellfile;
117         default_shell[0] = p;
118         default_shell[1] = 0;
119         default_nline = NLINE;
120         default_smooth = 1;
121         (void) gettimeofday(&starttime, (struct timezone *)0);
122         if (wwinit() < 0)
123                 errx(1, "%s", wwerror());
124
125 #ifdef OLD_TTY
126         if (debug)
127                 wwnewtty.ww_tchars.t_quitc = wwoldtty.ww_tchars.t_quitc;
128         if (xflag) {
129                 wwnewtty.ww_tchars.t_stopc = wwoldtty.ww_tchars.t_stopc;
130                 wwnewtty.ww_tchars.t_startc = wwoldtty.ww_tchars.t_startc;
131         }
132 #else
133         if (debug) {
134                 wwnewtty.ww_termios.c_cc[VQUIT] =
135                         wwoldtty.ww_termios.c_cc[VQUIT];
136                 wwnewtty.ww_termios.c_lflag |= ISIG;
137         }
138         if (xflag) {
139                 wwnewtty.ww_termios.c_cc[VSTOP] =
140                         wwoldtty.ww_termios.c_cc[VSTOP];
141                 wwnewtty.ww_termios.c_cc[VSTART] =
142                         wwoldtty.ww_termios.c_cc[VSTART];
143                 wwnewtty.ww_termios.c_iflag |= IXON;
144         }
145 #endif
146         if (debug || xflag)
147                 (void) wwsettty(0, &wwnewtty);
148
149         if ((cmdwin = wwopen(WWT_INTERNAL, wwbaud > 2400 ? WWO_REVERSE : 0, 1,
150                              wwncol, 0, 0, 0)) == 0) {
151                 wwflush();
152                 warnx("%s.\r", wwerror());
153                 goto bad;
154         }
155         SET(cmdwin->ww_wflags,
156             WWW_MAPNL | WWW_NOINTR | WWW_NOUPDATE | WWW_UNCTRL);
157         if ((framewin = wwopen(WWT_INTERNAL, WWO_GLASS|WWO_FRAME, wwnrow,
158                                wwncol, 0, 0, 0)) == 0) {
159                 wwflush();
160                 warnx("%s.\r", wwerror());
161                 goto bad;
162         }
163         wwadd(framewin, &wwhead);
164         if ((boxwin = wwopen(WWT_INTERNAL, WWO_GLASS, wwnrow, wwncol, 0, 0, 0))
165             == 0) {
166                 wwflush();
167                 warnx("%s.\r", wwerror());
168                 goto bad;
169         }
170         fgwin = framewin;
171
172         wwupdate();
173         wwflush();
174         setvars();
175
176         setterse(tflag);
177         setcmd(1);
178         if (cmd != 0)
179                 (void) dolongcmd(cmd, (struct value *)0, 0);
180         if (!fflag)
181                 if (dflag || doconfig() < 0)
182                         dodefault();
183         if (selwin != 0)
184                 setcmd(0);
185
186         mloop();
187
188 bad:
189         wwend(1);
190         return 0;
191 }
192
193 void
194 usage(void)
195 {
196         (void) fprintf(stderr,
197             "usage: %s [-e escape-char] [-c command] [-t] [-f] [-d]\n",
198             getprogname());
199         exit(1);
200 }