2012-07-23 21:31:53 +00:00
|
|
|
/*
|
2017-05-05 14:33:39 +00:00
|
|
|
* Copyright (C) 1984-2017 Mark Nudelman
|
2012-07-23 21:31:53 +00:00
|
|
|
*
|
|
|
|
* You may distribute under the terms of either the GNU General Public
|
|
|
|
* License or the Less License, as specified in the README file.
|
|
|
|
*
|
|
|
|
* For more information, see the README file.
|
|
|
|
*/
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Functions which manipulate the command buffer.
|
|
|
|
* Used only by command() and related functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "less.h"
|
|
|
|
#include "cmd.h"
|
2006-08-20 15:49:51 +00:00
|
|
|
#include "charset.h"
|
|
|
|
#if HAVE_STAT
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
extern int sc_width;
|
2006-08-20 15:49:51 +00:00
|
|
|
extern int utf_mode;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
static char cmdbuf[CMDBUF_SIZE]; /* Buffer for holding a multi-char command */
|
|
|
|
static int cmd_col; /* Current column of the cursor */
|
|
|
|
static int prompt_col; /* Column of cursor just after prompt */
|
|
|
|
static char *cp; /* Pointer into cmdbuf */
|
|
|
|
static int cmd_offset; /* Index into cmdbuf of first displayed char */
|
|
|
|
static int literal; /* Next input char should not be interpreted */
|
2012-06-26 20:38:53 +00:00
|
|
|
static int updown_match = -1; /* Prefix length in up/down movement */
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
#if TAB_COMPLETE_FILENAME
|
2017-05-05 14:33:39 +00:00
|
|
|
static int cmd_complete();
|
2000-05-22 09:53:22 +00:00
|
|
|
/*
|
|
|
|
* These variables are statics used by cmd_complete.
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int cmd_left();
|
|
|
|
static int cmd_right();
|
|
|
|
|
|
|
|
#if SPACES_IN_FILENAMES
|
|
|
|
public char openquote = '"';
|
|
|
|
public char closequote = '"';
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CMD_HISTORY
|
2006-08-20 15:49:51 +00:00
|
|
|
|
|
|
|
/* History file */
|
|
|
|
#define HISTFILE_FIRST_LINE ".less-history-file:"
|
|
|
|
#define HISTFILE_SEARCH_SECTION ".search"
|
|
|
|
#define HISTFILE_SHELL_SECTION ".shell"
|
|
|
|
|
2000-05-22 09:53:22 +00:00
|
|
|
/*
|
|
|
|
* A mlist structure represents a command history.
|
|
|
|
*/
|
|
|
|
struct mlist
|
|
|
|
{
|
|
|
|
struct mlist *next;
|
|
|
|
struct mlist *prev;
|
|
|
|
struct mlist *curr_mp;
|
|
|
|
char *string;
|
2007-06-04 01:42:54 +00:00
|
|
|
int modified;
|
2000-05-22 09:53:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These are the various command histories that exist.
|
|
|
|
*/
|
|
|
|
struct mlist mlist_search =
|
2007-06-04 01:42:54 +00:00
|
|
|
{ &mlist_search, &mlist_search, &mlist_search, NULL, 0 };
|
2017-05-05 14:33:39 +00:00
|
|
|
public void *ml_search = (void *) &mlist_search;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
struct mlist mlist_examine =
|
2007-06-04 01:42:54 +00:00
|
|
|
{ &mlist_examine, &mlist_examine, &mlist_examine, NULL, 0 };
|
2017-05-05 14:33:39 +00:00
|
|
|
public void *ml_examine = (void *) &mlist_examine;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
#if SHELL_ESCAPE || PIPEC
|
|
|
|
struct mlist mlist_shell =
|
2007-06-04 01:42:54 +00:00
|
|
|
{ &mlist_shell, &mlist_shell, &mlist_shell, NULL, 0 };
|
2017-05-05 14:33:39 +00:00
|
|
|
public void *ml_shell = (void *) &mlist_shell;
|
2000-05-22 09:53:22 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#else /* CMD_HISTORY */
|
|
|
|
|
|
|
|
/* If CMD_HISTORY is off, these are just flags. */
|
2017-05-05 14:33:39 +00:00
|
|
|
public void *ml_search = (void *)1;
|
|
|
|
public void *ml_examine = (void *)2;
|
2000-05-22 09:53:22 +00:00
|
|
|
#if SHELL_ESCAPE || PIPEC
|
2017-05-05 14:33:39 +00:00
|
|
|
public void *ml_shell = (void *)3;
|
2000-05-22 09:53:22 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* CMD_HISTORY */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* History for the current command.
|
|
|
|
*/
|
|
|
|
static struct mlist *curr_mlist = NULL;
|
|
|
|
static int curr_cmdflags;
|
|
|
|
|
2006-08-20 15:49:51 +00:00
|
|
|
static char cmd_mbc_buf[MAX_UTF_CHAR_LEN];
|
|
|
|
static int cmd_mbc_buf_len;
|
|
|
|
static int cmd_mbc_buf_index;
|
|
|
|
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Reset command buffer (to empty).
|
|
|
|
*/
|
|
|
|
public void
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_reset()
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
cp = cmdbuf;
|
|
|
|
*cp = '\0';
|
|
|
|
cmd_col = 0;
|
|
|
|
cmd_offset = 0;
|
|
|
|
literal = 0;
|
2006-08-20 15:49:51 +00:00
|
|
|
cmd_mbc_buf_len = 0;
|
2012-06-26 20:38:53 +00:00
|
|
|
updown_match = -1;
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-06-04 01:42:54 +00:00
|
|
|
* Clear command line.
|
2000-05-22 09:53:22 +00:00
|
|
|
*/
|
|
|
|
public void
|
2017-05-05 14:33:39 +00:00
|
|
|
clear_cmd()
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
cmd_col = prompt_col = 0;
|
2006-08-20 15:49:51 +00:00
|
|
|
cmd_mbc_buf_len = 0;
|
2012-06-26 20:38:53 +00:00
|
|
|
updown_match = -1;
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Display a string, usually as a prompt for input into the command buffer.
|
|
|
|
*/
|
|
|
|
public void
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_putstr(s)
|
|
|
|
constant char *s;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2006-08-20 15:49:51 +00:00
|
|
|
LWCHAR prev_ch = 0;
|
|
|
|
LWCHAR ch;
|
2017-03-31 21:29:43 +00:00
|
|
|
constant char *endline = s + strlen(s);
|
2006-08-20 15:49:51 +00:00
|
|
|
while (*s != '\0')
|
|
|
|
{
|
2017-05-05 14:33:39 +00:00
|
|
|
char *ns = (char *) s;
|
|
|
|
int width;
|
2006-08-20 15:49:51 +00:00
|
|
|
ch = step_char(&ns, +1, endline);
|
|
|
|
while (s < ns)
|
|
|
|
putchr(*s++);
|
|
|
|
if (!utf_mode)
|
2017-05-05 14:33:39 +00:00
|
|
|
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;
|
2006-08-20 15:49:51 +00:00
|
|
|
prev_ch = ch;
|
|
|
|
}
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* How many characters are in the command buffer?
|
|
|
|
*/
|
|
|
|
public int
|
2017-05-05 14:33:39 +00:00
|
|
|
len_cmdbuf()
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2017-05-05 14:33:39 +00:00
|
|
|
char *s = cmdbuf;
|
|
|
|
char *endline = s + strlen(s);
|
2006-08-20 15:49:51 +00:00
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
while (*s != '\0')
|
|
|
|
{
|
|
|
|
step_char(&s, +1, endline);
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
return (len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Common part of cmd_step_right() and cmd_step_left().
|
2017-05-05 14:33:39 +00:00
|
|
|
* {{ Returning pwidth and bswidth separately is a historical artifact
|
|
|
|
* since they're always the same. Maybe clean this up someday. }}
|
2006-08-20 15:49:51 +00:00
|
|
|
*/
|
|
|
|
static char *
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_step_common(p, ch, len, pwidth, bswidth)
|
|
|
|
char *p;
|
|
|
|
LWCHAR ch;
|
|
|
|
int len;
|
|
|
|
int *pwidth;
|
|
|
|
int *bswidth;
|
2006-08-20 15:49:51 +00:00
|
|
|
{
|
|
|
|
char *pr;
|
2017-05-05 14:33:39 +00:00
|
|
|
int width;
|
2006-08-20 15:49:51 +00:00
|
|
|
|
|
|
|
if (len == 1)
|
|
|
|
{
|
|
|
|
pr = prchar((int) ch);
|
2017-05-05 14:33:39 +00:00
|
|
|
width = (int) strlen(pr);
|
2006-08-20 15:49:51 +00:00
|
|
|
} else
|
|
|
|
{
|
|
|
|
pr = prutfchar(ch);
|
2017-05-05 14:33:39 +00:00
|
|
|
if (is_composing_char(ch))
|
|
|
|
width = 0;
|
|
|
|
else if (is_ubin_char(ch))
|
|
|
|
width = (int) strlen(pr);
|
|
|
|
else
|
2006-08-20 15:49:51 +00:00
|
|
|
{
|
2017-05-05 14:33:39 +00:00
|
|
|
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;
|
2006-08-20 15:49:51 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-05 14:33:39 +00:00
|
|
|
if (pwidth != NULL)
|
|
|
|
*pwidth = width;
|
|
|
|
if (bswidth != NULL)
|
|
|
|
*bswidth = width;
|
2006-08-20 15:49:51 +00:00
|
|
|
return (pr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Step a pointer one character right in the command buffer.
|
|
|
|
*/
|
|
|
|
static char *
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_step_right(pp, pwidth, bswidth)
|
|
|
|
char **pp;
|
|
|
|
int *pwidth;
|
|
|
|
int *bswidth;
|
2006-08-20 15:49:51 +00:00
|
|
|
{
|
|
|
|
char *p = *pp;
|
2017-05-05 14:33:39 +00:00
|
|
|
LWCHAR ch = step_char(pp, +1, p + strlen(p));
|
2006-08-20 15:49:51 +00:00
|
|
|
|
|
|
|
return cmd_step_common(p, ch, *pp - p, pwidth, bswidth);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Step a pointer one character left in the command buffer.
|
|
|
|
*/
|
|
|
|
static char *
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_step_left(pp, pwidth, bswidth)
|
|
|
|
char **pp;
|
|
|
|
int *pwidth;
|
|
|
|
int *bswidth;
|
2006-08-20 15:49:51 +00:00
|
|
|
{
|
|
|
|
char *p = *pp;
|
2017-05-05 14:33:39 +00:00
|
|
|
LWCHAR ch = step_char(pp, -1, cmdbuf);
|
2006-08-20 15:49:51 +00:00
|
|
|
|
|
|
|
return cmd_step_common(*pp, ch, p - *pp, pwidth, bswidth);
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Repaint the line from cp onwards.
|
|
|
|
* Then position the cursor just after the char old_cp (a pointer into cmdbuf).
|
|
|
|
*/
|
|
|
|
static void
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_repaint(old_cp)
|
|
|
|
constant char *old_cp;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Repaint the line from the current position.
|
|
|
|
*/
|
|
|
|
clear_eol();
|
2006-08-20 15:49:51 +00:00
|
|
|
while (*cp != '\0')
|
|
|
|
{
|
|
|
|
char *np = cp;
|
|
|
|
int width;
|
2017-05-05 14:33:39 +00:00
|
|
|
char *pr = cmd_step_right(&np, &width, NULL);
|
2006-08-20 15:49:51 +00:00
|
|
|
if (cmd_col + width >= sc_width)
|
|
|
|
break;
|
|
|
|
cp = np;
|
|
|
|
putstr(pr);
|
|
|
|
cmd_col += width;
|
|
|
|
}
|
|
|
|
while (*cp != '\0')
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2006-08-20 15:49:51 +00:00
|
|
|
char *np = cp;
|
|
|
|
int width;
|
|
|
|
char *pr = cmd_step_right(&np, &width, NULL);
|
|
|
|
if (width > 0)
|
2000-05-22 09:53:22 +00:00
|
|
|
break;
|
2006-08-20 15:49:51 +00:00
|
|
|
cp = np;
|
|
|
|
putstr(pr);
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Back up the cursor to the correct position.
|
|
|
|
*/
|
|
|
|
while (cp > old_cp)
|
|
|
|
cmd_left();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Put the cursor at "home" (just after the prompt),
|
|
|
|
* and set cp to the corresponding char in cmdbuf.
|
|
|
|
*/
|
|
|
|
static void
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_home()
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
while (cmd_col > prompt_col)
|
|
|
|
{
|
2006-08-20 15:49:51 +00:00
|
|
|
int width, bswidth;
|
|
|
|
|
|
|
|
cmd_step_left(&cp, &width, &bswidth);
|
|
|
|
while (bswidth-- > 0)
|
|
|
|
putbs();
|
|
|
|
cmd_col -= width;
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cp = &cmdbuf[cmd_offset];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shift the cmdbuf display left a half-screen.
|
|
|
|
*/
|
|
|
|
static void
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_lshift()
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
char *save_cp;
|
|
|
|
int cols;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Start at the first displayed char, count how far to the
|
|
|
|
* right we'd have to move to reach the center of the screen.
|
|
|
|
*/
|
|
|
|
s = cmdbuf + cmd_offset;
|
|
|
|
cols = 0;
|
|
|
|
while (cols < (sc_width - prompt_col) / 2 && *s != '\0')
|
2006-08-20 15:49:51 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2000-05-22 09:53:22 +00:00
|
|
|
|
2016-01-04 00:22:34 +00:00
|
|
|
cmd_offset = (int) (s - cmdbuf);
|
2000-05-22 09:53:22 +00:00
|
|
|
save_cp = cp;
|
|
|
|
cmd_home();
|
|
|
|
cmd_repaint(save_cp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shift the cmdbuf display right a half-screen.
|
|
|
|
*/
|
|
|
|
static void
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_rshift()
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
char *save_cp;
|
|
|
|
int cols;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Start at the first displayed char, count how far to the
|
|
|
|
* left we'd have to move to traverse a half-screen width
|
|
|
|
* of displayed characters.
|
|
|
|
*/
|
|
|
|
s = cmdbuf + cmd_offset;
|
|
|
|
cols = 0;
|
|
|
|
while (cols < (sc_width - prompt_col) / 2 && s > cmdbuf)
|
|
|
|
{
|
2006-08-20 15:49:51 +00:00
|
|
|
int width;
|
|
|
|
cmd_step_left(&s, &width, NULL);
|
|
|
|
cols += width;
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
|
2016-01-04 00:22:34 +00:00
|
|
|
cmd_offset = (int) (s - cmdbuf);
|
2000-05-22 09:53:22 +00:00
|
|
|
save_cp = cp;
|
|
|
|
cmd_home();
|
|
|
|
cmd_repaint(save_cp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Move cursor right one character.
|
|
|
|
*/
|
|
|
|
static int
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_right()
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2006-08-20 15:49:51 +00:00
|
|
|
char *pr;
|
|
|
|
char *ncp;
|
|
|
|
int width;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
if (*cp == '\0')
|
|
|
|
{
|
2006-08-20 15:49:51 +00:00
|
|
|
/* Already at the end of the line. */
|
2000-05-22 09:53:22 +00:00
|
|
|
return (CC_OK);
|
|
|
|
}
|
2006-08-20 15:49:51 +00:00
|
|
|
ncp = cp;
|
|
|
|
pr = cmd_step_right(&ncp, &width, NULL);
|
|
|
|
if (cmd_col + width >= sc_width)
|
2000-05-22 09:53:22 +00:00
|
|
|
cmd_lshift();
|
2006-08-20 15:49:51 +00:00
|
|
|
else if (cmd_col + width == sc_width - 1 && cp[1] != '\0')
|
2000-05-22 09:53:22 +00:00
|
|
|
cmd_lshift();
|
2006-08-20 15:49:51 +00:00
|
|
|
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;
|
|
|
|
}
|
2000-05-22 09:53:22 +00:00
|
|
|
return (CC_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Move cursor left one character.
|
|
|
|
*/
|
|
|
|
static int
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_left()
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2006-08-20 15:49:51 +00:00
|
|
|
char *ncp;
|
|
|
|
int width, bswidth;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
if (cp <= cmdbuf)
|
|
|
|
{
|
|
|
|
/* Already at the beginning of the line */
|
|
|
|
return (CC_OK);
|
|
|
|
}
|
2006-08-20 15:49:51 +00:00
|
|
|
ncp = cp;
|
|
|
|
while (ncp > cmdbuf)
|
|
|
|
{
|
|
|
|
cmd_step_left(&ncp, &width, &bswidth);
|
|
|
|
if (width > 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (cmd_col < prompt_col + width)
|
2000-05-22 09:53:22 +00:00
|
|
|
cmd_rshift();
|
2006-08-20 15:49:51 +00:00
|
|
|
cp = ncp;
|
|
|
|
cmd_col -= width;
|
|
|
|
while (bswidth-- > 0)
|
2000-05-22 09:53:22 +00:00
|
|
|
putbs();
|
|
|
|
return (CC_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert a char into the command buffer, at the current position.
|
|
|
|
*/
|
|
|
|
static int
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_ichar(cs, clen)
|
|
|
|
char *cs;
|
|
|
|
int clen;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
|
2006-08-20 15:49:51 +00:00
|
|
|
if (strlen(cmdbuf) + clen >= sizeof(cmdbuf)-1)
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2006-08-20 15:49:51 +00:00
|
|
|
/* No room in the command buffer for another char. */
|
2000-05-22 09:53:22 +00:00
|
|
|
bell();
|
|
|
|
return (CC_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-08-20 15:49:51 +00:00
|
|
|
* Make room for the new character (shift the tail of the buffer right).
|
2000-05-22 09:53:22 +00:00
|
|
|
*/
|
|
|
|
for (s = &cmdbuf[strlen(cmdbuf)]; s >= cp; s--)
|
2006-08-20 15:49:51 +00:00
|
|
|
s[clen] = s[0];
|
|
|
|
/*
|
|
|
|
* Insert the character into the buffer.
|
|
|
|
*/
|
|
|
|
for (s = cp; s < cp + clen; s++)
|
|
|
|
*s = *cs++;
|
2000-05-22 09:53:22 +00:00
|
|
|
/*
|
|
|
|
* Reprint the tail of the line from the inserted char.
|
|
|
|
*/
|
2012-06-26 20:38:53 +00:00
|
|
|
updown_match = -1;
|
2000-05-22 09:53:22 +00:00
|
|
|
cmd_repaint(cp);
|
|
|
|
cmd_right();
|
|
|
|
return (CC_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Backspace in the command buffer.
|
|
|
|
* Delete the char to the left of the cursor.
|
|
|
|
*/
|
|
|
|
static int
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_erase()
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2017-03-31 21:29:43 +00:00
|
|
|
char *s;
|
2006-08-20 15:49:51 +00:00
|
|
|
int clen;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
if (cp == cmdbuf)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Backspace past beginning of the buffer:
|
|
|
|
* this usually means abort the command.
|
|
|
|
*/
|
|
|
|
return (CC_QUIT);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Move cursor left (to the char being erased).
|
|
|
|
*/
|
2006-08-20 15:49:51 +00:00
|
|
|
s = cp;
|
2000-05-22 09:53:22 +00:00
|
|
|
cmd_left();
|
2016-01-04 00:22:34 +00:00
|
|
|
clen = (int) (s - cp);
|
2006-08-20 15:49:51 +00:00
|
|
|
|
2000-05-22 09:53:22 +00:00
|
|
|
/*
|
|
|
|
* Remove the char from the buffer (shift the buffer left).
|
|
|
|
*/
|
2006-08-20 15:49:51 +00:00
|
|
|
for (s = cp; ; s++)
|
|
|
|
{
|
|
|
|
s[0] = s[clen];
|
|
|
|
if (s[0] == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-05-22 09:53:22 +00:00
|
|
|
/*
|
|
|
|
* Repaint the buffer after the erased char.
|
|
|
|
*/
|
2012-06-26 20:38:53 +00:00
|
|
|
updown_match = -1;
|
2000-05-22 09:53:22 +00:00
|
|
|
cmd_repaint(cp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We say that erasing the entire command string causes us
|
|
|
|
* to abort the current command, if CF_QUIT_ON_ERASE is set.
|
|
|
|
*/
|
|
|
|
if ((curr_cmdflags & CF_QUIT_ON_ERASE) && cp == cmdbuf && *cp == '\0')
|
|
|
|
return (CC_QUIT);
|
|
|
|
return (CC_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete the char under the cursor.
|
|
|
|
*/
|
|
|
|
static int
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_delete()
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
if (*cp == '\0')
|
|
|
|
{
|
2006-08-20 15:49:51 +00:00
|
|
|
/* At end of string; there is no char under the cursor. */
|
2000-05-22 09:53:22 +00:00
|
|
|
return (CC_OK);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Move right, then use cmd_erase.
|
|
|
|
*/
|
|
|
|
cmd_right();
|
|
|
|
cmd_erase();
|
|
|
|
return (CC_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete the "word" to the left of the cursor.
|
|
|
|
*/
|
|
|
|
static int
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_werase()
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
if (cp > cmdbuf && cp[-1] == ' ')
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If the char left of cursor is a space,
|
|
|
|
* erase all the spaces left of cursor (to the first non-space).
|
|
|
|
*/
|
|
|
|
while (cp > cmdbuf && cp[-1] == ' ')
|
|
|
|
(void) cmd_erase();
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If the char left of cursor is not a space,
|
|
|
|
* erase all the nonspaces left of cursor (the whole "word").
|
|
|
|
*/
|
|
|
|
while (cp > cmdbuf && cp[-1] != ' ')
|
|
|
|
(void) cmd_erase();
|
|
|
|
}
|
|
|
|
return (CC_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete the "word" under the cursor.
|
|
|
|
*/
|
|
|
|
static int
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_wdelete()
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
if (*cp == ' ')
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If the char under the cursor is a space,
|
|
|
|
* delete it and all the spaces right of cursor.
|
|
|
|
*/
|
|
|
|
while (*cp == ' ')
|
|
|
|
(void) cmd_delete();
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If the char under the cursor is not a space,
|
|
|
|
* delete it and all nonspaces right of cursor (the whole word).
|
|
|
|
*/
|
|
|
|
while (*cp != ' ' && *cp != '\0')
|
|
|
|
(void) cmd_delete();
|
|
|
|
}
|
|
|
|
return (CC_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete all chars in the command buffer.
|
|
|
|
*/
|
|
|
|
static int
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_kill()
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
if (cmdbuf[0] == '\0')
|
|
|
|
{
|
2006-08-20 15:49:51 +00:00
|
|
|
/* Buffer is already empty; abort the current command. */
|
2000-05-22 09:53:22 +00:00
|
|
|
return (CC_QUIT);
|
|
|
|
}
|
|
|
|
cmd_offset = 0;
|
|
|
|
cmd_home();
|
|
|
|
*cp = '\0';
|
2012-06-26 20:38:53 +00:00
|
|
|
updown_match = -1;
|
2000-05-22 09:53:22 +00:00
|
|
|
cmd_repaint(cp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We say that erasing the entire command string causes us
|
|
|
|
* to abort the current command, if CF_QUIT_ON_ERASE is set.
|
|
|
|
*/
|
|
|
|
if (curr_cmdflags & CF_QUIT_ON_ERASE)
|
|
|
|
return (CC_QUIT);
|
|
|
|
return (CC_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Select an mlist structure to be the current command history.
|
|
|
|
*/
|
|
|
|
public void
|
2017-05-05 14:33:39 +00:00
|
|
|
set_mlist(mlist, cmdflags)
|
|
|
|
void *mlist;
|
|
|
|
int cmdflags;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2009-05-08 23:45:27 +00:00
|
|
|
#if CMD_HISTORY
|
2000-05-22 09:53:22 +00:00
|
|
|
curr_mlist = (struct mlist *) mlist;
|
|
|
|
curr_cmdflags = cmdflags;
|
2006-08-20 15:49:51 +00:00
|
|
|
|
|
|
|
/* Make sure the next up-arrow moves to the last string in the mlist. */
|
|
|
|
if (curr_mlist != NULL)
|
|
|
|
curr_mlist->curr_mp = curr_mlist;
|
2009-05-08 23:45:27 +00:00
|
|
|
#endif
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if CMD_HISTORY
|
|
|
|
/*
|
|
|
|
* Move up or down in the currently selected command history list.
|
2012-06-26 20:38:53 +00:00
|
|
|
* Only consider entries whose first updown_match chars are equal to
|
|
|
|
* cmdbuf's corresponding chars.
|
2000-05-22 09:53:22 +00:00
|
|
|
*/
|
|
|
|
static int
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_updown(action)
|
|
|
|
int action;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2017-05-05 14:33:39 +00:00
|
|
|
constant char *s;
|
2012-06-26 20:38:53 +00:00
|
|
|
struct mlist *ml;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
if (curr_mlist == NULL)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The current command has no history list.
|
|
|
|
*/
|
|
|
|
bell();
|
|
|
|
return (CC_OK);
|
|
|
|
}
|
2012-06-26 20:38:53 +00:00
|
|
|
|
|
|
|
if (updown_match < 0)
|
|
|
|
{
|
2016-01-04 00:22:34 +00:00
|
|
|
updown_match = (int) (cp - cmdbuf);
|
2012-06-26 20:38:53 +00:00
|
|
|
}
|
|
|
|
|
2000-05-22 09:53:22 +00:00
|
|
|
/*
|
2012-06-26 20:38:53 +00:00
|
|
|
* Find the next history entry which matches.
|
2000-05-22 09:53:22 +00:00
|
|
|
*/
|
2012-06-26 20:38:53 +00:00
|
|
|
for (ml = curr_mlist->curr_mp;;)
|
|
|
|
{
|
|
|
|
ml = (action == EC_UP) ? ml->prev : ml->next;
|
|
|
|
if (ml == curr_mlist)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We reached the end (or beginning) of the list.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (strncmp(cmdbuf, ml->string, updown_match) == 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This entry matches; stop here.
|
|
|
|
* Copy the entry into cmdbuf and echo it on the screen.
|
|
|
|
*/
|
|
|
|
curr_mlist->curr_mp = ml;
|
|
|
|
s = ml->string;
|
|
|
|
if (s == NULL)
|
|
|
|
s = "";
|
|
|
|
cmd_home();
|
|
|
|
clear_eol();
|
2013-05-13 05:27:43 +00:00
|
|
|
strcpy(cmdbuf, s);
|
2012-06-26 20:38:53 +00:00
|
|
|
for (cp = cmdbuf; *cp != '\0'; )
|
|
|
|
cmd_right();
|
|
|
|
return (CC_OK);
|
|
|
|
}
|
|
|
|
}
|
2000-05-22 09:53:22 +00:00
|
|
|
/*
|
2012-06-26 20:38:53 +00:00
|
|
|
* We didn't find a history entry that matches.
|
2000-05-22 09:53:22 +00:00
|
|
|
*/
|
2012-06-26 20:38:53 +00:00
|
|
|
bell();
|
2000-05-22 09:53:22 +00:00
|
|
|
return (CC_OK);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
2016-01-04 00:22:34 +00:00
|
|
|
* Add a string to an mlist.
|
2000-05-22 09:53:22 +00:00
|
|
|
*/
|
|
|
|
public void
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_addhist(mlist, cmd, modified)
|
|
|
|
struct mlist *mlist;
|
|
|
|
constant char *cmd;
|
|
|
|
int modified;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
#if CMD_HISTORY
|
|
|
|
struct mlist *ml;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't save a trivial command.
|
|
|
|
*/
|
|
|
|
if (strlen(cmd) == 0)
|
|
|
|
return;
|
2006-08-20 15:49:51 +00:00
|
|
|
|
2000-05-22 09:53:22 +00:00
|
|
|
/*
|
2006-08-20 15:49:51 +00:00
|
|
|
* Save the command unless it's a duplicate of the
|
|
|
|
* last command in the history.
|
2000-05-22 09:53:22 +00:00
|
|
|
*/
|
2006-08-20 15:49:51 +00:00
|
|
|
ml = mlist->prev;
|
|
|
|
if (ml == mlist || strcmp(ml->string, cmd) != 0)
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Did not find command in history.
|
|
|
|
* Save the command and put it at the end of the history list.
|
|
|
|
*/
|
|
|
|
ml = (struct mlist *) ecalloc(1, sizeof(struct mlist));
|
|
|
|
ml->string = save(cmd);
|
2016-01-04 00:22:34 +00:00
|
|
|
ml->modified = modified;
|
2000-05-22 09:53:22 +00:00
|
|
|
ml->next = mlist;
|
|
|
|
ml->prev = mlist->prev;
|
|
|
|
mlist->prev->next = ml;
|
|
|
|
mlist->prev = ml;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Point to the cmd just after the just-accepted command.
|
|
|
|
* Thus, an UPARROW will always retrieve the previous command.
|
|
|
|
*/
|
|
|
|
mlist->curr_mp = ml->next;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Accept the command in the command buffer.
|
|
|
|
* Add it to the currently selected history list.
|
|
|
|
*/
|
|
|
|
public void
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_accept()
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
#if CMD_HISTORY
|
|
|
|
/*
|
|
|
|
* Nothing to do if there is no currently selected history list.
|
|
|
|
*/
|
|
|
|
if (curr_mlist == NULL)
|
|
|
|
return;
|
2016-01-04 00:22:34 +00:00
|
|
|
cmd_addhist(curr_mlist, cmdbuf, 1);
|
2007-06-04 01:42:54 +00:00
|
|
|
curr_mlist->modified = 1;
|
2000-05-22 09:53:22 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to perform a line-edit function on the command buffer,
|
|
|
|
* using a specified char as a line-editing command.
|
|
|
|
* Returns:
|
|
|
|
* CC_PASS The char does not invoke a line edit function.
|
|
|
|
* CC_OK Line edit function done.
|
|
|
|
* CC_QUIT The char requests the current command to be aborted.
|
|
|
|
*/
|
|
|
|
static int
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_edit(c)
|
|
|
|
int c;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
int action;
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
#if TAB_COMPLETE_FILENAME
|
|
|
|
#define not_in_completion() in_completion = 0
|
|
|
|
#else
|
|
|
|
#define not_in_completion()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if the char is indeed a line-editing command.
|
|
|
|
*/
|
|
|
|
flags = 0;
|
|
|
|
#if CMD_HISTORY
|
|
|
|
if (curr_mlist == NULL)
|
|
|
|
/*
|
|
|
|
* No current history; don't accept history manipulation cmds.
|
|
|
|
*/
|
|
|
|
flags |= EC_NOHISTORY;
|
|
|
|
#endif
|
|
|
|
#if TAB_COMPLETE_FILENAME
|
|
|
|
if (curr_mlist == ml_search)
|
|
|
|
/*
|
|
|
|
* In a search command; don't accept file-completion cmds.
|
|
|
|
*/
|
|
|
|
flags |= EC_NOCOMPLETE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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());
|
2011-05-09 18:30:23 +00:00
|
|
|
case EC_ABORT:
|
|
|
|
not_in_completion();
|
|
|
|
(void) cmd_kill();
|
|
|
|
return (CC_QUIT);
|
2000-05-22 09:53:22 +00:00
|
|
|
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);
|
|
|
|
#if CMD_HISTORY
|
|
|
|
case EC_UP:
|
|
|
|
case EC_DOWN:
|
|
|
|
not_in_completion();
|
|
|
|
return (cmd_updown(action));
|
|
|
|
#endif
|
|
|
|
#if TAB_COMPLETE_FILENAME
|
|
|
|
case EC_F_COMPLETE:
|
|
|
|
case EC_B_COMPLETE:
|
|
|
|
case EC_EXPAND:
|
|
|
|
return (cmd_complete(action));
|
|
|
|
#endif
|
|
|
|
case EC_NOACTION:
|
|
|
|
return (CC_OK);
|
|
|
|
default:
|
|
|
|
not_in_completion();
|
|
|
|
return (CC_PASS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if TAB_COMPLETE_FILENAME
|
|
|
|
/*
|
|
|
|
* Insert a string into the command buffer, at the current position.
|
|
|
|
*/
|
|
|
|
static int
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_istr(str)
|
|
|
|
char *str;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int action;
|
2006-08-20 15:49:51 +00:00
|
|
|
char *endline = str + strlen(str);
|
2000-05-22 09:53:22 +00:00
|
|
|
|
2006-08-20 15:49:51 +00:00
|
|
|
for (s = str; *s != '\0'; )
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2006-08-20 15:49:51 +00:00
|
|
|
char *os = s;
|
2017-05-05 14:33:39 +00:00
|
|
|
step_char(&s, +1, endline);
|
2006-08-20 15:49:51 +00:00
|
|
|
action = cmd_ichar(os, s - os);
|
2000-05-22 09:53:22 +00:00
|
|
|
if (action != CC_OK)
|
|
|
|
{
|
|
|
|
bell();
|
|
|
|
return (action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (CC_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the beginning and end of the "current" word.
|
|
|
|
* This is the word which the cursor (cp) is inside or at the end of.
|
|
|
|
* Return pointer to the beginning of the word and put the
|
|
|
|
* cursor at the end of the word.
|
|
|
|
*/
|
|
|
|
static char *
|
2017-05-05 14:33:39 +00:00
|
|
|
delimit_word()
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
char *word;
|
|
|
|
#if SPACES_IN_FILENAMES
|
|
|
|
char *p;
|
2004-04-17 07:16:34 +00:00
|
|
|
int delim_quoted = 0;
|
|
|
|
int meta_quoted = 0;
|
2017-05-05 14:33:39 +00:00
|
|
|
constant char *esc = get_meta_escape();
|
2016-01-04 00:22:34 +00:00
|
|
|
int esclen = (int) strlen(esc);
|
2000-05-22 09:53:22 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Move cursor to end of word.
|
|
|
|
*/
|
|
|
|
if (*cp != ' ' && *cp != '\0')
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Cursor is on a nonspace.
|
|
|
|
* Move cursor right to the next space.
|
|
|
|
*/
|
|
|
|
while (*cp != ' ' && *cp != '\0')
|
|
|
|
cmd_right();
|
|
|
|
} else if (cp > cmdbuf && cp[-1] != ' ')
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Cursor is on a space, and char to the left is a nonspace.
|
|
|
|
* We're already at the end of the word.
|
|
|
|
*/
|
|
|
|
;
|
2004-04-17 07:16:34 +00:00
|
|
|
#if 0
|
2000-05-22 09:53:22 +00:00
|
|
|
} else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Cursor is on a space and char to the left is a space.
|
|
|
|
* Huh? There's no word here.
|
|
|
|
*/
|
|
|
|
return (NULL);
|
2004-04-17 07:16:34 +00:00
|
|
|
#endif
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
/*
|
2004-04-17 07:16:34 +00:00
|
|
|
* Find the beginning of the word which the cursor is in.
|
2000-05-22 09:53:22 +00:00
|
|
|
*/
|
|
|
|
if (cp == cmdbuf)
|
|
|
|
return (NULL);
|
|
|
|
#if SPACES_IN_FILENAMES
|
|
|
|
/*
|
|
|
|
* If we have an unbalanced quote (that is, an open quote
|
|
|
|
* without a corresponding close quote), we return everything
|
|
|
|
* from the open quote, including spaces.
|
|
|
|
*/
|
2004-04-17 07:16:34 +00:00
|
|
|
for (word = cmdbuf; word < cp; word++)
|
|
|
|
if (*word != ' ')
|
|
|
|
break;
|
|
|
|
if (word >= cp)
|
|
|
|
return (cp);
|
2000-05-22 09:53:22 +00:00
|
|
|
for (p = cmdbuf; p < cp; p++)
|
|
|
|
{
|
2004-04-17 07:16:34 +00:00
|
|
|
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)
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2004-04-17 07:16:34 +00:00
|
|
|
if (*p == closequote)
|
|
|
|
delim_quoted = 0;
|
|
|
|
} else /* (!delim_quoted) */
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2004-04-17 07:16:34 +00:00
|
|
|
if (*p == openquote)
|
|
|
|
delim_quoted = 1;
|
|
|
|
else if (*p == ' ')
|
|
|
|
word = p+1;
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return (word);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set things up to enter completion mode.
|
|
|
|
* Expand the word under the cursor into a list of filenames
|
|
|
|
* which start with that word, and set tk_text to that list.
|
|
|
|
*/
|
|
|
|
static void
|
2017-05-05 14:33:39 +00:00
|
|
|
init_compl()
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
char *word;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get rid of any previous tk_text.
|
|
|
|
*/
|
|
|
|
if (tk_text != NULL)
|
|
|
|
{
|
|
|
|
free(tk_text);
|
|
|
|
tk_text = NULL;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Find the original (uncompleted) word in the command buffer.
|
|
|
|
*/
|
|
|
|
word = delimit_word();
|
|
|
|
if (word == NULL)
|
|
|
|
return;
|
|
|
|
/*
|
|
|
|
* Set the insertion point to the point in the command buffer
|
|
|
|
* where the original (uncompleted) word now sits.
|
|
|
|
*/
|
|
|
|
tk_ipoint = word;
|
|
|
|
/*
|
|
|
|
* Save the original (uncompleted) word
|
|
|
|
*/
|
|
|
|
if (tk_original != NULL)
|
|
|
|
free(tk_original);
|
|
|
|
tk_original = (char *) ecalloc(cp-word+1, sizeof(char));
|
|
|
|
strncpy(tk_original, word, cp-word);
|
|
|
|
/*
|
|
|
|
* Get the expanded filename.
|
|
|
|
* This may result in a single filename, or
|
|
|
|
* a blank-separated list of filenames.
|
|
|
|
*/
|
|
|
|
c = *cp;
|
|
|
|
*cp = '\0';
|
2004-04-17 07:16:34 +00:00
|
|
|
if (*word != openquote)
|
|
|
|
{
|
|
|
|
tk_text = fcomplete(word);
|
|
|
|
} else
|
|
|
|
{
|
2012-07-23 21:31:53 +00:00
|
|
|
#if MSDOS_COMPILER
|
|
|
|
char *qword = NULL;
|
|
|
|
#else
|
2004-04-17 07:16:34 +00:00
|
|
|
char *qword = shell_quote(word+1);
|
2012-07-23 21:31:53 +00:00
|
|
|
#endif
|
2004-04-17 07:16:34 +00:00
|
|
|
if (qword == NULL)
|
|
|
|
tk_text = fcomplete(word+1);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tk_text = fcomplete(qword);
|
|
|
|
free(qword);
|
|
|
|
}
|
|
|
|
}
|
2000-05-22 09:53:22 +00:00
|
|
|
*cp = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the next word in the current completion list.
|
|
|
|
*/
|
|
|
|
static char *
|
2017-05-05 14:33:39 +00:00
|
|
|
next_compl(action, prev)
|
|
|
|
int action;
|
|
|
|
char *prev;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
switch (action)
|
|
|
|
{
|
|
|
|
case EC_F_COMPLETE:
|
|
|
|
return (forw_textlist(&tk_tlist, prev));
|
|
|
|
case EC_B_COMPLETE:
|
|
|
|
return (back_textlist(&tk_tlist, prev));
|
|
|
|
}
|
|
|
|
/* Cannot happen */
|
|
|
|
return ("?");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Complete the filename before (or under) the cursor.
|
|
|
|
* cmd_complete may be called multiple times. The global in_completion
|
|
|
|
* remembers whether this call is the first time (create the list),
|
|
|
|
* or a subsequent time (step thru the list).
|
|
|
|
*/
|
|
|
|
static int
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_complete(action)
|
|
|
|
int action;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
if (!in_completion || action == EC_EXPAND)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Expand the word under the cursor and
|
|
|
|
* use the first word in the expansion
|
|
|
|
* (or the entire expansion if we're doing EC_EXPAND).
|
|
|
|
*/
|
|
|
|
init_compl();
|
|
|
|
if (tk_text == NULL)
|
|
|
|
{
|
|
|
|
bell();
|
|
|
|
return (CC_OK);
|
|
|
|
}
|
|
|
|
if (action == EC_EXPAND)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Use the whole list.
|
|
|
|
*/
|
|
|
|
tk_trial = tk_text;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Use the first filename in the list.
|
|
|
|
*/
|
|
|
|
in_completion = 1;
|
|
|
|
init_textlist(&tk_tlist, tk_text);
|
|
|
|
tk_trial = next_compl(action, (char*)NULL);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We already have a completion list.
|
|
|
|
* Use the next/previous filename from the list.
|
|
|
|
*/
|
|
|
|
tk_trial = next_compl(action, tk_trial);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove the original word, or the previous trial completion.
|
|
|
|
*/
|
|
|
|
while (cp > tk_ipoint)
|
|
|
|
(void) cmd_erase();
|
|
|
|
|
|
|
|
if (tk_trial == NULL)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* There are no more trial completions.
|
|
|
|
* Insert the original (uncompleted) filename.
|
|
|
|
*/
|
|
|
|
in_completion = 0;
|
|
|
|
if (cmd_istr(tk_original) != CC_OK)
|
|
|
|
goto fail;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Insert trial completion.
|
|
|
|
*/
|
|
|
|
if (cmd_istr(tk_trial) != CC_OK)
|
|
|
|
goto fail;
|
|
|
|
/*
|
|
|
|
* If it is a directory, append a slash.
|
|
|
|
*/
|
|
|
|
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 = 0;
|
|
|
|
bell();
|
|
|
|
return (CC_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* TAB_COMPLETE_FILENAME */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process a single character of a multi-character command, such as
|
|
|
|
* a number, or the pattern of a search command.
|
|
|
|
* Returns:
|
|
|
|
* CC_OK The char was accepted.
|
|
|
|
* CC_QUIT The char requests the command to be aborted.
|
|
|
|
* CC_ERROR The char could not be accepted due to an error.
|
|
|
|
*/
|
|
|
|
public int
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_char(c)
|
|
|
|
int c;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
int action;
|
2006-08-20 15:49:51 +00:00
|
|
|
int len;
|
|
|
|
|
|
|
|
if (!utf_mode)
|
|
|
|
{
|
|
|
|
cmd_mbc_buf[0] = c;
|
|
|
|
len = 1;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
/* Perform strict validation in all possible cases. */
|
|
|
|
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;
|
|
|
|
else if (IS_UTF8_LEAD(c))
|
|
|
|
{
|
|
|
|
cmd_mbc_buf_len = utf_len(c);
|
|
|
|
return (CC_OK);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
/* UTF8_INVALID or stray UTF8_TRAIL */
|
|
|
|
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);
|
2016-01-04 00:22:34 +00:00
|
|
|
if (!is_utf8_well_formed(cmd_mbc_buf, cmd_mbc_buf_index))
|
2006-08-20 15:49:51 +00:00
|
|
|
{
|
|
|
|
/* complete, but not well formed (non-shortest form), sequence */
|
|
|
|
cmd_mbc_buf_len = 0;
|
|
|
|
bell();
|
|
|
|
return (CC_ERROR);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
/* Flush incomplete (truncated) sequence. */
|
|
|
|
cmd_mbc_buf_len = 0;
|
|
|
|
bell();
|
|
|
|
/* Handle new char. */
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = cmd_mbc_buf_len;
|
|
|
|
cmd_mbc_buf_len = 0;
|
|
|
|
}
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
if (literal)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Insert the char, even if it is a line-editing char.
|
|
|
|
*/
|
|
|
|
literal = 0;
|
2006-08-20 15:49:51 +00:00
|
|
|
return (cmd_ichar(cmd_mbc_buf, len));
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-08-20 15:49:51 +00:00
|
|
|
* See if it is a line-editing character.
|
2000-05-22 09:53:22 +00:00
|
|
|
*/
|
2006-08-20 15:49:51 +00:00
|
|
|
if (in_mca() && len == 1)
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
action = cmd_edit(c);
|
|
|
|
switch (action)
|
|
|
|
{
|
|
|
|
case CC_OK:
|
|
|
|
case CC_QUIT:
|
|
|
|
return (action);
|
|
|
|
case CC_PASS:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert the char into the command buffer.
|
|
|
|
*/
|
2006-08-20 15:49:51 +00:00
|
|
|
return (cmd_ichar(cmd_mbc_buf, len));
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the number currently in the command buffer.
|
|
|
|
*/
|
2004-04-17 07:16:34 +00:00
|
|
|
public LINENUM
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_int(frac)
|
|
|
|
long *frac;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2007-06-04 01:42:54 +00:00
|
|
|
char *p;
|
2004-04-17 07:16:34 +00:00
|
|
|
LINENUM n = 0;
|
2007-06-04 01:42:54 +00:00
|
|
|
int err;
|
2004-04-17 07:16:34 +00:00
|
|
|
|
2007-06-04 01:42:54 +00:00
|
|
|
for (p = cmdbuf; *p >= '0' && *p <= '9'; p++)
|
|
|
|
n = (n * 10) + (*p - '0');
|
|
|
|
*frac = 0;
|
|
|
|
if (*p++ == '.')
|
|
|
|
{
|
|
|
|
*frac = getfraction(&p, NULL, &err);
|
|
|
|
/* {{ do something if err is set? }} */
|
|
|
|
}
|
2004-04-17 07:16:34 +00:00
|
|
|
return (n);
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a pointer to the command buffer.
|
|
|
|
*/
|
|
|
|
public char *
|
2017-05-05 14:33:39 +00:00
|
|
|
get_cmdbuf()
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
return (cmdbuf);
|
|
|
|
}
|
2006-08-20 15:49:51 +00:00
|
|
|
|
2009-05-08 23:45:27 +00:00
|
|
|
#if CMD_HISTORY
|
2007-06-04 01:42:54 +00:00
|
|
|
/*
|
|
|
|
* Return the last (most recent) string in the current command history.
|
|
|
|
*/
|
|
|
|
public char *
|
2017-05-05 14:33:39 +00:00
|
|
|
cmd_lastpattern()
|
2007-06-04 01:42:54 +00:00
|
|
|
{
|
|
|
|
if (curr_mlist == NULL)
|
|
|
|
return (NULL);
|
|
|
|
return (curr_mlist->curr_mp->prev->string);
|
|
|
|
}
|
2009-05-08 23:45:27 +00:00
|
|
|
#endif
|
2007-06-04 01:42:54 +00:00
|
|
|
|
2006-08-20 15:49:51 +00:00
|
|
|
#if CMD_HISTORY
|
2016-01-04 00:22:34 +00:00
|
|
|
/*
|
|
|
|
*/
|
|
|
|
static int
|
2017-05-05 14:33:39 +00:00
|
|
|
mlist_size(ml)
|
|
|
|
struct mlist *ml;
|
2016-01-04 00:22:34 +00:00
|
|
|
{
|
|
|
|
int size = 0;
|
|
|
|
for (ml = ml->next; ml->string != NULL; ml = ml->next)
|
|
|
|
++size;
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2006-08-20 15:49:51 +00:00
|
|
|
/*
|
|
|
|
* Get the name of the history file.
|
|
|
|
*/
|
|
|
|
static char *
|
2017-05-05 14:33:39 +00:00
|
|
|
histfile_name()
|
2006-08-20 15:49:51 +00:00
|
|
|
{
|
|
|
|
char *home;
|
|
|
|
char *name;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
/* See if filename is explicitly specified by $LESSHISTFILE. */
|
|
|
|
name = lgetenv("LESSHISTFILE");
|
|
|
|
if (name != NULL && *name != '\0')
|
|
|
|
{
|
2007-06-04 01:42:54 +00:00
|
|
|
if (strcmp(name, "-") == 0 || strcmp(name, "/dev/null") == 0)
|
2006-08-20 15:49:51 +00:00
|
|
|
/* $LESSHISTFILE == "-" means don't use a history file. */
|
|
|
|
return (NULL);
|
|
|
|
return (save(name));
|
|
|
|
}
|
|
|
|
|
2016-01-04 00:22:34 +00:00
|
|
|
/* See if history file is disabled in the build. */
|
|
|
|
if (strcmp(LESSHISTFILE, "") == 0 || strcmp(LESSHISTFILE, "-") == 0)
|
|
|
|
return (NULL);
|
|
|
|
|
2006-08-20 15:49:51 +00:00
|
|
|
/* Otherwise, file is in $HOME. */
|
|
|
|
home = lgetenv("HOME");
|
|
|
|
if (home == NULL || *home == '\0')
|
|
|
|
{
|
|
|
|
#if OS2
|
|
|
|
home = lgetenv("INIT");
|
|
|
|
if (home == NULL || *home == '\0')
|
|
|
|
#endif
|
|
|
|
return (NULL);
|
|
|
|
}
|
2016-01-04 00:22:34 +00:00
|
|
|
len = (int) (strlen(home) + strlen(LESSHISTFILE) + 2);
|
2006-08-20 15:49:51 +00:00
|
|
|
name = (char *) ecalloc(len, sizeof(char));
|
|
|
|
SNPRINTF2(name, len, "%s/%s", home, LESSHISTFILE);
|
|
|
|
return (name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-01-04 00:22:34 +00:00
|
|
|
* Read a .lesshst file and call a callback for each line in the file.
|
2006-08-20 15:49:51 +00:00
|
|
|
*/
|
2016-01-04 00:22:34 +00:00
|
|
|
static void
|
2017-05-05 14:33:39 +00:00
|
|
|
read_cmdhist2(action, uparam, skip_search, skip_shell)
|
|
|
|
void (*action)(void*,struct mlist*,char*);
|
|
|
|
void *uparam;
|
|
|
|
int skip_search;
|
|
|
|
int skip_shell;
|
2006-08-20 15:49:51 +00:00
|
|
|
{
|
|
|
|
struct mlist *ml = NULL;
|
|
|
|
char line[CMDBUF_SIZE];
|
|
|
|
char *filename;
|
|
|
|
FILE *f;
|
|
|
|
char *p;
|
2016-01-04 00:22:34 +00:00
|
|
|
int *skip = NULL;
|
2006-08-20 15:49:51 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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)
|
2016-01-04 00:22:34 +00:00
|
|
|
{
|
2006-08-20 15:49:51 +00:00
|
|
|
ml = &mlist_search;
|
2016-01-04 00:22:34 +00:00
|
|
|
skip = &skip_search;
|
|
|
|
} else if (strcmp(line, HISTFILE_SHELL_SECTION) == 0)
|
2007-06-21 10:42:55 +00:00
|
|
|
{
|
|
|
|
#if SHELL_ESCAPE || PIPEC
|
2006-08-20 15:49:51 +00:00
|
|
|
ml = &mlist_shell;
|
2016-01-04 00:22:34 +00:00
|
|
|
skip = &skip_shell;
|
2007-06-21 10:42:55 +00:00
|
|
|
#else
|
|
|
|
ml = NULL;
|
2016-01-04 00:22:34 +00:00
|
|
|
skip = NULL;
|
2006-08-20 15:49:51 +00:00
|
|
|
#endif
|
2007-06-21 10:42:55 +00:00
|
|
|
} else if (*line == '"')
|
2006-08-20 15:49:51 +00:00
|
|
|
{
|
|
|
|
if (ml != NULL)
|
2016-01-04 00:22:34 +00:00
|
|
|
{
|
|
|
|
if (skip != NULL && *skip > 0)
|
|
|
|
--(*skip);
|
|
|
|
else
|
|
|
|
(*action)(uparam, ml, line+1);
|
|
|
|
}
|
2006-08-20 15:49:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(f);
|
2016-01-04 00:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-05-05 14:33:39 +00:00
|
|
|
read_cmdhist(action, uparam, skip_search, skip_shell)
|
|
|
|
void (*action)(void*,struct mlist*,char*);
|
|
|
|
void *uparam;
|
|
|
|
int skip_search;
|
|
|
|
int skip_shell;
|
2016-01-04 00:22:34 +00:00
|
|
|
{
|
|
|
|
read_cmdhist2(action, uparam, skip_search, skip_shell);
|
|
|
|
(*action)(uparam, NULL, NULL); /* signal end of file */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
addhist_init(void *uparam, struct mlist *ml, char *string)
|
|
|
|
{
|
|
|
|
if (ml == NULL || string == NULL)
|
|
|
|
return;
|
|
|
|
cmd_addhist(ml, string, 0);
|
|
|
|
}
|
|
|
|
#endif /* CMD_HISTORY */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize history from a .lesshist file.
|
|
|
|
*/
|
|
|
|
public void
|
2017-05-05 14:33:39 +00:00
|
|
|
init_cmdhist()
|
2016-01-04 00:22:34 +00:00
|
|
|
{
|
|
|
|
#if CMD_HISTORY
|
|
|
|
read_cmdhist(&addhist_init, NULL, 0, 0);
|
2006-08-20 15:49:51 +00:00
|
|
|
#endif /* CMD_HISTORY */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-01-04 00:22:34 +00:00
|
|
|
* Write the header for a section of the history file.
|
2006-08-20 15:49:51 +00:00
|
|
|
*/
|
|
|
|
#if CMD_HISTORY
|
|
|
|
static void
|
2017-05-05 14:33:39 +00:00
|
|
|
write_mlist_header(ml, f)
|
|
|
|
struct mlist *ml;
|
|
|
|
FILE *f;
|
2006-08-20 15:49:51 +00:00
|
|
|
{
|
2016-01-04 00:22:34 +00:00
|
|
|
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
|
|
|
|
}
|
2006-08-20 15:49:51 +00:00
|
|
|
|
2016-01-04 00:22:34 +00:00
|
|
|
/*
|
|
|
|
* Write all modified entries in an mlist to the history file.
|
|
|
|
*/
|
|
|
|
static void
|
2017-05-05 14:33:39 +00:00
|
|
|
write_mlist(ml, f)
|
|
|
|
struct mlist *ml;
|
|
|
|
FILE *f;
|
2016-01-04 00:22:34 +00:00
|
|
|
{
|
2006-08-20 15:49:51 +00:00
|
|
|
for (ml = ml->next; ml->string != NULL; ml = ml->next)
|
2016-01-04 00:22:34 +00:00
|
|
|
{
|
|
|
|
if (!ml->modified)
|
|
|
|
continue;
|
2006-08-20 15:49:51 +00:00
|
|
|
fprintf(f, "\"%s\n", ml->string);
|
2016-01-04 00:22:34 +00:00
|
|
|
ml->modified = 0;
|
|
|
|
}
|
|
|
|
ml->modified = 0; /* entire mlist is now unmodified */
|
2006-08-20 15:49:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-01-04 00:22:34 +00:00
|
|
|
* Make a temp name in the same directory as filename.
|
2006-08-20 15:49:51 +00:00
|
|
|
*/
|
2016-01-04 00:22:34 +00:00
|
|
|
static char *
|
2017-05-05 14:33:39 +00:00
|
|
|
make_tempname(filename)
|
|
|
|
char *filename;
|
2016-01-04 00:22:34 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2006-08-20 15:49:51 +00:00
|
|
|
|
2016-01-04 00:22:34 +00:00
|
|
|
struct save_ctx
|
|
|
|
{
|
|
|
|
struct mlist *mlist;
|
|
|
|
FILE *fout;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy entries from the saved history file to a new file.
|
|
|
|
* At the end of each mlist, append any new entries
|
|
|
|
* created during this session.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
copy_hist(void *uparam, struct mlist *ml, char *string)
|
|
|
|
{
|
|
|
|
struct save_ctx *ctx = (struct save_ctx *) uparam;
|
|
|
|
|
|
|
|
if (ml != ctx->mlist) {
|
|
|
|
/* We're changing mlists. */
|
|
|
|
if (ctx->mlist)
|
|
|
|
/* Append any new entries to the end of the current mlist. */
|
|
|
|
write_mlist(ctx->mlist, ctx->fout);
|
|
|
|
/* Write the header for the new mlist. */
|
|
|
|
ctx->mlist = ml;
|
|
|
|
write_mlist_header(ctx->mlist, ctx->fout);
|
|
|
|
}
|
|
|
|
if (string != NULL)
|
|
|
|
{
|
|
|
|
/* Copy the entry. */
|
|
|
|
fprintf(ctx->fout, "\"%s\n", string);
|
|
|
|
}
|
|
|
|
if (ml == NULL) /* End of file */
|
|
|
|
{
|
|
|
|
/* Write any sections that were not in the original file. */
|
|
|
|
if (mlist_search.modified)
|
|
|
|
{
|
|
|
|
write_mlist_header(&mlist_search, ctx->fout);
|
|
|
|
write_mlist(&mlist_search, ctx->fout);
|
|
|
|
}
|
2007-06-21 10:42:55 +00:00
|
|
|
#if SHELL_ESCAPE || PIPEC
|
2016-01-04 00:22:34 +00:00
|
|
|
if (mlist_shell.modified)
|
|
|
|
{
|
|
|
|
write_mlist_header(&mlist_shell, ctx->fout);
|
|
|
|
write_mlist(&mlist_shell, ctx->fout);
|
|
|
|
}
|
2007-06-21 10:42:55 +00:00
|
|
|
#endif
|
2016-01-04 00:22:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CMD_HISTORY */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make a file readable only by its owner.
|
|
|
|
*/
|
|
|
|
static void
|
2017-05-05 14:33:39 +00:00
|
|
|
make_file_private(f)
|
|
|
|
FILE *f;
|
2009-05-08 23:45:27 +00:00
|
|
|
{
|
2016-01-04 00:22:34 +00:00
|
|
|
#if HAVE_FCHMOD
|
2009-05-08 23:45:27 +00:00
|
|
|
int do_chmod = 1;
|
|
|
|
#if HAVE_STAT
|
|
|
|
struct stat statbuf;
|
|
|
|
int r = fstat(fileno(f), &statbuf);
|
|
|
|
if (r < 0 || !S_ISREG(statbuf.st_mode))
|
|
|
|
/* Don't chmod if not a regular file. */
|
|
|
|
do_chmod = 0;
|
|
|
|
#endif
|
|
|
|
if (do_chmod)
|
|
|
|
fchmod(fileno(f), 0600);
|
2006-08-20 15:49:51 +00:00
|
|
|
#endif
|
2016-01-04 00:22:34 +00:00
|
|
|
}
|
2006-08-20 15:49:51 +00:00
|
|
|
|
2016-01-04 00:22:34 +00:00
|
|
|
/*
|
|
|
|
* Does the history file need to be updated?
|
|
|
|
*/
|
|
|
|
static int
|
2017-05-05 14:33:39 +00:00
|
|
|
histfile_modified()
|
2016-01-04 00:22:34 +00:00
|
|
|
{
|
|
|
|
if (mlist_search.modified)
|
|
|
|
return 1;
|
|
|
|
#if SHELL_ESCAPE || PIPEC
|
|
|
|
if (mlist_shell.modified)
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
2006-08-20 15:49:51 +00:00
|
|
|
|
2016-01-04 00:22:34 +00:00
|
|
|
/*
|
|
|
|
* Update the .lesshst file.
|
|
|
|
*/
|
|
|
|
public void
|
2017-05-05 14:33:39 +00:00
|
|
|
save_cmdhist()
|
2016-01-04 00:22:34 +00:00
|
|
|
{
|
|
|
|
#if CMD_HISTORY
|
|
|
|
char *histname;
|
|
|
|
char *tempname;
|
|
|
|
int skip_search;
|
|
|
|
int skip_shell;
|
|
|
|
struct save_ctx ctx;
|
|
|
|
char *s;
|
|
|
|
FILE *fout = NULL;
|
|
|
|
int histsize = 0;
|
2006-08-20 15:49:51 +00:00
|
|
|
|
2016-01-04 00:22:34 +00:00
|
|
|
if (!histfile_modified())
|
|
|
|
return;
|
|
|
|
histname = histfile_name();
|
|
|
|
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;
|
2006-08-20 15:49:51 +00:00
|
|
|
#if SHELL_ESCAPE || PIPEC
|
2016-01-04 00:22:34 +00:00
|
|
|
skip_shell = mlist_size(&mlist_shell) - histsize;
|
2006-08-20 15:49:51 +00:00
|
|
|
#endif
|
2016-01-04 00:22:34 +00:00
|
|
|
fprintf(fout, "%s\n", HISTFILE_FIRST_LINE);
|
|
|
|
ctx.fout = fout;
|
|
|
|
ctx.mlist = NULL;
|
|
|
|
read_cmdhist(copy_hist, &ctx, skip_search, skip_shell);
|
|
|
|
fclose(fout);
|
|
|
|
#if MSDOS_COMPILER==WIN32C
|
|
|
|
/*
|
|
|
|
* Windows rename doesn't remove an existing file,
|
|
|
|
* making it useless for atomic operations. Sigh.
|
|
|
|
*/
|
|
|
|
remove(histname);
|
|
|
|
#endif
|
|
|
|
rename(tempname, histname);
|
|
|
|
}
|
|
|
|
free(tempname);
|
|
|
|
free(histname);
|
2006-08-20 15:49:51 +00:00
|
|
|
#endif /* CMD_HISTORY */
|
|
|
|
}
|