Add precautions so that threads won't race to exit1() and get stuck there.
[dragonfly.git] / contrib / less-394 / prompt.c
blobee34b28bf393f7931f77dc9b4d55b95e30353a64
1 /*
2 * Copyright (C) 1984-2004 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 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";
56 public char *prproto[3];
57 public char constant *eqproto = e_proto;
58 public char constant *hproto = h_proto;
59 public char constant *wproto = w_proto;
61 static char message[PROMPT_SIZE];
62 static char *mp;
65 * Initialize the prompt prototype strings.
67 public void
68 init_prompt()
70 prproto[0] = save(s_proto);
71 prproto[1] = save(m_proto);
72 prproto[2] = save(M_proto);
73 eqproto = save(e_proto);
74 hproto = save(h_proto);
75 wproto = save(w_proto);
79 * Append a string to the end of the message.
81 static void
82 ap_str(s)
83 char *s;
85 int len;
87 len = strlen(s);
88 if (mp + len >= message + PROMPT_SIZE)
89 len = message + PROMPT_SIZE - mp - 1;
90 strncpy(mp, s, len);
91 mp += len;
92 *mp = '\0';
96 * Append a character to the end of the message.
98 static void
99 ap_char(c)
100 char c;
102 char buf[2];
104 buf[0] = c;
105 buf[1] = '\0';
106 ap_str(buf);
110 * Append a POSITION (as a decimal integer) to the end of the message.
112 static void
113 ap_pos(pos)
114 POSITION pos;
116 char buf[INT_STRLEN_BOUND(pos) + 2];
118 postoa(pos, buf);
119 ap_str(buf);
123 * Append a line number to the end of the message.
125 static void
126 ap_linenum(linenum)
127 LINENUM linenum;
129 char buf[INT_STRLEN_BOUND(linenum) + 2];
131 linenumtoa(linenum, buf);
132 ap_str(buf);
136 * Append an integer to the end of the message.
138 static void
139 ap_int(num)
140 int num;
142 char buf[INT_STRLEN_BOUND(num) + 2];
144 inttoa(num, buf);
145 ap_str(buf);
149 * Append a question mark to the end of the message.
151 static void
152 ap_quest()
154 ap_str("?");
158 * Return the "current" byte offset in the file.
160 static POSITION
161 curr_byte(where)
162 int where;
164 POSITION pos;
166 pos = position(where);
167 while (pos == NULL_POSITION && where >= 0 && where < sc_height-1)
168 pos = position(++where);
169 if (pos == NULL_POSITION)
170 pos = ch_length();
171 return (pos);
175 * Return the value of a prototype conditional.
176 * A prototype string may include conditionals which consist of a
177 * question mark followed by a single letter.
178 * Here we decode that letter and return the appropriate boolean value.
180 static int
181 cond(c, where)
182 char c;
183 int where;
185 POSITION len;
187 switch (c)
189 case 'a': /* Anything in the message yet? */
190 return (mp > message);
191 case 'b': /* Current byte offset known? */
192 return (curr_byte(where) != NULL_POSITION);
193 case 'c':
194 return (hshift != 0);
195 case 'e': /* At end of file? */
196 return (hit_eof);
197 case 'f': /* Filename known? */
198 return (strcmp(get_filename(curr_ifile), "-") != 0);
199 case 'l': /* Line number known? */
200 case 'd': /* Same as l */
201 return (linenums);
202 case 'L': /* Final line number known? */
203 case 'D': /* Final page number known? */
204 return (linenums && ch_length() != NULL_POSITION);
205 case 'm': /* More than one file? */
206 #if TAGS
207 return (ntags() ? (ntags() > 1) : (nifile() > 1));
208 #else
209 return (nifile() > 1);
210 #endif
211 case 'n': /* First prompt in a new file? */
212 #if TAGS
213 return (ntags() ? 1 : new_file);
214 #else
215 return (new_file);
216 #endif
217 case 'p': /* Percent into file (bytes) known? */
218 return (curr_byte(where) != NULL_POSITION &&
219 ch_length() > 0);
220 case 'P': /* Percent into file (lines) known? */
221 return (currline(where) != 0 &&
222 (len = ch_length()) > 0 &&
223 find_linenum(len) != 0);
224 case 's': /* Size of file known? */
225 case 'B':
226 return (ch_length() != NULL_POSITION);
227 case 'x': /* Is there a "next" file? */
228 #if TAGS
229 if (ntags())
230 return (0);
231 #endif
232 return (next_ifile(curr_ifile) != NULL_IFILE);
234 return (0);
238 * Decode a "percent" prototype character.
239 * A prototype string may include various "percent" escapes;
240 * that is, a percent sign followed by a single letter.
241 * Here we decode that letter and take the appropriate action,
242 * usually by appending something to the message being built.
244 static void
245 protochar(c, where, iseditproto)
246 int c;
247 int where;
248 int iseditproto;
250 POSITION pos;
251 POSITION len;
252 int n;
253 LINENUM linenum;
254 LINENUM last_linenum;
255 IFILE h;
257 #undef PAGE_NUM
258 #define PAGE_NUM(linenum) ((((linenum) - 1) / (sc_height - 1)) + 1)
260 switch (c)
262 case 'b': /* Current byte offset */
263 pos = curr_byte(where);
264 if (pos != NULL_POSITION)
265 ap_pos(pos);
266 else
267 ap_quest();
268 break;
269 case 'c':
270 ap_int(hshift);
271 break;
272 case 'd': /* Current page number */
273 linenum = currline(where);
274 if (linenum > 0 && sc_height > 1)
275 ap_linenum(PAGE_NUM(linenum));
276 else
277 ap_quest();
278 break;
279 case 'D': /* Final page number */
280 /* Find the page number of the last byte in the file (len-1). */
281 len = ch_length();
282 if (len == NULL_POSITION)
283 ap_quest();
284 else if (len == 0)
285 /* An empty file has no pages. */
286 ap_linenum(0);
287 else
289 linenum = find_linenum(len - 1);
290 if (linenum <= 0)
291 ap_quest();
292 else
293 ap_linenum(PAGE_NUM(linenum));
295 break;
296 #if EDITOR
297 case 'E': /* Editor name */
298 ap_str(editor);
299 break;
300 #endif
301 case 'f': /* File name */
302 ap_str(get_filename(curr_ifile));
303 break;
304 case 'i': /* Index into list of files */
305 #if TAGS
306 if (ntags())
307 ap_int(curr_tag());
308 else
309 #endif
310 ap_int(get_index(curr_ifile));
311 break;
312 case 'l': /* Current line number */
313 linenum = currline(where);
314 if (linenum != 0)
315 ap_linenum(linenum);
316 else
317 ap_quest();
318 break;
319 case 'L': /* Final line number */
320 len = ch_length();
321 if (len == NULL_POSITION || len == ch_zero() ||
322 (linenum = find_linenum(len)) <= 0)
323 ap_quest();
324 else
325 ap_linenum(linenum-1);
326 break;
327 case 'm': /* Number of files */
328 #if TAGS
329 n = ntags();
330 if (n)
331 ap_int(n);
332 else
333 #endif
334 ap_int(nifile());
335 break;
336 case 'p': /* Percent into file (bytes) */
337 pos = curr_byte(where);
338 len = ch_length();
339 if (pos != NULL_POSITION && len > 0)
340 ap_int(percentage(pos,len));
341 else
342 ap_quest();
343 break;
344 case 'P': /* Percent into file (lines) */
345 linenum = currline(where);
346 if (linenum == 0 ||
347 (len = ch_length()) == NULL_POSITION || len == ch_zero() ||
348 (last_linenum = find_linenum(len)) <= 0)
349 ap_quest();
350 else
351 ap_int(percentage(linenum, last_linenum));
352 break;
353 case 's': /* Size of file */
354 case 'B':
355 len = ch_length();
356 if (len != NULL_POSITION)
357 ap_pos(len);
358 else
359 ap_quest();
360 break;
361 case 't': /* Truncate trailing spaces in the message */
362 while (mp > message && mp[-1] == ' ')
363 mp--;
364 break;
365 case 'T': /* Type of list */
366 #if TAGS
367 if (ntags())
368 ap_str("tag");
369 else
370 #endif
371 ap_str("file");
372 break;
373 case 'x': /* Name of next file */
374 h = next_ifile(curr_ifile);
375 if (h != NULL_IFILE)
376 ap_str(get_filename(h));
377 else
378 ap_quest();
379 break;
384 * Skip a false conditional.
385 * When a false condition is found (either a false IF or the ELSE part
386 * of a true IF), this routine scans the prototype string to decide
387 * where to resume parsing the string.
388 * We must keep track of nested IFs and skip them properly.
390 static char *
391 skipcond(p)
392 register char *p;
394 register int iflevel;
397 * We came in here after processing a ? or :,
398 * so we start nested one level deep.
400 iflevel = 1;
402 for (;;) switch (*++p)
404 case '?':
406 * Start of a nested IF.
408 iflevel++;
409 break;
410 case ':':
412 * Else.
413 * If this matches the IF we came in here with,
414 * then we're done.
416 if (iflevel == 1)
417 return (p);
418 break;
419 case '.':
421 * Endif.
422 * If this matches the IF we came in here with,
423 * then we're done.
425 if (--iflevel == 0)
426 return (p);
427 break;
428 case '\\':
430 * Backslash escapes the next character.
432 ++p;
433 break;
434 case '\0':
436 * Whoops. Hit end of string.
437 * This is a malformed conditional, but just treat it
438 * as if all active conditionals ends here.
440 return (p-1);
442 /*NOTREACHED*/
446 * Decode a char that represents a position on the screen.
448 static char *
449 wherechar(p, wp)
450 char *p;
451 int *wp;
453 switch (*p)
455 case 'b': case 'd': case 'l': case 'p': case 'P':
456 switch (*++p)
458 case 't': *wp = TOP; break;
459 case 'm': *wp = MIDDLE; break;
460 case 'b': *wp = BOTTOM; break;
461 case 'B': *wp = BOTTOM_PLUS_ONE; break;
462 case 'j': *wp = adjsline(jump_sline); break;
463 default: *wp = TOP; p--; break;
466 return (p);
470 * Construct a message based on a prototype string.
472 public char *
473 pr_expand(proto, maxwidth)
474 char *proto;
475 int maxwidth;
477 register char *p;
478 register int c;
479 int where;
481 mp = message;
483 if (*proto == '\0')
484 return ("");
486 for (p = proto; *p != '\0'; p++)
488 switch (*p)
490 default: /* Just put the character in the message */
491 ap_char(*p);
492 break;
493 case '\\': /* Backslash escapes the next character */
494 p++;
495 ap_char(*p);
496 break;
497 case '?': /* Conditional (IF) */
498 if ((c = *++p) == '\0')
499 --p;
500 else
502 where = 0;
503 p = wherechar(p, &where);
504 if (!cond(c, where))
505 p = skipcond(p);
507 break;
508 case ':': /* ELSE */
509 p = skipcond(p);
510 break;
511 case '.': /* ENDIF */
512 break;
513 case '%': /* Percent escape */
514 if ((c = *++p) == '\0')
515 --p;
516 else
518 where = 0;
519 p = wherechar(p, &where);
520 protochar(c, where,
521 #if EDITOR
522 (proto == editproto));
523 #else
525 #endif
528 break;
532 if (mp == message)
533 return ("");
534 if (maxwidth > 0 && mp >= message + maxwidth)
537 * Message is too long.
538 * Return just the final portion of it.
540 return (mp - maxwidth);
542 return (message);
546 * Return a message suitable for printing by the "=" command.
548 public char *
549 eq_message()
551 return (pr_expand(eqproto, 0));
555 * Return a prompt.
556 * This depends on the prompt type (SHORT, MEDIUM, LONG), etc.
557 * If we can't come up with an appropriate prompt, return NULL
558 * and the caller will prompt with a colon.
560 public char *
561 pr_string()
563 char *prompt;
565 prompt = pr_expand((ch_getflags() & CH_HELPFILE) ?
566 hproto : prproto[pr_type],
567 sc_width-so_s_width-so_e_width-2);
568 new_file = 0;
569 return (prompt);
573 * Return a message suitable for printing while waiting in the F command.
575 public char *
576 wait_message()
578 return (pr_expand(wproto, sc_width-so_s_width-so_e_width-2));