xref: /minix3/external/bsd/less/dist/prompt.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: prompt.c,v 1.4 2013/09/04 19:44:21 tron Exp $	*/
2f7cf2976SLionel Sambuc 
3f7cf2976SLionel Sambuc /*
4*84d9c625SLionel Sambuc  * Copyright (C) 1984-2012  Mark Nudelman
5f7cf2976SLionel Sambuc  *
6f7cf2976SLionel Sambuc  * You may distribute under the terms of either the GNU General Public
7f7cf2976SLionel Sambuc  * License or the Less License, as specified in the README file.
8f7cf2976SLionel Sambuc  *
9*84d9c625SLionel Sambuc  * For more information, see the README file.
10f7cf2976SLionel Sambuc  */
11f7cf2976SLionel Sambuc 
12f7cf2976SLionel Sambuc 
13f7cf2976SLionel Sambuc /*
14f7cf2976SLionel Sambuc  * Prompting and other messages.
15f7cf2976SLionel Sambuc  * There are three flavors of prompts, SHORT, MEDIUM and LONG,
16f7cf2976SLionel Sambuc  * selected by the -m/-M options.
17f7cf2976SLionel Sambuc  * There is also the "equals message", printed by the = command.
18f7cf2976SLionel Sambuc  * A prompt is a message composed of various pieces, such as the
19f7cf2976SLionel Sambuc  * name of the file being viewed, the percentage into the file, etc.
20f7cf2976SLionel Sambuc  */
21f7cf2976SLionel Sambuc 
22f7cf2976SLionel Sambuc #include "less.h"
23f7cf2976SLionel Sambuc #include "position.h"
24f7cf2976SLionel Sambuc 
25f7cf2976SLionel Sambuc extern int pr_type;
26f7cf2976SLionel Sambuc extern int new_file;
27f7cf2976SLionel Sambuc extern int sc_width;
28f7cf2976SLionel Sambuc extern int so_s_width, so_e_width;
29f7cf2976SLionel Sambuc extern int linenums;
30f7cf2976SLionel Sambuc extern int hshift;
31f7cf2976SLionel Sambuc extern int sc_height;
32f7cf2976SLionel Sambuc extern int jump_sline;
33f7cf2976SLionel Sambuc extern int less_is_more;
34f7cf2976SLionel Sambuc extern IFILE curr_ifile;
35f7cf2976SLionel Sambuc #if EDITOR
36f7cf2976SLionel Sambuc extern char *editor;
37f7cf2976SLionel Sambuc extern char *editproto;
38f7cf2976SLionel Sambuc #endif
39f7cf2976SLionel Sambuc 
40f7cf2976SLionel Sambuc /*
41f7cf2976SLionel Sambuc  * Prototypes for the three flavors of prompts.
42f7cf2976SLionel Sambuc  * These strings are expanded by pr_expand().
43f7cf2976SLionel Sambuc  */
44f7cf2976SLionel Sambuc static constant char s_proto[] =
45f7cf2976SLionel Sambuc   "?n?f%f .?m(%T %i of %m) ..?e(END) ?x- Next\\: %x..%t";
46f7cf2976SLionel Sambuc static constant char m_proto[] =
47f7cf2976SLionel Sambuc   "?n?f%f .?m(%T %i of %m) ..?e(END) ?x- Next\\: %x.:?pB%pB\\%:byte %bB?s/%s...%t";
48f7cf2976SLionel Sambuc static constant char M_proto[] =
49f7cf2976SLionel Sambuc   "?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";
50f7cf2976SLionel Sambuc static constant char e_proto[] =
51f7cf2976SLionel Sambuc   "?f%f .?m(%T %i of %m) .?ltlines %lt-%lb?L/%L. .byte %bB?s/%s. ?e(END) :?pB%pB\\%..%t";
52f7cf2976SLionel Sambuc static constant char h_proto[] =
53f7cf2976SLionel Sambuc   "HELP -- ?eEND -- Press g to see it again:Press RETURN for more., or q when done";
54f7cf2976SLionel Sambuc static constant char w_proto[] =
55f7cf2976SLionel Sambuc   "Waiting for data";
56f7cf2976SLionel Sambuc static constant char more_proto[] =
57f7cf2976SLionel Sambuc   "--More--(?eEND ?x- Next\\: %x.:?pB%pB\\%:byte %bB?s/%s...%t)";
58f7cf2976SLionel Sambuc 
59f7cf2976SLionel Sambuc public char constant *prproto[3];
60f7cf2976SLionel Sambuc public char constant *eqproto = e_proto;
61f7cf2976SLionel Sambuc public char constant *hproto = h_proto;
62f7cf2976SLionel Sambuc public char constant *wproto = w_proto;
63f7cf2976SLionel Sambuc 
64f7cf2976SLionel Sambuc static char message[PROMPT_SIZE];
65f7cf2976SLionel Sambuc static char *mp;
66f7cf2976SLionel Sambuc 
67f7cf2976SLionel Sambuc static void ap_pos __P((POSITION));
68f7cf2976SLionel Sambuc static void ap_int __P((int));
69f7cf2976SLionel Sambuc static void ap_str __P((char *));
70f7cf2976SLionel Sambuc static void ap_char __P((int));
71f7cf2976SLionel Sambuc static void ap_quest __P((void));
72f7cf2976SLionel Sambuc static POSITION curr_byte __P((int));
73f7cf2976SLionel Sambuc static int cond __P((int, int));
74f7cf2976SLionel Sambuc static void protochar __P((int, int, int));
75f7cf2976SLionel Sambuc static const char *skipcond __P((const char *));
76f7cf2976SLionel Sambuc static const char *wherechar __P((const char *, int *));
77f7cf2976SLionel Sambuc 
78f7cf2976SLionel Sambuc /*
79f7cf2976SLionel Sambuc  * Initialize the prompt prototype strings.
80f7cf2976SLionel Sambuc  */
81f7cf2976SLionel Sambuc 	public void
init_prompt()82f7cf2976SLionel Sambuc init_prompt()
83f7cf2976SLionel Sambuc {
84f7cf2976SLionel Sambuc 	prproto[0] = save(s_proto);
85f7cf2976SLionel Sambuc 	prproto[1] = save(less_is_more ? more_proto : m_proto);
86f7cf2976SLionel Sambuc 	prproto[2] = save(M_proto);
87f7cf2976SLionel Sambuc 	eqproto = save(e_proto);
88f7cf2976SLionel Sambuc 	hproto = save(h_proto);
89f7cf2976SLionel Sambuc 	wproto = save(w_proto);
90f7cf2976SLionel Sambuc }
91f7cf2976SLionel Sambuc 
92f7cf2976SLionel Sambuc /*
93f7cf2976SLionel Sambuc  * Append a string to the end of the message.
94f7cf2976SLionel Sambuc  */
95f7cf2976SLionel Sambuc 	static void
ap_str(s)96f7cf2976SLionel Sambuc ap_str(s)
97f7cf2976SLionel Sambuc 	char *s;
98f7cf2976SLionel Sambuc {
99f7cf2976SLionel Sambuc 	int len;
100f7cf2976SLionel Sambuc 
101f7cf2976SLionel Sambuc 	len = strlen(s);
102f7cf2976SLionel Sambuc 	if (mp + len >= message + PROMPT_SIZE)
103f7cf2976SLionel Sambuc 		len = message + PROMPT_SIZE - mp - 1;
104f7cf2976SLionel Sambuc 	strncpy(mp, s, len);
105f7cf2976SLionel Sambuc 	mp += len;
106f7cf2976SLionel Sambuc 	*mp = '\0';
107f7cf2976SLionel Sambuc }
108f7cf2976SLionel Sambuc 
109f7cf2976SLionel Sambuc /*
110f7cf2976SLionel Sambuc  * Append a character to the end of the message.
111f7cf2976SLionel Sambuc  */
112f7cf2976SLionel Sambuc 	static void
ap_char(i)113f7cf2976SLionel Sambuc ap_char(i)
114f7cf2976SLionel Sambuc 	int i;
115f7cf2976SLionel Sambuc {
116f7cf2976SLionel Sambuc 	char buf[2];
117f7cf2976SLionel Sambuc 	char c = (char)i;
118f7cf2976SLionel Sambuc 
119f7cf2976SLionel Sambuc 	buf[0] = c;
120f7cf2976SLionel Sambuc 	buf[1] = '\0';
121f7cf2976SLionel Sambuc 	ap_str(buf);
122f7cf2976SLionel Sambuc }
123f7cf2976SLionel Sambuc 
124f7cf2976SLionel Sambuc /*
125f7cf2976SLionel Sambuc  * Append a POSITION (as a decimal integer) to the end of the message.
126f7cf2976SLionel Sambuc  */
127f7cf2976SLionel Sambuc 	static void
ap_pos(pos)128f7cf2976SLionel Sambuc ap_pos(pos)
129f7cf2976SLionel Sambuc 	POSITION pos;
130f7cf2976SLionel Sambuc {
131f7cf2976SLionel Sambuc 	char buf[INT_STRLEN_BOUND(pos) + 2];
132f7cf2976SLionel Sambuc 
133f7cf2976SLionel Sambuc 	postoa(pos, buf);
134f7cf2976SLionel Sambuc 	ap_str(buf);
135f7cf2976SLionel Sambuc }
136f7cf2976SLionel Sambuc 
137f7cf2976SLionel Sambuc /*
138f7cf2976SLionel Sambuc  * Append a line number to the end of the message.
139f7cf2976SLionel Sambuc  */
140f7cf2976SLionel Sambuc  	static void
ap_linenum(linenum)141f7cf2976SLionel Sambuc ap_linenum(linenum)
142f7cf2976SLionel Sambuc 	LINENUM linenum;
143f7cf2976SLionel Sambuc {
144f7cf2976SLionel Sambuc 	char buf[INT_STRLEN_BOUND(linenum) + 2];
145f7cf2976SLionel Sambuc 
146f7cf2976SLionel Sambuc 	linenumtoa(linenum, buf);
147f7cf2976SLionel Sambuc 	ap_str(buf);
148f7cf2976SLionel Sambuc }
149f7cf2976SLionel Sambuc 
150f7cf2976SLionel Sambuc /*
151f7cf2976SLionel Sambuc  * Append an integer to the end of the message.
152f7cf2976SLionel Sambuc  */
153f7cf2976SLionel Sambuc 	static void
ap_int(num)154f7cf2976SLionel Sambuc ap_int(num)
155f7cf2976SLionel Sambuc 	int num;
156f7cf2976SLionel Sambuc {
157f7cf2976SLionel Sambuc 	char buf[INT_STRLEN_BOUND(num) + 2];
158f7cf2976SLionel Sambuc 
159f7cf2976SLionel Sambuc 	inttoa(num, buf);
160f7cf2976SLionel Sambuc 	ap_str(buf);
161f7cf2976SLionel Sambuc }
162f7cf2976SLionel Sambuc 
163f7cf2976SLionel Sambuc /*
164f7cf2976SLionel Sambuc  * Append a question mark to the end of the message.
165f7cf2976SLionel Sambuc  */
166f7cf2976SLionel Sambuc 	static void
ap_quest()167f7cf2976SLionel Sambuc ap_quest()
168f7cf2976SLionel Sambuc {
169f7cf2976SLionel Sambuc 	ap_str("?");
170f7cf2976SLionel Sambuc }
171f7cf2976SLionel Sambuc 
172f7cf2976SLionel Sambuc /*
173f7cf2976SLionel Sambuc  * Return the "current" byte offset in the file.
174f7cf2976SLionel Sambuc  */
175f7cf2976SLionel Sambuc 	static POSITION
curr_byte(where)176f7cf2976SLionel Sambuc curr_byte(where)
177f7cf2976SLionel Sambuc 	int where;
178f7cf2976SLionel Sambuc {
179f7cf2976SLionel Sambuc 	POSITION pos;
180f7cf2976SLionel Sambuc 
181f7cf2976SLionel Sambuc 	pos = position(where);
182f7cf2976SLionel Sambuc 	while (pos == NULL_POSITION && where >= 0 && where < sc_height-1)
183f7cf2976SLionel Sambuc 		pos = position(++where);
184f7cf2976SLionel Sambuc 	if (pos == NULL_POSITION)
185f7cf2976SLionel Sambuc 		pos = ch_length();
186f7cf2976SLionel Sambuc 	return (pos);
187f7cf2976SLionel Sambuc }
188f7cf2976SLionel Sambuc 
189f7cf2976SLionel Sambuc /*
190f7cf2976SLionel Sambuc  * Return the value of a prototype conditional.
191f7cf2976SLionel Sambuc  * A prototype string may include conditionals which consist of a
192f7cf2976SLionel Sambuc  * question mark followed by a single letter.
193f7cf2976SLionel Sambuc  * Here we decode that letter and return the appropriate boolean value.
194f7cf2976SLionel Sambuc  */
195f7cf2976SLionel Sambuc 	static int
cond(c,where)196f7cf2976SLionel Sambuc cond(c, where)
197f7cf2976SLionel Sambuc 	char c;
198f7cf2976SLionel Sambuc 	int where;
199f7cf2976SLionel Sambuc {
200f7cf2976SLionel Sambuc 	POSITION len;
201f7cf2976SLionel Sambuc 
202f7cf2976SLionel Sambuc 	switch (c)
203f7cf2976SLionel Sambuc 	{
204f7cf2976SLionel Sambuc 	case 'a':	/* Anything in the message yet? */
205f7cf2976SLionel Sambuc 		return (mp > message);
206f7cf2976SLionel Sambuc 	case 'b':	/* Current byte offset known? */
207f7cf2976SLionel Sambuc 		return (curr_byte(where) != NULL_POSITION);
208f7cf2976SLionel Sambuc 	case 'c':
209f7cf2976SLionel Sambuc 		return (hshift != 0);
210f7cf2976SLionel Sambuc 	case 'e':	/* At end of file? */
211f7cf2976SLionel Sambuc 		return (eof_displayed());
212f7cf2976SLionel Sambuc 	case 'f':	/* Filename known? */
213f7cf2976SLionel Sambuc 		return (strcmp(get_filename(curr_ifile), "-") != 0);
214f7cf2976SLionel Sambuc 	case 'l':	/* Line number known? */
215f7cf2976SLionel Sambuc 	case 'd':	/* Same as l */
216f7cf2976SLionel Sambuc 		return (linenums);
217f7cf2976SLionel Sambuc 	case 'L':	/* Final line number known? */
218f7cf2976SLionel Sambuc 	case 'D':	/* Final page number known? */
219f7cf2976SLionel Sambuc 		return (linenums && ch_length() != NULL_POSITION);
220f7cf2976SLionel Sambuc 	case 'm':	/* More than one file? */
221f7cf2976SLionel Sambuc #if TAGS
222f7cf2976SLionel Sambuc 		return (ntags() ? (ntags() > 1) : (nifile() > 1));
223f7cf2976SLionel Sambuc #else
224f7cf2976SLionel Sambuc 		return (nifile() > 1);
225f7cf2976SLionel Sambuc #endif
226f7cf2976SLionel Sambuc 	case 'n':	/* First prompt in a new file? */
227f7cf2976SLionel Sambuc #if TAGS
228f7cf2976SLionel Sambuc 		return (ntags() ? 1 : new_file);
229f7cf2976SLionel Sambuc #else
230f7cf2976SLionel Sambuc 		return (new_file);
231f7cf2976SLionel Sambuc #endif
232f7cf2976SLionel Sambuc 	case 'p':	/* Percent into file (bytes) known? */
233f7cf2976SLionel Sambuc 		return (curr_byte(where) != NULL_POSITION &&
234f7cf2976SLionel Sambuc 				ch_length() > 0);
235f7cf2976SLionel Sambuc 	case 'P':	/* Percent into file (lines) known? */
236f7cf2976SLionel Sambuc 		return (currline(where) != 0 &&
237f7cf2976SLionel Sambuc 				(len = ch_length()) > 0 &&
238f7cf2976SLionel Sambuc 				find_linenum(len) != 0);
239f7cf2976SLionel Sambuc 	case 's':	/* Size of file known? */
240f7cf2976SLionel Sambuc 	case 'B':
241f7cf2976SLionel Sambuc 		return (ch_length() != NULL_POSITION);
242f7cf2976SLionel Sambuc 	case 'x':	/* Is there a "next" file? */
243f7cf2976SLionel Sambuc #if TAGS
244f7cf2976SLionel Sambuc 		if (ntags())
245f7cf2976SLionel Sambuc 			return (0);
246f7cf2976SLionel Sambuc #endif
247f7cf2976SLionel Sambuc 		return (next_ifile(curr_ifile) != NULL_IFILE);
248f7cf2976SLionel Sambuc 	}
249f7cf2976SLionel Sambuc 	return (0);
250f7cf2976SLionel Sambuc }
251f7cf2976SLionel Sambuc 
252f7cf2976SLionel Sambuc /*
253f7cf2976SLionel Sambuc  * Decode a "percent" prototype character.
254f7cf2976SLionel Sambuc  * A prototype string may include various "percent" escapes;
255f7cf2976SLionel Sambuc  * that is, a percent sign followed by a single letter.
256f7cf2976SLionel Sambuc  * Here we decode that letter and take the appropriate action,
257f7cf2976SLionel Sambuc  * usually by appending something to the message being built.
258f7cf2976SLionel Sambuc  */
259f7cf2976SLionel Sambuc 	static void
protochar(c,where,iseditproto)260f7cf2976SLionel Sambuc protochar(c, where, iseditproto)
261f7cf2976SLionel Sambuc 	int c;
262f7cf2976SLionel Sambuc 	int where;
263f7cf2976SLionel Sambuc 	int iseditproto;
264f7cf2976SLionel Sambuc {
265f7cf2976SLionel Sambuc 	POSITION pos;
266f7cf2976SLionel Sambuc 	POSITION len;
267f7cf2976SLionel Sambuc 	int n;
268f7cf2976SLionel Sambuc 	LINENUM linenum;
269f7cf2976SLionel Sambuc 	LINENUM last_linenum;
270f7cf2976SLionel Sambuc 	IFILE h;
271f7cf2976SLionel Sambuc 
272f7cf2976SLionel Sambuc #undef  PAGE_NUM
273f7cf2976SLionel Sambuc #define PAGE_NUM(linenum)  ((((linenum) - 1) / (sc_height - 1)) + 1)
274f7cf2976SLionel Sambuc 
275f7cf2976SLionel Sambuc 	switch (c)
276f7cf2976SLionel Sambuc 	{
277f7cf2976SLionel Sambuc 	case 'b':	/* Current byte offset */
278f7cf2976SLionel Sambuc 		pos = curr_byte(where);
279f7cf2976SLionel Sambuc 		if (pos != NULL_POSITION)
280f7cf2976SLionel Sambuc 			ap_pos(pos);
281f7cf2976SLionel Sambuc 		else
282f7cf2976SLionel Sambuc 			ap_quest();
283f7cf2976SLionel Sambuc 		break;
284f7cf2976SLionel Sambuc 	case 'c':
285f7cf2976SLionel Sambuc 		ap_int(hshift);
286f7cf2976SLionel Sambuc 		break;
287f7cf2976SLionel Sambuc 	case 'd':	/* Current page number */
288f7cf2976SLionel Sambuc 		linenum = currline(where);
289f7cf2976SLionel Sambuc 		if (linenum > 0 && sc_height > 1)
290f7cf2976SLionel Sambuc 			ap_linenum(PAGE_NUM(linenum));
291f7cf2976SLionel Sambuc 		else
292f7cf2976SLionel Sambuc 			ap_quest();
293f7cf2976SLionel Sambuc 		break;
294f7cf2976SLionel Sambuc 	case 'D':	/* Final page number */
295f7cf2976SLionel Sambuc 		/* Find the page number of the last byte in the file (len-1). */
296f7cf2976SLionel Sambuc 		len = ch_length();
297f7cf2976SLionel Sambuc 		if (len == NULL_POSITION)
298f7cf2976SLionel Sambuc 			ap_quest();
299f7cf2976SLionel Sambuc 		else if (len == 0)
300f7cf2976SLionel Sambuc 			/* An empty file has no pages. */
301f7cf2976SLionel Sambuc 			ap_linenum(0);
302f7cf2976SLionel Sambuc 		else
303f7cf2976SLionel Sambuc 		{
304f7cf2976SLionel Sambuc 			linenum = find_linenum(len - 1);
305f7cf2976SLionel Sambuc 			if (linenum <= 0)
306f7cf2976SLionel Sambuc 				ap_quest();
307f7cf2976SLionel Sambuc 			else
308f7cf2976SLionel Sambuc 				ap_linenum(PAGE_NUM(linenum));
309f7cf2976SLionel Sambuc 		}
310f7cf2976SLionel Sambuc 		break;
311f7cf2976SLionel Sambuc #if EDITOR
312f7cf2976SLionel Sambuc 	case 'E':	/* Editor name */
313f7cf2976SLionel Sambuc 		ap_str(editor);
314f7cf2976SLionel Sambuc 		break;
315f7cf2976SLionel Sambuc #endif
316f7cf2976SLionel Sambuc 	case 'f':	/* File name */
317f7cf2976SLionel Sambuc 		ap_str(get_filename(curr_ifile));
318f7cf2976SLionel Sambuc 		break;
319f7cf2976SLionel Sambuc 	case 'F':	/* Last component of file name */
320f7cf2976SLionel Sambuc 		ap_str(last_component(get_filename(curr_ifile)));
321f7cf2976SLionel Sambuc 		break;
322f7cf2976SLionel Sambuc 	case 'i':	/* Index into list of files */
323f7cf2976SLionel Sambuc #if TAGS
324f7cf2976SLionel Sambuc 		if (ntags())
325f7cf2976SLionel Sambuc 			ap_int(curr_tag());
326f7cf2976SLionel Sambuc 		else
327f7cf2976SLionel Sambuc #endif
328f7cf2976SLionel Sambuc 			ap_int(get_index(curr_ifile));
329f7cf2976SLionel Sambuc 		break;
330f7cf2976SLionel Sambuc 	case 'l':	/* Current line number */
331f7cf2976SLionel Sambuc 		linenum = currline(where);
332f7cf2976SLionel Sambuc 		if (linenum != 0)
333f7cf2976SLionel Sambuc 			ap_linenum(linenum);
334f7cf2976SLionel Sambuc 		else
335f7cf2976SLionel Sambuc 			ap_quest();
336f7cf2976SLionel Sambuc 		break;
337f7cf2976SLionel Sambuc 	case 'L':	/* Final line number */
338f7cf2976SLionel Sambuc 		len = ch_length();
339f7cf2976SLionel Sambuc 		if (len == NULL_POSITION || len == ch_zero() ||
340f7cf2976SLionel Sambuc 		    (linenum = find_linenum(len)) <= 0)
341f7cf2976SLionel Sambuc 			ap_quest();
342f7cf2976SLionel Sambuc 		else
343f7cf2976SLionel Sambuc 			ap_linenum(linenum-1);
344f7cf2976SLionel Sambuc 		break;
345f7cf2976SLionel Sambuc 	case 'm':	/* Number of files */
346f7cf2976SLionel Sambuc #if TAGS
347f7cf2976SLionel Sambuc 		n = ntags();
348f7cf2976SLionel Sambuc 		if (n)
349f7cf2976SLionel Sambuc 			ap_int(n);
350f7cf2976SLionel Sambuc 		else
351f7cf2976SLionel Sambuc #endif
352f7cf2976SLionel Sambuc 			ap_int(nifile());
353f7cf2976SLionel Sambuc 		break;
354f7cf2976SLionel Sambuc 	case 'p':	/* Percent into file (bytes) */
355f7cf2976SLionel Sambuc 		pos = curr_byte(where);
356f7cf2976SLionel Sambuc 		len = ch_length();
357f7cf2976SLionel Sambuc 		if (pos != NULL_POSITION && len > 0)
358f7cf2976SLionel Sambuc 			ap_int(percentage(pos,len));
359f7cf2976SLionel Sambuc 		else
360f7cf2976SLionel Sambuc 			ap_quest();
361f7cf2976SLionel Sambuc 		break;
362f7cf2976SLionel Sambuc 	case 'P':	/* Percent into file (lines) */
363f7cf2976SLionel Sambuc 		linenum = currline(where);
364f7cf2976SLionel Sambuc 		if (linenum == 0 ||
365f7cf2976SLionel Sambuc 		    (len = ch_length()) == NULL_POSITION || len == ch_zero() ||
366f7cf2976SLionel Sambuc 		    (last_linenum = find_linenum(len)) <= 0)
367f7cf2976SLionel Sambuc 			ap_quest();
368f7cf2976SLionel Sambuc 		else
369f7cf2976SLionel Sambuc 			ap_int(percentage(linenum, last_linenum));
370f7cf2976SLionel Sambuc 		break;
371f7cf2976SLionel Sambuc 	case 's':	/* Size of file */
372f7cf2976SLionel Sambuc 	case 'B':
373f7cf2976SLionel Sambuc 		len = ch_length();
374f7cf2976SLionel Sambuc 		if (len != NULL_POSITION)
375f7cf2976SLionel Sambuc 			ap_pos(len);
376f7cf2976SLionel Sambuc 		else
377f7cf2976SLionel Sambuc 			ap_quest();
378f7cf2976SLionel Sambuc 		break;
379f7cf2976SLionel Sambuc 	case 't':	/* Truncate trailing spaces in the message */
380f7cf2976SLionel Sambuc 		while (mp > message && mp[-1] == ' ')
381f7cf2976SLionel Sambuc 			mp--;
382f7cf2976SLionel Sambuc 		*mp = '\0';
383f7cf2976SLionel Sambuc 		break;
384f7cf2976SLionel Sambuc 	case 'T':	/* Type of list */
385f7cf2976SLionel Sambuc #if TAGS
386f7cf2976SLionel Sambuc 		if (ntags())
387f7cf2976SLionel Sambuc 			ap_str("tag");
388f7cf2976SLionel Sambuc 		else
389f7cf2976SLionel Sambuc #endif
390f7cf2976SLionel Sambuc 			ap_str("file");
391f7cf2976SLionel Sambuc 		break;
392f7cf2976SLionel Sambuc 	case 'x':	/* Name of next file */
393f7cf2976SLionel Sambuc 		h = next_ifile(curr_ifile);
394f7cf2976SLionel Sambuc 		if (h != NULL_IFILE)
395f7cf2976SLionel Sambuc 			ap_str(get_filename(h));
396f7cf2976SLionel Sambuc 		else
397f7cf2976SLionel Sambuc 			ap_quest();
398f7cf2976SLionel Sambuc 		break;
399f7cf2976SLionel Sambuc 	}
400f7cf2976SLionel Sambuc }
401f7cf2976SLionel Sambuc 
402f7cf2976SLionel Sambuc /*
403f7cf2976SLionel Sambuc  * Skip a false conditional.
404f7cf2976SLionel Sambuc  * When a false condition is found (either a false IF or the ELSE part
405f7cf2976SLionel Sambuc  * of a true IF), this routine scans the prototype string to decide
406f7cf2976SLionel Sambuc  * where to resume parsing the string.
407f7cf2976SLionel Sambuc  * We must keep track of nested IFs and skip them properly.
408f7cf2976SLionel Sambuc  */
409*84d9c625SLionel Sambuc 	static constant char *
skipcond(p)410f7cf2976SLionel Sambuc skipcond(p)
411*84d9c625SLionel Sambuc 	register constant char *p;
412f7cf2976SLionel Sambuc {
413f7cf2976SLionel Sambuc 	register int iflevel;
414f7cf2976SLionel Sambuc 
415f7cf2976SLionel Sambuc 	/*
416f7cf2976SLionel Sambuc 	 * We came in here after processing a ? or :,
417f7cf2976SLionel Sambuc 	 * so we start nested one level deep.
418f7cf2976SLionel Sambuc 	 */
419f7cf2976SLionel Sambuc 	iflevel = 1;
420f7cf2976SLionel Sambuc 
421f7cf2976SLionel Sambuc 	for (;;) switch (*++p)
422f7cf2976SLionel Sambuc 	{
423f7cf2976SLionel Sambuc 	case '?':
424f7cf2976SLionel Sambuc 		/*
425f7cf2976SLionel Sambuc 		 * Start of a nested IF.
426f7cf2976SLionel Sambuc 		 */
427f7cf2976SLionel Sambuc 		iflevel++;
428f7cf2976SLionel Sambuc 		break;
429f7cf2976SLionel Sambuc 	case ':':
430f7cf2976SLionel Sambuc 		/*
431f7cf2976SLionel Sambuc 		 * Else.
432f7cf2976SLionel Sambuc 		 * If this matches the IF we came in here with,
433f7cf2976SLionel Sambuc 		 * then we're done.
434f7cf2976SLionel Sambuc 		 */
435f7cf2976SLionel Sambuc 		if (iflevel == 1)
436f7cf2976SLionel Sambuc 			return (p);
437f7cf2976SLionel Sambuc 		break;
438f7cf2976SLionel Sambuc 	case '.':
439f7cf2976SLionel Sambuc 		/*
440f7cf2976SLionel Sambuc 		 * Endif.
441f7cf2976SLionel Sambuc 		 * If this matches the IF we came in here with,
442f7cf2976SLionel Sambuc 		 * then we're done.
443f7cf2976SLionel Sambuc 		 */
444f7cf2976SLionel Sambuc 		if (--iflevel == 0)
445f7cf2976SLionel Sambuc 			return (p);
446f7cf2976SLionel Sambuc 		break;
447f7cf2976SLionel Sambuc 	case '\\':
448f7cf2976SLionel Sambuc 		/*
449f7cf2976SLionel Sambuc 		 * Backslash escapes the next character.
450f7cf2976SLionel Sambuc 		 */
451f7cf2976SLionel Sambuc 		++p;
452f7cf2976SLionel Sambuc 		break;
453f7cf2976SLionel Sambuc 	case '\0':
454f7cf2976SLionel Sambuc 		/*
455f7cf2976SLionel Sambuc 		 * Whoops.  Hit end of string.
456f7cf2976SLionel Sambuc 		 * This is a malformed conditional, but just treat it
457f7cf2976SLionel Sambuc 		 * as if all active conditionals ends here.
458f7cf2976SLionel Sambuc 		 */
459f7cf2976SLionel Sambuc 		return (p-1);
460f7cf2976SLionel Sambuc 	}
461f7cf2976SLionel Sambuc 	/*NOTREACHED*/
462f7cf2976SLionel Sambuc }
463f7cf2976SLionel Sambuc 
464f7cf2976SLionel Sambuc /*
465f7cf2976SLionel Sambuc  * Decode a char that represents a position on the screen.
466f7cf2976SLionel Sambuc  */
467*84d9c625SLionel Sambuc 	static constant char *
wherechar(p,wp)468f7cf2976SLionel Sambuc wherechar(p, wp)
469*84d9c625SLionel Sambuc 	char constant *p;
470f7cf2976SLionel Sambuc 	int *wp;
471f7cf2976SLionel Sambuc {
472f7cf2976SLionel Sambuc 	switch (*p)
473f7cf2976SLionel Sambuc 	{
474f7cf2976SLionel Sambuc 	case 'b': case 'd': case 'l': case 'p': case 'P':
475f7cf2976SLionel Sambuc 		switch (*++p)
476f7cf2976SLionel Sambuc 		{
477f7cf2976SLionel Sambuc 		case 't':   *wp = TOP;			break;
478f7cf2976SLionel Sambuc 		case 'm':   *wp = MIDDLE;		break;
479f7cf2976SLionel Sambuc 		case 'b':   *wp = BOTTOM;		break;
480f7cf2976SLionel Sambuc 		case 'B':   *wp = BOTTOM_PLUS_ONE;	break;
481f7cf2976SLionel Sambuc 		case 'j':   *wp = adjsline(jump_sline);	break;
482f7cf2976SLionel Sambuc 		default:    *wp = TOP;  p--;		break;
483f7cf2976SLionel Sambuc 		}
484f7cf2976SLionel Sambuc 	}
485f7cf2976SLionel Sambuc 	return (p);
486f7cf2976SLionel Sambuc }
487f7cf2976SLionel Sambuc 
488f7cf2976SLionel Sambuc /*
489f7cf2976SLionel Sambuc  * Construct a message based on a prototype string.
490f7cf2976SLionel Sambuc  */
491f7cf2976SLionel Sambuc 	public char *
pr_expand(proto,maxwidth)492f7cf2976SLionel Sambuc pr_expand(proto, maxwidth)
493*84d9c625SLionel Sambuc 	constant char *proto;
494f7cf2976SLionel Sambuc 	int maxwidth;
495f7cf2976SLionel Sambuc {
496*84d9c625SLionel Sambuc 	register constant char *p;
497f7cf2976SLionel Sambuc 	register int c;
498f7cf2976SLionel Sambuc 	int where;
499f7cf2976SLionel Sambuc 
500f7cf2976SLionel Sambuc 	mp = message;
501f7cf2976SLionel Sambuc 
502f7cf2976SLionel Sambuc 	if (*proto == '\0')
503f7cf2976SLionel Sambuc 		return ("");
504f7cf2976SLionel Sambuc 
505f7cf2976SLionel Sambuc 	for (p = proto;  *p != '\0';  p++)
506f7cf2976SLionel Sambuc 	{
507f7cf2976SLionel Sambuc 		switch (*p)
508f7cf2976SLionel Sambuc 		{
509f7cf2976SLionel Sambuc 		default:	/* Just put the character in the message */
510f7cf2976SLionel Sambuc 			ap_char(*p);
511f7cf2976SLionel Sambuc 			break;
512f7cf2976SLionel Sambuc 		case '\\':	/* Backslash escapes the next character */
513f7cf2976SLionel Sambuc 			p++;
514f7cf2976SLionel Sambuc 			ap_char(*p);
515f7cf2976SLionel Sambuc 			break;
516f7cf2976SLionel Sambuc 		case '?':	/* Conditional (IF) */
517f7cf2976SLionel Sambuc 			if ((c = *++p) == '\0')
518f7cf2976SLionel Sambuc 				--p;
519f7cf2976SLionel Sambuc 			else
520f7cf2976SLionel Sambuc 			{
521f7cf2976SLionel Sambuc 				where = 0;
522f7cf2976SLionel Sambuc 				p = wherechar(p, &where);
523f7cf2976SLionel Sambuc 				if (!cond(c, where))
524f7cf2976SLionel Sambuc 					p = skipcond(p);
525f7cf2976SLionel Sambuc 			}
526f7cf2976SLionel Sambuc 			break;
527f7cf2976SLionel Sambuc 		case ':':	/* ELSE */
528f7cf2976SLionel Sambuc 			p = skipcond(p);
529f7cf2976SLionel Sambuc 			break;
530f7cf2976SLionel Sambuc 		case '.':	/* ENDIF */
531f7cf2976SLionel Sambuc 			break;
532f7cf2976SLionel Sambuc 		case '%':	/* Percent escape */
533f7cf2976SLionel Sambuc 			if ((c = *++p) == '\0')
534f7cf2976SLionel Sambuc 				--p;
535f7cf2976SLionel Sambuc 			else
536f7cf2976SLionel Sambuc 			{
537f7cf2976SLionel Sambuc 				where = 0;
538f7cf2976SLionel Sambuc 				p = wherechar(p, &where);
539f7cf2976SLionel Sambuc 				protochar(c, where,
540f7cf2976SLionel Sambuc #if EDITOR
541f7cf2976SLionel Sambuc 					(proto == editproto));
542f7cf2976SLionel Sambuc #else
543f7cf2976SLionel Sambuc 					0);
544f7cf2976SLionel Sambuc #endif
545f7cf2976SLionel Sambuc 
546f7cf2976SLionel Sambuc 			}
547f7cf2976SLionel Sambuc 			break;
548f7cf2976SLionel Sambuc 		}
549f7cf2976SLionel Sambuc 	}
550f7cf2976SLionel Sambuc 
551f7cf2976SLionel Sambuc 	if (mp == message)
552f7cf2976SLionel Sambuc 		return ("");
553f7cf2976SLionel Sambuc 	if (maxwidth > 0 && mp >= message + maxwidth)
554f7cf2976SLionel Sambuc 	{
555f7cf2976SLionel Sambuc 		/*
556f7cf2976SLionel Sambuc 		 * Message is too long.
557f7cf2976SLionel Sambuc 		 * Return just the final portion of it.
558f7cf2976SLionel Sambuc 		 */
559f7cf2976SLionel Sambuc 		return (mp - maxwidth);
560f7cf2976SLionel Sambuc 	}
561f7cf2976SLionel Sambuc 	return (message);
562f7cf2976SLionel Sambuc }
563f7cf2976SLionel Sambuc 
564f7cf2976SLionel Sambuc /*
565f7cf2976SLionel Sambuc  * Return a message suitable for printing by the "=" command.
566f7cf2976SLionel Sambuc  */
567f7cf2976SLionel Sambuc 	public char *
eq_message()568f7cf2976SLionel Sambuc eq_message()
569f7cf2976SLionel Sambuc {
570f7cf2976SLionel Sambuc 	return (pr_expand(eqproto, 0));
571f7cf2976SLionel Sambuc }
572f7cf2976SLionel Sambuc 
573f7cf2976SLionel Sambuc /*
574f7cf2976SLionel Sambuc  * Return a prompt.
575f7cf2976SLionel Sambuc  * This depends on the prompt type (SHORT, MEDIUM, LONG), etc.
576f7cf2976SLionel Sambuc  * If we can't come up with an appropriate prompt, return NULL
577f7cf2976SLionel Sambuc  * and the caller will prompt with a colon.
578f7cf2976SLionel Sambuc  */
579f7cf2976SLionel Sambuc 	public char *
pr_string()580f7cf2976SLionel Sambuc pr_string()
581f7cf2976SLionel Sambuc {
582f7cf2976SLionel Sambuc 	char *prompt;
583f7cf2976SLionel Sambuc 	int type;
584f7cf2976SLionel Sambuc 
585f7cf2976SLionel Sambuc 	type = (!less_is_more) ? pr_type : pr_type ? 0 : 1;
586f7cf2976SLionel Sambuc 	prompt = pr_expand((ch_getflags() & CH_HELPFILE) ?
587f7cf2976SLionel Sambuc 				hproto : prproto[type],
588f7cf2976SLionel Sambuc 			sc_width-so_s_width-so_e_width-2);
589f7cf2976SLionel Sambuc 	new_file = 0;
590f7cf2976SLionel Sambuc 	return (prompt);
591f7cf2976SLionel Sambuc }
592f7cf2976SLionel Sambuc 
593f7cf2976SLionel Sambuc /*
594f7cf2976SLionel Sambuc  * Return a message suitable for printing while waiting in the F command.
595f7cf2976SLionel Sambuc  */
596f7cf2976SLionel Sambuc 	public char *
wait_message()597f7cf2976SLionel Sambuc wait_message()
598f7cf2976SLionel Sambuc {
599f7cf2976SLionel Sambuc 	return (pr_expand(wproto, sc_width-so_s_width-so_e_width-2));
600f7cf2976SLionel Sambuc }
601