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