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