Fix typo.
[dragonfly.git] / contrib / less-4 / prompt.c
blob67b5ddcdbfdffcd00407ca78279f466511195968
1 /*
2 * Copyright (C) 1984-2007 Mark Nudelman
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.
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
9 */
13 * Prompting and other messages.
14 * There are three flavors of prompts, SHORT, MEDIUM and LONG,
15 * selected by the -m/-M options.
16 * There is also the "equals message", printed by the = command.
17 * A prompt is a message composed of various pieces, such as the
18 * name of the file being viewed, the percentage into the file, etc.
21 #include "less.h"
22 #include "position.h"
24 extern int pr_type;
25 extern int hit_eof;
26 extern int new_file;
27 extern int sc_width;
28 extern int so_s_width, so_e_width;
29 extern int linenums;
30 extern int hshift;
31 extern int sc_height;
32 extern int jump_sline;
33 extern int less_is_more;
34 extern IFILE curr_ifile;
35 #if EDITOR
36 extern char *editor;
37 extern char *editproto;
38 #endif
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[] =
55 "Waiting for data";
56 static constant char more_proto[] =
57 "--More--(?eEND ?x- Next\\: %x.:?pB%pB\\%:byte %bB?s/%s...%t)";
59 public char *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];
65 static char *mp;
68 * Initialize the prompt prototype strings.
70 public void
71 init_prompt()
73 prproto[0] = save(s_proto);
74 prproto[1] = save(less_is_more ? more_proto : m_proto);
75 prproto[2] = save(M_proto);
76 eqproto = save(e_proto);
77 hproto = save(h_proto);
78 wproto = save(w_proto);
82 * Append a string to the end of the message.
84 static void
85 ap_str(s)
86 char *s;
88 int len;
90 len = strlen(s);
91 if (mp + len >= message + PROMPT_SIZE)
92 len = message + PROMPT_SIZE - mp - 1;
93 strncpy(mp, s, len);
94 mp += len;
95 *mp = '\0';
99 * Append a character to the end of the message.
101 static void
102 ap_char(c)
103 char c;
105 char buf[2];
107 buf[0] = c;
108 buf[1] = '\0';
109 ap_str(buf);
113 * Append a POSITION (as a decimal integer) to the end of the message.
115 static void
116 ap_pos(pos)
117 POSITION pos;
119 char buf[INT_STRLEN_BOUND(pos) + 2];
121 postoa(pos, buf);
122 ap_str(buf);
126 * Append a line number to the end of the message.
128 static void
129 ap_linenum(linenum)
130 LINENUM linenum;
132 char buf[INT_STRLEN_BOUND(linenum) + 2];
134 linenumtoa(linenum, buf);
135 ap_str(buf);
139 * Append an integer to the end of the message.
141 static void
142 ap_int(num)
143 int num;
145 char buf[INT_STRLEN_BOUND(num) + 2];
147 inttoa(num, buf);
148 ap_str(buf);
152 * Append a question mark to the end of the message.
154 static void
155 ap_quest()
157 ap_str("?");
161 * Return the "current" byte offset in the file.
163 static POSITION
164 curr_byte(where)
165 int where;
167 POSITION pos;
169 pos = position(where);
170 while (pos == NULL_POSITION && where >= 0 && where < sc_height-1)
171 pos = position(++where);
172 if (pos == NULL_POSITION)
173 pos = ch_length();
174 return (pos);
178 * Return the value of a prototype conditional.
179 * A prototype string may include conditionals which consist of a
180 * question mark followed by a single letter.
181 * Here we decode that letter and return the appropriate boolean value.
183 static int
184 cond(c, where)
185 char c;
186 int where;
188 POSITION len;
190 switch (c)
192 case 'a': /* Anything in the message yet? */
193 return (mp > message);
194 case 'b': /* Current byte offset known? */
195 return (curr_byte(where) != NULL_POSITION);
196 case 'c':
197 return (hshift != 0);
198 case 'e': /* At end of file? */
199 return (hit_eof);
200 case 'f': /* Filename known? */
201 return (strcmp(get_filename(curr_ifile), "-") != 0);
202 case 'l': /* Line number known? */
203 case 'd': /* Same as l */
204 return (linenums);
205 case 'L': /* Final line number known? */
206 case 'D': /* Final page number known? */
207 return (linenums && ch_length() != NULL_POSITION);
208 case 'm': /* More than one file? */
209 #if TAGS
210 return (ntags() ? (ntags() > 1) : (nifile() > 1));
211 #else
212 return (nifile() > 1);
213 #endif
214 case 'n': /* First prompt in a new file? */
215 #if TAGS
216 return (ntags() ? 1 : new_file);
217 #else
218 return (new_file);
219 #endif
220 case 'p': /* Percent into file (bytes) known? */
221 return (curr_byte(where) != NULL_POSITION &&
222 ch_length() > 0);
223 case 'P': /* Percent into file (lines) known? */
224 return (currline(where) != 0 &&
225 (len = ch_length()) > 0 &&
226 find_linenum(len) != 0);
227 case 's': /* Size of file known? */
228 case 'B':
229 return (ch_length() != NULL_POSITION);
230 case 'x': /* Is there a "next" file? */
231 #if TAGS
232 if (ntags())
233 return (0);
234 #endif
235 return (next_ifile(curr_ifile) != NULL_IFILE);
237 return (0);
241 * Decode a "percent" prototype character.
242 * A prototype string may include various "percent" escapes;
243 * that is, a percent sign followed by a single letter.
244 * Here we decode that letter and take the appropriate action,
245 * usually by appending something to the message being built.
247 static void
248 protochar(c, where, iseditproto)
249 int c;
250 int where;
251 int iseditproto;
253 POSITION pos;
254 POSITION len;
255 int n;
256 LINENUM linenum;
257 LINENUM last_linenum;
258 IFILE h;
260 #undef PAGE_NUM
261 #define PAGE_NUM(linenum) ((((linenum) - 1) / (sc_height - 1)) + 1)
263 switch (c)
265 case 'b': /* Current byte offset */
266 pos = curr_byte(where);
267 if (pos != NULL_POSITION)
268 ap_pos(pos);
269 else
270 ap_quest();
271 break;
272 case 'c':
273 ap_int(hshift);
274 break;
275 case 'd': /* Current page number */
276 linenum = currline(where);
277 if (linenum > 0 && sc_height > 1)
278 ap_linenum(PAGE_NUM(linenum));
279 else
280 ap_quest();
281 break;
282 case 'D': /* Final page number */
283 /* Find the page number of the last byte in the file (len-1). */
284 len = ch_length();
285 if (len == NULL_POSITION)
286 ap_quest();
287 else if (len == 0)
288 /* An empty file has no pages. */
289 ap_linenum(0);
290 else
292 linenum = find_linenum(len - 1);
293 if (linenum <= 0)
294 ap_quest();
295 else
296 ap_linenum(PAGE_NUM(linenum));
298 break;
299 #if EDITOR
300 case 'E': /* Editor name */
301 ap_str(editor);
302 break;
303 #endif
304 case 'f': /* File name */
305 ap_str(get_filename(curr_ifile));
306 break;
307 case 'i': /* Index into list of files */
308 #if TAGS
309 if (ntags())
310 ap_int(curr_tag());
311 else
312 #endif
313 ap_int(get_index(curr_ifile));
314 break;
315 case 'l': /* Current line number */
316 linenum = currline(where);
317 if (linenum != 0)
318 ap_linenum(linenum);
319 else
320 ap_quest();
321 break;
322 case 'L': /* Final line number */
323 len = ch_length();
324 if (len == NULL_POSITION || len == ch_zero() ||
325 (linenum = find_linenum(len)) <= 0)
326 ap_quest();
327 else
328 ap_linenum(linenum-1);
329 break;
330 case 'm': /* Number of files */
331 #if TAGS
332 n = ntags();
333 if (n)
334 ap_int(n);
335 else
336 #endif
337 ap_int(nifile());
338 break;
339 case 'p': /* Percent into file (bytes) */
340 pos = curr_byte(where);
341 len = ch_length();
342 if (pos != NULL_POSITION && len > 0)
343 ap_int(percentage(pos,len));
344 else
345 ap_quest();
346 break;
347 case 'P': /* Percent into file (lines) */
348 linenum = currline(where);
349 if (linenum == 0 ||
350 (len = ch_length()) == NULL_POSITION || len == ch_zero() ||
351 (last_linenum = find_linenum(len)) <= 0)
352 ap_quest();
353 else
354 ap_int(percentage(linenum, last_linenum));
355 break;
356 case 's': /* Size of file */
357 case 'B':
358 len = ch_length();
359 if (len != NULL_POSITION)
360 ap_pos(len);
361 else
362 ap_quest();
363 break;
364 case 't': /* Truncate trailing spaces in the message */
365 while (mp > message && mp[-1] == ' ')
366 mp--;
367 break;
368 case 'T': /* Type of list */
369 #if TAGS
370 if (ntags())
371 ap_str("tag");
372 else
373 #endif
374 ap_str("file");
375 break;
376 case 'x': /* Name of next file */
377 h = next_ifile(curr_ifile);
378 if (h != NULL_IFILE)
379 ap_str(get_filename(h));
380 else
381 ap_quest();
382 break;
387 * Skip a false conditional.
388 * When a false condition is found (either a false IF or the ELSE part
389 * of a true IF), this routine scans the prototype string to decide
390 * where to resume parsing the string.
391 * We must keep track of nested IFs and skip them properly.
393 static char *
394 skipcond(p)
395 register char *p;
397 register int iflevel;
400 * We came in here after processing a ? or :,
401 * so we start nested one level deep.
403 iflevel = 1;
405 for (;;) switch (*++p)
407 case '?':
409 * Start of a nested IF.
411 iflevel++;
412 break;
413 case ':':
415 * Else.
416 * If this matches the IF we came in here with,
417 * then we're done.
419 if (iflevel == 1)
420 return (p);
421 break;
422 case '.':
424 * Endif.
425 * If this matches the IF we came in here with,
426 * then we're done.
428 if (--iflevel == 0)
429 return (p);
430 break;
431 case '\\':
433 * Backslash escapes the next character.
435 ++p;
436 break;
437 case '\0':
439 * Whoops. Hit end of string.
440 * This is a malformed conditional, but just treat it
441 * as if all active conditionals ends here.
443 return (p-1);
445 /*NOTREACHED*/
449 * Decode a char that represents a position on the screen.
451 static char *
452 wherechar(p, wp)
453 char *p;
454 int *wp;
456 switch (*p)
458 case 'b': case 'd': case 'l': case 'p': case 'P':
459 switch (*++p)
461 case 't': *wp = TOP; break;
462 case 'm': *wp = MIDDLE; break;
463 case 'b': *wp = BOTTOM; break;
464 case 'B': *wp = BOTTOM_PLUS_ONE; break;
465 case 'j': *wp = adjsline(jump_sline); break;
466 default: *wp = TOP; p--; break;
469 return (p);
473 * Construct a message based on a prototype string.
475 public char *
476 pr_expand(proto, maxwidth)
477 char *proto;
478 int maxwidth;
480 register char *p;
481 register int c;
482 int where;
484 mp = message;
486 if (*proto == '\0')
487 return ("");
489 for (p = proto; *p != '\0'; p++)
491 switch (*p)
493 default: /* Just put the character in the message */
494 ap_char(*p);
495 break;
496 case '\\': /* Backslash escapes the next character */
497 p++;
498 ap_char(*p);
499 break;
500 case '?': /* Conditional (IF) */
501 if ((c = *++p) == '\0')
502 --p;
503 else
505 where = 0;
506 p = wherechar(p, &where);
507 if (!cond(c, where))
508 p = skipcond(p);
510 break;
511 case ':': /* ELSE */
512 p = skipcond(p);
513 break;
514 case '.': /* ENDIF */
515 break;
516 case '%': /* Percent escape */
517 if ((c = *++p) == '\0')
518 --p;
519 else
521 where = 0;
522 p = wherechar(p, &where);
523 protochar(c, where,
524 #if EDITOR
525 (proto == editproto));
526 #else
528 #endif
531 break;
535 if (mp == message)
536 return ("");
537 if (maxwidth > 0 && mp >= message + maxwidth)
540 * Message is too long.
541 * Return just the final portion of it.
543 return (mp - maxwidth);
545 return (message);
549 * Return a message suitable for printing by the "=" command.
551 public char *
552 eq_message()
554 return (pr_expand(eqproto, 0));
558 * Return a prompt.
559 * This depends on the prompt type (SHORT, MEDIUM, LONG), etc.
560 * If we can't come up with an appropriate prompt, return NULL
561 * and the caller will prompt with a colon.
563 public char *
564 pr_string()
566 char *prompt;
567 int type;
569 type = (!less_is_more) ? pr_type : pr_type ? 0 : 1;
570 prompt = pr_expand((ch_getflags() & CH_HELPFILE) ?
571 hproto : prproto[type],
572 sc_width-so_s_width-so_e_width-2);
573 new_file = 0;
574 return (prompt);
578 * Return a message suitable for printing while waiting in the F command.
580 public char *
581 wait_message()
583 return (pr_expand(wproto, sc_width-so_s_width-so_e_width-2));