xref: /original-bsd/usr.bin/window/lcmd1.c (revision 1403a0cd)
1 #ifndef lint
2 static char sccsid[] = "@(#)lcmd1.c	3.30 04/24/85";
3 #endif
4 
5 /*
6  * Copyright (c) 1983 Regents of the University of California,
7  * All rights reserved.  Redistribution permitted subject to
8  * the terms of the Berkeley Software License Agreement.
9  */
10 
11 #include "defs.h"
12 #include "string.h"
13 #include "value.h"
14 #include "lcmd.h"
15 #include "var.h"
16 
17 struct lcmd_arg arg_window[] = {
18 	{ "row",	1,	ARG_NUM },
19 	{ "column",	1,	ARG_NUM },
20 	{ "nrows",	2,	ARG_NUM },
21 	{ "ncols",	2,	ARG_NUM },
22 	{ "nlines",	2,	ARG_NUM },
23 	{ "label",	1,	ARG_STR },
24 	{ "pty",	1,	ARG_ANY },
25 	{ "frame",	1,	ARG_ANY },
26 	{ "mapnl",	1,	ARG_ANY },
27 	{ "shell",	1,	ARG_STR|ARG_LIST },
28 	0
29 };
30 
31 l_window(v, a)
32 struct value *v;
33 register struct value *a;
34 {
35 	struct ww *w;
36 	int col, row, ncol, nrow, id, nline;
37 	char *label;
38 	char haspty, hasframe, mapnl;
39 	char *shf, **sh;
40 	char *argv[sizeof shell / sizeof *shell];
41 	register char **pp;
42 
43 	if ((id = findid()) < 0)
44 		return;
45 	row = a->v_type == V_ERR ? 1 : a->v_num;
46 	a++;
47 	col = a->v_type == V_ERR ? 0 : a->v_num;
48 	a++;
49 	nrow = a->v_type == V_ERR ? wwnrow - row : a->v_num;
50 	a++;
51 	ncol = a->v_type == V_ERR ? wwncol - col : a->v_num;
52 	a++;
53 	nline = a->v_type == V_ERR ? nbufline : a->v_num;
54 	a++;
55 	label = a->v_type == V_ERR ? 0 : a->v_str;
56 	if ((haspty = vtobool(++a, 1, -1)) < 0)
57 		return;
58 	if ((hasframe = vtobool(++a, 1, -1)) < 0)
59 		return;
60 	if ((mapnl = vtobool(++a, !haspty, -1)) < 0)
61 		return;
62 	if ((++a)->v_type != V_ERR) {
63 		for (pp = argv; a->v_type != V_ERR &&
64 		     pp < &argv[sizeof argv/sizeof *argv-1]; pp++, a++)
65 			*pp = a->v_str;
66 		*pp = 0;
67 		shf = *(sh = argv);
68 		if (*sh = rindex(shf, '/'))
69 			(*sh)++;
70 		else
71 			*sh = shf;
72 	} else {
73 		sh = shell;
74 		shf = shellfile;
75 	}
76 	if ((w = openwin(id, row, col, nrow, ncol, nline, label, haspty,
77 	    hasframe, shf, sh)) == 0)
78 		return;
79 	w->ww_mapnl = mapnl;
80 	v->v_type = V_NUM;
81 	v->v_num = id + 1;
82 }
83 
84 struct lcmd_arg arg_nline[] = {
85 	{ "nlines",	1,	ARG_NUM },
86 	0
87 };
88 
89 l_nline(v, a)
90 register struct value *v, *a;
91 {
92 	v->v_num = nbufline;
93 	v->v_type = V_NUM;
94 	if (a->v_type != V_ERR)
95 		nbufline = a->v_num;
96 }
97 
98 struct lcmd_arg arg_select[] = {
99 	{ "window",	1,	ARG_NUM },
100 	0
101 };
102 
103 l_select(v, a)
104 register struct value *v, *a;
105 {
106 	struct ww *w;
107 
108 	v->v_type = V_NUM;
109 	v->v_num = selwin ? selwin->ww_id + 1 : -1;
110 	if (a->v_type == V_ERR)
111 		return;
112 	if ((w = vtowin(a, (struct ww *)0)) == 0)
113 		return;
114 	setselwin(w);
115 }
116 
117 struct lcmd_arg arg_debug[] = {
118 	{ "flag",	1,	ARG_ANY },
119 	0
120 };
121 
122 l_debug(v, a)
123 register struct value *v, *a;
124 {
125 	v->v_type = V_NUM;
126 	v->v_num = debug;
127 	debug = vtobool(a, debug, debug);
128 }
129 
130 struct lcmd_arg arg_escape[] = {
131 	{ "escapec",	1,	ARG_STR },
132 	0
133 };
134 
135 l_escape(v, a)
136 register struct value *v, *a;
137 {
138 	char buf[2];
139 
140 	buf[0] = escapec;
141 	buf[1] = 0;
142 	if ((v->v_str = str_cpy(buf)) == 0) {
143 		error("Out of memory.");
144 		return;
145 	}
146 	v->v_type = V_STR;
147 	if (a->v_type != V_ERR)
148 		setescape(a->v_str);
149 }
150 
151 struct lcmd_arg arg_label[] = {
152 	{ "window",	1,	ARG_NUM },
153 	{ "label",	1,	ARG_STR },
154 	0
155 };
156 
157 /*ARGSUSED*/
158 l_label(v, a)
159 struct value *v;
160 register struct value *a;
161 {
162 	struct ww *w;
163 
164 	if ((w = vtowin(a, selwin)) == 0)
165 		return;
166 	if ((++a)->v_type != V_ERR && setlabel(w, a->v_str) < 0)
167 		error("Out of memory.");
168 	reframe();
169 }
170 
171 struct lcmd_arg arg_foreground[] = {
172 	{ "window",	1,	ARG_NUM },
173 	{ "flag",	1,	ARG_ANY },
174 	0
175 };
176 
177 l_foreground(v, a)
178 register struct value *v, *a;
179 {
180 	struct ww *w;
181 	char flag;
182 
183 	if ((w = vtowin(a, selwin)) == 0)
184 		return;
185 	v->v_type = V_NUM;
186 	v->v_num = isfg(w);
187 	flag = vtobool(++a, v->v_num, v->v_num);
188 	if (flag == v->v_num)
189 		return;
190 	deletewin(w);
191 	addwin(w, flag);
192 	reframe();
193 }
194 
195 struct lcmd_arg arg_terse[] = {
196 	{ "flag",	1,	ARG_ANY },
197 	0
198 };
199 
200 l_terse(v, a)
201 register struct value *v, *a;
202 {
203 	v->v_type = V_NUM;
204 	v->v_num = terse;
205 	setterse(vtobool(a, terse, terse));
206 }
207 
208 struct lcmd_arg arg_source[] = {
209 	{ "filename",	1,	ARG_STR },
210 	0
211 };
212 
213 l_source(v, a)
214 register struct value *v, *a;
215 {
216 	v->v_type = V_NUM;
217 	if (a->v_type != V_ERR && dosource(a->v_str) < 0) {
218 		error("Can't open %s.", a->v_str);
219 		v->v_num = -1;
220 	} else
221 		v->v_num = 0;
222 }
223 
224 struct lcmd_arg arg_write[] = {
225 	{ "window",	1,	ARG_NUM },
226 	{ "",		0,	ARG_ANY|ARG_LIST },
227 	0
228 };
229 
230 /*ARGSUSED*/
231 l_write(v, a)
232 struct value *v;
233 register struct value *a;
234 {
235 	char buf[20];
236 	struct ww *w;
237 
238 	if ((w = vtowin(a++, selwin)) == 0)
239 		return;
240 	while (a->v_type != V_ERR) {
241 		if (a->v_type == V_NUM) {
242 			(void) sprintf(buf, "%d", a->v_num);
243 			(void) write(w->ww_pty, buf, strlen(buf));
244 		} else
245 			(void) write(w->ww_pty, a->v_str, strlen(a->v_str));
246 		if ((++a)->v_type != V_ERR)
247 			(void) write(w->ww_pty, " ", 1);
248 	}
249 }
250 
251 struct lcmd_arg arg_close[] = {
252 	{ "window",	1,	ARG_ANY|ARG_LIST },
253 	0
254 };
255 
256 /*ARGSUSED*/
257 l_close(v, a)
258 struct value *v;
259 register struct value *a;
260 {
261 	struct ww *w;
262 
263 	if (a->v_type == V_STR && str_match(a->v_str, "all", 3))
264 		c_close((struct ww *)0);
265 	else
266 		for (; a->v_type != V_ERR; a++)
267 			if ((w = vtowin(a, (struct ww *)0)) != 0)
268 				c_close(w);
269 }
270 
271 struct lcmd_arg arg_cursormodes[] = {
272 	{ "modes",	1,	ARG_NUM },
273 	0
274 };
275 
276 l_cursormodes(v, a)
277 register struct value *v, *a;
278 {
279 
280 	v->v_type = V_NUM;
281 	v->v_num = wwcursormodes;
282 	if (a->v_type != V_ERR)
283 		wwsetcursormodes(a->v_num);
284 }
285 
286 struct lcmd_arg arg_unset[] = {
287 	{ "variable",	1,	ARG_ANY },
288 	0
289 };
290 
291 l_unset(v, a)
292 register struct value *v, *a;
293 {
294 	v->v_type = V_NUM;
295 	switch (a->v_type) {
296 	case V_ERR:
297 		v->v_num = -1;
298 		return;
299 	case V_NUM:
300 		if ((a->v_str = str_itoa(a->v_num)) == 0) {
301 			error("Out of memory.");
302 			v->v_num = -1;
303 			return;
304 		}
305 		a->v_type = V_STR;
306 		break;
307 	}
308 	v->v_num = var_unset(a->v_str);
309 }
310 
311 struct ww *
312 vtowin(v, w)
313 register struct value *v;
314 struct ww *w;
315 {
316 	switch (v->v_type) {
317 	case V_ERR:
318 		if (w != 0)
319 			return w;
320 		error("No window specified.");
321 		return 0;
322 	case V_STR:
323 		error("%s: No such window.", v->v_str);
324 		return 0;
325 	}
326 	if (v->v_num < 1 || v->v_num > NWINDOW
327 	    || (w = window[v->v_num - 1]) == 0) {
328 		error("%d: No such window.", v->v_num);
329 		return 0;
330 	}
331 	return w;
332 }
333 
334 vtobool(v, def, err)
335 register struct value *v;
336 char def, err;
337 {
338 	switch (v->v_type) {
339 	case V_NUM:
340 		return v->v_num != 0;
341 	case V_STR:
342 		if (str_match(v->v_str, "true", 1)
343 		    || str_match(v->v_str, "on", 2)
344 		    || str_match(v->v_str, "yes", 1))
345 			return 1;
346 		else if (str_match(v->v_str, "false", 1)
347 		    || str_match(v->v_str, "off", 2)
348 		    || str_match(v->v_str, "no", 1))
349 			return 0;
350 		else {
351 			error("%s: Illegal boolean value.", v->v_str);
352 			return err;
353 		}
354 		/*NOTREACHED*/
355 	case V_ERR:
356 		return def;
357 	}
358 	/*NOTREACHED*/
359 }
360