]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/less/prompt.c
This commit was generated by cvs2svn to compensate for changes in r161537,
[FreeBSD/FreeBSD.git] / contrib / less / prompt.c
1 /* $FreeBSD$ */
2 /*
3  * Copyright (C) 1984-2004  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 hit_eof;
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 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
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(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 = strlen(s);
89         if (mp + len >= message + PROMPT_SIZE)
90                 len = 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 (hit_eof);
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 'i':       /* Index into list of files */
306 #if TAGS
307                 if (ntags())
308                         ap_int(curr_tag());
309                 else
310 #endif
311                         ap_int(get_index(curr_ifile));
312                 break;
313         case 'l':       /* Current line number */
314                 linenum = currline(where);
315                 if (linenum != 0)
316                         ap_linenum(linenum);
317                 else
318                         ap_quest();
319                 break;
320         case 'L':       /* Final line number */
321                 len = ch_length();
322                 if (len == NULL_POSITION || len == ch_zero() ||
323                     (linenum = find_linenum(len)) <= 0)
324                         ap_quest();
325                 else
326                         ap_linenum(linenum-1);
327                 break;
328         case 'm':       /* Number of files */
329 #if TAGS
330                 n = ntags();
331                 if (n)
332                         ap_int(n);
333                 else
334 #endif
335                         ap_int(nifile());
336                 break;
337         case 'p':       /* Percent into file (bytes) */
338                 pos = curr_byte(where);
339                 len = ch_length();
340                 if (pos != NULL_POSITION && len > 0)
341                         ap_int(percentage(pos,len));
342                 else
343                         ap_quest();
344                 break;
345         case 'P':       /* Percent into file (lines) */
346                 linenum = currline(where);
347                 if (linenum == 0 ||
348                     (len = ch_length()) == NULL_POSITION || len == ch_zero() ||
349                     (last_linenum = find_linenum(len)) <= 0)
350                         ap_quest();
351                 else
352                         ap_int(percentage(linenum, last_linenum));
353                 break;
354         case 's':       /* Size of file */
355         case 'B':
356                 len = ch_length();
357                 if (len != NULL_POSITION)
358                         ap_pos(len);
359                 else
360                         ap_quest();
361                 break;
362         case 't':       /* Truncate trailing spaces in the message */
363                 while (mp > message && mp[-1] == ' ')
364                         mp--;
365                 break;
366         case 'T':       /* Type of list */
367 #if TAGS
368                 if (ntags())
369                         ap_str("tag");
370                 else
371 #endif
372                         ap_str("file");
373                 break;
374         case 'x':       /* Name of next file */
375                 h = next_ifile(curr_ifile);
376                 if (h != NULL_IFILE)
377                         ap_str(get_filename(h));
378                 else
379                         ap_quest();
380                 break;
381         }
382 }
383
384 /*
385  * Skip a false conditional.
386  * When a false condition is found (either a false IF or the ELSE part 
387  * of a true IF), this routine scans the prototype string to decide
388  * where to resume parsing the string.
389  * We must keep track of nested IFs and skip them properly.
390  */
391         static char *
392 skipcond(p)
393         register char *p;
394 {
395         register int iflevel;
396
397         /*
398          * We came in here after processing a ? or :,
399          * so we start nested one level deep.
400          */
401         iflevel = 1;
402
403         for (;;) switch (*++p)
404         {
405         case '?':
406                 /*
407                  * Start of a nested IF.
408                  */
409                 iflevel++;
410                 break;
411         case ':':
412                 /*
413                  * Else.
414                  * If this matches the IF we came in here with,
415                  * then we're done.
416                  */
417                 if (iflevel == 1)
418                         return (p);
419                 break;
420         case '.':
421                 /*
422                  * Endif.
423                  * If this matches the IF we came in here with,
424                  * then we're done.
425                  */
426                 if (--iflevel == 0)
427                         return (p);
428                 break;
429         case '\\':
430                 /*
431                  * Backslash escapes the next character.
432                  */
433                 ++p;
434                 break;
435         case '\0':
436                 /*
437                  * Whoops.  Hit end of string.
438                  * This is a malformed conditional, but just treat it
439                  * as if all active conditionals ends here.
440                  */
441                 return (p-1);
442         }
443         /*NOTREACHED*/
444 }
445
446 /*
447  * Decode a char that represents a position on the screen.
448  */
449         static char *
450 wherechar(p, wp)
451         char *p;
452         int *wp;
453 {
454         switch (*p)
455         {
456         case 'b': case 'd': case 'l': case 'p': case 'P':
457                 switch (*++p)
458                 {
459                 case 't':   *wp = TOP;                  break;
460                 case 'm':   *wp = MIDDLE;               break;
461                 case 'b':   *wp = BOTTOM;               break;
462                 case 'B':   *wp = BOTTOM_PLUS_ONE;      break;
463                 case 'j':   *wp = adjsline(jump_sline); break;
464                 default:    *wp = TOP;  p--;            break;
465                 }
466         }
467         return (p);
468 }
469
470 /*
471  * Construct a message based on a prototype string.
472  */
473         public char *
474 pr_expand(proto, maxwidth)
475         char *proto;
476         int maxwidth;
477 {
478         register char *p;
479         register int c;
480         int where;
481
482         mp = message;
483
484         if (*proto == '\0')
485                 return ("");
486
487         for (p = proto;  *p != '\0';  p++)
488         {
489                 switch (*p)
490                 {
491                 default:        /* Just put the character in the message */
492                         ap_char(*p);
493                         break;
494                 case '\\':      /* Backslash escapes the next character */
495                         p++;
496                         ap_char(*p);
497                         break;
498                 case '?':       /* Conditional (IF) */
499                         if ((c = *++p) == '\0')
500                                 --p;
501                         else
502                         {
503                                 where = 0;
504                                 p = wherechar(p, &where);
505                                 if (!cond(c, where))
506                                         p = skipcond(p);
507                         }
508                         break;
509                 case ':':       /* ELSE */
510                         p = skipcond(p);
511                         break;
512                 case '.':       /* ENDIF */
513                         break;
514                 case '%':       /* Percent escape */
515                         if ((c = *++p) == '\0')
516                                 --p;
517                         else
518                         {
519                                 where = 0;
520                                 p = wherechar(p, &where);
521                                 protochar(c, where,
522 #if EDITOR
523                                         (proto == editproto));
524 #else
525                                         0);
526 #endif
527
528                         }
529                         break;
530                 }
531         }
532
533         if (mp == message)
534                 return ("");
535         if (maxwidth > 0 && mp >= message + maxwidth)
536         {
537                 /*
538                  * Message is too long.
539                  * Return just the final portion of it.
540                  */
541                 return (mp - maxwidth);
542         }
543         return (message);
544 }
545
546 /*
547  * Return a message suitable for printing by the "=" command.
548  */
549         public char *
550 eq_message()
551 {
552         return (pr_expand(eqproto, 0));
553 }
554
555 /*
556  * Return a prompt.
557  * This depends on the prompt type (SHORT, MEDIUM, LONG), etc.
558  * If we can't come up with an appropriate prompt, return NULL
559  * and the caller will prompt with a colon.
560  */
561         public char *
562 pr_string()
563 {
564         char *prompt;
565
566         prompt = pr_expand((ch_getflags() & CH_HELPFILE) ?
567                                 hproto : prproto[pr_type],
568                         sc_width-so_s_width-so_e_width-2);
569         new_file = 0;
570         return (prompt);
571 }
572
573 /*
574  * Return a message suitable for printing while waiting in the F command.
575  */
576         public char *
577 wait_message()
578 {
579         return (pr_expand(wproto, sc_width-so_s_width-so_e_width-2));
580 }