[BACK]Return to lcmd1.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / window

Annotation of src/usr.bin/window/lcmd1.c, Revision 1.1.1.1

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