]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/less/prompt.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / contrib / less / prompt.c
1 /* $FreeBSD$ */
2 /*
3  * Copyright (C) 1984-2011  Mark Nudelman
4  *
5  * You may distribute under the terms of either the GNU General Public
6  * License or the Less License, as specified in the README file.
7  *
8  * For more information about less, or for information on how to 
9  * contact the author, see the README file.
10  */
11
12
13 /*
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.
20  */
21
22 #include "less.h"
23 #include "position.h"
24
25 extern int pr_type;
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
39
40 /*
41  * Prototypes for the three flavors of prompts.
42  * These strings are expanded by pr_expand().
43  */
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)";
58
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;
63
64 static char message[PROMPT_SIZE];
65 static char *mp;
66
67 /*
68  * Initialize the prompt prototype strings.
69  */
70         public void
71 init_prompt()
72 {
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);
79 }
80
81 /*
82  * Append a string to the end of the message.
83  */
84         static void
85 ap_str(s)
86         char *s;
87 {
88         int len;
89
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';
96 }
97
98 /*
99  * Append a character to the end of the message.
100  */
101         static void
102 ap_char(c)
103         char c;
104 {
105         char buf[2];
106
107         buf[0] = c;
108         buf[1] = '\0';
109         ap_str(buf);
110 }
111
112 /*
113  * Append a POSITION (as a decimal integer) to the end of the message.
114  */
115         static void
116 ap_pos(pos)
117         POSITION pos;
118 {
119         char buf[INT_STRLEN_BOUND(pos) + 2];
120
121         postoa(pos, buf);
122         ap_str(buf);
123 }
124
125 /*
126  * Append a line number to the end of the message.
127  */
128         static void
129 ap_linenum(linenum)
130         LINENUM linenum;
131 {
132         char buf[INT_STRLEN_BOUND(linenum) + 2];
133
134         linenumtoa(linenum, buf);
135         ap_str(buf);
136 }
137
138 /*
139  * Append an integer to the end of the message.
140  */
141         static void
142 ap_int(num)
143         int num;
144 {
145         char buf[INT_STRLEN_BOUND(num) + 2];
146
147         inttoa(num, buf);
148         ap_str(buf);
149 }
150
151 /*
152  * Append a question mark to the end of the message.
153  */
154         static void
155 ap_quest()
156 {
157         ap_str("?");
158 }
159
160 /*
161  * Return the "current" byte offset in the file.
162  */
163         static POSITION
164 curr_byte(where)
165         int where;
166 {
167         POSITION pos;
168
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);
175 }
176
177 /*
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.
182  */
183         static int
184 cond(c, where)
185         char c;
186         int where;
187 {
188         POSITION len;
189
190         switch (c)
191         {
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 (eof_displayed());
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);
236         }
237         return (0);
238 }
239
240 /*
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.
246  */
247         static void
248 protochar(c, where, iseditproto)
249         int c;
250         int where;
251         int iseditproto;
252 {
253         POSITION pos;
254         POSITION len;
255         int n;
256         LINENUM linenum;
257         LINENUM last_linenum;
258         IFILE h;
259
260 #undef  PAGE_NUM
261 #define PAGE_NUM(linenum)  ((((linenum) - 1) / (sc_height - 1)) + 1)
262
263         switch (c)
264         {
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
291                 {
292                         linenum = find_linenum(len - 1);
293                         if (linenum <= 0)
294                                 ap_quest();
295                         else 
296                                 ap_linenum(PAGE_NUM(linenum));
297                 }
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 'F':       /* Last component of file name */
308                 ap_str(last_component(get_filename(curr_ifile)));
309                 break;
310         case 'i':       /* Index into list of files */
311 #if TAGS
312                 if (ntags())
313                         ap_int(curr_tag());
314                 else
315 #endif
316                         ap_int(get_index(curr_ifile));
317                 break;
318         case 'l':       /* Current line number */
319                 linenum = currline(where);
320                 if (linenum != 0)
321                         ap_linenum(linenum);
322                 else
323                         ap_quest();
324                 break;
325         case 'L':       /* Final line number */
326                 len = ch_length();
327                 if (len == NULL_POSITION || len == ch_zero() ||
328                     (linenum = find_linenum(len)) <= 0)
329                         ap_quest();
330                 else
331                         ap_linenum(linenum-1);
332                 break;
333         case 'm':       /* Number of files */
334 #if TAGS
335                 n = ntags();
336                 if (n)
337                         ap_int(n);
338                 else
339 #endif
340                         ap_int(nifile());
341                 break;
342         case 'p':       /* Percent into file (bytes) */
343                 pos = curr_byte(where);
344                 len = ch_length();
345                 if (pos != NULL_POSITION && len > 0)
346                         ap_int(percentage(pos,len));
347                 else
348                         ap_quest();
349                 break;
350         case 'P':       /* Percent into file (lines) */
351                 linenum = currline(where);
352                 if (linenum == 0 ||
353                     (len = ch_length()) == NULL_POSITION || len == ch_zero() ||
354                     (last_linenum = find_linenum(len)) <= 0)
355                         ap_quest();
356                 else
357                         ap_int(percentage(linenum, last_linenum));
358                 break;
359         case 's':       /* Size of file */
360         case 'B':
361                 len = ch_length();
362                 if (len != NULL_POSITION)
363                         ap_pos(len);
364                 else
365                         ap_quest();
366                 break;
367         case 't':       /* Truncate trailing spaces in the message */
368                 while (mp > message && mp[-1] == ' ')
369                         mp--;
370                 *mp = '\0';
371                 break;
372         case 'T':       /* Type of list */
373 #if TAGS
374                 if (ntags())
375                         ap_str("tag");
376                 else
377 #endif
378                         ap_str("file");
379                 break;
380         case 'x':       /* Name of next file */
381                 h = next_ifile(curr_ifile);
382                 if (h != NULL_IFILE)
383                         ap_str(get_filename(h));
384                 else
385                         ap_quest();
386                 break;
387         }
388 }
389
390 /*
391  * Skip a false conditional.
392  * When a false condition is found (either a false IF or the ELSE part 
393  * of a true IF), this routine scans the prototype string to decide
394  * where to resume parsing the string.
395  * We must keep track of nested IFs and skip them properly.
396  */
397         static char *
398 skipcond(p)
399         register char *p;
400 {
401         register int iflevel;
402
403         /*
404          * We came in here after processing a ? or :,
405          * so we start nested one level deep.
406          */
407         iflevel = 1;
408
409         for (;;) switch (*++p)
410         {
411         case '?':
412                 /*
413                  * Start of a nested IF.
414                  */
415                 iflevel++;
416                 break;
417         case ':':
418                 /*
419                  * Else.
420                  * If this matches the IF we came in here with,
421                  * then we're done.
422                  */
423                 if (iflevel == 1)
424                         return (p);
425                 break;
426         case '.':
427                 /*
428                  * Endif.
429                  * If this matches the IF we came in here with,
430                  * then we're done.
431                  */
432                 if (--iflevel == 0)
433                         return (p);
434                 break;
435         case '\\':
436                 /*
437                  * Backslash escapes the next character.
438                  */
439                 ++p;
440                 break;
441         case '\0':
442                 /*
443                  * Whoops.  Hit end of string.
444                  * This is a malformed conditional, but just treat it
445                  * as if all active conditionals ends here.
446                  */
447                 return (p-1);
448         }
449         /*NOTREACHED*/
450 }
451
452 /*
453  * Decode a char that represents a position on the screen.
454  */
455         static char *
456 wherechar(p, wp)
457         char *p;
458         int *wp;
459 {
460         switch (*p)
461         {
462         case 'b': case 'd': case 'l': case 'p': case 'P':
463                 switch (*++p)
464                 {
465                 case 't':   *wp = TOP;                  break;
466                 case 'm':   *wp = MIDDLE;               break;
467                 case 'b':   *wp = BOTTOM;               break;
468                 case 'B':   *wp = BOTTOM_PLUS_ONE;      break;
469                 case 'j':   *wp = adjsline(jump_sline); break;
470                 default:    *wp = TOP;  p--;            break;
471                 }
472         }
473         return (p);
474 }
475
476 /*
477  * Construct a message based on a prototype string.
478  */
479         public char *
480 pr_expand(proto, maxwidth)
481         char *proto;
482         int maxwidth;
483 {
484         register char *p;
485         register int c;
486         int where;
487
488         mp = message;
489
490         if (*proto == '\0')
491                 return ("");
492
493         for (p = proto;  *p != '\0';  p++)
494         {
495                 switch (*p)
496                 {
497                 default:        /* Just put the character in the message */
498                         ap_char(*p);
499                         break;
500                 case '\\':      /* Backslash escapes the next character */
501                         p++;
502                         ap_char(*p);
503                         break;
504                 case '?':       /* Conditional (IF) */
505                         if ((c = *++p) == '\0')
506                                 --p;
507                         else
508                         {
509                                 where = 0;
510                                 p = wherechar(p, &where);
511                                 if (!cond(c, where))
512                                         p = skipcond(p);
513                         }
514                         break;
515                 case ':':       /* ELSE */
516                         p = skipcond(p);
517                         break;
518                 case '.':       /* ENDIF */
519                         break;
520                 case '%':       /* Percent escape */
521                         if ((c = *++p) == '\0')
522                                 --p;
523                         else
524                         {
525                                 where = 0;
526                                 p = wherechar(p, &where);
527                                 protochar(c, where,
528 #if EDITOR
529                                         (proto == editproto));
530 #else
531                                         0);
532 #endif
533
534                         }
535                         break;
536                 }
537         }
538
539         if (mp == message)
540                 return ("");
541         if (maxwidth > 0 && mp >= message + maxwidth)
542         {
543                 /*
544                  * Message is too long.
545                  * Return just the final portion of it.
546                  */
547                 return (mp - maxwidth);
548         }
549         return (message);
550 }
551
552 /*
553  * Return a message suitable for printing by the "=" command.
554  */
555         public char *
556 eq_message()
557 {
558         return (pr_expand(eqproto, 0));
559 }
560
561 /*
562  * Return a prompt.
563  * This depends on the prompt type (SHORT, MEDIUM, LONG), etc.
564  * If we can't come up with an appropriate prompt, return NULL
565  * and the caller will prompt with a colon.
566  */
567         public char *
568 pr_string()
569 {
570         char *prompt;
571         int type;
572
573         type = (!less_is_more) ? pr_type : pr_type ? 0 : 1;
574         prompt = pr_expand((ch_getflags() & CH_HELPFILE) ?
575                                 hproto : prproto[type],
576                         sc_width-so_s_width-so_e_width-2);
577         new_file = 0;
578         return (prompt);
579 }
580
581 /*
582  * Return a message suitable for printing while waiting in the F command.
583  */
584         public char *
585 wait_message()
586 {
587         return (pr_expand(wproto, sc_width-so_s_width-so_e_width-2));
588 }