Skip to content

Commit

Permalink
Add #version to lesskey.
Browse files Browse the repository at this point in the history
  • Loading branch information
gwsw committed Sep 6, 2021
1 parent 93fee11 commit dc1a097
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 11 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

* Add the --redraw-on-quit option.

* Add #version conditional to lesskey.

* Fix display of multibyte and double-width chars in prompt.

* Fix ESC-BACKSPACE command when BACKSPACE key does not send 0x08.
Expand Down
8 changes: 8 additions & 0 deletions lesskey.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ lesskey_parse_error(s)
fprintf(stderr, "%s\n", s);
}

int
lstrtoi(buf, ebuf)
char *buf;
char **ebuf;
{
return (int) strtol(buf, ebuf, 10);
}

void *
ecalloc(count, size)
int count;
Expand Down
38 changes: 36 additions & 2 deletions lesskey.nro.VER
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ source file into a format understood by
.IR less .
This compilation step is no longer required and the
.I lesskey
program is therefore deprecated although the file format remains supported by
program is therefore deprecated, although the file format remains supported by
.I less
itself.
.PP
Expand All @@ -43,7 +43,7 @@ Defines new line-editing keys.
Defines environment variables.
.PP
Blank lines and lines which start with a pound sign (#) are ignored,
except for the special section header lines.
except as noted below.
.
.SH "COMMAND SECTION"
The command section begins with the line
Expand Down Expand Up @@ -355,6 +355,40 @@ is run, and specifies the character set to be "latin1":
.fi
.sp
.
.SH CONDITIONAL CONFIGURATION
If a line begins with #version followed by a relational operator and a version number,
the remainder of the line is parsed if and only if the running version of
.I less
(or
.IR lesskey )
matches the operator.
This can be helpful if a lesskey file is used by different versions of
.IR less .
For example, suppose that a new command named 'sideways-search' is added in version 777.
Then the following line would assign the command to the Q key, but only in versions of
.I less
which support it. The line would be ignored in earlier versions.
.sp
.nf
#version >= 777 Q sideways-search
.fi
.sp
These six operators are supported:
.RS 5m
.TS
l l.
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
= Equal to
<> Not equal to
.TE
.RE
.sp
In version 594 and earlier, all #version lines are ignored,
regardless of the result of the relational operator.
.
.SH "SEE ALSO"
.BR less (1)
.
Expand Down
92 changes: 84 additions & 8 deletions lesskey_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
extern void lesskey_parse_error(char *msg);
extern char *homefile(char *filename);
extern void *ecalloc(int count, unsigned int size);
extern int lstrtoi(char *str, char **end);
extern char version[];

static int linenum;
static int errors;
static int less_version = 0;
static char *lesskey_file;

static struct lesskey_cmdname cmdnames[] =
Expand Down Expand Up @@ -353,9 +356,75 @@ add_cmd_str(s, tables)
}

/*
* See if we have a special "control" line.
* Does a given version number match the running version?
* Operator compares the running version to the given version.
*/
static int
match_version(op, ver)
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: {
char sop[2] = { op, '\0' };
parse_error("invalid operator in #version: ", sop);
return 0; }
}
}

/*
* 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(s, tables)
char *s;
struct lesskey_tables *tables;
{
char op;
int ver;
char *e;

s += strlen("#version");
s = skipsp(s);
op = *s++;
if (op == '<' && *s == '=')
{
op = '-';
s++;
} else if (op == '>' && *s == '=')
{
op = '+';
s++;
} else if (op == '<' && *s == '>')
{
op = '!';
s++;
}
s = skipsp(s);
ver = lstrtoi(s, &e);
if (e == s)
{
parse_error("invalid version number in #version: ", s);
return (NULL);
}
if (!match_version(op, ver))
return (NULL);
return (e);
}

/*
* See if we have a special "control" line.
*/
static char *
control_line(s, tables)
char *s;
struct lesskey_tables *tables;
Expand All @@ -365,25 +434,29 @@ control_line(s, tables)
if (PREFIX(s, "#line-edit"))
{
tables->currtable = &tables->edittable;
return (1);
return (NULL);
}
if (PREFIX(s, "#command"))
{
tables->currtable = &tables->cmdtable;
return (1);
return (NULL);
}
if (PREFIX(s, "#env"))
{
tables->currtable = &tables->vartable;
return (1);
return (NULL);
}
if (PREFIX(s, "#stop"))
{
add_cmd_char('\0', tables);
add_cmd_char(A_END_LIST, tables);
return (1);
return (NULL);
}
if (PREFIX(s, "#version"))
{
return (version_line(s, tables));
}
return (0);
return (s);
}

/*
Expand Down Expand Up @@ -528,14 +601,15 @@ parse_line(line, tables)
/*
* See if it is a control line.
*/
if (control_line(line, tables))
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(line);
p = clean_line(p);
if (*p == '\0')
return;

Expand Down Expand Up @@ -563,6 +637,8 @@ parse_lesskey(infile, tables)
init_tables(tables);
errors = 0;
linenum = 0;
if (less_version == 0)
less_version = lstrtoi(version, NULL);

/*
* Open the input file.
Expand Down
3 changes: 2 additions & 1 deletion version.c
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,8 @@ v591 8/8/21 Use \kB for backspace key in lesskey; add more \k codes;
handle multibyte chars in prompt.
v592 8/24/21 Add --status-line option; limit use of /proc kludge; add --header.
v593 8/30/21 Add header columns, --no-number-headers.
v594 Let regex library handle caseless; add --redraw-on-quit option.
v594 Let regex library handle caseless; add --redraw-on-quit option;
add #version to lesskey.
*/

char version[] = "594x";

0 comments on commit dc1a097

Please sign in to comment.