/*
* Copyright (C) 1984-2023 Mark Nudelman
*
* You may distribute under the terms of either the GNU General Public
* License or the Less License, as specified in the README file.
*
* For more information, see the README file.
*/
/*
* Add a string to the output command table.
*/
static void add_cmd_str(char *s, struct lesskey_tables *tables)
{
for ( ; *s != '\0'; s++)
add_cmd_char(*s, tables);
}
/*
* Does a given version number match the running version?
* Operator compares the running version to the given version.
*/
static int match_version(char op, int ver)
{
switch (op)
{
case '>': return less_version > ver;
case '<': return less_version < ver;
case '+': return less_version >= ver;
case '-': return less_version <= ver;
case '=': return less_version == ver;
case '!': return less_version != ver;
default: return 0; /* cannot happen */
}
}
/*
* Handle a #version line.
* If the version matches, return the part of the line that should be executed.
* Otherwise, return NULL.
*/
static char * version_line(char *s, struct lesskey_tables *tables)
{
char op;
int ver;
char *e;
char buf[CHAR_STRING_LEN];
s += strlen("#version");
s = skipsp(s);
op = *s++;
/* Simplify 2-char op to one char. */
switch (op)
{
case '<': if (*s == '=') { s++; op = '-'; } break;
case '>': if (*s == '=') { s++; op = '+'; } break;
case '=': if (*s == '=') { s++; } break;
case '!': if (*s == '=') { s++; } break;
default:
parse_error("invalid operator '%s' in #version line", char_string(buf, op, 0));
return (NULL);
}
s = skipsp(s);
ver = lstrtoi(s, &e, 10);
if (e == s)
{
parse_error("non-numeric version number in #version line", "");
return (NULL);
}
if (!match_version(op, ver))
return (NULL);
return (e);
}
/*
* See if we have a special "control" line.
*/
static char * control_line(char *s, struct lesskey_tables *tables)
{
#define PREFIX(str,pat) (strncmp(str,pat,strlen(pat)) == 0)
/*
* Find an action, given the name of the action.
*/
static int findaction(char *actname, struct lesskey_tables *tables)
{
int i;
for (i = 0; tables->currtable->names[i].cn_name != NULL; i++)
if (strcmp(tables->currtable->names[i].cn_name, actname) == 0)
return (tables->currtable->names[i].cn_action);
parse_error("unknown action: \"%s\"", actname);
return (A_INVALID);
}
/*
* Parse a line describing one key binding, of the form
* KEY ACTION [EXTRA]
* where KEY is the user key sequence, ACTION is the
* resulting less action, and EXTRA is an "extra" user
* key sequence injected after the action.
*/
static void parse_cmdline(char *p, struct lesskey_tables *tables)
{
char *actname;
int action;
char *s;
char c;
/*
* Parse the command string and store it in the current table.
*/
do
{
s = tstr(&p, 1);
add_cmd_str(s, tables);
} while (*p != '\0' && !issp(*p));
/*
* Terminate the command string with a null byte.
*/
add_cmd_char('\0', tables);
/*
* Skip white space between the command string
* and the action name.
* Terminate the action name with a null byte.
*/
p = skipsp(p);
if (*p == '\0')
{
parse_error("missing action", "");
return;
}
actname = p;
p = skipnsp(p);
c = *p;
*p = '\0';
/*
* Parse the action name and store it in the current table.
*/
action = findaction(actname, tables);
/*
* See if an extra string follows the action name.
*/
*p = c;
p = skipsp(p);
if (*p == '\0')
{
add_cmd_char((unsigned char) action, tables);
} else
{
/*
* OR the special value A_EXTRA into the action byte.
* Put the extra string after the action byte.
*/
add_cmd_char((unsigned char) (action | A_EXTRA), tables);
while (*p != '\0')
add_cmd_str(tstr(&p, 0), tables);
add_cmd_char('\0', tables);
}
}
/*
* Parse a variable definition line, of the form
* NAME = VALUE
*/
static void parse_varline(char *line, struct lesskey_tables *tables)
{
char *s;
char *p = line;
char *eq;
eq = strchr(line, '=');
if (eq != NULL && eq > line && eq[-1] == '+')
{
/*
* Rather ugly way of handling a += line.
* {{ Note that we ignore the variable name and
* just append to the previously defined variable. }}
*/
erase_cmd_char(tables); /* backspace over the final null */
p = eq+1;
} else
{
do
{
s = tstr(&p, 0);
add_cmd_str(s, tables);
} while (*p != '\0' && !issp(*p) && *p != '=');
/*
* Terminate the variable name with a null byte.
*/
add_cmd_char('\0', tables);
p = skipsp(p);
if (*p++ != '=')
{
parse_error("missing = in variable definition", "");
return;
}
add_cmd_char(EV_OK|A_EXTRA, tables);
}
p = skipsp(p);
while (*p != '\0')
{
s = tstr(&p, 0);
add_cmd_str(s, tables);
}
add_cmd_char('\0', tables);
}
/*
* Parse a line from the lesskey file.
*/
static void parse_line(char *line, struct lesskey_tables *tables)
{
char *p;
/*
* See if it is a control line.
*/
p = control_line(line, tables);
if (p == NULL)
return;
/*
* Skip leading white space.
* Replace the final newline with a null byte.
* Ignore blank lines and comments.
*/
p = clean_line(p);
if (*p == '\0')
return;
if (tables->currtable->is_var)
parse_varline(p, tables);
else
parse_cmdline(p, tables);
}
/*
* Parse a lesskey source file and store result in tables.
*/
int parse_lesskey(char *infile, struct lesskey_tables *tables)
{
FILE *desc;
char line[1024];
if (infile == NULL)
infile = homefile(DEF_LESSKEYINFILE);
lesskey_file = infile;
/*
* Open the input file.
*/
if (strcmp(infile, "-") == 0)
desc = stdin;
else if ((desc = fopen(infile, "r")) == NULL)
{
/* parse_error("cannot open lesskey file %s", infile); */
return (-1);
}
/*
* Read and parse the input file, one line at a time.
*/
while (fgets(line, sizeof(line), desc) != NULL)
{
++linenum;
parse_line(line, tables);
}
fclose(desc);
return (errors);
}