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