Add spl's around queue manipulation
[unix-history] / usr / src / usr.bin / window / lcmd1.c
CommitLineData
bd002416 1#ifndef lint
855d0f8f 2static char sccsid[] = "@(#)lcmd1.c 3.26 %G%";
bd002416
EW
3#endif
4
5#include "defs.h"
1c0ca238
EW
6#include "string.h"
7#include "value.h"
8#include "lcmd.h"
bb4a0c0b 9#include "var.h"
1c0ca238
EW
10
11struct lcmd_arg arg_window[] = {
855d0f8f
EW
12 { "row", 1, ARG_NUM },
13 { "column", 1, ARG_NUM },
14 { "nrows", 2, ARG_NUM },
15 { "ncols", 2, ARG_NUM },
1c0ca238
EW
16 { "nlines", 2, ARG_NUM },
17 { "label", 1, ARG_STR },
7ecf4dca
EW
18 { "pty", 1, ARG_ANY },
19 { "frame", 1, ARG_ANY },
74bac1db 20 { "mapnl", 1, ARG_ANY },
855d0f8f 21 { "shell", 1, ARG_ANY|ARG_LIST },
4222244b 22 0
1c0ca238
EW
23};
24
4222244b 25l_window(v, a)
855d0f8f
EW
26struct value *v;
27register struct value *a;
bd002416 28{
855d0f8f 29 struct ww *w;
4cbe417d 30 int col, row, ncol, nrow, id, nline;
1c0ca238 31 char *label;
74bac1db 32 char haspty, hasframe, mapnl;
7ecf4dca
EW
33 char *shf, **sh;
34 char *argv[sizeof shell / sizeof *shell];
855d0f8f 35 register char **pp;
bd002416 36
03e75950 37 if ((id = findid()) < 0)
bd002416 38 return;
855d0f8f
EW
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;
7ecf4dca
EW
71 } else {
72 sh = shell;
73 shf = shellfile;
74 }
74bac1db
EW
75 if ((w = openwin(id, row, col, nrow, ncol, nline, label, haspty,
76 hasframe, shf, sh)) == 0)
77 return;
78 w->ww_mapnl = mapnl;
1c0ca238 79 v->v_type = V_NUM;
7a57b3e2 80 v->v_num = id + 1;
4cbe417d
EW
81}
82
1f12ea41 83struct lcmd_arg arg_nline[] = {
1c0ca238 84 { "nlines", 1, ARG_NUM },
4222244b 85 0
1c0ca238
EW
86};
87
1f12ea41 88l_nline(v, a)
4222244b 89register struct value *v, *a;
4cbe417d 90{
1c0ca238
EW
91 v->v_num = nbufline;
92 v->v_type = V_NUM;
4222244b
EW
93 if (a->v_type != V_ERR)
94 nbufline = a->v_num;
bd002416
EW
95}
96
1c0ca238
EW
97struct lcmd_arg arg_select[] = {
98 { "window", 1, ARG_NUM },
4222244b 99 0
1c0ca238
EW
100};
101
4222244b
EW
102l_select(v, a)
103register struct value *v, *a;
bd002416
EW
104{
105 struct ww *w;
106
1c0ca238 107 v->v_type = V_NUM;
7a57b3e2 108 v->v_num = selwin ? selwin->ww_id + 1 : -1;
4222244b 109 if (a->v_type == V_ERR)
bd002416 110 return;
4222244b 111 if ((w = vtowin(a)) == 0)
1c0ca238 112 return;
bd002416
EW
113 setselwin(w);
114}
115
b0640643
EW
116struct lcmd_arg arg_debug[] = {
117 { "flag", 1, ARG_ANY },
1f12ea41 118 0
b0640643
EW
119};
120
4222244b
EW
121l_debug(v, a)
122register struct value *v, *a;
b0640643
EW
123{
124 v->v_type = V_NUM;
125 v->v_num = debug;
4222244b 126 debug = vtobool(a, 1, debug);
b0640643
EW
127}
128
1c0ca238 129struct lcmd_arg arg_escape[] = {
4222244b
EW
130 { "escapec", 1, ARG_STR },
131 0
1c0ca238
EW
132};
133
4222244b
EW
134l_escape(v, a)
135register struct value *v, *a;
bd002416 136{
0e64e422
EW
137 char buf[2];
138
139 buf[0] = escapec;
140 buf[1] = 0;
141 if ((v->v_str = str_cpy(buf)) == 0) {
1c0ca238
EW
142 error("Out of memory.");
143 return;
144 }
145 v->v_type = V_STR;
4222244b
EW
146 if (a->v_type != V_ERR)
147 setescape(a->v_str);
bd002416
EW
148}
149
1c0ca238
EW
150struct lcmd_arg arg_label[] = {
151 { "window", 1, ARG_NUM },
152 { "label", 1, ARG_STR },
4222244b 153 0
1c0ca238
EW
154};
155
156/*ARGSUSED*/
4222244b 157l_label(v, a)
c654e7d4
EW
158struct value *v;
159register struct value *a;
bd002416
EW
160{
161 struct ww *w;
162
4222244b 163 if ((w = vtowin(a)) == 0)
bd002416 164 return;
4222244b 165 if ((++a)->v_type != V_ERR && setlabel(w, a->v_str) < 0)
bd002416
EW
166 error("Out of memory.");
167 reframe();
168}
169
e1dba5dd
EW
170struct lcmd_arg arg_foreground[] = {
171 { "window", 1, ARG_NUM },
172 { "flag", 1, ARG_ANY },
173 0
174};
175
176l_foreground(v, a)
177register 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);
d7c2cbb0 190 addwin(w, flag);
e1dba5dd
EW
191 reframe();
192}
193
1c0ca238 194struct lcmd_arg arg_terse[] = {
37661cbd 195 { "flag", 1, ARG_ANY },
4222244b 196 0
1c0ca238
EW
197};
198
4222244b
EW
199l_terse(v, a)
200register struct value *v, *a;
bd002416 201{
1c0ca238
EW
202 v->v_type = V_NUM;
203 v->v_num = terse;
ffa20db4 204 setterse(vtobool(a, 1, terse));
bd002416
EW
205}
206
1c0ca238
EW
207struct lcmd_arg arg_source[] = {
208 { "filename", 1, ARG_STR },
4222244b 209 0
1c0ca238
EW
210};
211
4222244b
EW
212l_source(v, a)
213register struct value *v, *a;
bd002416 214{
4222244b
EW
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);
1c0ca238
EW
218 v->v_num = -1;
219 } else
220 v->v_num = 0;
bd002416
EW
221}
222
855d0f8f
EW
223struct lcmd_arg arg_write[] = {
224 { "window", 1, ARG_NUM },
225 { "", 0, ARG_ANY|ARG_LIST },
226 0
227};
228
1c0ca238 229/*ARGSUSED*/
4222244b 230l_write(v, a)
c654e7d4
EW
231struct value *v;
232register struct value *a;
bd002416 233{
1f12ea41 234 char buf[20];
bd002416
EW
235 struct ww *w;
236
1f12ea41 237 if ((w = vtowin(a++)) == 0)
bd002416 238 return;
1f12ea41
EW
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 }
bd002416
EW
248}
249
855d0f8f
EW
250struct lcmd_arg arg_close[] = {
251 { "window", 1, ARG_ANY|ARG_LIST },
252 0
253};
254
1c0ca238 255/*ARGSUSED*/
4222244b 256l_close(v, a)
c654e7d4
EW
257struct value *v;
258register struct value *a;
8aee01cc 259{
1c0ca238 260 struct ww *w;
8aee01cc 261
1f12ea41 262 if (a->v_type == V_STR && str_match(a->v_str, "all", 1))
a4c3bba5 263 c_close((struct ww *)0);
1f12ea41
EW
264 else
265 for (; a->v_type != V_ERR; a++)
266 if ((w = vtowin(a)) != 0)
267 c_close(w);
8aee01cc
EW
268}
269
7a57b3e2
EW
270struct lcmd_arg arg_cursormodes[] = {
271 { "modes", 1, ARG_NUM },
4222244b 272 0
7a57b3e2
EW
273};
274
4222244b
EW
275l_cursormodes(v, a)
276register struct value *v, *a;
7a57b3e2 277{
7a57b3e2
EW
278
279 v->v_type = V_NUM;
280 v->v_num = wwcursormodes;
4222244b
EW
281 if (a->v_type != V_ERR)
282 wwsetcursormodes(a->v_num);
7a57b3e2
EW
283}
284
b65b69eb
EW
285struct lcmd_arg arg_unset[] = {
286 { "variable", 1, ARG_ANY },
4222244b 287 0
b65b69eb
EW
288};
289
4222244b
EW
290l_unset(v, a)
291register struct value *v, *a;
b65b69eb 292{
b65b69eb 293 v->v_type = V_NUM;
4222244b 294 switch (a->v_type) {
b65b69eb
EW
295 case V_ERR:
296 v->v_num = -1;
297 return;
298 case V_NUM:
4222244b 299 if ((a->v_str = str_itoa(a->v_num)) == 0) {
b65b69eb
EW
300 error("Out of memory.");
301 v->v_num = -1;
302 return;
303 }
4222244b 304 a->v_type = V_STR;
b65b69eb
EW
305 break;
306 }
4222244b 307 v->v_num = var_unset(a->v_str);
b65b69eb
EW
308}
309
bd002416 310struct ww *
1c0ca238
EW
311vtowin(v)
312register struct value *v;
bd002416 313{
bd002416
EW
314 struct ww *w;
315
1c0ca238
EW
316 switch (v->v_type) {
317 case V_ERR:
1f12ea41 318 error("No window specified.");
1c0ca238
EW
319 return 0;
320 case V_STR:
1f12ea41 321 error("%s: No such window.", v->v_str);
1c0ca238
EW
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);
bd002416
EW
327 return 0;
328 }
329 return w;
330}
1c0ca238
EW
331
332vtobool(v, def, err)
333register struct value *v;
334char 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}
7ecf4dca
EW
358
359mkargv(p, argv, n)
360register char *p;
361register char **argv;
362register 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}