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