1 /*
2 * Copyright (C) 1984-2025 Mark Nudelman
3 *
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.
6 *
7 * For more information, see the README file.
8 */
9
10
11 /*
12 * Prompting and other messages.
13 * There are three flavors of prompts, SHORT, MEDIUM and LONG,
14 * selected by the -m/-M options.
15 * There is also the "equals message", printed by the = command.
16 * A prompt is a message composed of various pieces, such as the
17 * name of the file being viewed, the percentage into the file, etc.
18 */
19
20 #include "less.h"
21 #include "position.h"
22
23 extern int pr_type;
24 extern lbool new_file;
25 extern int linenums;
26 extern int hshift;
27 extern int sc_height;
28 extern int jump_sline;
29 extern int less_is_more;
30 extern int header_lines;
31 extern int utf_mode;
32 extern IFILE curr_ifile;
33 #if OSC8_LINK
34 extern char *osc8_path;
35 #endif
36 #if EDITOR
37 extern constant char *editor;
38 extern constant char *editproto;
39 #endif
40
41 /*
42 * Prototypes for the three flavors of prompts.
43 * These strings are expanded by pr_expand().
44 */
45 static constant char s_proto[] =
46 "?n?f%f .?m(%T %i of %m) ..?e(END) ?x- Next\\: %x..%t";
47 static constant char m_proto[] =
48 "?n?f%f .?m(%T %i of %m) ..?e(END) ?x- Next\\: %x.:?pB%pB\\%:byte %bB?s/%s...%t";
49 static constant char M_proto[] =
50 "?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";
51 static constant char e_proto[] =
52 "?f%f .?m(%T %i of %m) .?ltlines %lt-%lb?L/%L. .byte %bB?s/%s. ?e(END) :?pB%pB\\%..%t";
53 static constant char h_proto[] =
54 "HELP -- ?eEND -- Press g to see it again:Press RETURN for more., or q when done";
55 static constant char w_proto[] =
56 "Waiting for data";
57 static constant char more_proto[] =
58 "--More--(?eEND ?x- Next\\: %x.:?pB%pB\\%:byte %bB?s/%s...%t)";
59
60 public char *prproto[3];
61 public char constant *eqproto = e_proto;
62 public char constant *hproto = h_proto;
63 public char constant *wproto = w_proto;
64
65 static char message[PROMPT_SIZE];
66 static char *mp;
67
68 /*
69 * Initialize the prompt prototype strings.
70 */
init_prompt(void)71 public void init_prompt(void)
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 * nprt means the character *may* be nonprintable
84 * and should be converted to printable form.
85 */
ap_estr(constant char * s,lbool nprt)86 static void ap_estr(constant char *s, lbool nprt)
87 {
88 constant char *es = s + strlen(s);
89 while (*s != '\0')
90 {
91 LWCHAR ch = step_charc(&s, +1, es);
92 constant char *ps;
93 char ubuf[MAX_UTF_CHAR_LEN+1];
94 size_t plen;
95
96 if (nprt)
97 {
98 ps = utf_mode ? prutfchar(ch) : prchar(ch);
99 } else
100 {
101 char *up = ubuf;
102 put_wchar(&up, ch);
103 *up = '\0';
104 ps = ubuf;
105 }
106 plen = strlen(ps);
107 if (mp + plen >= message + PROMPT_SIZE)
108 break;
109 strcpy(mp, ps);
110 mp += plen;
111 }
112 *mp = '\0';
113 }
114
ap_str(constant char * s)115 static void ap_str(constant char *s)
116 {
117 ap_estr(s, FALSE);
118 }
119
120 /*
121 * Append a character to the end of the message.
122 */
ap_char(char c)123 static void ap_char(char c)
124 {
125 if (mp + 1 >= message + PROMPT_SIZE)
126 return;
127 *mp++ = c;
128 *mp = '\0';
129 }
130
131 /*
132 * Append a POSITION (as a decimal integer) to the end of the message.
133 */
ap_pos(POSITION pos)134 static void ap_pos(POSITION pos)
135 {
136 char buf[INT_STRLEN_BOUND(pos) + 2];
137
138 postoa(pos, buf, 10);
139 ap_str(buf);
140 }
141
142 /*
143 * Append a line number to the end of the message.
144 */
ap_linenum(LINENUM linenum)145 static void ap_linenum(LINENUM linenum)
146 {
147 char buf[INT_STRLEN_BOUND(linenum) + 2];
148
149 linenumtoa(linenum, buf, 10);
150 ap_str(buf);
151 }
152
153 /*
154 * Append an integer to the end of the message.
155 */
ap_int(int num)156 static void ap_int(int num)
157 {
158 char buf[INT_STRLEN_BOUND(num) + 2];
159
160 inttoa(num, buf, 10);
161 ap_str(buf);
162 }
163
164 /*
165 * Append a question mark to the end of the message.
166 */
ap_quest(void)167 static void ap_quest(void)
168 {
169 ap_str("?");
170 }
171
172 /*
173 * Return the "current" byte offset in the file.
174 */
curr_byte(int where)175 static POSITION curr_byte(int where)
176 {
177 POSITION pos;
178
179 pos = position(where);
180 while (pos == NULL_POSITION && where >= 0 && where < sc_height-1)
181 pos = position(++where);
182 if (pos == NULL_POSITION)
183 pos = ch_length();
184 return (pos);
185 }
186
187 /*
188 * Return the value of a prototype conditional.
189 * A prototype string may include conditionals which consist of a
190 * question mark followed by a single letter.
191 * Here we decode that letter and return the appropriate boolean value.
192 */
cond(char c,int where)193 static lbool cond(char c, int where)
194 {
195 POSITION len;
196
197 switch (c)
198 {
199 case 'a': /* Anything in the message yet? */
200 return (mp > message);
201 case 'b': /* Current byte offset known? */
202 return (curr_byte(where) != NULL_POSITION);
203 case 'c':
204 return (hshift != 0);
205 case 'e': /* At end of file? */
206 return (eof_displayed(FALSE));
207 case 'f': /* Filename known? */
208 case 'g':
209 return (strcmp(get_filename(curr_ifile), "-") != 0);
210 case 'l': /* Line number known? */
211 case 'd': /* Same as l */
212 if (!linenums)
213 return FALSE;
214 return (currline(where) != 0);
215 case 'L': /* Final line number known? */
216 case 'D': /* Final page number known? */
217 return (linenums && ch_length() != NULL_POSITION);
218 case 'm': /* More than one file? */
219 #if TAGS
220 return (ntags() ? (ntags() > 1) : (nifile() > 1));
221 #else
222 return (nifile() > 1);
223 #endif
224 case 'n': /* First prompt in a new file? */
225 #if TAGS
226 return (ntags() ? TRUE : new_file ? TRUE : FALSE);
227 #else
228 return (new_file ? TRUE : FALSE);
229 #endif
230 case 'p': /* Percent into file (bytes) known? */
231 return (curr_byte(where) != NULL_POSITION && ch_length() > 0);
232 case 'P': /* Percent into file (lines) known? */
233 return (currline(where) != 0 &&
234 (len = ch_length()) > 0 &&
235 find_linenum(len) != 0);
236 case 's': /* Size of file known? */
237 case 'B':
238 return (ch_length() != NULL_POSITION);
239 case 'x': /* Is there a "next" file? */
240 #if TAGS
241 if (ntags())
242 return (FALSE);
243 #endif
244 return (next_ifile(curr_ifile) != NULL_IFILE);
245 }
246 return (FALSE);
247 }
248
249 /*
250 * Decode a "percent" prototype character.
251 * A prototype string may include various "percent" escapes;
252 * that is, a percent sign followed by a single letter.
253 * Here we decode that letter and take the appropriate action,
254 * usually by appending something to the message being built.
255 */
protochar(char c,int where)256 static void protochar(char c, int where)
257 {
258 POSITION pos;
259 POSITION len;
260 LINENUM linenum;
261 LINENUM last_linenum;
262 IFILE h;
263 char *s;
264
265 #undef PAGE_NUM
266 #define PAGE_NUM(linenum) ((((linenum) - 1) / (sc_height - header_lines - 1)) + 1)
267
268 switch (c)
269 {
270 case 'b': /* Current byte offset */
271 pos = curr_byte(where);
272 if (pos != NULL_POSITION)
273 ap_pos(pos);
274 else
275 ap_quest();
276 break;
277 case 'c':
278 ap_int(hshift);
279 break;
280 case 'd': /* Current page number */
281 linenum = currline(where);
282 if (linenum > 0 && sc_height > header_lines + 1)
283 ap_linenum(PAGE_NUM(linenum));
284 else
285 ap_quest();
286 break;
287 case 'D': /* Final page number */
288 /* Find the page number of the last byte in the file (len-1). */
289 len = ch_length();
290 if (len == NULL_POSITION)
291 ap_quest();
292 else if (len == 0)
293 /* An empty file has no pages. */
294 ap_linenum(0);
295 else
296 {
297 linenum = find_linenum(len - 1);
298 if (linenum <= 0)
299 ap_quest();
300 else
301 ap_linenum(PAGE_NUM(linenum));
302 }
303 break;
304 #if EDITOR
305 case 'E': /* Editor name */
306 ap_str(editor);
307 break;
308 #endif
309 case 'f': /* File name */
310 ap_estr(get_filename(curr_ifile), TRUE);
311 break;
312 case 'F': /* Last component of file name */
313 ap_estr(last_component(get_filename(curr_ifile)), TRUE);
314 break;
315 case 'g': /* Shell-escaped file name */
316 s = shell_quote(get_filename(curr_ifile));
317 ap_str(s);
318 free(s);
319 break;
320 case 'i': /* Index into list of files */
321 #if TAGS
322 if (ntags())
323 ap_int(curr_tag());
324 else
325 #endif
326 ap_int(get_index(curr_ifile));
327 break;
328 case 'l': /* Current line number */
329 linenum = currline(where);
330 if (linenum != 0)
331 ap_linenum(vlinenum(linenum));
332 else
333 ap_quest();
334 break;
335 case 'L': /* Final line number */
336 len = ch_length();
337 if (len == NULL_POSITION || len == ch_zero() ||
338 (linenum = find_linenum(len)) <= 0)
339 ap_quest();
340 else
341 ap_linenum(vlinenum(linenum-1));
342 break;
343 case 'm': { /* Number of files */
344 #if TAGS
345 int n = ntags();
346 if (n)
347 ap_int(n);
348 else
349 #endif
350 ap_int(nifile());
351 break; }
352 case 'o': /* path (URI without protocol) of selected OSC8 link */
353 #if OSC8_LINK
354 if (osc8_path != NULL)
355 ap_str(osc8_path);
356 else
357 #endif
358 ap_quest();
359 break;
360 case 'p': /* Percent into file (bytes) */
361 pos = curr_byte(where);
362 len = ch_length();
363 if (pos != NULL_POSITION && len > 0)
364 ap_int(percentage(pos,len));
365 else
366 ap_quest();
367 break;
368 case 'P': /* Percent into file (lines) */
369 linenum = currline(where);
370 if (linenum == 0 ||
371 (len = ch_length()) == NULL_POSITION || len == ch_zero() ||
372 (last_linenum = find_linenum(len)) <= 0)
373 ap_quest();
374 else
375 ap_int(percentage(linenum, last_linenum));
376 break;
377 case 's': /* Size of file */
378 case 'B':
379 len = ch_length();
380 if (len != NULL_POSITION)
381 ap_pos(len);
382 else
383 ap_quest();
384 break;
385 case 't': /* Truncate trailing spaces in the message */
386 while (mp > message && mp[-1] == ' ')
387 mp--;
388 *mp = '\0';
389 break;
390 case 'T': /* Type of list */
391 #if TAGS
392 if (ntags())
393 ap_str("tag");
394 else
395 #endif
396 ap_str("file");
397 break;
398 case 'x': /* Name of next file */
399 h = next_ifile(curr_ifile);
400 if (h != NULL_IFILE)
401 ap_str(get_filename(h));
402 else
403 ap_quest();
404 break;
405 }
406 }
407
408 /*
409 * Skip a false conditional.
410 * When a false condition is found (either a false IF or the ELSE part
411 * of a true IF), this routine scans the prototype string to decide
412 * where to resume parsing the string.
413 * We must keep track of nested IFs and skip them properly.
414 */
skipcond(constant char * p)415 static constant char * skipcond(constant char *p)
416 {
417 int iflevel;
418
419 /*
420 * We came in here after processing a ? or :,
421 * so we start nested one level deep.
422 */
423 iflevel = 1;
424
425 for (;;) switch (*++p)
426 {
427 case '?':
428 /*
429 * Start of a nested IF.
430 */
431 iflevel++;
432 break;
433 case ':':
434 /*
435 * Else.
436 * If this matches the IF we came in here with,
437 * then we're done.
438 */
439 if (iflevel == 1)
440 return (p);
441 break;
442 case '.':
443 /*
444 * Endif.
445 * If this matches the IF we came in here with,
446 * then we're done.
447 */
448 if (--iflevel == 0)
449 return (p);
450 break;
451 case '\\':
452 /*
453 * Backslash escapes the next character.
454 */
455 if (p[1] != '\0')
456 ++p;
457 break;
458 case '\0':
459 /*
460 * Whoops. Hit end of string.
461 * This is a malformed conditional, but just treat it
462 * as if all active conditionals ends here.
463 */
464 return (p-1);
465 }
466 /*NOTREACHED*/
467 }
468
469 /*
470 * Decode a char that represents a position on the screen.
471 */
wherechar(char constant * p,int * wp)472 static constant char * wherechar(char constant *p, int *wp)
473 {
474 switch (*p)
475 {
476 case 'b': case 'd': case 'l': case 'p': case 'P':
477 switch (*++p)
478 {
479 case 't': *wp = TOP; break;
480 case 'm': *wp = MIDDLE; break;
481 case 'b': *wp = BOTTOM; break;
482 case 'B': *wp = BOTTOM_PLUS_ONE; break;
483 case 'j': *wp = sindex_from_sline(jump_sline); break;
484 default: *wp = TOP; p--; break;
485 }
486 }
487 return (p);
488 }
489
490 /*
491 * Construct a message based on a prototype string.
492 */
pr_expand(constant char * proto)493 public constant char * pr_expand(constant char *proto)
494 {
495 constant char *p;
496 char c;
497 int where;
498
499 mp = message;
500
501 if (*proto == '\0')
502 return ("");
503
504 for (p = proto; *p != '\0'; p++)
505 {
506 switch (*p)
507 {
508 default: /* Just put the character in the message */
509 ap_char(*p);
510 break;
511 case '\\': /* Backslash escapes the next character */
512 if (p[1] != '\0')
513 ap_char(*++p);
514 break;
515 case '?': /* Conditional (IF) */
516 if ((c = *++p) == '\0')
517 --p;
518 else
519 {
520 where = 0;
521 p = wherechar(p, &where);
522 if (!cond(c, where))
523 p = skipcond(p);
524 }
525 break;
526 case ':': /* ELSE */
527 p = skipcond(p);
528 break;
529 case '.': /* ENDIF */
530 break;
531 case '%': /* Percent escape */
532 if ((c = *++p) == '\0')
533 --p;
534 else
535 {
536 where = 0;
537 p = wherechar(p, &where);
538 protochar(c, where);
539 }
540 break;
541 }
542 }
543
544 if (mp == message)
545 return ("");
546 return (message);
547 }
548
549 /*
550 * Return a message suitable for printing by the "=" command.
551 */
eq_message(void)552 public constant char * eq_message(void)
553 {
554 return (pr_expand(eqproto));
555 }
556
557 /*
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.
562 */
pr_string(void)563 public constant char * pr_string(void)
564 {
565 constant char *prompt;
566 int type;
567
568 type = (!less_is_more) ? pr_type : pr_type ? 0 : 1;
569 prompt = pr_expand((ch_getflags() & CH_HELPFILE) ?
570 hproto : prproto[type]);
571 new_file = FALSE;
572 return (prompt);
573 }
574
575 /*
576 * Return a message suitable for printing while waiting in the F command.
577 */
wait_message(void)578 public constant char * wait_message(void)
579 {
580 return (pr_expand(wproto));
581 }
582