xref: /freebsd/contrib/less/lesskey_parse.c (revision 66df505066f51e6d8411b966765d828817f88971)
1 /*
2  * Copyright (C) 1984-2021  Mark Nudelman
3  *
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.
6  *
7  * For more information, see the README file.
8  */
9 
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include "lesskey.h"
14 #include "cmd.h"
15 #include "xbuf.h"
16 #include "defines.h"
17 
18 #define CONTROL(c)      ((c)&037)
19 #define ESC             CONTROL('[')
20 
21 extern void lesskey_parse_error(char *msg);
22 extern char *homefile(char *filename);
23 extern void *ecalloc(int count, unsigned int size);
24 
25 static int linenum;
26 static int errors;
27 static char *lesskey_file;
28 
29 static struct lesskey_cmdname cmdnames[] =
30 {
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 },
39 	{ "debug",                A_DEBUG },
40 	{ "digit",                A_DIGIT },
41 	{ "display-flag",         A_DISP_OPTION },
42 	{ "display-option",       A_DISP_OPTION },
43 	{ "end",                  A_GOEND },
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 },
64 	{ "help",                 A_HELP },
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 },
73 	{ "pipe",                 A_PIPE },
74 	{ "prev-file",            A_PREV_FILE },
75 	{ "prev-tag",             A_PREV_TAG },
76 	{ "quit",                 A_QUIT },
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 },
87 	{ "shell",                A_SHELL },
88 	{ "status",               A_STAT },
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 },
95 	{ NULL,   0 }
96 };
97 
98 static struct lesskey_cmdname editnames[] =
99 {
100 	{ "back-complete",      EC_B_COMPLETE },
101 	{ "backspace",          EC_BACKSPACE },
102 	{ "delete",             EC_DELETE },
103 	{ "down",               EC_DOWN },
104 	{ "end",                EC_END },
105 	{ "expand",             EC_EXPAND },
106 	{ "forw-complete",      EC_F_COMPLETE },
107 	{ "home",               EC_HOME },
108 	{ "insert",             EC_INSERT },
109 	{ "invalid",            EC_UINVALID },
110 	{ "kill-line",          EC_LINEKILL },
111 	{ "abort",              EC_ABORT },
112 	{ "left",               EC_LEFT },
113 	{ "literal",            EC_LITERAL },
114 	{ "right",              EC_RIGHT },
115 	{ "up",                 EC_UP },
116 	{ "word-backspace",     EC_W_BACKSPACE },
117 	{ "word-delete",        EC_W_DELETE },
118 	{ "word-left",          EC_W_LEFT },
119 	{ "word-right",         EC_W_RIGHT },
120 	{ NULL, 0 }
121 };
122 
123 /*
124  * Print a parse error message.
125  */
126 	static void
127 parse_error(s1, s2)
128 	char *s1;
129 	char *s2;
130 {
131 	char buf[1024];
132 	++errors;
133 	snprintf(buf, sizeof(buf), "%s: line %d: %s%s", lesskey_file, linenum, s1, s2);
134 	lesskey_parse_error(buf);
135 }
136 
137 /*
138  * Initialize lesskey_tables.
139  */
140 	static void
141 init_tables(tables)
142 	struct lesskey_tables *tables;
143 {
144 	tables->currtable = &tables->cmdtable;
145 
146 	tables->cmdtable.names = cmdnames;
147 	tables->cmdtable.is_var = 0;
148 	xbuf_init(&tables->cmdtable.buf);
149 
150 	tables->edittable.names = editnames;
151 	tables->edittable.is_var = 0;
152 	xbuf_init(&tables->edittable.buf);
153 
154 	tables->vartable.names = NULL;
155 	tables->vartable.is_var = 1;
156 	xbuf_init(&tables->vartable.buf);
157 }
158 
159 /*
160  * Parse one character of a string.
161  */
162 	static char *
163 tstr(pp, xlate)
164 	char **pp;
165 	int xlate;
166 {
167 	char *p;
168 	char ch;
169 	int i;
170 	static char buf[10];
171 	static char tstr_control_k[] =
172 		{ SK_SPECIAL_KEY, SK_CONTROL_K, 6, 1, 1, 1, '\0' };
173 
174 	p = *pp;
175 	switch (*p)
176 	{
177 	case '\\':
178 		++p;
179 		switch (*p)
180 		{
181 		case '0': case '1': case '2': case '3':
182 		case '4': case '5': case '6': case '7':
183 			/*
184 			 * Parse an octal number.
185 			 */
186 			ch = 0;
187 			i = 0;
188 			do
189 				ch = 8*ch + (*p - '0');
190 			while (*++p >= '0' && *p <= '7' && ++i < 3);
191 			*pp = p;
192 			if (xlate && ch == CONTROL('K'))
193 				return tstr_control_k;
194 			buf[0] = ch;
195 			buf[1] = '\0';
196 			return (buf);
197 		case 'b':
198 			*pp = p+1;
199 			return ("\b");
200 		case 'e':
201 			*pp = p+1;
202 			buf[0] = ESC;
203 			buf[1] = '\0';
204 			return (buf);
205 		case 'n':
206 			*pp = p+1;
207 			return ("\n");
208 		case 'r':
209 			*pp = p+1;
210 			return ("\r");
211 		case 't':
212 			*pp = p+1;
213 			return ("\t");
214 		case 'k':
215 			if (xlate)
216 			{
217 				switch (*++p)
218 				{
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);
230 					*pp = p+1;
231 					return (""); }
232 				}
233 				*pp = p+1;
234 				buf[0] = SK_SPECIAL_KEY;
235 				buf[1] = ch;
236 				buf[2] = 6;
237 				buf[3] = 1;
238 				buf[4] = 1;
239 				buf[5] = 1;
240 				buf[6] = '\0';
241 				return (buf);
242 			}
243 			/* FALLTHRU */
244 		default:
245 			/*
246 			 * Backslash followed by any other char
247 			 * just means that char.
248 			 */
249 			*pp = p+1;
250 			buf[0] = *p;
251 			buf[1] = '\0';
252 			if (xlate && buf[0] == CONTROL('K'))
253 				return tstr_control_k;
254 			return (buf);
255 		}
256 	case '^':
257 		/*
258 		 * Caret means CONTROL.
259 		 */
260 		*pp = p+2;
261 		buf[0] = CONTROL(p[1]);
262 		buf[1] = '\0';
263 		if (xlate && buf[0] == CONTROL('K'))
264 			return tstr_control_k;
265 		return (buf);
266 	}
267 	*pp = p+1;
268 	buf[0] = *p;
269 	buf[1] = '\0';
270 	if (xlate && buf[0] == CONTROL('K'))
271 		return tstr_control_k;
272 	return (buf);
273 }
274 
275 	static int
276 issp(ch)
277 	char ch;
278 {
279 	return (ch == ' ' || ch == '\t');
280 }
281 
282 /*
283  * Skip leading spaces in a string.
284  */
285 	static char *
286 skipsp(s)
287 	char *s;
288 {
289 	while (issp(*s))
290 		s++;
291 	return (s);
292 }
293 
294 /*
295  * Skip non-space characters in a string.
296  */
297 	static char *
298 skipnsp(s)
299 	char *s;
300 {
301 	while (*s != '\0' && !issp(*s))
302 		s++;
303 	return (s);
304 }
305 
306 /*
307  * Clean up an input line:
308  * strip off the trailing newline & any trailing # comment.
309  */
310 	static char *
311 clean_line(s)
312 	char *s;
313 {
314 	int i;
315 
316 	s = skipsp(s);
317 	for (i = 0;  s[i] != '\0' && s[i] != '\n' && s[i] != '\r';  i++)
318 		if (s[i] == '#' && (i == 0 || s[i-1] != '\\'))
319 			break;
320 	s[i] = '\0';
321 	return (s);
322 }
323 
324 /*
325  * Add a byte to the output command table.
326  */
327 	static void
328 add_cmd_char(c, tables)
329 	int c;
330 	struct lesskey_tables *tables;
331 {
332 	xbuf_add(&tables->currtable->buf, c);
333 }
334 
335 /*
336  * Add a string to the output command table.
337  */
338 	static void
339 add_cmd_str(s, tables)
340 	char *s;
341 	struct lesskey_tables *tables;
342 {
343 	for ( ;  *s != '\0';  s++)
344 		add_cmd_char(*s, tables);
345 }
346 
347 /*
348  * See if we have a special "control" line.
349  */
350 	static int
351 control_line(s, tables)
352 	char *s;
353 	struct lesskey_tables *tables;
354 {
355 #define PREFIX(str,pat) (strncmp(str,pat,strlen(pat)) == 0)
356 
357 	if (PREFIX(s, "#line-edit"))
358 	{
359 		tables->currtable = &tables->edittable;
360 		return (1);
361 	}
362 	if (PREFIX(s, "#command"))
363 	{
364 		tables->currtable = &tables->cmdtable;
365 		return (1);
366 	}
367 	if (PREFIX(s, "#env"))
368 	{
369 		tables->currtable = &tables->vartable;
370 		return (1);
371 	}
372 	if (PREFIX(s, "#stop"))
373 	{
374 		add_cmd_char('\0', tables);
375 		add_cmd_char(A_END_LIST, tables);
376 		return (1);
377 	}
378 	return (0);
379 }
380 
381 /*
382  * Find an action, given the name of the action.
383  */
384 	static int
385 findaction(actname, tables)
386 	char *actname;
387 	struct lesskey_tables *tables;
388 {
389 	int i;
390 
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);
395 	return (A_INVALID);
396 }
397 
398 /*
399  * Parse a line describing one key binding, of the form
400  *  KEY ACTION [EXTRA]
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.
404  */
405 	static void
406 parse_cmdline(p, tables)
407 	char *p;
408 	struct lesskey_tables *tables;
409 {
410 	char *actname;
411 	int action;
412 	char *s;
413 	char c;
414 
415 	/*
416 	 * Parse the command string and store it in the current table.
417 	 */
418 	do
419 	{
420 		s = tstr(&p, 1);
421 		add_cmd_str(s, tables);
422 	} while (*p != '\0' && !issp(*p));
423 	/*
424 	 * Terminate the command string with a null byte.
425 	 */
426 	add_cmd_char('\0', tables);
427 
428 	/*
429 	 * Skip white space between the command string
430 	 * and the action name.
431 	 * Terminate the action name with a null byte.
432 	 */
433 	p = skipsp(p);
434 	if (*p == '\0')
435 	{
436 		parse_error("missing action", "");
437 		return;
438 	}
439 	actname = p;
440 	p = skipnsp(p);
441 	c = *p;
442 	*p = '\0';
443 
444 	/*
445 	 * Parse the action name and store it in the current table.
446 	 */
447 	action = findaction(actname, tables);
448 
449 	/*
450 	 * See if an extra string follows the action name.
451 	 */
452 	*p = c;
453 	p = skipsp(p);
454 	if (*p == '\0')
455 	{
456 		add_cmd_char(action, tables);
457 	} else
458 	{
459 		/*
460 		 * OR the special value A_EXTRA into the action byte.
461 		 * Put the extra string after the action byte.
462 		 */
463 		add_cmd_char(action | A_EXTRA, tables);
464 		while (*p != '\0')
465 			add_cmd_str(tstr(&p, 0), tables);
466 		add_cmd_char('\0', tables);
467 	}
468 }
469 
470 /*
471  * Parse a variable definition line, of the form
472  *  NAME = VALUE
473  */
474 	static void
475 parse_varline(line, tables)
476 	char *line;
477 	struct lesskey_tables *tables;
478 {
479 	char *s;
480 	char *p = line;
481 
482 	do
483 	{
484 		s = tstr(&p, 0);
485 		add_cmd_str(s, tables);
486 	} while (*p != '\0' && !issp(*p) && *p != '=');
487 	/*
488 	 * Terminate the variable name with a null byte.
489 	 */
490 	add_cmd_char('\0', tables);
491 
492 	p = skipsp(p);
493 	if (*p++ != '=')
494 	{
495 		parse_error("missing = in: ", line);
496 		return;
497 	}
498 
499 	add_cmd_char(EV_OK|A_EXTRA, tables);
500 
501 	p = skipsp(p);
502 	while (*p != '\0')
503 	{
504 		s = tstr(&p, 0);
505 		add_cmd_str(s, tables);
506 	}
507 	add_cmd_char('\0', tables);
508 }
509 
510 /*
511  * Parse a line from the lesskey file.
512  */
513 	static void
514 parse_line(line, tables)
515 	char *line;
516 	struct lesskey_tables *tables;
517 {
518 	char *p;
519 
520 	/*
521 	 * See if it is a control line.
522 	 */
523 	if (control_line(line, tables))
524 		return;
525 	/*
526 	 * Skip leading white space.
527 	 * Replace the final newline with a null byte.
528 	 * Ignore blank lines and comments.
529 	 */
530 	p = clean_line(line);
531 	if (*p == '\0')
532 		return;
533 
534 	if (tables->currtable->is_var)
535 		parse_varline(p, tables);
536 	else
537 		parse_cmdline(p, tables);
538 }
539 
540 /*
541  * Parse a lesskey source file and store result in tables.
542  */
543 	int
544 parse_lesskey(infile, tables)
545 	char *infile;
546 	struct lesskey_tables *tables;
547 {
548 	FILE *desc;
549 	char line[1024];
550 
551 	if (infile == NULL)
552 		infile = homefile(DEF_LESSKEYINFILE);
553 	lesskey_file = infile;
554 
555 	init_tables(tables);
556 	errors = 0;
557 	linenum = 0;
558 
559 	/*
560 	 * Open the input file.
561 	 */
562 	if (strcmp(infile, "-") == 0)
563 		desc = stdin;
564 	else if ((desc = fopen(infile, "r")) == NULL)
565 	{
566 		/* parse_error("cannot open lesskey file ", infile); */
567 		return (-1);
568 	}
569 
570 	/*
571 	 * Read and parse the input file, one line at a time.
572 	 */
573 	while (fgets(line, sizeof(line), desc) != NULL)
574 	{
575 		++linenum;
576 		parse_line(line, tables);
577 	}
578 
579 	return (errors);
580 }
581