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