[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.8

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