#include "less.h"
#include "cmd.h"
extern int sc_width;
static char cmdbuf[CMDBUF_SIZE];
static int cmd_col;
static int prompt_col;
static char *cp;
static int cmd_offset;
static int literal;
#if TAB_COMPLETE_FILENAME
static int 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
struct mlist
{
struct mlist *next;
struct mlist *prev;
struct mlist *curr_mp;
char *string;
};
struct mlist mlist_search =
{ &mlist_search, &mlist_search, &mlist_search, NULL };
public void constant *ml_search = (void *) &mlist_search;
struct mlist mlist_examine =
{ &mlist_examine, &mlist_examine, &mlist_examine, NULL };
public void constant *ml_examine = (void *) &mlist_examine;
#if SHELL_ESCAPE || PIPEC
struct mlist mlist_shell =
{ &mlist_shell, &mlist_shell, &mlist_shell, NULL };
public void constant *ml_shell = (void *) &mlist_shell;
#endif
#else
public void constant *ml_search = (void *)1;
public void constant *ml_examine = (void *)2;
#if SHELL_ESCAPE || PIPEC
public void constant *ml_shell = (void *)3;
#endif
#endif
static struct mlist *curr_mlist = NULL;
static int curr_cmdflags;
public void
cmd_reset()
{
cp = cmdbuf;
*cp = '\0';
cmd_col = 0;
cmd_offset = 0;
literal = 0;
}
public void
clear_cmd()
{
clear_bot();
cmd_col = prompt_col = 0;
}
public void
cmd_putstr(s)
char *s;
{
putstr(s);
cmd_col += strlen(s);
prompt_col += strlen(s);
}
public int
len_cmdbuf()
{
return (strlen(cmdbuf));
}
static void
cmd_repaint(old_cp)
char *old_cp;
{
char *p;
clear_eol();
for ( ; *cp != '\0'; cp++)
{
p = prchar(*cp);
if (cmd_col + strlen(p) >= sc_width)
break;
putstr(p);
cmd_col += strlen(p);
}
while (cp > old_cp)
cmd_left();
}
static void
cmd_home()
{
while (cmd_col > prompt_col)
{
putbs();
cmd_col--;
}
cp = &cmdbuf[cmd_offset];
}
static void
cmd_lshift()
{
char *s;
char *save_cp;
int cols;
s = cmdbuf + cmd_offset;
cols = 0;
while (cols < (sc_width - prompt_col) / 2 && *s != '\0')
cols += strlen(prchar(*s++));
cmd_offset = s - cmdbuf;
save_cp = cp;
cmd_home();
cmd_repaint(save_cp);
}
static void
cmd_rshift()
{
char *s;
char *p;
char *save_cp;
int cols;
s = cmdbuf + cmd_offset;
cols = 0;
while (cols < (sc_width - prompt_col) / 2 && s > cmdbuf)
{
p = prchar(*--s);
cols += strlen(p);
}
cmd_offset = s - cmdbuf;
save_cp = cp;
cmd_home();
cmd_repaint(save_cp);
}
static int
cmd_right()
{
char *p;
if (*cp == '\0')
{
return (CC_OK);
}
p = prchar(*cp);
if (cmd_col + strlen(p) >= sc_width)
cmd_lshift();
else if (cmd_col + strlen(p) == sc_width - 1 && cp[1] != '\0')
cmd_lshift();
cp++;
putstr(p);
cmd_col += strlen(p);
return (CC_OK);
}
static int
cmd_left()
{
char *p;
if (cp <= cmdbuf)
{
return (CC_OK);
}
p = prchar(cp[-1]);
if (cmd_col < prompt_col + strlen(p))
cmd_rshift();
cp--;
cmd_col -= strlen(p);
while (*p++ != '\0')
putbs();
return (CC_OK);
}
static int
cmd_ichar(c)
int c;
{
char *s;
if (strlen(cmdbuf) >= sizeof(cmdbuf)-2)
{
bell();
return (CC_ERROR);
}
for (s = &cmdbuf[strlen(cmdbuf)]; s >= cp; s--)
s[1] = s[0];
*cp = c;
cmd_repaint(cp);
cmd_right();
return (CC_OK);
}
static int
cmd_erase()
{
register char *s;
if (cp == cmdbuf)
{
return (CC_QUIT);
}
cmd_left();
for (s = cp; *s != '\0'; s++)
s[0] = s[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()
{
if (*cp == '\0')
{
return (CC_OK);
}
cmd_right();
cmd_erase();
return (CC_OK);
}
static int
cmd_werase()
{
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()
{
if (*cp == ' ')
{
while (*cp == ' ')
(void) cmd_delete();
} else
{
while (*cp != ' ' && *cp != '\0')
(void) cmd_delete();
}
return (CC_OK);
}
static int
cmd_kill()
{
if (cmdbuf[0] == '\0')
{
return (CC_QUIT);
}
cmd_offset = 0;
cmd_home();
*cp = '\0';
cmd_repaint(cp);
if (curr_cmdflags & CF_QUIT_ON_ERASE)
return (CC_QUIT);
return (CC_OK);
}
public void
set_mlist(mlist, cmdflags)
void *mlist;
int cmdflags;
{
curr_mlist = (struct mlist *) mlist;
curr_cmdflags = cmdflags;
}
#if CMD_HISTORY
static int
cmd_updown(action)
int action;
{
char *s;
if (curr_mlist == NULL)
{
bell();
return (CC_OK);
}
cmd_home();
clear_eol();
if (action == EC_UP)
curr_mlist->curr_mp = curr_mlist->curr_mp->prev;
else
curr_mlist->curr_mp = curr_mlist->curr_mp->next;
s = curr_mlist->curr_mp->string;
if (s == NULL)
s = "";
for (cp = cmdbuf; *s != '\0'; s++)
{
*cp = *s;
cmd_right();
}
*cp = '\0';
return (CC_OK);
}
#endif
public void
cmd_addhist(mlist, cmd)
struct mlist *mlist;
char *cmd;
{
#if CMD_HISTORY
struct mlist *ml;
if (strlen(cmd) == 0)
return;
for (ml = mlist->next; ml != mlist; ml = ml->next)
{
if (strcmp(ml->string, cmd) == 0)
break;
}
if (ml == mlist)
{
ml = (struct mlist *) ecalloc(1, sizeof(struct mlist));
ml->string = save(cmd);
ml->next = mlist;
ml->prev = mlist->prev;
mlist->prev->next = ml;
mlist->prev = ml;
}
mlist->curr_mp = ml->next;
#endif
}
public void
cmd_accept()
{
#if CMD_HISTORY
if (curr_mlist == NULL)
return;
cmd_addhist(curr_mlist, cmdbuf);
#endif
}
static int
cmd_edit(c)
int c;
{
int action;
int flags;
#if TAB_COMPLETE_FILENAME
#define not_in_completion() in_completion = 0
#else
#define not_in_completion()
#endif
flags = 0;
#if CMD_HISTORY
if (curr_mlist == NULL)
flags |= EC_NOHISTORY;
#endif
#if TAB_COMPLETE_FILENAME
if (curr_mlist == ml_search)
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());
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
static int
cmd_istr(str)
char *str;
{
char *s;
int action;
for (s = str; *s != '\0'; s++)
{
action = cmd_ichar(*s);
if (action != CC_OK)
{
bell();
return (action);
}
}
return (CC_OK);
}
static char *
delimit_word()
{
char *word;
#if SPACES_IN_FILENAMES
char *p;
int quoted;
#endif
if (*cp != ' ' && *cp != '\0')
{
while (*cp != ' ' && *cp != '\0')
cmd_right();
} else if (cp > cmdbuf && cp[-1] != ' ')
{
;
} else
{
return (NULL);
}
if (cp == cmdbuf)
return (NULL);
#if SPACES_IN_FILENAMES
quoted = 0;
for (p = cmdbuf; p < cp; p++)
{
if (!quoted && *p == openquote)
{
quoted = 1;
word = p;
} else if (quoted && *p == closequote)
{
quoted = 0;
}
}
if (quoted)
return (word);
#endif
for (word = cp-1; word > cmdbuf; word--)
if (word[-1] == ' ')
break;
return (word);
}
static void
init_compl()
{
char *word;
char c;
if (tk_text != NULL)
{
free(tk_text);
tk_text = NULL;
}
word = delimit_word();
if (word == NULL)
return;
tk_ipoint = word;
if (tk_original != NULL)
free(tk_original);
tk_original = (char *) ecalloc(cp-word+1, sizeof(char));
strncpy(tk_original, word, cp-word);
c = *cp;
*cp = '\0';
#if SPACES_IN_FILENAMES
if (*word == openquote)
word++;
#endif
tk_text = fcomplete(word);
*cp = c;
}
static char *
next_compl(action, prev)
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(action)
int action;
{
char *s;
if (!in_completion || action == EC_EXPAND)
{
init_compl();
if (tk_text == NULL)
{
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, (char*)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 = PATHNAME_SEP;
if (cmd_istr(s) != CC_OK)
goto fail;
}
}
return (CC_OK);
fail:
in_completion = 0;
bell();
return (CC_OK);
}
#endif
public int
cmd_char(c)
int c;
{
int action;
if (literal)
{
literal = 0;
return (cmd_ichar(c));
}
if (in_mca())
{
action = cmd_edit(c);
switch (action)
{
case CC_OK:
case CC_QUIT:
return (action);
case CC_PASS:
break;
}
}
return (cmd_ichar(c));
}
public int
cmd_int()
{
return (atoi(cmdbuf));
}
public char *
get_cmdbuf()
{
return (cmdbuf);
}