1 /* $NetBSD: prompt.c,v 1.4 2013/09/04 19:44:21 tron Exp $ */
4 * Copyright (C) 1984-2012 Mark Nudelman
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.
9 * For more information, see the README file.
14 * Prompting and other messages.
15 * There are three flavors of prompts, SHORT, MEDIUM and LONG,
16 * selected by the -m/-M options.
17 * There is also the "equals message", printed by the = command.
18 * A prompt is a message composed of various pieces, such as the
19 * name of the file being viewed, the percentage into the file, etc.
28 extern int so_s_width
, so_e_width
;
32 extern int jump_sline
;
33 extern int less_is_more
;
34 extern IFILE curr_ifile
;
37 extern char *editproto
;
41 * Prototypes for the three flavors of prompts.
42 * These strings are expanded by pr_expand().
44 static constant
char s_proto
[] =
45 "?n?f%f .?m(%T %i of %m) ..?e(END) ?x- Next\\: %x..%t";
46 static constant
char m_proto
[] =
47 "?n?f%f .?m(%T %i of %m) ..?e(END) ?x- Next\\: %x.:?pB%pB\\%:byte %bB?s/%s...%t";
48 static constant
char M_proto
[] =
49 "?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";
50 static constant
char e_proto
[] =
51 "?f%f .?m(%T %i of %m) .?ltlines %lt-%lb?L/%L. .byte %bB?s/%s. ?e(END) :?pB%pB\\%..%t";
52 static constant
char h_proto
[] =
53 "HELP -- ?eEND -- Press g to see it again:Press RETURN for more., or q when done";
54 static constant
char w_proto
[] =
56 static constant
char more_proto
[] =
57 "--More--(?eEND ?x- Next\\: %x.:?pB%pB\\%:byte %bB?s/%s...%t)";
59 public char constant
*prproto
[3];
60 public char constant
*eqproto
= e_proto
;
61 public char constant
*hproto
= h_proto
;
62 public char constant
*wproto
= w_proto
;
64 static char message
[PROMPT_SIZE
];
67 static void ap_pos
__P((POSITION
));
68 static void ap_int
__P((int));
69 static void ap_str
__P((char *));
70 static void ap_char
__P((int));
71 static void ap_quest
__P((void));
72 static POSITION curr_byte
__P((int));
73 static int cond
__P((int, int));
74 static void protochar
__P((int, int, int));
75 static const char *skipcond
__P((const char *));
76 static const char *wherechar
__P((const char *, int *));
79 * Initialize the prompt prototype strings.
84 prproto
[0] = save(s_proto
);
85 prproto
[1] = save(less_is_more
? more_proto
: m_proto
);
86 prproto
[2] = save(M_proto
);
87 eqproto
= save(e_proto
);
88 hproto
= save(h_proto
);
89 wproto
= save(w_proto
);
93 * Append a string to the end of the message.
102 if (mp
+ len
>= message
+ PROMPT_SIZE
)
103 len
= message
+ PROMPT_SIZE
- mp
- 1;
110 * Append a character to the end of the message.
125 * Append a POSITION (as a decimal integer) to the end of the message.
131 char buf
[INT_STRLEN_BOUND(pos
) + 2];
138 * Append a line number to the end of the message.
144 char buf
[INT_STRLEN_BOUND(linenum
) + 2];
146 linenumtoa(linenum
, buf
);
151 * Append an integer to the end of the message.
157 char buf
[INT_STRLEN_BOUND(num
) + 2];
164 * Append a question mark to the end of the message.
173 * Return the "current" byte offset in the file.
181 pos
= position(where
);
182 while (pos
== NULL_POSITION
&& where
>= 0 && where
< sc_height
-1)
183 pos
= position(++where
);
184 if (pos
== NULL_POSITION
)
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.
204 case 'a': /* Anything in the message yet? */
205 return (mp
> message
);
206 case 'b': /* Current byte offset known? */
207 return (curr_byte(where
) != NULL_POSITION
);
209 return (hshift
!= 0);
210 case 'e': /* At end of file? */
211 return (eof_displayed());
212 case 'f': /* Filename known? */
213 return (strcmp(get_filename(curr_ifile
), "-") != 0);
214 case 'l': /* Line number known? */
215 case 'd': /* Same as l */
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? */
222 return (ntags() ? (ntags() > 1) : (nifile() > 1));
224 return (nifile() > 1);
226 case 'n': /* First prompt in a new file? */
228 return (ntags() ? 1 : new_file
);
232 case 'p': /* Percent into file (bytes) known? */
233 return (curr_byte(where
) != NULL_POSITION
&&
235 case 'P': /* Percent into file (lines) known? */
236 return (currline(where
) != 0 &&
237 (len
= ch_length()) > 0 &&
238 find_linenum(len
) != 0);
239 case 's': /* Size of file known? */
241 return (ch_length() != NULL_POSITION
);
242 case 'x': /* Is there a "next" file? */
247 return (next_ifile(curr_ifile
) != NULL_IFILE
);
253 * Decode a "percent" prototype character.
254 * A prototype string may include various "percent" escapes;
255 * that is, a percent sign followed by a single letter.
256 * Here we decode that letter and take the appropriate action,
257 * usually by appending something to the message being built.
260 protochar(c
, where
, iseditproto
)
269 LINENUM last_linenum
;
273 #define PAGE_NUM(linenum) ((((linenum) - 1) / (sc_height - 1)) + 1)
277 case 'b': /* Current byte offset */
278 pos
= curr_byte(where
);
279 if (pos
!= NULL_POSITION
)
287 case 'd': /* Current page number */
288 linenum
= currline(where
);
289 if (linenum
> 0 && sc_height
> 1)
290 ap_linenum(PAGE_NUM(linenum
));
294 case 'D': /* Final page number */
295 /* Find the page number of the last byte in the file (len-1). */
297 if (len
== NULL_POSITION
)
300 /* An empty file has no pages. */
304 linenum
= find_linenum(len
- 1);
308 ap_linenum(PAGE_NUM(linenum
));
312 case 'E': /* Editor name */
316 case 'f': /* File name */
317 ap_str(get_filename(curr_ifile
));
319 case 'F': /* Last component of file name */
320 ap_str(last_component(get_filename(curr_ifile
)));
322 case 'i': /* Index into list of files */
328 ap_int(get_index(curr_ifile
));
330 case 'l': /* Current line number */
331 linenum
= currline(where
);
337 case 'L': /* Final line number */
339 if (len
== NULL_POSITION
|| len
== ch_zero() ||
340 (linenum
= find_linenum(len
)) <= 0)
343 ap_linenum(linenum
-1);
345 case 'm': /* Number of files */
354 case 'p': /* Percent into file (bytes) */
355 pos
= curr_byte(where
);
357 if (pos
!= NULL_POSITION
&& len
> 0)
358 ap_int(percentage(pos
,len
));
362 case 'P': /* Percent into file (lines) */
363 linenum
= currline(where
);
365 (len
= ch_length()) == NULL_POSITION
|| len
== ch_zero() ||
366 (last_linenum
= find_linenum(len
)) <= 0)
369 ap_int(percentage(linenum
, last_linenum
));
371 case 's': /* Size of file */
374 if (len
!= NULL_POSITION
)
379 case 't': /* Truncate trailing spaces in the message */
380 while (mp
> message
&& mp
[-1] == ' ')
384 case 'T': /* Type of list */
392 case 'x': /* Name of next file */
393 h
= next_ifile(curr_ifile
);
395 ap_str(get_filename(h
));
403 * Skip a false conditional.
404 * When a false condition is found (either a false IF or the ELSE part
405 * of a true IF), this routine scans the prototype string to decide
406 * where to resume parsing the string.
407 * We must keep track of nested IFs and skip them properly.
409 static constant
char *
411 register constant
char *p
;
413 register int iflevel
;
416 * We came in here after processing a ? or :,
417 * so we start nested one level deep.
421 for (;;) switch (*++p
)
425 * Start of a nested IF.
432 * If this matches the IF we came in here with,
441 * If this matches the IF we came in here with,
449 * Backslash escapes the next character.
455 * Whoops. Hit end of string.
456 * This is a malformed conditional, but just treat it
457 * as if all active conditionals ends here.
465 * Decode a char that represents a position on the screen.
467 static constant
char *
474 case 'b': case 'd': case 'l': case 'p': case 'P':
477 case 't': *wp
= TOP
; break;
478 case 'm': *wp
= MIDDLE
; break;
479 case 'b': *wp
= BOTTOM
; break;
480 case 'B': *wp
= BOTTOM_PLUS_ONE
; break;
481 case 'j': *wp
= adjsline(jump_sline
); break;
482 default: *wp
= TOP
; p
--; break;
489 * Construct a message based on a prototype string.
492 pr_expand(proto
, maxwidth
)
493 constant
char *proto
;
496 register constant
char *p
;
505 for (p
= proto
; *p
!= '\0'; p
++)
509 default: /* Just put the character in the message */
512 case '\\': /* Backslash escapes the next character */
516 case '?': /* Conditional (IF) */
517 if ((c
= *++p
) == '\0')
522 p
= wherechar(p
, &where
);
530 case '.': /* ENDIF */
532 case '%': /* Percent escape */
533 if ((c
= *++p
) == '\0')
538 p
= wherechar(p
, &where
);
541 (proto
== editproto
));
553 if (maxwidth
> 0 && mp
>= message
+ maxwidth
)
556 * Message is too long.
557 * Return just the final portion of it.
559 return (mp
- maxwidth
);
565 * Return a message suitable for printing by the "=" command.
570 return (pr_expand(eqproto
, 0));
575 * This depends on the prompt type (SHORT, MEDIUM, LONG), etc.
576 * If we can't come up with an appropriate prompt, return NULL
577 * and the caller will prompt with a colon.
585 type
= (!less_is_more
) ? pr_type
: pr_type
? 0 : 1;
586 prompt
= pr_expand((ch_getflags() & CH_HELPFILE
) ?
587 hproto
: prproto
[type
],
588 sc_width
-so_s_width
-so_e_width
-2);
594 * Return a message suitable for printing while waiting in the F command.
599 return (pr_expand(wproto
, sc_width
-so_s_width
-so_e_width
-2));