Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / window / cmd.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 static char sccsid[] = "@(#)cmd.c       8.1 (Berkeley) 6/6/93";
39 static char rcsid[] =
40   "$FreeBSD: src/usr.bin/window/cmd.c,v 1.2.14.1 2001/05/17 09:45:00 obrien Exp $";
41 #endif /* not lint */
42
43 #include "defs.h"
44 #include "char.h"
45
46 docmd()
47 {
48         register c;
49         register struct ww *w;
50         char out = 0;
51
52         while (!out && !quit) {
53                 if ((c = wwgetc()) < 0) {
54                         if (terse)
55                                 wwsetcursor(0, 0);
56                         else {
57                                 wwputs("Command: ", cmdwin);
58                                 wwcurtowin(cmdwin);
59                         }
60                         do
61                                 wwiomux();
62                         while ((c = wwgetc()) < 0);
63                 }
64                 if (!terse)
65                         wwputc('\n', cmdwin);
66                 switch (c) {
67                 default:
68                         if (c != escapec)
69                                 break;
70                 case 'h': case 'j': case 'k': case 'l':
71                 case 'y': case 'p':
72                 case ctrl('y'):
73                 case ctrl('e'):
74                 case ctrl('u'):
75                 case ctrl('d'):
76                 case ctrl('b'):
77                 case ctrl('f'):
78                 case ctrl('s'):
79                 case ctrl('q'):
80                 case ctrl('['):
81                         if (selwin == 0) {
82                                 error("No window.");
83                                 continue;
84                         }
85                 }
86                 switch (c) {
87                 case '1': case '2': case '3': case '4': case '5':
88                 case '6': case '7': case '8': case '9':
89                         if ((w = window[c - '1']) == 0) {
90                                 error("%c: No such window.", c);
91                                 break;
92                         }
93                         setselwin(w);
94                         if (checkproc(selwin) >= 0)
95                                  out = 1;
96                         break;
97                 case '%':
98                         if ((w = getwin()) != 0)
99                                 setselwin(w);
100                         break;
101                 case ctrl('^'):
102                         if (lastselwin != 0) {
103                                 setselwin(lastselwin);
104                                 if (checkproc(selwin) >= 0)
105                                         out = 1;
106                         } else
107                                 error("No previous window.");
108                         break;
109                 case 'c':
110                         if ((w = getwin()) != 0)
111                                 closewin(w);
112                         break;
113                 case 'w':
114                         c_window();
115                         break;
116                 case 'm':
117                         if ((w = getwin()) != 0)
118                                 c_move(w);
119                         break;
120                 case 'M':
121                         if ((w = getwin()) != 0)
122                                 movewin(w, w->ww_alt.t, w->ww_alt.l);
123                         break;
124                 case 's':
125                         if ((w = getwin()) != 0)
126                                 c_size(w);
127                         break;
128                 case 'S':
129                         if ((w = getwin()) != 0)
130                                 sizewin(w, w->ww_alt.nr, w->ww_alt.nc);
131                         break;
132                 case 'y':
133                         c_yank();
134                         break;
135                 case 'p':
136                         c_put();
137                         break;
138                 case ':':
139                         c_colon();
140                         break;
141                 case 'h':
142                         (void) wwwrite(selwin, "\b", 1);
143                         break;
144                 case 'j':
145                         (void) wwwrite(selwin, "\n", 1);
146                         break;
147                 case 'k':
148                         (void) wwwrite(selwin, "\033A", 2);
149                         break;
150                 case 'l':
151                         (void) wwwrite(selwin, "\033C", 2);
152                         break;
153                 case ctrl('e'):
154                         wwscroll(selwin, 1);
155                         break;
156                 case ctrl('y'):
157                         wwscroll(selwin, -1);
158                         break;
159                 case ctrl('d'):
160                         wwscroll(selwin, selwin->ww_w.nr / 2);
161                         break;
162                 case ctrl('u'):
163                         wwscroll(selwin, - selwin->ww_w.nr / 2);
164                         break;
165                 case ctrl('f'):
166                         wwscroll(selwin, selwin->ww_w.nr);
167                         break;
168                 case ctrl('b'):
169                         wwscroll(selwin, - selwin->ww_w.nr);
170                         break;
171                 case ctrl('s'):
172                         stopwin(selwin);
173                         break;
174                 case ctrl('q'):
175                         startwin(selwin);
176                         break;
177                 case ctrl('l'):
178                         wwredraw();
179                         break;
180                 case '?':
181                         c_help();
182                         break;
183                 case ctrl('['):
184                         if (checkproc(selwin) >= 0)
185                                 out = 1;
186                         break;
187                 case ctrl('z'):
188                         wwsuspend();
189                         break;
190                 case 'q':
191                         c_quit();
192                         break;
193                 /* debugging stuff */
194                 case '&':
195                         if (debug) {
196                                 c_debug();
197                                 break;
198                         }
199                 default:
200                         if (c == escapec) {
201                                 if (checkproc(selwin) >= 0) {
202                                         (void) write(selwin->ww_pty,
203                                                 &escapec, 1);
204                                         out = 1;
205                                 }
206                         } else {
207                                 if (!terse)
208                                         wwbell();
209                                 error("Type ? for help.");
210                         }
211                 }
212         }
213         if (!quit)
214                 setcmd(0);
215 }
216
217 struct ww *
218 getwin()
219 {
220         register int c;
221         struct ww *w = 0;
222
223         if (!terse)
224                 wwputs("Which window? ", cmdwin);
225         wwcurtowin(cmdwin);
226         while ((c = wwgetc()) < 0)
227                 wwiomux();
228         if (debug && c == 'c')
229                 w = cmdwin;
230         else if (debug && c == 'f')
231                 w = framewin;
232         else if (debug && c == 'b')
233                 w = boxwin;
234         else if (c >= '1' && c < NWINDOW + '1')
235                 w = window[c - '1'];
236         else if (c == '+')
237                 w = selwin;
238         else if (c == '-')
239                 w = lastselwin;
240         if (w == 0)
241                 wwbell();
242         if (!terse)
243                 wwputc('\n', cmdwin);
244         return w;
245 }
246
247 checkproc(w)
248 struct ww *w;
249 {
250         if (w->ww_state != WWS_HASPROC) {
251                 error("No process in window.");
252                 return -1;
253         }
254         return 0;
255 }
256
257 setcmd(new)
258 char new;
259 {
260         if (new && !incmd) {
261                 if (!terse)
262                         wwadd(cmdwin, &wwhead);
263                 if (selwin != 0)
264                         wwcursor(selwin, 1);
265                 wwcurwin = 0;
266         } else if (!new && incmd) {
267                 if (!terse) {
268                         wwdelete(cmdwin);
269                         reframe();
270                 }
271                 if (selwin != 0)
272                         wwcursor(selwin, 0);
273                 wwcurwin = selwin;
274         }
275         incmd = new;
276 }
277
278 setterse(new)
279 char new;
280 {
281         if (incmd)
282                 if (new && !terse) {
283                         wwdelete(cmdwin);
284                         reframe();
285                 } else if (!new && terse)
286                         wwadd(cmdwin, &wwhead);
287         terse = new;
288 }
289
290 /*
291  * Set the current window.
292  */
293 setselwin(w)
294 struct ww *w;
295 {
296         if (selwin == w)
297                 return;
298         if (selwin != 0)
299                 lastselwin = selwin;
300         if ((selwin = w) != 0)
301                 front(selwin, 1);
302 }