Skip to content

Commit

Permalink
Add --lesskey-content option.
Browse files Browse the repository at this point in the history
  • Loading branch information
gwsw committed Oct 27, 2023
1 parent b431d91 commit 3006aec
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 3 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

* Add --match-shift option.

* Add --lesskey-content option (github #447).

* Add LESSSECURE_ALLOW environment variable (github #449).

* Mouse right-click jumps to position marked by left-click (github #390).
Expand Down
14 changes: 12 additions & 2 deletions decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -876,12 +876,12 @@ public int lesskey(char *filename, int sysvar)
}

#if HAVE_LESSKEYSRC
public int lesskey_src(char *filename, int sysvar)
static int lesskey_text(char *filename, int sysvar, int content)
{
static struct lesskey_tables tables;
if (!secure_allow(SF_LESSKEY))
return (1);
int r = parse_lesskey(filename, &tables);
int r = content ? parse_lesskey_content(filename, &tables) : parse_lesskey(filename, &tables);
if (r != 0)
return (r);
add_fcmd_table(xbuf_char_data(&tables.cmdtable.buf), tables.cmdtable.buf.end);
Expand All @@ -891,6 +891,16 @@ public int lesskey_src(char *filename, int sysvar)
return (0);
}

public int lesskey_src(char *filename, int sysvar)
{
return lesskey_text(filename, sysvar, FALSE);
}

public int lesskey_content(char *content, int sysvar)
{
return lesskey_text(content, sysvar, TRUE);
}

void lesskey_parse_error(char *s)
{
PARG parg;
Expand Down
8 changes: 8 additions & 0 deletions less.nro.VER
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,14 @@ Newer versions of
read the
.I "lesskey source"
file directly and ignore the binary file if the source file exists.
.IP "\-\-lesskey-content=\fItext\fP"
Causes less to interpret the specified text as the contents of a
.BR lesskey (1)
source file.
In the text,
.B lesskey
lines may be separated by either newlines as usual, or by semicolons.
A literal semicolon may be represented by a backslash followed by a semicolon.
.IP "\-K or \-\-quit-on-intr"
Causes
.B less
Expand Down
1 change: 1 addition & 0 deletions lesskey.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ struct lesskey_tables
};

extern int parse_lesskey(char *infile, struct lesskey_tables *tables);
extern int parse_lesskey_content(char *content, struct lesskey_tables *tables);

/* keep in sync with less.h */
#if HAVE_SNPRINTF
Expand Down
33 changes: 33 additions & 0 deletions lesskey_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -649,3 +649,36 @@ int parse_lesskey(char *infile, struct lesskey_tables *tables)
fclose(desc);
return (errors);
}

/*
* Parse a lesskey source content and store result in tables.
*/
int parse_lesskey_content(char *content, struct lesskey_tables *tables)
{
int cx = 0;

init_tables(tables);
errors = 0;
linenum = 0;
if (less_version == 0)
less_version = lstrtoi(version, NULL, 10);

while (content[cx] != '\0')
{
/* Extract a line from the content buffer and parse it. */
char line[1024];
int lx = 0;
while (content[cx] != '\0' && content[cx] != '\n' && content[cx] != ';')
{
if (lx >= sizeof(line)-1) break;
if (content[cx] == '\\' && content[cx+1] == ';')
++cx; /* escaped semicolon: skip the backslash */
line[lx++] = content[cx++];
}
line[lx] = '\0';
++linenum;
parse_line(line, tables);
if (content[cx] != '\0') ++cx; /* skip newline or semicolon */
}
return (errors);
}
14 changes: 14 additions & 0 deletions optfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,20 @@ public void opt_ks(int type, char *s)
break;
}
}

public void opt_kc(int type, char *s)
{
switch (type)
{
case INIT:
if (lesskey_content(s, 0))
{
error("Error in lesskey content", NULL_PARG);
}
break;
}
}

#endif /* HAVE_LESSKEYSRC */
#endif /* USERFILE */

Expand Down
5 changes: 5 additions & 0 deletions opttbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ static struct optname J__optname = { "status-column", NULL };
static struct optname k_optname = { "lesskey-file", NULL };
#if HAVE_LESSKEYSRC
static struct optname ks_optname = { "lesskey-src", NULL };
static struct optname kc_optname = { "lesskey-content", NULL };
#endif /* HAVE_LESSKEYSRC */
#endif
static struct optname K__optname = { "quit-on-intr", NULL };
Expand Down Expand Up @@ -313,6 +314,10 @@ static struct loption option[] =
{ NULL, NULL, NULL }
},
#if HAVE_LESSKEYSRC
{ OLETTER_NONE, &kc_optname,
STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_kc,
{ NULL, NULL, NULL }
},
{ OLETTER_NONE, &ks_optname,
STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_ks,
{ NULL, NULL, NULL }
Expand Down
3 changes: 2 additions & 1 deletion version.c
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,8 @@ v644 9/16/23 Improve ^C on non-terminated pipe; fix crash when files are
fix # bug; don't filter header lines; fix shifting long lines;
add --match-shift.
v645 Default Windows charset is utf-8; update Unicode tables;
fix ESC-} bug; mouse right-click jumps to '#'; add LESSSECURE_ALLOW.
fix ESC-} bug; mouse right-click jumps to '#';
add LESSSECURE_ALLOW; add --lesskey-content.
*/

char version[] = "645x";

0 comments on commit 3006aec

Please sign in to comment.