xref: /dflybsd-src/contrib/less/lesskey_parse.c (revision e0f238eda64c20d98364903e0c003825fd0b66dd)
10c7ad07eSAntonio Huete Jimenez /*
2*e433da38SAaron LI  * Copyright (C) 1984-2024  Mark Nudelman
30c7ad07eSAntonio Huete Jimenez  *
40c7ad07eSAntonio Huete Jimenez  * You may distribute under the terms of either the GNU General Public
50c7ad07eSAntonio Huete Jimenez  * License or the Less License, as specified in the README file.
60c7ad07eSAntonio Huete Jimenez  *
70c7ad07eSAntonio Huete Jimenez  * For more information, see the README file.
80c7ad07eSAntonio Huete Jimenez  */
90c7ad07eSAntonio Huete Jimenez 
10320d7c8aSAaron LI #include "defines.h"
110c7ad07eSAntonio Huete Jimenez #include <stdio.h>
120c7ad07eSAntonio Huete Jimenez #include <string.h>
130c7ad07eSAntonio Huete Jimenez #include <stdlib.h>
140c7ad07eSAntonio Huete Jimenez #include "lesskey.h"
150c7ad07eSAntonio Huete Jimenez #include "cmd.h"
160c7ad07eSAntonio Huete Jimenez #include "xbuf.h"
170c7ad07eSAntonio Huete Jimenez 
180c7ad07eSAntonio Huete Jimenez #define CONTROL(c)      ((c)&037)
190c7ad07eSAntonio Huete Jimenez #define ESC             CONTROL('[')
200c7ad07eSAntonio Huete Jimenez 
210c7ad07eSAntonio Huete Jimenez extern void lesskey_parse_error(char *msg);
220c7ad07eSAntonio Huete Jimenez extern char *homefile(char *filename);
23*e433da38SAaron LI extern void *ecalloc(size_t count, size_t size);
24320d7c8aSAaron LI extern int lstrtoi(char *str, char **end, int radix);
250c7ad07eSAntonio Huete Jimenez extern char version[];
260c7ad07eSAntonio Huete Jimenez 
270c7ad07eSAntonio Huete Jimenez static int linenum;
280c7ad07eSAntonio Huete Jimenez static int errors;
290c7ad07eSAntonio Huete Jimenez static int less_version = 0;
30*e433da38SAaron LI static char *lesskey_file = NULL;
310c7ad07eSAntonio Huete Jimenez 
32*e433da38SAaron LI static constant struct lesskey_cmdname cmdnames[] =
330c7ad07eSAntonio Huete Jimenez {
340c7ad07eSAntonio Huete Jimenez 	{ "back-bracket",         A_B_BRACKET },
350c7ad07eSAntonio Huete Jimenez 	{ "back-line",            A_B_LINE },
360c7ad07eSAntonio Huete Jimenez 	{ "back-line-force",      A_BF_LINE },
370c7ad07eSAntonio Huete Jimenez 	{ "back-screen",          A_B_SCREEN },
380c7ad07eSAntonio Huete Jimenez 	{ "back-scroll",          A_B_SCROLL },
390c7ad07eSAntonio Huete Jimenez 	{ "back-search",          A_B_SEARCH },
400c7ad07eSAntonio Huete Jimenez 	{ "back-window",          A_B_WINDOW },
410c7ad07eSAntonio Huete Jimenez 	{ "clear-mark",           A_CLRMARK },
42*e433da38SAaron LI 	{ "clear-search",         A_CLR_SEARCH },
430c7ad07eSAntonio Huete Jimenez 	{ "debug",                A_DEBUG },
440c7ad07eSAntonio Huete Jimenez 	{ "digit",                A_DIGIT },
450c7ad07eSAntonio Huete Jimenez 	{ "display-flag",         A_DISP_OPTION },
460c7ad07eSAntonio Huete Jimenez 	{ "display-option",       A_DISP_OPTION },
470c7ad07eSAntonio Huete Jimenez 	{ "end",                  A_GOEND },
480c7ad07eSAntonio Huete Jimenez 	{ "end-scroll",           A_RRSHIFT },
490c7ad07eSAntonio Huete Jimenez 	{ "examine",              A_EXAMINE },
500c7ad07eSAntonio Huete Jimenez 	{ "filter",               A_FILTER },
510c7ad07eSAntonio Huete Jimenez 	{ "first-cmd",            A_FIRSTCMD },
520c7ad07eSAntonio Huete Jimenez 	{ "firstcmd",             A_FIRSTCMD },
530c7ad07eSAntonio Huete Jimenez 	{ "flush-repaint",        A_FREPAINT },
540c7ad07eSAntonio Huete Jimenez 	{ "forw-bracket",         A_F_BRACKET },
550c7ad07eSAntonio Huete Jimenez 	{ "forw-forever",         A_F_FOREVER },
560c7ad07eSAntonio Huete Jimenez 	{ "forw-line",            A_F_LINE },
570c7ad07eSAntonio Huete Jimenez 	{ "forw-line-force",      A_FF_LINE },
580c7ad07eSAntonio Huete Jimenez 	{ "forw-screen",          A_F_SCREEN },
590c7ad07eSAntonio Huete Jimenez 	{ "forw-screen-force",    A_FF_SCREEN },
600c7ad07eSAntonio Huete Jimenez 	{ "forw-scroll",          A_F_SCROLL },
610c7ad07eSAntonio Huete Jimenez 	{ "forw-search",          A_F_SEARCH },
62*e433da38SAaron LI 	{ "forw-until-hilite",    A_F_UNTIL_HILITE },
630c7ad07eSAntonio Huete Jimenez 	{ "forw-window",          A_F_WINDOW },
640c7ad07eSAntonio Huete Jimenez 	{ "goto-end",             A_GOEND },
650c7ad07eSAntonio Huete Jimenez 	{ "goto-end-buffered",    A_GOEND_BUF },
660c7ad07eSAntonio Huete Jimenez 	{ "goto-line",            A_GOLINE },
670c7ad07eSAntonio Huete Jimenez 	{ "goto-mark",            A_GOMARK },
680c7ad07eSAntonio Huete Jimenez 	{ "help",                 A_HELP },
690c7ad07eSAntonio Huete Jimenez 	{ "index-file",           A_INDEX_FILE },
700c7ad07eSAntonio Huete Jimenez 	{ "invalid",              A_UINVALID },
710c7ad07eSAntonio Huete Jimenez 	{ "left-scroll",          A_LSHIFT },
720c7ad07eSAntonio Huete Jimenez 	{ "next-file",            A_NEXT_FILE },
730c7ad07eSAntonio Huete Jimenez 	{ "next-tag",             A_NEXT_TAG },
740c7ad07eSAntonio Huete Jimenez 	{ "no-scroll",            A_LLSHIFT },
75*e433da38SAaron LI 	{ "noaction",             A_NOACTION },
76*e433da38SAaron LI 	{ "osc8-forw-search",     A_OSC8_F_SEARCH },
77*e433da38SAaron LI 	{ "osc8-back-search",     A_OSC8_B_SEARCH },
78*e433da38SAaron LI 	{ "osc8-open",            A_OSC8_OPEN },
790c7ad07eSAntonio Huete Jimenez 	{ "percent",              A_PERCENT },
800c7ad07eSAntonio Huete Jimenez 	{ "pipe",                 A_PIPE },
810c7ad07eSAntonio Huete Jimenez 	{ "prev-file",            A_PREV_FILE },
820c7ad07eSAntonio Huete Jimenez 	{ "prev-tag",             A_PREV_TAG },
83*e433da38SAaron LI 	{ "pshell",               A_PSHELL },
840c7ad07eSAntonio Huete Jimenez 	{ "quit",                 A_QUIT },
850c7ad07eSAntonio Huete Jimenez 	{ "remove-file",          A_REMOVE_FILE },
860c7ad07eSAntonio Huete Jimenez 	{ "repaint",              A_REPAINT },
870c7ad07eSAntonio Huete Jimenez 	{ "repaint-flush",        A_FREPAINT },
880c7ad07eSAntonio Huete Jimenez 	{ "repeat-search",        A_AGAIN_SEARCH },
890c7ad07eSAntonio Huete Jimenez 	{ "repeat-search-all",    A_T_AGAIN_SEARCH },
900c7ad07eSAntonio Huete Jimenez 	{ "reverse-search",       A_REVERSE_SEARCH },
910c7ad07eSAntonio Huete Jimenez 	{ "reverse-search-all",   A_T_REVERSE_SEARCH },
920c7ad07eSAntonio Huete Jimenez 	{ "right-scroll",         A_RSHIFT },
930c7ad07eSAntonio Huete Jimenez 	{ "set-mark",             A_SETMARK },
940c7ad07eSAntonio Huete Jimenez 	{ "set-mark-bottom",      A_SETMARKBOT },
950c7ad07eSAntonio Huete Jimenez 	{ "shell",                A_SHELL },
960c7ad07eSAntonio Huete Jimenez 	{ "status",               A_STAT },
970c7ad07eSAntonio Huete Jimenez 	{ "toggle-flag",          A_OPT_TOGGLE },
980c7ad07eSAntonio Huete Jimenez 	{ "toggle-option",        A_OPT_TOGGLE },
990c7ad07eSAntonio Huete Jimenez 	{ "undo-hilite",          A_UNDO_SEARCH },
1000c7ad07eSAntonio Huete Jimenez 	{ "version",              A_VERSION },
1010c7ad07eSAntonio Huete Jimenez 	{ "visual",               A_VISUAL },
1020c7ad07eSAntonio Huete Jimenez 	{ NULL,   0 }
1030c7ad07eSAntonio Huete Jimenez };
1040c7ad07eSAntonio Huete Jimenez 
105*e433da38SAaron LI static constant struct lesskey_cmdname editnames[] =
1060c7ad07eSAntonio Huete Jimenez {
1070c7ad07eSAntonio Huete Jimenez 	{ "back-complete",      EC_B_COMPLETE },
1080c7ad07eSAntonio Huete Jimenez 	{ "backspace",          EC_BACKSPACE },
1090c7ad07eSAntonio Huete Jimenez 	{ "delete",             EC_DELETE },
1100c7ad07eSAntonio Huete Jimenez 	{ "down",               EC_DOWN },
1110c7ad07eSAntonio Huete Jimenez 	{ "end",                EC_END },
1120c7ad07eSAntonio Huete Jimenez 	{ "expand",             EC_EXPAND },
1130c7ad07eSAntonio Huete Jimenez 	{ "forw-complete",      EC_F_COMPLETE },
1140c7ad07eSAntonio Huete Jimenez 	{ "home",               EC_HOME },
1150c7ad07eSAntonio Huete Jimenez 	{ "insert",             EC_INSERT },
1160c7ad07eSAntonio Huete Jimenez 	{ "invalid",            EC_UINVALID },
1170c7ad07eSAntonio Huete Jimenez 	{ "kill-line",          EC_LINEKILL },
1180c7ad07eSAntonio Huete Jimenez 	{ "abort",              EC_ABORT },
1190c7ad07eSAntonio Huete Jimenez 	{ "left",               EC_LEFT },
1200c7ad07eSAntonio Huete Jimenez 	{ "literal",            EC_LITERAL },
1210c7ad07eSAntonio Huete Jimenez 	{ "right",              EC_RIGHT },
1220c7ad07eSAntonio Huete Jimenez 	{ "up",                 EC_UP },
1230c7ad07eSAntonio Huete Jimenez 	{ "word-backspace",     EC_W_BACKSPACE },
1240c7ad07eSAntonio Huete Jimenez 	{ "word-delete",        EC_W_DELETE },
1250c7ad07eSAntonio Huete Jimenez 	{ "word-left",          EC_W_LEFT },
1260c7ad07eSAntonio Huete Jimenez 	{ "word-right",         EC_W_RIGHT },
1270c7ad07eSAntonio Huete Jimenez 	{ NULL, 0 }
1280c7ad07eSAntonio Huete Jimenez };
1290c7ad07eSAntonio Huete Jimenez 
1300c7ad07eSAntonio Huete Jimenez /*
1310c7ad07eSAntonio Huete Jimenez  * Print a parse error message.
1320c7ad07eSAntonio Huete Jimenez  */
parse_error(constant char * fmt,constant char * arg1)133*e433da38SAaron LI static void parse_error(constant char *fmt, constant char *arg1)
1340c7ad07eSAntonio Huete Jimenez {
1350c7ad07eSAntonio Huete Jimenez 	char buf[1024];
136*e433da38SAaron LI 	int n = SNPRINTF2(buf, sizeof(buf), "%s: line %d: ", lesskey_file, linenum);
137*e433da38SAaron LI 	if (n >= 0)
138*e433da38SAaron LI 	{
139*e433da38SAaron LI 		size_t len = (size_t) n;
140*e433da38SAaron LI 		if (len < sizeof(buf))
141*e433da38SAaron LI 			SNPRINTF1(buf+len, sizeof(buf)-len, fmt, arg1);
142*e433da38SAaron LI 	}
1430c7ad07eSAntonio Huete Jimenez 	++errors;
1440c7ad07eSAntonio Huete Jimenez 	lesskey_parse_error(buf);
1450c7ad07eSAntonio Huete Jimenez }
1460c7ad07eSAntonio Huete Jimenez 
1470c7ad07eSAntonio Huete Jimenez /*
1480c7ad07eSAntonio Huete Jimenez  * Initialize lesskey_tables.
1490c7ad07eSAntonio Huete Jimenez  */
init_tables(struct lesskey_tables * tables)150320d7c8aSAaron LI static void init_tables(struct lesskey_tables *tables)
1510c7ad07eSAntonio Huete Jimenez {
1520c7ad07eSAntonio Huete Jimenez 	tables->currtable = &tables->cmdtable;
1530c7ad07eSAntonio Huete Jimenez 
1540c7ad07eSAntonio Huete Jimenez 	tables->cmdtable.names = cmdnames;
1550c7ad07eSAntonio Huete Jimenez 	tables->cmdtable.is_var = 0;
1560c7ad07eSAntonio Huete Jimenez 	xbuf_init(&tables->cmdtable.buf);
1570c7ad07eSAntonio Huete Jimenez 
1580c7ad07eSAntonio Huete Jimenez 	tables->edittable.names = editnames;
1590c7ad07eSAntonio Huete Jimenez 	tables->edittable.is_var = 0;
1600c7ad07eSAntonio Huete Jimenez 	xbuf_init(&tables->edittable.buf);
1610c7ad07eSAntonio Huete Jimenez 
1620c7ad07eSAntonio Huete Jimenez 	tables->vartable.names = NULL;
1630c7ad07eSAntonio Huete Jimenez 	tables->vartable.is_var = 1;
1640c7ad07eSAntonio Huete Jimenez 	xbuf_init(&tables->vartable.buf);
1650c7ad07eSAntonio Huete Jimenez }
1660c7ad07eSAntonio Huete Jimenez 
1670c7ad07eSAntonio Huete Jimenez #define CHAR_STRING_LEN 8
1680c7ad07eSAntonio Huete Jimenez 
char_string(char * buf,char ch,int lit)169*e433da38SAaron LI static constant char * char_string(char *buf, char ch, int lit)
1700c7ad07eSAntonio Huete Jimenez {
1710c7ad07eSAntonio Huete Jimenez 	if (lit || (ch >= 0x20 && ch < 0x7f))
1720c7ad07eSAntonio Huete Jimenez 	{
1730c7ad07eSAntonio Huete Jimenez 		buf[0] = ch;
1740c7ad07eSAntonio Huete Jimenez 		buf[1] = '\0';
1750c7ad07eSAntonio Huete Jimenez 	} else
1760c7ad07eSAntonio Huete Jimenez 	{
177*e433da38SAaron LI 		SNPRINTF1(buf, CHAR_STRING_LEN, "\\x%02x", ch);
1780c7ad07eSAntonio Huete Jimenez 	}
1790c7ad07eSAntonio Huete Jimenez 	return buf;
1800c7ad07eSAntonio Huete Jimenez }
1810c7ad07eSAntonio Huete Jimenez 
1820c7ad07eSAntonio Huete Jimenez /*
1830c7ad07eSAntonio Huete Jimenez  * Increment char pointer by one up to terminating nul byte.
1840c7ad07eSAntonio Huete Jimenez  */
increment_pointer(char * p)185320d7c8aSAaron LI static char * increment_pointer(char *p)
1860c7ad07eSAntonio Huete Jimenez {
1870c7ad07eSAntonio Huete Jimenez 	if (*p == '\0')
1880c7ad07eSAntonio Huete Jimenez 		return p;
1890c7ad07eSAntonio Huete Jimenez 	return p+1;
1900c7ad07eSAntonio Huete Jimenez }
1910c7ad07eSAntonio Huete Jimenez 
1920c7ad07eSAntonio Huete Jimenez /*
1930c7ad07eSAntonio Huete Jimenez  * Parse one character of a string.
1940c7ad07eSAntonio Huete Jimenez  */
tstr(char ** pp,int xlate)195*e433da38SAaron LI static constant char * tstr(char **pp, int xlate)
1960c7ad07eSAntonio Huete Jimenez {
1970c7ad07eSAntonio Huete Jimenez 	char *p;
1980c7ad07eSAntonio Huete Jimenez 	char ch;
1990c7ad07eSAntonio Huete Jimenez 	int i;
2000c7ad07eSAntonio Huete Jimenez 	static char buf[CHAR_STRING_LEN];
2010c7ad07eSAntonio Huete Jimenez 	static char tstr_control_k[] =
2020c7ad07eSAntonio Huete Jimenez 		{ SK_SPECIAL_KEY, SK_CONTROL_K, 6, 1, 1, 1, '\0' };
2030c7ad07eSAntonio Huete Jimenez 
2040c7ad07eSAntonio Huete Jimenez 	p = *pp;
2050c7ad07eSAntonio Huete Jimenez 	switch (*p)
2060c7ad07eSAntonio Huete Jimenez 	{
2070c7ad07eSAntonio Huete Jimenez 	case '\\':
2080c7ad07eSAntonio Huete Jimenez 		++p;
2090c7ad07eSAntonio Huete Jimenez 		switch (*p)
2100c7ad07eSAntonio Huete Jimenez 		{
2110c7ad07eSAntonio Huete Jimenez 		case '0': case '1': case '2': case '3':
2120c7ad07eSAntonio Huete Jimenez 		case '4': case '5': case '6': case '7':
2130c7ad07eSAntonio Huete Jimenez 			/*
2140c7ad07eSAntonio Huete Jimenez 			 * Parse an octal number.
2150c7ad07eSAntonio Huete Jimenez 			 */
2160c7ad07eSAntonio Huete Jimenez 			ch = 0;
2170c7ad07eSAntonio Huete Jimenez 			i = 0;
2180c7ad07eSAntonio Huete Jimenez 			do
219*e433da38SAaron LI 				ch = (char) (8*ch + (*p - '0'));
2200c7ad07eSAntonio Huete Jimenez 			while (*++p >= '0' && *p <= '7' && ++i < 3);
2210c7ad07eSAntonio Huete Jimenez 			*pp = p;
2220c7ad07eSAntonio Huete Jimenez 			if (xlate && ch == CONTROL('K'))
2230c7ad07eSAntonio Huete Jimenez 				return tstr_control_k;
2240c7ad07eSAntonio Huete Jimenez 			return char_string(buf, ch, 1);
2250c7ad07eSAntonio Huete Jimenez 		case 'b':
2260c7ad07eSAntonio Huete Jimenez 			*pp = p+1;
2270c7ad07eSAntonio Huete Jimenez 			return ("\b");
2280c7ad07eSAntonio Huete Jimenez 		case 'e':
2290c7ad07eSAntonio Huete Jimenez 			*pp = p+1;
2300c7ad07eSAntonio Huete Jimenez 			return char_string(buf, ESC, 1);
2310c7ad07eSAntonio Huete Jimenez 		case 'n':
2320c7ad07eSAntonio Huete Jimenez 			*pp = p+1;
2330c7ad07eSAntonio Huete Jimenez 			return ("\n");
2340c7ad07eSAntonio Huete Jimenez 		case 'r':
2350c7ad07eSAntonio Huete Jimenez 			*pp = p+1;
2360c7ad07eSAntonio Huete Jimenez 			return ("\r");
2370c7ad07eSAntonio Huete Jimenez 		case 't':
2380c7ad07eSAntonio Huete Jimenez 			*pp = p+1;
2390c7ad07eSAntonio Huete Jimenez 			return ("\t");
2400c7ad07eSAntonio Huete Jimenez 		case 'k':
2410c7ad07eSAntonio Huete Jimenez 			if (xlate)
2420c7ad07eSAntonio Huete Jimenez 			{
2430c7ad07eSAntonio Huete Jimenez 				switch (*++p)
2440c7ad07eSAntonio Huete Jimenez 				{
2450c7ad07eSAntonio Huete Jimenez 				case 'b': ch = SK_BACKSPACE; break;
2460c7ad07eSAntonio Huete Jimenez 				case 'B': ch = SK_CTL_BACKSPACE; break;
2470c7ad07eSAntonio Huete Jimenez 				case 'd': ch = SK_DOWN_ARROW; break;
2480c7ad07eSAntonio Huete Jimenez 				case 'D': ch = SK_PAGE_DOWN; break;
2490c7ad07eSAntonio Huete Jimenez 				case 'e': ch = SK_END; break;
2500c7ad07eSAntonio Huete Jimenez 				case 'h': ch = SK_HOME; break;
2510c7ad07eSAntonio Huete Jimenez 				case 'i': ch = SK_INSERT; break;
2520c7ad07eSAntonio Huete Jimenez 				case 'l': ch = SK_LEFT_ARROW; break;
2530c7ad07eSAntonio Huete Jimenez 				case 'L': ch = SK_CTL_LEFT_ARROW; break;
2540c7ad07eSAntonio Huete Jimenez 				case 'r': ch = SK_RIGHT_ARROW; break;
2550c7ad07eSAntonio Huete Jimenez 				case 'R': ch = SK_CTL_RIGHT_ARROW; break;
2560c7ad07eSAntonio Huete Jimenez 				case 't': ch = SK_BACKTAB; break;
2570c7ad07eSAntonio Huete Jimenez 				case 'u': ch = SK_UP_ARROW; break;
2580c7ad07eSAntonio Huete Jimenez 				case 'U': ch = SK_PAGE_UP; break;
2590c7ad07eSAntonio Huete Jimenez 				case 'x': ch = SK_DELETE; break;
2600c7ad07eSAntonio Huete Jimenez 				case 'X': ch = SK_CTL_DELETE; break;
2610c7ad07eSAntonio Huete Jimenez 				case '1': ch = SK_F1; break;
2620c7ad07eSAntonio Huete Jimenez 				default:
2630c7ad07eSAntonio Huete Jimenez 					parse_error("invalid escape sequence \"\\k%s\"", char_string(buf, *p, 0));
2640c7ad07eSAntonio Huete Jimenez 					*pp = increment_pointer(p);
2650c7ad07eSAntonio Huete Jimenez 					return ("");
2660c7ad07eSAntonio Huete Jimenez 				}
2670c7ad07eSAntonio Huete Jimenez 				*pp = p+1;
2680c7ad07eSAntonio Huete Jimenez 				buf[0] = SK_SPECIAL_KEY;
2690c7ad07eSAntonio Huete Jimenez 				buf[1] = ch;
2700c7ad07eSAntonio Huete Jimenez 				buf[2] = 6;
2710c7ad07eSAntonio Huete Jimenez 				buf[3] = 1;
2720c7ad07eSAntonio Huete Jimenez 				buf[4] = 1;
2730c7ad07eSAntonio Huete Jimenez 				buf[5] = 1;
2740c7ad07eSAntonio Huete Jimenez 				buf[6] = '\0';
2750c7ad07eSAntonio Huete Jimenez 				return (buf);
2760c7ad07eSAntonio Huete Jimenez 			}
2770c7ad07eSAntonio Huete Jimenez 			/* FALLTHRU */
2780c7ad07eSAntonio Huete Jimenez 		default:
2790c7ad07eSAntonio Huete Jimenez 			/*
2800c7ad07eSAntonio Huete Jimenez 			 * Backslash followed by any other char
2810c7ad07eSAntonio Huete Jimenez 			 * just means that char.
2820c7ad07eSAntonio Huete Jimenez 			 */
2830c7ad07eSAntonio Huete Jimenez 			*pp = increment_pointer(p);
2840c7ad07eSAntonio Huete Jimenez 			char_string(buf, *p, 1);
2850c7ad07eSAntonio Huete Jimenez 			if (xlate && buf[0] == CONTROL('K'))
2860c7ad07eSAntonio Huete Jimenez 				return tstr_control_k;
2870c7ad07eSAntonio Huete Jimenez 			return (buf);
2880c7ad07eSAntonio Huete Jimenez 		}
2890c7ad07eSAntonio Huete Jimenez 	case '^':
2900c7ad07eSAntonio Huete Jimenez 		/*
2910c7ad07eSAntonio Huete Jimenez 		 * Caret means CONTROL.
2920c7ad07eSAntonio Huete Jimenez 		 */
2930c7ad07eSAntonio Huete Jimenez 		*pp = increment_pointer(p+1);
2940c7ad07eSAntonio Huete Jimenez 		char_string(buf, CONTROL(p[1]), 1);
2950c7ad07eSAntonio Huete Jimenez 		if (xlate && buf[0] == CONTROL('K'))
2960c7ad07eSAntonio Huete Jimenez 			return tstr_control_k;
2970c7ad07eSAntonio Huete Jimenez 		return (buf);
2980c7ad07eSAntonio Huete Jimenez 	}
2990c7ad07eSAntonio Huete Jimenez 	*pp = increment_pointer(p);
3000c7ad07eSAntonio Huete Jimenez 	char_string(buf, *p, 1);
3010c7ad07eSAntonio Huete Jimenez 	if (xlate && buf[0] == CONTROL('K'))
3020c7ad07eSAntonio Huete Jimenez 		return tstr_control_k;
3030c7ad07eSAntonio Huete Jimenez 	return (buf);
3040c7ad07eSAntonio Huete Jimenez }
3050c7ad07eSAntonio Huete Jimenez 
issp(char ch)306320d7c8aSAaron LI static int issp(char ch)
3070c7ad07eSAntonio Huete Jimenez {
3080c7ad07eSAntonio Huete Jimenez 	return (ch == ' ' || ch == '\t');
3090c7ad07eSAntonio Huete Jimenez }
3100c7ad07eSAntonio Huete Jimenez 
3110c7ad07eSAntonio Huete Jimenez /*
3120c7ad07eSAntonio Huete Jimenez  * Skip leading spaces in a string.
3130c7ad07eSAntonio Huete Jimenez  */
skipsp(char * s)314320d7c8aSAaron LI static char * skipsp(char *s)
3150c7ad07eSAntonio Huete Jimenez {
3160c7ad07eSAntonio Huete Jimenez 	while (issp(*s))
3170c7ad07eSAntonio Huete Jimenez 		s++;
3180c7ad07eSAntonio Huete Jimenez 	return (s);
3190c7ad07eSAntonio Huete Jimenez }
3200c7ad07eSAntonio Huete Jimenez 
3210c7ad07eSAntonio Huete Jimenez /*
3220c7ad07eSAntonio Huete Jimenez  * Skip non-space characters in a string.
3230c7ad07eSAntonio Huete Jimenez  */
skipnsp(char * s)324320d7c8aSAaron LI static char * skipnsp(char *s)
3250c7ad07eSAntonio Huete Jimenez {
3260c7ad07eSAntonio Huete Jimenez 	while (*s != '\0' && !issp(*s))
3270c7ad07eSAntonio Huete Jimenez 		s++;
3280c7ad07eSAntonio Huete Jimenez 	return (s);
3290c7ad07eSAntonio Huete Jimenez }
3300c7ad07eSAntonio Huete Jimenez 
3310c7ad07eSAntonio Huete Jimenez /*
3320c7ad07eSAntonio Huete Jimenez  * Clean up an input line:
3330c7ad07eSAntonio Huete Jimenez  * strip off the trailing newline & any trailing # comment.
3340c7ad07eSAntonio Huete Jimenez  */
clean_line(char * s)335320d7c8aSAaron LI static char * clean_line(char *s)
3360c7ad07eSAntonio Huete Jimenez {
3370c7ad07eSAntonio Huete Jimenez 	int i;
3380c7ad07eSAntonio Huete Jimenez 
3390c7ad07eSAntonio Huete Jimenez 	s = skipsp(s);
3400c7ad07eSAntonio Huete Jimenez 	for (i = 0;  s[i] != '\0' && s[i] != '\n' && s[i] != '\r';  i++)
3410c7ad07eSAntonio Huete Jimenez 		if (s[i] == '#' && (i == 0 || s[i-1] != '\\'))
3420c7ad07eSAntonio Huete Jimenez 			break;
3430c7ad07eSAntonio Huete Jimenez 	s[i] = '\0';
3440c7ad07eSAntonio Huete Jimenez 	return (s);
3450c7ad07eSAntonio Huete Jimenez }
3460c7ad07eSAntonio Huete Jimenez 
3470c7ad07eSAntonio Huete Jimenez /*
3480c7ad07eSAntonio Huete Jimenez  * Add a byte to the output command table.
3490c7ad07eSAntonio Huete Jimenez  */
add_cmd_char(unsigned char c,struct lesskey_tables * tables)350320d7c8aSAaron LI static void add_cmd_char(unsigned char c, struct lesskey_tables *tables)
3510c7ad07eSAntonio Huete Jimenez {
352320d7c8aSAaron LI 	xbuf_add_byte(&tables->currtable->buf, c);
3530c7ad07eSAntonio Huete Jimenez }
3540c7ad07eSAntonio Huete Jimenez 
erase_cmd_char(struct lesskey_tables * tables)355320d7c8aSAaron LI static void erase_cmd_char(struct lesskey_tables *tables)
3560c7ad07eSAntonio Huete Jimenez {
3570c7ad07eSAntonio Huete Jimenez 	xbuf_pop(&tables->currtable->buf);
3580c7ad07eSAntonio Huete Jimenez }
3590c7ad07eSAntonio Huete Jimenez 
3600c7ad07eSAntonio Huete Jimenez /*
3610c7ad07eSAntonio Huete Jimenez  * Add a string to the output command table.
3620c7ad07eSAntonio Huete Jimenez  */
add_cmd_str(constant char * s,struct lesskey_tables * tables)363*e433da38SAaron LI static void add_cmd_str(constant char *s, struct lesskey_tables *tables)
3640c7ad07eSAntonio Huete Jimenez {
3650c7ad07eSAntonio Huete Jimenez 	for ( ;  *s != '\0';  s++)
366*e433da38SAaron LI 		add_cmd_char((unsigned char) *s, tables);
3670c7ad07eSAntonio Huete Jimenez }
3680c7ad07eSAntonio Huete Jimenez 
3690c7ad07eSAntonio Huete Jimenez /*
3700c7ad07eSAntonio Huete Jimenez  * Does a given version number match the running version?
3710c7ad07eSAntonio Huete Jimenez  * Operator compares the running version to the given version.
3720c7ad07eSAntonio Huete Jimenez  */
match_version(char op,int ver)373320d7c8aSAaron LI static int match_version(char op, int ver)
3740c7ad07eSAntonio Huete Jimenez {
3750c7ad07eSAntonio Huete Jimenez 	switch (op)
3760c7ad07eSAntonio Huete Jimenez 	{
3770c7ad07eSAntonio Huete Jimenez 	case '>': return less_version > ver;
3780c7ad07eSAntonio Huete Jimenez 	case '<': return less_version < ver;
3790c7ad07eSAntonio Huete Jimenez 	case '+': return less_version >= ver;
3800c7ad07eSAntonio Huete Jimenez 	case '-': return less_version <= ver;
3810c7ad07eSAntonio Huete Jimenez 	case '=': return less_version == ver;
3820c7ad07eSAntonio Huete Jimenez 	case '!': return less_version != ver;
3830c7ad07eSAntonio Huete Jimenez 	default: return 0; /* cannot happen */
3840c7ad07eSAntonio Huete Jimenez 	}
3850c7ad07eSAntonio Huete Jimenez }
3860c7ad07eSAntonio Huete Jimenez 
3870c7ad07eSAntonio Huete Jimenez /*
3880c7ad07eSAntonio Huete Jimenez  * Handle a #version line.
3890c7ad07eSAntonio Huete Jimenez  * If the version matches, return the part of the line that should be executed.
3900c7ad07eSAntonio Huete Jimenez  * Otherwise, return NULL.
3910c7ad07eSAntonio Huete Jimenez  */
version_line(char * s)392*e433da38SAaron LI static char * version_line(char *s)
3930c7ad07eSAntonio Huete Jimenez {
3940c7ad07eSAntonio Huete Jimenez 	char op;
3950c7ad07eSAntonio Huete Jimenez 	int ver;
3960c7ad07eSAntonio Huete Jimenez 	char *e;
3970c7ad07eSAntonio Huete Jimenez 	char buf[CHAR_STRING_LEN];
3980c7ad07eSAntonio Huete Jimenez 
3990c7ad07eSAntonio Huete Jimenez 	s += strlen("#version");
4000c7ad07eSAntonio Huete Jimenez 	s = skipsp(s);
4010c7ad07eSAntonio Huete Jimenez 	op = *s++;
4020c7ad07eSAntonio Huete Jimenez 	/* Simplify 2-char op to one char. */
4030c7ad07eSAntonio Huete Jimenez 	switch (op)
4040c7ad07eSAntonio Huete Jimenez 	{
4050c7ad07eSAntonio Huete Jimenez 	case '<': if (*s == '=') { s++; op = '-'; } break;
4060c7ad07eSAntonio Huete Jimenez 	case '>': if (*s == '=') { s++; op = '+'; } break;
4070c7ad07eSAntonio Huete Jimenez 	case '=': if (*s == '=') { s++; } break;
4080c7ad07eSAntonio Huete Jimenez 	case '!': if (*s == '=') { s++; } break;
4090c7ad07eSAntonio Huete Jimenez 	default:
4100c7ad07eSAntonio Huete Jimenez 		parse_error("invalid operator '%s' in #version line", char_string(buf, op, 0));
4110c7ad07eSAntonio Huete Jimenez 		return (NULL);
4120c7ad07eSAntonio Huete Jimenez 	}
4130c7ad07eSAntonio Huete Jimenez 	s = skipsp(s);
414320d7c8aSAaron LI 	ver = lstrtoi(s, &e, 10);
4150c7ad07eSAntonio Huete Jimenez 	if (e == s)
4160c7ad07eSAntonio Huete Jimenez 	{
4170c7ad07eSAntonio Huete Jimenez 		parse_error("non-numeric version number in #version line", "");
4180c7ad07eSAntonio Huete Jimenez 		return (NULL);
4190c7ad07eSAntonio Huete Jimenez 	}
4200c7ad07eSAntonio Huete Jimenez 	if (!match_version(op, ver))
4210c7ad07eSAntonio Huete Jimenez 		return (NULL);
4220c7ad07eSAntonio Huete Jimenez 	return (e);
4230c7ad07eSAntonio Huete Jimenez }
4240c7ad07eSAntonio Huete Jimenez 
4250c7ad07eSAntonio Huete Jimenez /*
4260c7ad07eSAntonio Huete Jimenez  * See if we have a special "control" line.
4270c7ad07eSAntonio Huete Jimenez  */
control_line(char * s,struct lesskey_tables * tables)428320d7c8aSAaron LI static char * control_line(char *s, struct lesskey_tables *tables)
4290c7ad07eSAntonio Huete Jimenez {
4300c7ad07eSAntonio Huete Jimenez #define PREFIX(str,pat) (strncmp(str,pat,strlen(pat)) == 0)
4310c7ad07eSAntonio Huete Jimenez 
4320c7ad07eSAntonio Huete Jimenez 	if (PREFIX(s, "#line-edit"))
4330c7ad07eSAntonio Huete Jimenez 	{
4340c7ad07eSAntonio Huete Jimenez 		tables->currtable = &tables->edittable;
4350c7ad07eSAntonio Huete Jimenez 		return (NULL);
4360c7ad07eSAntonio Huete Jimenez 	}
4370c7ad07eSAntonio Huete Jimenez 	if (PREFIX(s, "#command"))
4380c7ad07eSAntonio Huete Jimenez 	{
4390c7ad07eSAntonio Huete Jimenez 		tables->currtable = &tables->cmdtable;
4400c7ad07eSAntonio Huete Jimenez 		return (NULL);
4410c7ad07eSAntonio Huete Jimenez 	}
4420c7ad07eSAntonio Huete Jimenez 	if (PREFIX(s, "#env"))
4430c7ad07eSAntonio Huete Jimenez 	{
4440c7ad07eSAntonio Huete Jimenez 		tables->currtable = &tables->vartable;
4450c7ad07eSAntonio Huete Jimenez 		return (NULL);
4460c7ad07eSAntonio Huete Jimenez 	}
4470c7ad07eSAntonio Huete Jimenez 	if (PREFIX(s, "#stop"))
4480c7ad07eSAntonio Huete Jimenez 	{
4490c7ad07eSAntonio Huete Jimenez 		add_cmd_char('\0', tables);
4500c7ad07eSAntonio Huete Jimenez 		add_cmd_char(A_END_LIST, tables);
4510c7ad07eSAntonio Huete Jimenez 		return (NULL);
4520c7ad07eSAntonio Huete Jimenez 	}
4530c7ad07eSAntonio Huete Jimenez 	if (PREFIX(s, "#version"))
4540c7ad07eSAntonio Huete Jimenez 	{
455*e433da38SAaron LI 		return (version_line(s));
4560c7ad07eSAntonio Huete Jimenez 	}
4570c7ad07eSAntonio Huete Jimenez 	return (s);
4580c7ad07eSAntonio Huete Jimenez }
4590c7ad07eSAntonio Huete Jimenez 
4600c7ad07eSAntonio Huete Jimenez /*
4610c7ad07eSAntonio Huete Jimenez  * Find an action, given the name of the action.
4620c7ad07eSAntonio Huete Jimenez  */
findaction(char * actname,struct lesskey_tables * tables)463320d7c8aSAaron LI static int findaction(char *actname, struct lesskey_tables *tables)
4640c7ad07eSAntonio Huete Jimenez {
4650c7ad07eSAntonio Huete Jimenez 	int i;
4660c7ad07eSAntonio Huete Jimenez 
4670c7ad07eSAntonio Huete Jimenez 	for (i = 0;  tables->currtable->names[i].cn_name != NULL;  i++)
4680c7ad07eSAntonio Huete Jimenez 		if (strcmp(tables->currtable->names[i].cn_name, actname) == 0)
4690c7ad07eSAntonio Huete Jimenez 			return (tables->currtable->names[i].cn_action);
4700c7ad07eSAntonio Huete Jimenez 	parse_error("unknown action: \"%s\"", actname);
4710c7ad07eSAntonio Huete Jimenez 	return (A_INVALID);
4720c7ad07eSAntonio Huete Jimenez }
4730c7ad07eSAntonio Huete Jimenez 
4740c7ad07eSAntonio Huete Jimenez /*
4750c7ad07eSAntonio Huete Jimenez  * Parse a line describing one key binding, of the form
4760c7ad07eSAntonio Huete Jimenez  *  KEY ACTION [EXTRA]
4770c7ad07eSAntonio Huete Jimenez  * where KEY is the user key sequence, ACTION is the
4780c7ad07eSAntonio Huete Jimenez  * resulting less action, and EXTRA is an "extra" user
4790c7ad07eSAntonio Huete Jimenez  * key sequence injected after the action.
4800c7ad07eSAntonio Huete Jimenez  */
parse_cmdline(char * p,struct lesskey_tables * tables)481320d7c8aSAaron LI static void parse_cmdline(char *p, struct lesskey_tables *tables)
4820c7ad07eSAntonio Huete Jimenez {
4830c7ad07eSAntonio Huete Jimenez 	char *actname;
4840c7ad07eSAntonio Huete Jimenez 	int action;
485*e433da38SAaron LI 	constant char *s;
4860c7ad07eSAntonio Huete Jimenez 	char c;
4870c7ad07eSAntonio Huete Jimenez 
4880c7ad07eSAntonio Huete Jimenez 	/*
4890c7ad07eSAntonio Huete Jimenez 	 * Parse the command string and store it in the current table.
4900c7ad07eSAntonio Huete Jimenez 	 */
4910c7ad07eSAntonio Huete Jimenez 	do
4920c7ad07eSAntonio Huete Jimenez 	{
4930c7ad07eSAntonio Huete Jimenez 		s = tstr(&p, 1);
4940c7ad07eSAntonio Huete Jimenez 		add_cmd_str(s, tables);
4950c7ad07eSAntonio Huete Jimenez 	} while (*p != '\0' && !issp(*p));
4960c7ad07eSAntonio Huete Jimenez 	/*
4970c7ad07eSAntonio Huete Jimenez 	 * Terminate the command string with a null byte.
4980c7ad07eSAntonio Huete Jimenez 	 */
4990c7ad07eSAntonio Huete Jimenez 	add_cmd_char('\0', tables);
5000c7ad07eSAntonio Huete Jimenez 
5010c7ad07eSAntonio Huete Jimenez 	/*
5020c7ad07eSAntonio Huete Jimenez 	 * Skip white space between the command string
5030c7ad07eSAntonio Huete Jimenez 	 * and the action name.
5040c7ad07eSAntonio Huete Jimenez 	 * Terminate the action name with a null byte.
5050c7ad07eSAntonio Huete Jimenez 	 */
5060c7ad07eSAntonio Huete Jimenez 	p = skipsp(p);
5070c7ad07eSAntonio Huete Jimenez 	if (*p == '\0')
5080c7ad07eSAntonio Huete Jimenez 	{
5090c7ad07eSAntonio Huete Jimenez 		parse_error("missing action", "");
5100c7ad07eSAntonio Huete Jimenez 		return;
5110c7ad07eSAntonio Huete Jimenez 	}
5120c7ad07eSAntonio Huete Jimenez 	actname = p;
5130c7ad07eSAntonio Huete Jimenez 	p = skipnsp(p);
5140c7ad07eSAntonio Huete Jimenez 	c = *p;
5150c7ad07eSAntonio Huete Jimenez 	*p = '\0';
5160c7ad07eSAntonio Huete Jimenez 
5170c7ad07eSAntonio Huete Jimenez 	/*
5180c7ad07eSAntonio Huete Jimenez 	 * Parse the action name and store it in the current table.
5190c7ad07eSAntonio Huete Jimenez 	 */
5200c7ad07eSAntonio Huete Jimenez 	action = findaction(actname, tables);
5210c7ad07eSAntonio Huete Jimenez 
5220c7ad07eSAntonio Huete Jimenez 	/*
5230c7ad07eSAntonio Huete Jimenez 	 * See if an extra string follows the action name.
5240c7ad07eSAntonio Huete Jimenez 	 */
5250c7ad07eSAntonio Huete Jimenez 	*p = c;
5260c7ad07eSAntonio Huete Jimenez 	p = skipsp(p);
5270c7ad07eSAntonio Huete Jimenez 	if (*p == '\0')
5280c7ad07eSAntonio Huete Jimenez 	{
529320d7c8aSAaron LI 		add_cmd_char((unsigned char) action, tables);
5300c7ad07eSAntonio Huete Jimenez 	} else
5310c7ad07eSAntonio Huete Jimenez 	{
5320c7ad07eSAntonio Huete Jimenez 		/*
5330c7ad07eSAntonio Huete Jimenez 		 * OR the special value A_EXTRA into the action byte.
5340c7ad07eSAntonio Huete Jimenez 		 * Put the extra string after the action byte.
5350c7ad07eSAntonio Huete Jimenez 		 */
536320d7c8aSAaron LI 		add_cmd_char((unsigned char) (action | A_EXTRA), tables);
5370c7ad07eSAntonio Huete Jimenez 		while (*p != '\0')
5380c7ad07eSAntonio Huete Jimenez 			add_cmd_str(tstr(&p, 0), tables);
5390c7ad07eSAntonio Huete Jimenez 		add_cmd_char('\0', tables);
5400c7ad07eSAntonio Huete Jimenez 	}
5410c7ad07eSAntonio Huete Jimenez }
5420c7ad07eSAntonio Huete Jimenez 
5430c7ad07eSAntonio Huete Jimenez /*
5440c7ad07eSAntonio Huete Jimenez  * Parse a variable definition line, of the form
5450c7ad07eSAntonio Huete Jimenez  *  NAME = VALUE
5460c7ad07eSAntonio Huete Jimenez  */
parse_varline(char * line,struct lesskey_tables * tables)547320d7c8aSAaron LI static void parse_varline(char *line, struct lesskey_tables *tables)
5480c7ad07eSAntonio Huete Jimenez {
549*e433da38SAaron LI 	constant char *s;
5500c7ad07eSAntonio Huete Jimenez 	char *p = line;
5510c7ad07eSAntonio Huete Jimenez 	char *eq;
5520c7ad07eSAntonio Huete Jimenez 
5530c7ad07eSAntonio Huete Jimenez 	eq = strchr(line, '=');
5540c7ad07eSAntonio Huete Jimenez 	if (eq != NULL && eq > line && eq[-1] == '+')
5550c7ad07eSAntonio Huete Jimenez 	{
5560c7ad07eSAntonio Huete Jimenez 		/*
5570c7ad07eSAntonio Huete Jimenez 		 * Rather ugly way of handling a += line.
5580c7ad07eSAntonio Huete Jimenez 		 * {{ Note that we ignore the variable name and
5590c7ad07eSAntonio Huete Jimenez 		 *    just append to the previously defined variable. }}
5600c7ad07eSAntonio Huete Jimenez 		 */
5610c7ad07eSAntonio Huete Jimenez 		erase_cmd_char(tables); /* backspace over the final null */
5620c7ad07eSAntonio Huete Jimenez 		p = eq+1;
5630c7ad07eSAntonio Huete Jimenez 	} else
5640c7ad07eSAntonio Huete Jimenez 	{
5650c7ad07eSAntonio Huete Jimenez 		do
5660c7ad07eSAntonio Huete Jimenez 		{
5670c7ad07eSAntonio Huete Jimenez 			s = tstr(&p, 0);
5680c7ad07eSAntonio Huete Jimenez 			add_cmd_str(s, tables);
5690c7ad07eSAntonio Huete Jimenez 		} while (*p != '\0' && !issp(*p) && *p != '=');
5700c7ad07eSAntonio Huete Jimenez 		/*
5710c7ad07eSAntonio Huete Jimenez 		 * Terminate the variable name with a null byte.
5720c7ad07eSAntonio Huete Jimenez 		 */
5730c7ad07eSAntonio Huete Jimenez 		add_cmd_char('\0', tables);
5740c7ad07eSAntonio Huete Jimenez 		p = skipsp(p);
5750c7ad07eSAntonio Huete Jimenez 		if (*p++ != '=')
5760c7ad07eSAntonio Huete Jimenez 		{
5770c7ad07eSAntonio Huete Jimenez 			parse_error("missing = in variable definition", "");
5780c7ad07eSAntonio Huete Jimenez 			return;
5790c7ad07eSAntonio Huete Jimenez 		}
5800c7ad07eSAntonio Huete Jimenez 		add_cmd_char(EV_OK|A_EXTRA, tables);
5810c7ad07eSAntonio Huete Jimenez 	}
5820c7ad07eSAntonio Huete Jimenez 	p = skipsp(p);
5830c7ad07eSAntonio Huete Jimenez 	while (*p != '\0')
5840c7ad07eSAntonio Huete Jimenez 	{
5850c7ad07eSAntonio Huete Jimenez 		s = tstr(&p, 0);
5860c7ad07eSAntonio Huete Jimenez 		add_cmd_str(s, tables);
5870c7ad07eSAntonio Huete Jimenez 	}
5880c7ad07eSAntonio Huete Jimenez 	add_cmd_char('\0', tables);
5890c7ad07eSAntonio Huete Jimenez }
5900c7ad07eSAntonio Huete Jimenez 
5910c7ad07eSAntonio Huete Jimenez /*
5920c7ad07eSAntonio Huete Jimenez  * Parse a line from the lesskey file.
5930c7ad07eSAntonio Huete Jimenez  */
parse_line(char * line,struct lesskey_tables * tables)594320d7c8aSAaron LI static void parse_line(char *line, struct lesskey_tables *tables)
5950c7ad07eSAntonio Huete Jimenez {
5960c7ad07eSAntonio Huete Jimenez 	char *p;
5970c7ad07eSAntonio Huete Jimenez 
5980c7ad07eSAntonio Huete Jimenez 	/*
5990c7ad07eSAntonio Huete Jimenez 	 * See if it is a control line.
6000c7ad07eSAntonio Huete Jimenez 	 */
6010c7ad07eSAntonio Huete Jimenez 	p = control_line(line, tables);
6020c7ad07eSAntonio Huete Jimenez 	if (p == NULL)
6030c7ad07eSAntonio Huete Jimenez 		return;
6040c7ad07eSAntonio Huete Jimenez 	/*
6050c7ad07eSAntonio Huete Jimenez 	 * Skip leading white space.
6060c7ad07eSAntonio Huete Jimenez 	 * Replace the final newline with a null byte.
6070c7ad07eSAntonio Huete Jimenez 	 * Ignore blank lines and comments.
6080c7ad07eSAntonio Huete Jimenez 	 */
6090c7ad07eSAntonio Huete Jimenez 	p = clean_line(p);
6100c7ad07eSAntonio Huete Jimenez 	if (*p == '\0')
6110c7ad07eSAntonio Huete Jimenez 		return;
6120c7ad07eSAntonio Huete Jimenez 
6130c7ad07eSAntonio Huete Jimenez 	if (tables->currtable->is_var)
6140c7ad07eSAntonio Huete Jimenez 		parse_varline(p, tables);
6150c7ad07eSAntonio Huete Jimenez 	else
6160c7ad07eSAntonio Huete Jimenez 		parse_cmdline(p, tables);
6170c7ad07eSAntonio Huete Jimenez }
6180c7ad07eSAntonio Huete Jimenez 
6190c7ad07eSAntonio Huete Jimenez /*
6200c7ad07eSAntonio Huete Jimenez  * Parse a lesskey source file and store result in tables.
6210c7ad07eSAntonio Huete Jimenez  */
parse_lesskey(constant char * infile,struct lesskey_tables * tables)622*e433da38SAaron LI int parse_lesskey(constant char *infile, struct lesskey_tables *tables)
6230c7ad07eSAntonio Huete Jimenez {
6240c7ad07eSAntonio Huete Jimenez 	FILE *desc;
6250c7ad07eSAntonio Huete Jimenez 	char line[1024];
6260c7ad07eSAntonio Huete Jimenez 
627*e433da38SAaron LI 	lesskey_file = (infile != NULL) ? strdup(infile) : homefile(DEF_LESSKEYINFILE);
628*e433da38SAaron LI 	if (lesskey_file == NULL)
629*e433da38SAaron LI 		return (-1);
6300c7ad07eSAntonio Huete Jimenez 
6310c7ad07eSAntonio Huete Jimenez 	init_tables(tables);
6320c7ad07eSAntonio Huete Jimenez 	errors = 0;
6330c7ad07eSAntonio Huete Jimenez 	linenum = 0;
6340c7ad07eSAntonio Huete Jimenez 	if (less_version == 0)
635320d7c8aSAaron LI 		less_version = lstrtoi(version, NULL, 10);
6360c7ad07eSAntonio Huete Jimenez 
6370c7ad07eSAntonio Huete Jimenez 	/*
6380c7ad07eSAntonio Huete Jimenez 	 * Open the input file.
6390c7ad07eSAntonio Huete Jimenez 	 */
640*e433da38SAaron LI 	if (strcmp(lesskey_file, "-") == 0)
6410c7ad07eSAntonio Huete Jimenez 		desc = stdin;
642*e433da38SAaron LI 	else if ((desc = fopen(lesskey_file, "r")) == NULL)
6430c7ad07eSAntonio Huete Jimenez 	{
644*e433da38SAaron LI 		/* parse_error("cannot open lesskey file %s", lesskey_file); */
645*e433da38SAaron LI 		errors = -1;
6460c7ad07eSAntonio Huete Jimenez 	}
6470c7ad07eSAntonio Huete Jimenez 
6480c7ad07eSAntonio Huete Jimenez 	/*
6490c7ad07eSAntonio Huete Jimenez 	 * Read and parse the input file, one line at a time.
6500c7ad07eSAntonio Huete Jimenez 	 */
651*e433da38SAaron LI 	if (desc != NULL)
652*e433da38SAaron LI 	{
6530c7ad07eSAntonio Huete Jimenez 		while (fgets(line, sizeof(line), desc) != NULL)
6540c7ad07eSAntonio Huete Jimenez 		{
6550c7ad07eSAntonio Huete Jimenez 			++linenum;
6560c7ad07eSAntonio Huete Jimenez 			parse_line(line, tables);
6570c7ad07eSAntonio Huete Jimenez 		}
658*e433da38SAaron LI 		if (desc != stdin)
6590c7ad07eSAntonio Huete Jimenez 			fclose(desc);
660*e433da38SAaron LI 	}
661*e433da38SAaron LI 	free(lesskey_file);
662*e433da38SAaron LI 	lesskey_file = NULL;
663*e433da38SAaron LI 	return (errors);
664*e433da38SAaron LI }
665*e433da38SAaron LI 
666*e433da38SAaron LI /*
667*e433da38SAaron LI  * Parse a lesskey source content and store result in tables.
668*e433da38SAaron LI  */
parse_lesskey_content(constant char * content,struct lesskey_tables * tables)669*e433da38SAaron LI int parse_lesskey_content(constant char *content, struct lesskey_tables *tables)
670*e433da38SAaron LI {
671*e433da38SAaron LI 	size_t cx = 0;
672*e433da38SAaron LI 
673*e433da38SAaron LI 	lesskey_file = "lesskey-content";
674*e433da38SAaron LI 	init_tables(tables);
675*e433da38SAaron LI 	errors = 0;
676*e433da38SAaron LI 	linenum = 0;
677*e433da38SAaron LI 	if (less_version == 0)
678*e433da38SAaron LI 		less_version = lstrtoi(version, NULL, 10);
679*e433da38SAaron LI 
680*e433da38SAaron LI 	while (content[cx] != '\0')
681*e433da38SAaron LI 	{
682*e433da38SAaron LI 		/* Extract a line from the content buffer and parse it. */
683*e433da38SAaron LI 		char line[1024];
684*e433da38SAaron LI 		size_t lx = 0;
685*e433da38SAaron LI 		while (content[cx] != '\0' && content[cx] != '\n' && content[cx] != ';')
686*e433da38SAaron LI 		{
687*e433da38SAaron LI 			if (lx >= sizeof(line)-1) break;
688*e433da38SAaron LI 			if (content[cx] == '\\' && content[cx+1] == ';')
689*e433da38SAaron LI 				++cx; /* escaped semicolon: skip the backslash */
690*e433da38SAaron LI 			line[lx++] = content[cx++];
691*e433da38SAaron LI 		}
692*e433da38SAaron LI 		line[lx] = '\0';
693*e433da38SAaron LI 		++linenum;
694*e433da38SAaron LI 		parse_line(line, tables);
695*e433da38SAaron LI 		if (content[cx] != '\0') ++cx; /* skip newline or semicolon */
696*e433da38SAaron LI 	}
697*e433da38SAaron LI 	lesskey_file = NULL;
6980c7ad07eSAntonio Huete Jimenez 	return (errors);
6990c7ad07eSAntonio Huete Jimenez }
700