#include "less.h"
#include "cmd.h"
#include "charset.h"
#if HAVE_STAT
#include <sys/stat.h>
#endif
extern int sc_width;
extern int utf_mode;
extern int no_hist_dups;
extern int marks_modified;
extern int no_paste;
public lbool pasting = FALSE;
static char cmdbuf[CMDBUF_SIZE];
static int cmd_col;
static int prompt_col;
static char *cp;
static int cmd_offset;
static lbool literal;
static size_t updown_match;
static lbool have_updown_match = FALSE;
static int cmd_complete(int action);
static lbool in_completion = FALSE;
static char *tk_text;
static char *tk_original;
static constant char *tk_ipoint;
static constant char *tk_trial = NULL;
static struct textlist tk_tlist;
static int cmd_left();
static int cmd_right();
#if SPACES_IN_FILENAMES
public char openquote = '"';
public char closequote = '"';
#endif
#if CMD_HISTORY
#define HISTFILE_FIRST_LINE ".less-history-file:"
#define HISTFILE_SEARCH_SECTION ".search"
#define HISTFILE_SHELL_SECTION ".shell"
#define HISTFILE_MARK_SECTION ".mark"
struct mlist
{
struct mlist *next;
struct mlist *prev;
struct mlist *curr_mp;
char *string;
lbool modified;
};
struct mlist mlist_search =
{ &mlist_search, &mlist_search, &mlist_search, NULL, 0 };
public void *ml_search = (void *) &mlist_search;
struct mlist mlist_examine =
{ &mlist_examine, &mlist_examine, &mlist_examine, NULL, 0 };
public void *ml_examine = (void *) &mlist_examine;
#if SHELL_ESCAPE || PIPEC
struct mlist mlist_shell =
{ &mlist_shell, &mlist_shell, &mlist_shell, NULL, 0 };
public void *ml_shell = (void *) &mlist_shell;
#endif
#else
public void *ml_search = (void *)1;
public void *ml_examine = (void *)2;
#if SHELL_ESCAPE || PIPEC
public void *ml_shell = (void *)3;
#endif
#endif
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;
public void cmd_reset(void)
{
cp = cmdbuf;
*cp = '\0';
cmd_col = 0;
cmd_offset = 0;
literal = FALSE;
cmd_mbc_buf_len = 0;
have_updown_match = FALSE;
}
public void clear_cmd(void)
{
cmd_col = prompt_col = 0;
cmd_mbc_buf_len = 0;
have_updown_match = FALSE;
}
public void cmd_putstr(constant char *s)
{
LWCHAR prev_ch = 0;
LWCHAR ch;
constant char *endline = s + strlen(s);
while (*s != '\0')
{
constant char *os = s;
int width;
ch = step_charc(&s, +1, endline);
while (os < s)
putchr(*os++);
if (!utf_mode)
width = 1;
else if (is_composing_char(ch) || is_combining_char(prev_ch, ch))
width = 0;
else
width = is_wide_char(ch) ? 2 : 1;
cmd_col += width;
prompt_col += width;
prev_ch = ch;
}
}
public int len_cmdbuf(void)
{
constant char *s = cmdbuf;
constant char *endline = s + strlen(s);
int len = 0;
while (*s != '\0')
{
step_charc(&s, +1, endline);
len++;
}
return (len);
}
public lbool cmdbuf_empty(void)
{
return cp == cmdbuf && cmd_mbc_buf_len == 0;
}
static constant char * cmd_step_common(char *p, LWCHAR ch, size_t len, int *pwidth, int *bswidth)
{
constant char *pr;
int width;
if (len == 1)
{
pr = prchar(ch);
width = (int) strlen(pr);
} else
{
pr = prutfchar(ch);
if (is_composing_char(ch))
width = 0;
else if (is_ubin_char(ch))
width = (int) strlen(pr);
else
{
LWCHAR prev_ch = step_char(&p, -1, cmdbuf);
if (is_combining_char(prev_ch, ch))
width = 0;
else
width = is_wide_char(ch) ? 2 : 1;
}
}
if (pwidth != NULL)
*pwidth = width;
if (bswidth != NULL)
*bswidth = width;
return (pr);
}
static constant 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, ptr_diff(*pp, p), pwidth, bswidth);
}
static constant 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, ptr_diff(p, *pp), pwidth, bswidth);
}
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];
}
public void cmd_repaint(constant char *old_cp)
{
if (old_cp == NULL)
{
old_cp = cp;
cmd_home();
}
clear_eol();
while (*cp != '\0')
{
char *np = cp;
int width;
constant 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;
constant 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_repaint_curr(void)
{
char *save_cp = cp;
cmd_home();
cmd_repaint(save_cp);
}
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 = (int) (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 = (int) (s - cmdbuf);
save_cp = cp;
cmd_home();
cmd_repaint(save_cp);
}
static int cmd_right(void)
{
constant 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 = 0;
int bswidth = 0;
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(constant char *cs, size_t clen)
{
char *s;
if (strlen(cmdbuf) + clen >= sizeof(cmdbuf)-1)
{
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++;
have_updown_match = FALSE;
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 = (int) (s - cp);
for (s = cp; ; s++)
{
s[0] = s[clen];
if (s[0] == '\0')
break;
}
have_updown_match = FALSE;
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';
have_updown_match = FALSE;
cmd_repaint(cp);
if (curr_cmdflags & CF_QUIT_ON_ERASE)
return (CC_QUIT);
return (CC_OK);
}
public void set_mlist(void *mlist, int cmdflags)
{
#if CMD_HISTORY
curr_mlist = (struct mlist *) mlist;
curr_cmdflags = cmdflags;
if (curr_mlist != NULL)
curr_mlist->curr_mp = curr_mlist;
#endif
}
#if CMD_HISTORY
static int cmd_updown(int action)
{
constant char *s;
struct mlist *ml;
if (curr_mlist == NULL)
{
bell();
return (CC_OK);
}
if (!have_updown_match)
{
updown_match = ptr_diff(cp, cmdbuf);
have_updown_match = TRUE;
}
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_offset = 0;
cmd_home();
clear_eol();
strcpy(cmdbuf, s);
for (cp = cmdbuf; *cp != '\0'; )
cmd_right();
return (CC_OK);
}
}
bell();
return (CC_OK);
}
public ssize_t save_updown_match(void)
{
if (!have_updown_match)
return (ssize_t)(-1);
return (ssize_t) updown_match;
}
public void restore_updown_match(ssize_t udm)
{
updown_match = udm;
have_updown_match = (udm != (ssize_t)(-1));
}
#endif
static void ml_link(struct mlist *mlist, struct mlist *ml)
{
ml->next = mlist;
ml->prev = mlist->prev;
mlist->prev->next = ml;
mlist->prev = ml;
}
static void ml_unlink(struct mlist *ml)
{
ml->prev->next = ml->next;
ml->next->prev = ml->prev;
}
public void cmd_addhist(struct mlist *mlist, constant char *cmd, lbool modified)
{
#if CMD_HISTORY
struct mlist *ml;
if (strlen(cmd) == 0)
return;
if (no_hist_dups)
{
struct mlist *next = NULL;
for (ml = mlist->next; ml->string != NULL; ml = next)
{
next = ml->next;
if (strcmp(ml->string, cmd) == 0)
{
ml_unlink(ml);
free(ml->string);
free(ml);
}
}
}
ml = mlist->prev;
if (ml == mlist || strcmp(ml->string, cmd) != 0)
{
ml = (struct mlist *) ecalloc(1, sizeof(struct mlist));
ml->string = save(cmd);
ml->modified = modified;
ml_link(mlist, ml);
}
mlist->curr_mp = ml->next;
#endif
}
public void cmd_accept(void)
{
#if CMD_HISTORY
if (curr_mlist == NULL || curr_mlist == ml_examine)
return;
cmd_addhist(curr_mlist, cmdbuf, TRUE);
curr_mlist->modified = TRUE;
#endif
}
static int cmd_edit(char c, lbool stay_in_completion)
{
int action;
int flags;
#define not_in_completion() do { if (!stay_in_completion) in_completion = FALSE; } while(0)
flags = 0;
#if CMD_HISTORY
if (curr_mlist == NULL)
flags |= ECF_NOHISTORY;
#endif
if ((curr_mlist == NULL && (curr_cmdflags & CF_OPTION))
#if TAB_COMPLETE_FILENAME
|| curr_mlist == ml_examine || curr_mlist == ml_shell
#endif
)
;
else
flags |= ECF_NOCOMPLETE;
action = editchar(c, flags);
if (is_ignoring_input(action))
return (CC_OK);
switch (action)
{
case A_NOACTION:
return (CC_OK);
case EC_START_PASTE:
if (no_paste)
pasting = TRUE;
return (CC_OK);
case EC_END_PASTE:
stop_ignoring_input();
return (CC_OK);
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 = TRUE;
return (CC_OK);
#if CMD_HISTORY
case EC_UP:
case EC_DOWN:
not_in_completion();
return (cmd_updown(action));
#endif
case EC_F_COMPLETE:
case EC_B_COMPLETE:
case EC_EXPAND:
return (cmd_complete(action));
default:
not_in_completion();
return (CC_PASS);
}
}
static int cmd_istr(constant char *str)
{
constant char *endline = str + strlen(str);
constant char *s;
int action;
for (s = str; *s != '\0'; )
{
constant char *os = s;
step_charc(&s, +1, endline);
action = cmd_ichar(os, ptr_diff(s, os));
if (action != CC_OK)
return (action);
}
return (CC_OK);
}
static void set_tk_original(constant char *word)
{
if (tk_original != NULL)
free(tk_original);
tk_original = (char *) ecalloc(ptr_diff(cp,word)+1, sizeof(char));
strncpy(tk_original, word, ptr_diff(cp,word));
}
#if TAB_COMPLETE_FILENAME
static char * delimit_word(void)
{
char *word;
#if SPACES_IN_FILENAMES
char *p;
int delim_quoted = FALSE;
int meta_quoted = FALSE;
constant char *esc = get_meta_escape();
size_t esclen = strlen(esc);
#endif
if (*cp != ' ' && *cp != '\0')
{
while (*cp != ' ' && *cp != '\0')
cmd_right();
} else if (cp > cmdbuf && cp[-1] != ' ')
{
;
#if 0
} else
{
return (NULL);
#endif
}
if (cp == cmdbuf)
return (NULL);
#if SPACES_IN_FILENAMES
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 = FALSE;
} else if (esclen > 0 && p + esclen < cp &&
strncmp(p, esc, esclen) == 0)
{
meta_quoted = TRUE;
p += esclen - 1;
} else if (delim_quoted)
{
if (*p == closequote)
delim_quoted = FALSE;
} else
{
if (*p == openquote)
delim_quoted = TRUE;
else if (*p == ' ')
word = p+1;
}
}
#endif
return (word);
}
static void init_file_compl(void)
{
char *word;
char c;
word = delimit_word();
if (word == NULL)
return;
tk_ipoint = word;
set_tk_original(word);
c = *cp;
*cp = '\0';
if (*word != openquote)
{
tk_text = fcomplete(word);
} else
{
#if MSDOS_COMPILER
char *qword = NULL;
#else
char *qword = shell_quote(word+1);
#endif
if (qword == NULL)
tk_text = fcomplete(word+1);
else
{
tk_text = fcomplete(qword);
free(qword);
}
}
*cp = c;
}
#endif
static void init_opt_compl(void)
{
tk_ipoint = cmdbuf;
set_tk_original(cmdbuf);
tk_text = findopts_name(cmdbuf);
}
static constant char * next_compl(int action, constant 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)
{
constant char *s;
if (!in_completion || action == EC_EXPAND)
{
if (tk_text != NULL)
{
free(tk_text);
tk_text = NULL;
}
if (curr_cmdflags & CF_OPTION)
init_opt_compl();
else
#if TAB_COMPLETE_FILENAME
init_file_compl();
#else
quit(QUIT_ERROR);
#endif
if (tk_text == NULL)
{
bell();
return (CC_OK);
}
if (action == EC_EXPAND)
{
tk_trial = tk_text;
} else
{
in_completion = TRUE;
init_textlist(&tk_tlist, tk_text);
tk_trial = next_compl(action, (char*)NULL);
}
} else
{
tk_trial = next_compl(action, tk_trial);
}
while (cp > tk_ipoint)
(void) cmd_erase();
if (tk_trial == NULL)
{
in_completion = FALSE;
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 = PATHNAME_SEP;
if (cmd_istr(s) != CC_OK)
goto fail;
}
}
return (CC_OK);
fail:
in_completion = FALSE;
bell();
return (CC_OK);
}
static int cmd_uchar(char c, size_t *plen)
{
if (!utf_mode)
{
cmd_mbc_buf[0] = c;
*plen = 1;
} else
{
if (cmd_mbc_buf_len == 0)
{
retry:
cmd_mbc_buf_index = 1;
*cmd_mbc_buf = c;
if (IS_ASCII_OCTET(c))
cmd_mbc_buf_len = 1;
#if MSDOS_COMPILER || OS2
else if (c == '\340' && IS_ASCII_OCTET(peekcc()))
{
cmd_mbc_buf_len = 1;
}
#endif
else if (IS_UTF8_LEAD(c))
{
cmd_mbc_buf_len = utf_len(c);
return (CC_OK);
} else
{
bell();
return (CC_ERROR);
}
} else if (IS_UTF8_TRAIL(c))
{
cmd_mbc_buf[cmd_mbc_buf_index++] = c;
if (cmd_mbc_buf_index < cmd_mbc_buf_len)
return (CC_OK);
if (!is_utf8_well_formed(cmd_mbc_buf, cmd_mbc_buf_index))
{
cmd_mbc_buf_len = 0;
bell();
return (CC_ERROR);
}
} else
{
cmd_mbc_buf_len = 0;
bell();
goto retry;
}
*plen = (size_t) cmd_mbc_buf_len;
cmd_mbc_buf_len = 0;
}
return (CC_PASS);
}
static int cmd_char2(char c, lbool stay_in_completion)
{
size_t len;
int action = cmd_uchar(c, &len);
if (action != CC_PASS)
return (action);
if (literal)
{
literal = FALSE;
return (cmd_ichar(cmd_mbc_buf, len));
}
if (in_mca() && len == 1)
{
action = cmd_edit(c, stay_in_completion);
switch (action)
{
case CC_OK:
case CC_QUIT:
return (action);
case CC_PASS:
break;
}
}
return (cmd_ichar(cmd_mbc_buf, len));
}
public int cmd_char(char c)
{
return cmd_char2(c, FALSE);
}
public int cmd_setstring(constant char *s, lbool uc)
{
while (*s != '\0')
{
int action;
char c = *s++;
if (uc && ASCII_IS_LOWER(c))
c = ASCII_TO_UPPER(c);
action = cmd_char2(c, TRUE);
if (action != CC_OK)
return (action);
}
cmd_repaint_curr();
return (CC_OK);
}
public LINENUM cmd_int(mutable long *frac)
{
constant char *p;
LINENUM n = 0;
lbool err;
for (p = cmdbuf; *p >= '0' && *p <= '9'; p++)
{
if (ckd_mul(&n, n, 10) || ckd_add(&n, n, *p - '0'))
{
error("Integer is too big", NULL_PARG);
return (0);
}
}
*frac = 0;
if (*p++ == '.')
{
*frac = getfraction(&p, NULL, &err);
}
return (n);
}
public constant char * get_cmdbuf(void)
{
if (cmd_mbc_buf_index < cmd_mbc_buf_len)
return (NULL);
return (cmdbuf);
}
#if CMD_HISTORY
public constant char * cmd_lastpattern(void)
{
if (curr_mlist == NULL)
return (NULL);
return (curr_mlist->curr_mp->prev->string);
}
#endif
#if CMD_HISTORY
static int mlist_size(struct mlist *ml)
{
int size = 0;
for (ml = ml->next; ml->string != NULL; ml = ml->next)
++size;
return size;
}
static char * histfile_find(lbool must_exist)
{
constant char *home = lgetenv("HOME");
char *name = NULL;
#if OS2
if (isnullenv(home))
home = lgetenv("INIT");
#endif
name = dirfile(lgetenv("XDG_STATE_HOME"), &LESSHISTFILE[1], must_exist);
if (name == NULL)
{
char *dir = dirfile(home, ".local/state", 1);
if (dir != NULL)
{
name = dirfile(dir, &LESSHISTFILE[1], must_exist);
free(dir);
}
}
if (name == NULL)
name = dirfile(lgetenv("XDG_DATA_HOME"), &LESSHISTFILE[1], must_exist);
if (name == NULL)
name = dirfile(home, LESSHISTFILE, must_exist);
return (name);
}
static char * histfile_name(lbool must_exist)
{
constant char *name;
char *wname;
name = lgetenv("LESSHISTFILE");
if (!isnullenv(name))
{
if (strcmp(name, "-") == 0 || strcmp(name, "/dev/null") == 0)
return (NULL);
return (save(name));
}
if (strcmp(LESSHISTFILE, "") == 0 || strcmp(LESSHISTFILE, "-") == 0)
return (NULL);
wname = NULL;
if (!must_exist)
{
wname = histfile_find(TRUE);
}
if (wname == NULL)
wname = histfile_find(must_exist);
return (wname);
}
static void read_cmdhist2(void (*action)(void*,struct mlist*,constant char*), void *uparam, int skip_search, int skip_shell)
{
struct mlist *ml = NULL;
char line[CMDBUF_SIZE];
char *filename;
FILE *f;
int *skip = NULL;
filename = histfile_name(TRUE);
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)
{
fclose(f);
return;
}
while (fgets(line, sizeof(line), f) != NULL)
{
char *p;
for (p = line; *p != '\0'; p++)
{
if (*p == '\n' || *p == '\r')
{
*p = '\0';
break;
}
}
if (strcmp(line, HISTFILE_SEARCH_SECTION) == 0)
{
ml = &mlist_search;
skip = &skip_search;
} else if (strcmp(line, HISTFILE_SHELL_SECTION) == 0)
{
#if SHELL_ESCAPE || PIPEC
ml = &mlist_shell;
skip = &skip_shell;
#else
ml = NULL;
skip = NULL;
(void) skip_shell;
#endif
} else if (strcmp(line, HISTFILE_MARK_SECTION) == 0)
{
ml = NULL;
} else if (*line == '"')
{
if (ml != NULL)
{
if (skip != NULL && *skip > 0)
--(*skip);
else
(*action)(uparam, ml, line+1);
}
} else if (*line == 'm')
{
(*action)(uparam, NULL, line);
}
}
fclose(f);
}
static void read_cmdhist(void (*action)(void*,struct mlist*,constant char*), void *uparam, lbool skip_search, lbool skip_shell)
{
if (!secure_allow(SF_HISTORY))
return;
read_cmdhist2(action, uparam, skip_search, skip_shell);
(*action)(uparam, NULL, NULL);
}
static void addhist_init(void *uparam, struct mlist *ml, constant char *string)
{
(void) uparam;
if (ml != NULL)
cmd_addhist(ml, string, 0);
else if (string != NULL)
restore_mark(string);
}
#endif
public void init_cmdhist(void)
{
#if CMD_HISTORY
read_cmdhist(&addhist_init, NULL, 0, 0);
#endif
}
#if CMD_HISTORY
static void write_mlist_header(struct mlist *ml, FILE *f)
{
if (ml == &mlist_search)
fprintf(f, "%s\n", HISTFILE_SEARCH_SECTION);
#if SHELL_ESCAPE || PIPEC
else if (ml == &mlist_shell)
fprintf(f, "%s\n", HISTFILE_SHELL_SECTION);
#endif
}
static void write_mlist(struct mlist *ml, FILE *f)
{
for (ml = ml->next; ml->string != NULL; ml = ml->next)
{
if (!ml->modified)
continue;
fprintf(f, "\"%s\n", ml->string);
ml->modified = FALSE;
}
ml->modified = FALSE;
}
static char * make_tempname(constant char *filename)
{
char lastch;
char *tempname = ecalloc(1, strlen(filename)+1);
strcpy(tempname, filename);
lastch = tempname[strlen(tempname)-1];
tempname[strlen(tempname)-1] = (lastch == 'Q') ? 'Z' : 'Q';
return tempname;
}
struct save_ctx
{
struct mlist *mlist;
FILE *fout;
};
static void copy_hist(void *uparam, struct mlist *ml, constant char *string)
{
struct save_ctx *ctx = (struct save_ctx *) uparam;
if (ml != NULL && ml != ctx->mlist) {
if (ctx->mlist)
write_mlist(ctx->mlist, ctx->fout);
ctx->mlist = ml;
write_mlist_header(ctx->mlist, ctx->fout);
}
if (string == NULL)
{
if (mlist_search.modified)
{
write_mlist_header(&mlist_search, ctx->fout);
write_mlist(&mlist_search, ctx->fout);
}
#if SHELL_ESCAPE || PIPEC
if (mlist_shell.modified)
{
write_mlist_header(&mlist_shell, ctx->fout);
write_mlist(&mlist_shell, ctx->fout);
}
#endif
} else if (ml != NULL)
{
fprintf(ctx->fout, "\"%s\n", string);
}
}
#endif
static void make_file_private(FILE *f)
{
#if HAVE_FCHMOD
lbool do_chmod = TRUE;
#if HAVE_STAT
struct stat statbuf;
int r = fstat(fileno(f), &statbuf);
if (r < 0 || !S_ISREG(statbuf.st_mode))
do_chmod = FALSE;
#endif
if (do_chmod)
fchmod(fileno(f), 0600);
#endif
}
#if CMD_HISTORY
static lbool histfile_modified(void)
{
if (mlist_search.modified)
return TRUE;
#if SHELL_ESCAPE || PIPEC
if (mlist_shell.modified)
return TRUE;
#endif
if (marks_modified)
return TRUE;
return FALSE;
}
#endif
public void save_cmdhist(void)
{
#if CMD_HISTORY
char *histname;
char *tempname;
int skip_search;
int skip_shell;
struct save_ctx ctx;
constant char *s;
FILE *fout = NULL;
int histsize = 0;
if (!secure_allow(SF_HISTORY) || !histfile_modified())
return;
histname = histfile_name(0);
if (histname == NULL)
return;
tempname = make_tempname(histname);
fout = fopen(tempname, "w");
if (fout != NULL)
{
make_file_private(fout);
s = lgetenv("LESSHISTSIZE");
if (s != NULL)
histsize = atoi(s);
if (histsize <= 0)
histsize = 100;
skip_search = mlist_size(&mlist_search) - histsize;
#if SHELL_ESCAPE || PIPEC
skip_shell = mlist_size(&mlist_shell) - histsize;
#else
skip_shell = 0;
#endif
fprintf(fout, "%s\n", HISTFILE_FIRST_LINE);
ctx.fout = fout;
ctx.mlist = NULL;
read_cmdhist(©_hist, &ctx, skip_search, skip_shell);
save_marks(fout, HISTFILE_MARK_SECTION);
fclose(fout);
#if MSDOS_COMPILER==WIN32C
remove(histname);
#endif
rename(tempname, histname);
}
free(tempname);
free(histname);
#endif
}