xref: /dragonfly/usr.bin/window/cmd.c (revision 91f1d6e1)
1 /*	@(#)cmd.c	8.1 (Berkeley) 6/6/93	*/
2 /*	$NetBSD: cmd.c,v 1.8 2003/08/07 11:17:21 agc 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 <unistd.h>
37 #include "defs.h"
38 #include "char.h"
39 
40 int	checkproc(struct ww *);
41 
42 void
43 docmd(void)
44 {
45 	int c;
46 	struct ww *w;
47 	char out = 0;
48 
49 	while (!out && !quit) {
50 		if ((c = wwgetc()) < 0) {
51 			if (terse)
52 				wwsetcursor(0, 0);
53 			else {
54 				wwputs("Command: ", cmdwin);
55 				wwcurtowin(cmdwin);
56 			}
57 			do
58 				wwiomux();
59 			while ((c = wwgetc()) < 0);
60 		}
61 		if (!terse)
62 			wwputc('\n', cmdwin);
63 		switch (c) {
64 		default:
65 			if (c != escapec)
66 				break;
67 			/* FALLTHROUGH */
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']) == NULL) {
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()) != NULL)
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()) != NULL)
109 				closewin(w);
110 			break;
111 		case 'w':
112 			c_window();
113 			break;
114 		case 'm':
115 			if ((w = getwin()) != NULL)
116 				c_move(w);
117 			break;
118 		case 'M':
119 			if ((w = getwin()) != NULL)
120 				movewin(w, w->ww_alt.t, w->ww_alt.l);
121 			break;
122 		case 's':
123 			if ((w = getwin()) != NULL)
124 				c_size(w);
125 			break;
126 		case 'S':
127 			if ((w = getwin()) != NULL)
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 			/* FALLTHROUGH */
198 		default:
199 			if (c == escapec) {
200 				if (checkproc(selwin) >= 0) {
201 					(void) write(selwin->ww_pty,
202 						&escapec, 1);
203 					out = 1;
204 				}
205 			} else {
206 				if (!terse)
207 					wwbell();
208 				error("Type ? for help.");
209 			}
210 		}
211 	}
212 	if (!quit)
213 		setcmd(0);
214 }
215 
216 struct ww *
217 getwin(void)
218 {
219 	int c;
220 	struct ww *w = NULL;
221 
222 	if (!terse)
223 		wwputs("Which window? ", cmdwin);
224 	wwcurtowin(cmdwin);
225 	while ((c = wwgetc()) < 0)
226 		wwiomux();
227 	if (debug && c == 'c')
228 		w = cmdwin;
229 	else if (debug && c == 'f')
230 		w = framewin;
231 	else if (debug && c == 'b')
232 		w = boxwin;
233 	else if (c >= '1' && c < NWINDOW + '1')
234 		w = window[c - '1'];
235 	else if (c == '+')
236 		w = selwin;
237 	else if (c == '-')
238 		w = lastselwin;
239 	if (w == NULL)
240 		wwbell();
241 	if (!terse)
242 		wwputc('\n', cmdwin);
243 	return w;
244 }
245 
246 int
247 checkproc(struct ww *w)
248 {
249 	if (w->ww_state != WWS_HASPROC) {
250 		error("No process in window.");
251 		return -1;
252 	}
253 	return 0;
254 }
255 
256 void
257 setcmd(char new)
258 {
259 	if (new && !incmd) {
260 		if (!terse)
261 			wwadd(cmdwin, &wwhead);
262 		if (selwin != 0)
263 			wwcursor(selwin, 1);
264 		wwcurwin = 0;
265 	} else if (!new && incmd) {
266 		if (!terse) {
267 			wwdelete(cmdwin);
268 			reframe();
269 		}
270 		if (selwin != 0)
271 			wwcursor(selwin, 0);
272 		wwcurwin = selwin;
273 	}
274 	incmd = new;
275 }
276 
277 void
278 setterse(char new)
279 {
280 	if (incmd) {
281 		if (new && !terse) {
282 			wwdelete(cmdwin);
283 			reframe();
284 		} else if (!new && terse)
285 			wwadd(cmdwin, &wwhead);
286 	}
287 	terse = new;
288 }
289 
290 /*
291  * Set the current window.
292  */
293 void
294 setselwin(struct ww *w)
295 {
296 	if (selwin == w)
297 		return;
298 	if (selwin != 0)
299 		lastselwin = selwin;
300 	if ((selwin = w) != NULL)
301 		front(selwin, 1);
302 }
303