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