]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/window/lcmd1.c
This commit was generated by cvs2svn to compensate for changes in r69408,
[FreeBSD/FreeBSD.git] / usr.bin / window / lcmd1.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Edward Wang at The University of California, Berkeley.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #ifndef lint
38 static char sccsid[] = "@(#)lcmd1.c     8.1 (Berkeley) 6/6/93";
39 static char rcsid[] = "@(#)$FreeBSD$";
40 #endif /* not lint */
41
42 #include "defs.h"
43 #include <string.h> /* System string definitions. */
44 #include "mystring.h" /* Local string definitions. */
45 #include "value.h"
46 #include "lcmd.h"
47 #include "var.h"
48
49 struct lcmd_arg arg_window[] = {
50         { "row",        1,      ARG_NUM },
51         { "column",     1,      ARG_NUM },
52         { "nrows",      2,      ARG_NUM },
53         { "ncols",      2,      ARG_NUM },
54         { "nlines",     2,      ARG_NUM },
55         { "label",      1,      ARG_STR },
56         { "pty",        1,      ARG_ANY },
57         { "frame",      1,      ARG_ANY },
58         { "mapnl",      1,      ARG_ANY },
59         { "keepopen",   1,      ARG_ANY },
60         { "smooth",     1,      ARG_ANY },
61         { "shell",      1,      ARG_STR|ARG_LIST },
62         0
63 };
64
65 l_window(v, a)
66 struct value *v;
67 register struct value *a;
68 {
69         struct ww *w;
70         int col, row, ncol, nrow, id, nline;
71         char *label;
72         char haspty, hasframe, mapnl, keepopen, smooth;
73         char *shf, **sh;
74         char *argv[sizeof default_shell / sizeof *default_shell];
75         register char **pp;
76
77         if ((id = findid()) < 0)
78                 return;
79         row = a->v_type == V_ERR ? 1 : a->v_num;
80         a++;
81         col = a->v_type == V_ERR ? 0 : a->v_num;
82         a++;
83         nrow = a->v_type == V_ERR ? wwnrow - row : a->v_num;
84         a++;
85         ncol = a->v_type == V_ERR ? wwncol - col : a->v_num;
86         a++;
87         nline = a->v_type == V_ERR ? default_nline : a->v_num;
88         a++;
89         label = a->v_type == V_ERR ? 0 : a->v_str;
90         if ((haspty = vtobool(++a, 1, -1)) < 0)
91                 return;
92         if ((hasframe = vtobool(++a, 1, -1)) < 0)
93                 return;
94         if ((mapnl = vtobool(++a, !haspty, -1)) < 0)
95                 return;
96         if ((keepopen = vtobool(++a, 0, -1)) < 0)
97                 return;
98         if ((smooth = vtobool(++a, default_smooth, -1)) < 0)
99                 return;
100         if ((++a)->v_type != V_ERR) {
101                 for (pp = argv; a->v_type != V_ERR &&
102                      pp < &argv[sizeof argv/sizeof *argv-1]; pp++, a++)
103                         *pp = a->v_str;
104                 *pp = 0;
105                 shf = *(sh = argv);
106                 if (*sh = rindex(shf, '/'))
107                         (*sh)++;
108                 else
109                         *sh = shf;
110         } else {
111                 sh = default_shell;
112                 shf = default_shellfile;
113         }
114         if ((w = openwin(id, row, col, nrow, ncol, nline, label, haspty,
115             hasframe, shf, sh)) == 0)
116                 return;
117         w->ww_mapnl = mapnl;
118         w->ww_keepopen = keepopen;
119         w->ww_noupdate = !smooth;
120         v->v_type = V_NUM;
121         v->v_num = id + 1;
122 }
123
124 struct lcmd_arg arg_def_nline[] = {
125         { "nlines",     1,      ARG_NUM },
126         0
127 };
128
129 l_def_nline(v, a)
130 register struct value *v, *a;
131 {
132         v->v_num = default_nline;
133         v->v_type = V_NUM;
134         if (a->v_type != V_ERR)
135                 default_nline = a->v_num;
136 }
137
138 struct lcmd_arg arg_smooth[] = {
139         { "window",     1,      ARG_NUM },
140         { "flag",       1,      ARG_ANY },
141         0
142 };
143
144 l_smooth(v, a)
145 register struct value *v, *a;
146 {
147         struct ww *w;
148
149         v->v_type = V_NUM;
150         v->v_num = 0;
151         if ((w = vtowin(a++, selwin)) == 0)
152                 return;
153         v->v_num = !w->ww_noupdate;
154         w->ww_noupdate = !vtobool(a, v->v_num, v->v_num);
155 }
156
157 struct lcmd_arg arg_def_smooth[] = {
158         { "flag",       1,      ARG_ANY },
159         0
160 };
161
162 l_def_smooth(v, a)
163 register struct value *v, *a;
164 {
165         v->v_type = V_NUM;
166         v->v_num = default_smooth;
167         default_smooth = vtobool(a, v->v_num, v->v_num);
168 }
169
170 struct lcmd_arg arg_select[] = {
171         { "window",     1,      ARG_NUM },
172         0
173 };
174
175 l_select(v, a)
176 register struct value *v, *a;
177 {
178         struct ww *w;
179
180         v->v_type = V_NUM;
181         v->v_num = selwin ? selwin->ww_id + 1 : -1;
182         if (a->v_type == V_ERR)
183                 return;
184         if ((w = vtowin(a, (struct ww *)0)) == 0)
185                 return;
186         setselwin(w);
187 }
188
189 struct lcmd_arg arg_debug[] = {
190         { "flag",       1,      ARG_ANY },
191         0
192 };
193
194 l_debug(v, a)
195 register struct value *v, *a;
196 {
197         v->v_type = V_NUM;
198         v->v_num = debug;
199         debug = vtobool(a, debug, debug);
200 }
201
202 struct lcmd_arg arg_escape[] = {
203         { "escapec",    1,      ARG_STR },
204         0
205 };
206
207 l_escape(v, a)
208 register struct value *v, *a;
209 {
210         char buf[2];
211
212         buf[0] = escapec;
213         buf[1] = 0;
214         if ((v->v_str = str_cpy(buf)) == 0) {
215                 error("Out of memory.");
216                 return;
217         }
218         v->v_type = V_STR;
219         if (a->v_type != V_ERR)
220                 setescape(a->v_str);
221 }
222
223 struct lcmd_arg arg_label[] = {
224         { "window",     1,      ARG_NUM },
225         { "label",      1,      ARG_STR },
226         0
227 };
228
229 /*ARGSUSED*/
230 l_label(v, a)
231 struct value *v;
232 register struct value *a;
233 {
234         struct ww *w;
235
236         if ((w = vtowin(a, selwin)) == 0)
237                 return;
238         if ((++a)->v_type != V_ERR && setlabel(w, a->v_str) < 0)
239                 error("Out of memory.");
240         reframe();
241 }
242
243 struct lcmd_arg arg_foreground[] = {
244         { "window",     1,      ARG_NUM },
245         { "flag",       1,      ARG_ANY },
246         0
247 };
248
249 l_foreground(v, a)
250 register struct value *v, *a;
251 {
252         struct ww *w;
253         char flag;
254
255         if ((w = vtowin(a, selwin)) == 0)
256                 return;
257         v->v_type = V_NUM;
258         v->v_num = isfg(w);
259         flag = vtobool(++a, v->v_num, v->v_num);
260         if (flag == v->v_num)
261                 return;
262         deletewin(w);
263         addwin(w, flag);
264         reframe();
265 }
266
267 struct lcmd_arg arg_terse[] = {
268         { "flag",       1,      ARG_ANY },
269         0
270 };
271
272 l_terse(v, a)
273 register struct value *v, *a;
274 {
275         v->v_type = V_NUM;
276         v->v_num = terse;
277         setterse(vtobool(a, terse, terse));
278 }
279
280 struct lcmd_arg arg_source[] = {
281         { "filename",   1,      ARG_STR },
282         0
283 };
284
285 l_source(v, a)
286 register struct value *v, *a;
287 {
288         v->v_type = V_NUM;
289         if (a->v_type != V_ERR && dosource(a->v_str) < 0) {
290                 error("Can't open %s.", a->v_str);
291                 v->v_num = -1;
292         } else
293                 v->v_num = 0;
294 }
295
296 struct lcmd_arg arg_write[] = {
297         { "window",     1,      ARG_NUM },
298         { "",           0,      ARG_ANY|ARG_LIST },
299         0
300 };
301
302 /*ARGSUSED*/
303 l_write(v, a)
304 struct value *v;
305 register struct value *a;
306 {
307         char buf[20];
308         struct ww *w;
309
310         if ((w = vtowin(a++, selwin)) == 0)
311                 return;
312         while (a->v_type != V_ERR) {
313                 if (a->v_type == V_NUM) {
314                         (void) sprintf(buf, "%d", a->v_num);
315                         (void) write(w->ww_pty, buf, strlen(buf));
316                 } else
317                         (void) write(w->ww_pty, a->v_str, strlen(a->v_str));
318                 if ((++a)->v_type != V_ERR)
319                         (void) write(w->ww_pty, " ", 1);
320         }
321 }
322
323 struct lcmd_arg arg_close[] = {
324         { "window",     1,      ARG_ANY|ARG_LIST },
325         0
326 };
327
328 /*ARGSUSED*/
329 l_close(v, a)
330 struct value *v;
331 register struct value *a;
332 {
333         struct ww *w;
334
335         if (a->v_type == V_STR && str_match(a->v_str, "all", 3))
336                 closewin((struct ww *)0);
337         else
338                 for (; a->v_type != V_ERR; a++)
339                         if ((w = vtowin(a, (struct ww *)0)) != 0)
340                                 closewin(w);
341 }
342
343 struct lcmd_arg arg_cursormodes[] = {
344         { "modes",      1,      ARG_NUM },
345         0
346 };
347
348 l_cursormodes(v, a)
349 register struct value *v, *a;
350 {
351
352         v->v_type = V_NUM;
353         v->v_num = wwcursormodes;
354         if (a->v_type != V_ERR)
355                 wwsetcursormodes(a->v_num);
356 }
357
358 struct lcmd_arg arg_unset[] = {
359         { "variable",   1,      ARG_ANY },
360         0
361 };
362
363 l_unset(v, a)
364 register struct value *v, *a;
365 {
366         v->v_type = V_NUM;
367         switch (a->v_type) {
368         case V_ERR:
369                 v->v_num = -1;
370                 return;
371         case V_NUM:
372                 if ((a->v_str = str_itoa(a->v_num)) == 0) {
373                         error("Out of memory.");
374                         v->v_num = -1;
375                         return;
376                 }
377                 a->v_type = V_STR;
378                 break;
379         }
380         v->v_num = var_unset(a->v_str);
381 }
382
383 struct ww *
384 vtowin(v, w)
385 register struct value *v;
386 struct ww *w;
387 {
388         switch (v->v_type) {
389         case V_ERR:
390                 if (w != 0)
391                         return w;
392                 error("No window specified.");
393                 return 0;
394         case V_STR:
395                 error("%s: No such window.", v->v_str);
396                 return 0;
397         }
398         if (v->v_num < 1 || v->v_num > NWINDOW
399             || (w = window[v->v_num - 1]) == 0) {
400                 error("%d: No such window.", v->v_num);
401                 return 0;
402         }
403         return w;
404 }
405
406 vtobool(v, def, err)
407 register struct value *v;
408 char def, err;
409 {
410         switch (v->v_type) {
411         case V_NUM:
412                 return v->v_num != 0;
413         case V_STR:
414                 if (str_match(v->v_str, "true", 1)
415                     || str_match(v->v_str, "on", 2)
416                     || str_match(v->v_str, "yes", 1))
417                         return 1;
418                 else if (str_match(v->v_str, "false", 1)
419                     || str_match(v->v_str, "off", 2)
420                     || str_match(v->v_str, "no", 1))
421                         return 0;
422                 else {
423                         error("%s: Illegal boolean value.", v->v_str);
424                         return err;
425                 }
426                 /*NOTREACHED*/
427         case V_ERR:
428                 return def;
429         }
430         /*NOTREACHED*/
431 }