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