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