Changeset View
Changeset View
Standalone View
Standalone View
head/contrib/less/decode.c
Show First 20 Lines • Show All 223 Lines • ▼ Show 20 Lines | |||||
static struct tablelist *list_var_tables = NULL; | static struct tablelist *list_var_tables = NULL; | ||||
static struct tablelist *list_sysvar_tables = NULL; | static struct tablelist *list_sysvar_tables = NULL; | ||||
/* | /* | ||||
* Expand special key abbreviations in a command table. | * Expand special key abbreviations in a command table. | ||||
*/ | */ | ||||
static void | static void | ||||
expand_special_keys(table, len) | expand_special_keys(char *table, int len) | ||||
char *table; | |||||
int len; | |||||
{ | { | ||||
register char *fm; | char *fm; | ||||
register char *to; | char *to; | ||||
register int a; | int a; | ||||
char *repl; | char *repl; | ||||
int klen; | int klen; | ||||
for (fm = table; fm < table + len; ) | for (fm = table; fm < table + len; ) | ||||
{ | { | ||||
/* | /* | ||||
* Rewrite each command in the table with any | * Rewrite each command in the table with any | ||||
* special key abbreviations expanded. | * special key abbreviations expanded. | ||||
Show All 38 Lines | for (fm = table; fm < table + len; ) | ||||
} | } | ||||
} | } | ||||
} | } | ||||
/* | /* | ||||
* Initialize the command lists. | * Initialize the command lists. | ||||
*/ | */ | ||||
public void | public void | ||||
init_cmds() | init_cmds(void) | ||||
{ | { | ||||
/* | /* | ||||
* Add the default command tables. | * Add the default command tables. | ||||
*/ | */ | ||||
add_fcmd_table((char*)cmdtable, sizeof(cmdtable)); | add_fcmd_table((char*)cmdtable, sizeof(cmdtable)); | ||||
add_ecmd_table((char*)edittable, sizeof(edittable)); | add_ecmd_table((char*)edittable, sizeof(edittable)); | ||||
#if USERFILE | #if USERFILE | ||||
/* | /* | ||||
Show All 13 Lines | #endif | ||||
add_hometable("LESSKEY", LESSKEYFILE, 0); | add_hometable("LESSKEY", LESSKEYFILE, 0); | ||||
#endif | #endif | ||||
} | } | ||||
/* | /* | ||||
* Add a command table. | * Add a command table. | ||||
*/ | */ | ||||
static int | static int | ||||
add_cmd_table(tlist, buf, len) | add_cmd_table(struct tablelist **tlist, char *buf, int len) | ||||
struct tablelist **tlist; | |||||
char *buf; | |||||
int len; | |||||
{ | { | ||||
register struct tablelist *t; | struct tablelist *t; | ||||
if (len == 0) | if (len == 0) | ||||
return (0); | return (0); | ||||
/* | /* | ||||
* Allocate a tablelist structure, initialize it, | * Allocate a tablelist structure, initialize it, | ||||
* and link it into the list of tables. | * and link it into the list of tables. | ||||
*/ | */ | ||||
if ((t = (struct tablelist *) | if ((t = (struct tablelist *) | ||||
calloc(1, sizeof(struct tablelist))) == NULL) | calloc(1, sizeof(struct tablelist))) == NULL) | ||||
{ | { | ||||
return (-1); | return (-1); | ||||
} | } | ||||
expand_special_keys(buf, len); | expand_special_keys(buf, len); | ||||
t->t_start = buf; | t->t_start = buf; | ||||
t->t_end = buf + len; | t->t_end = buf + len; | ||||
t->t_next = *tlist; | t->t_next = *tlist; | ||||
*tlist = t; | *tlist = t; | ||||
return (0); | return (0); | ||||
} | } | ||||
/* | /* | ||||
* Add a command table. | * Add a command table. | ||||
*/ | */ | ||||
public void | public void | ||||
add_fcmd_table(buf, len) | add_fcmd_table(char *buf, int len) | ||||
char *buf; | |||||
int len; | |||||
{ | { | ||||
if (add_cmd_table(&list_fcmd_tables, buf, len) < 0) | if (add_cmd_table(&list_fcmd_tables, buf, len) < 0) | ||||
error("Warning: some commands disabled", NULL_PARG); | error("Warning: some commands disabled", NULL_PARG); | ||||
} | } | ||||
/* | /* | ||||
* Add an editing command table. | * Add an editing command table. | ||||
*/ | */ | ||||
public void | public void | ||||
add_ecmd_table(buf, len) | add_ecmd_table(char *buf, int len) | ||||
char *buf; | |||||
int len; | |||||
{ | { | ||||
if (add_cmd_table(&list_ecmd_tables, buf, len) < 0) | if (add_cmd_table(&list_ecmd_tables, buf, len) < 0) | ||||
error("Warning: some edit commands disabled", NULL_PARG); | error("Warning: some edit commands disabled", NULL_PARG); | ||||
} | } | ||||
/* | /* | ||||
* Add an environment variable table. | * Add an environment variable table. | ||||
*/ | */ | ||||
static void | static void | ||||
add_var_table(tlist, buf, len) | add_var_table(struct tablelist **tlist, char *buf, int len) | ||||
struct tablelist **tlist; | |||||
char *buf; | |||||
int len; | |||||
{ | { | ||||
if (add_cmd_table(tlist, buf, len) < 0) | if (add_cmd_table(tlist, buf, len) < 0) | ||||
error("Warning: environment variables from lesskey file unavailable", NULL_PARG); | error("Warning: environment variables from lesskey file unavailable", NULL_PARG); | ||||
} | } | ||||
/* | /* | ||||
* Search a single command table for the command string in cmd. | * Search a single command table for the command string in cmd. | ||||
*/ | */ | ||||
static int | static int | ||||
cmd_search(cmd, table, endtable, sp) | cmd_search(char *cmd, char *table, char *endtable, char **sp) | ||||
char *cmd; | |||||
char *table; | |||||
char *endtable; | |||||
char **sp; | |||||
{ | { | ||||
register char *p; | char *p; | ||||
register char *q; | char *q; | ||||
register int a; | int a; | ||||
*sp = NULL; | *sp = NULL; | ||||
for (p = table, q = cmd; p < endtable; p++, q++) | for (p = table, q = cmd; p < endtable; p++, q++) | ||||
{ | { | ||||
if (*p == *q) | if (*p == *q) | ||||
{ | { | ||||
/* | /* | ||||
* Current characters match. | * Current characters match. | ||||
▲ Show 20 Lines • Show All 67 Lines • ▼ Show 20 Lines | cmd_search(char *cmd, char *table, char *endtable, char **sp) | ||||
return (A_INVALID); | return (A_INVALID); | ||||
} | } | ||||
/* | /* | ||||
* Decode a command character and return the associated action. | * Decode a command character and return the associated action. | ||||
* The "extra" string, if any, is returned in sp. | * The "extra" string, if any, is returned in sp. | ||||
*/ | */ | ||||
static int | static int | ||||
cmd_decode(tlist, cmd, sp) | cmd_decode(struct tablelist *tlist, char *cmd, char **sp) | ||||
struct tablelist *tlist; | |||||
char *cmd; | |||||
char **sp; | |||||
{ | { | ||||
register struct tablelist *t; | struct tablelist *t; | ||||
register int action = A_INVALID; | int action = A_INVALID; | ||||
/* | /* | ||||
* Search thru all the command tables. | * Search thru all the command tables. | ||||
* Stop when we find an action which is not A_INVALID. | * Stop when we find an action which is not A_INVALID. | ||||
*/ | */ | ||||
for (t = tlist; t != NULL; t = t->t_next) | for (t = tlist; t != NULL; t = t->t_next) | ||||
{ | { | ||||
action = cmd_search(cmd, t->t_start, t->t_end, sp); | action = cmd_search(cmd, t->t_start, t->t_end, sp); | ||||
if (action != A_INVALID) | if (action != A_INVALID) | ||||
break; | break; | ||||
} | } | ||||
if (action == A_UINVALID) | if (action == A_UINVALID) | ||||
action = A_INVALID; | action = A_INVALID; | ||||
return (action); | return (action); | ||||
} | } | ||||
/* | /* | ||||
* Decode a command from the cmdtables list. | * Decode a command from the cmdtables list. | ||||
*/ | */ | ||||
public int | public int | ||||
fcmd_decode(cmd, sp) | fcmd_decode(char *cmd, char **sp) | ||||
char *cmd; | |||||
char **sp; | |||||
{ | { | ||||
return (cmd_decode(list_fcmd_tables, cmd, sp)); | return (cmd_decode(list_fcmd_tables, cmd, sp)); | ||||
} | } | ||||
/* | /* | ||||
* Decode a command from the edittables list. | * Decode a command from the edittables list. | ||||
*/ | */ | ||||
public int | public int | ||||
ecmd_decode(cmd, sp) | ecmd_decode(char *cmd, char **sp) | ||||
char *cmd; | |||||
char **sp; | |||||
{ | { | ||||
return (cmd_decode(list_ecmd_tables, cmd, sp)); | return (cmd_decode(list_ecmd_tables, cmd, sp)); | ||||
} | } | ||||
/* | /* | ||||
* Get the value of an environment variable. | * Get the value of an environment variable. | ||||
* Looks first in the lesskey file, then in the real environment. | * Looks first in the lesskey file, then in the real environment. | ||||
*/ | */ | ||||
public char * | public char * | ||||
lgetenv(var) | lgetenv(char *var) | ||||
char *var; | |||||
{ | { | ||||
int a; | int a; | ||||
char *s; | char *s; | ||||
a = cmd_decode(list_var_tables, var, &s); | a = cmd_decode(list_var_tables, var, &s); | ||||
if (a == EV_OK) | if (a == EV_OK) | ||||
return (s); | return (s); | ||||
s = getenv(var); | s = getenv(var); | ||||
if (s != NULL && *s != '\0') | if (s != NULL && *s != '\0') | ||||
return (s); | return (s); | ||||
a = cmd_decode(list_sysvar_tables, var, &s); | a = cmd_decode(list_sysvar_tables, var, &s); | ||||
if (a == EV_OK) | if (a == EV_OK) | ||||
return (s); | return (s); | ||||
return (NULL); | return (NULL); | ||||
} | } | ||||
#if USERFILE | #if USERFILE | ||||
/* | /* | ||||
* Get an "integer" from a lesskey file. | * Get an "integer" from a lesskey file. | ||||
* Integers are stored in a funny format: | * Integers are stored in a funny format: | ||||
* two bytes, low order first, in radix KRADIX. | * two bytes, low order first, in radix KRADIX. | ||||
*/ | */ | ||||
static int | static int | ||||
gint(sp) | gint(char **sp) | ||||
char **sp; | |||||
{ | { | ||||
int n; | int n; | ||||
n = *(*sp)++; | n = *(*sp)++; | ||||
n += *(*sp)++ * KRADIX; | n += *(*sp)++ * KRADIX; | ||||
return (n); | return (n); | ||||
} | } | ||||
/* | /* | ||||
* Process an old (pre-v241) lesskey file. | * Process an old (pre-v241) lesskey file. | ||||
*/ | */ | ||||
static int | static int | ||||
old_lesskey(buf, len) | old_lesskey(char *buf, int len) | ||||
char *buf; | |||||
int len; | |||||
{ | { | ||||
/* | /* | ||||
* Old-style lesskey file. | * Old-style lesskey file. | ||||
* The file must end with either | * The file must end with either | ||||
* ...,cmd,0,action | * ...,cmd,0,action | ||||
* or ...,cmd,0,action|A_EXTRA,string,0 | * or ...,cmd,0,action|A_EXTRA,string,0 | ||||
* So the last byte or the second to last byte must be zero. | * So the last byte or the second to last byte must be zero. | ||||
*/ | */ | ||||
if (buf[len-1] != '\0' && buf[len-2] != '\0') | if (buf[len-1] != '\0' && buf[len-2] != '\0') | ||||
return (-1); | return (-1); | ||||
add_fcmd_table(buf, len); | add_fcmd_table(buf, len); | ||||
return (0); | return (0); | ||||
} | } | ||||
/* | /* | ||||
* Process a new (post-v241) lesskey file. | * Process a new (post-v241) lesskey file. | ||||
*/ | */ | ||||
static int | static int | ||||
new_lesskey(buf, len, sysvar) | new_lesskey(char *buf, int len, int sysvar) | ||||
char *buf; | |||||
int len; | |||||
int sysvar; | |||||
{ | { | ||||
char *p; | char *p; | ||||
register int c; | int c; | ||||
register int n; | int n; | ||||
/* | /* | ||||
* New-style lesskey file. | * New-style lesskey file. | ||||
* Extract the pieces. | * Extract the pieces. | ||||
*/ | */ | ||||
if (buf[len-3] != C0_END_LESSKEY_MAGIC || | if (buf[len-3] != C0_END_LESSKEY_MAGIC || | ||||
buf[len-2] != C1_END_LESSKEY_MAGIC || | buf[len-2] != C1_END_LESSKEY_MAGIC || | ||||
buf[len-1] != C2_END_LESSKEY_MAGIC) | buf[len-1] != C2_END_LESSKEY_MAGIC) | ||||
Show All 30 Lines | for (;;) | ||||
} | } | ||||
} | } | ||||
} | } | ||||
/* | /* | ||||
* Set up a user command table, based on a "lesskey" file. | * Set up a user command table, based on a "lesskey" file. | ||||
*/ | */ | ||||
public int | public int | ||||
lesskey(filename, sysvar) | lesskey(char *filename, int sysvar) | ||||
char *filename; | |||||
int sysvar; | |||||
{ | { | ||||
register char *buf; | char *buf; | ||||
register POSITION len; | POSITION len; | ||||
register long n; | long n; | ||||
register int f; | int f; | ||||
if (secure) | if (secure) | ||||
return (1); | return (1); | ||||
/* | /* | ||||
* Try to open the lesskey file. | * Try to open the lesskey file. | ||||
*/ | */ | ||||
filename = shell_unquote(filename); | filename = shell_unquote(filename); | ||||
f = open(filename, OPEN_READ); | f = open(filename, OPEN_READ); | ||||
▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | if (buf[0] != C0_LESSKEY_MAGIC || buf[1] != C1_LESSKEY_MAGIC || | ||||
return (old_lesskey(buf, (int)len)); | return (old_lesskey(buf, (int)len)); | ||||
return (new_lesskey(buf, (int)len, sysvar)); | return (new_lesskey(buf, (int)len, sysvar)); | ||||
} | } | ||||
/* | /* | ||||
* Add the standard lesskey file "$HOME/.less" | * Add the standard lesskey file "$HOME/.less" | ||||
*/ | */ | ||||
public void | public void | ||||
add_hometable(envname, def_filename, sysvar) | add_hometable(char *envname, char *def_filename, int sysvar) | ||||
char *envname; | |||||
char *def_filename; | |||||
int sysvar; | |||||
{ | { | ||||
char *filename; | char *filename; | ||||
PARG parg; | PARG parg; | ||||
if (envname != NULL && (filename = lgetenv(envname)) != NULL) | if (envname != NULL && (filename = lgetenv(envname)) != NULL) | ||||
filename = save(filename); | filename = save(filename); | ||||
else if (sysvar) | else if (sysvar) | ||||
filename = save(def_filename); | filename = save(def_filename); | ||||
Show All 9 Lines | add_hometable(char *envname, char *def_filename, int sysvar) | ||||
free(filename); | free(filename); | ||||
} | } | ||||
#endif | #endif | ||||
/* | /* | ||||
* See if a char is a special line-editing command. | * See if a char is a special line-editing command. | ||||
*/ | */ | ||||
public int | public int | ||||
editchar(c, flags) | editchar(int c, int flags) | ||||
int c; | |||||
int flags; | |||||
{ | { | ||||
int action; | int action; | ||||
int nch; | int nch; | ||||
char *s; | char *s; | ||||
char usercmd[MAX_CMDLEN+1]; | char usercmd[MAX_CMDLEN+1]; | ||||
/* | /* | ||||
* An editing character could actually be a sequence of characters; | * An editing character could actually be a sequence of characters; | ||||
▲ Show 20 Lines • Show All 87 Lines • Show Last 20 Lines |