1 /* @(#)lcmd1.c 8.1 (Berkeley) 6/6/93 */
2 /* $NetBSD: lcmd1.c,v 1.11 2006/12/18 20:04:55 christos Exp $ */
5 * Copyright (c) 1983, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Edward Wang at The University of California, Berkeley.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include "window_string.h"
43 char vtobool(struct value *, char, char);
45 struct lcmd_arg arg_window[] = {
46 { "row", 1, ARG_NUM },
47 { "column", 1, ARG_NUM },
48 { "nrows", 2, ARG_NUM },
49 { "ncols", 2, ARG_NUM },
50 { "nlines", 2, ARG_NUM },
51 { "label", 1, ARG_STR },
52 { "pty", 1, ARG_ANY },
53 { "frame", 1, ARG_ANY },
54 { "mapnl", 1, ARG_ANY },
55 { "keepopen", 1, ARG_ANY },
56 { "smooth", 1, ARG_ANY },
57 { "shell", 1, ARG_STR|ARG_LIST },
62 l_window(struct value *v, struct value *a)
65 int col, row, ncol, nrow, id, nline;
67 int haspty, hasframe, mapnl, keepopen, smooth;
69 char *argv[sizeof default_shell / sizeof *default_shell];
72 if ((id = findid()) < 0)
74 row = a->v_type == V_ERR ? 1 : a->v_num;
76 col = a->v_type == V_ERR ? 0 : a->v_num;
78 nrow = a->v_type == V_ERR ? wwnrow - row : a->v_num;
80 ncol = a->v_type == V_ERR ? wwncol - col : a->v_num;
82 nline = a->v_type == V_ERR ? default_nline : a->v_num;
84 label = a->v_type == V_ERR ? 0 : a->v_str;
85 if ((haspty = vtobool(++a, 1, -1)) < 0)
87 if ((hasframe = vtobool(++a, 1, -1)) < 0)
89 if ((mapnl = vtobool(++a, !haspty, -1)) < 0)
91 if ((keepopen = vtobool(++a, 0, -1)) < 0)
93 if ((smooth = vtobool(++a, default_smooth, -1)) < 0)
95 if ((++a)->v_type != V_ERR) {
96 for (pp = argv; a->v_type != V_ERR &&
97 pp < &argv[sizeof argv/sizeof *argv-1]; pp++, a++)
101 if ((*sh = strrchr(shf, '/')))
107 shf = default_shellfile;
109 if ((w = openwin(id, row, col, nrow, ncol, nline, label,
110 haspty ? WWT_PTY : WWT_SOCKET, hasframe ? WWU_HASFRAME : 0, shf,
114 SET(w->ww_wflags, WWW_MAPNL);
116 CLR(w->ww_wflags, WWW_MAPNL);
118 SET(w->ww_uflags, WWU_KEEPOPEN);
120 CLR(w->ww_uflags, WWU_KEEPOPEN);
122 SET(w->ww_wflags, WWW_NOUPDATE);
124 CLR(w->ww_wflags, WWW_NOUPDATE);
129 struct lcmd_arg arg_def_nline[] = {
130 { "nlines", 1, ARG_NUM },
135 l_def_nline(struct value *v, struct value *a)
137 v->v_num = default_nline;
139 if (a->v_type != V_ERR)
140 default_nline = a->v_num;
143 struct lcmd_arg arg_smooth[] = {
144 { "window", 1, ARG_NUM },
145 { "flag", 1, ARG_ANY },
150 l_smooth(struct value *v, struct value *a)
156 if ((w = vtowin(a++, selwin)) == 0)
158 v->v_num = ISSET(w->ww_wflags, WWW_NOUPDATE) == 0;
159 if (!vtobool(a, v->v_num, v->v_num))
160 SET(w->ww_wflags, WWW_NOUPDATE);
162 CLR(w->ww_wflags, WWW_NOUPDATE);
165 struct lcmd_arg arg_def_smooth[] = {
166 { "flag", 1, ARG_ANY },
171 l_def_smooth(struct value *v, struct value *a)
174 v->v_num = default_smooth;
175 default_smooth = vtobool(a, v->v_num, v->v_num);
178 struct lcmd_arg arg_select[] = {
179 { "window", 1, ARG_NUM },
184 l_select(struct value *v, struct value *a)
189 v->v_num = selwin ? selwin->ww_id + 1 : -1;
190 if (a->v_type == V_ERR)
192 if ((w = vtowin(a, (struct ww *)0)) == 0)
197 struct lcmd_arg arg_debug[] = {
198 { "flag", 1, ARG_ANY },
203 l_debug(struct value *v, struct value *a)
207 debug = vtobool(a, debug, debug);
210 struct lcmd_arg arg_escape[] = {
211 { "escapec", 1, ARG_STR },
216 l_escape(struct value *v, struct value *a)
222 if ((v->v_str = str_cpy(buf)) == 0) {
223 error("Out of memory.");
227 if (a->v_type != V_ERR)
231 struct lcmd_arg arg_label[] = {
232 { "window", 1, ARG_NUM },
233 { "label", 1, ARG_STR },
238 l_label(struct value *v __unused, struct value *a)
242 if ((w = vtowin(a, selwin)) == 0)
244 if ((++a)->v_type != V_ERR && setlabel(w, a->v_str) < 0)
245 error("Out of memory.");
249 struct lcmd_arg arg_foreground[] = {
250 { "window", 1, ARG_NUM },
251 { "flag", 1, ARG_ANY },
256 l_foreground(struct value *v, struct value *a)
261 if ((w = vtowin(a, selwin)) == 0)
265 flag = vtobool(++a, v->v_num, v->v_num);
266 if (flag == v->v_num)
273 struct lcmd_arg arg_terse[] = {
274 { "flag", 1, ARG_ANY },
279 l_terse(struct value *v, struct value *a)
283 setterse(vtobool(a, terse, terse));
286 struct lcmd_arg arg_source[] = {
287 { "filename", 1, ARG_STR },
292 l_source(struct value *v, struct value *a)
295 if (a->v_type != V_ERR && dosource(a->v_str) < 0) {
296 error("Can't open %s.", a->v_str);
302 struct lcmd_arg arg_write[] = {
303 { "window", 1, ARG_NUM },
304 { "", 0, ARG_ANY|ARG_LIST },
309 l_write(struct value *v __unused, struct value *a)
314 if ((w = vtowin(a++, selwin)) == 0)
316 while (a->v_type != V_ERR) {
317 if (a->v_type == V_NUM) {
318 (void) sprintf(buf, "%d", a->v_num);
319 (void) write(w->ww_pty, buf, strlen(buf));
321 (void) write(w->ww_pty, a->v_str, strlen(a->v_str));
322 if ((++a)->v_type != V_ERR)
323 (void) write(w->ww_pty, " ", 1);
327 struct lcmd_arg arg_close[] = {
328 { "window", 1, ARG_ANY|ARG_LIST },
333 l_close(struct value *v __unused, struct value *a)
337 if (a->v_type == V_STR && str_match(a->v_str, "all", 3))
338 closewin((struct ww *)0);
340 for (; a->v_type != V_ERR; a++)
341 if ((w = vtowin(a, (struct ww *)0)) != 0)
345 struct lcmd_arg arg_cursormodes[] = {
346 { "modes", 1, ARG_NUM },
351 l_cursormodes(struct value *v, struct value *a)
355 v->v_num = wwcursormodes;
356 if (a->v_type != V_ERR)
357 wwsetcursormodes(a->v_num);
360 struct lcmd_arg arg_unset[] = {
361 { "variable", 1, ARG_ANY },
366 l_unset(struct value *v, struct value *a)
374 if ((a->v_str = str_itoa(a->v_num)) == 0) {
375 error("Out of memory.");
382 v->v_num = var_unset(a->v_str);
386 vtowin(struct value *v, struct ww *w)
392 error("No window specified.");
395 error("%s: No such window.", v->v_str);
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);
407 vtobool(struct value *v, char def, char err)
411 return v->v_num != 0;
413 if (str_match(v->v_str, "true", 1)
414 || str_match(v->v_str, "on", 2)
415 || str_match(v->v_str, "yes", 1))
417 else if (str_match(v->v_str, "false", 1)
418 || str_match(v->v_str, "off", 2)
419 || str_match(v->v_str, "no", 1))
422 error("%s: Illegal boolean value.", v->v_str);