xref: /original-bsd/usr.bin/window/lcmd1.c (revision 00986467)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)lcmd1.c	3.34 (Berkeley) 08/04/88";
20 #endif /* not lint */
21 
22 #include "defs.h"
23 #include "string.h"
24 #include "value.h"
25 #include "lcmd.h"
26 #include "var.h"
27 
28 struct lcmd_arg arg_window[] = {
29 	{ "row",	1,	ARG_NUM },
30 	{ "column",	1,	ARG_NUM },
31 	{ "nrows",	2,	ARG_NUM },
32 	{ "ncols",	2,	ARG_NUM },
33 	{ "nlines",	2,	ARG_NUM },
34 	{ "label",	1,	ARG_STR },
35 	{ "pty",	1,	ARG_ANY },
36 	{ "frame",	1,	ARG_ANY },
37 	{ "mapnl",	1,	ARG_ANY },
38 	{ "keepopen",	1,	ARG_ANY },
39 	{ "smooth",	1,	ARG_ANY },
40 	{ "shell",	1,	ARG_STR|ARG_LIST },
41 	0
42 };
43 
44 l_window(v, a)
45 struct value *v;
46 register struct value *a;
47 {
48 	struct ww *w;
49 	int col, row, ncol, nrow, id, nline;
50 	char *label;
51 	char haspty, hasframe, mapnl, keepopen, smooth;
52 	char *shf, **sh;
53 	char *argv[sizeof default_shell / sizeof *default_shell];
54 	register char **pp;
55 
56 	if ((id = findid()) < 0)
57 		return;
58 	row = a->v_type == V_ERR ? 1 : a->v_num;
59 	a++;
60 	col = a->v_type == V_ERR ? 0 : a->v_num;
61 	a++;
62 	nrow = a->v_type == V_ERR ? wwnrow - row : a->v_num;
63 	a++;
64 	ncol = a->v_type == V_ERR ? wwncol - col : a->v_num;
65 	a++;
66 	nline = a->v_type == V_ERR ? default_nline : a->v_num;
67 	a++;
68 	label = a->v_type == V_ERR ? 0 : a->v_str;
69 	if ((haspty = vtobool(++a, 1, -1)) < 0)
70 		return;
71 	if ((hasframe = vtobool(++a, 1, -1)) < 0)
72 		return;
73 	if ((mapnl = vtobool(++a, !haspty, -1)) < 0)
74 		return;
75 	if ((keepopen = vtobool(++a, 0, -1)) < 0)
76 		return;
77 	if ((smooth = vtobool(++a, default_smooth, -1)) < 0)
78 		return;
79 	if ((++a)->v_type != V_ERR) {
80 		for (pp = argv; a->v_type != V_ERR &&
81 		     pp < &argv[sizeof argv/sizeof *argv-1]; pp++, a++)
82 			*pp = a->v_str;
83 		*pp = 0;
84 		shf = *(sh = argv);
85 		if (*sh = rindex(shf, '/'))
86 			(*sh)++;
87 		else
88 			*sh = shf;
89 	} else {
90 		sh = default_shell;
91 		shf = default_shellfile;
92 	}
93 	if ((w = openwin(id, row, col, nrow, ncol, nline, label, haspty,
94 	    hasframe, shf, sh)) == 0)
95 		return;
96 	w->ww_mapnl = mapnl;
97 	w->ww_keepopen = keepopen;
98 	w->ww_noupdate = !smooth;
99 	v->v_type = V_NUM;
100 	v->v_num = id + 1;
101 }
102 
103 struct lcmd_arg arg_def_nline[] = {
104 	{ "nlines",	1,	ARG_NUM },
105 	0
106 };
107 
108 l_def_nline(v, a)
109 register struct value *v, *a;
110 {
111 	v->v_num = default_nline;
112 	v->v_type = V_NUM;
113 	if (a->v_type != V_ERR)
114 		default_nline = a->v_num;
115 }
116 
117 struct lcmd_arg arg_smooth[] = {
118 	{ "window",	1,	ARG_NUM },
119 	{ "flag",	1,	ARG_ANY },
120 	0
121 };
122 
123 l_smooth(v, a)
124 register struct value *v, *a;
125 {
126 	struct ww *w;
127 
128 	v->v_type = V_NUM;
129 	v->v_num = 0;
130 	if ((w = vtowin(a++, selwin)) == 0)
131 		return;
132 	v->v_num = !w->ww_noupdate;
133 	w->ww_noupdate = !vtobool(a, v->v_num, v->v_num);
134 }
135 
136 struct lcmd_arg arg_def_smooth[] = {
137 	{ "flag",	1,	ARG_ANY },
138 	0
139 };
140 
141 l_def_smooth(v, a)
142 register struct value *v, *a;
143 {
144 	v->v_type = V_NUM;
145 	v->v_num = default_smooth;
146 	default_smooth = vtobool(a, v->v_num, v->v_num);
147 }
148 
149 struct lcmd_arg arg_select[] = {
150 	{ "window",	1,	ARG_NUM },
151 	0
152 };
153 
154 l_select(v, a)
155 register struct value *v, *a;
156 {
157 	struct ww *w;
158 
159 	v->v_type = V_NUM;
160 	v->v_num = selwin ? selwin->ww_id + 1 : -1;
161 	if (a->v_type == V_ERR)
162 		return;
163 	if ((w = vtowin(a, (struct ww *)0)) == 0)
164 		return;
165 	setselwin(w);
166 }
167 
168 struct lcmd_arg arg_debug[] = {
169 	{ "flag",	1,	ARG_ANY },
170 	0
171 };
172 
173 l_debug(v, a)
174 register struct value *v, *a;
175 {
176 	v->v_type = V_NUM;
177 	v->v_num = debug;
178 	debug = vtobool(a, debug, debug);
179 }
180 
181 struct lcmd_arg arg_escape[] = {
182 	{ "escapec",	1,	ARG_STR },
183 	0
184 };
185 
186 l_escape(v, a)
187 register struct value *v, *a;
188 {
189 	char buf[2];
190 
191 	buf[0] = escapec;
192 	buf[1] = 0;
193 	if ((v->v_str = str_cpy(buf)) == 0) {
194 		error("Out of memory.");
195 		return;
196 	}
197 	v->v_type = V_STR;
198 	if (a->v_type != V_ERR)
199 		setescape(a->v_str);
200 }
201 
202 struct lcmd_arg arg_label[] = {
203 	{ "window",	1,	ARG_NUM },
204 	{ "label",	1,	ARG_STR },
205 	0
206 };
207 
208 /*ARGSUSED*/
209 l_label(v, a)
210 struct value *v;
211 register struct value *a;
212 {
213 	struct ww *w;
214 
215 	if ((w = vtowin(a, selwin)) == 0)
216 		return;
217 	if ((++a)->v_type != V_ERR && setlabel(w, a->v_str) < 0)
218 		error("Out of memory.");
219 	reframe();
220 }
221 
222 struct lcmd_arg arg_foreground[] = {
223 	{ "window",	1,	ARG_NUM },
224 	{ "flag",	1,	ARG_ANY },
225 	0
226 };
227 
228 l_foreground(v, a)
229 register struct value *v, *a;
230 {
231 	struct ww *w;
232 	char flag;
233 
234 	if ((w = vtowin(a, selwin)) == 0)
235 		return;
236 	v->v_type = V_NUM;
237 	v->v_num = isfg(w);
238 	flag = vtobool(++a, v->v_num, v->v_num);
239 	if (flag == v->v_num)
240 		return;
241 	deletewin(w);
242 	addwin(w, flag);
243 	reframe();
244 }
245 
246 struct lcmd_arg arg_terse[] = {
247 	{ "flag",	1,	ARG_ANY },
248 	0
249 };
250 
251 l_terse(v, a)
252 register struct value *v, *a;
253 {
254 	v->v_type = V_NUM;
255 	v->v_num = terse;
256 	setterse(vtobool(a, terse, terse));
257 }
258 
259 struct lcmd_arg arg_source[] = {
260 	{ "filename",	1,	ARG_STR },
261 	0
262 };
263 
264 l_source(v, a)
265 register struct value *v, *a;
266 {
267 	v->v_type = V_NUM;
268 	if (a->v_type != V_ERR && dosource(a->v_str) < 0) {
269 		error("Can't open %s.", a->v_str);
270 		v->v_num = -1;
271 	} else
272 		v->v_num = 0;
273 }
274 
275 struct lcmd_arg arg_write[] = {
276 	{ "window",	1,	ARG_NUM },
277 	{ "",		0,	ARG_ANY|ARG_LIST },
278 	0
279 };
280 
281 /*ARGSUSED*/
282 l_write(v, a)
283 struct value *v;
284 register struct value *a;
285 {
286 	char buf[20];
287 	struct ww *w;
288 
289 	if ((w = vtowin(a++, selwin)) == 0)
290 		return;
291 	while (a->v_type != V_ERR) {
292 		if (a->v_type == V_NUM) {
293 			(void) sprintf(buf, "%d", a->v_num);
294 			(void) write(w->ww_pty, buf, strlen(buf));
295 		} else
296 			(void) write(w->ww_pty, a->v_str, strlen(a->v_str));
297 		if ((++a)->v_type != V_ERR)
298 			(void) write(w->ww_pty, " ", 1);
299 	}
300 }
301 
302 struct lcmd_arg arg_close[] = {
303 	{ "window",	1,	ARG_ANY|ARG_LIST },
304 	0
305 };
306 
307 /*ARGSUSED*/
308 l_close(v, a)
309 struct value *v;
310 register struct value *a;
311 {
312 	struct ww *w;
313 
314 	if (a->v_type == V_STR && str_match(a->v_str, "all", 3))
315 		closewin((struct ww *)0);
316 	else
317 		for (; a->v_type != V_ERR; a++)
318 			if ((w = vtowin(a, (struct ww *)0)) != 0)
319 				closewin(w);
320 }
321 
322 struct lcmd_arg arg_cursormodes[] = {
323 	{ "modes",	1,	ARG_NUM },
324 	0
325 };
326 
327 l_cursormodes(v, a)
328 register struct value *v, *a;
329 {
330 
331 	v->v_type = V_NUM;
332 	v->v_num = wwcursormodes;
333 	if (a->v_type != V_ERR)
334 		wwsetcursormodes(a->v_num);
335 }
336 
337 struct lcmd_arg arg_unset[] = {
338 	{ "variable",	1,	ARG_ANY },
339 	0
340 };
341 
342 l_unset(v, a)
343 register struct value *v, *a;
344 {
345 	v->v_type = V_NUM;
346 	switch (a->v_type) {
347 	case V_ERR:
348 		v->v_num = -1;
349 		return;
350 	case V_NUM:
351 		if ((a->v_str = str_itoa(a->v_num)) == 0) {
352 			error("Out of memory.");
353 			v->v_num = -1;
354 			return;
355 		}
356 		a->v_type = V_STR;
357 		break;
358 	}
359 	v->v_num = var_unset(a->v_str);
360 }
361 
362 struct ww *
363 vtowin(v, w)
364 register struct value *v;
365 struct ww *w;
366 {
367 	switch (v->v_type) {
368 	case V_ERR:
369 		if (w != 0)
370 			return w;
371 		error("No window specified.");
372 		return 0;
373 	case V_STR:
374 		error("%s: No such window.", v->v_str);
375 		return 0;
376 	}
377 	if (v->v_num < 1 || v->v_num > NWINDOW
378 	    || (w = window[v->v_num - 1]) == 0) {
379 		error("%d: No such window.", v->v_num);
380 		return 0;
381 	}
382 	return w;
383 }
384 
385 vtobool(v, def, err)
386 register struct value *v;
387 char def, err;
388 {
389 	switch (v->v_type) {
390 	case V_NUM:
391 		return v->v_num != 0;
392 	case V_STR:
393 		if (str_match(v->v_str, "true", 1)
394 		    || str_match(v->v_str, "on", 2)
395 		    || str_match(v->v_str, "yes", 1))
396 			return 1;
397 		else if (str_match(v->v_str, "false", 1)
398 		    || str_match(v->v_str, "off", 2)
399 		    || str_match(v->v_str, "no", 1))
400 			return 0;
401 		else {
402 			error("%s: Illegal boolean value.", v->v_str);
403 			return err;
404 		}
405 		/*NOTREACHED*/
406 	case V_ERR:
407 		return def;
408 	}
409 	/*NOTREACHED*/
410 }
411