1 /* $NetBSD: lcmd1.c,v 1.11 2006/12/18 20:04:55 christos Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Edward Wang at The University of California, Berkeley.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
38 static char sccsid[] = "@(#)lcmd1.c 8.1 (Berkeley) 6/6/93";
40 __RCSID("$NetBSD: lcmd1.c,v 1.11 2006/12/18 20:04:55 christos Exp $");
47 #include "window_string.h"
51 char vtobool(struct value *, char, char);
53 struct lcmd_arg arg_window[] = {
54 { "row", 1, ARG_NUM },
55 { "column", 1, ARG_NUM },
56 { "nrows", 2, ARG_NUM },
57 { "ncols", 2, ARG_NUM },
58 { "nlines", 2, ARG_NUM },
59 { "label", 1, ARG_STR },
60 { "pty", 1, ARG_ANY },
61 { "frame", 1, ARG_ANY },
62 { "mapnl", 1, ARG_ANY },
63 { "keepopen", 1, ARG_ANY },
64 { "smooth", 1, ARG_ANY },
65 { "shell", 1, ARG_STR|ARG_LIST },
70 l_window(struct value *v, struct value *a)
73 int col, row, ncol, nrow, id, nline;
75 int haspty, hasframe, mapnl, keepopen, smooth;
77 char *argv[sizeof default_shell / sizeof *default_shell];
80 if ((id = findid()) < 0)
82 row = a->v_type == V_ERR ? 1 : a->v_num;
84 col = a->v_type == V_ERR ? 0 : a->v_num;
86 nrow = a->v_type == V_ERR ? wwnrow - row : a->v_num;
88 ncol = a->v_type == V_ERR ? wwncol - col : a->v_num;
90 nline = a->v_type == V_ERR ? default_nline : a->v_num;
92 label = a->v_type == V_ERR ? 0 : a->v_str;
93 if ((haspty = vtobool(++a, 1, -1)) < 0)
95 if ((hasframe = vtobool(++a, 1, -1)) < 0)
97 if ((mapnl = vtobool(++a, !haspty, -1)) < 0)
99 if ((keepopen = vtobool(++a, 0, -1)) < 0)
101 if ((smooth = vtobool(++a, default_smooth, -1)) < 0)
103 if ((++a)->v_type != V_ERR) {
104 for (pp = argv; a->v_type != V_ERR &&
105 pp < &argv[sizeof argv/sizeof *argv-1]; pp++, a++)
109 if ((*sh = strrchr(shf, '/')))
115 shf = default_shellfile;
117 if ((w = openwin(id, row, col, nrow, ncol, nline, label,
118 haspty ? WWT_PTY : WWT_SOCKET, hasframe ? WWU_HASFRAME : 0, shf,
122 SET(w->ww_wflags, WWW_MAPNL);
124 CLR(w->ww_wflags, WWW_MAPNL);
126 SET(w->ww_uflags, WWU_KEEPOPEN);
128 CLR(w->ww_uflags, WWU_KEEPOPEN);
130 SET(w->ww_wflags, WWW_NOUPDATE);
132 CLR(w->ww_wflags, WWW_NOUPDATE);
137 struct lcmd_arg arg_def_nline[] = {
138 { "nlines", 1, ARG_NUM },
143 l_def_nline(struct value *v, struct value *a)
145 v->v_num = default_nline;
147 if (a->v_type != V_ERR)
148 default_nline = a->v_num;
151 struct lcmd_arg arg_smooth[] = {
152 { "window", 1, ARG_NUM },
153 { "flag", 1, ARG_ANY },
158 l_smooth(struct value *v, struct value *a)
164 if ((w = vtowin(a++, selwin)) == 0)
166 v->v_num = ISSET(w->ww_wflags, WWW_NOUPDATE) == 0;
167 if (!vtobool(a, v->v_num, v->v_num))
168 SET(w->ww_wflags, WWW_NOUPDATE);
170 CLR(w->ww_wflags, WWW_NOUPDATE);
173 struct lcmd_arg arg_def_smooth[] = {
174 { "flag", 1, ARG_ANY },
179 l_def_smooth(struct value *v, struct value *a)
182 v->v_num = default_smooth;
183 default_smooth = vtobool(a, v->v_num, v->v_num);
186 struct lcmd_arg arg_select[] = {
187 { "window", 1, ARG_NUM },
192 l_select(struct value *v, struct value *a)
197 v->v_num = selwin ? selwin->ww_id + 1 : -1;
198 if (a->v_type == V_ERR)
200 if ((w = vtowin(a, (struct ww *)0)) == 0)
205 struct lcmd_arg arg_debug[] = {
206 { "flag", 1, ARG_ANY },
211 l_debug(struct value *v, struct value *a)
215 debug = vtobool(a, debug, debug);
218 struct lcmd_arg arg_escape[] = {
219 { "escapec", 1, ARG_STR },
224 l_escape(struct value *v, struct value *a)
230 if ((v->v_str = str_cpy(buf)) == 0) {
231 error("Out of memory.");
235 if (a->v_type != V_ERR)
239 struct lcmd_arg arg_label[] = {
240 { "window", 1, ARG_NUM },
241 { "label", 1, ARG_STR },
246 l_label(struct value *v __unused, struct value *a)
250 if ((w = vtowin(a, selwin)) == 0)
252 if ((++a)->v_type != V_ERR && setlabel(w, a->v_str) < 0)
253 error("Out of memory.");
257 struct lcmd_arg arg_foreground[] = {
258 { "window", 1, ARG_NUM },
259 { "flag", 1, ARG_ANY },
264 l_foreground(struct value *v, struct value *a)
269 if ((w = vtowin(a, selwin)) == 0)
273 flag = vtobool(++a, v->v_num, v->v_num);
274 if (flag == v->v_num)
281 struct lcmd_arg arg_terse[] = {
282 { "flag", 1, ARG_ANY },
287 l_terse(struct value *v, struct value *a)
291 setterse(vtobool(a, terse, terse));
294 struct lcmd_arg arg_source[] = {
295 { "filename", 1, ARG_STR },
300 l_source(struct value *v, struct value *a)
303 if (a->v_type != V_ERR && dosource(a->v_str) < 0) {
304 error("Can't open %s.", a->v_str);
310 struct lcmd_arg arg_write[] = {
311 { "window", 1, ARG_NUM },
312 { "", 0, ARG_ANY|ARG_LIST },
317 l_write(struct value *v __unused, struct value *a)
322 if ((w = vtowin(a++, selwin)) == 0)
324 while (a->v_type != V_ERR) {
325 if (a->v_type == V_NUM) {
326 (void) sprintf(buf, "%d", a->v_num);
327 (void) write(w->ww_pty, buf, strlen(buf));
329 (void) write(w->ww_pty, a->v_str, strlen(a->v_str));
330 if ((++a)->v_type != V_ERR)
331 (void) write(w->ww_pty, " ", 1);
335 struct lcmd_arg arg_close[] = {
336 { "window", 1, ARG_ANY|ARG_LIST },
341 l_close(struct value *v __unused, struct value *a)
345 if (a->v_type == V_STR && str_match(a->v_str, "all", 3))
346 closewin((struct ww *)0);
348 for (; a->v_type != V_ERR; a++)
349 if ((w = vtowin(a, (struct ww *)0)) != 0)
353 struct lcmd_arg arg_cursormodes[] = {
354 { "modes", 1, ARG_NUM },
359 l_cursormodes(struct value *v, struct value *a)
363 v->v_num = wwcursormodes;
364 if (a->v_type != V_ERR)
365 wwsetcursormodes(a->v_num);
368 struct lcmd_arg arg_unset[] = {
369 { "variable", 1, ARG_ANY },
374 l_unset(struct value *v, struct value *a)
382 if ((a->v_str = str_itoa(a->v_num)) == 0) {
383 error("Out of memory.");
390 v->v_num = var_unset(a->v_str);
394 vtowin(struct value *v, struct ww *w)
400 error("No window specified.");
403 error("%s: No such window.", v->v_str);
406 if (v->v_num < 1 || v->v_num > NWINDOW
407 || (w = window[v->v_num - 1]) == 0) {
408 error("%d: No such window.", v->v_num);
415 vtobool(struct value *v, char def, char err)
419 return v->v_num != 0;
421 if (str_match(v->v_str, "true", 1)
422 || str_match(v->v_str, "on", 2)
423 || str_match(v->v_str, "yes", 1))
425 else if (str_match(v->v_str, "false", 1)
426 || str_match(v->v_str, "off", 2)
427 || str_match(v->v_str, "no", 1))
430 error("%s: Illegal boolean value.", v->v_str);