Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [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 | /* |
Bram Moolenaar | 9810cfb | 2019-12-11 21:23:00 +0100 | [diff] [blame] | 11 | * filepath.c: dealing with file names and paths. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
| 16 | #ifdef MSWIN |
| 17 | /* |
| 18 | * Functions for ":8" filename modifier: get 8.3 version of a filename. |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * Get the short path (8.3) for the filename in "fnamep". |
| 23 | * Only works for a valid file name. |
| 24 | * When the path gets longer "fnamep" is changed and the allocated buffer |
| 25 | * is put in "bufp". |
| 26 | * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path. |
| 27 | * Returns OK on success, FAIL on failure. |
| 28 | */ |
| 29 | static int |
| 30 | get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen) |
| 31 | { |
| 32 | int l, len; |
Bram Moolenaar | 3f39697 | 2019-10-30 04:10:06 +0100 | [diff] [blame] | 33 | WCHAR *newbuf; |
| 34 | WCHAR *wfname; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 35 | |
Bram Moolenaar | 3f39697 | 2019-10-30 04:10:06 +0100 | [diff] [blame] | 36 | len = MAXPATHL; |
| 37 | newbuf = malloc(len * sizeof(*newbuf)); |
| 38 | if (newbuf == NULL) |
| 39 | return FAIL; |
| 40 | |
| 41 | wfname = enc_to_utf16(*fnamep, NULL); |
| 42 | if (wfname == NULL) |
| 43 | { |
| 44 | vim_free(newbuf); |
| 45 | return FAIL; |
| 46 | } |
| 47 | |
| 48 | l = GetShortPathNameW(wfname, newbuf, len); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 49 | if (l > len - 1) |
| 50 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 51 | // If that doesn't work (not enough space), then save the string |
| 52 | // and try again with a new buffer big enough. |
Bram Moolenaar | 3f39697 | 2019-10-30 04:10:06 +0100 | [diff] [blame] | 53 | WCHAR *newbuf_t = newbuf; |
| 54 | newbuf = vim_realloc(newbuf, (l + 1) * sizeof(*newbuf)); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 55 | if (newbuf == NULL) |
Bram Moolenaar | 3f39697 | 2019-10-30 04:10:06 +0100 | [diff] [blame] | 56 | { |
| 57 | vim_free(wfname); |
| 58 | vim_free(newbuf_t); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 59 | return FAIL; |
Bram Moolenaar | 3f39697 | 2019-10-30 04:10:06 +0100 | [diff] [blame] | 60 | } |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 61 | // Really should always succeed, as the buffer is big enough. |
Bram Moolenaar | 3f39697 | 2019-10-30 04:10:06 +0100 | [diff] [blame] | 62 | l = GetShortPathNameW(wfname, newbuf, l+1); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 63 | } |
Bram Moolenaar | 3f39697 | 2019-10-30 04:10:06 +0100 | [diff] [blame] | 64 | if (l != 0) |
| 65 | { |
| 66 | char_u *p = utf16_to_enc(newbuf, NULL); |
Bram Moolenaar | c74fbfe | 2020-04-06 22:56:28 +0200 | [diff] [blame] | 67 | |
Bram Moolenaar | 3f39697 | 2019-10-30 04:10:06 +0100 | [diff] [blame] | 68 | if (p != NULL) |
| 69 | { |
| 70 | vim_free(*bufp); |
| 71 | *fnamep = *bufp = p; |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | vim_free(wfname); |
| 76 | vim_free(newbuf); |
| 77 | return FAIL; |
| 78 | } |
| 79 | } |
| 80 | vim_free(wfname); |
| 81 | vim_free(newbuf); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 82 | |
Bram Moolenaar | 2ade714 | 2019-11-04 20:36:50 +0100 | [diff] [blame] | 83 | *fnamelen = l == 0 ? l : (int)STRLEN(*bufp); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 84 | return OK; |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * Get the short path (8.3) for the filename in "fname". The converted |
| 89 | * path is returned in "bufp". |
| 90 | * |
| 91 | * Some of the directories specified in "fname" may not exist. This function |
| 92 | * will shorten the existing directories at the beginning of the path and then |
| 93 | * append the remaining non-existing path. |
| 94 | * |
| 95 | * fname - Pointer to the filename to shorten. On return, contains the |
| 96 | * pointer to the shortened pathname |
| 97 | * bufp - Pointer to an allocated buffer for the filename. |
| 98 | * fnamelen - Length of the filename pointed to by fname |
| 99 | * |
| 100 | * Returns OK on success (or nothing done) and FAIL on failure (out of memory). |
| 101 | */ |
| 102 | static int |
| 103 | shortpath_for_invalid_fname( |
| 104 | char_u **fname, |
| 105 | char_u **bufp, |
| 106 | int *fnamelen) |
| 107 | { |
| 108 | char_u *short_fname, *save_fname, *pbuf_unused; |
| 109 | char_u *endp, *save_endp; |
| 110 | char_u ch; |
| 111 | int old_len, len; |
| 112 | int new_len, sfx_len; |
| 113 | int retval = OK; |
| 114 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 115 | // Make a copy |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 116 | old_len = *fnamelen; |
| 117 | save_fname = vim_strnsave(*fname, old_len); |
| 118 | pbuf_unused = NULL; |
| 119 | short_fname = NULL; |
| 120 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 121 | endp = save_fname + old_len - 1; // Find the end of the copy |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 122 | save_endp = endp; |
| 123 | |
| 124 | /* |
| 125 | * Try shortening the supplied path till it succeeds by removing one |
| 126 | * directory at a time from the tail of the path. |
| 127 | */ |
| 128 | len = 0; |
| 129 | for (;;) |
| 130 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 131 | // go back one path-separator |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 132 | while (endp > save_fname && !after_pathsep(save_fname, endp + 1)) |
| 133 | --endp; |
| 134 | if (endp <= save_fname) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 135 | break; // processed the complete path |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 136 | |
| 137 | /* |
| 138 | * Replace the path separator with a NUL and try to shorten the |
| 139 | * resulting path. |
| 140 | */ |
| 141 | ch = *endp; |
| 142 | *endp = 0; |
| 143 | short_fname = save_fname; |
| 144 | len = (int)STRLEN(short_fname) + 1; |
| 145 | if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL) |
| 146 | { |
| 147 | retval = FAIL; |
| 148 | goto theend; |
| 149 | } |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 150 | *endp = ch; // preserve the string |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 151 | |
| 152 | if (len > 0) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 153 | break; // successfully shortened the path |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 154 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 155 | // failed to shorten the path. Skip the path separator |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 156 | --endp; |
| 157 | } |
| 158 | |
| 159 | if (len > 0) |
| 160 | { |
| 161 | /* |
| 162 | * Succeeded in shortening the path. Now concatenate the shortened |
| 163 | * path with the remaining path at the tail. |
| 164 | */ |
| 165 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 166 | // Compute the length of the new path. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 167 | sfx_len = (int)(save_endp - endp) + 1; |
| 168 | new_len = len + sfx_len; |
| 169 | |
| 170 | *fnamelen = new_len; |
| 171 | vim_free(*bufp); |
| 172 | if (new_len > old_len) |
| 173 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 174 | // There is not enough space in the currently allocated string, |
| 175 | // copy it to a buffer big enough. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 176 | *fname = *bufp = vim_strnsave(short_fname, new_len); |
| 177 | if (*fname == NULL) |
| 178 | { |
| 179 | retval = FAIL; |
| 180 | goto theend; |
| 181 | } |
| 182 | } |
| 183 | else |
| 184 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 185 | // Transfer short_fname to the main buffer (it's big enough), |
| 186 | // unless get_short_pathname() did its work in-place. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 187 | *fname = *bufp = save_fname; |
| 188 | if (short_fname != save_fname) |
| 189 | vim_strncpy(save_fname, short_fname, len); |
| 190 | save_fname = NULL; |
| 191 | } |
| 192 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 193 | // concat the not-shortened part of the path |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 194 | vim_strncpy(*fname + len, endp, sfx_len); |
| 195 | (*fname)[new_len] = NUL; |
| 196 | } |
| 197 | |
| 198 | theend: |
| 199 | vim_free(pbuf_unused); |
| 200 | vim_free(save_fname); |
| 201 | |
| 202 | return retval; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * Get a pathname for a partial path. |
| 207 | * Returns OK for success, FAIL for failure. |
| 208 | */ |
| 209 | static int |
| 210 | shortpath_for_partial( |
| 211 | char_u **fnamep, |
| 212 | char_u **bufp, |
| 213 | int *fnamelen) |
| 214 | { |
| 215 | int sepcount, len, tflen; |
| 216 | char_u *p; |
| 217 | char_u *pbuf, *tfname; |
| 218 | int hasTilde; |
| 219 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 220 | // Count up the path separators from the RHS.. so we know which part |
| 221 | // of the path to return. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 222 | sepcount = 0; |
| 223 | for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p)) |
| 224 | if (vim_ispathsep(*p)) |
| 225 | ++sepcount; |
| 226 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 227 | // Need full path first (use expand_env() to remove a "~/") |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 228 | hasTilde = (**fnamep == '~'); |
| 229 | if (hasTilde) |
| 230 | pbuf = tfname = expand_env_save(*fnamep); |
| 231 | else |
| 232 | pbuf = tfname = FullName_save(*fnamep, FALSE); |
| 233 | |
| 234 | len = tflen = (int)STRLEN(tfname); |
| 235 | |
| 236 | if (get_short_pathname(&tfname, &pbuf, &len) == FAIL) |
| 237 | return FAIL; |
| 238 | |
| 239 | if (len == 0) |
| 240 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 241 | // Don't have a valid filename, so shorten the rest of the |
| 242 | // path if we can. This CAN give us invalid 8.3 filenames, but |
| 243 | // there's not a lot of point in guessing what it might be. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 244 | len = tflen; |
| 245 | if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL) |
| 246 | return FAIL; |
| 247 | } |
| 248 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 249 | // Count the paths backward to find the beginning of the desired string. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 250 | for (p = tfname + len - 1; p >= tfname; --p) |
| 251 | { |
| 252 | if (has_mbyte) |
| 253 | p -= mb_head_off(tfname, p); |
| 254 | if (vim_ispathsep(*p)) |
| 255 | { |
| 256 | if (sepcount == 0 || (hasTilde && sepcount == 1)) |
| 257 | break; |
| 258 | else |
| 259 | sepcount --; |
| 260 | } |
| 261 | } |
| 262 | if (hasTilde) |
| 263 | { |
| 264 | --p; |
| 265 | if (p >= tfname) |
| 266 | *p = '~'; |
| 267 | else |
| 268 | return FAIL; |
| 269 | } |
| 270 | else |
| 271 | ++p; |
| 272 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 273 | // Copy in the string - p indexes into tfname - allocated at pbuf |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 274 | vim_free(*bufp); |
| 275 | *fnamelen = (int)STRLEN(p); |
| 276 | *bufp = pbuf; |
| 277 | *fnamep = p; |
| 278 | |
| 279 | return OK; |
| 280 | } |
| 281 | #endif // MSWIN |
| 282 | |
| 283 | /* |
| 284 | * Adjust a filename, according to a string of modifiers. |
| 285 | * *fnamep must be NUL terminated when called. When returning, the length is |
| 286 | * determined by *fnamelen. |
| 287 | * Returns VALID_ flags or -1 for failure. |
| 288 | * When there is an error, *fnamep is set to NULL. |
| 289 | */ |
| 290 | int |
| 291 | modify_fname( |
| 292 | char_u *src, // string with modifiers |
| 293 | int tilde_file, // "~" is a file name, not $HOME |
| 294 | int *usedlen, // characters after src that are used |
| 295 | char_u **fnamep, // file name so far |
| 296 | char_u **bufp, // buffer for allocated file name or NULL |
| 297 | int *fnamelen) // length of fnamep |
| 298 | { |
| 299 | int valid = 0; |
| 300 | char_u *tail; |
| 301 | char_u *s, *p, *pbuf; |
| 302 | char_u dirname[MAXPATHL]; |
| 303 | int c; |
| 304 | int has_fullname = 0; |
Bram Moolenaar | d816cd9 | 2020-02-04 22:23:09 +0100 | [diff] [blame] | 305 | int has_homerelative = 0; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 306 | #ifdef MSWIN |
| 307 | char_u *fname_start = *fnamep; |
| 308 | int has_shortname = 0; |
| 309 | #endif |
| 310 | |
| 311 | repeat: |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 312 | // ":p" - full path/file_name |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 313 | if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p') |
| 314 | { |
| 315 | has_fullname = 1; |
| 316 | |
| 317 | valid |= VALID_PATH; |
| 318 | *usedlen += 2; |
| 319 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 320 | // Expand "~/path" for all systems and "~user/path" for Unix and VMS |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 321 | if ((*fnamep)[0] == '~' |
| 322 | #if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME)) |
| 323 | && ((*fnamep)[1] == '/' |
| 324 | # ifdef BACKSLASH_IN_FILENAME |
| 325 | || (*fnamep)[1] == '\\' |
| 326 | # endif |
| 327 | || (*fnamep)[1] == NUL) |
| 328 | #endif |
| 329 | && !(tilde_file && (*fnamep)[1] == NUL) |
| 330 | ) |
| 331 | { |
| 332 | *fnamep = expand_env_save(*fnamep); |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 333 | vim_free(*bufp); // free any allocated file name |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 334 | *bufp = *fnamep; |
| 335 | if (*fnamep == NULL) |
| 336 | return -1; |
| 337 | } |
| 338 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 339 | // When "/." or "/.." is used: force expansion to get rid of it. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 340 | for (p = *fnamep; *p != NUL; MB_PTR_ADV(p)) |
| 341 | { |
| 342 | if (vim_ispathsep(*p) |
| 343 | && p[1] == '.' |
| 344 | && (p[2] == NUL |
| 345 | || vim_ispathsep(p[2]) |
| 346 | || (p[2] == '.' |
| 347 | && (p[3] == NUL || vim_ispathsep(p[3]))))) |
| 348 | break; |
| 349 | } |
| 350 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 351 | // FullName_save() is slow, don't use it when not needed. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 352 | if (*p != NUL || !vim_isAbsName(*fnamep)) |
| 353 | { |
| 354 | *fnamep = FullName_save(*fnamep, *p != NUL); |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 355 | vim_free(*bufp); // free any allocated file name |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 356 | *bufp = *fnamep; |
| 357 | if (*fnamep == NULL) |
| 358 | return -1; |
| 359 | } |
| 360 | |
| 361 | #ifdef MSWIN |
| 362 | # if _WIN32_WINNT >= 0x0500 |
| 363 | if (vim_strchr(*fnamep, '~') != NULL) |
| 364 | { |
| 365 | // Expand 8.3 filename to full path. Needed to make sure the same |
| 366 | // file does not have two different names. |
| 367 | // Note: problem does not occur if _WIN32_WINNT < 0x0500. |
| 368 | WCHAR *wfname = enc_to_utf16(*fnamep, NULL); |
| 369 | WCHAR buf[_MAX_PATH]; |
| 370 | |
| 371 | if (wfname != NULL) |
| 372 | { |
| 373 | if (GetLongPathNameW(wfname, buf, _MAX_PATH)) |
| 374 | { |
| 375 | char_u *p = utf16_to_enc(buf, NULL); |
| 376 | |
| 377 | if (p != NULL) |
| 378 | { |
| 379 | vim_free(*bufp); // free any allocated file name |
| 380 | *bufp = *fnamep = p; |
| 381 | } |
| 382 | } |
| 383 | vim_free(wfname); |
| 384 | } |
| 385 | } |
| 386 | # endif |
| 387 | #endif |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 388 | // Append a path separator to a directory. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 389 | if (mch_isdir(*fnamep)) |
| 390 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 391 | // Make room for one or two extra characters. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 392 | *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2); |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 393 | vim_free(*bufp); // free any allocated file name |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 394 | *bufp = *fnamep; |
| 395 | if (*fnamep == NULL) |
| 396 | return -1; |
| 397 | add_pathsep(*fnamep); |
| 398 | } |
| 399 | } |
| 400 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 401 | // ":." - path relative to the current directory |
| 402 | // ":~" - path relative to the home directory |
| 403 | // ":8" - shortname path - postponed till after |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 404 | while (src[*usedlen] == ':' |
| 405 | && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8')) |
| 406 | { |
| 407 | *usedlen += 2; |
| 408 | if (c == '8') |
| 409 | { |
| 410 | #ifdef MSWIN |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 411 | has_shortname = 1; // Postpone this. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 412 | #endif |
| 413 | continue; |
| 414 | } |
| 415 | pbuf = NULL; |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 416 | // Need full path first (use expand_env() to remove a "~/") |
Bram Moolenaar | d816cd9 | 2020-02-04 22:23:09 +0100 | [diff] [blame] | 417 | if (!has_fullname && !has_homerelative) |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 418 | { |
| 419 | if (c == '.' && **fnamep == '~') |
| 420 | p = pbuf = expand_env_save(*fnamep); |
| 421 | else |
| 422 | p = pbuf = FullName_save(*fnamep, FALSE); |
| 423 | } |
| 424 | else |
| 425 | p = *fnamep; |
| 426 | |
| 427 | has_fullname = 0; |
| 428 | |
| 429 | if (p != NULL) |
| 430 | { |
| 431 | if (c == '.') |
| 432 | { |
Bram Moolenaar | d816cd9 | 2020-02-04 22:23:09 +0100 | [diff] [blame] | 433 | size_t namelen; |
| 434 | |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 435 | mch_dirname(dirname, MAXPATHL); |
Bram Moolenaar | d816cd9 | 2020-02-04 22:23:09 +0100 | [diff] [blame] | 436 | if (has_homerelative) |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 437 | { |
Bram Moolenaar | d816cd9 | 2020-02-04 22:23:09 +0100 | [diff] [blame] | 438 | s = vim_strsave(dirname); |
| 439 | if (s != NULL) |
| 440 | { |
| 441 | home_replace(NULL, s, dirname, MAXPATHL, TRUE); |
| 442 | vim_free(s); |
| 443 | } |
| 444 | } |
| 445 | namelen = STRLEN(dirname); |
| 446 | |
| 447 | // Do not call shorten_fname() here since it removes the prefix |
| 448 | // even though the path does not have a prefix. |
| 449 | if (fnamencmp(p, dirname, namelen) == 0) |
| 450 | { |
| 451 | p += namelen; |
Bram Moolenaar | a78e9c6 | 2020-02-05 21:14:00 +0100 | [diff] [blame] | 452 | if (vim_ispathsep(*p)) |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 453 | { |
Bram Moolenaar | a78e9c6 | 2020-02-05 21:14:00 +0100 | [diff] [blame] | 454 | while (*p && vim_ispathsep(*p)) |
| 455 | ++p; |
| 456 | *fnamep = p; |
| 457 | if (pbuf != NULL) |
| 458 | { |
| 459 | // free any allocated file name |
| 460 | vim_free(*bufp); |
| 461 | *bufp = pbuf; |
| 462 | pbuf = NULL; |
| 463 | } |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | } |
| 467 | else |
| 468 | { |
| 469 | home_replace(NULL, p, dirname, MAXPATHL, TRUE); |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 470 | // Only replace it when it starts with '~' |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 471 | if (*dirname == '~') |
| 472 | { |
| 473 | s = vim_strsave(dirname); |
| 474 | if (s != NULL) |
| 475 | { |
| 476 | *fnamep = s; |
| 477 | vim_free(*bufp); |
| 478 | *bufp = s; |
Bram Moolenaar | d816cd9 | 2020-02-04 22:23:09 +0100 | [diff] [blame] | 479 | has_homerelative = TRUE; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | } |
| 483 | vim_free(pbuf); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | tail = gettail(*fnamep); |
| 488 | *fnamelen = (int)STRLEN(*fnamep); |
| 489 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 490 | // ":h" - head, remove "/file_name", can be repeated |
| 491 | // Don't remove the first "/" or "c:\" |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 492 | while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h') |
| 493 | { |
| 494 | valid |= VALID_HEAD; |
| 495 | *usedlen += 2; |
| 496 | s = get_past_head(*fnamep); |
| 497 | while (tail > s && after_pathsep(s, tail)) |
| 498 | MB_PTR_BACK(*fnamep, tail); |
| 499 | *fnamelen = (int)(tail - *fnamep); |
| 500 | #ifdef VMS |
| 501 | if (*fnamelen > 0) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 502 | *fnamelen += 1; // the path separator is part of the path |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 503 | #endif |
| 504 | if (*fnamelen == 0) |
| 505 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 506 | // Result is empty. Turn it into "." to make ":cd %:h" work. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 507 | p = vim_strsave((char_u *)"."); |
| 508 | if (p == NULL) |
| 509 | return -1; |
| 510 | vim_free(*bufp); |
| 511 | *bufp = *fnamep = tail = p; |
| 512 | *fnamelen = 1; |
| 513 | } |
| 514 | else |
| 515 | { |
| 516 | while (tail > s && !after_pathsep(s, tail)) |
| 517 | MB_PTR_BACK(*fnamep, tail); |
| 518 | } |
| 519 | } |
| 520 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 521 | // ":8" - shortname |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 522 | if (src[*usedlen] == ':' && src[*usedlen + 1] == '8') |
| 523 | { |
| 524 | *usedlen += 2; |
| 525 | #ifdef MSWIN |
| 526 | has_shortname = 1; |
| 527 | #endif |
| 528 | } |
| 529 | |
| 530 | #ifdef MSWIN |
| 531 | /* |
| 532 | * Handle ":8" after we have done 'heads' and before we do 'tails'. |
| 533 | */ |
| 534 | if (has_shortname) |
| 535 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 536 | // Copy the string if it is shortened by :h and when it wasn't copied |
| 537 | // yet, because we are going to change it in place. Avoids changing |
| 538 | // the buffer name for "%:8". |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 539 | if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start) |
| 540 | { |
| 541 | p = vim_strnsave(*fnamep, *fnamelen); |
| 542 | if (p == NULL) |
| 543 | return -1; |
| 544 | vim_free(*bufp); |
| 545 | *bufp = *fnamep = p; |
| 546 | } |
| 547 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 548 | // Split into two implementations - makes it easier. First is where |
| 549 | // there isn't a full name already, second is where there is. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 550 | if (!has_fullname && !vim_isAbsName(*fnamep)) |
| 551 | { |
| 552 | if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL) |
| 553 | return -1; |
| 554 | } |
| 555 | else |
| 556 | { |
| 557 | int l = *fnamelen; |
| 558 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 559 | // Simple case, already have the full-name. |
| 560 | // Nearly always shorter, so try first time. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 561 | if (get_short_pathname(fnamep, bufp, &l) == FAIL) |
| 562 | return -1; |
| 563 | |
| 564 | if (l == 0) |
| 565 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 566 | // Couldn't find the filename, search the paths. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 567 | l = *fnamelen; |
| 568 | if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL) |
| 569 | return -1; |
| 570 | } |
| 571 | *fnamelen = l; |
| 572 | } |
| 573 | } |
| 574 | #endif // MSWIN |
| 575 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 576 | // ":t" - tail, just the basename |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 577 | if (src[*usedlen] == ':' && src[*usedlen + 1] == 't') |
| 578 | { |
| 579 | *usedlen += 2; |
| 580 | *fnamelen -= (int)(tail - *fnamep); |
| 581 | *fnamep = tail; |
| 582 | } |
| 583 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 584 | // ":e" - extension, can be repeated |
| 585 | // ":r" - root, without extension, can be repeated |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 586 | while (src[*usedlen] == ':' |
| 587 | && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r')) |
| 588 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 589 | // find a '.' in the tail: |
| 590 | // - for second :e: before the current fname |
| 591 | // - otherwise: The last '.' |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 592 | if (src[*usedlen + 1] == 'e' && *fnamep > tail) |
| 593 | s = *fnamep - 2; |
| 594 | else |
| 595 | s = *fnamep + *fnamelen - 1; |
| 596 | for ( ; s > tail; --s) |
| 597 | if (s[0] == '.') |
| 598 | break; |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 599 | if (src[*usedlen + 1] == 'e') // :e |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 600 | { |
| 601 | if (s > tail) |
| 602 | { |
| 603 | *fnamelen += (int)(*fnamep - (s + 1)); |
| 604 | *fnamep = s + 1; |
| 605 | #ifdef VMS |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 606 | // cut version from the extension |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 607 | s = *fnamep + *fnamelen - 1; |
| 608 | for ( ; s > *fnamep; --s) |
| 609 | if (s[0] == ';') |
| 610 | break; |
| 611 | if (s > *fnamep) |
| 612 | *fnamelen = s - *fnamep; |
| 613 | #endif |
| 614 | } |
| 615 | else if (*fnamep <= tail) |
| 616 | *fnamelen = 0; |
| 617 | } |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 618 | else // :r |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 619 | { |
Bram Moolenaar | b189295 | 2019-10-08 23:26:50 +0200 | [diff] [blame] | 620 | char_u *limit = *fnamep; |
| 621 | |
| 622 | if (limit < tail) |
| 623 | limit = tail; |
| 624 | if (s > limit) // remove one extension |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 625 | *fnamelen = (int)(s - *fnamep); |
| 626 | } |
| 627 | *usedlen += 2; |
| 628 | } |
| 629 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 630 | // ":s?pat?foo?" - substitute |
| 631 | // ":gs?pat?foo?" - global substitute |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 632 | if (src[*usedlen] == ':' |
| 633 | && (src[*usedlen + 1] == 's' |
| 634 | || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's'))) |
| 635 | { |
| 636 | char_u *str; |
| 637 | char_u *pat; |
| 638 | char_u *sub; |
| 639 | int sep; |
| 640 | char_u *flags; |
| 641 | int didit = FALSE; |
| 642 | |
| 643 | flags = (char_u *)""; |
| 644 | s = src + *usedlen + 2; |
| 645 | if (src[*usedlen + 1] == 'g') |
| 646 | { |
| 647 | flags = (char_u *)"g"; |
| 648 | ++s; |
| 649 | } |
| 650 | |
| 651 | sep = *s++; |
| 652 | if (sep) |
| 653 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 654 | // find end of pattern |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 655 | p = vim_strchr(s, sep); |
| 656 | if (p != NULL) |
| 657 | { |
| 658 | pat = vim_strnsave(s, (int)(p - s)); |
| 659 | if (pat != NULL) |
| 660 | { |
| 661 | s = p + 1; |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 662 | // find end of substitution |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 663 | p = vim_strchr(s, sep); |
| 664 | if (p != NULL) |
| 665 | { |
| 666 | sub = vim_strnsave(s, (int)(p - s)); |
| 667 | str = vim_strnsave(*fnamep, *fnamelen); |
| 668 | if (sub != NULL && str != NULL) |
| 669 | { |
| 670 | *usedlen = (int)(p + 1 - src); |
| 671 | s = do_string_sub(str, pat, sub, NULL, flags); |
| 672 | if (s != NULL) |
| 673 | { |
| 674 | *fnamep = s; |
| 675 | *fnamelen = (int)STRLEN(s); |
| 676 | vim_free(*bufp); |
| 677 | *bufp = s; |
| 678 | didit = TRUE; |
| 679 | } |
| 680 | } |
| 681 | vim_free(sub); |
| 682 | vim_free(str); |
| 683 | } |
| 684 | vim_free(pat); |
| 685 | } |
| 686 | } |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 687 | // after using ":s", repeat all the modifiers |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 688 | if (didit) |
| 689 | goto repeat; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S') |
| 694 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 695 | // vim_strsave_shellescape() needs a NUL terminated string. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 696 | c = (*fnamep)[*fnamelen]; |
| 697 | if (c != NUL) |
| 698 | (*fnamep)[*fnamelen] = NUL; |
| 699 | p = vim_strsave_shellescape(*fnamep, FALSE, FALSE); |
| 700 | if (c != NUL) |
| 701 | (*fnamep)[*fnamelen] = c; |
| 702 | if (p == NULL) |
| 703 | return -1; |
| 704 | vim_free(*bufp); |
| 705 | *bufp = *fnamep = p; |
| 706 | *fnamelen = (int)STRLEN(p); |
| 707 | *usedlen += 2; |
| 708 | } |
| 709 | |
| 710 | return valid; |
| 711 | } |
| 712 | |
| 713 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 714 | |
| 715 | /* |
| 716 | * "chdir(dir)" function |
| 717 | */ |
| 718 | void |
| 719 | f_chdir(typval_T *argvars, typval_T *rettv) |
| 720 | { |
| 721 | char_u *cwd; |
| 722 | cdscope_T scope = CDSCOPE_GLOBAL; |
| 723 | |
| 724 | rettv->v_type = VAR_STRING; |
| 725 | rettv->vval.v_string = NULL; |
| 726 | |
| 727 | if (argvars[0].v_type != VAR_STRING) |
Bram Moolenaar | d816cd9 | 2020-02-04 22:23:09 +0100 | [diff] [blame] | 728 | // Returning an empty string means it failed. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 729 | return; |
| 730 | |
| 731 | // Return the current directory |
| 732 | cwd = alloc(MAXPATHL); |
| 733 | if (cwd != NULL) |
| 734 | { |
| 735 | if (mch_dirname(cwd, MAXPATHL) != FAIL) |
| 736 | { |
| 737 | #ifdef BACKSLASH_IN_FILENAME |
| 738 | slash_adjust(cwd); |
| 739 | #endif |
| 740 | rettv->vval.v_string = vim_strsave(cwd); |
| 741 | } |
| 742 | vim_free(cwd); |
| 743 | } |
| 744 | |
| 745 | if (curwin->w_localdir != NULL) |
| 746 | scope = CDSCOPE_WINDOW; |
| 747 | else if (curtab->tp_localdir != NULL) |
| 748 | scope = CDSCOPE_TABPAGE; |
| 749 | |
| 750 | if (!changedir_func(argvars[0].vval.v_string, TRUE, scope)) |
| 751 | // Directory change failed |
| 752 | VIM_CLEAR(rettv->vval.v_string); |
| 753 | } |
| 754 | |
| 755 | /* |
| 756 | * "delete()" function |
| 757 | */ |
| 758 | void |
| 759 | f_delete(typval_T *argvars, typval_T *rettv) |
| 760 | { |
| 761 | char_u nbuf[NUMBUFLEN]; |
| 762 | char_u *name; |
| 763 | char_u *flags; |
| 764 | |
| 765 | rettv->vval.v_number = -1; |
| 766 | if (check_restricted() || check_secure()) |
| 767 | return; |
| 768 | |
| 769 | name = tv_get_string(&argvars[0]); |
| 770 | if (name == NULL || *name == NUL) |
| 771 | { |
| 772 | emsg(_(e_invarg)); |
| 773 | return; |
| 774 | } |
| 775 | |
| 776 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 777 | flags = tv_get_string_buf(&argvars[1], nbuf); |
| 778 | else |
| 779 | flags = (char_u *)""; |
| 780 | |
| 781 | if (*flags == NUL) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 782 | // delete a file |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 783 | rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1; |
| 784 | else if (STRCMP(flags, "d") == 0) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 785 | // delete an empty directory |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 786 | rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1; |
| 787 | else if (STRCMP(flags, "rf") == 0) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 788 | // delete a directory recursively |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 789 | rettv->vval.v_number = delete_recursive(name); |
| 790 | else |
| 791 | semsg(_(e_invexpr2), flags); |
| 792 | } |
| 793 | |
| 794 | /* |
| 795 | * "executable()" function |
| 796 | */ |
| 797 | void |
| 798 | f_executable(typval_T *argvars, typval_T *rettv) |
| 799 | { |
| 800 | char_u *name = tv_get_string(&argvars[0]); |
| 801 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 802 | // Check in $PATH and also check directly if there is a directory name. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 803 | rettv->vval.v_number = mch_can_exe(name, NULL, TRUE); |
| 804 | } |
| 805 | |
| 806 | /* |
| 807 | * "exepath()" function |
| 808 | */ |
| 809 | void |
| 810 | f_exepath(typval_T *argvars, typval_T *rettv) |
| 811 | { |
| 812 | char_u *p = NULL; |
| 813 | |
| 814 | (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE); |
| 815 | rettv->v_type = VAR_STRING; |
| 816 | rettv->vval.v_string = p; |
| 817 | } |
| 818 | |
| 819 | /* |
| 820 | * "filereadable()" function |
| 821 | */ |
| 822 | void |
| 823 | f_filereadable(typval_T *argvars, typval_T *rettv) |
| 824 | { |
| 825 | int fd; |
| 826 | char_u *p; |
| 827 | int n; |
| 828 | |
| 829 | #ifndef O_NONBLOCK |
| 830 | # define O_NONBLOCK 0 |
| 831 | #endif |
| 832 | p = tv_get_string(&argvars[0]); |
| 833 | if (*p && !mch_isdir(p) && (fd = mch_open((char *)p, |
| 834 | O_RDONLY | O_NONBLOCK, 0)) >= 0) |
| 835 | { |
| 836 | n = TRUE; |
| 837 | close(fd); |
| 838 | } |
| 839 | else |
| 840 | n = FALSE; |
| 841 | |
| 842 | rettv->vval.v_number = n; |
| 843 | } |
| 844 | |
| 845 | /* |
| 846 | * Return 0 for not writable, 1 for writable file, 2 for a dir which we have |
| 847 | * rights to write into. |
| 848 | */ |
| 849 | void |
| 850 | f_filewritable(typval_T *argvars, typval_T *rettv) |
| 851 | { |
| 852 | rettv->vval.v_number = filewritable(tv_get_string(&argvars[0])); |
| 853 | } |
| 854 | |
Bram Moolenaar | 840d16f | 2019-09-10 21:27:18 +0200 | [diff] [blame] | 855 | static void |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 856 | findfilendir( |
| 857 | typval_T *argvars UNUSED, |
| 858 | typval_T *rettv, |
| 859 | int find_what UNUSED) |
| 860 | { |
| 861 | #ifdef FEAT_SEARCHPATH |
| 862 | char_u *fname; |
| 863 | char_u *fresult = NULL; |
| 864 | char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path; |
| 865 | char_u *p; |
| 866 | char_u pathbuf[NUMBUFLEN]; |
| 867 | int count = 1; |
| 868 | int first = TRUE; |
| 869 | int error = FALSE; |
| 870 | #endif |
| 871 | |
| 872 | rettv->vval.v_string = NULL; |
| 873 | rettv->v_type = VAR_STRING; |
| 874 | |
| 875 | #ifdef FEAT_SEARCHPATH |
| 876 | fname = tv_get_string(&argvars[0]); |
| 877 | |
| 878 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 879 | { |
| 880 | p = tv_get_string_buf_chk(&argvars[1], pathbuf); |
| 881 | if (p == NULL) |
| 882 | error = TRUE; |
| 883 | else |
| 884 | { |
| 885 | if (*p != NUL) |
| 886 | path = p; |
| 887 | |
| 888 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 889 | count = (int)tv_get_number_chk(&argvars[2], &error); |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | if (count < 0 && rettv_list_alloc(rettv) == FAIL) |
| 894 | error = TRUE; |
| 895 | |
| 896 | if (*fname != NUL && !error) |
| 897 | { |
| 898 | do |
| 899 | { |
| 900 | if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST) |
| 901 | vim_free(fresult); |
| 902 | fresult = find_file_in_path_option(first ? fname : NULL, |
| 903 | first ? (int)STRLEN(fname) : 0, |
| 904 | 0, first, path, |
| 905 | find_what, |
| 906 | curbuf->b_ffname, |
| 907 | find_what == FINDFILE_DIR |
| 908 | ? (char_u *)"" : curbuf->b_p_sua); |
| 909 | first = FALSE; |
| 910 | |
| 911 | if (fresult != NULL && rettv->v_type == VAR_LIST) |
| 912 | list_append_string(rettv->vval.v_list, fresult, -1); |
| 913 | |
| 914 | } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL); |
| 915 | } |
| 916 | |
| 917 | if (rettv->v_type == VAR_STRING) |
| 918 | rettv->vval.v_string = fresult; |
| 919 | #endif |
| 920 | } |
| 921 | |
| 922 | /* |
| 923 | * "finddir({fname}[, {path}[, {count}]])" function |
| 924 | */ |
| 925 | void |
| 926 | f_finddir(typval_T *argvars, typval_T *rettv) |
| 927 | { |
| 928 | findfilendir(argvars, rettv, FINDFILE_DIR); |
| 929 | } |
| 930 | |
| 931 | /* |
| 932 | * "findfile({fname}[, {path}[, {count}]])" function |
| 933 | */ |
| 934 | void |
| 935 | f_findfile(typval_T *argvars, typval_T *rettv) |
| 936 | { |
| 937 | findfilendir(argvars, rettv, FINDFILE_FILE); |
| 938 | } |
| 939 | |
| 940 | /* |
| 941 | * "fnamemodify({fname}, {mods})" function |
| 942 | */ |
| 943 | void |
| 944 | f_fnamemodify(typval_T *argvars, typval_T *rettv) |
| 945 | { |
| 946 | char_u *fname; |
| 947 | char_u *mods; |
| 948 | int usedlen = 0; |
| 949 | int len; |
| 950 | char_u *fbuf = NULL; |
| 951 | char_u buf[NUMBUFLEN]; |
| 952 | |
| 953 | fname = tv_get_string_chk(&argvars[0]); |
| 954 | mods = tv_get_string_buf_chk(&argvars[1], buf); |
| 955 | if (fname == NULL || mods == NULL) |
| 956 | fname = NULL; |
| 957 | else |
| 958 | { |
| 959 | len = (int)STRLEN(fname); |
| 960 | (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len); |
| 961 | } |
| 962 | |
| 963 | rettv->v_type = VAR_STRING; |
| 964 | if (fname == NULL) |
| 965 | rettv->vval.v_string = NULL; |
| 966 | else |
| 967 | rettv->vval.v_string = vim_strnsave(fname, len); |
| 968 | vim_free(fbuf); |
| 969 | } |
| 970 | |
| 971 | /* |
| 972 | * "getcwd()" function |
| 973 | * |
| 974 | * Return the current working directory of a window in a tab page. |
| 975 | * First optional argument 'winnr' is the window number or -1 and the second |
| 976 | * optional argument 'tabnr' is the tab page number. |
| 977 | * |
| 978 | * If no arguments are supplied, then return the directory of the current |
| 979 | * window. |
| 980 | * If only 'winnr' is specified and is not -1 or 0 then return the directory of |
| 981 | * the specified window. |
| 982 | * If 'winnr' is 0 then return the directory of the current window. |
| 983 | * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the |
| 984 | * directory of the specified tab page. Otherwise return the directory of the |
| 985 | * specified window in the specified tab page. |
| 986 | * If the window or the tab page doesn't exist then return NULL. |
| 987 | */ |
| 988 | void |
| 989 | f_getcwd(typval_T *argvars, typval_T *rettv) |
| 990 | { |
| 991 | win_T *wp = NULL; |
| 992 | tabpage_T *tp = NULL; |
| 993 | char_u *cwd; |
| 994 | int global = FALSE; |
| 995 | |
| 996 | rettv->v_type = VAR_STRING; |
| 997 | rettv->vval.v_string = NULL; |
| 998 | |
| 999 | if (argvars[0].v_type == VAR_NUMBER |
| 1000 | && argvars[0].vval.v_number == -1 |
| 1001 | && argvars[1].v_type == VAR_UNKNOWN) |
| 1002 | global = TRUE; |
| 1003 | else |
| 1004 | wp = find_tabwin(&argvars[0], &argvars[1], &tp); |
| 1005 | |
| 1006 | if (wp != NULL && wp->w_localdir != NULL) |
| 1007 | rettv->vval.v_string = vim_strsave(wp->w_localdir); |
| 1008 | else if (tp != NULL && tp->tp_localdir != NULL) |
| 1009 | rettv->vval.v_string = vim_strsave(tp->tp_localdir); |
| 1010 | else if (wp != NULL || tp != NULL || global) |
| 1011 | { |
| 1012 | if (globaldir != NULL) |
| 1013 | rettv->vval.v_string = vim_strsave(globaldir); |
| 1014 | else |
| 1015 | { |
| 1016 | cwd = alloc(MAXPATHL); |
| 1017 | if (cwd != NULL) |
| 1018 | { |
| 1019 | if (mch_dirname(cwd, MAXPATHL) != FAIL) |
| 1020 | rettv->vval.v_string = vim_strsave(cwd); |
| 1021 | vim_free(cwd); |
| 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 | #ifdef BACKSLASH_IN_FILENAME |
| 1026 | if (rettv->vval.v_string != NULL) |
| 1027 | slash_adjust(rettv->vval.v_string); |
| 1028 | #endif |
| 1029 | } |
| 1030 | |
| 1031 | /* |
| 1032 | * "getfperm({fname})" function |
| 1033 | */ |
| 1034 | void |
| 1035 | f_getfperm(typval_T *argvars, typval_T *rettv) |
| 1036 | { |
| 1037 | char_u *fname; |
| 1038 | stat_T st; |
| 1039 | char_u *perm = NULL; |
| 1040 | char_u flags[] = "rwx"; |
| 1041 | int i; |
| 1042 | |
| 1043 | fname = tv_get_string(&argvars[0]); |
| 1044 | |
| 1045 | rettv->v_type = VAR_STRING; |
| 1046 | if (mch_stat((char *)fname, &st) >= 0) |
| 1047 | { |
| 1048 | perm = vim_strsave((char_u *)"---------"); |
| 1049 | if (perm != NULL) |
| 1050 | { |
| 1051 | for (i = 0; i < 9; i++) |
| 1052 | { |
| 1053 | if (st.st_mode & (1 << (8 - i))) |
| 1054 | perm[i] = flags[i % 3]; |
| 1055 | } |
| 1056 | } |
| 1057 | } |
| 1058 | rettv->vval.v_string = perm; |
| 1059 | } |
| 1060 | |
| 1061 | /* |
| 1062 | * "getfsize({fname})" function |
| 1063 | */ |
| 1064 | void |
| 1065 | f_getfsize(typval_T *argvars, typval_T *rettv) |
| 1066 | { |
| 1067 | char_u *fname; |
| 1068 | stat_T st; |
| 1069 | |
| 1070 | fname = tv_get_string(&argvars[0]); |
| 1071 | |
| 1072 | rettv->v_type = VAR_NUMBER; |
| 1073 | |
| 1074 | if (mch_stat((char *)fname, &st) >= 0) |
| 1075 | { |
| 1076 | if (mch_isdir(fname)) |
| 1077 | rettv->vval.v_number = 0; |
| 1078 | else |
| 1079 | { |
| 1080 | rettv->vval.v_number = (varnumber_T)st.st_size; |
| 1081 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1082 | // non-perfect check for overflow |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1083 | if ((off_T)rettv->vval.v_number != (off_T)st.st_size) |
| 1084 | rettv->vval.v_number = -2; |
| 1085 | } |
| 1086 | } |
| 1087 | else |
| 1088 | rettv->vval.v_number = -1; |
| 1089 | } |
| 1090 | |
| 1091 | /* |
| 1092 | * "getftime({fname})" function |
| 1093 | */ |
| 1094 | void |
| 1095 | f_getftime(typval_T *argvars, typval_T *rettv) |
| 1096 | { |
| 1097 | char_u *fname; |
| 1098 | stat_T st; |
| 1099 | |
| 1100 | fname = tv_get_string(&argvars[0]); |
| 1101 | |
| 1102 | if (mch_stat((char *)fname, &st) >= 0) |
| 1103 | rettv->vval.v_number = (varnumber_T)st.st_mtime; |
| 1104 | else |
| 1105 | rettv->vval.v_number = -1; |
| 1106 | } |
| 1107 | |
| 1108 | /* |
| 1109 | * "getftype({fname})" function |
| 1110 | */ |
| 1111 | void |
| 1112 | f_getftype(typval_T *argvars, typval_T *rettv) |
| 1113 | { |
| 1114 | char_u *fname; |
| 1115 | stat_T st; |
| 1116 | char_u *type = NULL; |
| 1117 | char *t; |
| 1118 | |
| 1119 | fname = tv_get_string(&argvars[0]); |
| 1120 | |
| 1121 | rettv->v_type = VAR_STRING; |
| 1122 | if (mch_lstat((char *)fname, &st) >= 0) |
| 1123 | { |
| 1124 | if (S_ISREG(st.st_mode)) |
| 1125 | t = "file"; |
| 1126 | else if (S_ISDIR(st.st_mode)) |
| 1127 | t = "dir"; |
| 1128 | else if (S_ISLNK(st.st_mode)) |
| 1129 | t = "link"; |
| 1130 | else if (S_ISBLK(st.st_mode)) |
| 1131 | t = "bdev"; |
| 1132 | else if (S_ISCHR(st.st_mode)) |
| 1133 | t = "cdev"; |
| 1134 | else if (S_ISFIFO(st.st_mode)) |
| 1135 | t = "fifo"; |
| 1136 | else if (S_ISSOCK(st.st_mode)) |
| 1137 | t = "socket"; |
| 1138 | else |
| 1139 | t = "other"; |
| 1140 | type = vim_strsave((char_u *)t); |
| 1141 | } |
| 1142 | rettv->vval.v_string = type; |
| 1143 | } |
| 1144 | |
| 1145 | /* |
| 1146 | * "glob()" function |
| 1147 | */ |
| 1148 | void |
| 1149 | f_glob(typval_T *argvars, typval_T *rettv) |
| 1150 | { |
| 1151 | int options = WILD_SILENT|WILD_USE_NL; |
| 1152 | expand_T xpc; |
| 1153 | int error = FALSE; |
| 1154 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1155 | // When the optional second argument is non-zero, don't remove matches |
| 1156 | // for 'wildignore' and don't put matches for 'suffixes' at the end. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1157 | rettv->v_type = VAR_STRING; |
| 1158 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 1159 | { |
| 1160 | if (tv_get_number_chk(&argvars[1], &error)) |
| 1161 | options |= WILD_KEEP_ALL; |
| 1162 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 1163 | { |
| 1164 | if (tv_get_number_chk(&argvars[2], &error)) |
| 1165 | rettv_list_set(rettv, NULL); |
| 1166 | if (argvars[3].v_type != VAR_UNKNOWN |
| 1167 | && tv_get_number_chk(&argvars[3], &error)) |
| 1168 | options |= WILD_ALLLINKS; |
| 1169 | } |
| 1170 | } |
| 1171 | if (!error) |
| 1172 | { |
| 1173 | ExpandInit(&xpc); |
| 1174 | xpc.xp_context = EXPAND_FILES; |
| 1175 | if (p_wic) |
| 1176 | options += WILD_ICASE; |
| 1177 | if (rettv->v_type == VAR_STRING) |
| 1178 | rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]), |
| 1179 | NULL, options, WILD_ALL); |
| 1180 | else if (rettv_list_alloc(rettv) != FAIL) |
| 1181 | { |
| 1182 | int i; |
| 1183 | |
| 1184 | ExpandOne(&xpc, tv_get_string(&argvars[0]), |
| 1185 | NULL, options, WILD_ALL_KEEP); |
| 1186 | for (i = 0; i < xpc.xp_numfiles; i++) |
| 1187 | list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1); |
| 1188 | |
| 1189 | ExpandCleanup(&xpc); |
| 1190 | } |
| 1191 | } |
| 1192 | else |
| 1193 | rettv->vval.v_string = NULL; |
| 1194 | } |
| 1195 | |
| 1196 | /* |
| 1197 | * "glob2regpat()" function |
| 1198 | */ |
| 1199 | void |
| 1200 | f_glob2regpat(typval_T *argvars, typval_T *rettv) |
| 1201 | { |
| 1202 | char_u *pat = tv_get_string_chk(&argvars[0]); |
| 1203 | |
| 1204 | rettv->v_type = VAR_STRING; |
| 1205 | rettv->vval.v_string = (pat == NULL) |
| 1206 | ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE); |
| 1207 | } |
| 1208 | |
| 1209 | /* |
| 1210 | * "globpath()" function |
| 1211 | */ |
| 1212 | void |
| 1213 | f_globpath(typval_T *argvars, typval_T *rettv) |
| 1214 | { |
| 1215 | int flags = WILD_IGNORE_COMPLETESLASH; |
| 1216 | char_u buf1[NUMBUFLEN]; |
| 1217 | char_u *file = tv_get_string_buf_chk(&argvars[1], buf1); |
| 1218 | int error = FALSE; |
| 1219 | garray_T ga; |
| 1220 | int i; |
| 1221 | |
| 1222 | // When the optional second argument is non-zero, don't remove matches |
| 1223 | // for 'wildignore' and don't put matches for 'suffixes' at the end. |
| 1224 | rettv->v_type = VAR_STRING; |
| 1225 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 1226 | { |
| 1227 | if (tv_get_number_chk(&argvars[2], &error)) |
| 1228 | flags |= WILD_KEEP_ALL; |
| 1229 | if (argvars[3].v_type != VAR_UNKNOWN) |
| 1230 | { |
| 1231 | if (tv_get_number_chk(&argvars[3], &error)) |
| 1232 | rettv_list_set(rettv, NULL); |
| 1233 | if (argvars[4].v_type != VAR_UNKNOWN |
| 1234 | && tv_get_number_chk(&argvars[4], &error)) |
| 1235 | flags |= WILD_ALLLINKS; |
| 1236 | } |
| 1237 | } |
| 1238 | if (file != NULL && !error) |
| 1239 | { |
| 1240 | ga_init2(&ga, (int)sizeof(char_u *), 10); |
| 1241 | globpath(tv_get_string(&argvars[0]), file, &ga, flags); |
| 1242 | if (rettv->v_type == VAR_STRING) |
| 1243 | rettv->vval.v_string = ga_concat_strings(&ga, "\n"); |
| 1244 | else if (rettv_list_alloc(rettv) != FAIL) |
| 1245 | for (i = 0; i < ga.ga_len; ++i) |
| 1246 | list_append_string(rettv->vval.v_list, |
| 1247 | ((char_u **)(ga.ga_data))[i], -1); |
| 1248 | ga_clear_strings(&ga); |
| 1249 | } |
| 1250 | else |
| 1251 | rettv->vval.v_string = NULL; |
| 1252 | } |
| 1253 | |
| 1254 | /* |
| 1255 | * "isdirectory()" function |
| 1256 | */ |
| 1257 | void |
| 1258 | f_isdirectory(typval_T *argvars, typval_T *rettv) |
| 1259 | { |
| 1260 | rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0])); |
| 1261 | } |
| 1262 | |
| 1263 | /* |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1264 | * Create the directory in which "dir" is located, and higher levels when |
| 1265 | * needed. |
| 1266 | * Return OK or FAIL. |
| 1267 | */ |
| 1268 | static int |
| 1269 | mkdir_recurse(char_u *dir, int prot) |
| 1270 | { |
| 1271 | char_u *p; |
| 1272 | char_u *updir; |
| 1273 | int r = FAIL; |
| 1274 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1275 | // Get end of directory name in "dir". |
| 1276 | // We're done when it's "/" or "c:/". |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1277 | p = gettail_sep(dir); |
| 1278 | if (p <= get_past_head(dir)) |
| 1279 | return OK; |
| 1280 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1281 | // If the directory exists we're done. Otherwise: create it. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1282 | updir = vim_strnsave(dir, (int)(p - dir)); |
| 1283 | if (updir == NULL) |
| 1284 | return FAIL; |
| 1285 | if (mch_isdir(updir)) |
| 1286 | r = OK; |
| 1287 | else if (mkdir_recurse(updir, prot) == OK) |
| 1288 | r = vim_mkdir_emsg(updir, prot); |
| 1289 | vim_free(updir); |
| 1290 | return r; |
| 1291 | } |
| 1292 | |
| 1293 | /* |
| 1294 | * "mkdir()" function |
| 1295 | */ |
| 1296 | void |
| 1297 | f_mkdir(typval_T *argvars, typval_T *rettv) |
| 1298 | { |
| 1299 | char_u *dir; |
| 1300 | char_u buf[NUMBUFLEN]; |
| 1301 | int prot = 0755; |
| 1302 | |
| 1303 | rettv->vval.v_number = FAIL; |
| 1304 | if (check_restricted() || check_secure()) |
| 1305 | return; |
| 1306 | |
| 1307 | dir = tv_get_string_buf(&argvars[0], buf); |
| 1308 | if (*dir == NUL) |
| 1309 | return; |
| 1310 | |
| 1311 | if (*gettail(dir) == NUL) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1312 | // remove trailing slashes |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1313 | *gettail_sep(dir) = NUL; |
| 1314 | |
| 1315 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 1316 | { |
| 1317 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 1318 | { |
| 1319 | prot = (int)tv_get_number_chk(&argvars[2], NULL); |
| 1320 | if (prot == -1) |
| 1321 | return; |
| 1322 | } |
| 1323 | if (STRCMP(tv_get_string(&argvars[1]), "p") == 0) |
| 1324 | { |
| 1325 | if (mch_isdir(dir)) |
| 1326 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1327 | // With the "p" flag it's OK if the dir already exists. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1328 | rettv->vval.v_number = OK; |
| 1329 | return; |
| 1330 | } |
| 1331 | mkdir_recurse(dir, prot); |
| 1332 | } |
| 1333 | } |
| 1334 | rettv->vval.v_number = vim_mkdir_emsg(dir, prot); |
| 1335 | } |
| 1336 | |
| 1337 | /* |
Bram Moolenaar | af7645d | 2019-09-05 22:33:28 +0200 | [diff] [blame] | 1338 | * "pathshorten()" function |
| 1339 | */ |
| 1340 | void |
| 1341 | f_pathshorten(typval_T *argvars, typval_T *rettv) |
| 1342 | { |
| 1343 | char_u *p; |
| 1344 | |
| 1345 | rettv->v_type = VAR_STRING; |
| 1346 | p = tv_get_string_chk(&argvars[0]); |
| 1347 | if (p == NULL) |
| 1348 | rettv->vval.v_string = NULL; |
| 1349 | else |
| 1350 | { |
| 1351 | p = vim_strsave(p); |
| 1352 | rettv->vval.v_string = p; |
| 1353 | if (p != NULL) |
| 1354 | shorten_dir(p); |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | /* |
Bram Moolenaar | 80147dd | 2020-02-04 22:32:59 +0100 | [diff] [blame] | 1359 | * Evaluate "expr" (= "context") for readdir(). |
| 1360 | */ |
| 1361 | static int |
| 1362 | readdir_checkitem(void *context, char_u *name) |
| 1363 | { |
| 1364 | typval_T *expr = (typval_T *)context; |
| 1365 | typval_T save_val; |
| 1366 | typval_T rettv; |
| 1367 | typval_T argv[2]; |
| 1368 | int retval = 0; |
| 1369 | int error = FALSE; |
| 1370 | |
| 1371 | if (expr->v_type == VAR_UNKNOWN) |
| 1372 | return 1; |
| 1373 | |
| 1374 | prepare_vimvar(VV_VAL, &save_val); |
| 1375 | set_vim_var_string(VV_VAL, name, -1); |
| 1376 | argv[0].v_type = VAR_STRING; |
| 1377 | argv[0].vval.v_string = name; |
| 1378 | |
| 1379 | if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL) |
| 1380 | goto theend; |
| 1381 | |
| 1382 | retval = tv_get_number_chk(&rettv, &error); |
| 1383 | if (error) |
| 1384 | retval = -1; |
| 1385 | clear_tv(&rettv); |
| 1386 | |
| 1387 | theend: |
| 1388 | set_vim_var_string(VV_VAL, NULL, 0); |
| 1389 | restore_vimvar(VV_VAL, &save_val); |
| 1390 | return retval; |
| 1391 | } |
| 1392 | |
| 1393 | /* |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1394 | * "readdir()" function |
| 1395 | */ |
| 1396 | void |
| 1397 | f_readdir(typval_T *argvars, typval_T *rettv) |
| 1398 | { |
| 1399 | typval_T *expr; |
| 1400 | int ret; |
| 1401 | char_u *path; |
| 1402 | char_u *p; |
| 1403 | garray_T ga; |
| 1404 | int i; |
| 1405 | |
| 1406 | if (rettv_list_alloc(rettv) == FAIL) |
| 1407 | return; |
| 1408 | path = tv_get_string(&argvars[0]); |
| 1409 | expr = &argvars[1]; |
| 1410 | |
| 1411 | ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem); |
| 1412 | if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0) |
| 1413 | { |
| 1414 | for (i = 0; i < ga.ga_len; i++) |
| 1415 | { |
| 1416 | p = ((char_u **)ga.ga_data)[i]; |
| 1417 | list_append_string(rettv->vval.v_list, p, -1); |
| 1418 | } |
| 1419 | } |
| 1420 | ga_clear_strings(&ga); |
| 1421 | } |
| 1422 | |
| 1423 | /* |
| 1424 | * "readfile()" function |
| 1425 | */ |
| 1426 | void |
| 1427 | f_readfile(typval_T *argvars, typval_T *rettv) |
| 1428 | { |
| 1429 | int binary = FALSE; |
| 1430 | int blob = FALSE; |
| 1431 | int failed = FALSE; |
| 1432 | char_u *fname; |
| 1433 | FILE *fd; |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1434 | char_u buf[(IOSIZE/256)*256]; // rounded to avoid odd + 1 |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1435 | int io_size = sizeof(buf); |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1436 | int readlen; // size of last fread() |
| 1437 | char_u *prev = NULL; // previously read bytes, if any |
| 1438 | long prevlen = 0; // length of data in prev |
| 1439 | long prevsize = 0; // size of prev buffer |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1440 | long maxline = MAXLNUM; |
| 1441 | long cnt = 0; |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1442 | char_u *p; // position in buf |
| 1443 | char_u *start; // start of current line |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1444 | |
| 1445 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 1446 | { |
| 1447 | if (STRCMP(tv_get_string(&argvars[1]), "b") == 0) |
| 1448 | binary = TRUE; |
| 1449 | if (STRCMP(tv_get_string(&argvars[1]), "B") == 0) |
| 1450 | blob = TRUE; |
| 1451 | |
| 1452 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 1453 | maxline = (long)tv_get_number(&argvars[2]); |
| 1454 | } |
| 1455 | |
Bram Moolenaar | 15352dc | 2020-04-06 21:12:42 +0200 | [diff] [blame] | 1456 | if ((blob ? rettv_blob_alloc(rettv) : rettv_list_alloc(rettv)) == FAIL) |
| 1457 | return; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1458 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1459 | // Always open the file in binary mode, library functions have a mind of |
| 1460 | // their own about CR-LF conversion. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1461 | fname = tv_get_string(&argvars[0]); |
Bram Moolenaar | 15352dc | 2020-04-06 21:12:42 +0200 | [diff] [blame] | 1462 | |
| 1463 | if (mch_isdir(fname)) |
| 1464 | { |
| 1465 | semsg(_(e_isadir2), fname); |
| 1466 | return; |
| 1467 | } |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1468 | if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL) |
| 1469 | { |
| 1470 | semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname); |
| 1471 | return; |
| 1472 | } |
| 1473 | |
| 1474 | if (blob) |
| 1475 | { |
| 1476 | if (read_blob(fd, rettv->vval.v_blob) == FAIL) |
| 1477 | { |
Bram Moolenaar | 15352dc | 2020-04-06 21:12:42 +0200 | [diff] [blame] | 1478 | semsg(_(e_notread), fname); |
| 1479 | // An empty blob is returned on error. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1480 | blob_free(rettv->vval.v_blob); |
Bram Moolenaar | 15352dc | 2020-04-06 21:12:42 +0200 | [diff] [blame] | 1481 | rettv->vval.v_blob = NULL; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1482 | } |
| 1483 | fclose(fd); |
| 1484 | return; |
| 1485 | } |
| 1486 | |
| 1487 | while (cnt < maxline || maxline < 0) |
| 1488 | { |
| 1489 | readlen = (int)fread(buf, 1, io_size, fd); |
| 1490 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1491 | // This for loop processes what was read, but is also entered at end |
| 1492 | // of file so that either: |
| 1493 | // - an incomplete line gets written |
| 1494 | // - a "binary" file gets an empty line at the end if it ends in a |
| 1495 | // newline. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1496 | for (p = buf, start = buf; |
| 1497 | p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary)); |
| 1498 | ++p) |
| 1499 | { |
| 1500 | if (*p == '\n' || readlen <= 0) |
| 1501 | { |
| 1502 | listitem_T *li; |
| 1503 | char_u *s = NULL; |
| 1504 | long_u len = p - start; |
| 1505 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1506 | // Finished a line. Remove CRs before NL. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1507 | if (readlen > 0 && !binary) |
| 1508 | { |
| 1509 | while (len > 0 && start[len - 1] == '\r') |
| 1510 | --len; |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1511 | // removal may cross back to the "prev" string |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1512 | if (len == 0) |
| 1513 | while (prevlen > 0 && prev[prevlen - 1] == '\r') |
| 1514 | --prevlen; |
| 1515 | } |
| 1516 | if (prevlen == 0) |
| 1517 | s = vim_strnsave(start, (int)len); |
| 1518 | else |
| 1519 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1520 | // Change "prev" buffer to be the right size. This way |
| 1521 | // the bytes are only copied once, and very long lines are |
| 1522 | // allocated only once. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1523 | if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL) |
| 1524 | { |
| 1525 | mch_memmove(s + prevlen, start, len); |
| 1526 | s[prevlen + len] = NUL; |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1527 | prev = NULL; // the list will own the string |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1528 | prevlen = prevsize = 0; |
| 1529 | } |
| 1530 | } |
| 1531 | if (s == NULL) |
| 1532 | { |
| 1533 | do_outofmem_msg((long_u) prevlen + len + 1); |
| 1534 | failed = TRUE; |
| 1535 | break; |
| 1536 | } |
| 1537 | |
| 1538 | if ((li = listitem_alloc()) == NULL) |
| 1539 | { |
| 1540 | vim_free(s); |
| 1541 | failed = TRUE; |
| 1542 | break; |
| 1543 | } |
| 1544 | li->li_tv.v_type = VAR_STRING; |
| 1545 | li->li_tv.v_lock = 0; |
| 1546 | li->li_tv.vval.v_string = s; |
| 1547 | list_append(rettv->vval.v_list, li); |
| 1548 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1549 | start = p + 1; // step over newline |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1550 | if ((++cnt >= maxline && maxline >= 0) || readlen <= 0) |
| 1551 | break; |
| 1552 | } |
| 1553 | else if (*p == NUL) |
| 1554 | *p = '\n'; |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1555 | // Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this |
| 1556 | // when finding the BF and check the previous two bytes. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1557 | else if (*p == 0xbf && enc_utf8 && !binary) |
| 1558 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1559 | // Find the two bytes before the 0xbf. If p is at buf, or buf |
| 1560 | // + 1, these may be in the "prev" string. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1561 | char_u back1 = p >= buf + 1 ? p[-1] |
| 1562 | : prevlen >= 1 ? prev[prevlen - 1] : NUL; |
| 1563 | char_u back2 = p >= buf + 2 ? p[-2] |
| 1564 | : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1] |
| 1565 | : prevlen >= 2 ? prev[prevlen - 2] : NUL; |
| 1566 | |
| 1567 | if (back2 == 0xef && back1 == 0xbb) |
| 1568 | { |
| 1569 | char_u *dest = p - 2; |
| 1570 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1571 | // Usually a BOM is at the beginning of a file, and so at |
| 1572 | // the beginning of a line; then we can just step over it. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1573 | if (start == dest) |
| 1574 | start = p + 1; |
| 1575 | else |
| 1576 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1577 | // have to shuffle buf to close gap |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1578 | int adjust_prevlen = 0; |
| 1579 | |
| 1580 | if (dest < buf) |
| 1581 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1582 | adjust_prevlen = (int)(buf - dest); // must be 1 or 2 |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1583 | dest = buf; |
| 1584 | } |
| 1585 | if (readlen > p - buf + 1) |
| 1586 | mch_memmove(dest, p + 1, readlen - (p - buf) - 1); |
| 1587 | readlen -= 3 - adjust_prevlen; |
| 1588 | prevlen -= adjust_prevlen; |
| 1589 | p = dest - 1; |
| 1590 | } |
| 1591 | } |
| 1592 | } |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1593 | } // for |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1594 | |
| 1595 | if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0) |
| 1596 | break; |
| 1597 | if (start < p) |
| 1598 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1599 | // There's part of a line in buf, store it in "prev". |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1600 | if (p - start + prevlen >= prevsize) |
| 1601 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1602 | // need bigger "prev" buffer |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1603 | char_u *newprev; |
| 1604 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1605 | // A common use case is ordinary text files and "prev" gets a |
| 1606 | // fragment of a line, so the first allocation is made |
| 1607 | // small, to avoid repeatedly 'allocing' large and |
| 1608 | // 'reallocing' small. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1609 | if (prevsize == 0) |
| 1610 | prevsize = (long)(p - start); |
| 1611 | else |
| 1612 | { |
| 1613 | long grow50pc = (prevsize * 3) / 2; |
| 1614 | long growmin = (long)((p - start) * 2 + prevlen); |
| 1615 | prevsize = grow50pc > growmin ? grow50pc : growmin; |
| 1616 | } |
| 1617 | newprev = vim_realloc(prev, prevsize); |
| 1618 | if (newprev == NULL) |
| 1619 | { |
| 1620 | do_outofmem_msg((long_u)prevsize); |
| 1621 | failed = TRUE; |
| 1622 | break; |
| 1623 | } |
| 1624 | prev = newprev; |
| 1625 | } |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1626 | // Add the line part to end of "prev". |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1627 | mch_memmove(prev + prevlen, start, p - start); |
| 1628 | prevlen += (long)(p - start); |
| 1629 | } |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1630 | } // while |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1631 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1632 | // For a negative line count use only the lines at the end of the file, |
| 1633 | // free the rest. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1634 | if (!failed && maxline < 0) |
| 1635 | while (cnt > -maxline) |
| 1636 | { |
| 1637 | listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first); |
| 1638 | --cnt; |
| 1639 | } |
| 1640 | |
| 1641 | if (failed) |
| 1642 | { |
| 1643 | // an empty list is returned on error |
| 1644 | list_free(rettv->vval.v_list); |
| 1645 | rettv_list_alloc(rettv); |
| 1646 | } |
| 1647 | |
| 1648 | vim_free(prev); |
| 1649 | fclose(fd); |
| 1650 | } |
| 1651 | |
| 1652 | /* |
| 1653 | * "resolve()" function |
| 1654 | */ |
| 1655 | void |
| 1656 | f_resolve(typval_T *argvars, typval_T *rettv) |
| 1657 | { |
| 1658 | char_u *p; |
| 1659 | #ifdef HAVE_READLINK |
| 1660 | char_u *buf = NULL; |
| 1661 | #endif |
| 1662 | |
| 1663 | p = tv_get_string(&argvars[0]); |
| 1664 | #ifdef FEAT_SHORTCUT |
| 1665 | { |
| 1666 | char_u *v = NULL; |
| 1667 | |
| 1668 | v = mch_resolve_path(p, TRUE); |
| 1669 | if (v != NULL) |
| 1670 | rettv->vval.v_string = v; |
| 1671 | else |
| 1672 | rettv->vval.v_string = vim_strsave(p); |
| 1673 | } |
| 1674 | #else |
| 1675 | # ifdef HAVE_READLINK |
| 1676 | { |
| 1677 | char_u *cpy; |
| 1678 | int len; |
| 1679 | char_u *remain = NULL; |
| 1680 | char_u *q; |
| 1681 | int is_relative_to_current = FALSE; |
| 1682 | int has_trailing_pathsep = FALSE; |
| 1683 | int limit = 100; |
| 1684 | |
| 1685 | p = vim_strsave(p); |
Bram Moolenaar | 70188f5 | 2019-12-23 18:18:52 +0100 | [diff] [blame] | 1686 | if (p == NULL) |
| 1687 | goto fail; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1688 | if (p[0] == '.' && (vim_ispathsep(p[1]) |
| 1689 | || (p[1] == '.' && (vim_ispathsep(p[2]))))) |
| 1690 | is_relative_to_current = TRUE; |
| 1691 | |
| 1692 | len = STRLEN(p); |
| 1693 | if (len > 0 && after_pathsep(p, p + len)) |
| 1694 | { |
| 1695 | has_trailing_pathsep = TRUE; |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1696 | p[len - 1] = NUL; // the trailing slash breaks readlink() |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | q = getnextcomp(p); |
| 1700 | if (*q != NUL) |
| 1701 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1702 | // Separate the first path component in "p", and keep the |
| 1703 | // remainder (beginning with the path separator). |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1704 | remain = vim_strsave(q - 1); |
| 1705 | q[-1] = NUL; |
| 1706 | } |
| 1707 | |
| 1708 | buf = alloc(MAXPATHL + 1); |
| 1709 | if (buf == NULL) |
Bram Moolenaar | 70188f5 | 2019-12-23 18:18:52 +0100 | [diff] [blame] | 1710 | { |
| 1711 | vim_free(p); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1712 | goto fail; |
Bram Moolenaar | 70188f5 | 2019-12-23 18:18:52 +0100 | [diff] [blame] | 1713 | } |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1714 | |
| 1715 | for (;;) |
| 1716 | { |
| 1717 | for (;;) |
| 1718 | { |
| 1719 | len = readlink((char *)p, (char *)buf, MAXPATHL); |
| 1720 | if (len <= 0) |
| 1721 | break; |
| 1722 | buf[len] = NUL; |
| 1723 | |
| 1724 | if (limit-- == 0) |
| 1725 | { |
| 1726 | vim_free(p); |
| 1727 | vim_free(remain); |
| 1728 | emsg(_("E655: Too many symbolic links (cycle?)")); |
| 1729 | rettv->vval.v_string = NULL; |
| 1730 | goto fail; |
| 1731 | } |
| 1732 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1733 | // Ensure that the result will have a trailing path separator |
| 1734 | // if the argument has one. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1735 | if (remain == NULL && has_trailing_pathsep) |
| 1736 | add_pathsep(buf); |
| 1737 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1738 | // Separate the first path component in the link value and |
| 1739 | // concatenate the remainders. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1740 | q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf); |
| 1741 | if (*q != NUL) |
| 1742 | { |
| 1743 | if (remain == NULL) |
| 1744 | remain = vim_strsave(q - 1); |
| 1745 | else |
| 1746 | { |
| 1747 | cpy = concat_str(q - 1, remain); |
| 1748 | if (cpy != NULL) |
| 1749 | { |
| 1750 | vim_free(remain); |
| 1751 | remain = cpy; |
| 1752 | } |
| 1753 | } |
| 1754 | q[-1] = NUL; |
| 1755 | } |
| 1756 | |
| 1757 | q = gettail(p); |
| 1758 | if (q > p && *q == NUL) |
| 1759 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1760 | // Ignore trailing path separator. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1761 | q[-1] = NUL; |
| 1762 | q = gettail(p); |
| 1763 | } |
| 1764 | if (q > p && !mch_isFullName(buf)) |
| 1765 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1766 | // symlink is relative to directory of argument |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1767 | cpy = alloc(STRLEN(p) + STRLEN(buf) + 1); |
| 1768 | if (cpy != NULL) |
| 1769 | { |
| 1770 | STRCPY(cpy, p); |
| 1771 | STRCPY(gettail(cpy), buf); |
| 1772 | vim_free(p); |
| 1773 | p = cpy; |
| 1774 | } |
| 1775 | } |
| 1776 | else |
| 1777 | { |
| 1778 | vim_free(p); |
| 1779 | p = vim_strsave(buf); |
| 1780 | } |
| 1781 | } |
| 1782 | |
| 1783 | if (remain == NULL) |
| 1784 | break; |
| 1785 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1786 | // Append the first path component of "remain" to "p". |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1787 | q = getnextcomp(remain + 1); |
| 1788 | len = q - remain - (*q != NUL); |
| 1789 | cpy = vim_strnsave(p, STRLEN(p) + len); |
| 1790 | if (cpy != NULL) |
| 1791 | { |
| 1792 | STRNCAT(cpy, remain, len); |
| 1793 | vim_free(p); |
| 1794 | p = cpy; |
| 1795 | } |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1796 | // Shorten "remain". |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1797 | if (*q != NUL) |
| 1798 | STRMOVE(remain, q - 1); |
| 1799 | else |
| 1800 | VIM_CLEAR(remain); |
| 1801 | } |
| 1802 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1803 | // If the result is a relative path name, make it explicitly relative to |
| 1804 | // the current directory if and only if the argument had this form. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1805 | if (!vim_ispathsep(*p)) |
| 1806 | { |
| 1807 | if (is_relative_to_current |
| 1808 | && *p != NUL |
| 1809 | && !(p[0] == '.' |
| 1810 | && (p[1] == NUL |
| 1811 | || vim_ispathsep(p[1]) |
| 1812 | || (p[1] == '.' |
| 1813 | && (p[2] == NUL |
| 1814 | || vim_ispathsep(p[2])))))) |
| 1815 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1816 | // Prepend "./". |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1817 | cpy = concat_str((char_u *)"./", p); |
| 1818 | if (cpy != NULL) |
| 1819 | { |
| 1820 | vim_free(p); |
| 1821 | p = cpy; |
| 1822 | } |
| 1823 | } |
| 1824 | else if (!is_relative_to_current) |
| 1825 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1826 | // Strip leading "./". |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1827 | q = p; |
| 1828 | while (q[0] == '.' && vim_ispathsep(q[1])) |
| 1829 | q += 2; |
| 1830 | if (q > p) |
| 1831 | STRMOVE(p, p + 2); |
| 1832 | } |
| 1833 | } |
| 1834 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1835 | // Ensure that the result will have no trailing path separator |
| 1836 | // if the argument had none. But keep "/" or "//". |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1837 | if (!has_trailing_pathsep) |
| 1838 | { |
| 1839 | q = p + STRLEN(p); |
| 1840 | if (after_pathsep(p, q)) |
| 1841 | *gettail_sep(p) = NUL; |
| 1842 | } |
| 1843 | |
| 1844 | rettv->vval.v_string = p; |
| 1845 | } |
| 1846 | # else |
| 1847 | rettv->vval.v_string = vim_strsave(p); |
| 1848 | # endif |
| 1849 | #endif |
| 1850 | |
| 1851 | simplify_filename(rettv->vval.v_string); |
| 1852 | |
| 1853 | #ifdef HAVE_READLINK |
| 1854 | fail: |
| 1855 | vim_free(buf); |
| 1856 | #endif |
| 1857 | rettv->v_type = VAR_STRING; |
| 1858 | } |
| 1859 | |
| 1860 | /* |
| 1861 | * "tempname()" function |
| 1862 | */ |
| 1863 | void |
| 1864 | f_tempname(typval_T *argvars UNUSED, typval_T *rettv) |
| 1865 | { |
| 1866 | static int x = 'A'; |
| 1867 | |
| 1868 | rettv->v_type = VAR_STRING; |
| 1869 | rettv->vval.v_string = vim_tempname(x, FALSE); |
| 1870 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1871 | // Advance 'x' to use A-Z and 0-9, so that there are at least 34 different |
| 1872 | // names. Skip 'I' and 'O', they are used for shell redirection. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1873 | do |
| 1874 | { |
| 1875 | if (x == 'Z') |
| 1876 | x = '0'; |
| 1877 | else if (x == '9') |
| 1878 | x = 'A'; |
| 1879 | else |
| 1880 | { |
| 1881 | #ifdef EBCDIC |
| 1882 | if (x == 'I') |
| 1883 | x = 'J'; |
| 1884 | else if (x == 'R') |
| 1885 | x = 'S'; |
| 1886 | else |
| 1887 | #endif |
| 1888 | ++x; |
| 1889 | } |
| 1890 | } while (x == 'I' || x == 'O'); |
| 1891 | } |
| 1892 | |
| 1893 | /* |
| 1894 | * "writefile()" function |
| 1895 | */ |
| 1896 | void |
| 1897 | f_writefile(typval_T *argvars, typval_T *rettv) |
| 1898 | { |
| 1899 | int binary = FALSE; |
| 1900 | int append = FALSE; |
| 1901 | #ifdef HAVE_FSYNC |
| 1902 | int do_fsync = p_fs; |
| 1903 | #endif |
| 1904 | char_u *fname; |
| 1905 | FILE *fd; |
| 1906 | int ret = 0; |
| 1907 | listitem_T *li; |
| 1908 | list_T *list = NULL; |
| 1909 | blob_T *blob = NULL; |
| 1910 | |
| 1911 | rettv->vval.v_number = -1; |
| 1912 | if (check_secure()) |
| 1913 | return; |
| 1914 | |
| 1915 | if (argvars[0].v_type == VAR_LIST) |
| 1916 | { |
| 1917 | list = argvars[0].vval.v_list; |
| 1918 | if (list == NULL) |
| 1919 | return; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1920 | range_list_materialize(list); |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 1921 | FOR_ALL_LIST_ITEMS(list, li) |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1922 | if (tv_get_string_chk(&li->li_tv) == NULL) |
| 1923 | return; |
| 1924 | } |
| 1925 | else if (argvars[0].v_type == VAR_BLOB) |
| 1926 | { |
| 1927 | blob = argvars[0].vval.v_blob; |
| 1928 | if (blob == NULL) |
| 1929 | return; |
| 1930 | } |
| 1931 | else |
| 1932 | { |
Bram Moolenaar | 18a2b87 | 2020-03-19 13:08:45 +0100 | [diff] [blame] | 1933 | semsg(_(e_invarg2), |
| 1934 | _("writefile() first argument must be a List or a Blob")); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1935 | return; |
| 1936 | } |
| 1937 | |
| 1938 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 1939 | { |
| 1940 | char_u *arg2 = tv_get_string_chk(&argvars[2]); |
| 1941 | |
| 1942 | if (arg2 == NULL) |
| 1943 | return; |
| 1944 | if (vim_strchr(arg2, 'b') != NULL) |
| 1945 | binary = TRUE; |
| 1946 | if (vim_strchr(arg2, 'a') != NULL) |
| 1947 | append = TRUE; |
| 1948 | #ifdef HAVE_FSYNC |
| 1949 | if (vim_strchr(arg2, 's') != NULL) |
| 1950 | do_fsync = TRUE; |
| 1951 | else if (vim_strchr(arg2, 'S') != NULL) |
| 1952 | do_fsync = FALSE; |
| 1953 | #endif |
| 1954 | } |
| 1955 | |
| 1956 | fname = tv_get_string_chk(&argvars[1]); |
| 1957 | if (fname == NULL) |
| 1958 | return; |
| 1959 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1960 | // Always open the file in binary mode, library functions have a mind of |
| 1961 | // their own about CR-LF conversion. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1962 | if (*fname == NUL || (fd = mch_fopen((char *)fname, |
| 1963 | append ? APPENDBIN : WRITEBIN)) == NULL) |
| 1964 | { |
| 1965 | semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname); |
| 1966 | ret = -1; |
| 1967 | } |
| 1968 | else if (blob) |
| 1969 | { |
| 1970 | if (write_blob(fd, blob) == FAIL) |
| 1971 | ret = -1; |
| 1972 | #ifdef HAVE_FSYNC |
| 1973 | else if (do_fsync) |
| 1974 | // Ignore the error, the user wouldn't know what to do about it. |
| 1975 | // May happen for a device. |
| 1976 | vim_ignored = vim_fsync(fileno(fd)); |
| 1977 | #endif |
| 1978 | fclose(fd); |
| 1979 | } |
| 1980 | else |
| 1981 | { |
| 1982 | if (write_list(fd, list, binary) == FAIL) |
| 1983 | ret = -1; |
| 1984 | #ifdef HAVE_FSYNC |
| 1985 | else if (do_fsync) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1986 | // Ignore the error, the user wouldn't know what to do about it. |
| 1987 | // May happen for a device. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1988 | vim_ignored = vim_fsync(fileno(fd)); |
| 1989 | #endif |
| 1990 | fclose(fd); |
| 1991 | } |
| 1992 | |
| 1993 | rettv->vval.v_number = ret; |
| 1994 | } |
| 1995 | |
| 1996 | #endif // FEAT_EVAL |
| 1997 | |
| 1998 | #if defined(FEAT_BROWSE) || defined(PROTO) |
| 1999 | /* |
| 2000 | * Generic browse function. Calls gui_mch_browse() when possible. |
| 2001 | * Later this may pop-up a non-GUI file selector (external command?). |
| 2002 | */ |
| 2003 | char_u * |
| 2004 | do_browse( |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2005 | int flags, // BROWSE_SAVE and BROWSE_DIR |
| 2006 | char_u *title, // title for the window |
| 2007 | char_u *dflt, // default file name (may include directory) |
| 2008 | char_u *ext, // extension added |
| 2009 | char_u *initdir, // initial directory, NULL for current dir or |
| 2010 | // when using path from "dflt" |
| 2011 | char_u *filter, // file name filter |
| 2012 | buf_T *buf) // buffer to read/write for |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2013 | { |
| 2014 | char_u *fname; |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2015 | static char_u *last_dir = NULL; // last used directory |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2016 | char_u *tofree = NULL; |
| 2017 | int save_browse = cmdmod.browse; |
| 2018 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2019 | // Must turn off browse to avoid that autocommands will get the |
| 2020 | // flag too! |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2021 | cmdmod.browse = FALSE; |
| 2022 | |
| 2023 | if (title == NULL || *title == NUL) |
| 2024 | { |
| 2025 | if (flags & BROWSE_DIR) |
| 2026 | title = (char_u *)_("Select Directory dialog"); |
| 2027 | else if (flags & BROWSE_SAVE) |
| 2028 | title = (char_u *)_("Save File dialog"); |
| 2029 | else |
| 2030 | title = (char_u *)_("Open File dialog"); |
| 2031 | } |
| 2032 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2033 | // When no directory specified, use default file name, default dir, buffer |
| 2034 | // dir, last dir or current dir |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2035 | if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL) |
| 2036 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2037 | if (mch_isdir(dflt)) // default file name is a directory |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2038 | { |
| 2039 | initdir = dflt; |
| 2040 | dflt = NULL; |
| 2041 | } |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2042 | else if (gettail(dflt) != dflt) // default file name includes a path |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2043 | { |
| 2044 | tofree = vim_strsave(dflt); |
| 2045 | if (tofree != NULL) |
| 2046 | { |
| 2047 | initdir = tofree; |
| 2048 | *gettail(initdir) = NUL; |
| 2049 | dflt = gettail(dflt); |
| 2050 | } |
| 2051 | } |
| 2052 | } |
| 2053 | |
| 2054 | if (initdir == NULL || *initdir == NUL) |
| 2055 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2056 | // When 'browsedir' is a directory, use it |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2057 | if (STRCMP(p_bsdir, "last") != 0 |
| 2058 | && STRCMP(p_bsdir, "buffer") != 0 |
| 2059 | && STRCMP(p_bsdir, "current") != 0 |
| 2060 | && mch_isdir(p_bsdir)) |
| 2061 | initdir = p_bsdir; |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2062 | // When saving or 'browsedir' is "buffer", use buffer fname |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2063 | else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b') |
| 2064 | && buf != NULL && buf->b_ffname != NULL) |
| 2065 | { |
| 2066 | if (dflt == NULL || *dflt == NUL) |
| 2067 | dflt = gettail(curbuf->b_ffname); |
| 2068 | tofree = vim_strsave(curbuf->b_ffname); |
| 2069 | if (tofree != NULL) |
| 2070 | { |
| 2071 | initdir = tofree; |
| 2072 | *gettail(initdir) = NUL; |
| 2073 | } |
| 2074 | } |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2075 | // When 'browsedir' is "last", use dir from last browse |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2076 | else if (*p_bsdir == 'l') |
| 2077 | initdir = last_dir; |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2078 | // When 'browsedir is "current", use current directory. This is the |
| 2079 | // default already, leave initdir empty. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2080 | } |
| 2081 | |
| 2082 | # ifdef FEAT_GUI |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2083 | if (gui.in_use) // when this changes, also adjust f_has()! |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2084 | { |
| 2085 | if (filter == NULL |
| 2086 | # ifdef FEAT_EVAL |
| 2087 | && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL |
| 2088 | && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL |
| 2089 | # endif |
| 2090 | ) |
| 2091 | filter = BROWSE_FILTER_DEFAULT; |
| 2092 | if (flags & BROWSE_DIR) |
| 2093 | { |
| 2094 | # if defined(FEAT_GUI_GTK) || defined(MSWIN) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2095 | // For systems that have a directory dialog. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2096 | fname = gui_mch_browsedir(title, initdir); |
| 2097 | # else |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2098 | // Generic solution for selecting a directory: select a file and |
| 2099 | // remove the file name. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2100 | fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)""); |
| 2101 | # endif |
| 2102 | # if !defined(FEAT_GUI_GTK) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2103 | // Win32 adds a dummy file name, others return an arbitrary file |
| 2104 | // name. GTK+ 2 returns only the directory, |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2105 | if (fname != NULL && *fname != NUL && !mch_isdir(fname)) |
| 2106 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2107 | // Remove the file name. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2108 | char_u *tail = gettail_sep(fname); |
| 2109 | |
| 2110 | if (tail == fname) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2111 | *tail++ = '.'; // use current dir |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2112 | *tail = NUL; |
| 2113 | } |
| 2114 | # endif |
| 2115 | } |
| 2116 | else |
| 2117 | fname = gui_mch_browse(flags & BROWSE_SAVE, |
| 2118 | title, dflt, ext, initdir, (char_u *)_(filter)); |
| 2119 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2120 | // We hang around in the dialog for a while, the user might do some |
| 2121 | // things to our files. The Win32 dialog allows deleting or renaming |
| 2122 | // a file, check timestamps. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2123 | need_check_timestamps = TRUE; |
| 2124 | did_check_timestamps = FALSE; |
| 2125 | } |
| 2126 | else |
| 2127 | # endif |
| 2128 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2129 | // TODO: non-GUI file selector here |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2130 | emsg(_("E338: Sorry, no file browser in console mode")); |
| 2131 | fname = NULL; |
| 2132 | } |
| 2133 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2134 | // keep the directory for next time |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2135 | if (fname != NULL) |
| 2136 | { |
| 2137 | vim_free(last_dir); |
| 2138 | last_dir = vim_strsave(fname); |
| 2139 | if (last_dir != NULL && !(flags & BROWSE_DIR)) |
| 2140 | { |
| 2141 | *gettail(last_dir) = NUL; |
| 2142 | if (*last_dir == NUL) |
| 2143 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2144 | // filename only returned, must be in current dir |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2145 | vim_free(last_dir); |
| 2146 | last_dir = alloc(MAXPATHL); |
| 2147 | if (last_dir != NULL) |
| 2148 | mch_dirname(last_dir, MAXPATHL); |
| 2149 | } |
| 2150 | } |
| 2151 | } |
| 2152 | |
| 2153 | vim_free(tofree); |
| 2154 | cmdmod.browse = save_browse; |
| 2155 | |
| 2156 | return fname; |
| 2157 | } |
| 2158 | #endif |
| 2159 | |
| 2160 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 2161 | |
| 2162 | /* |
| 2163 | * "browse(save, title, initdir, default)" function |
| 2164 | */ |
| 2165 | void |
| 2166 | f_browse(typval_T *argvars UNUSED, typval_T *rettv) |
| 2167 | { |
| 2168 | # ifdef FEAT_BROWSE |
| 2169 | int save; |
| 2170 | char_u *title; |
| 2171 | char_u *initdir; |
| 2172 | char_u *defname; |
| 2173 | char_u buf[NUMBUFLEN]; |
| 2174 | char_u buf2[NUMBUFLEN]; |
| 2175 | int error = FALSE; |
| 2176 | |
| 2177 | save = (int)tv_get_number_chk(&argvars[0], &error); |
| 2178 | title = tv_get_string_chk(&argvars[1]); |
| 2179 | initdir = tv_get_string_buf_chk(&argvars[2], buf); |
| 2180 | defname = tv_get_string_buf_chk(&argvars[3], buf2); |
| 2181 | |
| 2182 | if (error || title == NULL || initdir == NULL || defname == NULL) |
| 2183 | rettv->vval.v_string = NULL; |
| 2184 | else |
| 2185 | rettv->vval.v_string = |
| 2186 | do_browse(save ? BROWSE_SAVE : 0, |
| 2187 | title, defname, NULL, initdir, NULL, curbuf); |
| 2188 | # else |
| 2189 | rettv->vval.v_string = NULL; |
| 2190 | # endif |
| 2191 | rettv->v_type = VAR_STRING; |
| 2192 | } |
| 2193 | |
| 2194 | /* |
| 2195 | * "browsedir(title, initdir)" function |
| 2196 | */ |
| 2197 | void |
| 2198 | f_browsedir(typval_T *argvars UNUSED, typval_T *rettv) |
| 2199 | { |
| 2200 | # ifdef FEAT_BROWSE |
| 2201 | char_u *title; |
| 2202 | char_u *initdir; |
| 2203 | char_u buf[NUMBUFLEN]; |
| 2204 | |
| 2205 | title = tv_get_string_chk(&argvars[0]); |
| 2206 | initdir = tv_get_string_buf_chk(&argvars[1], buf); |
| 2207 | |
| 2208 | if (title == NULL || initdir == NULL) |
| 2209 | rettv->vval.v_string = NULL; |
| 2210 | else |
| 2211 | rettv->vval.v_string = do_browse(BROWSE_DIR, |
| 2212 | title, NULL, NULL, initdir, NULL, curbuf); |
| 2213 | # else |
| 2214 | rettv->vval.v_string = NULL; |
| 2215 | # endif |
| 2216 | rettv->v_type = VAR_STRING; |
| 2217 | } |
| 2218 | |
| 2219 | #endif // FEAT_EVAL |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2220 | |
| 2221 | /* |
| 2222 | * Replace home directory by "~" in each space or comma separated file name in |
| 2223 | * 'src'. |
| 2224 | * If anything fails (except when out of space) dst equals src. |
| 2225 | */ |
| 2226 | void |
| 2227 | home_replace( |
| 2228 | buf_T *buf, // when not NULL, check for help files |
| 2229 | char_u *src, // input file name |
| 2230 | char_u *dst, // where to put the result |
| 2231 | int dstlen, // maximum length of the result |
| 2232 | int one) // if TRUE, only replace one file name, include |
| 2233 | // spaces and commas in the file name. |
| 2234 | { |
| 2235 | size_t dirlen = 0, envlen = 0; |
| 2236 | size_t len; |
| 2237 | char_u *homedir_env, *homedir_env_orig; |
| 2238 | char_u *p; |
| 2239 | |
| 2240 | if (src == NULL) |
| 2241 | { |
| 2242 | *dst = NUL; |
| 2243 | return; |
| 2244 | } |
| 2245 | |
| 2246 | /* |
| 2247 | * If the file is a help file, remove the path completely. |
| 2248 | */ |
| 2249 | if (buf != NULL && buf->b_help) |
| 2250 | { |
| 2251 | vim_snprintf((char *)dst, dstlen, "%s", gettail(src)); |
| 2252 | return; |
| 2253 | } |
| 2254 | |
| 2255 | /* |
| 2256 | * We check both the value of the $HOME environment variable and the |
| 2257 | * "real" home directory. |
| 2258 | */ |
| 2259 | if (homedir != NULL) |
| 2260 | dirlen = STRLEN(homedir); |
| 2261 | |
| 2262 | #ifdef VMS |
| 2263 | homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN"); |
| 2264 | #else |
| 2265 | homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME"); |
| 2266 | #endif |
| 2267 | #ifdef MSWIN |
| 2268 | if (homedir_env == NULL) |
| 2269 | homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE"); |
| 2270 | #endif |
| 2271 | // Empty is the same as not set. |
| 2272 | if (homedir_env != NULL && *homedir_env == NUL) |
| 2273 | homedir_env = NULL; |
| 2274 | |
| 2275 | if (homedir_env != NULL && *homedir_env == '~') |
| 2276 | { |
| 2277 | int usedlen = 0; |
| 2278 | int flen; |
| 2279 | char_u *fbuf = NULL; |
| 2280 | |
| 2281 | flen = (int)STRLEN(homedir_env); |
| 2282 | (void)modify_fname((char_u *)":p", FALSE, &usedlen, |
| 2283 | &homedir_env, &fbuf, &flen); |
| 2284 | flen = (int)STRLEN(homedir_env); |
| 2285 | if (flen > 0 && vim_ispathsep(homedir_env[flen - 1])) |
| 2286 | // Remove the trailing / that is added to a directory. |
| 2287 | homedir_env[flen - 1] = NUL; |
| 2288 | } |
| 2289 | |
| 2290 | if (homedir_env != NULL) |
| 2291 | envlen = STRLEN(homedir_env); |
| 2292 | |
| 2293 | if (!one) |
| 2294 | src = skipwhite(src); |
| 2295 | while (*src && dstlen > 0) |
| 2296 | { |
| 2297 | /* |
| 2298 | * Here we are at the beginning of a file name. |
| 2299 | * First, check to see if the beginning of the file name matches |
| 2300 | * $HOME or the "real" home directory. Check that there is a '/' |
| 2301 | * after the match (so that if e.g. the file is "/home/pieter/bla", |
| 2302 | * and the home directory is "/home/piet", the file does not end up |
| 2303 | * as "~er/bla" (which would seem to indicate the file "bla" in user |
| 2304 | * er's home directory)). |
| 2305 | */ |
| 2306 | p = homedir; |
| 2307 | len = dirlen; |
| 2308 | for (;;) |
| 2309 | { |
| 2310 | if ( len |
| 2311 | && fnamencmp(src, p, len) == 0 |
| 2312 | && (vim_ispathsep(src[len]) |
| 2313 | || (!one && (src[len] == ',' || src[len] == ' ')) |
| 2314 | || src[len] == NUL)) |
| 2315 | { |
| 2316 | src += len; |
| 2317 | if (--dstlen > 0) |
| 2318 | *dst++ = '~'; |
| 2319 | |
| 2320 | /* |
| 2321 | * If it's just the home directory, add "/". |
| 2322 | */ |
| 2323 | if (!vim_ispathsep(src[0]) && --dstlen > 0) |
| 2324 | *dst++ = '/'; |
| 2325 | break; |
| 2326 | } |
| 2327 | if (p == homedir_env) |
| 2328 | break; |
| 2329 | p = homedir_env; |
| 2330 | len = envlen; |
| 2331 | } |
| 2332 | |
| 2333 | // if (!one) skip to separator: space or comma |
| 2334 | while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0) |
| 2335 | *dst++ = *src++; |
| 2336 | // skip separator |
| 2337 | while ((*src == ' ' || *src == ',') && --dstlen > 0) |
| 2338 | *dst++ = *src++; |
| 2339 | } |
| 2340 | // if (dstlen == 0) out of space, what to do??? |
| 2341 | |
| 2342 | *dst = NUL; |
| 2343 | |
| 2344 | if (homedir_env != homedir_env_orig) |
| 2345 | vim_free(homedir_env); |
| 2346 | } |
| 2347 | |
| 2348 | /* |
| 2349 | * Like home_replace, store the replaced string in allocated memory. |
| 2350 | * When something fails, NULL is returned. |
| 2351 | */ |
| 2352 | char_u * |
| 2353 | home_replace_save( |
| 2354 | buf_T *buf, // when not NULL, check for help files |
| 2355 | char_u *src) // input file name |
| 2356 | { |
| 2357 | char_u *dst; |
| 2358 | unsigned len; |
| 2359 | |
| 2360 | len = 3; // space for "~/" and trailing NUL |
| 2361 | if (src != NULL) // just in case |
| 2362 | len += (unsigned)STRLEN(src); |
| 2363 | dst = alloc(len); |
| 2364 | if (dst != NULL) |
| 2365 | home_replace(buf, src, dst, len, TRUE); |
| 2366 | return dst; |
| 2367 | } |
| 2368 | |
| 2369 | /* |
| 2370 | * Compare two file names and return: |
| 2371 | * FPC_SAME if they both exist and are the same file. |
| 2372 | * FPC_SAMEX if they both don't exist and have the same file name. |
| 2373 | * FPC_DIFF if they both exist and are different files. |
| 2374 | * FPC_NOTX if they both don't exist. |
| 2375 | * FPC_DIFFX if one of them doesn't exist. |
| 2376 | * For the first name environment variables are expanded if "expandenv" is |
| 2377 | * TRUE. |
| 2378 | */ |
| 2379 | int |
| 2380 | fullpathcmp( |
| 2381 | char_u *s1, |
| 2382 | char_u *s2, |
| 2383 | int checkname, // when both don't exist, check file names |
| 2384 | int expandenv) |
| 2385 | { |
| 2386 | #ifdef UNIX |
| 2387 | char_u exp1[MAXPATHL]; |
| 2388 | char_u full1[MAXPATHL]; |
| 2389 | char_u full2[MAXPATHL]; |
| 2390 | stat_T st1, st2; |
| 2391 | int r1, r2; |
| 2392 | |
| 2393 | if (expandenv) |
| 2394 | expand_env(s1, exp1, MAXPATHL); |
| 2395 | else |
| 2396 | vim_strncpy(exp1, s1, MAXPATHL - 1); |
| 2397 | r1 = mch_stat((char *)exp1, &st1); |
| 2398 | r2 = mch_stat((char *)s2, &st2); |
| 2399 | if (r1 != 0 && r2 != 0) |
| 2400 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2401 | // if mch_stat() doesn't work, may compare the names |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2402 | if (checkname) |
| 2403 | { |
| 2404 | if (fnamecmp(exp1, s2) == 0) |
| 2405 | return FPC_SAMEX; |
| 2406 | r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); |
| 2407 | r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); |
| 2408 | if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0) |
| 2409 | return FPC_SAMEX; |
| 2410 | } |
| 2411 | return FPC_NOTX; |
| 2412 | } |
| 2413 | if (r1 != 0 || r2 != 0) |
| 2414 | return FPC_DIFFX; |
| 2415 | if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) |
| 2416 | return FPC_SAME; |
| 2417 | return FPC_DIFF; |
| 2418 | #else |
| 2419 | char_u *exp1; // expanded s1 |
| 2420 | char_u *full1; // full path of s1 |
| 2421 | char_u *full2; // full path of s2 |
| 2422 | int retval = FPC_DIFF; |
| 2423 | int r1, r2; |
| 2424 | |
| 2425 | // allocate one buffer to store three paths (alloc()/free() is slow!) |
| 2426 | if ((exp1 = alloc(MAXPATHL * 3)) != NULL) |
| 2427 | { |
| 2428 | full1 = exp1 + MAXPATHL; |
| 2429 | full2 = full1 + MAXPATHL; |
| 2430 | |
| 2431 | if (expandenv) |
| 2432 | expand_env(s1, exp1, MAXPATHL); |
| 2433 | else |
| 2434 | vim_strncpy(exp1, s1, MAXPATHL - 1); |
| 2435 | r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); |
| 2436 | r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); |
| 2437 | |
| 2438 | // If vim_FullName() fails, the file probably doesn't exist. |
| 2439 | if (r1 != OK && r2 != OK) |
| 2440 | { |
| 2441 | if (checkname && fnamecmp(exp1, s2) == 0) |
| 2442 | retval = FPC_SAMEX; |
| 2443 | else |
| 2444 | retval = FPC_NOTX; |
| 2445 | } |
| 2446 | else if (r1 != OK || r2 != OK) |
| 2447 | retval = FPC_DIFFX; |
| 2448 | else if (fnamecmp(full1, full2)) |
| 2449 | retval = FPC_DIFF; |
| 2450 | else |
| 2451 | retval = FPC_SAME; |
| 2452 | vim_free(exp1); |
| 2453 | } |
| 2454 | return retval; |
| 2455 | #endif |
| 2456 | } |
| 2457 | |
| 2458 | /* |
| 2459 | * Get the tail of a path: the file name. |
| 2460 | * When the path ends in a path separator the tail is the NUL after it. |
| 2461 | * Fail safe: never returns NULL. |
| 2462 | */ |
| 2463 | char_u * |
| 2464 | gettail(char_u *fname) |
| 2465 | { |
| 2466 | char_u *p1, *p2; |
| 2467 | |
| 2468 | if (fname == NULL) |
| 2469 | return (char_u *)""; |
| 2470 | for (p1 = p2 = get_past_head(fname); *p2; ) // find last part of path |
| 2471 | { |
| 2472 | if (vim_ispathsep_nocolon(*p2)) |
| 2473 | p1 = p2 + 1; |
| 2474 | MB_PTR_ADV(p2); |
| 2475 | } |
| 2476 | return p1; |
| 2477 | } |
| 2478 | |
| 2479 | /* |
| 2480 | * Get pointer to tail of "fname", including path separators. Putting a NUL |
| 2481 | * here leaves the directory name. Takes care of "c:/" and "//". |
| 2482 | * Always returns a valid pointer. |
| 2483 | */ |
| 2484 | char_u * |
| 2485 | gettail_sep(char_u *fname) |
| 2486 | { |
| 2487 | char_u *p; |
| 2488 | char_u *t; |
| 2489 | |
| 2490 | p = get_past_head(fname); // don't remove the '/' from "c:/file" |
| 2491 | t = gettail(fname); |
| 2492 | while (t > p && after_pathsep(fname, t)) |
| 2493 | --t; |
| 2494 | #ifdef VMS |
| 2495 | // path separator is part of the path |
| 2496 | ++t; |
| 2497 | #endif |
| 2498 | return t; |
| 2499 | } |
| 2500 | |
| 2501 | /* |
| 2502 | * get the next path component (just after the next path separator). |
| 2503 | */ |
| 2504 | char_u * |
| 2505 | getnextcomp(char_u *fname) |
| 2506 | { |
| 2507 | while (*fname && !vim_ispathsep(*fname)) |
| 2508 | MB_PTR_ADV(fname); |
| 2509 | if (*fname) |
| 2510 | ++fname; |
| 2511 | return fname; |
| 2512 | } |
| 2513 | |
| 2514 | /* |
| 2515 | * Get a pointer to one character past the head of a path name. |
| 2516 | * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head. |
| 2517 | * If there is no head, path is returned. |
| 2518 | */ |
| 2519 | char_u * |
| 2520 | get_past_head(char_u *path) |
| 2521 | { |
| 2522 | char_u *retval; |
| 2523 | |
| 2524 | #if defined(MSWIN) |
| 2525 | // may skip "c:" |
| 2526 | if (isalpha(path[0]) && path[1] == ':') |
| 2527 | retval = path + 2; |
| 2528 | else |
| 2529 | retval = path; |
| 2530 | #else |
| 2531 | # if defined(AMIGA) |
| 2532 | // may skip "label:" |
| 2533 | retval = vim_strchr(path, ':'); |
| 2534 | if (retval == NULL) |
| 2535 | retval = path; |
| 2536 | # else // Unix |
| 2537 | retval = path; |
| 2538 | # endif |
| 2539 | #endif |
| 2540 | |
| 2541 | while (vim_ispathsep(*retval)) |
| 2542 | ++retval; |
| 2543 | |
| 2544 | return retval; |
| 2545 | } |
| 2546 | |
| 2547 | /* |
| 2548 | * Return TRUE if 'c' is a path separator. |
| 2549 | * Note that for MS-Windows this includes the colon. |
| 2550 | */ |
| 2551 | int |
| 2552 | vim_ispathsep(int c) |
| 2553 | { |
| 2554 | #ifdef UNIX |
| 2555 | return (c == '/'); // UNIX has ':' inside file names |
| 2556 | #else |
| 2557 | # ifdef BACKSLASH_IN_FILENAME |
| 2558 | return (c == ':' || c == '/' || c == '\\'); |
| 2559 | # else |
| 2560 | # ifdef VMS |
| 2561 | // server"user passwd"::device:[full.path.name]fname.extension;version" |
| 2562 | return (c == ':' || c == '[' || c == ']' || c == '/' |
| 2563 | || c == '<' || c == '>' || c == '"' ); |
| 2564 | # else |
| 2565 | return (c == ':' || c == '/'); |
| 2566 | # endif // VMS |
| 2567 | # endif |
| 2568 | #endif |
| 2569 | } |
| 2570 | |
| 2571 | /* |
| 2572 | * Like vim_ispathsep(c), but exclude the colon for MS-Windows. |
| 2573 | */ |
| 2574 | int |
| 2575 | vim_ispathsep_nocolon(int c) |
| 2576 | { |
| 2577 | return vim_ispathsep(c) |
| 2578 | #ifdef BACKSLASH_IN_FILENAME |
| 2579 | && c != ':' |
| 2580 | #endif |
| 2581 | ; |
| 2582 | } |
| 2583 | |
| 2584 | /* |
| 2585 | * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname" |
| 2586 | * It's done in-place. |
| 2587 | */ |
| 2588 | void |
| 2589 | shorten_dir(char_u *str) |
| 2590 | { |
| 2591 | char_u *tail, *s, *d; |
| 2592 | int skip = FALSE; |
| 2593 | |
| 2594 | tail = gettail(str); |
| 2595 | d = str; |
| 2596 | for (s = str; ; ++s) |
| 2597 | { |
| 2598 | if (s >= tail) // copy the whole tail |
| 2599 | { |
| 2600 | *d++ = *s; |
| 2601 | if (*s == NUL) |
| 2602 | break; |
| 2603 | } |
| 2604 | else if (vim_ispathsep(*s)) // copy '/' and next char |
| 2605 | { |
| 2606 | *d++ = *s; |
| 2607 | skip = FALSE; |
| 2608 | } |
| 2609 | else if (!skip) |
| 2610 | { |
| 2611 | *d++ = *s; // copy next char |
| 2612 | if (*s != '~' && *s != '.') // and leading "~" and "." |
| 2613 | skip = TRUE; |
| 2614 | if (has_mbyte) |
| 2615 | { |
| 2616 | int l = mb_ptr2len(s); |
| 2617 | |
| 2618 | while (--l > 0) |
| 2619 | *d++ = *++s; |
| 2620 | } |
| 2621 | } |
| 2622 | } |
| 2623 | } |
| 2624 | |
| 2625 | /* |
| 2626 | * Return TRUE if the directory of "fname" exists, FALSE otherwise. |
| 2627 | * Also returns TRUE if there is no directory name. |
| 2628 | * "fname" must be writable!. |
| 2629 | */ |
| 2630 | int |
| 2631 | dir_of_file_exists(char_u *fname) |
| 2632 | { |
| 2633 | char_u *p; |
| 2634 | int c; |
| 2635 | int retval; |
| 2636 | |
| 2637 | p = gettail_sep(fname); |
| 2638 | if (p == fname) |
| 2639 | return TRUE; |
| 2640 | c = *p; |
| 2641 | *p = NUL; |
| 2642 | retval = mch_isdir(fname); |
| 2643 | *p = c; |
| 2644 | return retval; |
| 2645 | } |
| 2646 | |
| 2647 | /* |
| 2648 | * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally |
| 2649 | * and deal with 'fileignorecase'. |
| 2650 | */ |
| 2651 | int |
| 2652 | vim_fnamecmp(char_u *x, char_u *y) |
| 2653 | { |
| 2654 | #ifdef BACKSLASH_IN_FILENAME |
| 2655 | return vim_fnamencmp(x, y, MAXPATHL); |
| 2656 | #else |
| 2657 | if (p_fic) |
| 2658 | return MB_STRICMP(x, y); |
| 2659 | return STRCMP(x, y); |
| 2660 | #endif |
| 2661 | } |
| 2662 | |
| 2663 | int |
| 2664 | vim_fnamencmp(char_u *x, char_u *y, size_t len) |
| 2665 | { |
| 2666 | #ifdef BACKSLASH_IN_FILENAME |
| 2667 | char_u *px = x; |
| 2668 | char_u *py = y; |
| 2669 | int cx = NUL; |
| 2670 | int cy = NUL; |
| 2671 | |
| 2672 | while (len > 0) |
| 2673 | { |
| 2674 | cx = PTR2CHAR(px); |
| 2675 | cy = PTR2CHAR(py); |
| 2676 | if (cx == NUL || cy == NUL |
| 2677 | || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy) |
| 2678 | && !(cx == '/' && cy == '\\') |
| 2679 | && !(cx == '\\' && cy == '/'))) |
| 2680 | break; |
Bram Moolenaar | 1614a14 | 2019-10-06 22:00:13 +0200 | [diff] [blame] | 2681 | len -= mb_ptr2len(px); |
| 2682 | px += mb_ptr2len(px); |
| 2683 | py += mb_ptr2len(py); |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2684 | } |
| 2685 | if (len == 0) |
| 2686 | return 0; |
| 2687 | return (cx - cy); |
| 2688 | #else |
| 2689 | if (p_fic) |
| 2690 | return MB_STRNICMP(x, y, len); |
| 2691 | return STRNCMP(x, y, len); |
| 2692 | #endif |
| 2693 | } |
| 2694 | |
| 2695 | /* |
| 2696 | * Concatenate file names fname1 and fname2 into allocated memory. |
| 2697 | * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary. |
| 2698 | */ |
| 2699 | char_u * |
| 2700 | concat_fnames(char_u *fname1, char_u *fname2, int sep) |
| 2701 | { |
| 2702 | char_u *dest; |
| 2703 | |
| 2704 | dest = alloc(STRLEN(fname1) + STRLEN(fname2) + 3); |
| 2705 | if (dest != NULL) |
| 2706 | { |
| 2707 | STRCPY(dest, fname1); |
| 2708 | if (sep) |
| 2709 | add_pathsep(dest); |
| 2710 | STRCAT(dest, fname2); |
| 2711 | } |
| 2712 | return dest; |
| 2713 | } |
| 2714 | |
| 2715 | /* |
| 2716 | * Add a path separator to a file name, unless it already ends in a path |
| 2717 | * separator. |
| 2718 | */ |
| 2719 | void |
| 2720 | add_pathsep(char_u *p) |
| 2721 | { |
| 2722 | if (*p != NUL && !after_pathsep(p, p + STRLEN(p))) |
| 2723 | STRCAT(p, PATHSEPSTR); |
| 2724 | } |
| 2725 | |
| 2726 | /* |
| 2727 | * FullName_save - Make an allocated copy of a full file name. |
| 2728 | * Returns NULL when out of memory. |
| 2729 | */ |
| 2730 | char_u * |
| 2731 | FullName_save( |
| 2732 | char_u *fname, |
| 2733 | int force) // force expansion, even when it already looks |
| 2734 | // like a full path name |
| 2735 | { |
| 2736 | char_u *buf; |
| 2737 | char_u *new_fname = NULL; |
| 2738 | |
| 2739 | if (fname == NULL) |
| 2740 | return NULL; |
| 2741 | |
| 2742 | buf = alloc(MAXPATHL); |
| 2743 | if (buf != NULL) |
| 2744 | { |
| 2745 | if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL) |
| 2746 | new_fname = vim_strsave(buf); |
| 2747 | else |
| 2748 | new_fname = vim_strsave(fname); |
| 2749 | vim_free(buf); |
| 2750 | } |
| 2751 | return new_fname; |
| 2752 | } |
| 2753 | |
| 2754 | /* |
| 2755 | * return TRUE if "fname" exists. |
| 2756 | */ |
| 2757 | int |
| 2758 | vim_fexists(char_u *fname) |
| 2759 | { |
| 2760 | stat_T st; |
| 2761 | |
| 2762 | if (mch_stat((char *)fname, &st)) |
| 2763 | return FALSE; |
| 2764 | return TRUE; |
| 2765 | } |
| 2766 | |
| 2767 | /* |
| 2768 | * Invoke expand_wildcards() for one pattern. |
| 2769 | * Expand items like "%:h" before the expansion. |
| 2770 | * Returns OK or FAIL. |
| 2771 | */ |
| 2772 | int |
| 2773 | expand_wildcards_eval( |
| 2774 | char_u **pat, // pointer to input pattern |
| 2775 | int *num_file, // resulting number of files |
| 2776 | char_u ***file, // array of resulting files |
| 2777 | int flags) // EW_DIR, etc. |
| 2778 | { |
| 2779 | int ret = FAIL; |
| 2780 | char_u *eval_pat = NULL; |
| 2781 | char_u *exp_pat = *pat; |
| 2782 | char *ignored_msg; |
| 2783 | int usedlen; |
| 2784 | |
| 2785 | if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<') |
| 2786 | { |
| 2787 | ++emsg_off; |
| 2788 | eval_pat = eval_vars(exp_pat, exp_pat, &usedlen, |
| 2789 | NULL, &ignored_msg, NULL); |
| 2790 | --emsg_off; |
| 2791 | if (eval_pat != NULL) |
| 2792 | exp_pat = concat_str(eval_pat, exp_pat + usedlen); |
| 2793 | } |
| 2794 | |
| 2795 | if (exp_pat != NULL) |
| 2796 | ret = expand_wildcards(1, &exp_pat, num_file, file, flags); |
| 2797 | |
| 2798 | if (eval_pat != NULL) |
| 2799 | { |
| 2800 | vim_free(exp_pat); |
| 2801 | vim_free(eval_pat); |
| 2802 | } |
| 2803 | |
| 2804 | return ret; |
| 2805 | } |
| 2806 | |
| 2807 | /* |
| 2808 | * Expand wildcards. Calls gen_expand_wildcards() and removes files matching |
| 2809 | * 'wildignore'. |
| 2810 | * Returns OK or FAIL. When FAIL then "num_files" won't be set. |
| 2811 | */ |
| 2812 | int |
| 2813 | expand_wildcards( |
| 2814 | int num_pat, // number of input patterns |
| 2815 | char_u **pat, // array of input patterns |
| 2816 | int *num_files, // resulting number of files |
| 2817 | char_u ***files, // array of resulting files |
| 2818 | int flags) // EW_DIR, etc. |
| 2819 | { |
| 2820 | int retval; |
| 2821 | int i, j; |
| 2822 | char_u *p; |
| 2823 | int non_suf_match; // number without matching suffix |
| 2824 | |
| 2825 | retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags); |
| 2826 | |
| 2827 | // When keeping all matches, return here |
| 2828 | if ((flags & EW_KEEPALL) || retval == FAIL) |
| 2829 | return retval; |
| 2830 | |
| 2831 | #ifdef FEAT_WILDIGN |
| 2832 | /* |
| 2833 | * Remove names that match 'wildignore'. |
| 2834 | */ |
| 2835 | if (*p_wig) |
| 2836 | { |
| 2837 | char_u *ffname; |
| 2838 | |
| 2839 | // check all files in (*files)[] |
| 2840 | for (i = 0; i < *num_files; ++i) |
| 2841 | { |
| 2842 | ffname = FullName_save((*files)[i], FALSE); |
| 2843 | if (ffname == NULL) // out of memory |
| 2844 | break; |
| 2845 | # ifdef VMS |
| 2846 | vms_remove_version(ffname); |
| 2847 | # endif |
| 2848 | if (match_file_list(p_wig, (*files)[i], ffname)) |
| 2849 | { |
| 2850 | // remove this matching file from the list |
| 2851 | vim_free((*files)[i]); |
| 2852 | for (j = i; j + 1 < *num_files; ++j) |
| 2853 | (*files)[j] = (*files)[j + 1]; |
| 2854 | --*num_files; |
| 2855 | --i; |
| 2856 | } |
| 2857 | vim_free(ffname); |
| 2858 | } |
| 2859 | |
| 2860 | // If the number of matches is now zero, we fail. |
| 2861 | if (*num_files == 0) |
| 2862 | { |
| 2863 | VIM_CLEAR(*files); |
| 2864 | return FAIL; |
| 2865 | } |
| 2866 | } |
| 2867 | #endif |
| 2868 | |
| 2869 | /* |
| 2870 | * Move the names where 'suffixes' match to the end. |
| 2871 | */ |
| 2872 | if (*num_files > 1) |
| 2873 | { |
| 2874 | non_suf_match = 0; |
| 2875 | for (i = 0; i < *num_files; ++i) |
| 2876 | { |
| 2877 | if (!match_suffix((*files)[i])) |
| 2878 | { |
| 2879 | /* |
| 2880 | * Move the name without matching suffix to the front |
| 2881 | * of the list. |
| 2882 | */ |
| 2883 | p = (*files)[i]; |
| 2884 | for (j = i; j > non_suf_match; --j) |
| 2885 | (*files)[j] = (*files)[j - 1]; |
| 2886 | (*files)[non_suf_match++] = p; |
| 2887 | } |
| 2888 | } |
| 2889 | } |
| 2890 | |
| 2891 | return retval; |
| 2892 | } |
| 2893 | |
| 2894 | /* |
| 2895 | * Return TRUE if "fname" matches with an entry in 'suffixes'. |
| 2896 | */ |
| 2897 | int |
| 2898 | match_suffix(char_u *fname) |
| 2899 | { |
| 2900 | int fnamelen, setsuflen; |
| 2901 | char_u *setsuf; |
| 2902 | #define MAXSUFLEN 30 // maximum length of a file suffix |
| 2903 | char_u suf_buf[MAXSUFLEN]; |
| 2904 | |
| 2905 | fnamelen = (int)STRLEN(fname); |
| 2906 | setsuflen = 0; |
| 2907 | for (setsuf = p_su; *setsuf; ) |
| 2908 | { |
| 2909 | setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,"); |
| 2910 | if (setsuflen == 0) |
| 2911 | { |
| 2912 | char_u *tail = gettail(fname); |
| 2913 | |
| 2914 | // empty entry: match name without a '.' |
| 2915 | if (vim_strchr(tail, '.') == NULL) |
| 2916 | { |
| 2917 | setsuflen = 1; |
| 2918 | break; |
| 2919 | } |
| 2920 | } |
| 2921 | else |
| 2922 | { |
| 2923 | if (fnamelen >= setsuflen |
| 2924 | && fnamencmp(suf_buf, fname + fnamelen - setsuflen, |
| 2925 | (size_t)setsuflen) == 0) |
| 2926 | break; |
| 2927 | setsuflen = 0; |
| 2928 | } |
| 2929 | } |
| 2930 | return (setsuflen != 0); |
| 2931 | } |
| 2932 | |
| 2933 | #ifdef VIM_BACKTICK |
| 2934 | |
| 2935 | /* |
| 2936 | * Return TRUE if we can expand this backtick thing here. |
| 2937 | */ |
| 2938 | static int |
| 2939 | vim_backtick(char_u *p) |
| 2940 | { |
| 2941 | return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`'); |
| 2942 | } |
| 2943 | |
| 2944 | /* |
| 2945 | * Expand an item in `backticks` by executing it as a command. |
| 2946 | * Currently only works when pat[] starts and ends with a `. |
| 2947 | * Returns number of file names found, -1 if an error is encountered. |
| 2948 | */ |
| 2949 | static int |
| 2950 | expand_backtick( |
| 2951 | garray_T *gap, |
| 2952 | char_u *pat, |
| 2953 | int flags) // EW_* flags |
| 2954 | { |
| 2955 | char_u *p; |
| 2956 | char_u *cmd; |
| 2957 | char_u *buffer; |
| 2958 | int cnt = 0; |
| 2959 | int i; |
| 2960 | |
| 2961 | // Create the command: lop off the backticks. |
| 2962 | cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2); |
| 2963 | if (cmd == NULL) |
| 2964 | return -1; |
| 2965 | |
| 2966 | #ifdef FEAT_EVAL |
| 2967 | if (*cmd == '=') // `={expr}`: Expand expression |
| 2968 | buffer = eval_to_string(cmd + 1, &p, TRUE); |
| 2969 | else |
| 2970 | #endif |
| 2971 | buffer = get_cmd_output(cmd, NULL, |
| 2972 | (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL); |
| 2973 | vim_free(cmd); |
| 2974 | if (buffer == NULL) |
| 2975 | return -1; |
| 2976 | |
| 2977 | cmd = buffer; |
| 2978 | while (*cmd != NUL) |
| 2979 | { |
| 2980 | cmd = skipwhite(cmd); // skip over white space |
| 2981 | p = cmd; |
| 2982 | while (*p != NUL && *p != '\r' && *p != '\n') // skip over entry |
| 2983 | ++p; |
| 2984 | // add an entry if it is not empty |
| 2985 | if (p > cmd) |
| 2986 | { |
| 2987 | i = *p; |
| 2988 | *p = NUL; |
| 2989 | addfile(gap, cmd, flags); |
| 2990 | *p = i; |
| 2991 | ++cnt; |
| 2992 | } |
| 2993 | cmd = p; |
| 2994 | while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n')) |
| 2995 | ++cmd; |
| 2996 | } |
| 2997 | |
| 2998 | vim_free(buffer); |
| 2999 | return cnt; |
| 3000 | } |
| 3001 | #endif // VIM_BACKTICK |
| 3002 | |
| 3003 | #if defined(MSWIN) |
| 3004 | /* |
| 3005 | * File name expansion code for MS-DOS, Win16 and Win32. It's here because |
| 3006 | * it's shared between these systems. |
| 3007 | */ |
| 3008 | |
| 3009 | /* |
| 3010 | * comparison function for qsort in dos_expandpath() |
| 3011 | */ |
| 3012 | static int |
| 3013 | pstrcmp(const void *a, const void *b) |
| 3014 | { |
| 3015 | return (pathcmp(*(char **)a, *(char **)b, -1)); |
| 3016 | } |
| 3017 | |
| 3018 | /* |
| 3019 | * Recursively expand one path component into all matching files and/or |
| 3020 | * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. |
| 3021 | * Return the number of matches found. |
| 3022 | * "path" has backslashes before chars that are not to be expanded, starting |
| 3023 | * at "path[wildoff]". |
| 3024 | * Return the number of matches found. |
| 3025 | * NOTE: much of this is identical to unix_expandpath(), keep in sync! |
| 3026 | */ |
| 3027 | static int |
| 3028 | dos_expandpath( |
| 3029 | garray_T *gap, |
| 3030 | char_u *path, |
| 3031 | int wildoff, |
| 3032 | int flags, // EW_* flags |
| 3033 | int didstar) // expanded "**" once already |
| 3034 | { |
| 3035 | char_u *buf; |
| 3036 | char_u *path_end; |
| 3037 | char_u *p, *s, *e; |
| 3038 | int start_len = gap->ga_len; |
| 3039 | char_u *pat; |
| 3040 | regmatch_T regmatch; |
| 3041 | int starts_with_dot; |
| 3042 | int matches; |
| 3043 | int len; |
| 3044 | int starstar = FALSE; |
| 3045 | static int stardepth = 0; // depth for "**" expansion |
| 3046 | HANDLE hFind = INVALID_HANDLE_VALUE; |
| 3047 | WIN32_FIND_DATAW wfb; |
| 3048 | WCHAR *wn = NULL; // UCS-2 name, NULL when not used. |
| 3049 | char_u *matchname; |
| 3050 | int ok; |
Bram Moolenaar | c74fbfe | 2020-04-06 22:56:28 +0200 | [diff] [blame] | 3051 | char_u *p_alt; |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3052 | |
| 3053 | // Expanding "**" may take a long time, check for CTRL-C. |
| 3054 | if (stardepth > 0) |
| 3055 | { |
| 3056 | ui_breakcheck(); |
| 3057 | if (got_int) |
| 3058 | return 0; |
| 3059 | } |
| 3060 | |
| 3061 | // Make room for file name. When doing encoding conversion the actual |
| 3062 | // length may be quite a bit longer, thus use the maximum possible length. |
| 3063 | buf = alloc(MAXPATHL); |
| 3064 | if (buf == NULL) |
| 3065 | return 0; |
| 3066 | |
| 3067 | /* |
| 3068 | * Find the first part in the path name that contains a wildcard or a ~1. |
| 3069 | * Copy it into buf, including the preceding characters. |
| 3070 | */ |
| 3071 | p = buf; |
| 3072 | s = buf; |
| 3073 | e = NULL; |
| 3074 | path_end = path; |
| 3075 | while (*path_end != NUL) |
| 3076 | { |
| 3077 | // May ignore a wildcard that has a backslash before it; it will |
| 3078 | // be removed by rem_backslash() or file_pat_to_reg_pat() below. |
| 3079 | if (path_end >= path + wildoff && rem_backslash(path_end)) |
| 3080 | *p++ = *path_end++; |
| 3081 | else if (*path_end == '\\' || *path_end == ':' || *path_end == '/') |
| 3082 | { |
| 3083 | if (e != NULL) |
| 3084 | break; |
| 3085 | s = p + 1; |
| 3086 | } |
| 3087 | else if (path_end >= path + wildoff |
| 3088 | && vim_strchr((char_u *)"*?[~", *path_end) != NULL) |
| 3089 | e = p; |
| 3090 | if (has_mbyte) |
| 3091 | { |
| 3092 | len = (*mb_ptr2len)(path_end); |
| 3093 | STRNCPY(p, path_end, len); |
| 3094 | p += len; |
| 3095 | path_end += len; |
| 3096 | } |
| 3097 | else |
| 3098 | *p++ = *path_end++; |
| 3099 | } |
| 3100 | e = p; |
| 3101 | *e = NUL; |
| 3102 | |
| 3103 | // now we have one wildcard component between s and e |
| 3104 | // Remove backslashes between "wildoff" and the start of the wildcard |
| 3105 | // component. |
| 3106 | for (p = buf + wildoff; p < s; ++p) |
| 3107 | if (rem_backslash(p)) |
| 3108 | { |
| 3109 | STRMOVE(p, p + 1); |
| 3110 | --e; |
| 3111 | --s; |
| 3112 | } |
| 3113 | |
| 3114 | // Check for "**" between "s" and "e". |
| 3115 | for (p = s; p < e; ++p) |
| 3116 | if (p[0] == '*' && p[1] == '*') |
| 3117 | starstar = TRUE; |
| 3118 | |
| 3119 | starts_with_dot = *s == '.'; |
| 3120 | pat = file_pat_to_reg_pat(s, e, NULL, FALSE); |
| 3121 | if (pat == NULL) |
| 3122 | { |
| 3123 | vim_free(buf); |
| 3124 | return 0; |
| 3125 | } |
| 3126 | |
| 3127 | // compile the regexp into a program |
| 3128 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
| 3129 | ++emsg_silent; |
| 3130 | regmatch.rm_ic = TRUE; // Always ignore case |
| 3131 | regmatch.regprog = vim_regcomp(pat, RE_MAGIC); |
| 3132 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
| 3133 | --emsg_silent; |
| 3134 | vim_free(pat); |
| 3135 | |
| 3136 | if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) |
| 3137 | { |
| 3138 | vim_free(buf); |
| 3139 | return 0; |
| 3140 | } |
| 3141 | |
| 3142 | // remember the pattern or file name being looked for |
| 3143 | matchname = vim_strsave(s); |
| 3144 | |
| 3145 | // If "**" is by itself, this is the first time we encounter it and more |
| 3146 | // is following then find matches without any directory. |
| 3147 | if (!didstar && stardepth < 100 && starstar && e - s == 2 |
| 3148 | && *path_end == '/') |
| 3149 | { |
| 3150 | STRCPY(s, path_end + 1); |
| 3151 | ++stardepth; |
| 3152 | (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE); |
| 3153 | --stardepth; |
| 3154 | } |
| 3155 | |
| 3156 | // Scan all files in the directory with "dir/ *.*" |
| 3157 | STRCPY(s, "*.*"); |
| 3158 | wn = enc_to_utf16(buf, NULL); |
| 3159 | if (wn != NULL) |
| 3160 | hFind = FindFirstFileW(wn, &wfb); |
| 3161 | ok = (hFind != INVALID_HANDLE_VALUE); |
| 3162 | |
| 3163 | while (ok) |
| 3164 | { |
| 3165 | p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here |
Bram Moolenaar | c74fbfe | 2020-04-06 22:56:28 +0200 | [diff] [blame] | 3166 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3167 | if (p == NULL) |
| 3168 | break; // out of memory |
| 3169 | |
Bram Moolenaar | c74fbfe | 2020-04-06 22:56:28 +0200 | [diff] [blame] | 3170 | if (*wfb.cAlternateFileName == NUL) |
Bram Moolenaar | 40655d5 | 2020-04-06 23:49:50 +0200 | [diff] [blame] | 3171 | p_alt = NULL; |
Bram Moolenaar | c74fbfe | 2020-04-06 22:56:28 +0200 | [diff] [blame] | 3172 | else |
| 3173 | p_alt = utf16_to_enc(wfb.cAlternateFileName, NULL); |
| 3174 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3175 | // Ignore entries starting with a dot, unless when asked for. Accept |
| 3176 | // all entries found with "matchname". |
| 3177 | if ((p[0] != '.' || starts_with_dot |
| 3178 | || ((flags & EW_DODOT) |
| 3179 | && p[1] != NUL && (p[1] != '.' || p[2] != NUL))) |
| 3180 | && (matchname == NULL |
| 3181 | || (regmatch.regprog != NULL |
Bram Moolenaar | 40655d5 | 2020-04-06 23:49:50 +0200 | [diff] [blame] | 3182 | && (vim_regexec(®match, p, (colnr_T)0) |
Bram Moolenaar | c74fbfe | 2020-04-06 22:56:28 +0200 | [diff] [blame] | 3183 | || (p_alt != NULL |
Bram Moolenaar | 40655d5 | 2020-04-06 23:49:50 +0200 | [diff] [blame] | 3184 | && vim_regexec(®match, p_alt, (colnr_T)0)))) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3185 | || ((flags & EW_NOTWILD) |
| 3186 | && fnamencmp(path + (s - buf), p, e - s) == 0))) |
| 3187 | { |
| 3188 | STRCPY(s, p); |
| 3189 | len = (int)STRLEN(buf); |
| 3190 | |
Bram Moolenaar | c74fbfe | 2020-04-06 22:56:28 +0200 | [diff] [blame] | 3191 | if (starstar && stardepth < 100 |
| 3192 | && (wfb.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3193 | { |
| 3194 | // For "**" in the pattern first go deeper in the tree to |
| 3195 | // find matches. |
| 3196 | STRCPY(buf + len, "/**"); |
| 3197 | STRCPY(buf + len + 3, path_end); |
| 3198 | ++stardepth; |
| 3199 | (void)dos_expandpath(gap, buf, len + 1, flags, TRUE); |
| 3200 | --stardepth; |
| 3201 | } |
| 3202 | |
| 3203 | STRCPY(buf + len, path_end); |
| 3204 | if (mch_has_exp_wildcard(path_end)) |
| 3205 | { |
| 3206 | // need to expand another component of the path |
| 3207 | // remove backslashes for the remaining components only |
| 3208 | (void)dos_expandpath(gap, buf, len + 1, flags, FALSE); |
| 3209 | } |
| 3210 | else |
| 3211 | { |
| 3212 | // no more wildcards, check if there is a match |
| 3213 | // remove backslashes for the remaining components only |
| 3214 | if (*path_end != 0) |
| 3215 | backslash_halve(buf + len + 1); |
| 3216 | if (mch_getperm(buf) >= 0) // add existing file |
| 3217 | addfile(gap, buf, flags); |
| 3218 | } |
| 3219 | } |
| 3220 | |
Bram Moolenaar | c74fbfe | 2020-04-06 22:56:28 +0200 | [diff] [blame] | 3221 | vim_free(p_alt); |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3222 | vim_free(p); |
| 3223 | ok = FindNextFileW(hFind, &wfb); |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3224 | } |
| 3225 | |
| 3226 | FindClose(hFind); |
| 3227 | vim_free(wn); |
| 3228 | vim_free(buf); |
| 3229 | vim_regfree(regmatch.regprog); |
| 3230 | vim_free(matchname); |
| 3231 | |
| 3232 | matches = gap->ga_len - start_len; |
| 3233 | if (matches > 0) |
| 3234 | qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches, |
| 3235 | sizeof(char_u *), pstrcmp); |
| 3236 | return matches; |
| 3237 | } |
| 3238 | |
| 3239 | int |
| 3240 | mch_expandpath( |
| 3241 | garray_T *gap, |
| 3242 | char_u *path, |
| 3243 | int flags) // EW_* flags |
| 3244 | { |
| 3245 | return dos_expandpath(gap, path, 0, flags, FALSE); |
| 3246 | } |
| 3247 | #endif // MSWIN |
| 3248 | |
| 3249 | #if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \ |
| 3250 | || defined(PROTO) |
| 3251 | /* |
| 3252 | * Unix style wildcard expansion code. |
| 3253 | * It's here because it's used both for Unix and Mac. |
| 3254 | */ |
| 3255 | static int |
| 3256 | pstrcmp(const void *a, const void *b) |
| 3257 | { |
| 3258 | return (pathcmp(*(char **)a, *(char **)b, -1)); |
| 3259 | } |
| 3260 | |
| 3261 | /* |
| 3262 | * Recursively expand one path component into all matching files and/or |
| 3263 | * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. |
| 3264 | * "path" has backslashes before chars that are not to be expanded, starting |
| 3265 | * at "path + wildoff". |
| 3266 | * Return the number of matches found. |
| 3267 | * NOTE: much of this is identical to dos_expandpath(), keep in sync! |
| 3268 | */ |
| 3269 | int |
| 3270 | unix_expandpath( |
| 3271 | garray_T *gap, |
| 3272 | char_u *path, |
| 3273 | int wildoff, |
| 3274 | int flags, // EW_* flags |
| 3275 | int didstar) // expanded "**" once already |
| 3276 | { |
| 3277 | char_u *buf; |
| 3278 | char_u *path_end; |
| 3279 | char_u *p, *s, *e; |
| 3280 | int start_len = gap->ga_len; |
| 3281 | char_u *pat; |
| 3282 | regmatch_T regmatch; |
| 3283 | int starts_with_dot; |
| 3284 | int matches; |
| 3285 | int len; |
| 3286 | int starstar = FALSE; |
| 3287 | static int stardepth = 0; // depth for "**" expansion |
| 3288 | |
| 3289 | DIR *dirp; |
| 3290 | struct dirent *dp; |
| 3291 | |
| 3292 | // Expanding "**" may take a long time, check for CTRL-C. |
| 3293 | if (stardepth > 0) |
| 3294 | { |
| 3295 | ui_breakcheck(); |
| 3296 | if (got_int) |
| 3297 | return 0; |
| 3298 | } |
| 3299 | |
| 3300 | // make room for file name |
| 3301 | buf = alloc(STRLEN(path) + BASENAMELEN + 5); |
| 3302 | if (buf == NULL) |
| 3303 | return 0; |
| 3304 | |
| 3305 | /* |
| 3306 | * Find the first part in the path name that contains a wildcard. |
| 3307 | * When EW_ICASE is set every letter is considered to be a wildcard. |
| 3308 | * Copy it into "buf", including the preceding characters. |
| 3309 | */ |
| 3310 | p = buf; |
| 3311 | s = buf; |
| 3312 | e = NULL; |
| 3313 | path_end = path; |
| 3314 | while (*path_end != NUL) |
| 3315 | { |
| 3316 | // May ignore a wildcard that has a backslash before it; it will |
| 3317 | // be removed by rem_backslash() or file_pat_to_reg_pat() below. |
| 3318 | if (path_end >= path + wildoff && rem_backslash(path_end)) |
| 3319 | *p++ = *path_end++; |
| 3320 | else if (*path_end == '/') |
| 3321 | { |
| 3322 | if (e != NULL) |
| 3323 | break; |
| 3324 | s = p + 1; |
| 3325 | } |
| 3326 | else if (path_end >= path + wildoff |
| 3327 | && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL |
| 3328 | || (!p_fic && (flags & EW_ICASE) |
| 3329 | && isalpha(PTR2CHAR(path_end))))) |
| 3330 | e = p; |
| 3331 | if (has_mbyte) |
| 3332 | { |
| 3333 | len = (*mb_ptr2len)(path_end); |
| 3334 | STRNCPY(p, path_end, len); |
| 3335 | p += len; |
| 3336 | path_end += len; |
| 3337 | } |
| 3338 | else |
| 3339 | *p++ = *path_end++; |
| 3340 | } |
| 3341 | e = p; |
| 3342 | *e = NUL; |
| 3343 | |
| 3344 | // Now we have one wildcard component between "s" and "e". |
| 3345 | // Remove backslashes between "wildoff" and the start of the wildcard |
| 3346 | // component. |
| 3347 | for (p = buf + wildoff; p < s; ++p) |
| 3348 | if (rem_backslash(p)) |
| 3349 | { |
| 3350 | STRMOVE(p, p + 1); |
| 3351 | --e; |
| 3352 | --s; |
| 3353 | } |
| 3354 | |
| 3355 | // Check for "**" between "s" and "e". |
| 3356 | for (p = s; p < e; ++p) |
| 3357 | if (p[0] == '*' && p[1] == '*') |
| 3358 | starstar = TRUE; |
| 3359 | |
| 3360 | // convert the file pattern to a regexp pattern |
| 3361 | starts_with_dot = *s == '.'; |
| 3362 | pat = file_pat_to_reg_pat(s, e, NULL, FALSE); |
| 3363 | if (pat == NULL) |
| 3364 | { |
| 3365 | vim_free(buf); |
| 3366 | return 0; |
| 3367 | } |
| 3368 | |
| 3369 | // compile the regexp into a program |
| 3370 | if (flags & EW_ICASE) |
| 3371 | regmatch.rm_ic = TRUE; // 'wildignorecase' set |
| 3372 | else |
| 3373 | regmatch.rm_ic = p_fic; // ignore case when 'fileignorecase' is set |
| 3374 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
| 3375 | ++emsg_silent; |
| 3376 | regmatch.regprog = vim_regcomp(pat, RE_MAGIC); |
| 3377 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
| 3378 | --emsg_silent; |
| 3379 | vim_free(pat); |
| 3380 | |
| 3381 | if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) |
| 3382 | { |
| 3383 | vim_free(buf); |
| 3384 | return 0; |
| 3385 | } |
| 3386 | |
| 3387 | // If "**" is by itself, this is the first time we encounter it and more |
| 3388 | // is following then find matches without any directory. |
| 3389 | if (!didstar && stardepth < 100 && starstar && e - s == 2 |
| 3390 | && *path_end == '/') |
| 3391 | { |
| 3392 | STRCPY(s, path_end + 1); |
| 3393 | ++stardepth; |
| 3394 | (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE); |
| 3395 | --stardepth; |
| 3396 | } |
| 3397 | |
| 3398 | // open the directory for scanning |
| 3399 | *s = NUL; |
| 3400 | dirp = opendir(*buf == NUL ? "." : (char *)buf); |
| 3401 | |
| 3402 | // Find all matching entries |
| 3403 | if (dirp != NULL) |
| 3404 | { |
| 3405 | for (;;) |
| 3406 | { |
| 3407 | dp = readdir(dirp); |
| 3408 | if (dp == NULL) |
| 3409 | break; |
| 3410 | if ((dp->d_name[0] != '.' || starts_with_dot |
| 3411 | || ((flags & EW_DODOT) |
| 3412 | && dp->d_name[1] != NUL |
| 3413 | && (dp->d_name[1] != '.' || dp->d_name[2] != NUL))) |
| 3414 | && ((regmatch.regprog != NULL && vim_regexec(®match, |
| 3415 | (char_u *)dp->d_name, (colnr_T)0)) |
| 3416 | || ((flags & EW_NOTWILD) |
| 3417 | && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0))) |
| 3418 | { |
| 3419 | STRCPY(s, dp->d_name); |
| 3420 | len = STRLEN(buf); |
| 3421 | |
| 3422 | if (starstar && stardepth < 100) |
| 3423 | { |
| 3424 | // For "**" in the pattern first go deeper in the tree to |
| 3425 | // find matches. |
| 3426 | STRCPY(buf + len, "/**"); |
| 3427 | STRCPY(buf + len + 3, path_end); |
| 3428 | ++stardepth; |
| 3429 | (void)unix_expandpath(gap, buf, len + 1, flags, TRUE); |
| 3430 | --stardepth; |
| 3431 | } |
| 3432 | |
| 3433 | STRCPY(buf + len, path_end); |
| 3434 | if (mch_has_exp_wildcard(path_end)) // handle more wildcards |
| 3435 | { |
| 3436 | // need to expand another component of the path |
| 3437 | // remove backslashes for the remaining components only |
| 3438 | (void)unix_expandpath(gap, buf, len + 1, flags, FALSE); |
| 3439 | } |
| 3440 | else |
| 3441 | { |
| 3442 | stat_T sb; |
| 3443 | |
| 3444 | // no more wildcards, check if there is a match |
| 3445 | // remove backslashes for the remaining components only |
| 3446 | if (*path_end != NUL) |
| 3447 | backslash_halve(buf + len + 1); |
| 3448 | // add existing file or symbolic link |
| 3449 | if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0 |
| 3450 | : mch_getperm(buf) >= 0) |
| 3451 | { |
| 3452 | #ifdef MACOS_CONVERT |
| 3453 | size_t precomp_len = STRLEN(buf)+1; |
| 3454 | char_u *precomp_buf = |
| 3455 | mac_precompose_path(buf, precomp_len, &precomp_len); |
| 3456 | |
| 3457 | if (precomp_buf) |
| 3458 | { |
| 3459 | mch_memmove(buf, precomp_buf, precomp_len); |
| 3460 | vim_free(precomp_buf); |
| 3461 | } |
| 3462 | #endif |
| 3463 | addfile(gap, buf, flags); |
| 3464 | } |
| 3465 | } |
| 3466 | } |
| 3467 | } |
| 3468 | |
| 3469 | closedir(dirp); |
| 3470 | } |
| 3471 | |
| 3472 | vim_free(buf); |
| 3473 | vim_regfree(regmatch.regprog); |
| 3474 | |
| 3475 | matches = gap->ga_len - start_len; |
| 3476 | if (matches > 0) |
| 3477 | qsort(((char_u **)gap->ga_data) + start_len, matches, |
| 3478 | sizeof(char_u *), pstrcmp); |
| 3479 | return matches; |
| 3480 | } |
| 3481 | #endif |
| 3482 | |
| 3483 | /* |
| 3484 | * Return TRUE if "p" contains what looks like an environment variable. |
| 3485 | * Allowing for escaping. |
| 3486 | */ |
| 3487 | static int |
| 3488 | has_env_var(char_u *p) |
| 3489 | { |
| 3490 | for ( ; *p; MB_PTR_ADV(p)) |
| 3491 | { |
| 3492 | if (*p == '\\' && p[1] != NUL) |
| 3493 | ++p; |
| 3494 | else if (vim_strchr((char_u *) |
| 3495 | #if defined(MSWIN) |
| 3496 | "$%" |
| 3497 | #else |
| 3498 | "$" |
| 3499 | #endif |
| 3500 | , *p) != NULL) |
| 3501 | return TRUE; |
| 3502 | } |
| 3503 | return FALSE; |
| 3504 | } |
| 3505 | |
| 3506 | #ifdef SPECIAL_WILDCHAR |
| 3507 | /* |
| 3508 | * Return TRUE if "p" contains a special wildcard character, one that Vim |
| 3509 | * cannot expand, requires using a shell. |
| 3510 | */ |
| 3511 | static int |
| 3512 | has_special_wildchar(char_u *p) |
| 3513 | { |
| 3514 | for ( ; *p; MB_PTR_ADV(p)) |
| 3515 | { |
| 3516 | // Disallow line break characters. |
| 3517 | if (*p == '\r' || *p == '\n') |
| 3518 | break; |
| 3519 | // Allow for escaping. |
| 3520 | if (*p == '\\' && p[1] != NUL && p[1] != '\r' && p[1] != '\n') |
| 3521 | ++p; |
| 3522 | else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL) |
| 3523 | { |
| 3524 | // A { must be followed by a matching }. |
| 3525 | if (*p == '{' && vim_strchr(p, '}') == NULL) |
| 3526 | continue; |
| 3527 | // A quote and backtick must be followed by another one. |
| 3528 | if ((*p == '`' || *p == '\'') && vim_strchr(p, *p) == NULL) |
| 3529 | continue; |
| 3530 | return TRUE; |
| 3531 | } |
| 3532 | } |
| 3533 | return FALSE; |
| 3534 | } |
| 3535 | #endif |
| 3536 | |
| 3537 | /* |
| 3538 | * Generic wildcard expansion code. |
| 3539 | * |
| 3540 | * Characters in "pat" that should not be expanded must be preceded with a |
| 3541 | * backslash. E.g., "/path\ with\ spaces/my\*star*" |
| 3542 | * |
| 3543 | * Return FAIL when no single file was found. In this case "num_file" is not |
| 3544 | * set, and "file" may contain an error message. |
| 3545 | * Return OK when some files found. "num_file" is set to the number of |
| 3546 | * matches, "file" to the array of matches. Call FreeWild() later. |
| 3547 | */ |
| 3548 | int |
| 3549 | gen_expand_wildcards( |
| 3550 | int num_pat, // number of input patterns |
| 3551 | char_u **pat, // array of input patterns |
| 3552 | int *num_file, // resulting number of files |
| 3553 | char_u ***file, // array of resulting files |
| 3554 | int flags) // EW_* flags |
| 3555 | { |
| 3556 | int i; |
| 3557 | garray_T ga; |
| 3558 | char_u *p; |
| 3559 | static int recursive = FALSE; |
| 3560 | int add_pat; |
| 3561 | int retval = OK; |
| 3562 | #if defined(FEAT_SEARCHPATH) |
| 3563 | int did_expand_in_path = FALSE; |
| 3564 | #endif |
| 3565 | |
| 3566 | /* |
| 3567 | * expand_env() is called to expand things like "~user". If this fails, |
| 3568 | * it calls ExpandOne(), which brings us back here. In this case, always |
| 3569 | * call the machine specific expansion function, if possible. Otherwise, |
| 3570 | * return FAIL. |
| 3571 | */ |
| 3572 | if (recursive) |
| 3573 | #ifdef SPECIAL_WILDCHAR |
| 3574 | return mch_expand_wildcards(num_pat, pat, num_file, file, flags); |
| 3575 | #else |
| 3576 | return FAIL; |
| 3577 | #endif |
| 3578 | |
| 3579 | #ifdef SPECIAL_WILDCHAR |
| 3580 | /* |
| 3581 | * If there are any special wildcard characters which we cannot handle |
| 3582 | * here, call machine specific function for all the expansion. This |
| 3583 | * avoids starting the shell for each argument separately. |
| 3584 | * For `=expr` do use the internal function. |
| 3585 | */ |
| 3586 | for (i = 0; i < num_pat; i++) |
| 3587 | { |
| 3588 | if (has_special_wildchar(pat[i]) |
| 3589 | # ifdef VIM_BACKTICK |
| 3590 | && !(vim_backtick(pat[i]) && pat[i][1] == '=') |
| 3591 | # endif |
| 3592 | ) |
| 3593 | return mch_expand_wildcards(num_pat, pat, num_file, file, flags); |
| 3594 | } |
| 3595 | #endif |
| 3596 | |
| 3597 | recursive = TRUE; |
| 3598 | |
| 3599 | /* |
| 3600 | * The matching file names are stored in a growarray. Init it empty. |
| 3601 | */ |
| 3602 | ga_init2(&ga, (int)sizeof(char_u *), 30); |
| 3603 | |
| 3604 | for (i = 0; i < num_pat; ++i) |
| 3605 | { |
| 3606 | add_pat = -1; |
| 3607 | p = pat[i]; |
| 3608 | |
| 3609 | #ifdef VIM_BACKTICK |
| 3610 | if (vim_backtick(p)) |
| 3611 | { |
| 3612 | add_pat = expand_backtick(&ga, p, flags); |
| 3613 | if (add_pat == -1) |
| 3614 | retval = FAIL; |
| 3615 | } |
| 3616 | else |
| 3617 | #endif |
| 3618 | { |
| 3619 | /* |
| 3620 | * First expand environment variables, "~/" and "~user/". |
| 3621 | */ |
| 3622 | if ((has_env_var(p) && !(flags & EW_NOTENV)) || *p == '~') |
| 3623 | { |
| 3624 | p = expand_env_save_opt(p, TRUE); |
| 3625 | if (p == NULL) |
| 3626 | p = pat[i]; |
| 3627 | #ifdef UNIX |
| 3628 | /* |
| 3629 | * On Unix, if expand_env() can't expand an environment |
| 3630 | * variable, use the shell to do that. Discard previously |
| 3631 | * found file names and start all over again. |
| 3632 | */ |
| 3633 | else if (has_env_var(p) || *p == '~') |
| 3634 | { |
| 3635 | vim_free(p); |
| 3636 | ga_clear_strings(&ga); |
| 3637 | i = mch_expand_wildcards(num_pat, pat, num_file, file, |
| 3638 | flags|EW_KEEPDOLLAR); |
| 3639 | recursive = FALSE; |
| 3640 | return i; |
| 3641 | } |
| 3642 | #endif |
| 3643 | } |
| 3644 | |
| 3645 | /* |
| 3646 | * If there are wildcards: Expand file names and add each match to |
| 3647 | * the list. If there is no match, and EW_NOTFOUND is given, add |
| 3648 | * the pattern. |
| 3649 | * If there are no wildcards: Add the file name if it exists or |
| 3650 | * when EW_NOTFOUND is given. |
| 3651 | */ |
| 3652 | if (mch_has_exp_wildcard(p)) |
| 3653 | { |
| 3654 | #if defined(FEAT_SEARCHPATH) |
| 3655 | if ((flags & EW_PATH) |
| 3656 | && !mch_isFullName(p) |
| 3657 | && !(p[0] == '.' |
| 3658 | && (vim_ispathsep(p[1]) |
| 3659 | || (p[1] == '.' && vim_ispathsep(p[2])))) |
| 3660 | ) |
| 3661 | { |
| 3662 | // :find completion where 'path' is used. |
| 3663 | // Recursiveness is OK here. |
| 3664 | recursive = FALSE; |
| 3665 | add_pat = expand_in_path(&ga, p, flags); |
| 3666 | recursive = TRUE; |
| 3667 | did_expand_in_path = TRUE; |
| 3668 | } |
| 3669 | else |
| 3670 | #endif |
| 3671 | add_pat = mch_expandpath(&ga, p, flags); |
| 3672 | } |
| 3673 | } |
| 3674 | |
| 3675 | if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND))) |
| 3676 | { |
| 3677 | char_u *t = backslash_halve_save(p); |
| 3678 | |
| 3679 | // When EW_NOTFOUND is used, always add files and dirs. Makes |
| 3680 | // "vim c:/" work. |
| 3681 | if (flags & EW_NOTFOUND) |
| 3682 | addfile(&ga, t, flags | EW_DIR | EW_FILE); |
| 3683 | else |
| 3684 | addfile(&ga, t, flags); |
| 3685 | |
| 3686 | if (t != p) |
| 3687 | vim_free(t); |
| 3688 | } |
| 3689 | |
| 3690 | #if defined(FEAT_SEARCHPATH) |
| 3691 | if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH)) |
| 3692 | uniquefy_paths(&ga, p); |
| 3693 | #endif |
| 3694 | if (p != pat[i]) |
| 3695 | vim_free(p); |
| 3696 | } |
| 3697 | |
| 3698 | *num_file = ga.ga_len; |
| 3699 | *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)""; |
| 3700 | |
| 3701 | recursive = FALSE; |
| 3702 | |
| 3703 | return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL; |
| 3704 | } |
| 3705 | |
| 3706 | /* |
| 3707 | * Add a file to a file list. Accepted flags: |
| 3708 | * EW_DIR add directories |
| 3709 | * EW_FILE add files |
| 3710 | * EW_EXEC add executable files |
| 3711 | * EW_NOTFOUND add even when it doesn't exist |
| 3712 | * EW_ADDSLASH add slash after directory name |
| 3713 | * EW_ALLLINKS add symlink also when the referred file does not exist |
| 3714 | */ |
| 3715 | void |
| 3716 | addfile( |
| 3717 | garray_T *gap, |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3718 | char_u *f, // filename |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3719 | int flags) |
| 3720 | { |
| 3721 | char_u *p; |
| 3722 | int isdir; |
| 3723 | stat_T sb; |
| 3724 | |
| 3725 | // if the file/dir/link doesn't exist, may not add it |
| 3726 | if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS) |
| 3727 | ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0)) |
| 3728 | return; |
| 3729 | |
| 3730 | #ifdef FNAME_ILLEGAL |
| 3731 | // if the file/dir contains illegal characters, don't add it |
| 3732 | if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL) |
| 3733 | return; |
| 3734 | #endif |
| 3735 | |
| 3736 | isdir = mch_isdir(f); |
| 3737 | if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE))) |
| 3738 | return; |
| 3739 | |
| 3740 | // If the file isn't executable, may not add it. Do accept directories. |
| 3741 | // When invoked from expand_shellcmd() do not use $PATH. |
| 3742 | if (!isdir && (flags & EW_EXEC) |
| 3743 | && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD))) |
| 3744 | return; |
| 3745 | |
| 3746 | // Make room for another item in the file list. |
| 3747 | if (ga_grow(gap, 1) == FAIL) |
| 3748 | return; |
| 3749 | |
| 3750 | p = alloc(STRLEN(f) + 1 + isdir); |
| 3751 | if (p == NULL) |
| 3752 | return; |
| 3753 | |
| 3754 | STRCPY(p, f); |
| 3755 | #ifdef BACKSLASH_IN_FILENAME |
| 3756 | slash_adjust(p); |
| 3757 | #endif |
| 3758 | /* |
| 3759 | * Append a slash or backslash after directory names if none is present. |
| 3760 | */ |
| 3761 | #ifndef DONT_ADD_PATHSEP_TO_DIR |
| 3762 | if (isdir && (flags & EW_ADDSLASH)) |
| 3763 | add_pathsep(p); |
| 3764 | #endif |
| 3765 | ((char_u **)gap->ga_data)[gap->ga_len++] = p; |
| 3766 | } |
| 3767 | |
| 3768 | /* |
| 3769 | * Free the list of files returned by expand_wildcards() or other expansion |
| 3770 | * functions. |
| 3771 | */ |
| 3772 | void |
| 3773 | FreeWild(int count, char_u **files) |
| 3774 | { |
| 3775 | if (count <= 0 || files == NULL) |
| 3776 | return; |
| 3777 | while (count--) |
| 3778 | vim_free(files[count]); |
| 3779 | vim_free(files); |
| 3780 | } |
| 3781 | |
| 3782 | /* |
| 3783 | * Compare path "p[]" to "q[]". |
| 3784 | * If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]" |
| 3785 | * Return value like strcmp(p, q), but consider path separators. |
| 3786 | */ |
| 3787 | int |
| 3788 | pathcmp(const char *p, const char *q, int maxlen) |
| 3789 | { |
| 3790 | int i, j; |
| 3791 | int c1, c2; |
| 3792 | const char *s = NULL; |
| 3793 | |
| 3794 | for (i = 0, j = 0; maxlen < 0 || (i < maxlen && j < maxlen);) |
| 3795 | { |
| 3796 | c1 = PTR2CHAR((char_u *)p + i); |
| 3797 | c2 = PTR2CHAR((char_u *)q + j); |
| 3798 | |
| 3799 | // End of "p": check if "q" also ends or just has a slash. |
| 3800 | if (c1 == NUL) |
| 3801 | { |
| 3802 | if (c2 == NUL) // full match |
| 3803 | return 0; |
| 3804 | s = q; |
| 3805 | i = j; |
| 3806 | break; |
| 3807 | } |
| 3808 | |
| 3809 | // End of "q": check if "p" just has a slash. |
| 3810 | if (c2 == NUL) |
| 3811 | { |
| 3812 | s = p; |
| 3813 | break; |
| 3814 | } |
| 3815 | |
| 3816 | if ((p_fic ? MB_TOUPPER(c1) != MB_TOUPPER(c2) : c1 != c2) |
| 3817 | #ifdef BACKSLASH_IN_FILENAME |
| 3818 | // consider '/' and '\\' to be equal |
| 3819 | && !((c1 == '/' && c2 == '\\') |
| 3820 | || (c1 == '\\' && c2 == '/')) |
| 3821 | #endif |
| 3822 | ) |
| 3823 | { |
| 3824 | if (vim_ispathsep(c1)) |
| 3825 | return -1; |
| 3826 | if (vim_ispathsep(c2)) |
| 3827 | return 1; |
| 3828 | return p_fic ? MB_TOUPPER(c1) - MB_TOUPPER(c2) |
| 3829 | : c1 - c2; // no match |
| 3830 | } |
| 3831 | |
Bram Moolenaar | 1614a14 | 2019-10-06 22:00:13 +0200 | [diff] [blame] | 3832 | i += mb_ptr2len((char_u *)p + i); |
| 3833 | j += mb_ptr2len((char_u *)q + j); |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3834 | } |
| 3835 | if (s == NULL) // "i" or "j" ran into "maxlen" |
| 3836 | return 0; |
| 3837 | |
| 3838 | c1 = PTR2CHAR((char_u *)s + i); |
Bram Moolenaar | 1614a14 | 2019-10-06 22:00:13 +0200 | [diff] [blame] | 3839 | c2 = PTR2CHAR((char_u *)s + i + mb_ptr2len((char_u *)s + i)); |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3840 | // ignore a trailing slash, but not "//" or ":/" |
| 3841 | if (c2 == NUL |
| 3842 | && i > 0 |
| 3843 | && !after_pathsep((char_u *)s, (char_u *)s + i) |
| 3844 | #ifdef BACKSLASH_IN_FILENAME |
| 3845 | && (c1 == '/' || c1 == '\\') |
| 3846 | #else |
| 3847 | && c1 == '/' |
| 3848 | #endif |
| 3849 | ) |
| 3850 | return 0; // match with trailing slash |
| 3851 | if (s == q) |
| 3852 | return -1; // no match |
| 3853 | return 1; |
| 3854 | } |
| 3855 | |
| 3856 | /* |
| 3857 | * Return TRUE if "name" is a full (absolute) path name or URL. |
| 3858 | */ |
| 3859 | int |
| 3860 | vim_isAbsName(char_u *name) |
| 3861 | { |
| 3862 | return (path_with_url(name) != 0 || mch_isFullName(name)); |
| 3863 | } |
| 3864 | |
| 3865 | /* |
| 3866 | * Get absolute file name into buffer "buf[len]". |
| 3867 | * |
| 3868 | * return FAIL for failure, OK otherwise |
| 3869 | */ |
| 3870 | int |
| 3871 | vim_FullName( |
| 3872 | char_u *fname, |
| 3873 | char_u *buf, |
| 3874 | int len, |
| 3875 | int force) // force expansion even when already absolute |
| 3876 | { |
| 3877 | int retval = OK; |
| 3878 | int url; |
| 3879 | |
| 3880 | *buf = NUL; |
| 3881 | if (fname == NULL) |
| 3882 | return FAIL; |
| 3883 | |
| 3884 | url = path_with_url(fname); |
| 3885 | if (!url) |
| 3886 | retval = mch_FullName(fname, buf, len, force); |
| 3887 | if (url || retval == FAIL) |
| 3888 | { |
| 3889 | // something failed; use the file name (truncate when too long) |
| 3890 | vim_strncpy(buf, fname, len - 1); |
| 3891 | } |
| 3892 | #if defined(MSWIN) |
| 3893 | slash_adjust(buf); |
| 3894 | #endif |
| 3895 | return retval; |
| 3896 | } |