home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / window / lcmd1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-18  |  8.9 KB  |  430 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * 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    3.36 (Berkeley) 6/6/90";
  39. #endif /* not lint */
  40.  
  41. #include "defs.h"
  42. #include "string.h"
  43. #include "value.h"
  44. #include "lcmd.h"
  45. #include "var.h"
  46.  
  47. struct lcmd_arg arg_window[] = {
  48.     { "row",    1,    ARG_NUM },
  49.     { "column",    1,    ARG_NUM },
  50.     { "nrows",    2,    ARG_NUM },
  51.     { "ncols",    2,    ARG_NUM },
  52.     { "nlines",    2,    ARG_NUM },
  53.     { "label",    1,    ARG_STR },
  54.     { "pty",    1,    ARG_ANY },
  55.     { "frame",    1,    ARG_ANY },
  56.     { "mapnl",    1,    ARG_ANY },
  57.     { "keepopen",    1,    ARG_ANY },
  58.     { "smooth",    1,    ARG_ANY },
  59.     { "shell",    1,    ARG_STR|ARG_LIST },
  60.     0
  61. };
  62.  
  63. l_window(v, a)
  64. struct value *v;
  65. register struct value *a;
  66. {
  67.     struct ww *w;
  68.     int col, row, ncol, nrow, id, nline;
  69.     char *label;
  70.     char haspty, hasframe, mapnl, keepopen, smooth;
  71.     char *shf, **sh;
  72.     char *argv[sizeof default_shell / sizeof *default_shell];
  73.     register char **pp;
  74.  
  75.     if ((id = findid()) < 0)
  76.         return;
  77.     row = a->v_type == V_ERR ? 1 : a->v_num;
  78.     a++;
  79.     col = a->v_type == V_ERR ? 0 : a->v_num;
  80.     a++;
  81.     nrow = a->v_type == V_ERR ? wwnrow - row : a->v_num;
  82.     a++;
  83.     ncol = a->v_type == V_ERR ? wwncol - col : a->v_num;
  84.     a++;
  85.     nline = a->v_type == V_ERR ? default_nline : a->v_num;
  86.     a++;
  87.     label = a->v_type == V_ERR ? 0 : a->v_str;
  88.     if ((haspty = vtobool(++a, 1, -1)) < 0)
  89.         return;
  90.     if ((hasframe = vtobool(++a, 1, -1)) < 0)
  91.         return;
  92.     if ((mapnl = vtobool(++a, !haspty, -1)) < 0)
  93.         return;
  94.     if ((keepopen = vtobool(++a, 0, -1)) < 0)
  95.         return;
  96.     if ((smooth = vtobool(++a, default_smooth, -1)) < 0)
  97.         return;
  98.     if ((++a)->v_type != V_ERR) {
  99.         for (pp = argv; a->v_type != V_ERR &&
  100.              pp < &argv[sizeof argv/sizeof *argv-1]; pp++, a++)
  101.             *pp = a->v_str;
  102.         *pp = 0;
  103.         shf = *(sh = argv);
  104.         if (*sh = rindex(shf, '/'))
  105.             (*sh)++;
  106.         else
  107.             *sh = shf;
  108.     } else {
  109.         sh = default_shell;
  110.         shf = default_shellfile;
  111.     }
  112.     if ((w = openwin(id, row, col, nrow, ncol, nline, label, haspty,
  113.         hasframe, shf, sh)) == 0)
  114.         return;
  115.     w->ww_mapnl = mapnl;
  116.     w->ww_keepopen = keepopen;
  117.     w->ww_noupdate = !smooth;
  118.     v->v_type = V_NUM;
  119.     v->v_num = id + 1;
  120. }
  121.  
  122. struct lcmd_arg arg_def_nline[] = {
  123.     { "nlines",    1,    ARG_NUM },
  124.     0
  125. };
  126.  
  127. l_def_nline(v, a)
  128. register struct value *v, *a;
  129. {
  130.     v->v_num = default_nline;
  131.     v->v_type = V_NUM;
  132.     if (a->v_type != V_ERR)
  133.         default_nline = a->v_num;
  134. }
  135.  
  136. struct lcmd_arg arg_smooth[] = {
  137.     { "window",    1,    ARG_NUM },
  138.     { "flag",    1,    ARG_ANY },
  139.     0
  140. };
  141.  
  142. l_smooth(v, a)
  143. register struct value *v, *a;
  144. {
  145.     struct ww *w;
  146.  
  147.     v->v_type = V_NUM;
  148.     v->v_num = 0;
  149.     if ((w = vtowin(a++, selwin)) == 0)
  150.         return;
  151.     v->v_num = !w->ww_noupdate;
  152.     w->ww_noupdate = !vtobool(a, v->v_num, v->v_num);
  153. }
  154.  
  155. struct lcmd_arg arg_def_smooth[] = {
  156.     { "flag",    1,    ARG_ANY },
  157.     0
  158. };
  159.  
  160. l_def_smooth(v, a)
  161. register struct value *v, *a;
  162. {
  163.     v->v_type = V_NUM;
  164.     v->v_num = default_smooth;
  165.     default_smooth = vtobool(a, v->v_num, v->v_num);
  166. }
  167.  
  168. struct lcmd_arg arg_select[] = {
  169.     { "window",    1,    ARG_NUM },
  170.     0
  171. };
  172.  
  173. l_select(v, a)
  174. register struct value *v, *a;
  175. {
  176.     struct ww *w;
  177.  
  178.     v->v_type = V_NUM;
  179.     v->v_num = selwin ? selwin->ww_id + 1 : -1;
  180.     if (a->v_type == V_ERR)
  181.         return;
  182.     if ((w = vtowin(a, (struct ww *)0)) == 0)
  183.         return;
  184.     setselwin(w);
  185. }
  186.  
  187. struct lcmd_arg arg_debug[] = {
  188.     { "flag",    1,    ARG_ANY },
  189.     0
  190. };
  191.  
  192. l_debug(v, a)
  193. register struct value *v, *a;
  194. {
  195.     v->v_type = V_NUM;
  196.     v->v_num = debug;
  197.     debug = vtobool(a, debug, debug);
  198. }
  199.  
  200. struct lcmd_arg arg_escape[] = {
  201.     { "escapec",    1,    ARG_STR },
  202.     0
  203. };
  204.  
  205. l_escape(v, a)
  206. register struct value *v, *a;
  207. {
  208.     char buf[2];
  209.  
  210.     buf[0] = escapec;
  211.     buf[1] = 0;
  212.     if ((v->v_str = str_cpy(buf)) == 0) {
  213.         error("Out of memory.");
  214.         return;
  215.     }
  216.     v->v_type = V_STR;
  217.     if (a->v_type != V_ERR)
  218.         setescape(a->v_str);
  219. }
  220.  
  221. struct lcmd_arg arg_label[] = {
  222.     { "window",    1,    ARG_NUM },
  223.     { "label",    1,    ARG_STR },
  224.     0
  225. };
  226.  
  227. /*ARGSUSED*/
  228. l_label(v, a)
  229. struct value *v;
  230. register struct value *a;
  231. {
  232.     struct ww *w;
  233.  
  234.     if ((w = vtowin(a, selwin)) == 0)
  235.         return;
  236.     if ((++a)->v_type != V_ERR && setlabel(w, a->v_str) < 0)
  237.         error("Out of memory.");
  238.     reframe();
  239. }
  240.  
  241. struct lcmd_arg arg_foreground[] = {
  242.     { "window",    1,    ARG_NUM },
  243.     { "flag",    1,    ARG_ANY },
  244.     0
  245. };
  246.  
  247. l_foreground(v, a)
  248. register struct value *v, *a;
  249. {
  250.     struct ww *w;
  251.     char flag;
  252.  
  253.     if ((w = vtowin(a, selwin)) == 0)
  254.         return;
  255.     v->v_type = V_NUM;
  256.     v->v_num = isfg(w);
  257.     flag = vtobool(++a, v->v_num, v->v_num);
  258.     if (flag == v->v_num)
  259.         return;
  260.     deletewin(w);
  261.     addwin(w, flag);
  262.     reframe();
  263. }
  264.  
  265. struct lcmd_arg arg_terse[] = {
  266.     { "flag",    1,    ARG_ANY },
  267.     0
  268. };
  269.  
  270. l_terse(v, a)
  271. register struct value *v, *a;
  272. {
  273.     v->v_type = V_NUM;
  274.     v->v_num = terse;
  275.     setterse(vtobool(a, terse, terse));
  276. }
  277.  
  278. struct lcmd_arg arg_source[] = {
  279.     { "filename",    1,    ARG_STR },
  280.     0
  281. };
  282.  
  283. l_source(v, a)
  284. register struct value *v, *a;
  285. {
  286.     v->v_type = V_NUM;
  287.     if (a->v_type != V_ERR && dosource(a->v_str) < 0) {
  288.         error("Can't open %s.", a->v_str);
  289.         v->v_num = -1;
  290.     } else
  291.         v->v_num = 0;
  292. }
  293.  
  294. struct lcmd_arg arg_write[] = {
  295.     { "window",    1,    ARG_NUM },
  296.     { "",        0,    ARG_ANY|ARG_LIST },
  297.     0
  298. };
  299.  
  300. /*ARGSUSED*/
  301. l_write(v, a)
  302. struct value *v;
  303. register struct value *a;
  304. {
  305.     char buf[20];
  306.     struct ww *w;
  307.  
  308.     if ((w = vtowin(a++, selwin)) == 0)
  309.         return;
  310.     while (a->v_type != V_ERR) {
  311.         if (a->v_type == V_NUM) {
  312.             (void) sprintf(buf, "%d", a->v_num);
  313.             (void) write(w->ww_pty, buf, strlen(buf));
  314.         } else
  315.             (void) write(w->ww_pty, a->v_str, strlen(a->v_str));
  316.         if ((++a)->v_type != V_ERR)
  317.             (void) write(w->ww_pty, " ", 1);
  318.     }
  319. }
  320.  
  321. struct lcmd_arg arg_close[] = {
  322.     { "window",    1,    ARG_ANY|ARG_LIST },
  323.     0
  324. };
  325.  
  326. /*ARGSUSED*/
  327. l_close(v, a)
  328. struct value *v;
  329. register struct value *a;
  330. {
  331.     struct ww *w;
  332.  
  333.     if (a->v_type == V_STR && str_match(a->v_str, "all", 3))
  334.         closewin((struct ww *)0);
  335.     else
  336.         for (; a->v_type != V_ERR; a++)
  337.             if ((w = vtowin(a, (struct ww *)0)) != 0)
  338.                 closewin(w);
  339. }
  340.  
  341. struct lcmd_arg arg_cursormodes[] = {
  342.     { "modes",    1,    ARG_NUM },
  343.     0
  344. };
  345.  
  346. l_cursormodes(v, a)
  347. register struct value *v, *a;
  348. {
  349.  
  350.     v->v_type = V_NUM;
  351.     v->v_num = wwcursormodes;
  352.     if (a->v_type != V_ERR)
  353.         wwsetcursormodes(a->v_num);
  354. }
  355.  
  356. struct lcmd_arg arg_unset[] = {
  357.     { "variable",    1,    ARG_ANY },
  358.     0
  359. };
  360.  
  361. l_unset(v, a)
  362. register struct value *v, *a;
  363. {
  364.     v->v_type = V_NUM;
  365.     switch (a->v_type) {
  366.     case V_ERR:
  367.         v->v_num = -1;
  368.         return;
  369.     case V_NUM:
  370.         if ((a->v_str = str_itoa(a->v_num)) == 0) {
  371.             error("Out of memory.");
  372.             v->v_num = -1;
  373.             return;
  374.         }
  375.         a->v_type = V_STR;
  376.         break;
  377.     }
  378.     v->v_num = var_unset(a->v_str);
  379. }
  380.  
  381. struct ww *
  382. vtowin(v, w)
  383. register struct value *v;
  384. struct ww *w;
  385. {
  386.     switch (v->v_type) {
  387.     case V_ERR:
  388.         if (w != 0)
  389.             return w;
  390.         error("No window specified.");
  391.         return 0;
  392.     case V_STR:
  393.         error("%s: No such window.", v->v_str);
  394.         return 0;
  395.     }
  396.     if (v->v_num < 1 || v->v_num > NWINDOW
  397.         || (w = window[v->v_num - 1]) == 0) {
  398.         error("%d: No such window.", v->v_num);
  399.         return 0;
  400.     }
  401.     return w;
  402. }
  403.  
  404. vtobool(v, def, err)
  405. register struct value *v;
  406. char def, err;
  407. {
  408.     switch (v->v_type) {
  409.     case V_NUM:
  410.         return v->v_num != 0;
  411.     case V_STR:
  412.         if (str_match(v->v_str, "true", 1)
  413.             || str_match(v->v_str, "on", 2)
  414.             || str_match(v->v_str, "yes", 1))
  415.             return 1;
  416.         else if (str_match(v->v_str, "false", 1)
  417.             || str_match(v->v_str, "off", 2)
  418.             || str_match(v->v_str, "no", 1))
  419.             return 0;
  420.         else {
  421.             error("%s: Illegal boolean value.", v->v_str);
  422.             return err;
  423.         }
  424.         /*NOTREACHED*/
  425.     case V_ERR:
  426.         return def;
  427.     }
  428.     /*NOTREACHED*/
  429. }
  430.