1 /* 2 * Copyright (C) 1984-2024 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 */ 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 */ 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 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 */ 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 */ 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 */ 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 */ 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 */ 167 static void ap_quest(void) 168 { 169 ap_str("?"); 170 } 171 172 /* 173 * Return the "current" byte offset in the file. 174 */ 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 */ 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()); 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 */ 256 static void protochar(char c, int where) 257 { 258 POSITION pos; 259 POSITION len; 260 int n; 261 LINENUM linenum; 262 LINENUM last_linenum; 263 IFILE h; 264 char *s; 265 266 #undef PAGE_NUM 267 #define PAGE_NUM(linenum) ((((linenum) - 1) / (sc_height - header_lines - 1)) + 1) 268 269 switch (c) 270 { 271 case 'b': /* Current byte offset */ 272 pos = curr_byte(where); 273 if (pos != NULL_POSITION) 274 ap_pos(pos); 275 else 276 ap_quest(); 277 break; 278 case 'c': 279 ap_int(hshift); 280 break; 281 case 'd': /* Current page number */ 282 linenum = currline(where); 283 if (linenum > 0 && sc_height > header_lines + 1) 284 ap_linenum(PAGE_NUM(linenum)); 285 else 286 ap_quest(); 287 break; 288 case 'D': /* Final page number */ 289 /* Find the page number of the last byte in the file (len-1). */ 290 len = ch_length(); 291 if (len == NULL_POSITION) 292 ap_quest(); 293 else if (len == 0) 294 /* An empty file has no pages. */ 295 ap_linenum(0); 296 else 297 { 298 linenum = find_linenum(len - 1); 299 if (linenum <= 0) 300 ap_quest(); 301 else 302 ap_linenum(PAGE_NUM(linenum)); 303 } 304 break; 305 #if EDITOR 306 case 'E': /* Editor name */ 307 ap_str(editor); 308 break; 309 #endif 310 case 'f': /* File name */ 311 ap_estr(get_filename(curr_ifile), TRUE); 312 break; 313 case 'F': /* Last component of file name */ 314 ap_estr(last_component(get_filename(curr_ifile)), TRUE); 315 break; 316 case 'g': /* Shell-escaped file name */ 317 s = shell_quote(get_filename(curr_ifile)); 318 ap_str(s); 319 free(s); 320 break; 321 case 'i': /* Index into list of files */ 322 #if TAGS 323 if (ntags()) 324 ap_int(curr_tag()); 325 else 326 #endif 327 ap_int(get_index(curr_ifile)); 328 break; 329 case 'l': /* Current line number */ 330 linenum = currline(where); 331 if (linenum != 0) 332 ap_linenum(vlinenum(linenum)); 333 else 334 ap_quest(); 335 break; 336 case 'L': /* Final line number */ 337 len = ch_length(); 338 if (len == NULL_POSITION || len == ch_zero() || 339 (linenum = find_linenum(len)) <= 0) 340 ap_quest(); 341 else 342 ap_linenum(vlinenum(linenum-1)); 343 break; 344 case 'm': /* Number of files */ 345 #if TAGS 346 n = ntags(); 347 if (n) 348 ap_int(n); 349 else 350 #endif 351 ap_int(nifile()); 352 break; 353 case 'o': /* path (URI without protocol) of selected OSC8 link */ 354 #if OSC8_LINK 355 if (osc8_path != NULL) 356 ap_str(osc8_path); 357 else 358 #endif 359 ap_quest(); 360 break; 361 case 'p': /* Percent into file (bytes) */ 362 pos = curr_byte(where); 363 len = ch_length(); 364 if (pos != NULL_POSITION && len > 0) 365 ap_int(percentage(pos,len)); 366 else 367 ap_quest(); 368 break; 369 case 'P': /* Percent into file (lines) */ 370 linenum = currline(where); 371 if (linenum == 0 || 372 (len = ch_length()) == NULL_POSITION || len == ch_zero() || 373 (last_linenum = find_linenum(len)) <= 0) 374 ap_quest(); 375 else 376 ap_int(percentage(linenum, last_linenum)); 377 break; 378 case 's': /* Size of file */ 379 case 'B': 380 len = ch_length(); 381 if (len != NULL_POSITION) 382 ap_pos(len); 383 else 384 ap_quest(); 385 break; 386 case 't': /* Truncate trailing spaces in the message */ 387 while (mp > message && mp[-1] == ' ') 388 mp--; 389 *mp = '\0'; 390 break; 391 case 'T': /* Type of list */ 392 #if TAGS 393 if (ntags()) 394 ap_str("tag"); 395 else 396 #endif 397 ap_str("file"); 398 break; 399 case 'x': /* Name of next file */ 400 h = next_ifile(curr_ifile); 401 if (h != NULL_IFILE) 402 ap_str(get_filename(h)); 403 else 404 ap_quest(); 405 break; 406 } 407 } 408 409 /* 410 * Skip a false conditional. 411 * When a false condition is found (either a false IF or the ELSE part 412 * of a true IF), this routine scans the prototype string to decide 413 * where to resume parsing the string. 414 * We must keep track of nested IFs and skip them properly. 415 */ 416 static constant char * skipcond(constant char *p) 417 { 418 int iflevel; 419 420 /* 421 * We came in here after processing a ? or :, 422 * so we start nested one level deep. 423 */ 424 iflevel = 1; 425 426 for (;;) switch (*++p) 427 { 428 case '?': 429 /* 430 * Start of a nested IF. 431 */ 432 iflevel++; 433 break; 434 case ':': 435 /* 436 * Else. 437 * If this matches the IF we came in here with, 438 * then we're done. 439 */ 440 if (iflevel == 1) 441 return (p); 442 break; 443 case '.': 444 /* 445 * Endif. 446 * If this matches the IF we came in here with, 447 * then we're done. 448 */ 449 if (--iflevel == 0) 450 return (p); 451 break; 452 case '\\': 453 /* 454 * Backslash escapes the next character. 455 */ 456 if (p[1] != '\0') 457 ++p; 458 break; 459 case '\0': 460 /* 461 * Whoops. Hit end of string. 462 * This is a malformed conditional, but just treat it 463 * as if all active conditionals ends here. 464 */ 465 return (p-1); 466 } 467 /*NOTREACHED*/ 468 } 469 470 /* 471 * Decode a char that represents a position on the screen. 472 */ 473 static constant char * wherechar(char constant *p, int *wp) 474 { 475 switch (*p) 476 { 477 case 'b': case 'd': case 'l': case 'p': case 'P': 478 switch (*++p) 479 { 480 case 't': *wp = TOP; break; 481 case 'm': *wp = MIDDLE; break; 482 case 'b': *wp = BOTTOM; break; 483 case 'B': *wp = BOTTOM_PLUS_ONE; break; 484 case 'j': *wp = sindex_from_sline(jump_sline); break; 485 default: *wp = TOP; p--; break; 486 } 487 } 488 return (p); 489 } 490 491 /* 492 * Construct a message based on a prototype string. 493 */ 494 public constant char * pr_expand(constant char *proto) 495 { 496 constant char *p; 497 char c; 498 int where; 499 500 mp = message; 501 502 if (*proto == '\0') 503 return (""); 504 505 for (p = proto; *p != '\0'; p++) 506 { 507 switch (*p) 508 { 509 default: /* Just put the character in the message */ 510 ap_char(*p); 511 break; 512 case '\\': /* Backslash escapes the next character */ 513 if (p[1] != '\0') 514 ap_char(*++p); 515 break; 516 case '?': /* Conditional (IF) */ 517 if ((c = *++p) == '\0') 518 --p; 519 else 520 { 521 where = 0; 522 p = wherechar(p, &where); 523 if (!cond(c, where)) 524 p = skipcond(p); 525 } 526 break; 527 case ':': /* ELSE */ 528 p = skipcond(p); 529 break; 530 case '.': /* ENDIF */ 531 break; 532 case '%': /* Percent escape */ 533 if ((c = *++p) == '\0') 534 --p; 535 else 536 { 537 where = 0; 538 p = wherechar(p, &where); 539 protochar(c, where); 540 } 541 break; 542 } 543 } 544 545 if (mp == message) 546 return (""); 547 return (message); 548 } 549 550 /* 551 * Return a message suitable for printing by the "=" command. 552 */ 553 public constant char * eq_message(void) 554 { 555 return (pr_expand(eqproto)); 556 } 557 558 /* 559 * Return a prompt. 560 * This depends on the prompt type (SHORT, MEDIUM, LONG), etc. 561 * If we can't come up with an appropriate prompt, return NULL 562 * and the caller will prompt with a colon. 563 */ 564 public constant char * pr_string(void) 565 { 566 constant char *prompt; 567 int type; 568 569 type = (!less_is_more) ? pr_type : pr_type ? 0 : 1; 570 prompt = pr_expand((ch_getflags() & CH_HELPFILE) ? 571 hproto : prproto[type]); 572 new_file = FALSE; 573 return (prompt); 574 } 575 576 /* 577 * Return a message suitable for printing while waiting in the F command. 578 */ 579 public constant char * wait_message(void) 580 { 581 return (pr_expand(wproto)); 582 } 583