Skip to content

Commit

Permalink
Allow expansion of environment variables in lesskey files,
Browse files Browse the repository at this point in the history
using the syntax ${NAME} and ${NAME/STRING/REPL}.
Change lesskey parsing so that definitions override earlier definitions.
  • Loading branch information
gwsw committed Nov 2, 2023
1 parent f5702b3 commit 09377f7
Show file tree
Hide file tree
Showing 9 changed files with 337 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Makefile.aut
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ endif

SRC = \
main.c screen.c brac.c ch.c charset.c cmdbuf.c \
command.c cvt.c decode.c edit.c filename.c forwback.c \
command.c cvt.c decode.c edit.c evar.c filename.c forwback.c \
help.c ifile.c input.c jump.c line.c linenum.c \
lsystem.c mark.c optfunc.c option.c opttbl.c os.c \
output.c pattern.c position.c prompt.c search.c signal.c \
Expand Down
2 changes: 1 addition & 1 deletion Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ SHELL = /bin/sh

OBJ = \
main.${O} screen.${O} brac.${O} ch.${O} charset.${O} cmdbuf.${O} \
command.${O} cvt.${O} decode.${O} edit.${O} filename.${O} forwback.${O} \
command.${O} cvt.${O} decode.${O} edit.${O} evar.${O} filename.${O} forwback.${O} \
help.${O} ifile.${O} input.${O} jump.${O} \
line.${O} linenum.${O} \
lsystem.${O} mark.${O} optfunc.${O} option.${O} opttbl.${O} os.${O} \
Expand Down
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

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

* Add LESSKEY_CONTENT environment variable.

* Allow expansion of environment variables in lesskey files.

* Add LESSSECURE_ALLOW environment variable (github #449).

* Mouse right-click jumps to position marked by left-click (github #390).
Expand Down
122 changes: 104 additions & 18 deletions decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ extern int erase_char, erase2_char, kill_char;
extern int mousecap;
extern int sc_height;

static void add_content_table(int (*call_lesskey)(char *, int), char *envname, int sysvar);

#define SK(k) \
SK_SPECIAL_KEY, (k), 6, 1, 1, 1
/*
Expand Down Expand Up @@ -319,7 +321,6 @@ public void expand_cmd_tables(void)
expand_cmd_table(list_sysvar_tables);
}


/*
* Initialize the command lists.
*/
Expand Down Expand Up @@ -367,6 +368,9 @@ public void init_cmds(void)
add_hometable(lesskey, "LESSKEY", LESSKEYFILE, 0);
}
#endif

add_content_table(lesskey_content, "LESSKEY_CONTENT_SYSTEM", 1);
add_content_table(lesskey_content, "LESSKEY_CONTENT", 0);
}

/*
Expand All @@ -389,11 +393,42 @@ static int add_cmd_table(struct tablelist **tlist, char *buf, int len)
}
t->t_start = buf;
t->t_end = buf + len;
t->t_next = *tlist;
*tlist = t;
t->t_next = NULL;
if (*tlist == NULL)
*tlist = t;
else
{
struct tablelist *e;
for (e = *tlist; e->t_next != NULL; e = e->t_next)
continue;
e->t_next = t;
}
return (0);
}

/*
* Remove the last command table in a list.
*/
static void pop_cmd_table(struct tablelist **tlist)
{
struct tablelist *t;
if (*tlist == NULL)
return;
if ((*tlist)->t_next == NULL)
{
t = *tlist;
*tlist = NULL;
} else
{
struct tablelist *e;
for (e = *tlist; e->t_next->t_next != NULL; e = e->t_next)
continue;
t = e->t_next;
e->t_next = NULL;
}
free(t);
}

/*
* Add a command table.
*/
Expand All @@ -417,7 +452,11 @@ public void add_ecmd_table(char *buf, int len)
*/
static void add_var_table(struct tablelist **tlist, char *buf, int len)
{
if (add_cmd_table(tlist, buf, len) < 0)
struct xbuffer xbuf;

xbuf_init(&xbuf);
expand_evars(buf, len, &xbuf);
if (add_cmd_table(tlist, xbuf_char_data(&xbuf), xbuf.end) < 0)
error("Warning: environment variables from lesskey file unavailable", NULL_PARG);
}

Expand Down Expand Up @@ -566,7 +605,7 @@ static int cmd_search(char *cmd, char *table, char *endtable, char **sp)
{
char *p;
char *q;
int a;
int a = A_INVALID;

*sp = NULL;
for (p = table, q = cmd; p < endtable; p++, q++)
Expand Down Expand Up @@ -601,13 +640,15 @@ static int cmd_search(char *cmd, char *table, char *endtable, char **sp)
if (a & A_EXTRA)
{
*sp = ++p;
while (*p != '\0')
++p;
a &= ~A_EXTRA;
}
if (a == A_X11MOUSE_IN)
a = x11mouse_action(0);
else if (a == A_X116MOUSE_IN)
a = x116mouse_action(0);
return (a);
q = cmd-1;
}
} else if (*q == '\0')
{
Expand Down Expand Up @@ -643,10 +684,7 @@ static int cmd_search(char *cmd, char *table, char *endtable, char **sp)
q = cmd-1;
}
}
/*
* No match found in the entire command table.
*/
return (A_INVALID);
return (a);
}

/*
Expand All @@ -659,17 +697,22 @@ static int cmd_decode(struct tablelist *tlist, char *cmd, char **sp)
int action = A_INVALID;

/*
* Search thru all the command tables.
* Stop when we find an action which is not A_INVALID.
* Search for the cmd thru all the command tables.
* If we find it more than once, take the last one.
*/
*sp = NULL;
for (t = tlist; t != NULL; t = t->t_next)
{
action = cmd_search(cmd, t->t_start, t->t_end, sp);
if (action != A_INVALID)
break;
char *tsp;
int taction = cmd_search(cmd, t->t_start, t->t_end, &tsp);
if (taction == A_UINVALID)
taction = A_INVALID;
if (taction != A_INVALID)
{
action = taction;
*sp = tsp;
}
}
if (action == A_UINVALID)
action = A_INVALID;
return (action);
}

Expand Down Expand Up @@ -710,6 +753,38 @@ public char * lgetenv(char *var)
return (NULL);
}

/*
* Like lgetenv, but also uses a buffer partially filled with an env table.
*/
public char * lgetenv_ext(char *var, char *env_buf, int env_buf_len)
{
char *r;
int e;
int env_end = 0;

for (e = 0;;)
{
for (; e < env_buf_len; e++)
if (env_buf[e] == '\0')
break;
if (e >= env_buf_len) break;
if (env_buf[++e] & A_EXTRA)
{
for (e = e+1; e < env_buf_len; e++)
if (env_buf[e] == '\0')
break;
}
e++;
if (e >= env_buf_len) break;
env_end = e;
}
/* Temporarily add env_buf to var_tables, do the lookup, then remove it. */
add_var_table(&list_var_tables, env_buf, env_end);
r = lgetenv(var);
pop_cmd_table(&list_var_tables);
return r;
}

/*
* Is a string null or empty?
*/
Expand Down Expand Up @@ -946,7 +1021,18 @@ public int add_hometable(int (*call_lesskey)(char *, int), char *envname, char *
free(filename);
return (r);
}
#endif

/*
* Add the content of a lesskey source file.
*/
static void add_content_table(int (*call_lesskey)(char *, int), char *envname, int sysvar)
{
char *content = lgetenv(envname);
if (isnullenv(content))
return;
lesskey_content(content, sysvar);
}
#endif /* USERFILE */

/*
* See if a char is a special line-editing command.
Expand Down
Loading

0 comments on commit 09377f7

Please sign in to comment.