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 * copy a space a number of times 1604 */ 1605 void 1606 copy_spaces(ptr, count) 1607 char_u *ptr; 1608 size_t count; 1609 { 1610 size_t i = count; 1611 char_u *p = ptr; 1612 1613 while (i--) 1614 *p++ = ' '; 1615 } 1616 1617 #if defined(FEAT_VISUALEXTRA) || defined(PROTO) 1618 /* 1619 * Copy a character a number of times. 1620 * Does not work for multi-byte characters! 1621 */ 1622 void 1623 copy_chars(ptr, count, c) 1624 char_u *ptr; 1625 size_t count; 1626 int c; 1627 { 1628 size_t i = count; 1629 char_u *p = ptr; 1630 1631 while (i--) 1632 *p++ = c; 1633 } 1634 #endif 1635 1636 /* 1637 * delete spaces at the end of a string 1638 */ 1639 void 1640 del_trailing_spaces(ptr) 1641 char_u *ptr; 1642 { 1643 char_u *q; 1644 1645 q = ptr + STRLEN(ptr); 1646 while (--q > ptr && vim_iswhite(q[0]) && q[-1] != '\\' && q[-1] != Ctrl_V) 1647 *q = NUL; 1648 } 1649 1650 /* 1651 * Like strncpy(), but always terminate the result with one NUL. 1652 * "to" must be "len + 1" long! 1653 */ 1654 void 1655 vim_strncpy(to, from, len) 1656 char_u *to; 1657 char_u *from; 1658 size_t len; 1659 { 1660 STRNCPY(to, from, len); 1661 to[len] = NUL; 1662 } 1663 1664 /* 1665 * Like strcat(), but make sure the result fits in "tosize" bytes and is 1666 * always NUL terminated. 1667 */ 1668 void 1669 vim_strcat(to, from, tosize) 1670 char_u *to; 1671 char_u *from; 1672 size_t tosize; 1673 { 1674 size_t tolen = STRLEN(to); 1675 size_t fromlen = STRLEN(from); 1676 1677 if (tolen + fromlen + 1 > tosize) 1678 { 1679 mch_memmove(to + tolen, from, tosize - tolen - 1); 1680 to[tosize - 1] = NUL; 1681 } 1682 else 1683 STRCPY(to + tolen, from); 1684 } 1685 1686 /* 1687 * Isolate one part of a string option where parts are separated with 1688 * "sep_chars". 1689 * The part is copied into "buf[maxlen]". 1690 * "*option" is advanced to the next part. 1691 * The length is returned. 1692 */ 1693 int 1694 copy_option_part(option, buf, maxlen, sep_chars) 1695 char_u **option; 1696 char_u *buf; 1697 int maxlen; 1698 char *sep_chars; 1699 { 1700 int len = 0; 1701 char_u *p = *option; 1702 1703 /* skip '.' at start of option part, for 'suffixes' */ 1704 if (*p == '.') 1705 buf[len++] = *p++; 1706 while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL) 1707 { 1708 /* 1709 * Skip backslash before a separator character and space. 1710 */ 1711 if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL) 1712 ++p; 1713 if (len < maxlen - 1) 1714 buf[len++] = *p; 1715 ++p; 1716 } 1717 buf[len] = NUL; 1718 1719 if (*p != NUL && *p != ',') /* skip non-standard separator */ 1720 ++p; 1721 p = skip_to_option_part(p); /* p points to next file name */ 1722 1723 *option = p; 1724 return len; 1725 } 1726 1727 /* 1728 * Replacement for free() that ignores NULL pointers. 1729 * Also skip free() when exiting for sure, this helps when we caught a deadly 1730 * signal that was caused by a crash in free(). 1731 */ 1732 void 1733 vim_free(x) 1734 void *x; 1735 { 1736 if (x != NULL && !really_exiting) 1737 { 1738 #ifdef MEM_PROFILE 1739 mem_pre_free(&x); 1740 #endif 1741 free(x); 1742 } 1743 } 1744 1745 #ifndef HAVE_MEMSET 1746 void * 1747 vim_memset(ptr, c, size) 1748 void *ptr; 1749 int c; 1750 size_t size; 1751 { 1752 char *p = ptr; 1753 1754 while (size-- > 0) 1755 *p++ = c; 1756 return ptr; 1757 } 1758 #endif 1759 1760 #ifdef VIM_MEMCMP 1761 /* 1762 * Return zero when "b1" and "b2" are the same for "len" bytes. 1763 * Return non-zero otherwise. 1764 */ 1765 int 1766 vim_memcmp(b1, b2, len) 1767 void *b1; 1768 void *b2; 1769 size_t len; 1770 { 1771 char_u *p1 = (char_u *)b1, *p2 = (char_u *)b2; 1772 1773 for ( ; len > 0; --len) 1774 { 1775 if (*p1 != *p2) 1776 return 1; 1777 ++p1; 1778 ++p2; 1779 } 1780 return 0; 1781 } 1782 #endif 1783 1784 #ifdef VIM_MEMMOVE 1785 /* 1786 * Version of memmove() that handles overlapping source and destination. 1787 * For systems that don't have a function that is guaranteed to do that (SYSV). 1788 */ 1789 void 1790 mch_memmove(dst_arg, src_arg, len) 1791 void *src_arg, *dst_arg; 1792 size_t len; 1793 { 1794 /* 1795 * A void doesn't have a size, we use char pointers. 1796 */ 1797 char *dst = dst_arg, *src = src_arg; 1798 1799 /* overlap, copy backwards */ 1800 if (dst > src && dst < src + len) 1801 { 1802 src += len; 1803 dst += len; 1804 while (len-- > 0) 1805 *--dst = *--src; 1806 } 1807 else /* copy forwards */ 1808 while (len-- > 0) 1809 *dst++ = *src++; 1810 } 1811 #endif 1812 1813 #if (!defined(HAVE_STRCASECMP) && !defined(HAVE_STRICMP)) || defined(PROTO) 1814 /* 1815 * Compare two strings, ignoring case, using current locale. 1816 * Doesn't work for multi-byte characters. 1817 * return 0 for match, < 0 for smaller, > 0 for bigger 1818 */ 1819 int 1820 vim_stricmp(s1, s2) 1821 char *s1; 1822 char *s2; 1823 { 1824 int i; 1825 1826 for (;;) 1827 { 1828 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2); 1829 if (i != 0) 1830 return i; /* this character different */ 1831 if (*s1 == NUL) 1832 break; /* strings match until NUL */ 1833 ++s1; 1834 ++s2; 1835 } 1836 return 0; /* strings match */ 1837 } 1838 #endif 1839 1840 #if (!defined(HAVE_STRNCASECMP) && !defined(HAVE_STRNICMP)) || defined(PROTO) 1841 /* 1842 * Compare two strings, for length "len", ignoring case, using current locale. 1843 * Doesn't work for multi-byte characters. 1844 * return 0 for match, < 0 for smaller, > 0 for bigger 1845 */ 1846 int 1847 vim_strnicmp(s1, s2, len) 1848 char *s1; 1849 char *s2; 1850 size_t len; 1851 { 1852 int i; 1853 1854 while (len > 0) 1855 { 1856 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2); 1857 if (i != 0) 1858 return i; /* this character different */ 1859 if (*s1 == NUL) 1860 break; /* strings match until NUL */ 1861 ++s1; 1862 ++s2; 1863 --len; 1864 } 1865 return 0; /* strings match */ 1866 } 1867 #endif 1868 1869 /* 1870 * Version of strchr() and strrchr() that handle unsigned char strings 1871 * with characters from 128 to 255 correctly. It also doesn't return a 1872 * pointer to the NUL at the end of the string. 1873 */ 1874 char_u * 1875 vim_strchr(string, c) 1876 char_u *string; 1877 int c; 1878 { 1879 char_u *p; 1880 int b; 1881 1882 p = string; 1883 #ifdef FEAT_MBYTE 1884 if (enc_utf8 && c >= 0x80) 1885 { 1886 while (*p != NUL) 1887 { 1888 int l = (*mb_ptr2len)(p); 1889 1890 /* Avoid matching an illegal byte here. */ 1891 if (utf_ptr2char(p) == c && l > 1) 1892 return p; 1893 p += l; 1894 } 1895 return NULL; 1896 } 1897 if (enc_dbcs != 0 && c > 255) 1898 { 1899 int n2 = c & 0xff; 1900 1901 c = ((unsigned)c >> 8) & 0xff; 1902 while ((b = *p) != NUL) 1903 { 1904 if (b == c && p[1] == n2) 1905 return p; 1906 p += (*mb_ptr2len)(p); 1907 } 1908 return NULL; 1909 } 1910 if (has_mbyte) 1911 { 1912 while ((b = *p) != NUL) 1913 { 1914 if (b == c) 1915 return p; 1916 p += (*mb_ptr2len)(p); 1917 } 1918 return NULL; 1919 } 1920 #endif 1921 while ((b = *p) != NUL) 1922 { 1923 if (b == c) 1924 return p; 1925 ++p; 1926 } 1927 return NULL; 1928 } 1929 1930 /* 1931 * Version of strchr() that only works for bytes and handles unsigned char 1932 * strings with characters above 128 correctly. It also doesn't return a 1933 * pointer to the NUL at the end of the string. 1934 */ 1935 char_u * 1936 vim_strbyte(string, c) 1937 char_u *string; 1938 int c; 1939 { 1940 char_u *p = string; 1941 1942 while (*p != NUL) 1943 { 1944 if (*p == c) 1945 return p; 1946 ++p; 1947 } 1948 return NULL; 1949 } 1950 1951 /* 1952 * Search for last occurrence of "c" in "string". 1953 * Return NULL if not found. 1954 * Does not handle multi-byte char for "c"! 1955 */ 1956 char_u * 1957 vim_strrchr(string, c) 1958 char_u *string; 1959 int c; 1960 { 1961 char_u *retval = NULL; 1962 char_u *p = string; 1963 1964 while (*p) 1965 { 1966 if (*p == c) 1967 retval = p; 1968 mb_ptr_adv(p); 1969 } 1970 return retval; 1971 } 1972 1973 /* 1974 * Vim's version of strpbrk(), in case it's missing. 1975 * Don't generate a prototype for this, causes problems when it's not used. 1976 */ 1977 #ifndef PROTO 1978 # ifndef HAVE_STRPBRK 1979 # ifdef vim_strpbrk 1980 # undef vim_strpbrk 1981 # endif 1982 char_u * 1983 vim_strpbrk(s, charset) 1984 char_u *s; 1985 char_u *charset; 1986 { 1987 while (*s) 1988 { 1989 if (vim_strchr(charset, *s) != NULL) 1990 return s; 1991 mb_ptr_adv(s); 1992 } 1993 return NULL; 1994 } 1995 # endif 1996 #endif 1997 1998 /* 1999 * Vim has its own isspace() function, because on some machines isspace() 2000 * can't handle characters above 128. 2001 */ 2002 int 2003 vim_isspace(x) 2004 int x; 2005 { 2006 return ((x >= 9 && x <= 13) || x == ' '); 2007 } 2008 2009 /************************************************************************ 2010 * Functions for handling growing arrays. 2011 */ 2012 2013 /* 2014 * Clear an allocated growing array. 2015 */ 2016 void 2017 ga_clear(gap) 2018 garray_T *gap; 2019 { 2020 vim_free(gap->ga_data); 2021 ga_init(gap); 2022 } 2023 2024 /* 2025 * Clear a growing array that contains a list of strings. 2026 */ 2027 void 2028 ga_clear_strings(gap) 2029 garray_T *gap; 2030 { 2031 int i; 2032 2033 for (i = 0; i < gap->ga_len; ++i) 2034 vim_free(((char_u **)(gap->ga_data))[i]); 2035 ga_clear(gap); 2036 } 2037 2038 /* 2039 * Initialize a growing array. Don't forget to set ga_itemsize and 2040 * ga_growsize! Or use ga_init2(). 2041 */ 2042 void 2043 ga_init(gap) 2044 garray_T *gap; 2045 { 2046 gap->ga_data = NULL; 2047 gap->ga_maxlen = 0; 2048 gap->ga_len = 0; 2049 } 2050 2051 void 2052 ga_init2(gap, itemsize, growsize) 2053 garray_T *gap; 2054 int itemsize; 2055 int growsize; 2056 { 2057 ga_init(gap); 2058 gap->ga_itemsize = itemsize; 2059 gap->ga_growsize = growsize; 2060 } 2061 2062 /* 2063 * Make room in growing array "gap" for at least "n" items. 2064 * Return FAIL for failure, OK otherwise. 2065 */ 2066 int 2067 ga_grow(gap, n) 2068 garray_T *gap; 2069 int n; 2070 { 2071 size_t old_len; 2072 size_t new_len; 2073 char_u *pp; 2074 2075 if (gap->ga_maxlen - gap->ga_len < n) 2076 { 2077 if (n < gap->ga_growsize) 2078 n = gap->ga_growsize; 2079 new_len = gap->ga_itemsize * (gap->ga_len + n); 2080 pp = (gap->ga_data == NULL) 2081 ? alloc((unsigned)new_len) : vim_realloc(gap->ga_data, new_len); 2082 if (pp == NULL) 2083 return FAIL; 2084 old_len = gap->ga_itemsize * gap->ga_maxlen; 2085 vim_memset(pp + old_len, 0, new_len - old_len); 2086 gap->ga_maxlen = gap->ga_len + n; 2087 gap->ga_data = pp; 2088 } 2089 return OK; 2090 } 2091 2092 /* 2093 * For a growing array that contains a list of strings: concatenate all the 2094 * strings with a separating "sep". 2095 * Returns NULL when out of memory. 2096 */ 2097 char_u * 2098 ga_concat_strings(gap, sep) 2099 garray_T *gap; 2100 char *sep; 2101 { 2102 int i; 2103 int len = 0; 2104 int sep_len = (int)STRLEN(sep); 2105 char_u *s; 2106 char_u *p; 2107 2108 for (i = 0; i < gap->ga_len; ++i) 2109 len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + sep_len; 2110 2111 s = alloc(len + 1); 2112 if (s != NULL) 2113 { 2114 *s = NUL; 2115 p = s; 2116 for (i = 0; i < gap->ga_len; ++i) 2117 { 2118 if (p != s) 2119 { 2120 STRCPY(p, sep); 2121 p += sep_len; 2122 } 2123 STRCPY(p, ((char_u **)(gap->ga_data))[i]); 2124 p += STRLEN(p); 2125 } 2126 } 2127 return s; 2128 } 2129 2130 /* 2131 * Concatenate a string to a growarray which contains characters. 2132 * Note: Does NOT copy the NUL at the end! 2133 */ 2134 void 2135 ga_concat(gap, s) 2136 garray_T *gap; 2137 char_u *s; 2138 { 2139 int len = (int)STRLEN(s); 2140 2141 if (ga_grow(gap, len) == OK) 2142 { 2143 mch_memmove((char *)gap->ga_data + gap->ga_len, s, (size_t)len); 2144 gap->ga_len += len; 2145 } 2146 } 2147 2148 /* 2149 * Append one byte to a growarray which contains bytes. 2150 */ 2151 void 2152 ga_append(gap, c) 2153 garray_T *gap; 2154 int c; 2155 { 2156 if (ga_grow(gap, 1) == OK) 2157 { 2158 *((char *)gap->ga_data + gap->ga_len) = c; 2159 ++gap->ga_len; 2160 } 2161 } 2162 2163 #if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(WIN3264) \ 2164 || defined(PROTO) 2165 /* 2166 * Append the text in "gap" below the cursor line and clear "gap". 2167 */ 2168 void 2169 append_ga_line(gap) 2170 garray_T *gap; 2171 { 2172 /* Remove trailing CR. */ 2173 if (gap->ga_len > 0 2174 && !curbuf->b_p_bin 2175 && ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR) 2176 --gap->ga_len; 2177 ga_append(gap, NUL); 2178 ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE); 2179 gap->ga_len = 0; 2180 } 2181 #endif 2182 2183 /************************************************************************ 2184 * functions that use lookup tables for various things, generally to do with 2185 * special key codes. 2186 */ 2187 2188 /* 2189 * Some useful tables. 2190 */ 2191 2192 static struct modmasktable 2193 { 2194 short mod_mask; /* Bit-mask for particular key modifier */ 2195 short mod_flag; /* Bit(s) for particular key modifier */ 2196 char_u name; /* Single letter name of modifier */ 2197 } mod_mask_table[] = 2198 { 2199 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'}, 2200 {MOD_MASK_META, MOD_MASK_META, (char_u)'T'}, 2201 {MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'}, 2202 {MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'}, 2203 {MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'}, 2204 {MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'}, 2205 {MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'}, 2206 #ifdef MACOS 2207 {MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'}, 2208 #endif 2209 /* 'A' must be the last one */ 2210 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'}, 2211 {0, 0, NUL} 2212 }; 2213 2214 /* 2215 * Shifted key terminal codes and their unshifted equivalent. 2216 * Don't add mouse codes here, they are handled separately! 2217 */ 2218 #define MOD_KEYS_ENTRY_SIZE 5 2219 2220 static char_u modifier_keys_table[] = 2221 { 2222 /* mod mask with modifier without modifier */ 2223 MOD_MASK_SHIFT, '&', '9', '@', '1', /* begin */ 2224 MOD_MASK_SHIFT, '&', '0', '@', '2', /* cancel */ 2225 MOD_MASK_SHIFT, '*', '1', '@', '4', /* command */ 2226 MOD_MASK_SHIFT, '*', '2', '@', '5', /* copy */ 2227 MOD_MASK_SHIFT, '*', '3', '@', '6', /* create */ 2228 MOD_MASK_SHIFT, '*', '4', 'k', 'D', /* delete char */ 2229 MOD_MASK_SHIFT, '*', '5', 'k', 'L', /* delete line */ 2230 MOD_MASK_SHIFT, '*', '7', '@', '7', /* end */ 2231 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7', /* end */ 2232 MOD_MASK_SHIFT, '*', '9', '@', '9', /* exit */ 2233 MOD_MASK_SHIFT, '*', '0', '@', '0', /* find */ 2234 MOD_MASK_SHIFT, '#', '1', '%', '1', /* help */ 2235 MOD_MASK_SHIFT, '#', '2', 'k', 'h', /* home */ 2236 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h', /* home */ 2237 MOD_MASK_SHIFT, '#', '3', 'k', 'I', /* insert */ 2238 MOD_MASK_SHIFT, '#', '4', 'k', 'l', /* left arrow */ 2239 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l', /* left arrow */ 2240 MOD_MASK_SHIFT, '%', 'a', '%', '3', /* message */ 2241 MOD_MASK_SHIFT, '%', 'b', '%', '4', /* move */ 2242 MOD_MASK_SHIFT, '%', 'c', '%', '5', /* next */ 2243 MOD_MASK_SHIFT, '%', 'd', '%', '7', /* options */ 2244 MOD_MASK_SHIFT, '%', 'e', '%', '8', /* previous */ 2245 MOD_MASK_SHIFT, '%', 'f', '%', '9', /* print */ 2246 MOD_MASK_SHIFT, '%', 'g', '%', '0', /* redo */ 2247 MOD_MASK_SHIFT, '%', 'h', '&', '3', /* replace */ 2248 MOD_MASK_SHIFT, '%', 'i', 'k', 'r', /* right arr. */ 2249 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r', /* right arr. */ 2250 MOD_MASK_SHIFT, '%', 'j', '&', '5', /* resume */ 2251 MOD_MASK_SHIFT, '!', '1', '&', '6', /* save */ 2252 MOD_MASK_SHIFT, '!', '2', '&', '7', /* suspend */ 2253 MOD_MASK_SHIFT, '!', '3', '&', '8', /* undo */ 2254 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u', /* up arrow */ 2255 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd', /* down arrow */ 2256 2257 /* vt100 F1 */ 2258 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1, 2259 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2, 2260 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3, 2261 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4, 2262 2263 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1', /* F1 */ 2264 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2', 2265 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3', 2266 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4', 2267 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5', 2268 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6', 2269 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7', 2270 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8', 2271 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9', 2272 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';', /* F10 */ 2273 2274 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1', 2275 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2', 2276 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3', 2277 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4', 2278 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5', 2279 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6', 2280 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7', 2281 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8', 2282 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9', 2283 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A', 2284 2285 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B', 2286 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C', 2287 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D', 2288 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E', 2289 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F', 2290 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G', 2291 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H', 2292 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I', 2293 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J', 2294 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K', 2295 2296 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L', 2297 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M', 2298 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N', 2299 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O', 2300 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P', 2301 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q', 2302 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R', 2303 2304 /* TAB pseudo code*/ 2305 MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB, 2306 2307 NUL 2308 }; 2309 2310 static struct key_name_entry 2311 { 2312 int key; /* Special key code or ascii value */ 2313 char_u *name; /* Name of key */ 2314 } key_names_table[] = 2315 { 2316 {' ', (char_u *)"Space"}, 2317 {TAB, (char_u *)"Tab"}, 2318 {K_TAB, (char_u *)"Tab"}, 2319 {NL, (char_u *)"NL"}, 2320 {NL, (char_u *)"NewLine"}, /* Alternative name */ 2321 {NL, (char_u *)"LineFeed"}, /* Alternative name */ 2322 {NL, (char_u *)"LF"}, /* Alternative name */ 2323 {CAR, (char_u *)"CR"}, 2324 {CAR, (char_u *)"Return"}, /* Alternative name */ 2325 {CAR, (char_u *)"Enter"}, /* Alternative name */ 2326 {K_BS, (char_u *)"BS"}, 2327 {K_BS, (char_u *)"BackSpace"}, /* Alternative name */ 2328 {ESC, (char_u *)"Esc"}, 2329 {CSI, (char_u *)"CSI"}, 2330 {K_CSI, (char_u *)"xCSI"}, 2331 {'|', (char_u *)"Bar"}, 2332 {'\\', (char_u *)"Bslash"}, 2333 {K_DEL, (char_u *)"Del"}, 2334 {K_DEL, (char_u *)"Delete"}, /* Alternative name */ 2335 {K_KDEL, (char_u *)"kDel"}, 2336 {K_UP, (char_u *)"Up"}, 2337 {K_DOWN, (char_u *)"Down"}, 2338 {K_LEFT, (char_u *)"Left"}, 2339 {K_RIGHT, (char_u *)"Right"}, 2340 {K_XUP, (char_u *)"xUp"}, 2341 {K_XDOWN, (char_u *)"xDown"}, 2342 {K_XLEFT, (char_u *)"xLeft"}, 2343 {K_XRIGHT, (char_u *)"xRight"}, 2344 2345 {K_F1, (char_u *)"F1"}, 2346 {K_F2, (char_u *)"F2"}, 2347 {K_F3, (char_u *)"F3"}, 2348 {K_F4, (char_u *)"F4"}, 2349 {K_F5, (char_u *)"F5"}, 2350 {K_F6, (char_u *)"F6"}, 2351 {K_F7, (char_u *)"F7"}, 2352 {K_F8, (char_u *)"F8"}, 2353 {K_F9, (char_u *)"F9"}, 2354 {K_F10, (char_u *)"F10"}, 2355 2356 {K_F11, (char_u *)"F11"}, 2357 {K_F12, (char_u *)"F12"}, 2358 {K_F13, (char_u *)"F13"}, 2359 {K_F14, (char_u *)"F14"}, 2360 {K_F15, (char_u *)"F15"}, 2361 {K_F16, (char_u *)"F16"}, 2362 {K_F17, (char_u *)"F17"}, 2363 {K_F18, (char_u *)"F18"}, 2364 {K_F19, (char_u *)"F19"}, 2365 {K_F20, (char_u *)"F20"}, 2366 2367 {K_F21, (char_u *)"F21"}, 2368 {K_F22, (char_u *)"F22"}, 2369 {K_F23, (char_u *)"F23"}, 2370 {K_F24, (char_u *)"F24"}, 2371 {K_F25, (char_u *)"F25"}, 2372 {K_F26, (char_u *)"F26"}, 2373 {K_F27, (char_u *)"F27"}, 2374 {K_F28, (char_u *)"F28"}, 2375 {K_F29, (char_u *)"F29"}, 2376 {K_F30, (char_u *)"F30"}, 2377 2378 {K_F31, (char_u *)"F31"}, 2379 {K_F32, (char_u *)"F32"}, 2380 {K_F33, (char_u *)"F33"}, 2381 {K_F34, (char_u *)"F34"}, 2382 {K_F35, (char_u *)"F35"}, 2383 {K_F36, (char_u *)"F36"}, 2384 {K_F37, (char_u *)"F37"}, 2385 2386 {K_XF1, (char_u *)"xF1"}, 2387 {K_XF2, (char_u *)"xF2"}, 2388 {K_XF3, (char_u *)"xF3"}, 2389 {K_XF4, (char_u *)"xF4"}, 2390 2391 {K_HELP, (char_u *)"Help"}, 2392 {K_UNDO, (char_u *)"Undo"}, 2393 {K_INS, (char_u *)"Insert"}, 2394 {K_INS, (char_u *)"Ins"}, /* Alternative name */ 2395 {K_KINS, (char_u *)"kInsert"}, 2396 {K_HOME, (char_u *)"Home"}, 2397 {K_KHOME, (char_u *)"kHome"}, 2398 {K_XHOME, (char_u *)"xHome"}, 2399 {K_ZHOME, (char_u *)"zHome"}, 2400 {K_END, (char_u *)"End"}, 2401 {K_KEND, (char_u *)"kEnd"}, 2402 {K_XEND, (char_u *)"xEnd"}, 2403 {K_ZEND, (char_u *)"zEnd"}, 2404 {K_PAGEUP, (char_u *)"PageUp"}, 2405 {K_PAGEDOWN, (char_u *)"PageDown"}, 2406 {K_KPAGEUP, (char_u *)"kPageUp"}, 2407 {K_KPAGEDOWN, (char_u *)"kPageDown"}, 2408 2409 {K_KPLUS, (char_u *)"kPlus"}, 2410 {K_KMINUS, (char_u *)"kMinus"}, 2411 {K_KDIVIDE, (char_u *)"kDivide"}, 2412 {K_KMULTIPLY, (char_u *)"kMultiply"}, 2413 {K_KENTER, (char_u *)"kEnter"}, 2414 {K_KPOINT, (char_u *)"kPoint"}, 2415 2416 {K_K0, (char_u *)"k0"}, 2417 {K_K1, (char_u *)"k1"}, 2418 {K_K2, (char_u *)"k2"}, 2419 {K_K3, (char_u *)"k3"}, 2420 {K_K4, (char_u *)"k4"}, 2421 {K_K5, (char_u *)"k5"}, 2422 {K_K6, (char_u *)"k6"}, 2423 {K_K7, (char_u *)"k7"}, 2424 {K_K8, (char_u *)"k8"}, 2425 {K_K9, (char_u *)"k9"}, 2426 2427 {'<', (char_u *)"lt"}, 2428 2429 {K_MOUSE, (char_u *)"Mouse"}, 2430 #ifdef FEAT_MOUSE_NET 2431 {K_NETTERM_MOUSE, (char_u *)"NetMouse"}, 2432 #endif 2433 #ifdef FEAT_MOUSE_DEC 2434 {K_DEC_MOUSE, (char_u *)"DecMouse"}, 2435 #endif 2436 #ifdef FEAT_MOUSE_JSB 2437 {K_JSBTERM_MOUSE, (char_u *)"JsbMouse"}, 2438 #endif 2439 #ifdef FEAT_MOUSE_PTERM 2440 {K_PTERM_MOUSE, (char_u *)"PtermMouse"}, 2441 #endif 2442 #ifdef FEAT_MOUSE_URXVT 2443 {K_URXVT_MOUSE, (char_u *)"UrxvtMouse"}, 2444 #endif 2445 #ifdef FEAT_MOUSE_SGR 2446 {K_SGR_MOUSE, (char_u *)"SgrMouse"}, 2447 #endif 2448 {K_LEFTMOUSE, (char_u *)"LeftMouse"}, 2449 {K_LEFTMOUSE_NM, (char_u *)"LeftMouseNM"}, 2450 {K_LEFTDRAG, (char_u *)"LeftDrag"}, 2451 {K_LEFTRELEASE, (char_u *)"LeftRelease"}, 2452 {K_LEFTRELEASE_NM, (char_u *)"LeftReleaseNM"}, 2453 {K_MIDDLEMOUSE, (char_u *)"MiddleMouse"}, 2454 {K_MIDDLEDRAG, (char_u *)"MiddleDrag"}, 2455 {K_MIDDLERELEASE, (char_u *)"MiddleRelease"}, 2456 {K_RIGHTMOUSE, (char_u *)"RightMouse"}, 2457 {K_RIGHTDRAG, (char_u *)"RightDrag"}, 2458 {K_RIGHTRELEASE, (char_u *)"RightRelease"}, 2459 {K_MOUSEDOWN, (char_u *)"ScrollWheelUp"}, 2460 {K_MOUSEUP, (char_u *)"ScrollWheelDown"}, 2461 {K_MOUSELEFT, (char_u *)"ScrollWheelRight"}, 2462 {K_MOUSERIGHT, (char_u *)"ScrollWheelLeft"}, 2463 {K_MOUSEDOWN, (char_u *)"MouseDown"}, /* OBSOLETE: Use */ 2464 {K_MOUSEUP, (char_u *)"MouseUp"}, /* ScrollWheelXXX instead */ 2465 {K_X1MOUSE, (char_u *)"X1Mouse"}, 2466 {K_X1DRAG, (char_u *)"X1Drag"}, 2467 {K_X1RELEASE, (char_u *)"X1Release"}, 2468 {K_X2MOUSE, (char_u *)"X2Mouse"}, 2469 {K_X2DRAG, (char_u *)"X2Drag"}, 2470 {K_X2RELEASE, (char_u *)"X2Release"}, 2471 {K_DROP, (char_u *)"Drop"}, 2472 {K_ZERO, (char_u *)"Nul"}, 2473 #ifdef FEAT_EVAL 2474 {K_SNR, (char_u *)"SNR"}, 2475 #endif 2476 {K_PLUG, (char_u *)"Plug"}, 2477 {K_CURSORHOLD, (char_u *)"CursorHold"}, 2478 {0, NULL} 2479 }; 2480 2481 #define KEY_NAMES_TABLE_LEN (sizeof(key_names_table) / sizeof(struct key_name_entry)) 2482 2483 #ifdef FEAT_MOUSE 2484 static struct mousetable 2485 { 2486 int pseudo_code; /* Code for pseudo mouse event */ 2487 int button; /* Which mouse button is it? */ 2488 int is_click; /* Is it a mouse button click event? */ 2489 int is_drag; /* Is it a mouse drag event? */ 2490 } mouse_table[] = 2491 { 2492 {(int)KE_LEFTMOUSE, MOUSE_LEFT, TRUE, FALSE}, 2493 #ifdef FEAT_GUI 2494 {(int)KE_LEFTMOUSE_NM, MOUSE_LEFT, TRUE, FALSE}, 2495 #endif 2496 {(int)KE_LEFTDRAG, MOUSE_LEFT, FALSE, TRUE}, 2497 {(int)KE_LEFTRELEASE, MOUSE_LEFT, FALSE, FALSE}, 2498 #ifdef FEAT_GUI 2499 {(int)KE_LEFTRELEASE_NM, MOUSE_LEFT, FALSE, FALSE}, 2500 #endif 2501 {(int)KE_MIDDLEMOUSE, MOUSE_MIDDLE, TRUE, FALSE}, 2502 {(int)KE_MIDDLEDRAG, MOUSE_MIDDLE, FALSE, TRUE}, 2503 {(int)KE_MIDDLERELEASE, MOUSE_MIDDLE, FALSE, FALSE}, 2504 {(int)KE_RIGHTMOUSE, MOUSE_RIGHT, TRUE, FALSE}, 2505 {(int)KE_RIGHTDRAG, MOUSE_RIGHT, FALSE, TRUE}, 2506 {(int)KE_RIGHTRELEASE, MOUSE_RIGHT, FALSE, FALSE}, 2507 {(int)KE_X1MOUSE, MOUSE_X1, TRUE, FALSE}, 2508 {(int)KE_X1DRAG, MOUSE_X1, FALSE, TRUE}, 2509 {(int)KE_X1RELEASE, MOUSE_X1, FALSE, FALSE}, 2510 {(int)KE_X2MOUSE, MOUSE_X2, TRUE, FALSE}, 2511 {(int)KE_X2DRAG, MOUSE_X2, FALSE, TRUE}, 2512 {(int)KE_X2RELEASE, MOUSE_X2, FALSE, FALSE}, 2513 /* DRAG without CLICK */ 2514 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, TRUE}, 2515 /* RELEASE without CLICK */ 2516 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, FALSE}, 2517 {0, 0, 0, 0}, 2518 }; 2519 #endif /* FEAT_MOUSE */ 2520 2521 /* 2522 * Return the modifier mask bit (MOD_MASK_*) which corresponds to the given 2523 * modifier name ('S' for Shift, 'C' for Ctrl etc). 2524 */ 2525 int 2526 name_to_mod_mask(c) 2527 int c; 2528 { 2529 int i; 2530 2531 c = TOUPPER_ASC(c); 2532 for (i = 0; mod_mask_table[i].mod_mask != 0; i++) 2533 if (c == mod_mask_table[i].name) 2534 return mod_mask_table[i].mod_flag; 2535 return 0; 2536 } 2537 2538 /* 2539 * Check if if there is a special key code for "key" that includes the 2540 * modifiers specified. 2541 */ 2542 int 2543 simplify_key(key, modifiers) 2544 int key; 2545 int *modifiers; 2546 { 2547 int i; 2548 int key0; 2549 int key1; 2550 2551 if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT)) 2552 { 2553 /* TAB is a special case */ 2554 if (key == TAB && (*modifiers & MOD_MASK_SHIFT)) 2555 { 2556 *modifiers &= ~MOD_MASK_SHIFT; 2557 return K_S_TAB; 2558 } 2559 key0 = KEY2TERMCAP0(key); 2560 key1 = KEY2TERMCAP1(key); 2561 for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE) 2562 if (key0 == modifier_keys_table[i + 3] 2563 && key1 == modifier_keys_table[i + 4] 2564 && (*modifiers & modifier_keys_table[i])) 2565 { 2566 *modifiers &= ~modifier_keys_table[i]; 2567 return TERMCAP2KEY(modifier_keys_table[i + 1], 2568 modifier_keys_table[i + 2]); 2569 } 2570 } 2571 return key; 2572 } 2573 2574 /* 2575 * Change <xHome> to <Home>, <xUp> to <Up>, etc. 2576 */ 2577 int 2578 handle_x_keys(key) 2579 int key; 2580 { 2581 switch (key) 2582 { 2583 case K_XUP: return K_UP; 2584 case K_XDOWN: return K_DOWN; 2585 case K_XLEFT: return K_LEFT; 2586 case K_XRIGHT: return K_RIGHT; 2587 case K_XHOME: return K_HOME; 2588 case K_ZHOME: return K_HOME; 2589 case K_XEND: return K_END; 2590 case K_ZEND: return K_END; 2591 case K_XF1: return K_F1; 2592 case K_XF2: return K_F2; 2593 case K_XF3: return K_F3; 2594 case K_XF4: return K_F4; 2595 case K_S_XF1: return K_S_F1; 2596 case K_S_XF2: return K_S_F2; 2597 case K_S_XF3: return K_S_F3; 2598 case K_S_XF4: return K_S_F4; 2599 } 2600 return key; 2601 } 2602 2603 /* 2604 * Return a string which contains the name of the given key when the given 2605 * modifiers are down. 2606 */ 2607 char_u * 2608 get_special_key_name(c, modifiers) 2609 int c; 2610 int modifiers; 2611 { 2612 static char_u string[MAX_KEY_NAME_LEN + 1]; 2613 2614 int i, idx; 2615 int table_idx; 2616 char_u *s; 2617 2618 string[0] = '<'; 2619 idx = 1; 2620 2621 /* Key that stands for a normal character. */ 2622 if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY) 2623 c = KEY2TERMCAP1(c); 2624 2625 /* 2626 * Translate shifted special keys into unshifted keys and set modifier. 2627 * Same for CTRL and ALT modifiers. 2628 */ 2629 if (IS_SPECIAL(c)) 2630 { 2631 for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE) 2632 if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1] 2633 && (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2]) 2634 { 2635 modifiers |= modifier_keys_table[i]; 2636 c = TERMCAP2KEY(modifier_keys_table[i + 3], 2637 modifier_keys_table[i + 4]); 2638 break; 2639 } 2640 } 2641 2642 /* try to find the key in the special key table */ 2643 table_idx = find_special_key_in_table(c); 2644 2645 /* 2646 * When not a known special key, and not a printable character, try to 2647 * extract modifiers. 2648 */ 2649 if (c > 0 2650 #ifdef FEAT_MBYTE 2651 && (*mb_char2len)(c) == 1 2652 #endif 2653 ) 2654 { 2655 if (table_idx < 0 2656 && (!vim_isprintc(c) || (c & 0x7f) == ' ') 2657 && (c & 0x80)) 2658 { 2659 c &= 0x7f; 2660 modifiers |= MOD_MASK_ALT; 2661 /* try again, to find the un-alted key in the special key table */ 2662 table_idx = find_special_key_in_table(c); 2663 } 2664 if (table_idx < 0 && !vim_isprintc(c) && c < ' ') 2665 { 2666 #ifdef EBCDIC 2667 c = CtrlChar(c); 2668 #else 2669 c += '@'; 2670 #endif 2671 modifiers |= MOD_MASK_CTRL; 2672 } 2673 } 2674 2675 /* translate the modifier into a string */ 2676 for (i = 0; mod_mask_table[i].name != 'A'; i++) 2677 if ((modifiers & mod_mask_table[i].mod_mask) 2678 == mod_mask_table[i].mod_flag) 2679 { 2680 string[idx++] = mod_mask_table[i].name; 2681 string[idx++] = (char_u)'-'; 2682 } 2683 2684 if (table_idx < 0) /* unknown special key, may output t_xx */ 2685 { 2686 if (IS_SPECIAL(c)) 2687 { 2688 string[idx++] = 't'; 2689 string[idx++] = '_'; 2690 string[idx++] = KEY2TERMCAP0(c); 2691 string[idx++] = KEY2TERMCAP1(c); 2692 } 2693 /* Not a special key, only modifiers, output directly */ 2694 else 2695 { 2696 #ifdef FEAT_MBYTE 2697 if (has_mbyte && (*mb_char2len)(c) > 1) 2698 idx += (*mb_char2bytes)(c, string + idx); 2699 else 2700 #endif 2701 if (vim_isprintc(c)) 2702 string[idx++] = c; 2703 else 2704 { 2705 s = transchar(c); 2706 while (*s) 2707 string[idx++] = *s++; 2708 } 2709 } 2710 } 2711 else /* use name of special key */ 2712 { 2713 STRCPY(string + idx, key_names_table[table_idx].name); 2714 idx = (int)STRLEN(string); 2715 } 2716 string[idx++] = '>'; 2717 string[idx] = NUL; 2718 return string; 2719 } 2720 2721 /* 2722 * Try translating a <> name at (*srcp)[] to dst[]. 2723 * Return the number of characters added to dst[], zero for no match. 2724 * If there is a match, srcp is advanced to after the <> name. 2725 * dst[] must be big enough to hold the result (up to six characters)! 2726 */ 2727 int 2728 trans_special(srcp, dst, keycode) 2729 char_u **srcp; 2730 char_u *dst; 2731 int keycode; /* prefer key code, e.g. K_DEL instead of DEL */ 2732 { 2733 int modifiers = 0; 2734 int key; 2735 int dlen = 0; 2736 2737 key = find_special_key(srcp, &modifiers, keycode, FALSE); 2738 if (key == 0) 2739 return 0; 2740 2741 /* Put the appropriate modifier in a string */ 2742 if (modifiers != 0) 2743 { 2744 dst[dlen++] = K_SPECIAL; 2745 dst[dlen++] = KS_MODIFIER; 2746 dst[dlen++] = modifiers; 2747 } 2748 2749 if (IS_SPECIAL(key)) 2750 { 2751 dst[dlen++] = K_SPECIAL; 2752 dst[dlen++] = KEY2TERMCAP0(key); 2753 dst[dlen++] = KEY2TERMCAP1(key); 2754 } 2755 #ifdef FEAT_MBYTE 2756 else if (has_mbyte && !keycode) 2757 dlen += (*mb_char2bytes)(key, dst + dlen); 2758 #endif 2759 else if (keycode) 2760 dlen = (int)(add_char2buf(key, dst + dlen) - dst); 2761 else 2762 dst[dlen++] = key; 2763 2764 return dlen; 2765 } 2766 2767 /* 2768 * Try translating a <> name at (*srcp)[], return the key and modifiers. 2769 * srcp is advanced to after the <> name. 2770 * returns 0 if there is no match. 2771 */ 2772 int 2773 find_special_key(srcp, modp, keycode, keep_x_key) 2774 char_u **srcp; 2775 int *modp; 2776 int keycode; /* prefer key code, e.g. K_DEL instead of DEL */ 2777 int keep_x_key; /* don't translate xHome to Home key */ 2778 { 2779 char_u *last_dash; 2780 char_u *end_of_name; 2781 char_u *src; 2782 char_u *bp; 2783 int modifiers; 2784 int bit; 2785 int key; 2786 unsigned long n; 2787 int l; 2788 2789 src = *srcp; 2790 if (src[0] != '<') 2791 return 0; 2792 2793 /* Find end of modifier list */ 2794 last_dash = src; 2795 for (bp = src + 1; *bp == '-' || vim_isIDc(*bp); bp++) 2796 { 2797 if (*bp == '-') 2798 { 2799 last_dash = bp; 2800 if (bp[1] != NUL) 2801 { 2802 #ifdef FEAT_MBYTE 2803 if (has_mbyte) 2804 l = mb_ptr2len(bp + 1); 2805 else 2806 #endif 2807 l = 1; 2808 if (bp[l + 1] == '>') 2809 bp += l; /* anything accepted, like <C-?> */ 2810 } 2811 } 2812 if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3]) 2813 bp += 3; /* skip t_xx, xx may be '-' or '>' */ 2814 else if (STRNICMP(bp, "char-", 5) == 0) 2815 { 2816 vim_str2nr(bp + 5, NULL, &l, TRUE, TRUE, NULL, NULL); 2817 bp += l + 5; 2818 break; 2819 } 2820 } 2821 2822 if (*bp == '>') /* found matching '>' */ 2823 { 2824 end_of_name = bp + 1; 2825 2826 /* Which modifiers are given? */ 2827 modifiers = 0x0; 2828 for (bp = src + 1; bp < last_dash; bp++) 2829 { 2830 if (*bp != '-') 2831 { 2832 bit = name_to_mod_mask(*bp); 2833 if (bit == 0x0) 2834 break; /* Illegal modifier name */ 2835 modifiers |= bit; 2836 } 2837 } 2838 2839 /* 2840 * Legal modifier name. 2841 */ 2842 if (bp >= last_dash) 2843 { 2844 if (STRNICMP(last_dash + 1, "char-", 5) == 0 2845 && VIM_ISDIGIT(last_dash[6])) 2846 { 2847 /* <Char-123> or <Char-033> or <Char-0x33> */ 2848 vim_str2nr(last_dash + 6, NULL, NULL, TRUE, TRUE, NULL, &n); 2849 key = (int)n; 2850 } 2851 else 2852 { 2853 /* 2854 * Modifier with single letter, or special key name. 2855 */ 2856 #ifdef FEAT_MBYTE 2857 if (has_mbyte) 2858 l = mb_ptr2len(last_dash + 1); 2859 else 2860 #endif 2861 l = 1; 2862 if (modifiers != 0 && last_dash[l + 1] == '>') 2863 key = PTR2CHAR(last_dash + 1); 2864 else 2865 { 2866 key = get_special_key_code(last_dash + 1); 2867 if (!keep_x_key) 2868 key = handle_x_keys(key); 2869 } 2870 } 2871 2872 /* 2873 * get_special_key_code() may return NUL for invalid 2874 * special key name. 2875 */ 2876 if (key != NUL) 2877 { 2878 /* 2879 * Only use a modifier when there is no special key code that 2880 * includes the modifier. 2881 */ 2882 key = simplify_key(key, &modifiers); 2883 2884 if (!keycode) 2885 { 2886 /* don't want keycode, use single byte code */ 2887 if (key == K_BS) 2888 key = BS; 2889 else if (key == K_DEL || key == K_KDEL) 2890 key = DEL; 2891 } 2892 2893 /* 2894 * Normal Key with modifier: Try to make a single byte code. 2895 */ 2896 if (!IS_SPECIAL(key)) 2897 key = extract_modifiers(key, &modifiers); 2898 2899 *modp = modifiers; 2900 *srcp = end_of_name; 2901 return key; 2902 } 2903 } 2904 } 2905 return 0; 2906 } 2907 2908 /* 2909 * Try to include modifiers in the key. 2910 * Changes "Shift-a" to 'A', "Alt-A" to 0xc0, etc. 2911 */ 2912 int 2913 extract_modifiers(key, modp) 2914 int key; 2915 int *modp; 2916 { 2917 int modifiers = *modp; 2918 2919 #ifdef MACOS 2920 /* Command-key really special, no fancynest */ 2921 if (!(modifiers & MOD_MASK_CMD)) 2922 #endif 2923 if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key)) 2924 { 2925 key = TOUPPER_ASC(key); 2926 modifiers &= ~MOD_MASK_SHIFT; 2927 } 2928 if ((modifiers & MOD_MASK_CTRL) 2929 #ifdef EBCDIC 2930 /* * TODO: EBCDIC Better use: 2931 * && (Ctrl_chr(key) || key == '?') 2932 * ??? */ 2933 && strchr("?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_", key) 2934 != NULL 2935 #else 2936 && ((key >= '?' && key <= '_') || ASCII_ISALPHA(key)) 2937 #endif 2938 ) 2939 { 2940 key = Ctrl_chr(key); 2941 modifiers &= ~MOD_MASK_CTRL; 2942 /* <C-@> is <Nul> */ 2943 if (key == 0) 2944 key = K_ZERO; 2945 } 2946 #ifdef MACOS 2947 /* Command-key really special, no fancynest */ 2948 if (!(modifiers & MOD_MASK_CMD)) 2949 #endif 2950 if ((modifiers & MOD_MASK_ALT) && key < 0x80 2951 #ifdef FEAT_MBYTE 2952 && !enc_dbcs /* avoid creating a lead byte */ 2953 #endif 2954 ) 2955 { 2956 key |= 0x80; 2957 modifiers &= ~MOD_MASK_ALT; /* remove the META modifier */ 2958 } 2959 2960 *modp = modifiers; 2961 return key; 2962 } 2963 2964 /* 2965 * Try to find key "c" in the special key table. 2966 * Return the index when found, -1 when not found. 2967 */ 2968 int 2969 find_special_key_in_table(c) 2970 int c; 2971 { 2972 int i; 2973 2974 for (i = 0; key_names_table[i].name != NULL; i++) 2975 if (c == key_names_table[i].key) 2976 break; 2977 if (key_names_table[i].name == NULL) 2978 i = -1; 2979 return i; 2980 } 2981 2982 /* 2983 * Find the special key with the given name (the given string does not have to 2984 * end with NUL, the name is assumed to end before the first non-idchar). 2985 * If the name starts with "t_" the next two characters are interpreted as a 2986 * termcap name. 2987 * Return the key code, or 0 if not found. 2988 */ 2989 int 2990 get_special_key_code(name) 2991 char_u *name; 2992 { 2993 char_u *table_name; 2994 char_u string[3]; 2995 int i, j; 2996 2997 /* 2998 * If it's <t_xx> we get the code for xx from the termcap 2999 */ 3000 if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL) 3001 { 3002 string[0] = name[2]; 3003 string[1] = name[3]; 3004 string[2] = NUL; 3005 if (add_termcap_entry(string, FALSE) == OK) 3006 return TERMCAP2KEY(name[2], name[3]); 3007 } 3008 else 3009 for (i = 0; key_names_table[i].name != NULL; i++) 3010 { 3011 table_name = key_names_table[i].name; 3012 for (j = 0; vim_isIDc(name[j]) && table_name[j] != NUL; j++) 3013 if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j])) 3014 break; 3015 if (!vim_isIDc(name[j]) && table_name[j] == NUL) 3016 return key_names_table[i].key; 3017 } 3018 return 0; 3019 } 3020 3021 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) 3022 char_u * 3023 get_key_name(i) 3024 int i; 3025 { 3026 if (i >= (int)KEY_NAMES_TABLE_LEN) 3027 return NULL; 3028 return key_names_table[i].name; 3029 } 3030 #endif 3031 3032 #if defined(FEAT_MOUSE) || defined(PROTO) 3033 /* 3034 * Look up the given mouse code to return the relevant information in the other 3035 * arguments. Return which button is down or was released. 3036 */ 3037 int 3038 get_mouse_button(code, is_click, is_drag) 3039 int code; 3040 int *is_click; 3041 int *is_drag; 3042 { 3043 int i; 3044 3045 for (i = 0; mouse_table[i].pseudo_code; i++) 3046 if (code == mouse_table[i].pseudo_code) 3047 { 3048 *is_click = mouse_table[i].is_click; 3049 *is_drag = mouse_table[i].is_drag; 3050 return mouse_table[i].button; 3051 } 3052 return 0; /* Shouldn't get here */ 3053 } 3054 3055 /* 3056 * Return the appropriate pseudo mouse event token (KE_LEFTMOUSE etc) based on 3057 * the given information about which mouse button is down, and whether the 3058 * mouse was clicked, dragged or released. 3059 */ 3060 int 3061 get_pseudo_mouse_code(button, is_click, is_drag) 3062 int button; /* eg MOUSE_LEFT */ 3063 int is_click; 3064 int is_drag; 3065 { 3066 int i; 3067 3068 for (i = 0; mouse_table[i].pseudo_code; i++) 3069 if (button == mouse_table[i].button 3070 && is_click == mouse_table[i].is_click 3071 && is_drag == mouse_table[i].is_drag) 3072 { 3073 #ifdef FEAT_GUI 3074 /* Trick: a non mappable left click and release has mouse_col -1 3075 * or added MOUSE_COLOFF. Used for 'mousefocus' in 3076 * gui_mouse_moved() */ 3077 if (mouse_col < 0 || mouse_col > MOUSE_COLOFF) 3078 { 3079 if (mouse_col < 0) 3080 mouse_col = 0; 3081 else 3082 mouse_col -= MOUSE_COLOFF; 3083 if (mouse_table[i].pseudo_code == (int)KE_LEFTMOUSE) 3084 return (int)KE_LEFTMOUSE_NM; 3085 if (mouse_table[i].pseudo_code == (int)KE_LEFTRELEASE) 3086 return (int)KE_LEFTRELEASE_NM; 3087 } 3088 #endif 3089 return mouse_table[i].pseudo_code; 3090 } 3091 return (int)KE_IGNORE; /* not recognized, ignore it */ 3092 } 3093 #endif /* FEAT_MOUSE */ 3094 3095 /* 3096 * Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC. 3097 */ 3098 int 3099 get_fileformat(buf) 3100 buf_T *buf; 3101 { 3102 int c = *buf->b_p_ff; 3103 3104 if (buf->b_p_bin || c == 'u') 3105 return EOL_UNIX; 3106 if (c == 'm') 3107 return EOL_MAC; 3108 return EOL_DOS; 3109 } 3110 3111 /* 3112 * Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val" 3113 * argument. 3114 */ 3115 int 3116 get_fileformat_force(buf, eap) 3117 buf_T *buf; 3118 exarg_T *eap; /* can be NULL! */ 3119 { 3120 int c; 3121 3122 if (eap != NULL && eap->force_ff != 0) 3123 c = eap->cmd[eap->force_ff]; 3124 else 3125 { 3126 if ((eap != NULL && eap->force_bin != 0) 3127 ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin) 3128 return EOL_UNIX; 3129 c = *buf->b_p_ff; 3130 } 3131 if (c == 'u') 3132 return EOL_UNIX; 3133 if (c == 'm') 3134 return EOL_MAC; 3135 return EOL_DOS; 3136 } 3137 3138 /* 3139 * Set the current end-of-line type to EOL_DOS, EOL_UNIX or EOL_MAC. 3140 * Sets both 'textmode' and 'fileformat'. 3141 * Note: Does _not_ set global value of 'textmode'! 3142 */ 3143 void 3144 set_fileformat(t, opt_flags) 3145 int t; 3146 int opt_flags; /* OPT_LOCAL and/or OPT_GLOBAL */ 3147 { 3148 char *p = NULL; 3149 3150 switch (t) 3151 { 3152 case EOL_DOS: 3153 p = FF_DOS; 3154 curbuf->b_p_tx = TRUE; 3155 break; 3156 case EOL_UNIX: 3157 p = FF_UNIX; 3158 curbuf->b_p_tx = FALSE; 3159 break; 3160 case EOL_MAC: 3161 p = FF_MAC; 3162 curbuf->b_p_tx = FALSE; 3163 break; 3164 } 3165 if (p != NULL) 3166 set_string_option_direct((char_u *)"ff", -1, (char_u *)p, 3167 OPT_FREE | opt_flags, 0); 3168 3169 #ifdef FEAT_WINDOWS 3170 /* This may cause the buffer to become (un)modified. */ 3171 check_status(curbuf); 3172 redraw_tabline = TRUE; 3173 #endif 3174 #ifdef FEAT_TITLE 3175 need_maketitle = TRUE; /* set window title later */ 3176 #endif 3177 } 3178 3179 /* 3180 * Return the default fileformat from 'fileformats'. 3181 */ 3182 int 3183 default_fileformat() 3184 { 3185 switch (*p_ffs) 3186 { 3187 case 'm': return EOL_MAC; 3188 case 'd': return EOL_DOS; 3189 } 3190 return EOL_UNIX; 3191 } 3192 3193 /* 3194 * Call shell. Calls mch_call_shell, with 'shellxquote' added. 3195 */ 3196 int 3197 call_shell(cmd, opt) 3198 char_u *cmd; 3199 int opt; 3200 { 3201 char_u *ncmd; 3202 int retval; 3203 #ifdef FEAT_PROFILE 3204 proftime_T wait_time; 3205 #endif 3206 3207 if (p_verbose > 3) 3208 { 3209 verbose_enter(); 3210 smsg((char_u *)_("Calling shell to execute: \"%s\""), 3211 cmd == NULL ? p_sh : cmd); 3212 out_char('\n'); 3213 cursor_on(); 3214 verbose_leave(); 3215 } 3216 3217 #ifdef FEAT_PROFILE 3218 if (do_profiling == PROF_YES) 3219 prof_child_enter(&wait_time); 3220 #endif 3221 3222 if (*p_sh == NUL) 3223 { 3224 EMSG(_(e_shellempty)); 3225 retval = -1; 3226 } 3227 else 3228 { 3229 #ifdef FEAT_GUI_MSWIN 3230 /* Don't hide the pointer while executing a shell command. */ 3231 gui_mch_mousehide(FALSE); 3232 #endif 3233 #ifdef FEAT_GUI 3234 ++hold_gui_events; 3235 #endif 3236 /* The external command may update a tags file, clear cached tags. */ 3237 tag_freematch(); 3238 3239 if (cmd == NULL || *p_sxq == NUL) 3240 retval = mch_call_shell(cmd, opt); 3241 else 3242 { 3243 char_u *ecmd = cmd; 3244 3245 if (*p_sxe != NUL && STRCMP(p_sxq, "(") == 0) 3246 { 3247 ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE); 3248 if (ecmd == NULL) 3249 ecmd = cmd; 3250 } 3251 ncmd = alloc((unsigned)(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1)); 3252 if (ncmd != NULL) 3253 { 3254 STRCPY(ncmd, p_sxq); 3255 STRCAT(ncmd, ecmd); 3256 /* When 'shellxquote' is ( append ). 3257 * When 'shellxquote' is "( append )". */ 3258 STRCAT(ncmd, STRCMP(p_sxq, "(") == 0 ? (char_u *)")" 3259 : STRCMP(p_sxq, "\"(") == 0 ? (char_u *)")\"" 3260 : p_sxq); 3261 retval = mch_call_shell(ncmd, opt); 3262 vim_free(ncmd); 3263 } 3264 else 3265 retval = -1; 3266 if (ecmd != cmd) 3267 vim_free(ecmd); 3268 } 3269 #ifdef FEAT_GUI 3270 --hold_gui_events; 3271 #endif 3272 /* 3273 * Check the window size, in case it changed while executing the 3274 * external command. 3275 */ 3276 shell_resized_check(); 3277 } 3278 3279 #ifdef FEAT_EVAL 3280 set_vim_var_nr(VV_SHELL_ERROR, (long)retval); 3281 # ifdef FEAT_PROFILE 3282 if (do_profiling == PROF_YES) 3283 prof_child_exit(&wait_time); 3284 # endif 3285 #endif 3286 3287 return retval; 3288 } 3289 3290 /* 3291 * VISUAL, SELECTMODE and OP_PENDING State are never set, they are equal to 3292 * NORMAL State with a condition. This function returns the real State. 3293 */ 3294 int 3295 get_real_state() 3296 { 3297 if (State & NORMAL) 3298 { 3299 if (VIsual_active) 3300 { 3301 if (VIsual_select) 3302 return SELECTMODE; 3303 return VISUAL; 3304 } 3305 else if (finish_op) 3306 return OP_PENDING; 3307 } 3308 return State; 3309 } 3310 3311 #if defined(FEAT_MBYTE) || defined(PROTO) 3312 /* 3313 * Return TRUE if "p" points to just after a path separator. 3314 * Takes care of multi-byte characters. 3315 * "b" must point to the start of the file name 3316 */ 3317 int 3318 after_pathsep(b, p) 3319 char_u *b; 3320 char_u *p; 3321 { 3322 return p > b && vim_ispathsep(p[-1]) 3323 && (!has_mbyte || (*mb_head_off)(b, p - 1) == 0); 3324 } 3325 #endif 3326 3327 /* 3328 * Return TRUE if file names "f1" and "f2" are in the same directory. 3329 * "f1" may be a short name, "f2" must be a full path. 3330 */ 3331 int 3332 same_directory(f1, f2) 3333 char_u *f1; 3334 char_u *f2; 3335 { 3336 char_u ffname[MAXPATHL]; 3337 char_u *t1; 3338 char_u *t2; 3339 3340 /* safety check */ 3341 if (f1 == NULL || f2 == NULL) 3342 return FALSE; 3343 3344 (void)vim_FullName(f1, ffname, MAXPATHL, FALSE); 3345 t1 = gettail_sep(ffname); 3346 t2 = gettail_sep(f2); 3347 return (t1 - ffname == t2 - f2 3348 && pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0); 3349 } 3350 3351 #if defined(FEAT_SESSION) || defined(MSWIN) || defined(FEAT_GUI_MAC) \ 3352 || ((defined(FEAT_GUI_GTK)) \ 3353 && ( defined(FEAT_WINDOWS) || defined(FEAT_DND)) ) \ 3354 || defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \ 3355 || defined(PROTO) 3356 /* 3357 * Change to a file's directory. 3358 * Caller must call shorten_fnames()! 3359 * Return OK or FAIL. 3360 */ 3361 int 3362 vim_chdirfile(fname) 3363 char_u *fname; 3364 { 3365 char_u dir[MAXPATHL]; 3366 3367 vim_strncpy(dir, fname, MAXPATHL - 1); 3368 *gettail_sep(dir) = NUL; 3369 return mch_chdir((char *)dir) == 0 ? OK : FAIL; 3370 } 3371 #endif 3372 3373 #if defined(STAT_IGNORES_SLASH) || defined(PROTO) 3374 /* 3375 * Check if "name" ends in a slash and is not a directory. 3376 * Used for systems where stat() ignores a trailing slash on a file name. 3377 * The Vim code assumes a trailing slash is only ignored for a directory. 3378 */ 3379 int 3380 illegal_slash(name) 3381 char *name; 3382 { 3383 if (name[0] == NUL) 3384 return FALSE; /* no file name is not illegal */ 3385 if (name[strlen(name) - 1] != '/') 3386 return FALSE; /* no trailing slash */ 3387 if (mch_isdir((char_u *)name)) 3388 return FALSE; /* trailing slash for a directory */ 3389 return TRUE; 3390 } 3391 #endif 3392 3393 #if defined(CURSOR_SHAPE) || defined(PROTO) 3394 3395 /* 3396 * Handling of cursor and mouse pointer shapes in various modes. 3397 */ 3398 3399 cursorentry_T shape_table[SHAPE_IDX_COUNT] = 3400 { 3401 /* The values will be filled in from the 'guicursor' and 'mouseshape' 3402 * defaults when Vim starts. 3403 * Adjust the SHAPE_IDX_ defines when making changes! */ 3404 {0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE}, 3405 {0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE}, 3406 {0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE}, 3407 {0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE}, 3408 {0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE}, 3409 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE}, 3410 {0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE}, 3411 {0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE}, 3412 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE}, 3413 {0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE}, 3414 {0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE}, 3415 {0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE}, 3416 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE}, 3417 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE}, 3418 {0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE}, 3419 {0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE}, 3420 {0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR}, 3421 }; 3422 3423 #ifdef FEAT_MOUSESHAPE 3424 /* 3425 * Table with names for mouse shapes. Keep in sync with all the tables for 3426 * mch_set_mouse_shape()!. 3427 */ 3428 static char * mshape_names[] = 3429 { 3430 "arrow", /* default, must be the first one */ 3431 "blank", /* hidden */ 3432 "beam", 3433 "updown", 3434 "udsizing", 3435 "leftright", 3436 "lrsizing", 3437 "busy", 3438 "no", 3439 "crosshair", 3440 "hand1", 3441 "hand2", 3442 "pencil", 3443 "question", 3444 "rightup-arrow", 3445 "up-arrow", 3446 NULL 3447 }; 3448 #endif 3449 3450 /* 3451 * Parse the 'guicursor' option ("what" is SHAPE_CURSOR) or 'mouseshape' 3452 * ("what" is SHAPE_MOUSE). 3453 * Returns error message for an illegal option, NULL otherwise. 3454 */ 3455 char_u * 3456 parse_shape_opt(what) 3457 int what; 3458 { 3459 char_u *modep; 3460 char_u *colonp; 3461 char_u *commap; 3462 char_u *slashp; 3463 char_u *p, *endp; 3464 int idx = 0; /* init for GCC */ 3465 int all_idx; 3466 int len; 3467 int i; 3468 long n; 3469 int found_ve = FALSE; /* found "ve" flag */ 3470 int round; 3471 3472 /* 3473 * First round: check for errors; second round: do it for real. 3474 */ 3475 for (round = 1; round <= 2; ++round) 3476 { 3477 /* 3478 * Repeat for all comma separated parts. 3479 */ 3480 #ifdef FEAT_MOUSESHAPE 3481 if (what == SHAPE_MOUSE) 3482 modep = p_mouseshape; 3483 else 3484 #endif 3485 modep = p_guicursor; 3486 while (*modep != NUL) 3487 { 3488 colonp = vim_strchr(modep, ':'); 3489 if (colonp == NULL) 3490 return (char_u *)N_("E545: Missing colon"); 3491 if (colonp == modep) 3492 return (char_u *)N_("E546: Illegal mode"); 3493 commap = vim_strchr(modep, ','); 3494 3495 /* 3496 * Repeat for all mode's before the colon. 3497 * For the 'a' mode, we loop to handle all the modes. 3498 */ 3499 all_idx = -1; 3500 while (modep < colonp || all_idx >= 0) 3501 { 3502 if (all_idx < 0) 3503 { 3504 /* Find the mode. */ 3505 if (modep[1] == '-' || modep[1] == ':') 3506 len = 1; 3507 else 3508 len = 2; 3509 if (len == 1 && TOLOWER_ASC(modep[0]) == 'a') 3510 all_idx = SHAPE_IDX_COUNT - 1; 3511 else 3512 { 3513 for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx) 3514 if (STRNICMP(modep, shape_table[idx].name, len) 3515 == 0) 3516 break; 3517 if (idx == SHAPE_IDX_COUNT 3518 || (shape_table[idx].used_for & what) == 0) 3519 return (char_u *)N_("E546: Illegal mode"); 3520 if (len == 2 && modep[0] == 'v' && modep[1] == 'e') 3521 found_ve = TRUE; 3522 } 3523 modep += len + 1; 3524 } 3525 3526 if (all_idx >= 0) 3527 idx = all_idx--; 3528 else if (round == 2) 3529 { 3530 #ifdef FEAT_MOUSESHAPE 3531 if (what == SHAPE_MOUSE) 3532 { 3533 /* Set the default, for the missing parts */ 3534 shape_table[idx].mshape = 0; 3535 } 3536 else 3537 #endif 3538 { 3539 /* Set the defaults, for the missing parts */ 3540 shape_table[idx].shape = SHAPE_BLOCK; 3541 shape_table[idx].blinkwait = 700L; 3542 shape_table[idx].blinkon = 400L; 3543 shape_table[idx].blinkoff = 250L; 3544 } 3545 } 3546 3547 /* Parse the part after the colon */ 3548 for (p = colonp + 1; *p && *p != ','; ) 3549 { 3550 #ifdef FEAT_MOUSESHAPE 3551 if (what == SHAPE_MOUSE) 3552 { 3553 for (i = 0; ; ++i) 3554 { 3555 if (mshape_names[i] == NULL) 3556 { 3557 if (!VIM_ISDIGIT(*p)) 3558 return (char_u *)N_("E547: Illegal mouseshape"); 3559 if (round == 2) 3560 shape_table[idx].mshape = 3561 getdigits(&p) + MSHAPE_NUMBERED; 3562 else 3563 (void)getdigits(&p); 3564 break; 3565 } 3566 len = (int)STRLEN(mshape_names[i]); 3567 if (STRNICMP(p, mshape_names[i], len) == 0) 3568 { 3569 if (round == 2) 3570 shape_table[idx].mshape = i; 3571 p += len; 3572 break; 3573 } 3574 } 3575 } 3576 else /* if (what == SHAPE_MOUSE) */ 3577 #endif 3578 { 3579 /* 3580 * First handle the ones with a number argument. 3581 */ 3582 i = *p; 3583 len = 0; 3584 if (STRNICMP(p, "ver", 3) == 0) 3585 len = 3; 3586 else if (STRNICMP(p, "hor", 3) == 0) 3587 len = 3; 3588 else if (STRNICMP(p, "blinkwait", 9) == 0) 3589 len = 9; 3590 else if (STRNICMP(p, "blinkon", 7) == 0) 3591 len = 7; 3592 else if (STRNICMP(p, "blinkoff", 8) == 0) 3593 len = 8; 3594 if (len != 0) 3595 { 3596 p += len; 3597 if (!VIM_ISDIGIT(*p)) 3598 return (char_u *)N_("E548: digit expected"); 3599 n = getdigits(&p); 3600 if (len == 3) /* "ver" or "hor" */ 3601 { 3602 if (n == 0) 3603 return (char_u *)N_("E549: Illegal percentage"); 3604 if (round == 2) 3605 { 3606 if (TOLOWER_ASC(i) == 'v') 3607 shape_table[idx].shape = SHAPE_VER; 3608 else 3609 shape_table[idx].shape = SHAPE_HOR; 3610 shape_table[idx].percentage = n; 3611 } 3612 } 3613 else if (round == 2) 3614 { 3615 if (len == 9) 3616 shape_table[idx].blinkwait = n; 3617 else if (len == 7) 3618 shape_table[idx].blinkon = n; 3619 else 3620 shape_table[idx].blinkoff = n; 3621 } 3622 } 3623 else if (STRNICMP(p, "block", 5) == 0) 3624 { 3625 if (round == 2) 3626 shape_table[idx].shape = SHAPE_BLOCK; 3627 p += 5; 3628 } 3629 else /* must be a highlight group name then */ 3630 { 3631 endp = vim_strchr(p, '-'); 3632 if (commap == NULL) /* last part */ 3633 { 3634 if (endp == NULL) 3635 endp = p + STRLEN(p); /* find end of part */ 3636 } 3637 else if (endp > commap || endp == NULL) 3638 endp = commap; 3639 slashp = vim_strchr(p, '/'); 3640 if (slashp != NULL && slashp < endp) 3641 { 3642 /* "group/langmap_group" */ 3643 i = syn_check_group(p, (int)(slashp - p)); 3644 p = slashp + 1; 3645 } 3646 if (round == 2) 3647 { 3648 shape_table[idx].id = syn_check_group(p, 3649 (int)(endp - p)); 3650 shape_table[idx].id_lm = shape_table[idx].id; 3651 if (slashp != NULL && slashp < endp) 3652 shape_table[idx].id = i; 3653 } 3654 p = endp; 3655 } 3656 } /* if (what != SHAPE_MOUSE) */ 3657 3658 if (*p == '-') 3659 ++p; 3660 } 3661 } 3662 modep = p; 3663 if (*modep == ',') 3664 ++modep; 3665 } 3666 } 3667 3668 /* If the 's' flag is not given, use the 'v' cursor for 's' */ 3669 if (!found_ve) 3670 { 3671 #ifdef FEAT_MOUSESHAPE 3672 if (what == SHAPE_MOUSE) 3673 { 3674 shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape; 3675 } 3676 else 3677 #endif 3678 { 3679 shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape; 3680 shape_table[SHAPE_IDX_VE].percentage = 3681 shape_table[SHAPE_IDX_V].percentage; 3682 shape_table[SHAPE_IDX_VE].blinkwait = 3683 shape_table[SHAPE_IDX_V].blinkwait; 3684 shape_table[SHAPE_IDX_VE].blinkon = 3685 shape_table[SHAPE_IDX_V].blinkon; 3686 shape_table[SHAPE_IDX_VE].blinkoff = 3687 shape_table[SHAPE_IDX_V].blinkoff; 3688 shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id; 3689 shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm; 3690 } 3691 } 3692 3693 return NULL; 3694 } 3695 3696 # if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \ 3697 || defined(FEAT_MOUSESHAPE) || defined(PROTO) 3698 /* 3699 * Return the index into shape_table[] for the current mode. 3700 * When "mouse" is TRUE, consider indexes valid for the mouse pointer. 3701 */ 3702 int 3703 get_shape_idx(mouse) 3704 int mouse; 3705 { 3706 #ifdef FEAT_MOUSESHAPE 3707 if (mouse && (State == HITRETURN || State == ASKMORE)) 3708 { 3709 # ifdef FEAT_GUI 3710 int x, y; 3711 gui_mch_getmouse(&x, &y); 3712 if (Y_2_ROW(y) == Rows - 1) 3713 return SHAPE_IDX_MOREL; 3714 # endif 3715 return SHAPE_IDX_MORE; 3716 } 3717 if (mouse && drag_status_line) 3718 return SHAPE_IDX_SDRAG; 3719 # ifdef FEAT_VERTSPLIT 3720 if (mouse && drag_sep_line) 3721 return SHAPE_IDX_VDRAG; 3722 # endif 3723 #endif 3724 if (!mouse && State == SHOWMATCH) 3725 return SHAPE_IDX_SM; 3726 #ifdef FEAT_VREPLACE 3727 if (State & VREPLACE_FLAG) 3728 return SHAPE_IDX_R; 3729 #endif 3730 if (State & REPLACE_FLAG) 3731 return SHAPE_IDX_R; 3732 if (State & INSERT) 3733 return SHAPE_IDX_I; 3734 if (State & CMDLINE) 3735 { 3736 if (cmdline_at_end()) 3737 return SHAPE_IDX_C; 3738 if (cmdline_overstrike()) 3739 return SHAPE_IDX_CR; 3740 return SHAPE_IDX_CI; 3741 } 3742 if (finish_op) 3743 return SHAPE_IDX_O; 3744 if (VIsual_active) 3745 { 3746 if (*p_sel == 'e') 3747 return SHAPE_IDX_VE; 3748 else 3749 return SHAPE_IDX_V; 3750 } 3751 return SHAPE_IDX_N; 3752 } 3753 #endif 3754 3755 # if defined(FEAT_MOUSESHAPE) || defined(PROTO) 3756 static int old_mouse_shape = 0; 3757 3758 /* 3759 * Set the mouse shape: 3760 * If "shape" is -1, use shape depending on the current mode, 3761 * depending on the current state. 3762 * If "shape" is -2, only update the shape when it's CLINE or STATUS (used 3763 * when the mouse moves off the status or command line). 3764 */ 3765 void 3766 update_mouseshape(shape_idx) 3767 int shape_idx; 3768 { 3769 int new_mouse_shape; 3770 3771 /* Only works in GUI mode. */ 3772 if (!gui.in_use || gui.starting) 3773 return; 3774 3775 /* Postpone the updating when more is to come. Speeds up executing of 3776 * mappings. */ 3777 if (shape_idx == -1 && char_avail()) 3778 { 3779 postponed_mouseshape = TRUE; 3780 return; 3781 } 3782 3783 /* When ignoring the mouse don't change shape on the statusline. */ 3784 if (*p_mouse == NUL 3785 && (shape_idx == SHAPE_IDX_CLINE 3786 || shape_idx == SHAPE_IDX_STATUS 3787 || shape_idx == SHAPE_IDX_VSEP)) 3788 shape_idx = -2; 3789 3790 if (shape_idx == -2 3791 && old_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape 3792 && old_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape 3793 && old_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape) 3794 return; 3795 if (shape_idx < 0) 3796 new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape; 3797 else 3798 new_mouse_shape = shape_table[shape_idx].mshape; 3799 if (new_mouse_shape != old_mouse_shape) 3800 { 3801 mch_set_mouse_shape(new_mouse_shape); 3802 old_mouse_shape = new_mouse_shape; 3803 } 3804 postponed_mouseshape = FALSE; 3805 } 3806 # endif 3807 3808 #endif /* CURSOR_SHAPE */ 3809 3810 3811 /* TODO: make some #ifdef for this */ 3812 /*--------[ file searching ]-------------------------------------------------*/ 3813 /* 3814 * File searching functions for 'path', 'tags' and 'cdpath' options. 3815 * External visible functions: 3816 * vim_findfile_init() creates/initialises the search context 3817 * vim_findfile_free_visited() free list of visited files/dirs of search 3818 * context 3819 * vim_findfile() find a file in the search context 3820 * vim_findfile_cleanup() cleanup/free search context created by 3821 * vim_findfile_init() 3822 * 3823 * All static functions and variables start with 'ff_' 3824 * 3825 * In general it works like this: 3826 * First you create yourself a search context by calling vim_findfile_init(). 3827 * It is possible to give a search context from a previous call to 3828 * vim_findfile_init(), so it can be reused. After this you call vim_findfile() 3829 * until you are satisfied with the result or it returns NULL. On every call it 3830 * returns the next file which matches the conditions given to 3831 * vim_findfile_init(). If it doesn't find a next file it returns NULL. 3832 * 3833 * It is possible to call vim_findfile_init() again to reinitialise your search 3834 * with some new parameters. Don't forget to pass your old search context to 3835 * it, so it can reuse it and especially reuse the list of already visited 3836 * directories. If you want to delete the list of already visited directories 3837 * simply call vim_findfile_free_visited(). 3838 * 3839 * When you are done call vim_findfile_cleanup() to free the search context. 3840 * 3841 * The function vim_findfile_init() has a long comment, which describes the 3842 * needed parameters. 3843 * 3844 * 3845 * 3846 * ATTENTION: 3847 * ========== 3848 * Also we use an allocated search context here, this functions are NOT 3849 * thread-safe!!!!! 3850 * 3851 * To minimize parameter passing (or because I'm to lazy), only the 3852 * external visible functions get a search context as a parameter. This is 3853 * then assigned to a static global, which is used throughout the local 3854 * functions. 3855 */ 3856 3857 /* 3858 * type for the directory search stack 3859 */ 3860 typedef struct ff_stack 3861 { 3862 struct ff_stack *ffs_prev; 3863 3864 /* the fix part (no wildcards) and the part containing the wildcards 3865 * of the search path 3866 */ 3867 char_u *ffs_fix_path; 3868 #ifdef FEAT_PATH_EXTRA 3869 char_u *ffs_wc_path; 3870 #endif 3871 3872 /* files/dirs found in the above directory, matched by the first wildcard 3873 * of wc_part 3874 */ 3875 char_u **ffs_filearray; 3876 int ffs_filearray_size; 3877 char_u ffs_filearray_cur; /* needed for partly handled dirs */ 3878 3879 /* to store status of partly handled directories 3880 * 0: we work on this directory for the first time 3881 * 1: this directory was partly searched in an earlier step 3882 */ 3883 int ffs_stage; 3884 3885 /* How deep are we in the directory tree? 3886 * Counts backward from value of level parameter to vim_findfile_init 3887 */ 3888 int ffs_level; 3889 3890 /* Did we already expand '**' to an empty string? */ 3891 int ffs_star_star_empty; 3892 } ff_stack_T; 3893 3894 /* 3895 * type for already visited directories or files. 3896 */ 3897 typedef struct ff_visited 3898 { 3899 struct ff_visited *ffv_next; 3900 3901 #ifdef FEAT_PATH_EXTRA 3902 /* Visited directories are different if the wildcard string are 3903 * different. So we have to save it. 3904 */ 3905 char_u *ffv_wc_path; 3906 #endif 3907 /* for unix use inode etc for comparison (needed because of links), else 3908 * use filename. 3909 */ 3910 #ifdef UNIX 3911 int ffv_dev_valid; /* ffv_dev and ffv_ino were set */ 3912 dev_t ffv_dev; /* device number */ 3913 ino_t ffv_ino; /* inode number */ 3914 #endif 3915 /* The memory for this struct is allocated according to the length of 3916 * ffv_fname. 3917 */ 3918 char_u ffv_fname[1]; /* actually longer */ 3919 } ff_visited_T; 3920 3921 /* 3922 * We might have to manage several visited lists during a search. 3923 * This is especially needed for the tags option. If tags is set to: 3924 * "./++/tags,./++/TAGS,++/tags" (replace + with *) 3925 * So we have to do 3 searches: 3926 * 1) search from the current files directory downward for the file "tags" 3927 * 2) search from the current files directory downward for the file "TAGS" 3928 * 3) search from Vims current directory downwards for the file "tags" 3929 * As you can see, the first and the third search are for the same file, so for 3930 * the third search we can use the visited list of the first search. For the 3931 * second search we must start from a empty visited list. 3932 * The struct ff_visited_list_hdr is used to manage a linked list of already 3933 * visited lists. 3934 */ 3935 typedef struct ff_visited_list_hdr 3936 { 3937 struct ff_visited_list_hdr *ffvl_next; 3938 3939 /* the filename the attached visited list is for */ 3940 char_u *ffvl_filename; 3941 3942 ff_visited_T *ffvl_visited_list; 3943 3944 } ff_visited_list_hdr_T; 3945 3946 3947 /* 3948 * '**' can be expanded to several directory levels. 3949 * Set the default maximum depth. 3950 */ 3951 #define FF_MAX_STAR_STAR_EXPAND ((char_u)30) 3952 3953 /* 3954 * The search context: 3955 * ffsc_stack_ptr: the stack for the dirs to search 3956 * ffsc_visited_list: the currently active visited list 3957 * ffsc_dir_visited_list: the currently active visited list for search dirs 3958 * ffsc_visited_lists_list: the list of all visited lists 3959 * ffsc_dir_visited_lists_list: the list of all visited lists for search dirs 3960 * ffsc_file_to_search: the file to search for 3961 * ffsc_start_dir: the starting directory, if search path was relative 3962 * ffsc_fix_path: the fix part of the given path (without wildcards) 3963 * Needed for upward search. 3964 * ffsc_wc_path: the part of the given path containing wildcards 3965 * ffsc_level: how many levels of dirs to search downwards 3966 * ffsc_stopdirs_v: array of stop directories for upward search 3967 * ffsc_find_what: FINDFILE_BOTH, FINDFILE_DIR or FINDFILE_FILE 3968 * ffsc_tagfile: searching for tags file, don't use 'suffixesadd' 3969 */ 3970 typedef struct ff_search_ctx_T 3971 { 3972 ff_stack_T *ffsc_stack_ptr; 3973 ff_visited_list_hdr_T *ffsc_visited_list; 3974 ff_visited_list_hdr_T *ffsc_dir_visited_list; 3975 ff_visited_list_hdr_T *ffsc_visited_lists_list; 3976 ff_visited_list_hdr_T *ffsc_dir_visited_lists_list; 3977 char_u *ffsc_file_to_search; 3978 char_u *ffsc_start_dir; 3979 char_u *ffsc_fix_path; 3980 #ifdef FEAT_PATH_EXTRA 3981 char_u *ffsc_wc_path; 3982 int ffsc_level; 3983 char_u **ffsc_stopdirs_v; 3984 #endif 3985 int ffsc_find_what; 3986 int ffsc_tagfile; 3987 } ff_search_ctx_T; 3988 3989 /* locally needed functions */ 3990 #ifdef FEAT_PATH_EXTRA 3991 static int ff_check_visited __ARGS((ff_visited_T **, char_u *, char_u *)); 3992 #else 3993 static int ff_check_visited __ARGS((ff_visited_T **, char_u *)); 3994 #endif 3995 static void vim_findfile_free_visited_list __ARGS((ff_visited_list_hdr_T **list_headp)); 3996 static void ff_free_visited_list __ARGS((ff_visited_T *vl)); 3997 static ff_visited_list_hdr_T* ff_get_visited_list __ARGS((char_u *, ff_visited_list_hdr_T **list_headp)); 3998 #ifdef FEAT_PATH_EXTRA 3999 static int ff_wc_equal __ARGS((char_u *s1, char_u *s2)); 4000 #endif 4001 4002 static void ff_push __ARGS((ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr)); 4003 static ff_stack_T *ff_pop __ARGS((ff_search_ctx_T *search_ctx)); 4004 static void ff_clear __ARGS((ff_search_ctx_T *search_ctx)); 4005 static void ff_free_stack_element __ARGS((ff_stack_T *stack_ptr)); 4006 #ifdef FEAT_PATH_EXTRA 4007 static ff_stack_T *ff_create_stack_element __ARGS((char_u *, char_u *, int, int)); 4008 #else 4009 static ff_stack_T *ff_create_stack_element __ARGS((char_u *, int, int)); 4010 #endif 4011 #ifdef FEAT_PATH_EXTRA 4012 static int ff_path_in_stoplist __ARGS((char_u *, int, char_u **)); 4013 #endif 4014 4015 static char_u e_pathtoolong[] = N_("E854: path too long for completion"); 4016 4017 #if 0 4018 /* 4019 * if someone likes findfirst/findnext, here are the functions 4020 * NOT TESTED!! 4021 */ 4022 4023 static void *ff_fn_search_context = NULL; 4024 4025 char_u * 4026 vim_findfirst(path, filename, level) 4027 char_u *path; 4028 char_u *filename; 4029 int level; 4030 { 4031 ff_fn_search_context = 4032 vim_findfile_init(path, filename, NULL, level, TRUE, FALSE, 4033 ff_fn_search_context, rel_fname); 4034 if (NULL == ff_fn_search_context) 4035 return NULL; 4036 else 4037 return vim_findnext() 4038 } 4039 4040 char_u * 4041 vim_findnext() 4042 { 4043 char_u *ret = vim_findfile(ff_fn_search_context); 4044 4045 if (NULL == ret) 4046 { 4047 vim_findfile_cleanup(ff_fn_search_context); 4048 ff_fn_search_context = NULL; 4049 } 4050 return ret; 4051 } 4052 #endif 4053 4054 /* 4055 * Initialization routine for vim_findfile(). 4056 * 4057 * Returns the newly allocated search context or NULL if an error occurred. 4058 * 4059 * Don't forget to clean up by calling vim_findfile_cleanup() if you are done 4060 * with the search context. 4061 * 4062 * Find the file 'filename' in the directory 'path'. 4063 * The parameter 'path' may contain wildcards. If so only search 'level' 4064 * directories deep. The parameter 'level' is the absolute maximum and is 4065 * not related to restricts given to the '**' wildcard. If 'level' is 100 4066 * and you use '**200' vim_findfile() will stop after 100 levels. 4067 * 4068 * 'filename' cannot contain wildcards! It is used as-is, no backslashes to 4069 * escape special characters. 4070 * 4071 * If 'stopdirs' is not NULL and nothing is found downward, the search is 4072 * restarted on the next higher directory level. This is repeated until the 4073 * start-directory of a search is contained in 'stopdirs'. 'stopdirs' has the 4074 * format ";*<dirname>*\(;<dirname>\)*;\=$". 4075 * 4076 * If the 'path' is relative, the starting dir for the search is either VIM's 4077 * current dir or if the path starts with "./" the current files dir. 4078 * If the 'path' is absolute, the starting dir is that part of the path before 4079 * the first wildcard. 4080 * 4081 * Upward search is only done on the starting dir. 4082 * 4083 * If 'free_visited' is TRUE the list of already visited files/directories is 4084 * cleared. Set this to FALSE if you just want to search from another 4085 * directory, but want to be sure that no directory from a previous search is 4086 * searched again. This is useful if you search for a file at different places. 4087 * The list of visited files/dirs can also be cleared with the function 4088 * vim_findfile_free_visited(). 4089 * 4090 * Set the parameter 'find_what' to FINDFILE_DIR if you want to search for 4091 * directories only, FINDFILE_FILE for files only, FINDFILE_BOTH for both. 4092 * 4093 * A search context returned by a previous call to vim_findfile_init() can be 4094 * passed in the parameter "search_ctx_arg". This context is reused and 4095 * reinitialized with the new parameters. The list of already visited 4096 * directories from this context is only deleted if the parameter 4097 * "free_visited" is true. Be aware that the passed "search_ctx_arg" is freed 4098 * if the reinitialization fails. 4099 * 4100 * If you don't have a search context from a previous call "search_ctx_arg" 4101 * must be NULL. 4102 * 4103 * This function silently ignores a few errors, vim_findfile() will have 4104 * limited functionality then. 4105 */ 4106 void * 4107 vim_findfile_init(path, filename, stopdirs, level, free_visited, find_what, 4108 search_ctx_arg, tagfile, rel_fname) 4109 char_u *path; 4110 char_u *filename; 4111 char_u *stopdirs UNUSED; 4112 int level; 4113 int free_visited; 4114 int find_what; 4115 void *search_ctx_arg; 4116 int tagfile; /* expanding names of tags files */ 4117 char_u *rel_fname; /* file name to use for "." */ 4118 { 4119 #ifdef FEAT_PATH_EXTRA 4120 char_u *wc_part; 4121 #endif 4122 ff_stack_T *sptr; 4123 ff_search_ctx_T *search_ctx; 4124 4125 /* If a search context is given by the caller, reuse it, else allocate a 4126 * new one. 4127 */ 4128 if (search_ctx_arg != NULL) 4129 search_ctx = search_ctx_arg; 4130 else 4131 { 4132 search_ctx = (ff_search_ctx_T*)alloc((unsigned)sizeof(ff_search_ctx_T)); 4133 if (search_ctx == NULL) 4134 goto error_return; 4135 vim_memset(search_ctx, 0, sizeof(ff_search_ctx_T)); 4136 } 4137 search_ctx->ffsc_find_what = find_what; 4138 search_ctx->ffsc_tagfile = tagfile; 4139 4140 /* clear the search context, but NOT the visited lists */ 4141 ff_clear(search_ctx); 4142 4143 /* clear visited list if wanted */ 4144 if (free_visited == TRUE) 4145 vim_findfile_free_visited(search_ctx); 4146 else 4147 { 4148 /* Reuse old visited lists. Get the visited list for the given 4149 * filename. If no list for the current filename exists, creates a new 4150 * one. */ 4151 search_ctx->ffsc_visited_list = ff_get_visited_list(filename, 4152 &search_ctx->ffsc_visited_lists_list); 4153 if (search_ctx->ffsc_visited_list == NULL) 4154 goto error_return; 4155 search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename, 4156 &search_ctx->ffsc_dir_visited_lists_list); 4157 if (search_ctx->ffsc_dir_visited_list == NULL) 4158 goto error_return; 4159 } 4160 4161 if (ff_expand_buffer == NULL) 4162 { 4163 ff_expand_buffer = (char_u*)alloc(MAXPATHL); 4164 if (ff_expand_buffer == NULL) 4165 goto error_return; 4166 } 4167 4168 /* Store information on starting dir now if path is relative. 4169 * If path is absolute, we do that later. */ 4170 if (path[0] == '.' 4171 && (vim_ispathsep(path[1]) || path[1] == NUL) 4172 && (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL) 4173 && rel_fname != NULL) 4174 { 4175 int len = (int)(gettail(rel_fname) - rel_fname); 4176 4177 if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL) 4178 { 4179 /* Make the start dir an absolute path name. */ 4180 vim_strncpy(ff_expand_buffer, rel_fname, len); 4181 search_ctx->ffsc_start_dir = FullName_save(ff_expand_buffer, FALSE); 4182 } 4183 else 4184 search_ctx->ffsc_start_dir = vim_strnsave(rel_fname, len); 4185 if (search_ctx->ffsc_start_dir == NULL) 4186 goto error_return; 4187 if (*++path != NUL) 4188 ++path; 4189 } 4190 else if (*path == NUL || !vim_isAbsName(path)) 4191 { 4192 #ifdef BACKSLASH_IN_FILENAME 4193 /* "c:dir" needs "c:" to be expanded, otherwise use current dir */ 4194 if (*path != NUL && path[1] == ':') 4195 { 4196 char_u drive[3]; 4197 4198 drive[0] = path[0]; 4199 drive[1] = ':'; 4200 drive[2] = NUL; 4201 if (vim_FullName(drive, ff_expand_buffer, MAXPATHL, TRUE) == FAIL) 4202 goto error_return; 4203 path += 2; 4204 } 4205 else 4206 #endif 4207 if (mch_dirname(ff_expand_buffer, MAXPATHL) == FAIL) 4208 goto error_return; 4209 4210 search_ctx->ffsc_start_dir = vim_strsave(ff_expand_buffer); 4211 if (search_ctx->ffsc_start_dir == NULL) 4212 goto error_return; 4213 4214 #ifdef BACKSLASH_IN_FILENAME 4215 /* A path that starts with "/dir" is relative to the drive, not to the 4216 * directory (but not for "//machine/dir"). Only use the drive name. */ 4217 if ((*path == '/' || *path == '\\') 4218 && path[1] != path[0] 4219 && search_ctx->ffsc_start_dir[1] == ':') 4220 search_ctx->ffsc_start_dir[2] = NUL; 4221 #endif 4222 } 4223 4224 #ifdef FEAT_PATH_EXTRA 4225 /* 4226 * If stopdirs are given, split them into an array of pointers. 4227 * If this fails (mem allocation), there is no upward search at all or a 4228 * stop directory is not recognized -> continue silently. 4229 * If stopdirs just contains a ";" or is empty, 4230 * search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This 4231 * is handled as unlimited upward search. See function 4232 * ff_path_in_stoplist() for details. 4233 */ 4234 if (stopdirs != NULL) 4235 { 4236 char_u *walker = stopdirs; 4237 int dircount; 4238 4239 while (*walker == ';') 4240 walker++; 4241 4242 dircount = 1; 4243 search_ctx->ffsc_stopdirs_v = 4244 (char_u **)alloc((unsigned)sizeof(char_u *)); 4245 4246 if (search_ctx->ffsc_stopdirs_v != NULL) 4247 { 4248 do 4249 { 4250 char_u *helper; 4251 void *ptr; 4252 4253 helper = walker; 4254 ptr = vim_realloc(search_ctx->ffsc_stopdirs_v, 4255 (dircount + 1) * sizeof(char_u *)); 4256 if (ptr) 4257 search_ctx->ffsc_stopdirs_v = ptr; 4258 else 4259 /* ignore, keep what we have and continue */ 4260 break; 4261 walker = vim_strchr(walker, ';'); 4262 if (walker) 4263 { 4264 search_ctx->ffsc_stopdirs_v[dircount-1] = 4265 vim_strnsave(helper, (int)(walker - helper)); 4266 walker++; 4267 } 4268 else 4269 /* this might be "", which means ascent till top 4270 * of directory tree. 4271 */ 4272 search_ctx->ffsc_stopdirs_v[dircount-1] = 4273 vim_strsave(helper); 4274 4275 dircount++; 4276 4277 } while (walker != NULL); 4278 search_ctx->ffsc_stopdirs_v[dircount-1] = NULL; 4279 } 4280 } 4281 #endif 4282 4283 #ifdef FEAT_PATH_EXTRA 4284 search_ctx->ffsc_level = level; 4285 4286 /* split into: 4287 * -fix path 4288 * -wildcard_stuff (might be NULL) 4289 */ 4290 wc_part = vim_strchr(path, '*'); 4291 if (wc_part != NULL) 4292 { 4293 int llevel; 4294 int len; 4295 char *errpt; 4296 4297 /* save the fix part of the path */ 4298 search_ctx->ffsc_fix_path = vim_strnsave(path, (int)(wc_part - path)); 4299 4300 /* 4301 * copy wc_path and add restricts to the '**' wildcard. 4302 * The octet after a '**' is used as a (binary) counter. 4303 * So '**3' is transposed to '**^C' ('^C' is ASCII value 3) 4304 * or '**76' is transposed to '**N'( 'N' is ASCII value 76). 4305 * For EBCDIC you get different character values. 4306 * If no restrict is given after '**' the default is used. 4307 * Due to this technique the path looks awful if you print it as a 4308 * string. 4309 */ 4310 len = 0; 4311 while (*wc_part != NUL) 4312 { 4313 if (len + 5 >= MAXPATHL) 4314 { 4315 EMSG(_(e_pathtoolong)); 4316 break; 4317 } 4318 if (STRNCMP(wc_part, "**", 2) == 0) 4319 { 4320 ff_expand_buffer[len++] = *wc_part++; 4321 ff_expand_buffer[len++] = *wc_part++; 4322 4323 llevel = strtol((char *)wc_part, &errpt, 10); 4324 if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255) 4325 ff_expand_buffer[len++] = llevel; 4326 else if ((char_u *)errpt != wc_part && llevel == 0) 4327 /* restrict is 0 -> remove already added '**' */ 4328 len -= 2; 4329 else 4330 ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND; 4331 wc_part = (char_u *)errpt; 4332 if (*wc_part != NUL && !vim_ispathsep(*wc_part)) 4333 { 4334 EMSG2(_("E343: Invalid path: '**[number]' must be at the end of the path or be followed by '%s'."), PATHSEPSTR); 4335 goto error_return; 4336 } 4337 } 4338 else 4339 ff_expand_buffer[len++] = *wc_part++; 4340 } 4341 ff_expand_buffer[len] = NUL; 4342 search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer); 4343 4344 if (search_ctx->ffsc_wc_path == NULL) 4345 goto error_return; 4346 } 4347 else 4348 #endif 4349 search_ctx->ffsc_fix_path = vim_strsave(path); 4350 4351 if (search_ctx->ffsc_start_dir == NULL) 4352 { 4353 /* store the fix part as startdir. 4354 * This is needed if the parameter path is fully qualified. 4355 */ 4356 search_ctx->ffsc_start_dir = vim_strsave(search_ctx->ffsc_fix_path); 4357 if (search_ctx->ffsc_start_dir == NULL) 4358 goto error_return; 4359 search_ctx->ffsc_fix_path[0] = NUL; 4360 } 4361 4362 /* create an absolute path */ 4363 if (STRLEN(search_ctx->ffsc_start_dir) 4364 + STRLEN(search_ctx->ffsc_fix_path) + 3 >= MAXPATHL) 4365 { 4366 EMSG(_(e_pathtoolong)); 4367 goto error_return; 4368 } 4369 STRCPY(ff_expand_buffer, search_ctx->ffsc_start_dir); 4370 add_pathsep(ff_expand_buffer); 4371 { 4372 int eb_len = (int)STRLEN(ff_expand_buffer); 4373 char_u *buf = alloc(eb_len 4374 + (int)STRLEN(search_ctx->ffsc_fix_path) + 1); 4375 4376 STRCPY(buf, ff_expand_buffer); 4377 STRCPY(buf + eb_len, search_ctx->ffsc_fix_path); 4378 if (mch_isdir(buf)) 4379 { 4380 STRCAT(ff_expand_buffer, search_ctx->ffsc_fix_path); 4381 add_pathsep(ff_expand_buffer); 4382 } 4383 #ifdef FEAT_PATH_EXTRA 4384 else 4385 { 4386 char_u *p = gettail(search_ctx->ffsc_fix_path); 4387 char_u *wc_path = NULL; 4388 char_u *temp = NULL; 4389 int len = 0; 4390 4391 if (p > search_ctx->ffsc_fix_path) 4392 { 4393 len = (int)(p - search_ctx->ffsc_fix_path) - 1; 4394 STRNCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, len); 4395 add_pathsep(ff_expand_buffer); 4396 } 4397 else 4398 len = (int)STRLEN(search_ctx->ffsc_fix_path); 4399 4400 if (search_ctx->ffsc_wc_path != NULL) 4401 { 4402 wc_path = vim_strsave(search_ctx->ffsc_wc_path); 4403 temp = alloc((int)(STRLEN(search_ctx->ffsc_wc_path) 4404 + STRLEN(search_ctx->ffsc_fix_path + len) 4405 + 1)); 4406 } 4407 4408 if (temp == NULL || wc_path == NULL) 4409 { 4410 vim_free(buf); 4411 vim_free(temp); 4412 vim_free(wc_path); 4413 goto error_return; 4414 } 4415 4416 STRCPY(temp, search_ctx->ffsc_fix_path + len); 4417 STRCAT(temp, search_ctx->ffsc_wc_path); 4418 vim_free(search_ctx->ffsc_wc_path); 4419 vim_free(wc_path); 4420 search_ctx->ffsc_wc_path = temp; 4421 } 4422 #endif 4423 vim_free(buf); 4424 } 4425 4426 sptr = ff_create_stack_element(ff_expand_buffer, 4427 #ifdef FEAT_PATH_EXTRA 4428 search_ctx->ffsc_wc_path, 4429 #endif 4430 level, 0); 4431 4432 if (sptr == NULL) 4433 goto error_return; 4434 4435 ff_push(search_ctx, sptr); 4436 4437 search_ctx->ffsc_file_to_search = vim_strsave(filename); 4438 if (search_ctx->ffsc_file_to_search == NULL) 4439 goto error_return; 4440 4441 return search_ctx; 4442 4443 error_return: 4444 /* 4445 * We clear the search context now! 4446 * Even when the caller gave us a (perhaps valid) context we free it here, 4447 * as we might have already destroyed it. 4448 */ 4449 vim_findfile_cleanup(search_ctx); 4450 return NULL; 4451 } 4452 4453 #if defined(FEAT_PATH_EXTRA) || defined(PROTO) 4454 /* 4455 * Get the stopdir string. Check that ';' is not escaped. 4456 */ 4457 char_u * 4458 vim_findfile_stopdir(buf) 4459 char_u *buf; 4460 { 4461 char_u *r_ptr = buf; 4462 4463 while (*r_ptr != NUL && *r_ptr != ';') 4464 { 4465 if (r_ptr[0] == '\\' && r_ptr[1] == ';') 4466 { 4467 /* Overwrite the escape char, 4468 * use STRLEN(r_ptr) to move the trailing '\0'. */ 4469 STRMOVE(r_ptr, r_ptr + 1); 4470 r_ptr++; 4471 } 4472 r_ptr++; 4473 } 4474 if (*r_ptr == ';') 4475 { 4476 *r_ptr = 0; 4477 r_ptr++; 4478 } 4479 else if (*r_ptr == NUL) 4480 r_ptr = NULL; 4481 return r_ptr; 4482 } 4483 #endif 4484 4485 /* 4486 * Clean up the given search context. Can handle a NULL pointer. 4487 */ 4488 void 4489 vim_findfile_cleanup(ctx) 4490 void *ctx; 4491 { 4492 if (ctx == NULL) 4493 return; 4494 4495 vim_findfile_free_visited(ctx); 4496 ff_clear(ctx); 4497 vim_free(ctx); 4498 } 4499 4500 /* 4501 * Find a file in a search context. 4502 * The search context was created with vim_findfile_init() above. 4503 * Return a pointer to an allocated file name or NULL if nothing found. 4504 * To get all matching files call this function until you get NULL. 4505 * 4506 * If the passed search_context is NULL, NULL is returned. 4507 * 4508 * The search algorithm is depth first. To change this replace the 4509 * stack with a list (don't forget to leave partly searched directories on the 4510 * top of the list). 4511 */ 4512 char_u * 4513 vim_findfile(search_ctx_arg) 4514 void *search_ctx_arg; 4515 { 4516 char_u *file_path; 4517 #ifdef FEAT_PATH_EXTRA 4518 char_u *rest_of_wildcards; 4519 char_u *path_end = NULL; 4520 #endif 4521 ff_stack_T *stackp; 4522 #if defined(FEAT_SEARCHPATH) || defined(FEAT_PATH_EXTRA) 4523 int len; 4524 #endif 4525 int i; 4526 char_u *p; 4527 #ifdef FEAT_SEARCHPATH 4528 char_u *suf; 4529 #endif 4530 ff_search_ctx_T *search_ctx; 4531 4532 if (search_ctx_arg == NULL) 4533 return NULL; 4534 4535 search_ctx = (ff_search_ctx_T *)search_ctx_arg; 4536 4537 /* 4538 * filepath is used as buffer for various actions and as the storage to 4539 * return a found filename. 4540 */ 4541 if ((file_path = alloc((int)MAXPATHL)) == NULL) 4542 return NULL; 4543 4544 #ifdef FEAT_PATH_EXTRA 4545 /* store the end of the start dir -- needed for upward search */ 4546 if (search_ctx->ffsc_start_dir != NULL) 4547 path_end = &search_ctx->ffsc_start_dir[ 4548 STRLEN(search_ctx->ffsc_start_dir)]; 4549 #endif 4550 4551 #ifdef FEAT_PATH_EXTRA 4552 /* upward search loop */ 4553 for (;;) 4554 { 4555 #endif 4556 /* downward search loop */ 4557 for (;;) 4558 { 4559 /* check if user user wants to stop the search*/ 4560 ui_breakcheck(); 4561 if (got_int) 4562 break; 4563 4564 /* get directory to work on from stack */ 4565 stackp = ff_pop(search_ctx); 4566 if (stackp == NULL) 4567 break; 4568 4569 /* 4570 * TODO: decide if we leave this test in 4571 * 4572 * GOOD: don't search a directory(-tree) twice. 4573 * BAD: - check linked list for every new directory entered. 4574 * - check for double files also done below 4575 * 4576 * Here we check if we already searched this directory. 4577 * We already searched a directory if: 4578 * 1) The directory is the same. 4579 * 2) We would use the same wildcard string. 4580 * 4581 * Good if you have links on same directory via several ways 4582 * or you have selfreferences in directories (e.g. SuSE Linux 6.3: 4583 * /etc/rc.d/init.d is linked to /etc/rc.d -> endless loop) 4584 * 4585 * This check is only needed for directories we work on for the 4586 * first time (hence stackp->ff_filearray == NULL) 4587 */ 4588 if (stackp->ffs_filearray == NULL 4589 && ff_check_visited(&search_ctx->ffsc_dir_visited_list 4590 ->ffvl_visited_list, 4591 stackp->ffs_fix_path 4592 #ifdef FEAT_PATH_EXTRA 4593 , stackp->ffs_wc_path 4594 #endif 4595 ) == FAIL) 4596 { 4597 #ifdef FF_VERBOSE 4598 if (p_verbose >= 5) 4599 { 4600 verbose_enter_scroll(); 4601 smsg((char_u *)"Already Searched: %s (%s)", 4602 stackp->ffs_fix_path, stackp->ffs_wc_path); 4603 /* don't overwrite this either */ 4604 msg_puts((char_u *)"\n"); 4605 verbose_leave_scroll(); 4606 } 4607 #endif 4608 ff_free_stack_element(stackp); 4609 continue; 4610 } 4611 #ifdef FF_VERBOSE 4612 else if (p_verbose >= 5) 4613 { 4614 verbose_enter_scroll(); 4615 smsg((char_u *)"Searching: %s (%s)", 4616 stackp->ffs_fix_path, stackp->ffs_wc_path); 4617 /* don't overwrite this either */ 4618 msg_puts((char_u *)"\n"); 4619 verbose_leave_scroll(); 4620 } 4621 #endif 4622 4623 /* check depth */ 4624 if (stackp->ffs_level <= 0) 4625 { 4626 ff_free_stack_element(stackp); 4627 continue; 4628 } 4629 4630 file_path[0] = NUL; 4631 4632 /* 4633 * If no filearray till now expand wildcards 4634 * The function expand_wildcards() can handle an array of paths 4635 * and all possible expands are returned in one array. We use this 4636 * to handle the expansion of '**' into an empty string. 4637 */ 4638 if (stackp->ffs_filearray == NULL) 4639 { 4640 char_u *dirptrs[2]; 4641 4642 /* we use filepath to build the path expand_wildcards() should 4643 * expand. 4644 */ 4645 dirptrs[0] = file_path; 4646 dirptrs[1] = NULL; 4647 4648 /* if we have a start dir copy it in */ 4649 if (!vim_isAbsName(stackp->ffs_fix_path) 4650 && search_ctx->ffsc_start_dir) 4651 { 4652 STRCPY(file_path, search_ctx->ffsc_start_dir); 4653 add_pathsep(file_path); 4654 } 4655 4656 /* append the fix part of the search path */ 4657 STRCAT(file_path, stackp->ffs_fix_path); 4658 add_pathsep(file_path); 4659 4660 #ifdef FEAT_PATH_EXTRA 4661 rest_of_wildcards = stackp->ffs_wc_path; 4662 if (*rest_of_wildcards != NUL) 4663 { 4664 len = (int)STRLEN(file_path); 4665 if (STRNCMP(rest_of_wildcards, "**", 2) == 0) 4666 { 4667 /* pointer to the restrict byte 4668 * The restrict byte is not a character! 4669 */ 4670 p = rest_of_wildcards + 2; 4671 4672 if (*p > 0) 4673 { 4674 (*p)--; 4675 file_path[len++] = '*'; 4676 } 4677 4678 if (*p == 0) 4679 { 4680 /* remove '**<numb> from wildcards */ 4681 STRMOVE(rest_of_wildcards, rest_of_wildcards + 3); 4682 } 4683 else 4684 rest_of_wildcards += 3; 4685 4686 if (stackp->ffs_star_star_empty == 0) 4687 { 4688 /* if not done before, expand '**' to empty */ 4689 stackp->ffs_star_star_empty = 1; 4690 dirptrs[1] = stackp->ffs_fix_path; 4691 } 4692 } 4693 4694 /* 4695 * Here we copy until the next path separator or the end of 4696 * the path. If we stop at a path separator, there is 4697 * still something else left. This is handled below by 4698 * pushing every directory returned from expand_wildcards() 4699 * on the stack again for further search. 4700 */ 4701 while (*rest_of_wildcards 4702 && !vim_ispathsep(*rest_of_wildcards)) 4703 file_path[len++] = *rest_of_wildcards++; 4704 4705 file_path[len] = NUL; 4706 if (vim_ispathsep(*rest_of_wildcards)) 4707 rest_of_wildcards++; 4708 } 4709 #endif 4710 4711 /* 4712 * Expand wildcards like "*" and "$VAR". 4713 * If the path is a URL don't try this. 4714 */ 4715 if (path_with_url(dirptrs[0])) 4716 { 4717 stackp->ffs_filearray = (char_u **) 4718 alloc((unsigned)sizeof(char *)); 4719 if (stackp->ffs_filearray != NULL 4720 && (stackp->ffs_filearray[0] 4721 = vim_strsave(dirptrs[0])) != NULL) 4722 stackp->ffs_filearray_size = 1; 4723 else 4724 stackp->ffs_filearray_size = 0; 4725 } 4726 else 4727 /* Add EW_NOTWILD because the expanded path may contain 4728 * wildcard characters that are to be taken literally. 4729 * This is a bit of a hack. */ 4730 expand_wildcards((dirptrs[1] == NULL) ? 1 : 2, dirptrs, 4731 &stackp->ffs_filearray_size, 4732 &stackp->ffs_filearray, 4733 EW_DIR|EW_ADDSLASH|EW_SILENT|EW_NOTWILD); 4734 4735 stackp->ffs_filearray_cur = 0; 4736 stackp->ffs_stage = 0; 4737 } 4738 #ifdef FEAT_PATH_EXTRA 4739 else 4740 rest_of_wildcards = &stackp->ffs_wc_path[ 4741 STRLEN(stackp->ffs_wc_path)]; 4742 #endif 4743 4744 if (stackp->ffs_stage == 0) 4745 { 4746 /* this is the first time we work on this directory */ 4747 #ifdef FEAT_PATH_EXTRA 4748 if (*rest_of_wildcards == NUL) 4749 #endif 4750 { 4751 /* 4752 * We don't have further wildcards to expand, so we have to 4753 * check for the final file now. 4754 */ 4755 for (i = stackp->ffs_filearray_cur; 4756 i < stackp->ffs_filearray_size; ++i) 4757 { 4758 if (!path_with_url(stackp->ffs_filearray[i]) 4759 && !mch_isdir(stackp->ffs_filearray[i])) 4760 continue; /* not a directory */ 4761 4762 /* prepare the filename to be checked for existence 4763 * below */ 4764 STRCPY(file_path, stackp->ffs_filearray[i]); 4765 add_pathsep(file_path); 4766 STRCAT(file_path, search_ctx->ffsc_file_to_search); 4767 4768 /* 4769 * Try without extra suffix and then with suffixes 4770 * from 'suffixesadd'. 4771 */ 4772 #ifdef FEAT_SEARCHPATH 4773 len = (int)STRLEN(file_path); 4774 if (search_ctx->ffsc_tagfile) 4775 suf = (char_u *)""; 4776 else 4777 suf = curbuf->b_p_sua; 4778 for (;;) 4779 #endif 4780 { 4781 /* if file exists and we didn't already find it */ 4782 if ((path_with_url(file_path) 4783 || (mch_getperm(file_path) >= 0 4784 && (search_ctx->ffsc_find_what 4785 == FINDFILE_BOTH 4786 || ((search_ctx->ffsc_find_what 4787 == FINDFILE_DIR) 4788 == mch_isdir(file_path))))) 4789 #ifndef FF_VERBOSE 4790 && (ff_check_visited( 4791 &search_ctx->ffsc_visited_list->ffvl_visited_list, 4792 file_path 4793 #ifdef FEAT_PATH_EXTRA 4794 , (char_u *)"" 4795 #endif 4796 ) == OK) 4797 #endif 4798 ) 4799 { 4800 #ifdef FF_VERBOSE 4801 if (ff_check_visited( 4802 &search_ctx->ffsc_visited_list->ffvl_visited_list, 4803 file_path 4804 #ifdef FEAT_PATH_EXTRA 4805 , (char_u *)"" 4806 #endif 4807 ) == FAIL) 4808 { 4809 if (p_verbose >= 5) 4810 { 4811 verbose_enter_scroll(); 4812 smsg((char_u *)"Already: %s", 4813 file_path); 4814 /* don't overwrite this either */ 4815 msg_puts((char_u *)"\n"); 4816 verbose_leave_scroll(); 4817 } 4818 continue; 4819 } 4820 #endif 4821 4822 /* push dir to examine rest of subdirs later */ 4823 stackp->ffs_filearray_cur = i + 1; 4824 ff_push(search_ctx, stackp); 4825 4826 if (!path_with_url(file_path)) 4827 simplify_filename(file_path); 4828 if (mch_dirname(ff_expand_buffer, MAXPATHL) 4829 == OK) 4830 { 4831 p = shorten_fname(file_path, 4832 ff_expand_buffer); 4833 if (p != NULL) 4834 STRMOVE(file_path, p); 4835 } 4836 #ifdef FF_VERBOSE 4837 if (p_verbose >= 5) 4838 { 4839 verbose_enter_scroll(); 4840 smsg((char_u *)"HIT: %s", file_path); 4841 /* don't overwrite this either */ 4842 msg_puts((char_u *)"\n"); 4843 verbose_leave_scroll(); 4844 } 4845 #endif 4846 return file_path; 4847 } 4848 4849 #ifdef FEAT_SEARCHPATH 4850 /* Not found or found already, try next suffix. */ 4851 if (*suf == NUL) 4852 break; 4853 copy_option_part(&suf, file_path + len, 4854 MAXPATHL - len, ","); 4855 #endif 4856 } 4857 } 4858 } 4859 #ifdef FEAT_PATH_EXTRA 4860 else 4861 { 4862 /* 4863 * still wildcards left, push the directories for further 4864 * search 4865 */ 4866 for (i = stackp->ffs_filearray_cur; 4867 i < stackp->ffs_filearray_size; ++i) 4868 { 4869 if (!mch_isdir(stackp->ffs_filearray[i])) 4870 continue; /* not a directory */ 4871 4872 ff_push(search_ctx, 4873 ff_create_stack_element( 4874 stackp->ffs_filearray[i], 4875 rest_of_wildcards, 4876 stackp->ffs_level - 1, 0)); 4877 } 4878 } 4879 #endif 4880 stackp->ffs_filearray_cur = 0; 4881 stackp->ffs_stage = 1; 4882 } 4883 4884 #ifdef FEAT_PATH_EXTRA 4885 /* 4886 * if wildcards contains '**' we have to descent till we reach the 4887 * leaves of the directory tree. 4888 */ 4889 if (STRNCMP(stackp->ffs_wc_path, "**", 2) == 0) 4890 { 4891 for (i = stackp->ffs_filearray_cur; 4892 i < stackp->ffs_filearray_size; ++i) 4893 { 4894 if (fnamecmp(stackp->ffs_filearray[i], 4895 stackp->ffs_fix_path) == 0) 4896 continue; /* don't repush same directory */ 4897 if (!mch_isdir(stackp->ffs_filearray[i])) 4898 continue; /* not a directory */ 4899 ff_push(search_ctx, 4900 ff_create_stack_element(stackp->ffs_filearray[i], 4901 stackp->ffs_wc_path, stackp->ffs_level - 1, 1)); 4902 } 4903 } 4904 #endif 4905 4906 /* we are done with the current directory */ 4907 ff_free_stack_element(stackp); 4908 4909 } 4910 4911 #ifdef FEAT_PATH_EXTRA 4912 /* If we reached this, we didn't find anything downwards. 4913 * Let's check if we should do an upward search. 4914 */ 4915 if (search_ctx->ffsc_start_dir 4916 && search_ctx->ffsc_stopdirs_v != NULL && !got_int) 4917 { 4918 ff_stack_T *sptr; 4919 4920 /* is the last starting directory in the stop list? */ 4921 if (ff_path_in_stoplist(search_ctx->ffsc_start_dir, 4922 (int)(path_end - search_ctx->ffsc_start_dir), 4923 search_ctx->ffsc_stopdirs_v) == TRUE) 4924 break; 4925 4926 /* cut of last dir */ 4927 while (path_end > search_ctx->ffsc_start_dir 4928 && vim_ispathsep(*path_end)) 4929 path_end--; 4930 while (path_end > search_ctx->ffsc_start_dir 4931 && !vim_ispathsep(path_end[-1])) 4932 path_end--; 4933 *path_end = 0; 4934 path_end--; 4935 4936 if (*search_ctx->ffsc_start_dir == 0) 4937 break; 4938 4939 STRCPY(file_path, search_ctx->ffsc_start_dir); 4940 add_pathsep(file_path); 4941 STRCAT(file_path, search_ctx->ffsc_fix_path); 4942 4943 /* create a new stack entry */ 4944 sptr = ff_create_stack_element(file_path, 4945 search_ctx->ffsc_wc_path, search_ctx->ffsc_level, 0); 4946 if (sptr == NULL) 4947 break; 4948 ff_push(search_ctx, sptr); 4949 } 4950 else 4951 break; 4952 } 4953 #endif 4954 4955 vim_free(file_path); 4956 return NULL; 4957 } 4958 4959 /* 4960 * Free the list of lists of visited files and directories 4961 * Can handle it if the passed search_context is NULL; 4962 */ 4963 void 4964 vim_findfile_free_visited(search_ctx_arg) 4965 void *search_ctx_arg; 4966 { 4967 ff_search_ctx_T *search_ctx; 4968 4969 if (search_ctx_arg == NULL) 4970 return; 4971 4972 search_ctx = (ff_search_ctx_T *)search_ctx_arg; 4973 vim_findfile_free_visited_list(&search_ctx->ffsc_visited_lists_list); 4974 vim_findfile_free_visited_list(&search_ctx->ffsc_dir_visited_lists_list); 4975 } 4976 4977 static void 4978 vim_findfile_free_visited_list(list_headp) 4979 ff_visited_list_hdr_T **list_headp; 4980 { 4981 ff_visited_list_hdr_T *vp; 4982 4983 while (*list_headp != NULL) 4984 { 4985 vp = (*list_headp)->ffvl_next; 4986 ff_free_visited_list((*list_headp)->ffvl_visited_list); 4987 4988 vim_free((*list_headp)->ffvl_filename); 4989 vim_free(*list_headp); 4990 *list_headp = vp; 4991 } 4992 *list_headp = NULL; 4993 } 4994 4995 static void 4996 ff_free_visited_list(vl) 4997 ff_visited_T *vl; 4998 { 4999 ff_visited_T *vp; 5000 5001 while (vl != NULL) 5002 { 5003 vp = vl->ffv_next; 5004 #ifdef FEAT_PATH_EXTRA 5005 vim_free(vl->ffv_wc_path); 5006 #endif 5007 vim_free(vl); 5008 vl = vp; 5009 } 5010 vl = NULL; 5011 } 5012 5013 /* 5014 * Returns the already visited list for the given filename. If none is found it 5015 * allocates a new one. 5016 */ 5017 static ff_visited_list_hdr_T* 5018 ff_get_visited_list(filename, list_headp) 5019 char_u *filename; 5020 ff_visited_list_hdr_T **list_headp; 5021 { 5022 ff_visited_list_hdr_T *retptr = NULL; 5023 5024 /* check if a visited list for the given filename exists */ 5025 if (*list_headp != NULL) 5026 { 5027 retptr = *list_headp; 5028 while (retptr != NULL) 5029 { 5030 if (fnamecmp(filename, retptr->ffvl_filename) == 0) 5031 { 5032 #ifdef FF_VERBOSE 5033 if (p_verbose >= 5) 5034 { 5035 verbose_enter_scroll(); 5036 smsg((char_u *)"ff_get_visited_list: FOUND list for %s", 5037 filename); 5038 /* don't overwrite this either */ 5039 msg_puts((char_u *)"\n"); 5040 verbose_leave_scroll(); 5041 } 5042 #endif 5043 return retptr; 5044 } 5045 retptr = retptr->ffvl_next; 5046 } 5047 } 5048 5049 #ifdef FF_VERBOSE 5050 if (p_verbose >= 5) 5051 { 5052 verbose_enter_scroll(); 5053 smsg((char_u *)"ff_get_visited_list: new list for %s", filename); 5054 /* don't overwrite this either */ 5055 msg_puts((char_u *)"\n"); 5056 verbose_leave_scroll(); 5057 } 5058 #endif 5059 5060 /* 5061 * if we reach this we didn't find a list and we have to allocate new list 5062 */ 5063 retptr = (ff_visited_list_hdr_T*)alloc((unsigned)sizeof(*retptr)); 5064 if (retptr == NULL) 5065 return NULL; 5066 5067 retptr->ffvl_visited_list = NULL; 5068 retptr->ffvl_filename = vim_strsave(filename); 5069 if (retptr->ffvl_filename == NULL) 5070 { 5071 vim_free(retptr); 5072 return NULL; 5073 } 5074 retptr->ffvl_next = *list_headp; 5075 *list_headp = retptr; 5076 5077 return retptr; 5078 } 5079 5080 #ifdef FEAT_PATH_EXTRA 5081 /* 5082 * check if two wildcard paths are equal. Returns TRUE or FALSE. 5083 * They are equal if: 5084 * - both paths are NULL 5085 * - they have the same length 5086 * - char by char comparison is OK 5087 * - the only differences are in the counters behind a '**', so 5088 * '**\20' is equal to '**\24' 5089 */ 5090 static int 5091 ff_wc_equal(s1, s2) 5092 char_u *s1; 5093 char_u *s2; 5094 { 5095 int i; 5096 int prev1 = NUL; 5097 int prev2 = NUL; 5098 5099 if (s1 == s2) 5100 return TRUE; 5101 5102 if (s1 == NULL || s2 == NULL) 5103 return FALSE; 5104 5105 if (STRLEN(s1) != STRLEN(s2)) 5106 return FAIL; 5107 5108 for (i = 0; s1[i] != NUL && s2[i] != NUL; i += MB_PTR2LEN(s1 + i)) 5109 { 5110 int c1 = PTR2CHAR(s1 + i); 5111 int c2 = PTR2CHAR(s2 + i); 5112 5113 if ((p_fic ? MB_TOLOWER(c1) != MB_TOLOWER(c2) : c1 != c2) 5114 && (prev1 != '*' || prev2 != '*')) 5115 return FAIL; 5116 prev2 = prev1; 5117 prev1 = c1; 5118 } 5119 return TRUE; 5120 } 5121 #endif 5122 5123 /* 5124 * maintains the list of already visited files and dirs 5125 * returns FAIL if the given file/dir is already in the list 5126 * returns OK if it is newly added 5127 * 5128 * TODO: What to do on memory allocation problems? 5129 * -> return TRUE - Better the file is found several times instead of 5130 * never. 5131 */ 5132 static int 5133 ff_check_visited(visited_list, fname 5134 #ifdef FEAT_PATH_EXTRA 5135 , wc_path 5136 #endif 5137 ) 5138 ff_visited_T **visited_list; 5139 char_u *fname; 5140 #ifdef FEAT_PATH_EXTRA 5141 char_u *wc_path; 5142 #endif 5143 { 5144 ff_visited_T *vp; 5145 #ifdef UNIX 5146 struct stat st; 5147 int url = FALSE; 5148 #endif 5149 5150 /* For an URL we only compare the name, otherwise we compare the 5151 * device/inode (unix) or the full path name (not Unix). */ 5152 if (path_with_url(fname)) 5153 { 5154 vim_strncpy(ff_expand_buffer, fname, MAXPATHL - 1); 5155 #ifdef UNIX 5156 url = TRUE; 5157 #endif 5158 } 5159 else 5160 { 5161 ff_expand_buffer[0] = NUL; 5162 #ifdef UNIX 5163 if (mch_stat((char *)fname, &st) < 0) 5164 #else 5165 if (vim_FullName(fname, ff_expand_buffer, MAXPATHL, TRUE) == FAIL) 5166 #endif 5167 return FAIL; 5168 } 5169 5170 /* check against list of already visited files */ 5171 for (vp = *visited_list; vp != NULL; vp = vp->ffv_next) 5172 { 5173 if ( 5174 #ifdef UNIX 5175 !url ? (vp->ffv_dev_valid && vp->ffv_dev == st.st_dev 5176 && vp->ffv_ino == st.st_ino) 5177 : 5178 #endif 5179 fnamecmp(vp->ffv_fname, ff_expand_buffer) == 0 5180 ) 5181 { 5182 #ifdef FEAT_PATH_EXTRA 5183 /* are the wildcard parts equal */ 5184 if (ff_wc_equal(vp->ffv_wc_path, wc_path) == TRUE) 5185 #endif 5186 /* already visited */ 5187 return FAIL; 5188 } 5189 } 5190 5191 /* 5192 * New file/dir. Add it to the list of visited files/dirs. 5193 */ 5194 vp = (ff_visited_T *)alloc((unsigned)(sizeof(ff_visited_T) 5195 + STRLEN(ff_expand_buffer))); 5196 5197 if (vp != NULL) 5198 { 5199 #ifdef UNIX 5200 if (!url) 5201 { 5202 vp->ffv_dev_valid = TRUE; 5203 vp->ffv_ino = st.st_ino; 5204 vp->ffv_dev = st.st_dev; 5205 vp->ffv_fname[0] = NUL; 5206 } 5207 else 5208 { 5209 vp->ffv_dev_valid = FALSE; 5210 #endif 5211 STRCPY(vp->ffv_fname, ff_expand_buffer); 5212 #ifdef UNIX 5213 } 5214 #endif 5215 #ifdef FEAT_PATH_EXTRA 5216 if (wc_path != NULL) 5217 vp->ffv_wc_path = vim_strsave(wc_path); 5218 else 5219 vp->ffv_wc_path = NULL; 5220 #endif 5221 5222 vp->ffv_next = *visited_list; 5223 *visited_list = vp; 5224 } 5225 5226 return OK; 5227 } 5228 5229 /* 5230 * create stack element from given path pieces 5231 */ 5232 static ff_stack_T * 5233 ff_create_stack_element(fix_part, 5234 #ifdef FEAT_PATH_EXTRA 5235 wc_part, 5236 #endif 5237 level, star_star_empty) 5238 char_u *fix_part; 5239 #ifdef FEAT_PATH_EXTRA 5240 char_u *wc_part; 5241 #endif 5242 int level; 5243 int star_star_empty; 5244 { 5245 ff_stack_T *new; 5246 5247 new = (ff_stack_T *)alloc((unsigned)sizeof(ff_stack_T)); 5248 if (new == NULL) 5249 return NULL; 5250 5251 new->ffs_prev = NULL; 5252 new->ffs_filearray = NULL; 5253 new->ffs_filearray_size = 0; 5254 new->ffs_filearray_cur = 0; 5255 new->ffs_stage = 0; 5256 new->ffs_level = level; 5257 new->ffs_star_star_empty = star_star_empty;; 5258 5259 /* the following saves NULL pointer checks in vim_findfile */ 5260 if (fix_part == NULL) 5261 fix_part = (char_u *)""; 5262 new->ffs_fix_path = vim_strsave(fix_part); 5263 5264 #ifdef FEAT_PATH_EXTRA 5265 if (wc_part == NULL) 5266 wc_part = (char_u *)""; 5267 new->ffs_wc_path = vim_strsave(wc_part); 5268 #endif 5269 5270 if (new->ffs_fix_path == NULL 5271 #ifdef FEAT_PATH_EXTRA 5272 || new->ffs_wc_path == NULL 5273 #endif 5274 ) 5275 { 5276 ff_free_stack_element(new); 5277 new = NULL; 5278 } 5279 5280 return new; 5281 } 5282 5283 /* 5284 * Push a dir on the directory stack. 5285 */ 5286 static void 5287 ff_push(search_ctx, stack_ptr) 5288 ff_search_ctx_T *search_ctx; 5289 ff_stack_T *stack_ptr; 5290 { 5291 /* check for NULL pointer, not to return an error to the user, but 5292 * to prevent a crash */ 5293 if (stack_ptr != NULL) 5294 { 5295 stack_ptr->ffs_prev = search_ctx->ffsc_stack_ptr; 5296 search_ctx->ffsc_stack_ptr = stack_ptr; 5297 } 5298 } 5299 5300 /* 5301 * Pop a dir from the directory stack. 5302 * Returns NULL if stack is empty. 5303 */ 5304 static ff_stack_T * 5305 ff_pop(search_ctx) 5306 ff_search_ctx_T *search_ctx; 5307 { 5308 ff_stack_T *sptr; 5309 5310 sptr = search_ctx->ffsc_stack_ptr; 5311 if (search_ctx->ffsc_stack_ptr != NULL) 5312 search_ctx->ffsc_stack_ptr = search_ctx->ffsc_stack_ptr->ffs_prev; 5313 5314 return sptr; 5315 } 5316 5317 /* 5318 * free the given stack element 5319 */ 5320 static void 5321 ff_free_stack_element(stack_ptr) 5322 ff_stack_T *stack_ptr; 5323 { 5324 /* vim_free handles possible NULL pointers */ 5325 vim_free(stack_ptr->ffs_fix_path); 5326 #ifdef FEAT_PATH_EXTRA 5327 vim_free(stack_ptr->ffs_wc_path); 5328 #endif 5329 5330 if (stack_ptr->ffs_filearray != NULL) 5331 FreeWild(stack_ptr->ffs_filearray_size, stack_ptr->ffs_filearray); 5332 5333 vim_free(stack_ptr); 5334 } 5335 5336 /* 5337 * Clear the search context, but NOT the visited list. 5338 */ 5339 static void 5340 ff_clear(search_ctx) 5341 ff_search_ctx_T *search_ctx; 5342 { 5343 ff_stack_T *sptr; 5344 5345 /* clear up stack */ 5346 while ((sptr = ff_pop(search_ctx)) != NULL) 5347 ff_free_stack_element(sptr); 5348 5349 vim_free(search_ctx->ffsc_file_to_search); 5350 vim_free(search_ctx->ffsc_start_dir); 5351 vim_free(search_ctx->ffsc_fix_path); 5352 #ifdef FEAT_PATH_EXTRA 5353 vim_free(search_ctx->ffsc_wc_path); 5354 #endif 5355 5356 #ifdef FEAT_PATH_EXTRA 5357 if (search_ctx->ffsc_stopdirs_v != NULL) 5358 { 5359 int i = 0; 5360 5361 while (search_ctx->ffsc_stopdirs_v[i] != NULL) 5362 { 5363 vim_free(search_ctx->ffsc_stopdirs_v[i]); 5364 i++; 5365 } 5366 vim_free(search_ctx->ffsc_stopdirs_v); 5367 } 5368 search_ctx->ffsc_stopdirs_v = NULL; 5369 #endif 5370 5371 /* reset everything */ 5372 search_ctx->ffsc_file_to_search = NULL; 5373 search_ctx->ffsc_start_dir = NULL; 5374 search_ctx->ffsc_fix_path = NULL; 5375 #ifdef FEAT_PATH_EXTRA 5376 search_ctx->ffsc_wc_path = NULL; 5377 search_ctx->ffsc_level = 0; 5378 #endif 5379 } 5380 5381 #ifdef FEAT_PATH_EXTRA 5382 /* 5383 * check if the given path is in the stopdirs 5384 * returns TRUE if yes else FALSE 5385 */ 5386 static int 5387 ff_path_in_stoplist(path, path_len, stopdirs_v) 5388 char_u *path; 5389 int path_len; 5390 char_u **stopdirs_v; 5391 { 5392 int i = 0; 5393 5394 /* eat up trailing path separators, except the first */ 5395 while (path_len > 1 && vim_ispathsep(path[path_len - 1])) 5396 path_len--; 5397 5398 /* if no path consider it as match */ 5399 if (path_len == 0) 5400 return TRUE; 5401 5402 for (i = 0; stopdirs_v[i] != NULL; i++) 5403 { 5404 if ((int)STRLEN(stopdirs_v[i]) > path_len) 5405 { 5406 /* match for parent directory. So '/home' also matches 5407 * '/home/rks'. Check for PATHSEP in stopdirs_v[i], else 5408 * '/home/r' would also match '/home/rks' 5409 */ 5410 if (fnamencmp(stopdirs_v[i], path, path_len) == 0 5411 && vim_ispathsep(stopdirs_v[i][path_len])) 5412 return TRUE; 5413 } 5414 else 5415 { 5416 if (fnamecmp(stopdirs_v[i], path) == 0) 5417 return TRUE; 5418 } 5419 } 5420 return FALSE; 5421 } 5422 #endif 5423 5424 #if defined(FEAT_SEARCHPATH) || defined(PROTO) 5425 /* 5426 * Find the file name "ptr[len]" in the path. Also finds directory names. 5427 * 5428 * On the first call set the parameter 'first' to TRUE to initialize 5429 * the search. For repeating calls to FALSE. 5430 * 5431 * Repeating calls will return other files called 'ptr[len]' from the path. 5432 * 5433 * Only on the first call 'ptr' and 'len' are used. For repeating calls they 5434 * don't need valid values. 5435 * 5436 * If nothing found on the first call the option FNAME_MESS will issue the 5437 * message: 5438 * 'Can't find file "<file>" in path' 5439 * On repeating calls: 5440 * 'No more file "<file>" found in path' 5441 * 5442 * options: 5443 * FNAME_MESS give error message when not found 5444 * 5445 * Uses NameBuff[]! 5446 * 5447 * Returns an allocated string for the file name. NULL for error. 5448 * 5449 */ 5450 char_u * 5451 find_file_in_path(ptr, len, options, first, rel_fname) 5452 char_u *ptr; /* file name */ 5453 int len; /* length of file name */ 5454 int options; 5455 int first; /* use count'th matching file name */ 5456 char_u *rel_fname; /* file name searching relative to */ 5457 { 5458 return find_file_in_path_option(ptr, len, options, first, 5459 *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path, 5460 FINDFILE_BOTH, rel_fname, curbuf->b_p_sua); 5461 } 5462 5463 static char_u *ff_file_to_find = NULL; 5464 static void *fdip_search_ctx = NULL; 5465 5466 #if defined(EXITFREE) 5467 static void 5468 free_findfile() 5469 { 5470 vim_free(ff_file_to_find); 5471 vim_findfile_cleanup(fdip_search_ctx); 5472 } 5473 #endif 5474 5475 /* 5476 * Find the directory name "ptr[len]" in the path. 5477 * 5478 * options: 5479 * FNAME_MESS give error message when not found 5480 * FNAME_UNESC unescape backslashes. 5481 * 5482 * Uses NameBuff[]! 5483 * 5484 * Returns an allocated string for the file name. NULL for error. 5485 */ 5486 char_u * 5487 find_directory_in_path(ptr, len, options, rel_fname) 5488 char_u *ptr; /* file name */ 5489 int len; /* length of file name */ 5490 int options; 5491 char_u *rel_fname; /* file name searching relative to */ 5492 { 5493 return find_file_in_path_option(ptr, len, options, TRUE, p_cdpath, 5494 FINDFILE_DIR, rel_fname, (char_u *)""); 5495 } 5496 5497 char_u * 5498 find_file_in_path_option(ptr, len, options, first, path_option, 5499 find_what, rel_fname, suffixes) 5500 char_u *ptr; /* file name */ 5501 int len; /* length of file name */ 5502 int options; 5503 int first; /* use count'th matching file name */ 5504 char_u *path_option; /* p_path or p_cdpath */ 5505 int find_what; /* FINDFILE_FILE, _DIR or _BOTH */ 5506 char_u *rel_fname; /* file name we are looking relative to. */ 5507 char_u *suffixes; /* list of suffixes, 'suffixesadd' option */ 5508 { 5509 static char_u *dir; 5510 static int did_findfile_init = FALSE; 5511 char_u save_char; 5512 char_u *file_name = NULL; 5513 char_u *buf = NULL; 5514 int rel_to_curdir; 5515 #ifdef AMIGA 5516 struct Process *proc = (struct Process *)FindTask(0L); 5517 APTR save_winptr = proc->pr_WindowPtr; 5518 5519 /* Avoid a requester here for a volume that doesn't exist. */ 5520 proc->pr_WindowPtr = (APTR)-1L; 5521 #endif 5522 5523 if (first == TRUE) 5524 { 5525 /* copy file name into NameBuff, expanding environment variables */ 5526 save_char = ptr[len]; 5527 ptr[len] = NUL; 5528 expand_env(ptr, NameBuff, MAXPATHL); 5529 ptr[len] = save_char; 5530 5531 vim_free(ff_file_to_find); 5532 ff_file_to_find = vim_strsave(NameBuff); 5533 if (ff_file_to_find == NULL) /* out of memory */ 5534 { 5535 file_name = NULL; 5536 goto theend; 5537 } 5538 if (options & FNAME_UNESC) 5539 { 5540 /* Change all "\ " to " ". */ 5541 for (ptr = ff_file_to_find; *ptr != NUL; ++ptr) 5542 if (ptr[0] == '\\' && ptr[1] == ' ') 5543 mch_memmove(ptr, ptr + 1, STRLEN(ptr)); 5544 } 5545 } 5546 5547 rel_to_curdir = (ff_file_to_find[0] == '.' 5548 && (ff_file_to_find[1] == NUL 5549 || vim_ispathsep(ff_file_to_find[1]) 5550 || (ff_file_to_find[1] == '.' 5551 && (ff_file_to_find[2] == NUL 5552 || vim_ispathsep(ff_file_to_find[2]))))); 5553 if (vim_isAbsName(ff_file_to_find) 5554 /* "..", "../path", "." and "./path": don't use the path_option */ 5555 || rel_to_curdir 5556 #if defined(MSWIN) || defined(MSDOS) || defined(OS2) 5557 /* handle "\tmp" as absolute path */ 5558 || vim_ispathsep(ff_file_to_find[0]) 5559 /* handle "c:name" as absolute path */ 5560 || (ff_file_to_find[0] != NUL && ff_file_to_find[1] == ':') 5561 #endif 5562 #ifdef AMIGA 5563 /* handle ":tmp" as absolute path */ 5564 || ff_file_to_find[0] == ':' 5565 #endif 5566 ) 5567 { 5568 /* 5569 * Absolute path, no need to use "path_option". 5570 * If this is not a first call, return NULL. We already returned a 5571 * filename on the first call. 5572 */ 5573 if (first == TRUE) 5574 { 5575 int l; 5576 int run; 5577 5578 if (path_with_url(ff_file_to_find)) 5579 { 5580 file_name = vim_strsave(ff_file_to_find); 5581 goto theend; 5582 } 5583 5584 /* When FNAME_REL flag given first use the directory of the file. 5585 * Otherwise or when this fails use the current directory. */ 5586 for (run = 1; run <= 2; ++run) 5587 { 5588 l = (int)STRLEN(ff_file_to_find); 5589 if (run == 1 5590 && rel_to_curdir 5591 && (options & FNAME_REL) 5592 && rel_fname != NULL 5593 && STRLEN(rel_fname) + l < MAXPATHL) 5594 { 5595 STRCPY(NameBuff, rel_fname); 5596 STRCPY(gettail(NameBuff), ff_file_to_find); 5597 l = (int)STRLEN(NameBuff); 5598 } 5599 else 5600 { 5601 STRCPY(NameBuff, ff_file_to_find); 5602 run = 2; 5603 } 5604 5605 /* When the file doesn't exist, try adding parts of 5606 * 'suffixesadd'. */ 5607 buf = suffixes; 5608 for (;;) 5609 { 5610 if ( 5611 #ifdef DJGPP 5612 /* "C:" by itself will fail for mch_getperm(), 5613 * assume it's always valid. */ 5614 (find_what != FINDFILE_FILE && NameBuff[0] != NUL 5615 && NameBuff[1] == ':' 5616 && NameBuff[2] == NUL) || 5617 #endif 5618 (mch_getperm(NameBuff) >= 0 5619 && (find_what == FINDFILE_BOTH 5620 || ((find_what == FINDFILE_DIR) 5621 == mch_isdir(NameBuff))))) 5622 { 5623 file_name = vim_strsave(NameBuff); 5624 goto theend; 5625 } 5626 if (*buf == NUL) 5627 break; 5628 copy_option_part(&buf, NameBuff + l, MAXPATHL - l, ","); 5629 } 5630 } 5631 } 5632 } 5633 else 5634 { 5635 /* 5636 * Loop over all paths in the 'path' or 'cdpath' option. 5637 * When "first" is set, first setup to the start of the option. 5638 * Otherwise continue to find the next match. 5639 */ 5640 if (first == TRUE) 5641 { 5642 /* vim_findfile_free_visited can handle a possible NULL pointer */ 5643 vim_findfile_free_visited(fdip_search_ctx); 5644 dir = path_option; 5645 did_findfile_init = FALSE; 5646 } 5647 5648 for (;;) 5649 { 5650 if (did_findfile_init) 5651 { 5652 file_name = vim_findfile(fdip_search_ctx); 5653 if (file_name != NULL) 5654 break; 5655 5656 did_findfile_init = FALSE; 5657 } 5658 else 5659 { 5660 char_u *r_ptr; 5661 5662 if (dir == NULL || *dir == NUL) 5663 { 5664 /* We searched all paths of the option, now we can 5665 * free the search context. */ 5666 vim_findfile_cleanup(fdip_search_ctx); 5667 fdip_search_ctx = NULL; 5668 break; 5669 } 5670 5671 if ((buf = alloc((int)(MAXPATHL))) == NULL) 5672 break; 5673 5674 /* copy next path */ 5675 buf[0] = 0; 5676 copy_option_part(&dir, buf, MAXPATHL, " ,"); 5677 5678 #ifdef FEAT_PATH_EXTRA 5679 /* get the stopdir string */ 5680 r_ptr = vim_findfile_stopdir(buf); 5681 #else 5682 r_ptr = NULL; 5683 #endif 5684 fdip_search_ctx = vim_findfile_init(buf, ff_file_to_find, 5685 r_ptr, 100, FALSE, find_what, 5686 fdip_search_ctx, FALSE, rel_fname); 5687 if (fdip_search_ctx != NULL) 5688 did_findfile_init = TRUE; 5689 vim_free(buf); 5690 } 5691 } 5692 } 5693 if (file_name == NULL && (options & FNAME_MESS)) 5694 { 5695 if (first == TRUE) 5696 { 5697 if (find_what == FINDFILE_DIR) 5698 EMSG2(_("E344: Can't find directory \"%s\" in cdpath"), 5699 ff_file_to_find); 5700 else 5701 EMSG2(_("E345: Can't find file \"%s\" in path"), 5702 ff_file_to_find); 5703 } 5704 else 5705 { 5706 if (find_what == FINDFILE_DIR) 5707 EMSG2(_("E346: No more directory \"%s\" found in cdpath"), 5708 ff_file_to_find); 5709 else 5710 EMSG2(_("E347: No more file \"%s\" found in path"), 5711 ff_file_to_find); 5712 } 5713 } 5714 5715 theend: 5716 #ifdef AMIGA 5717 proc->pr_WindowPtr = save_winptr; 5718 #endif 5719 return file_name; 5720 } 5721 5722 #endif /* FEAT_SEARCHPATH */ 5723 5724 /* 5725 * Change directory to "new_dir". If FEAT_SEARCHPATH is defined, search 5726 * 'cdpath' for relative directory names, otherwise just mch_chdir(). 5727 */ 5728 int 5729 vim_chdir(new_dir) 5730 char_u *new_dir; 5731 { 5732 #ifndef FEAT_SEARCHPATH 5733 return mch_chdir((char *)new_dir); 5734 #else 5735 char_u *dir_name; 5736 int r; 5737 5738 dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir), 5739 FNAME_MESS, curbuf->b_ffname); 5740 if (dir_name == NULL) 5741 return -1; 5742 r = mch_chdir((char *)dir_name); 5743 vim_free(dir_name); 5744 return r; 5745 #endif 5746 } 5747 5748 /* 5749 * Get user name from machine-specific function. 5750 * Returns the user name in "buf[len]". 5751 * Some systems are quite slow in obtaining the user name (Windows NT), thus 5752 * cache the result. 5753 * Returns OK or FAIL. 5754 */ 5755 int 5756 get_user_name(buf, len) 5757 char_u *buf; 5758 int len; 5759 { 5760 if (username == NULL) 5761 { 5762 if (mch_get_user_name(buf, len) == FAIL) 5763 return FAIL; 5764 username = vim_strsave(buf); 5765 } 5766 else 5767 vim_strncpy(buf, username, len - 1); 5768 return OK; 5769 } 5770 5771 #ifndef HAVE_QSORT 5772 /* 5773 * Our own qsort(), for systems that don't have it. 5774 * It's simple and slow. From the K&R C book. 5775 */ 5776 void 5777 qsort(base, elm_count, elm_size, cmp) 5778 void *base; 5779 size_t elm_count; 5780 size_t elm_size; 5781 int (*cmp) __ARGS((const void *, const void *)); 5782 { 5783 char_u *buf; 5784 char_u *p1; 5785 char_u *p2; 5786 int i, j; 5787 int gap; 5788 5789 buf = alloc((unsigned)elm_size); 5790 if (buf == NULL) 5791 return; 5792 5793 for (gap = elm_count / 2; gap > 0; gap /= 2) 5794 for (i = gap; i < elm_count; ++i) 5795 for (j = i - gap; j >= 0; j -= gap) 5796 { 5797 /* Compare the elements. */ 5798 p1 = (char_u *)base + j * elm_size; 5799 p2 = (char_u *)base + (j + gap) * elm_size; 5800 if ((*cmp)((void *)p1, (void *)p2) <= 0) 5801 break; 5802 /* Exchange the elements. */ 5803 mch_memmove(buf, p1, elm_size); 5804 mch_memmove(p1, p2, elm_size); 5805 mch_memmove(p2, buf, elm_size); 5806 } 5807 5808 vim_free(buf); 5809 } 5810 #endif 5811 5812 /* 5813 * Sort an array of strings. 5814 */ 5815 static int 5816 #ifdef __BORLANDC__ 5817 _RTLENTRYF 5818 #endif 5819 sort_compare __ARGS((const void *s1, const void *s2)); 5820 5821 static int 5822 #ifdef __BORLANDC__ 5823 _RTLENTRYF 5824 #endif 5825 sort_compare(s1, s2) 5826 const void *s1; 5827 const void *s2; 5828 { 5829 return STRCMP(*(char **)s1, *(char **)s2); 5830 } 5831 5832 void 5833 sort_strings(files, count) 5834 char_u **files; 5835 int count; 5836 { 5837 qsort((void *)files, (size_t)count, sizeof(char_u *), sort_compare); 5838 } 5839 5840 #if !defined(NO_EXPANDPATH) || defined(PROTO) 5841 /* 5842 * Compare path "p[]" to "q[]". 5843 * If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]" 5844 * Return value like strcmp(p, q), but consider path separators. 5845 */ 5846 int 5847 pathcmp(p, q, maxlen) 5848 const char *p, *q; 5849 int maxlen; 5850 { 5851 int i; 5852 int c1, c2; 5853 const char *s = NULL; 5854 5855 for (i = 0; maxlen < 0 || i < maxlen; i += MB_PTR2LEN((char_u *)p + i)) 5856 { 5857 c1 = PTR2CHAR((char_u *)p + i); 5858 c2 = PTR2CHAR((char_u *)q + i); 5859 5860 /* End of "p": check if "q" also ends or just has a slash. */ 5861 if (c1 == NUL) 5862 { 5863 if (c2 == NUL) /* full match */ 5864 return 0; 5865 s = q; 5866 break; 5867 } 5868 5869 /* End of "q": check if "p" just has a slash. */ 5870 if (c2 == NUL) 5871 { 5872 s = p; 5873 break; 5874 } 5875 5876 if ((p_fic ? MB_TOUPPER(c1) != MB_TOUPPER(c2) : c1 != c2) 5877 #ifdef BACKSLASH_IN_FILENAME 5878 /* consider '/' and '\\' to be equal */ 5879 && !((c1 == '/' && c2 == '\\') 5880 || (c1 == '\\' && c2 == '/')) 5881 #endif 5882 ) 5883 { 5884 if (vim_ispathsep(c1)) 5885 return -1; 5886 if (vim_ispathsep(c2)) 5887 return 1; 5888 return p_fic ? MB_TOUPPER(c1) - MB_TOUPPER(c2) 5889 : c1 - c2; /* no match */ 5890 } 5891 } 5892 if (s == NULL) /* "i" ran into "maxlen" */ 5893 return 0; 5894 5895 c1 = PTR2CHAR((char_u *)s + i); 5896 c2 = PTR2CHAR((char_u *)s + i + MB_PTR2LEN((char_u *)s + i)); 5897 /* ignore a trailing slash, but not "//" or ":/" */ 5898 if (c2 == NUL 5899 && i > 0 5900 && !after_pathsep((char_u *)s, (char_u *)s + i) 5901 #ifdef BACKSLASH_IN_FILENAME 5902 && (c1 == '/' || c1 == '\\') 5903 #else 5904 && c1 == '/' 5905 #endif 5906 ) 5907 return 0; /* match with trailing slash */ 5908 if (s == q) 5909 return -1; /* no match */ 5910 return 1; 5911 } 5912 #endif 5913 5914 /* 5915 * The putenv() implementation below comes from the "screen" program. 5916 * Included with permission from Juergen Weigert. 5917 * See pty.c for the copyright notice. 5918 */ 5919 5920 /* 5921 * putenv -- put value into environment 5922 * 5923 * Usage: i = putenv (string) 5924 * int i; 5925 * char *string; 5926 * 5927 * where string is of the form <name>=<value>. 5928 * Putenv returns 0 normally, -1 on error (not enough core for malloc). 5929 * 5930 * Putenv may need to add a new name into the environment, or to 5931 * associate a value longer than the current value with a particular 5932 * name. So, to make life simpler, putenv() copies your entire 5933 * environment into the heap (i.e. malloc()) from the stack 5934 * (i.e. where it resides when your process is initiated) the first 5935 * time you call it. 5936 * 5937 * (history removed, not very interesting. See the "screen" sources.) 5938 */ 5939 5940 #if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV) 5941 5942 #define EXTRASIZE 5 /* increment to add to env. size */ 5943 5944 static int envsize = -1; /* current size of environment */ 5945 #ifndef MACOS_CLASSIC 5946 extern 5947 #endif 5948 char **environ; /* the global which is your env. */ 5949 5950 static int findenv __ARGS((char *name)); /* look for a name in the env. */ 5951 static int newenv __ARGS((void)); /* copy env. from stack to heap */ 5952 static int moreenv __ARGS((void)); /* incr. size of env. */ 5953 5954 int 5955 putenv(string) 5956 const char *string; 5957 { 5958 int i; 5959 char *p; 5960 5961 if (envsize < 0) 5962 { /* first time putenv called */ 5963 if (newenv() < 0) /* copy env. to heap */ 5964 return -1; 5965 } 5966 5967 i = findenv((char *)string); /* look for name in environment */ 5968 5969 if (i < 0) 5970 { /* name must be added */ 5971 for (i = 0; environ[i]; i++); 5972 if (i >= (envsize - 1)) 5973 { /* need new slot */ 5974 if (moreenv() < 0) 5975 return -1; 5976 } 5977 p = (char *)alloc((unsigned)(strlen(string) + 1)); 5978 if (p == NULL) /* not enough core */ 5979 return -1; 5980 environ[i + 1] = 0; /* new end of env. */ 5981 } 5982 else 5983 { /* name already in env. */ 5984 p = vim_realloc(environ[i], strlen(string) + 1); 5985 if (p == NULL) 5986 return -1; 5987 } 5988 sprintf(p, "%s", string); /* copy into env. */ 5989 environ[i] = p; 5990 5991 return 0; 5992 } 5993 5994 static int 5995 findenv(name) 5996 char *name; 5997 { 5998 char *namechar, *envchar; 5999 int i, found; 6000 6001 found = 0; 6002 for (i = 0; environ[i] && !found; i++) 6003 { 6004 envchar = environ[i]; 6005 namechar = name; 6006 while (*namechar && *namechar != '=' && (*namechar == *envchar)) 6007 { 6008 namechar++; 6009 envchar++; 6010 } 6011 found = ((*namechar == '\0' || *namechar == '=') && *envchar == '='); 6012 } 6013 return found ? i - 1 : -1; 6014 } 6015 6016 static int 6017 newenv() 6018 { 6019 char **env, *elem; 6020 int i, esize; 6021 6022 #ifdef MACOS 6023 /* for Mac a new, empty environment is created */ 6024 i = 0; 6025 #else 6026 for (i = 0; environ[i]; i++) 6027 ; 6028 #endif 6029 esize = i + EXTRASIZE + 1; 6030 env = (char **)alloc((unsigned)(esize * sizeof (elem))); 6031 if (env == NULL) 6032 return -1; 6033 6034 #ifndef MACOS 6035 for (i = 0; environ[i]; i++) 6036 { 6037 elem = (char *)alloc((unsigned)(strlen(environ[i]) + 1)); 6038 if (elem == NULL) 6039 return -1; 6040 env[i] = elem; 6041 strcpy(elem, environ[i]); 6042 } 6043 #endif 6044 6045 env[i] = 0; 6046 environ = env; 6047 envsize = esize; 6048 return 0; 6049 } 6050 6051 static int 6052 moreenv() 6053 { 6054 int esize; 6055 char **env; 6056 6057 esize = envsize + EXTRASIZE; 6058 env = (char **)vim_realloc((char *)environ, esize * sizeof (*env)); 6059 if (env == 0) 6060 return -1; 6061 environ = env; 6062 envsize = esize; 6063 return 0; 6064 } 6065 6066 # ifdef USE_VIMPTY_GETENV 6067 char_u * 6068 vimpty_getenv(string) 6069 const char_u *string; 6070 { 6071 int i; 6072 char_u *p; 6073 6074 if (envsize < 0) 6075 return NULL; 6076 6077 i = findenv((char *)string); 6078 6079 if (i < 0) 6080 return NULL; 6081 6082 p = vim_strchr((char_u *)environ[i], '='); 6083 return (p + 1); 6084 } 6085 # endif 6086 6087 #endif /* !defined(HAVE_SETENV) && !defined(HAVE_PUTENV) */ 6088 6089 #if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO) 6090 /* 6091 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have 6092 * rights to write into. 6093 */ 6094 int 6095 filewritable(fname) 6096 char_u *fname; 6097 { 6098 int retval = 0; 6099 #if defined(UNIX) || defined(VMS) 6100 int perm = 0; 6101 #endif 6102 6103 #if defined(UNIX) || defined(VMS) 6104 perm = mch_getperm(fname); 6105 #endif 6106 #ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */ 6107 if ( 6108 # ifdef WIN3264 6109 mch_writable(fname) && 6110 # else 6111 # if defined(UNIX) || defined(VMS) 6112 (perm & 0222) && 6113 # endif 6114 # endif 6115 mch_access((char *)fname, W_OK) == 0 6116 ) 6117 #endif 6118 { 6119 ++retval; 6120 if (mch_isdir(fname)) 6121 ++retval; 6122 } 6123 return retval; 6124 } 6125 #endif 6126 6127 /* 6128 * Print an error message with one or two "%s" and one or two string arguments. 6129 * This is not in message.c to avoid a warning for prototypes. 6130 */ 6131 int 6132 emsg3(s, a1, a2) 6133 char_u *s, *a1, *a2; 6134 { 6135 if (emsg_not_now()) 6136 return TRUE; /* no error messages at the moment */ 6137 #ifdef HAVE_STDARG_H 6138 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, a1, a2); 6139 #else 6140 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, (long_u)a1, (long_u)a2); 6141 #endif 6142 return emsg(IObuff); 6143 } 6144 6145 /* 6146 * Print an error message with one "%ld" and one long int argument. 6147 * This is not in message.c to avoid a warning for prototypes. 6148 */ 6149 int 6150 emsgn(s, n) 6151 char_u *s; 6152 long n; 6153 { 6154 if (emsg_not_now()) 6155 return TRUE; /* no error messages at the moment */ 6156 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, n); 6157 return emsg(IObuff); 6158 } 6159 6160 #if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO) 6161 /* 6162 * Read 2 bytes from "fd" and turn them into an int, MSB first. 6163 */ 6164 int 6165 get2c(fd) 6166 FILE *fd; 6167 { 6168 int n; 6169 6170 n = getc(fd); 6171 n = (n << 8) + getc(fd); 6172 return n; 6173 } 6174 6175 /* 6176 * Read 3 bytes from "fd" and turn them into an int, MSB first. 6177 */ 6178 int 6179 get3c(fd) 6180 FILE *fd; 6181 { 6182 int n; 6183 6184 n = getc(fd); 6185 n = (n << 8) + getc(fd); 6186 n = (n << 8) + getc(fd); 6187 return n; 6188 } 6189 6190 /* 6191 * Read 4 bytes from "fd" and turn them into an int, MSB first. 6192 */ 6193 int 6194 get4c(fd) 6195 FILE *fd; 6196 { 6197 /* Use unsigned rather than int otherwise result is undefined 6198 * when left-shift sets the MSB. */ 6199 unsigned n; 6200 6201 n = (unsigned)getc(fd); 6202 n = (n << 8) + (unsigned)getc(fd); 6203 n = (n << 8) + (unsigned)getc(fd); 6204 n = (n << 8) + (unsigned)getc(fd); 6205 return (int)n; 6206 } 6207 6208 /* 6209 * Read 8 bytes from "fd" and turn them into a time_t, MSB first. 6210 */ 6211 time_t 6212 get8ctime(fd) 6213 FILE *fd; 6214 { 6215 time_t n = 0; 6216 int i; 6217 6218 for (i = 0; i < 8; ++i) 6219 n = (n << 8) + getc(fd); 6220 return n; 6221 } 6222 6223 /* 6224 * Read a string of length "cnt" from "fd" into allocated memory. 6225 * Returns NULL when out of memory or unable to read that many bytes. 6226 */ 6227 char_u * 6228 read_string(fd, cnt) 6229 FILE *fd; 6230 int cnt; 6231 { 6232 char_u *str; 6233 int i; 6234 int c; 6235 6236 /* allocate memory */ 6237 str = alloc((unsigned)cnt + 1); 6238 if (str != NULL) 6239 { 6240 /* Read the string. Quit when running into the EOF. */ 6241 for (i = 0; i < cnt; ++i) 6242 { 6243 c = getc(fd); 6244 if (c == EOF) 6245 { 6246 vim_free(str); 6247 return NULL; 6248 } 6249 str[i] = c; 6250 } 6251 str[i] = NUL; 6252 } 6253 return str; 6254 } 6255 6256 /* 6257 * Write a number to file "fd", MSB first, in "len" bytes. 6258 */ 6259 int 6260 put_bytes(fd, nr, len) 6261 FILE *fd; 6262 long_u nr; 6263 int len; 6264 { 6265 int i; 6266 6267 for (i = len - 1; i >= 0; --i) 6268 if (putc((int)(nr >> (i * 8)), fd) == EOF) 6269 return FAIL; 6270 return OK; 6271 } 6272 6273 #ifdef _MSC_VER 6274 # if (_MSC_VER <= 1200) 6275 /* This line is required for VC6 without the service pack. Also see the 6276 * matching #pragma below. */ 6277 # pragma optimize("", off) 6278 # endif 6279 #endif 6280 6281 /* 6282 * Write time_t to file "fd" in 8 bytes. 6283 */ 6284 void 6285 put_time(fd, the_time) 6286 FILE *fd; 6287 time_t the_time; 6288 { 6289 char_u buf[8]; 6290 6291 time_to_bytes(the_time, buf); 6292 (void)fwrite(buf, (size_t)8, (size_t)1, fd); 6293 } 6294 6295 /* 6296 * Write time_t to "buf[8]". 6297 */ 6298 void 6299 time_to_bytes(the_time, buf) 6300 time_t the_time; 6301 char_u *buf; 6302 { 6303 int c; 6304 int i; 6305 int bi = 0; 6306 time_t wtime = the_time; 6307 6308 /* time_t can be up to 8 bytes in size, more than long_u, thus we 6309 * can't use put_bytes() here. 6310 * Another problem is that ">>" may do an arithmetic shift that keeps the 6311 * sign. This happens for large values of wtime. A cast to long_u may 6312 * truncate if time_t is 8 bytes. So only use a cast when it is 4 bytes, 6313 * it's safe to assume that long_u is 4 bytes or more and when using 8 6314 * bytes the top bit won't be set. */ 6315 for (i = 7; i >= 0; --i) 6316 { 6317 if (i + 1 > (int)sizeof(time_t)) 6318 /* ">>" doesn't work well when shifting more bits than avail */ 6319 buf[bi++] = 0; 6320 else 6321 { 6322 #if defined(SIZEOF_TIME_T) && SIZEOF_TIME_T > 4 6323 c = (int)(wtime >> (i * 8)); 6324 #else 6325 c = (int)((long_u)wtime >> (i * 8)); 6326 #endif 6327 buf[bi++] = c; 6328 } 6329 } 6330 } 6331 6332 #ifdef _MSC_VER 6333 # if (_MSC_VER <= 1200) 6334 # pragma optimize("", on) 6335 # endif 6336 #endif 6337 6338 #endif 6339 6340 #if (defined(FEAT_MBYTE) && defined(FEAT_QUICKFIX)) \ 6341 || defined(FEAT_SPELL) || defined(PROTO) 6342 /* 6343 * Return TRUE if string "s" contains a non-ASCII character (128 or higher). 6344 * When "s" is NULL FALSE is returned. 6345 */ 6346 int 6347 has_non_ascii(s) 6348 char_u *s; 6349 { 6350 char_u *p; 6351 6352 if (s != NULL) 6353 for (p = s; *p != NUL; ++p) 6354 if (*p >= 128) 6355 return TRUE; 6356 return FALSE; 6357 } 6358 #endif 6359