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