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