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