Changeset View
Changeset View
Standalone View
Standalone View
contrib/less/prompt.c
Show First 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | |||||
static char message[PROMPT_SIZE]; | static char message[PROMPT_SIZE]; | ||||
static char *mp; | static char *mp; | ||||
/* | /* | ||||
* Initialize the prompt prototype strings. | * Initialize the prompt prototype strings. | ||||
*/ | */ | ||||
public void | public void | ||||
init_prompt() | init_prompt(void) | ||||
{ | { | ||||
prproto[0] = save(s_proto); | prproto[0] = save(s_proto); | ||||
prproto[1] = save(less_is_more ? more_proto : m_proto); | prproto[1] = save(less_is_more ? more_proto : m_proto); | ||||
prproto[2] = save(M_proto); | prproto[2] = save(M_proto); | ||||
eqproto = save(e_proto); | eqproto = save(e_proto); | ||||
hproto = save(h_proto); | hproto = save(h_proto); | ||||
wproto = save(w_proto); | wproto = save(w_proto); | ||||
} | } | ||||
/* | /* | ||||
* Append a string to the end of the message. | * Append a string to the end of the message. | ||||
*/ | */ | ||||
static void | static void | ||||
ap_str(s) | ap_str(char *s) | ||||
char *s; | |||||
{ | { | ||||
int len; | int len; | ||||
len = (int) strlen(s); | len = (int) strlen(s); | ||||
if (mp + len >= message + PROMPT_SIZE) | if (mp + len >= message + PROMPT_SIZE) | ||||
len = (int) (message + PROMPT_SIZE - mp - 1); | len = (int) (message + PROMPT_SIZE - mp - 1); | ||||
strncpy(mp, s, len); | strncpy(mp, s, len); | ||||
mp += len; | mp += len; | ||||
*mp = '\0'; | *mp = '\0'; | ||||
} | } | ||||
/* | /* | ||||
* Append a character to the end of the message. | * Append a character to the end of the message. | ||||
*/ | */ | ||||
static void | static void | ||||
ap_char(c) | ap_char(char c) | ||||
char c; | |||||
{ | { | ||||
char buf[2]; | char buf[2]; | ||||
buf[0] = c; | buf[0] = c; | ||||
buf[1] = '\0'; | buf[1] = '\0'; | ||||
ap_str(buf); | ap_str(buf); | ||||
} | } | ||||
/* | /* | ||||
* Append a POSITION (as a decimal integer) to the end of the message. | * Append a POSITION (as a decimal integer) to the end of the message. | ||||
*/ | */ | ||||
static void | static void | ||||
ap_pos(pos) | ap_pos(POSITION pos) | ||||
POSITION pos; | |||||
{ | { | ||||
char buf[INT_STRLEN_BOUND(pos) + 2]; | char buf[INT_STRLEN_BOUND(pos) + 2]; | ||||
postoa(pos, buf); | postoa(pos, buf); | ||||
ap_str(buf); | ap_str(buf); | ||||
} | } | ||||
/* | /* | ||||
* Append a line number to the end of the message. | * Append a line number to the end of the message. | ||||
*/ | */ | ||||
static void | static void | ||||
ap_linenum(linenum) | ap_linenum(LINENUM linenum) | ||||
LINENUM linenum; | |||||
{ | { | ||||
char buf[INT_STRLEN_BOUND(linenum) + 2]; | char buf[INT_STRLEN_BOUND(linenum) + 2]; | ||||
linenumtoa(linenum, buf); | linenumtoa(linenum, buf); | ||||
ap_str(buf); | ap_str(buf); | ||||
} | } | ||||
/* | /* | ||||
* Append an integer to the end of the message. | * Append an integer to the end of the message. | ||||
*/ | */ | ||||
static void | static void | ||||
ap_int(num) | ap_int(int num) | ||||
int num; | |||||
{ | { | ||||
char buf[INT_STRLEN_BOUND(num) + 2]; | char buf[INT_STRLEN_BOUND(num) + 2]; | ||||
inttoa(num, buf); | inttoa(num, buf); | ||||
ap_str(buf); | ap_str(buf); | ||||
} | } | ||||
/* | /* | ||||
* Append a question mark to the end of the message. | * Append a question mark to the end of the message. | ||||
*/ | */ | ||||
static void | static void | ||||
ap_quest() | ap_quest(void) | ||||
{ | { | ||||
ap_str("?"); | ap_str("?"); | ||||
} | } | ||||
/* | /* | ||||
* Return the "current" byte offset in the file. | * Return the "current" byte offset in the file. | ||||
*/ | */ | ||||
static POSITION | static POSITION | ||||
curr_byte(where) | curr_byte(int where) | ||||
int where; | |||||
{ | { | ||||
POSITION pos; | POSITION pos; | ||||
pos = position(where); | pos = position(where); | ||||
while (pos == NULL_POSITION && where >= 0 && where < sc_height-1) | while (pos == NULL_POSITION && where >= 0 && where < sc_height-1) | ||||
pos = position(++where); | pos = position(++where); | ||||
if (pos == NULL_POSITION) | if (pos == NULL_POSITION) | ||||
pos = ch_length(); | pos = ch_length(); | ||||
return (pos); | return (pos); | ||||
} | } | ||||
/* | /* | ||||
* Return the value of a prototype conditional. | * Return the value of a prototype conditional. | ||||
* A prototype string may include conditionals which consist of a | * A prototype string may include conditionals which consist of a | ||||
* question mark followed by a single letter. | * question mark followed by a single letter. | ||||
* Here we decode that letter and return the appropriate boolean value. | * Here we decode that letter and return the appropriate boolean value. | ||||
*/ | */ | ||||
static int | static int | ||||
cond(c, where) | cond(char c, int where) | ||||
char c; | |||||
int where; | |||||
{ | { | ||||
POSITION len; | POSITION len; | ||||
switch (c) | switch (c) | ||||
{ | { | ||||
case 'a': /* Anything in the message yet? */ | case 'a': /* Anything in the message yet? */ | ||||
return (mp > message); | return (mp > message); | ||||
case 'b': /* Current byte offset known? */ | case 'b': /* Current byte offset known? */ | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | |||||
/* | /* | ||||
* Decode a "percent" prototype character. | * Decode a "percent" prototype character. | ||||
* A prototype string may include various "percent" escapes; | * A prototype string may include various "percent" escapes; | ||||
* that is, a percent sign followed by a single letter. | * that is, a percent sign followed by a single letter. | ||||
* Here we decode that letter and take the appropriate action, | * Here we decode that letter and take the appropriate action, | ||||
* usually by appending something to the message being built. | * usually by appending something to the message being built. | ||||
*/ | */ | ||||
static void | static void | ||||
protochar(c, where, iseditproto) | protochar(int c, int where, int iseditproto) | ||||
int c; | |||||
int where; | |||||
int iseditproto; | |||||
{ | { | ||||
POSITION pos; | POSITION pos; | ||||
POSITION len; | POSITION len; | ||||
int n; | int n; | ||||
LINENUM linenum; | LINENUM linenum; | ||||
LINENUM last_linenum; | LINENUM last_linenum; | ||||
IFILE h; | IFILE h; | ||||
▲ Show 20 Lines • Show All 130 Lines • ▼ Show 20 Lines | |||||
/* | /* | ||||
* Skip a false conditional. | * Skip a false conditional. | ||||
* When a false condition is found (either a false IF or the ELSE part | * When a false condition is found (either a false IF or the ELSE part | ||||
* of a true IF), this routine scans the prototype string to decide | * of a true IF), this routine scans the prototype string to decide | ||||
* where to resume parsing the string. | * where to resume parsing the string. | ||||
* We must keep track of nested IFs and skip them properly. | * We must keep track of nested IFs and skip them properly. | ||||
*/ | */ | ||||
static constant char * | static constant char * | ||||
skipcond(p) | skipcond(constant char *p) | ||||
register constant char *p; | |||||
{ | { | ||||
register int iflevel; | int iflevel; | ||||
/* | /* | ||||
* We came in here after processing a ? or :, | * We came in here after processing a ? or :, | ||||
* so we start nested one level deep. | * so we start nested one level deep. | ||||
*/ | */ | ||||
iflevel = 1; | iflevel = 1; | ||||
for (;;) switch (*++p) | for (;;) switch (*++p) | ||||
Show All 38 Lines | skipcond(constant char *p) | ||||
} | } | ||||
/*NOTREACHED*/ | /*NOTREACHED*/ | ||||
} | } | ||||
/* | /* | ||||
* Decode a char that represents a position on the screen. | * Decode a char that represents a position on the screen. | ||||
*/ | */ | ||||
static constant char * | static constant char * | ||||
wherechar(p, wp) | wherechar(char constant *p, int *wp) | ||||
char constant *p; | |||||
int *wp; | |||||
{ | { | ||||
switch (*p) | switch (*p) | ||||
{ | { | ||||
case 'b': case 'd': case 'l': case 'p': case 'P': | case 'b': case 'd': case 'l': case 'p': case 'P': | ||||
switch (*++p) | switch (*++p) | ||||
{ | { | ||||
case 't': *wp = TOP; break; | case 't': *wp = TOP; break; | ||||
case 'm': *wp = MIDDLE; break; | case 'm': *wp = MIDDLE; break; | ||||
case 'b': *wp = BOTTOM; break; | case 'b': *wp = BOTTOM; break; | ||||
case 'B': *wp = BOTTOM_PLUS_ONE; break; | case 'B': *wp = BOTTOM_PLUS_ONE; break; | ||||
case 'j': *wp = adjsline(jump_sline); break; | case 'j': *wp = adjsline(jump_sline); break; | ||||
default: *wp = TOP; p--; break; | default: *wp = TOP; p--; break; | ||||
} | } | ||||
} | } | ||||
return (p); | return (p); | ||||
} | } | ||||
/* | /* | ||||
* Construct a message based on a prototype string. | * Construct a message based on a prototype string. | ||||
*/ | */ | ||||
public char * | public char * | ||||
pr_expand(proto, maxwidth) | pr_expand(constant char *proto, int maxwidth) | ||||
constant char *proto; | |||||
int maxwidth; | |||||
{ | { | ||||
register constant char *p; | constant char *p; | ||||
register int c; | int c; | ||||
int where; | int where; | ||||
mp = message; | mp = message; | ||||
if (*proto == '\0') | if (*proto == '\0') | ||||
return (""); | return (""); | ||||
for (p = proto; *p != '\0'; p++) | for (p = proto; *p != '\0'; p++) | ||||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | #endif | ||||
} | } | ||||
return (message); | return (message); | ||||
} | } | ||||
/* | /* | ||||
* Return a message suitable for printing by the "=" command. | * Return a message suitable for printing by the "=" command. | ||||
*/ | */ | ||||
public char * | public char * | ||||
eq_message() | eq_message(void) | ||||
{ | { | ||||
return (pr_expand(eqproto, 0)); | return (pr_expand(eqproto, 0)); | ||||
} | } | ||||
/* | /* | ||||
* Return a prompt. | * Return a prompt. | ||||
* This depends on the prompt type (SHORT, MEDIUM, LONG), etc. | * This depends on the prompt type (SHORT, MEDIUM, LONG), etc. | ||||
* If we can't come up with an appropriate prompt, return NULL | * If we can't come up with an appropriate prompt, return NULL | ||||
* and the caller will prompt with a colon. | * and the caller will prompt with a colon. | ||||
*/ | */ | ||||
public char * | public char * | ||||
pr_string() | pr_string(void) | ||||
{ | { | ||||
char *prompt; | char *prompt; | ||||
int type; | int type; | ||||
type = (!less_is_more) ? pr_type : pr_type ? 0 : 1; | type = (!less_is_more) ? pr_type : pr_type ? 0 : 1; | ||||
prompt = pr_expand((ch_getflags() & CH_HELPFILE) ? | prompt = pr_expand((ch_getflags() & CH_HELPFILE) ? | ||||
hproto : prproto[type], | hproto : prproto[type], | ||||
sc_width-so_s_width-so_e_width-2); | sc_width-so_s_width-so_e_width-2); | ||||
new_file = 0; | new_file = 0; | ||||
return (prompt); | return (prompt); | ||||
} | } | ||||
/* | /* | ||||
* Return a message suitable for printing while waiting in the F command. | * Return a message suitable for printing while waiting in the F command. | ||||
*/ | */ | ||||
public char * | public char * | ||||
wait_message() | wait_message(void) | ||||
{ | { | ||||
return (pr_expand(wproto, sc_width-so_s_width-so_e_width-2)); | return (pr_expand(wproto, sc_width-so_s_width-so_e_width-2)); | ||||
} | } |