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

Annotation of src/usr.bin/less/lesskey.c, Revision 1.12

1.1       etheisen    1: /*
1.7       shadchin    2:  * Copyright (C) 1984-2012  Mark Nudelman
1.9       nicm        3:  * Modified for use with illumos by Garrett D'Amore.
                      4:  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
1.5       millert     5:  *
                      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:  *     lesskey [-o output] [input]
                     14:  *
1.8       nicm       15:  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1.1       etheisen   16:  *
                     17:  *     Make a .less file.
                     18:  *     If no input file is specified, standard input is used.
                     19:  *     If no output file is specified, $HOME/.less is used.
                     20:  *
                     21:  *     The .less file is used to specify (to "less") user-defined
                     22:  *     key bindings.  Basically any sequence of 1 to MAX_CMDLEN
                     23:  *     keystrokes may be bound to an existing less function.
                     24:  *
1.8       nicm       25:  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1.1       etheisen   26:  *
1.8       nicm       27:  *     The input file is an ascii file consisting of a
1.1       etheisen   28:  *     sequence of lines of the form:
                     29:  *             string <whitespace> action [chars] <newline>
                     30:  *
                     31:  *     "string" is a sequence of command characters which form
                     32:  *             the new user-defined command.  The command
                     33:  *             characters may be:
                     34:  *             1. The actual character itself.
                     35:  *             2. A character preceded by ^ to specify a
                     36:  *                control character (e.g. ^X means control-X).
                     37:  *             3. A backslash followed by one to three octal digits
                     38:  *                to specify a character by its octal value.
                     39:  *             4. A backslash followed by b, e, n, r or t
                     40:  *                to specify \b, ESC, \n, \r or \t, respectively.
1.8       nicm       41:  *             5. Any character (other than those mentioned above) preceded
1.1       etheisen   42:  *                by a \ to specify the character itself (characters which
                     43:  *                must be preceded by \ include ^, \, and whitespace.
                     44:  *     "action" is the name of a "less" action, from the table below.
                     45:  *     "chars" is an optional sequence of characters which is treated
                     46:  *             as keyboard input after the command is executed.
                     47:  *
1.8       nicm       48:  *     Blank lines and lines which start with # are ignored,
1.1       etheisen   49:  *     except for the special control lines:
1.5       millert    50:  *             #command        Signals the beginning of the command
                     51:  *                             keys section.
1.1       etheisen   52:  *             #line-edit      Signals the beginning of the line-editing
                     53:  *                             keys section.
1.5       millert    54:  *             #env            Signals the beginning of the environment
                     55:  *                             variable section.
1.1       etheisen   56:  *             #stop           Stops command parsing in less;
                     57:  *                             causes all default keys to be disabled.
                     58:  *
1.8       nicm       59:  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1.1       etheisen   60:  *
                     61:  *     The output file is a non-ascii file, consisting of a header,
                     62:  *     one or more sections, and a trailer.
                     63:  *     Each section begins with a section header, a section length word
                     64:  *     and the section data.  Normally there are three sections:
                     65:  *             CMD_SECTION     Definition of command keys.
                     66:  *             EDIT_SECTION    Definition of editing keys.
1.8       nicm       67:  *             END_SECTION     A special section header, with no
1.1       etheisen   68:  *                             length word or section data.
                     69:  *
                     70:  *     Section data consists of zero or more byte sequences of the form:
                     71:  *             string <0> <action>
                     72:  *     or
                     73:  *             string <0> <action|A_EXTRA> chars <0>
                     74:  *
                     75:  *     "string" is the command string.
                     76:  *     "<0>" is one null byte.
                     77:  *     "<action>" is one byte containing the action code (the A_xxx value).
                     78:  *     If action is ORed with A_EXTRA, the action byte is followed
                     79:  *             by the null-terminated "chars" string.
                     80:  *
1.8       nicm       81:  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1.1       etheisen   82:  */
                     83:
                     84: #include "less.h"
                     85: #include "lesskey.h"
                     86: #include "cmd.h"
                     87:
1.11      deraadt    88: struct cmdname {
1.1       etheisen   89:        char *cn_name;
                     90:        int cn_action;
                     91: };
                     92:
1.8       nicm       93: static void lkerr(char *s);
                     94:
1.11      deraadt    95: struct cmdname cmdnames[] = {
1.8       nicm       96:        { "back-bracket",               A_B_BRACKET },
                     97:        { "back-line",                  A_B_LINE },
                     98:        { "back-line-force",            A_BF_LINE },
                     99:        { "back-screen",                A_B_SCREEN },
                    100:        { "back-scroll",                A_B_SCROLL },
                    101:        { "back-search",                A_B_SEARCH },
                    102:        { "back-window",                A_B_WINDOW },
                    103:        { "debug",                      A_DEBUG },
                    104:        { "digit",                      A_DIGIT },
                    105:        { "display-flag",               A_DISP_OPTION },
                    106:        { "display-option",             A_DISP_OPTION },
                    107:        { "end",                        A_GOEND },
                    108:        { "examine",                    A_EXAMINE },
                    109:        { "filter",                     A_FILTER },
                    110:        { "first-cmd",                  A_FIRSTCMD },
                    111:        { "firstcmd",                   A_FIRSTCMD },
                    112:        { "flush-repaint",              A_FREPAINT },
                    113:        { "forw-bracket",               A_F_BRACKET },
                    114:        { "forw-forever",               A_F_FOREVER },
                    115:        { "forw-until-hilite",          A_F_UNTIL_HILITE },
                    116:        { "forw-line",                  A_F_LINE },
                    117:        { "forw-line-force",            A_FF_LINE },
                    118:        { "forw-screen",                A_F_SCREEN },
                    119:        { "forw-screen-force",          A_FF_SCREEN },
                    120:        { "forw-scroll",                A_F_SCROLL },
                    121:        { "forw-search",                A_F_SEARCH },
                    122:        { "forw-skip",                  A_F_SKIP },
                    123:        { "forw-window",                A_F_WINDOW },
                    124:        { "goto-end",                   A_GOEND },
                    125:        { "goto-line",                  A_GOLINE },
                    126:        { "goto-mark",                  A_GOMARK },
                    127:        { "help",                       A_HELP },
                    128:        { "index-file",                 A_INDEX_FILE },
                    129:        { "invalid",                    A_UINVALID },
                    130:        { "left-scroll",                A_LSHIFT },
                    131:        { "next-file",                  A_NEXT_FILE },
                    132:        { "next-tag",                   A_NEXT_TAG },
                    133:        { "noaction",                   A_NOACTION },
                    134:        { "percent",                    A_PERCENT },
                    135:        { "pipe",                       A_PIPE },
                    136:        { "prev-file",                  A_PREV_FILE },
                    137:        { "prev-tag",                   A_PREV_TAG },
                    138:        { "quit",                       A_QUIT },
                    139:        { "remove-file",                A_REMOVE_FILE },
                    140:        { "repaint",                    A_REPAINT },
                    141:        { "repaint-flush",              A_FREPAINT },
                    142:        { "repeat-search",              A_AGAIN_SEARCH },
                    143:        { "repeat-search-all",          A_T_AGAIN_SEARCH },
                    144:        { "reverse-search",             A_REVERSE_SEARCH },
                    145:        { "reverse-search-all",         A_T_REVERSE_SEARCH },
                    146:        { "right-scroll",               A_RSHIFT },
                    147:        { "set-mark",                   A_SETMARK },
                    148:        { "status",                     A_STAT },
                    149:        { "toggle-flag",                A_OPT_TOGGLE },
                    150:        { "toggle-option",              A_OPT_TOGGLE },
                    151:        { "undo-hilite",                A_UNDO_SEARCH },
                    152:        { "version",                    A_VERSION },
                    153:        { "visual",                     A_VISUAL },
                    154:        { NULL,                         0 }
1.1       etheisen  155: };
                    156:
1.8       nicm      157: struct cmdname editnames[] = {
1.5       millert   158:        { "back-complete",      EC_B_COMPLETE },
                    159:        { "backspace",          EC_BACKSPACE },
                    160:        { "delete",             EC_DELETE },
                    161:        { "down",               EC_DOWN },
                    162:        { "end",                EC_END },
                    163:        { "expand",             EC_EXPAND },
                    164:        { "forw-complete",      EC_F_COMPLETE },
                    165:        { "home",               EC_HOME },
                    166:        { "insert",             EC_INSERT },
                    167:        { "invalid",            EC_UINVALID },
                    168:        { "kill-line",          EC_LINEKILL },
1.6       shadchin  169:        { "abort",              EC_ABORT },
1.5       millert   170:        { "left",               EC_LEFT },
                    171:        { "literal",            EC_LITERAL },
                    172:        { "right",              EC_RIGHT },
                    173:        { "up",                 EC_UP },
                    174:        { "word-backspace",     EC_W_BACKSPACE },
                    175:        { "word-delete",        EC_W_DELETE },
                    176:        { "word-left",          EC_W_LEFT },
                    177:        { "word-right",         EC_W_RIGHT },
                    178:        { NULL, 0 }
1.1       etheisen  179: };
                    180:
1.11      deraadt   181: struct table {
1.1       etheisen  182:        struct cmdname *names;
                    183:        char *pbuffer;
                    184:        char buffer[MAX_USERCMD];
                    185: };
                    186:
                    187: struct table cmdtable;
                    188: struct table edittable;
1.5       millert   189: struct table vartable;
1.1       etheisen  190: struct table *currtable = &cmdtable;
                    191:
                    192: char fileheader[] = {
1.8       nicm      193:        C0_LESSKEY_MAGIC,
                    194:        C1_LESSKEY_MAGIC,
                    195:        C2_LESSKEY_MAGIC,
1.1       etheisen  196:        C3_LESSKEY_MAGIC
                    197: };
                    198: char filetrailer[] = {
1.8       nicm      199:        C0_END_LESSKEY_MAGIC,
                    200:        C1_END_LESSKEY_MAGIC,
1.1       etheisen  201:        C2_END_LESSKEY_MAGIC
                    202: };
                    203: char cmdsection[1] =   { CMD_SECTION };
                    204: char editsection[1] =  { EDIT_SECTION };
1.5       millert   205: char varsection[1] =   { VAR_SECTION };
1.1       etheisen  206: char endsection[1] =   { END_SECTION };
                    207:
                    208: char *infile = NULL;
1.8       nicm      209: char *outfile = NULL;
1.1       etheisen  210:
                    211: int linenum;
                    212: int errors;
                    213:
                    214: extern char version[];
                    215:
1.8       nicm      216: void
                    217: usage(void)
1.5       millert   218: {
1.8       nicm      219:        (void) fprintf(stderr, "usage: lesskey [-o output] [input]\n");
1.5       millert   220:        exit(1);
                    221: }
                    222:
1.8       nicm      223: char *
                    224: mkpathname(char *dirname, char *filename)
1.1       etheisen  225: {
                    226:        char *pathname;
1.4       deraadt   227:        size_t len;
1.1       etheisen  228:
1.4       deraadt   229:        len = strlen(dirname) + strlen(filename) + 2;
1.8       nicm      230:        pathname = calloc(1, len);
                    231:        if (pathname == NULL) {
                    232:                fprintf(stderr, "mkpathname: out of memory\n");
                    233:                exit(1);
                    234:        }
                    235:        (void) snprintf(pathname, len, "%s/%s", dirname, filename);
1.1       etheisen  236:        return (pathname);
                    237: }
                    238:
                    239: /*
                    240:  * Figure out the name of a default file (in the user's HOME directory).
                    241:  */
1.8       nicm      242: char *
                    243: homefile(char *filename)
1.1       etheisen  244: {
                    245:        char *p;
                    246:        char *pathname;
                    247:
1.8       nicm      248:        if ((p = getenv("HOME")) != NULL && *p != '\0') {
1.1       etheisen  249:                pathname = mkpathname(p, filename);
1.8       nicm      250:        } else {
                    251:                (void) fprintf(stderr, "cannot find $HOME - "
                    252:                    "using current directory\n");
1.1       etheisen  253:                pathname = mkpathname(".", filename);
                    254:        }
                    255:        return (pathname);
                    256: }
                    257:
                    258: /*
                    259:  * Parse command line arguments.
                    260:  */
1.8       nicm      261: void
                    262: parse_args(int argc, char **argv)
1.1       etheisen  263: {
1.5       millert   264:        char *arg;
                    265:
1.1       etheisen  266:        outfile = NULL;
1.8       nicm      267:        while (--argc > 0) {
1.5       millert   268:                arg = *++argv;
                    269:                if (arg[0] != '-')
                    270:                        /* Arg does not start with "-"; it's not an option. */
                    271:                        break;
                    272:                if (arg[1] == '\0')
                    273:                        /* "-" means standard input. */
                    274:                        break;
1.8       nicm      275:                if (arg[1] == '-' && arg[2] == '\0') {
1.5       millert   276:                        /* "--" means end of options. */
                    277:                        argc--;
                    278:                        argv++;
                    279:                        break;
                    280:                }
1.8       nicm      281:                switch (arg[1]) {
1.5       millert   282:                case '-':
1.8       nicm      283:                        if (strncmp(arg, "--output", 8) == 0) {
1.5       millert   284:                                if (arg[8] == '\0')
                    285:                                        outfile = &arg[8];
                    286:                                else if (arg[8] == '=')
                    287:                                        outfile = &arg[9];
                    288:                                else
                    289:                                        usage();
                    290:                                goto opt_o;
                    291:                        }
1.8       nicm      292:                        if (strcmp(arg, "--version") == 0) {
1.5       millert   293:                                goto opt_V;
                    294:                        }
                    295:                        usage();
                    296:                        break;
1.1       etheisen  297:                case 'o':
                    298:                        outfile = &argv[0][2];
1.5       millert   299:                opt_o:
1.8       nicm      300:                        if (*outfile == '\0') {
1.1       etheisen  301:                                if (--argc <= 0)
                    302:                                        usage();
                    303:                                outfile = *(++argv);
                    304:                        }
                    305:                        break;
                    306:                case 'V':
1.5       millert   307:                opt_V:
1.8       nicm      308:                        (void) printf("lesskey  version %s\n", version);
1.1       etheisen  309:                        exit(0);
                    310:                default:
                    311:                        usage();
                    312:                }
                    313:        }
                    314:        if (argc > 1)
                    315:                usage();
                    316:        /*
                    317:         * Open the input file, or use DEF_LESSKEYINFILE if none specified.
                    318:         */
                    319:        if (argc > 0)
                    320:                infile = *argv;
                    321:        else
                    322:                infile = homefile(DEF_LESSKEYINFILE);
                    323: }
                    324:
                    325: /*
                    326:  * Initialize data structures.
                    327:  */
1.8       nicm      328: void
                    329: init_tables(void)
1.1       etheisen  330: {
                    331:        cmdtable.names = cmdnames;
                    332:        cmdtable.pbuffer = cmdtable.buffer;
                    333:
                    334:        edittable.names = editnames;
                    335:        edittable.pbuffer = edittable.buffer;
1.5       millert   336:
                    337:        vartable.names = NULL;
                    338:        vartable.pbuffer = vartable.buffer;
1.1       etheisen  339: }
                    340:
                    341: /*
                    342:  * Parse one character of a string.
                    343:  */
1.8       nicm      344: char *
                    345: tstr(char **pp, int xlate)
                    346: {
                    347:        char *p;
                    348:        char ch;
                    349:        int i;
1.5       millert   350:        static char buf[10];
                    351:        static char tstr_control_k[] =
                    352:                { SK_SPECIAL_KEY, SK_CONTROL_K, 6, 1, 1, 1, '\0' };
1.1       etheisen  353:
                    354:        p = *pp;
1.8       nicm      355:        switch (*p) {
1.1       etheisen  356:        case '\\':
                    357:                ++p;
1.8       nicm      358:                switch (*p) {
1.1       etheisen  359:                case '0': case '1': case '2': case '3':
                    360:                case '4': case '5': case '6': case '7':
                    361:                        /*
                    362:                         * Parse an octal number.
                    363:                         */
                    364:                        ch = 0;
                    365:                        i = 0;
                    366:                        do
                    367:                                ch = 8*ch + (*p - '0');
1.8       nicm      368:                        while (*++p >= '0' && *p <= '7' && ++i < 3)
                    369:                                ;
1.1       etheisen  370:                        *pp = p;
1.5       millert   371:                        if (xlate && ch == CONTROL('K'))
1.8       nicm      372:                                return (tstr_control_k);
1.5       millert   373:                        buf[0] = ch;
                    374:                        buf[1] = '\0';
                    375:                        return (buf);
1.1       etheisen  376:                case 'b':
                    377:                        *pp = p+1;
1.5       millert   378:                        return ("\b");
1.1       etheisen  379:                case 'e':
                    380:                        *pp = p+1;
1.5       millert   381:                        buf[0] = ESC;
                    382:                        buf[1] = '\0';
                    383:                        return (buf);
1.1       etheisen  384:                case 'n':
                    385:                        *pp = p+1;
1.5       millert   386:                        return ("\n");
1.1       etheisen  387:                case 'r':
                    388:                        *pp = p+1;
1.5       millert   389:                        return ("\r");
1.1       etheisen  390:                case 't':
                    391:                        *pp = p+1;
1.5       millert   392:                        return ("\t");
                    393:                case 'k':
1.8       nicm      394:                        if (xlate) {
                    395:                                switch (*++p) {
1.5       millert   396:                                case 'u': ch = SK_UP_ARROW; break;
                    397:                                case 'd': ch = SK_DOWN_ARROW; break;
                    398:                                case 'r': ch = SK_RIGHT_ARROW; break;
                    399:                                case 'l': ch = SK_LEFT_ARROW; break;
                    400:                                case 'U': ch = SK_PAGE_UP; break;
                    401:                                case 'D': ch = SK_PAGE_DOWN; break;
                    402:                                case 'h': ch = SK_HOME; break;
                    403:                                case 'e': ch = SK_END; break;
                    404:                                case 'x': ch = SK_DELETE; break;
                    405:                                default:
1.8       nicm      406:                                        lkerr("illegal char after \\k");
1.5       millert   407:                                        *pp = p+1;
                    408:                                        return ("");
                    409:                                }
                    410:                                *pp = p+1;
                    411:                                buf[0] = SK_SPECIAL_KEY;
                    412:                                buf[1] = ch;
                    413:                                buf[2] = 6;
                    414:                                buf[3] = 1;
                    415:                                buf[4] = 1;
                    416:                                buf[5] = 1;
                    417:                                buf[6] = '\0';
                    418:                                return (buf);
                    419:                        }
                    420:                        /* FALLTHRU */
1.1       etheisen  421:                default:
                    422:                        /*
1.8       nicm      423:                         * Backslash followed by any other char
1.1       etheisen  424:                         * just means that char.
                    425:                         */
                    426:                        *pp = p+1;
1.5       millert   427:                        buf[0] = *p;
                    428:                        buf[1] = '\0';
                    429:                        if (xlate && buf[0] == CONTROL('K'))
1.8       nicm      430:                                return (tstr_control_k);
1.5       millert   431:                        return (buf);
1.1       etheisen  432:                }
                    433:        case '^':
                    434:                /*
1.7       shadchin  435:                 * Caret means CONTROL.
1.1       etheisen  436:                 */
                    437:                *pp = p+2;
1.5       millert   438:                buf[0] = CONTROL(p[1]);
                    439:                buf[1] = '\0';
                    440:                if (buf[0] == CONTROL('K'))
1.8       nicm      441:                        return (tstr_control_k);
1.5       millert   442:                return (buf);
1.1       etheisen  443:        }
                    444:        *pp = p+1;
1.5       millert   445:        buf[0] = *p;
                    446:        buf[1] = '\0';
                    447:        if (xlate && buf[0] == CONTROL('K'))
1.8       nicm      448:                return (tstr_control_k);
1.5       millert   449:        return (buf);
1.1       etheisen  450: }
                    451:
                    452: /*
                    453:  * Skip leading spaces in a string.
                    454:  */
1.8       nicm      455: char *
                    456: skipsp(char *s)
1.1       etheisen  457: {
1.8       nicm      458:        while (*s == ' ' || *s == '\t')
1.1       etheisen  459:                s++;
                    460:        return (s);
                    461: }
                    462:
                    463: /*
                    464:  * Skip non-space characters in a string.
                    465:  */
1.8       nicm      466: char *
                    467: skipnsp(char *s)
1.1       etheisen  468: {
                    469:        while (*s != '\0' && *s != ' ' && *s != '\t')
                    470:                s++;
                    471:        return (s);
                    472: }
                    473:
                    474: /*
                    475:  * Clean up an input line:
                    476:  * strip off the trailing newline & any trailing # comment.
                    477:  */
1.8       nicm      478: char *
                    479: clean_line(char *s)
1.1       etheisen  480: {
1.8       nicm      481:        int i;
1.1       etheisen  482:
                    483:        s = skipsp(s);
1.5       millert   484:        for (i = 0;  s[i] != '\n' && s[i] != '\r' && s[i] != '\0';  i++)
1.1       etheisen  485:                if (s[i] == '#' && (i == 0 || s[i-1] != '\\'))
                    486:                        break;
                    487:        s[i] = '\0';
                    488:        return (s);
                    489: }
                    490:
                    491: /*
                    492:  * Add a byte to the output command table.
                    493:  */
1.8       nicm      494: void
                    495: add_cmd_char(int c)
                    496: {
                    497:        if (currtable->pbuffer >= currtable->buffer + MAX_USERCMD) {
                    498:                lkerr("too many commands");
1.1       etheisen  499:                exit(1);
                    500:        }
1.8       nicm      501:        *(currtable->pbuffer)++ = (char)c;
1.1       etheisen  502: }
                    503:
                    504: /*
1.5       millert   505:  * Add a string to the output command table.
                    506:  */
1.8       nicm      507: void
                    508: add_cmd_str(char *s)
1.5       millert   509: {
1.8       nicm      510:        for (; *s != '\0'; s++)
1.5       millert   511:                add_cmd_char(*s);
                    512: }
                    513:
                    514: /*
1.1       etheisen  515:  * See if we have a special "control" line.
                    516:  */
1.8       nicm      517: int
                    518: control_line(char *s)
1.1       etheisen  519: {
1.8       nicm      520: #define        PREFIX(str, pat)        (strncmp(str, pat, strlen(pat)) == 0)
1.1       etheisen  521:
1.8       nicm      522:        if (PREFIX(s, "#line-edit")) {
1.1       etheisen  523:                currtable = &edittable;
                    524:                return (1);
                    525:        }
1.8       nicm      526:        if (PREFIX(s, "#command")) {
1.1       etheisen  527:                currtable = &cmdtable;
                    528:                return (1);
                    529:        }
1.8       nicm      530:        if (PREFIX(s, "#env")) {
1.5       millert   531:                currtable = &vartable;
                    532:                return (1);
                    533:        }
1.8       nicm      534:        if (PREFIX(s, "#stop")) {
1.1       etheisen  535:                add_cmd_char('\0');
                    536:                add_cmd_char(A_END_LIST);
                    537:                return (1);
                    538:        }
                    539:        return (0);
                    540: }
                    541:
                    542: /*
                    543:  * Output some bytes.
                    544:  */
1.8       nicm      545: void
                    546: fputbytes(FILE *fd, char *buf, int len)
                    547: {
                    548:        while (len-- > 0) {
                    549:                (void) fwrite(buf, sizeof (char), 1, fd);
1.1       etheisen  550:                buf++;
                    551:        }
                    552: }
                    553:
                    554: /*
                    555:  * Output an integer, in special KRADIX form.
                    556:  */
1.8       nicm      557: void
                    558: fputint(FILE *fd, unsigned int val)
1.1       etheisen  559: {
                    560:        char c;
                    561:
1.8       nicm      562:        if (val >= KRADIX*KRADIX) {
                    563:                (void) fprintf(stderr, "error: integer too big (%d > %d)\n",
                    564:                    val, KRADIX*KRADIX);
1.1       etheisen  565:                exit(1);
                    566:        }
                    567:        c = val % KRADIX;
1.8       nicm      568:        (void) fwrite(&c, sizeof (char), 1, fd);
1.1       etheisen  569:        c = val / KRADIX;
1.8       nicm      570:        (void) fwrite(&c, sizeof (char), 1, fd);
1.1       etheisen  571: }
                    572:
                    573: /*
                    574:  * Find an action, given the name of the action.
                    575:  */
1.8       nicm      576: int
                    577: findaction(char *actname)
1.1       etheisen  578: {
                    579:        int i;
                    580:
                    581:        for (i = 0;  currtable->names[i].cn_name != NULL;  i++)
                    582:                if (strcmp(currtable->names[i].cn_name, actname) == 0)
                    583:                        return (currtable->names[i].cn_action);
1.8       nicm      584:        lkerr("unknown action");
1.1       etheisen  585:        return (A_INVALID);
                    586: }
                    587:
1.8       nicm      588: void
                    589: lkerr(char *s)
1.1       etheisen  590: {
1.8       nicm      591:        (void) fprintf(stderr, "line %d: %s\n", linenum, s);
1.1       etheisen  592:        errors++;
                    593: }
                    594:
                    595:
1.8       nicm      596: void
                    597: parse_cmdline(char *p)
1.1       etheisen  598: {
                    599:        int cmdlen;
                    600:        char *actname;
                    601:        int action;
1.5       millert   602:        char *s;
                    603:        char c;
1.1       etheisen  604:
                    605:        /*
                    606:         * Parse the command string and store it in the current table.
                    607:         */
                    608:        cmdlen = 0;
1.8       nicm      609:        do {
1.5       millert   610:                s = tstr(&p, 1);
                    611:                cmdlen += strlen(s);
                    612:                if (cmdlen > MAX_CMDLEN)
1.8       nicm      613:                        lkerr("command too long");
1.1       etheisen  614:                else
1.5       millert   615:                        add_cmd_str(s);
1.1       etheisen  616:        } while (*p != ' ' && *p != '\t' && *p != '\0');
                    617:        /*
                    618:         * Terminate the command string with a null byte.
                    619:         */
                    620:        add_cmd_char('\0');
                    621:
                    622:        /*
                    623:         * Skip white space between the command string
                    624:         * and the action name.
                    625:         * Terminate the action name with a null byte.
                    626:         */
                    627:        p = skipsp(p);
1.8       nicm      628:        if (*p == '\0') {
                    629:                lkerr("missing action");
1.1       etheisen  630:                return;
                    631:        }
                    632:        actname = p;
                    633:        p = skipnsp(p);
                    634:        c = *p;
                    635:        *p = '\0';
                    636:
                    637:        /*
                    638:         * Parse the action name and store it in the current table.
                    639:         */
                    640:        action = findaction(actname);
                    641:
                    642:        /*
                    643:         * See if an extra string follows the action name.
                    644:         */
                    645:        *p = c;
                    646:        p = skipsp(p);
1.8       nicm      647:        if (*p == '\0') {
1.1       etheisen  648:                add_cmd_char(action);
1.8       nicm      649:        } else {
1.1       etheisen  650:                /*
                    651:                 * OR the special value A_EXTRA into the action byte.
                    652:                 * Put the extra string after the action byte.
                    653:                 */
                    654:                add_cmd_char(action | A_EXTRA);
                    655:                while (*p != '\0')
1.5       millert   656:                        add_cmd_str(tstr(&p, 0));
1.1       etheisen  657:                add_cmd_char('\0');
                    658:        }
                    659: }
                    660:
1.8       nicm      661: void
                    662: parse_varline(char *p)
1.5       millert   663: {
                    664:        char *s;
                    665:
1.8       nicm      666:        do {
1.5       millert   667:                s = tstr(&p, 0);
                    668:                add_cmd_str(s);
                    669:        } while (*p != ' ' && *p != '\t' && *p != '=' && *p != '\0');
                    670:        /*
                    671:         * Terminate the variable name with a null byte.
                    672:         */
                    673:        add_cmd_char('\0');
                    674:
                    675:        p = skipsp(p);
1.8       nicm      676:        if (*p++ != '=') {
                    677:                lkerr("missing =");
1.5       millert   678:                return;
                    679:        }
                    680:
                    681:        add_cmd_char(EV_OK|A_EXTRA);
                    682:
                    683:        p = skipsp(p);
1.8       nicm      684:        while (*p != '\0') {
1.5       millert   685:                s = tstr(&p, 0);
                    686:                add_cmd_str(s);
                    687:        }
                    688:        add_cmd_char('\0');
                    689: }
                    690:
                    691: /*
                    692:  * Parse a line from the lesskey file.
                    693:  */
1.8       nicm      694: void
                    695: parse_line(char *line)
1.5       millert   696: {
                    697:        char *p;
                    698:
                    699:        /*
                    700:         * See if it is a control line.
                    701:         */
                    702:        if (control_line(line))
                    703:                return;
                    704:        /*
                    705:         * Skip leading white space.
                    706:         * Replace the final newline with a null byte.
                    707:         * Ignore blank lines and comments.
                    708:         */
                    709:        p = clean_line(line);
                    710:        if (*p == '\0')
                    711:                return;
                    712:
                    713:        if (currtable == &vartable)
                    714:                parse_varline(p);
                    715:        else
                    716:                parse_cmdline(p);
                    717: }
                    718:
1.8       nicm      719: int
                    720: main(int argc, char **argv)
1.1       etheisen  721: {
                    722:        FILE *desc;
                    723:        FILE *out;
1.5       millert   724:        char line[1024];
                    725:
1.12    ! deraadt   726:        if (pledge("stdio rpath wpath cpath", NULL) == -1)
        !           727:                err(1, "pledge");
        !           728:
1.1       etheisen  729:        /*
                    730:         * Process command line arguments.
                    731:         */
                    732:        parse_args(argc, argv);
                    733:        init_tables();
                    734:
                    735:        /*
                    736:         * Open the input file.
                    737:         */
                    738:        if (strcmp(infile, "-") == 0)
                    739:                desc = stdin;
1.8       nicm      740:        else if ((desc = fopen(infile, "r")) == NULL) {
1.1       etheisen  741:                perror(infile);
1.5       millert   742:                usage();
1.1       etheisen  743:        }
                    744:
                    745:        /*
                    746:         * Read and parse the input file, one line at a time.
                    747:         */
                    748:        errors = 0;
                    749:        linenum = 0;
1.8       nicm      750:        while (fgets(line, sizeof (line), desc) != NULL) {
1.1       etheisen  751:                ++linenum;
                    752:                parse_line(line);
                    753:        }
                    754:
                    755:        /*
                    756:         * Write the output file.
                    757:         * If no output file was specified, use "$HOME/.less"
                    758:         */
1.8       nicm      759:        if (errors > 0) {
                    760:                (void) fprintf(stderr, "%d errors; no output produced\n",
                    761:                    errors);
1.1       etheisen  762:                exit(1);
                    763:        }
                    764:
                    765:        if (outfile == NULL)
1.5       millert   766:                outfile = getenv("LESSKEY");
                    767:        if (outfile == NULL)
1.1       etheisen  768:                outfile = homefile(LESSKEYFILE);
1.8       nicm      769:        if ((out = fopen(outfile, "wb")) == NULL) {
1.1       etheisen  770:                perror(outfile);
                    771:                exit(1);
                    772:        }
                    773:
                    774:        /* File header */
1.8       nicm      775:        fputbytes(out, fileheader, sizeof (fileheader));
1.1       etheisen  776:
                    777:        /* Command key section */
1.8       nicm      778:        fputbytes(out, cmdsection, sizeof (cmdsection));
1.1       etheisen  779:        fputint(out, cmdtable.pbuffer - cmdtable.buffer);
1.8       nicm      780:        fputbytes(out, (char *)cmdtable.buffer,
                    781:            cmdtable.pbuffer-cmdtable.buffer);
                    782:
1.1       etheisen  783:        /* Edit key section */
1.8       nicm      784:        fputbytes(out, editsection, sizeof (editsection));
1.1       etheisen  785:        fputint(out, edittable.pbuffer - edittable.buffer);
1.8       nicm      786:        fputbytes(out, (char *)edittable.buffer,
                    787:            edittable.pbuffer-edittable.buffer);
1.1       etheisen  788:
1.5       millert   789:        /* Environment variable section */
1.8       nicm      790:        fputbytes(out, varsection, sizeof (varsection));
1.5       millert   791:        fputint(out, vartable.pbuffer - vartable.buffer);
1.8       nicm      792:        fputbytes(out, (char *)vartable.buffer,
                    793:            vartable.pbuffer-vartable.buffer);
1.5       millert   794:
1.1       etheisen  795:        /* File trailer */
1.8       nicm      796:        fputbytes(out, endsection, sizeof (endsection));
                    797:        fputbytes(out, filetrailer, sizeof (filetrailer));
1.5       millert   798:        return (0);
1.1       etheisen  799: }