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