#include <sys/stat.h>
#include "charset.h"
#include "cmd.h"
#include "less.h"
extern int secure;
extern int sc_width;
extern int utf_mode;
static char cmdbuf[CMDBUF_SIZE];
static int cmd_col;
static int prompt_col;
static char *cp;
static int cmd_offset;
static int literal;
static int updown_match = -1;
static int cmd_complete(int);
static int in_completion = 0;
static char *tk_text;
static char *tk_original;
static char *tk_ipoint;
static char *tk_trial;
static struct textlist tk_tlist;
static int cmd_left(void);
static int cmd_right(void);
char openquote = '"';
char closequote = '"';
#define HISTFILE_FIRST_LINE ".less-history-file:"
#define HISTFILE_SEARCH_SECTION ".search"
#define HISTFILE_SHELL_SECTION ".shell"
struct mlist {
struct mlist *next;
struct mlist *prev;
struct mlist *curr_mp;
char *string;
int modified;
};
struct mlist mlist_search =
{ &mlist_search, &mlist_search, &mlist_search, NULL, 0 };
void * const ml_search = (void *) &mlist_search;
struct mlist mlist_examine =
{ &mlist_examine, &mlist_examine, &mlist_examine, NULL, 0 };
void * const ml_examine = (void *) &mlist_examine;
struct mlist mlist_shell =
{ &mlist_shell, &mlist_shell, &mlist_shell, NULL, 0 };
void * const ml_shell = (void *) &mlist_shell;
static struct mlist *curr_mlist = NULL;
static int curr_cmdflags;
static char cmd_mbc_buf[MAX_UTF_CHAR_LEN];
static int cmd_mbc_buf_len;
static int cmd_mbc_buf_index;
void
cmd_reset(void)
{
cp = cmdbuf;
*cp = '\0';
cmd_col = 0;
cmd_offset = 0;
literal = 0;
cmd_mbc_buf_len = 0;
updown_match = -1;
}
void
clear_cmd(void)
{
cmd_col = prompt_col = 0;
cmd_mbc_buf_len = 0;
updown_match = -1;
}
void
cmd_putstr(char *s)
{
while (*s != '\0') {
putchr(*s++);
cmd_col++;
prompt_col++;
}
}
int
len_cmdbuf(void)
{
char *s = cmdbuf;
char *endline = s + strlen(s);
int len = 0;
while (*s != '\0') {
step_char(&s, +1, endline);
len++;
}
return (len);
}
static char *
cmd_step_common(char *p, LWCHAR ch, int len, int *pwidth, int *bswidth)
{
char *pr;
if (len == 1) {
pr = prchar((int)ch);
if (pwidth != NULL || bswidth != NULL) {
int prlen = strlen(pr);
if (pwidth != NULL)
*pwidth = prlen;
if (bswidth != NULL)
*bswidth = prlen;
}
} else {
pr = prutfchar(ch);
if (pwidth != NULL || bswidth != NULL) {
if (is_composing_char(ch)) {
if (pwidth != NULL)
*pwidth = 0;
if (bswidth != NULL)
*bswidth = 0;
} else if (is_ubin_char(ch)) {
int prlen = strlen(pr);
if (pwidth != NULL)
*pwidth = prlen;
if (bswidth != NULL)
*bswidth = prlen;
} else {
if (pwidth != NULL)
*pwidth = is_wide_char(ch) ? 2 : 1;
if (bswidth != NULL)
*bswidth = 1;
}
}
}
return (pr);
}
static char *
cmd_step_right(char **pp, int *pwidth, int *bswidth)
{
char *p = *pp;
LWCHAR ch = step_char(pp, +1, p + strlen(p));
return (cmd_step_common(p, ch, *pp - p, pwidth, bswidth));
}
static char *
cmd_step_left(char **pp, int *pwidth, int *bswidth)
{
char *p = *pp;
LWCHAR ch = step_char(pp, -1, cmdbuf);
return (cmd_step_common(*pp, ch, p - *pp, pwidth, bswidth));
}
static void
cmd_repaint(char *old_cp)
{
clear_eol();
while (*cp != '\0') {
char *np = cp;
int width;
char *pr = cmd_step_right(&np, &width, NULL);
if (cmd_col + width >= sc_width)
break;
cp = np;
putstr(pr);
cmd_col += width;
}
while (*cp != '\0') {
char *np = cp;
int width;
char *pr = cmd_step_right(&np, &width, NULL);
if (width > 0)
break;
cp = np;
putstr(pr);
}
while (cp > old_cp)
cmd_left();
}
static void
cmd_home(void)
{
while (cmd_col > prompt_col) {
int width, bswidth;
cmd_step_left(&cp, &width, &bswidth);
while (bswidth-- > 0)
putbs();
cmd_col -= width;
}
cp = &cmdbuf[cmd_offset];
}
static void
cmd_lshift(void)
{
char *s;
char *save_cp;
int cols;
s = cmdbuf + cmd_offset;
cols = 0;
while (cols < (sc_width - prompt_col) / 2 && *s != '\0') {
int width;
cmd_step_right(&s, &width, NULL);
cols += width;
}
while (*s != '\0') {
int width;
char *ns = s;
cmd_step_right(&ns, &width, NULL);
if (width > 0)
break;
s = ns;
}
cmd_offset = s - cmdbuf;
save_cp = cp;
cmd_home();
cmd_repaint(save_cp);
}
static void
cmd_rshift(void)
{
char *s;
char *save_cp;
int cols;
s = cmdbuf + cmd_offset;
cols = 0;
while (cols < (sc_width - prompt_col) / 2 && s > cmdbuf) {
int width;
cmd_step_left(&s, &width, NULL);
cols += width;
}
cmd_offset = s - cmdbuf;
save_cp = cp;
cmd_home();
cmd_repaint(save_cp);
}
static int
cmd_right(void)
{
char *pr;
char *ncp;
int width;
if (*cp == '\0') {
return (CC_OK);
}
ncp = cp;
pr = cmd_step_right(&ncp, &width, NULL);
if (cmd_col + width >= sc_width)
cmd_lshift();
else if (cmd_col + width == sc_width - 1 && cp[1] != '\0')
cmd_lshift();
cp = ncp;
cmd_col += width;
putstr(pr);
while (*cp != '\0') {
pr = cmd_step_right(&ncp, &width, NULL);
if (width > 0)
break;
putstr(pr);
cp = ncp;
}
return (CC_OK);
}
static int
cmd_left(void)
{
char *ncp;
int width, bswidth;
if (cp <= cmdbuf) {
return (CC_OK);
}
ncp = cp;
while (ncp > cmdbuf) {
cmd_step_left(&ncp, &width, &bswidth);
if (width > 0)
break;
}
if (cmd_col < prompt_col + width)
cmd_rshift();
cp = ncp;
cmd_col -= width;
while (bswidth-- > 0)
putbs();
return (CC_OK);
}
static int
cmd_ichar(char *cs, int clen)
{
char *s;
if (strlen(cmdbuf) + clen >= sizeof (cmdbuf)-1) {
ring_bell();
return (CC_ERROR);
}
for (s = &cmdbuf[strlen(cmdbuf)]; s >= cp; s--)
s[clen] = s[0];
for (s = cp; s < cp + clen; s++)
*s = *cs++;
updown_match = -1;
cmd_repaint(cp);
cmd_right();
return (CC_OK);
}
static int
cmd_erase(void)
{
char *s;
int clen;
if (cp == cmdbuf) {
return (CC_QUIT);
}
s = cp;
cmd_left();
clen = s - cp;
for (s = cp; ; s++) {
s[0] = s[clen];
if (s[0] == '\0')
break;
}
updown_match = -1;
cmd_repaint(cp);
if ((curr_cmdflags & CF_QUIT_ON_ERASE) && cp == cmdbuf && *cp == '\0')
return (CC_QUIT);
return (CC_OK);
}
static int
cmd_delete(void)
{
if (*cp == '\0') {
return (CC_OK);
}
cmd_right();
cmd_erase();
return (CC_OK);
}
static int
cmd_werase(void)
{
if (cp > cmdbuf && cp[-1] == ' ') {
while (cp > cmdbuf && cp[-1] == ' ')
(void) cmd_erase();
} else {
while (cp > cmdbuf && cp[-1] != ' ')
(void) cmd_erase();
}
return (CC_OK);
}
static int
cmd_wdelete(void)
{
if (*cp == ' ') {
while (*cp == ' ')
(void) cmd_delete();
} else {
while (*cp != ' ' && *cp != '\0')
(void) cmd_delete();
}
return (CC_OK);
}
static int
cmd_kill(void)
{
if (cmdbuf[0] == '\0') {
return (CC_QUIT);
}
cmd_offset = 0;
cmd_home();
*cp = '\0';
updown_match = -1;
cmd_repaint(cp);
if (curr_cmdflags & CF_QUIT_ON_ERASE)
return (CC_QUIT);
return (CC_OK);
}
void
set_mlist(void *mlist, int cmdflags)
{
curr_mlist = (struct mlist *)mlist;
curr_cmdflags = cmdflags;
if (curr_mlist != NULL)
curr_mlist->curr_mp = curr_mlist;
}
static int
cmd_updown(int action)
{
char *s;
struct mlist *ml;
if (curr_mlist == NULL) {
ring_bell();
return (CC_OK);
}
if (updown_match < 0) {
updown_match = cp - cmdbuf;
}
for (ml = curr_mlist->curr_mp; ; ) {
ml = (action == EC_UP) ? ml->prev : ml->next;
if (ml == curr_mlist) {
break;
}
if (strncmp(cmdbuf, ml->string, updown_match) == 0) {
curr_mlist->curr_mp = ml;
s = ml->string;
if (s == NULL)
s = "";
cmd_home();
clear_eol();
strlcpy(cmdbuf, s, sizeof (cmdbuf));
for (cp = cmdbuf; *cp != '\0'; )
cmd_right();
return (CC_OK);
}
}
ring_bell();
return (CC_OK);
}
void
cmd_addhist(struct mlist *mlist, const char *cmd)
{
struct mlist *ml;
if (strlen(cmd) == 0)
return;
ml = mlist->prev;
if (ml == mlist || strcmp(ml->string, cmd) != 0) {
ml = ecalloc(1, sizeof (struct mlist));
ml->string = estrdup(cmd);
ml->next = mlist;
ml->prev = mlist->prev;
mlist->prev->next = ml;
mlist->prev = ml;
}
mlist->curr_mp = ml->next;
}
void
cmd_accept(void)
{
if (curr_mlist == NULL)
return;
cmd_addhist(curr_mlist, cmdbuf);
curr_mlist->modified = 1;
}
static int
cmd_edit(int c)
{
int action;
int flags;
#define not_in_completion() in_completion = 0
flags = 0;
if (curr_mlist == NULL)
flags |= EC_NOHISTORY;
if (curr_mlist == ml_search)
flags |= EC_NOCOMPLETE;
action = editchar(c, flags);
switch (action) {
case EC_RIGHT:
not_in_completion();
return (cmd_right());
case EC_LEFT:
not_in_completion();
return (cmd_left());
case EC_W_RIGHT:
not_in_completion();
while (*cp != '\0' && *cp != ' ')
cmd_right();
while (*cp == ' ')
cmd_right();
return (CC_OK);
case EC_W_LEFT:
not_in_completion();
while (cp > cmdbuf && cp[-1] == ' ')
cmd_left();
while (cp > cmdbuf && cp[-1] != ' ')
cmd_left();
return (CC_OK);
case EC_HOME:
not_in_completion();
cmd_offset = 0;
cmd_home();
cmd_repaint(cp);
return (CC_OK);
case EC_END:
not_in_completion();
while (*cp != '\0')
cmd_right();
return (CC_OK);
case EC_INSERT:
not_in_completion();
return (CC_OK);
case EC_BACKSPACE:
not_in_completion();
return (cmd_erase());
case EC_LINEKILL:
not_in_completion();
return (cmd_kill());
case EC_ABORT:
not_in_completion();
(void) cmd_kill();
return (CC_QUIT);
case EC_W_BACKSPACE:
not_in_completion();
return (cmd_werase());
case EC_DELETE:
not_in_completion();
return (cmd_delete());
case EC_W_DELETE:
not_in_completion();
return (cmd_wdelete());
case EC_LITERAL:
literal = 1;
return (CC_OK);
case EC_UP:
case EC_DOWN:
not_in_completion();
return (cmd_updown(action));
case EC_F_COMPLETE:
case EC_B_COMPLETE:
case EC_EXPAND:
return (cmd_complete(action));
case EC_NOACTION:
return (CC_OK);
default:
not_in_completion();
return (CC_PASS);
}
}
static int
cmd_istr(char *str)
{
char *s;
int action;
char *endline = str + strlen(str);
for (s = str; *s != '\0'; ) {
char *os = s;
step_char(&s, +1, endline);
action = cmd_ichar(os, s - os);
if (action != CC_OK) {
ring_bell();
return (action);
}
}
return (CC_OK);
}
static char *
delimit_word(void)
{
char *word;
char *p;
int delim_quoted = 0;
int meta_quoted = 0;
char *esc = get_meta_escape();
int esclen = strlen(esc);
if (*cp != ' ' && *cp != '\0') {
while (*cp != ' ' && *cp != '\0')
cmd_right();
}
if (cp == cmdbuf)
return (NULL);
for (word = cmdbuf; word < cp; word++)
if (*word != ' ')
break;
if (word >= cp)
return (cp);
for (p = cmdbuf; p < cp; p++) {
if (meta_quoted) {
meta_quoted = 0;
} else if (esclen > 0 && p + esclen < cp &&
strncmp(p, esc, esclen) == 0) {
meta_quoted = 1;
p += esclen - 1;
} else if (delim_quoted) {
if (*p == closequote)
delim_quoted = 0;
} else {
if (*p == openquote)
delim_quoted = 1;
else if (*p == ' ')
word = p+1;
}
}
return (word);
}
static void
init_compl(void)
{
char *word;
char c;
free(tk_text);
tk_text = NULL;
word = delimit_word();
if (word == NULL)
return;
tk_ipoint = word;
free(tk_original);
tk_original = ecalloc(cp-word+1, sizeof (char));
(void) strncpy(tk_original, word, cp-word);
c = *cp;
*cp = '\0';
if (*word != openquote) {
tk_text = fcomplete(word);
} else {
char *qword = shell_quote(word+1);
if (qword == NULL)
tk_text = fcomplete(word+1);
else
tk_text = fcomplete(qword);
free(qword);
}
*cp = c;
}
static char *
next_compl(int action, char *prev)
{
switch (action) {
case EC_F_COMPLETE:
return (forw_textlist(&tk_tlist, prev));
case EC_B_COMPLETE:
return (back_textlist(&tk_tlist, prev));
}
return ("?");
}
static int
cmd_complete(int action)
{
char *s;
if (!in_completion || action == EC_EXPAND) {
init_compl();
if (tk_text == NULL) {
ring_bell();
return (CC_OK);
}
if (action == EC_EXPAND) {
tk_trial = tk_text;
} else {
in_completion = 1;
init_textlist(&tk_tlist, tk_text);
tk_trial = next_compl(action, NULL);
}
} else {
tk_trial = next_compl(action, tk_trial);
}
while (cp > tk_ipoint)
(void) cmd_erase();
if (tk_trial == NULL) {
in_completion = 0;
if (cmd_istr(tk_original) != CC_OK)
goto fail;
} else {
if (cmd_istr(tk_trial) != CC_OK)
goto fail;
if (is_dir(tk_trial)) {
if (cp > cmdbuf && cp[-1] == closequote)
(void) cmd_erase();
s = lgetenv("LESSSEPARATOR");
if (s == NULL)
s = "/";
if (cmd_istr(s) != CC_OK)
goto fail;
}
}
return (CC_OK);
fail:
in_completion = 0;
ring_bell();
return (CC_OK);
}
int
cmd_char(int c)
{
int action;
int len;
if (!utf_mode) {
cmd_mbc_buf[0] = c & 0xff;
len = 1;
} else {
if (cmd_mbc_buf_len == 0) {
retry:
cmd_mbc_buf_index = 1;
*cmd_mbc_buf = c & 0xff;
if (isascii((unsigned char)c))
cmd_mbc_buf_len = 1;
else if (IS_UTF8_LEAD(c)) {
cmd_mbc_buf_len = utf_len(c);
return (CC_OK);
} else {
ring_bell();
return (CC_ERROR);
}
} else if (IS_UTF8_TRAIL(c)) {
cmd_mbc_buf[cmd_mbc_buf_index++] = c & 0xff;
if (cmd_mbc_buf_index < cmd_mbc_buf_len)
return (CC_OK);
if (!is_utf8_well_formed(cmd_mbc_buf)) {
cmd_mbc_buf_len = 0;
ring_bell();
return (CC_ERROR);
}
} else {
cmd_mbc_buf_len = 0;
ring_bell();
goto retry;
}
len = cmd_mbc_buf_len;
cmd_mbc_buf_len = 0;
}
if (literal) {
literal = 0;
return (cmd_ichar(cmd_mbc_buf, len));
}
if (in_mca() && len == 1) {
action = cmd_edit(c);
switch (action) {
case CC_OK:
case CC_QUIT:
return (action);
case CC_PASS:
break;
}
}
return (cmd_ichar(cmd_mbc_buf, len));
}
off_t
cmd_int(long *frac)
{
char *p;
off_t n = 0;
int err;
for (p = cmdbuf; *p >= '0' && *p <= '9'; p++)
n = (n * 10) + (*p - '0');
*frac = 0;
if (*p++ == '.') {
*frac = getfraction(&p, NULL, &err);
}
return (n);
}
char *
get_cmdbuf(void)
{
return (cmdbuf);
}
char *
cmd_lastpattern(void)
{
if (curr_mlist == NULL)
return (NULL);
return (curr_mlist->curr_mp->prev->string);
}
static char *
histfile_name(void)
{
char *home;
char *name;
name = lgetenv("LESSHISTFILE");
if (name != NULL && *name != '\0') {
if (strcmp(name, "-") == 0 || strcmp(name, "/dev/null") == 0)
return (NULL);
return (estrdup(name));
}
if (strcmp(LESSHISTFILE, "-") == 0)
return (NULL);
home = lgetenv("HOME");
if (home == NULL || *home == '\0') {
return (NULL);
}
return (easprintf("%s/%s", home, LESSHISTFILE));
}
void
init_cmdhist(void)
{
struct mlist *ml = NULL;
char line[CMDBUF_SIZE];
char *filename;
FILE *f;
char *p;
if (secure)
return;
filename = histfile_name();
if (filename == NULL)
return;
f = fopen(filename, "r");
free(filename);
if (f == NULL)
return;
if (fgets(line, sizeof (line), f) == NULL ||
strncmp(line, HISTFILE_FIRST_LINE,
strlen(HISTFILE_FIRST_LINE)) != 0) {
(void) fclose(f);
return;
}
while (fgets(line, sizeof (line), f) != NULL) {
for (p = line; *p != '\0'; p++) {
if (*p == '\n' || *p == '\r') {
*p = '\0';
break;
}
}
if (strcmp(line, HISTFILE_SEARCH_SECTION) == 0)
ml = &mlist_search;
else if (strcmp(line, HISTFILE_SHELL_SECTION) == 0) {
ml = &mlist_shell;
} else if (*line == '"') {
if (ml != NULL)
cmd_addhist(ml, line+1);
}
}
(void) fclose(f);
}
static void
save_mlist(struct mlist *ml, FILE *f)
{
int histsize = 0;
int n;
char *s;
s = lgetenv("LESSHISTSIZE");
if (s != NULL)
histsize = atoi(s);
if (histsize == 0)
histsize = 100;
ml = ml->prev;
for (n = 0; n < histsize; n++) {
if (ml->string == NULL)
break;
ml = ml->prev;
}
for (ml = ml->next; ml->string != NULL; ml = ml->next)
(void) fprintf(f, "\"%s\n", ml->string);
}
void
save_cmdhist(void)
{
char *filename;
FILE *f;
int modified = 0;
int do_chmod = 1;
struct stat statbuf;
int r;
if (secure)
return;
if (mlist_search.modified)
modified = 1;
if (mlist_shell.modified)
modified = 1;
if (!modified)
return;
filename = histfile_name();
if (filename == NULL)
return;
f = fopen(filename, "w");
free(filename);
if (f == NULL)
return;
r = fstat(fileno(f), &statbuf);
if (r == -1 || !S_ISREG(statbuf.st_mode))
do_chmod = 0;
if (do_chmod)
(void) fchmod(fileno(f), 0600);
(void) fprintf(f, "%s\n", HISTFILE_FIRST_LINE);
(void) fprintf(f, "%s\n", HISTFILE_SEARCH_SECTION);
save_mlist(&mlist_search, f);
(void) fprintf(f, "%s\n", HISTFILE_SHELL_SECTION);
save_mlist(&mlist_shell, f);
(void) fclose(f);
}