Annotation of src/usr.bin/less/decode.c, Revision 1.11
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_FIRSTCMD,
154:
155: 'H', 0, A_HELP,
156: 'h', 0, A_HELP,
157: SK(SK_F1), 0, A_HELP,
158: 'V', 0, A_VERSION,
159: 'q', 0, A_QUIT,
160: 'Q', 0, A_QUIT,
161: ':', 'q', 0, A_QUIT,
162: ':', 'Q', 0, A_QUIT,
163: 'Z', 'Z', 0, A_QUIT
164: };
165:
166: static unsigned char lesstable[] = {
167: '-', 0, A_OPT_TOGGLE,
168: 's', 0, A_OPT_TOGGLE|A_EXTRA, 'o', 0,
169: '_', 0, A_DISP_OPTION
170: };
171:
172: static unsigned char moretable[] = {
173: 's', 0, A_F_SKIP
1.1 etheisen 174: };
175:
176: static unsigned char edittable[] =
177: {
1.9 deraadt 178: '\t', 0, EC_F_COMPLETE, /* TAB */
1.8 nicm 179: '\17', 0, EC_B_COMPLETE, /* BACKTAB */
180: SK(SK_BACKTAB), 0, EC_B_COMPLETE, /* BACKTAB */
181: ESC, '\t', 0, EC_B_COMPLETE, /* ESC TAB */
182: CONTROL('L'), 0, EC_EXPAND, /* CTRL-L */
183: CONTROL('V'), 0, EC_LITERAL, /* BACKSLASH */
184: CONTROL('A'), 0, EC_LITERAL, /* BACKSLASH */
185: ESC, 'l', 0, EC_RIGHT, /* ESC l */
186: SK(SK_RIGHT_ARROW), 0, EC_RIGHT, /* RIGHTARROW */
187: ESC, 'h', 0, EC_LEFT, /* ESC h */
188: SK(SK_LEFT_ARROW), 0, EC_LEFT, /* LEFTARROW */
189: ESC, 'b', 0, EC_W_LEFT, /* ESC b */
190: ESC, SK(SK_LEFT_ARROW), 0, EC_W_LEFT, /* ESC LEFTARROW */
191: SK(SK_CTL_LEFT_ARROW), 0, EC_W_LEFT, /* CTRL-LEFTARROW */
192: ESC, 'w', 0, EC_W_RIGHT, /* ESC w */
193: ESC, SK(SK_RIGHT_ARROW), 0, EC_W_RIGHT, /* ESC RIGHTARROW */
194: SK(SK_CTL_RIGHT_ARROW), 0, EC_W_RIGHT, /* CTRL-RIGHTARROW */
195: ESC, 'i', 0, EC_INSERT, /* ESC i */
196: SK(SK_INSERT), 0, EC_INSERT, /* INSERT */
197: ESC, 'x', 0, EC_DELETE, /* ESC x */
198: SK(SK_DELETE), 0, EC_DELETE, /* DELETE */
199: ESC, 'X', 0, EC_W_DELETE, /* ESC X */
200: ESC, SK(SK_DELETE), 0, EC_W_DELETE, /* ESC DELETE */
201: SK(SK_CTL_DELETE), 0, EC_W_DELETE, /* CTRL-DELETE */
202: SK(SK_CTL_BACKSPACE), 0, EC_W_BACKSPACE, /* CTRL-BACKSPACE */
203: ESC, '\b', 0, EC_W_BACKSPACE, /* ESC BACKSPACE */
204: ESC, '0', 0, EC_HOME, /* ESC 0 */
205: SK(SK_HOME), 0, EC_HOME, /* HOME */
206: ESC, '$', 0, EC_END, /* ESC $ */
207: SK(SK_END), 0, EC_END, /* END */
208: ESC, 'k', 0, EC_UP, /* ESC k */
209: SK(SK_UP_ARROW), 0, EC_UP, /* UPARROW */
210: ESC, 'j', 0, EC_DOWN, /* ESC j */
211: SK(SK_DOWN_ARROW), 0, EC_DOWN, /* DOWNARROW */
212: CONTROL('G'), 0, EC_ABORT, /* CTRL-G */
1.1 etheisen 213: };
214:
215: /*
216: * Structure to support a list of command tables.
217: */
218: struct tablelist
219: {
220: struct tablelist *t_next;
221: char *t_start;
222: char *t_end;
223: };
224:
225: /*
226: * List of command tables and list of line-edit tables.
227: */
228: static struct tablelist *list_fcmd_tables = NULL;
229: static struct tablelist *list_ecmd_tables = NULL;
1.4 millert 230: static struct tablelist *list_var_tables = NULL;
231: static struct tablelist *list_sysvar_tables = NULL;
1.1 etheisen 232:
233:
234: /*
1.4 millert 235: * Expand special key abbreviations in a command table.
236: */
1.8 nicm 237: static void
238: expand_special_keys(char *table, int len)
239: {
240: char *fm;
241: char *to;
242: int a;
1.4 millert 243: char *repl;
244: int klen;
245:
1.8 nicm 246: for (fm = table; fm < table + len; ) {
1.4 millert 247: /*
248: * Rewrite each command in the table with any
249: * special key abbreviations expanded.
250: */
1.8 nicm 251: for (to = fm; *fm != '\0'; ) {
252: if (*fm != SK_SPECIAL_KEY) {
1.4 millert 253: *to++ = *fm++;
254: continue;
255: }
256: /*
257: * After SK_SPECIAL_KEY, next byte is the type
258: * of special key (one of the SK_* contants),
259: * and the byte after that is the number of bytes,
260: * N, reserved by the abbreviation (including the
261: * SK_SPECIAL_KEY and key type bytes).
262: * Replace all N bytes with the actual bytes
263: * output by the special key on this terminal.
264: */
265: repl = special_key_str(fm[1]);
266: klen = fm[2] & 0377;
267: fm += klen;
1.8 nicm 268: if (repl == NULL || strlen(repl) > klen)
1.4 millert 269: repl = "\377";
270: while (*repl != '\0')
271: *to++ = *repl++;
272: }
273: *to++ = '\0';
274: /*
1.8 nicm 275: * Fill any unused bytes between end of command and
1.4 millert 276: * the action byte with A_SKIP.
277: */
278: while (to <= fm)
279: *to++ = A_SKIP;
280: fm++;
281: a = *fm++ & 0377;
1.8 nicm 282: if (a & A_EXTRA) {
1.4 millert 283: while (*fm++ != '\0')
284: continue;
285: }
286: }
287: }
288:
289: /*
1.1 etheisen 290: * Initialize the command lists.
291: */
1.8 nicm 292: void
293: init_cmds(void)
1.1 etheisen 294: {
295: /*
296: * Add the default command tables.
297: */
1.8 nicm 298: add_fcmd_table((char *)cmdtable, sizeof (cmdtable));
299: add_ecmd_table((char *)edittable, sizeof (edittable));
300: if (less_is_more) {
301: add_fcmd_table((char *)moretable, sizeof (moretable));
302: return;
303: } else {
304: add_fcmd_table((char *)lesstable, sizeof (lesstable));
305: }
306:
1.4 millert 307: /*
308: * Try to add the tables in the system lesskey file.
309: */
310: add_hometable("LESSKEY_SYSTEM", LESSKEYFILE_SYS, 1);
311: /*
1.1 etheisen 312: * Try to add the tables in the standard lesskey file "$HOME/.less".
313: */
1.4 millert 314: add_hometable("LESSKEY", LESSKEYFILE, 0);
1.1 etheisen 315: }
316:
317: /*
1.4 millert 318: * Add a command table.
1.1 etheisen 319: */
1.8 nicm 320: static int
321: add_cmd_table(struct tablelist **tlist, char *buf, int len)
1.1 etheisen 322: {
1.8 nicm 323: struct tablelist *t;
1.1 etheisen 324:
325: if (len == 0)
326: return (0);
327: /*
1.8 nicm 328: * Allocate a tablelist structure, initialize it,
1.1 etheisen 329: * and link it into the list of tables.
330: */
1.8 nicm 331: if ((t = calloc(1, sizeof (struct tablelist))) == NULL) {
1.1 etheisen 332: return (-1);
333: }
1.4 millert 334: expand_special_keys(buf, len);
1.1 etheisen 335: t->t_start = buf;
336: t->t_end = buf + len;
337: t->t_next = *tlist;
338: *tlist = t;
339: return (0);
340: }
341:
342: /*
343: * Add a command table.
344: */
1.8 nicm 345: void
346: add_fcmd_table(char *buf, int len)
1.1 etheisen 347: {
348: if (add_cmd_table(&list_fcmd_tables, buf, len) < 0)
349: error("Warning: some commands disabled", NULL_PARG);
350: }
351:
352: /*
353: * Add an editing command table.
354: */
1.8 nicm 355: void
356: add_ecmd_table(char *buf, int len)
1.1 etheisen 357: {
358: if (add_cmd_table(&list_ecmd_tables, buf, len) < 0)
359: error("Warning: some edit commands disabled", NULL_PARG);
360: }
361:
362: /*
1.4 millert 363: * Add an environment variable table.
364: */
1.8 nicm 365: static void
366: add_var_table(struct tablelist **tlist, char *buf, int len)
1.4 millert 367: {
368: if (add_cmd_table(tlist, buf, len) < 0)
1.8 nicm 369: error("Warning: environment variables from "
370: "lesskey file unavailable", NULL_PARG);
1.4 millert 371: }
372:
373: /*
1.1 etheisen 374: * Search a single command table for the command string in cmd.
375: */
1.8 nicm 376: static int
377: cmd_search(const char *cmd, char *table, char *endtable, char **sp)
378: {
379: char *p;
380: const char *q;
381: int a;
1.1 etheisen 382:
1.4 millert 383: *sp = NULL;
1.8 nicm 384: for (p = table, q = cmd; p < endtable; p++, q++) {
385: if (*p == *q) {
1.1 etheisen 386: /*
387: * Current characters match.
388: * If we're at the end of the string, we've found it.
389: * Return the action code, which is the character
390: * after the null at the end of the string
391: * in the command table.
392: */
1.8 nicm 393: if (*p == '\0') {
1.1 etheisen 394: a = *++p & 0377;
1.4 millert 395: while (a == A_SKIP)
396: a = *++p & 0377;
1.8 nicm 397: if (a == A_END_LIST) {
1.1 etheisen 398: /*
399: * We get here only if the original
400: * cmd string passed in was empty ("").
401: * I don't think that can happen,
402: * but just in case ...
403: */
404: return (A_UINVALID);
405: }
406: /*
407: * Check for an "extra" string.
408: */
1.8 nicm 409: if (a & A_EXTRA) {
1.1 etheisen 410: *sp = ++p;
411: a &= ~A_EXTRA;
1.4 millert 412: }
1.1 etheisen 413: return (a);
414: }
1.8 nicm 415: } else if (*q == '\0') {
1.1 etheisen 416: /*
417: * Hit the end of the user's command,
418: * but not the end of the string in the command table.
419: * The user's command is incomplete.
420: */
421: return (A_PREFIX);
1.8 nicm 422: } else {
1.1 etheisen 423: /*
424: * Not a match.
425: * Skip ahead to the next command in the
426: * command table, and reset the pointer
427: * to the beginning of the user's command.
428: */
1.8 nicm 429: if (*p == '\0' && p[1] == A_END_LIST) {
1.1 etheisen 430: /*
1.8 nicm 431: * A_END_LIST is a special marker that tells
1.1 etheisen 432: * us to abort the cmd search.
433: */
434: return (A_UINVALID);
435: }
1.4 millert 436: while (*p++ != '\0')
437: continue;
438: while (*p == A_SKIP)
439: p++;
1.1 etheisen 440: if (*p & A_EXTRA)
1.4 millert 441: while (*++p != '\0')
442: continue;
1.1 etheisen 443: q = cmd-1;
444: }
445: }
446: /*
447: * No match found in the entire command table.
448: */
449: return (A_INVALID);
450: }
451:
452: /*
453: * Decode a command character and return the associated action.
454: * The "extra" string, if any, is returned in sp.
455: */
1.8 nicm 456: static int
457: cmd_decode(struct tablelist *tlist, const char *cmd, char **sp)
1.1 etheisen 458: {
1.8 nicm 459: struct tablelist *t;
460: int action = A_INVALID;
1.1 etheisen 461:
462: /*
463: * Search thru all the command tables.
464: * Stop when we find an action which is not A_INVALID.
465: */
1.8 nicm 466: for (t = tlist; t != NULL; t = t->t_next) {
1.1 etheisen 467: action = cmd_search(cmd, t->t_start, t->t_end, sp);
468: if (action != A_INVALID)
469: break;
470: }
1.4 millert 471: if (action == A_UINVALID)
472: action = A_INVALID;
1.1 etheisen 473: return (action);
474: }
475:
476: /*
477: * Decode a command from the cmdtables list.
478: */
1.8 nicm 479: int
480: fcmd_decode(const char *cmd, char **sp)
1.1 etheisen 481: {
482: return (cmd_decode(list_fcmd_tables, cmd, sp));
483: }
484:
485: /*
486: * Decode a command from the edittables list.
487: */
1.8 nicm 488: int
489: ecmd_decode(const char *cmd, char **sp)
1.1 etheisen 490: {
491: return (cmd_decode(list_ecmd_tables, cmd, sp));
492: }
493:
1.4 millert 494: /*
495: * Get the value of an environment variable.
496: * Looks first in the lesskey file, then in the real environment.
497: */
1.8 nicm 498: char *
499: lgetenv(char *var)
1.4 millert 500: {
501: int a;
502: char *s;
503:
1.8 nicm 504: /*
505: * Ignore lookups of any LESS* setting when we are more, and ignore
506: * the less key files
507: */
508: if (less_is_more) {
509: if (strncmp(var, "LESS", 4) == 0) {
510: return (NULL);
511: }
512: return (getenv(var));
513: }
1.4 millert 514: a = cmd_decode(list_var_tables, var, &s);
515: if (a == EV_OK)
516: return (s);
517: s = getenv(var);
518: if (s != NULL && *s != '\0')
519: return (s);
520: a = cmd_decode(list_sysvar_tables, var, &s);
521: if (a == EV_OK)
522: return (s);
523: return (NULL);
524: }
525:
526: /*
527: * Get an "integer" from a lesskey file.
1.8 nicm 528: * Integers are stored in a funny format:
1.4 millert 529: * two bytes, low order first, in radix KRADIX.
530: */
1.8 nicm 531: static int
532: gint(char **sp)
1.1 etheisen 533: {
534: int n;
535:
536: n = *(*sp)++;
537: n += *(*sp)++ * KRADIX;
538: return (n);
539: }
540:
1.4 millert 541: /*
542: * Process an old (pre-v241) lesskey file.
543: */
1.8 nicm 544: static int
545: old_lesskey(char *buf, int len)
1.1 etheisen 546: {
547: /*
548: * Old-style lesskey file.
1.8 nicm 549: * The file must end with either
1.9 deraadt 550: * ..,cmd,0,action
1.8 nicm 551: * or ...,cmd,0,action|A_EXTRA,string,0
1.1 etheisen 552: * So the last byte or the second to last byte must be zero.
553: */
554: if (buf[len-1] != '\0' && buf[len-2] != '\0')
555: return (-1);
556: add_fcmd_table(buf, len);
557: return (0);
558: }
559:
1.8 nicm 560: /*
1.4 millert 561: * Process a new (post-v241) lesskey file.
562: */
1.8 nicm 563: static int
564: new_lesskey(char *buf, int len, int sysvar)
1.1 etheisen 565: {
566: char *p;
1.8 nicm 567: int c;
568: int n;
1.1 etheisen 569:
570: /*
571: * New-style lesskey file.
572: * Extract the pieces.
573: */
574: if (buf[len-3] != C0_END_LESSKEY_MAGIC ||
575: buf[len-2] != C1_END_LESSKEY_MAGIC ||
576: buf[len-1] != C2_END_LESSKEY_MAGIC)
577: return (-1);
578: p = buf + 4;
1.8 nicm 579: for (;;) {
1.1 etheisen 580: c = *p++;
1.8 nicm 581: switch (c) {
1.1 etheisen 582: case CMD_SECTION:
583: n = gint(&p);
584: add_fcmd_table(p, n);
585: p += n;
586: break;
587: case EDIT_SECTION:
588: n = gint(&p);
589: add_ecmd_table(p, n);
590: p += n;
591: break;
1.4 millert 592: case VAR_SECTION:
593: n = gint(&p);
1.8 nicm 594: add_var_table((sysvar) ?
595: &list_sysvar_tables : &list_var_tables, p, n);
1.4 millert 596: p += n;
597: break;
1.1 etheisen 598: case END_SECTION:
1.4 millert 599: return (0);
1.1 etheisen 600: default:
1.4 millert 601: /*
602: * Unrecognized section type.
603: */
1.1 etheisen 604: return (-1);
605: }
606: }
607: }
608:
609: /*
610: * Set up a user command table, based on a "lesskey" file.
611: */
1.8 nicm 612: int
613: lesskey(char *filename, int sysvar)
1.1 etheisen 614: {
1.8 nicm 615: char *buf;
616: off_t len;
617: long n;
618: int f;
1.1 etheisen 619:
1.4 millert 620: if (secure)
621: return (1);
1.1 etheisen 622: /*
623: * Try to open the lesskey file.
624: */
1.4 millert 625: filename = shell_unquote(filename);
1.1 etheisen 626: f = open(filename, OPEN_READ);
1.4 millert 627: free(filename);
1.1 etheisen 628: if (f < 0)
629: return (1);
630:
631: /*
632: * Read the file into a buffer.
633: * We first figure out the size of the file and allocate space for it.
634: * {{ Minimal error checking is done here.
635: * A garbage .less file will produce strange results.
636: * To avoid a large amount of error checking code here, we
637: * rely on the lesskey program to generate a good .less file. }}
638: */
639: len = filesize(f);
1.8 nicm 640: if (len == -1 || len < 3) {
1.1 etheisen 641: /*
642: * Bad file (valid file must have at least 3 chars).
643: */
1.8 nicm 644: (void) close(f);
1.1 etheisen 645: return (-1);
646: }
1.8 nicm 647: if ((buf = calloc((int)len, sizeof (char))) == NULL) {
648: (void) close(f);
1.1 etheisen 649: return (-1);
650: }
1.8 nicm 651: if (lseek(f, (off_t)0, SEEK_SET) == BAD_LSEEK) {
1.1 etheisen 652: free(buf);
1.8 nicm 653: (void) close(f);
1.1 etheisen 654: return (-1);
655: }
656: n = read(f, buf, (unsigned int) len);
657: close(f);
1.8 nicm 658: if (n != len) {
1.1 etheisen 659: free(buf);
660: return (-1);
661: }
662:
663: /*
664: * Figure out if this is an old-style (before version 241)
665: * or new-style lesskey file format.
666: */
667: if (buf[0] != C0_LESSKEY_MAGIC || buf[1] != C1_LESSKEY_MAGIC ||
668: buf[2] != C2_LESSKEY_MAGIC || buf[3] != C3_LESSKEY_MAGIC)
669: return (old_lesskey(buf, (int)len));
1.4 millert 670: return (new_lesskey(buf, (int)len, sysvar));
1.1 etheisen 671: }
672:
673: /*
674: * Add the standard lesskey file "$HOME/.less"
675: */
1.8 nicm 676: void
677: add_hometable(char *envname, char *def_filename, int sysvar)
1.1 etheisen 678: {
679: char *filename;
680: PARG parg;
681:
1.4 millert 682: if (envname != NULL && (filename = lgetenv(envname)) != NULL)
683: filename = save(filename);
684: else if (sysvar)
685: filename = save(def_filename);
686: else
687: filename = homefile(def_filename);
1.1 etheisen 688: if (filename == NULL)
689: return;
1.8 nicm 690: if (lesskey(filename, sysvar) < 0) {
1.1 etheisen 691: parg.p_string = filename;
692: error("Cannot use lesskey file \"%s\"", &parg);
693: }
694: free(filename);
695: }
696:
697: /*
698: * See if a char is a special line-editing command.
699: */
1.8 nicm 700: int
701: editchar(int c, int flags)
1.1 etheisen 702: {
703: int action;
704: int nch;
705: char *s;
706: char usercmd[MAX_CMDLEN+1];
1.8 nicm 707:
1.1 etheisen 708: /*
709: * An editing character could actually be a sequence of characters;
710: * for example, an escape sequence sent by pressing the uparrow key.
711: * To match the editing string, we use the command decoder
712: * but give it the edit-commands command table
713: * This table is constructed to match the user's keyboard.
714: */
1.6 shadchin 715: if (c == erase_char || c == erase2_char)
1.1 etheisen 716: return (EC_BACKSPACE);
717: if (c == kill_char)
718: return (EC_LINEKILL);
1.8 nicm 719:
1.1 etheisen 720: /*
721: * Collect characters in a buffer.
722: * Start with the one we have, and get more if we need them.
723: */
724: nch = 0;
725: do {
1.8 nicm 726: if (nch > 0)
1.1 etheisen 727: c = getcc();
1.8 nicm 728: usercmd[nch] = (char)c;
1.1 etheisen 729: usercmd[nch+1] = '\0';
730: nch++;
731: action = ecmd_decode(usercmd, &s);
732: } while (action == A_PREFIX);
1.8 nicm 733:
734: if (flags & EC_NORIGHTLEFT) {
735: switch (action) {
1.4 millert 736: case EC_RIGHT:
737: case EC_LEFT:
738: action = A_INVALID;
739: break;
740: }
741: }
1.8 nicm 742: if (flags & EC_NOHISTORY) {
1.1 etheisen 743: /*
744: * The caller says there is no history list.
745: * Reject any history-manipulation action.
746: */
1.8 nicm 747: switch (action) {
1.1 etheisen 748: case EC_UP:
749: case EC_DOWN:
750: action = A_INVALID;
751: break;
752: }
753: }
1.8 nicm 754: if (flags & EC_NOCOMPLETE) {
1.1 etheisen 755: /*
756: * The caller says we don't want any filename completion cmds.
757: * Reject them.
758: */
1.8 nicm 759: switch (action) {
1.1 etheisen 760: case EC_F_COMPLETE:
761: case EC_B_COMPLETE:
762: case EC_EXPAND:
763: action = A_INVALID;
764: break;
765: }
766: }
1.8 nicm 767: if ((flags & EC_PEEK) || action == A_INVALID) {
1.1 etheisen 768: /*
769: * We're just peeking, or we didn't understand the command.
770: * Unget all the characters we read in the loop above.
1.8 nicm 771: * This does NOT include the original character that was
1.1 etheisen 772: * passed in as a parameter.
773: */
1.8 nicm 774: while (nch > 1) {
1.1 etheisen 775: ungetcc(usercmd[--nch]);
776: }
1.8 nicm 777: } else {
1.1 etheisen 778: if (s != NULL)
779: ungetsc(s);
780: }
1.8 nicm 781: return (action);
1.1 etheisen 782: }