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