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