Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * findfile.c: Search for files in directories listed in 'path' |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
| 16 | /* |
| 17 | * File searching functions for 'path', 'tags' and 'cdpath' options. |
| 18 | * External visible functions: |
| 19 | * vim_findfile_init() creates/initialises the search context |
| 20 | * vim_findfile_free_visited() free list of visited files/dirs of search |
| 21 | * context |
| 22 | * vim_findfile() find a file in the search context |
| 23 | * vim_findfile_cleanup() cleanup/free search context created by |
| 24 | * vim_findfile_init() |
| 25 | * |
| 26 | * All static functions and variables start with 'ff_' |
| 27 | * |
| 28 | * In general it works like this: |
| 29 | * First you create yourself a search context by calling vim_findfile_init(). |
| 30 | * It is possible to give a search context from a previous call to |
| 31 | * vim_findfile_init(), so it can be reused. After this you call vim_findfile() |
| 32 | * until you are satisfied with the result or it returns NULL. On every call it |
| 33 | * returns the next file which matches the conditions given to |
| 34 | * vim_findfile_init(). If it doesn't find a next file it returns NULL. |
| 35 | * |
| 36 | * It is possible to call vim_findfile_init() again to reinitialise your search |
| 37 | * with some new parameters. Don't forget to pass your old search context to |
| 38 | * it, so it can reuse it and especially reuse the list of already visited |
| 39 | * directories. If you want to delete the list of already visited directories |
| 40 | * simply call vim_findfile_free_visited(). |
| 41 | * |
| 42 | * When you are done call vim_findfile_cleanup() to free the search context. |
| 43 | * |
| 44 | * The function vim_findfile_init() has a long comment, which describes the |
| 45 | * needed parameters. |
| 46 | * |
| 47 | * |
| 48 | * |
| 49 | * ATTENTION: |
| 50 | * ========== |
Christian Brabandt | ee17b6f | 2023-09-09 11:23:50 +0200 | [diff] [blame] | 51 | * Also we use an allocated search context here, these functions are NOT |
| 52 | * thread-safe! |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 53 | * |
| 54 | * To minimize parameter passing (or because I'm to lazy), only the |
| 55 | * external visible functions get a search context as a parameter. This is |
| 56 | * then assigned to a static global, which is used throughout the local |
| 57 | * functions. |
| 58 | */ |
| 59 | |
| 60 | /* |
| 61 | * type for the directory search stack |
| 62 | */ |
| 63 | typedef struct ff_stack |
| 64 | { |
| 65 | struct ff_stack *ffs_prev; |
| 66 | |
| 67 | // the fix part (no wildcards) and the part containing the wildcards |
| 68 | // of the search path |
| 69 | char_u *ffs_fix_path; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 70 | char_u *ffs_wc_path; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 71 | |
| 72 | // files/dirs found in the above directory, matched by the first wildcard |
| 73 | // of wc_part |
| 74 | char_u **ffs_filearray; |
| 75 | int ffs_filearray_size; |
John Drouhard | 95fca12 | 2022-08-01 11:38:17 +0100 | [diff] [blame] | 76 | int ffs_filearray_cur; // needed for partly handled dirs |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 77 | |
| 78 | // to store status of partly handled directories |
| 79 | // 0: we work on this directory for the first time |
| 80 | // 1: this directory was partly searched in an earlier step |
| 81 | int ffs_stage; |
| 82 | |
| 83 | // How deep are we in the directory tree? |
| 84 | // Counts backward from value of level parameter to vim_findfile_init |
| 85 | int ffs_level; |
| 86 | |
| 87 | // Did we already expand '**' to an empty string? |
| 88 | int ffs_star_star_empty; |
| 89 | } ff_stack_T; |
| 90 | |
| 91 | /* |
| 92 | * type for already visited directories or files. |
| 93 | */ |
| 94 | typedef struct ff_visited |
| 95 | { |
| 96 | struct ff_visited *ffv_next; |
| 97 | |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 98 | // Visited directories are different if the wildcard string are |
| 99 | // different. So we have to save it. |
| 100 | char_u *ffv_wc_path; |
Bram Moolenaar | 2bd9dbc | 2022-08-25 18:12:06 +0100 | [diff] [blame] | 101 | |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 102 | // for unix use inode etc for comparison (needed because of links), else |
| 103 | // use filename. |
| 104 | #ifdef UNIX |
| 105 | int ffv_dev_valid; // ffv_dev and ffv_ino were set |
| 106 | dev_t ffv_dev; // device number |
| 107 | ino_t ffv_ino; // inode number |
| 108 | #endif |
| 109 | // The memory for this struct is allocated according to the length of |
| 110 | // ffv_fname. |
| 111 | char_u ffv_fname[1]; // actually longer |
| 112 | } ff_visited_T; |
| 113 | |
| 114 | /* |
| 115 | * We might have to manage several visited lists during a search. |
| 116 | * This is especially needed for the tags option. If tags is set to: |
| 117 | * "./++/tags,./++/TAGS,++/tags" (replace + with *) |
| 118 | * So we have to do 3 searches: |
| 119 | * 1) search from the current files directory downward for the file "tags" |
| 120 | * 2) search from the current files directory downward for the file "TAGS" |
| 121 | * 3) search from Vims current directory downwards for the file "tags" |
| 122 | * As you can see, the first and the third search are for the same file, so for |
| 123 | * the third search we can use the visited list of the first search. For the |
| 124 | * second search we must start from a empty visited list. |
| 125 | * The struct ff_visited_list_hdr is used to manage a linked list of already |
| 126 | * visited lists. |
| 127 | */ |
| 128 | typedef struct ff_visited_list_hdr |
| 129 | { |
| 130 | struct ff_visited_list_hdr *ffvl_next; |
| 131 | |
| 132 | // the filename the attached visited list is for |
| 133 | char_u *ffvl_filename; |
| 134 | |
| 135 | ff_visited_T *ffvl_visited_list; |
| 136 | |
| 137 | } ff_visited_list_hdr_T; |
| 138 | |
| 139 | |
| 140 | /* |
| 141 | * '**' can be expanded to several directory levels. |
| 142 | * Set the default maximum depth. |
| 143 | */ |
| 144 | #define FF_MAX_STAR_STAR_EXPAND ((char_u)30) |
| 145 | |
| 146 | /* |
| 147 | * The search context: |
| 148 | * ffsc_stack_ptr: the stack for the dirs to search |
| 149 | * ffsc_visited_list: the currently active visited list |
| 150 | * ffsc_dir_visited_list: the currently active visited list for search dirs |
| 151 | * ffsc_visited_lists_list: the list of all visited lists |
| 152 | * ffsc_dir_visited_lists_list: the list of all visited lists for search dirs |
| 153 | * ffsc_file_to_search: the file to search for |
| 154 | * ffsc_start_dir: the starting directory, if search path was relative |
| 155 | * ffsc_fix_path: the fix part of the given path (without wildcards) |
| 156 | * Needed for upward search. |
| 157 | * ffsc_wc_path: the part of the given path containing wildcards |
| 158 | * ffsc_level: how many levels of dirs to search downwards |
| 159 | * ffsc_stopdirs_v: array of stop directories for upward search |
| 160 | * ffsc_find_what: FINDFILE_BOTH, FINDFILE_DIR or FINDFILE_FILE |
| 161 | * ffsc_tagfile: searching for tags file, don't use 'suffixesadd' |
| 162 | */ |
| 163 | typedef struct ff_search_ctx_T |
| 164 | { |
| 165 | ff_stack_T *ffsc_stack_ptr; |
| 166 | ff_visited_list_hdr_T *ffsc_visited_list; |
| 167 | ff_visited_list_hdr_T *ffsc_dir_visited_list; |
| 168 | ff_visited_list_hdr_T *ffsc_visited_lists_list; |
| 169 | ff_visited_list_hdr_T *ffsc_dir_visited_lists_list; |
| 170 | char_u *ffsc_file_to_search; |
| 171 | char_u *ffsc_start_dir; |
| 172 | char_u *ffsc_fix_path; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 173 | char_u *ffsc_wc_path; |
| 174 | int ffsc_level; |
| 175 | char_u **ffsc_stopdirs_v; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 176 | int ffsc_find_what; |
| 177 | int ffsc_tagfile; |
| 178 | } ff_search_ctx_T; |
| 179 | |
| 180 | // locally needed functions |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 181 | static int ff_check_visited(ff_visited_T **, char_u *, char_u *); |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 182 | static void vim_findfile_free_visited(void *search_ctx_arg); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 183 | static void vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp); |
| 184 | static void ff_free_visited_list(ff_visited_T *vl); |
| 185 | static ff_visited_list_hdr_T* ff_get_visited_list(char_u *, ff_visited_list_hdr_T **list_headp); |
| 186 | |
| 187 | static void ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr); |
| 188 | static ff_stack_T *ff_pop(ff_search_ctx_T *search_ctx); |
| 189 | static void ff_clear(ff_search_ctx_T *search_ctx); |
| 190 | static void ff_free_stack_element(ff_stack_T *stack_ptr); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 191 | static ff_stack_T *ff_create_stack_element(char_u *, char_u *, int, int); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 192 | static int ff_path_in_stoplist(char_u *, int, char_u **); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 193 | |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 194 | static char_u *ff_expand_buffer = NULL; // used for expanding filenames |
| 195 | |
| 196 | #if 0 |
| 197 | /* |
| 198 | * if someone likes findfirst/findnext, here are the functions |
| 199 | * NOT TESTED!! |
| 200 | */ |
| 201 | |
| 202 | static void *ff_fn_search_context = NULL; |
| 203 | |
| 204 | char_u * |
| 205 | vim_findfirst(char_u *path, char_u *filename, int level) |
| 206 | { |
| 207 | ff_fn_search_context = |
| 208 | vim_findfile_init(path, filename, NULL, level, TRUE, FALSE, |
| 209 | ff_fn_search_context, rel_fname); |
| 210 | if (NULL == ff_fn_search_context) |
| 211 | return NULL; |
| 212 | else |
| 213 | return vim_findnext() |
| 214 | } |
| 215 | |
| 216 | char_u * |
| 217 | vim_findnext(void) |
| 218 | { |
| 219 | char_u *ret = vim_findfile(ff_fn_search_context); |
| 220 | |
| 221 | if (NULL == ret) |
| 222 | { |
| 223 | vim_findfile_cleanup(ff_fn_search_context); |
| 224 | ff_fn_search_context = NULL; |
| 225 | } |
| 226 | return ret; |
| 227 | } |
| 228 | #endif |
| 229 | |
| 230 | /* |
| 231 | * Initialization routine for vim_findfile(). |
| 232 | * |
| 233 | * Returns the newly allocated search context or NULL if an error occurred. |
| 234 | * |
| 235 | * Don't forget to clean up by calling vim_findfile_cleanup() if you are done |
| 236 | * with the search context. |
| 237 | * |
| 238 | * Find the file 'filename' in the directory 'path'. |
| 239 | * The parameter 'path' may contain wildcards. If so only search 'level' |
| 240 | * directories deep. The parameter 'level' is the absolute maximum and is |
| 241 | * not related to restricts given to the '**' wildcard. If 'level' is 100 |
| 242 | * and you use '**200' vim_findfile() will stop after 100 levels. |
| 243 | * |
| 244 | * 'filename' cannot contain wildcards! It is used as-is, no backslashes to |
| 245 | * escape special characters. |
| 246 | * |
| 247 | * If 'stopdirs' is not NULL and nothing is found downward, the search is |
| 248 | * restarted on the next higher directory level. This is repeated until the |
| 249 | * start-directory of a search is contained in 'stopdirs'. 'stopdirs' has the |
| 250 | * format ";*<dirname>*\(;<dirname>\)*;\=$". |
| 251 | * |
| 252 | * If the 'path' is relative, the starting dir for the search is either VIM's |
| 253 | * current dir or if the path starts with "./" the current files dir. |
| 254 | * If the 'path' is absolute, the starting dir is that part of the path before |
| 255 | * the first wildcard. |
| 256 | * |
| 257 | * Upward search is only done on the starting dir. |
| 258 | * |
| 259 | * If 'free_visited' is TRUE the list of already visited files/directories is |
| 260 | * cleared. Set this to FALSE if you just want to search from another |
| 261 | * directory, but want to be sure that no directory from a previous search is |
| 262 | * searched again. This is useful if you search for a file at different places. |
| 263 | * The list of visited files/dirs can also be cleared with the function |
| 264 | * vim_findfile_free_visited(). |
| 265 | * |
| 266 | * Set the parameter 'find_what' to FINDFILE_DIR if you want to search for |
| 267 | * directories only, FINDFILE_FILE for files only, FINDFILE_BOTH for both. |
| 268 | * |
| 269 | * A search context returned by a previous call to vim_findfile_init() can be |
| 270 | * passed in the parameter "search_ctx_arg". This context is reused and |
| 271 | * reinitialized with the new parameters. The list of already visited |
| 272 | * directories from this context is only deleted if the parameter |
| 273 | * "free_visited" is true. Be aware that the passed "search_ctx_arg" is freed |
| 274 | * if the reinitialization fails. |
| 275 | * |
| 276 | * If you don't have a search context from a previous call "search_ctx_arg" |
| 277 | * must be NULL. |
| 278 | * |
| 279 | * This function silently ignores a few errors, vim_findfile() will have |
| 280 | * limited functionality then. |
| 281 | */ |
| 282 | void * |
| 283 | vim_findfile_init( |
| 284 | char_u *path, |
| 285 | char_u *filename, |
| 286 | char_u *stopdirs UNUSED, |
| 287 | int level, |
| 288 | int free_visited, |
| 289 | int find_what, |
| 290 | void *search_ctx_arg, |
| 291 | int tagfile, // expanding names of tags files |
| 292 | char_u *rel_fname) // file name to use for "." |
| 293 | { |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 294 | char_u *wc_part; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 295 | ff_stack_T *sptr; |
| 296 | ff_search_ctx_T *search_ctx; |
| 297 | |
| 298 | // If a search context is given by the caller, reuse it, else allocate a |
| 299 | // new one. |
| 300 | if (search_ctx_arg != NULL) |
| 301 | search_ctx = search_ctx_arg; |
| 302 | else |
| 303 | { |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 304 | search_ctx = ALLOC_CLEAR_ONE(ff_search_ctx_T); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 305 | if (search_ctx == NULL) |
| 306 | goto error_return; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 307 | } |
| 308 | search_ctx->ffsc_find_what = find_what; |
| 309 | search_ctx->ffsc_tagfile = tagfile; |
| 310 | |
| 311 | // clear the search context, but NOT the visited lists |
| 312 | ff_clear(search_ctx); |
| 313 | |
| 314 | // clear visited list if wanted |
| 315 | if (free_visited == TRUE) |
| 316 | vim_findfile_free_visited(search_ctx); |
| 317 | else |
| 318 | { |
| 319 | // Reuse old visited lists. Get the visited list for the given |
| 320 | // filename. If no list for the current filename exists, creates a new |
| 321 | // one. |
| 322 | search_ctx->ffsc_visited_list = ff_get_visited_list(filename, |
| 323 | &search_ctx->ffsc_visited_lists_list); |
| 324 | if (search_ctx->ffsc_visited_list == NULL) |
| 325 | goto error_return; |
| 326 | search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename, |
| 327 | &search_ctx->ffsc_dir_visited_lists_list); |
| 328 | if (search_ctx->ffsc_dir_visited_list == NULL) |
| 329 | goto error_return; |
| 330 | } |
| 331 | |
| 332 | if (ff_expand_buffer == NULL) |
| 333 | { |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 334 | ff_expand_buffer = alloc(MAXPATHL); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 335 | if (ff_expand_buffer == NULL) |
| 336 | goto error_return; |
| 337 | } |
| 338 | |
| 339 | // Store information on starting dir now if path is relative. |
| 340 | // If path is absolute, we do that later. |
| 341 | if (path[0] == '.' |
| 342 | && (vim_ispathsep(path[1]) || path[1] == NUL) |
| 343 | && (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL) |
| 344 | && rel_fname != NULL) |
| 345 | { |
| 346 | int len = (int)(gettail(rel_fname) - rel_fname); |
| 347 | |
| 348 | if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL) |
| 349 | { |
| 350 | // Make the start dir an absolute path name. |
| 351 | vim_strncpy(ff_expand_buffer, rel_fname, len); |
| 352 | search_ctx->ffsc_start_dir = FullName_save(ff_expand_buffer, FALSE); |
| 353 | } |
| 354 | else |
| 355 | search_ctx->ffsc_start_dir = vim_strnsave(rel_fname, len); |
| 356 | if (search_ctx->ffsc_start_dir == NULL) |
| 357 | goto error_return; |
| 358 | if (*++path != NUL) |
| 359 | ++path; |
| 360 | } |
| 361 | else if (*path == NUL || !vim_isAbsName(path)) |
| 362 | { |
| 363 | #ifdef BACKSLASH_IN_FILENAME |
| 364 | // "c:dir" needs "c:" to be expanded, otherwise use current dir |
| 365 | if (*path != NUL && path[1] == ':') |
| 366 | { |
| 367 | char_u drive[3]; |
| 368 | |
| 369 | drive[0] = path[0]; |
| 370 | drive[1] = ':'; |
| 371 | drive[2] = NUL; |
| 372 | if (vim_FullName(drive, ff_expand_buffer, MAXPATHL, TRUE) == FAIL) |
| 373 | goto error_return; |
| 374 | path += 2; |
| 375 | } |
| 376 | else |
| 377 | #endif |
| 378 | if (mch_dirname(ff_expand_buffer, MAXPATHL) == FAIL) |
| 379 | goto error_return; |
| 380 | |
| 381 | search_ctx->ffsc_start_dir = vim_strsave(ff_expand_buffer); |
| 382 | if (search_ctx->ffsc_start_dir == NULL) |
| 383 | goto error_return; |
| 384 | |
| 385 | #ifdef BACKSLASH_IN_FILENAME |
| 386 | // A path that starts with "/dir" is relative to the drive, not to the |
| 387 | // directory (but not for "//machine/dir"). Only use the drive name. |
| 388 | if ((*path == '/' || *path == '\\') |
| 389 | && path[1] != path[0] |
| 390 | && search_ctx->ffsc_start_dir[1] == ':') |
| 391 | search_ctx->ffsc_start_dir[2] = NUL; |
| 392 | #endif |
| 393 | } |
| 394 | |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 395 | /* |
| 396 | * If stopdirs are given, split them into an array of pointers. |
| 397 | * If this fails (mem allocation), there is no upward search at all or a |
| 398 | * stop directory is not recognized -> continue silently. |
| 399 | * If stopdirs just contains a ";" or is empty, |
| 400 | * search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This |
| 401 | * is handled as unlimited upward search. See function |
| 402 | * ff_path_in_stoplist() for details. |
| 403 | */ |
| 404 | if (stopdirs != NULL) |
| 405 | { |
| 406 | char_u *walker = stopdirs; |
| 407 | int dircount; |
| 408 | |
| 409 | while (*walker == ';') |
| 410 | walker++; |
| 411 | |
| 412 | dircount = 1; |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 413 | search_ctx->ffsc_stopdirs_v = ALLOC_ONE(char_u *); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 414 | |
| 415 | if (search_ctx->ffsc_stopdirs_v != NULL) |
| 416 | { |
| 417 | do |
| 418 | { |
| 419 | char_u *helper; |
| 420 | void *ptr; |
zeertzjq | 764526e | 2024-07-11 22:24:15 +0200 | [diff] [blame] | 421 | size_t len; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 422 | |
| 423 | helper = walker; |
| 424 | ptr = vim_realloc(search_ctx->ffsc_stopdirs_v, |
| 425 | (dircount + 1) * sizeof(char_u *)); |
| 426 | if (ptr) |
| 427 | search_ctx->ffsc_stopdirs_v = ptr; |
| 428 | else |
| 429 | // ignore, keep what we have and continue |
| 430 | break; |
| 431 | walker = vim_strchr(walker, ';'); |
zeertzjq | 764526e | 2024-07-11 22:24:15 +0200 | [diff] [blame] | 432 | len = walker ? (size_t)(walker - helper) : STRLEN(helper); |
| 433 | // "" means ascent till top of directory tree. |
| 434 | if (*helper != NUL && !vim_isAbsName(helper) |
| 435 | && len + 1 < MAXPATHL) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 436 | { |
zeertzjq | 764526e | 2024-07-11 22:24:15 +0200 | [diff] [blame] | 437 | // Make the stop dir an absolute path name. |
| 438 | vim_strncpy(ff_expand_buffer, helper, len); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 439 | search_ctx->ffsc_stopdirs_v[dircount-1] = |
zeertzjq | 764526e | 2024-07-11 22:24:15 +0200 | [diff] [blame] | 440 | FullName_save(ff_expand_buffer, FALSE); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 441 | } |
| 442 | else |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 443 | search_ctx->ffsc_stopdirs_v[dircount-1] = |
zeertzjq | 764526e | 2024-07-11 22:24:15 +0200 | [diff] [blame] | 444 | vim_strnsave(helper, len); |
| 445 | if (walker) |
| 446 | walker++; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 447 | dircount++; |
| 448 | |
| 449 | } while (walker != NULL); |
| 450 | search_ctx->ffsc_stopdirs_v[dircount-1] = NULL; |
| 451 | } |
| 452 | } |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 453 | |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 454 | search_ctx->ffsc_level = level; |
| 455 | |
| 456 | /* |
| 457 | * split into: |
| 458 | * -fix path |
| 459 | * -wildcard_stuff (might be NULL) |
| 460 | */ |
| 461 | wc_part = vim_strchr(path, '*'); |
| 462 | if (wc_part != NULL) |
| 463 | { |
| 464 | int llevel; |
| 465 | int len; |
| 466 | char *errpt; |
| 467 | |
| 468 | // save the fix part of the path |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 469 | search_ctx->ffsc_fix_path = vim_strnsave(path, wc_part - path); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 470 | |
| 471 | /* |
| 472 | * copy wc_path and add restricts to the '**' wildcard. |
| 473 | * The octet after a '**' is used as a (binary) counter. |
| 474 | * So '**3' is transposed to '**^C' ('^C' is ASCII value 3) |
| 475 | * or '**76' is transposed to '**N'( 'N' is ASCII value 76). |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 476 | * If no restrict is given after '**' the default is used. |
| 477 | * Due to this technique the path looks awful if you print it as a |
| 478 | * string. |
| 479 | */ |
| 480 | len = 0; |
| 481 | while (*wc_part != NUL) |
| 482 | { |
| 483 | if (len + 5 >= MAXPATHL) |
| 484 | { |
Bram Moolenaar | 9d00e4a | 2022-01-05 17:49:15 +0000 | [diff] [blame] | 485 | emsg(_(e_path_too_long_for_completion)); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 486 | break; |
| 487 | } |
| 488 | if (STRNCMP(wc_part, "**", 2) == 0) |
| 489 | { |
| 490 | ff_expand_buffer[len++] = *wc_part++; |
| 491 | ff_expand_buffer[len++] = *wc_part++; |
| 492 | |
| 493 | llevel = strtol((char *)wc_part, &errpt, 10); |
| 494 | if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255) |
| 495 | ff_expand_buffer[len++] = llevel; |
| 496 | else if ((char_u *)errpt != wc_part && llevel == 0) |
| 497 | // restrict is 0 -> remove already added '**' |
| 498 | len -= 2; |
| 499 | else |
| 500 | ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND; |
| 501 | wc_part = (char_u *)errpt; |
| 502 | if (*wc_part != NUL && !vim_ispathsep(*wc_part)) |
| 503 | { |
Bram Moolenaar | eaaac01 | 2022-01-02 17:00:40 +0000 | [diff] [blame] | 504 | semsg(_(e_invalid_path_number_must_be_at_end_of_path_or_be_followed_by_str), PATHSEPSTR); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 505 | goto error_return; |
| 506 | } |
| 507 | } |
| 508 | else |
| 509 | ff_expand_buffer[len++] = *wc_part++; |
| 510 | } |
| 511 | ff_expand_buffer[len] = NUL; |
| 512 | search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer); |
| 513 | |
| 514 | if (search_ctx->ffsc_wc_path == NULL) |
| 515 | goto error_return; |
| 516 | } |
| 517 | else |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 518 | search_ctx->ffsc_fix_path = vim_strsave(path); |
| 519 | |
| 520 | if (search_ctx->ffsc_start_dir == NULL) |
| 521 | { |
| 522 | // store the fix part as startdir. |
| 523 | // This is needed if the parameter path is fully qualified. |
| 524 | search_ctx->ffsc_start_dir = vim_strsave(search_ctx->ffsc_fix_path); |
| 525 | if (search_ctx->ffsc_start_dir == NULL) |
| 526 | goto error_return; |
| 527 | search_ctx->ffsc_fix_path[0] = NUL; |
| 528 | } |
| 529 | |
| 530 | // create an absolute path |
| 531 | if (STRLEN(search_ctx->ffsc_start_dir) |
| 532 | + STRLEN(search_ctx->ffsc_fix_path) + 3 >= MAXPATHL) |
| 533 | { |
Bram Moolenaar | 9d00e4a | 2022-01-05 17:49:15 +0000 | [diff] [blame] | 534 | emsg(_(e_path_too_long_for_completion)); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 535 | goto error_return; |
| 536 | } |
| 537 | STRCPY(ff_expand_buffer, search_ctx->ffsc_start_dir); |
| 538 | add_pathsep(ff_expand_buffer); |
| 539 | { |
| 540 | int eb_len = (int)STRLEN(ff_expand_buffer); |
| 541 | char_u *buf = alloc(eb_len |
| 542 | + (int)STRLEN(search_ctx->ffsc_fix_path) + 1); |
| 543 | |
| 544 | STRCPY(buf, ff_expand_buffer); |
| 545 | STRCPY(buf + eb_len, search_ctx->ffsc_fix_path); |
| 546 | if (mch_isdir(buf)) |
| 547 | { |
| 548 | STRCAT(ff_expand_buffer, search_ctx->ffsc_fix_path); |
| 549 | add_pathsep(ff_expand_buffer); |
| 550 | } |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 551 | else |
| 552 | { |
| 553 | char_u *p = gettail(search_ctx->ffsc_fix_path); |
| 554 | char_u *wc_path = NULL; |
| 555 | char_u *temp = NULL; |
| 556 | int len = 0; |
| 557 | |
| 558 | if (p > search_ctx->ffsc_fix_path) |
| 559 | { |
Christian Brabandt | 7a4ca32 | 2021-07-25 15:08:05 +0200 | [diff] [blame] | 560 | // do not add '..' to the path and start upwards searching |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 561 | len = (int)(p - search_ctx->ffsc_fix_path) - 1; |
Christian Brabandt | 7a4ca32 | 2021-07-25 15:08:05 +0200 | [diff] [blame] | 562 | if ((len >= 2 |
| 563 | && STRNCMP(search_ctx->ffsc_fix_path, "..", 2) == 0) |
| 564 | && (len == 2 |
| 565 | || search_ctx->ffsc_fix_path[2] == PATHSEP)) |
| 566 | { |
| 567 | vim_free(buf); |
| 568 | goto error_return; |
| 569 | } |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 570 | STRNCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, len); |
| 571 | add_pathsep(ff_expand_buffer); |
| 572 | } |
| 573 | else |
| 574 | len = (int)STRLEN(search_ctx->ffsc_fix_path); |
| 575 | |
| 576 | if (search_ctx->ffsc_wc_path != NULL) |
| 577 | { |
| 578 | wc_path = vim_strsave(search_ctx->ffsc_wc_path); |
Bram Moolenaar | 51e1438 | 2019-05-25 20:21:28 +0200 | [diff] [blame] | 579 | temp = alloc(STRLEN(search_ctx->ffsc_wc_path) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 580 | + STRLEN(search_ctx->ffsc_fix_path + len) |
Bram Moolenaar | 51e1438 | 2019-05-25 20:21:28 +0200 | [diff] [blame] | 581 | + 1); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 582 | if (temp == NULL || wc_path == NULL) |
| 583 | { |
| 584 | vim_free(buf); |
| 585 | vim_free(temp); |
| 586 | vim_free(wc_path); |
| 587 | goto error_return; |
| 588 | } |
| 589 | |
| 590 | STRCPY(temp, search_ctx->ffsc_fix_path + len); |
| 591 | STRCAT(temp, search_ctx->ffsc_wc_path); |
| 592 | vim_free(search_ctx->ffsc_wc_path); |
| 593 | vim_free(wc_path); |
| 594 | search_ctx->ffsc_wc_path = temp; |
| 595 | } |
| 596 | } |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 597 | vim_free(buf); |
| 598 | } |
| 599 | |
| 600 | sptr = ff_create_stack_element(ff_expand_buffer, |
Bram Moolenaar | 2bd9dbc | 2022-08-25 18:12:06 +0100 | [diff] [blame] | 601 | search_ctx->ffsc_wc_path, level, 0); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 602 | |
| 603 | if (sptr == NULL) |
| 604 | goto error_return; |
| 605 | |
| 606 | ff_push(search_ctx, sptr); |
| 607 | |
| 608 | search_ctx->ffsc_file_to_search = vim_strsave(filename); |
| 609 | if (search_ctx->ffsc_file_to_search == NULL) |
| 610 | goto error_return; |
| 611 | |
| 612 | return search_ctx; |
| 613 | |
| 614 | error_return: |
| 615 | /* |
| 616 | * We clear the search context now! |
| 617 | * Even when the caller gave us a (perhaps valid) context we free it here, |
| 618 | * as we might have already destroyed it. |
| 619 | */ |
| 620 | vim_findfile_cleanup(search_ctx); |
| 621 | return NULL; |
| 622 | } |
| 623 | |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 624 | /* |
| 625 | * Get the stopdir string. Check that ';' is not escaped. |
| 626 | */ |
| 627 | char_u * |
| 628 | vim_findfile_stopdir(char_u *buf) |
| 629 | { |
| 630 | char_u *r_ptr = buf; |
| 631 | |
| 632 | while (*r_ptr != NUL && *r_ptr != ';') |
| 633 | { |
| 634 | if (r_ptr[0] == '\\' && r_ptr[1] == ';') |
| 635 | { |
| 636 | // Overwrite the escape char, |
| 637 | // use STRLEN(r_ptr) to move the trailing '\0'. |
| 638 | STRMOVE(r_ptr, r_ptr + 1); |
| 639 | r_ptr++; |
| 640 | } |
| 641 | r_ptr++; |
| 642 | } |
| 643 | if (*r_ptr == ';') |
| 644 | { |
| 645 | *r_ptr = 0; |
| 646 | r_ptr++; |
| 647 | } |
| 648 | else if (*r_ptr == NUL) |
| 649 | r_ptr = NULL; |
| 650 | return r_ptr; |
| 651 | } |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 652 | |
| 653 | /* |
| 654 | * Clean up the given search context. Can handle a NULL pointer. |
| 655 | */ |
| 656 | void |
| 657 | vim_findfile_cleanup(void *ctx) |
| 658 | { |
| 659 | if (ctx == NULL) |
| 660 | return; |
| 661 | |
| 662 | vim_findfile_free_visited(ctx); |
| 663 | ff_clear(ctx); |
| 664 | vim_free(ctx); |
| 665 | } |
| 666 | |
| 667 | /* |
| 668 | * Find a file in a search context. |
| 669 | * The search context was created with vim_findfile_init() above. |
| 670 | * Return a pointer to an allocated file name or NULL if nothing found. |
| 671 | * To get all matching files call this function until you get NULL. |
| 672 | * |
| 673 | * If the passed search_context is NULL, NULL is returned. |
| 674 | * |
| 675 | * The search algorithm is depth first. To change this replace the |
| 676 | * stack with a list (don't forget to leave partly searched directories on the |
| 677 | * top of the list). |
| 678 | */ |
| 679 | char_u * |
| 680 | vim_findfile(void *search_ctx_arg) |
| 681 | { |
| 682 | char_u *file_path; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 683 | char_u *rest_of_wildcards; |
| 684 | char_u *path_end = NULL; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 685 | ff_stack_T *stackp; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 686 | int len; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 687 | int i; |
| 688 | char_u *p; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 689 | char_u *suf; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 690 | ff_search_ctx_T *search_ctx; |
| 691 | |
| 692 | if (search_ctx_arg == NULL) |
| 693 | return NULL; |
| 694 | |
| 695 | search_ctx = (ff_search_ctx_T *)search_ctx_arg; |
| 696 | |
| 697 | /* |
| 698 | * filepath is used as buffer for various actions and as the storage to |
| 699 | * return a found filename. |
| 700 | */ |
Bram Moolenaar | 51e1438 | 2019-05-25 20:21:28 +0200 | [diff] [blame] | 701 | if ((file_path = alloc(MAXPATHL)) == NULL) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 702 | return NULL; |
| 703 | |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 704 | // store the end of the start dir -- needed for upward search |
| 705 | if (search_ctx->ffsc_start_dir != NULL) |
| 706 | path_end = &search_ctx->ffsc_start_dir[ |
| 707 | STRLEN(search_ctx->ffsc_start_dir)]; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 708 | |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 709 | // upward search loop |
| 710 | for (;;) |
| 711 | { |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 712 | // downward search loop |
| 713 | for (;;) |
| 714 | { |
Dominique Pelle | af4a61a | 2021-12-27 17:21:41 +0000 | [diff] [blame] | 715 | // check if user wants to stop the search |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 716 | ui_breakcheck(); |
| 717 | if (got_int) |
| 718 | break; |
| 719 | |
| 720 | // get directory to work on from stack |
| 721 | stackp = ff_pop(search_ctx); |
| 722 | if (stackp == NULL) |
| 723 | break; |
| 724 | |
| 725 | /* |
| 726 | * TODO: decide if we leave this test in |
| 727 | * |
| 728 | * GOOD: don't search a directory(-tree) twice. |
| 729 | * BAD: - check linked list for every new directory entered. |
| 730 | * - check for double files also done below |
| 731 | * |
| 732 | * Here we check if we already searched this directory. |
| 733 | * We already searched a directory if: |
| 734 | * 1) The directory is the same. |
| 735 | * 2) We would use the same wildcard string. |
| 736 | * |
| 737 | * Good if you have links on same directory via several ways |
| 738 | * or you have selfreferences in directories (e.g. SuSE Linux 6.3: |
| 739 | * /etc/rc.d/init.d is linked to /etc/rc.d -> endless loop) |
| 740 | * |
| 741 | * This check is only needed for directories we work on for the |
| 742 | * first time (hence stackp->ff_filearray == NULL) |
| 743 | */ |
| 744 | if (stackp->ffs_filearray == NULL |
| 745 | && ff_check_visited(&search_ctx->ffsc_dir_visited_list |
| 746 | ->ffvl_visited_list, |
Bram Moolenaar | 2bd9dbc | 2022-08-25 18:12:06 +0100 | [diff] [blame] | 747 | stackp->ffs_fix_path, stackp->ffs_wc_path) == FAIL) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 748 | { |
| 749 | #ifdef FF_VERBOSE |
| 750 | if (p_verbose >= 5) |
| 751 | { |
| 752 | verbose_enter_scroll(); |
| 753 | smsg("Already Searched: %s (%s)", |
| 754 | stackp->ffs_fix_path, stackp->ffs_wc_path); |
| 755 | // don't overwrite this either |
| 756 | msg_puts("\n"); |
| 757 | verbose_leave_scroll(); |
| 758 | } |
| 759 | #endif |
| 760 | ff_free_stack_element(stackp); |
| 761 | continue; |
| 762 | } |
| 763 | #ifdef FF_VERBOSE |
| 764 | else if (p_verbose >= 5) |
| 765 | { |
| 766 | verbose_enter_scroll(); |
| 767 | smsg("Searching: %s (%s)", |
| 768 | stackp->ffs_fix_path, stackp->ffs_wc_path); |
| 769 | // don't overwrite this either |
| 770 | msg_puts("\n"); |
| 771 | verbose_leave_scroll(); |
| 772 | } |
| 773 | #endif |
| 774 | |
| 775 | // check depth |
| 776 | if (stackp->ffs_level <= 0) |
| 777 | { |
| 778 | ff_free_stack_element(stackp); |
| 779 | continue; |
| 780 | } |
| 781 | |
| 782 | file_path[0] = NUL; |
| 783 | |
| 784 | /* |
| 785 | * If no filearray till now expand wildcards |
| 786 | * The function expand_wildcards() can handle an array of paths |
| 787 | * and all possible expands are returned in one array. We use this |
| 788 | * to handle the expansion of '**' into an empty string. |
| 789 | */ |
| 790 | if (stackp->ffs_filearray == NULL) |
| 791 | { |
| 792 | char_u *dirptrs[2]; |
| 793 | |
| 794 | // we use filepath to build the path expand_wildcards() should |
| 795 | // expand. |
| 796 | dirptrs[0] = file_path; |
| 797 | dirptrs[1] = NULL; |
| 798 | |
| 799 | // if we have a start dir copy it in |
| 800 | if (!vim_isAbsName(stackp->ffs_fix_path) |
| 801 | && search_ctx->ffsc_start_dir) |
| 802 | { |
| 803 | if (STRLEN(search_ctx->ffsc_start_dir) + 1 < MAXPATHL) |
| 804 | { |
| 805 | STRCPY(file_path, search_ctx->ffsc_start_dir); |
| 806 | add_pathsep(file_path); |
| 807 | } |
| 808 | else |
| 809 | { |
| 810 | ff_free_stack_element(stackp); |
| 811 | goto fail; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | // append the fix part of the search path |
| 816 | if (STRLEN(file_path) + STRLEN(stackp->ffs_fix_path) + 1 |
| 817 | < MAXPATHL) |
| 818 | { |
| 819 | STRCAT(file_path, stackp->ffs_fix_path); |
| 820 | add_pathsep(file_path); |
| 821 | } |
| 822 | else |
| 823 | { |
| 824 | ff_free_stack_element(stackp); |
| 825 | goto fail; |
| 826 | } |
| 827 | |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 828 | rest_of_wildcards = stackp->ffs_wc_path; |
| 829 | if (*rest_of_wildcards != NUL) |
| 830 | { |
| 831 | len = (int)STRLEN(file_path); |
| 832 | if (STRNCMP(rest_of_wildcards, "**", 2) == 0) |
| 833 | { |
| 834 | // pointer to the restrict byte |
| 835 | // The restrict byte is not a character! |
| 836 | p = rest_of_wildcards + 2; |
| 837 | |
| 838 | if (*p > 0) |
| 839 | { |
| 840 | (*p)--; |
| 841 | if (len + 1 < MAXPATHL) |
| 842 | file_path[len++] = '*'; |
| 843 | else |
| 844 | { |
| 845 | ff_free_stack_element(stackp); |
| 846 | goto fail; |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | if (*p == 0) |
| 851 | { |
| 852 | // remove '**<numb> from wildcards |
| 853 | STRMOVE(rest_of_wildcards, rest_of_wildcards + 3); |
| 854 | } |
| 855 | else |
| 856 | rest_of_wildcards += 3; |
| 857 | |
| 858 | if (stackp->ffs_star_star_empty == 0) |
| 859 | { |
| 860 | // if not done before, expand '**' to empty |
| 861 | stackp->ffs_star_star_empty = 1; |
| 862 | dirptrs[1] = stackp->ffs_fix_path; |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | /* |
| 867 | * Here we copy until the next path separator or the end of |
| 868 | * the path. If we stop at a path separator, there is |
| 869 | * still something else left. This is handled below by |
| 870 | * pushing every directory returned from expand_wildcards() |
| 871 | * on the stack again for further search. |
| 872 | */ |
| 873 | while (*rest_of_wildcards |
| 874 | && !vim_ispathsep(*rest_of_wildcards)) |
| 875 | if (len + 1 < MAXPATHL) |
| 876 | file_path[len++] = *rest_of_wildcards++; |
| 877 | else |
| 878 | { |
| 879 | ff_free_stack_element(stackp); |
| 880 | goto fail; |
| 881 | } |
| 882 | |
| 883 | file_path[len] = NUL; |
| 884 | if (vim_ispathsep(*rest_of_wildcards)) |
| 885 | rest_of_wildcards++; |
| 886 | } |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 887 | |
| 888 | /* |
| 889 | * Expand wildcards like "*" and "$VAR". |
| 890 | * If the path is a URL don't try this. |
| 891 | */ |
| 892 | if (path_with_url(dirptrs[0])) |
| 893 | { |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 894 | stackp->ffs_filearray = ALLOC_ONE(char_u *); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 895 | if (stackp->ffs_filearray != NULL |
| 896 | && (stackp->ffs_filearray[0] |
| 897 | = vim_strsave(dirptrs[0])) != NULL) |
| 898 | stackp->ffs_filearray_size = 1; |
| 899 | else |
| 900 | stackp->ffs_filearray_size = 0; |
| 901 | } |
| 902 | else |
| 903 | // Add EW_NOTWILD because the expanded path may contain |
| 904 | // wildcard characters that are to be taken literally. |
| 905 | // This is a bit of a hack. |
| 906 | expand_wildcards((dirptrs[1] == NULL) ? 1 : 2, dirptrs, |
| 907 | &stackp->ffs_filearray_size, |
| 908 | &stackp->ffs_filearray, |
| 909 | EW_DIR|EW_ADDSLASH|EW_SILENT|EW_NOTWILD); |
| 910 | |
| 911 | stackp->ffs_filearray_cur = 0; |
| 912 | stackp->ffs_stage = 0; |
| 913 | } |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 914 | else |
| 915 | rest_of_wildcards = &stackp->ffs_wc_path[ |
| 916 | STRLEN(stackp->ffs_wc_path)]; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 917 | |
| 918 | if (stackp->ffs_stage == 0) |
| 919 | { |
| 920 | // this is the first time we work on this directory |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 921 | if (*rest_of_wildcards == NUL) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 922 | { |
| 923 | /* |
| 924 | * We don't have further wildcards to expand, so we have to |
| 925 | * check for the final file now. |
| 926 | */ |
| 927 | for (i = stackp->ffs_filearray_cur; |
| 928 | i < stackp->ffs_filearray_size; ++i) |
| 929 | { |
| 930 | if (!path_with_url(stackp->ffs_filearray[i]) |
| 931 | && !mch_isdir(stackp->ffs_filearray[i])) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 932 | continue; // not a directory |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 933 | |
| 934 | // prepare the filename to be checked for existence |
| 935 | // below |
| 936 | if (STRLEN(stackp->ffs_filearray[i]) + 1 |
| 937 | + STRLEN(search_ctx->ffsc_file_to_search) |
| 938 | < MAXPATHL) |
| 939 | { |
| 940 | STRCPY(file_path, stackp->ffs_filearray[i]); |
| 941 | add_pathsep(file_path); |
| 942 | STRCAT(file_path, search_ctx->ffsc_file_to_search); |
| 943 | } |
| 944 | else |
| 945 | { |
| 946 | ff_free_stack_element(stackp); |
| 947 | goto fail; |
| 948 | } |
| 949 | |
| 950 | /* |
| 951 | * Try without extra suffix and then with suffixes |
| 952 | * from 'suffixesadd'. |
| 953 | */ |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 954 | len = (int)STRLEN(file_path); |
| 955 | if (search_ctx->ffsc_tagfile) |
| 956 | suf = (char_u *)""; |
| 957 | else |
| 958 | suf = curbuf->b_p_sua; |
| 959 | for (;;) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 960 | { |
| 961 | // if file exists and we didn't already find it |
| 962 | if ((path_with_url(file_path) |
| 963 | || (mch_getperm(file_path) >= 0 |
| 964 | && (search_ctx->ffsc_find_what |
| 965 | == FINDFILE_BOTH |
| 966 | || ((search_ctx->ffsc_find_what |
| 967 | == FINDFILE_DIR) |
| 968 | == mch_isdir(file_path))))) |
| 969 | #ifndef FF_VERBOSE |
| 970 | && (ff_check_visited( |
Bram Moolenaar | 2bd9dbc | 2022-08-25 18:12:06 +0100 | [diff] [blame] | 971 | &search_ctx->ffsc_visited_list |
| 972 | ->ffvl_visited_list, |
| 973 | file_path, (char_u *)"") == OK) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 974 | #endif |
| 975 | ) |
| 976 | { |
| 977 | #ifdef FF_VERBOSE |
| 978 | if (ff_check_visited( |
Bram Moolenaar | 2bd9dbc | 2022-08-25 18:12:06 +0100 | [diff] [blame] | 979 | &search_ctx->ffsc_visited_list |
| 980 | ->ffvl_visited_list, |
| 981 | file_path, (char_u *)"") == FAIL) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 982 | { |
| 983 | if (p_verbose >= 5) |
| 984 | { |
| 985 | verbose_enter_scroll(); |
| 986 | smsg("Already: %s", |
| 987 | file_path); |
| 988 | // don't overwrite this either |
| 989 | msg_puts("\n"); |
| 990 | verbose_leave_scroll(); |
| 991 | } |
| 992 | continue; |
| 993 | } |
| 994 | #endif |
| 995 | |
| 996 | // push dir to examine rest of subdirs later |
| 997 | stackp->ffs_filearray_cur = i + 1; |
| 998 | ff_push(search_ctx, stackp); |
| 999 | |
| 1000 | if (!path_with_url(file_path)) |
| 1001 | simplify_filename(file_path); |
| 1002 | if (mch_dirname(ff_expand_buffer, MAXPATHL) |
| 1003 | == OK) |
| 1004 | { |
| 1005 | p = shorten_fname(file_path, |
| 1006 | ff_expand_buffer); |
| 1007 | if (p != NULL) |
| 1008 | STRMOVE(file_path, p); |
| 1009 | } |
| 1010 | #ifdef FF_VERBOSE |
| 1011 | if (p_verbose >= 5) |
| 1012 | { |
| 1013 | verbose_enter_scroll(); |
| 1014 | smsg("HIT: %s", file_path); |
| 1015 | // don't overwrite this either |
| 1016 | msg_puts("\n"); |
| 1017 | verbose_leave_scroll(); |
| 1018 | } |
| 1019 | #endif |
| 1020 | return file_path; |
| 1021 | } |
| 1022 | |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1023 | // Not found or found already, try next suffix. |
| 1024 | if (*suf == NUL) |
| 1025 | break; |
| 1026 | copy_option_part(&suf, file_path + len, |
| 1027 | MAXPATHL - len, ","); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1028 | } |
| 1029 | } |
| 1030 | } |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1031 | else |
| 1032 | { |
| 1033 | /* |
| 1034 | * still wildcards left, push the directories for further |
| 1035 | * search |
| 1036 | */ |
| 1037 | for (i = stackp->ffs_filearray_cur; |
| 1038 | i < stackp->ffs_filearray_size; ++i) |
| 1039 | { |
| 1040 | if (!mch_isdir(stackp->ffs_filearray[i])) |
| 1041 | continue; // not a directory |
| 1042 | |
| 1043 | ff_push(search_ctx, |
| 1044 | ff_create_stack_element( |
| 1045 | stackp->ffs_filearray[i], |
| 1046 | rest_of_wildcards, |
| 1047 | stackp->ffs_level - 1, 0)); |
| 1048 | } |
| 1049 | } |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1050 | stackp->ffs_filearray_cur = 0; |
| 1051 | stackp->ffs_stage = 1; |
| 1052 | } |
| 1053 | |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1054 | /* |
| 1055 | * if wildcards contains '**' we have to descent till we reach the |
| 1056 | * leaves of the directory tree. |
| 1057 | */ |
| 1058 | if (STRNCMP(stackp->ffs_wc_path, "**", 2) == 0) |
| 1059 | { |
| 1060 | for (i = stackp->ffs_filearray_cur; |
| 1061 | i < stackp->ffs_filearray_size; ++i) |
| 1062 | { |
| 1063 | if (fnamecmp(stackp->ffs_filearray[i], |
| 1064 | stackp->ffs_fix_path) == 0) |
| 1065 | continue; // don't repush same directory |
| 1066 | if (!mch_isdir(stackp->ffs_filearray[i])) |
| 1067 | continue; // not a directory |
| 1068 | ff_push(search_ctx, |
| 1069 | ff_create_stack_element(stackp->ffs_filearray[i], |
| 1070 | stackp->ffs_wc_path, stackp->ffs_level - 1, 1)); |
| 1071 | } |
| 1072 | } |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1073 | |
| 1074 | // we are done with the current directory |
| 1075 | ff_free_stack_element(stackp); |
| 1076 | |
| 1077 | } |
| 1078 | |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1079 | // If we reached this, we didn't find anything downwards. |
| 1080 | // Let's check if we should do an upward search. |
| 1081 | if (search_ctx->ffsc_start_dir |
| 1082 | && search_ctx->ffsc_stopdirs_v != NULL && !got_int) |
| 1083 | { |
| 1084 | ff_stack_T *sptr; |
zeertzjq | e6ab23b | 2024-07-11 22:22:26 +0200 | [diff] [blame] | 1085 | // path_end may point to the NUL or the previous path separator |
| 1086 | int plen = (path_end - search_ctx->ffsc_start_dir) |
| 1087 | + (*path_end != NUL); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1088 | |
| 1089 | // is the last starting directory in the stop list? |
| 1090 | if (ff_path_in_stoplist(search_ctx->ffsc_start_dir, |
zeertzjq | e6ab23b | 2024-07-11 22:22:26 +0200 | [diff] [blame] | 1091 | plen, search_ctx->ffsc_stopdirs_v) == TRUE) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1092 | break; |
| 1093 | |
| 1094 | // cut of last dir |
| 1095 | while (path_end > search_ctx->ffsc_start_dir |
| 1096 | && vim_ispathsep(*path_end)) |
| 1097 | path_end--; |
| 1098 | while (path_end > search_ctx->ffsc_start_dir |
| 1099 | && !vim_ispathsep(path_end[-1])) |
| 1100 | path_end--; |
| 1101 | *path_end = 0; |
| 1102 | path_end--; |
| 1103 | |
| 1104 | if (*search_ctx->ffsc_start_dir == 0) |
| 1105 | break; |
| 1106 | |
| 1107 | if (STRLEN(search_ctx->ffsc_start_dir) + 1 |
| 1108 | + STRLEN(search_ctx->ffsc_fix_path) < MAXPATHL) |
| 1109 | { |
| 1110 | STRCPY(file_path, search_ctx->ffsc_start_dir); |
| 1111 | add_pathsep(file_path); |
| 1112 | STRCAT(file_path, search_ctx->ffsc_fix_path); |
| 1113 | } |
| 1114 | else |
| 1115 | goto fail; |
| 1116 | |
| 1117 | // create a new stack entry |
| 1118 | sptr = ff_create_stack_element(file_path, |
| 1119 | search_ctx->ffsc_wc_path, search_ctx->ffsc_level, 0); |
| 1120 | if (sptr == NULL) |
| 1121 | break; |
| 1122 | ff_push(search_ctx, sptr); |
| 1123 | } |
| 1124 | else |
| 1125 | break; |
| 1126 | } |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1127 | |
| 1128 | fail: |
| 1129 | vim_free(file_path); |
| 1130 | return NULL; |
| 1131 | } |
| 1132 | |
| 1133 | /* |
| 1134 | * Free the list of lists of visited files and directories |
| 1135 | * Can handle it if the passed search_context is NULL; |
| 1136 | */ |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 1137 | static void |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1138 | vim_findfile_free_visited(void *search_ctx_arg) |
| 1139 | { |
| 1140 | ff_search_ctx_T *search_ctx; |
| 1141 | |
| 1142 | if (search_ctx_arg == NULL) |
| 1143 | return; |
| 1144 | |
| 1145 | search_ctx = (ff_search_ctx_T *)search_ctx_arg; |
| 1146 | vim_findfile_free_visited_list(&search_ctx->ffsc_visited_lists_list); |
| 1147 | vim_findfile_free_visited_list(&search_ctx->ffsc_dir_visited_lists_list); |
| 1148 | } |
| 1149 | |
| 1150 | static void |
| 1151 | vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp) |
| 1152 | { |
| 1153 | ff_visited_list_hdr_T *vp; |
| 1154 | |
| 1155 | while (*list_headp != NULL) |
| 1156 | { |
| 1157 | vp = (*list_headp)->ffvl_next; |
| 1158 | ff_free_visited_list((*list_headp)->ffvl_visited_list); |
| 1159 | |
| 1160 | vim_free((*list_headp)->ffvl_filename); |
| 1161 | vim_free(*list_headp); |
| 1162 | *list_headp = vp; |
| 1163 | } |
| 1164 | *list_headp = NULL; |
| 1165 | } |
| 1166 | |
| 1167 | static void |
| 1168 | ff_free_visited_list(ff_visited_T *vl) |
| 1169 | { |
| 1170 | ff_visited_T *vp; |
| 1171 | |
| 1172 | while (vl != NULL) |
| 1173 | { |
| 1174 | vp = vl->ffv_next; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1175 | vim_free(vl->ffv_wc_path); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1176 | vim_free(vl); |
| 1177 | vl = vp; |
| 1178 | } |
| 1179 | vl = NULL; |
| 1180 | } |
| 1181 | |
| 1182 | /* |
| 1183 | * Returns the already visited list for the given filename. If none is found it |
| 1184 | * allocates a new one. |
| 1185 | */ |
| 1186 | static ff_visited_list_hdr_T* |
| 1187 | ff_get_visited_list( |
| 1188 | char_u *filename, |
| 1189 | ff_visited_list_hdr_T **list_headp) |
| 1190 | { |
| 1191 | ff_visited_list_hdr_T *retptr = NULL; |
| 1192 | |
| 1193 | // check if a visited list for the given filename exists |
| 1194 | if (*list_headp != NULL) |
| 1195 | { |
| 1196 | retptr = *list_headp; |
| 1197 | while (retptr != NULL) |
| 1198 | { |
| 1199 | if (fnamecmp(filename, retptr->ffvl_filename) == 0) |
| 1200 | { |
| 1201 | #ifdef FF_VERBOSE |
| 1202 | if (p_verbose >= 5) |
| 1203 | { |
| 1204 | verbose_enter_scroll(); |
| 1205 | smsg("ff_get_visited_list: FOUND list for %s", |
| 1206 | filename); |
| 1207 | // don't overwrite this either |
| 1208 | msg_puts("\n"); |
| 1209 | verbose_leave_scroll(); |
| 1210 | } |
| 1211 | #endif |
| 1212 | return retptr; |
| 1213 | } |
| 1214 | retptr = retptr->ffvl_next; |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | #ifdef FF_VERBOSE |
| 1219 | if (p_verbose >= 5) |
| 1220 | { |
| 1221 | verbose_enter_scroll(); |
| 1222 | smsg("ff_get_visited_list: new list for %s", filename); |
| 1223 | // don't overwrite this either |
| 1224 | msg_puts("\n"); |
| 1225 | verbose_leave_scroll(); |
| 1226 | } |
| 1227 | #endif |
| 1228 | |
| 1229 | /* |
| 1230 | * if we reach this we didn't find a list and we have to allocate new list |
| 1231 | */ |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 1232 | retptr = ALLOC_ONE(ff_visited_list_hdr_T); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1233 | if (retptr == NULL) |
| 1234 | return NULL; |
| 1235 | |
| 1236 | retptr->ffvl_visited_list = NULL; |
| 1237 | retptr->ffvl_filename = vim_strsave(filename); |
| 1238 | if (retptr->ffvl_filename == NULL) |
| 1239 | { |
| 1240 | vim_free(retptr); |
| 1241 | return NULL; |
| 1242 | } |
| 1243 | retptr->ffvl_next = *list_headp; |
| 1244 | *list_headp = retptr; |
| 1245 | |
| 1246 | return retptr; |
| 1247 | } |
| 1248 | |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1249 | /* |
| 1250 | * check if two wildcard paths are equal. Returns TRUE or FALSE. |
| 1251 | * They are equal if: |
| 1252 | * - both paths are NULL |
| 1253 | * - they have the same length |
| 1254 | * - char by char comparison is OK |
| 1255 | * - the only differences are in the counters behind a '**', so |
| 1256 | * '**\20' is equal to '**\24' |
| 1257 | */ |
| 1258 | static int |
| 1259 | ff_wc_equal(char_u *s1, char_u *s2) |
| 1260 | { |
| 1261 | int i, j; |
| 1262 | int c1 = NUL; |
| 1263 | int c2 = NUL; |
| 1264 | int prev1 = NUL; |
| 1265 | int prev2 = NUL; |
| 1266 | |
| 1267 | if (s1 == s2) |
| 1268 | return TRUE; |
| 1269 | |
| 1270 | if (s1 == NULL || s2 == NULL) |
| 1271 | return FALSE; |
| 1272 | |
| 1273 | for (i = 0, j = 0; s1[i] != NUL && s2[j] != NUL;) |
| 1274 | { |
| 1275 | c1 = PTR2CHAR(s1 + i); |
| 1276 | c2 = PTR2CHAR(s2 + j); |
| 1277 | |
| 1278 | if ((p_fic ? MB_TOLOWER(c1) != MB_TOLOWER(c2) : c1 != c2) |
| 1279 | && (prev1 != '*' || prev2 != '*')) |
| 1280 | return FALSE; |
| 1281 | prev2 = prev1; |
| 1282 | prev1 = c1; |
| 1283 | |
Bram Moolenaar | 1614a14 | 2019-10-06 22:00:13 +0200 | [diff] [blame] | 1284 | i += mb_ptr2len(s1 + i); |
| 1285 | j += mb_ptr2len(s2 + j); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1286 | } |
| 1287 | return s1[i] == s2[j]; |
| 1288 | } |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1289 | |
| 1290 | /* |
| 1291 | * maintains the list of already visited files and dirs |
| 1292 | * returns FAIL if the given file/dir is already in the list |
| 1293 | * returns OK if it is newly added |
| 1294 | * |
| 1295 | * TODO: What to do on memory allocation problems? |
| 1296 | * -> return TRUE - Better the file is found several times instead of |
| 1297 | * never. |
| 1298 | */ |
| 1299 | static int |
| 1300 | ff_check_visited( |
| 1301 | ff_visited_T **visited_list, |
Bram Moolenaar | 2bd9dbc | 2022-08-25 18:12:06 +0100 | [diff] [blame] | 1302 | char_u *fname, |
| 1303 | char_u *wc_path) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1304 | { |
| 1305 | ff_visited_T *vp; |
| 1306 | #ifdef UNIX |
| 1307 | stat_T st; |
| 1308 | int url = FALSE; |
| 1309 | #endif |
| 1310 | |
Dominique Pelle | af4a61a | 2021-12-27 17:21:41 +0000 | [diff] [blame] | 1311 | // For a URL we only compare the name, otherwise we compare the |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1312 | // device/inode (unix) or the full path name (not Unix). |
| 1313 | if (path_with_url(fname)) |
| 1314 | { |
| 1315 | vim_strncpy(ff_expand_buffer, fname, MAXPATHL - 1); |
| 1316 | #ifdef UNIX |
| 1317 | url = TRUE; |
| 1318 | #endif |
| 1319 | } |
| 1320 | else |
| 1321 | { |
| 1322 | ff_expand_buffer[0] = NUL; |
| 1323 | #ifdef UNIX |
| 1324 | if (mch_stat((char *)fname, &st) < 0) |
| 1325 | #else |
| 1326 | if (vim_FullName(fname, ff_expand_buffer, MAXPATHL, TRUE) == FAIL) |
| 1327 | #endif |
| 1328 | return FAIL; |
| 1329 | } |
| 1330 | |
| 1331 | // check against list of already visited files |
| 1332 | for (vp = *visited_list; vp != NULL; vp = vp->ffv_next) |
| 1333 | { |
| 1334 | if ( |
| 1335 | #ifdef UNIX |
| 1336 | !url ? (vp->ffv_dev_valid && vp->ffv_dev == st.st_dev |
| 1337 | && vp->ffv_ino == st.st_ino) |
| 1338 | : |
| 1339 | #endif |
| 1340 | fnamecmp(vp->ffv_fname, ff_expand_buffer) == 0 |
| 1341 | ) |
| 1342 | { |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1343 | // are the wildcard parts equal |
| 1344 | if (ff_wc_equal(vp->ffv_wc_path, wc_path) == TRUE) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1345 | // already visited |
| 1346 | return FAIL; |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | /* |
| 1351 | * New file/dir. Add it to the list of visited files/dirs. |
| 1352 | */ |
zeertzjq | 1b438a8 | 2023-02-01 13:11:15 +0000 | [diff] [blame] | 1353 | vp = alloc( |
| 1354 | offsetof(ff_visited_T, ffv_fname) + STRLEN(ff_expand_buffer) + 1); |
Yegappan Lakshmanan | 1cfb14a | 2023-01-09 19:04:23 +0000 | [diff] [blame] | 1355 | if (vp == NULL) |
| 1356 | return OK; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1357 | |
Yegappan Lakshmanan | 1cfb14a | 2023-01-09 19:04:23 +0000 | [diff] [blame] | 1358 | #ifdef UNIX |
| 1359 | if (!url) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1360 | { |
Yegappan Lakshmanan | 1cfb14a | 2023-01-09 19:04:23 +0000 | [diff] [blame] | 1361 | vp->ffv_dev_valid = TRUE; |
| 1362 | vp->ffv_ino = st.st_ino; |
| 1363 | vp->ffv_dev = st.st_dev; |
| 1364 | vp->ffv_fname[0] = NUL; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1365 | } |
Yegappan Lakshmanan | 1cfb14a | 2023-01-09 19:04:23 +0000 | [diff] [blame] | 1366 | else |
| 1367 | { |
| 1368 | vp->ffv_dev_valid = FALSE; |
| 1369 | #endif |
| 1370 | STRCPY(vp->ffv_fname, ff_expand_buffer); |
| 1371 | #ifdef UNIX |
| 1372 | } |
| 1373 | #endif |
| 1374 | if (wc_path != NULL) |
| 1375 | vp->ffv_wc_path = vim_strsave(wc_path); |
| 1376 | else |
| 1377 | vp->ffv_wc_path = NULL; |
| 1378 | |
| 1379 | vp->ffv_next = *visited_list; |
| 1380 | *visited_list = vp; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1381 | |
| 1382 | return OK; |
| 1383 | } |
| 1384 | |
| 1385 | /* |
| 1386 | * create stack element from given path pieces |
| 1387 | */ |
| 1388 | static ff_stack_T * |
| 1389 | ff_create_stack_element( |
| 1390 | char_u *fix_part, |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1391 | char_u *wc_part, |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1392 | int level, |
| 1393 | int star_star_empty) |
| 1394 | { |
| 1395 | ff_stack_T *new; |
| 1396 | |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 1397 | new = ALLOC_ONE(ff_stack_T); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1398 | if (new == NULL) |
| 1399 | return NULL; |
| 1400 | |
| 1401 | new->ffs_prev = NULL; |
| 1402 | new->ffs_filearray = NULL; |
| 1403 | new->ffs_filearray_size = 0; |
| 1404 | new->ffs_filearray_cur = 0; |
| 1405 | new->ffs_stage = 0; |
| 1406 | new->ffs_level = level; |
| 1407 | new->ffs_star_star_empty = star_star_empty; |
| 1408 | |
| 1409 | // the following saves NULL pointer checks in vim_findfile |
| 1410 | if (fix_part == NULL) |
| 1411 | fix_part = (char_u *)""; |
| 1412 | new->ffs_fix_path = vim_strsave(fix_part); |
| 1413 | |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1414 | if (wc_part == NULL) |
| 1415 | wc_part = (char_u *)""; |
| 1416 | new->ffs_wc_path = vim_strsave(wc_part); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1417 | |
Bram Moolenaar | 2bd9dbc | 2022-08-25 18:12:06 +0100 | [diff] [blame] | 1418 | if (new->ffs_fix_path == NULL || new->ffs_wc_path == NULL) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1419 | { |
| 1420 | ff_free_stack_element(new); |
| 1421 | new = NULL; |
| 1422 | } |
| 1423 | |
| 1424 | return new; |
| 1425 | } |
| 1426 | |
| 1427 | /* |
| 1428 | * Push a dir on the directory stack. |
| 1429 | */ |
| 1430 | static void |
| 1431 | ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr) |
| 1432 | { |
| 1433 | // check for NULL pointer, not to return an error to the user, but |
| 1434 | // to prevent a crash |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 1435 | if (stack_ptr == NULL) |
| 1436 | return; |
| 1437 | |
| 1438 | stack_ptr->ffs_prev = search_ctx->ffsc_stack_ptr; |
| 1439 | search_ctx->ffsc_stack_ptr = stack_ptr; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | /* |
| 1443 | * Pop a dir from the directory stack. |
| 1444 | * Returns NULL if stack is empty. |
| 1445 | */ |
| 1446 | static ff_stack_T * |
| 1447 | ff_pop(ff_search_ctx_T *search_ctx) |
| 1448 | { |
| 1449 | ff_stack_T *sptr; |
| 1450 | |
| 1451 | sptr = search_ctx->ffsc_stack_ptr; |
| 1452 | if (search_ctx->ffsc_stack_ptr != NULL) |
| 1453 | search_ctx->ffsc_stack_ptr = search_ctx->ffsc_stack_ptr->ffs_prev; |
| 1454 | |
| 1455 | return sptr; |
| 1456 | } |
| 1457 | |
| 1458 | /* |
| 1459 | * free the given stack element |
| 1460 | */ |
| 1461 | static void |
| 1462 | ff_free_stack_element(ff_stack_T *stack_ptr) |
| 1463 | { |
| 1464 | // vim_free handles possible NULL pointers |
| 1465 | vim_free(stack_ptr->ffs_fix_path); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1466 | vim_free(stack_ptr->ffs_wc_path); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1467 | |
| 1468 | if (stack_ptr->ffs_filearray != NULL) |
| 1469 | FreeWild(stack_ptr->ffs_filearray_size, stack_ptr->ffs_filearray); |
| 1470 | |
| 1471 | vim_free(stack_ptr); |
| 1472 | } |
| 1473 | |
| 1474 | /* |
| 1475 | * Clear the search context, but NOT the visited list. |
| 1476 | */ |
| 1477 | static void |
| 1478 | ff_clear(ff_search_ctx_T *search_ctx) |
| 1479 | { |
| 1480 | ff_stack_T *sptr; |
| 1481 | |
| 1482 | // clear up stack |
| 1483 | while ((sptr = ff_pop(search_ctx)) != NULL) |
| 1484 | ff_free_stack_element(sptr); |
| 1485 | |
| 1486 | vim_free(search_ctx->ffsc_file_to_search); |
| 1487 | vim_free(search_ctx->ffsc_start_dir); |
| 1488 | vim_free(search_ctx->ffsc_fix_path); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1489 | vim_free(search_ctx->ffsc_wc_path); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1490 | |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1491 | if (search_ctx->ffsc_stopdirs_v != NULL) |
| 1492 | { |
| 1493 | int i = 0; |
| 1494 | |
| 1495 | while (search_ctx->ffsc_stopdirs_v[i] != NULL) |
| 1496 | { |
| 1497 | vim_free(search_ctx->ffsc_stopdirs_v[i]); |
| 1498 | i++; |
| 1499 | } |
| 1500 | vim_free(search_ctx->ffsc_stopdirs_v); |
| 1501 | } |
| 1502 | search_ctx->ffsc_stopdirs_v = NULL; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1503 | |
| 1504 | // reset everything |
| 1505 | search_ctx->ffsc_file_to_search = NULL; |
| 1506 | search_ctx->ffsc_start_dir = NULL; |
| 1507 | search_ctx->ffsc_fix_path = NULL; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1508 | search_ctx->ffsc_wc_path = NULL; |
| 1509 | search_ctx->ffsc_level = 0; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1510 | } |
| 1511 | |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1512 | /* |
| 1513 | * check if the given path is in the stopdirs |
| 1514 | * returns TRUE if yes else FALSE |
| 1515 | */ |
| 1516 | static int |
| 1517 | ff_path_in_stoplist(char_u *path, int path_len, char_u **stopdirs_v) |
| 1518 | { |
| 1519 | int i = 0; |
| 1520 | |
| 1521 | // eat up trailing path separators, except the first |
| 1522 | while (path_len > 1 && vim_ispathsep(path[path_len - 1])) |
| 1523 | path_len--; |
| 1524 | |
| 1525 | // if no path consider it as match |
| 1526 | if (path_len == 0) |
| 1527 | return TRUE; |
| 1528 | |
| 1529 | for (i = 0; stopdirs_v[i] != NULL; i++) |
zeertzjq | e6ab23b | 2024-07-11 22:22:26 +0200 | [diff] [blame] | 1530 | // match for parent directory. So '/home' also matches |
| 1531 | // '/home/rks'. Check for PATHSEP in stopdirs_v[i], else |
| 1532 | // '/home/r' would also match '/home/rks' |
| 1533 | if (fnamencmp(stopdirs_v[i], path, path_len) == 0 |
| 1534 | && ((int)STRLEN(stopdirs_v[i]) <= path_len |
| 1535 | || vim_ispathsep(stopdirs_v[i][path_len]))) |
| 1536 | return TRUE; |
| 1537 | |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1538 | return FALSE; |
| 1539 | } |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1540 | |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1541 | /* |
| 1542 | * Find the file name "ptr[len]" in the path. Also finds directory names. |
| 1543 | * |
| 1544 | * On the first call set the parameter 'first' to TRUE to initialize |
| 1545 | * the search. For repeating calls to FALSE. |
| 1546 | * |
| 1547 | * Repeating calls will return other files called 'ptr[len]' from the path. |
| 1548 | * |
| 1549 | * Only on the first call 'ptr' and 'len' are used. For repeating calls they |
| 1550 | * don't need valid values. |
| 1551 | * |
| 1552 | * If nothing found on the first call the option FNAME_MESS will issue the |
| 1553 | * message: |
| 1554 | * 'Can't find file "<file>" in path' |
| 1555 | * On repeating calls: |
| 1556 | * 'No more file "<file>" found in path' |
| 1557 | * |
| 1558 | * options: |
| 1559 | * FNAME_MESS give error message when not found |
| 1560 | * |
| 1561 | * Uses NameBuff[]! |
| 1562 | * |
| 1563 | * Returns an allocated string for the file name. NULL for error. |
| 1564 | * |
| 1565 | */ |
| 1566 | char_u * |
| 1567 | find_file_in_path( |
| 1568 | char_u *ptr, // file name |
| 1569 | int len, // length of file name |
| 1570 | int options, |
| 1571 | int first, // use count'th matching file name |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1572 | char_u *rel_fname, // file name searching relative to |
| 1573 | char_u **file_to_find, // in/out: modified copy of file name |
| 1574 | char **search_ctx) // in/out: state of the search |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1575 | { |
| 1576 | return find_file_in_path_option(ptr, len, options, first, |
| 1577 | *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path, |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1578 | FINDFILE_BOTH, rel_fname, curbuf->b_p_sua, |
| 1579 | file_to_find, search_ctx); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1580 | } |
| 1581 | |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1582 | # if defined(EXITFREE) || defined(PROTO) |
| 1583 | void |
| 1584 | free_findfile(void) |
| 1585 | { |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1586 | VIM_CLEAR(ff_expand_buffer); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1587 | } |
| 1588 | # endif |
| 1589 | |
| 1590 | /* |
| 1591 | * Find the directory name "ptr[len]" in the path. |
| 1592 | * |
| 1593 | * options: |
| 1594 | * FNAME_MESS give error message when not found |
| 1595 | * FNAME_UNESC unescape backslashes. |
| 1596 | * |
| 1597 | * Uses NameBuff[]! |
| 1598 | * |
| 1599 | * Returns an allocated string for the file name. NULL for error. |
| 1600 | */ |
| 1601 | char_u * |
| 1602 | find_directory_in_path( |
| 1603 | char_u *ptr, // file name |
| 1604 | int len, // length of file name |
| 1605 | int options, |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1606 | char_u *rel_fname, // file name searching relative to |
| 1607 | char_u **file_to_find, // in/out: modified copy of file name |
| 1608 | char **search_ctx) // in/out: state of the search |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1609 | { |
| 1610 | return find_file_in_path_option(ptr, len, options, TRUE, p_cdpath, |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1611 | FINDFILE_DIR, rel_fname, (char_u *)"", |
| 1612 | file_to_find, search_ctx); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | char_u * |
| 1616 | find_file_in_path_option( |
| 1617 | char_u *ptr, // file name |
| 1618 | int len, // length of file name |
| 1619 | int options, |
| 1620 | int first, // use count'th matching file name |
| 1621 | char_u *path_option, // p_path or p_cdpath |
| 1622 | int find_what, // FINDFILE_FILE, _DIR or _BOTH |
| 1623 | char_u *rel_fname, // file name we are looking relative to. |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1624 | char_u *suffixes, // list of suffixes, 'suffixesadd' option |
| 1625 | char_u **file_to_find, // in/out: modified copy of file name |
| 1626 | char **search_ctx_arg) // in/out: state of the search |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1627 | { |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1628 | ff_search_ctx_T **search_ctx = (ff_search_ctx_T **)search_ctx_arg; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1629 | static char_u *dir; |
| 1630 | static int did_findfile_init = FALSE; |
| 1631 | char_u save_char; |
| 1632 | char_u *file_name = NULL; |
| 1633 | char_u *buf = NULL; |
| 1634 | int rel_to_curdir; |
| 1635 | # ifdef AMIGA |
| 1636 | struct Process *proc = (struct Process *)FindTask(0L); |
| 1637 | APTR save_winptr = proc->pr_WindowPtr; |
| 1638 | |
| 1639 | // Avoid a requester here for a volume that doesn't exist. |
| 1640 | proc->pr_WindowPtr = (APTR)-1L; |
| 1641 | # endif |
| 1642 | |
| 1643 | if (first == TRUE) |
| 1644 | { |
Bram Moolenaar | e015d99 | 2021-11-17 19:01:53 +0000 | [diff] [blame] | 1645 | if (len == 0) |
| 1646 | return NULL; |
| 1647 | |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1648 | // copy file name into NameBuff, expanding environment variables |
| 1649 | save_char = ptr[len]; |
| 1650 | ptr[len] = NUL; |
| 1651 | expand_env_esc(ptr, NameBuff, MAXPATHL, FALSE, TRUE, NULL); |
| 1652 | ptr[len] = save_char; |
| 1653 | |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1654 | vim_free(*file_to_find); |
| 1655 | *file_to_find = vim_strsave(NameBuff); |
| 1656 | if (*file_to_find == NULL) // out of memory |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1657 | { |
| 1658 | file_name = NULL; |
| 1659 | goto theend; |
| 1660 | } |
| 1661 | if (options & FNAME_UNESC) |
| 1662 | { |
| 1663 | // Change all "\ " to " ". |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1664 | for (ptr = *file_to_find; *ptr != NUL; ++ptr) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1665 | if (ptr[0] == '\\' && ptr[1] == ' ') |
| 1666 | mch_memmove(ptr, ptr + 1, STRLEN(ptr)); |
| 1667 | } |
| 1668 | } |
| 1669 | |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1670 | rel_to_curdir = ((*file_to_find)[0] == '.' |
| 1671 | && ((*file_to_find)[1] == NUL |
| 1672 | || vim_ispathsep((*file_to_find)[1]) |
| 1673 | || ((*file_to_find)[1] == '.' |
| 1674 | && ((*file_to_find)[2] == NUL |
| 1675 | || vim_ispathsep((*file_to_find)[2]))))); |
| 1676 | if (vim_isAbsName(*file_to_find) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1677 | // "..", "../path", "." and "./path": don't use the path_option |
| 1678 | || rel_to_curdir |
| 1679 | # if defined(MSWIN) |
| 1680 | // handle "\tmp" as absolute path |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1681 | || vim_ispathsep((*file_to_find)[0]) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1682 | // handle "c:name" as absolute path |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1683 | || ((*file_to_find)[0] != NUL && (*file_to_find)[1] == ':') |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1684 | # endif |
| 1685 | # ifdef AMIGA |
| 1686 | // handle ":tmp" as absolute path |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1687 | || (*file_to_find)[0] == ':' |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1688 | # endif |
| 1689 | ) |
| 1690 | { |
| 1691 | /* |
| 1692 | * Absolute path, no need to use "path_option". |
| 1693 | * If this is not a first call, return NULL. We already returned a |
| 1694 | * filename on the first call. |
| 1695 | */ |
| 1696 | if (first == TRUE) |
| 1697 | { |
| 1698 | int l; |
| 1699 | int run; |
| 1700 | |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1701 | if (path_with_url(*file_to_find)) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1702 | { |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1703 | file_name = vim_strsave(*file_to_find); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1704 | goto theend; |
| 1705 | } |
| 1706 | |
| 1707 | // When FNAME_REL flag given first use the directory of the file. |
| 1708 | // Otherwise or when this fails use the current directory. |
| 1709 | for (run = 1; run <= 2; ++run) |
| 1710 | { |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1711 | l = (int)STRLEN(*file_to_find); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1712 | if (run == 1 |
| 1713 | && rel_to_curdir |
| 1714 | && (options & FNAME_REL) |
| 1715 | && rel_fname != NULL |
| 1716 | && STRLEN(rel_fname) + l < MAXPATHL) |
| 1717 | { |
| 1718 | STRCPY(NameBuff, rel_fname); |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1719 | STRCPY(gettail(NameBuff), *file_to_find); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1720 | l = (int)STRLEN(NameBuff); |
| 1721 | } |
| 1722 | else |
| 1723 | { |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1724 | STRCPY(NameBuff, *file_to_find); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1725 | run = 2; |
| 1726 | } |
| 1727 | |
| 1728 | // When the file doesn't exist, try adding parts of |
| 1729 | // 'suffixesadd'. |
| 1730 | buf = suffixes; |
| 1731 | for (;;) |
| 1732 | { |
| 1733 | if (mch_getperm(NameBuff) >= 0 |
| 1734 | && (find_what == FINDFILE_BOTH |
| 1735 | || ((find_what == FINDFILE_DIR) |
| 1736 | == mch_isdir(NameBuff)))) |
| 1737 | { |
| 1738 | file_name = vim_strsave(NameBuff); |
| 1739 | goto theend; |
| 1740 | } |
| 1741 | if (*buf == NUL) |
| 1742 | break; |
| 1743 | copy_option_part(&buf, NameBuff + l, MAXPATHL - l, ","); |
| 1744 | } |
| 1745 | } |
| 1746 | } |
| 1747 | } |
| 1748 | else |
| 1749 | { |
| 1750 | /* |
| 1751 | * Loop over all paths in the 'path' or 'cdpath' option. |
| 1752 | * When "first" is set, first setup to the start of the option. |
| 1753 | * Otherwise continue to find the next match. |
| 1754 | */ |
| 1755 | if (first == TRUE) |
| 1756 | { |
| 1757 | // vim_findfile_free_visited can handle a possible NULL pointer |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1758 | vim_findfile_free_visited(*search_ctx); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1759 | dir = path_option; |
| 1760 | did_findfile_init = FALSE; |
| 1761 | } |
| 1762 | |
| 1763 | for (;;) |
| 1764 | { |
| 1765 | if (did_findfile_init) |
| 1766 | { |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1767 | file_name = vim_findfile(*search_ctx); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1768 | if (file_name != NULL) |
| 1769 | break; |
| 1770 | |
| 1771 | did_findfile_init = FALSE; |
| 1772 | } |
| 1773 | else |
| 1774 | { |
| 1775 | char_u *r_ptr; |
| 1776 | |
| 1777 | if (dir == NULL || *dir == NUL) |
| 1778 | { |
| 1779 | // We searched all paths of the option, now we can |
| 1780 | // free the search context. |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1781 | vim_findfile_cleanup(*search_ctx); |
| 1782 | *search_ctx = NULL; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1783 | break; |
| 1784 | } |
| 1785 | |
Bram Moolenaar | 51e1438 | 2019-05-25 20:21:28 +0200 | [diff] [blame] | 1786 | if ((buf = alloc(MAXPATHL)) == NULL) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1787 | break; |
| 1788 | |
| 1789 | // copy next path |
| 1790 | buf[0] = 0; |
| 1791 | copy_option_part(&dir, buf, MAXPATHL, " ,"); |
| 1792 | |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1793 | // get the stopdir string |
| 1794 | r_ptr = vim_findfile_stopdir(buf); |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1795 | *search_ctx = vim_findfile_init(buf, *file_to_find, |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1796 | r_ptr, 100, FALSE, find_what, |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1797 | *search_ctx, FALSE, rel_fname); |
| 1798 | if (*search_ctx != NULL) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1799 | did_findfile_init = TRUE; |
| 1800 | vim_free(buf); |
| 1801 | } |
| 1802 | } |
| 1803 | } |
| 1804 | if (file_name == NULL && (options & FNAME_MESS)) |
| 1805 | { |
| 1806 | if (first == TRUE) |
| 1807 | { |
| 1808 | if (find_what == FINDFILE_DIR) |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1809 | semsg(_(e_cant_find_directory_str_in_cdpath), *file_to_find); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1810 | else |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1811 | semsg(_(e_cant_find_file_str_in_path), *file_to_find); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1812 | } |
| 1813 | else |
| 1814 | { |
| 1815 | if (find_what == FINDFILE_DIR) |
Bram Moolenaar | eaaac01 | 2022-01-02 17:00:40 +0000 | [diff] [blame] | 1816 | semsg(_(e_no_more_directory_str_found_in_cdpath), |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1817 | *file_to_find); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1818 | else |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 1819 | semsg(_(e_no_more_file_str_found_in_path), *file_to_find); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1820 | } |
| 1821 | } |
| 1822 | |
| 1823 | theend: |
| 1824 | # ifdef AMIGA |
| 1825 | proc->pr_WindowPtr = save_winptr; |
| 1826 | # endif |
| 1827 | return file_name; |
| 1828 | } |
| 1829 | |
| 1830 | /* |
| 1831 | * Get the file name at the cursor. |
| 1832 | * If Visual mode is active, use the selected text if it's in one line. |
| 1833 | * Returns the name in allocated memory, NULL for failure. |
| 1834 | */ |
| 1835 | char_u * |
| 1836 | grab_file_name(long count, linenr_T *file_lnum) |
| 1837 | { |
| 1838 | int options = FNAME_MESS|FNAME_EXP|FNAME_REL|FNAME_UNESC; |
| 1839 | |
| 1840 | if (VIsual_active) |
| 1841 | { |
| 1842 | int len; |
| 1843 | char_u *ptr; |
| 1844 | |
| 1845 | if (get_visual_text(NULL, &ptr, &len) == FAIL) |
| 1846 | return NULL; |
Bram Moolenaar | efd5d8a | 2020-09-14 19:11:45 +0200 | [diff] [blame] | 1847 | // Only recognize ":123" here |
Keith Thompson | 184f71c | 2024-01-04 21:19:04 +0100 | [diff] [blame] | 1848 | if (file_lnum != NULL && ptr[len] == ':' && SAFE_isdigit(ptr[len + 1])) |
Bram Moolenaar | efd5d8a | 2020-09-14 19:11:45 +0200 | [diff] [blame] | 1849 | { |
| 1850 | char_u *p = ptr + len + 1; |
| 1851 | |
| 1852 | *file_lnum = getdigits(&p); |
| 1853 | } |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1854 | return find_file_name_in_path(ptr, len, options, |
| 1855 | count, curbuf->b_ffname); |
| 1856 | } |
| 1857 | return file_name_at_cursor(options | FNAME_HYP, count, file_lnum); |
| 1858 | } |
| 1859 | |
| 1860 | /* |
| 1861 | * Return the file name under or after the cursor. |
| 1862 | * |
| 1863 | * The 'path' option is searched if the file name is not absolute. |
| 1864 | * The string returned has been alloc'ed and should be freed by the caller. |
| 1865 | * NULL is returned if the file name or file is not found. |
| 1866 | * |
| 1867 | * options: |
| 1868 | * FNAME_MESS give error messages |
| 1869 | * FNAME_EXP expand to path |
| 1870 | * FNAME_HYP check for hypertext link |
| 1871 | * FNAME_INCL apply "includeexpr" |
| 1872 | */ |
| 1873 | char_u * |
| 1874 | file_name_at_cursor(int options, long count, linenr_T *file_lnum) |
| 1875 | { |
| 1876 | return file_name_in_line(ml_get_curline(), |
| 1877 | curwin->w_cursor.col, options, count, curbuf->b_ffname, |
| 1878 | file_lnum); |
| 1879 | } |
| 1880 | |
| 1881 | /* |
| 1882 | * Return the name of the file under or after ptr[col]. |
| 1883 | * Otherwise like file_name_at_cursor(). |
| 1884 | */ |
| 1885 | char_u * |
| 1886 | file_name_in_line( |
| 1887 | char_u *line, |
| 1888 | int col, |
| 1889 | int options, |
| 1890 | long count, |
| 1891 | char_u *rel_fname, // file we are searching relative to |
| 1892 | linenr_T *file_lnum) // line number after the file name |
| 1893 | { |
| 1894 | char_u *ptr; |
| 1895 | int len; |
| 1896 | int in_type = TRUE; |
| 1897 | int is_url = FALSE; |
| 1898 | |
| 1899 | /* |
| 1900 | * search forward for what could be the start of a file name |
| 1901 | */ |
| 1902 | ptr = line + col; |
| 1903 | while (*ptr != NUL && !vim_isfilec(*ptr)) |
| 1904 | MB_PTR_ADV(ptr); |
| 1905 | if (*ptr == NUL) // nothing found |
| 1906 | { |
| 1907 | if (options & FNAME_MESS) |
Bram Moolenaar | ac78dd4 | 2022-01-02 19:25:26 +0000 | [diff] [blame] | 1908 | emsg(_(e_no_file_name_under_cursor)); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1909 | return NULL; |
| 1910 | } |
| 1911 | |
| 1912 | /* |
| 1913 | * Search backward for first char of the file name. |
| 1914 | * Go one char back to ":" before "//" even when ':' is not in 'isfname'. |
| 1915 | */ |
| 1916 | while (ptr > line) |
| 1917 | { |
| 1918 | if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0) |
| 1919 | ptr -= len + 1; |
| 1920 | else if (vim_isfilec(ptr[-1]) |
| 1921 | || ((options & FNAME_HYP) && path_is_url(ptr - 1))) |
| 1922 | --ptr; |
| 1923 | else |
| 1924 | break; |
| 1925 | } |
| 1926 | |
| 1927 | /* |
| 1928 | * Search forward for the last char of the file name. |
| 1929 | * Also allow "://" when ':' is not in 'isfname'. |
| 1930 | */ |
| 1931 | len = 0; |
| 1932 | while (vim_isfilec(ptr[len]) || (ptr[len] == '\\' && ptr[len + 1] == ' ') |
Bram Moolenaar | 747f110 | 2022-09-18 13:06:41 +0100 | [diff] [blame] | 1933 | || ((options & FNAME_HYP) && path_is_url(ptr + len)) |
| 1934 | || (is_url && vim_strchr((char_u *)":?&=", ptr[len]) != NULL)) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1935 | { |
Bram Moolenaar | 747f110 | 2022-09-18 13:06:41 +0100 | [diff] [blame] | 1936 | // After type:// we also include :, ?, & and = as valid characters, so |
| 1937 | // that http://google.com:8080?q=this&that=ok works. |
| 1938 | if ((ptr[len] >= 'A' && ptr[len] <= 'Z') |
| 1939 | || (ptr[len] >= 'a' && ptr[len] <= 'z')) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1940 | { |
| 1941 | if (in_type && path_is_url(ptr + len + 1)) |
| 1942 | is_url = TRUE; |
| 1943 | } |
| 1944 | else |
| 1945 | in_type = FALSE; |
| 1946 | |
| 1947 | if (ptr[len] == '\\') |
| 1948 | // Skip over the "\" in "\ ". |
| 1949 | ++len; |
| 1950 | if (has_mbyte) |
| 1951 | len += (*mb_ptr2len)(ptr + len); |
| 1952 | else |
| 1953 | ++len; |
| 1954 | } |
| 1955 | |
| 1956 | /* |
| 1957 | * If there is trailing punctuation, remove it. |
| 1958 | * But don't remove "..", could be a directory name. |
| 1959 | */ |
| 1960 | if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL |
| 1961 | && ptr[len - 2] != '.') |
| 1962 | --len; |
| 1963 | |
| 1964 | if (file_lnum != NULL) |
| 1965 | { |
| 1966 | char_u *p; |
Bram Moolenaar | 64e74c9 | 2019-12-22 15:38:06 +0100 | [diff] [blame] | 1967 | char *line_english = " line "; |
| 1968 | char *line_transl = _(line_msg); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1969 | |
Bram Moolenaar | 64e74c9 | 2019-12-22 15:38:06 +0100 | [diff] [blame] | 1970 | // Get the number after the file name and a separator character. |
| 1971 | // Also accept " line 999" with and without the same translation as |
| 1972 | // used in last_set_msg(). |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1973 | p = ptr + len; |
Bram Moolenaar | 64e74c9 | 2019-12-22 15:38:06 +0100 | [diff] [blame] | 1974 | if (STRNCMP(p, line_english, STRLEN(line_english)) == 0) |
| 1975 | p += STRLEN(line_english); |
| 1976 | else if (STRNCMP(p, line_transl, STRLEN(line_transl)) == 0) |
| 1977 | p += STRLEN(line_transl); |
| 1978 | else |
| 1979 | p = skipwhite(p); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1980 | if (*p != NUL) |
| 1981 | { |
Keith Thompson | 184f71c | 2024-01-04 21:19:04 +0100 | [diff] [blame] | 1982 | if (!SAFE_isdigit(*p)) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1983 | ++p; // skip the separator |
| 1984 | p = skipwhite(p); |
Keith Thompson | 184f71c | 2024-01-04 21:19:04 +0100 | [diff] [blame] | 1985 | if (SAFE_isdigit(*p)) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1986 | *file_lnum = (int)getdigits(&p); |
| 1987 | } |
| 1988 | } |
| 1989 | |
| 1990 | return find_file_name_in_path(ptr, len, options, count, rel_fname); |
| 1991 | } |
| 1992 | |
| 1993 | # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL) |
| 1994 | static char_u * |
| 1995 | eval_includeexpr(char_u *ptr, int len) |
| 1996 | { |
| 1997 | char_u *res; |
Bram Moolenaar | 47bcc5f | 2022-01-22 20:19:22 +0000 | [diff] [blame] | 1998 | sctx_T save_sctx = current_sctx; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 1999 | |
| 2000 | set_vim_var_string(VV_FNAME, ptr, len); |
Bram Moolenaar | 47bcc5f | 2022-01-22 20:19:22 +0000 | [diff] [blame] | 2001 | current_sctx = curbuf->b_p_script_ctx[BV_INEX]; |
| 2002 | |
Bram Moolenaar | b171fb1 | 2020-06-24 20:34:03 +0200 | [diff] [blame] | 2003 | res = eval_to_string_safe(curbuf->b_p_inex, |
Bram Moolenaar | a4e0b97 | 2022-10-01 19:43:52 +0100 | [diff] [blame] | 2004 | was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL), |
| 2005 | TRUE, TRUE); |
Bram Moolenaar | 47bcc5f | 2022-01-22 20:19:22 +0000 | [diff] [blame] | 2006 | |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 2007 | set_vim_var_string(VV_FNAME, NULL, 0); |
Bram Moolenaar | 47bcc5f | 2022-01-22 20:19:22 +0000 | [diff] [blame] | 2008 | current_sctx = save_sctx; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 2009 | return res; |
| 2010 | } |
| 2011 | # endif |
| 2012 | |
| 2013 | /* |
| 2014 | * Return the name of the file ptr[len] in 'path'. |
| 2015 | * Otherwise like file_name_at_cursor(). |
| 2016 | */ |
| 2017 | char_u * |
| 2018 | find_file_name_in_path( |
| 2019 | char_u *ptr, |
| 2020 | int len, |
| 2021 | int options, |
| 2022 | long count, |
| 2023 | char_u *rel_fname) // file we are searching relative to |
| 2024 | { |
| 2025 | char_u *file_name; |
| 2026 | int c; |
| 2027 | # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL) |
| 2028 | char_u *tofree = NULL; |
Bram Moolenaar | 615ddd5 | 2021-11-17 18:00:31 +0000 | [diff] [blame] | 2029 | # endif |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 2030 | |
Bram Moolenaar | 615ddd5 | 2021-11-17 18:00:31 +0000 | [diff] [blame] | 2031 | if (len == 0) |
| 2032 | return NULL; |
| 2033 | |
| 2034 | # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 2035 | if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL) |
| 2036 | { |
| 2037 | tofree = eval_includeexpr(ptr, len); |
| 2038 | if (tofree != NULL) |
| 2039 | { |
| 2040 | ptr = tofree; |
| 2041 | len = (int)STRLEN(ptr); |
| 2042 | } |
| 2043 | } |
| 2044 | # endif |
| 2045 | |
| 2046 | if (options & FNAME_EXP) |
| 2047 | { |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 2048 | char_u *file_to_find = NULL; |
| 2049 | char *search_ctx = NULL; |
| 2050 | |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 2051 | file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS, |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 2052 | TRUE, rel_fname, &file_to_find, &search_ctx); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 2053 | |
| 2054 | # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL) |
| 2055 | /* |
| 2056 | * If the file could not be found in a normal way, try applying |
| 2057 | * 'includeexpr' (unless done already). |
| 2058 | */ |
| 2059 | if (file_name == NULL |
| 2060 | && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL) |
| 2061 | { |
| 2062 | tofree = eval_includeexpr(ptr, len); |
| 2063 | if (tofree != NULL) |
| 2064 | { |
| 2065 | ptr = tofree; |
| 2066 | len = (int)STRLEN(ptr); |
| 2067 | file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS, |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 2068 | TRUE, rel_fname, &file_to_find, &search_ctx); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 2069 | } |
| 2070 | } |
| 2071 | # endif |
| 2072 | if (file_name == NULL && (options & FNAME_MESS)) |
| 2073 | { |
| 2074 | c = ptr[len]; |
| 2075 | ptr[len] = NUL; |
Bram Moolenaar | ac78dd4 | 2022-01-02 19:25:26 +0000 | [diff] [blame] | 2076 | semsg(_(e_cant_find_file_str_in_path_2), ptr); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 2077 | ptr[len] = c; |
| 2078 | } |
| 2079 | |
| 2080 | // Repeat finding the file "count" times. This matters when it |
| 2081 | // appears several times in the path. |
| 2082 | while (file_name != NULL && --count > 0) |
| 2083 | { |
| 2084 | vim_free(file_name); |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 2085 | file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname, |
| 2086 | &file_to_find, &search_ctx); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 2087 | } |
Bram Moolenaar | 5145c9a | 2023-03-11 13:55:53 +0000 | [diff] [blame] | 2088 | |
| 2089 | vim_free(file_to_find); |
| 2090 | vim_findfile_cleanup(search_ctx); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 2091 | } |
| 2092 | else |
| 2093 | file_name = vim_strnsave(ptr, len); |
| 2094 | |
| 2095 | # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL) |
| 2096 | vim_free(tofree); |
| 2097 | # endif |
| 2098 | |
| 2099 | return file_name; |
| 2100 | } |
| 2101 | |
| 2102 | /* |
| 2103 | * Return the end of the directory name, on the first path |
| 2104 | * separator: |
| 2105 | * "/path/file", "/path/dir/", "/path//dir", "/file" |
| 2106 | * ^ ^ ^ ^ |
| 2107 | */ |
| 2108 | static char_u * |
| 2109 | gettail_dir(char_u *fname) |
| 2110 | { |
| 2111 | char_u *dir_end = fname; |
| 2112 | char_u *next_dir_end = fname; |
| 2113 | int look_for_sep = TRUE; |
| 2114 | char_u *p; |
| 2115 | |
| 2116 | for (p = fname; *p != NUL; ) |
| 2117 | { |
| 2118 | if (vim_ispathsep(*p)) |
| 2119 | { |
| 2120 | if (look_for_sep) |
| 2121 | { |
| 2122 | next_dir_end = p; |
| 2123 | look_for_sep = FALSE; |
| 2124 | } |
| 2125 | } |
| 2126 | else |
| 2127 | { |
| 2128 | if (!look_for_sep) |
| 2129 | dir_end = next_dir_end; |
| 2130 | look_for_sep = TRUE; |
| 2131 | } |
| 2132 | MB_PTR_ADV(p); |
| 2133 | } |
| 2134 | return dir_end; |
| 2135 | } |
| 2136 | |
| 2137 | /* |
| 2138 | * return TRUE if 'c' is a path list separator. |
| 2139 | */ |
| 2140 | int |
| 2141 | vim_ispathlistsep(int c) |
| 2142 | { |
| 2143 | # ifdef UNIX |
| 2144 | return (c == ':'); |
| 2145 | # else |
| 2146 | return (c == ';'); // might not be right for every system... |
| 2147 | # endif |
| 2148 | } |
| 2149 | |
| 2150 | /* |
| 2151 | * Moves "*psep" back to the previous path separator in "path". |
| 2152 | * Returns FAIL is "*psep" ends up at the beginning of "path". |
| 2153 | */ |
| 2154 | static int |
| 2155 | find_previous_pathsep(char_u *path, char_u **psep) |
| 2156 | { |
| 2157 | // skip the current separator |
| 2158 | if (*psep > path && vim_ispathsep(**psep)) |
| 2159 | --*psep; |
| 2160 | |
| 2161 | // find the previous separator |
| 2162 | while (*psep > path) |
| 2163 | { |
| 2164 | if (vim_ispathsep(**psep)) |
| 2165 | return OK; |
| 2166 | MB_PTR_BACK(path, *psep); |
| 2167 | } |
| 2168 | |
| 2169 | return FAIL; |
| 2170 | } |
| 2171 | |
| 2172 | /* |
| 2173 | * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap". |
| 2174 | * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]". |
| 2175 | */ |
| 2176 | static int |
| 2177 | is_unique(char_u *maybe_unique, garray_T *gap, int i) |
| 2178 | { |
| 2179 | int j; |
| 2180 | int candidate_len; |
| 2181 | int other_path_len; |
| 2182 | char_u **other_paths = (char_u **)gap->ga_data; |
| 2183 | char_u *rival; |
| 2184 | |
| 2185 | for (j = 0; j < gap->ga_len; j++) |
| 2186 | { |
| 2187 | if (j == i) |
| 2188 | continue; // don't compare it with itself |
| 2189 | |
| 2190 | candidate_len = (int)STRLEN(maybe_unique); |
| 2191 | other_path_len = (int)STRLEN(other_paths[j]); |
| 2192 | if (other_path_len < candidate_len) |
| 2193 | continue; // it's different when it's shorter |
| 2194 | |
| 2195 | rival = other_paths[j] + other_path_len - candidate_len; |
| 2196 | if (fnamecmp(maybe_unique, rival) == 0 |
| 2197 | && (rival == other_paths[j] || vim_ispathsep(*(rival - 1)))) |
| 2198 | return FALSE; // match |
| 2199 | } |
| 2200 | |
| 2201 | return TRUE; // no match found |
| 2202 | } |
| 2203 | |
| 2204 | /* |
| 2205 | * Split the 'path' option into an array of strings in garray_T. Relative |
| 2206 | * paths are expanded to their equivalent fullpath. This includes the "." |
| 2207 | * (relative to current buffer directory) and empty path (relative to current |
| 2208 | * directory) notations. |
| 2209 | * |
| 2210 | * TODO: handle upward search (;) and path limiter (**N) notations by |
| 2211 | * expanding each into their equivalent path(s). |
| 2212 | */ |
| 2213 | static void |
LemonBoy | a20bf69 | 2024-07-11 22:35:53 +0200 | [diff] [blame] | 2214 | expand_path_option( |
| 2215 | char_u *curdir, |
| 2216 | char_u *path_option, // p_path or p_cdpath |
| 2217 | garray_T *gap) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 2218 | { |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 2219 | char_u *buf; |
| 2220 | char_u *p; |
| 2221 | int len; |
| 2222 | |
Bram Moolenaar | 51e1438 | 2019-05-25 20:21:28 +0200 | [diff] [blame] | 2223 | if ((buf = alloc(MAXPATHL)) == NULL) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 2224 | return; |
| 2225 | |
| 2226 | while (*path_option != NUL) |
| 2227 | { |
| 2228 | copy_option_part(&path_option, buf, MAXPATHL, " ,"); |
| 2229 | |
| 2230 | if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1]))) |
| 2231 | { |
| 2232 | // Relative to current buffer: |
| 2233 | // "/path/file" + "." -> "/path/" |
| 2234 | // "/path/file" + "./subdir" -> "/path/subdir" |
| 2235 | if (curbuf->b_ffname == NULL) |
| 2236 | continue; |
| 2237 | p = gettail(curbuf->b_ffname); |
| 2238 | len = (int)(p - curbuf->b_ffname); |
| 2239 | if (len + (int)STRLEN(buf) >= MAXPATHL) |
| 2240 | continue; |
| 2241 | if (buf[1] == NUL) |
| 2242 | buf[len] = NUL; |
| 2243 | else |
| 2244 | STRMOVE(buf + len, buf + 2); |
| 2245 | mch_memmove(buf, curbuf->b_ffname, len); |
| 2246 | simplify_filename(buf); |
| 2247 | } |
| 2248 | else if (buf[0] == NUL) |
| 2249 | // relative to current directory |
| 2250 | STRCPY(buf, curdir); |
| 2251 | else if (path_with_url(buf)) |
| 2252 | // URL can't be used here |
| 2253 | continue; |
| 2254 | else if (!mch_isFullName(buf)) |
| 2255 | { |
| 2256 | // Expand relative path to their full path equivalent |
| 2257 | len = (int)STRLEN(curdir); |
| 2258 | if (len + (int)STRLEN(buf) + 3 > MAXPATHL) |
| 2259 | continue; |
| 2260 | STRMOVE(buf + len + 1, buf); |
| 2261 | STRCPY(buf, curdir); |
| 2262 | buf[len] = PATHSEP; |
| 2263 | simplify_filename(buf); |
| 2264 | } |
| 2265 | |
| 2266 | if (ga_grow(gap, 1) == FAIL) |
| 2267 | break; |
| 2268 | |
| 2269 | # if defined(MSWIN) |
| 2270 | // Avoid the path ending in a backslash, it fails when a comma is |
| 2271 | // appended. |
| 2272 | len = (int)STRLEN(buf); |
| 2273 | if (buf[len - 1] == '\\') |
| 2274 | buf[len - 1] = '/'; |
| 2275 | # endif |
| 2276 | |
| 2277 | p = vim_strsave(buf); |
| 2278 | if (p == NULL) |
| 2279 | break; |
| 2280 | ((char_u **)gap->ga_data)[gap->ga_len++] = p; |
| 2281 | } |
| 2282 | |
| 2283 | vim_free(buf); |
| 2284 | } |
| 2285 | |
| 2286 | /* |
| 2287 | * Returns a pointer to the file or directory name in "fname" that matches the |
| 2288 | * longest path in "ga"p, or NULL if there is no match. For example: |
| 2289 | * |
| 2290 | * path: /foo/bar/baz |
| 2291 | * fname: /foo/bar/baz/quux.txt |
| 2292 | * returns: ^this |
| 2293 | */ |
| 2294 | static char_u * |
| 2295 | get_path_cutoff(char_u *fname, garray_T *gap) |
| 2296 | { |
| 2297 | int i; |
| 2298 | int maxlen = 0; |
| 2299 | char_u **path_part = (char_u **)gap->ga_data; |
| 2300 | char_u *cutoff = NULL; |
| 2301 | |
| 2302 | for (i = 0; i < gap->ga_len; i++) |
| 2303 | { |
| 2304 | int j = 0; |
| 2305 | |
| 2306 | while ((fname[j] == path_part[i][j] |
| 2307 | # if defined(MSWIN) |
| 2308 | || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j])) |
| 2309 | # endif |
| 2310 | ) && fname[j] != NUL && path_part[i][j] != NUL) |
| 2311 | j++; |
| 2312 | if (j > maxlen) |
| 2313 | { |
| 2314 | maxlen = j; |
| 2315 | cutoff = &fname[j]; |
| 2316 | } |
| 2317 | } |
| 2318 | |
| 2319 | // skip to the file or directory name |
| 2320 | if (cutoff != NULL) |
| 2321 | while (vim_ispathsep(*cutoff)) |
| 2322 | MB_PTR_ADV(cutoff); |
| 2323 | |
| 2324 | return cutoff; |
| 2325 | } |
| 2326 | |
| 2327 | /* |
| 2328 | * Sorts, removes duplicates and modifies all the fullpath names in "gap" so |
| 2329 | * that they are unique with respect to each other while conserving the part |
| 2330 | * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len". |
| 2331 | */ |
| 2332 | void |
LemonBoy | a20bf69 | 2024-07-11 22:35:53 +0200 | [diff] [blame] | 2333 | uniquefy_paths( |
| 2334 | garray_T *gap, |
| 2335 | char_u *pattern, |
| 2336 | char_u *path_option) // p_path or p_cdpath |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 2337 | { |
| 2338 | int i; |
| 2339 | int len; |
| 2340 | char_u **fnames = (char_u **)gap->ga_data; |
| 2341 | int sort_again = FALSE; |
| 2342 | char_u *pat; |
| 2343 | char_u *file_pattern; |
| 2344 | char_u *curdir; |
| 2345 | regmatch_T regmatch; |
| 2346 | garray_T path_ga; |
| 2347 | char_u **in_curdir = NULL; |
| 2348 | char_u *short_name; |
| 2349 | |
| 2350 | remove_duplicates(gap); |
Bram Moolenaar | 04935fb | 2022-01-08 16:19:22 +0000 | [diff] [blame] | 2351 | ga_init2(&path_ga, sizeof(char_u *), 1); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 2352 | |
| 2353 | /* |
| 2354 | * We need to prepend a '*' at the beginning of file_pattern so that the |
| 2355 | * regex matches anywhere in the path. FIXME: is this valid for all |
| 2356 | * possible patterns? |
| 2357 | */ |
| 2358 | len = (int)STRLEN(pattern); |
| 2359 | file_pattern = alloc(len + 2); |
| 2360 | if (file_pattern == NULL) |
| 2361 | return; |
| 2362 | file_pattern[0] = '*'; |
| 2363 | file_pattern[1] = NUL; |
| 2364 | STRCAT(file_pattern, pattern); |
Christian Brabandt | 1a31c43 | 2024-10-06 16:34:20 +0200 | [diff] [blame] | 2365 | pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, FALSE); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 2366 | vim_free(file_pattern); |
| 2367 | if (pat == NULL) |
| 2368 | return; |
| 2369 | |
| 2370 | regmatch.rm_ic = TRUE; // always ignore case |
| 2371 | regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING); |
| 2372 | vim_free(pat); |
| 2373 | if (regmatch.regprog == NULL) |
| 2374 | return; |
| 2375 | |
Bram Moolenaar | 51e1438 | 2019-05-25 20:21:28 +0200 | [diff] [blame] | 2376 | if ((curdir = alloc(MAXPATHL)) == NULL) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 2377 | goto theend; |
| 2378 | mch_dirname(curdir, MAXPATHL); |
LemonBoy | a20bf69 | 2024-07-11 22:35:53 +0200 | [diff] [blame] | 2379 | expand_path_option(curdir, path_option, &path_ga); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 2380 | |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 2381 | in_curdir = ALLOC_CLEAR_MULT(char_u *, gap->ga_len); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 2382 | if (in_curdir == NULL) |
| 2383 | goto theend; |
| 2384 | |
| 2385 | for (i = 0; i < gap->ga_len && !got_int; i++) |
| 2386 | { |
| 2387 | char_u *path = fnames[i]; |
| 2388 | int is_in_curdir; |
| 2389 | char_u *dir_end = gettail_dir(path); |
| 2390 | char_u *pathsep_p; |
| 2391 | char_u *path_cutoff; |
| 2392 | |
| 2393 | len = (int)STRLEN(path); |
| 2394 | is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0 |
| 2395 | && curdir[dir_end - path] == NUL; |
| 2396 | if (is_in_curdir) |
| 2397 | in_curdir[i] = vim_strsave(path); |
| 2398 | |
| 2399 | // Shorten the filename while maintaining its uniqueness |
| 2400 | path_cutoff = get_path_cutoff(path, &path_ga); |
| 2401 | |
| 2402 | // Don't assume all files can be reached without path when search |
| 2403 | // pattern starts with star star slash, so only remove path_cutoff |
| 2404 | // when possible. |
| 2405 | if (pattern[0] == '*' && pattern[1] == '*' |
| 2406 | && vim_ispathsep_nocolon(pattern[2]) |
| 2407 | && path_cutoff != NULL |
| 2408 | && vim_regexec(®match, path_cutoff, (colnr_T)0) |
| 2409 | && is_unique(path_cutoff, gap, i)) |
| 2410 | { |
| 2411 | sort_again = TRUE; |
| 2412 | mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1); |
| 2413 | } |
| 2414 | else |
| 2415 | { |
| 2416 | // Here all files can be reached without path, so get shortest |
| 2417 | // unique path. We start at the end of the path. |
| 2418 | pathsep_p = path + len - 1; |
| 2419 | |
| 2420 | while (find_previous_pathsep(path, &pathsep_p)) |
| 2421 | if (vim_regexec(®match, pathsep_p + 1, (colnr_T)0) |
| 2422 | && is_unique(pathsep_p + 1, gap, i) |
| 2423 | && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff) |
| 2424 | { |
| 2425 | sort_again = TRUE; |
| 2426 | mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p)); |
| 2427 | break; |
| 2428 | } |
| 2429 | } |
| 2430 | |
| 2431 | if (mch_isFullName(path)) |
| 2432 | { |
| 2433 | /* |
| 2434 | * Last resort: shorten relative to curdir if possible. |
| 2435 | * 'possible' means: |
| 2436 | * 1. It is under the current directory. |
| 2437 | * 2. The result is actually shorter than the original. |
| 2438 | * |
| 2439 | * Before curdir After |
| 2440 | * /foo/bar/file.txt /foo/bar ./file.txt |
| 2441 | * c:\foo\bar\file.txt c:\foo\bar .\file.txt |
| 2442 | * /file.txt / /file.txt |
| 2443 | * c:\file.txt c:\ .\file.txt |
| 2444 | */ |
| 2445 | short_name = shorten_fname(path, curdir); |
| 2446 | if (short_name != NULL && short_name > path + 1 |
| 2447 | # if defined(MSWIN) |
| 2448 | // On windows, |
| 2449 | // shorten_fname("c:\a\a.txt", "c:\a\b") |
| 2450 | // returns "\a\a.txt", which is not really the short |
| 2451 | // name, hence: |
| 2452 | && !vim_ispathsep(*short_name) |
| 2453 | # endif |
| 2454 | ) |
| 2455 | { |
| 2456 | STRCPY(path, "."); |
| 2457 | add_pathsep(path); |
| 2458 | STRMOVE(path + STRLEN(path), short_name); |
| 2459 | } |
| 2460 | } |
| 2461 | ui_breakcheck(); |
| 2462 | } |
| 2463 | |
| 2464 | // Shorten filenames in /in/current/directory/{filename} |
| 2465 | for (i = 0; i < gap->ga_len && !got_int; i++) |
| 2466 | { |
| 2467 | char_u *rel_path; |
| 2468 | char_u *path = in_curdir[i]; |
| 2469 | |
| 2470 | if (path == NULL) |
| 2471 | continue; |
| 2472 | |
| 2473 | // If the {filename} is not unique, change it to ./{filename}. |
| 2474 | // Else reduce it to {filename} |
| 2475 | short_name = shorten_fname(path, curdir); |
| 2476 | if (short_name == NULL) |
| 2477 | short_name = path; |
| 2478 | if (is_unique(short_name, gap, i)) |
| 2479 | { |
| 2480 | STRCPY(fnames[i], short_name); |
| 2481 | continue; |
| 2482 | } |
| 2483 | |
Bram Moolenaar | 51e1438 | 2019-05-25 20:21:28 +0200 | [diff] [blame] | 2484 | rel_path = alloc(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 2485 | if (rel_path == NULL) |
| 2486 | goto theend; |
| 2487 | STRCPY(rel_path, "."); |
| 2488 | add_pathsep(rel_path); |
| 2489 | STRCAT(rel_path, short_name); |
| 2490 | |
| 2491 | vim_free(fnames[i]); |
| 2492 | fnames[i] = rel_path; |
| 2493 | sort_again = TRUE; |
| 2494 | ui_breakcheck(); |
| 2495 | } |
| 2496 | |
| 2497 | theend: |
| 2498 | vim_free(curdir); |
| 2499 | if (in_curdir != NULL) |
| 2500 | { |
| 2501 | for (i = 0; i < gap->ga_len; i++) |
| 2502 | vim_free(in_curdir[i]); |
| 2503 | vim_free(in_curdir); |
| 2504 | } |
| 2505 | ga_clear_strings(&path_ga); |
| 2506 | vim_regfree(regmatch.regprog); |
| 2507 | |
| 2508 | if (sort_again) |
| 2509 | remove_duplicates(gap); |
| 2510 | } |
| 2511 | |
| 2512 | /* |
| 2513 | * Calls globpath() with 'path' values for the given pattern and stores the |
| 2514 | * result in "gap". |
| 2515 | * Returns the total number of matches. |
| 2516 | */ |
| 2517 | int |
| 2518 | expand_in_path( |
| 2519 | garray_T *gap, |
| 2520 | char_u *pattern, |
| 2521 | int flags) // EW_* flags |
| 2522 | { |
| 2523 | char_u *curdir; |
| 2524 | garray_T path_ga; |
| 2525 | char_u *paths = NULL; |
| 2526 | int glob_flags = 0; |
LemonBoy | a20bf69 | 2024-07-11 22:35:53 +0200 | [diff] [blame] | 2527 | char_u *path_option = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path; |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 2528 | |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 2529 | if ((curdir = alloc(MAXPATHL)) == NULL) |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 2530 | return 0; |
| 2531 | mch_dirname(curdir, MAXPATHL); |
| 2532 | |
Bram Moolenaar | 04935fb | 2022-01-08 16:19:22 +0000 | [diff] [blame] | 2533 | ga_init2(&path_ga, sizeof(char_u *), 1); |
LemonBoy | a20bf69 | 2024-07-11 22:35:53 +0200 | [diff] [blame] | 2534 | if (flags & EW_CDPATH) |
| 2535 | expand_path_option(curdir, p_cdpath, &path_ga); |
| 2536 | else |
| 2537 | expand_path_option(curdir, path_option, &path_ga); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 2538 | vim_free(curdir); |
| 2539 | if (path_ga.ga_len == 0) |
| 2540 | return 0; |
| 2541 | |
| 2542 | paths = ga_concat_strings(&path_ga, ","); |
| 2543 | ga_clear_strings(&path_ga); |
| 2544 | if (paths == NULL) |
| 2545 | return 0; |
| 2546 | |
| 2547 | if (flags & EW_ICASE) |
| 2548 | glob_flags |= WILD_ICASE; |
| 2549 | if (flags & EW_ADDSLASH) |
| 2550 | glob_flags |= WILD_ADD_SLASH; |
LemonBoy | a20bf69 | 2024-07-11 22:35:53 +0200 | [diff] [blame] | 2551 | globpath(paths, pattern, gap, glob_flags, !!(flags & EW_CDPATH)); |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 2552 | vim_free(paths); |
| 2553 | |
| 2554 | return gap->ga_len; |
| 2555 | } |
| 2556 | |
Bram Moolenaar | b4a6020 | 2019-03-31 19:40:07 +0200 | [diff] [blame] | 2557 | |
| 2558 | /* |
| 2559 | * Converts a file name into a canonical form. It simplifies a file name into |
| 2560 | * its simplest form by stripping out unneeded components, if any. The |
| 2561 | * resulting file name is simplified in place and will either be the same |
| 2562 | * length as that supplied, or shorter. |
| 2563 | */ |
| 2564 | void |
| 2565 | simplify_filename(char_u *filename) |
| 2566 | { |
| 2567 | #ifndef AMIGA // Amiga doesn't have "..", it uses "/" |
| 2568 | int components = 0; |
| 2569 | char_u *p, *tail, *start; |
| 2570 | int stripping_disabled = FALSE; |
| 2571 | int relative = TRUE; |
| 2572 | |
| 2573 | p = filename; |
| 2574 | # ifdef BACKSLASH_IN_FILENAME |
Yegappan Lakshmanan | 6df0f27 | 2021-12-16 13:06:10 +0000 | [diff] [blame] | 2575 | if (p[0] != NUL && p[1] == ':') // skip "x:" |
Bram Moolenaar | b4a6020 | 2019-03-31 19:40:07 +0200 | [diff] [blame] | 2576 | p += 2; |
| 2577 | # endif |
| 2578 | |
| 2579 | if (vim_ispathsep(*p)) |
| 2580 | { |
| 2581 | relative = FALSE; |
| 2582 | do |
| 2583 | ++p; |
| 2584 | while (vim_ispathsep(*p)); |
| 2585 | } |
| 2586 | start = p; // remember start after "c:/" or "/" or "///" |
Bram Moolenaar | fdcbe3c | 2020-06-15 21:41:56 +0200 | [diff] [blame] | 2587 | #ifdef UNIX |
| 2588 | // Posix says that "//path" is unchanged but "///path" is "/path". |
| 2589 | if (start > filename + 2) |
| 2590 | { |
| 2591 | STRMOVE(filename + 1, p); |
| 2592 | start = p = filename + 1; |
| 2593 | } |
| 2594 | #endif |
Bram Moolenaar | b4a6020 | 2019-03-31 19:40:07 +0200 | [diff] [blame] | 2595 | |
| 2596 | do |
| 2597 | { |
| 2598 | // At this point "p" is pointing to the char following a single "/" |
| 2599 | // or "p" is at the "start" of the (absolute or relative) path name. |
| 2600 | # ifdef VMS |
| 2601 | // VMS allows device:[path] - don't strip the [ in directory |
| 2602 | if ((*p == '[' || *p == '<') && p > filename && p[-1] == ':') |
| 2603 | { |
| 2604 | // :[ or :< composition: vms directory component |
| 2605 | ++components; |
| 2606 | p = getnextcomp(p + 1); |
| 2607 | } |
| 2608 | // allow remote calls as host"user passwd"::device:[path] |
| 2609 | else if (p[0] == ':' && p[1] == ':' && p > filename && p[-1] == '"' ) |
| 2610 | { |
| 2611 | // ":: composition: vms host/passwd component |
| 2612 | ++components; |
| 2613 | p = getnextcomp(p + 2); |
| 2614 | } |
| 2615 | else |
| 2616 | # endif |
| 2617 | if (vim_ispathsep(*p)) |
| 2618 | STRMOVE(p, p + 1); // remove duplicate "/" |
| 2619 | else if (p[0] == '.' && (vim_ispathsep(p[1]) || p[1] == NUL)) |
| 2620 | { |
| 2621 | if (p == start && relative) |
| 2622 | p += 1 + (p[1] != NUL); // keep single "." or leading "./" |
| 2623 | else |
| 2624 | { |
| 2625 | // Strip "./" or ".///". If we are at the end of the file name |
| 2626 | // and there is no trailing path separator, either strip "/." if |
| 2627 | // we are after "start", or strip "." if we are at the beginning |
| 2628 | // of an absolute path name . |
| 2629 | tail = p + 1; |
| 2630 | if (p[1] != NUL) |
| 2631 | while (vim_ispathsep(*tail)) |
| 2632 | MB_PTR_ADV(tail); |
| 2633 | else if (p > start) |
| 2634 | --p; // strip preceding path separator |
| 2635 | STRMOVE(p, tail); |
| 2636 | } |
| 2637 | } |
| 2638 | else if (p[0] == '.' && p[1] == '.' && |
| 2639 | (vim_ispathsep(p[2]) || p[2] == NUL)) |
| 2640 | { |
| 2641 | // Skip to after ".." or "../" or "..///". |
| 2642 | tail = p + 2; |
| 2643 | while (vim_ispathsep(*tail)) |
| 2644 | MB_PTR_ADV(tail); |
| 2645 | |
| 2646 | if (components > 0) // strip one preceding component |
| 2647 | { |
| 2648 | int do_strip = FALSE; |
| 2649 | char_u saved_char; |
| 2650 | stat_T st; |
| 2651 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2652 | // Don't strip for an erroneous file name. |
Bram Moolenaar | b4a6020 | 2019-03-31 19:40:07 +0200 | [diff] [blame] | 2653 | if (!stripping_disabled) |
| 2654 | { |
| 2655 | // If the preceding component does not exist in the file |
| 2656 | // system, we strip it. On Unix, we don't accept a symbolic |
| 2657 | // link that refers to a non-existent file. |
| 2658 | saved_char = p[-1]; |
| 2659 | p[-1] = NUL; |
| 2660 | # ifdef UNIX |
| 2661 | if (mch_lstat((char *)filename, &st) < 0) |
| 2662 | # else |
| 2663 | if (mch_stat((char *)filename, &st) < 0) |
| 2664 | # endif |
| 2665 | do_strip = TRUE; |
| 2666 | p[-1] = saved_char; |
| 2667 | |
| 2668 | --p; |
| 2669 | // Skip back to after previous '/'. |
| 2670 | while (p > start && !after_pathsep(start, p)) |
| 2671 | MB_PTR_BACK(start, p); |
| 2672 | |
| 2673 | if (!do_strip) |
| 2674 | { |
| 2675 | // If the component exists in the file system, check |
| 2676 | // that stripping it won't change the meaning of the |
| 2677 | // file name. First get information about the |
| 2678 | // unstripped file name. This may fail if the component |
| 2679 | // to strip is not a searchable directory (but a regular |
| 2680 | // file, for instance), since the trailing "/.." cannot |
| 2681 | // be applied then. We don't strip it then since we |
| 2682 | // don't want to replace an erroneous file name by |
| 2683 | // a valid one, and we disable stripping of later |
| 2684 | // components. |
| 2685 | saved_char = *tail; |
| 2686 | *tail = NUL; |
| 2687 | if (mch_stat((char *)filename, &st) >= 0) |
| 2688 | do_strip = TRUE; |
| 2689 | else |
| 2690 | stripping_disabled = TRUE; |
| 2691 | *tail = saved_char; |
| 2692 | # ifdef UNIX |
| 2693 | if (do_strip) |
| 2694 | { |
| 2695 | stat_T new_st; |
| 2696 | |
| 2697 | // On Unix, the check for the unstripped file name |
| 2698 | // above works also for a symbolic link pointing to |
| 2699 | // a searchable directory. But then the parent of |
| 2700 | // the directory pointed to by the link must be the |
| 2701 | // same as the stripped file name. (The latter |
| 2702 | // exists in the file system since it is the |
| 2703 | // component's parent directory.) |
| 2704 | if (p == start && relative) |
| 2705 | (void)mch_stat(".", &new_st); |
| 2706 | else |
| 2707 | { |
| 2708 | saved_char = *p; |
| 2709 | *p = NUL; |
| 2710 | (void)mch_stat((char *)filename, &new_st); |
| 2711 | *p = saved_char; |
| 2712 | } |
| 2713 | |
| 2714 | if (new_st.st_ino != st.st_ino || |
| 2715 | new_st.st_dev != st.st_dev) |
| 2716 | { |
| 2717 | do_strip = FALSE; |
| 2718 | // We don't disable stripping of later |
| 2719 | // components since the unstripped path name is |
| 2720 | // still valid. |
| 2721 | } |
| 2722 | } |
| 2723 | # endif |
| 2724 | } |
| 2725 | } |
| 2726 | |
| 2727 | if (!do_strip) |
| 2728 | { |
| 2729 | // Skip the ".." or "../" and reset the counter for the |
| 2730 | // components that might be stripped later on. |
| 2731 | p = tail; |
| 2732 | components = 0; |
| 2733 | } |
| 2734 | else |
| 2735 | { |
| 2736 | // Strip previous component. If the result would get empty |
| 2737 | // and there is no trailing path separator, leave a single |
| 2738 | // "." instead. If we are at the end of the file name and |
| 2739 | // there is no trailing path separator and a preceding |
| 2740 | // component is left after stripping, strip its trailing |
| 2741 | // path separator as well. |
| 2742 | if (p == start && relative && tail[-1] == '.') |
| 2743 | { |
| 2744 | *p++ = '.'; |
| 2745 | *p = NUL; |
| 2746 | } |
| 2747 | else |
| 2748 | { |
| 2749 | if (p > start && tail[-1] == '.') |
| 2750 | --p; |
| 2751 | STRMOVE(p, tail); // strip previous component |
| 2752 | } |
| 2753 | |
| 2754 | --components; |
| 2755 | } |
| 2756 | } |
| 2757 | else if (p == start && !relative) // leading "/.." or "/../" |
| 2758 | STRMOVE(p, tail); // strip ".." or "../" |
| 2759 | else |
| 2760 | { |
| 2761 | if (p == start + 2 && p[-2] == '.') // leading "./../" |
| 2762 | { |
| 2763 | STRMOVE(p - 2, p); // strip leading "./" |
| 2764 | tail -= 2; |
| 2765 | } |
| 2766 | p = tail; // skip to char after ".." or "../" |
| 2767 | } |
| 2768 | } |
| 2769 | else |
| 2770 | { |
| 2771 | ++components; // simple path component |
| 2772 | p = getnextcomp(p); |
| 2773 | } |
| 2774 | } while (*p != NUL); |
| 2775 | #endif // !AMIGA |
| 2776 | } |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2777 | |
| 2778 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 2779 | /* |
| 2780 | * "simplify()" function |
| 2781 | */ |
| 2782 | void |
| 2783 | f_simplify(typval_T *argvars, typval_T *rettv) |
| 2784 | { |
| 2785 | char_u *p; |
| 2786 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 2787 | if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL) |
| 2788 | return; |
| 2789 | |
Bram Moolenaar | 3cfa5b1 | 2021-06-06 14:14:39 +0200 | [diff] [blame] | 2790 | p = tv_get_string_strict(&argvars[0]); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2791 | rettv->vval.v_string = vim_strsave(p); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2792 | simplify_filename(rettv->vval.v_string); // simplify in place |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2793 | rettv->v_type = VAR_STRING; |
| 2794 | } |
| 2795 | #endif // FEAT_EVAL |