Skip to content

Commit

Permalink
Make less read lesskey source file rather than binary file.
Browse files Browse the repository at this point in the history
lesskey tool is no longer needed.
  • Loading branch information
gwsw committed Apr 13, 2021
1 parent abf688a commit a2137dc
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 57 deletions.
3 changes: 2 additions & 1 deletion Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ 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} \
help.${O} ifile.${O} input.${O} jump.${O} line.${O} linenum.${O} \
help.${O} ifile.${O} input.${O} jump.${O} \
lesskey_parse.${O} line.${O} linenum.${O} \
lsystem.${O} mark.${O} optfunc.${O} option.${O} opttbl.${O} os.${O} \
output.${O} pattern.${O} position.${O} prompt.${O} search.${O} signal.${O} \
tags.${O} ttyin.${O} version.${O} @REGEX_O@
Expand Down
2 changes: 2 additions & 0 deletions NEWS.VER
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

* Add the --use-color and --color options.

* Make less read lesskey source file, and add the --lesskey-src option.

* Display -w highlight even if highlighted line is empty.

* If search result is in a long line, scroll to ensure it is visible.
Expand Down
42 changes: 34 additions & 8 deletions decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,14 +339,17 @@ init_cmds(VOID_PARAM)
#ifdef BINDIR
add_hometable(NULL, BINDIR "/.sysless", 1);
#endif
/*
* Try to add the tables in the system lesskey file.
*/
add_hometable("LESSKEY_SYSTEM", LESSKEYFILE_SYS, 1);
/*
* Try to add the tables in the standard lesskey file "$HOME/.less".
*/
add_hometable("LESSKEY", LESSKEYFILE, 0);
if (lesskey_src(NULL) != 0)
{
/*
* Try to add the tables in the system lesskey file.
*/
add_hometable("LESSKEY_SYSTEM", LESSKEYFILE_SYS, 1);
/*
* Try to add the tables in the standard lesskey file "$HOME/.less".
*/
add_hometable("LESSKEY", LESSKEYFILE, 0);
}
#endif
}

Expand Down Expand Up @@ -870,6 +873,29 @@ lesskey(filename, sysvar)
return (new_lesskey(buf, (int)len, sysvar));
}

public int
lesskey_src(filename)
char *filename;
{
static struct lesskey_tables tables;
int r = parse_lesskey(filename, &tables);
if (r != 0)
return (r);
add_fcmd_table(tables.cmdtable.buf.data, tables.cmdtable.buf.end);
add_ecmd_table(tables.edittable.buf.data, tables.edittable.buf.end);
add_var_table(&list_var_tables, tables.vartable.buf.data, tables.vartable.buf.end);
return (0);
}

void
lesskey_parse_error(s)
char *s;
{
PARG parg;
parg.p_string = s;
error("%s", &parg);
}

/*
* Add the standard lesskey file "$HOME/.less"
*/
Expand Down
17 changes: 17 additions & 0 deletions less.nro.VER
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,23 @@ if a lesskey file is found in a standard place (see KEY BINDINGS),
it is also used as a
.I lesskey
file.
.IP "\-\-lesskey-src=\fIfilename\fP"
Causes
.I less
to open and interpret the named file as a
.IR lesskey (1)
source file. The
.I lesskey
source file is the file which was used as the input file to the
.I lesskey
program in versions of less prior to v582.
Current versions of
.I less
(v582 and later) do not require the
.I lesskey
program to be run, and instead interpret the
.I lesskey
source file directly.
.IP "\-K or \-\-quit-on-intr"
Causes
.I less
Expand Down
41 changes: 39 additions & 2 deletions lesskey.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ char endsection[1] = { END_SECTION };
char *infile = NULL;
char *outfile = NULL ;

extern char *homefile(char *filename);
extern char version[];

static void
Expand All @@ -116,6 +115,44 @@ lesskey_parse_error(s)
fprintf(stderr, "%s\n", s);
}

static char *
mkpathname(dirname, filename)
char *dirname;
char *filename;
{
char *pathname;

pathname = calloc(strlen(dirname) + strlen(filename) + 2, sizeof(char));
strcpy(pathname, dirname);
strcat(pathname, PATHNAME_SEP);
strcat(pathname, filename);
return (pathname);
}

/*
* Figure out the name of a default file (in the user's HOME directory).
*/
char *
homefile(filename)
char *filename;
{
char *p;
char *pathname;

if ((p = getenv("HOME")) != NULL && *p != '\0')
pathname = mkpathname(p, filename);
#if OS2
else if ((p = getenv("INIT")) != NULL && *p != '\0')
pathname = mkpathname(p, filename);
#endif
else
{
fprintf(stderr, "cannot find $HOME - using current directory\n");
pathname = mkpathname(".", filename);
}
return (pathname);
}

/*
* Parse command line arguments.
*/
Expand Down Expand Up @@ -261,7 +298,7 @@ main(argc, argv)
* Process command line arguments.
*/
parse_args(argc, argv);
errors = parse_lesskey(NULL, &tables);
errors = parse_lesskey(infile, &tables);
if (errors)
{
fprintf(stderr, "%d errors; no output produced\n", errors);
Expand Down
17 changes: 15 additions & 2 deletions lesskey.nro.VER
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'\" t
.TH LESSKEY 1 "Version @@VERSION@@: @@DATE@@"
.SH NAME
lesskey \- specify key bindings for less
lesskey \- specify key bindings for less (deprecated)
.SH SYNOPSIS
.B "lesskey [\-o output] [\-\-] [input]"
.br
Expand All @@ -10,6 +10,19 @@ lesskey \- specify key bindings for less
.B "lesskey \-V"
.br
.B "lesskey \-\-version"
.SH NOTE
.I less
version 582 and later no longer requires
.I lesskey
to be used. Instead, the source (non-binary)
.I lesskey
file is read directly by
.IR less ,
and any binary file produced by
.I lesskey
is ignored.
This document describes the format of the source file.
.PP
.SH DESCRIPTION
.I Lesskey
is used to specify a set of key bindings to be used by
Expand Down Expand Up @@ -41,7 +54,7 @@ The \-V or \-\-version option causes
.I lesskey
to print its version number and immediately exit.
If \-V or \-\-version is present, other options and arguments are ignored.
.PP
.SH SOURCE FILE
The input file consists of one or more
.I sections.
Each section starts with a line that identifies the type of section.
Expand Down
50 changes: 7 additions & 43 deletions lesskey_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
#define ESC CONTROL('[')

extern void lesskey_parse_error(char *s);
extern char *homefile(char *filename);

static int linenum;
static int errors;
static char *lesskey_file;

static struct lesskey_cmdname cmdnames[] =
{
Expand Down Expand Up @@ -107,7 +109,7 @@ static struct lesskey_cmdname editnames[] =
{ NULL, 0 }
};

void
static void
xbuf_init(xbuf)
struct xbuffer *xbuf;
{
Expand All @@ -116,7 +118,7 @@ xbuf_init(xbuf)
xbuf->end = 0;
}

void
static void
xbuf_add(xbuf, ch)
struct xbuffer *xbuf;
char ch;
Expand All @@ -136,50 +138,12 @@ xbuf_add(xbuf, ch)
parse_error(msg)
char *msg;
{
char buf[128];
char buf[1024];
++errors;
snprintf(buf, sizeof(buf), "line %d: %s", linenum, msg);
snprintf(buf, sizeof(buf), "%s, line %d: %s", lesskey_file, linenum, msg);
lesskey_parse_error(buf);
}

static char *
mkpathname(dirname, filename)
char *dirname;
char *filename;
{
char *pathname;

pathname = calloc(strlen(dirname) + strlen(filename) + 2, sizeof(char));
strcpy(pathname, dirname);
strcat(pathname, PATHNAME_SEP);
strcat(pathname, filename);
return (pathname);
}

/*
* Figure out the name of a default file (in the user's HOME directory).
*/
char *
homefile(filename)
char *filename;
{
char *p;
char *pathname;

if ((p = getenv("HOME")) != NULL && *p != '\0')
pathname = mkpathname(p, filename);
#if OS2
else if ((p = getenv("INIT")) != NULL && *p != '\0')
pathname = mkpathname(p, filename);
#endif
else
{
fprintf(stderr, "cannot find $HOME - using current directory\n");
pathname = mkpathname(".", filename);
}
return (pathname);
}

/*
* Initialize data structures.
*/
Expand Down Expand Up @@ -573,10 +537,10 @@ parse_lesskey(infile, tables)
{
FILE *desc;
char line[1024];
int linenum;

if (infile == NULL)
infile = homefile(DEF_LESSKEYINFILE);
lesskey_file = infile;

init_tables(tables);

Expand Down
19 changes: 19 additions & 0 deletions optfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,25 @@ opt_k(type, s)
break;
}
}

public void
opt_ks(type, s)
int type;
char *s;
{
PARG parg;

switch (type)
{
case INIT:
if (lesskey_src(s))
{
parg.p_string = s;
error("Cannot use lesskey source file \"%s\"", &parg);
}
break;
}
}
#endif

#if TAGS
Expand Down
5 changes: 5 additions & 0 deletions opttbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ static struct optname j_optname = { "jump-target", NULL };
static struct optname J__optname = { "status-column", NULL };
#if USERFILE
static struct optname k_optname = { "lesskey-file", NULL };
static struct optname ks_optname = { "lesskey-src", NULL };
#endif
static struct optname K__optname = { "quit-on-intr", NULL };
static struct optname L__optname = { "no-lessopen", NULL };
Expand Down Expand Up @@ -267,6 +268,10 @@ static struct loption option[] =
STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_k,
{ NULL, NULL, NULL }
},
{ OLETTER_NONE, &ks_optname,
STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_ks,
{ NULL, NULL, NULL }
},
#endif
{ 'K', &K__optname,
BOOL, OPT_OFF, &quit_on_intr, NULL,
Expand Down
3 changes: 2 additions & 1 deletion version.c
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,8 @@ v578 2/9/21 Doc
v579 2/14/21 Fix double-width char bugs and non-match search crash.
v580 3/2/21 Some color fixes; fix compiler warnings; some lesstest support.
v581 4/6/21 Ignore SIGTSTP in secure mode; don't print "skipping" when filtering.
v582 Fix bug in finding tags with backslashes.
v582 Less now reads lesskey source file rather than binary;
fix bug in finding tags with backslashes.
*/

char version[] = "582x";

0 comments on commit a2137dc

Please sign in to comment.