Annotation of src/usr.bin/window/lcmd1.c, Revision 1.6
1.6 ! mpech 1: /* $OpenBSD: lcmd1.c,v 1.5 1997/02/25 00:04:07 downsj Exp $ */
1.2 niklas 2: /* $NetBSD: lcmd1.c,v 1.6 1996/02/08 20:45:00 mycroft Exp $ */
1.1 deraadt 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. All advertising materials mentioning features or use of this software
20: * must display the following acknowledgement:
21: * This product includes software developed by the University of
22: * California, Berkeley and its contributors.
23: * 4. Neither the name of the University nor the names of its contributors
24: * may be used to endorse or promote products derived from this software
25: * without specific prior written permission.
26: *
27: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37: * SUCH DAMAGE.
38: */
39:
40: #ifndef lint
41: #if 0
42: static char sccsid[] = "@(#)lcmd1.c 8.1 (Berkeley) 6/6/93";
43: #else
1.6 ! mpech 44: static char rcsid[] = "$OpenBSD: lcmd1.c,v 1.5 1997/02/25 00:04:07 downsj Exp $";
1.1 deraadt 45: #endif
46: #endif /* not lint */
47:
48: #include "defs.h"
49: #include "string.h"
50: #include "value.h"
51: #include "lcmd.h"
52: #include "var.h"
53: #include <string.h>
54:
55: struct lcmd_arg arg_window[] = {
56: { "row", 1, ARG_NUM },
57: { "column", 1, ARG_NUM },
58: { "nrows", 2, ARG_NUM },
59: { "ncols", 2, ARG_NUM },
60: { "nlines", 2, ARG_NUM },
61: { "label", 1, ARG_STR },
62: { "pty", 1, ARG_ANY },
63: { "frame", 1, ARG_ANY },
64: { "mapnl", 1, ARG_ANY },
65: { "keepopen", 1, ARG_ANY },
66: { "smooth", 1, ARG_ANY },
67: { "shell", 1, ARG_STR|ARG_LIST },
68: 0
69: };
70:
71: l_window(v, a)
72: struct value *v;
1.6 ! mpech 73: struct value *a;
1.1 deraadt 74: {
75: struct ww *w;
76: int col, row, ncol, nrow, id, nline;
77: char *label;
1.2 niklas 78: int haspty, hasframe, mapnl, keepopen, smooth;
1.1 deraadt 79: char *shf, **sh;
80: char *argv[sizeof default_shell / sizeof *default_shell];
1.6 ! mpech 81: char **pp;
1.1 deraadt 82:
83: if ((id = findid()) < 0)
84: return;
85: row = a->v_type == V_ERR ? 1 : a->v_num;
86: a++;
87: col = a->v_type == V_ERR ? 0 : a->v_num;
88: a++;
89: nrow = a->v_type == V_ERR ? wwnrow - row : a->v_num;
90: a++;
91: ncol = a->v_type == V_ERR ? wwncol - col : a->v_num;
92: a++;
93: nline = a->v_type == V_ERR ? default_nline : a->v_num;
94: a++;
95: label = a->v_type == V_ERR ? 0 : a->v_str;
96: if ((haspty = vtobool(++a, 1, -1)) < 0)
97: return;
98: if ((hasframe = vtobool(++a, 1, -1)) < 0)
99: return;
100: if ((mapnl = vtobool(++a, !haspty, -1)) < 0)
101: return;
102: if ((keepopen = vtobool(++a, 0, -1)) < 0)
103: return;
104: if ((smooth = vtobool(++a, default_smooth, -1)) < 0)
105: return;
106: if ((++a)->v_type != V_ERR) {
107: for (pp = argv; a->v_type != V_ERR &&
108: pp < &argv[sizeof argv/sizeof *argv-1]; pp++, a++)
109: *pp = a->v_str;
110: *pp = 0;
111: shf = *(sh = argv);
1.4 millert 112: if (*sh = strrchr(shf, '/'))
1.1 deraadt 113: (*sh)++;
114: else
115: *sh = shf;
116: } else {
117: sh = default_shell;
118: shf = default_shellfile;
119: }
1.2 niklas 120: if ((w = openwin(id, row, col, nrow, ncol, nline, label,
121: haspty ? WWT_PTY : WWT_SOCKET, hasframe ? WWU_HASFRAME : 0, shf,
122: sh)) == 0)
123: return;
124: if (mapnl)
125: SET(w->ww_wflags, WWW_MAPNL);
126: else
127: CLR(w->ww_wflags, WWW_MAPNL);
128: if (keepopen)
129: SET(w->ww_uflags, WWU_KEEPOPEN);
130: else
131: CLR(w->ww_uflags, WWU_KEEPOPEN);
132: if (!smooth)
133: SET(w->ww_wflags, WWW_NOUPDATE);
134: else
135: CLR(w->ww_wflags, WWW_NOUPDATE);
1.1 deraadt 136: v->v_type = V_NUM;
137: v->v_num = id + 1;
138: }
139:
140: struct lcmd_arg arg_def_nline[] = {
141: { "nlines", 1, ARG_NUM },
142: 0
143: };
144:
145: l_def_nline(v, a)
1.6 ! mpech 146: struct value *v, *a;
1.1 deraadt 147: {
148: v->v_num = default_nline;
149: v->v_type = V_NUM;
150: if (a->v_type != V_ERR)
151: default_nline = a->v_num;
152: }
153:
154: struct lcmd_arg arg_smooth[] = {
155: { "window", 1, ARG_NUM },
156: { "flag", 1, ARG_ANY },
157: 0
158: };
159:
160: l_smooth(v, a)
1.6 ! mpech 161: struct value *v, *a;
1.1 deraadt 162: {
163: struct ww *w;
164:
165: v->v_type = V_NUM;
166: v->v_num = 0;
167: if ((w = vtowin(a++, selwin)) == 0)
168: return;
1.2 niklas 169: v->v_num = ISSET(w->ww_wflags, WWW_NOUPDATE) == 0;
170: if (!vtobool(a, v->v_num, v->v_num))
171: SET(w->ww_wflags, WWW_NOUPDATE);
172: else
173: CLR(w->ww_wflags, WWW_NOUPDATE);
1.1 deraadt 174: }
175:
176: struct lcmd_arg arg_def_smooth[] = {
177: { "flag", 1, ARG_ANY },
178: 0
179: };
180:
181: l_def_smooth(v, a)
1.6 ! mpech 182: struct value *v, *a;
1.1 deraadt 183: {
184: v->v_type = V_NUM;
185: v->v_num = default_smooth;
186: default_smooth = vtobool(a, v->v_num, v->v_num);
187: }
188:
189: struct lcmd_arg arg_select[] = {
190: { "window", 1, ARG_NUM },
191: 0
192: };
193:
194: l_select(v, a)
1.6 ! mpech 195: struct value *v, *a;
1.1 deraadt 196: {
197: struct ww *w;
198:
199: v->v_type = V_NUM;
200: v->v_num = selwin ? selwin->ww_id + 1 : -1;
201: if (a->v_type == V_ERR)
202: return;
203: if ((w = vtowin(a, (struct ww *)0)) == 0)
204: return;
205: setselwin(w);
206: }
207:
208: struct lcmd_arg arg_debug[] = {
209: { "flag", 1, ARG_ANY },
210: 0
211: };
212:
213: l_debug(v, a)
1.6 ! mpech 214: struct value *v, *a;
1.1 deraadt 215: {
216: v->v_type = V_NUM;
217: v->v_num = debug;
218: debug = vtobool(a, debug, debug);
219: }
220:
221: struct lcmd_arg arg_escape[] = {
222: { "escapec", 1, ARG_STR },
223: 0
224: };
225:
226: l_escape(v, a)
1.6 ! mpech 227: struct value *v, *a;
1.1 deraadt 228: {
229: char buf[2];
230:
231: buf[0] = escapec;
232: buf[1] = 0;
233: if ((v->v_str = str_cpy(buf)) == 0) {
234: error("Out of memory.");
235: return;
236: }
237: v->v_type = V_STR;
238: if (a->v_type != V_ERR)
239: setescape(a->v_str);
240: }
241:
242: struct lcmd_arg arg_label[] = {
243: { "window", 1, ARG_NUM },
244: { "label", 1, ARG_STR },
245: 0
246: };
247:
248: /*ARGSUSED*/
249: l_label(v, a)
250: struct value *v;
1.6 ! mpech 251: struct value *a;
1.1 deraadt 252: {
253: struct ww *w;
254:
255: if ((w = vtowin(a, selwin)) == 0)
256: return;
257: if ((++a)->v_type != V_ERR && setlabel(w, a->v_str) < 0)
258: error("Out of memory.");
259: reframe();
260: }
261:
262: struct lcmd_arg arg_foreground[] = {
263: { "window", 1, ARG_NUM },
264: { "flag", 1, ARG_ANY },
265: 0
266: };
267:
268: l_foreground(v, a)
1.6 ! mpech 269: struct value *v, *a;
1.1 deraadt 270: {
271: struct ww *w;
272: char flag;
273:
274: if ((w = vtowin(a, selwin)) == 0)
275: return;
276: v->v_type = V_NUM;
277: v->v_num = isfg(w);
278: flag = vtobool(++a, v->v_num, v->v_num);
279: if (flag == v->v_num)
280: return;
281: deletewin(w);
282: addwin(w, flag);
283: reframe();
284: }
285:
286: struct lcmd_arg arg_terse[] = {
287: { "flag", 1, ARG_ANY },
288: 0
289: };
290:
291: l_terse(v, a)
1.6 ! mpech 292: struct value *v, *a;
1.1 deraadt 293: {
294: v->v_type = V_NUM;
295: v->v_num = terse;
296: setterse(vtobool(a, terse, terse));
297: }
298:
299: struct lcmd_arg arg_source[] = {
300: { "filename", 1, ARG_STR },
301: 0
302: };
303:
304: l_source(v, a)
1.6 ! mpech 305: struct value *v, *a;
1.1 deraadt 306: {
307: v->v_type = V_NUM;
308: if (a->v_type != V_ERR && dosource(a->v_str) < 0) {
309: error("Can't open %s.", a->v_str);
310: v->v_num = -1;
311: } else
312: v->v_num = 0;
313: }
314:
315: struct lcmd_arg arg_write[] = {
316: { "window", 1, ARG_NUM },
317: { "", 0, ARG_ANY|ARG_LIST },
318: 0
319: };
320:
321: /*ARGSUSED*/
322: l_write(v, a)
323: struct value *v;
1.6 ! mpech 324: struct value *a;
1.1 deraadt 325: {
326: char buf[20];
327: struct ww *w;
328:
329: if ((w = vtowin(a++, selwin)) == 0)
330: return;
331: while (a->v_type != V_ERR) {
332: if (a->v_type == V_NUM) {
333: (void) sprintf(buf, "%d", a->v_num);
334: (void) write(w->ww_pty, buf, strlen(buf));
335: } else
336: (void) write(w->ww_pty, a->v_str, strlen(a->v_str));
337: if ((++a)->v_type != V_ERR)
338: (void) write(w->ww_pty, " ", 1);
339: }
340: }
341:
342: struct lcmd_arg arg_close[] = {
343: { "window", 1, ARG_ANY|ARG_LIST },
344: 0
345: };
346:
347: /*ARGSUSED*/
348: l_close(v, a)
349: struct value *v;
1.6 ! mpech 350: struct value *a;
1.1 deraadt 351: {
352: struct ww *w;
353:
354: if (a->v_type == V_STR && str_match(a->v_str, "all", 3))
355: closewin((struct ww *)0);
356: else
357: for (; a->v_type != V_ERR; a++)
358: if ((w = vtowin(a, (struct ww *)0)) != 0)
359: closewin(w);
360: }
361:
362: struct lcmd_arg arg_cursormodes[] = {
363: { "modes", 1, ARG_NUM },
364: 0
365: };
366:
367: l_cursormodes(v, a)
1.6 ! mpech 368: struct value *v, *a;
1.1 deraadt 369: {
370:
371: v->v_type = V_NUM;
372: v->v_num = wwcursormodes;
373: if (a->v_type != V_ERR)
374: wwsetcursormodes(a->v_num);
375: }
376:
377: struct lcmd_arg arg_unset[] = {
378: { "variable", 1, ARG_ANY },
379: 0
380: };
381:
382: l_unset(v, a)
1.6 ! mpech 383: struct value *v, *a;
1.1 deraadt 384: {
385: v->v_type = V_NUM;
386: switch (a->v_type) {
387: case V_ERR:
388: v->v_num = -1;
389: return;
390: case V_NUM:
391: if ((a->v_str = str_itoa(a->v_num)) == 0) {
392: error("Out of memory.");
393: v->v_num = -1;
394: return;
395: }
396: a->v_type = V_STR;
397: break;
398: }
399: v->v_num = var_unset(a->v_str);
400: }
401:
402: struct ww *
403: vtowin(v, w)
1.6 ! mpech 404: struct value *v;
1.1 deraadt 405: struct ww *w;
406: {
407: switch (v->v_type) {
408: case V_ERR:
409: if (w != 0)
410: return w;
411: error("No window specified.");
412: return 0;
413: case V_STR:
414: error("%s: No such window.", v->v_str);
415: return 0;
416: }
417: if (v->v_num < 1 || v->v_num > NWINDOW
418: || (w = window[v->v_num - 1]) == 0) {
419: error("%d: No such window.", v->v_num);
420: return 0;
421: }
422: return w;
423: }
424:
425: vtobool(v, def, err)
1.6 ! mpech 426: struct value *v;
1.1 deraadt 427: char def, err;
428: {
429: switch (v->v_type) {
430: case V_NUM:
431: return v->v_num != 0;
432: case V_STR:
433: if (str_match(v->v_str, "true", 1)
434: || str_match(v->v_str, "on", 2)
435: || str_match(v->v_str, "yes", 1))
436: return 1;
437: else if (str_match(v->v_str, "false", 1)
438: || str_match(v->v_str, "off", 2)
439: || str_match(v->v_str, "no", 1))
440: return 0;
441: else {
442: error("%s: Illegal boolean value.", v->v_str);
443: return err;
444: }
445: /*NOTREACHED*/
446: case V_ERR:
447: return def;
448: }
449: /*NOTREACHED*/
450: }