1 /* vi:set ts=8 sts=4 sw=4: 2 * 3 * VIM - Vi IMproved by Bram Moolenaar 4 * 5 * Do ":help uganda" in Vim to read copying and usage conditions. 6 * Do ":help credits" in Vim to see a list of people who contributed. 7 * See README.txt for an overview of the Vim source code. 8 */ 9 10 /* 11 * misc2.c: Various functions. 12 */ 13 #include "vim.h" 14 15 static char_u *username = NULL; /* cached result of mch_get_user_name() */ 16 17 static char_u *ff_expand_buffer = NULL; /* used for expanding filenames */ 18 19 #if defined(FEAT_VIRTUALEDIT) || defined(PROTO) 20 static int coladvance2 __ARGS((pos_T *pos, int addspaces, int finetune, colnr_T wcol)); 21 22 /* 23 * Return TRUE if in the current mode we need to use virtual. 24 */ 25 int 26 virtual_active() 27 { 28 /* While an operator is being executed we return "virtual_op", because 29 * VIsual_active has already been reset, thus we can't check for "block" 30 * being used. */ 31 if (virtual_op != MAYBE) 32 return virtual_op; 33 return (ve_flags == VE_ALL 34 || ((ve_flags & VE_BLOCK) && VIsual_active && VIsual_mode == Ctrl_V) 35 || ((ve_flags & VE_INSERT) && (State & INSERT))); 36 } 37 38 /* 39 * Get the screen position of the cursor. 40 */ 41 int 42 getviscol() 43 { 44 colnr_T x; 45 46 getvvcol(curwin, &curwin->w_cursor, &x, NULL, NULL); 47 return (int)x; 48 } 49 50 /* 51 * Get the screen position of character col with a coladd in the cursor line. 52 */ 53 int 54 getviscol2(col, coladd) 55 colnr_T col; 56 colnr_T coladd; 57 { 58 colnr_T x; 59 pos_T pos; 60 61 pos.lnum = curwin->w_cursor.lnum; 62 pos.col = col; 63 pos.coladd = coladd; 64 getvvcol(curwin, &pos, &x, NULL, NULL); 65 return (int)x; 66 } 67 68 /* 69 * Go to column "wcol", and add/insert white space as necessary to get the 70 * cursor in that column. 71 * The caller must have saved the cursor line for undo! 72 */ 73 int 74 coladvance_force(wcol) 75 colnr_T wcol; 76 { 77 int rc = coladvance2(&curwin->w_cursor, TRUE, FALSE, wcol); 78 79 if (wcol == MAXCOL) 80 curwin->w_valid &= ~VALID_VIRTCOL; 81 else 82 { 83 /* Virtcol is valid */ 84 curwin->w_valid |= VALID_VIRTCOL; 85 curwin->w_virtcol = wcol; 86 } 87 return rc; 88 } 89 #endif 90 91 /* 92 * Try to advance the Cursor to the specified screen column. 93 * If virtual editing: fine tune the cursor position. 94 * Note that all virtual positions off the end of a line should share 95 * a curwin->w_cursor.col value (n.b. this is equal to STRLEN(line)), 96 * beginning at coladd 0. 97 * 98 * return OK if desired column is reached, FAIL if not 99 */ 100 int 101 coladvance(wcol) 102 colnr_T wcol; 103 { 104 int rc = getvpos(&curwin->w_cursor, wcol); 105 106 if (wcol == MAXCOL || rc == FAIL) 107 curwin->w_valid &= ~VALID_VIRTCOL; 108 else if (*ml_get_cursor() != TAB) 109 { 110 /* Virtcol is valid when not on a TAB */ 111 curwin->w_valid |= VALID_VIRTCOL; 112 curwin->w_virtcol = wcol; 113 } 114 return rc; 115 } 116 117 /* 118 * Return in "pos" the position of the cursor advanced to screen column "wcol". 119 * return OK if desired column is reached, FAIL if not 120 */ 121 int 122 getvpos(pos, wcol) 123 pos_T *pos; 124 colnr_T wcol; 125 { 126 #ifdef FEAT_VIRTUALEDIT 127 return coladvance2(pos, FALSE, virtual_active(), wcol); 128 } 129 130 static int 131 coladvance2(pos, addspaces, finetune, wcol) 132 pos_T *pos; 133 int addspaces; /* change the text to achieve our goal? */ 134 int finetune; /* change char offset for the exact column */ 135 colnr_T wcol; /* column to move to */ 136 { 137 #endif 138 int idx; 139 char_u *ptr; 140 char_u *line; 141 colnr_T col = 0; 142 int csize = 0; 143 int one_more; 144 #ifdef FEAT_LINEBREAK 145 int head = 0; 146 #endif 147 148 one_more = (State & INSERT) 149 || restart_edit != NUL 150 || (VIsual_active && *p_sel != 'o') 151 #ifdef FEAT_VIRTUALEDIT 152 || ((ve_flags & VE_ONEMORE) && wcol < MAXCOL) 153 #endif 154 ; 155 line = ml_get_buf(curbuf, pos->lnum, FALSE); 156 157 if (wcol >= MAXCOL) 158 { 159 idx = (int)STRLEN(line) - 1 + one_more; 160 col = wcol; 161 162 #ifdef FEAT_VIRTUALEDIT 163 if ((addspaces || finetune) && !VIsual_active) 164 { 165 curwin->w_curswant = linetabsize(line) + one_more; 166 if (curwin->w_curswant > 0) 167 --curwin->w_curswant; 168 } 169 #endif 170 } 171 else 172 { 173 #ifdef FEAT_VIRTUALEDIT 174 int width = W_WIDTH(curwin) - win_col_off(curwin); 175 176 if (finetune 177 && curwin->w_p_wrap 178 # ifdef FEAT_VERTSPLIT 179 && curwin->w_width != 0 180 # endif 181 && wcol >= (colnr_T)width) 182 { 183 csize = linetabsize(line); 184 if (csize > 0) 185 csize--; 186 187 if (wcol / width > (colnr_T)csize / width 188 && ((State & INSERT) == 0 || (int)wcol > csize + 1)) 189 { 190 /* In case of line wrapping don't move the cursor beyond the 191 * right screen edge. In Insert mode allow going just beyond 192 * the last character (like what happens when typing and 193 * reaching the right window edge). */ 194 wcol = (csize / width + 1) * width - 1; 195 } 196 } 197 #endif 198 199 ptr = line; 200 while (col <= wcol && *ptr != NUL) 201 { 202 /* Count a tab for what it's worth (if list mode not on) */ 203 #ifdef FEAT_LINEBREAK 204 csize = win_lbr_chartabsize(curwin, line, ptr, col, &head); 205 mb_ptr_adv(ptr); 206 #else 207 csize = lbr_chartabsize_adv(line, &ptr, col); 208 #endif 209 col += csize; 210 } 211 idx = (int)(ptr - line); 212 /* 213 * Handle all the special cases. The virtual_active() check 214 * is needed to ensure that a virtual position off the end of 215 * a line has the correct indexing. The one_more comparison 216 * replaces an explicit add of one_more later on. 217 */ 218 if (col > wcol || (!virtual_active() && one_more == 0)) 219 { 220 idx -= 1; 221 # ifdef FEAT_LINEBREAK 222 /* Don't count the chars from 'showbreak'. */ 223 csize -= head; 224 # endif 225 col -= csize; 226 } 227 228 #ifdef FEAT_VIRTUALEDIT 229 if (virtual_active() 230 && addspaces 231 && ((col != wcol && col != wcol + 1) || csize > 1)) 232 { 233 /* 'virtualedit' is set: The difference between wcol and col is 234 * filled with spaces. */ 235 236 if (line[idx] == NUL) 237 { 238 /* Append spaces */ 239 int correct = wcol - col; 240 char_u *newline = alloc(idx + correct + 1); 241 int t; 242 243 if (newline == NULL) 244 return FAIL; 245 246 for (t = 0; t < idx; ++t) 247 newline[t] = line[t]; 248 249 for (t = 0; t < correct; ++t) 250 newline[t + idx] = ' '; 251 252 newline[idx + correct] = NUL; 253 254 ml_replace(pos->lnum, newline, FALSE); 255 changed_bytes(pos->lnum, (colnr_T)idx); 256 idx += correct; 257 col = wcol; 258 } 259 else 260 { 261 /* Break a tab */ 262 int linelen = (int)STRLEN(line); 263 int correct = wcol - col - csize + 1; /* negative!! */ 264 char_u *newline; 265 int t, s = 0; 266 int v; 267 268 if (-correct > csize) 269 return FAIL; 270 271 newline = alloc(linelen + csize); 272 if (newline == NULL) 273 return FAIL; 274 275 for (t = 0; t < linelen; t++) 276 { 277 if (t != idx) 278 newline[s++] = line[t]; 279 else 280 for (v = 0; v < csize; v++) 281 newline[s++] = ' '; 282 } 283 284 newline[linelen + csize - 1] = NUL; 285 286 ml_replace(pos->lnum, newline, FALSE); 287 changed_bytes(pos->lnum, idx); 288 idx += (csize - 1 + correct); 289 col += correct; 290 } 291 } 292 #endif 293 } 294 295 if (idx < 0) 296 pos->col = 0; 297 else 298 pos->col = idx; 299 300 #ifdef FEAT_VIRTUALEDIT 301 pos->coladd = 0; 302 303 if (finetune) 304 { 305 if (wcol == MAXCOL) 306 { 307 /* The width of the last character is used to set coladd. */ 308 if (!one_more) 309 { 310 colnr_T scol, ecol; 311 312 getvcol(curwin, pos, &scol, NULL, &ecol); 313 pos->coladd = ecol - scol; 314 } 315 } 316 else 317 { 318 int b = (int)wcol - (int)col; 319 320 /* The difference between wcol and col is used to set coladd. */ 321 if (b > 0 && b < (MAXCOL - 2 * W_WIDTH(curwin))) 322 pos->coladd = b; 323 324 col += b; 325 } 326 } 327 #endif 328 329 #ifdef FEAT_MBYTE 330 /* prevent from moving onto a trail byte */ 331 if (has_mbyte) 332 mb_adjustpos(curbuf, pos); 333 #endif 334 335 if (col < wcol) 336 return FAIL; 337 return OK; 338 } 339 340 /* 341 * Increment the cursor position. See inc() for return values. 342 */ 343 int 344 inc_cursor() 345 { 346 return inc(&curwin->w_cursor); 347 } 348 349 /* 350 * Increment the line pointer "lp" crossing line boundaries as necessary. 351 * Return 1 when going to the next line. 352 * Return 2 when moving forward onto a NUL at the end of the line). 353 * Return -1 when at the end of file. 354 * Return 0 otherwise. 355 */ 356 int 357 inc(lp) 358 pos_T *lp; 359 { 360 char_u *p = ml_get_pos(lp); 361 362 if (*p != NUL) /* still within line, move to next char (may be NUL) */ 363 { 364 #ifdef FEAT_MBYTE 365 if (has_mbyte) 366 { 367 int l = (*mb_ptr2len)(p); 368 369 lp->col += l; 370 return ((p[l] != NUL) ? 0 : 2); 371 } 372 #endif 373 lp->col++; 374 #ifdef FEAT_VIRTUALEDIT 375 lp->coladd = 0; 376 #endif 377 return ((p[1] != NUL) ? 0 : 2); 378 } 379 if (lp->lnum != curbuf->b_ml.ml_line_count) /* there is a next line */ 380 { 381 lp->col = 0; 382 lp->lnum++; 383 #ifdef FEAT_VIRTUALEDIT 384 lp->coladd = 0; 385 #endif 386 return 1; 387 } 388 return -1; 389 } 390 391 /* 392 * incl(lp): same as inc(), but skip the NUL at the end of non-empty lines 393 */ 394 int 395 incl(lp) 396 pos_T *lp; 397 { 398 int r; 399 400 if ((r = inc(lp)) >= 1 && lp->col) 401 r = inc(lp); 402 return r; 403 } 404 405 /* 406 * dec(p) 407 * 408 * Decrement the line pointer 'p' crossing line boundaries as necessary. 409 * Return 1 when crossing a line, -1 when at start of file, 0 otherwise. 410 */ 411 int 412 dec_cursor() 413 { 414 return dec(&curwin->w_cursor); 415 } 416 417 int 418 dec(lp) 419 pos_T *lp; 420 { 421 char_u *p; 422 423 #ifdef FEAT_VIRTUALEDIT 424 lp->coladd = 0; 425 #endif 426 if (lp->col > 0) /* still within line */ 427 { 428 lp->col--; 429 #ifdef FEAT_MBYTE 430 if (has_mbyte) 431 { 432 p = ml_get(lp->lnum); 433 lp->col -= (*mb_head_off)(p, p + lp->col); 434 } 435 #endif 436 return 0; 437 } 438 if (lp->lnum > 1) /* there is a prior line */ 439 { 440 lp->lnum--; 441 p = ml_get(lp->lnum); 442 lp->col = (colnr_T)STRLEN(p); 443 #ifdef FEAT_MBYTE 444 if (has_mbyte) 445 lp->col -= (*mb_head_off)(p, p + lp->col); 446 #endif 447 return 1; 448 } 449 return -1; /* at start of file */ 450 } 451 452 /* 453 * decl(lp): same as dec(), but skip the NUL at the end of non-empty lines 454 */ 455 int 456 decl(lp) 457 pos_T *lp; 458 { 459 int r; 460 461 if ((r = dec(lp)) == 1 && lp->col) 462 r = dec(lp); 463 return r; 464 } 465 466 /* 467 * Get the line number relative to the current cursor position, i.e. the 468 * difference between line number and cursor position. Only look for lines that 469 * can be visible, folded lines don't count. 470 */ 471 linenr_T 472 get_cursor_rel_lnum(wp, lnum) 473 win_T *wp; 474 linenr_T lnum; /* line number to get the result for */ 475 { 476 linenr_T cursor = wp->w_cursor.lnum; 477 linenr_T retval = 0; 478 479 #ifdef FEAT_FOLDING 480 if (hasAnyFolding(wp)) 481 { 482 if (lnum > cursor) 483 { 484 while (lnum > cursor) 485 { 486 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL); 487 /* if lnum and cursor are in the same fold, 488 * now lnum <= cursor */ 489 if (lnum > cursor) 490 retval++; 491 lnum--; 492 } 493 } 494 else if (lnum < cursor) 495 { 496 while (lnum < cursor) 497 { 498 (void)hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL); 499 /* if lnum and cursor are in the same fold, 500 * now lnum >= cursor */ 501 if (lnum < cursor) 502 retval--; 503 lnum++; 504 } 505 } 506 /* else if (lnum == cursor) 507 * retval = 0; 508 */ 509 } 510 else 511 #endif 512 retval = lnum - cursor; 513 514 return retval; 515 } 516 517 /* 518 * Make sure curwin->w_cursor.lnum is valid. 519 */ 520 void 521 check_cursor_lnum() 522 { 523 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) 524 { 525 #ifdef FEAT_FOLDING 526 /* If there is a closed fold at the end of the file, put the cursor in 527 * its first line. Otherwise in the last line. */ 528 if (!hasFolding(curbuf->b_ml.ml_line_count, 529 &curwin->w_cursor.lnum, NULL)) 530 #endif 531 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; 532 } 533 if (curwin->w_cursor.lnum <= 0) 534 curwin->w_cursor.lnum = 1; 535 } 536 537 /* 538 * Make sure curwin->w_cursor.col is valid. 539 */ 540 void 541 check_cursor_col() 542 { 543 check_cursor_col_win(curwin); 544 } 545 546 /* 547 * Make sure win->w_cursor.col is valid. 548 */ 549 void 550 check_cursor_col_win(win) 551 win_T *win; 552 { 553 colnr_T len; 554 #ifdef FEAT_VIRTUALEDIT 555 colnr_T oldcol = win->w_cursor.col; 556 colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd; 557 #endif 558 559 len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, FALSE)); 560 if (len == 0) 561 win->w_cursor.col = 0; 562 else if (win->w_cursor.col >= len) 563 { 564 /* Allow cursor past end-of-line when: 565 * - in Insert mode or restarting Insert mode 566 * - in Visual mode and 'selection' isn't "old" 567 * - 'virtualedit' is set */ 568 if ((State & INSERT) || restart_edit 569 || (VIsual_active && *p_sel != 'o') 570 #ifdef FEAT_VIRTUALEDIT 571 || (ve_flags & VE_ONEMORE) 572 #endif 573 || virtual_active()) 574 win->w_cursor.col = len; 575 else 576 { 577 win->w_cursor.col = len - 1; 578 #ifdef FEAT_MBYTE 579 /* Move the cursor to the head byte. */ 580 if (has_mbyte) 581 mb_adjustpos(win->w_buffer, &win->w_cursor); 582 #endif 583 } 584 } 585 else if (win->w_cursor.col < 0) 586 win->w_cursor.col = 0; 587 588 #ifdef FEAT_VIRTUALEDIT 589 /* If virtual editing is on, we can leave the cursor on the old position, 590 * only we must set it to virtual. But don't do it when at the end of the 591 * line. */ 592 if (oldcol == MAXCOL) 593 win->w_cursor.coladd = 0; 594 else if (ve_flags == VE_ALL) 595 { 596 if (oldcoladd > win->w_cursor.col) 597 win->w_cursor.coladd = oldcoladd - win->w_cursor.col; 598 else 599 /* avoid weird number when there is a miscalculation or overflow */ 600 win->w_cursor.coladd = 0; 601 } 602 #endif 603 } 604 605 /* 606 * make sure curwin->w_cursor in on a valid character 607 */ 608 void 609 check_cursor() 610 { 611 check_cursor_lnum(); 612 check_cursor_col(); 613 } 614 615 #if defined(FEAT_TEXTOBJ) || defined(PROTO) 616 /* 617 * Make sure curwin->w_cursor is not on the NUL at the end of the line. 618 * Allow it when in Visual mode and 'selection' is not "old". 619 */ 620 void 621 adjust_cursor_col() 622 { 623 if (curwin->w_cursor.col > 0 624 && (!VIsual_active || *p_sel == 'o') 625 && gchar_cursor() == NUL) 626 --curwin->w_cursor.col; 627 } 628 #endif 629 630 /* 631 * When curwin->w_leftcol has changed, adjust the cursor position. 632 * Return TRUE if the cursor was moved. 633 */ 634 int 635 leftcol_changed() 636 { 637 long lastcol; 638 colnr_T s, e; 639 int retval = FALSE; 640 641 changed_cline_bef_curs(); 642 lastcol = curwin->w_leftcol + W_WIDTH(curwin) - curwin_col_off() - 1; 643 validate_virtcol(); 644 645 /* 646 * If the cursor is right or left of the screen, move it to last or first 647 * character. 648 */ 649 if (curwin->w_virtcol > (colnr_T)(lastcol - p_siso)) 650 { 651 retval = TRUE; 652 coladvance((colnr_T)(lastcol - p_siso)); 653 } 654 else if (curwin->w_virtcol < curwin->w_leftcol + p_siso) 655 { 656 retval = TRUE; 657 (void)coladvance((colnr_T)(curwin->w_leftcol + p_siso)); 658 } 659 660 /* 661 * If the start of the character under the cursor is not on the screen, 662 * advance the cursor one more char. If this fails (last char of the 663 * line) adjust the scrolling. 664 */ 665 getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e); 666 if (e > (colnr_T)lastcol) 667 { 668 retval = TRUE; 669 coladvance(s - 1); 670 } 671 else if (s < curwin->w_leftcol) 672 { 673 retval = TRUE; 674 if (coladvance(e + 1) == FAIL) /* there isn't another character */ 675 { 676 curwin->w_leftcol = s; /* adjust w_leftcol instead */ 677 changed_cline_bef_curs(); 678 } 679 } 680 681 if (retval) 682 curwin->w_set_curswant = TRUE; 683 redraw_later(NOT_VALID); 684 return retval; 685 } 686 687 /********************************************************************** 688 * Various routines dealing with allocation and deallocation of memory. 689 */ 690 691 #if defined(MEM_PROFILE) || defined(PROTO) 692 693 # define MEM_SIZES 8200 694 static long_u mem_allocs[MEM_SIZES]; 695 static long_u mem_frees[MEM_SIZES]; 696 static long_u mem_allocated; 697 static long_u mem_freed; 698 static long_u mem_peak; 699 static long_u num_alloc; 700 static long_u num_freed; 701 702 static void mem_pre_alloc_s __ARGS((size_t *sizep)); 703 static void mem_pre_alloc_l __ARGS((long_u *sizep)); 704 static void mem_post_alloc __ARGS((void **pp, size_t size)); 705 static void mem_pre_free __ARGS((void **pp)); 706 707 static void 708 mem_pre_alloc_s(sizep) 709 size_t *sizep; 710 { 711 *sizep += sizeof(size_t); 712 } 713 714 static void 715 mem_pre_alloc_l(sizep) 716 long_u *sizep; 717 { 718 *sizep += sizeof(size_t); 719 } 720 721 static void 722 mem_post_alloc(pp, size) 723 void **pp; 724 size_t size; 725 { 726 if (*pp == NULL) 727 return; 728 size -= sizeof(size_t); 729 *(long_u *)*pp = size; 730 if (size <= MEM_SIZES-1) 731 mem_allocs[size-1]++; 732 else 733 mem_allocs[MEM_SIZES-1]++; 734 mem_allocated += size; 735 if (mem_allocated - mem_freed > mem_peak) 736 mem_peak = mem_allocated - mem_freed; 737 num_alloc++; 738 *pp = (void *)((char *)*pp + sizeof(size_t)); 739 } 740 741 static void 742 mem_pre_free(pp) 743 void **pp; 744 { 745 long_u size; 746 747 *pp = (void *)((char *)*pp - sizeof(size_t)); 748 size = *(size_t *)*pp; 749 if (size <= MEM_SIZES-1) 750 mem_frees[size-1]++; 751 else 752 mem_frees[MEM_SIZES-1]++; 753 mem_freed += size; 754 num_freed++; 755 } 756 757 /* 758 * called on exit via atexit() 759 */ 760 void 761 vim_mem_profile_dump() 762 { 763 int i, j; 764 765 printf("\r\n"); 766 j = 0; 767 for (i = 0; i < MEM_SIZES - 1; i++) 768 { 769 if (mem_allocs[i] || mem_frees[i]) 770 { 771 if (mem_frees[i] > mem_allocs[i]) 772 printf("\r\n%s", _("ERROR: ")); 773 printf("[%4d / %4lu-%-4lu] ", i + 1, mem_allocs[i], mem_frees[i]); 774 j++; 775 if (j > 3) 776 { 777 j = 0; 778 printf("\r\n"); 779 } 780 } 781 } 782 783 i = MEM_SIZES - 1; 784 if (mem_allocs[i]) 785 { 786 printf("\r\n"); 787 if (mem_frees[i] > mem_allocs[i]) 788 puts(_("ERROR: ")); 789 printf("[>%d / %4lu-%-4lu]", i, mem_allocs[i], mem_frees[i]); 790 } 791 792 printf(_("\n[bytes] total alloc-freed %lu-%lu, in use %lu, peak use %lu\n"), 793 mem_allocated, mem_freed, mem_allocated - mem_freed, mem_peak); 794 printf(_("[calls] total re/malloc()'s %lu, total free()'s %lu\n\n"), 795 num_alloc, num_freed); 796 } 797 798 #endif /* MEM_PROFILE */ 799 800 /* 801 * Some memory is reserved for error messages and for being able to 802 * call mf_release_all(), which needs some memory for mf_trans_add(). 803 */ 804 #if defined(MSDOS) && !defined(DJGPP) 805 # define SMALL_MEM 806 # define KEEP_ROOM 8192L 807 #else 808 # define KEEP_ROOM (2 * 8192L) 809 #endif 810 #define KEEP_ROOM_KB (KEEP_ROOM / 1024L) 811 812 /* 813 * Note: if unsigned is 16 bits we can only allocate up to 64K with alloc(). 814 * Use lalloc for larger blocks. 815 */ 816 char_u * 817 alloc(size) 818 unsigned size; 819 { 820 return (lalloc((long_u)size, TRUE)); 821 } 822 823 /* 824 * Allocate memory and set all bytes to zero. 825 */ 826 char_u * 827 alloc_clear(size) 828 unsigned size; 829 { 830 char_u *p; 831 832 p = lalloc((long_u)size, TRUE); 833 if (p != NULL) 834 (void)vim_memset(p, 0, (size_t)size); 835 return p; 836 } 837 838 /* 839 * alloc() with check for maximum line length 840 */ 841 char_u * 842 alloc_check(size) 843 unsigned size; 844 { 845 #if !defined(UNIX) && !defined(__EMX__) 846 if (sizeof(int) == 2 && size > 0x7fff) 847 { 848 /* Don't hide this message */ 849 emsg_silent = 0; 850 EMSG(_("E340: Line is becoming too long")); 851 return NULL; 852 } 853 #endif 854 return (lalloc((long_u)size, TRUE)); 855 } 856 857 /* 858 * Allocate memory like lalloc() and set all bytes to zero. 859 */ 860 char_u * 861 lalloc_clear(size, message) 862 long_u size; 863 int message; 864 { 865 char_u *p; 866 867 p = (lalloc(size, message)); 868 if (p != NULL) 869 (void)vim_memset(p, 0, (size_t)size); 870 return p; 871 } 872 873 /* 874 * Low level memory allocation function. 875 * This is used often, KEEP IT FAST! 876 */ 877 char_u * 878 lalloc(size, message) 879 long_u size; 880 int message; 881 { 882 char_u *p; /* pointer to new storage space */ 883 static int releasing = FALSE; /* don't do mf_release_all() recursive */ 884 int try_again; 885 #if defined(HAVE_AVAIL_MEM) && !defined(SMALL_MEM) 886 static long_u allocated = 0; /* allocated since last avail check */ 887 #endif 888 889 /* Safety check for allocating zero bytes */ 890 if (size == 0) 891 { 892 /* Don't hide this message */ 893 emsg_silent = 0; 894 EMSGN(_("E341: Internal error: lalloc(%ld, )"), size); 895 return NULL; 896 } 897 898 #ifdef MEM_PROFILE 899 mem_pre_alloc_l(&size); 900 #endif 901 902 #if defined(MSDOS) && !defined(DJGPP) 903 if (size >= 0xfff0) /* in MSDOS we can't deal with >64K blocks */ 904 p = NULL; 905 else 906 #endif 907 908 /* 909 * Loop when out of memory: Try to release some memfile blocks and 910 * if some blocks are released call malloc again. 911 */ 912 for (;;) 913 { 914 /* 915 * Handle three kind of systems: 916 * 1. No check for available memory: Just return. 917 * 2. Slow check for available memory: call mch_avail_mem() after 918 * allocating KEEP_ROOM amount of memory. 919 * 3. Strict check for available memory: call mch_avail_mem() 920 */ 921 if ((p = (char_u *)malloc((size_t)size)) != NULL) 922 { 923 #ifndef HAVE_AVAIL_MEM 924 /* 1. No check for available memory: Just return. */ 925 goto theend; 926 #else 927 # ifndef SMALL_MEM 928 /* 2. Slow check for available memory: call mch_avail_mem() after 929 * allocating (KEEP_ROOM / 2) amount of memory. */ 930 allocated += size; 931 if (allocated < KEEP_ROOM / 2) 932 goto theend; 933 allocated = 0; 934 # endif 935 /* 3. check for available memory: call mch_avail_mem() */ 936 if (mch_avail_mem(TRUE) < KEEP_ROOM_KB && !releasing) 937 { 938 free((char *)p); /* System is low... no go! */ 939 p = NULL; 940 } 941 else 942 goto theend; 943 #endif 944 } 945 /* 946 * Remember that mf_release_all() is being called to avoid an endless 947 * loop, because mf_release_all() may call alloc() recursively. 948 */ 949 if (releasing) 950 break; 951 releasing = TRUE; 952 953 clear_sb_text(); /* free any scrollback text */ 954 try_again = mf_release_all(); /* release as many blocks as possible */ 955 #ifdef FEAT_EVAL 956 try_again |= garbage_collect(); /* cleanup recursive lists/dicts */ 957 #endif 958 959 releasing = FALSE; 960 if (!try_again) 961 break; 962 } 963 964 if (message && p == NULL) 965 do_outofmem_msg(size); 966 967 theend: 968 #ifdef MEM_PROFILE 969 mem_post_alloc((void **)&p, (size_t)size); 970 #endif 971 return p; 972 } 973 974 #if defined(MEM_PROFILE) || defined(PROTO) 975 /* 976 * realloc() with memory profiling. 977 */ 978 void * 979 mem_realloc(ptr, size) 980 void *ptr; 981 size_t size; 982 { 983 void *p; 984 985 mem_pre_free(&ptr); 986 mem_pre_alloc_s(&size); 987 988 p = realloc(ptr, size); 989 990 mem_post_alloc(&p, size); 991 992 return p; 993 } 994 #endif 995 996 /* 997 * Avoid repeating the error message many times (they take 1 second each). 998 * Did_outofmem_msg is reset when a character is read. 999 */ 1000 void 1001 do_outofmem_msg(size) 1002 long_u size; 1003 { 1004 if (!did_outofmem_msg) 1005 { 1006 /* Don't hide this message */ 1007 emsg_silent = 0; 1008 1009 /* Must come first to avoid coming back here when printing the error 1010 * message fails, e.g. when setting v:errmsg. */ 1011 did_outofmem_msg = TRUE; 1012 1013 EMSGN(_("E342: Out of memory! (allocating %lu bytes)"), size); 1014 } 1015 } 1016 1017 #if defined(EXITFREE) || defined(PROTO) 1018 1019 # if defined(FEAT_SEARCHPATH) 1020 static void free_findfile __ARGS((void)); 1021 # endif 1022 1023 /* 1024 * Free everything that we allocated. 1025 * Can be used to detect memory leaks, e.g., with ccmalloc. 1026 * NOTE: This is tricky! Things are freed that functions depend on. Don't be 1027 * surprised if Vim crashes... 1028 * Some things can't be freed, esp. things local to a library function. 1029 */ 1030 void 1031 free_all_mem() 1032 { 1033 buf_T *buf, *nextbuf; 1034 static int entered = FALSE; 1035 1036 /* When we cause a crash here it is caught and Vim tries to exit cleanly. 1037 * Don't try freeing everything again. */ 1038 if (entered) 1039 return; 1040 entered = TRUE; 1041 1042 # ifdef FEAT_AUTOCMD 1043 /* Don't want to trigger autocommands from here on. */ 1044 block_autocmds(); 1045 # endif 1046 1047 # ifdef FEAT_WINDOWS 1048 /* Close all tabs and windows. Reset 'equalalways' to avoid redraws. */ 1049 p_ea = FALSE; 1050 if (first_tabpage->tp_next != NULL) 1051 do_cmdline_cmd((char_u *)"tabonly!"); 1052 if (firstwin != lastwin) 1053 do_cmdline_cmd((char_u *)"only!"); 1054 # endif 1055 1056 # if defined(FEAT_SPELL) 1057 /* Free all spell info. */ 1058 spell_free_all(); 1059 # endif 1060 1061 # if defined(FEAT_USR_CMDS) 1062 /* Clear user commands (before deleting buffers). */ 1063 ex_comclear(NULL); 1064 # endif 1065 1066 # ifdef FEAT_MENU 1067 /* Clear menus. */ 1068 do_cmdline_cmd((char_u *)"aunmenu *"); 1069 # ifdef FEAT_MULTI_LANG 1070 do_cmdline_cmd((char_u *)"menutranslate clear"); 1071 # endif 1072 # endif 1073 1074 /* Clear mappings, abbreviations, breakpoints. */ 1075 do_cmdline_cmd((char_u *)"lmapclear"); 1076 do_cmdline_cmd((char_u *)"xmapclear"); 1077 do_cmdline_cmd((char_u *)"mapclear"); 1078 do_cmdline_cmd((char_u *)"mapclear!"); 1079 do_cmdline_cmd((char_u *)"abclear"); 1080 # if defined(FEAT_EVAL) 1081 do_cmdline_cmd((char_u *)"breakdel *"); 1082 # endif 1083 # if defined(FEAT_PROFILE) 1084 do_cmdline_cmd((char_u *)"profdel *"); 1085 # endif 1086 # if defined(FEAT_KEYMAP) 1087 do_cmdline_cmd((char_u *)"set keymap="); 1088 #endif 1089 1090 # ifdef FEAT_TITLE 1091 free_titles(); 1092 # endif 1093 # if defined(FEAT_SEARCHPATH) 1094 free_findfile(); 1095 # endif 1096 1097 /* Obviously named calls. */ 1098 # if defined(FEAT_AUTOCMD) 1099 free_all_autocmds(); 1100 # endif 1101 clear_termcodes(); 1102 free_all_options(); 1103 free_all_marks(); 1104 alist_clear(&global_alist); 1105 free_homedir(); 1106 # if defined(FEAT_CMDL_COMPL) 1107 free_users(); 1108 # endif 1109 free_search_patterns(); 1110 free_old_sub(); 1111 free_last_insert(); 1112 free_prev_shellcmd(); 1113 free_regexp_stuff(); 1114 free_tag_stuff(); 1115 free_cd_dir(); 1116 # ifdef FEAT_SIGNS 1117 free_signs(); 1118 # endif 1119 # ifdef FEAT_EVAL 1120 set_expr_line(NULL); 1121 # endif 1122 # ifdef FEAT_DIFF 1123 diff_clear(curtab); 1124 # endif 1125 clear_sb_text(); /* free any scrollback text */ 1126 1127 /* Free some global vars. */ 1128 vim_free(username); 1129 # ifdef FEAT_CLIPBOARD 1130 vim_regfree(clip_exclude_prog); 1131 # endif 1132 vim_free(last_cmdline); 1133 # ifdef FEAT_CMDHIST 1134 vim_free(new_last_cmdline); 1135 # endif 1136 set_keep_msg(NULL, 0); 1137 vim_free(ff_expand_buffer); 1138 1139 /* Clear cmdline history. */ 1140 p_hi = 0; 1141 # ifdef FEAT_CMDHIST 1142 init_history(); 1143 # endif 1144 1145 #ifdef FEAT_QUICKFIX 1146 { 1147 win_T *win; 1148 tabpage_T *tab; 1149 1150 qf_free_all(NULL); 1151 /* Free all location lists */ 1152 FOR_ALL_TAB_WINDOWS(tab, win) 1153 qf_free_all(win); 1154 } 1155 #endif 1156 1157 /* Close all script inputs. */ 1158 close_all_scripts(); 1159 1160 #if defined(FEAT_WINDOWS) 1161 /* Destroy all windows. Must come before freeing buffers. */ 1162 win_free_all(); 1163 #endif 1164 1165 /* Free all buffers. Reset 'autochdir' to avoid accessing things that 1166 * were freed already. */ 1167 #ifdef FEAT_AUTOCHDIR 1168 p_acd = FALSE; 1169 #endif 1170 for (buf = firstbuf; buf != NULL; ) 1171 { 1172 nextbuf = buf->b_next; 1173 close_buffer(NULL, buf, DOBUF_WIPE, FALSE); 1174 if (buf_valid(buf)) 1175 buf = nextbuf; /* didn't work, try next one */ 1176 else 1177 buf = firstbuf; 1178 } 1179 1180 #ifdef FEAT_ARABIC 1181 free_cmdline_buf(); 1182 #endif 1183 1184 /* Clear registers. */ 1185 clear_registers(); 1186 ResetRedobuff(); 1187 ResetRedobuff(); 1188 1189 #if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11) 1190 vim_free(serverDelayedStartName); 1191 #endif 1192 1193 /* highlight info */ 1194 free_highlight(); 1195 1196 reset_last_sourcing(); 1197 1198 #ifdef FEAT_WINDOWS 1199 free_tabpage(first_tabpage); 1200 first_tabpage = NULL; 1201 #endif 1202 1203 # ifdef UNIX 1204 /* Machine-specific free. */ 1205 mch_free_mem(); 1206 # endif 1207 1208 /* message history */ 1209 for (;;) 1210 if (delete_first_msg() == FAIL) 1211 break; 1212 1213 # ifdef FEAT_EVAL 1214 eval_clear(); 1215 # endif 1216 1217 free_termoptions(); 1218 1219 /* screenlines (can't display anything now!) */ 1220 free_screenlines(); 1221 1222 #if defined(USE_XSMP) 1223 xsmp_close(); 1224 #endif 1225 #ifdef FEAT_GUI_GTK 1226 gui_mch_free_all(); 1227 #endif 1228 clear_hl_tables(); 1229 1230 vim_free(IObuff); 1231 vim_free(NameBuff); 1232 } 1233 #endif 1234 1235 /* 1236 * Copy "string" into newly allocated memory. 1237 */ 1238 char_u * 1239 vim_strsave(string) 1240 char_u *string; 1241 { 1242 char_u *p; 1243 unsigned len; 1244 1245 len = (unsigned)STRLEN(string) + 1; 1246 p = alloc(len); 1247 if (p != NULL) 1248 mch_memmove(p, string, (size_t)len); 1249 return p; 1250 } 1251 1252 /* 1253 * Copy up to "len" bytes of "string" into newly allocated memory and 1254 * terminate with a NUL. 1255 * The allocated memory always has size "len + 1", also when "string" is 1256 * shorter. 1257 */ 1258 char_u * 1259 vim_strnsave(string, len) 1260 char_u *string; 1261 int len; 1262 { 1263 char_u *p; 1264 1265 p = alloc((unsigned)(len + 1)); 1266 if (p != NULL) 1267 { 1268 STRNCPY(p, string, len); 1269 p[len] = NUL; 1270 } 1271 return p; 1272 } 1273 1274 /* 1275 * Same as vim_strsave(), but any characters found in esc_chars are preceded 1276 * by a backslash. 1277 */ 1278 char_u * 1279 vim_strsave_escaped(string, esc_chars) 1280 char_u *string; 1281 char_u *esc_chars; 1282 { 1283 return vim_strsave_escaped_ext(string, esc_chars, '\\', FALSE); 1284 } 1285 1286 /* 1287 * Same as vim_strsave_escaped(), but when "bsl" is TRUE also escape 1288 * characters where rem_backslash() would remove the backslash. 1289 * Escape the characters with "cc". 1290 */ 1291 char_u * 1292 vim_strsave_escaped_ext(string, esc_chars, cc, bsl) 1293 char_u *string; 1294 char_u *esc_chars; 1295 int cc; 1296 int bsl; 1297 { 1298 char_u *p; 1299 char_u *p2; 1300 char_u *escaped_string; 1301 unsigned length; 1302 #ifdef FEAT_MBYTE 1303 int l; 1304 #endif 1305 1306 /* 1307 * First count the number of backslashes required. 1308 * Then allocate the memory and insert them. 1309 */ 1310 length = 1; /* count the trailing NUL */ 1311 for (p = string; *p; p++) 1312 { 1313 #ifdef FEAT_MBYTE 1314 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) 1315 { 1316 length += l; /* count a multibyte char */ 1317 p += l - 1; 1318 continue; 1319 } 1320 #endif 1321 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p))) 1322 ++length; /* count a backslash */ 1323 ++length; /* count an ordinary char */ 1324 } 1325 escaped_string = alloc(length); 1326 if (escaped_string != NULL) 1327 { 1328 p2 = escaped_string; 1329 for (p = string; *p; p++) 1330 { 1331 #ifdef FEAT_MBYTE 1332 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) 1333 { 1334 mch_memmove(p2, p, (size_t)l); 1335 p2 += l; 1336 p += l - 1; /* skip multibyte char */ 1337 continue; 1338 } 1339 #endif 1340 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p))) 1341 *p2++ = cc; 1342 *p2++ = *p; 1343 } 1344 *p2 = NUL; 1345 } 1346 return escaped_string; 1347 } 1348 1349 /* 1350 * Return TRUE when 'shell' has "csh" in the tail. 1351 */ 1352 int 1353 csh_like_shell() 1354 { 1355 return (strstr((char *)gettail(p_sh), "csh") != NULL); 1356 } 1357 1358 /* 1359 * Escape "string" for use as a shell argument with system(). 1360 * This uses single quotes, except when we know we need to use double quotes 1361 * (MS-DOS and MS-Windows without 'shellslash' set). 1362 * Escape a newline, depending on the 'shell' option. 1363 * When "do_special" is TRUE also replace "!", "%", "#" and things starting 1364 * with "<" like "<cfile>". 1365 * When "do_newline" is FALSE do not escape newline unless it is csh shell. 1366 * Returns the result in allocated memory, NULL if we have run out. 1367 */ 1368 char_u * 1369 vim_strsave_shellescape(string, do_special, do_newline) 1370 char_u *string; 1371 int do_special; 1372 int do_newline; 1373 { 1374 unsigned length; 1375 char_u *p; 1376 char_u *d; 1377 char_u *escaped_string; 1378 int l; 1379 int csh_like; 1380 1381 /* Only csh and similar shells expand '!' within single quotes. For sh and 1382 * the like we must not put a backslash before it, it will be taken 1383 * literally. If do_special is set the '!' will be escaped twice. 1384 * Csh also needs to have "\n" escaped twice when do_special is set. */ 1385 csh_like = csh_like_shell(); 1386 1387 /* First count the number of extra bytes required. */ 1388 length = (unsigned)STRLEN(string) + 3; /* two quotes and a trailing NUL */ 1389 for (p = string; *p != NUL; mb_ptr_adv(p)) 1390 { 1391 # if defined(WIN32) || defined(WIN16) || defined(DOS) 1392 if (!p_ssl) 1393 { 1394 if (*p == '"') 1395 ++length; /* " -> "" */ 1396 } 1397 else 1398 # endif 1399 if (*p == '\'') 1400 length += 3; /* ' => '\'' */ 1401 if ((*p == '\n' && (csh_like || do_newline)) 1402 || (*p == '!' && (csh_like || do_special))) 1403 { 1404 ++length; /* insert backslash */ 1405 if (csh_like && do_special) 1406 ++length; /* insert backslash */ 1407 } 1408 if (do_special && find_cmdline_var(p, &l) >= 0) 1409 { 1410 ++length; /* insert backslash */ 1411 p += l - 1; 1412 } 1413 } 1414 1415 /* Allocate memory for the result and fill it. */ 1416 escaped_string = alloc(length); 1417 if (escaped_string != NULL) 1418 { 1419 d = escaped_string; 1420 1421 /* add opening quote */ 1422 # if defined(WIN32) || defined(WIN16) || defined(DOS) 1423 if (!p_ssl) 1424 *d++ = '"'; 1425 else 1426 # endif 1427 *d++ = '\''; 1428 1429 for (p = string; *p != NUL; ) 1430 { 1431 # if defined(WIN32) || defined(WIN16) || defined(DOS) 1432 if (!p_ssl) 1433 { 1434 if (*p == '"') 1435 { 1436 *d++ = '"'; 1437 *d++ = '"'; 1438 ++p; 1439 continue; 1440 } 1441 } 1442 else 1443 # endif 1444 if (*p == '\'') 1445 { 1446 *d++ = '\''; 1447 *d++ = '\\'; 1448 *d++ = '\''; 1449 *d++ = '\''; 1450 ++p; 1451 continue; 1452 } 1453 if ((*p == '\n' && (csh_like || do_newline)) 1454 || (*p == '!' && (csh_like || do_special))) 1455 { 1456 *d++ = '\\'; 1457 if (csh_like && do_special) 1458 *d++ = '\\'; 1459 *d++ = *p++; 1460 continue; 1461 } 1462 if (do_special && find_cmdline_var(p, &l) >= 0) 1463 { 1464 *d++ = '\\'; /* insert backslash */ 1465 while (--l >= 0) /* copy the var */ 1466 *d++ = *p++; 1467 continue; 1468 } 1469 1470 MB_COPY_CHAR(p, d); 1471 } 1472 1473 /* add terminating quote and finish with a NUL */ 1474 # if defined(WIN32) || defined(WIN16) || defined(DOS) 1475 if (!p_ssl) 1476 *d++ = '"'; 1477 else 1478 # endif 1479 *d++ = '\''; 1480 *d = NUL; 1481 } 1482 1483 return escaped_string; 1484 } 1485 1486 /* 1487 * Like vim_strsave(), but make all characters uppercase. 1488 * This uses ASCII lower-to-upper case translation, language independent. 1489 */ 1490 char_u * 1491 vim_strsave_up(string) 1492 char_u *string; 1493 { 1494 char_u *p1; 1495 1496 p1 = vim_strsave(string); 1497 vim_strup(p1); 1498 return p1; 1499 } 1500 1501 /* 1502 * Like vim_strnsave(), but make all characters uppercase. 1503 * This uses ASCII lower-to-upper case translation, language independent. 1504 */ 1505 char_u * 1506 vim_strnsave_up(string, len) 1507 char_u *string; 1508 int len; 1509 { 1510 char_u *p1; 1511 1512 p1 = vim_strnsave(string, len); 1513 vim_strup(p1); 1514 return p1; 1515 } 1516 1517 /* 1518 * ASCII lower-to-upper case translation, language independent. 1519 */ 1520 void 1521 vim_strup(p) 1522 char_u *p; 1523 { 1524 char_u *p2; 1525 int c; 1526 1527 if (p != NULL) 1528 { 1529 p2 = p; 1530 while ((c = *p2) != NUL) 1531 #ifdef EBCDIC 1532 *p2++ = isalpha(c) ? toupper(c) : c; 1533 #else 1534 *p2++ = (c < 'a' || c > 'z') ? c : (c - 0x20); 1535 #endif 1536 } 1537 } 1538 1539 #if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO) 1540 /* 1541 * Make string "s" all upper-case and return it in allocated memory. 1542 * Handles multi-byte characters as well as possible. 1543 * Returns NULL when out of memory. 1544 */ 1545 char_u * 1546 strup_save(orig) 1547 char_u *orig; 1548 { 1549 char_u *p; 1550 char_u *res; 1551 1552 res = p = vim_strsave(orig); 1553 1554 if (res != NULL) 1555 while (*p != NUL) 1556 { 1557 # ifdef FEAT_MBYTE 1558 int l; 1559 1560 if (enc_utf8) 1561 { 1562 int c, uc; 1563 int newl; 1564 char_u *s; 1565 1566 c = utf_ptr2char(p); 1567 uc = utf_toupper(c); 1568 1569 /* Reallocate string when byte count changes. This is rare, 1570 * thus it's OK to do another malloc()/free(). */ 1571 l = utf_ptr2len(p); 1572 newl = utf_char2len(uc); 1573 if (newl != l) 1574 { 1575 s = alloc((unsigned)STRLEN(res) + 1 + newl - l); 1576 if (s == NULL) 1577 break; 1578 mch_memmove(s, res, p - res); 1579 STRCPY(s + (p - res) + newl, p + l); 1580 p = s + (p - res); 1581 vim_free(res); 1582 res = s; 1583 } 1584 1585 utf_char2bytes(uc, p); 1586 p += newl; 1587 } 1588 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) 1589 p += l; /* skip multi-byte character */ 1590 else 1591 # endif 1592 { 1593 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */ 1594 p++; 1595 } 1596 } 1597 1598 return res; 1599 } 1600 #endif 1601 1602 /* 1603 * delete spaces at the end of a string 1604 */ 1605 void 1606 del_trailing_spaces(ptr) 1607 char_u *ptr; 1608 { 1609 char_u *q; 1610 1611 q = ptr + STRLEN(ptr); 1612 while (--q > ptr && vim_iswhite(q[0]) && q[-1] != '\\' && q[-1] != Ctrl_V) 1613 *q = NUL; 1614 } 1615 1616 /* 1617 * Like strncpy(), but always terminate the result with one NUL. 1618 * "to" must be "len + 1" long! 1619 */ 1620 void 1621 vim_strncpy(to, from, len) 1622 char_u *to; 1623 char_u *from; 1624 size_t len; 1625 { 1626 STRNCPY(to, from, len); 1627 to[len] = NUL; 1628 } 1629 1630 /* 1631 * Like strcat(), but make sure the result fits in "tosize" bytes and is 1632 * always NUL terminated. 1633 */ 1634 void 1635 vim_strcat(to, from, tosize) 1636 char_u *to; 1637 char_u *from; 1638 size_t tosize; 1639 { 1640 size_t tolen = STRLEN(to); 1641 size_t fromlen = STRLEN(from); 1642 1643 if (tolen + fromlen + 1 > tosize) 1644 { 1645 mch_memmove(to + tolen, from, tosize - tolen - 1); 1646 to[tosize - 1] = NUL; 1647 } 1648 else 1649 STRCPY(to + tolen, from); 1650 } 1651 1652 /* 1653 * Isolate one part of a string option where parts are separated with 1654 * "sep_chars". 1655 * The part is copied into "buf[maxlen]". 1656 * "*option" is advanced to the next part. 1657 * The length is returned. 1658 */ 1659 int 1660 copy_option_part(option, buf, maxlen, sep_chars) 1661 char_u **option; 1662 char_u *buf; 1663 int maxlen; 1664 char *sep_chars; 1665 { 1666 int len = 0; 1667 char_u *p = *option; 1668 1669 /* skip '.' at start of option part, for 'suffixes' */ 1670 if (*p == '.') 1671 buf[len++] = *p++; 1672 while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL) 1673 { 1674 /* 1675 * Skip backslash before a separator character and space. 1676 */ 1677 if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL) 1678 ++p; 1679 if (len < maxlen - 1) 1680 buf[len++] = *p; 1681 ++p; 1682 } 1683 buf[len] = NUL; 1684 1685 if (*p != NUL && *p != ',') /* skip non-standard separator */ 1686 ++p; 1687 p = skip_to_option_part(p); /* p points to next file name */ 1688 1689 *option = p; 1690 return len; 1691 } 1692 1693 /* 1694 * Replacement for free() that ignores NULL pointers. 1695 * Also skip free() when exiting for sure, this helps when we caught a deadly 1696 * signal that was caused by a crash in free(). 1697 */ 1698 void 1699 vim_free(x) 1700 void *x; 1701 { 1702 if (x != NULL && !really_exiting) 1703 { 1704 #ifdef MEM_PROFILE 1705 mem_pre_free(&x); 1706 #endif 1707 free(x); 1708 } 1709 } 1710 1711 #ifndef HAVE_MEMSET 1712 void * 1713 vim_memset(ptr, c, size) 1714 void *ptr; 1715 int c; 1716 size_t size; 1717 { 1718 char *p = ptr; 1719 1720 while (size-- > 0) 1721 *p++ = c; 1722 return ptr; 1723 } 1724 #endif 1725 1726 #ifdef VIM_MEMCMP 1727 /* 1728 * Return zero when "b1" and "b2" are the same for "len" bytes. 1729 * Return non-zero otherwise. 1730 */ 1731 int 1732 vim_memcmp(b1, b2, len) 1733 void *b1; 1734 void *b2; 1735 size_t len; 1736 { 1737 char_u *p1 = (char_u *)b1, *p2 = (char_u *)b2; 1738 1739 for ( ; len > 0; --len) 1740 { 1741 if (*p1 != *p2) 1742 return 1; 1743 ++p1; 1744 ++p2; 1745 } 1746 return 0; 1747 } 1748 #endif 1749 1750 #ifdef VIM_MEMMOVE 1751 /* 1752 * Version of memmove() that handles overlapping source and destination. 1753 * For systems that don't have a function that is guaranteed to do that (SYSV). 1754 */ 1755 void 1756 mch_memmove(dst_arg, src_arg, len) 1757 void *src_arg, *dst_arg; 1758 size_t len; 1759 { 1760 /* 1761 * A void doesn't have a size, we use char pointers. 1762 */ 1763 char *dst = dst_arg, *src = src_arg; 1764 1765 /* overlap, copy backwards */ 1766 if (dst > src && dst < src + len) 1767 { 1768 src += len; 1769 dst += len; 1770 while (len-- > 0) 1771 *--dst = *--src; 1772 } 1773 else /* copy forwards */ 1774 while (len-- > 0) 1775 *dst++ = *src++; 1776 } 1777 #endif 1778 1779 #if (!defined(HAVE_STRCASECMP) && !defined(HAVE_STRICMP)) || defined(PROTO) 1780 /* 1781 * Compare two strings, ignoring case, using current locale. 1782 * Doesn't work for multi-byte characters. 1783 * return 0 for match, < 0 for smaller, > 0 for bigger 1784 */ 1785 int 1786 vim_stricmp(s1, s2) 1787 char *s1; 1788 char *s2; 1789 { 1790 int i; 1791 1792 for (;;) 1793 { 1794 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2); 1795 if (i != 0) 1796 return i; /* this character different */ 1797 if (*s1 == NUL) 1798 break; /* strings match until NUL */ 1799 ++s1; 1800 ++s2; 1801 } 1802 return 0; /* strings match */ 1803 } 1804 #endif 1805 1806 #if (!defined(HAVE_STRNCASECMP) && !defined(HAVE_STRNICMP)) || defined(PROTO) 1807 /* 1808 * Compare two strings, for length "len", ignoring case, using current locale. 1809 * Doesn't work for multi-byte characters. 1810 * return 0 for match, < 0 for smaller, > 0 for bigger 1811 */ 1812 int 1813 vim_strnicmp(s1, s2, len) 1814 char *s1; 1815 char *s2; 1816 size_t len; 1817 { 1818 int i; 1819 1820 while (len > 0) 1821 { 1822 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2); 1823 if (i != 0) 1824 return i; /* this character different */ 1825 if (*s1 == NUL) 1826 break; /* strings match until NUL */ 1827 ++s1; 1828 ++s2; 1829 --len; 1830 } 1831 return 0; /* strings match */ 1832 } 1833 #endif 1834 1835 /* 1836 * Version of strchr() and strrchr() that handle unsigned char strings 1837 * with characters from 128 to 255 correctly. It also doesn't return a 1838 * pointer to the NUL at the end of the string. 1839 */ 1840 char_u * 1841 vim_strchr(string, c) 1842 char_u *string; 1843 int c; 1844 { 1845 char_u *p; 1846 int b; 1847 1848 p = string; 1849 #ifdef FEAT_MBYTE 1850 if (enc_utf8 && c >= 0x80) 1851 { 1852 while (*p != NUL) 1853 { 1854 int l = (*mb_ptr2len)(p); 1855 1856 /* Avoid matching an illegal byte here. */ 1857 if (utf_ptr2char(p) == c && l > 1) 1858 return p; 1859 p += l; 1860 } 1861 return NULL; 1862 } 1863 if (enc_dbcs != 0 && c > 255) 1864 { 1865 int n2 = c & 0xff; 1866 1867 c = ((unsigned)c >> 8) & 0xff; 1868 while ((b = *p) != NUL) 1869 { 1870 if (b == c && p[1] == n2) 1871 return p; 1872 p += (*mb_ptr2len)(p); 1873 } 1874 return NULL; 1875 } 1876 if (has_mbyte) 1877 { 1878 while ((b = *p) != NUL) 1879 { 1880 if (b == c) 1881 return p; 1882 p += (*mb_ptr2len)(p); 1883 } 1884 return NULL; 1885 } 1886 #endif 1887 while ((b = *p) != NUL) 1888 { 1889 if (b == c) 1890 return p; 1891 ++p; 1892 } 1893 return NULL; 1894 } 1895 1896 /* 1897 * Version of strchr() that only works for bytes and handles unsigned char 1898 * strings with characters above 128 correctly. It also doesn't return a 1899 * pointer to the NUL at the end of the string. 1900 */ 1901 char_u * 1902 vim_strbyte(string, c) 1903 char_u *string; 1904 int c; 1905 { 1906 char_u *p = string; 1907 1908 while (*p != NUL) 1909 { 1910 if (*p == c) 1911 return p; 1912 ++p; 1913 } 1914 return NULL; 1915 } 1916 1917 /* 1918 * Search for last occurrence of "c" in "string". 1919 * Return NULL if not found. 1920 * Does not handle multi-byte char for "c"! 1921 */ 1922 char_u * 1923 vim_strrchr(string, c) 1924 char_u *string; 1925 int c; 1926 { 1927 char_u *retval = NULL; 1928 char_u *p = string; 1929 1930 while (*p) 1931 { 1932 if (*p == c) 1933 retval = p; 1934 mb_ptr_adv(p); 1935 } 1936 return retval; 1937 } 1938 1939 /* 1940 * Vim's version of strpbrk(), in case it's missing. 1941 * Don't generate a prototype for this, causes problems when it's not used. 1942 */ 1943 #ifndef PROTO 1944 # ifndef HAVE_STRPBRK 1945 # ifdef vim_strpbrk 1946 # undef vim_strpbrk 1947 # endif 1948 char_u * 1949 vim_strpbrk(s, charset) 1950 char_u *s; 1951 char_u *charset; 1952 { 1953 while (*s) 1954 { 1955 if (vim_strchr(charset, *s) != NULL) 1956 return s; 1957 mb_ptr_adv(s); 1958 } 1959 return NULL; 1960 } 1961 # endif 1962 #endif 1963 1964 /* 1965 * Vim has its own isspace() function, because on some machines isspace() 1966 * can't handle characters above 128. 1967 */ 1968 int 1969 vim_isspace(x) 1970 int x; 1971 { 1972 return ((x >= 9 && x <= 13) || x == ' '); 1973 } 1974 1975 /************************************************************************ 1976 * Functions for handling growing arrays. 1977 */ 1978 1979 /* 1980 * Clear an allocated growing array. 1981 */ 1982 void 1983 ga_clear(gap) 1984 garray_T *gap; 1985 { 1986 vim_free(gap->ga_data); 1987 ga_init(gap); 1988 } 1989 1990 /* 1991 * Clear a growing array that contains a list of strings. 1992 */ 1993 void 1994 ga_clear_strings(gap) 1995 garray_T *gap; 1996 { 1997 int i; 1998 1999 for (i = 0; i < gap->ga_len; ++i) 2000 vim_free(((char_u **)(gap->ga_data))[i]); 2001 ga_clear(gap); 2002 } 2003 2004 /* 2005 * Initialize a growing array. Don't forget to set ga_itemsize and 2006 * ga_growsize! Or use ga_init2(). 2007 */ 2008 void 2009 ga_init(gap) 2010 garray_T *gap; 2011 { 2012 gap->ga_data = NULL; 2013 gap->ga_maxlen = 0; 2014 gap->ga_len = 0; 2015 } 2016 2017 void 2018 ga_init2(gap, itemsize, growsize) 2019 garray_T *gap; 2020 int itemsize; 2021 int growsize; 2022 { 2023 ga_init(gap); 2024 gap->ga_itemsize = itemsize; 2025 gap->ga_growsize = growsize; 2026 } 2027 2028 /* 2029 * Make room in growing array "gap" for at least "n" items. 2030 * Return FAIL for failure, OK otherwise. 2031 */ 2032 int 2033 ga_grow(gap, n) 2034 garray_T *gap; 2035 int n; 2036 { 2037 size_t old_len; 2038 size_t new_len; 2039 char_u *pp; 2040 2041 if (gap->ga_maxlen - gap->ga_len < n) 2042 { 2043 if (n < gap->ga_growsize) 2044 n = gap->ga_growsize; 2045 new_len = gap->ga_itemsize * (gap->ga_len + n); 2046 pp = (gap->ga_data == NULL) 2047 ? alloc((unsigned)new_len) : vim_realloc(gap->ga_data, new_len); 2048 if (pp == NULL) 2049 return FAIL; 2050 old_len = gap->ga_itemsize * gap->ga_maxlen; 2051 vim_memset(pp + old_len, 0, new_len - old_len); 2052 gap->ga_maxlen = gap->ga_len + n; 2053 gap->ga_data = pp; 2054 } 2055 return OK; 2056 } 2057 2058 /* 2059 * For a growing array that contains a list of strings: concatenate all the 2060 * strings with a separating "sep". 2061 * Returns NULL when out of memory. 2062 */ 2063 char_u * 2064 ga_concat_strings(gap, sep) 2065 garray_T *gap; 2066 char *sep; 2067 { 2068 int i; 2069 int len = 0; 2070 int sep_len = (int)STRLEN(sep); 2071 char_u *s; 2072 char_u *p; 2073 2074 for (i = 0; i < gap->ga_len; ++i) 2075 len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + sep_len; 2076 2077 s = alloc(len + 1); 2078 if (s != NULL) 2079 { 2080 *s = NUL; 2081 p = s; 2082 for (i = 0; i < gap->ga_len; ++i) 2083 { 2084 if (p != s) 2085 { 2086 STRCPY(p, sep); 2087 p += sep_len; 2088 } 2089 STRCPY(p, ((char_u **)(gap->ga_data))[i]); 2090 p += STRLEN(p); 2091 } 2092 } 2093 return s; 2094 } 2095 2096 /* 2097 * Concatenate a string to a growarray which contains characters. 2098 * Note: Does NOT copy the NUL at the end! 2099 */ 2100 void 2101 ga_concat(gap, s) 2102 garray_T *gap; 2103 char_u *s; 2104 { 2105 int len = (int)STRLEN(s); 2106 2107 if (ga_grow(gap, len) == OK) 2108 { 2109 mch_memmove((char *)gap->ga_data + gap->ga_len, s, (size_t)len); 2110 gap->ga_len += len; 2111 } 2112 } 2113 2114 /* 2115 * Append one byte to a growarray which contains bytes. 2116 */ 2117 void 2118 ga_append(gap, c) 2119 garray_T *gap; 2120 int c; 2121 { 2122 if (ga_grow(gap, 1) == OK) 2123 { 2124 *((char *)gap->ga_data + gap->ga_len) = c; 2125 ++gap->ga_len; 2126 } 2127 } 2128 2129 #if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(WIN3264) \ 2130 || defined(PROTO) 2131 /* 2132 * Append the text in "gap" below the cursor line and clear "gap". 2133 */ 2134 void 2135 append_ga_line(gap) 2136 garray_T *gap; 2137 { 2138 /* Remove trailing CR. */ 2139 if (gap->ga_len > 0 2140 && !curbuf->b_p_bin 2141 && ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR) 2142 --gap->ga_len; 2143 ga_append(gap, NUL); 2144 ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE); 2145 gap->ga_len = 0; 2146 } 2147 #endif 2148 2149 /************************************************************************ 2150 * functions that use lookup tables for various things, generally to do with 2151 * special key codes. 2152 */ 2153 2154 /* 2155 * Some useful tables. 2156 */ 2157 2158 static struct modmasktable 2159 { 2160 short mod_mask; /* Bit-mask for particular key modifier */ 2161 short mod_flag; /* Bit(s) for particular key modifier */ 2162 char_u name; /* Single letter name of modifier */ 2163 } mod_mask_table[] = 2164 { 2165 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'}, 2166 {MOD_MASK_META, MOD_MASK_META, (char_u)'T'}, 2167 {MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'}, 2168 {MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'}, 2169 {MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'}, 2170 {MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'}, 2171 {MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'}, 2172 #ifdef MACOS 2173 {MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'}, 2174 #endif 2175 /* 'A' must be the last one */ 2176 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'}, 2177 {0, 0, NUL} 2178 }; 2179 2180 /* 2181 * Shifted key terminal codes and their unshifted equivalent. 2182 * Don't add mouse codes here, they are handled separately! 2183 */ 2184 #define MOD_KEYS_ENTRY_SIZE 5 2185 2186 static char_u modifier_keys_table[] = 2187 { 2188 /* mod mask with modifier without modifier */ 2189 MOD_MASK_SHIFT, '&', '9', '@', '1', /* begin */ 2190 MOD_MASK_SHIFT, '&', '0', '@', '2', /* cancel */ 2191 MOD_MASK_SHIFT, '*', '1', '@', '4', /* command */ 2192 MOD_MASK_SHIFT, '*', '2', '@', '5', /* copy */ 2193 MOD_MASK_SHIFT, '*', '3', '@', '6', /* create */ 2194 MOD_MASK_SHIFT, '*', '4', 'k', 'D', /* delete char */ 2195 MOD_MASK_SHIFT, '*', '5', 'k', 'L', /* delete line */ 2196 MOD_MASK_SHIFT, '*', '7', '@', '7', /* end */ 2197 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7', /* end */ 2198 MOD_MASK_SHIFT, '*', '9', '@', '9', /* exit */ 2199 MOD_MASK_SHIFT, '*', '0', '@', '0', /* find */ 2200 MOD_MASK_SHIFT, '#', '1', '%', '1', /* help */ 2201 MOD_MASK_SHIFT, '#', '2', 'k', 'h', /* home */ 2202 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h', /* home */ 2203 MOD_MASK_SHIFT, '#', '3', 'k', 'I', /* insert */ 2204 MOD_MASK_SHIFT, '#', '4', 'k', 'l', /* left arrow */ 2205 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l', /* left arrow */ 2206 MOD_MASK_SHIFT, '%', 'a', '%', '3', /* message */ 2207 MOD_MASK_SHIFT, '%', 'b', '%', '4', /* move */ 2208 MOD_MASK_SHIFT, '%', 'c', '%', '5', /* next */ 2209 MOD_MASK_SHIFT, '%', 'd', '%', '7', /* options */ 2210 MOD_MASK_SHIFT, '%', 'e', '%', '8', /* previous */ 2211 MOD_MASK_SHIFT, '%', 'f', '%', '9', /* print */ 2212 MOD_MASK_SHIFT, '%', 'g', '%', '0', /* redo */ 2213 MOD_MASK_SHIFT, '%', 'h', '&', '3', /* replace */ 2214 MOD_MASK_SHIFT, '%', 'i', 'k', 'r', /* right arr. */ 2215 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r', /* right arr. */ 2216 MOD_MASK_SHIFT, '%', 'j', '&', '5', /* resume */ 2217 MOD_MASK_SHIFT, '!', '1', '&', '6', /* save */ 2218 MOD_MASK_SHIFT, '!', '2', '&', '7', /* suspend */ 2219 MOD_MASK_SHIFT, '!', '3', '&', '8', /* undo */ 2220 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u', /* up arrow */ 2221 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd', /* down arrow */ 2222 2223 /* vt100 F1 */ 2224 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1, 2225 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2, 2226 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3, 2227 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4, 2228 2229 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1', /* F1 */ 2230 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2', 2231 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3', 2232 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4', 2233 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5', 2234 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6', 2235 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7', 2236 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8', 2237 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9', 2238 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';', /* F10 */ 2239 2240 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1', 2241 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2', 2242 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3', 2243 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4', 2244 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5', 2245 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6', 2246 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7', 2247 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8', 2248 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9', 2249 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A', 2250 2251 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B', 2252 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C', 2253 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D', 2254 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E', 2255 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F', 2256 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G', 2257 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H', 2258 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I', 2259 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J', 2260 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K', 2261 2262 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L', 2263 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M', 2264 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N', 2265 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O', 2266 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P', 2267 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q', 2268 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R', 2269 2270 /* TAB pseudo code*/ 2271 MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB, 2272 2273 NUL 2274 }; 2275 2276 static struct key_name_entry 2277 { 2278 int key; /* Special key code or ascii value */ 2279 char_u *name; /* Name of key */ 2280 } key_names_table[] = 2281 { 2282 {' ', (char_u *)"Space"}, 2283 {TAB, (char_u *)"Tab"}, 2284 {K_TAB, (char_u *)"Tab"}, 2285 {NL, (char_u *)"NL"}, 2286 {NL, (char_u *)"NewLine"}, /* Alternative name */ 2287 {NL, (char_u *)"LineFeed"}, /* Alternative name */ 2288 {NL, (char_u *)"LF"}, /* Alternative name */ 2289 {CAR, (char_u *)"CR"}, 2290 {CAR, (char_u *)"Return"}, /* Alternative name */ 2291 {CAR, (char_u *)"Enter"}, /* Alternative name */ 2292 {K_BS, (char_u *)"BS"}, 2293 {K_BS, (char_u *)"BackSpace"}, /* Alternative name */ 2294 {ESC, (char_u *)"Esc"}, 2295 {CSI, (char_u *)"CSI"}, 2296 {K_CSI, (char_u *)"xCSI"}, 2297 {'|', (char_u *)"Bar"}, 2298 {'\\', (char_u *)"Bslash"}, 2299 {K_DEL, (char_u *)"Del"}, 2300 {K_DEL, (char_u *)"Delete"}, /* Alternative name */ 2301 {K_KDEL, (char_u *)"kDel"}, 2302 {K_UP, (char_u *)"Up"}, 2303 {K_DOWN, (char_u *)"Down"}, 2304 {K_LEFT, (char_u *)"Left"}, 2305 {K_RIGHT, (char_u *)"Right"}, 2306 {K_XUP, (char_u *)"xUp"}, 2307 {K_XDOWN, (char_u *)"xDown"}, 2308 {K_XLEFT, (char_u *)"xLeft"}, 2309 {K_XRIGHT, (char_u *)"xRight"}, 2310 2311 {K_F1, (char_u *)"F1"}, 2312 {K_F2, (char_u *)"F2"}, 2313 {K_F3, (char_u *)"F3"}, 2314 {K_F4, (char_u *)"F4"}, 2315 {K_F5, (char_u *)"F5"}, 2316 {K_F6, (char_u *)"F6"}, 2317 {K_F7, (char_u *)"F7"}, 2318 {K_F8, (char_u *)"F8"}, 2319 {K_F9, (char_u *)"F9"}, 2320 {K_F10, (char_u *)"F10"}, 2321 2322 {K_F11, (char_u *)"F11"}, 2323 {K_F12, (char_u *)"F12"}, 2324 {K_F13, (char_u *)"F13"}, 2325 {K_F14, (char_u *)"F14"}, 2326 {K_F15, (char_u *)"F15"}, 2327 {K_F16, (char_u *)"F16"}, 2328 {K_F17, (char_u *)"F17"}, 2329 {K_F18, (char_u *)"F18"}, 2330 {K_F19, (char_u *)"F19"}, 2331 {K_F20, (char_u *)"F20"}, 2332 2333 {K_F21, (char_u *)"F21"}, 2334 {K_F22, (char_u *)"F22"}, 2335 {K_F23, (char_u *)"F23"}, 2336 {K_F24, (char_u *)"F24"}, 2337 {K_F25, (char_u *)"F25"}, 2338 {K_F26, (char_u *)"F26"}, 2339 {K_F27, (char_u *)"F27"}, 2340 {K_F28, (char_u *)"F28"}, 2341 {K_F29, (char_u *)"F29"}, 2342 {K_F30, (char_u *)"F30"}, 2343 2344 {K_F31, (char_u *)"F31"}, 2345 {K_F32, (char_u *)"F32"}, 2346 {K_F33, (char_u *)"F33"}, 2347 {K_F34, (char_u *)"F34"}, 2348 {K_F35, (char_u *)"F35"}, 2349 {K_F36, (char_u *)"F36"}, 2350 {K_F37, (char_u *)"F37"}, 2351 2352 {K_XF1, (char_u *)"xF1"}, 2353 {K_XF2, (char_u *)"xF2"}, 2354 {K_XF3, (char_u *)"xF3"}, 2355 {K_XF4, (char_u *)"xF4"}, 2356 2357 {K_HELP, (char_u *)"Help"}, 2358 {K_UNDO, (char_u *)"Undo"}, 2359 {K_INS, (char_u *)"Insert"}, 2360 {K_INS, (char_u *)"Ins"}, /* Alternative name */ 2361 {K_KINS, (char_u *)"kInsert"}, 2362 {K_HOME, (char_u *)"Home"}, 2363 {K_KHOME, (char_u *)"kHome"}, 2364 {K_XHOME, (char_u *)"xHome"}, 2365 {K_ZHOME, (char_u *)"zHome"}, 2366 {K_END, (char_u *)"End"}, 2367 {K_KEND, (char_u *)"kEnd"}, 2368 {K_XEND, (char_u *)"xEnd"}, 2369 {K_ZEND, (char_u *)"zEnd"}, 2370 {K_PAGEUP, (char_u *)"PageUp"}, 2371 {K_PAGEDOWN, (char_u *)"PageDown"}, 2372 {K_KPAGEUP, (char_u *)"kPageUp"}, 2373 {K_KPAGEDOWN, (char_u *)"kPageDown"}, 2374 2375 {K_KPLUS, (char_u *)"kPlus"}, 2376 {K_KMINUS, (char_u *)"kMinus"}, 2377 {K_KDIVIDE, (char_u *)"kDivide"}, 2378 {K_KMULTIPLY, (char_u *)"kMultiply"}, 2379 {K_KENTER, (char_u *)"kEnter"}, 2380 {K_KPOINT, (char_u *)"kPoint"}, 2381 2382 {K_K0, (char_u *)"k0"}, 2383 {K_K1, (char_u *)"k1"}, 2384 {K_K2, (char_u *)"k2"}, 2385 {K_K3, (char_u *)"k3"}, 2386 {K_K4, (char_u *)"k4"}, 2387 {K_K5, (char_u *)"k5"}, 2388 {K_K6, (char_u *)"k6"}, 2389 {K_K7, (char_u *)"k7"}, 2390 {K_K8, (char_u *)"k8"}, 2391 {K_K9, (char_u *)"k9"}, 2392 2393 {'<', (char_u *)"lt"}, 2394 2395 {K_MOUSE, (char_u *)"Mouse"}, 2396 #ifdef FEAT_MOUSE_NET 2397 {K_NETTERM_MOUSE, (char_u *)"NetMouse"}, 2398 #endif 2399 #ifdef FEAT_MOUSE_DEC 2400 {K_DEC_MOUSE, (char_u *)"DecMouse"}, 2401 #endif 2402 #ifdef FEAT_MOUSE_JSB 2403 {K_JSBTERM_MOUSE, (char_u *)"JsbMouse"}, 2404 #endif 2405 #ifdef FEAT_MOUSE_PTERM 2406 {K_PTERM_MOUSE, (char_u *)"PtermMouse"}, 2407 #endif 2408 #ifdef FEAT_MOUSE_URXVT 2409 {K_URXVT_MOUSE, (char_u *)"UrxvtMouse"}, 2410 #endif 2411 #ifdef FEAT_MOUSE_SGR 2412 {K_SGR_MOUSE, (char_u *)"SgrMouse"}, 2413 #endif 2414 {K_LEFTMOUSE, (char_u *)"LeftMouse"}, 2415 {K_LEFTMOUSE_NM, (char_u *)"LeftMouseNM"}, 2416 {K_LEFTDRAG, (char_u *)"LeftDrag"}, 2417 {K_LEFTRELEASE, (char_u *)"LeftRelease"}, 2418 {K_LEFTRELEASE_NM, (char_u *)"LeftReleaseNM"}, 2419 {K_MIDDLEMOUSE, (char_u *)"MiddleMouse"}, 2420 {K_MIDDLEDRAG, (char_u *)"MiddleDrag"}, 2421 {K_MIDDLERELEASE, (char_u *)"MiddleRelease"}, 2422 {K_RIGHTMOUSE, (char_u *)"RightMouse"}, 2423 {K_RIGHTDRAG, (char_u *)"RightDrag"}, 2424 {K_RIGHTRELEASE, (char_u *)"RightRelease"}, 2425 {K_MOUSEDOWN, (char_u *)"ScrollWheelUp"}, 2426 {K_MOUSEUP, (char_u *)"ScrollWheelDown"}, 2427 {K_MOUSELEFT, (char_u *)"ScrollWheelRight"}, 2428 {K_MOUSERIGHT, (char_u *)"ScrollWheelLeft"}, 2429 {K_MOUSEDOWN, (char_u *)"MouseDown"}, /* OBSOLETE: Use */ 2430 {K_MOUSEUP, (char_u *)"MouseUp"}, /* ScrollWheelXXX instead */ 2431 {K_X1MOUSE, (char_u *)"X1Mouse"}, 2432 {K_X1DRAG, (char_u *)"X1Drag"}, 2433 {K_X1RELEASE, (char_u *)"X1Release"}, 2434 {K_X2MOUSE, (char_u *)"X2Mouse"}, 2435 {K_X2DRAG, (char_u *)"X2Drag"}, 2436 {K_X2RELEASE, (char_u *)"X2Release"}, 2437 {K_DROP, (char_u *)"Drop"}, 2438 {K_ZERO, (char_u *)"Nul"}, 2439 #ifdef FEAT_EVAL 2440 {K_SNR, (char_u *)"SNR"}, 2441 #endif 2442 {K_PLUG, (char_u *)"Plug"}, 2443 {K_CURSORHOLD, (char_u *)"CursorHold"}, 2444 {0, NULL} 2445 }; 2446 2447 #define KEY_NAMES_TABLE_LEN (sizeof(key_names_table) / sizeof(struct key_name_entry)) 2448 2449 #ifdef FEAT_MOUSE 2450 static struct mousetable 2451 { 2452 int pseudo_code; /* Code for pseudo mouse event */ 2453 int button; /* Which mouse button is it? */ 2454 int is_click; /* Is it a mouse button click event? */ 2455 int is_drag; /* Is it a mouse drag event? */ 2456 } mouse_table[] = 2457 { 2458 {(int)KE_LEFTMOUSE, MOUSE_LEFT, TRUE, FALSE}, 2459 #ifdef FEAT_GUI 2460 {(int)KE_LEFTMOUSE_NM, MOUSE_LEFT, TRUE, FALSE}, 2461 #endif 2462 {(int)KE_LEFTDRAG, MOUSE_LEFT, FALSE, TRUE}, 2463 {(int)KE_LEFTRELEASE, MOUSE_LEFT, FALSE, FALSE}, 2464 #ifdef FEAT_GUI 2465 {(int)KE_LEFTRELEASE_NM, MOUSE_LEFT, FALSE, FALSE}, 2466 #endif 2467 {(int)KE_MIDDLEMOUSE, MOUSE_MIDDLE, TRUE, FALSE}, 2468 {(int)KE_MIDDLEDRAG, MOUSE_MIDDLE, FALSE, TRUE}, 2469 {(int)KE_MIDDLERELEASE, MOUSE_MIDDLE, FALSE, FALSE}, 2470 {(int)KE_RIGHTMOUSE, MOUSE_RIGHT, TRUE, FALSE}, 2471 {(int)KE_RIGHTDRAG, MOUSE_RIGHT, FALSE, TRUE}, 2472 {(int)KE_RIGHTRELEASE, MOUSE_RIGHT, FALSE, FALSE}, 2473 {(int)KE_X1MOUSE, MOUSE_X1, TRUE, FALSE}, 2474 {(int)KE_X1DRAG, MOUSE_X1, FALSE, TRUE}, 2475 {(int)KE_X1RELEASE, MOUSE_X1, FALSE, FALSE}, 2476 {(int)KE_X2MOUSE, MOUSE_X2, TRUE, FALSE}, 2477 {(int)KE_X2DRAG, MOUSE_X2, FALSE, TRUE}, 2478 {(int)KE_X2RELEASE, MOUSE_X2, FALSE, FALSE}, 2479 /* DRAG without CLICK */ 2480 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, TRUE}, 2481 /* RELEASE without CLICK */ 2482 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, FALSE}, 2483 {0, 0, 0, 0}, 2484 }; 2485 #endif /* FEAT_MOUSE */ 2486 2487 /* 2488 * Return the modifier mask bit (MOD_MASK_*) which corresponds to the given 2489 * modifier name ('S' for Shift, 'C' for Ctrl etc). 2490 */ 2491 int 2492 name_to_mod_mask(c) 2493 int c; 2494 { 2495 int i; 2496 2497 c = TOUPPER_ASC(c); 2498 for (i = 0; mod_mask_table[i].mod_mask != 0; i++) 2499 if (c == mod_mask_table[i].name) 2500 return mod_mask_table[i].mod_flag; 2501 return 0; 2502 } 2503 2504 /* 2505 * Check if if there is a special key code for "key" that includes the 2506 * modifiers specified. 2507 */ 2508 int 2509 simplify_key(key, modifiers) 2510 int key; 2511 int *modifiers; 2512 { 2513 int i; 2514 int key0; 2515 int key1; 2516 2517 if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT)) 2518 { 2519 /* TAB is a special case */ 2520 if (key == TAB && (*modifiers & MOD_MASK_SHIFT)) 2521 { 2522 *modifiers &= ~MOD_MASK_SHIFT; 2523 return K_S_TAB; 2524 } 2525 key0 = KEY2TERMCAP0(key); 2526 key1 = KEY2TERMCAP1(key); 2527 for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE) 2528 if (key0 == modifier_keys_table[i + 3] 2529 && key1 == modifier_keys_table[i + 4] 2530 && (*modifiers & modifier_keys_table[i])) 2531 { 2532 *modifiers &= ~modifier_keys_table[i]; 2533 return TERMCAP2KEY(modifier_keys_table[i + 1], 2534 modifier_keys_table[i + 2]); 2535 } 2536 } 2537 return key; 2538 } 2539 2540 /* 2541 * Change <xHome> to <Home>, <xUp> to <Up>, etc. 2542 */ 2543 int 2544 handle_x_keys(key) 2545 int key; 2546 { 2547 switch (key) 2548 { 2549 case K_XUP: return K_UP; 2550 case K_XDOWN: return K_DOWN; 2551 case K_XLEFT: return K_LEFT; 2552 case K_XRIGHT: return K_RIGHT; 2553 case K_XHOME: return K_HOME; 2554 case K_ZHOME: return K_HOME; 2555 case K_XEND: return K_END; 2556 case K_ZEND: return K_END; 2557 case K_XF1: return K_F1; 2558 case K_XF2: return K_F2; 2559 case K_XF3: return K_F3; 2560 case K_XF4: return K_F4; 2561 case K_S_XF1: return K_S_F1; 2562 case K_S_XF2: return K_S_F2; 2563 case K_S_XF3: return K_S_F3; 2564 case K_S_XF4: return K_S_F4; 2565 } 2566 return key; 2567 } 2568 2569 /* 2570 * Return a string which contains the name of the given key when the given 2571 * modifiers are down. 2572 */ 2573 char_u * 2574 get_special_key_name(c, modifiers) 2575 int c; 2576 int modifiers; 2577 { 2578 static char_u string[MAX_KEY_NAME_LEN + 1]; 2579 2580 int i, idx; 2581 int table_idx; 2582 char_u *s; 2583 2584 string[0] = '<'; 2585 idx = 1; 2586 2587 /* Key that stands for a normal character. */ 2588 if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY) 2589 c = KEY2TERMCAP1(c); 2590 2591 /* 2592 * Translate shifted special keys into unshifted keys and set modifier. 2593 * Same for CTRL and ALT modifiers. 2594 */ 2595 if (IS_SPECIAL(c)) 2596 { 2597 for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE) 2598 if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1] 2599 && (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2]) 2600 { 2601 modifiers |= modifier_keys_table[i]; 2602 c = TERMCAP2KEY(modifier_keys_table[i + 3], 2603 modifier_keys_table[i + 4]); 2604 break; 2605 } 2606 } 2607 2608 /* try to find the key in the special key table */ 2609 table_idx = find_special_key_in_table(c); 2610 2611 /* 2612 * When not a known special key, and not a printable character, try to 2613 * extract modifiers. 2614 */ 2615 if (c > 0 2616 #ifdef FEAT_MBYTE 2617 && (*mb_char2len)(c) == 1 2618 #endif 2619 ) 2620 { 2621 if (table_idx < 0 2622 && (!vim_isprintc(c) || (c & 0x7f) == ' ') 2623 && (c & 0x80)) 2624 { 2625 c &= 0x7f; 2626 modifiers |= MOD_MASK_ALT; 2627 /* try again, to find the un-alted key in the special key table */ 2628 table_idx = find_special_key_in_table(c); 2629 } 2630 if (table_idx < 0 && !vim_isprintc(c) && c < ' ') 2631 { 2632 #ifdef EBCDIC 2633 c = CtrlChar(c); 2634 #else 2635 c += '@'; 2636 #endif 2637 modifiers |= MOD_MASK_CTRL; 2638 } 2639 } 2640 2641 /* translate the modifier into a string */ 2642 for (i = 0; mod_mask_table[i].name != 'A'; i++) 2643 if ((modifiers & mod_mask_table[i].mod_mask) 2644 == mod_mask_table[i].mod_flag) 2645 { 2646 string[idx++] = mod_mask_table[i].name; 2647 string[idx++] = (char_u)'-'; 2648 } 2649 2650 if (table_idx < 0) /* unknown special key, may output t_xx */ 2651 { 2652 if (IS_SPECIAL(c)) 2653 { 2654 string[idx++] = 't'; 2655 string[idx++] = '_'; 2656 string[idx++] = KEY2TERMCAP0(c); 2657 string[idx++] = KEY2TERMCAP1(c); 2658 } 2659 /* Not a special key, only modifiers, output directly */ 2660 else 2661 { 2662 #ifdef FEAT_MBYTE 2663 if (has_mbyte && (*mb_char2len)(c) > 1) 2664 idx += (*mb_char2bytes)(c, string + idx); 2665 else 2666 #endif 2667 if (vim_isprintc(c)) 2668 string[idx++] = c; 2669 else 2670 { 2671 s = transchar(c); 2672 while (*s) 2673 string[idx++] = *s++; 2674 } 2675 } 2676 } 2677 else /* use name of special key */ 2678 { 2679 STRCPY(string + idx, key_names_table[table_idx].name); 2680 idx = (int)STRLEN(string); 2681 } 2682 string[idx++] = '>'; 2683 string[idx] = NUL; 2684 return string; 2685 } 2686 2687 /* 2688 * Try translating a <> name at (*srcp)[] to dst[]. 2689 * Return the number of characters added to dst[], zero for no match. 2690 * If there is a match, srcp is advanced to after the <> name. 2691 * dst[] must be big enough to hold the result (up to six characters)! 2692 */ 2693 int 2694 trans_special(srcp, dst, keycode) 2695 char_u **srcp; 2696 char_u *dst; 2697 int keycode; /* prefer key code, e.g. K_DEL instead of DEL */ 2698 { 2699 int modifiers = 0; 2700 int key; 2701 int dlen = 0; 2702 2703 key = find_special_key(srcp, &modifiers, keycode, FALSE); 2704 if (key == 0) 2705 return 0; 2706 2707 /* Put the appropriate modifier in a string */ 2708 if (modifiers != 0) 2709 { 2710 dst[dlen++] = K_SPECIAL; 2711 dst[dlen++] = KS_MODIFIER; 2712 dst[dlen++] = modifiers; 2713 } 2714 2715 if (IS_SPECIAL(key)) 2716 { 2717 dst[dlen++] = K_SPECIAL; 2718 dst[dlen++] = KEY2TERMCAP0(key); 2719 dst[dlen++] = KEY2TERMCAP1(key); 2720 } 2721 #ifdef FEAT_MBYTE 2722 else if (has_mbyte && !keycode) 2723 dlen += (*mb_char2bytes)(key, dst + dlen); 2724 #endif 2725 else if (keycode) 2726 dlen = (int)(add_char2buf(key, dst + dlen) - dst); 2727 else 2728 dst[dlen++] = key; 2729 2730 return dlen; 2731 } 2732 2733 /* 2734 * Try translating a <> name at (*srcp)[], return the key and modifiers. 2735 * srcp is advanced to after the <> name. 2736 * returns 0 if there is no match. 2737 */ 2738 int 2739 find_special_key(srcp, modp, keycode, keep_x_key) 2740 char_u **srcp; 2741 int *modp; 2742 int keycode; /* prefer key code, e.g. K_DEL instead of DEL */ 2743 int keep_x_key; /* don't translate xHome to Home key */ 2744 { 2745 char_u *last_dash; 2746 char_u *end_of_name; 2747 char_u *src; 2748 char_u *bp; 2749 int modifiers; 2750 int bit; 2751 int key; 2752 unsigned long n; 2753 int l; 2754 2755 src = *srcp; 2756 if (src[0] != '<') 2757 return 0; 2758 2759 /* Find end of modifier list */ 2760 last_dash = src; 2761 for (bp = src + 1; *bp == '-' || vim_isIDc(*bp); bp++) 2762 { 2763 if (*bp == '-') 2764 { 2765 last_dash = bp; 2766 if (bp[1] != NUL) 2767 { 2768 #ifdef FEAT_MBYTE 2769 if (has_mbyte) 2770 l = mb_ptr2len(bp + 1); 2771 else 2772 #endif 2773 l = 1; 2774 if (bp[l + 1] == '>') 2775 bp += l; /* anything accepted, like <C-?> */ 2776 } 2777 } 2778 if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3]) 2779 bp += 3; /* skip t_xx, xx may be '-' or '>' */ 2780 else if (STRNICMP(bp, "char-", 5) == 0) 2781 { 2782 vim_str2nr(bp + 5, NULL, &l, TRUE, TRUE, NULL, NULL, 0); 2783 bp += l + 5; 2784 break; 2785 } 2786 } 2787 2788 if (*bp == '>') /* found matching '>' */ 2789 { 2790 end_of_name = bp + 1; 2791 2792 /* Which modifiers are given? */ 2793 modifiers = 0x0; 2794 for (bp = src + 1; bp < last_dash; bp++) 2795 { 2796 if (*bp != '-') 2797 { 2798 bit = name_to_mod_mask(*bp); 2799 if (bit == 0x0) 2800 break; /* Illegal modifier name */ 2801 modifiers |= bit; 2802 } 2803 } 2804 2805 /* 2806 * Legal modifier name. 2807 */ 2808 if (bp >= last_dash) 2809 { 2810 if (STRNICMP(last_dash + 1, "char-", 5) == 0 2811 && VIM_ISDIGIT(last_dash[6])) 2812 { 2813 /* <Char-123> or <Char-033> or <Char-0x33> */ 2814 vim_str2nr(last_dash + 6, NULL, NULL, TRUE, TRUE, NULL, &n, 0); 2815 key = (int)n; 2816 } 2817 else 2818 { 2819 /* 2820 * Modifier with single letter, or special key name. 2821 */ 2822 #ifdef FEAT_MBYTE 2823 if (has_mbyte) 2824 l = mb_ptr2len(last_dash + 1); 2825 else 2826 #endif 2827 l = 1; 2828 if (modifiers != 0 && last_dash[l + 1] == '>') 2829 key = PTR2CHAR(last_dash + 1); 2830 else 2831 { 2832 key = get_special_key_code(last_dash + 1); 2833 if (!keep_x_key) 2834 key = handle_x_keys(key); 2835 } 2836 } 2837 2838 /* 2839 * get_special_key_code() may return NUL for invalid 2840 * special key name. 2841 */ 2842 if (key != NUL) 2843 { 2844 /* 2845 * Only use a modifier when there is no special key code that 2846 * includes the modifier. 2847 */ 2848 key = simplify_key(key, &modifiers); 2849 2850 if (!keycode) 2851 { 2852 /* don't want keycode, use single byte code */ 2853 if (key == K_BS) 2854 key = BS; 2855 else if (key == K_DEL || key == K_KDEL) 2856 key = DEL; 2857 } 2858 2859 /* 2860 * Normal Key with modifier: Try to make a single byte code. 2861 */ 2862 if (!IS_SPECIAL(key)) 2863 key = extract_modifiers(key, &modifiers); 2864 2865 *modp = modifiers; 2866 *srcp = end_of_name; 2867 return key; 2868 } 2869 } 2870 } 2871 return 0; 2872 } 2873 2874 /* 2875 * Try to include modifiers in the key. 2876 * Changes "Shift-a" to 'A', "Alt-A" to 0xc0, etc. 2877 */ 2878 int 2879 extract_modifiers(key, modp) 2880 int key; 2881 int *modp; 2882 { 2883 int modifiers = *modp; 2884 2885 #ifdef MACOS 2886 /* Command-key really special, no fancynest */ 2887 if (!(modifiers & MOD_MASK_CMD)) 2888 #endif 2889 if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key)) 2890 { 2891 key = TOUPPER_ASC(key); 2892 modifiers &= ~MOD_MASK_SHIFT; 2893 } 2894 if ((modifiers & MOD_MASK_CTRL) 2895 #ifdef EBCDIC 2896 /* * TODO: EBCDIC Better use: 2897 * && (Ctrl_chr(key) || key == '?') 2898 * ??? */ 2899 && strchr("?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_", key) 2900 != NULL 2901 #else 2902 && ((key >= '?' && key <= '_') || ASCII_ISALPHA(key)) 2903 #endif 2904 ) 2905 { 2906 key = Ctrl_chr(key); 2907 modifiers &= ~MOD_MASK_CTRL; 2908 /* <C-@> is <Nul> */ 2909 if (key == 0) 2910 key = K_ZERO; 2911 } 2912 #ifdef MACOS 2913 /* Command-key really special, no fancynest */ 2914 if (!(modifiers & MOD_MASK_CMD)) 2915 #endif 2916 if ((modifiers & MOD_MASK_ALT) && key < 0x80 2917 #ifdef FEAT_MBYTE 2918 && !enc_dbcs /* avoid creating a lead byte */ 2919 #endif 2920 ) 2921 { 2922 key |= 0x80; 2923 modifiers &= ~MOD_MASK_ALT; /* remove the META modifier */ 2924 } 2925 2926 *modp = modifiers; 2927 return key; 2928 } 2929 2930 /* 2931 * Try to find key "c" in the special key table. 2932 * Return the index when found, -1 when not found. 2933 */ 2934 int 2935 find_special_key_in_table(c) 2936 int c; 2937 { 2938 int i; 2939 2940 for (i = 0; key_names_table[i].name != NULL; i++) 2941 if (c == key_names_table[i].key) 2942 break; 2943 if (key_names_table[i].name == NULL) 2944 i = -1; 2945 return i; 2946 } 2947 2948 /* 2949 * Find the special key with the given name (the given string does not have to 2950 * end with NUL, the name is assumed to end before the first non-idchar). 2951 * If the name starts with "t_" the next two characters are interpreted as a 2952 * termcap name. 2953 * Return the key code, or 0 if not found. 2954 */ 2955 int 2956 get_special_key_code(name) 2957 char_u *name; 2958 { 2959 char_u *table_name; 2960 char_u string[3]; 2961 int i, j; 2962 2963 /* 2964 * If it's <t_xx> we get the code for xx from the termcap 2965 */ 2966 if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL) 2967 { 2968 string[0] = name[2]; 2969 string[1] = name[3]; 2970 string[2] = NUL; 2971 if (add_termcap_entry(string, FALSE) == OK) 2972 return TERMCAP2KEY(name[2], name[3]); 2973 } 2974 else 2975 for (i = 0; key_names_table[i].name != NULL; i++) 2976 { 2977 table_name = key_names_table[i].name; 2978 for (j = 0; vim_isIDc(name[j]) && table_name[j] != NUL; j++) 2979 if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j])) 2980 break; 2981 if (!vim_isIDc(name[j]) && table_name[j] == NUL) 2982 return key_names_table[i].key; 2983 } 2984 return 0; 2985 } 2986 2987 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) 2988 char_u * 2989 get_key_name(i) 2990 int i; 2991 { 2992 if (i >= (int)KEY_NAMES_TABLE_LEN) 2993 return NULL; 2994 return key_names_table[i].name; 2995 } 2996 #endif 2997 2998 #if defined(FEAT_MOUSE) || defined(PROTO) 2999 /* 3000 * Look up the given mouse code to return the relevant information in the other 3001 * arguments. Return which button is down or was released. 3002 */ 3003 int 3004 get_mouse_button(code, is_click, is_drag) 3005 int code; 3006 int *is_click; 3007 int *is_drag; 3008 { 3009 int i; 3010 3011 for (i = 0; mouse_table[i].pseudo_code; i++) 3012 if (code == mouse_table[i].pseudo_code) 3013 { 3014 *is_click = mouse_table[i].is_click; 3015 *is_drag = mouse_table[i].is_drag; 3016 return mouse_table[i].button; 3017 } 3018 return 0; /* Shouldn't get here */ 3019 } 3020 3021 /* 3022 * Return the appropriate pseudo mouse event token (KE_LEFTMOUSE etc) based on 3023 * the given information about which mouse button is down, and whether the 3024 * mouse was clicked, dragged or released. 3025 */ 3026 int 3027 get_pseudo_mouse_code(button, is_click, is_drag) 3028 int button; /* eg MOUSE_LEFT */ 3029 int is_click; 3030 int is_drag; 3031 { 3032 int i; 3033 3034 for (i = 0; mouse_table[i].pseudo_code; i++) 3035 if (button == mouse_table[i].button 3036 && is_click == mouse_table[i].is_click 3037 && is_drag == mouse_table[i].is_drag) 3038 { 3039 #ifdef FEAT_GUI 3040 /* Trick: a non mappable left click and release has mouse_col -1 3041 * or added MOUSE_COLOFF. Used for 'mousefocus' in 3042 * gui_mouse_moved() */ 3043 if (mouse_col < 0 || mouse_col > MOUSE_COLOFF) 3044 { 3045 if (mouse_col < 0) 3046 mouse_col = 0; 3047 else 3048 mouse_col -= MOUSE_COLOFF; 3049 if (mouse_table[i].pseudo_code == (int)KE_LEFTMOUSE) 3050 return (int)KE_LEFTMOUSE_NM; 3051 if (mouse_table[i].pseudo_code == (int)KE_LEFTRELEASE) 3052 return (int)KE_LEFTRELEASE_NM; 3053 } 3054 #endif 3055 return mouse_table[i].pseudo_code; 3056 } 3057 return (int)KE_IGNORE; /* not recognized, ignore it */ 3058 } 3059 #endif /* FEAT_MOUSE */ 3060 3061 /* 3062 * Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC. 3063 */ 3064 int 3065 get_fileformat(buf) 3066 buf_T *buf; 3067 { 3068 int c = *buf->b_p_ff; 3069 3070 if (buf->b_p_bin || c == 'u') 3071 return EOL_UNIX; 3072 if (c == 'm') 3073 return EOL_MAC; 3074 return EOL_DOS; 3075 } 3076 3077 /* 3078 * Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val" 3079 * argument. 3080 */ 3081 int 3082 get_fileformat_force(buf, eap) 3083 buf_T *buf; 3084 exarg_T *eap; /* can be NULL! */ 3085 { 3086 int c; 3087 3088 if (eap != NULL && eap->force_ff != 0) 3089 c = eap->cmd[eap->force_ff]; 3090 else 3091 { 3092 if ((eap != NULL && eap->force_bin != 0) 3093 ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin) 3094 return EOL_UNIX; 3095 c = *buf->b_p_ff; 3096 } 3097 if (c == 'u') 3098 return EOL_UNIX; 3099 if (c == 'm') 3100 return EOL_MAC; 3101 return EOL_DOS; 3102 } 3103 3104 /* 3105 * Set the current end-of-line type to EOL_DOS, EOL_UNIX or EOL_MAC. 3106 * Sets both 'textmode' and 'fileformat'. 3107 * Note: Does _not_ set global value of 'textmode'! 3108 */ 3109 void 3110 set_fileformat(t, opt_flags) 3111 int t; 3112 int opt_flags; /* OPT_LOCAL and/or OPT_GLOBAL */ 3113 { 3114 char *p = NULL; 3115 3116 switch (t) 3117 { 3118 case EOL_DOS: 3119 p = FF_DOS; 3120 curbuf->b_p_tx = TRUE; 3121 break; 3122 case EOL_UNIX: 3123 p = FF_UNIX; 3124 curbuf->b_p_tx = FALSE; 3125 break; 3126 case EOL_MAC: 3127 p = FF_MAC; 3128 curbuf->b_p_tx = FALSE; 3129 break; 3130 } 3131 if (p != NULL) 3132 set_string_option_direct((char_u *)"ff", -1, (char_u *)p, 3133 OPT_FREE | opt_flags, 0); 3134 3135 #ifdef FEAT_WINDOWS 3136 /* This may cause the buffer to become (un)modified. */ 3137 check_status(curbuf); 3138 redraw_tabline = TRUE; 3139 #endif 3140 #ifdef FEAT_TITLE 3141 need_maketitle = TRUE; /* set window title later */ 3142 #endif 3143 } 3144 3145 /* 3146 * Return the default fileformat from 'fileformats'. 3147 */ 3148 int 3149 default_fileformat() 3150 { 3151 switch (*p_ffs) 3152 { 3153 case 'm': return EOL_MAC; 3154 case 'd': return EOL_DOS; 3155 } 3156 return EOL_UNIX; 3157 } 3158 3159 /* 3160 * Call shell. Calls mch_call_shell, with 'shellxquote' added. 3161 */ 3162 int 3163 call_shell(cmd, opt) 3164 char_u *cmd; 3165 int opt; 3166 { 3167 char_u *ncmd; 3168 int retval; 3169 #ifdef FEAT_PROFILE 3170 proftime_T wait_time; 3171 #endif 3172 3173 if (p_verbose > 3) 3174 { 3175 verbose_enter(); 3176 smsg((char_u *)_("Calling shell to execute: \"%s\""), 3177 cmd == NULL ? p_sh : cmd); 3178 out_char('\n'); 3179 cursor_on(); 3180 verbose_leave(); 3181 } 3182 3183 #ifdef FEAT_PROFILE 3184 if (do_profiling == PROF_YES) 3185 prof_child_enter(&wait_time); 3186 #endif 3187 3188 if (*p_sh == NUL) 3189 { 3190 EMSG(_(e_shellempty)); 3191 retval = -1; 3192 } 3193 else 3194 { 3195 #ifdef FEAT_GUI_MSWIN 3196 /* Don't hide the pointer while executing a shell command. */ 3197 gui_mch_mousehide(FALSE); 3198 #endif 3199 #ifdef FEAT_GUI 3200 ++hold_gui_events; 3201 #endif 3202 /* The external command may update a tags file, clear cached tags. */ 3203 tag_freematch(); 3204 3205 if (cmd == NULL || *p_sxq == NUL) 3206 retval = mch_call_shell(cmd, opt); 3207 else 3208 { 3209 char_u *ecmd = cmd; 3210 3211 if (*p_sxe != NUL && STRCMP(p_sxq, "(") == 0) 3212 { 3213 ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE); 3214 if (ecmd == NULL) 3215 ecmd = cmd; 3216 } 3217 ncmd = alloc((unsigned)(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1)); 3218 if (ncmd != NULL) 3219 { 3220 STRCPY(ncmd, p_sxq); 3221 STRCAT(ncmd, ecmd); 3222 /* When 'shellxquote' is ( append ). 3223 * When 'shellxquote' is "( append )". */ 3224 STRCAT(ncmd, STRCMP(p_sxq, "(") == 0 ? (char_u *)")" 3225 : STRCMP(p_sxq, "\"(") == 0 ? (char_u *)")\"" 3226 : p_sxq); 3227 retval = mch_call_shell(ncmd, opt); 3228 vim_free(ncmd); 3229 } 3230 else 3231 retval = -1; 3232 if (ecmd != cmd) 3233 vim_free(ecmd); 3234 } 3235 #ifdef FEAT_GUI 3236 --hold_gui_events; 3237 #endif 3238 /* 3239 * Check the window size, in case it changed while executing the 3240 * external command. 3241 */ 3242 shell_resized_check(); 3243 } 3244 3245 #ifdef FEAT_EVAL 3246 set_vim_var_nr(VV_SHELL_ERROR, (long)retval); 3247 # ifdef FEAT_PROFILE 3248 if (do_profiling == PROF_YES) 3249 prof_child_exit(&wait_time); 3250 # endif 3251 #endif 3252 3253 return retval; 3254 } 3255 3256 /* 3257 * VISUAL, SELECTMODE and OP_PENDING State are never set, they are equal to 3258 * NORMAL State with a condition. This function returns the real State. 3259 */ 3260 int 3261 get_real_state() 3262 { 3263 if (State & NORMAL) 3264 { 3265 if (VIsual_active) 3266 { 3267 if (VIsual_select) 3268 return SELECTMODE; 3269 return VISUAL; 3270 } 3271 else if (finish_op) 3272 return OP_PENDING; 3273 } 3274 return State; 3275 } 3276 3277 #if defined(FEAT_MBYTE) || defined(PROTO) 3278 /* 3279 * Return TRUE if "p" points to just after a path separator. 3280 * Takes care of multi-byte characters. 3281 * "b" must point to the start of the file name 3282 */ 3283 int 3284 after_pathsep(b, p) 3285 char_u *b; 3286 char_u *p; 3287 { 3288 return p > b && vim_ispathsep(p[-1]) 3289 && (!has_mbyte || (*mb_head_off)(b, p - 1) == 0); 3290 } 3291 #endif 3292 3293 /* 3294 * Return TRUE if file names "f1" and "f2" are in the same directory. 3295 * "f1" may be a short name, "f2" must be a full path. 3296 */ 3297 int 3298 same_directory(f1, f2) 3299 char_u *f1; 3300 char_u *f2; 3301 { 3302 char_u ffname[MAXPATHL]; 3303 char_u *t1; 3304 char_u *t2; 3305 3306 /* safety check */ 3307 if (f1 == NULL || f2 == NULL) 3308 return FALSE; 3309 3310 (void)vim_FullName(f1, ffname, MAXPATHL, FALSE); 3311 t1 = gettail_sep(ffname); 3312 t2 = gettail_sep(f2); 3313 return (t1 - ffname == t2 - f2 3314 && pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0); 3315 } 3316 3317 #if defined(FEAT_SESSION) || defined(MSWIN) || defined(FEAT_GUI_MAC) \ 3318 || ((defined(FEAT_GUI_GTK)) \ 3319 && ( defined(FEAT_WINDOWS) || defined(FEAT_DND)) ) \ 3320 || defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \ 3321 || defined(PROTO) 3322 /* 3323 * Change to a file's directory. 3324 * Caller must call shorten_fnames()! 3325 * Return OK or FAIL. 3326 */ 3327 int 3328 vim_chdirfile(fname) 3329 char_u *fname; 3330 { 3331 char_u dir[MAXPATHL]; 3332 3333 vim_strncpy(dir, fname, MAXPATHL - 1); 3334 *gettail_sep(dir) = NUL; 3335 return mch_chdir((char *)dir) == 0 ? OK : FAIL; 3336 } 3337 #endif 3338 3339 #if defined(STAT_IGNORES_SLASH) || defined(PROTO) 3340 /* 3341 * Check if "name" ends in a slash and is not a directory. 3342 * Used for systems where stat() ignores a trailing slash on a file name. 3343 * The Vim code assumes a trailing slash is only ignored for a directory. 3344 */ 3345 int 3346 illegal_slash(name) 3347 char *name; 3348 { 3349 if (name[0] == NUL) 3350 return FALSE; /* no file name is not illegal */ 3351 if (name[strlen(name) - 1] != '/') 3352 return FALSE; /* no trailing slash */ 3353 if (mch_isdir((char_u *)name)) 3354 return FALSE; /* trailing slash for a directory */ 3355 return TRUE; 3356 } 3357 #endif 3358 3359 #if defined(CURSOR_SHAPE) || defined(PROTO) 3360 3361 /* 3362 * Handling of cursor and mouse pointer shapes in various modes. 3363 */ 3364 3365 cursorentry_T shape_table[SHAPE_IDX_COUNT] = 3366 { 3367 /* The values will be filled in from the 'guicursor' and 'mouseshape' 3368 * defaults when Vim starts. 3369 * Adjust the SHAPE_IDX_ defines when making changes! */ 3370 {0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE}, 3371 {0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE}, 3372 {0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE}, 3373 {0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE}, 3374 {0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE}, 3375 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE}, 3376 {0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE}, 3377 {0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE}, 3378 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE}, 3379 {0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE}, 3380 {0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE}, 3381 {0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE}, 3382 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE}, 3383 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE}, 3384 {0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE}, 3385 {0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE}, 3386 {0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR}, 3387 }; 3388 3389 #ifdef FEAT_MOUSESHAPE 3390 /* 3391 * Table with names for mouse shapes. Keep in sync with all the tables for 3392 * mch_set_mouse_shape()!. 3393 */ 3394 static char * mshape_names[] = 3395 { 3396 "arrow", /* default, must be the first one */ 3397 "blank", /* hidden */ 3398 "beam", 3399 "updown", 3400 "udsizing", 3401 "leftright", 3402 "lrsizing", 3403 "busy", 3404 "no", 3405 "crosshair", 3406 "hand1", 3407 "hand2", 3408 "pencil", 3409 "question", 3410 "rightup-arrow", 3411 "up-arrow", 3412 NULL 3413 }; 3414 #endif 3415 3416 /* 3417 * Parse the 'guicursor' option ("what" is SHAPE_CURSOR) or 'mouseshape' 3418 * ("what" is SHAPE_MOUSE). 3419 * Returns error message for an illegal option, NULL otherwise. 3420 */ 3421 char_u * 3422 parse_shape_opt(what) 3423 int what; 3424 { 3425 char_u *modep; 3426 char_u *colonp; 3427 char_u *commap; 3428 char_u *slashp; 3429 char_u *p, *endp; 3430 int idx = 0; /* init for GCC */ 3431 int all_idx; 3432 int len; 3433 int i; 3434 long n; 3435 int found_ve = FALSE; /* found "ve" flag */ 3436 int round; 3437 3438 /* 3439 * First round: check for errors; second round: do it for real. 3440 */ 3441 for (round = 1; round <= 2; ++round) 3442 { 3443 /* 3444 * Repeat for all comma separated parts. 3445 */ 3446 #ifdef FEAT_MOUSESHAPE 3447 if (what == SHAPE_MOUSE) 3448 modep = p_mouseshape; 3449 else 3450 #endif 3451 modep = p_guicursor; 3452 while (*modep != NUL) 3453 { 3454 colonp = vim_strchr(modep, ':'); 3455 if (colonp == NULL) 3456 return (char_u *)N_("E545: Missing colon"); 3457 if (colonp == modep) 3458 return (char_u *)N_("E546: Illegal mode"); 3459 commap = vim_strchr(modep, ','); 3460 3461 /* 3462 * Repeat for all mode's before the colon. 3463 * For the 'a' mode, we loop to handle all the modes. 3464 */ 3465 all_idx = -1; 3466 while (modep < colonp || all_idx >= 0) 3467 { 3468 if (all_idx < 0) 3469 { 3470 /* Find the mode. */ 3471 if (modep[1] == '-' || modep[1] == ':') 3472 len = 1; 3473 else 3474 len = 2; 3475 if (len == 1 && TOLOWER_ASC(modep[0]) == 'a') 3476 all_idx = SHAPE_IDX_COUNT - 1; 3477 else 3478 { 3479 for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx) 3480 if (STRNICMP(modep, shape_table[idx].name, len) 3481 == 0) 3482 break; 3483 if (idx == SHAPE_IDX_COUNT 3484 || (shape_table[idx].used_for & what) == 0) 3485 return (char_u *)N_("E546: Illegal mode"); 3486 if (len == 2 && modep[0] == 'v' && modep[1] == 'e') 3487 found_ve = TRUE; 3488 } 3489 modep += len + 1; 3490 } 3491 3492 if (all_idx >= 0) 3493 idx = all_idx--; 3494 else if (round == 2) 3495 { 3496 #ifdef FEAT_MOUSESHAPE 3497 if (what == SHAPE_MOUSE) 3498 { 3499 /* Set the default, for the missing parts */ 3500 shape_table[idx].mshape = 0; 3501 } 3502 else 3503 #endif 3504 { 3505 /* Set the defaults, for the missing parts */ 3506 shape_table[idx].shape = SHAPE_BLOCK; 3507 shape_table[idx].blinkwait = 700L; 3508 shape_table[idx].blinkon = 400L; 3509 shape_table[idx].blinkoff = 250L; 3510 } 3511 } 3512 3513 /* Parse the part after the colon */ 3514 for (p = colonp + 1; *p && *p != ','; ) 3515 { 3516 #ifdef FEAT_MOUSESHAPE 3517 if (what == SHAPE_MOUSE) 3518 { 3519 for (i = 0; ; ++i) 3520 { 3521 if (mshape_names[i] == NULL) 3522 { 3523 if (!VIM_ISDIGIT(*p)) 3524 return (char_u *)N_("E547: Illegal mouseshape"); 3525 if (round == 2) 3526 shape_table[idx].mshape = 3527 getdigits(&p) + MSHAPE_NUMBERED; 3528 else 3529 (void)getdigits(&p); 3530 break; 3531 } 3532 len = (int)STRLEN(mshape_names[i]); 3533 if (STRNICMP(p, mshape_names[i], len) == 0) 3534 { 3535 if (round == 2) 3536 shape_table[idx].mshape = i; 3537 p += len; 3538 break; 3539 } 3540 } 3541 } 3542 else /* if (what == SHAPE_MOUSE) */ 3543 #endif 3544 { 3545 /* 3546 * First handle the ones with a number argument. 3547 */ 3548 i = *p; 3549 len = 0; 3550 if (STRNICMP(p, "ver", 3) == 0) 3551 len = 3; 3552 else if (STRNICMP(p, "hor", 3) == 0) 3553 len = 3; 3554 else if (STRNICMP(p, "blinkwait", 9) == 0) 3555 len = 9; 3556 else if (STRNICMP(p, "blinkon", 7) == 0) 3557 len = 7; 3558 else if (STRNICMP(p, "blinkoff", 8) == 0) 3559 len = 8; 3560 if (len != 0) 3561 { 3562 p += len; 3563 if (!VIM_ISDIGIT(*p)) 3564 return (char_u *)N_("E548: digit expected"); 3565 n = getdigits(&p); 3566 if (len == 3) /* "ver" or "hor" */ 3567 { 3568 if (n == 0) 3569 return (char_u *)N_("E549: Illegal percentage"); 3570 if (round == 2) 3571 { 3572 if (TOLOWER_ASC(i) == 'v') 3573 shape_table[idx].shape = SHAPE_VER; 3574 else 3575 shape_table[idx].shape = SHAPE_HOR; 3576 shape_table[idx].percentage = n; 3577 } 3578 } 3579 else if (round == 2) 3580 { 3581 if (len == 9) 3582 shape_table[idx].blinkwait = n; 3583 else if (len == 7) 3584 shape_table[idx].blinkon = n; 3585 else 3586 shape_table[idx].blinkoff = n; 3587 } 3588 } 3589 else if (STRNICMP(p, "block", 5) == 0) 3590 { 3591 if (round == 2) 3592 shape_table[idx].shape = SHAPE_BLOCK; 3593 p += 5; 3594 } 3595 else /* must be a highlight group name then */ 3596 { 3597 endp = vim_strchr(p, '-'); 3598 if (commap == NULL) /* last part */ 3599 { 3600 if (endp == NULL) 3601 endp = p + STRLEN(p); /* find end of part */ 3602 } 3603 else if (endp > commap || endp == NULL) 3604 endp = commap; 3605 slashp = vim_strchr(p, '/'); 3606 if (slashp != NULL && slashp < endp) 3607 { 3608 /* "group/langmap_group" */ 3609 i = syn_check_group(p, (int)(slashp - p)); 3610 p = slashp + 1; 3611 } 3612 if (round == 2) 3613 { 3614 shape_table[idx].id = syn_check_group(p, 3615 (int)(endp - p)); 3616 shape_table[idx].id_lm = shape_table[idx].id; 3617 if (slashp != NULL && slashp < endp) 3618 shape_table[idx].id = i; 3619 } 3620 p = endp; 3621 } 3622 } /* if (what != SHAPE_MOUSE) */ 3623 3624 if (*p == '-') 3625 ++p; 3626 } 3627 } 3628 modep = p; 3629 if (*modep == ',') 3630 ++modep; 3631 } 3632 } 3633 3634 /* If the 's' flag is not given, use the 'v' cursor for 's' */ 3635 if (!found_ve) 3636 { 3637 #ifdef FEAT_MOUSESHAPE 3638 if (what == SHAPE_MOUSE) 3639 { 3640 shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape; 3641 } 3642 else 3643 #endif 3644 { 3645 shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape; 3646 shape_table[SHAPE_IDX_VE].percentage = 3647 shape_table[SHAPE_IDX_V].percentage; 3648 shape_table[SHAPE_IDX_VE].blinkwait = 3649 shape_table[SHAPE_IDX_V].blinkwait; 3650 shape_table[SHAPE_IDX_VE].blinkon = 3651 shape_table[SHAPE_IDX_V].blinkon; 3652 shape_table[SHAPE_IDX_VE].blinkoff = 3653 shape_table[SHAPE_IDX_V].blinkoff; 3654 shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id; 3655 shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm; 3656 } 3657 } 3658 3659 return NULL; 3660 } 3661 3662 # if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \ 3663 || defined(FEAT_MOUSESHAPE) || defined(PROTO) 3664 /* 3665 * Return the index into shape_table[] for the current mode. 3666 * When "mouse" is TRUE, consider indexes valid for the mouse pointer. 3667 */ 3668 int 3669 get_shape_idx(mouse) 3670 int mouse; 3671 { 3672 #ifdef FEAT_MOUSESHAPE 3673 if (mouse && (State == HITRETURN || State == ASKMORE)) 3674 { 3675 # ifdef FEAT_GUI 3676 int x, y; 3677 gui_mch_getmouse(&x, &y); 3678 if (Y_2_ROW(y) == Rows - 1) 3679 return SHAPE_IDX_MOREL; 3680 # endif 3681 return SHAPE_IDX_MORE; 3682 } 3683 if (mouse && drag_status_line) 3684 return SHAPE_IDX_SDRAG; 3685 # ifdef FEAT_VERTSPLIT 3686 if (mouse && drag_sep_line) 3687 return SHAPE_IDX_VDRAG; 3688 # endif 3689 #endif 3690 if (!mouse && State == SHOWMATCH) 3691 return SHAPE_IDX_SM; 3692 #ifdef FEAT_VREPLACE 3693 if (State & VREPLACE_FLAG) 3694 return SHAPE_IDX_R; 3695 #endif 3696 if (State & REPLACE_FLAG) 3697 return SHAPE_IDX_R; 3698 if (State & INSERT) 3699 return SHAPE_IDX_I; 3700 if (State & CMDLINE) 3701 { 3702 if (cmdline_at_end()) 3703 return SHAPE_IDX_C; 3704 if (cmdline_overstrike()) 3705 return SHAPE_IDX_CR; 3706 return SHAPE_IDX_CI; 3707 } 3708 if (finish_op) 3709 return SHAPE_IDX_O; 3710 if (VIsual_active) 3711 { 3712 if (*p_sel == 'e') 3713 return SHAPE_IDX_VE; 3714 else 3715 return SHAPE_IDX_V; 3716 } 3717 return SHAPE_IDX_N; 3718 } 3719 #endif 3720 3721 # if defined(FEAT_MOUSESHAPE) || defined(PROTO) 3722 static int old_mouse_shape = 0; 3723 3724 /* 3725 * Set the mouse shape: 3726 * If "shape" is -1, use shape depending on the current mode, 3727 * depending on the current state. 3728 * If "shape" is -2, only update the shape when it's CLINE or STATUS (used 3729 * when the mouse moves off the status or command line). 3730 */ 3731 void 3732 update_mouseshape(shape_idx) 3733 int shape_idx; 3734 { 3735 int new_mouse_shape; 3736 3737 /* Only works in GUI mode. */ 3738 if (!gui.in_use || gui.starting) 3739 return; 3740 3741 /* Postpone the updating when more is to come. Speeds up executing of 3742 * mappings. */ 3743 if (shape_idx == -1 && char_avail()) 3744 { 3745 postponed_mouseshape = TRUE; 3746 return; 3747 } 3748 3749 /* When ignoring the mouse don't change shape on the statusline. */ 3750 if (*p_mouse == NUL 3751 && (shape_idx == SHAPE_IDX_CLINE 3752 || shape_idx == SHAPE_IDX_STATUS 3753 || shape_idx == SHAPE_IDX_VSEP)) 3754 shape_idx = -2; 3755 3756 if (shape_idx == -2 3757 && old_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape 3758 && old_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape 3759 && old_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape) 3760 return; 3761 if (shape_idx < 0) 3762 new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape; 3763 else 3764 new_mouse_shape = shape_table[shape_idx].mshape; 3765 if (new_mouse_shape != old_mouse_shape) 3766 { 3767 mch_set_mouse_shape(new_mouse_shape); 3768 old_mouse_shape = new_mouse_shape; 3769 } 3770 postponed_mouseshape = FALSE; 3771 } 3772 # endif 3773 3774 #endif /* CURSOR_SHAPE */ 3775 3776 3777 /* TODO: make some #ifdef for this */ 3778 /*--------[ file searching ]-------------------------------------------------*/ 3779 /* 3780 * File searching functions for 'path', 'tags' and 'cdpath' options. 3781 * External visible functions: 3782 * vim_findfile_init() creates/initialises the search context 3783 * vim_findfile_free_visited() free list of visited files/dirs of search 3784 * context 3785 * vim_findfile() find a file in the search context 3786 * vim_findfile_cleanup() cleanup/free search context created by 3787 * vim_findfile_init() 3788 * 3789 * All static functions and variables start with 'ff_' 3790 * 3791 * In general it works like this: 3792 * First you create yourself a search context by calling vim_findfile_init(). 3793 * It is possible to give a search context from a previous call to 3794 * vim_findfile_init(), so it can be reused. After this you call vim_findfile() 3795 * until you are satisfied with the result or it returns NULL. On every call it 3796 * returns the next file which matches the conditions given to 3797 * vim_findfile_init(). If it doesn't find a next file it returns NULL. 3798 * 3799 * It is possible to call vim_findfile_init() again to reinitialise your search 3800 * with some new parameters. Don't forget to pass your old search context to 3801 * it, so it can reuse it and especially reuse the list of already visited 3802 * directories. If you want to delete the list of already visited directories 3803 * simply call vim_findfile_free_visited(). 3804 * 3805 * When you are done call vim_findfile_cleanup() to free the search context. 3806 * 3807 * The function vim_findfile_init() has a long comment, which describes the 3808 * needed parameters. 3809 * 3810 * 3811 * 3812 * ATTENTION: 3813 * ========== 3814 * Also we use an allocated search context here, this functions are NOT 3815 * thread-safe!!!!! 3816 * 3817 * To minimize parameter passing (or because I'm to lazy), only the 3818 * external visible functions get a search context as a parameter. This is 3819 * then assigned to a static global, which is used throughout the local 3820 * functions. 3821 */ 3822 3823 /* 3824 * type for the directory search stack 3825 */ 3826 typedef struct ff_stack 3827 { 3828 struct ff_stack *ffs_prev; 3829 3830 /* the fix part (no wildcards) and the part containing the wildcards 3831 * of the search path 3832 */ 3833 char_u *ffs_fix_path; 3834 #ifdef FEAT_PATH_EXTRA 3835 char_u *ffs_wc_path; 3836 #endif 3837 3838 /* files/dirs found in the above directory, matched by the first wildcard 3839 * of wc_part 3840 */ 3841 char_u **ffs_filearray; 3842 int ffs_filearray_size; 3843 char_u ffs_filearray_cur; /* needed for partly handled dirs */ 3844 3845 /* to store status of partly handled directories 3846 * 0: we work on this directory for the first time 3847 * 1: this directory was partly searched in an earlier step 3848 */ 3849 int ffs_stage; 3850 3851 /* How deep are we in the directory tree? 3852 * Counts backward from value of level parameter to vim_findfile_init 3853 */ 3854 int ffs_level; 3855 3856 /* Did we already expand '**' to an empty string? */ 3857 int ffs_star_star_empty; 3858 } ff_stack_T; 3859 3860 /* 3861 * type for already visited directories or files. 3862 */ 3863 typedef struct ff_visited 3864 { 3865 struct ff_visited *ffv_next; 3866 3867 #ifdef FEAT_PATH_EXTRA 3868 /* Visited directories are different if the wildcard string are 3869 * different. So we have to save it. 3870 */ 3871 char_u *ffv_wc_path; 3872 #endif 3873 /* for unix use inode etc for comparison (needed because of links), else 3874 * use filename. 3875 */ 3876 #ifdef UNIX 3877 int ffv_dev_valid; /* ffv_dev and ffv_ino were set */ 3878 dev_t ffv_dev; /* device number */ 3879 ino_t ffv_ino; /* inode number */ 3880 #endif 3881 /* The memory for this struct is allocated according to the length of 3882 * ffv_fname. 3883 */ 3884 char_u ffv_fname[1]; /* actually longer */ 3885 } ff_visited_T; 3886 3887 /* 3888 * We might have to manage several visited lists during a search. 3889 * This is especially needed for the tags option. If tags is set to: 3890 * "./++/tags,./++/TAGS,++/tags" (replace + with *) 3891 * So we have to do 3 searches: 3892 * 1) search from the current files directory downward for the file "tags" 3893 * 2) search from the current files directory downward for the file "TAGS" 3894 * 3) search from Vims current directory downwards for the file "tags" 3895 * As you can see, the first and the third search are for the same file, so for 3896 * the third search we can use the visited list of the first search. For the 3897 * second search we must start from a empty visited list. 3898 * The struct ff_visited_list_hdr is used to manage a linked list of already 3899 * visited lists. 3900 */ 3901 typedef struct ff_visited_list_hdr 3902 { 3903 struct ff_visited_list_hdr *ffvl_next; 3904 3905 /* the filename the attached visited list is for */ 3906 char_u *ffvl_filename; 3907 3908 ff_visited_T *ffvl_visited_list; 3909 3910 } ff_visited_list_hdr_T; 3911 3912 3913 /* 3914 * '**' can be expanded to several directory levels. 3915 * Set the default maximum depth. 3916 */ 3917 #define FF_MAX_STAR_STAR_EXPAND ((char_u)30) 3918 3919 /* 3920 * The search context: 3921 * ffsc_stack_ptr: the stack for the dirs to search 3922 * ffsc_visited_list: the currently active visited list 3923 * ffsc_dir_visited_list: the currently active visited list for search dirs 3924 * ffsc_visited_lists_list: the list of all visited lists 3925 * ffsc_dir_visited_lists_list: the list of all visited lists for search dirs 3926 * ffsc_file_to_search: the file to search for 3927 * ffsc_start_dir: the starting directory, if search path was relative 3928 * ffsc_fix_path: the fix part of the given path (without wildcards) 3929 * Needed for upward search. 3930 * ffsc_wc_path: the part of the given path containing wildcards 3931 * ffsc_level: how many levels of dirs to search downwards 3932 * ffsc_stopdirs_v: array of stop directories for upward search 3933 * ffsc_find_what: FINDFILE_BOTH, FINDFILE_DIR or FINDFILE_FILE 3934 * ffsc_tagfile: searching for tags file, don't use 'suffixesadd' 3935 */ 3936 typedef struct ff_search_ctx_T 3937 { 3938 ff_stack_T *ffsc_stack_ptr; 3939 ff_visited_list_hdr_T *ffsc_visited_list; 3940 ff_visited_list_hdr_T *ffsc_dir_visited_list; 3941 ff_visited_list_hdr_T *ffsc_visited_lists_list; 3942 ff_visited_list_hdr_T *ffsc_dir_visited_lists_list; 3943 char_u *ffsc_file_to_search; 3944 char_u *ffsc_start_dir; 3945 char_u *ffsc_fix_path; 3946 #ifdef FEAT_PATH_EXTRA 3947 char_u *ffsc_wc_path; 3948 int ffsc_level; 3949 char_u **ffsc_stopdirs_v; 3950 #endif 3951 int ffsc_find_what; 3952 int ffsc_tagfile; 3953 } ff_search_ctx_T; 3954 3955 /* locally needed functions */ 3956 #ifdef FEAT_PATH_EXTRA 3957 static int ff_check_visited __ARGS((ff_visited_T **, char_u *, char_u *)); 3958 #else 3959 static int ff_check_visited __ARGS((ff_visited_T **, char_u *)); 3960 #endif 3961 static void vim_findfile_free_visited_list __ARGS((ff_visited_list_hdr_T **list_headp)); 3962 static void ff_free_visited_list __ARGS((ff_visited_T *vl)); 3963 static ff_visited_list_hdr_T* ff_get_visited_list __ARGS((char_u *, ff_visited_list_hdr_T **list_headp)); 3964 #ifdef FEAT_PATH_EXTRA 3965 static int ff_wc_equal __ARGS((char_u *s1, char_u *s2)); 3966 #endif 3967 3968 static void ff_push __ARGS((ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr)); 3969 static ff_stack_T *ff_pop __ARGS((ff_search_ctx_T *search_ctx)); 3970 static void ff_clear __ARGS((ff_search_ctx_T *search_ctx)); 3971 static void ff_free_stack_element __ARGS((ff_stack_T *stack_ptr)); 3972 #ifdef FEAT_PATH_EXTRA 3973 static ff_stack_T *ff_create_stack_element __ARGS((char_u *, char_u *, int, int)); 3974 #else 3975 static ff_stack_T *ff_create_stack_element __ARGS((char_u *, int, int)); 3976 #endif 3977 #ifdef FEAT_PATH_EXTRA 3978 static int ff_path_in_stoplist __ARGS((char_u *, int, char_u **)); 3979 #endif 3980 3981 static char_u e_pathtoolong[] = N_("E854: path too long for completion"); 3982 3983 #if 0 3984 /* 3985 * if someone likes findfirst/findnext, here are the functions 3986 * NOT TESTED!! 3987 */ 3988 3989 static void *ff_fn_search_context = NULL; 3990 3991 char_u * 3992 vim_findfirst(path, filename, level) 3993 char_u *path; 3994 char_u *filename; 3995 int level; 3996 { 3997 ff_fn_search_context = 3998 vim_findfile_init(path, filename, NULL, level, TRUE, FALSE, 3999 ff_fn_search_context, rel_fname); 4000 if (NULL == ff_fn_search_context) 4001 return NULL; 4002 else 4003 return vim_findnext() 4004 } 4005 4006 char_u * 4007 vim_findnext() 4008 { 4009 char_u *ret = vim_findfile(ff_fn_search_context); 4010 4011 if (NULL == ret) 4012 { 4013 vim_findfile_cleanup(ff_fn_search_context); 4014 ff_fn_search_context = NULL; 4015 } 4016 return ret; 4017 } 4018 #endif 4019 4020 /* 4021 * Initialization routine for vim_findfile(). 4022 * 4023 * Returns the newly allocated search context or NULL if an error occurred. 4024 * 4025 * Don't forget to clean up by calling vim_findfile_cleanup() if you are done 4026 * with the search context. 4027 * 4028 * Find the file 'filename' in the directory 'path'. 4029 * The parameter 'path' may contain wildcards. If so only search 'level' 4030 * directories deep. The parameter 'level' is the absolute maximum and is 4031 * not related to restricts given to the '**' wildcard. If 'level' is 100 4032 * and you use '**200' vim_findfile() will stop after 100 levels. 4033 * 4034 * 'filename' cannot contain wildcards! It is used as-is, no backslashes to 4035 * escape special characters. 4036 * 4037 * If 'stopdirs' is not NULL and nothing is found downward, the search is 4038 * restarted on the next higher directory level. This is repeated until the 4039 * start-directory of a search is contained in 'stopdirs'. 'stopdirs' has the 4040 * format ";*<dirname>*\(;<dirname>\)*;\=$". 4041 * 4042 * If the 'path' is relative, the starting dir for the search is either VIM's 4043 * current dir or if the path starts with "./" the current files dir. 4044 * If the 'path' is absolute, the starting dir is that part of the path before 4045 * the first wildcard. 4046 * 4047 * Upward search is only done on the starting dir. 4048 * 4049 * If 'free_visited' is TRUE the list of already visited files/directories is 4050 * cleared. Set this to FALSE if you just want to search from another 4051 * directory, but want to be sure that no directory from a previous search is 4052 * searched again. This is useful if you search for a file at different places. 4053 * The list of visited files/dirs can also be cleared with the function 4054 * vim_findfile_free_visited(). 4055 * 4056 * Set the parameter 'find_what' to FINDFILE_DIR if you want to search for 4057 * directories only, FINDFILE_FILE for files only, FINDFILE_BOTH for both. 4058 * 4059 * A search context returned by a previous call to vim_findfile_init() can be 4060 * passed in the parameter "search_ctx_arg". This context is reused and 4061 * reinitialized with the new parameters. The list of already visited 4062 * directories from this context is only deleted if the parameter 4063 * "free_visited" is true. Be aware that the passed "search_ctx_arg" is freed 4064 * if the reinitialization fails. 4065 * 4066 * If you don't have a search context from a previous call "search_ctx_arg" 4067 * must be NULL. 4068 * 4069 * This function silently ignores a few errors, vim_findfile() will have 4070 * limited functionality then. 4071 */ 4072 void * 4073 vim_findfile_init(path, filename, stopdirs, level, free_visited, find_what, 4074 search_ctx_arg, tagfile, rel_fname) 4075 char_u *path; 4076 char_u *filename; 4077 char_u *stopdirs UNUSED; 4078 int level; 4079 int free_visited; 4080 int find_what; 4081 void *search_ctx_arg; 4082 int tagfile; /* expanding names of tags files */ 4083 char_u *rel_fname; /* file name to use for "." */ 4084 { 4085 #ifdef FEAT_PATH_EXTRA 4086 char_u *wc_part; 4087 #endif 4088 ff_stack_T *sptr; 4089 ff_search_ctx_T *search_ctx; 4090 4091 /* If a search context is given by the caller, reuse it, else allocate a 4092 * new one. 4093 */ 4094 if (search_ctx_arg != NULL) 4095 search_ctx = search_ctx_arg; 4096 else 4097 { 4098 search_ctx = (ff_search_ctx_T*)alloc((unsigned)sizeof(ff_search_ctx_T)); 4099 if (search_ctx == NULL) 4100 goto error_return; 4101 vim_memset(search_ctx, 0, sizeof(ff_search_ctx_T)); 4102 } 4103 search_ctx->ffsc_find_what = find_what; 4104 search_ctx->ffsc_tagfile = tagfile; 4105 4106 /* clear the search context, but NOT the visited lists */ 4107 ff_clear(search_ctx); 4108 4109 /* clear visited list if wanted */ 4110 if (free_visited == TRUE) 4111 vim_findfile_free_visited(search_ctx); 4112 else 4113 { 4114 /* Reuse old visited lists. Get the visited list for the given 4115 * filename. If no list for the current filename exists, creates a new 4116 * one. */ 4117 search_ctx->ffsc_visited_list = ff_get_visited_list(filename, 4118 &search_ctx->ffsc_visited_lists_list); 4119 if (search_ctx->ffsc_visited_list == NULL) 4120 goto error_return; 4121 search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename, 4122 &search_ctx->ffsc_dir_visited_lists_list); 4123 if (search_ctx->ffsc_dir_visited_list == NULL) 4124 goto error_return; 4125 } 4126 4127 if (ff_expand_buffer == NULL) 4128 { 4129 ff_expand_buffer = (char_u*)alloc(MAXPATHL); 4130 if (ff_expand_buffer == NULL) 4131 goto error_return; 4132 } 4133 4134 /* Store information on starting dir now if path is relative. 4135 * If path is absolute, we do that later. */ 4136 if (path[0] == '.' 4137 && (vim_ispathsep(path[1]) || path[1] == NUL) 4138 && (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL) 4139 && rel_fname != NULL) 4140 { 4141 int len = (int)(gettail(rel_fname) - rel_fname); 4142 4143 if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL) 4144 { 4145 /* Make the start dir an absolute path name. */ 4146 vim_strncpy(ff_expand_buffer, rel_fname, len); 4147 search_ctx->ffsc_start_dir = FullName_save(ff_expand_buffer, FALSE); 4148 } 4149 else 4150 search_ctx->ffsc_start_dir = vim_strnsave(rel_fname, len); 4151 if (search_ctx->ffsc_start_dir == NULL) 4152 goto error_return; 4153 if (*++path != NUL) 4154 ++path; 4155 } 4156 else if (*path == NUL || !vim_isAbsName(path)) 4157 { 4158 #ifdef BACKSLASH_IN_FILENAME 4159 /* "c:dir" needs "c:" to be expanded, otherwise use current dir */ 4160 if (*path != NUL && path[1] == ':') 4161 { 4162 char_u drive[3]; 4163 4164 drive[0] = path[0]; 4165 drive[1] = ':'; 4166 drive[2] = NUL; 4167 if (vim_FullName(drive, ff_expand_buffer, MAXPATHL, TRUE) == FAIL) 4168 goto error_return; 4169 path += 2; 4170 } 4171 else 4172 #endif 4173 if (mch_dirname(ff_expand_buffer, MAXPATHL) == FAIL) 4174 goto error_return; 4175 4176 search_ctx->ffsc_start_dir = vim_strsave(ff_expand_buffer); 4177 if (search_ctx->ffsc_start_dir == NULL) 4178 goto error_return; 4179 4180 #ifdef BACKSLASH_IN_FILENAME 4181 /* A path that starts with "/dir" is relative to the drive, not to the 4182 * directory (but not for "//machine/dir"). Only use the drive name. */ 4183 if ((*path == '/' || *path == '\\') 4184 && path[1] != path[0] 4185 && search_ctx->ffsc_start_dir[1] == ':') 4186 search_ctx->ffsc_start_dir[2] = NUL; 4187 #endif 4188 } 4189 4190 #ifdef FEAT_PATH_EXTRA 4191 /* 4192 * If stopdirs are given, split them into an array of pointers. 4193 * If this fails (mem allocation), there is no upward search at all or a 4194 * stop directory is not recognized -> continue silently. 4195 * If stopdirs just contains a ";" or is empty, 4196 * search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This 4197 * is handled as unlimited upward search. See function 4198 * ff_path_in_stoplist() for details. 4199 */ 4200 if (stopdirs != NULL) 4201 { 4202 char_u *walker = stopdirs; 4203 int dircount; 4204 4205 while (*walker == ';') 4206 walker++; 4207 4208 dircount = 1; 4209 search_ctx->ffsc_stopdirs_v = 4210 (char_u **)alloc((unsigned)sizeof(char_u *)); 4211 4212 if (search_ctx->ffsc_stopdirs_v != NULL) 4213 { 4214 do 4215 { 4216 char_u *helper; 4217 void *ptr; 4218 4219 helper = walker; 4220 ptr = vim_realloc(search_ctx->ffsc_stopdirs_v, 4221 (dircount + 1) * sizeof(char_u *)); 4222 if (ptr) 4223 search_ctx->ffsc_stopdirs_v = ptr; 4224 else 4225 /* ignore, keep what we have and continue */ 4226 break; 4227 walker = vim_strchr(walker, ';'); 4228 if (walker) 4229 { 4230 search_ctx->ffsc_stopdirs_v[dircount-1] = 4231 vim_strnsave(helper, (int)(walker - helper)); 4232 walker++; 4233 } 4234 else 4235 /* this might be "", which means ascent till top 4236 * of directory tree. 4237 */ 4238 search_ctx->ffsc_stopdirs_v[dircount-1] = 4239 vim_strsave(helper); 4240 4241 dircount++; 4242 4243 } while (walker != NULL); 4244 search_ctx->ffsc_stopdirs_v[dircount-1] = NULL; 4245 } 4246 } 4247 #endif 4248 4249 #ifdef FEAT_PATH_EXTRA 4250 search_ctx->ffsc_level = level; 4251 4252 /* split into: 4253 * -fix path 4254 * -wildcard_stuff (might be NULL) 4255 */ 4256 wc_part = vim_strchr(path, '*'); 4257 if (wc_part != NULL) 4258 { 4259 int llevel; 4260 int len; 4261 char *errpt; 4262 4263 /* save the fix part of the path */ 4264 search_ctx->ffsc_fix_path = vim_strnsave(path, (int)(wc_part - path)); 4265 4266 /* 4267 * copy wc_path and add restricts to the '**' wildcard. 4268 * The octet after a '**' is used as a (binary) counter. 4269 * So '**3' is transposed to '**^C' ('^C' is ASCII value 3) 4270 * or '**76' is transposed to '**N'( 'N' is ASCII value 76). 4271 * For EBCDIC you get different character values. 4272 * If no restrict is given after '**' the default is used. 4273 * Due to this technique the path looks awful if you print it as a 4274 * string. 4275 */ 4276 len = 0; 4277 while (*wc_part != NUL) 4278 { 4279 if (len + 5 >= MAXPATHL) 4280 { 4281 EMSG(_(e_pathtoolong)); 4282 break; 4283 } 4284 if (STRNCMP(wc_part, "**", 2) == 0) 4285 { 4286 ff_expand_buffer[len++] = *wc_part++; 4287 ff_expand_buffer[len++] = *wc_part++; 4288 4289 llevel = strtol((char *)wc_part, &errpt, 10); 4290 if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255) 4291 ff_expand_buffer[len++] = llevel; 4292 else if ((char_u *)errpt != wc_part && llevel == 0) 4293 /* restrict is 0 -> remove already added '**' */ 4294 len -= 2; 4295 else 4296 ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND; 4297 wc_part = (char_u *)errpt; 4298 if (*wc_part != NUL && !vim_ispathsep(*wc_part)) 4299 { 4300 EMSG2(_("E343: Invalid path: '**[number]' must be at the end of the path or be followed by '%s'."), PATHSEPSTR); 4301 goto error_return; 4302 } 4303 } 4304 else 4305 ff_expand_buffer[len++] = *wc_part++; 4306 } 4307 ff_expand_buffer[len] = NUL; 4308 search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer); 4309 4310 if (search_ctx->ffsc_wc_path == NULL) 4311 goto error_return; 4312 } 4313 else 4314 #endif 4315 search_ctx->ffsc_fix_path = vim_strsave(path); 4316 4317 if (search_ctx->ffsc_start_dir == NULL) 4318 { 4319 /* store the fix part as startdir. 4320 * This is needed if the parameter path is fully qualified. 4321 */ 4322 search_ctx->ffsc_start_dir = vim_strsave(search_ctx->ffsc_fix_path); 4323 if (search_ctx->ffsc_start_dir == NULL) 4324 goto error_return; 4325 search_ctx->ffsc_fix_path[0] = NUL; 4326 } 4327 4328 /* create an absolute path */ 4329 if (STRLEN(search_ctx->ffsc_start_dir) 4330 + STRLEN(search_ctx->ffsc_fix_path) + 3 >= MAXPATHL) 4331 { 4332 EMSG(_(e_pathtoolong)); 4333 goto error_return; 4334 } 4335 STRCPY(ff_expand_buffer, search_ctx->ffsc_start_dir); 4336 add_pathsep(ff_expand_buffer); 4337 { 4338 int eb_len = (int)STRLEN(ff_expand_buffer); 4339 char_u *buf = alloc(eb_len 4340 + (int)STRLEN(search_ctx->ffsc_fix_path) + 1); 4341 4342 STRCPY(buf, ff_expand_buffer); 4343 STRCPY(buf + eb_len, search_ctx->ffsc_fix_path); 4344 if (mch_isdir(buf)) 4345 { 4346 STRCAT(ff_expand_buffer, search_ctx->ffsc_fix_path); 4347 add_pathsep(ff_expand_buffer); 4348 } 4349 #ifdef FEAT_PATH_EXTRA 4350 else 4351 { 4352 char_u *p = gettail(search_ctx->ffsc_fix_path); 4353 char_u *wc_path = NULL; 4354 char_u *temp = NULL; 4355 int len = 0; 4356 4357 if (p > search_ctx->ffsc_fix_path) 4358 { 4359 len = (int)(p - search_ctx->ffsc_fix_path) - 1; 4360 STRNCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, len); 4361 add_pathsep(ff_expand_buffer); 4362 } 4363 else 4364 len = (int)STRLEN(search_ctx->ffsc_fix_path); 4365 4366 if (search_ctx->ffsc_wc_path != NULL) 4367 { 4368 wc_path = vim_strsave(search_ctx->ffsc_wc_path); 4369 temp = alloc((int)(STRLEN(search_ctx->ffsc_wc_path) 4370 + STRLEN(search_ctx->ffsc_fix_path + len) 4371 + 1)); 4372 } 4373 4374 if (temp == NULL || wc_path == NULL) 4375 { 4376 vim_free(buf); 4377 vim_free(temp); 4378 vim_free(wc_path); 4379 goto error_return; 4380 } 4381 4382 STRCPY(temp, search_ctx->ffsc_fix_path + len); 4383 STRCAT(temp, search_ctx->ffsc_wc_path); 4384 vim_free(search_ctx->ffsc_wc_path); 4385 vim_free(wc_path); 4386 search_ctx->ffsc_wc_path = temp; 4387 } 4388 #endif 4389 vim_free(buf); 4390 } 4391 4392 sptr = ff_create_stack_element(ff_expand_buffer, 4393 #ifdef FEAT_PATH_EXTRA 4394 search_ctx->ffsc_wc_path, 4395 #endif 4396 level, 0); 4397 4398 if (sptr == NULL) 4399 goto error_return; 4400 4401 ff_push(search_ctx, sptr); 4402 4403 search_ctx->ffsc_file_to_search = vim_strsave(filename); 4404 if (search_ctx->ffsc_file_to_search == NULL) 4405 goto error_return; 4406 4407 return search_ctx; 4408 4409 error_return: 4410 /* 4411 * We clear the search context now! 4412 * Even when the caller gave us a (perhaps valid) context we free it here, 4413 * as we might have already destroyed it. 4414 */ 4415 vim_findfile_cleanup(search_ctx); 4416 return NULL; 4417 } 4418 4419 #if defined(FEAT_PATH_EXTRA) || defined(PROTO) 4420 /* 4421 * Get the stopdir string. Check that ';' is not escaped. 4422 */ 4423 char_u * 4424 vim_findfile_stopdir(buf) 4425 char_u *buf; 4426 { 4427 char_u *r_ptr = buf; 4428 4429 while (*r_ptr != NUL && *r_ptr != ';') 4430 { 4431 if (r_ptr[0] == '\\' && r_ptr[1] == ';') 4432 { 4433 /* Overwrite the escape char, 4434 * use STRLEN(r_ptr) to move the trailing '\0'. */ 4435 STRMOVE(r_ptr, r_ptr + 1); 4436 r_ptr++; 4437 } 4438 r_ptr++; 4439 } 4440 if (*r_ptr == ';') 4441 { 4442 *r_ptr = 0; 4443 r_ptr++; 4444 } 4445 else if (*r_ptr == NUL) 4446 r_ptr = NULL; 4447 return r_ptr; 4448 } 4449 #endif 4450 4451 /* 4452 * Clean up the given search context. Can handle a NULL pointer. 4453 */ 4454 void 4455 vim_findfile_cleanup(ctx) 4456 void *ctx; 4457 { 4458 if (ctx == NULL) 4459 return; 4460 4461 vim_findfile_free_visited(ctx); 4462 ff_clear(ctx); 4463 vim_free(ctx); 4464 } 4465 4466 /* 4467 * Find a file in a search context. 4468 * The search context was created with vim_findfile_init() above. 4469 * Return a pointer to an allocated file name or NULL if nothing found. 4470 * To get all matching files call this function until you get NULL. 4471 * 4472 * If the passed search_context is NULL, NULL is returned. 4473 * 4474 * The search algorithm is depth first. To change this replace the 4475 * stack with a list (don't forget to leave partly searched directories on the 4476 * top of the list). 4477 */ 4478 char_u * 4479 vim_findfile(search_ctx_arg) 4480 void *search_ctx_arg; 4481 { 4482 char_u *file_path; 4483 #ifdef FEAT_PATH_EXTRA 4484 char_u *rest_of_wildcards; 4485 char_u *path_end = NULL; 4486 #endif 4487 ff_stack_T *stackp; 4488 #if defined(FEAT_SEARCHPATH) || defined(FEAT_PATH_EXTRA) 4489 int len; 4490 #endif 4491 int i; 4492 char_u *p; 4493 #ifdef FEAT_SEARCHPATH 4494 char_u *suf; 4495 #endif 4496 ff_search_ctx_T *search_ctx; 4497 4498 if (search_ctx_arg == NULL) 4499 return NULL; 4500 4501 search_ctx = (ff_search_ctx_T *)search_ctx_arg; 4502 4503 /* 4504 * filepath is used as buffer for various actions and as the storage to 4505 * return a found filename. 4506 */ 4507 if ((file_path = alloc((int)MAXPATHL)) == NULL) 4508 return NULL; 4509 4510 #ifdef FEAT_PATH_EXTRA 4511 /* store the end of the start dir -- needed for upward search */ 4512 if (search_ctx->ffsc_start_dir != NULL) 4513 path_end = &search_ctx->ffsc_start_dir[ 4514 STRLEN(search_ctx->ffsc_start_dir)]; 4515 #endif 4516 4517 #ifdef FEAT_PATH_EXTRA 4518 /* upward search loop */ 4519 for (;;) 4520 { 4521 #endif 4522 /* downward search loop */ 4523 for (;;) 4524 { 4525 /* check if user user wants to stop the search*/ 4526 ui_breakcheck(); 4527 if (got_int) 4528 break; 4529 4530 /* get directory to work on from stack */ 4531 stackp = ff_pop(search_ctx); 4532 if (stackp == NULL) 4533 break; 4534 4535 /* 4536 * TODO: decide if we leave this test in 4537 * 4538 * GOOD: don't search a directory(-tree) twice. 4539 * BAD: - check linked list for every new directory entered. 4540 * - check for double files also done below 4541 * 4542 * Here we check if we already searched this directory. 4543 * We already searched a directory if: 4544 * 1) The directory is the same. 4545 * 2) We would use the same wildcard string. 4546 * 4547 * Good if you have links on same directory via several ways 4548 * or you have selfreferences in directories (e.g. SuSE Linux 6.3: 4549 * /etc/rc.d/init.d is linked to /etc/rc.d -> endless loop) 4550 * 4551 * This check is only needed for directories we work on for the 4552 * first time (hence stackp->ff_filearray == NULL) 4553 */ 4554 if (stackp->ffs_filearray == NULL 4555 && ff_check_visited(&search_ctx->ffsc_dir_visited_list 4556 ->ffvl_visited_list, 4557 stackp->ffs_fix_path 4558 #ifdef FEAT_PATH_EXTRA 4559 , stackp->ffs_wc_path 4560 #endif 4561 ) == FAIL) 4562 { 4563 #ifdef FF_VERBOSE 4564 if (p_verbose >= 5) 4565 { 4566 verbose_enter_scroll(); 4567 smsg((char_u *)"Already Searched: %s (%s)", 4568 stackp->ffs_fix_path, stackp->ffs_wc_path); 4569 /* don't overwrite this either */ 4570 msg_puts((char_u *)"\n"); 4571 verbose_leave_scroll(); 4572 } 4573 #endif 4574 ff_free_stack_element(stackp); 4575 continue; 4576 } 4577 #ifdef FF_VERBOSE 4578 else if (p_verbose >= 5) 4579 { 4580 verbose_enter_scroll(); 4581 smsg((char_u *)"Searching: %s (%s)", 4582 stackp->ffs_fix_path, stackp->ffs_wc_path); 4583 /* don't overwrite this either */ 4584 msg_puts((char_u *)"\n"); 4585 verbose_leave_scroll(); 4586 } 4587 #endif 4588 4589 /* check depth */ 4590 if (stackp->ffs_level <= 0) 4591 { 4592 ff_free_stack_element(stackp); 4593 continue; 4594 } 4595 4596 file_path[0] = NUL; 4597 4598 /* 4599 * If no filearray till now expand wildcards 4600 * The function expand_wildcards() can handle an array of paths 4601 * and all possible expands are returned in one array. We use this 4602 * to handle the expansion of '**' into an empty string. 4603 */ 4604 if (stackp->ffs_filearray == NULL) 4605 { 4606 char_u *dirptrs[2]; 4607 4608 /* we use filepath to build the path expand_wildcards() should 4609 * expand. 4610 */ 4611 dirptrs[0] = file_path; 4612 dirptrs[1] = NULL; 4613 4614 /* if we have a start dir copy it in */ 4615 if (!vim_isAbsName(stackp->ffs_fix_path) 4616 && search_ctx->ffsc_start_dir) 4617 { 4618 STRCPY(file_path, search_ctx->ffsc_start_dir); 4619 add_pathsep(file_path); 4620 } 4621 4622 /* append the fix part of the search path */ 4623 STRCAT(file_path, stackp->ffs_fix_path); 4624 add_pathsep(file_path); 4625 4626 #ifdef FEAT_PATH_EXTRA 4627 rest_of_wildcards = stackp->ffs_wc_path; 4628 if (*rest_of_wildcards != NUL) 4629 { 4630 len = (int)STRLEN(file_path); 4631 if (STRNCMP(rest_of_wildcards, "**", 2) == 0) 4632 { 4633 /* pointer to the restrict byte 4634 * The restrict byte is not a character! 4635 */ 4636 p = rest_of_wildcards + 2; 4637 4638 if (*p > 0) 4639 { 4640 (*p)--; 4641 file_path[len++] = '*'; 4642 } 4643 4644 if (*p == 0) 4645 { 4646 /* remove '**<numb> from wildcards */ 4647 STRMOVE(rest_of_wildcards, rest_of_wildcards + 3); 4648 } 4649 else 4650 rest_of_wildcards += 3; 4651 4652 if (stackp->ffs_star_star_empty == 0) 4653 { 4654 /* if not done before, expand '**' to empty */ 4655 stackp->ffs_star_star_empty = 1; 4656 dirptrs[1] = stackp->ffs_fix_path; 4657 } 4658 } 4659 4660 /* 4661 * Here we copy until the next path separator or the end of 4662 * the path. If we stop at a path separator, there is 4663 * still something else left. This is handled below by 4664 * pushing every directory returned from expand_wildcards() 4665 * on the stack again for further search. 4666 */ 4667 while (*rest_of_wildcards 4668 && !vim_ispathsep(*rest_of_wildcards)) 4669 file_path[len++] = *rest_of_wildcards++; 4670 4671 file_path[len] = NUL; 4672 if (vim_ispathsep(*rest_of_wildcards)) 4673 rest_of_wildcards++; 4674 } 4675 #endif 4676 4677 /* 4678 * Expand wildcards like "*" and "$VAR". 4679 * If the path is a URL don't try this. 4680 */ 4681 if (path_with_url(dirptrs[0])) 4682 { 4683 stackp->ffs_filearray = (char_u **) 4684 alloc((unsigned)sizeof(char *)); 4685 if (stackp->ffs_filearray != NULL 4686 && (stackp->ffs_filearray[0] 4687 = vim_strsave(dirptrs[0])) != NULL) 4688 stackp->ffs_filearray_size = 1; 4689 else 4690 stackp->ffs_filearray_size = 0; 4691 } 4692 else 4693 /* Add EW_NOTWILD because the expanded path may contain 4694 * wildcard characters that are to be taken literally. 4695 * This is a bit of a hack. */ 4696 expand_wildcards((dirptrs[1] == NULL) ? 1 : 2, dirptrs, 4697 &stackp->ffs_filearray_size, 4698 &stackp->ffs_filearray, 4699 EW_DIR|EW_ADDSLASH|EW_SILENT|EW_NOTWILD); 4700 4701 stackp->ffs_filearray_cur = 0; 4702 stackp->ffs_stage = 0; 4703 } 4704 #ifdef FEAT_PATH_EXTRA 4705 else 4706 rest_of_wildcards = &stackp->ffs_wc_path[ 4707 STRLEN(stackp->ffs_wc_path)]; 4708 #endif 4709 4710 if (stackp->ffs_stage == 0) 4711 { 4712 /* this is the first time we work on this directory */ 4713 #ifdef FEAT_PATH_EXTRA 4714 if (*rest_of_wildcards == NUL) 4715 #endif 4716 { 4717 /* 4718 * We don't have further wildcards to expand, so we have to 4719 * check for the final file now. 4720 */ 4721 for (i = stackp->ffs_filearray_cur; 4722 i < stackp->ffs_filearray_size; ++i) 4723 { 4724 if (!path_with_url(stackp->ffs_filearray[i]) 4725 && !mch_isdir(stackp->ffs_filearray[i])) 4726 continue; /* not a directory */ 4727 4728 /* prepare the filename to be checked for existence 4729 * below */ 4730 STRCPY(file_path, stackp->ffs_filearray[i]); 4731 add_pathsep(file_path); 4732 STRCAT(file_path, search_ctx->ffsc_file_to_search); 4733 4734 /* 4735 * Try without extra suffix and then with suffixes 4736 * from 'suffixesadd'. 4737 */ 4738 #ifdef FEAT_SEARCHPATH 4739 len = (int)STRLEN(file_path); 4740 if (search_ctx->ffsc_tagfile) 4741 suf = (char_u *)""; 4742 else 4743 suf = curbuf->b_p_sua; 4744 for (;;) 4745 #endif 4746 { 4747 /* if file exists and we didn't already find it */ 4748 if ((path_with_url(file_path) 4749 || (mch_getperm(file_path) >= 0 4750 && (search_ctx->ffsc_find_what 4751 == FINDFILE_BOTH 4752 || ((search_ctx->ffsc_find_what 4753 == FINDFILE_DIR) 4754 == mch_isdir(file_path))))) 4755 #ifndef FF_VERBOSE 4756 && (ff_check_visited( 4757 &search_ctx->ffsc_visited_list->ffvl_visited_list, 4758 file_path 4759 #ifdef FEAT_PATH_EXTRA 4760 , (char_u *)"" 4761 #endif 4762 ) == OK) 4763 #endif 4764 ) 4765 { 4766 #ifdef FF_VERBOSE 4767 if (ff_check_visited( 4768 &search_ctx->ffsc_visited_list->ffvl_visited_list, 4769 file_path 4770 #ifdef FEAT_PATH_EXTRA 4771 , (char_u *)"" 4772 #endif 4773 ) == FAIL) 4774 { 4775 if (p_verbose >= 5) 4776 { 4777 verbose_enter_scroll(); 4778 smsg((char_u *)"Already: %s", 4779 file_path); 4780 /* don't overwrite this either */ 4781 msg_puts((char_u *)"\n"); 4782 verbose_leave_scroll(); 4783 } 4784 continue; 4785 } 4786 #endif 4787 4788 /* push dir to examine rest of subdirs later */ 4789 stackp->ffs_filearray_cur = i + 1; 4790 ff_push(search_ctx, stackp); 4791 4792 if (!path_with_url(file_path)) 4793 simplify_filename(file_path); 4794 if (mch_dirname(ff_expand_buffer, MAXPATHL) 4795 == OK) 4796 { 4797 p = shorten_fname(file_path, 4798 ff_expand_buffer); 4799 if (p != NULL) 4800 STRMOVE(file_path, p); 4801 } 4802 #ifdef FF_VERBOSE 4803 if (p_verbose >= 5) 4804 { 4805 verbose_enter_scroll(); 4806 smsg((char_u *)"HIT: %s", file_path); 4807 /* don't overwrite this either */ 4808 msg_puts((char_u *)"\n"); 4809 verbose_leave_scroll(); 4810 } 4811 #endif 4812 return file_path; 4813 } 4814 4815 #ifdef FEAT_SEARCHPATH 4816 /* Not found or found already, try next suffix. */ 4817 if (*suf == NUL) 4818 break; 4819 copy_option_part(&suf, file_path + len, 4820 MAXPATHL - len, ","); 4821 #endif 4822 } 4823 } 4824 } 4825 #ifdef FEAT_PATH_EXTRA 4826 else 4827 { 4828 /* 4829 * still wildcards left, push the directories for further 4830 * search 4831 */ 4832 for (i = stackp->ffs_filearray_cur; 4833 i < stackp->ffs_filearray_size; ++i) 4834 { 4835 if (!mch_isdir(stackp->ffs_filearray[i])) 4836 continue; /* not a directory */ 4837 4838 ff_push(search_ctx, 4839 ff_create_stack_element( 4840 stackp->ffs_filearray[i], 4841 rest_of_wildcards, 4842 stackp->ffs_level - 1, 0)); 4843 } 4844 } 4845 #endif 4846 stackp->ffs_filearray_cur = 0; 4847 stackp->ffs_stage = 1; 4848 } 4849 4850 #ifdef FEAT_PATH_EXTRA 4851 /* 4852 * if wildcards contains '**' we have to descent till we reach the 4853 * leaves of the directory tree. 4854 */ 4855 if (STRNCMP(stackp->ffs_wc_path, "**", 2) == 0) 4856 { 4857 for (i = stackp->ffs_filearray_cur; 4858 i < stackp->ffs_filearray_size; ++i) 4859 { 4860 if (fnamecmp(stackp->ffs_filearray[i], 4861 stackp->ffs_fix_path) == 0) 4862 continue; /* don't repush same directory */ 4863 if (!mch_isdir(stackp->ffs_filearray[i])) 4864 continue; /* not a directory */ 4865 ff_push(search_ctx, 4866 ff_create_stack_element(stackp->ffs_filearray[i], 4867 stackp->ffs_wc_path, stackp->ffs_level - 1, 1)); 4868 } 4869 } 4870 #endif 4871 4872 /* we are done with the current directory */ 4873 ff_free_stack_element(stackp); 4874 4875 } 4876 4877 #ifdef FEAT_PATH_EXTRA 4878 /* If we reached this, we didn't find anything downwards. 4879 * Let's check if we should do an upward search. 4880 */ 4881 if (search_ctx->ffsc_start_dir 4882 && search_ctx->ffsc_stopdirs_v != NULL && !got_int) 4883 { 4884 ff_stack_T *sptr; 4885 4886 /* is the last starting directory in the stop list? */ 4887 if (ff_path_in_stoplist(search_ctx->ffsc_start_dir, 4888 (int)(path_end - search_ctx->ffsc_start_dir), 4889 search_ctx->ffsc_stopdirs_v) == TRUE) 4890 break; 4891 4892 /* cut of last dir */ 4893 while (path_end > search_ctx->ffsc_start_dir 4894 && vim_ispathsep(*path_end)) 4895 path_end--; 4896 while (path_end > search_ctx->ffsc_start_dir 4897 && !vim_ispathsep(path_end[-1])) 4898 path_end--; 4899 *path_end = 0; 4900 path_end--; 4901 4902 if (*search_ctx->ffsc_start_dir == 0) 4903 break; 4904 4905 STRCPY(file_path, search_ctx->ffsc_start_dir); 4906 add_pathsep(file_path); 4907 STRCAT(file_path, search_ctx->ffsc_fix_path); 4908 4909 /* create a new stack entry */ 4910 sptr = ff_create_stack_element(file_path, 4911 search_ctx->ffsc_wc_path, search_ctx->ffsc_level, 0); 4912 if (sptr == NULL) 4913 break; 4914 ff_push(search_ctx, sptr); 4915 } 4916 else 4917 break; 4918 } 4919 #endif 4920 4921 vim_free(file_path); 4922 return NULL; 4923 } 4924 4925 /* 4926 * Free the list of lists of visited files and directories 4927 * Can handle it if the passed search_context is NULL; 4928 */ 4929 void 4930 vim_findfile_free_visited(search_ctx_arg) 4931 void *search_ctx_arg; 4932 { 4933 ff_search_ctx_T *search_ctx; 4934 4935 if (search_ctx_arg == NULL) 4936 return; 4937 4938 search_ctx = (ff_search_ctx_T *)search_ctx_arg; 4939 vim_findfile_free_visited_list(&search_ctx->ffsc_visited_lists_list); 4940 vim_findfile_free_visited_list(&search_ctx->ffsc_dir_visited_lists_list); 4941 } 4942 4943 static void 4944 vim_findfile_free_visited_list(list_headp) 4945 ff_visited_list_hdr_T **list_headp; 4946 { 4947 ff_visited_list_hdr_T *vp; 4948 4949 while (*list_headp != NULL) 4950 { 4951 vp = (*list_headp)->ffvl_next; 4952 ff_free_visited_list((*list_headp)->ffvl_visited_list); 4953 4954 vim_free((*list_headp)->ffvl_filename); 4955 vim_free(*list_headp); 4956 *list_headp = vp; 4957 } 4958 *list_headp = NULL; 4959 } 4960 4961 static void 4962 ff_free_visited_list(vl) 4963 ff_visited_T *vl; 4964 { 4965 ff_visited_T *vp; 4966 4967 while (vl != NULL) 4968 { 4969 vp = vl->ffv_next; 4970 #ifdef FEAT_PATH_EXTRA 4971 vim_free(vl->ffv_wc_path); 4972 #endif 4973 vim_free(vl); 4974 vl = vp; 4975 } 4976 vl = NULL; 4977 } 4978 4979 /* 4980 * Returns the already visited list for the given filename. If none is found it 4981 * allocates a new one. 4982 */ 4983 static ff_visited_list_hdr_T* 4984 ff_get_visited_list(filename, list_headp) 4985 char_u *filename; 4986 ff_visited_list_hdr_T **list_headp; 4987 { 4988 ff_visited_list_hdr_T *retptr = NULL; 4989 4990 /* check if a visited list for the given filename exists */ 4991 if (*list_headp != NULL) 4992 { 4993 retptr = *list_headp; 4994 while (retptr != NULL) 4995 { 4996 if (fnamecmp(filename, retptr->ffvl_filename) == 0) 4997 { 4998 #ifdef FF_VERBOSE 4999 if (p_verbose >= 5) 5000 { 5001 verbose_enter_scroll(); 5002 smsg((char_u *)"ff_get_visited_list: FOUND list for %s", 5003 filename); 5004 /* don't overwrite this either */ 5005 msg_puts((char_u *)"\n"); 5006 verbose_leave_scroll(); 5007 } 5008 #endif 5009 return retptr; 5010 } 5011 retptr = retptr->ffvl_next; 5012 } 5013 } 5014 5015 #ifdef FF_VERBOSE 5016 if (p_verbose >= 5) 5017 { 5018 verbose_enter_scroll(); 5019 smsg((char_u *)"ff_get_visited_list: new list for %s", filename); 5020 /* don't overwrite this either */ 5021 msg_puts((char_u *)"\n"); 5022 verbose_leave_scroll(); 5023 } 5024 #endif 5025 5026 /* 5027 * if we reach this we didn't find a list and we have to allocate new list 5028 */ 5029 retptr = (ff_visited_list_hdr_T*)alloc((unsigned)sizeof(*retptr)); 5030 if (retptr == NULL) 5031 return NULL; 5032 5033 retptr->ffvl_visited_list = NULL; 5034 retptr->ffvl_filename = vim_strsave(filename); 5035 if (retptr->ffvl_filename == NULL) 5036 { 5037 vim_free(retptr); 5038 return NULL; 5039 } 5040 retptr->ffvl_next = *list_headp; 5041 *list_headp = retptr; 5042 5043 return retptr; 5044 } 5045 5046 #ifdef FEAT_PATH_EXTRA 5047 /* 5048 * check if two wildcard paths are equal. Returns TRUE or FALSE. 5049 * They are equal if: 5050 * - both paths are NULL 5051 * - they have the same length 5052 * - char by char comparison is OK 5053 * - the only differences are in the counters behind a '**', so 5054 * '**\20' is equal to '**\24' 5055 */ 5056 static int 5057 ff_wc_equal(s1, s2) 5058 char_u *s1; 5059 char_u *s2; 5060 { 5061 int i; 5062 int prev1 = NUL; 5063 int prev2 = NUL; 5064 5065 if (s1 == s2) 5066 return TRUE; 5067 5068 if (s1 == NULL || s2 == NULL) 5069 return FALSE; 5070 5071 if (STRLEN(s1) != STRLEN(s2)) 5072 return FAIL; 5073 5074 for (i = 0; s1[i] != NUL && s2[i] != NUL; i += MB_PTR2LEN(s1 + i)) 5075 { 5076 int c1 = PTR2CHAR(s1 + i); 5077 int c2 = PTR2CHAR(s2 + i); 5078 5079 if ((p_fic ? MB_TOLOWER(c1) != MB_TOLOWER(c2) : c1 != c2) 5080 && (prev1 != '*' || prev2 != '*')) 5081 return FAIL; 5082 prev2 = prev1; 5083 prev1 = c1; 5084 } 5085 return TRUE; 5086 } 5087 #endif 5088 5089 /* 5090 * maintains the list of already visited files and dirs 5091 * returns FAIL if the given file/dir is already in the list 5092 * returns OK if it is newly added 5093 * 5094 * TODO: What to do on memory allocation problems? 5095 * -> return TRUE - Better the file is found several times instead of 5096 * never. 5097 */ 5098 static int 5099 ff_check_visited(visited_list, fname 5100 #ifdef FEAT_PATH_EXTRA 5101 , wc_path 5102 #endif 5103 ) 5104 ff_visited_T **visited_list; 5105 char_u *fname; 5106 #ifdef FEAT_PATH_EXTRA 5107 char_u *wc_path; 5108 #endif 5109 { 5110 ff_visited_T *vp; 5111 #ifdef UNIX 5112 struct stat st; 5113 int url = FALSE; 5114 #endif 5115 5116 /* For an URL we only compare the name, otherwise we compare the 5117 * device/inode (unix) or the full path name (not Unix). */ 5118 if (path_with_url(fname)) 5119 { 5120 vim_strncpy(ff_expand_buffer, fname, MAXPATHL - 1); 5121 #ifdef UNIX 5122 url = TRUE; 5123 #endif 5124 } 5125 else 5126 { 5127 ff_expand_buffer[0] = NUL; 5128 #ifdef UNIX 5129 if (mch_stat((char *)fname, &st) < 0) 5130 #else 5131 if (vim_FullName(fname, ff_expand_buffer, MAXPATHL, TRUE) == FAIL) 5132 #endif 5133 return FAIL; 5134 } 5135 5136 /* check against list of already visited files */ 5137 for (vp = *visited_list; vp != NULL; vp = vp->ffv_next) 5138 { 5139 if ( 5140 #ifdef UNIX 5141 !url ? (vp->ffv_dev_valid && vp->ffv_dev == st.st_dev 5142 && vp->ffv_ino == st.st_ino) 5143 : 5144 #endif 5145 fnamecmp(vp->ffv_fname, ff_expand_buffer) == 0 5146 ) 5147 { 5148 #ifdef FEAT_PATH_EXTRA 5149 /* are the wildcard parts equal */ 5150 if (ff_wc_equal(vp->ffv_wc_path, wc_path) == TRUE) 5151 #endif 5152 /* already visited */ 5153 return FAIL; 5154 } 5155 } 5156 5157 /* 5158 * New file/dir. Add it to the list of visited files/dirs. 5159 */ 5160 vp = (ff_visited_T *)alloc((unsigned)(sizeof(ff_visited_T) 5161 + STRLEN(ff_expand_buffer))); 5162 5163 if (vp != NULL) 5164 { 5165 #ifdef UNIX 5166 if (!url) 5167 { 5168 vp->ffv_dev_valid = TRUE; 5169 vp->ffv_ino = st.st_ino; 5170 vp->ffv_dev = st.st_dev; 5171 vp->ffv_fname[0] = NUL; 5172 } 5173 else 5174 { 5175 vp->ffv_dev_valid = FALSE; 5176 #endif 5177 STRCPY(vp->ffv_fname, ff_expand_buffer); 5178 #ifdef UNIX 5179 } 5180 #endif 5181 #ifdef FEAT_PATH_EXTRA 5182 if (wc_path != NULL) 5183 vp->ffv_wc_path = vim_strsave(wc_path); 5184 else 5185 vp->ffv_wc_path = NULL; 5186 #endif 5187 5188 vp->ffv_next = *visited_list; 5189 *visited_list = vp; 5190 } 5191 5192 return OK; 5193 } 5194 5195 /* 5196 * create stack element from given path pieces 5197 */ 5198 static ff_stack_T * 5199 ff_create_stack_element(fix_part, 5200 #ifdef FEAT_PATH_EXTRA 5201 wc_part, 5202 #endif 5203 level, star_star_empty) 5204 char_u *fix_part; 5205 #ifdef FEAT_PATH_EXTRA 5206 char_u *wc_part; 5207 #endif 5208 int level; 5209 int star_star_empty; 5210 { 5211 ff_stack_T *new; 5212 5213 new = (ff_stack_T *)alloc((unsigned)sizeof(ff_stack_T)); 5214 if (new == NULL) 5215 return NULL; 5216 5217 new->ffs_prev = NULL; 5218 new->ffs_filearray = NULL; 5219 new->ffs_filearray_size = 0; 5220 new->ffs_filearray_cur = 0; 5221 new->ffs_stage = 0; 5222 new->ffs_level = level; 5223 new->ffs_star_star_empty = star_star_empty;; 5224 5225 /* the following saves NULL pointer checks in vim_findfile */ 5226 if (fix_part == NULL) 5227 fix_part = (char_u *)""; 5228 new->ffs_fix_path = vim_strsave(fix_part); 5229 5230 #ifdef FEAT_PATH_EXTRA 5231 if (wc_part == NULL) 5232 wc_part = (char_u *)""; 5233 new->ffs_wc_path = vim_strsave(wc_part); 5234 #endif 5235 5236 if (new->ffs_fix_path == NULL 5237 #ifdef FEAT_PATH_EXTRA 5238 || new->ffs_wc_path == NULL 5239 #endif 5240 ) 5241 { 5242 ff_free_stack_element(new); 5243 new = NULL; 5244 } 5245 5246 return new; 5247 } 5248 5249 /* 5250 * Push a dir on the directory stack. 5251 */ 5252 static void 5253 ff_push(search_ctx, stack_ptr) 5254 ff_search_ctx_T *search_ctx; 5255 ff_stack_T *stack_ptr; 5256 { 5257 /* check for NULL pointer, not to return an error to the user, but 5258 * to prevent a crash */ 5259 if (stack_ptr != NULL) 5260 { 5261 stack_ptr->ffs_prev = search_ctx->ffsc_stack_ptr; 5262 search_ctx->ffsc_stack_ptr = stack_ptr; 5263 } 5264 } 5265 5266 /* 5267 * Pop a dir from the directory stack. 5268 * Returns NULL if stack is empty. 5269 */ 5270 static ff_stack_T * 5271 ff_pop(search_ctx) 5272 ff_search_ctx_T *search_ctx; 5273 { 5274 ff_stack_T *sptr; 5275 5276 sptr = search_ctx->ffsc_stack_ptr; 5277 if (search_ctx->ffsc_stack_ptr != NULL) 5278 search_ctx->ffsc_stack_ptr = search_ctx->ffsc_stack_ptr->ffs_prev; 5279 5280 return sptr; 5281 } 5282 5283 /* 5284 * free the given stack element 5285 */ 5286 static void 5287 ff_free_stack_element(stack_ptr) 5288 ff_stack_T *stack_ptr; 5289 { 5290 /* vim_free handles possible NULL pointers */ 5291 vim_free(stack_ptr->ffs_fix_path); 5292 #ifdef FEAT_PATH_EXTRA 5293 vim_free(stack_ptr->ffs_wc_path); 5294 #endif 5295 5296 if (stack_ptr->ffs_filearray != NULL) 5297 FreeWild(stack_ptr->ffs_filearray_size, stack_ptr->ffs_filearray); 5298 5299 vim_free(stack_ptr); 5300 } 5301 5302 /* 5303 * Clear the search context, but NOT the visited list. 5304 */ 5305 static void 5306 ff_clear(search_ctx) 5307 ff_search_ctx_T *search_ctx; 5308 { 5309 ff_stack_T *sptr; 5310 5311 /* clear up stack */ 5312 while ((sptr = ff_pop(search_ctx)) != NULL) 5313 ff_free_stack_element(sptr); 5314 5315 vim_free(search_ctx->ffsc_file_to_search); 5316 vim_free(search_ctx->ffsc_start_dir); 5317 vim_free(search_ctx->ffsc_fix_path); 5318 #ifdef FEAT_PATH_EXTRA 5319 vim_free(search_ctx->ffsc_wc_path); 5320 #endif 5321 5322 #ifdef FEAT_PATH_EXTRA 5323 if (search_ctx->ffsc_stopdirs_v != NULL) 5324 { 5325 int i = 0; 5326 5327 while (search_ctx->ffsc_stopdirs_v[i] != NULL) 5328 { 5329 vim_free(search_ctx->ffsc_stopdirs_v[i]); 5330 i++; 5331 } 5332 vim_free(search_ctx->ffsc_stopdirs_v); 5333 } 5334 search_ctx->ffsc_stopdirs_v = NULL; 5335 #endif 5336 5337 /* reset everything */ 5338 search_ctx->ffsc_file_to_search = NULL; 5339 search_ctx->ffsc_start_dir = NULL; 5340 search_ctx->ffsc_fix_path = NULL; 5341 #ifdef FEAT_PATH_EXTRA 5342 search_ctx->ffsc_wc_path = NULL; 5343 search_ctx->ffsc_level = 0; 5344 #endif 5345 } 5346 5347 #ifdef FEAT_PATH_EXTRA 5348 /* 5349 * check if the given path is in the stopdirs 5350 * returns TRUE if yes else FALSE 5351 */ 5352 static int 5353 ff_path_in_stoplist(path, path_len, stopdirs_v) 5354 char_u *path; 5355 int path_len; 5356 char_u **stopdirs_v; 5357 { 5358 int i = 0; 5359 5360 /* eat up trailing path separators, except the first */ 5361 while (path_len > 1 && vim_ispathsep(path[path_len - 1])) 5362 path_len--; 5363 5364 /* if no path consider it as match */ 5365 if (path_len == 0) 5366 return TRUE; 5367 5368 for (i = 0; stopdirs_v[i] != NULL; i++) 5369 { 5370 if ((int)STRLEN(stopdirs_v[i]) > path_len) 5371 { 5372 /* match for parent directory. So '/home' also matches 5373 * '/home/rks'. Check for PATHSEP in stopdirs_v[i], else 5374 * '/home/r' would also match '/home/rks' 5375 */ 5376 if (fnamencmp(stopdirs_v[i], path, path_len) == 0 5377 && vim_ispathsep(stopdirs_v[i][path_len])) 5378 return TRUE; 5379 } 5380 else 5381 { 5382 if (fnamecmp(stopdirs_v[i], path) == 0) 5383 return TRUE; 5384 } 5385 } 5386 return FALSE; 5387 } 5388 #endif 5389 5390 #if defined(FEAT_SEARCHPATH) || defined(PROTO) 5391 /* 5392 * Find the file name "ptr[len]" in the path. Also finds directory names. 5393 * 5394 * On the first call set the parameter 'first' to TRUE to initialize 5395 * the search. For repeating calls to FALSE. 5396 * 5397 * Repeating calls will return other files called 'ptr[len]' from the path. 5398 * 5399 * Only on the first call 'ptr' and 'len' are used. For repeating calls they 5400 * don't need valid values. 5401 * 5402 * If nothing found on the first call the option FNAME_MESS will issue the 5403 * message: 5404 * 'Can't find file "<file>" in path' 5405 * On repeating calls: 5406 * 'No more file "<file>" found in path' 5407 * 5408 * options: 5409 * FNAME_MESS give error message when not found 5410 * 5411 * Uses NameBuff[]! 5412 * 5413 * Returns an allocated string for the file name. NULL for error. 5414 * 5415 */ 5416 char_u * 5417 find_file_in_path(ptr, len, options, first, rel_fname) 5418 char_u *ptr; /* file name */ 5419 int len; /* length of file name */ 5420 int options; 5421 int first; /* use count'th matching file name */ 5422 char_u *rel_fname; /* file name searching relative to */ 5423 { 5424 return find_file_in_path_option(ptr, len, options, first, 5425 *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path, 5426 FINDFILE_BOTH, rel_fname, curbuf->b_p_sua); 5427 } 5428 5429 static char_u *ff_file_to_find = NULL; 5430 static void *fdip_search_ctx = NULL; 5431 5432 #if defined(EXITFREE) 5433 static void 5434 free_findfile() 5435 { 5436 vim_free(ff_file_to_find); 5437 vim_findfile_cleanup(fdip_search_ctx); 5438 } 5439 #endif 5440 5441 /* 5442 * Find the directory name "ptr[len]" in the path. 5443 * 5444 * options: 5445 * FNAME_MESS give error message when not found 5446 * FNAME_UNESC unescape backslashes. 5447 * 5448 * Uses NameBuff[]! 5449 * 5450 * Returns an allocated string for the file name. NULL for error. 5451 */ 5452 char_u * 5453 find_directory_in_path(ptr, len, options, rel_fname) 5454 char_u *ptr; /* file name */ 5455 int len; /* length of file name */ 5456 int options; 5457 char_u *rel_fname; /* file name searching relative to */ 5458 { 5459 return find_file_in_path_option(ptr, len, options, TRUE, p_cdpath, 5460 FINDFILE_DIR, rel_fname, (char_u *)""); 5461 } 5462 5463 char_u * 5464 find_file_in_path_option(ptr, len, options, first, path_option, 5465 find_what, rel_fname, suffixes) 5466 char_u *ptr; /* file name */ 5467 int len; /* length of file name */ 5468 int options; 5469 int first; /* use count'th matching file name */ 5470 char_u *path_option; /* p_path or p_cdpath */ 5471 int find_what; /* FINDFILE_FILE, _DIR or _BOTH */ 5472 char_u *rel_fname; /* file name we are looking relative to. */ 5473 char_u *suffixes; /* list of suffixes, 'suffixesadd' option */ 5474 { 5475 static char_u *dir; 5476 static int did_findfile_init = FALSE; 5477 char_u save_char; 5478 char_u *file_name = NULL; 5479 char_u *buf = NULL; 5480 int rel_to_curdir; 5481 #ifdef AMIGA 5482 struct Process *proc = (struct Process *)FindTask(0L); 5483 APTR save_winptr = proc->pr_WindowPtr; 5484 5485 /* Avoid a requester here for a volume that doesn't exist. */ 5486 proc->pr_WindowPtr = (APTR)-1L; 5487 #endif 5488 5489 if (first == TRUE) 5490 { 5491 /* copy file name into NameBuff, expanding environment variables */ 5492 save_char = ptr[len]; 5493 ptr[len] = NUL; 5494 expand_env(ptr, NameBuff, MAXPATHL); 5495 ptr[len] = save_char; 5496 5497 vim_free(ff_file_to_find); 5498 ff_file_to_find = vim_strsave(NameBuff); 5499 if (ff_file_to_find == NULL) /* out of memory */ 5500 { 5501 file_name = NULL; 5502 goto theend; 5503 } 5504 if (options & FNAME_UNESC) 5505 { 5506 /* Change all "\ " to " ". */ 5507 for (ptr = ff_file_to_find; *ptr != NUL; ++ptr) 5508 if (ptr[0] == '\\' && ptr[1] == ' ') 5509 mch_memmove(ptr, ptr + 1, STRLEN(ptr)); 5510 } 5511 } 5512 5513 rel_to_curdir = (ff_file_to_find[0] == '.' 5514 && (ff_file_to_find[1] == NUL 5515 || vim_ispathsep(ff_file_to_find[1]) 5516 || (ff_file_to_find[1] == '.' 5517 && (ff_file_to_find[2] == NUL 5518 || vim_ispathsep(ff_file_to_find[2]))))); 5519 if (vim_isAbsName(ff_file_to_find) 5520 /* "..", "../path", "." and "./path": don't use the path_option */ 5521 || rel_to_curdir 5522 #if defined(MSWIN) || defined(MSDOS) || defined(OS2) 5523 /* handle "\tmp" as absolute path */ 5524 || vim_ispathsep(ff_file_to_find[0]) 5525 /* handle "c:name" as absolute path */ 5526 || (ff_file_to_find[0] != NUL && ff_file_to_find[1] == ':') 5527 #endif 5528 #ifdef AMIGA 5529 /* handle ":tmp" as absolute path */ 5530 || ff_file_to_find[0] == ':' 5531 #endif 5532 ) 5533 { 5534 /* 5535 * Absolute path, no need to use "path_option". 5536 * If this is not a first call, return NULL. We already returned a 5537 * filename on the first call. 5538 */ 5539 if (first == TRUE) 5540 { 5541 int l; 5542 int run; 5543 5544 if (path_with_url(ff_file_to_find)) 5545 { 5546 file_name = vim_strsave(ff_file_to_find); 5547 goto theend; 5548 } 5549 5550 /* When FNAME_REL flag given first use the directory of the file. 5551 * Otherwise or when this fails use the current directory. */ 5552 for (run = 1; run <= 2; ++run) 5553 { 5554 l = (int)STRLEN(ff_file_to_find); 5555 if (run == 1 5556 && rel_to_curdir 5557 && (options & FNAME_REL) 5558 && rel_fname != NULL 5559 && STRLEN(rel_fname) + l < MAXPATHL) 5560 { 5561 STRCPY(NameBuff, rel_fname); 5562 STRCPY(gettail(NameBuff), ff_file_to_find); 5563 l = (int)STRLEN(NameBuff); 5564 } 5565 else 5566 { 5567 STRCPY(NameBuff, ff_file_to_find); 5568 run = 2; 5569 } 5570 5571 /* When the file doesn't exist, try adding parts of 5572 * 'suffixesadd'. */ 5573 buf = suffixes; 5574 for (;;) 5575 { 5576 if ( 5577 #ifdef DJGPP 5578 /* "C:" by itself will fail for mch_getperm(), 5579 * assume it's always valid. */ 5580 (find_what != FINDFILE_FILE && NameBuff[0] != NUL 5581 && NameBuff[1] == ':' 5582 && NameBuff[2] == NUL) || 5583 #endif 5584 (mch_getperm(NameBuff) >= 0 5585 && (find_what == FINDFILE_BOTH 5586 || ((find_what == FINDFILE_DIR) 5587 == mch_isdir(NameBuff))))) 5588 { 5589 file_name = vim_strsave(NameBuff); 5590 goto theend; 5591 } 5592 if (*buf == NUL) 5593 break; 5594 copy_option_part(&buf, NameBuff + l, MAXPATHL - l, ","); 5595 } 5596 } 5597 } 5598 } 5599 else 5600 { 5601 /* 5602 * Loop over all paths in the 'path' or 'cdpath' option. 5603 * When "first" is set, first setup to the start of the option. 5604 * Otherwise continue to find the next match. 5605 */ 5606 if (first == TRUE) 5607 { 5608 /* vim_findfile_free_visited can handle a possible NULL pointer */ 5609 vim_findfile_free_visited(fdip_search_ctx); 5610 dir = path_option; 5611 did_findfile_init = FALSE; 5612 } 5613 5614 for (;;) 5615 { 5616 if (did_findfile_init) 5617 { 5618 file_name = vim_findfile(fdip_search_ctx); 5619 if (file_name != NULL) 5620 break; 5621 5622 did_findfile_init = FALSE; 5623 } 5624 else 5625 { 5626 char_u *r_ptr; 5627 5628 if (dir == NULL || *dir == NUL) 5629 { 5630 /* We searched all paths of the option, now we can 5631 * free the search context. */ 5632 vim_findfile_cleanup(fdip_search_ctx); 5633 fdip_search_ctx = NULL; 5634 break; 5635 } 5636 5637 if ((buf = alloc((int)(MAXPATHL))) == NULL) 5638 break; 5639 5640 /* copy next path */ 5641 buf[0] = 0; 5642 copy_option_part(&dir, buf, MAXPATHL, " ,"); 5643 5644 #ifdef FEAT_PATH_EXTRA 5645 /* get the stopdir string */ 5646 r_ptr = vim_findfile_stopdir(buf); 5647 #else 5648 r_ptr = NULL; 5649 #endif 5650 fdip_search_ctx = vim_findfile_init(buf, ff_file_to_find, 5651 r_ptr, 100, FALSE, find_what, 5652 fdip_search_ctx, FALSE, rel_fname); 5653 if (fdip_search_ctx != NULL) 5654 did_findfile_init = TRUE; 5655 vim_free(buf); 5656 } 5657 } 5658 } 5659 if (file_name == NULL && (options & FNAME_MESS)) 5660 { 5661 if (first == TRUE) 5662 { 5663 if (find_what == FINDFILE_DIR) 5664 EMSG2(_("E344: Can't find directory \"%s\" in cdpath"), 5665 ff_file_to_find); 5666 else 5667 EMSG2(_("E345: Can't find file \"%s\" in path"), 5668 ff_file_to_find); 5669 } 5670 else 5671 { 5672 if (find_what == FINDFILE_DIR) 5673 EMSG2(_("E346: No more directory \"%s\" found in cdpath"), 5674 ff_file_to_find); 5675 else 5676 EMSG2(_("E347: No more file \"%s\" found in path"), 5677 ff_file_to_find); 5678 } 5679 } 5680 5681 theend: 5682 #ifdef AMIGA 5683 proc->pr_WindowPtr = save_winptr; 5684 #endif 5685 return file_name; 5686 } 5687 5688 #endif /* FEAT_SEARCHPATH */ 5689 5690 /* 5691 * Change directory to "new_dir". If FEAT_SEARCHPATH is defined, search 5692 * 'cdpath' for relative directory names, otherwise just mch_chdir(). 5693 */ 5694 int 5695 vim_chdir(new_dir) 5696 char_u *new_dir; 5697 { 5698 #ifndef FEAT_SEARCHPATH 5699 return mch_chdir((char *)new_dir); 5700 #else 5701 char_u *dir_name; 5702 int r; 5703 5704 dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir), 5705 FNAME_MESS, curbuf->b_ffname); 5706 if (dir_name == NULL) 5707 return -1; 5708 r = mch_chdir((char *)dir_name); 5709 vim_free(dir_name); 5710 return r; 5711 #endif 5712 } 5713 5714 /* 5715 * Get user name from machine-specific function. 5716 * Returns the user name in "buf[len]". 5717 * Some systems are quite slow in obtaining the user name (Windows NT), thus 5718 * cache the result. 5719 * Returns OK or FAIL. 5720 */ 5721 int 5722 get_user_name(buf, len) 5723 char_u *buf; 5724 int len; 5725 { 5726 if (username == NULL) 5727 { 5728 if (mch_get_user_name(buf, len) == FAIL) 5729 return FAIL; 5730 username = vim_strsave(buf); 5731 } 5732 else 5733 vim_strncpy(buf, username, len - 1); 5734 return OK; 5735 } 5736 5737 #ifndef HAVE_QSORT 5738 /* 5739 * Our own qsort(), for systems that don't have it. 5740 * It's simple and slow. From the K&R C book. 5741 */ 5742 void 5743 qsort(base, elm_count, elm_size, cmp) 5744 void *base; 5745 size_t elm_count; 5746 size_t elm_size; 5747 int (*cmp) __ARGS((const void *, const void *)); 5748 { 5749 char_u *buf; 5750 char_u *p1; 5751 char_u *p2; 5752 int i, j; 5753 int gap; 5754 5755 buf = alloc((unsigned)elm_size); 5756 if (buf == NULL) 5757 return; 5758 5759 for (gap = elm_count / 2; gap > 0; gap /= 2) 5760 for (i = gap; i < elm_count; ++i) 5761 for (j = i - gap; j >= 0; j -= gap) 5762 { 5763 /* Compare the elements. */ 5764 p1 = (char_u *)base + j * elm_size; 5765 p2 = (char_u *)base + (j + gap) * elm_size; 5766 if ((*cmp)((void *)p1, (void *)p2) <= 0) 5767 break; 5768 /* Exchange the elements. */ 5769 mch_memmove(buf, p1, elm_size); 5770 mch_memmove(p1, p2, elm_size); 5771 mch_memmove(p2, buf, elm_size); 5772 } 5773 5774 vim_free(buf); 5775 } 5776 #endif 5777 5778 /* 5779 * Sort an array of strings. 5780 */ 5781 static int 5782 #ifdef __BORLANDC__ 5783 _RTLENTRYF 5784 #endif 5785 sort_compare __ARGS((const void *s1, const void *s2)); 5786 5787 static int 5788 #ifdef __BORLANDC__ 5789 _RTLENTRYF 5790 #endif 5791 sort_compare(s1, s2) 5792 const void *s1; 5793 const void *s2; 5794 { 5795 return STRCMP(*(char **)s1, *(char **)s2); 5796 } 5797 5798 void 5799 sort_strings(files, count) 5800 char_u **files; 5801 int count; 5802 { 5803 qsort((void *)files, (size_t)count, sizeof(char_u *), sort_compare); 5804 } 5805 5806 #if !defined(NO_EXPANDPATH) || defined(PROTO) 5807 /* 5808 * Compare path "p[]" to "q[]". 5809 * If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]" 5810 * Return value like strcmp(p, q), but consider path separators. 5811 */ 5812 int 5813 pathcmp(p, q, maxlen) 5814 const char *p, *q; 5815 int maxlen; 5816 { 5817 int i; 5818 int c1, c2; 5819 const char *s = NULL; 5820 5821 for (i = 0; maxlen < 0 || i < maxlen; i += MB_PTR2LEN((char_u *)p + i)) 5822 { 5823 c1 = PTR2CHAR((char_u *)p + i); 5824 c2 = PTR2CHAR((char_u *)q + i); 5825 5826 /* End of "p": check if "q" also ends or just has a slash. */ 5827 if (c1 == NUL) 5828 { 5829 if (c2 == NUL) /* full match */ 5830 return 0; 5831 s = q; 5832 break; 5833 } 5834 5835 /* End of "q": check if "p" just has a slash. */ 5836 if (c2 == NUL) 5837 { 5838 s = p; 5839 break; 5840 } 5841 5842 if ((p_fic ? MB_TOUPPER(c1) != MB_TOUPPER(c2) : c1 != c2) 5843 #ifdef BACKSLASH_IN_FILENAME 5844 /* consider '/' and '\\' to be equal */ 5845 && !((c1 == '/' && c2 == '\\') 5846 || (c1 == '\\' && c2 == '/')) 5847 #endif 5848 ) 5849 { 5850 if (vim_ispathsep(c1)) 5851 return -1; 5852 if (vim_ispathsep(c2)) 5853 return 1; 5854 return p_fic ? MB_TOUPPER(c1) - MB_TOUPPER(c2) 5855 : c1 - c2; /* no match */ 5856 } 5857 } 5858 if (s == NULL) /* "i" ran into "maxlen" */ 5859 return 0; 5860 5861 c1 = PTR2CHAR((char_u *)s + i); 5862 c2 = PTR2CHAR((char_u *)s + i + MB_PTR2LEN((char_u *)s + i)); 5863 /* ignore a trailing slash, but not "//" or ":/" */ 5864 if (c2 == NUL 5865 && i > 0 5866 && !after_pathsep((char_u *)s, (char_u *)s + i) 5867 #ifdef BACKSLASH_IN_FILENAME 5868 && (c1 == '/' || c1 == '\\') 5869 #else 5870 && c1 == '/' 5871 #endif 5872 ) 5873 return 0; /* match with trailing slash */ 5874 if (s == q) 5875 return -1; /* no match */ 5876 return 1; 5877 } 5878 #endif 5879 5880 /* 5881 * The putenv() implementation below comes from the "screen" program. 5882 * Included with permission from Juergen Weigert. 5883 * See pty.c for the copyright notice. 5884 */ 5885 5886 /* 5887 * putenv -- put value into environment 5888 * 5889 * Usage: i = putenv (string) 5890 * int i; 5891 * char *string; 5892 * 5893 * where string is of the form <name>=<value>. 5894 * Putenv returns 0 normally, -1 on error (not enough core for malloc). 5895 * 5896 * Putenv may need to add a new name into the environment, or to 5897 * associate a value longer than the current value with a particular 5898 * name. So, to make life simpler, putenv() copies your entire 5899 * environment into the heap (i.e. malloc()) from the stack 5900 * (i.e. where it resides when your process is initiated) the first 5901 * time you call it. 5902 * 5903 * (history removed, not very interesting. See the "screen" sources.) 5904 */ 5905 5906 #if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV) 5907 5908 #define EXTRASIZE 5 /* increment to add to env. size */ 5909 5910 static int envsize = -1; /* current size of environment */ 5911 #ifndef MACOS_CLASSIC 5912 extern 5913 #endif 5914 char **environ; /* the global which is your env. */ 5915 5916 static int findenv __ARGS((char *name)); /* look for a name in the env. */ 5917 static int newenv __ARGS((void)); /* copy env. from stack to heap */ 5918 static int moreenv __ARGS((void)); /* incr. size of env. */ 5919 5920 int 5921 putenv(string) 5922 const char *string; 5923 { 5924 int i; 5925 char *p; 5926 5927 if (envsize < 0) 5928 { /* first time putenv called */ 5929 if (newenv() < 0) /* copy env. to heap */ 5930 return -1; 5931 } 5932 5933 i = findenv((char *)string); /* look for name in environment */ 5934 5935 if (i < 0) 5936 { /* name must be added */ 5937 for (i = 0; environ[i]; i++); 5938 if (i >= (envsize - 1)) 5939 { /* need new slot */ 5940 if (moreenv() < 0) 5941 return -1; 5942 } 5943 p = (char *)alloc((unsigned)(strlen(string) + 1)); 5944 if (p == NULL) /* not enough core */ 5945 return -1; 5946 environ[i + 1] = 0; /* new end of env. */ 5947 } 5948 else 5949 { /* name already in env. */ 5950 p = vim_realloc(environ[i], strlen(string) + 1); 5951 if (p == NULL) 5952 return -1; 5953 } 5954 sprintf(p, "%s", string); /* copy into env. */ 5955 environ[i] = p; 5956 5957 return 0; 5958 } 5959 5960 static int 5961 findenv(name) 5962 char *name; 5963 { 5964 char *namechar, *envchar; 5965 int i, found; 5966 5967 found = 0; 5968 for (i = 0; environ[i] && !found; i++) 5969 { 5970 envchar = environ[i]; 5971 namechar = name; 5972 while (*namechar && *namechar != '=' && (*namechar == *envchar)) 5973 { 5974 namechar++; 5975 envchar++; 5976 } 5977 found = ((*namechar == '\0' || *namechar == '=') && *envchar == '='); 5978 } 5979 return found ? i - 1 : -1; 5980 } 5981 5982 static int 5983 newenv() 5984 { 5985 char **env, *elem; 5986 int i, esize; 5987 5988 #ifdef MACOS 5989 /* for Mac a new, empty environment is created */ 5990 i = 0; 5991 #else 5992 for (i = 0; environ[i]; i++) 5993 ; 5994 #endif 5995 esize = i + EXTRASIZE + 1; 5996 env = (char **)alloc((unsigned)(esize * sizeof (elem))); 5997 if (env == NULL) 5998 return -1; 5999 6000 #ifndef MACOS 6001 for (i = 0; environ[i]; i++) 6002 { 6003 elem = (char *)alloc((unsigned)(strlen(environ[i]) + 1)); 6004 if (elem == NULL) 6005 return -1; 6006 env[i] = elem; 6007 strcpy(elem, environ[i]); 6008 } 6009 #endif 6010 6011 env[i] = 0; 6012 environ = env; 6013 envsize = esize; 6014 return 0; 6015 } 6016 6017 static int 6018 moreenv() 6019 { 6020 int esize; 6021 char **env; 6022 6023 esize = envsize + EXTRASIZE; 6024 env = (char **)vim_realloc((char *)environ, esize * sizeof (*env)); 6025 if (env == 0) 6026 return -1; 6027 environ = env; 6028 envsize = esize; 6029 return 0; 6030 } 6031 6032 # ifdef USE_VIMPTY_GETENV 6033 char_u * 6034 vimpty_getenv(string) 6035 const char_u *string; 6036 { 6037 int i; 6038 char_u *p; 6039 6040 if (envsize < 0) 6041 return NULL; 6042 6043 i = findenv((char *)string); 6044 6045 if (i < 0) 6046 return NULL; 6047 6048 p = vim_strchr((char_u *)environ[i], '='); 6049 return (p + 1); 6050 } 6051 # endif 6052 6053 #endif /* !defined(HAVE_SETENV) && !defined(HAVE_PUTENV) */ 6054 6055 #if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO) 6056 /* 6057 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have 6058 * rights to write into. 6059 */ 6060 int 6061 filewritable(fname) 6062 char_u *fname; 6063 { 6064 int retval = 0; 6065 #if defined(UNIX) || defined(VMS) 6066 int perm = 0; 6067 #endif 6068 6069 #if defined(UNIX) || defined(VMS) 6070 perm = mch_getperm(fname); 6071 #endif 6072 #ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */ 6073 if ( 6074 # ifdef WIN3264 6075 mch_writable(fname) && 6076 # else 6077 # if defined(UNIX) || defined(VMS) 6078 (perm & 0222) && 6079 # endif 6080 # endif 6081 mch_access((char *)fname, W_OK) == 0 6082 ) 6083 #endif 6084 { 6085 ++retval; 6086 if (mch_isdir(fname)) 6087 ++retval; 6088 } 6089 return retval; 6090 } 6091 #endif 6092 6093 /* 6094 * Print an error message with one or two "%s" and one or two string arguments. 6095 * This is not in message.c to avoid a warning for prototypes. 6096 */ 6097 int 6098 emsg3(s, a1, a2) 6099 char_u *s, *a1, *a2; 6100 { 6101 if (emsg_not_now()) 6102 return TRUE; /* no error messages at the moment */ 6103 #ifdef HAVE_STDARG_H 6104 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, a1, a2); 6105 #else 6106 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, (long_u)a1, (long_u)a2); 6107 #endif 6108 return emsg(IObuff); 6109 } 6110 6111 /* 6112 * Print an error message with one "%ld" and one long int argument. 6113 * This is not in message.c to avoid a warning for prototypes. 6114 */ 6115 int 6116 emsgn(s, n) 6117 char_u *s; 6118 long n; 6119 { 6120 if (emsg_not_now()) 6121 return TRUE; /* no error messages at the moment */ 6122 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, n); 6123 return emsg(IObuff); 6124 } 6125 6126 #if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO) 6127 /* 6128 * Read 2 bytes from "fd" and turn them into an int, MSB first. 6129 */ 6130 int 6131 get2c(fd) 6132 FILE *fd; 6133 { 6134 int n; 6135 6136 n = getc(fd); 6137 n = (n << 8) + getc(fd); 6138 return n; 6139 } 6140 6141 /* 6142 * Read 3 bytes from "fd" and turn them into an int, MSB first. 6143 */ 6144 int 6145 get3c(fd) 6146 FILE *fd; 6147 { 6148 int n; 6149 6150 n = getc(fd); 6151 n = (n << 8) + getc(fd); 6152 n = (n << 8) + getc(fd); 6153 return n; 6154 } 6155 6156 /* 6157 * Read 4 bytes from "fd" and turn them into an int, MSB first. 6158 */ 6159 int 6160 get4c(fd) 6161 FILE *fd; 6162 { 6163 /* Use unsigned rather than int otherwise result is undefined 6164 * when left-shift sets the MSB. */ 6165 unsigned n; 6166 6167 n = (unsigned)getc(fd); 6168 n = (n << 8) + (unsigned)getc(fd); 6169 n = (n << 8) + (unsigned)getc(fd); 6170 n = (n << 8) + (unsigned)getc(fd); 6171 return (int)n; 6172 } 6173 6174 /* 6175 * Read 8 bytes from "fd" and turn them into a time_t, MSB first. 6176 */ 6177 time_t 6178 get8ctime(fd) 6179 FILE *fd; 6180 { 6181 time_t n = 0; 6182 int i; 6183 6184 for (i = 0; i < 8; ++i) 6185 n = (n << 8) + getc(fd); 6186 return n; 6187 } 6188 6189 /* 6190 * Read a string of length "cnt" from "fd" into allocated memory. 6191 * Returns NULL when out of memory or unable to read that many bytes. 6192 */ 6193 char_u * 6194 read_string(fd, cnt) 6195 FILE *fd; 6196 int cnt; 6197 { 6198 char_u *str; 6199 int i; 6200 int c; 6201 6202 /* allocate memory */ 6203 str = alloc((unsigned)cnt + 1); 6204 if (str != NULL) 6205 { 6206 /* Read the string. Quit when running into the EOF. */ 6207 for (i = 0; i < cnt; ++i) 6208 { 6209 c = getc(fd); 6210 if (c == EOF) 6211 { 6212 vim_free(str); 6213 return NULL; 6214 } 6215 str[i] = c; 6216 } 6217 str[i] = NUL; 6218 } 6219 return str; 6220 } 6221 6222 /* 6223 * Write a number to file "fd", MSB first, in "len" bytes. 6224 */ 6225 int 6226 put_bytes(fd, nr, len) 6227 FILE *fd; 6228 long_u nr; 6229 int len; 6230 { 6231 int i; 6232 6233 for (i = len - 1; i >= 0; --i) 6234 if (putc((int)(nr >> (i * 8)), fd) == EOF) 6235 return FAIL; 6236 return OK; 6237 } 6238 6239 #ifdef _MSC_VER 6240 # if (_MSC_VER <= 1200) 6241 /* This line is required for VC6 without the service pack. Also see the 6242 * matching #pragma below. */ 6243 # pragma optimize("", off) 6244 # endif 6245 #endif 6246 6247 /* 6248 * Write time_t to file "fd" in 8 bytes. 6249 */ 6250 void 6251 put_time(fd, the_time) 6252 FILE *fd; 6253 time_t the_time; 6254 { 6255 char_u buf[8]; 6256 6257 time_to_bytes(the_time, buf); 6258 (void)fwrite(buf, (size_t)8, (size_t)1, fd); 6259 } 6260 6261 /* 6262 * Write time_t to "buf[8]". 6263 */ 6264 void 6265 time_to_bytes(the_time, buf) 6266 time_t the_time; 6267 char_u *buf; 6268 { 6269 int c; 6270 int i; 6271 int bi = 0; 6272 time_t wtime = the_time; 6273 6274 /* time_t can be up to 8 bytes in size, more than long_u, thus we 6275 * can't use put_bytes() here. 6276 * Another problem is that ">>" may do an arithmetic shift that keeps the 6277 * sign. This happens for large values of wtime. A cast to long_u may 6278 * truncate if time_t is 8 bytes. So only use a cast when it is 4 bytes, 6279 * it's safe to assume that long_u is 4 bytes or more and when using 8 6280 * bytes the top bit won't be set. */ 6281 for (i = 7; i >= 0; --i) 6282 { 6283 if (i + 1 > (int)sizeof(time_t)) 6284 /* ">>" doesn't work well when shifting more bits than avail */ 6285 buf[bi++] = 0; 6286 else 6287 { 6288 #if defined(SIZEOF_TIME_T) && SIZEOF_TIME_T > 4 6289 c = (int)(wtime >> (i * 8)); 6290 #else 6291 c = (int)((long_u)wtime >> (i * 8)); 6292 #endif 6293 buf[bi++] = c; 6294 } 6295 } 6296 } 6297 6298 #ifdef _MSC_VER 6299 # if (_MSC_VER <= 1200) 6300 # pragma optimize("", on) 6301 # endif 6302 #endif 6303 6304 #endif 6305 6306 #if (defined(FEAT_MBYTE) && defined(FEAT_QUICKFIX)) \ 6307 || defined(FEAT_SPELL) || defined(PROTO) 6308 /* 6309 * Return TRUE if string "s" contains a non-ASCII character (128 or higher). 6310 * When "s" is NULL FALSE is returned. 6311 */ 6312 int 6313 has_non_ascii(s) 6314 char_u *s; 6315 { 6316 char_u *p; 6317 6318 if (s != NULL) 6319 for (p = s; *p != NUL; ++p) 6320 if (*p >= 128) 6321 return TRUE; 6322 return FALSE; 6323 } 6324 #endif 6325