must free token_str if not used.
[unix-history] / usr / src / usr.bin / window / lcmd1.c
CommitLineData
bd002416 1#ifndef lint
11428657 2static char *sccsid = "@(#)lcmd1.c 3.11 83/12/09";
bd002416
EW
3#endif
4
5#include "defs.h"
1c0ca238
EW
6#include "string.h"
7#include "value.h"
8#include "lcmd.h"
9
10struct lcmd_arg arg_window[] = {
11428657
EW
11 { "row", 1, ARG_ANY },
12 { "column", 1, ARG_ANY },
13 { "nrows", 2, ARG_ANY },
14 { "ncols", 2, ARG_ANY },
1c0ca238
EW
15 { "nlines", 2, ARG_NUM },
16 { "label", 1, ARG_STR },
17 { 0, 0, 0 }
18};
19
20l_window(v)
21register struct value *v;
bd002416 22{
1c0ca238 23 register struct lcmd_arg *a = arg_window;
4cbe417d 24 int col, row, ncol, nrow, id, nline;
1c0ca238 25 char *label;
bd002416 26
03e75950 27 if ((id = findid()) < 0)
bd002416 28 return;
11428657
EW
29 row = a->arg_vtype != V_NUM ? 1 : a->arg_num;
30 col = (++a)->arg_vtype != V_NUM ? 0 : a->arg_num;
31 nrow = (++a)->arg_vtype != V_NUM ? wwnrow - row : a->arg_num;
32 ncol = (++a)->arg_vtype != V_NUM ? wwncol - col : a->arg_num;
1c0ca238
EW
33 nline = (++a)->arg_vtype == V_ERR ? nbufline : a->arg_num;
34 label = (++a)->arg_vtype == V_ERR ? 0 : a->arg_str;
35 if (openwin(id, row, col, nrow, ncol, nline, label) == 0)
36 return;
37 v->v_type = V_NUM;
7a57b3e2 38 v->v_num = id + 1;
4cbe417d
EW
39}
40
1c0ca238
EW
41struct lcmd_arg arg_buffer[] = {
42 { "nlines", 1, ARG_NUM },
43 { 0, 0, 0 }
44};
45
46l_buffer(v)
47struct value *v;
4cbe417d 48{
1c0ca238
EW
49 v->v_num = nbufline;
50 v->v_type = V_NUM;
51 if (arg_buffer[0].arg_vtype != V_ERR)
52 nbufline = arg_buffer[0].arg_num;
bd002416
EW
53}
54
1c0ca238
EW
55struct lcmd_arg arg_select[] = {
56 { "window", 1, ARG_NUM },
57 { 0, 0, 0 }
58};
59
60l_select(v)
61struct value *v;
bd002416
EW
62{
63 struct ww *w;
64
1c0ca238 65 v->v_type = V_NUM;
7a57b3e2 66 v->v_num = selwin ? selwin->ww_id + 1 : -1;
1c0ca238 67 if (arg_select[0].arg_vtype == V_ERR)
bd002416 68 return;
37661cbd 69 if ((w = vtowin(&arg_select[0].arg_val)) == 0)
1c0ca238 70 return;
bd002416
EW
71 setselwin(w);
72}
73
1c0ca238
EW
74struct lcmd_arg arg_escape[] = {
75 { "escapec", 1, ARG_NUM },
76 { 0, 0, 0 }
77};
78
79l_escape(v)
80struct value *v;
bd002416 81{
1c0ca238
EW
82 if ((v->v_str = str_cpy(unctrl(escapec))) == 0) {
83 error("Out of memory.");
84 return;
85 }
86 v->v_type = V_STR;
87 if (arg_escape[0].arg_type != V_ERR)
88 setescape(arg_escape[0].arg_str);
bd002416
EW
89}
90
1c0ca238
EW
91struct lcmd_arg arg_label[] = {
92 { "window", 1, ARG_NUM },
93 { "label", 1, ARG_STR },
94 { 0, 0, 0 }
95};
96
97/*ARGSUSED*/
98l_label(v)
99struct value *v;
bd002416
EW
100{
101 struct ww *w;
1c0ca238 102 register struct lcmd_arg *a = arg_label;
bd002416 103
1c0ca238 104 if ((w = vtowin(&a->arg_val)) == 0)
bd002416 105 return;
1c0ca238 106 if ((++a)->arg_vtype != V_ERR && setlabel(w, a->arg_str) < 0)
bd002416
EW
107 error("Out of memory.");
108 reframe();
109}
110
1c0ca238 111struct lcmd_arg arg_terse[] = {
37661cbd 112 { "flag", 1, ARG_ANY },
1c0ca238
EW
113 { 0, 0, 0 }
114};
115
116l_terse(v)
117struct value *v;
bd002416 118{
1c0ca238
EW
119 v->v_type = V_NUM;
120 v->v_num = terse;
121 terse = vtobool(&arg_terse[0].arg_val, 1, terse);
122 if (!terse && v->v_num)
bd002416 123 wwadd(cmdwin, &wwhead);
37661cbd
EW
124 else if (!v->v_num && terse)
125 wwdelete(cmdwin);
bd002416
EW
126 reframe();
127}
128
1c0ca238
EW
129struct lcmd_arg arg_source[] = {
130 { "filename", 1, ARG_STR },
131 { 0, 0, 0 }
132};
133
134/*ARGSUSED*/
135l_source(v)
136struct value *v;
bd002416 137{
1c0ca238
EW
138 if (arg_source[0].arg_vtype != V_ERR
139 && dosource(arg_source[0].arg_str) < 0) {
140 error("Can't open %s.", arg_source[0].arg_str);
141 v->v_num = -1;
142 } else
143 v->v_num = 0;
144 v->v_type = V_NUM;
bd002416
EW
145}
146
1c0ca238
EW
147struct lcmd_arg arg_write[] = {
148 { "window", 1, ARG_NUM },
149 { "string", 1, ARG_STR },
150 { 0, 0, 0 }
151};
152
153/*ARGSUSED*/
154l_write(v)
155struct value *v;
bd002416 156{
1c0ca238 157 register struct lcmd_arg *a = arg_write;
bd002416
EW
158 struct ww *w;
159
1c0ca238 160 if ((w = vtowin(&a->arg_val)) == 0)
bd002416 161 return;
1c0ca238
EW
162 a++;
163 (void) write(w->ww_pty, a->arg_str, strlen(a->arg_str));
bd002416
EW
164}
165
1c0ca238
EW
166struct lcmd_arg arg_close[] = {
167 { "window", 1, ARG_NUM },
168 { 0, 0, 0 }
169};
170
171/*ARGSUSED*/
172l_close(v)
173struct value *v;
8aee01cc 174{
1c0ca238 175 register struct lcmd_arg *a = arg_close;
1c0ca238 176 struct ww *w;
8aee01cc 177
7c0483b9 178 if (a->arg_vtype == V_ERR)
a4c3bba5 179 c_close((struct ww *)0);
7c0483b9
EW
180 else if ((w = vtowin(&a->arg_val)) != 0)
181 c_close(w);
8aee01cc
EW
182}
183
7a57b3e2
EW
184struct lcmd_arg arg_cursormodes[] = {
185 { "modes", 1, ARG_NUM },
186 { 0, 0, 0 }
187};
188
189l_cursormodes(v)
190register struct value *v;
191{
192 register struct lcmd_arg *a = arg_cursormodes;
193
194 v->v_type = V_NUM;
195 v->v_num = wwcursormodes;
196 if (a->arg_vtype != V_ERR)
197 wwsetcursormodes(a->arg_num);
198}
199
bd002416 200struct ww *
1c0ca238
EW
201vtowin(v)
202register struct value *v;
bd002416 203{
bd002416
EW
204 struct ww *w;
205
1c0ca238
EW
206 switch (v->v_type) {
207 case V_ERR:
208 error("Window identifier required.");
209 return 0;
210 case V_STR:
211 error("Number required for window identifier.");
212 return 0;
213 }
214 if (v->v_num < 1 || v->v_num > NWINDOW
215 || (w = window[v->v_num - 1]) == 0) {
216 error("%d: No such window.", v->v_num);
bd002416
EW
217 return 0;
218 }
219 return w;
220}
1c0ca238
EW
221
222vtobool(v, def, err)
223register struct value *v;
224char def, err;
225{
226 switch (v->v_type) {
227 case V_NUM:
228 return v->v_num != 0;
229 case V_STR:
230 if (str_match(v->v_str, "true", 1)
231 || str_match(v->v_str, "on", 2)
232 || str_match(v->v_str, "yes", 1))
233 return 1;
234 else if (str_match(v->v_str, "false", 1)
235 || str_match(v->v_str, "off", 2)
236 || str_match(v->v_str, "no", 1))
237 return 0;
238 else {
239 error("%s: Illegal boolean value.", v->v_str);
240 return err;
241 }
242 /*NOTREACHED*/
243 case V_ERR:
244 return def;
245 }
246 /*NOTREACHED*/
247}