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