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