xref: /dragonfly/contrib/less/prompt.c (revision 1310e0bb)
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()
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(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
100 ap_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
114 ap_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
127 ap_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
140 ap_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
153 ap_quest()
154 {
155 	ap_str("?");
156 }
157 
158 /*
159  * Return the "current" byte offset in the file.
160  */
161 	static POSITION
162 curr_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
182 cond(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 		return (strcmp(get_filename(curr_ifile), "-") != 0);
200 	case 'l':	/* Line number known? */
201 	case 'd':	/* Same as l */
202 		return (linenums);
203 	case 'L':	/* Final line number known? */
204 	case 'D':	/* Final page number known? */
205 		return (linenums && ch_length() != NULL_POSITION);
206 	case 'm':	/* More than one file? */
207 #if TAGS
208 		return (ntags() ? (ntags() > 1) : (nifile() > 1));
209 #else
210 		return (nifile() > 1);
211 #endif
212 	case 'n':	/* First prompt in a new file? */
213 #if TAGS
214 		return (ntags() ? 1 : new_file);
215 #else
216 		return (new_file);
217 #endif
218 	case 'p':	/* Percent into file (bytes) known? */
219 		return (curr_byte(where) != NULL_POSITION &&
220 				ch_length() > 0);
221 	case 'P':	/* Percent into file (lines) known? */
222 		return (currline(where) != 0 &&
223 				(len = ch_length()) > 0 &&
224 				find_linenum(len) != 0);
225 	case 's':	/* Size of file known? */
226 	case 'B':
227 		return (ch_length() != NULL_POSITION);
228 	case 'x':	/* Is there a "next" file? */
229 #if TAGS
230 		if (ntags())
231 			return (0);
232 #endif
233 		return (next_ifile(curr_ifile) != NULL_IFILE);
234 	}
235 	return (0);
236 }
237 
238 /*
239  * Decode a "percent" prototype character.
240  * A prototype string may include various "percent" escapes;
241  * that is, a percent sign followed by a single letter.
242  * Here we decode that letter and take the appropriate action,
243  * usually by appending something to the message being built.
244  */
245 	static void
246 protochar(c, where, iseditproto)
247 	int c;
248 	int where;
249 	int iseditproto;
250 {
251 	POSITION pos;
252 	POSITION len;
253 	int n;
254 	LINENUM linenum;
255 	LINENUM last_linenum;
256 	IFILE h;
257 
258 #undef  PAGE_NUM
259 #define PAGE_NUM(linenum)  ((((linenum) - 1) / (sc_height - 1)) + 1)
260 
261 	switch (c)
262 	{
263 	case 'b':	/* Current byte offset */
264 		pos = curr_byte(where);
265 		if (pos != NULL_POSITION)
266 			ap_pos(pos);
267 		else
268 			ap_quest();
269 		break;
270 	case 'c':
271 		ap_int(hshift);
272 		break;
273 	case 'd':	/* Current page number */
274 		linenum = currline(where);
275 		if (linenum > 0 && sc_height > 1)
276 			ap_linenum(PAGE_NUM(linenum));
277 		else
278 			ap_quest();
279 		break;
280 	case 'D':	/* Final page number */
281 		/* Find the page number of the last byte in the file (len-1). */
282 		len = ch_length();
283 		if (len == NULL_POSITION)
284 			ap_quest();
285 		else if (len == 0)
286 			/* An empty file has no pages. */
287 			ap_linenum(0);
288 		else
289 		{
290 			linenum = find_linenum(len - 1);
291 			if (linenum <= 0)
292 				ap_quest();
293 			else
294 				ap_linenum(PAGE_NUM(linenum));
295 		}
296 		break;
297 #if EDITOR
298 	case 'E':	/* Editor name */
299 		ap_str(editor);
300 		break;
301 #endif
302 	case 'f':	/* File name */
303 		ap_str(get_filename(curr_ifile));
304 		break;
305 	case 'F':	/* Last component of file name */
306 		ap_str(last_component(get_filename(curr_ifile)));
307 		break;
308 	case 'i':	/* Index into list of files */
309 #if TAGS
310 		if (ntags())
311 			ap_int(curr_tag());
312 		else
313 #endif
314 			ap_int(get_index(curr_ifile));
315 		break;
316 	case 'l':	/* Current line number */
317 		linenum = currline(where);
318 		if (linenum != 0)
319 			ap_linenum(linenum);
320 		else
321 			ap_quest();
322 		break;
323 	case 'L':	/* Final line number */
324 		len = ch_length();
325 		if (len == NULL_POSITION || len == ch_zero() ||
326 		    (linenum = find_linenum(len)) <= 0)
327 			ap_quest();
328 		else
329 			ap_linenum(linenum-1);
330 		break;
331 	case 'm':	/* Number of files */
332 #if TAGS
333 		n = ntags();
334 		if (n)
335 			ap_int(n);
336 		else
337 #endif
338 			ap_int(nifile());
339 		break;
340 	case 'p':	/* Percent into file (bytes) */
341 		pos = curr_byte(where);
342 		len = ch_length();
343 		if (pos != NULL_POSITION && len > 0)
344 			ap_int(percentage(pos,len));
345 		else
346 			ap_quest();
347 		break;
348 	case 'P':	/* Percent into file (lines) */
349 		linenum = currline(where);
350 		if (linenum == 0 ||
351 		    (len = ch_length()) == NULL_POSITION || len == ch_zero() ||
352 		    (last_linenum = find_linenum(len)) <= 0)
353 			ap_quest();
354 		else
355 			ap_int(percentage(linenum, last_linenum));
356 		break;
357 	case 's':	/* Size of file */
358 	case 'B':
359 		len = ch_length();
360 		if (len != NULL_POSITION)
361 			ap_pos(len);
362 		else
363 			ap_quest();
364 		break;
365 	case 't':	/* Truncate trailing spaces in the message */
366 		while (mp > message && mp[-1] == ' ')
367 			mp--;
368 		*mp = '\0';
369 		break;
370 	case 'T':	/* Type of list */
371 #if TAGS
372 		if (ntags())
373 			ap_str("tag");
374 		else
375 #endif
376 			ap_str("file");
377 		break;
378 	case 'x':	/* Name of next file */
379 		h = next_ifile(curr_ifile);
380 		if (h != NULL_IFILE)
381 			ap_str(get_filename(h));
382 		else
383 			ap_quest();
384 		break;
385 	}
386 }
387 
388 /*
389  * Skip a false conditional.
390  * When a false condition is found (either a false IF or the ELSE part
391  * of a true IF), this routine scans the prototype string to decide
392  * where to resume parsing the string.
393  * We must keep track of nested IFs and skip them properly.
394  */
395 	static constant char *
396 skipcond(p)
397 	register constant char *p;
398 {
399 	register int iflevel;
400 
401 	/*
402 	 * We came in here after processing a ? or :,
403 	 * so we start nested one level deep.
404 	 */
405 	iflevel = 1;
406 
407 	for (;;) switch (*++p)
408 	{
409 	case '?':
410 		/*
411 		 * Start of a nested IF.
412 		 */
413 		iflevel++;
414 		break;
415 	case ':':
416 		/*
417 		 * Else.
418 		 * If this matches the IF we came in here with,
419 		 * then we're done.
420 		 */
421 		if (iflevel == 1)
422 			return (p);
423 		break;
424 	case '.':
425 		/*
426 		 * Endif.
427 		 * If this matches the IF we came in here with,
428 		 * then we're done.
429 		 */
430 		if (--iflevel == 0)
431 			return (p);
432 		break;
433 	case '\\':
434 		/*
435 		 * Backslash escapes the next character.
436 		 */
437 		++p;
438 		break;
439 	case '\0':
440 		/*
441 		 * Whoops.  Hit end of string.
442 		 * This is a malformed conditional, but just treat it
443 		 * as if all active conditionals ends here.
444 		 */
445 		return (p-1);
446 	}
447 	/*NOTREACHED*/
448 }
449 
450 /*
451  * Decode a char that represents a position on the screen.
452  */
453 	static constant char *
454 wherechar(p, wp)
455 	char constant *p;
456 	int *wp;
457 {
458 	switch (*p)
459 	{
460 	case 'b': case 'd': case 'l': case 'p': case 'P':
461 		switch (*++p)
462 		{
463 		case 't':   *wp = TOP;			break;
464 		case 'm':   *wp = MIDDLE;		break;
465 		case 'b':   *wp = BOTTOM;		break;
466 		case 'B':   *wp = BOTTOM_PLUS_ONE;	break;
467 		case 'j':   *wp = adjsline(jump_sline);	break;
468 		default:    *wp = TOP;  p--;		break;
469 		}
470 	}
471 	return (p);
472 }
473 
474 /*
475  * Construct a message based on a prototype string.
476  */
477 	public char *
478 pr_expand(proto, maxwidth)
479 	constant char *proto;
480 	int maxwidth;
481 {
482 	register constant char *p;
483 	register int c;
484 	int where;
485 
486 	mp = message;
487 
488 	if (*proto == '\0')
489 		return ("");
490 
491 	for (p = proto;  *p != '\0';  p++)
492 	{
493 		switch (*p)
494 		{
495 		default:	/* Just put the character in the message */
496 			ap_char(*p);
497 			break;
498 		case '\\':	/* Backslash escapes the next character */
499 			p++;
500 			ap_char(*p);
501 			break;
502 		case '?':	/* Conditional (IF) */
503 			if ((c = *++p) == '\0')
504 				--p;
505 			else
506 			{
507 				where = 0;
508 				p = wherechar(p, &where);
509 				if (!cond(c, where))
510 					p = skipcond(p);
511 			}
512 			break;
513 		case ':':	/* ELSE */
514 			p = skipcond(p);
515 			break;
516 		case '.':	/* ENDIF */
517 			break;
518 		case '%':	/* Percent escape */
519 			if ((c = *++p) == '\0')
520 				--p;
521 			else
522 			{
523 				where = 0;
524 				p = wherechar(p, &where);
525 				protochar(c, where,
526 #if EDITOR
527 					(proto == editproto));
528 #else
529 					0);
530 #endif
531 
532 			}
533 			break;
534 		}
535 	}
536 
537 	if (mp == message)
538 		return ("");
539 	if (maxwidth > 0 && mp >= message + maxwidth)
540 	{
541 		/*
542 		 * Message is too long.
543 		 * Return just the final portion of it.
544 		 */
545 		return (mp - maxwidth);
546 	}
547 	return (message);
548 }
549 
550 /*
551  * Return a message suitable for printing by the "=" command.
552  */
553 	public char *
554 eq_message()
555 {
556 	return (pr_expand(eqproto, 0));
557 }
558 
559 /*
560  * Return a prompt.
561  * This depends on the prompt type (SHORT, MEDIUM, LONG), etc.
562  * If we can't come up with an appropriate prompt, return NULL
563  * and the caller will prompt with a colon.
564  */
565 	public char *
566 pr_string()
567 {
568 	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 			sc_width-so_s_width-so_e_width-2);
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()
584 {
585 	return (pr_expand(wproto, sc_width-so_s_width-so_e_width-2));
586 }
587