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

Annotation of src/usr.bin/less/decode.c, Revision 1.10

1.1       etheisen    1: /*
1.7       shadchin    2:  * Copyright (C) 1984-2012  Mark Nudelman
1.10    ! nicm        3:  * Modified for use with illumos by Garrett D'Amore.
        !             4:  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
1.1       etheisen    5:  *
1.4       millert     6:  * You may distribute under the terms of either the GNU General Public
                      7:  * License or the Less License, as specified in the README file.
1.1       etheisen    8:  *
1.7       shadchin    9:  * For more information, see the README file.
1.8       nicm       10:  */
1.1       etheisen   11:
                     12: /*
                     13:  * Routines to decode user commands.
                     14:  *
                     15:  * This is all table driven.
                     16:  * A command table is a sequence of command descriptors.
                     17:  * Each command descriptor is a sequence of bytes with the following format:
                     18:  *     <c1><c2>...<cN><0><action>
                     19:  * The characters c1,c2,...,cN are the command string; that is,
                     20:  * the characters which the user must type.
                     21:  * It is terminated by a null <0> byte.
                     22:  * The byte after the null byte is the action code associated
                     23:  * with the command string.
                     24:  * If an action byte is OR-ed with A_EXTRA, this indicates
                     25:  * that the option byte is followed by an extra string.
                     26:  *
                     27:  * There may be many command tables.
                     28:  * The first (default) table is built-in.
                     29:  * Other tables are read in from "lesskey" files.
                     30:  * All the tables are linked together and are searched in order.
                     31:  */
                     32:
                     33: #include "less.h"
                     34: #include "cmd.h"
                     35: #include "lesskey.h"
                     36:
1.6       shadchin   37: extern int erase_char, erase2_char, kill_char;
1.8       nicm       38: extern int secure, less_is_more;
1.1       etheisen   39:
1.8       nicm       40: #define        SK(k) \
1.4       millert    41:        SK_SPECIAL_KEY, (k), 6, 1, 1, 1
1.1       etheisen   42: /*
                     43:  * Command table is ordered roughly according to expected
                     44:  * frequency of use, so the common commands are near the beginning.
                     45:  */
1.4       millert    46:
1.1       etheisen   47: static unsigned char cmdtable[] =
                     48: {
1.8       nicm       49:        '\r', 0,                        A_F_LINE,
                     50:        '\n', 0,                        A_F_LINE,
                     51:        'e', 0,                         A_F_LINE,
                     52:        'j', 0,                         A_F_LINE,
                     53:        SK(SK_DOWN_ARROW), 0,           A_F_LINE,
                     54:        CONTROL('E'), 0,                A_F_LINE,
                     55:        CONTROL('N'), 0,                A_F_LINE,
                     56:        'k', 0,                         A_B_LINE,
                     57:        'y', 0,                         A_B_LINE,
                     58:        CONTROL('Y'), 0,                A_B_LINE,
                     59:        SK(SK_CONTROL_K), 0,            A_B_LINE,
                     60:        CONTROL('P'), 0,                A_B_LINE,
                     61:        SK(SK_UP_ARROW), 0,             A_B_LINE,
                     62:        'J', 0,                         A_FF_LINE,
                     63:        'K', 0,                         A_BF_LINE,
                     64:        'Y', 0,                         A_BF_LINE,
                     65:        'd', 0,                         A_F_SCROLL,
                     66:        CONTROL('D'), 0,                A_F_SCROLL,
                     67:        'u', 0,                         A_B_SCROLL,
                     68:        CONTROL('U'), 0,                A_B_SCROLL,
                     69:        ' ', 0,                         A_F_SCREEN,
                     70:        'f', 0,                         A_F_SCREEN,
                     71:        CONTROL('F'), 0,                A_F_SCREEN,
                     72:        CONTROL('V'), 0,                A_F_SCREEN,
                     73:        SK(SK_PAGE_DOWN), 0,            A_F_SCREEN,
                     74:        'b', 0,                         A_B_SCREEN,
                     75:        CONTROL('B'), 0,                A_B_SCREEN,
                     76:        ESC, 'v', 0,                    A_B_SCREEN,
                     77:        SK(SK_PAGE_UP), 0,              A_B_SCREEN,
                     78:        'z', 0,                         A_F_WINDOW,
                     79:        'w', 0,                         A_B_WINDOW,
                     80:        ESC, ' ', 0,                    A_FF_SCREEN,
                     81:        'F', 0,                         A_F_FOREVER,
                     82:        ESC, 'F', 0,                    A_F_UNTIL_HILITE,
                     83:        'R', 0,                         A_FREPAINT,
                     84:        'r', 0,                         A_REPAINT,
                     85:        CONTROL('R'), 0,                A_REPAINT,
                     86:        CONTROL('L'), 0,                A_REPAINT,
                     87:        ESC, 'u', 0,                    A_UNDO_SEARCH,
                     88:        'g', 0,                         A_GOLINE,
                     89:        SK(SK_HOME), 0,                 A_GOLINE,
                     90:        '<', 0,                         A_GOLINE,
                     91:        ESC, '<', 0,                    A_GOLINE,
                     92:        'p', 0,                         A_PERCENT,
                     93:        '%', 0,                         A_PERCENT,
                     94:        ESC, '[', 0,                    A_LSHIFT,
                     95:        ESC, ']', 0,                    A_RSHIFT,
                     96:        ESC, '(', 0,                    A_LSHIFT,
                     97:        ESC, ')', 0,                    A_RSHIFT,
                     98:        SK(SK_RIGHT_ARROW), 0,          A_RSHIFT,
                     99:        SK(SK_LEFT_ARROW), 0,           A_LSHIFT,
                    100:        '{', 0,                         A_F_BRACKET|A_EXTRA,    '{', '}', 0,
                    101:        '}', 0,                         A_B_BRACKET|A_EXTRA,    '{', '}', 0,
                    102:        '(', 0,                         A_F_BRACKET|A_EXTRA,    '(', ')', 0,
                    103:        ')', 0,                         A_B_BRACKET|A_EXTRA,    '(', ')', 0,
                    104:        '[', 0,                         A_F_BRACKET|A_EXTRA,    '[', ']', 0,
                    105:        ']', 0,                         A_B_BRACKET|A_EXTRA,    '[', ']', 0,
                    106:        ESC, CONTROL('F'), 0,           A_F_BRACKET,
                    107:        ESC, CONTROL('B'), 0,           A_B_BRACKET,
                    108:        'G', 0,                         A_GOEND,
                    109:        ESC, '>', 0,                    A_GOEND,
                    110:        '>', 0,                         A_GOEND,
                    111:        SK(SK_END), 0,                  A_GOEND,
                    112:        'P', 0,                         A_GOPOS,
                    113:
                    114:        '0', 0,                         A_DIGIT,
                    115:        '1', 0,                         A_DIGIT,
                    116:        '2', 0,                         A_DIGIT,
                    117:        '3', 0,                         A_DIGIT,
                    118:        '4', 0,                         A_DIGIT,
                    119:        '5', 0,                         A_DIGIT,
                    120:        '6', 0,                         A_DIGIT,
                    121:        '7', 0,                         A_DIGIT,
                    122:        '8', 0,                         A_DIGIT,
                    123:        '9', 0,                         A_DIGIT,
                    124:        '.', 0,                         A_DIGIT,
                    125:
                    126:        '=', 0,                         A_STAT,
                    127:        CONTROL('G'), 0,                A_STAT,
                    128:        ':', 'f', 0,                    A_STAT,
                    129:        '/', 0,                         A_F_SEARCH,
                    130:        '?', 0,                         A_B_SEARCH,
                    131:        ESC, '/', 0,                    A_F_SEARCH|A_EXTRA,     '*', 0,
                    132:        ESC, '?', 0,                    A_B_SEARCH|A_EXTRA,     '*', 0,
                    133:        'n', 0,                         A_AGAIN_SEARCH,
                    134:        ESC, 'n', 0,                    A_T_AGAIN_SEARCH,
                    135:        'N', 0,                         A_REVERSE_SEARCH,
                    136:        ESC, 'N', 0,                    A_T_REVERSE_SEARCH,
                    137:        '&', 0,                         A_FILTER,
                    138:        'm', 0,                         A_SETMARK,
                    139:        '\'', 0,                        A_GOMARK,
                    140:        CONTROL('X'), CONTROL('X'), 0,  A_GOMARK,
                    141:        'E', 0,                         A_EXAMINE,
                    142:        ':', 'e', 0,                    A_EXAMINE,
                    143:        CONTROL('X'), CONTROL('V'), 0,  A_EXAMINE,
                    144:        ':', 'n', 0,                    A_NEXT_FILE,
                    145:        ':', 'p', 0,                    A_PREV_FILE,
                    146:        't', 0,                         A_NEXT_TAG,
                    147:        'T', 0,                         A_PREV_TAG,
                    148:        ':', 'x', 0,                    A_INDEX_FILE,
                    149:        ':', 'd', 0,                    A_REMOVE_FILE,
                    150:        ':', 't', 0,                    A_OPT_TOGGLE|A_EXTRA,   't', 0,
                    151:        '|', 0,                         A_PIPE,
                    152:        'v', 0,                         A_VISUAL,
                    153:        '!', 0,                         A_SHELL,
                    154:        '+', 0,                         A_FIRSTCMD,
                    155:
                    156:        'H', 0,                         A_HELP,
                    157:        'h', 0,                         A_HELP,
                    158:        SK(SK_F1), 0,                   A_HELP,
                    159:        'V', 0,                         A_VERSION,
                    160:        'q', 0,                         A_QUIT,
                    161:        'Q', 0,                         A_QUIT,
                    162:        ':', 'q', 0,                    A_QUIT,
                    163:        ':', 'Q', 0,                    A_QUIT,
                    164:        'Z', 'Z', 0,                    A_QUIT
                    165: };
                    166:
                    167: static unsigned char lesstable[] = {
                    168:        '-', 0,                         A_OPT_TOGGLE,
                    169:        's', 0,                         A_OPT_TOGGLE|A_EXTRA,   'o', 0,
                    170:        '_', 0,                         A_DISP_OPTION
                    171: };
                    172:
                    173: static unsigned char moretable[] = {
                    174:        's', 0,                         A_F_SKIP
1.1       etheisen  175: };
                    176:
                    177: static unsigned char edittable[] =
                    178: {
1.9       deraadt   179:        '\t', 0,                        EC_F_COMPLETE,  /* TAB */
1.8       nicm      180:        '\17', 0,                       EC_B_COMPLETE,  /* BACKTAB */
                    181:        SK(SK_BACKTAB), 0,              EC_B_COMPLETE,  /* BACKTAB */
                    182:        ESC, '\t', 0,                   EC_B_COMPLETE,  /* ESC TAB */
                    183:        CONTROL('L'), 0,                EC_EXPAND,      /* CTRL-L */
                    184:        CONTROL('V'), 0,                EC_LITERAL,     /* BACKSLASH */
                    185:        CONTROL('A'), 0,                EC_LITERAL,     /* BACKSLASH */
                    186:        ESC, 'l', 0,                    EC_RIGHT,       /* ESC l */
                    187:        SK(SK_RIGHT_ARROW), 0,          EC_RIGHT,       /* RIGHTARROW */
                    188:        ESC, 'h', 0,                    EC_LEFT,        /* ESC h */
                    189:        SK(SK_LEFT_ARROW), 0,           EC_LEFT,        /* LEFTARROW */
                    190:        ESC, 'b', 0,                    EC_W_LEFT,      /* ESC b */
                    191:        ESC, SK(SK_LEFT_ARROW), 0,      EC_W_LEFT,      /* ESC LEFTARROW */
                    192:        SK(SK_CTL_LEFT_ARROW), 0,       EC_W_LEFT,      /* CTRL-LEFTARROW */
                    193:        ESC, 'w', 0,                    EC_W_RIGHT,     /* ESC w */
                    194:        ESC, SK(SK_RIGHT_ARROW), 0,     EC_W_RIGHT,     /* ESC RIGHTARROW */
                    195:        SK(SK_CTL_RIGHT_ARROW), 0,      EC_W_RIGHT,     /* CTRL-RIGHTARROW */
                    196:        ESC, 'i', 0,                    EC_INSERT,      /* ESC i */
                    197:        SK(SK_INSERT), 0,               EC_INSERT,      /* INSERT */
                    198:        ESC, 'x', 0,                    EC_DELETE,      /* ESC x */
                    199:        SK(SK_DELETE), 0,               EC_DELETE,      /* DELETE */
                    200:        ESC, 'X', 0,                    EC_W_DELETE,    /* ESC X */
                    201:        ESC, SK(SK_DELETE), 0,          EC_W_DELETE,    /* ESC DELETE */
                    202:        SK(SK_CTL_DELETE), 0,           EC_W_DELETE,    /* CTRL-DELETE */
                    203:        SK(SK_CTL_BACKSPACE), 0,        EC_W_BACKSPACE, /* CTRL-BACKSPACE */
                    204:        ESC, '\b', 0,                   EC_W_BACKSPACE, /* ESC BACKSPACE */
                    205:        ESC, '0', 0,                    EC_HOME,        /* ESC 0 */
                    206:        SK(SK_HOME), 0,                 EC_HOME,        /* HOME */
                    207:        ESC, '$', 0,                    EC_END,         /* ESC $ */
                    208:        SK(SK_END), 0,                  EC_END,         /* END */
                    209:        ESC, 'k', 0,                    EC_UP,          /* ESC k */
                    210:        SK(SK_UP_ARROW), 0,             EC_UP,          /* UPARROW */
                    211:        ESC, 'j', 0,                    EC_DOWN,        /* ESC j */
                    212:        SK(SK_DOWN_ARROW), 0,           EC_DOWN,        /* DOWNARROW */
                    213:        CONTROL('G'), 0,                EC_ABORT,       /* CTRL-G */
1.1       etheisen  214: };
                    215:
                    216: /*
                    217:  * Structure to support a list of command tables.
                    218:  */
                    219: struct tablelist
                    220: {
                    221:        struct tablelist *t_next;
                    222:        char *t_start;
                    223:        char *t_end;
                    224: };
                    225:
                    226: /*
                    227:  * List of command tables and list of line-edit tables.
                    228:  */
                    229: static struct tablelist *list_fcmd_tables = NULL;
                    230: static struct tablelist *list_ecmd_tables = NULL;
1.4       millert   231: static struct tablelist *list_var_tables = NULL;
                    232: static struct tablelist *list_sysvar_tables = NULL;
1.1       etheisen  233:
                    234:
                    235: /*
1.4       millert   236:  * Expand special key abbreviations in a command table.
                    237:  */
1.8       nicm      238: static void
                    239: expand_special_keys(char *table, int len)
                    240: {
                    241:        char *fm;
                    242:        char *to;
                    243:        int a;
1.4       millert   244:        char *repl;
                    245:        int klen;
                    246:
1.8       nicm      247:        for (fm = table;  fm < table + len; ) {
1.4       millert   248:                /*
                    249:                 * Rewrite each command in the table with any
                    250:                 * special key abbreviations expanded.
                    251:                 */
1.8       nicm      252:                for (to = fm;  *fm != '\0'; ) {
                    253:                        if (*fm != SK_SPECIAL_KEY) {
1.4       millert   254:                                *to++ = *fm++;
                    255:                                continue;
                    256:                        }
                    257:                        /*
                    258:                         * After SK_SPECIAL_KEY, next byte is the type
                    259:                         * of special key (one of the SK_* contants),
                    260:                         * and the byte after that is the number of bytes,
                    261:                         * N, reserved by the abbreviation (including the
                    262:                         * SK_SPECIAL_KEY and key type bytes).
                    263:                         * Replace all N bytes with the actual bytes
                    264:                         * output by the special key on this terminal.
                    265:                         */
                    266:                        repl = special_key_str(fm[1]);
                    267:                        klen = fm[2] & 0377;
                    268:                        fm += klen;
1.8       nicm      269:                        if (repl == NULL || strlen(repl) > klen)
1.4       millert   270:                                repl = "\377";
                    271:                        while (*repl != '\0')
                    272:                                *to++ = *repl++;
                    273:                }
                    274:                *to++ = '\0';
                    275:                /*
1.8       nicm      276:                 * Fill any unused bytes between end of command and
1.4       millert   277:                 * the action byte with A_SKIP.
                    278:                 */
                    279:                while (to <= fm)
                    280:                        *to++ = A_SKIP;
                    281:                fm++;
                    282:                a = *fm++ & 0377;
1.8       nicm      283:                if (a & A_EXTRA) {
1.4       millert   284:                        while (*fm++ != '\0')
                    285:                                continue;
                    286:                }
                    287:        }
                    288: }
                    289:
                    290: /*
1.1       etheisen  291:  * Initialize the command lists.
                    292:  */
1.8       nicm      293: void
                    294: init_cmds(void)
1.1       etheisen  295: {
                    296:        /*
                    297:         * Add the default command tables.
                    298:         */
1.8       nicm      299:        add_fcmd_table((char *)cmdtable, sizeof (cmdtable));
                    300:        add_ecmd_table((char *)edittable, sizeof (edittable));
                    301:        if (less_is_more) {
                    302:                add_fcmd_table((char *)moretable, sizeof (moretable));
                    303:                return;
                    304:        } else {
                    305:                add_fcmd_table((char *)lesstable, sizeof (lesstable));
                    306:        }
                    307:
1.4       millert   308:        /*
                    309:         * Try to add the tables in the system lesskey file.
                    310:         */
                    311:        add_hometable("LESSKEY_SYSTEM", LESSKEYFILE_SYS, 1);
                    312:        /*
1.1       etheisen  313:         * Try to add the tables in the standard lesskey file "$HOME/.less".
                    314:         */
1.4       millert   315:        add_hometable("LESSKEY", LESSKEYFILE, 0);
1.1       etheisen  316: }
                    317:
                    318: /*
1.4       millert   319:  * Add a command table.
1.1       etheisen  320:  */
1.8       nicm      321: static int
                    322: add_cmd_table(struct tablelist **tlist, char *buf, int len)
1.1       etheisen  323: {
1.8       nicm      324:        struct tablelist *t;
1.1       etheisen  325:
                    326:        if (len == 0)
                    327:                return (0);
                    328:        /*
1.8       nicm      329:         * Allocate a tablelist structure, initialize it,
1.1       etheisen  330:         * and link it into the list of tables.
                    331:         */
1.8       nicm      332:        if ((t = calloc(1, sizeof (struct tablelist))) == NULL) {
1.1       etheisen  333:                return (-1);
                    334:        }
1.4       millert   335:        expand_special_keys(buf, len);
1.1       etheisen  336:        t->t_start = buf;
                    337:        t->t_end = buf + len;
                    338:        t->t_next = *tlist;
                    339:        *tlist = t;
                    340:        return (0);
                    341: }
                    342:
                    343: /*
                    344:  * Add a command table.
                    345:  */
1.8       nicm      346: void
                    347: add_fcmd_table(char *buf, int len)
1.1       etheisen  348: {
                    349:        if (add_cmd_table(&list_fcmd_tables, buf, len) < 0)
                    350:                error("Warning: some commands disabled", NULL_PARG);
                    351: }
                    352:
                    353: /*
                    354:  * Add an editing command table.
                    355:  */
1.8       nicm      356: void
                    357: add_ecmd_table(char *buf, int len)
1.1       etheisen  358: {
                    359:        if (add_cmd_table(&list_ecmd_tables, buf, len) < 0)
                    360:                error("Warning: some edit commands disabled", NULL_PARG);
                    361: }
                    362:
                    363: /*
1.4       millert   364:  * Add an environment variable table.
                    365:  */
1.8       nicm      366: static void
                    367: add_var_table(struct tablelist **tlist, char *buf, int len)
1.4       millert   368: {
                    369:        if (add_cmd_table(tlist, buf, len) < 0)
1.8       nicm      370:                error("Warning: environment variables from "
                    371:                    "lesskey file unavailable", NULL_PARG);
1.4       millert   372: }
                    373:
                    374: /*
1.1       etheisen  375:  * Search a single command table for the command string in cmd.
                    376:  */
1.8       nicm      377: static int
                    378: cmd_search(const char *cmd, char *table, char *endtable, char **sp)
                    379: {
                    380:        char *p;
                    381:        const char *q;
                    382:        int a;
1.1       etheisen  383:
1.4       millert   384:        *sp = NULL;
1.8       nicm      385:        for (p = table, q = cmd;  p < endtable;  p++, q++) {
                    386:                if (*p == *q) {
1.1       etheisen  387:                        /*
                    388:                         * Current characters match.
                    389:                         * If we're at the end of the string, we've found it.
                    390:                         * Return the action code, which is the character
                    391:                         * after the null at the end of the string
                    392:                         * in the command table.
                    393:                         */
1.8       nicm      394:                        if (*p == '\0') {
1.1       etheisen  395:                                a = *++p & 0377;
1.4       millert   396:                                while (a == A_SKIP)
                    397:                                        a = *++p & 0377;
1.8       nicm      398:                                if (a == A_END_LIST) {
1.1       etheisen  399:                                        /*
                    400:                                         * We get here only if the original
                    401:                                         * cmd string passed in was empty ("").
                    402:                                         * I don't think that can happen,
                    403:                                         * but just in case ...
                    404:                                         */
                    405:                                        return (A_UINVALID);
                    406:                                }
                    407:                                /*
                    408:                                 * Check for an "extra" string.
                    409:                                 */
1.8       nicm      410:                                if (a & A_EXTRA) {
1.1       etheisen  411:                                        *sp = ++p;
                    412:                                        a &= ~A_EXTRA;
1.4       millert   413:                                }
1.1       etheisen  414:                                return (a);
                    415:                        }
1.8       nicm      416:                } else if (*q == '\0') {
1.1       etheisen  417:                        /*
                    418:                         * Hit the end of the user's command,
                    419:                         * but not the end of the string in the command table.
                    420:                         * The user's command is incomplete.
                    421:                         */
                    422:                        return (A_PREFIX);
1.8       nicm      423:                } else {
1.1       etheisen  424:                        /*
                    425:                         * Not a match.
                    426:                         * Skip ahead to the next command in the
                    427:                         * command table, and reset the pointer
                    428:                         * to the beginning of the user's command.
                    429:                         */
1.8       nicm      430:                        if (*p == '\0' && p[1] == A_END_LIST) {
1.1       etheisen  431:                                /*
1.8       nicm      432:                                 * A_END_LIST is a special marker that tells
1.1       etheisen  433:                                 * us to abort the cmd search.
                    434:                                 */
                    435:                                return (A_UINVALID);
                    436:                        }
1.4       millert   437:                        while (*p++ != '\0')
                    438:                                continue;
                    439:                        while (*p == A_SKIP)
                    440:                                p++;
1.1       etheisen  441:                        if (*p & A_EXTRA)
1.4       millert   442:                                while (*++p != '\0')
                    443:                                        continue;
1.1       etheisen  444:                        q = cmd-1;
                    445:                }
                    446:        }
                    447:        /*
                    448:         * No match found in the entire command table.
                    449:         */
                    450:        return (A_INVALID);
                    451: }
                    452:
                    453: /*
                    454:  * Decode a command character and return the associated action.
                    455:  * The "extra" string, if any, is returned in sp.
                    456:  */
1.8       nicm      457: static int
                    458: cmd_decode(struct tablelist *tlist, const char *cmd, char **sp)
1.1       etheisen  459: {
1.8       nicm      460:        struct tablelist *t;
                    461:        int action = A_INVALID;
1.1       etheisen  462:
                    463:        /*
                    464:         * Search thru all the command tables.
                    465:         * Stop when we find an action which is not A_INVALID.
                    466:         */
1.8       nicm      467:        for (t = tlist;  t != NULL;  t = t->t_next) {
1.1       etheisen  468:                action = cmd_search(cmd, t->t_start, t->t_end, sp);
                    469:                if (action != A_INVALID)
                    470:                        break;
                    471:        }
1.4       millert   472:        if (action == A_UINVALID)
                    473:                action = A_INVALID;
1.1       etheisen  474:        return (action);
                    475: }
                    476:
                    477: /*
                    478:  * Decode a command from the cmdtables list.
                    479:  */
1.8       nicm      480: int
                    481: fcmd_decode(const char *cmd, char **sp)
1.1       etheisen  482: {
                    483:        return (cmd_decode(list_fcmd_tables, cmd, sp));
                    484: }
                    485:
                    486: /*
                    487:  * Decode a command from the edittables list.
                    488:  */
1.8       nicm      489: int
                    490: ecmd_decode(const char *cmd, char **sp)
1.1       etheisen  491: {
                    492:        return (cmd_decode(list_ecmd_tables, cmd, sp));
                    493: }
                    494:
1.4       millert   495: /*
                    496:  * Get the value of an environment variable.
                    497:  * Looks first in the lesskey file, then in the real environment.
                    498:  */
1.8       nicm      499: char *
                    500: lgetenv(char *var)
1.4       millert   501: {
                    502:        int a;
                    503:        char *s;
                    504:
1.8       nicm      505:        /*
                    506:         * Ignore lookups of any LESS* setting when we are more, and ignore
                    507:         * the less key files
                    508:         */
                    509:        if (less_is_more) {
                    510:                if (strncmp(var, "LESS", 4) == 0) {
                    511:                        return (NULL);
                    512:                }
                    513:                return (getenv(var));
                    514:        }
1.4       millert   515:        a = cmd_decode(list_var_tables, var, &s);
                    516:        if (a == EV_OK)
                    517:                return (s);
                    518:        s = getenv(var);
                    519:        if (s != NULL && *s != '\0')
                    520:                return (s);
                    521:        a = cmd_decode(list_sysvar_tables, var, &s);
                    522:        if (a == EV_OK)
                    523:                return (s);
                    524:        return (NULL);
                    525: }
                    526:
                    527: /*
                    528:  * Get an "integer" from a lesskey file.
1.8       nicm      529:  * Integers are stored in a funny format:
1.4       millert   530:  * two bytes, low order first, in radix KRADIX.
                    531:  */
1.8       nicm      532: static int
                    533: gint(char **sp)
1.1       etheisen  534: {
                    535:        int n;
                    536:
                    537:        n = *(*sp)++;
                    538:        n += *(*sp)++ * KRADIX;
                    539:        return (n);
                    540: }
                    541:
1.4       millert   542: /*
                    543:  * Process an old (pre-v241) lesskey file.
                    544:  */
1.8       nicm      545: static int
                    546: old_lesskey(char *buf, int len)
1.1       etheisen  547: {
                    548:        /*
                    549:         * Old-style lesskey file.
1.8       nicm      550:         * The file must end with either
1.9       deraadt   551:         *      ..,cmd,0,action
1.8       nicm      552:         * or   ...,cmd,0,action|A_EXTRA,string,0
1.1       etheisen  553:         * So the last byte or the second to last byte must be zero.
                    554:         */
                    555:        if (buf[len-1] != '\0' && buf[len-2] != '\0')
                    556:                return (-1);
                    557:        add_fcmd_table(buf, len);
                    558:        return (0);
                    559: }
                    560:
1.8       nicm      561: /*
1.4       millert   562:  * Process a new (post-v241) lesskey file.
                    563:  */
1.8       nicm      564: static int
                    565: new_lesskey(char *buf, int len, int sysvar)
1.1       etheisen  566: {
                    567:        char *p;
1.8       nicm      568:        int c;
                    569:        int n;
1.1       etheisen  570:
                    571:        /*
                    572:         * New-style lesskey file.
                    573:         * Extract the pieces.
                    574:         */
                    575:        if (buf[len-3] != C0_END_LESSKEY_MAGIC ||
                    576:            buf[len-2] != C1_END_LESSKEY_MAGIC ||
                    577:            buf[len-1] != C2_END_LESSKEY_MAGIC)
                    578:                return (-1);
                    579:        p = buf + 4;
1.8       nicm      580:        for (;;) {
1.1       etheisen  581:                c = *p++;
1.8       nicm      582:                switch (c) {
1.1       etheisen  583:                case CMD_SECTION:
                    584:                        n = gint(&p);
                    585:                        add_fcmd_table(p, n);
                    586:                        p += n;
                    587:                        break;
                    588:                case EDIT_SECTION:
                    589:                        n = gint(&p);
                    590:                        add_ecmd_table(p, n);
                    591:                        p += n;
                    592:                        break;
1.4       millert   593:                case VAR_SECTION:
                    594:                        n = gint(&p);
1.8       nicm      595:                        add_var_table((sysvar) ?
                    596:                            &list_sysvar_tables : &list_var_tables, p, n);
1.4       millert   597:                        p += n;
                    598:                        break;
1.1       etheisen  599:                case END_SECTION:
1.4       millert   600:                        return (0);
1.1       etheisen  601:                default:
1.4       millert   602:                        /*
                    603:                         * Unrecognized section type.
                    604:                         */
1.1       etheisen  605:                        return (-1);
                    606:                }
                    607:        }
                    608: }
                    609:
                    610: /*
                    611:  * Set up a user command table, based on a "lesskey" file.
                    612:  */
1.8       nicm      613: int
                    614: lesskey(char *filename, int sysvar)
1.1       etheisen  615: {
1.8       nicm      616:        char *buf;
                    617:        off_t len;
                    618:        long n;
                    619:        int f;
1.1       etheisen  620:
1.4       millert   621:        if (secure)
                    622:                return (1);
1.1       etheisen  623:        /*
                    624:         * Try to open the lesskey file.
                    625:         */
1.4       millert   626:        filename = shell_unquote(filename);
1.1       etheisen  627:        f = open(filename, OPEN_READ);
1.4       millert   628:        free(filename);
1.1       etheisen  629:        if (f < 0)
                    630:                return (1);
                    631:
                    632:        /*
                    633:         * Read the file into a buffer.
                    634:         * We first figure out the size of the file and allocate space for it.
                    635:         * {{ Minimal error checking is done here.
                    636:         *    A garbage .less file will produce strange results.
                    637:         *    To avoid a large amount of error checking code here, we
                    638:         *    rely on the lesskey program to generate a good .less file. }}
                    639:         */
                    640:        len = filesize(f);
1.8       nicm      641:        if (len == -1 || len < 3) {
1.1       etheisen  642:                /*
                    643:                 * Bad file (valid file must have at least 3 chars).
                    644:                 */
1.8       nicm      645:                (void) close(f);
1.1       etheisen  646:                return (-1);
                    647:        }
1.8       nicm      648:        if ((buf = calloc((int)len, sizeof (char))) == NULL) {
                    649:                (void) close(f);
1.1       etheisen  650:                return (-1);
                    651:        }
1.8       nicm      652:        if (lseek(f, (off_t)0, SEEK_SET) == BAD_LSEEK) {
1.1       etheisen  653:                free(buf);
1.8       nicm      654:                (void) close(f);
1.1       etheisen  655:                return (-1);
                    656:        }
                    657:        n = read(f, buf, (unsigned int) len);
                    658:        close(f);
1.8       nicm      659:        if (n != len) {
1.1       etheisen  660:                free(buf);
                    661:                return (-1);
                    662:        }
                    663:
                    664:        /*
                    665:         * Figure out if this is an old-style (before version 241)
                    666:         * or new-style lesskey file format.
                    667:         */
                    668:        if (buf[0] != C0_LESSKEY_MAGIC || buf[1] != C1_LESSKEY_MAGIC ||
                    669:            buf[2] != C2_LESSKEY_MAGIC || buf[3] != C3_LESSKEY_MAGIC)
                    670:                return (old_lesskey(buf, (int)len));
1.4       millert   671:        return (new_lesskey(buf, (int)len, sysvar));
1.1       etheisen  672: }
                    673:
                    674: /*
                    675:  * Add the standard lesskey file "$HOME/.less"
                    676:  */
1.8       nicm      677: void
                    678: add_hometable(char *envname, char *def_filename, int sysvar)
1.1       etheisen  679: {
                    680:        char *filename;
                    681:        PARG parg;
                    682:
1.4       millert   683:        if (envname != NULL && (filename = lgetenv(envname)) != NULL)
                    684:                filename = save(filename);
                    685:        else if (sysvar)
                    686:                filename = save(def_filename);
                    687:        else
                    688:                filename = homefile(def_filename);
1.1       etheisen  689:        if (filename == NULL)
                    690:                return;
1.8       nicm      691:        if (lesskey(filename, sysvar) < 0) {
1.1       etheisen  692:                parg.p_string = filename;
                    693:                error("Cannot use lesskey file \"%s\"", &parg);
                    694:        }
                    695:        free(filename);
                    696: }
                    697:
                    698: /*
                    699:  * See if a char is a special line-editing command.
                    700:  */
1.8       nicm      701: int
                    702: editchar(int c, int flags)
1.1       etheisen  703: {
                    704:        int action;
                    705:        int nch;
                    706:        char *s;
                    707:        char usercmd[MAX_CMDLEN+1];
1.8       nicm      708:
1.1       etheisen  709:        /*
                    710:         * An editing character could actually be a sequence of characters;
                    711:         * for example, an escape sequence sent by pressing the uparrow key.
                    712:         * To match the editing string, we use the command decoder
                    713:         * but give it the edit-commands command table
                    714:         * This table is constructed to match the user's keyboard.
                    715:         */
1.6       shadchin  716:        if (c == erase_char || c == erase2_char)
1.1       etheisen  717:                return (EC_BACKSPACE);
                    718:        if (c == kill_char)
                    719:                return (EC_LINEKILL);
1.8       nicm      720:
1.1       etheisen  721:        /*
                    722:         * Collect characters in a buffer.
                    723:         * Start with the one we have, and get more if we need them.
                    724:         */
                    725:        nch = 0;
                    726:        do {
1.8       nicm      727:                if (nch > 0)
1.1       etheisen  728:                        c = getcc();
1.8       nicm      729:                usercmd[nch] = (char)c;
1.1       etheisen  730:                usercmd[nch+1] = '\0';
                    731:                nch++;
                    732:                action = ecmd_decode(usercmd, &s);
                    733:        } while (action == A_PREFIX);
1.8       nicm      734:
                    735:        if (flags & EC_NORIGHTLEFT) {
                    736:                switch (action) {
1.4       millert   737:                case EC_RIGHT:
                    738:                case EC_LEFT:
                    739:                        action = A_INVALID;
                    740:                        break;
                    741:                }
                    742:        }
1.8       nicm      743:        if (flags & EC_NOHISTORY) {
1.1       etheisen  744:                /*
                    745:                 * The caller says there is no history list.
                    746:                 * Reject any history-manipulation action.
                    747:                 */
1.8       nicm      748:                switch (action) {
1.1       etheisen  749:                case EC_UP:
                    750:                case EC_DOWN:
                    751:                        action = A_INVALID;
                    752:                        break;
                    753:                }
                    754:        }
1.8       nicm      755:        if (flags & EC_NOCOMPLETE) {
1.1       etheisen  756:                /*
                    757:                 * The caller says we don't want any filename completion cmds.
                    758:                 * Reject them.
                    759:                 */
1.8       nicm      760:                switch (action) {
1.1       etheisen  761:                case EC_F_COMPLETE:
                    762:                case EC_B_COMPLETE:
                    763:                case EC_EXPAND:
                    764:                        action = A_INVALID;
                    765:                        break;
                    766:                }
                    767:        }
1.8       nicm      768:        if ((flags & EC_PEEK) || action == A_INVALID) {
1.1       etheisen  769:                /*
                    770:                 * We're just peeking, or we didn't understand the command.
                    771:                 * Unget all the characters we read in the loop above.
1.8       nicm      772:                 * This does NOT include the original character that was
1.1       etheisen  773:                 * passed in as a parameter.
                    774:                 */
1.8       nicm      775:                while (nch > 1) {
1.1       etheisen  776:                        ungetcc(usercmd[--nch]);
                    777:                }
1.8       nicm      778:        } else {
1.1       etheisen  779:                if (s != NULL)
                    780:                        ungetsc(s);
                    781:        }
1.8       nicm      782:        return (action);
1.1       etheisen  783: }