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