]> git.saurik.com Git - apple/shell_cmds.git/blob - window/cmd.c
d0b1bb97c193b62640d7c069e3f83744a304d799
[apple/shell_cmds.git] / window / cmd.c
1 /* $NetBSD: cmd.c,v 1.6 1998/08/25 20:59:42 ross 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)cmd.c 8.1 (Berkeley) 6/6/93";
43 #else
44 __RCSID("$NetBSD: cmd.c,v 1.6 1998/08/25 20:59:42 ross Exp $");
45 #endif
46 #endif /* not lint */
47
48 #include <unistd.h>
49 #include "defs.h"
50 #include "char.h"
51
52 int checkproc __P((struct ww *));
53
54 void
55 docmd()
56 {
57 int c;
58 struct ww *w;
59 char out = 0;
60
61 while (!out && !quit) {
62 if ((c = wwgetc()) < 0) {
63 if (terse)
64 wwsetcursor(0, 0);
65 else {
66 wwputs("Command: ", cmdwin);
67 wwcurtowin(cmdwin);
68 }
69 do
70 wwiomux();
71 while ((c = wwgetc()) < 0);
72 }
73 if (!terse)
74 wwputc('\n', cmdwin);
75 switch (c) {
76 default:
77 if (c != escapec)
78 break;
79 case 'h': case 'j': case 'k': case 'l':
80 case 'y': case 'p':
81 case ctrl('y'):
82 case ctrl('e'):
83 case ctrl('u'):
84 case ctrl('d'):
85 case ctrl('b'):
86 case ctrl('f'):
87 case ctrl('s'):
88 case ctrl('q'):
89 case ctrl('['):
90 if (selwin == 0) {
91 error("No window.");
92 continue;
93 }
94 }
95 switch (c) {
96 case '1': case '2': case '3': case '4': case '5':
97 case '6': case '7': case '8': case '9':
98 if ((w = window[c - '1']) == 0) {
99 error("%c: No such window.", c);
100 break;
101 }
102 setselwin(w);
103 if (checkproc(selwin) >= 0)
104 out = 1;
105 break;
106 case '%':
107 if ((w = getwin()) != 0)
108 setselwin(w);
109 break;
110 case ctrl('^'):
111 if (lastselwin != 0) {
112 setselwin(lastselwin);
113 if (checkproc(selwin) >= 0)
114 out = 1;
115 } else
116 error("No previous window.");
117 break;
118 case 'c':
119 if ((w = getwin()) != 0)
120 closewin(w);
121 break;
122 case 'w':
123 c_window();
124 break;
125 case 'm':
126 if ((w = getwin()) != 0)
127 c_move(w);
128 break;
129 case 'M':
130 if ((w = getwin()) != 0)
131 movewin(w, w->ww_alt.t, w->ww_alt.l);
132 break;
133 case 's':
134 if ((w = getwin()) != 0)
135 c_size(w);
136 break;
137 case 'S':
138 if ((w = getwin()) != 0)
139 sizewin(w, w->ww_alt.nr, w->ww_alt.nc);
140 break;
141 case 'y':
142 c_yank();
143 break;
144 case 'p':
145 c_put();
146 break;
147 case ':':
148 c_colon();
149 break;
150 case 'h':
151 (void) wwwrite(selwin, "\b", 1);
152 break;
153 case 'j':
154 (void) wwwrite(selwin, "\n", 1);
155 break;
156 case 'k':
157 (void) wwwrite(selwin, "\033A", 2);
158 break;
159 case 'l':
160 (void) wwwrite(selwin, "\033C", 2);
161 break;
162 case ctrl('e'):
163 wwscroll(selwin, 1);
164 break;
165 case ctrl('y'):
166 wwscroll(selwin, -1);
167 break;
168 case ctrl('d'):
169 wwscroll(selwin, selwin->ww_w.nr / 2);
170 break;
171 case ctrl('u'):
172 wwscroll(selwin, - selwin->ww_w.nr / 2);
173 break;
174 case ctrl('f'):
175 wwscroll(selwin, selwin->ww_w.nr);
176 break;
177 case ctrl('b'):
178 wwscroll(selwin, - selwin->ww_w.nr);
179 break;
180 case ctrl('s'):
181 stopwin(selwin);
182 break;
183 case ctrl('q'):
184 startwin(selwin);
185 break;
186 case ctrl('l'):
187 wwredraw();
188 break;
189 case '?':
190 c_help();
191 break;
192 case ctrl('['):
193 if (checkproc(selwin) >= 0)
194 out = 1;
195 break;
196 case ctrl('z'):
197 wwsuspend();
198 break;
199 case 'q':
200 c_quit();
201 break;
202 /* debugging stuff */
203 case '&':
204 if (debug) {
205 c_debug();
206 break;
207 }
208 default:
209 if (c == escapec) {
210 if (checkproc(selwin) >= 0) {
211 (void) write(selwin->ww_pty,
212 &escapec, 1);
213 out = 1;
214 }
215 } else {
216 if (!terse)
217 wwbell();
218 error("Type ? for help.");
219 }
220 }
221 }
222 if (!quit)
223 setcmd(0);
224 }
225
226 struct ww *
227 getwin()
228 {
229 int c;
230 struct ww *w = 0;
231
232 if (!terse)
233 wwputs("Which window? ", cmdwin);
234 wwcurtowin(cmdwin);
235 while ((c = wwgetc()) < 0)
236 wwiomux();
237 if (debug && c == 'c')
238 w = cmdwin;
239 else if (debug && c == 'f')
240 w = framewin;
241 else if (debug && c == 'b')
242 w = boxwin;
243 else if (c >= '1' && c < NWINDOW + '1')
244 w = window[c - '1'];
245 else if (c == '+')
246 w = selwin;
247 else if (c == '-')
248 w = lastselwin;
249 if (w == 0)
250 wwbell();
251 if (!terse)
252 wwputc('\n', cmdwin);
253 return w;
254 }
255
256 int
257 checkproc(w)
258 struct ww *w;
259 {
260 if (w->ww_state != WWS_HASPROC) {
261 error("No process in window.");
262 return -1;
263 }
264 return 0;
265 }
266
267 void
268 setcmd(new)
269 char new;
270 {
271 if (new && !incmd) {
272 if (!terse)
273 wwadd(cmdwin, &wwhead);
274 if (selwin != 0)
275 wwcursor(selwin, 1);
276 wwcurwin = 0;
277 } else if (!new && incmd) {
278 if (!terse) {
279 wwdelete(cmdwin);
280 reframe();
281 }
282 if (selwin != 0)
283 wwcursor(selwin, 0);
284 wwcurwin = selwin;
285 }
286 incmd = new;
287 }
288
289 void
290 setterse(new)
291 char new;
292 {
293 if (incmd) {
294 if (new && !terse) {
295 wwdelete(cmdwin);
296 reframe();
297 } else if (!new && terse)
298 wwadd(cmdwin, &wwhead);
299 }
300 terse = new;
301 }
302
303 /*
304 * Set the current window.
305 */
306 void
307 setselwin(w)
308 struct ww *w;
309 {
310 if (selwin == w)
311 return;
312 if (selwin != 0)
313 lastselwin = selwin;
314 if ((selwin = w) != 0)
315 front(selwin, 1);
316 }