ktrace - Add kern.ktrace_suid, allowing suid/sgid execs to be ktraced.
[dragonfly.git] / contrib / less / prompt.c
blob9a3485bb50f540003f50373bb38d82a23fc0d394
1 /*
2 * Copyright (C) 1984-2008 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 new_file;
26 extern int sc_width;
27 extern int so_s_width, so_e_width;
28 extern int linenums;
29 extern int hshift;
30 extern int sc_height;
31 extern int jump_sline;
32 extern int less_is_more;
33 extern IFILE curr_ifile;
34 #if EDITOR
35 extern char *editor;
36 extern char *editproto;
37 #endif
40 * Prototypes for the three flavors of prompts.
41 * These strings are expanded by pr_expand().
43 static constant char s_proto[] =
44 "?n?f%f .?m(%T %i of %m) ..?e(END) ?x- Next\\: %x..%t";
45 static constant char m_proto[] =
46 "?n?f%f .?m(%T %i of %m) ..?e(END) ?x- Next\\: %x.:?pB%pB\\%:byte %bB?s/%s...%t";
47 static constant char M_proto[] =
48 "?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";
49 static constant char e_proto[] =
50 "?f%f .?m(%T %i of %m) .?ltlines %lt-%lb?L/%L. .byte %bB?s/%s. ?e(END) :?pB%pB\\%..%t";
51 static constant char h_proto[] =
52 "HELP -- ?eEND -- Press g to see it again:Press RETURN for more., or q when done";
53 static constant char w_proto[] =
54 "Waiting for data";
55 static constant char more_proto[] =
56 "--More--(?eEND ?x- Next\\: %x.:?pB%pB\\%:byte %bB?s/%s...%t)";
58 public char *prproto[3];
59 public char constant *eqproto = e_proto;
60 public char constant *hproto = h_proto;
61 public char constant *wproto = w_proto;
63 static char message[PROMPT_SIZE];
64 static char *mp;
67 * Initialize the prompt prototype strings.
69 public void
70 init_prompt()
72 prproto[0] = save(s_proto);
73 prproto[1] = save(less_is_more ? more_proto : m_proto);
74 prproto[2] = save(M_proto);
75 eqproto = save(e_proto);
76 hproto = save(h_proto);
77 wproto = save(w_proto);
81 * Append a string to the end of the message.
83 static void
84 ap_str(s)
85 char *s;
87 int len;
89 len = strlen(s);
90 if (mp + len >= message + PROMPT_SIZE)
91 len = message + PROMPT_SIZE - mp - 1;
92 strncpy(mp, s, len);
93 mp += len;
94 *mp = '\0';
98 * Append a character to the end of the message.
100 static void
101 ap_char(c)
102 char c;
104 char buf[2];
106 buf[0] = c;
107 buf[1] = '\0';
108 ap_str(buf);
112 * Append a POSITION (as a decimal integer) to the end of the message.
114 static void
115 ap_pos(pos)
116 POSITION pos;
118 char buf[INT_STRLEN_BOUND(pos) + 2];
120 postoa(pos, buf);
121 ap_str(buf);
125 * Append a line number to the end of the message.
127 static void
128 ap_linenum(linenum)
129 LINENUM linenum;
131 char buf[INT_STRLEN_BOUND(linenum) + 2];
133 linenumtoa(linenum, buf);
134 ap_str(buf);
138 * Append an integer to the end of the message.
140 static void
141 ap_int(num)
142 int num;
144 char buf[INT_STRLEN_BOUND(num) + 2];
146 inttoa(num, buf);
147 ap_str(buf);
151 * Append a question mark to the end of the message.
153 static void
154 ap_quest()
156 ap_str("?");
160 * Return the "current" byte offset in the file.
162 static POSITION
163 curr_byte(where)
164 int where;
166 POSITION pos;
168 pos = position(where);
169 while (pos == NULL_POSITION && where >= 0 && where < sc_height-1)
170 pos = position(++where);
171 if (pos == NULL_POSITION)
172 pos = ch_length();
173 return (pos);
177 * Return the value of a prototype conditional.
178 * A prototype string may include conditionals which consist of a
179 * question mark followed by a single letter.
180 * Here we decode that letter and return the appropriate boolean value.
182 static int
183 cond(c, where)
184 char c;
185 int where;
187 POSITION len;
189 switch (c)
191 case 'a': /* Anything in the message yet? */
192 return (mp > message);
193 case 'b': /* Current byte offset known? */
194 return (curr_byte(where) != NULL_POSITION);
195 case 'c':
196 return (hshift != 0);
197 case 'e': /* At end of file? */
198 return (eof_displayed());
199 case 'f': /* Filename known? */
200 return (strcmp(get_filename(curr_ifile), "-") != 0);
201 case 'l': /* Line number known? */
202 case 'd': /* Same as l */
203 return (linenums);
204 case 'L': /* Final line number known? */
205 case 'D': /* Final page number known? */
206 return (linenums && ch_length() != NULL_POSITION);
207 case 'm': /* More than one file? */
208 #if TAGS
209 return (ntags() ? (ntags() > 1) : (nifile() > 1));
210 #else
211 return (nifile() > 1);
212 #endif
213 case 'n': /* First prompt in a new file? */
214 #if TAGS
215 return (ntags() ? 1 : new_file);
216 #else
217 return (new_file);
218 #endif
219 case 'p': /* Percent into file (bytes) known? */
220 return (curr_byte(where) != NULL_POSITION &&
221 ch_length() > 0);
222 case 'P': /* Percent into file (lines) known? */
223 return (currline(where) != 0 &&
224 (len = ch_length()) > 0 &&
225 find_linenum(len) != 0);
226 case 's': /* Size of file known? */
227 case 'B':
228 return (ch_length() != NULL_POSITION);
229 case 'x': /* Is there a "next" file? */
230 #if TAGS
231 if (ntags())
232 return (0);
233 #endif
234 return (next_ifile(curr_ifile) != NULL_IFILE);
236 return (0);
240 * Decode a "percent" prototype character.
241 * A prototype string may include various "percent" escapes;
242 * that is, a percent sign followed by a single letter.
243 * Here we decode that letter and take the appropriate action,
244 * usually by appending something to the message being built.
246 static void
247 protochar(c, where, iseditproto)
248 int c;
249 int where;
250 int iseditproto;
252 POSITION pos;
253 POSITION len;
254 int n;
255 LINENUM linenum;
256 LINENUM last_linenum;
257 IFILE h;
259 #undef PAGE_NUM
260 #define PAGE_NUM(linenum) ((((linenum) - 1) / (sc_height - 1)) + 1)
262 switch (c)
264 case 'b': /* Current byte offset */
265 pos = curr_byte(where);
266 if (pos != NULL_POSITION)
267 ap_pos(pos);
268 else
269 ap_quest();
270 break;
271 case 'c':
272 ap_int(hshift);
273 break;
274 case 'd': /* Current page number */
275 linenum = currline(where);
276 if (linenum > 0 && sc_height > 1)
277 ap_linenum(PAGE_NUM(linenum));
278 else
279 ap_quest();
280 break;
281 case 'D': /* Final page number */
282 /* Find the page number of the last byte in the file (len-1). */
283 len = ch_length();
284 if (len == NULL_POSITION)
285 ap_quest();
286 else if (len == 0)
287 /* An empty file has no pages. */
288 ap_linenum(0);
289 else
291 linenum = find_linenum(len - 1);
292 if (linenum <= 0)
293 ap_quest();
294 else
295 ap_linenum(PAGE_NUM(linenum));
297 break;
298 #if EDITOR
299 case 'E': /* Editor name */
300 ap_str(editor);
301 break;
302 #endif
303 case 'f': /* File name */
304 ap_str(get_filename(curr_ifile));
305 break;
306 case 'i': /* Index into list of files */
307 #if TAGS
308 if (ntags())
309 ap_int(curr_tag());
310 else
311 #endif
312 ap_int(get_index(curr_ifile));
313 break;
314 case 'l': /* Current line number */
315 linenum = currline(where);
316 if (linenum != 0)
317 ap_linenum(linenum);
318 else
319 ap_quest();
320 break;
321 case 'L': /* Final line number */
322 len = ch_length();
323 if (len == NULL_POSITION || len == ch_zero() ||
324 (linenum = find_linenum(len)) <= 0)
325 ap_quest();
326 else
327 ap_linenum(linenum-1);
328 break;
329 case 'm': /* Number of files */
330 #if TAGS
331 n = ntags();
332 if (n)
333 ap_int(n);
334 else
335 #endif
336 ap_int(nifile());
337 break;
338 case 'p': /* Percent into file (bytes) */
339 pos = curr_byte(where);
340 len = ch_length();
341 if (pos != NULL_POSITION && len > 0)
342 ap_int(percentage(pos,len));
343 else
344 ap_quest();
345 break;
346 case 'P': /* Percent into file (lines) */
347 linenum = currline(where);
348 if (linenum == 0 ||
349 (len = ch_length()) == NULL_POSITION || len == ch_zero() ||
350 (last_linenum = find_linenum(len)) <= 0)
351 ap_quest();
352 else
353 ap_int(percentage(linenum, last_linenum));
354 break;
355 case 's': /* Size of file */
356 case 'B':
357 len = ch_length();
358 if (len != NULL_POSITION)
359 ap_pos(len);
360 else
361 ap_quest();
362 break;
363 case 't': /* Truncate trailing spaces in the message */
364 while (mp > message && mp[-1] == ' ')
365 mp--;
366 break;
367 case 'T': /* Type of list */
368 #if TAGS
369 if (ntags())
370 ap_str("tag");
371 else
372 #endif
373 ap_str("file");
374 break;
375 case 'x': /* Name of next file */
376 h = next_ifile(curr_ifile);
377 if (h != NULL_IFILE)
378 ap_str(get_filename(h));
379 else
380 ap_quest();
381 break;
386 * Skip a false conditional.
387 * When a false condition is found (either a false IF or the ELSE part
388 * of a true IF), this routine scans the prototype string to decide
389 * where to resume parsing the string.
390 * We must keep track of nested IFs and skip them properly.
392 static char *
393 skipcond(p)
394 register char *p;
396 register int iflevel;
399 * We came in here after processing a ? or :,
400 * so we start nested one level deep.
402 iflevel = 1;
404 for (;;) switch (*++p)
406 case '?':
408 * Start of a nested IF.
410 iflevel++;
411 break;
412 case ':':
414 * Else.
415 * If this matches the IF we came in here with,
416 * then we're done.
418 if (iflevel == 1)
419 return (p);
420 break;
421 case '.':
423 * Endif.
424 * If this matches the IF we came in here with,
425 * then we're done.
427 if (--iflevel == 0)
428 return (p);
429 break;
430 case '\\':
432 * Backslash escapes the next character.
434 ++p;
435 break;
436 case '\0':
438 * Whoops. Hit end of string.
439 * This is a malformed conditional, but just treat it
440 * as if all active conditionals ends here.
442 return (p-1);
444 /*NOTREACHED*/
448 * Decode a char that represents a position on the screen.
450 static char *
451 wherechar(p, wp)
452 char *p;
453 int *wp;
455 switch (*p)
457 case 'b': case 'd': case 'l': case 'p': case 'P':
458 switch (*++p)
460 case 't': *wp = TOP; break;
461 case 'm': *wp = MIDDLE; break;
462 case 'b': *wp = BOTTOM; break;
463 case 'B': *wp = BOTTOM_PLUS_ONE; break;
464 case 'j': *wp = adjsline(jump_sline); break;
465 default: *wp = TOP; p--; break;
468 return (p);
472 * Construct a message based on a prototype string.
474 public char *
475 pr_expand(proto, maxwidth)
476 char *proto;
477 int maxwidth;
479 register char *p;
480 register int c;
481 int where;
483 mp = message;
485 if (*proto == '\0')
486 return ("");
488 for (p = proto; *p != '\0'; p++)
490 switch (*p)
492 default: /* Just put the character in the message */
493 ap_char(*p);
494 break;
495 case '\\': /* Backslash escapes the next character */
496 p++;
497 ap_char(*p);
498 break;
499 case '?': /* Conditional (IF) */
500 if ((c = *++p) == '\0')
501 --p;
502 else
504 where = 0;
505 p = wherechar(p, &where);
506 if (!cond(c, where))
507 p = skipcond(p);
509 break;
510 case ':': /* ELSE */
511 p = skipcond(p);
512 break;
513 case '.': /* ENDIF */
514 break;
515 case '%': /* Percent escape */
516 if ((c = *++p) == '\0')
517 --p;
518 else
520 where = 0;
521 p = wherechar(p, &where);
522 protochar(c, where,
523 #if EDITOR
524 (proto == editproto));
525 #else
527 #endif
530 break;
534 if (mp == message)
535 return ("");
536 if (maxwidth > 0 && mp >= message + maxwidth)
539 * Message is too long.
540 * Return just the final portion of it.
542 return (mp - maxwidth);
544 return (message);
548 * Return a message suitable for printing by the "=" command.
550 public char *
551 eq_message()
553 return (pr_expand(eqproto, 0));
557 * Return a prompt.
558 * This depends on the prompt type (SHORT, MEDIUM, LONG), etc.
559 * If we can't come up with an appropriate prompt, return NULL
560 * and the caller will prompt with a colon.
562 public char *
563 pr_string()
565 char *prompt;
566 int type;
568 type = (!less_is_more) ? pr_type : pr_type ? 0 : 1;
569 prompt = pr_expand((ch_getflags() & CH_HELPFILE) ?
570 hproto : prproto[type],
571 sc_width-so_s_width-so_e_width-2);
572 new_file = 0;
573 return (prompt);
577 * Return a message suitable for printing while waiting in the F command.
579 public char *
580 wait_message()
582 return (pr_expand(wproto, sc_width-so_s_width-so_e_width-2));