2 * Copyright (C) 1984-2021 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information, see the README file.
18 #define CONTROL(c) ((c)&037)
19 #define ESC CONTROL('[')
21 extern void lesskey_parse_error(char *msg);
22 extern char *homefile(char *filename);
23 extern void *ecalloc(int count, unsigned int size);
27 static char *lesskey_file;
29 static struct lesskey_cmdname cmdnames[] =
31 { "back-bracket", A_B_BRACKET },
32 { "back-line", A_B_LINE },
33 { "back-line-force", A_BF_LINE },
34 { "back-screen", A_B_SCREEN },
35 { "back-scroll", A_B_SCROLL },
36 { "back-search", A_B_SEARCH },
37 { "back-window", A_B_WINDOW },
38 { "clear-mark", A_CLRMARK },
41 { "display-flag", A_DISP_OPTION },
42 { "display-option", A_DISP_OPTION },
44 { "end-scroll", A_RRSHIFT },
45 { "examine", A_EXAMINE },
46 { "filter", A_FILTER },
47 { "first-cmd", A_FIRSTCMD },
48 { "firstcmd", A_FIRSTCMD },
49 { "flush-repaint", A_FREPAINT },
50 { "forw-bracket", A_F_BRACKET },
51 { "forw-forever", A_F_FOREVER },
52 { "forw-until-hilite", A_F_UNTIL_HILITE },
53 { "forw-line", A_F_LINE },
54 { "forw-line-force", A_FF_LINE },
55 { "forw-screen", A_F_SCREEN },
56 { "forw-screen-force", A_FF_SCREEN },
57 { "forw-scroll", A_F_SCROLL },
58 { "forw-search", A_F_SEARCH },
59 { "forw-window", A_F_WINDOW },
60 { "goto-end", A_GOEND },
61 { "goto-end-buffered", A_GOEND_BUF },
62 { "goto-line", A_GOLINE },
63 { "goto-mark", A_GOMARK },
65 { "index-file", A_INDEX_FILE },
66 { "invalid", A_UINVALID },
67 { "left-scroll", A_LSHIFT },
68 { "next-file", A_NEXT_FILE },
69 { "next-tag", A_NEXT_TAG },
70 { "noaction", A_NOACTION },
71 { "no-scroll", A_LLSHIFT },
72 { "percent", A_PERCENT },
74 { "prev-file", A_PREV_FILE },
75 { "prev-tag", A_PREV_TAG },
77 { "remove-file", A_REMOVE_FILE },
78 { "repaint", A_REPAINT },
79 { "repaint-flush", A_FREPAINT },
80 { "repeat-search", A_AGAIN_SEARCH },
81 { "repeat-search-all", A_T_AGAIN_SEARCH },
82 { "reverse-search", A_REVERSE_SEARCH },
83 { "reverse-search-all", A_T_REVERSE_SEARCH },
84 { "right-scroll", A_RSHIFT },
85 { "set-mark", A_SETMARK },
86 { "set-mark-bottom", A_SETMARKBOT },
89 { "toggle-flag", A_OPT_TOGGLE },
90 { "toggle-option", A_OPT_TOGGLE },
91 { "undo-hilite", A_UNDO_SEARCH },
92 { "clear-search", A_CLR_SEARCH },
93 { "version", A_VERSION },
94 { "visual", A_VISUAL },
98 static struct lesskey_cmdname editnames[] =
100 { "back-complete", EC_B_COMPLETE },
101 { "backspace", EC_BACKSPACE },
102 { "delete", EC_DELETE },
105 { "expand", EC_EXPAND },
106 { "forw-complete", EC_F_COMPLETE },
108 { "insert", EC_INSERT },
109 { "invalid", EC_UINVALID },
110 { "kill-line", EC_LINEKILL },
111 { "abort", EC_ABORT },
113 { "literal", EC_LITERAL },
114 { "right", EC_RIGHT },
116 { "word-backspace", EC_W_BACKSPACE },
117 { "word-delete", EC_W_DELETE },
118 { "word-left", EC_W_LEFT },
119 { "word-right", EC_W_RIGHT },
124 * Print a parse error message.
133 snprintf(buf, sizeof(buf), "%s: line %d: %s%s", lesskey_file, linenum, s1, s2);
134 lesskey_parse_error(buf);
138 * Initialize lesskey_tables.
142 struct lesskey_tables *tables;
144 tables->currtable = &tables->cmdtable;
146 tables->cmdtable.names = cmdnames;
147 tables->cmdtable.is_var = 0;
148 xbuf_init(&tables->cmdtable.buf);
150 tables->edittable.names = editnames;
151 tables->edittable.is_var = 0;
152 xbuf_init(&tables->edittable.buf);
154 tables->vartable.names = NULL;
155 tables->vartable.is_var = 1;
156 xbuf_init(&tables->vartable.buf);
160 * Parse one character of a string.
171 static char tstr_control_k[] =
172 { SK_SPECIAL_KEY, SK_CONTROL_K, 6, 1, 1, 1, '\0' };
181 case '0': case '1': case '2': case '3':
182 case '4': case '5': case '6': case '7':
184 * Parse an octal number.
189 ch = 8*ch + (*p - '0');
190 while (*++p >= '0' && *p <= '7' && ++i < 3);
192 if (xlate && ch == CONTROL('K'))
193 return tstr_control_k;
219 case 'u': ch = SK_UP_ARROW; break;
220 case 'd': ch = SK_DOWN_ARROW; break;
221 case 'r': ch = SK_RIGHT_ARROW; break;
222 case 'l': ch = SK_LEFT_ARROW; break;
223 case 'U': ch = SK_PAGE_UP; break;
224 case 'D': ch = SK_PAGE_DOWN; break;
225 case 'h': ch = SK_HOME; break;
226 case 'e': ch = SK_END; break;
227 case 'x': ch = SK_DELETE; break;
228 default: { char buf[2]; buf[0] = *p; buf[1] = '\0';
229 parse_error("illegal escape sequence \\k", buf);
234 buf[0] = SK_SPECIAL_KEY;
246 * Backslash followed by any other char
247 * just means that char.
252 if (xlate && buf[0] == CONTROL('K'))
253 return tstr_control_k;
258 * Caret means CONTROL.
261 buf[0] = CONTROL(p[1]);
263 if (xlate && buf[0] == CONTROL('K'))
264 return tstr_control_k;
270 if (xlate && buf[0] == CONTROL('K'))
271 return tstr_control_k;
279 return (ch == ' ' || ch == '\t');
283 * Skip leading spaces in a string.
295 * Skip non-space characters in a string.
301 while (*s != '\0' && !issp(*s))
307 * Clean up an input line:
308 * strip off the trailing newline & any trailing # comment.
317 for (i = 0; s[i] != '\0' && s[i] != '\n' && s[i] != '\r'; i++)
318 if (s[i] == '#' && (i == 0 || s[i-1] != '\\'))
325 * Add a byte to the output command table.
328 add_cmd_char(c, tables)
330 struct lesskey_tables *tables;
332 xbuf_add(&tables->currtable->buf, c);
336 * Add a string to the output command table.
339 add_cmd_str(s, tables)
341 struct lesskey_tables *tables;
343 for ( ; *s != '\0'; s++)
344 add_cmd_char(*s, tables);
348 * See if we have a special "control" line.
351 control_line(s, tables)
353 struct lesskey_tables *tables;
355 #define PREFIX(str,pat) (strncmp(str,pat,strlen(pat)) == 0)
357 if (PREFIX(s, "#line-edit"))
359 tables->currtable = &tables->edittable;
362 if (PREFIX(s, "#command"))
364 tables->currtable = &tables->cmdtable;
367 if (PREFIX(s, "#env"))
369 tables->currtable = &tables->vartable;
372 if (PREFIX(s, "#stop"))
374 add_cmd_char('\0', tables);
375 add_cmd_char(A_END_LIST, tables);
382 * Find an action, given the name of the action.
385 findaction(actname, tables)
387 struct lesskey_tables *tables;
391 for (i = 0; tables->currtable->names[i].cn_name != NULL; i++)
392 if (strcmp(tables->currtable->names[i].cn_name, actname) == 0)
393 return (tables->currtable->names[i].cn_action);
394 parse_error("unknown action: ", actname);
399 * Parse a line describing one key binding, of the form
401 * where KEY is the user key sequence, ACTION is the
402 * resulting less action, and EXTRA is an "extra" user
403 * key sequence injected after the action.
406 parse_cmdline(p, tables)
408 struct lesskey_tables *tables;
416 * Parse the command string and store it in the current table.
421 add_cmd_str(s, tables);
422 } while (*p != '\0' && !issp(*p));
424 * Terminate the command string with a null byte.
426 add_cmd_char('\0', tables);
429 * Skip white space between the command string
430 * and the action name.
431 * Terminate the action name with a null byte.
436 parse_error("missing action", "");
445 * Parse the action name and store it in the current table.
447 action = findaction(actname, tables);
450 * See if an extra string follows the action name.
456 add_cmd_char(action, tables);
460 * OR the special value A_EXTRA into the action byte.
461 * Put the extra string after the action byte.
463 add_cmd_char(action | A_EXTRA, tables);
465 add_cmd_str(tstr(&p, 0), tables);
466 add_cmd_char('\0', tables);
471 * Parse a variable definition line, of the form
475 parse_varline(line, tables)
477 struct lesskey_tables *tables;
485 add_cmd_str(s, tables);
486 } while (*p != '\0' && !issp(*p) && *p != '=');
488 * Terminate the variable name with a null byte.
490 add_cmd_char('\0', tables);
495 parse_error("missing = in: ", line);
499 add_cmd_char(EV_OK|A_EXTRA, tables);
505 add_cmd_str(s, tables);
507 add_cmd_char('\0', tables);
511 * Parse a line from the lesskey file.
514 parse_line(line, tables)
516 struct lesskey_tables *tables;
521 * See if it is a control line.
523 if (control_line(line, tables))
526 * Skip leading white space.
527 * Replace the final newline with a null byte.
528 * Ignore blank lines and comments.
530 p = clean_line(line);
534 if (tables->currtable->is_var)
535 parse_varline(p, tables);
537 parse_cmdline(p, tables);
541 * Parse a lesskey source file and store result in tables.
544 parse_lesskey(infile, tables)
546 struct lesskey_tables *tables;
552 infile = homefile(DEF_LESSKEYINFILE);
553 lesskey_file = infile;
560 * Open the input file.
562 if (strcmp(infile, "-") == 0)
564 else if ((desc = fopen(infile, "r")) == NULL)
566 /* parse_error("cannot open lesskey file ", infile); */
571 * Read and parse the input file, one line at a time.
573 while (fgets(line, sizeof(line), desc) != NULL)
576 parse_line(line, tables);