#include "less.h"
#include "position.h"
extern int pr_type;
extern int new_file;
extern int sc_width;
extern int so_s_width, so_e_width;
extern int linenums;
extern int hshift;
extern int sc_height;
extern int jump_sline;
extern int less_is_more;
extern IFILE curr_ifile;
extern char *editor;
extern char *editproto;
static const char s_proto[] =
"?n?f%f .?m(%T %i of %m) ..?e(END) ?x- Next\\: %x..%t";
static const char m_proto[] =
"?n?f%f .?m(%T %i of %m) ..?e(END) "
"?x- Next\\: %x.:?pB%pB\\%:byte %bB?s/%s...%t";
static const char M_proto[] =
"?f%f .?n?m(%T %i of %m) ..?"
"ltlines %lt-%lb?L/%L. :byte %bB?s/%s. .?e(END)"
" ?x- Next\\: %x.:?pB%pB\\%..%t";
static const char e_proto[] =
"?f%f .?m(%T %i of %m) .?ltlines "
"%lt-%lb?L/%L. .byte %bB?s/%s. ?e(END) :?pB%pB\\%..%t";
static const char h_proto[] =
"HELP -- ?eEND -- Press g to see it again:"
"Press RETURN for more., or q when done";
static const char w_proto[] =
"Waiting for data";
static const char more_proto[] =
"%f (?eEND ?x- Next\\: %x.:?pB%pB\\%:byte %bB?s/%s...%t)";
static const char more_M_proto[] =
"%f (?eEND ?x- Next\\: %x.:?pB%pB\\%:byte %bB?s/%s...%t)"
"[Press space to continue, q to quit, h for help]";
char *prproto[3];
char const *eqproto = e_proto;
char const *hproto = h_proto;
char const *wproto = w_proto;
static char message[PROMPT_SIZE];
static char *mp;
void
init_prompt(void)
{
prproto[0] = estrdup(s_proto);
prproto[1] = estrdup(less_is_more ? more_proto : m_proto);
prproto[2] = estrdup(less_is_more ? more_M_proto : M_proto);
eqproto = estrdup(e_proto);
hproto = estrdup(h_proto);
wproto = estrdup(w_proto);
}
static void
ap_str(char *s)
{
int len;
len = strlen(s);
if (mp + len >= message + PROMPT_SIZE)
len = message + PROMPT_SIZE - mp - 1;
(void) strncpy(mp, s, len);
mp += len;
*mp = '\0';
}
static void
ap_char(char c)
{
char buf[2];
buf[0] = c;
buf[1] = '\0';
ap_str(buf);
}
static void
ap_pos(off_t pos)
{
char buf[23];
postoa(pos, buf, sizeof(buf));
ap_str(buf);
}
static void
ap_int(int num)
{
char buf[13];
inttoa(num, buf, sizeof buf);
ap_str(buf);
}
static void
ap_quest(void)
{
ap_str("?");
}
static off_t
curr_byte(int where)
{
off_t pos;
pos = position(where);
while (pos == -1 && where >= 0 && where < sc_height-1)
pos = position(++where);
if (pos == -1)
pos = ch_length();
return (pos);
}
static int
cond(char c, int where)
{
off_t len;
switch (c) {
case 'a':
return (*message != '\0');
case 'b':
return (curr_byte(where) != -1);
case 'c':
return (hshift != 0);
case 'e':
return (eof_displayed());
case 'f':
return (strcmp(get_filename(curr_ifile), "-") != 0);
case 'l':
case 'd':
return (linenums);
case 'L':
case 'D':
return (linenums && ch_length() != -1);
case 'm':
return (ntags() ? (ntags() > 1) : (nifile() > 1));
case 'n':
return (ntags() ? 1 : new_file);
case 'p':
return (curr_byte(where) != -1 && ch_length() > 0);
case 'P':
return (currline(where) != 0 &&
(len = ch_length()) > 0 && find_linenum(len) != 0);
case 's':
case 'B':
return (ch_length() != -1);
case 'x':
if (ntags())
return (0);
return (next_ifile(curr_ifile) != NULL);
}
return (0);
}
static void
protochar(int c, int where)
{
off_t pos;
off_t len;
int n;
off_t linenum;
off_t last_linenum;
IFILE h;
#undef PAGE_NUM
#define PAGE_NUM(linenum) ((((linenum) - 1) / (sc_height - 1)) + 1)
switch (c) {
case 'b':
pos = curr_byte(where);
if (pos != -1)
ap_pos(pos);
else
ap_quest();
break;
case 'c':
ap_int(hshift);
break;
case 'd':
linenum = currline(where);
if (linenum > 0 && sc_height > 1)
ap_pos(PAGE_NUM(linenum));
else
ap_quest();
break;
case 'D':
len = ch_length();
if (len == -1) {
ap_quest();
} else if (len == 0) {
ap_pos(0);
} else {
linenum = find_linenum(len - 1);
if (linenum <= 0)
ap_quest();
else
ap_pos(PAGE_NUM(linenum));
}
break;
case 'E':
ap_str(editor);
break;
case 'f':
ap_str(get_filename(curr_ifile));
break;
case 'F':
ap_str(last_component(get_filename(curr_ifile)));
break;
case 'i':
if (ntags())
ap_int(curr_tag());
else
ap_int(get_index(curr_ifile));
break;
case 'l':
linenum = currline(where);
if (linenum != 0)
ap_pos(linenum);
else
ap_quest();
break;
case 'L':
len = ch_length();
if (len == -1 || len == ch_zero() ||
(linenum = find_linenum(len)) <= 0)
ap_quest();
else
ap_pos(linenum-1);
break;
case 'm':
n = ntags();
if (n)
ap_int(n);
else
ap_int(nifile());
break;
case 'p':
pos = curr_byte(where);
len = ch_length();
if (pos != -1 && len > 0)
ap_int(percentage(pos, len));
else
ap_quest();
break;
case 'P':
linenum = currline(where);
if (linenum == 0 ||
(len = ch_length()) == -1 || len == ch_zero() ||
(last_linenum = find_linenum(len)) <= 0)
ap_quest();
else
ap_int(percentage(linenum, last_linenum));
break;
case 's':
case 'B':
len = ch_length();
if (len != -1)
ap_pos(len);
else
ap_quest();
break;
case 't':
while (mp > message && mp[-1] == ' ')
mp--;
*mp = '\0';
break;
case 'T':
if (ntags())
ap_str("tag");
else
ap_str("file");
break;
case 'x':
h = next_ifile(curr_ifile);
if (h != NULL)
ap_str(get_filename(h));
else
ap_quest();
break;
}
}
static const char *
skipcond(const char *p)
{
int iflevel;
iflevel = 1;
for (;;) {
switch (*++p) {
case '?':
iflevel++;
break;
case ':':
if (iflevel == 1)
return (p);
break;
case '.':
if (--iflevel == 0)
return (p);
break;
case '\\':
++p;
break;
case '\0':
return (p-1);
}
}
}
static const char *
wherechar(const char *p, int *wp)
{
switch (*p) {
case 'b': case 'd': case 'l': case 'p': case 'P':
switch (*++p) {
case 't': *wp = TOP; break;
case 'm': *wp = MIDDLE; break;
case 'b': *wp = BOTTOM; break;
case 'B': *wp = BOTTOM_PLUS_ONE; break;
case 'j': *wp = adjsline(jump_sline); break;
default: *wp = TOP; p--; break;
}
}
return (p);
}
char *
pr_expand(const char *proto, int maxwidth)
{
const char *p;
int c;
int where;
mp = message;
if (*proto == '\0')
return ("");
for (p = proto; *p != '\0'; p++) {
switch (*p) {
default:
ap_char(*p);
break;
case '\\':
p++;
ap_char(*p);
break;
case '?':
if ((c = *++p) == '\0') {
--p;
} else {
where = 0;
p = wherechar(p, &where);
if (!cond(c, where))
p = skipcond(p);
}
break;
case ':':
p = skipcond(p);
break;
case '.':
break;
case '%':
if ((c = *++p) == '\0') {
--p;
} else {
where = 0;
p = wherechar(p, &where);
protochar(c, where);
}
break;
}
}
if (*message == '\0')
return ("");
if (maxwidth > 0 && mp >= message + maxwidth) {
return (mp - maxwidth);
}
return (message);
}
char *
eq_message(void)
{
return (pr_expand(eqproto, 0));
}
char *
prompt_string(void)
{
char *prompt;
int type;
type = pr_type;
prompt = pr_expand((ch_getflags() & CH_HELPFILE) ?
hproto : prproto[type], sc_width-so_s_width-so_e_width-2);
new_file = 0;
return (prompt);
}
char *
wait_message(void)
{
return (pr_expand(wproto, sc_width-so_s_width-so_e_width-2));
}