]> git.saurik.com Git - apple/shell_cmds.git/blob - window/lcmd1.c
shell_cmds-34.tar.gz
[apple/shell_cmds.git] / window / lcmd1.c
1 /* $NetBSD: lcmd1.c,v 1.7 1997/11/21 08:36:03 lukem 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[] = "@(#)lcmd1.c 8.1 (Berkeley) 6/6/93";
43 #else
44 __RCSID("$NetBSD: lcmd1.c,v 1.7 1997/11/21 08:36:03 lukem Exp $");
45 #endif
46 #endif /* not lint */
47
48 #include <string.h>
49 #include <unistd.h>
50 #include "defs.h"
51 #include "window_string.h"
52 #include "lcmd.h"
53 #include "var.h"
54
55 char vtobool __P((struct value *, char, char));
56
57 struct lcmd_arg arg_window[] = {
58 { "row", 1, ARG_NUM },
59 { "column", 1, ARG_NUM },
60 { "nrows", 2, ARG_NUM },
61 { "ncols", 2, ARG_NUM },
62 { "nlines", 2, ARG_NUM },
63 { "label", 1, ARG_STR },
64 { "pty", 1, ARG_ANY },
65 { "frame", 1, ARG_ANY },
66 { "mapnl", 1, ARG_ANY },
67 { "keepopen", 1, ARG_ANY },
68 { "smooth", 1, ARG_ANY },
69 { "shell", 1, ARG_STR|ARG_LIST },
70 { 0 }
71 };
72
73 void
74 l_window(v, a)
75 struct value *v;
76 struct value *a;
77 {
78 struct ww *w;
79 int col, row, ncol, nrow, id, nline;
80 char *label;
81 int haspty, hasframe, mapnl, keepopen, smooth;
82 char *shf, **sh;
83 char *argv[sizeof default_shell / sizeof *default_shell];
84 char **pp;
85
86 if ((id = findid()) < 0)
87 return;
88 row = a->v_type == V_ERR ? 1 : a->v_num;
89 a++;
90 col = a->v_type == V_ERR ? 0 : a->v_num;
91 a++;
92 nrow = a->v_type == V_ERR ? wwnrow - row : a->v_num;
93 a++;
94 ncol = a->v_type == V_ERR ? wwncol - col : a->v_num;
95 a++;
96 nline = a->v_type == V_ERR ? default_nline : a->v_num;
97 a++;
98 label = a->v_type == V_ERR ? 0 : a->v_str;
99 if ((haspty = vtobool(++a, 1, -1)) < 0)
100 return;
101 if ((hasframe = vtobool(++a, 1, -1)) < 0)
102 return;
103 if ((mapnl = vtobool(++a, !haspty, -1)) < 0)
104 return;
105 if ((keepopen = vtobool(++a, 0, -1)) < 0)
106 return;
107 if ((smooth = vtobool(++a, default_smooth, -1)) < 0)
108 return;
109 if ((++a)->v_type != V_ERR) {
110 for (pp = argv; a->v_type != V_ERR &&
111 pp < &argv[sizeof argv/sizeof *argv-1]; pp++, a++)
112 *pp = a->v_str;
113 *pp = 0;
114 shf = *(sh = argv);
115 if ((*sh = strrchr(shf, '/')))
116 (*sh)++;
117 else
118 *sh = shf;
119 } else {
120 sh = default_shell;
121 shf = default_shellfile;
122 }
123 if ((w = openwin(id, row, col, nrow, ncol, nline, label,
124 haspty ? WWT_PTY : WWT_SOCKET, hasframe ? WWU_HASFRAME : 0, shf,
125 sh)) == 0)
126 return;
127 if (mapnl)
128 SET(w->ww_wflags, WWW_MAPNL);
129 else
130 CLR(w->ww_wflags, WWW_MAPNL);
131 if (keepopen)
132 SET(w->ww_uflags, WWU_KEEPOPEN);
133 else
134 CLR(w->ww_uflags, WWU_KEEPOPEN);
135 if (!smooth)
136 SET(w->ww_wflags, WWW_NOUPDATE);
137 else
138 CLR(w->ww_wflags, WWW_NOUPDATE);
139 v->v_type = V_NUM;
140 v->v_num = id + 1;
141 }
142
143 struct lcmd_arg arg_def_nline[] = {
144 { "nlines", 1, ARG_NUM },
145 { 0 }
146 };
147
148 void
149 l_def_nline(v, a)
150 struct value *v, *a;
151 {
152 v->v_num = default_nline;
153 v->v_type = V_NUM;
154 if (a->v_type != V_ERR)
155 default_nline = a->v_num;
156 }
157
158 struct lcmd_arg arg_smooth[] = {
159 { "window", 1, ARG_NUM },
160 { "flag", 1, ARG_ANY },
161 { 0 }
162 };
163
164 void
165 l_smooth(v, a)
166 struct value *v, *a;
167 {
168 struct ww *w;
169
170 v->v_type = V_NUM;
171 v->v_num = 0;
172 if ((w = vtowin(a++, selwin)) == 0)
173 return;
174 v->v_num = ISSET(w->ww_wflags, WWW_NOUPDATE) == 0;
175 if (!vtobool(a, v->v_num, v->v_num))
176 SET(w->ww_wflags, WWW_NOUPDATE);
177 else
178 CLR(w->ww_wflags, WWW_NOUPDATE);
179 }
180
181 struct lcmd_arg arg_def_smooth[] = {
182 { "flag", 1, ARG_ANY },
183 { 0 }
184 };
185
186 void
187 l_def_smooth(v, a)
188 struct value *v, *a;
189 {
190 v->v_type = V_NUM;
191 v->v_num = default_smooth;
192 default_smooth = vtobool(a, v->v_num, v->v_num);
193 }
194
195 struct lcmd_arg arg_select[] = {
196 { "window", 1, ARG_NUM },
197 { 0 }
198 };
199
200 void
201 l_select(v, a)
202 struct value *v, *a;
203 {
204 struct ww *w;
205
206 v->v_type = V_NUM;
207 v->v_num = selwin ? selwin->ww_id + 1 : -1;
208 if (a->v_type == V_ERR)
209 return;
210 if ((w = vtowin(a, (struct ww *)0)) == 0)
211 return;
212 setselwin(w);
213 }
214
215 struct lcmd_arg arg_debug[] = {
216 { "flag", 1, ARG_ANY },
217 { 0 }
218 };
219
220 void
221 l_debug(v, a)
222 struct value *v, *a;
223 {
224 v->v_type = V_NUM;
225 v->v_num = debug;
226 debug = vtobool(a, debug, debug);
227 }
228
229 struct lcmd_arg arg_escape[] = {
230 { "escapec", 1, ARG_STR },
231 { 0 }
232 };
233
234 void
235 l_escape(v, a)
236 struct value *v, *a;
237 {
238 char buf[2];
239
240 buf[0] = escapec;
241 buf[1] = 0;
242 if ((v->v_str = str_cpy(buf)) == 0) {
243 error("Out of memory.");
244 return;
245 }
246 v->v_type = V_STR;
247 if (a->v_type != V_ERR)
248 setescape(a->v_str);
249 }
250
251 struct lcmd_arg arg_label[] = {
252 { "window", 1, ARG_NUM },
253 { "label", 1, ARG_STR },
254 { 0 }
255 };
256
257 void
258 l_label(v, a)
259 struct value *v;
260 struct value *a;
261 {
262 struct ww *w;
263
264 if ((w = vtowin(a, selwin)) == 0)
265 return;
266 if ((++a)->v_type != V_ERR && setlabel(w, a->v_str) < 0)
267 error("Out of memory.");
268 reframe();
269 }
270
271 struct lcmd_arg arg_foreground[] = {
272 { "window", 1, ARG_NUM },
273 { "flag", 1, ARG_ANY },
274 { 0 }
275 };
276
277 void
278 l_foreground(v, a)
279 struct value *v, *a;
280 {
281 struct ww *w;
282 char flag;
283
284 if ((w = vtowin(a, selwin)) == 0)
285 return;
286 v->v_type = V_NUM;
287 v->v_num = isfg(w);
288 flag = vtobool(++a, v->v_num, v->v_num);
289 if (flag == v->v_num)
290 return;
291 deletewin(w);
292 addwin(w, flag);
293 reframe();
294 }
295
296 struct lcmd_arg arg_terse[] = {
297 { "flag", 1, ARG_ANY },
298 { 0 }
299 };
300
301 void
302 l_terse(v, a)
303 struct value *v, *a;
304 {
305 v->v_type = V_NUM;
306 v->v_num = terse;
307 setterse(vtobool(a, terse, terse));
308 }
309
310 struct lcmd_arg arg_source[] = {
311 { "filename", 1, ARG_STR },
312 { 0 }
313 };
314
315 void
316 l_source(v, a)
317 struct value *v, *a;
318 {
319 v->v_type = V_NUM;
320 if (a->v_type != V_ERR && dosource(a->v_str) < 0) {
321 error("Can't open %s.", a->v_str);
322 v->v_num = -1;
323 } else
324 v->v_num = 0;
325 }
326
327 struct lcmd_arg arg_write[] = {
328 { "window", 1, ARG_NUM },
329 { "", 0, ARG_ANY|ARG_LIST },
330 { 0 }
331 };
332
333 void
334 l_write(v, a)
335 struct value *v;
336 struct value *a;
337 {
338 char buf[20];
339 struct ww *w;
340
341 if ((w = vtowin(a++, selwin)) == 0)
342 return;
343 while (a->v_type != V_ERR) {
344 if (a->v_type == V_NUM) {
345 (void) sprintf(buf, "%d", a->v_num);
346 (void) write(w->ww_pty, buf, strlen(buf));
347 } else
348 (void) write(w->ww_pty, a->v_str, strlen(a->v_str));
349 if ((++a)->v_type != V_ERR)
350 (void) write(w->ww_pty, " ", 1);
351 }
352 }
353
354 struct lcmd_arg arg_close[] = {
355 { "window", 1, ARG_ANY|ARG_LIST },
356 { 0 }
357 };
358
359 void
360 l_close(v, a)
361 struct value *v;
362 struct value *a;
363 {
364 struct ww *w;
365
366 if (a->v_type == V_STR && str_match(a->v_str, "all", 3))
367 closewin((struct ww *)0);
368 else
369 for (; a->v_type != V_ERR; a++)
370 if ((w = vtowin(a, (struct ww *)0)) != 0)
371 closewin(w);
372 }
373
374 struct lcmd_arg arg_cursormodes[] = {
375 { "modes", 1, ARG_NUM },
376 { 0 }
377 };
378
379 void
380 l_cursormodes(v, a)
381 struct value *v, *a;
382 {
383
384 v->v_type = V_NUM;
385 v->v_num = wwcursormodes;
386 if (a->v_type != V_ERR)
387 wwsetcursormodes(a->v_num);
388 }
389
390 struct lcmd_arg arg_unset[] = {
391 { "variable", 1, ARG_ANY },
392 { 0 }
393 };
394
395 void
396 l_unset(v, a)
397 struct value *v, *a;
398 {
399 v->v_type = V_NUM;
400 switch (a->v_type) {
401 case V_ERR:
402 v->v_num = -1;
403 return;
404 case V_NUM:
405 if ((a->v_str = str_itoa(a->v_num)) == 0) {
406 error("Out of memory.");
407 v->v_num = -1;
408 return;
409 }
410 a->v_type = V_STR;
411 break;
412 }
413 v->v_num = var_unset(a->v_str);
414 }
415
416 struct ww *
417 vtowin(v, w)
418 struct value *v;
419 struct ww *w;
420 {
421 switch (v->v_type) {
422 case V_ERR:
423 if (w != 0)
424 return w;
425 error("No window specified.");
426 return 0;
427 case V_STR:
428 error("%s: No such window.", v->v_str);
429 return 0;
430 }
431 if (v->v_num < 1 || v->v_num > NWINDOW
432 || (w = window[v->v_num - 1]) == 0) {
433 error("%d: No such window.", v->v_num);
434 return 0;
435 }
436 return w;
437 }
438
439 char
440 vtobool(v, def, err)
441 struct value *v;
442 char def, err;
443 {
444 switch (v->v_type) {
445 case V_NUM:
446 return v->v_num != 0;
447 case V_STR:
448 if (str_match(v->v_str, "true", 1)
449 || str_match(v->v_str, "on", 2)
450 || str_match(v->v_str, "yes", 1))
451 return 1;
452 else if (str_match(v->v_str, "false", 1)
453 || str_match(v->v_str, "off", 2)
454 || str_match(v->v_str, "no", 1))
455 return 0;
456 else {
457 error("%s: Illegal boolean value.", v->v_str);
458 return err;
459 }
460 /*NOTREACHED*/
461 case V_ERR:
462 return def;
463 }
464 /*NOTREACHED*/
465 return (0);
466 }