Skip to content

Commit

Permalink
Add sysvar param to lesskey_src.
Browse files Browse the repository at this point in the history
Cleanup.
  • Loading branch information
gwsw committed Apr 13, 2021
1 parent a2137dc commit 6ce718a
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 35 deletions.
29 changes: 22 additions & 7 deletions decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,25 @@ init_cmds(VOID_PARAM)
#ifdef BINDIR
add_hometable(NULL, BINDIR "/.sysless", 1);
#endif
if (lesskey_src(NULL) != 0)
#ifdef LESSKEYSRCFILE_SYS
/*
* Try to add tables in system lesskey src file.
*/
(void) lesskey_src(LESSKEYSRCFILE_SYS, 1);
#endif
/*
* Try to add the tables in the system lesskey file.
*/
add_hometable("LESSKEY_SYSTEM", LESSKEYFILE_SYS, 1);
/*
* Try to add tables in the lesskey src file "$HOME/.lesskey".
* If it succeeds, don't load binary file.
* The binary file is likely to have been generated from
* a (possibly out of date) copy of the src file,
* so loading it is at best redundant.
*/
if (lesskey_src(NULL, 0) != 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".
*/
Expand Down Expand Up @@ -874,16 +887,18 @@ lesskey(filename, sysvar)
}

public int
lesskey_src(filename)
lesskey_src(filename, sysvar)
char *filename;
int sysvar;
{
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);
add_var_table(sysvar ? &list_sysvar_tables : &list_var_tables,
tables.vartable.buf.data, tables.vartable.buf.end);
return (0);
}

Expand Down
2 changes: 2 additions & 0 deletions lesskey.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ main(argc, argv)
return (1);
}

fprintf(stderr, "NOTE: lesskey is deprecated.\n It is no longer necessary to run lesskey,\n when using less version 582 and later.\n");

/*
* Write the output file.
* If no output file was specified, use "$HOME/.less"
Expand Down
1 change: 1 addition & 0 deletions lesskey.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct lesskey_table
{
struct lesskey_cmdname *names;
struct xbuffer buf;
int is_var;
};

struct lesskey_tables
Expand Down
85 changes: 58 additions & 27 deletions lesskey_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,22 @@ static struct lesskey_cmdname editnames[] =
{ NULL, 0 }
};

/*
* Print a parse error message.
*/
static void
parse_error(msg)
char *msg;
{
char buf[1024];
++errors;
snprintf(buf, sizeof(buf), "%s: line %d: %s", lesskey_file, linenum, msg);
lesskey_parse_error(buf);
}

/*
* Initialize an expandable text buffer.
*/
static void
xbuf_init(xbuf)
struct xbuffer *xbuf;
Expand All @@ -118,6 +134,9 @@ xbuf_init(xbuf)
xbuf->end = 0;
}

/*
* Add a char to an expandable text buffer.
*/
static void
xbuf_add(xbuf, ch)
struct xbuffer *xbuf;
Expand All @@ -134,18 +153,8 @@ xbuf_add(xbuf, ch)
xbuf->data[xbuf->end++] = ch;
}

static void
parse_error(msg)
char *msg;
{
char buf[1024];
++errors;
snprintf(buf, sizeof(buf), "%s, line %d: %s", lesskey_file, linenum, msg);
lesskey_parse_error(buf);
}

/*
* Initialize data structures.
* Initialize lesskey_tables.
*/
static void
init_tables(tables)
Expand All @@ -154,12 +163,15 @@ init_tables(tables)
tables->currtable = &tables->cmdtable;

tables->cmdtable.names = cmdnames;
tables->cmdtable.is_var = 0;
xbuf_init(&tables->cmdtable.buf);

tables->edittable.names = editnames;
tables->edittable.is_var = 0;
xbuf_init(&tables->edittable.buf);

tables->vartable.names = NULL;
tables->vartable.is_var = 1;
xbuf_init(&tables->vartable.buf);
}

Expand Down Expand Up @@ -279,14 +291,21 @@ tstr(pp, xlate)
return (buf);
}

static int
issp(ch)
char ch;
{
return (ch == ' ' || ch == '\t');
}

/*
* Skip leading spaces in a string.
*/
static char *
skipsp(s)
char *s;
{
while (*s == ' ' || *s == '\t')
while (issp(*s))
s++;
return (s);
}
Expand All @@ -298,7 +317,7 @@ skipsp(s)
skipnsp(s)
char *s;
{
while (*s != '\0' && *s != ' ' && *s != '\t')
while (*s != '\0' && !issp(*s))
s++;
return (s);
}
Expand All @@ -314,7 +333,7 @@ clean_line(s)
int i;

s = skipsp(s);
for (i = 0; s[i] != '\n' && s[i] != '\r' && s[i] != '\0'; i++)
for (i = 0; s[i] != '\0' && s[i] != '\n' && s[i] != '\r'; i++)
if (s[i] == '#' && (i == 0 || s[i-1] != '\\'))
break;
s[i] = '\0';
Expand Down Expand Up @@ -377,6 +396,7 @@ control_line(s, tables)
}
return (0);
}

/*
* Find an action, given the name of the action.
*/
Expand All @@ -394,6 +414,13 @@ findaction(actname, tables)
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(p, tables)
char *p;
Expand All @@ -414,10 +441,12 @@ parse_cmdline(p, tables)
s = tstr(&p, 1);
cmdlen += (int) strlen(s);
if (cmdlen > MAX_CMDLEN)
{
parse_error("command too long");
else
add_cmd_str(s, tables);
} while (*p != ' ' && *p != '\t' && *p != '\0');
break;
}
add_cmd_str(s, tables);
} while (*p != '\0' && !issp(*p));
/*
* Terminate the command string with a null byte.
*/
Expand Down Expand Up @@ -465,6 +494,10 @@ parse_cmdline(p, tables)
}
}

/*
* Parse a variable definition line, of the form
* NAME = VALUE
*/
static void
parse_varline(p, tables)
char *p;
Expand All @@ -476,7 +509,7 @@ parse_varline(p, tables)
{
s = tstr(&p, 0);
add_cmd_str(s, tables);
} while (*p != ' ' && *p != '\t' && *p != '=' && *p != '\0');
} while (*p != '\0' && !issp(*p) && *p != '=');
/*
* Terminate the variable name with a null byte.
*/
Expand Down Expand Up @@ -524,12 +557,15 @@ parse_line(line, tables)
if (*p == '\0')
return;

if (tables->currtable == &tables->vartable)
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(infile, tables)
char *infile;
Expand All @@ -543,6 +579,8 @@ parse_lesskey(infile, tables)
lesskey_file = infile;

init_tables(tables);
errors = 0;
linenum = 0;

/*
* Open the input file.
Expand All @@ -551,19 +589,13 @@ parse_lesskey(infile, tables)
desc = stdin;
else if ((desc = fopen(infile, "r")) == NULL)
{
#if HAVE_PERROR
perror(infile);
#else
fprintf(stderr, "Cannot open %s\n", infile);
#endif
parse_error("cannot open lesskey file");
return (-1);
}

/*
* Read and parse the input file, one line at a time.
*/
errors = 0;
linenum = 0;
while (fgets(line, sizeof(line), desc) != NULL)
{
++linenum;
Expand All @@ -572,4 +604,3 @@ parse_lesskey(infile, tables)

return (errors);
}

2 changes: 1 addition & 1 deletion optfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ opt_ks(type, s)
switch (type)
{
case INIT:
if (lesskey_src(s))
if (lesskey_src(s, 0))
{
parg.p_string = s;
error("Cannot use lesskey source file \"%s\"", &parg);
Expand Down

0 comments on commit 6ce718a

Please sign in to comment.