robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1// File searching functions for 'path', 'tags' and 'cdpath' options. 2// 3// External visible functions: 4// vim_findfile_init() creates/initialises the search context 5// vim_findfile_free_visited() free list of visited files/dirs of search 6// context 7// vim_findfile() find a file in the search context 8// vim_findfile_cleanup() cleanup/free search context created by 9// vim_findfile_init() 10// 11// All static functions and variables start with 'ff_' 12// 13// In general it works like this: 14// First you create yourself a search context by calling vim_findfile_init(). 15// It is possible to give a search context from a previous call to 16// vim_findfile_init(), so it can be reused. After this you call vim_findfile() 17// until you are satisfied with the result or it returns NULL. On every call it 18// returns the next file which matches the conditions given to 19// vim_findfile_init(). If it doesn't find a next file it returns NULL. 20// 21// It is possible to call vim_findfile_init() again to reinitialise your search 22// with some new parameters. Don't forget to pass your old search context to 23// it, so it can reuse it and especially reuse the list of already visited 24// directories. If you want to delete the list of already visited directories 25// simply call vim_findfile_free_visited(). 26// 27// When you are done call vim_findfile_cleanup() to free the search context. 28// 29// The function vim_findfile_init() has a long comment, which describes the 30// needed parameters. 31// 32// 33// 34// ATTENTION: 35// ========== 36// We use an allocated search context, these functions are NOT thread-safe!!!!! 37// 38// To minimize parameter passing (or because I'm too lazy), only the 39// external visible functions get a search context as a parameter. This is 40// then assigned to a static global, which is used throughout the local 41// functions. 42 43#include <assert.h> 44#include <ctype.h> 45#include <inttypes.h> 46#include <limits.h> 47#include <stdbool.h> 48#include <stddef.h> 49#include <stdio.h> 50#include <stdlib.h> 51#include <string.h> 52 53#include "nvim/api/private/defs.h" 54#include "nvim/api/private/helpers.h" 55#include "nvim/ascii_defs.h" 56#include "nvim/autocmd.h" 57#include "nvim/autocmd_defs.h" 58#include "nvim/buffer_defs.h" 59#include "nvim/charset.h" 60#include "nvim/cursor.h" 61#include "nvim/errors.h" 62#include "nvim/eval.h" 63#include "nvim/eval/typval.h" 64#include "nvim/eval/typval_defs.h" 65#include "nvim/eval/vars.h" 66#include "nvim/file_search.h" 67#include "nvim/gettext_defs.h" 68#include "nvim/globals.h" 69#include "nvim/macros_defs.h" 70#include "nvim/mbyte.h" 71#include "nvim/memory.h" 72#include "nvim/message.h" 73#include "nvim/normal.h" 74#include "nvim/option.h" 75#include "nvim/option_defs.h" 76#include "nvim/option_vars.h" 77#include "nvim/os/fs.h" 78#include "nvim/os/fs_defs.h" 79#include "nvim/os/input.h" 80#include "nvim/os/os.h" 81#include "nvim/os/os_defs.h" 82#include "nvim/path.h" 83#include "nvim/strings.h" 84#include "nvim/vim_defs.h" 85 86static String ff_expand_buffer = STRING_INIT ;// used for expanding filenames 87 88// type for the directory search stack 89typedef struct ff_stack { 90struct ff_stack * ffs_prev ; 91 92// the fix part (no wildcards) and the part containing the wildcards 93// of the search path 94String ffs_fix_path ; 95String ffs_wc_path ; 96 97// files/dirs found in the above directory, matched by the first wildcard 98// of wc_part 99char ** ffs_filearray ; 100int ffs_filearray_size ; 101int ffs_filearray_cur ;// needed for partly handled dirs 102 103// to store status of partly handled directories 104// 0: we work on this directory for the first time 105// 1: this directory was partly searched in an earlier step 106int ffs_stage ; 107 108// How deep are we in the directory tree? 109// Counts backward from value of level parameter to vim_findfile_init 110int ffs_level ; 111 112// Did we already expand '**' to an empty string? 113int ffs_star_star_empty ; 114}ff_stack_T ; 115 116// type for already visited directories or files. 117typedef struct ff_visited { 118struct ff_visited * ffv_next ; 119 120// Visited directories are different if the wildcard string are 121// different. So we have to save it. 122char * ffv_wc_path ; 123 124// use FileID for comparison (needed because of links), else use filename. 125bool file_id_valid ; 126FileID file_id ; 127// The memory for this struct is allocated according to the length of 128// ffv_fname. 129char ffv_fname []; 130}ff_visited_T ; 131 132// We might have to manage several visited lists during a search. 133// This is especially needed for the tags option. If tags is set to: 134// "./++/tags,./++/TAGS,++/tags" (replace + with *) 135// So we have to do 3 searches: 136// 1) search from the current files directory downward for the file "tags" 137// 2) search from the current files directory downward for the file "TAGS" 138// 3) search from Vims current directory downwards for the file "tags" 139// As you can see, the first and the third search are for the same file, so for 140// the third search we can use the visited list of the first search. For the 141// second search we must start from an empty visited list. 142// The struct ff_visited_list_hdr is used to manage a linked list of already 143// visited lists. 144typedef struct ff_visited_list_hdr { 145struct ff_visited_list_hdr * ffvl_next ; 146 147// the filename the attached visited list is for 148char * ffvl_filename ; 149 150ff_visited_T * ffvl_visited_list ; 151}ff_visited_list_hdr_T ; 152 153// '**' can be expanded to several directory levels. 154// Set the default maximum depth. 155#define FF_MAX_STAR_STAR_EXPAND 30 156 157// The search context: 158// ffsc_stack_ptr: the stack for the dirs to search 159// ffsc_visited_list: the currently active visited list 160// ffsc_dir_visited_list: the currently active visited list for search dirs 161// ffsc_visited_lists_list: the list of all visited lists 162// ffsc_dir_visited_lists_list: the list of all visited lists for search dirs 163// ffsc_file_to_search: the file to search for 164// ffsc_start_dir: the starting directory, if search path was relative 165// ffsc_fix_path: the fix part of the given path (without wildcards) 166// Needed for upward search. 167// ffsc_wc_path: the part of the given path containing wildcards 168// ffsc_level: how many levels of dirs to search downwards 169// ffsc_stopdirs_v: array of stop directories for upward search 170// ffsc_find_what: FINDFILE_BOTH, FINDFILE_DIR or FINDFILE_FILE 171// ffsc_tagfile: searching for tags file, don't use 'suffixesadd' 172typedef struct { 173ff_stack_T * ffsc_stack_ptr ; 174ff_visited_list_hdr_T * ffsc_visited_list ; 175ff_visited_list_hdr_T * ffsc_dir_visited_list ; 176ff_visited_list_hdr_T * ffsc_visited_lists_list ; 177ff_visited_list_hdr_T * ffsc_dir_visited_lists_list ; 178String ffsc_file_to_search ; 179String ffsc_start_dir ; 180String ffsc_fix_path ; 181String ffsc_wc_path ; 182int ffsc_level ; 183String * ffsc_stopdirs_v ; 184int ffsc_find_what ; 185int ffsc_tagfile ; 186}ff_search_ctx_T ; 187 188// locally needed functions 189 190#include "file_search.c.generated.h" 191 192static const char e_path_too_long_for_completion [] 193= N_ ("E854: Path too long for completion" ); 194 195/// Initialization routine for vim_findfile(). 196/// 197/// Returns the newly allocated search context or NULL if an error occurred. 198/// 199/// Don't forget to clean up by calling vim_findfile_cleanup() if you are done 200/// with the search context. 201/// 202/// Find the file 'filename' in the directory 'path'. 203/// The parameter 'path' may contain wildcards. If so only search 'level' 204/// directories deep. The parameter 'level' is the absolute maximum and is 205/// not related to restricts given to the '**' wildcard. If 'level' is 100 206/// and you use '**200' vim_findfile() will stop after 100 levels. 207/// 208/// 'filename' cannot contain wildcards! It is used as-is, no backslashes to 209/// escape special characters. 210/// 211/// If 'stopdirs' is not NULL and nothing is found downward, the search is 212/// restarted on the next higher directory level. This is repeated until the 213/// start-directory of a search is contained in 'stopdirs'. 'stopdirs' has the 214/// format ";*<dirname>*\(;<dirname>\)*;\=$". 215/// 216/// If the 'path' is relative, the starting dir for the search is either VIM's 217/// current dir or if the path starts with "./" the current files dir. 218/// If the 'path' is absolute, the starting dir is that part of the path before 219/// the first wildcard. 220/// 221/// Upward search is only done on the starting dir. 222/// 223/// If 'free_visited' is true the list of already visited files/directories is 224/// cleared. Set this to false if you just want to search from another 225/// directory, but want to be sure that no directory from a previous search is 226/// searched again. This is useful if you search for a file at different places. 227/// The list of visited files/dirs can also be cleared with the function 228/// vim_findfile_free_visited(). 229/// 230/// Set the parameter 'find_what' to FINDFILE_DIR if you want to search for 231/// directories only, FINDFILE_FILE for files only, FINDFILE_BOTH for both. 232/// 233/// A search context returned by a previous call to vim_findfile_init() can be 234/// passed in the parameter "search_ctx_arg". This context is reused and 235/// reinitialized with the new parameters. The list of already visited 236/// directories from this context is only deleted if the parameter 237/// "free_visited" is true. Be aware that the passed "search_ctx_arg" is freed 238/// if the reinitialization fails. 239/// 240/// If you don't have a search context from a previous call "search_ctx_arg" 241/// must be NULL. 242/// 243/// This function silently ignores a few errors, vim_findfile() will have 244/// limited functionality then. 245/// 246/// @param tagfile expanding names of tags files 247/// @param rel_fname file name to use for "." 248void * vim_findfile_init (char * path ,char * filename ,size_t filenamelen ,char * stopdirs ,int level , 249int free_visited ,int find_what ,void * search_ctx_arg ,int tagfile , 250char * rel_fname ) 251{ 252ff_stack_T * sptr ; 253ff_search_ctx_T * search_ctx ; 254 255// If a search context is given by the caller, reuse it, else allocate a 256// new one. 257if (search_ctx_arg != NULL ) { 258search_ctx = search_ctx_arg ; 259 }else { 260search_ctx = xcalloc (1 ,sizeof (ff_search_ctx_T )); 261 } 262search_ctx -> ffsc_find_what = find_what ; 263search_ctx -> ffsc_tagfile = tagfile ; 264 265// clear the search context, but NOT the visited lists 266ff_clear (search_ctx ); 267 268// clear visited list if wanted 269if (free_visited == true) { 270vim_findfile_free_visited (search_ctx ); 271 }else { 272// Reuse old visited lists. Get the visited list for the given 273// filename. If no list for the current filename exists, creates a new 274// one. 275search_ctx -> ffsc_visited_list 276= ff_get_visited_list (filename ,filenamelen , 277& search_ctx -> ffsc_visited_lists_list ); 278if (search_ctx -> ffsc_visited_list == NULL ) { 279 gotoerror_return ; 280 } 281search_ctx -> ffsc_dir_visited_list 282= ff_get_visited_list (filename ,filenamelen , 283& search_ctx -> ffsc_dir_visited_lists_list ); 284if (search_ctx -> ffsc_dir_visited_list == NULL ) { 285 gotoerror_return ; 286 } 287 } 288 289if (ff_expand_buffer .data == NULL ) { 290ff_expand_buffer .size = 0 ; 291ff_expand_buffer .data = xmalloc (MAXPATHL ); 292 } 293 294// Store information on starting dir now if path is relative. 295// If path is absolute, we do that later. 296if (path [0 ]== '.' 297&& (vim_ispathsep (path [1 ])|| path [1 ]== NUL ) 298&& (!tagfile || vim_strchr (p_cpo ,kCpoDottag )== NULL ) 299&& rel_fname != NULL ) { 300size_t len = (size_t )(path_tail (rel_fname )- rel_fname ); 301 302if (!vim_isAbsName (rel_fname )&& len + 1 < MAXPATHL ) { 303// Make the start dir an absolute path name. 304xmemcpyz (ff_expand_buffer .data ,rel_fname ,len ); 305ff_expand_buffer .size = len ; 306search_ctx -> ffsc_start_dir = cstr_as_string (FullName_save (ff_expand_buffer .data , false)); 307 }else { 308search_ctx -> ffsc_start_dir = cbuf_to_string (rel_fname ,len ); 309 } 310if (*++ path != NUL ) { 311path ++ ; 312 } 313 }else if (* path == NUL || !vim_isAbsName (path )) { 314#ifdef BACKSLASH_IN_FILENAME 315// "c:dir" needs "c:" to be expanded, otherwise use current dir 316if (* path != NUL && path [1 ]== ':' ) { 317char drive [3 ]; 318 319drive [0 ]= path [0 ]; 320drive [1 ]= ':' ; 321drive [2 ]= NUL ; 322if (vim_FullName (drive ,ff_expand_buffer .data ,MAXPATHL , true)== FAIL ) { 323 gotoerror_return ; 324 } 325path += 2 ; 326 }else 327#endif 328if (os_dirname (ff_expand_buffer .data ,MAXPATHL )== FAIL ) { 329 gotoerror_return ; 330 } 331ff_expand_buffer .size = strlen (ff_expand_buffer .data ); 332 333search_ctx -> ffsc_start_dir = copy_string (ff_expand_buffer ,NULL ); 334 } 335 336// If stopdirs are given, split them into an array of pointers. 337// If this fails (mem allocation), there is no upward search at all or a 338// stop directory is not recognized -> continue silently. 339// If stopdirs just contains a ";" or is empty, 340// search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This 341// is handled as unlimited upward search. See function 342// ff_path_in_stoplist() for details. 343if (stopdirs != NULL ) { 344char * walker = stopdirs ; 345 346while (* walker == ';' ) { 347walker ++ ; 348 } 349 350size_t dircount = 1 ; 351search_ctx -> ffsc_stopdirs_v = xmalloc (sizeof (String )); 352 353do { 354char * helper = walker ; 355void * ptr = xrealloc (search_ctx -> ffsc_stopdirs_v , 356 (dircount + 1 )* sizeof (String )); 357search_ctx -> ffsc_stopdirs_v = ptr ; 358walker = vim_strchr (walker ,';' ); 359assert (!walker || walker - helper >=0 ); 360size_t len = walker ? (size_t )(walker - helper ) :strlen (helper ); 361// "" means ascent till top of directory tree. 362if (* helper != NUL && !vim_isAbsName (helper )&& len + 1 < MAXPATHL ) { 363// Make the stop dir an absolute path name. 364xmemcpyz (ff_expand_buffer .data ,helper ,len ); 365ff_expand_buffer .size = len ; 366search_ctx -> ffsc_stopdirs_v [dircount - 1 ]= cstr_as_string (FullName_save (helper ,len )); 367 }else { 368search_ctx -> ffsc_stopdirs_v [dircount - 1 ]= cbuf_to_string (helper ,len ); 369 } 370if (walker ) { 371walker ++ ; 372 } 373dircount ++ ; 374 }while (walker != NULL ); 375 376search_ctx -> ffsc_stopdirs_v [dircount - 1 ]= NULL_STRING ; 377 } 378 379search_ctx -> ffsc_level = level ; 380 381// split into: 382// -fix path 383// -wildcard_stuff (might be NULL) 384char * wc_part = vim_strchr (path ,'*' ); 385if (wc_part != NULL ) { 386int64_t llevel ; 387char * errpt ; 388 389// save the fix part of the path 390assert (wc_part - path >=0 ); 391search_ctx -> ffsc_fix_path = cbuf_to_string (path , (size_t )(wc_part - path )); 392 393// copy wc_path and add restricts to the '**' wildcard. 394// The octet after a '**' is used as a (binary) counter. 395// So '**3' is transposed to '**^C' ('^C' is ASCII value 3) 396// or '**76' is transposed to '**N'( 'N' is ASCII value 76). 397// If no restrict is given after '**' the default is used. 398// Due to this technique the path looks awful if you print it as a 399// string. 400ff_expand_buffer .size = 0 ; 401while (* wc_part != NUL ) { 402if (ff_expand_buffer .size + 5 >=MAXPATHL ) { 403emsg (_ (e_path_too_long_for_completion )); 404break ; 405 } 406if (strncmp (wc_part ,"**" ,2 )== 0 ) { 407ff_expand_buffer .data [ff_expand_buffer .size ++ ]= * wc_part ++ ; 408ff_expand_buffer .data [ff_expand_buffer .size ++ ]= * wc_part ++ ; 409 410errpt = wc_part ; 411// Use def=255 so that overflow/too-large values keep the "max expand" behavior. 412llevel = getdigits (& errpt , false,255 ); 413if (errpt != wc_part && llevel > 0 && llevel < 255 ) { 414ff_expand_buffer .data [ff_expand_buffer .size ++ ]= (char )llevel ; 415 }else if (errpt != wc_part && llevel == 0 ) { 416// restrict is 0 -> remove already added '**' 417ff_expand_buffer .size -= 2 ; 418 }else { 419ff_expand_buffer .data [ff_expand_buffer .size ++ ]= FF_MAX_STAR_STAR_EXPAND ; 420 } 421wc_part = errpt ; 422if (* wc_part != NUL && !vim_ispathsep (* wc_part )) { 423semsg (_ ( 424"E343: Invalid path: '**[number]' must be at the end of the path or be followed by '%s'." ), 425PATHSEPSTR ); 426 gotoerror_return ; 427 } 428 }else { 429ff_expand_buffer .data [ff_expand_buffer .size ++ ]= * wc_part ++ ; 430 } 431 } 432ff_expand_buffer .data [ff_expand_buffer .size ]= NUL ; 433search_ctx -> ffsc_wc_path = copy_string (ff_expand_buffer , false); 434 }else { 435search_ctx -> ffsc_fix_path = cstr_to_string (path ); 436 } 437 438if (search_ctx -> ffsc_start_dir .data == NULL ) { 439// store the fix part as startdir. 440// This is needed if the parameter path is fully qualified. 441search_ctx -> ffsc_start_dir = copy_string (search_ctx -> ffsc_fix_path , false); 442search_ctx -> ffsc_fix_path .data [0 ]= NUL ; 443search_ctx -> ffsc_fix_path .size = 0 ; 444 } 445 446// create an absolute path 447if (search_ctx -> ffsc_start_dir .size 448+ search_ctx -> ffsc_fix_path .size + 3 >=MAXPATHL ) { 449emsg (_ (e_path_too_long_for_completion )); 450 gotoerror_return ; 451 } 452 453bool add_sep = !after_pathsep (search_ctx -> ffsc_start_dir .data , 454search_ctx -> ffsc_start_dir .data + search_ctx -> ffsc_start_dir .size ); 455ff_expand_buffer .size = (size_t )vim_snprintf (ff_expand_buffer .data , 456MAXPATHL , 457"%s%s" , 458search_ctx -> ffsc_start_dir .data , 459add_sep ?PATHSEPSTR :"" ); 460assert (ff_expand_buffer .size < MAXPATHL ); 461 462 { 463size_t bufsize = ff_expand_buffer .size + search_ctx -> ffsc_fix_path .size + 1 ; 464char * buf = xmalloc (bufsize ); 465 466vim_snprintf (buf , 467bufsize , 468"%s%s" , 469ff_expand_buffer .data , 470search_ctx -> ffsc_fix_path .data ); 471if (os_isdir (buf )) { 472if (search_ctx -> ffsc_fix_path .size > 0 ) { 473add_sep = !after_pathsep (search_ctx -> ffsc_fix_path .data , 474search_ctx -> ffsc_fix_path .data + search_ctx -> ffsc_fix_path .size ); 475ff_expand_buffer .size += (size_t )vim_snprintf (ff_expand_buffer .data + ff_expand_buffer .size , 476MAXPATHL - ff_expand_buffer .size , 477"%s%s" , 478search_ctx -> ffsc_fix_path .data , 479add_sep ?PATHSEPSTR :"" ); 480assert (ff_expand_buffer .size < MAXPATHL ); 481 } 482 }else { 483char * p = path_tail (search_ctx -> ffsc_fix_path .data ); 484int len = (int )search_ctx -> ffsc_fix_path .size ; 485 486if (p > search_ctx -> ffsc_fix_path .data ) { 487// do not add '..' to the path and start upwards searching 488len = (int )(p - search_ctx -> ffsc_fix_path .data )- 1 ; 489if ((len >=2 && strncmp (search_ctx -> ffsc_fix_path .data ,".." ,2 )== 0 ) 490&& (len == 2 || search_ctx -> ffsc_fix_path .data [2 ]== PATHSEP )) { 491xfree (buf ); 492 gotoerror_return ; 493 } 494 495add_sep = !after_pathsep (search_ctx -> ffsc_fix_path .data , 496search_ctx -> ffsc_fix_path .data + search_ctx -> ffsc_fix_path .size ); 497ff_expand_buffer .size += (size_t )vim_snprintf (ff_expand_buffer .data + ff_expand_buffer .size , 498MAXPATHL - ff_expand_buffer .size , 499"%.*s%s" , 500len , 501search_ctx -> ffsc_fix_path .data , 502add_sep ?PATHSEPSTR :"" ); 503assert (ff_expand_buffer .size < MAXPATHL ); 504 } 505 506if (search_ctx -> ffsc_wc_path .data != NULL ) { 507size_t tempsize = (search_ctx -> ffsc_fix_path .size - (size_t )len ) 508+ search_ctx -> ffsc_wc_path .size + 1 ; 509char * temp = xmalloc (tempsize ); 510search_ctx -> ffsc_wc_path .size = (size_t )vim_snprintf (temp , 511tempsize , 512"%s%s" , 513search_ctx -> ffsc_fix_path .data + len , 514search_ctx -> ffsc_wc_path .data ); 515assert (search_ctx -> ffsc_wc_path .size < tempsize ); 516xfree (search_ctx -> ffsc_wc_path .data ); 517search_ctx -> ffsc_wc_path .data = temp ; 518 } 519 } 520xfree (buf ); 521 } 522 523sptr = ff_create_stack_element (ff_expand_buffer .data , 524ff_expand_buffer .size , 525search_ctx -> ffsc_wc_path .data , 526search_ctx -> ffsc_wc_path .size , 527level ,0 ); 528 529ff_push (search_ctx ,sptr ); 530search_ctx -> ffsc_file_to_search = cbuf_to_string (filename ,filenamelen ); 531return search_ctx ; 532 533error_return : 534// We clear the search context now! 535// Even when the caller gave us a (perhaps valid) context we free it here, 536// as we might have already destroyed it. 537vim_findfile_cleanup (search_ctx ); 538return NULL ; 539} 540 541/// @return the stopdir string. Check that ';' is not escaped. 542char * vim_findfile_stopdir (char * buf ) 543{ 544for (;* buf != NUL && * buf != ';' && (buf [0 ]!= '\\' || buf [1 ]!= ';' );buf ++ ) {} 545char * dst = buf ; 546if (* buf == ';' ) { 547 gotois_semicolon ; 548 } 549if (* buf == NUL ) { 550 gotois_nul ; 551 } 552 gotostart ; 553while (* buf != NUL && * buf != ';' ) { 554if (buf [0 ]== '\\' && buf [1 ]== ';' ) { 555start : 556// Overwrite the escape char. 557* dst ++ = ';' ; 558buf += 2 ; 559 }else { 560* dst ++ = * buf ++ ; 561 } 562 } 563assert (dst < buf ); 564* dst = NUL ; 565if (* buf == ';' ) { 566is_semicolon : 567* buf = NUL ; 568buf ++ ; 569 }else {// if (*buf == NUL) 570is_nul : 571buf = NULL ; 572 } 573return buf ; 574} 575 576/// Clean up the given search context. Can handle a NULL pointer. 577void vim_findfile_cleanup (void * ctx ) 578{ 579if (ctx == NULL ) { 580return ; 581 } 582 583vim_findfile_free_visited (ctx ); 584ff_clear (ctx ); 585xfree (ctx ); 586} 587 588/// Find a file in a search context. 589/// The search context was created with vim_findfile_init() above. 590/// 591/// To get all matching files call this function until you get NULL. 592/// 593/// If the passed search_context is NULL, NULL is returned. 594/// 595/// The search algorithm is depth first. To change this replace the 596/// stack with a list (don't forget to leave partly searched directories on the 597/// top of the list). 598/// 599/// @return a pointer to an allocated file name or, 600/// NULL if nothing found. 601char * vim_findfile (void * search_ctx_arg ) 602{ 603String rest_of_wildcards ; 604char * path_end = NULL ; 605ff_stack_T * stackp = NULL ; 606 607if (search_ctx_arg == NULL ) { 608return NULL ; 609 } 610 611ff_search_ctx_T * search_ctx = (ff_search_ctx_T * )search_ctx_arg ; 612 613// filepath is used as buffer for various actions and as the storage to 614// return a found filename. 615String file_path = { .data = xmalloc (MAXPATHL ) }; 616 617// store the end of the start dir -- needed for upward search 618if (search_ctx -> ffsc_start_dir .data != NULL ) { 619path_end = & search_ctx -> ffsc_start_dir .data [search_ctx -> ffsc_start_dir .size ]; 620 } 621 622// upward search loop 623while (true) { 624// downward search loop 625while (true) { 626// check if user wants to stop the search 627os_breakcheck (); 628if (got_int ) { 629break ; 630 } 631 632// get directory to work on from stack 633stackp = ff_pop (search_ctx ); 634if (stackp == NULL ) { 635break ; 636 } 637 638// TODO(vim): decide if we leave this test in 639// 640// GOOD: don't search a directory(-tree) twice. 641// BAD: - check linked list for every new directory entered. 642// - check for double files also done below 643// 644// Here we check if we already searched this directory. 645// We already searched a directory if: 646// 1) The directory is the same. 647// 2) We would use the same wildcard string. 648// 649// Good if you have links on same directory via several ways 650// or you have selfreferences in directories (e.g. SuSE Linux 6.3: 651// /etc/rc.d/init.d is linked to /etc/rc.d -> endless loop) 652// 653// This check is only needed for directories we work on for the 654// first time (hence stackp->ff_filearray == NULL) 655if (stackp -> ffs_filearray == NULL 656&& ff_check_visited (& search_ctx -> ffsc_dir_visited_list -> ffvl_visited_list , 657stackp -> ffs_fix_path .data ,stackp -> ffs_fix_path .size , 658stackp -> ffs_wc_path .data ,stackp -> ffs_wc_path .size )== FAIL ) { 659#ifdef FF_VERBOSE 660if (p_verbose >=5 ) { 661verbose_enter_scroll (); 662smsg (0 ,"Already Searched: %s (%s)" , 663stackp -> ffs_fix_path .data ,stackp -> ffs_wc_path .data ); 664msg_puts ("\n" );// don't overwrite this either 665verbose_leave_scroll (); 666 } 667#endif 668ff_free_stack_element (stackp ); 669continue ; 670#ifdef FF_VERBOSE 671} else if ( p_verbose >= 5 ) { 672verbose_enter_scroll (); 673smsg ( 0 , "Searching: %s (%s)" , 674stackp -> ffs_fix_path . data , stackp -> ffs_wc_path . data ); 675msg_puts ( "\n" ); // don't overwrite this either 676verbose_leave_scroll (); 677#endif 678} 679 680// check depth 681if ( stackp -> ffs_level <= 0 ) { 682ff_free_stack_element ( stackp ); 683continue; 684} 685 686file_path . data [ 0 ] = NUL ; 687file_path . size = 0 ; 688 689// If no filearray till now expand wildcards 690// The function expand_wildcards() can handle an array of paths 691// and all possible expands are returned in one array. We use this 692// to handle the expansion of '**' into an empty string. 693if ( stackp -> ffs_filearray == NULL ) { 694char * dirptrs [ 2 ]; 695 696// we use filepath to build the path expand_wildcards() should expand. 697dirptrs [ 0 ] = file_path . data ; 698dirptrs [ 1 ] = NULL ; 699 700// if we have a start dir copy it in 701if (! vim_isAbsName ( stackp -> ffs_fix_path . data ) 702&& search_ctx -> ffsc_start_dir . data ) { 703if ( search_ctx -> ffsc_start_dir . size + 1 >= MAXPATHL ) { 704ff_free_stack_element ( stackp ); 705goto fail ; 706} 707bool add_sep = ! after_pathsep ( search_ctx -> ffsc_start_dir . data , 708search_ctx -> ffsc_start_dir . data 709+ search_ctx -> ffsc_start_dir . size ); 710file_path . size = ( size_t ) vim_snprintf ( file_path . data , 711MAXPATHL , 712"%s%s" , 713search_ctx -> ffsc_start_dir . data , 714add_sep ? PATHSEPSTR : "" ); 715if ( file_path . size >= MAXPATHL ) { 716ff_free_stack_element ( stackp ); 717goto fail ; 718} 719} 720 721// append the fix part of the search path 722if ( file_path . size + stackp -> ffs_fix_path . size + 1 >= MAXPATHL ) { 723ff_free_stack_element ( stackp ); 724goto fail ; 725} 726bool add_sep = ! after_pathsep ( stackp -> ffs_fix_path . data , 727stackp -> ffs_fix_path . data + stackp -> ffs_fix_path . size ); 728file_path . size += ( size_t ) vim_snprintf ( file_path . data + file_path . size , 729MAXPATHL - file_path . size , 730"%s%s" , 731stackp -> ffs_fix_path . data , 732add_sep ? PATHSEPSTR : "" ); 733if ( file_path . size >= MAXPATHL ) { 734ff_free_stack_element ( stackp ); 735goto fail ; 736} 737 738rest_of_wildcards = stackp -> ffs_wc_path ; 739if ( * rest_of_wildcards . data != NUL ) { 740if ( strncmp ( rest_of_wildcards . data , "**" , 2 ) == 0 ) { 741// pointer to the restrict byte 742// The restrict byte is not a character! 743char * p = rest_of_wildcards . data + 2 ; 744 745if ( * p > 0 ) { 746( * p ) -- ; 747if ( file_path . size + 1 >= MAXPATHL ) { 748ff_free_stack_element ( stackp ); 749goto fail ; 750} 751file_path . data [ file_path . size ++ ] = '*' ; 752} 753 754if ( * p == 0 ) { 755// remove '**<numb> from wildcards 756memmove ( rest_of_wildcards . data , 757rest_of_wildcards . data + 3 , 758( rest_of_wildcards . size - 3 ) + 1 ); // +1 for NUL 759rest_of_wildcards . size -= 3 ; 760stackp -> ffs_wc_path . size = rest_of_wildcards . size ; 761} else { 762rest_of_wildcards . data += 3 ; 763rest_of_wildcards . size -= 3 ; 764} 765 766if ( stackp -> ffs_star_star_empty == 0 ) { 767// if not done before, expand '**' to empty 768stackp -> ffs_star_star_empty = 1 ; 769dirptrs [ 1 ] = stackp -> ffs_fix_path . data ; 770} 771} 772 773// Here we copy until the next path separator or the end of 774// the path. If we stop at a path separator, there is 775// still something else left. This is handled below by 776// pushing every directory returned from expand_wildcards() 777// on the stack again for further search. 778while ( * rest_of_wildcards . data 779&& ! vim_ispathsep ( * rest_of_wildcards . data )) { 780if ( file_path . size + 1 >= MAXPATHL ) { 781ff_free_stack_element ( stackp ); 782goto fail ; 783} 784file_path . data [ file_path . size ++ ] = * rest_of_wildcards . data ++ ; 785rest_of_wildcards . size -- ; 786} 787 788file_path . data [ file_path . size ] = NUL ; 789if ( vim_ispathsep ( * rest_of_wildcards . data )) { 790rest_of_wildcards . data ++ ; 791rest_of_wildcards . size -- ; 792} 793} 794 795// Expand wildcards like "*" and "$VAR". 796// If the path is a URL don't try this. 797if ( path_with_url ( dirptrs [ 0 ])) { 798stackp -> ffs_filearray = xmalloc (sizeof( char * )); 799stackp -> ffs_filearray [ 0 ] = xmemdupz ( dirptrs [ 0 ], file_path . size ); 800stackp -> ffs_filearray_size = 1 ; 801} else { 802// Add EW_NOTWILD because the expanded path may contain 803// wildcard characters that are to be taken literally. 804// This is a bit of a hack. 805expand_wildcards (( dirptrs [ 1 ] == NULL ) ? 1 : 2 , dirptrs , 806& stackp -> ffs_filearray_size , 807& stackp -> ffs_filearray , 808EW_DIR | EW_ADDSLASH | EW_SILENT | EW_NOTWILD ); 809} 810 811stackp -> ffs_filearray_cur = 0 ; 812stackp -> ffs_stage = 0 ; 813} else { 814rest_of_wildcards . data = & stackp -> ffs_wc_path . data [ stackp -> ffs_wc_path . size ]; 815rest_of_wildcards . size = 0 ; 816} 817 818if ( stackp -> ffs_stage == 0 ) { 819// this is the first time we work on this directory 820if ( * rest_of_wildcards . data == NUL ) { 821// We don't have further wildcards to expand, so we have to 822// check for the final file now. 823for ( int i = stackp -> ffs_filearray_cur ; i < stackp -> ffs_filearray_size ; i ++ ) { 824if (! path_with_url ( stackp -> ffs_filearray [ i ]) 825&& ! os_isdir ( stackp -> ffs_filearray [ i ])) { 826continue; // not a directory 827} 828// prepare the filename to be checked for existence below 829size_t len = strlen ( stackp -> ffs_filearray [ i ]); 830if ( len + 1 + search_ctx -> ffsc_file_to_search . size >= MAXPATHL ) { 831ff_free_stack_element ( stackp ); 832goto fail ; 833} 834bool add_sep = ! after_pathsep ( stackp -> ffs_filearray [ i ], 835stackp -> ffs_filearray [ i ] + len ); 836file_path . size = ( size_t ) vim_snprintf ( file_path . data , 837MAXPATHL , 838"%s%s%s" , 839stackp -> ffs_filearray [ i ], 840add_sep ? PATHSEPSTR : "" , 841search_ctx -> ffsc_file_to_search . data ); 842if ( file_path . size >= MAXPATHL ) { 843ff_free_stack_element ( stackp ); 844goto fail ; 845} 846 847// Try without extra suffix and then with suffixes 848// from 'suffixesadd'. 849len = file_path . size ; 850char * suf = search_ctx -> ffsc_tagfile ? "" : curbuf -> b_p_sua ; 851while (true) { 852// if file exists and we didn't already find it 853if (( path_with_url ( file_path . data ) 854|| ( os_path_exists ( file_path . data ) 855&& ( search_ctx -> ffsc_find_what == FINDFILE_BOTH 856|| (( search_ctx -> ffsc_find_what == FINDFILE_DIR ) 857== os_isdir ( file_path . data ))))) 858#ifndef FF_VERBOSE 859&& ( ff_check_visited ( & search_ctx -> ffsc_visited_list -> ffvl_visited_list , 860file_path . data , file_path . size , "" , 0 ) == OK ) 861#endif 862) { 863#ifdef FF_VERBOSE 864if ( ff_check_visited ( & search_ctx -> ffsc_visited_list -> ffvl_visited_list , 865file_path . data , file_path . size , "" , 0 ) == FAIL ) { 866if ( p_verbose >= 5 ) { 867verbose_enter_scroll (); 868smsg ( 0 , "Already: %s" , file_path . data ); 869msg_puts ( "\n" ); // don't overwrite this either 870verbose_leave_scroll (); 871} 872continue; 873} 874#endif 875 876// push dir to examine rest of subdirs later 877assert ( i < INT_MAX ); 878stackp -> ffs_filearray_cur = i + 1 ; 879ff_push ( search_ctx , stackp ); 880 881if (! path_with_url ( file_path . data )) { 882file_path . size = simplify_filename ( file_path . data ); 883} 884 885if ( os_dirname ( ff_expand_buffer . data , MAXPATHL ) == OK ) { 886ff_expand_buffer . size = strlen ( ff_expand_buffer . data ); 887char * p = path_shorten_fname ( file_path . data , ff_expand_buffer . data ); 888if ( p != NULL ) { 889memmove ( file_path . data , p , 890( size_t )(( file_path . data + file_path . size ) - p ) + 1 ); // +1 for NUL 891file_path . size -= ( size_t )( p - file_path . data ); 892} 893} 894#ifdef FF_VERBOSE 895if ( p_verbose >= 5 ) { 896verbose_enter_scroll (); 897smsg ( 0 , "HIT: %s" , file_path . data ); 898msg_puts ( "\n" ); // don't overwrite this either 899verbose_leave_scroll (); 900} 901#endif 902return file_path . data ; 903} 904 905// Not found or found already, try next suffix. 906if ( * suf == NUL ) { 907break; 908} 909assert ( MAXPATHL >= file_path . size ); 910file_path . size = len + copy_option_part ( & suf , file_path . data + len , 911MAXPATHL - len , "," ); 912} 913} 914} else { 915// still wildcards left, push the directories for further search 916for ( int i = stackp -> ffs_filearray_cur ; i < stackp -> ffs_filearray_size ; i ++ ) { 917if (! os_isdir ( stackp -> ffs_filearray [ i ])) { 918continue; // not a directory 919} 920ff_push ( search_ctx , 921ff_create_stack_element ( stackp -> ffs_filearray [ i ], 922strlen ( stackp -> ffs_filearray [ i ]), 923rest_of_wildcards . data , 924rest_of_wildcards . size , 925stackp -> ffs_level - 1 , 0 )); 926} 927} 928stackp -> ffs_filearray_cur = 0 ; 929stackp -> ffs_stage = 1 ; 930} 931 932// if wildcards contains '**' we have to descent till we reach the 933// leaves of the directory tree. 934if ( strncmp ( stackp -> ffs_wc_path . data , "**" , 2 ) == 0 ) { 935for ( int i = stackp -> ffs_filearray_cur ; 936i < stackp -> ffs_filearray_size ; i ++ ) { 937if ( path_equal ( stackp -> ffs_filearray [ i ], 938stackp -> ffs_fix_path . data , kPathCmpLiteral )) { 939continue; // don't repush same directory 940} 941if (! os_isdir ( stackp -> ffs_filearray [ i ])) { 942continue; // not a directory 943} 944ff_push ( search_ctx , 945ff_create_stack_element ( stackp -> ffs_filearray [ i ], 946strlen ( stackp -> ffs_filearray [ i ]), 947stackp -> ffs_wc_path . data , 948stackp -> ffs_wc_path . size , 949stackp -> ffs_level - 1 , 1 )); 950} 951} 952 953// we are done with the current directory 954ff_free_stack_element ( stackp ); 955} 956 957// If we reached this, we didn't find anything downwards. 958// Let's check if we should do an upward search. 959if ( search_ctx -> ffsc_start_dir . data 960&& search_ctx -> ffsc_stopdirs_v != NULL && ! got_int ) { 961ff_stack_T * sptr ; 962// path_end may point to the NUL or the previous path separator 963ptrdiff_t plen = ( path_end - search_ctx -> ffsc_start_dir . data ) + ( * path_end != NUL ); 964 965// is the last starting directory in the stop list? 966if ( ff_path_in_stoplist ( search_ctx -> ffsc_start_dir . data , 967( size_t ) plen , search_ctx -> ffsc_stopdirs_v )) { 968break; 969} 970 971// cut of last dir 972while ( path_end > search_ctx -> ffsc_start_dir . data && vim_ispathsep ( * path_end )) { 973path_end -- ; 974} 975while ( path_end > search_ctx -> ffsc_start_dir . data && ! vim_ispathsep ( path_end [ -1 ])) { 976path_end -- ; 977} 978* path_end = NUL ; 979 980// we may have shortened search_ctx->ffsc_start_dir, so update it's length 981search_ctx -> ffsc_start_dir . size = ( size_t )( path_end - search_ctx -> ffsc_start_dir . data ); 982path_end -- ; 983 984if ( * search_ctx -> ffsc_start_dir . data == NUL ) { 985break; 986} 987 988if ( search_ctx -> ffsc_start_dir . size + 1 989+ search_ctx -> ffsc_fix_path . size >= MAXPATHL ) { 990goto fail ; 991} 992bool add_sep = ! after_pathsep ( search_ctx -> ffsc_start_dir . data , 993search_ctx -> ffsc_start_dir . data 994+ search_ctx -> ffsc_start_dir . size ); 995file_path . size = ( size_t ) vim_snprintf ( file_path . data , 996MAXPATHL , 997"%s%s%s" , 998search_ctx -> ffsc_start_dir . data , 999add_sep ? PATHSEPSTR : "" , 1000search_ctx -> ffsc_fix_path . data ); 1001if ( file_path . size >= MAXPATHL ) { 1002goto fail ; 1003} 1004 1005// create a new stack entry 1006sptr = ff_create_stack_element ( file_path . data , 1007file_path . size , 1008search_ctx -> ffsc_wc_path . data , 1009search_ctx -> ffsc_wc_path . size , 1010search_ctx -> ffsc_level , 0 ); 1011ff_push ( search_ctx , sptr ); 1012} else { 1013break; 1014} 1015} 1016 1017fail : 1018xfree ( file_path . data ); 1019return NULL ; 1020} 1021 1022/// Free the list of lists of visited files and directories 1023/// Can handle it if the passed search_context is NULL; 1024static void vim_findfile_free_visited ( void * search_ctx_arg ) 1025{ 1026if ( search_ctx_arg == NULL ) { 1027return; 1028} 1029 1030ff_search_ctx_T * search_ctx = ( ff_search_ctx_T * ) search_ctx_arg ; 1031vim_findfile_free_visited_list ( & search_ctx -> ffsc_visited_lists_list ); 1032vim_findfile_free_visited_list ( & search_ctx -> ffsc_dir_visited_lists_list ); 1033} 1034 1035static void vim_findfile_free_visited_list ( ff_visited_list_hdr_T ** list_headp ) 1036{ 1037ff_visited_list_hdr_T * vp ; 1038 1039while ( * list_headp != NULL ) { 1040vp = ( * list_headp ) -> ffvl_next ; 1041ff_free_visited_list (( * list_headp ) -> ffvl_visited_list ); 1042 1043xfree (( * list_headp ) -> ffvl_filename ); 1044xfree ( * list_headp ); 1045* list_headp = vp ; 1046} 1047* list_headp = NULL ; 1048} 1049 1050static void ff_free_visited_list ( ff_visited_T * vl ) 1051{ 1052ff_visited_T * vp ; 1053 1054while ( vl != NULL ) { 1055vp = vl -> ffv_next ; 1056xfree ( vl -> ffv_wc_path ); 1057xfree ( vl ); 1058vl = vp ; 1059} 1060vl = NULL ; 1061} 1062 1063/// @return the already visited list for the given filename. If none is found it 1064/// allocates a new one. 1065static ff_visited_list_hdr_T * ff_get_visited_list ( char * filename , size_t filenamelen , 1066ff_visited_list_hdr_T ** list_headp ) 1067{ 1068ff_visited_list_hdr_T * retptr = NULL ; 1069 1070// check if a visited list for the given filename exists 1071if ( * list_headp != NULL ) { 1072retptr = * list_headp ; 1073while ( retptr != NULL ) { 1074if ( path_equal ( filename , retptr -> ffvl_filename , kPathCmpLiteral )) { 1075#ifdef FF_VERBOSE 1076if ( p_verbose >= 5 ) { 1077verbose_enter_scroll (); 1078smsg ( 0 , "ff_get_visited_list: FOUND list for %s" , filename ); 1079msg_puts ( "\n" ); // don't overwrite this either 1080verbose_leave_scroll (); 1081} 1082#endif 1083return retptr ; 1084} 1085retptr = retptr -> ffvl_next ; 1086} 1087} 1088 1089#ifdef FF_VERBOSE 1090if ( p_verbose >= 5 ) { 1091verbose_enter_scroll (); 1092smsg ( 0 , "ff_get_visited_list: new list for %s" , filename ); 1093msg_puts ( "\n" ); // don't overwrite this either 1094verbose_leave_scroll (); 1095} 1096#endif 1097 1098// if we reach this we didn't find a list and we have to allocate new list 1099retptr = xmalloc (sizeof( * retptr )); 1100 1101retptr -> ffvl_visited_list = NULL ; 1102retptr -> ffvl_filename = xmemdupz ( filename , filenamelen ); 1103retptr -> ffvl_next = * list_headp ; 1104* list_headp = retptr ; 1105 1106return retptr ; 1107} 1108 1109/// Check if two wildcard paths are equal. 1110/// They are equal if: 1111/// - both paths are NULL 1112/// - they have the same length 1113/// - char by char comparison is OK 1114/// - the only differences are in the counters behind a '**', so 1115/// '**\20' is equal to '**\24' 1116static bool ff_wc_equal ( char * s1 , char * s2 ) 1117{ 1118int i , j ; 1119int prev1 = NUL ; 1120int prev2 = NUL ; 1121 1122if ( s1 == s2 ) { 1123return true; 1124} 1125 1126if ( s1 == NULL || s2 == NULL ) { 1127return false; 1128} 1129 1130for ( i = 0 , j = 0 ; s1 [ i ] != NUL && s2 [ j ] != NUL ;) { 1131int c1 = utf_ptr2char ( s1 + i ); 1132int c2 = utf_ptr2char ( s2 + j ); 1133 1134if (( p_fic ? mb_tolower ( c1 ) != mb_tolower ( c2 ) : c1 != c2 ) 1135&& ( prev1 != '*' || prev2 != '*' )) { 1136return false; 1137} 1138prev2 = prev1 ; 1139prev1 = c1 ; 1140 1141i += utfc_ptr2len ( s1 + i ); 1142j += utfc_ptr2len ( s2 + j ); 1143} 1144return s1 [ i ] == s2 [ j ]; 1145} 1146 1147/// maintains the list of already visited files and dirs 1148/// 1149/// @return FAIL if the given file/dir is already in the list or, 1150/// OK if it is newly added 1151static int ff_check_visited ( ff_visited_T ** visited_list , char * fname , size_t fnamelen , 1152char * wc_path , size_t wc_pathlen ) 1153{ 1154ff_visited_T * vp ; 1155bool url = false; 1156 1157FileID file_id ; 1158// For a URL we only compare the name, otherwise we compare the 1159// device/inode. 1160if ( path_with_url ( fname )) { 1161xmemcpyz ( ff_expand_buffer . data , fname , fnamelen ); 1162ff_expand_buffer . size = fnamelen ; 1163url = true; 1164} else { 1165ff_expand_buffer . data [ 0 ] = NUL ; 1166ff_expand_buffer . size = 0 ; 1167if (! os_fileid ( fname , & file_id )) { 1168return FAIL ; 1169} 1170} 1171 1172// check against list of already visited files 1173for ( vp = * visited_list ; vp != NULL ; vp = vp -> ffv_next ) { 1174if (( url && path_equal ( vp -> ffv_fname , ff_expand_buffer . data , kPathCmpLiteral )) 1175|| (! url && vp -> file_id_valid 1176&& os_fileid_equal ( & ( vp -> file_id ), & file_id ))) { 1177// are the wildcard parts equal 1178if ( ff_wc_equal ( vp -> ffv_wc_path , wc_path )) { 1179// already visited 1180return FAIL ; 1181} 1182} 1183} 1184 1185// New file/dir. Add it to the list of visited files/dirs. 1186vp = xmalloc (offsetof( ff_visited_T , ffv_fname ) + ff_expand_buffer . size + 1 ); 1187 1188if (! url ) { 1189vp -> file_id_valid = true; 1190vp -> file_id = file_id ; 1191vp -> ffv_fname [ 0 ] = NUL ; 1192} else { 1193vp -> file_id_valid = false; 1194STRCPY ( vp -> ffv_fname , ff_expand_buffer . data ); 1195} 1196 1197if ( wc_path != NULL ) { 1198vp -> ffv_wc_path = xmemdupz ( wc_path , wc_pathlen ); 1199} else { 1200vp -> ffv_wc_path = NULL ; 1201} 1202 1203vp -> ffv_next = * visited_list ; 1204* visited_list = vp ; 1205 1206return OK ; 1207} 1208 1209/// create stack element from given path pieces 1210static ff_stack_T * ff_create_stack_element ( char * fix_part , size_t fix_partlen , char * wc_part , 1211size_t wc_partlen , int level , int star_star_empty ) 1212{ 1213ff_stack_T * stack = xmalloc (sizeof( ff_stack_T )); 1214 1215stack -> ffs_prev = NULL ; 1216stack -> ffs_filearray = NULL ; 1217stack -> ffs_filearray_size = 0 ; 1218stack -> ffs_filearray_cur = 0 ; 1219stack -> ffs_stage = 0 ; 1220stack -> ffs_level = level ; 1221stack -> ffs_star_star_empty = star_star_empty ; 1222 1223// the following saves NULL pointer checks in vim_findfile 1224if ( fix_part == NULL ) { 1225fix_part = "" ; 1226fix_partlen = 0 ; 1227} 1228stack -> ffs_fix_path = cbuf_to_string ( fix_part , fix_partlen ); 1229 1230if ( wc_part == NULL ) { 1231wc_part = "" ; 1232wc_partlen = 0 ; 1233} 1234stack -> ffs_wc_path = cbuf_to_string ( wc_part , wc_partlen ); 1235 1236return stack ; 1237} 1238 1239/// Push a dir on the directory stack. 1240static void ff_push ( ff_search_ctx_T * search_ctx , ff_stack_T * stack_ptr ) 1241{ 1242// check for NULL pointer, not to return an error to the user, but 1243// to prevent a crash 1244if ( stack_ptr == NULL ) { 1245return; 1246} 1247 1248stack_ptr -> ffs_prev = search_ctx -> ffsc_stack_ptr ; 1249search_ctx -> ffsc_stack_ptr = stack_ptr ; 1250} 1251 1252/// Pop a dir from the directory stack. 1253/// 1254/// @return NULL if stack is empty. 1255static ff_stack_T * ff_pop ( ff_search_ctx_T * search_ctx ) 1256{ 1257ff_stack_T * sptr = search_ctx -> ffsc_stack_ptr ; 1258if ( search_ctx -> ffsc_stack_ptr != NULL ) { 1259search_ctx -> ffsc_stack_ptr = search_ctx -> ffsc_stack_ptr -> ffs_prev ; 1260} 1261 1262return sptr ; 1263} 1264 1265/// free the given stack element 1266static void ff_free_stack_element ( ff_stack_T * const stack_ptr ) 1267{ 1268if ( stack_ptr == NULL ) { 1269return; 1270} 1271 1272// API_CLEAR_STRING handles possible NULL pointers 1273API_CLEAR_STRING ( stack_ptr -> ffs_fix_path ); 1274API_CLEAR_STRING ( stack_ptr -> ffs_wc_path ); 1275 1276if ( stack_ptr -> ffs_filearray != NULL ) { 1277FreeWild ( stack_ptr -> ffs_filearray_size , stack_ptr -> ffs_filearray ); 1278} 1279 1280xfree ( stack_ptr ); 1281} 1282 1283/// Clear the search context, but NOT the visited list. 1284static void ff_clear ( ff_search_ctx_T * search_ctx ) 1285{ 1286ff_stack_T * sptr ; 1287 1288// clear up stack 1289while (( sptr = ff_pop ( search_ctx )) != NULL ) { 1290ff_free_stack_element ( sptr ); 1291} 1292 1293if ( search_ctx -> ffsc_stopdirs_v != NULL ) { 1294int i = 0 ; 1295 1296while ( search_ctx -> ffsc_stopdirs_v [ i ]. data != NULL ) { 1297xfree ( search_ctx -> ffsc_stopdirs_v [ i ]. data ); 1298i ++ ; 1299} 1300XFREE_CLEAR ( search_ctx -> ffsc_stopdirs_v ); 1301} 1302 1303// reset everything 1304API_CLEAR_STRING ( search_ctx -> ffsc_file_to_search ); 1305API_CLEAR_STRING ( search_ctx -> ffsc_start_dir ); 1306API_CLEAR_STRING ( search_ctx -> ffsc_fix_path ); 1307API_CLEAR_STRING ( search_ctx -> ffsc_wc_path ); 1308search_ctx -> ffsc_level = 0 ; 1309} 1310 1311/// check if the given path is in the stopdirs 1312/// 1313/// @return true if yes else false 1314static bool ff_path_in_stoplist ( char * path , size_t path_len , String * stopdirs_v ) 1315{ 1316// eat up trailing path separators, except the first 1317while ( path_len > 1 && vim_ispathsep ( path [ path_len - 1 ])) { 1318path_len -- ; 1319} 1320 1321// if no path consider it as match 1322if ( path_len == 0 ) { 1323return true; 1324} 1325 1326for ( int i = 0 ; stopdirs_v [ i ]. data != NULL ; i ++ ) { 1327// match for parent directory. So '/home' also matches 1328// '/home/rks'. Check for PATHSEP in stopdirs_v[i], else 1329// '/home/r' would also match '/home/rks' 1330if ( path_cmp ( p_fic , stopdirs_v [ i ]. data , path , path_len ) == 0 1331&& ( stopdirs_v [ i ]. size <= path_len 1332|| vim_ispathsep ( stopdirs_v [ i ]. data [ path_len ]))) { 1333return true; 1334} 1335} 1336 1337return false; 1338} 1339 1340/// Find the file name "ptr[len]" in the path. Also finds directory names. 1341/// 1342/// On the first call set the parameter 'first' to true to initialize 1343/// the search. For repeating calls to false. 1344/// 1345/// Repeating calls will return other files called 'ptr[len]' from the path. 1346/// 1347/// Only on the first call 'ptr' and 'len' are used. For repeating calls they 1348/// don't need valid values. 1349/// 1350/// If nothing found on the first call the option FNAME_MESS will issue the 1351/// message: 1352/// 'Can't find file "<file>" in path' 1353/// On repeating calls: 1354/// 'No more file "<file>" found in path' 1355/// 1356/// options: 1357/// FNAME_MESS give error message when not found 1358/// 1359/// Uses NameBuff[]! 1360/// 1361/// @param ptr file name 1362/// @param len length of file name 1363/// @param first use count'th matching file name 1364/// @param rel_fname file name searching relative to 1365/// @param[in,out] file_to_find modified copy of file name 1366/// @param[in,out] search_ctx state of the search 1367/// 1368/// @return an allocated string for the file name. NULL for error. 1369char * find_file_in_path ( char * ptr , size_t len , int options , int first , char * rel_fname , 1370char ** file_to_find , char ** search_ctx ) 1371{ 1372return find_file_in_path_option ( ptr , len , options , first , 1373( * curbuf -> b_p_path == NUL 1374? p_path 1375: curbuf -> b_p_path ), 1376FINDFILE_BOTH , rel_fname , curbuf -> b_p_sua , 1377file_to_find , search_ctx ); 1378} 1379 1380#ifdef EXITFREE 1381void free_findfile ( void ) 1382{ 1383API_CLEAR_STRING ( ff_expand_buffer ); 1384} 1385#endif 1386 1387/// Find the directory name "ptr[len]" in the path. 1388/// 1389/// options: 1390/// FNAME_MESS give error message when not found 1391/// FNAME_UNESC unescape backslashes 1392/// 1393/// Uses NameBuff[]! 1394/// 1395/// @param ptr file name 1396/// @param len length of file name 1397/// @param rel_fname file name searching relative to 1398/// @param[in,out] file_to_find modified copy of file name 1399/// @param[in,out] search_ctx state of the search 1400/// 1401/// @return an allocated string for the file name. NULL for error. 1402char * find_directory_in_path ( char * ptr , size_t len , int options , char * rel_fname , 1403char ** file_to_find , char ** search_ctx ) 1404{ 1405return find_file_in_path_option ( ptr , len , options , true, p_cdpath , 1406FINDFILE_DIR , rel_fname , "" , 1407file_to_find , search_ctx ); 1408} 1409 1410/// @param ptr file name 1411/// @param len length of file name 1412/// @param first use count'th matching file name 1413/// @param path_option p_path or p_cdpath 1414/// @param find_what FINDFILE_FILE, _DIR or _BOTH 1415/// @param rel_fname file name we are looking relative to. 1416/// @param suffixes list of suffixes, 'suffixesadd' option 1417/// @param[in,out] file_to_find modified copy of file name 1418/// @param[in,out] search_ctx_arg state of the search 1419char * find_file_in_path_option ( char * ptr , size_t len , int options , int first , char * path_option , 1420int find_what , char * rel_fname , char * suffixes , char ** file_to_find , 1421char ** search_ctx_arg ) 1422{ 1423ff_search_ctx_T ** search_ctx = ( ff_search_ctx_T ** ) search_ctx_arg ; 1424static char * dir ; 1425static bool did_findfile_init = false; 1426char * file_name = NULL ; 1427static size_t file_to_findlen = 0 ; 1428 1429if ( rel_fname != NULL && path_with_url ( rel_fname )) { 1430// Do not attempt to search "relative" to a URL. #6009 1431rel_fname = NULL ; 1432} 1433 1434if ( first == true) { 1435if ( len == 0 ) { 1436return NULL ; 1437} 1438 1439// copy file name into NameBuff, expanding environment variables 1440char save_char = ptr [ len ]; 1441ptr [ len ] = NUL ; 1442file_to_findlen = expand_env_esc ( ptr , NameBuff , MAXPATHL , NULL , true, NULL ); 1443ptr [ len ] = save_char ; 1444 1445xfree ( * file_to_find ); 1446* file_to_find = xmemdupz ( NameBuff , file_to_findlen ); 1447if ( options & FNAME_UNESC ) { 1448// Change all "\ " to " ". 1449for ( ptr = * file_to_find ; * ptr != NUL ; ptr ++ ) { 1450if ( ptr [ 0 ] == '\\' && ptr [ 1 ] == ' ' ) { 1451memmove ( ptr , ptr + 1 , 1452( size_t )(( * file_to_find + file_to_findlen ) - ( ptr + 1 )) + 1 ); 1453file_to_findlen -- ; 1454} 1455} 1456} 1457} 1458 1459bool rel_to_curdir = (( * file_to_find )[ 0 ] == '.' 1460&& (( * file_to_find )[ 1 ] == NUL 1461|| vim_ispathsep (( * file_to_find )[ 1 ]) 1462|| (( * file_to_find )[ 1 ] == '.' 1463&& (( * file_to_find )[ 2 ] == NUL 1464|| vim_ispathsep (( * file_to_find )[ 2 ]))))); 1465if ( vim_isAbsName ( * file_to_find ) 1466// "..", "../path", "." and "./path": don't use the path_option 1467|| rel_to_curdir 1468#ifdef MSWIN 1469// handle "\tmp" as absolute path 1470|| vim_ispathsep (( * file_to_find )[ 0 ]) 1471// handle "c:name" as absolute path 1472|| (( * file_to_find )[ 0 ] != NUL && ( * file_to_find )[ 1 ] == ':' ) 1473#endif 1474) { 1475// Absolute path, no need to use "path_option". 1476// If this is not a first call, return NULL. We already returned a 1477// filename on the first call. 1478if ( first == true) { 1479if ( path_with_url ( * file_to_find )) { 1480file_name = xmemdupz ( * file_to_find , file_to_findlen ); 1481goto theend ; 1482} 1483 1484size_t rel_fnamelen = rel_fname != NULL ? strlen ( rel_fname ) : 0 ; 1485 1486// When FNAME_REL flag given first use the directory of the file. 1487// Otherwise or when this fails use the current directory. 1488for ( int run = 1 ; run <= 2 ; run ++ ) { 1489size_t l = file_to_findlen ; 1490if ( run == 1 1491&& rel_to_curdir 1492&& ( options & FNAME_REL ) 1493&& rel_fname != NULL 1494&& rel_fnamelen + l < MAXPATHL ) { 1495l = ( size_t ) vim_snprintf ( NameBuff , 1496MAXPATHL , 1497"%.*s%s" , 1498( int )( path_tail ( rel_fname ) - rel_fname ), 1499rel_fname , 1500* file_to_find ); 1501assert ( l < MAXPATHL ); 1502} else { 1503STRCPY ( NameBuff , * file_to_find ); 1504run = 2 ; 1505} 1506 1507// When the file doesn't exist, try adding parts of 'suffixesadd'. 1508char * suffix = suffixes ; 1509while (true) { 1510if (( os_path_exists ( NameBuff ) 1511&& ( find_what == FINDFILE_BOTH 1512|| (( find_what == FINDFILE_DIR ) == os_isdir ( NameBuff ))))) { 1513file_name = xmemdupz ( NameBuff , simplify_filename ( NameBuff )); 1514goto theend ; 1515} 1516if ( * suffix == NUL ) { 1517break; 1518} 1519assert ( MAXPATHL >= l ); 1520copy_option_part ( & suffix , NameBuff + l , MAXPATHL - l , "," ); 1521} 1522} 1523} 1524} else { 1525// Loop over all paths in the 'path' or 'cdpath' option. 1526// When "first" is set, first setup to the start of the option. 1527// Otherwise continue to find the next match. 1528if ( first == true) { 1529// vim_findfile_free_visited can handle a possible NULL pointer 1530vim_findfile_free_visited ( * search_ctx ); 1531dir = path_option ; 1532did_findfile_init = false; 1533} 1534 1535while (true) { 1536if ( did_findfile_init ) { 1537file_name = vim_findfile ( * search_ctx ); 1538if ( file_name != NULL ) { 1539break; 1540} 1541 1542did_findfile_init = false; 1543} else { 1544char * r_ptr ; 1545 1546if ( dir == NULL || * dir == NUL ) { 1547// We searched all paths of the option, now we can free the search context. 1548vim_findfile_cleanup ( * search_ctx ); 1549* search_ctx = NULL ; 1550break; 1551} 1552 1553char * buf = xmalloc ( MAXPATHL ); 1554 1555// copy next path 1556buf [ 0 ] = NUL ; 1557copy_option_part ( & dir , buf , MAXPATHL , " ," ); 1558 1559// get the stopdir string 1560r_ptr = vim_findfile_stopdir ( buf ); 1561* search_ctx = vim_findfile_init ( buf , * file_to_find , file_to_findlen , 1562r_ptr , 100 , false, find_what , 1563* search_ctx , false, rel_fname ); 1564if ( * search_ctx != NULL ) { 1565did_findfile_init = true; 1566} 1567xfree ( buf ); 1568} 1569} 1570} 1571if ( file_name == NULL && ( options & FNAME_MESS )) { 1572if ( first == true) { 1573if ( find_what == FINDFILE_DIR ) { 1574semsg ( _ ( e_cant_find_directory_str_in_cdpath ), * file_to_find ); 1575} else { 1576semsg ( _ ( e_cant_find_file_str_in_path ), * file_to_find ); 1577} 1578} else { 1579if ( find_what == FINDFILE_DIR ) { 1580semsg ( _ ( e_no_more_directory_str_found_in_cdpath ), * file_to_find ); 1581} else { 1582semsg ( _ ( e_no_more_file_str_found_in_path ), * file_to_find ); 1583} 1584} 1585} 1586 1587theend : 1588return file_name ; 1589} 1590 1591/// Get the file name at the cursor. 1592/// If Visual mode is active, use the selected text if it's in one line. 1593/// Returns the name in allocated memory, NULL for failure. 1594char * grab_file_name ( int count , linenr_T * file_lnum ) 1595{ 1596int options = FNAME_MESS | FNAME_EXP | FNAME_REL | FNAME_UNESC ; 1597char * fname ; 1598if ( Visual . active ) { 1599size_t len ; 1600char * ptr ; 1601if ( get_visual_text ( NULL , & ptr , & len ) == FAIL ) { 1602return NULL ; 1603} 1604// Only recognize ":123" here 1605if ( file_lnum != NULL && ptr [ len ] == ':' && isdigit (( uint8_t ) ptr [ len + 1 ])) { 1606char * p = ptr + len + 1 ; 1607 1608* file_lnum = getdigits_int32 ( & p , false, 0 ); 1609} 1610fname = find_file_name_in_path ( ptr , len , options , count , curbuf -> b_ffname ); 1611} else { 1612fname = file_name_at_cursor ( options | FNAME_HYP , count , file_lnum ); 1613} 1614TO_SLASH ( fname ); 1615return fname ; 1616} 1617 1618/// Return the file name under or after the cursor. 1619/// 1620/// The 'path' option is searched if the file name is not absolute. 1621/// The string returned has been alloc'ed and should be freed by the caller. 1622/// NULL is returned if the file name or file is not found. 1623/// 1624/// options: 1625/// FNAME_MESS give error messages 1626/// FNAME_EXP expand to path 1627/// FNAME_HYP check for hypertext link 1628/// FNAME_INCL apply "includeexpr" 1629char * file_name_at_cursor ( int options , int count , linenr_T * file_lnum ) 1630{ 1631return file_name_in_line ( get_cursor_line_ptr (), 1632curwin -> w_cursor . col , options , count , curbuf -> b_ffname , 1633file_lnum ); 1634} 1635 1636/// @param rel_fname file we are searching relative to 1637/// @param file_lnum line number after the file name 1638/// 1639/// @return the name of the file under or after ptr[col]. 1640/// 1641/// Otherwise like file_name_at_cursor(). 1642char * file_name_in_line ( char * line , int col , int options , int count , char * rel_fname , 1643linenr_T * file_lnum ) 1644{ 1645// search forward for what could be the start of a file name 1646char * ptr = line + col ; 1647while ( * ptr != NUL && ! vim_isfilec (( uint8_t )( * ptr ))) { 1648MB_PTR_ADV ( ptr ); 1649} 1650if ( * ptr == NUL ) { // nothing found 1651if ( options & FNAME_MESS ) { 1652emsg ( _ ( "E446: No file name under cursor" )); 1653} 1654return NULL ; 1655} 1656 1657size_t len ; 1658bool in_type = true; 1659bool is_url = false; 1660 1661// Search backward for first char of the file name. 1662// Go one char back to ":" before "//", or to the drive letter before ":\" (even if ":" 1663// is not in 'isfname'). 1664while ( ptr > line ) { 1665if (( len = ( size_t )( utf_head_off ( line , ptr - 1 ))) > 0 ) { 1666ptr -= len + 1 ; 1667} else if ( vim_isfilec (( uint8_t ) ptr [ -1 ]) || (( options & FNAME_HYP ) && path_is_url ( ptr - 1 ))) { 1668ptr -- ; 1669} else { 1670break; 1671} 1672} 1673 1674// Search forward for the last char of the file name. 1675// Also allow ":/" when ':' is not in 'isfname'. 1676// TODO(justinmk): Check for driveletter "x:/" at start, regardless of 'isfname'. 1677len = path_has_drive_letter ( ptr , strlen ( ptr )) ? 2 : 0 ; 1678while ( vim_isfilec (( uint8_t ) ptr [ len ]) || ( ptr [ len ] == '\\' && ptr [ len + 1 ] == ' ' ) 1679|| (( options & FNAME_HYP ) && path_is_url ( ptr + len )) 1680|| ( is_url && vim_strchr ( ":?&=" , ( uint8_t ) ptr [ len ]) != NULL )) { 1681// After type:// we also include :, ?, & and = as valid characters, so that 1682// http://google.com:8080?q=this&that=ok works. 1683if (( ptr [ len ] >= 'A' && ptr [ len ] <= 'Z' ) || ( ptr [ len ] >= 'a' && ptr [ len ] <= 'z' )) { 1684if ( in_type && path_is_url ( ptr + len + 1 )) { 1685is_url = true; 1686} 1687} else { 1688in_type = false; 1689} 1690 1691if ( ptr [ len ] == '\\' && ptr [ len + 1 ] == ' ' ) { 1692// Skip over the "\" in "\ ". 1693len ++ ; 1694} 1695len += ( size_t )( utfc_ptr2len ( ptr + len )); 1696} 1697 1698// If there is trailing punctuation, remove it. 1699// But don't remove "..", could be a directory name. 1700if ( len > 2 && vim_strchr ( ".,:;!" , ( uint8_t ) ptr [ len - 1 ]) != NULL 1701&& ptr [ len - 2 ] != '.' ) { 1702len -- ; 1703} 1704 1705if ( file_lnum != NULL ) { 1706const char * match_text = " line " ; // english 1707size_t match_textlen = 6 ; 1708 1709// Get the number after the file name and a separator character. 1710// Also accept " line 999" with and without the same translation as 1711// used in last_set_msg(). 1712char * p = ptr + len ; 1713if ( strncmp ( p , match_text , match_textlen ) == 0 ) { 1714p += match_textlen ; 1715} else { 1716// no match with english, try localized 1717match_text = _ ( line_msg ); 1718match_textlen = strlen ( match_text ); 1719if ( strncmp ( p , match_text , match_textlen ) == 0 ) { 1720p += match_textlen ; 1721} else { 1722p = skipwhite ( p ); 1723} 1724} 1725if ( * p != NUL ) { 1726if (! isdigit (( uint8_t )( * p ))) { 1727p ++ ; // skip the separator 1728} 1729p = skipwhite ( p ); 1730if ( isdigit (( uint8_t )( * p ))) { 1731* file_lnum = ( linenr_T ) getdigits_long ( & p , false, 0 ); 1732} 1733} 1734} 1735 1736return find_file_name_in_path ( ptr , len , options , count , rel_fname ); 1737} 1738 1739static char * eval_includeexpr (const char * const ptr , const size_t len ) 1740{ 1741const sctx_T save_sctx = current_sctx ; 1742set_vim_var_string ( VV_FNAME , ptr , ( ptrdiff_t ) len ); 1743current_sctx = curbuf -> b_p_script_ctx [ kBufOptIncludeexpr ]; 1744 1745const bool use_sandbox = was_set_insecurely ( curwin , kOptIncludeexpr , OPT_LOCAL ); 1746typval_T tv ; 1747char * res = NULL ; 1748if ( eval_expr_option_tv ( & curbuf -> b_p_inex , use_sandbox , & tv )) { 1749res = xstrdup ( tv_get_string ( & tv )); 1750tv_clear ( & tv ); 1751} 1752 1753set_vim_var_string ( VV_FNAME , NULL , 0 ); 1754current_sctx = save_sctx ; 1755return res ; 1756} 1757 1758/// Return the name of the file ptr[len] in 'path'. 1759/// Otherwise like file_name_at_cursor(). 1760/// 1761/// @param rel_fname file we are searching relative to 1762char * find_file_name_in_path ( char * ptr , size_t len , int options , long count , char * rel_fname ) 1763{ 1764char * file_name ; 1765char * tofree = NULL ; 1766 1767if ( len == 0 ) { 1768return NULL ; 1769} 1770 1771if (( options & FNAME_HYP ) && len > 6 && strncmp ( ptr , "file:/" , 17726 ) == 0 && ! vim_ispathsep ( ptr [ 6 ])) { 1773size_t off = path_has_drive_letter ( ptr + 6 , len - 6 ) ? 6 : 5 ; 1774ptr += off ; 1775len -= off ; 1776} 1777 1778if (( options & FNAME_INCL ) && curbuf -> b_p_inex . type != kCallbackNone ) { 1779tofree = eval_includeexpr ( ptr , len ); 1780if ( tofree != NULL ) { 1781ptr = tofree ; 1782len = strlen ( ptr ); 1783} 1784} 1785 1786if ( options & FNAME_EXP ) { 1787char * file_to_find = NULL ; 1788char * search_ctx = NULL ; 1789 1790file_name = find_file_in_path ( ptr , len , options & ~ FNAME_MESS , 1791true, rel_fname , & file_to_find , & search_ctx ); 1792 1793// If the file could not be found in a normal way, try applying 1794// 'includeexpr' (unless done already). 1795if ( file_name == NULL 1796&& !( options & FNAME_INCL ) && curbuf -> b_p_inex . type != kCallbackNone ) { 1797tofree = eval_includeexpr ( ptr , len ); 1798if ( tofree != NULL ) { 1799ptr = tofree ; 1800len = strlen ( ptr ); 1801file_name = find_file_in_path ( ptr , len , options & ~ FNAME_MESS , 1802true, rel_fname , & file_to_find , & search_ctx ); 1803} 1804} 1805if ( file_name == NULL && ( options & FNAME_MESS )) { 1806char c = ptr [ len ]; 1807ptr [ len ] = NUL ; 1808semsg ( _ ( "E447: Can't find file \"%s\" in path" ), ptr ); 1809ptr [ len ] = c ; 1810} 1811 1812// Repeat finding the file "count" times. This matters when it 1813// appears several times in the path. 1814while ( file_name != NULL && -- count > 0 ) { 1815xfree ( file_name ); 1816file_name = find_file_in_path ( ptr , len , options , false, rel_fname , 1817& file_to_find , & search_ctx ); 1818} 1819 1820xfree ( file_to_find ); 1821vim_findfile_cleanup ( search_ctx ); 1822} else { 1823file_name = xstrnsave ( ptr , len ); 1824} 1825 1826xfree ( tofree ); 1827 1828return file_name ; 1829} 1830 1831void do_autocmd_dirchanged ( char * new_dir , CdScope scope , CdCause cause , bool pre ) 1832{ 1833static bool recursive = false; 1834 1835event_T event = pre ? EVENT_DIRCHANGEDPRE : EVENT_DIRCHANGED ; 1836 1837if ( recursive || ! has_event ( event )) { 1838// No autocommand was defined or we changed 1839// the directory from this autocommand. 1840return; 1841} 1842 1843recursive = true; 1844 1845save_v_event_T save_v_event ; 1846dict_T * dict = get_v_event ( & save_v_event ); 1847char buf [ 8 ]; 1848 1849switch ( scope ) { 1850case kCdScopeGlobal : 1851snprintf ( buf , sizeof( buf ), "global" ); 1852break; 1853case kCdScopeTabpage : 1854snprintf ( buf , sizeof( buf ), "tabpage" ); 1855break; 1856case kCdScopeBuffer : 1857snprintf ( buf , sizeof( buf ), "buffer" ); 1858break; 1859case kCdScopeWindow : 1860snprintf ( buf , sizeof( buf ), "window" ); 1861break; 1862case kCdScopeInvalid : 1863// Should never happen. 1864abort (); 1865} 1866 1867if ( pre ) { 1868tv_dict_add_str ( dict , S_LEN ( "directory" ), new_dir ); 1869} else { 1870tv_dict_add_str ( dict , S_LEN ( "cwd" ), new_dir ); 1871} 1872tv_dict_add_str ( dict , S_LEN ( "scope" ), buf ); 1873tv_dict_add_bool ( dict , S_LEN ( "changed_window" ), cause == kCdCauseWindow ); 1874tv_dict_set_keys_readonly ( dict ); 1875 1876switch ( cause ) { 1877case kCdCauseManual : 1878case kCdCauseWindow : 1879case kCdCauseBuffer : 1880break; 1881case kCdCauseAuto : 1882snprintf ( buf , sizeof( buf ), "auto" ); 1883break; 1884case kCdCauseOther : 1885// Should never happen. 1886abort (); 1887} 1888 1889apply_autocmds ( event , buf , new_dir , false, curbuf ); 1890 1891restore_v_event ( dict , & save_v_event ); 1892 1893recursive = false; 1894} 1895 1896/// Change to a file's directory. 1897/// Caller must call shorten_fnames()! 1898/// 1899/// @return OK or FAIL 1900int vim_chdirfile ( char * fname , CdCause cause ) 1901{ 1902char dir [ MAXPATHL ]; 1903 1904xstrlcpy ( dir , fname , MAXPATHL ); 1905* path_tail_with_sep ( dir ) = NUL ; 1906 1907if ( os_dirname ( NameBuff , sizeof( NameBuff )) != OK ) { 1908NameBuff [ 0 ] = NUL ; 1909} 1910 1911if ( path_equal ( dir , NameBuff , kPathCmpLiteral )) { 1912// nothing to do 1913return OK ; 1914} 1915 1916if ( cause != kCdCauseOther ) { 1917do_autocmd_dirchanged ( dir , kCdScopeWindow , cause , true); 1918} 1919 1920if ( os_chdir ( dir ) != 0 ) { 1921return FAIL ; 1922} 1923 1924if ( cause != kCdCauseOther ) { 1925do_autocmd_dirchanged ( dir , kCdScopeWindow , cause , false); 1926} 1927 1928return OK ; 1929} 1930 1931/// Change directory to "new_dir". Search 'cdpath' for relative directory names. 1932int vim_chdir ( char * new_dir ) 1933{ 1934char * file_to_find = NULL ; 1935char * search_ctx = NULL ; 1936char * dir_name = find_directory_in_path ( new_dir , strlen ( new_dir ), FNAME_MESS , 1937curbuf -> b_ffname , & file_to_find , & search_ctx ); 1938xfree ( file_to_find ); 1939vim_findfile_cleanup ( search_ctx ); 1940if ( dir_name == NULL ) { 1941return -1 ; 1942} 1943 1944int r = os_chdir ( dir_name ); 1945xfree ( dir_name ); 1946return r ; 1947}