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) |
Christian Brabandt | 81da16b | 2022-03-10 12:24:02 +0000 | [diff] [blame] | 189 | STRNCPY(save_fname, short_fname, len); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 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 | { |
K.Takata | 5411910 | 2022-02-03 13:33:03 +0000 | [diff] [blame] | 375 | char_u *q = utf16_to_enc(buf, NULL); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 376 | |
K.Takata | 5411910 | 2022-02-03 13:33:03 +0000 | [diff] [blame] | 377 | if (q != NULL) |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 378 | { |
| 379 | vim_free(*bufp); // free any allocated file name |
K.Takata | 5411910 | 2022-02-03 13:33:03 +0000 | [diff] [blame] | 380 | *bufp = *fnamep = q; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 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 | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 392 | *fnamep = vim_strnsave(*fnamep, 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 | { |
=?UTF-8?q?Dundar=20G=C3=B6c?= | 78a8404 | 2022-02-09 15:20:39 +0000 | [diff] [blame] | 419 | if (**fnamep == '~') |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 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 | { |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 658 | pat = vim_strnsave(s, p - s); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 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 | { |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 666 | sub = vim_strnsave(s, p - s); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 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 | |
Bram Moolenaar | 273af49 | 2020-09-25 23:49:01 +0200 | [diff] [blame] | 713 | /* |
| 714 | * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname" |
| 715 | * "trim_len" specifies how many characters to keep for each directory. |
| 716 | * Must be 1 or more. |
| 717 | * It's done in-place. |
| 718 | */ |
| 719 | static void |
| 720 | shorten_dir_len(char_u *str, int trim_len) |
| 721 | { |
| 722 | char_u *tail, *s, *d; |
| 723 | int skip = FALSE; |
| 724 | int dirchunk_len = 0; |
| 725 | |
| 726 | tail = gettail(str); |
| 727 | d = str; |
| 728 | for (s = str; ; ++s) |
| 729 | { |
| 730 | if (s >= tail) // copy the whole tail |
| 731 | { |
| 732 | *d++ = *s; |
| 733 | if (*s == NUL) |
| 734 | break; |
| 735 | } |
| 736 | else if (vim_ispathsep(*s)) // copy '/' and next char |
| 737 | { |
| 738 | *d++ = *s; |
| 739 | skip = FALSE; |
| 740 | dirchunk_len = 0; |
| 741 | } |
| 742 | else if (!skip) |
| 743 | { |
| 744 | *d++ = *s; // copy next char |
| 745 | if (*s != '~' && *s != '.') // and leading "~" and "." |
| 746 | { |
| 747 | ++dirchunk_len; // only count word chars for the size |
| 748 | |
| 749 | // keep copying chars until we have our preferred length (or |
| 750 | // until the above if/else branches move us along) |
| 751 | if (dirchunk_len >= trim_len) |
| 752 | skip = TRUE; |
| 753 | } |
| 754 | |
| 755 | if (has_mbyte) |
| 756 | { |
| 757 | int l = mb_ptr2len(s); |
| 758 | |
| 759 | while (--l > 0) |
| 760 | *d++ = *++s; |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | /* |
| 767 | * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname" |
| 768 | * It's done in-place. |
| 769 | */ |
| 770 | void |
| 771 | shorten_dir(char_u *str) |
| 772 | { |
| 773 | shorten_dir_len(str, 1); |
| 774 | } |
| 775 | |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 776 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 777 | |
| 778 | /* |
| 779 | * "chdir(dir)" function |
| 780 | */ |
| 781 | void |
| 782 | f_chdir(typval_T *argvars, typval_T *rettv) |
| 783 | { |
| 784 | char_u *cwd; |
| 785 | cdscope_T scope = CDSCOPE_GLOBAL; |
| 786 | |
| 787 | rettv->v_type = VAR_STRING; |
| 788 | rettv->vval.v_string = NULL; |
| 789 | |
| 790 | if (argvars[0].v_type != VAR_STRING) |
Bram Moolenaar | c580943 | 2021-03-27 21:23:30 +0100 | [diff] [blame] | 791 | { |
Bram Moolenaar | d816cd9 | 2020-02-04 22:23:09 +0100 | [diff] [blame] | 792 | // Returning an empty string means it failed. |
Bram Moolenaar | 002bc79 | 2020-06-05 22:33:42 +0200 | [diff] [blame] | 793 | // No error message, for historic reasons. |
Bram Moolenaar | c580943 | 2021-03-27 21:23:30 +0100 | [diff] [blame] | 794 | if (in_vim9script()) |
| 795 | (void) check_for_string_arg(argvars, 0); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 796 | return; |
Bram Moolenaar | c580943 | 2021-03-27 21:23:30 +0100 | [diff] [blame] | 797 | } |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 798 | |
| 799 | // Return the current directory |
| 800 | cwd = alloc(MAXPATHL); |
| 801 | if (cwd != NULL) |
| 802 | { |
| 803 | if (mch_dirname(cwd, MAXPATHL) != FAIL) |
| 804 | { |
| 805 | #ifdef BACKSLASH_IN_FILENAME |
| 806 | slash_adjust(cwd); |
| 807 | #endif |
| 808 | rettv->vval.v_string = vim_strsave(cwd); |
| 809 | } |
| 810 | vim_free(cwd); |
| 811 | } |
| 812 | |
| 813 | if (curwin->w_localdir != NULL) |
| 814 | scope = CDSCOPE_WINDOW; |
| 815 | else if (curtab->tp_localdir != NULL) |
| 816 | scope = CDSCOPE_TABPAGE; |
| 817 | |
| 818 | if (!changedir_func(argvars[0].vval.v_string, TRUE, scope)) |
| 819 | // Directory change failed |
| 820 | VIM_CLEAR(rettv->vval.v_string); |
| 821 | } |
| 822 | |
| 823 | /* |
| 824 | * "delete()" function |
| 825 | */ |
| 826 | void |
| 827 | f_delete(typval_T *argvars, typval_T *rettv) |
| 828 | { |
| 829 | char_u nbuf[NUMBUFLEN]; |
| 830 | char_u *name; |
| 831 | char_u *flags; |
| 832 | |
| 833 | rettv->vval.v_number = -1; |
| 834 | if (check_restricted() || check_secure()) |
| 835 | return; |
| 836 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 837 | if (in_vim9script() |
| 838 | && (check_for_string_arg(argvars, 0) == FAIL |
| 839 | || check_for_opt_string_arg(argvars, 1) == FAIL)) |
| 840 | return; |
| 841 | |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 842 | name = tv_get_string(&argvars[0]); |
| 843 | if (name == NULL || *name == NUL) |
| 844 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 845 | emsg(_(e_invalid_argument)); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 846 | return; |
| 847 | } |
| 848 | |
| 849 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 850 | flags = tv_get_string_buf(&argvars[1], nbuf); |
| 851 | else |
| 852 | flags = (char_u *)""; |
| 853 | |
| 854 | if (*flags == NUL) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 855 | // delete a file |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 856 | rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1; |
| 857 | else if (STRCMP(flags, "d") == 0) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 858 | // delete an empty directory |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 859 | rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1; |
| 860 | else if (STRCMP(flags, "rf") == 0) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 861 | // delete a directory recursively |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 862 | rettv->vval.v_number = delete_recursive(name); |
| 863 | else |
Bram Moolenaar | 108010a | 2021-06-27 22:03:33 +0200 | [diff] [blame] | 864 | semsg(_(e_invalid_expression_str), flags); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | /* |
| 868 | * "executable()" function |
| 869 | */ |
| 870 | void |
| 871 | f_executable(typval_T *argvars, typval_T *rettv) |
| 872 | { |
Bram Moolenaar | 32105ae | 2021-03-27 18:59:25 +0100 | [diff] [blame] | 873 | if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL) |
Bram Moolenaar | 7bb4e74 | 2020-12-09 12:41:50 +0100 | [diff] [blame] | 874 | return; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 875 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 876 | // Check in $PATH and also check directly if there is a directory name. |
Bram Moolenaar | 7bb4e74 | 2020-12-09 12:41:50 +0100 | [diff] [blame] | 877 | rettv->vval.v_number = mch_can_exe(tv_get_string(&argvars[0]), NULL, TRUE); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | /* |
| 881 | * "exepath()" function |
| 882 | */ |
| 883 | void |
| 884 | f_exepath(typval_T *argvars, typval_T *rettv) |
| 885 | { |
| 886 | char_u *p = NULL; |
| 887 | |
Bram Moolenaar | 32105ae | 2021-03-27 18:59:25 +0100 | [diff] [blame] | 888 | if (in_vim9script() && check_for_nonempty_string_arg(argvars, 0) == FAIL) |
Bram Moolenaar | 7bb4e74 | 2020-12-09 12:41:50 +0100 | [diff] [blame] | 889 | return; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 890 | (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE); |
| 891 | rettv->v_type = VAR_STRING; |
| 892 | rettv->vval.v_string = p; |
| 893 | } |
| 894 | |
| 895 | /* |
Bram Moolenaar | 4dea2d9 | 2022-03-31 11:37:57 +0100 | [diff] [blame] | 896 | * Return TRUE if "fname" is a readable file. |
| 897 | */ |
| 898 | int |
| 899 | file_is_readable(char_u *fname) |
| 900 | { |
| 901 | int fd; |
| 902 | |
| 903 | #ifndef O_NONBLOCK |
| 904 | # define O_NONBLOCK 0 |
| 905 | #endif |
| 906 | if (*fname && !mch_isdir(fname) |
| 907 | && (fd = mch_open((char *)fname, O_RDONLY | O_NONBLOCK, 0)) >= 0) |
| 908 | { |
| 909 | close(fd); |
| 910 | return TRUE; |
| 911 | } |
| 912 | return FALSE; |
| 913 | } |
| 914 | |
| 915 | /* |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 916 | * "filereadable()" function |
| 917 | */ |
| 918 | void |
| 919 | f_filereadable(typval_T *argvars, typval_T *rettv) |
| 920 | { |
Bram Moolenaar | 32105ae | 2021-03-27 18:59:25 +0100 | [diff] [blame] | 921 | if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL) |
Bram Moolenaar | 7bb4e74 | 2020-12-09 12:41:50 +0100 | [diff] [blame] | 922 | return; |
Bram Moolenaar | 4dea2d9 | 2022-03-31 11:37:57 +0100 | [diff] [blame] | 923 | rettv->vval.v_number = file_is_readable(tv_get_string(&argvars[0])); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | /* |
| 927 | * Return 0 for not writable, 1 for writable file, 2 for a dir which we have |
| 928 | * rights to write into. |
| 929 | */ |
| 930 | void |
| 931 | f_filewritable(typval_T *argvars, typval_T *rettv) |
| 932 | { |
Bram Moolenaar | 32105ae | 2021-03-27 18:59:25 +0100 | [diff] [blame] | 933 | if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL) |
Bram Moolenaar | 7bb4e74 | 2020-12-09 12:41:50 +0100 | [diff] [blame] | 934 | return; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 935 | rettv->vval.v_number = filewritable(tv_get_string(&argvars[0])); |
| 936 | } |
| 937 | |
Bram Moolenaar | 840d16f | 2019-09-10 21:27:18 +0200 | [diff] [blame] | 938 | static void |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 939 | findfilendir( |
| 940 | typval_T *argvars UNUSED, |
| 941 | typval_T *rettv, |
| 942 | int find_what UNUSED) |
| 943 | { |
| 944 | #ifdef FEAT_SEARCHPATH |
| 945 | char_u *fname; |
| 946 | char_u *fresult = NULL; |
| 947 | char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path; |
| 948 | char_u *p; |
| 949 | char_u pathbuf[NUMBUFLEN]; |
| 950 | int count = 1; |
| 951 | int first = TRUE; |
| 952 | int error = FALSE; |
| 953 | #endif |
| 954 | |
| 955 | rettv->vval.v_string = NULL; |
| 956 | rettv->v_type = VAR_STRING; |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 957 | if (in_vim9script() |
| 958 | && (check_for_nonempty_string_arg(argvars, 0) == FAIL |
| 959 | || check_for_opt_string_arg(argvars, 1) == FAIL |
| 960 | || (argvars[1].v_type != VAR_UNKNOWN |
| 961 | && check_for_opt_number_arg(argvars, 2) == FAIL))) |
Bram Moolenaar | 7bb4e74 | 2020-12-09 12:41:50 +0100 | [diff] [blame] | 962 | return; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 963 | |
| 964 | #ifdef FEAT_SEARCHPATH |
| 965 | fname = tv_get_string(&argvars[0]); |
| 966 | |
| 967 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 968 | { |
| 969 | p = tv_get_string_buf_chk(&argvars[1], pathbuf); |
| 970 | if (p == NULL) |
| 971 | error = TRUE; |
| 972 | else |
| 973 | { |
| 974 | if (*p != NUL) |
| 975 | path = p; |
| 976 | |
| 977 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 978 | count = (int)tv_get_number_chk(&argvars[2], &error); |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | if (count < 0 && rettv_list_alloc(rettv) == FAIL) |
| 983 | error = TRUE; |
| 984 | |
| 985 | if (*fname != NUL && !error) |
| 986 | { |
| 987 | do |
| 988 | { |
| 989 | if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST) |
| 990 | vim_free(fresult); |
| 991 | fresult = find_file_in_path_option(first ? fname : NULL, |
| 992 | first ? (int)STRLEN(fname) : 0, |
| 993 | 0, first, path, |
| 994 | find_what, |
| 995 | curbuf->b_ffname, |
| 996 | find_what == FINDFILE_DIR |
| 997 | ? (char_u *)"" : curbuf->b_p_sua); |
| 998 | first = FALSE; |
| 999 | |
| 1000 | if (fresult != NULL && rettv->v_type == VAR_LIST) |
| 1001 | list_append_string(rettv->vval.v_list, fresult, -1); |
| 1002 | |
| 1003 | } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL); |
| 1004 | } |
| 1005 | |
| 1006 | if (rettv->v_type == VAR_STRING) |
| 1007 | rettv->vval.v_string = fresult; |
| 1008 | #endif |
| 1009 | } |
| 1010 | |
| 1011 | /* |
| 1012 | * "finddir({fname}[, {path}[, {count}]])" function |
| 1013 | */ |
| 1014 | void |
| 1015 | f_finddir(typval_T *argvars, typval_T *rettv) |
| 1016 | { |
| 1017 | findfilendir(argvars, rettv, FINDFILE_DIR); |
| 1018 | } |
| 1019 | |
| 1020 | /* |
| 1021 | * "findfile({fname}[, {path}[, {count}]])" function |
| 1022 | */ |
| 1023 | void |
| 1024 | f_findfile(typval_T *argvars, typval_T *rettv) |
| 1025 | { |
| 1026 | findfilendir(argvars, rettv, FINDFILE_FILE); |
| 1027 | } |
| 1028 | |
| 1029 | /* |
| 1030 | * "fnamemodify({fname}, {mods})" function |
| 1031 | */ |
| 1032 | void |
| 1033 | f_fnamemodify(typval_T *argvars, typval_T *rettv) |
| 1034 | { |
| 1035 | char_u *fname; |
| 1036 | char_u *mods; |
| 1037 | int usedlen = 0; |
Bram Moolenaar | c530852 | 2020-12-13 12:25:35 +0100 | [diff] [blame] | 1038 | int len = 0; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1039 | char_u *fbuf = NULL; |
| 1040 | char_u buf[NUMBUFLEN]; |
| 1041 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1042 | if (in_vim9script() |
| 1043 | && (check_for_string_arg(argvars, 0) == FAIL |
| 1044 | || check_for_string_arg(argvars, 1) == FAIL)) |
Bram Moolenaar | 7bb4e74 | 2020-12-09 12:41:50 +0100 | [diff] [blame] | 1045 | return; |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1046 | |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1047 | fname = tv_get_string_chk(&argvars[0]); |
| 1048 | mods = tv_get_string_buf_chk(&argvars[1], buf); |
Bram Moolenaar | c530852 | 2020-12-13 12:25:35 +0100 | [diff] [blame] | 1049 | if (mods == NULL || fname == NULL) |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1050 | fname = NULL; |
Bram Moolenaar | c530852 | 2020-12-13 12:25:35 +0100 | [diff] [blame] | 1051 | else |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1052 | { |
| 1053 | len = (int)STRLEN(fname); |
Bram Moolenaar | c530852 | 2020-12-13 12:25:35 +0100 | [diff] [blame] | 1054 | if (mods != NULL && *mods != NUL) |
| 1055 | (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | rettv->v_type = VAR_STRING; |
| 1059 | if (fname == NULL) |
| 1060 | rettv->vval.v_string = NULL; |
| 1061 | else |
| 1062 | rettv->vval.v_string = vim_strnsave(fname, len); |
| 1063 | vim_free(fbuf); |
| 1064 | } |
| 1065 | |
| 1066 | /* |
| 1067 | * "getcwd()" function |
| 1068 | * |
| 1069 | * Return the current working directory of a window in a tab page. |
| 1070 | * First optional argument 'winnr' is the window number or -1 and the second |
| 1071 | * optional argument 'tabnr' is the tab page number. |
| 1072 | * |
| 1073 | * If no arguments are supplied, then return the directory of the current |
| 1074 | * window. |
| 1075 | * If only 'winnr' is specified and is not -1 or 0 then return the directory of |
| 1076 | * the specified window. |
| 1077 | * If 'winnr' is 0 then return the directory of the current window. |
| 1078 | * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the |
| 1079 | * directory of the specified tab page. Otherwise return the directory of the |
| 1080 | * specified window in the specified tab page. |
| 1081 | * If the window or the tab page doesn't exist then return NULL. |
| 1082 | */ |
| 1083 | void |
| 1084 | f_getcwd(typval_T *argvars, typval_T *rettv) |
| 1085 | { |
| 1086 | win_T *wp = NULL; |
| 1087 | tabpage_T *tp = NULL; |
| 1088 | char_u *cwd; |
| 1089 | int global = FALSE; |
| 1090 | |
| 1091 | rettv->v_type = VAR_STRING; |
| 1092 | rettv->vval.v_string = NULL; |
| 1093 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1094 | if (in_vim9script() |
| 1095 | && (check_for_opt_number_arg(argvars, 0) == FAIL |
| 1096 | || (argvars[0].v_type != VAR_UNKNOWN |
| 1097 | && check_for_opt_number_arg(argvars, 1) == FAIL))) |
| 1098 | return; |
| 1099 | |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1100 | if (argvars[0].v_type == VAR_NUMBER |
| 1101 | && argvars[0].vval.v_number == -1 |
| 1102 | && argvars[1].v_type == VAR_UNKNOWN) |
| 1103 | global = TRUE; |
| 1104 | else |
| 1105 | wp = find_tabwin(&argvars[0], &argvars[1], &tp); |
| 1106 | |
Bram Moolenaar | 851c7a6 | 2021-11-18 20:47:31 +0000 | [diff] [blame] | 1107 | if (wp != NULL && wp->w_localdir != NULL |
| 1108 | && argvars[0].v_type != VAR_UNKNOWN) |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1109 | rettv->vval.v_string = vim_strsave(wp->w_localdir); |
Bram Moolenaar | 851c7a6 | 2021-11-18 20:47:31 +0000 | [diff] [blame] | 1110 | else if (tp != NULL && tp->tp_localdir != NULL |
| 1111 | && argvars[0].v_type != VAR_UNKNOWN) |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1112 | rettv->vval.v_string = vim_strsave(tp->tp_localdir); |
| 1113 | else if (wp != NULL || tp != NULL || global) |
| 1114 | { |
Bram Moolenaar | 851c7a6 | 2021-11-18 20:47:31 +0000 | [diff] [blame] | 1115 | if (globaldir != NULL && argvars[0].v_type != VAR_UNKNOWN) |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1116 | rettv->vval.v_string = vim_strsave(globaldir); |
| 1117 | else |
| 1118 | { |
| 1119 | cwd = alloc(MAXPATHL); |
| 1120 | if (cwd != NULL) |
| 1121 | { |
| 1122 | if (mch_dirname(cwd, MAXPATHL) != FAIL) |
| 1123 | rettv->vval.v_string = vim_strsave(cwd); |
| 1124 | vim_free(cwd); |
| 1125 | } |
| 1126 | } |
| 1127 | } |
| 1128 | #ifdef BACKSLASH_IN_FILENAME |
| 1129 | if (rettv->vval.v_string != NULL) |
| 1130 | slash_adjust(rettv->vval.v_string); |
| 1131 | #endif |
| 1132 | } |
| 1133 | |
| 1134 | /* |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 1135 | * Convert "st" to file permission string. |
| 1136 | */ |
| 1137 | char_u * |
| 1138 | getfpermst(stat_T *st, char_u *perm) |
| 1139 | { |
| 1140 | char_u flags[] = "rwx"; |
| 1141 | int i; |
| 1142 | |
| 1143 | for (i = 0; i < 9; i++) |
| 1144 | { |
| 1145 | if (st->st_mode & (1 << (8 - i))) |
| 1146 | perm[i] = flags[i % 3]; |
| 1147 | else |
| 1148 | perm[i] = '-'; |
| 1149 | } |
| 1150 | return perm; |
| 1151 | } |
| 1152 | |
| 1153 | /* |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1154 | * "getfperm({fname})" function |
| 1155 | */ |
| 1156 | void |
| 1157 | f_getfperm(typval_T *argvars, typval_T *rettv) |
| 1158 | { |
| 1159 | char_u *fname; |
| 1160 | stat_T st; |
| 1161 | char_u *perm = NULL; |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 1162 | char_u permbuf[] = "---------"; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1163 | |
Bram Moolenaar | 32105ae | 2021-03-27 18:59:25 +0100 | [diff] [blame] | 1164 | if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL) |
Bram Moolenaar | 7bb4e74 | 2020-12-09 12:41:50 +0100 | [diff] [blame] | 1165 | return; |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1166 | |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1167 | fname = tv_get_string(&argvars[0]); |
| 1168 | |
| 1169 | rettv->v_type = VAR_STRING; |
| 1170 | if (mch_stat((char *)fname, &st) >= 0) |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 1171 | perm = vim_strsave(getfpermst(&st, permbuf)); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1172 | rettv->vval.v_string = perm; |
| 1173 | } |
| 1174 | |
| 1175 | /* |
| 1176 | * "getfsize({fname})" function |
| 1177 | */ |
| 1178 | void |
| 1179 | f_getfsize(typval_T *argvars, typval_T *rettv) |
| 1180 | { |
| 1181 | char_u *fname; |
| 1182 | stat_T st; |
| 1183 | |
Bram Moolenaar | 32105ae | 2021-03-27 18:59:25 +0100 | [diff] [blame] | 1184 | if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL) |
Bram Moolenaar | 7bb4e74 | 2020-12-09 12:41:50 +0100 | [diff] [blame] | 1185 | return; |
| 1186 | |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1187 | fname = tv_get_string(&argvars[0]); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1188 | if (mch_stat((char *)fname, &st) >= 0) |
| 1189 | { |
| 1190 | if (mch_isdir(fname)) |
| 1191 | rettv->vval.v_number = 0; |
| 1192 | else |
| 1193 | { |
| 1194 | rettv->vval.v_number = (varnumber_T)st.st_size; |
| 1195 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1196 | // non-perfect check for overflow |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1197 | if ((off_T)rettv->vval.v_number != (off_T)st.st_size) |
| 1198 | rettv->vval.v_number = -2; |
| 1199 | } |
| 1200 | } |
| 1201 | else |
| 1202 | rettv->vval.v_number = -1; |
| 1203 | } |
| 1204 | |
| 1205 | /* |
| 1206 | * "getftime({fname})" function |
| 1207 | */ |
| 1208 | void |
| 1209 | f_getftime(typval_T *argvars, typval_T *rettv) |
| 1210 | { |
| 1211 | char_u *fname; |
| 1212 | stat_T st; |
| 1213 | |
Bram Moolenaar | 32105ae | 2021-03-27 18:59:25 +0100 | [diff] [blame] | 1214 | if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL) |
Bram Moolenaar | 7bb4e74 | 2020-12-09 12:41:50 +0100 | [diff] [blame] | 1215 | return; |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1216 | |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1217 | fname = tv_get_string(&argvars[0]); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1218 | if (mch_stat((char *)fname, &st) >= 0) |
| 1219 | rettv->vval.v_number = (varnumber_T)st.st_mtime; |
| 1220 | else |
| 1221 | rettv->vval.v_number = -1; |
| 1222 | } |
| 1223 | |
| 1224 | /* |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 1225 | * Convert "st" to file type string. |
| 1226 | */ |
| 1227 | char_u * |
| 1228 | getftypest(stat_T *st) |
| 1229 | { |
| 1230 | char *t; |
| 1231 | |
| 1232 | if (S_ISREG(st->st_mode)) |
| 1233 | t = "file"; |
| 1234 | else if (S_ISDIR(st->st_mode)) |
| 1235 | t = "dir"; |
| 1236 | else if (S_ISLNK(st->st_mode)) |
| 1237 | t = "link"; |
| 1238 | else if (S_ISBLK(st->st_mode)) |
| 1239 | t = "bdev"; |
| 1240 | else if (S_ISCHR(st->st_mode)) |
| 1241 | t = "cdev"; |
| 1242 | else if (S_ISFIFO(st->st_mode)) |
| 1243 | t = "fifo"; |
| 1244 | else if (S_ISSOCK(st->st_mode)) |
| 1245 | t = "socket"; |
| 1246 | else |
| 1247 | t = "other"; |
| 1248 | return (char_u*)t; |
| 1249 | } |
| 1250 | |
| 1251 | /* |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1252 | * "getftype({fname})" function |
| 1253 | */ |
| 1254 | void |
| 1255 | f_getftype(typval_T *argvars, typval_T *rettv) |
| 1256 | { |
| 1257 | char_u *fname; |
| 1258 | stat_T st; |
| 1259 | char_u *type = NULL; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1260 | |
Bram Moolenaar | 32105ae | 2021-03-27 18:59:25 +0100 | [diff] [blame] | 1261 | if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL) |
Bram Moolenaar | 7bb4e74 | 2020-12-09 12:41:50 +0100 | [diff] [blame] | 1262 | return; |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1263 | |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1264 | fname = tv_get_string(&argvars[0]); |
| 1265 | |
| 1266 | rettv->v_type = VAR_STRING; |
| 1267 | if (mch_lstat((char *)fname, &st) >= 0) |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 1268 | type = vim_strsave(getftypest(&st)); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1269 | rettv->vval.v_string = type; |
| 1270 | } |
| 1271 | |
| 1272 | /* |
| 1273 | * "glob()" function |
| 1274 | */ |
| 1275 | void |
| 1276 | f_glob(typval_T *argvars, typval_T *rettv) |
| 1277 | { |
| 1278 | int options = WILD_SILENT|WILD_USE_NL; |
| 1279 | expand_T xpc; |
| 1280 | int error = FALSE; |
| 1281 | |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1282 | if (in_vim9script() |
| 1283 | && (check_for_string_arg(argvars, 0) == FAIL |
| 1284 | || check_for_opt_bool_arg(argvars, 1) == FAIL |
| 1285 | || (argvars[1].v_type != VAR_UNKNOWN |
| 1286 | && (check_for_opt_bool_arg(argvars, 2) == FAIL |
| 1287 | || (argvars[2].v_type != VAR_UNKNOWN |
| 1288 | && check_for_opt_bool_arg(argvars, 3) == FAIL))))) |
| 1289 | return; |
| 1290 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1291 | // When the optional second argument is non-zero, don't remove matches |
| 1292 | // for 'wildignore' and don't put matches for 'suffixes' at the end. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1293 | rettv->v_type = VAR_STRING; |
| 1294 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 1295 | { |
Bram Moolenaar | 5892ea1 | 2020-09-02 21:53:11 +0200 | [diff] [blame] | 1296 | if (tv_get_bool_chk(&argvars[1], &error)) |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1297 | options |= WILD_KEEP_ALL; |
| 1298 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 1299 | { |
Bram Moolenaar | 5892ea1 | 2020-09-02 21:53:11 +0200 | [diff] [blame] | 1300 | if (tv_get_bool_chk(&argvars[2], &error)) |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1301 | rettv_list_set(rettv, NULL); |
| 1302 | if (argvars[3].v_type != VAR_UNKNOWN |
Bram Moolenaar | 5892ea1 | 2020-09-02 21:53:11 +0200 | [diff] [blame] | 1303 | && tv_get_bool_chk(&argvars[3], &error)) |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1304 | options |= WILD_ALLLINKS; |
| 1305 | } |
| 1306 | } |
| 1307 | if (!error) |
| 1308 | { |
| 1309 | ExpandInit(&xpc); |
| 1310 | xpc.xp_context = EXPAND_FILES; |
| 1311 | if (p_wic) |
| 1312 | options += WILD_ICASE; |
| 1313 | if (rettv->v_type == VAR_STRING) |
| 1314 | rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]), |
| 1315 | NULL, options, WILD_ALL); |
| 1316 | else if (rettv_list_alloc(rettv) != FAIL) |
| 1317 | { |
| 1318 | int i; |
| 1319 | |
| 1320 | ExpandOne(&xpc, tv_get_string(&argvars[0]), |
| 1321 | NULL, options, WILD_ALL_KEEP); |
| 1322 | for (i = 0; i < xpc.xp_numfiles; i++) |
| 1323 | list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1); |
| 1324 | |
| 1325 | ExpandCleanup(&xpc); |
| 1326 | } |
| 1327 | } |
| 1328 | else |
| 1329 | rettv->vval.v_string = NULL; |
| 1330 | } |
| 1331 | |
| 1332 | /* |
| 1333 | * "glob2regpat()" function |
| 1334 | */ |
| 1335 | void |
| 1336 | f_glob2regpat(typval_T *argvars, typval_T *rettv) |
| 1337 | { |
Bram Moolenaar | 3cfa5b1 | 2021-06-06 14:14:39 +0200 | [diff] [blame] | 1338 | char_u buf[NUMBUFLEN]; |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1339 | char_u *pat; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1340 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1341 | if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL) |
| 1342 | return; |
| 1343 | |
| 1344 | pat = tv_get_string_buf_chk_strict(&argvars[0], buf, in_vim9script()); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1345 | rettv->v_type = VAR_STRING; |
| 1346 | rettv->vval.v_string = (pat == NULL) |
| 1347 | ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE); |
| 1348 | } |
| 1349 | |
| 1350 | /* |
| 1351 | * "globpath()" function |
| 1352 | */ |
| 1353 | void |
| 1354 | f_globpath(typval_T *argvars, typval_T *rettv) |
| 1355 | { |
| 1356 | int flags = WILD_IGNORE_COMPLETESLASH; |
| 1357 | char_u buf1[NUMBUFLEN]; |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1358 | char_u *file; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1359 | int error = FALSE; |
| 1360 | garray_T ga; |
| 1361 | int i; |
| 1362 | |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1363 | if (in_vim9script() |
| 1364 | && (check_for_string_arg(argvars, 0) == FAIL |
| 1365 | || check_for_string_arg(argvars, 1) == FAIL |
| 1366 | || check_for_opt_bool_arg(argvars, 2) == FAIL |
| 1367 | || (argvars[2].v_type != VAR_UNKNOWN |
| 1368 | && (check_for_opt_bool_arg(argvars, 3) == FAIL |
| 1369 | || (argvars[3].v_type != VAR_UNKNOWN |
| 1370 | && check_for_opt_bool_arg(argvars, 4) == FAIL))))) |
| 1371 | return; |
| 1372 | |
| 1373 | file = tv_get_string_buf_chk(&argvars[1], buf1); |
| 1374 | |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1375 | // When the optional second argument is non-zero, don't remove matches |
| 1376 | // for 'wildignore' and don't put matches for 'suffixes' at the end. |
| 1377 | rettv->v_type = VAR_STRING; |
| 1378 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 1379 | { |
Bram Moolenaar | f966ce5 | 2020-09-02 21:57:07 +0200 | [diff] [blame] | 1380 | if (tv_get_bool_chk(&argvars[2], &error)) |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1381 | flags |= WILD_KEEP_ALL; |
| 1382 | if (argvars[3].v_type != VAR_UNKNOWN) |
| 1383 | { |
Bram Moolenaar | f966ce5 | 2020-09-02 21:57:07 +0200 | [diff] [blame] | 1384 | if (tv_get_bool_chk(&argvars[3], &error)) |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1385 | rettv_list_set(rettv, NULL); |
| 1386 | if (argvars[4].v_type != VAR_UNKNOWN |
Bram Moolenaar | f966ce5 | 2020-09-02 21:57:07 +0200 | [diff] [blame] | 1387 | && tv_get_bool_chk(&argvars[4], &error)) |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1388 | flags |= WILD_ALLLINKS; |
| 1389 | } |
| 1390 | } |
| 1391 | if (file != NULL && !error) |
| 1392 | { |
Bram Moolenaar | 04935fb | 2022-01-08 16:19:22 +0000 | [diff] [blame] | 1393 | ga_init2(&ga, sizeof(char_u *), 10); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1394 | globpath(tv_get_string(&argvars[0]), file, &ga, flags); |
| 1395 | if (rettv->v_type == VAR_STRING) |
| 1396 | rettv->vval.v_string = ga_concat_strings(&ga, "\n"); |
| 1397 | else if (rettv_list_alloc(rettv) != FAIL) |
| 1398 | for (i = 0; i < ga.ga_len; ++i) |
| 1399 | list_append_string(rettv->vval.v_list, |
| 1400 | ((char_u **)(ga.ga_data))[i], -1); |
| 1401 | ga_clear_strings(&ga); |
| 1402 | } |
| 1403 | else |
| 1404 | rettv->vval.v_string = NULL; |
| 1405 | } |
| 1406 | |
| 1407 | /* |
| 1408 | * "isdirectory()" function |
| 1409 | */ |
| 1410 | void |
| 1411 | f_isdirectory(typval_T *argvars, typval_T *rettv) |
| 1412 | { |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1413 | if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL) |
| 1414 | return; |
| 1415 | |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1416 | rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0])); |
| 1417 | } |
| 1418 | |
| 1419 | /* |
LemonBoy | dca1d40 | 2022-04-28 15:26:33 +0100 | [diff] [blame] | 1420 | * "isabsolutepath()" function |
| 1421 | */ |
| 1422 | void |
| 1423 | f_isabsolutepath(typval_T *argvars, typval_T *rettv) |
| 1424 | { |
| 1425 | if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL) |
| 1426 | return; |
| 1427 | |
| 1428 | rettv->vval.v_number = mch_isFullName(tv_get_string_strict(&argvars[0])); |
| 1429 | } |
| 1430 | |
| 1431 | /* |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1432 | * Create the directory in which "dir" is located, and higher levels when |
| 1433 | * needed. |
| 1434 | * Return OK or FAIL. |
| 1435 | */ |
| 1436 | static int |
| 1437 | mkdir_recurse(char_u *dir, int prot) |
| 1438 | { |
| 1439 | char_u *p; |
| 1440 | char_u *updir; |
| 1441 | int r = FAIL; |
| 1442 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1443 | // Get end of directory name in "dir". |
| 1444 | // We're done when it's "/" or "c:/". |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1445 | p = gettail_sep(dir); |
| 1446 | if (p <= get_past_head(dir)) |
| 1447 | return OK; |
| 1448 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1449 | // If the directory exists we're done. Otherwise: create it. |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1450 | updir = vim_strnsave(dir, p - dir); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1451 | if (updir == NULL) |
| 1452 | return FAIL; |
| 1453 | if (mch_isdir(updir)) |
| 1454 | r = OK; |
| 1455 | else if (mkdir_recurse(updir, prot) == OK) |
| 1456 | r = vim_mkdir_emsg(updir, prot); |
| 1457 | vim_free(updir); |
| 1458 | return r; |
| 1459 | } |
| 1460 | |
| 1461 | /* |
| 1462 | * "mkdir()" function |
| 1463 | */ |
| 1464 | void |
| 1465 | f_mkdir(typval_T *argvars, typval_T *rettv) |
| 1466 | { |
| 1467 | char_u *dir; |
| 1468 | char_u buf[NUMBUFLEN]; |
| 1469 | int prot = 0755; |
| 1470 | |
| 1471 | rettv->vval.v_number = FAIL; |
| 1472 | if (check_restricted() || check_secure()) |
| 1473 | return; |
| 1474 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1475 | if (in_vim9script() |
| 1476 | && (check_for_nonempty_string_arg(argvars, 0) == FAIL |
| 1477 | || check_for_opt_string_arg(argvars, 1) == FAIL |
| 1478 | || (argvars[1].v_type != VAR_UNKNOWN |
| 1479 | && check_for_opt_number_arg(argvars, 2) == FAIL))) |
| 1480 | return; |
| 1481 | |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1482 | dir = tv_get_string_buf(&argvars[0], buf); |
| 1483 | if (*dir == NUL) |
| 1484 | return; |
| 1485 | |
| 1486 | if (*gettail(dir) == NUL) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1487 | // remove trailing slashes |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1488 | *gettail_sep(dir) = NUL; |
| 1489 | |
| 1490 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 1491 | { |
| 1492 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 1493 | { |
| 1494 | prot = (int)tv_get_number_chk(&argvars[2], NULL); |
| 1495 | if (prot == -1) |
| 1496 | return; |
| 1497 | } |
| 1498 | if (STRCMP(tv_get_string(&argvars[1]), "p") == 0) |
| 1499 | { |
| 1500 | if (mch_isdir(dir)) |
| 1501 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1502 | // With the "p" flag it's OK if the dir already exists. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1503 | rettv->vval.v_number = OK; |
| 1504 | return; |
| 1505 | } |
| 1506 | mkdir_recurse(dir, prot); |
| 1507 | } |
| 1508 | } |
| 1509 | rettv->vval.v_number = vim_mkdir_emsg(dir, prot); |
| 1510 | } |
| 1511 | |
| 1512 | /* |
Bram Moolenaar | af7645d | 2019-09-05 22:33:28 +0200 | [diff] [blame] | 1513 | * "pathshorten()" function |
| 1514 | */ |
| 1515 | void |
| 1516 | f_pathshorten(typval_T *argvars, typval_T *rettv) |
| 1517 | { |
| 1518 | char_u *p; |
Bram Moolenaar | 6a33ef0 | 2020-09-25 22:42:48 +0200 | [diff] [blame] | 1519 | int trim_len = 1; |
| 1520 | |
Yegappan Lakshmanan | 1a71d31 | 2021-07-15 12:49:58 +0200 | [diff] [blame] | 1521 | if (in_vim9script() |
| 1522 | && (check_for_string_arg(argvars, 0) == FAIL |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1523 | || check_for_opt_number_arg(argvars, 1) == FAIL)) |
Yegappan Lakshmanan | 1a71d31 | 2021-07-15 12:49:58 +0200 | [diff] [blame] | 1524 | return; |
| 1525 | |
Bram Moolenaar | 6a33ef0 | 2020-09-25 22:42:48 +0200 | [diff] [blame] | 1526 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 1527 | { |
| 1528 | trim_len = (int)tv_get_number(&argvars[1]); |
| 1529 | if (trim_len < 1) |
| 1530 | trim_len = 1; |
| 1531 | } |
Bram Moolenaar | af7645d | 2019-09-05 22:33:28 +0200 | [diff] [blame] | 1532 | |
| 1533 | rettv->v_type = VAR_STRING; |
| 1534 | p = tv_get_string_chk(&argvars[0]); |
Bram Moolenaar | 6a33ef0 | 2020-09-25 22:42:48 +0200 | [diff] [blame] | 1535 | |
Bram Moolenaar | af7645d | 2019-09-05 22:33:28 +0200 | [diff] [blame] | 1536 | if (p == NULL) |
| 1537 | rettv->vval.v_string = NULL; |
| 1538 | else |
| 1539 | { |
| 1540 | p = vim_strsave(p); |
| 1541 | rettv->vval.v_string = p; |
| 1542 | if (p != NULL) |
Bram Moolenaar | 6a33ef0 | 2020-09-25 22:42:48 +0200 | [diff] [blame] | 1543 | shorten_dir_len(p, trim_len); |
Bram Moolenaar | af7645d | 2019-09-05 22:33:28 +0200 | [diff] [blame] | 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | /* |
Bram Moolenaar | f8abbf3 | 2020-08-19 16:00:06 +0200 | [diff] [blame] | 1548 | * Common code for readdir_checkitem() and readdirex_checkitem(). |
| 1549 | * Either "name" or "dict" is NULL. |
Bram Moolenaar | 80147dd | 2020-02-04 22:32:59 +0100 | [diff] [blame] | 1550 | */ |
| 1551 | static int |
Bram Moolenaar | f8abbf3 | 2020-08-19 16:00:06 +0200 | [diff] [blame] | 1552 | checkitem_common(void *context, char_u *name, dict_T *dict) |
Bram Moolenaar | 80147dd | 2020-02-04 22:32:59 +0100 | [diff] [blame] | 1553 | { |
| 1554 | typval_T *expr = (typval_T *)context; |
| 1555 | typval_T save_val; |
| 1556 | typval_T rettv; |
| 1557 | typval_T argv[2]; |
| 1558 | int retval = 0; |
| 1559 | int error = FALSE; |
Bram Moolenaar | 80147dd | 2020-02-04 22:32:59 +0100 | [diff] [blame] | 1560 | |
| 1561 | prepare_vimvar(VV_VAL, &save_val); |
Bram Moolenaar | f8abbf3 | 2020-08-19 16:00:06 +0200 | [diff] [blame] | 1562 | if (name != NULL) |
| 1563 | { |
| 1564 | set_vim_var_string(VV_VAL, name, -1); |
| 1565 | argv[0].v_type = VAR_STRING; |
| 1566 | argv[0].vval.v_string = name; |
| 1567 | } |
| 1568 | else |
| 1569 | { |
| 1570 | set_vim_var_dict(VV_VAL, dict); |
| 1571 | argv[0].v_type = VAR_DICT; |
| 1572 | argv[0].vval.v_dict = dict; |
| 1573 | } |
Bram Moolenaar | 80147dd | 2020-02-04 22:32:59 +0100 | [diff] [blame] | 1574 | |
| 1575 | if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL) |
| 1576 | goto theend; |
| 1577 | |
Bram Moolenaar | f8abbf3 | 2020-08-19 16:00:06 +0200 | [diff] [blame] | 1578 | // We want to use -1, but also true/false should be allowed. |
| 1579 | if (rettv.v_type == VAR_SPECIAL || rettv.v_type == VAR_BOOL) |
| 1580 | { |
| 1581 | rettv.v_type = VAR_NUMBER; |
| 1582 | rettv.vval.v_number = rettv.vval.v_number == VVAL_TRUE; |
| 1583 | } |
Bram Moolenaar | 80147dd | 2020-02-04 22:32:59 +0100 | [diff] [blame] | 1584 | retval = tv_get_number_chk(&rettv, &error); |
| 1585 | if (error) |
| 1586 | retval = -1; |
| 1587 | clear_tv(&rettv); |
| 1588 | |
| 1589 | theend: |
Bram Moolenaar | f8abbf3 | 2020-08-19 16:00:06 +0200 | [diff] [blame] | 1590 | if (name != NULL) |
| 1591 | set_vim_var_string(VV_VAL, NULL, 0); |
| 1592 | else |
| 1593 | set_vim_var_dict(VV_VAL, NULL); |
Bram Moolenaar | 80147dd | 2020-02-04 22:32:59 +0100 | [diff] [blame] | 1594 | restore_vimvar(VV_VAL, &save_val); |
| 1595 | return retval; |
| 1596 | } |
| 1597 | |
Bram Moolenaar | f8abbf3 | 2020-08-19 16:00:06 +0200 | [diff] [blame] | 1598 | /* |
| 1599 | * Evaluate "expr" (= "context") for readdir(). |
| 1600 | */ |
| 1601 | static int |
| 1602 | readdir_checkitem(void *context, void *item) |
| 1603 | { |
| 1604 | char_u *name = (char_u *)item; |
| 1605 | |
| 1606 | return checkitem_common(context, name, NULL); |
| 1607 | } |
| 1608 | |
Bram Moolenaar | 84cf6bd | 2020-06-16 20:03:43 +0200 | [diff] [blame] | 1609 | static int |
| 1610 | readdirex_dict_arg(typval_T *tv, int *cmp) |
| 1611 | { |
| 1612 | char_u *compare; |
| 1613 | |
| 1614 | if (tv->v_type != VAR_DICT) |
| 1615 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 1616 | emsg(_(e_dictionary_required)); |
Bram Moolenaar | 84cf6bd | 2020-06-16 20:03:43 +0200 | [diff] [blame] | 1617 | return FAIL; |
| 1618 | } |
| 1619 | |
Yegappan Lakshmanan | 4829c1c | 2022-04-04 15:16:54 +0100 | [diff] [blame] | 1620 | if (dict_has_key(tv->vval.v_dict, "sort")) |
Bram Moolenaar | 84cf6bd | 2020-06-16 20:03:43 +0200 | [diff] [blame] | 1621 | compare = dict_get_string(tv->vval.v_dict, (char_u *)"sort", FALSE); |
| 1622 | else |
| 1623 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 1624 | semsg(_(e_dictionary_key_str_required), "sort"); |
Bram Moolenaar | 84cf6bd | 2020-06-16 20:03:43 +0200 | [diff] [blame] | 1625 | return FAIL; |
| 1626 | } |
| 1627 | |
| 1628 | if (STRCMP(compare, (char_u *) "none") == 0) |
| 1629 | *cmp = READDIR_SORT_NONE; |
| 1630 | else if (STRCMP(compare, (char_u *) "case") == 0) |
| 1631 | *cmp = READDIR_SORT_BYTE; |
| 1632 | else if (STRCMP(compare, (char_u *) "icase") == 0) |
| 1633 | *cmp = READDIR_SORT_IC; |
| 1634 | else if (STRCMP(compare, (char_u *) "collate") == 0) |
| 1635 | *cmp = READDIR_SORT_COLLATE; |
| 1636 | return OK; |
| 1637 | } |
| 1638 | |
Bram Moolenaar | 80147dd | 2020-02-04 22:32:59 +0100 | [diff] [blame] | 1639 | /* |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1640 | * "readdir()" function |
| 1641 | */ |
| 1642 | void |
| 1643 | f_readdir(typval_T *argvars, typval_T *rettv) |
| 1644 | { |
| 1645 | typval_T *expr; |
| 1646 | int ret; |
| 1647 | char_u *path; |
| 1648 | char_u *p; |
| 1649 | garray_T ga; |
| 1650 | int i; |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 1651 | int sort = READDIR_SORT_BYTE; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1652 | |
| 1653 | if (rettv_list_alloc(rettv) == FAIL) |
| 1654 | return; |
Yegappan Lakshmanan | 5bca906 | 2021-07-24 21:33:26 +0200 | [diff] [blame] | 1655 | |
| 1656 | if (in_vim9script() |
| 1657 | && (check_for_string_arg(argvars, 0) == FAIL |
| 1658 | || (argvars[1].v_type != VAR_UNKNOWN |
| 1659 | && check_for_opt_dict_arg(argvars, 2) == FAIL))) |
| 1660 | return; |
| 1661 | |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1662 | path = tv_get_string(&argvars[0]); |
| 1663 | expr = &argvars[1]; |
| 1664 | |
Bram Moolenaar | 84cf6bd | 2020-06-16 20:03:43 +0200 | [diff] [blame] | 1665 | if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN && |
| 1666 | readdirex_dict_arg(&argvars[2], &sort) == FAIL) |
| 1667 | return; |
| 1668 | |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 1669 | ret = readdir_core(&ga, path, FALSE, (void *)expr, |
Bram Moolenaar | 84cf6bd | 2020-06-16 20:03:43 +0200 | [diff] [blame] | 1670 | (expr->v_type == VAR_UNKNOWN) ? NULL : readdir_checkitem, sort); |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 1671 | if (ret == OK) |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1672 | { |
| 1673 | for (i = 0; i < ga.ga_len; i++) |
| 1674 | { |
| 1675 | p = ((char_u **)ga.ga_data)[i]; |
| 1676 | list_append_string(rettv->vval.v_list, p, -1); |
| 1677 | } |
| 1678 | } |
| 1679 | ga_clear_strings(&ga); |
| 1680 | } |
| 1681 | |
| 1682 | /* |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 1683 | * Evaluate "expr" (= "context") for readdirex(). |
| 1684 | */ |
| 1685 | static int |
| 1686 | readdirex_checkitem(void *context, void *item) |
| 1687 | { |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 1688 | dict_T *dict = (dict_T*)item; |
| 1689 | |
Bram Moolenaar | f8abbf3 | 2020-08-19 16:00:06 +0200 | [diff] [blame] | 1690 | return checkitem_common(context, NULL, dict); |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | /* |
| 1694 | * "readdirex()" function |
| 1695 | */ |
| 1696 | void |
| 1697 | f_readdirex(typval_T *argvars, typval_T *rettv) |
| 1698 | { |
| 1699 | typval_T *expr; |
| 1700 | int ret; |
| 1701 | char_u *path; |
| 1702 | garray_T ga; |
| 1703 | int i; |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 1704 | int sort = READDIR_SORT_BYTE; |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 1705 | |
| 1706 | if (rettv_list_alloc(rettv) == FAIL) |
| 1707 | return; |
Yegappan Lakshmanan | 5bca906 | 2021-07-24 21:33:26 +0200 | [diff] [blame] | 1708 | |
| 1709 | if (in_vim9script() |
| 1710 | && (check_for_string_arg(argvars, 0) == FAIL |
| 1711 | || (argvars[1].v_type != VAR_UNKNOWN |
| 1712 | && check_for_opt_dict_arg(argvars, 2) == FAIL))) |
| 1713 | return; |
| 1714 | |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 1715 | path = tv_get_string(&argvars[0]); |
| 1716 | expr = &argvars[1]; |
| 1717 | |
Bram Moolenaar | 84cf6bd | 2020-06-16 20:03:43 +0200 | [diff] [blame] | 1718 | if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN && |
| 1719 | readdirex_dict_arg(&argvars[2], &sort) == FAIL) |
| 1720 | return; |
| 1721 | |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 1722 | ret = readdir_core(&ga, path, TRUE, (void *)expr, |
Bram Moolenaar | 84cf6bd | 2020-06-16 20:03:43 +0200 | [diff] [blame] | 1723 | (expr->v_type == VAR_UNKNOWN) ? NULL : readdirex_checkitem, sort); |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 1724 | if (ret == OK) |
| 1725 | { |
| 1726 | for (i = 0; i < ga.ga_len; i++) |
| 1727 | { |
| 1728 | dict_T *dict = ((dict_T**)ga.ga_data)[i]; |
| 1729 | list_append_dict(rettv->vval.v_list, dict); |
| 1730 | dict_unref(dict); |
| 1731 | } |
| 1732 | } |
| 1733 | ga_clear(&ga); |
| 1734 | } |
| 1735 | |
| 1736 | /* |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1737 | * "readfile()" function |
| 1738 | */ |
Bram Moolenaar | c423ad7 | 2021-01-13 20:38:03 +0100 | [diff] [blame] | 1739 | static void |
| 1740 | read_file_or_blob(typval_T *argvars, typval_T *rettv, int always_blob) |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1741 | { |
| 1742 | int binary = FALSE; |
Bram Moolenaar | c423ad7 | 2021-01-13 20:38:03 +0100 | [diff] [blame] | 1743 | int blob = always_blob; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1744 | int failed = FALSE; |
| 1745 | char_u *fname; |
| 1746 | FILE *fd; |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1747 | char_u buf[(IOSIZE/256)*256]; // rounded to avoid odd + 1 |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1748 | int io_size = sizeof(buf); |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1749 | int readlen; // size of last fread() |
| 1750 | char_u *prev = NULL; // previously read bytes, if any |
| 1751 | long prevlen = 0; // length of data in prev |
| 1752 | long prevsize = 0; // size of prev buffer |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1753 | long maxline = MAXLNUM; |
| 1754 | long cnt = 0; |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1755 | char_u *p; // position in buf |
| 1756 | char_u *start; // start of current line |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1757 | |
| 1758 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 1759 | { |
| 1760 | if (STRCMP(tv_get_string(&argvars[1]), "b") == 0) |
| 1761 | binary = TRUE; |
| 1762 | if (STRCMP(tv_get_string(&argvars[1]), "B") == 0) |
| 1763 | blob = TRUE; |
| 1764 | |
| 1765 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 1766 | maxline = (long)tv_get_number(&argvars[2]); |
| 1767 | } |
| 1768 | |
Bram Moolenaar | 15352dc | 2020-04-06 21:12:42 +0200 | [diff] [blame] | 1769 | if ((blob ? rettv_blob_alloc(rettv) : rettv_list_alloc(rettv)) == FAIL) |
| 1770 | return; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1771 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1772 | // Always open the file in binary mode, library functions have a mind of |
| 1773 | // their own about CR-LF conversion. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1774 | fname = tv_get_string(&argvars[0]); |
Bram Moolenaar | 15352dc | 2020-04-06 21:12:42 +0200 | [diff] [blame] | 1775 | |
| 1776 | if (mch_isdir(fname)) |
| 1777 | { |
Bram Moolenaar | 4dea2d9 | 2022-03-31 11:37:57 +0100 | [diff] [blame] | 1778 | semsg(_(e_str_is_directory), fname); |
Bram Moolenaar | 15352dc | 2020-04-06 21:12:42 +0200 | [diff] [blame] | 1779 | return; |
| 1780 | } |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1781 | if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL) |
| 1782 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 1783 | semsg(_(e_cant_open_file_str), *fname == NUL ? (char_u *)_("<empty>") : fname); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1784 | return; |
| 1785 | } |
| 1786 | |
| 1787 | if (blob) |
| 1788 | { |
| 1789 | if (read_blob(fd, rettv->vval.v_blob) == FAIL) |
| 1790 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 1791 | semsg(_(e_cant_read_file_str), fname); |
Bram Moolenaar | 15352dc | 2020-04-06 21:12:42 +0200 | [diff] [blame] | 1792 | // An empty blob is returned on error. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1793 | blob_free(rettv->vval.v_blob); |
Bram Moolenaar | 15352dc | 2020-04-06 21:12:42 +0200 | [diff] [blame] | 1794 | rettv->vval.v_blob = NULL; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1795 | } |
| 1796 | fclose(fd); |
| 1797 | return; |
| 1798 | } |
| 1799 | |
| 1800 | while (cnt < maxline || maxline < 0) |
| 1801 | { |
| 1802 | readlen = (int)fread(buf, 1, io_size, fd); |
| 1803 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1804 | // This for loop processes what was read, but is also entered at end |
| 1805 | // of file so that either: |
| 1806 | // - an incomplete line gets written |
| 1807 | // - a "binary" file gets an empty line at the end if it ends in a |
| 1808 | // newline. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1809 | for (p = buf, start = buf; |
| 1810 | p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary)); |
| 1811 | ++p) |
| 1812 | { |
Dominique Pelle | f5d639a | 2022-01-12 15:24:40 +0000 | [diff] [blame] | 1813 | if (readlen <= 0 || *p == '\n') |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1814 | { |
| 1815 | listitem_T *li; |
| 1816 | char_u *s = NULL; |
| 1817 | long_u len = p - start; |
| 1818 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1819 | // Finished a line. Remove CRs before NL. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1820 | if (readlen > 0 && !binary) |
| 1821 | { |
| 1822 | while (len > 0 && start[len - 1] == '\r') |
| 1823 | --len; |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1824 | // removal may cross back to the "prev" string |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1825 | if (len == 0) |
| 1826 | while (prevlen > 0 && prev[prevlen - 1] == '\r') |
| 1827 | --prevlen; |
| 1828 | } |
| 1829 | if (prevlen == 0) |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1830 | s = vim_strnsave(start, len); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1831 | else |
| 1832 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1833 | // Change "prev" buffer to be the right size. This way |
| 1834 | // the bytes are only copied once, and very long lines are |
| 1835 | // allocated only once. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1836 | if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL) |
| 1837 | { |
| 1838 | mch_memmove(s + prevlen, start, len); |
| 1839 | s[prevlen + len] = NUL; |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1840 | prev = NULL; // the list will own the string |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1841 | prevlen = prevsize = 0; |
| 1842 | } |
| 1843 | } |
| 1844 | if (s == NULL) |
| 1845 | { |
| 1846 | do_outofmem_msg((long_u) prevlen + len + 1); |
| 1847 | failed = TRUE; |
| 1848 | break; |
| 1849 | } |
| 1850 | |
| 1851 | if ((li = listitem_alloc()) == NULL) |
| 1852 | { |
| 1853 | vim_free(s); |
| 1854 | failed = TRUE; |
| 1855 | break; |
| 1856 | } |
| 1857 | li->li_tv.v_type = VAR_STRING; |
| 1858 | li->li_tv.v_lock = 0; |
| 1859 | li->li_tv.vval.v_string = s; |
| 1860 | list_append(rettv->vval.v_list, li); |
| 1861 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1862 | start = p + 1; // step over newline |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1863 | if ((++cnt >= maxline && maxline >= 0) || readlen <= 0) |
| 1864 | break; |
| 1865 | } |
| 1866 | else if (*p == NUL) |
| 1867 | *p = '\n'; |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1868 | // Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this |
| 1869 | // when finding the BF and check the previous two bytes. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1870 | else if (*p == 0xbf && enc_utf8 && !binary) |
| 1871 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1872 | // Find the two bytes before the 0xbf. If p is at buf, or buf |
| 1873 | // + 1, these may be in the "prev" string. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1874 | char_u back1 = p >= buf + 1 ? p[-1] |
| 1875 | : prevlen >= 1 ? prev[prevlen - 1] : NUL; |
| 1876 | char_u back2 = p >= buf + 2 ? p[-2] |
| 1877 | : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1] |
| 1878 | : prevlen >= 2 ? prev[prevlen - 2] : NUL; |
| 1879 | |
| 1880 | if (back2 == 0xef && back1 == 0xbb) |
| 1881 | { |
| 1882 | char_u *dest = p - 2; |
| 1883 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1884 | // Usually a BOM is at the beginning of a file, and so at |
| 1885 | // the beginning of a line; then we can just step over it. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1886 | if (start == dest) |
| 1887 | start = p + 1; |
| 1888 | else |
| 1889 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1890 | // have to shuffle buf to close gap |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1891 | int adjust_prevlen = 0; |
| 1892 | |
| 1893 | if (dest < buf) |
| 1894 | { |
Bram Moolenaar | c423ad7 | 2021-01-13 20:38:03 +0100 | [diff] [blame] | 1895 | // must be 1 or 2 |
| 1896 | adjust_prevlen = (int)(buf - dest); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1897 | dest = buf; |
| 1898 | } |
| 1899 | if (readlen > p - buf + 1) |
| 1900 | mch_memmove(dest, p + 1, readlen - (p - buf) - 1); |
| 1901 | readlen -= 3 - adjust_prevlen; |
| 1902 | prevlen -= adjust_prevlen; |
| 1903 | p = dest - 1; |
| 1904 | } |
| 1905 | } |
| 1906 | } |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1907 | } // for |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1908 | |
| 1909 | if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0) |
| 1910 | break; |
| 1911 | if (start < p) |
| 1912 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1913 | // There's part of a line in buf, store it in "prev". |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1914 | if (p - start + prevlen >= prevsize) |
| 1915 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1916 | // need bigger "prev" buffer |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1917 | char_u *newprev; |
| 1918 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1919 | // A common use case is ordinary text files and "prev" gets a |
| 1920 | // fragment of a line, so the first allocation is made |
| 1921 | // small, to avoid repeatedly 'allocing' large and |
| 1922 | // 'reallocing' small. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1923 | if (prevsize == 0) |
| 1924 | prevsize = (long)(p - start); |
| 1925 | else |
| 1926 | { |
| 1927 | long grow50pc = (prevsize * 3) / 2; |
| 1928 | long growmin = (long)((p - start) * 2 + prevlen); |
| 1929 | prevsize = grow50pc > growmin ? grow50pc : growmin; |
| 1930 | } |
| 1931 | newprev = vim_realloc(prev, prevsize); |
| 1932 | if (newprev == NULL) |
| 1933 | { |
| 1934 | do_outofmem_msg((long_u)prevsize); |
| 1935 | failed = TRUE; |
| 1936 | break; |
| 1937 | } |
| 1938 | prev = newprev; |
| 1939 | } |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1940 | // Add the line part to end of "prev". |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1941 | mch_memmove(prev + prevlen, start, p - start); |
| 1942 | prevlen += (long)(p - start); |
| 1943 | } |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1944 | } // while |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1945 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 1946 | // For a negative line count use only the lines at the end of the file, |
| 1947 | // free the rest. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1948 | if (!failed && maxline < 0) |
| 1949 | while (cnt > -maxline) |
| 1950 | { |
| 1951 | listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first); |
| 1952 | --cnt; |
| 1953 | } |
| 1954 | |
| 1955 | if (failed) |
| 1956 | { |
| 1957 | // an empty list is returned on error |
| 1958 | list_free(rettv->vval.v_list); |
| 1959 | rettv_list_alloc(rettv); |
| 1960 | } |
| 1961 | |
| 1962 | vim_free(prev); |
| 1963 | fclose(fd); |
| 1964 | } |
| 1965 | |
| 1966 | /* |
Bram Moolenaar | c423ad7 | 2021-01-13 20:38:03 +0100 | [diff] [blame] | 1967 | * "readblob()" function |
| 1968 | */ |
| 1969 | void |
| 1970 | f_readblob(typval_T *argvars, typval_T *rettv) |
| 1971 | { |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1972 | if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL) |
| 1973 | return; |
| 1974 | |
Bram Moolenaar | c423ad7 | 2021-01-13 20:38:03 +0100 | [diff] [blame] | 1975 | read_file_or_blob(argvars, rettv, TRUE); |
| 1976 | } |
| 1977 | |
| 1978 | /* |
| 1979 | * "readfile()" function |
| 1980 | */ |
| 1981 | void |
| 1982 | f_readfile(typval_T *argvars, typval_T *rettv) |
| 1983 | { |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1984 | if (in_vim9script() |
| 1985 | && (check_for_nonempty_string_arg(argvars, 0) == FAIL |
| 1986 | || check_for_opt_string_arg(argvars, 1) == FAIL |
| 1987 | || (argvars[1].v_type != VAR_UNKNOWN |
| 1988 | && check_for_opt_number_arg(argvars, 2) == FAIL))) |
| 1989 | return; |
| 1990 | |
Bram Moolenaar | c423ad7 | 2021-01-13 20:38:03 +0100 | [diff] [blame] | 1991 | read_file_or_blob(argvars, rettv, FALSE); |
| 1992 | } |
| 1993 | |
| 1994 | /* |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 1995 | * "resolve()" function |
| 1996 | */ |
| 1997 | void |
| 1998 | f_resolve(typval_T *argvars, typval_T *rettv) |
| 1999 | { |
| 2000 | char_u *p; |
| 2001 | #ifdef HAVE_READLINK |
| 2002 | char_u *buf = NULL; |
| 2003 | #endif |
| 2004 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 2005 | if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL) |
| 2006 | return; |
| 2007 | |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2008 | p = tv_get_string(&argvars[0]); |
| 2009 | #ifdef FEAT_SHORTCUT |
| 2010 | { |
| 2011 | char_u *v = NULL; |
| 2012 | |
| 2013 | v = mch_resolve_path(p, TRUE); |
| 2014 | if (v != NULL) |
| 2015 | rettv->vval.v_string = v; |
| 2016 | else |
| 2017 | rettv->vval.v_string = vim_strsave(p); |
| 2018 | } |
| 2019 | #else |
| 2020 | # ifdef HAVE_READLINK |
| 2021 | { |
| 2022 | char_u *cpy; |
| 2023 | int len; |
| 2024 | char_u *remain = NULL; |
| 2025 | char_u *q; |
| 2026 | int is_relative_to_current = FALSE; |
| 2027 | int has_trailing_pathsep = FALSE; |
| 2028 | int limit = 100; |
| 2029 | |
| 2030 | p = vim_strsave(p); |
Bram Moolenaar | 70188f5 | 2019-12-23 18:18:52 +0100 | [diff] [blame] | 2031 | if (p == NULL) |
| 2032 | goto fail; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2033 | if (p[0] == '.' && (vim_ispathsep(p[1]) |
| 2034 | || (p[1] == '.' && (vim_ispathsep(p[2]))))) |
| 2035 | is_relative_to_current = TRUE; |
| 2036 | |
| 2037 | len = STRLEN(p); |
Bram Moolenaar | 50c4e9e | 2020-10-05 20:38:06 +0200 | [diff] [blame] | 2038 | if (len > 1 && after_pathsep(p, p + len)) |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2039 | { |
| 2040 | has_trailing_pathsep = TRUE; |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2041 | p[len - 1] = NUL; // the trailing slash breaks readlink() |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2042 | } |
| 2043 | |
| 2044 | q = getnextcomp(p); |
| 2045 | if (*q != NUL) |
| 2046 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2047 | // Separate the first path component in "p", and keep the |
| 2048 | // remainder (beginning with the path separator). |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2049 | remain = vim_strsave(q - 1); |
| 2050 | q[-1] = NUL; |
| 2051 | } |
| 2052 | |
| 2053 | buf = alloc(MAXPATHL + 1); |
| 2054 | if (buf == NULL) |
Bram Moolenaar | 70188f5 | 2019-12-23 18:18:52 +0100 | [diff] [blame] | 2055 | { |
| 2056 | vim_free(p); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2057 | goto fail; |
Bram Moolenaar | 70188f5 | 2019-12-23 18:18:52 +0100 | [diff] [blame] | 2058 | } |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2059 | |
| 2060 | for (;;) |
| 2061 | { |
| 2062 | for (;;) |
| 2063 | { |
| 2064 | len = readlink((char *)p, (char *)buf, MAXPATHL); |
| 2065 | if (len <= 0) |
| 2066 | break; |
| 2067 | buf[len] = NUL; |
| 2068 | |
| 2069 | if (limit-- == 0) |
| 2070 | { |
| 2071 | vim_free(p); |
| 2072 | vim_free(remain); |
Bram Moolenaar | a6f7929 | 2022-01-04 21:30:47 +0000 | [diff] [blame] | 2073 | emsg(_(e_too_many_symbolic_links_cycle)); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2074 | rettv->vval.v_string = NULL; |
| 2075 | goto fail; |
| 2076 | } |
| 2077 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2078 | // Ensure that the result will have a trailing path separator |
| 2079 | // if the argument has one. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2080 | if (remain == NULL && has_trailing_pathsep) |
| 2081 | add_pathsep(buf); |
| 2082 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2083 | // Separate the first path component in the link value and |
| 2084 | // concatenate the remainders. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2085 | q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf); |
| 2086 | if (*q != NUL) |
| 2087 | { |
| 2088 | if (remain == NULL) |
| 2089 | remain = vim_strsave(q - 1); |
| 2090 | else |
| 2091 | { |
| 2092 | cpy = concat_str(q - 1, remain); |
| 2093 | if (cpy != NULL) |
| 2094 | { |
| 2095 | vim_free(remain); |
| 2096 | remain = cpy; |
| 2097 | } |
| 2098 | } |
| 2099 | q[-1] = NUL; |
| 2100 | } |
| 2101 | |
| 2102 | q = gettail(p); |
| 2103 | if (q > p && *q == NUL) |
| 2104 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2105 | // Ignore trailing path separator. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2106 | q[-1] = NUL; |
| 2107 | q = gettail(p); |
| 2108 | } |
| 2109 | if (q > p && !mch_isFullName(buf)) |
| 2110 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2111 | // symlink is relative to directory of argument |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2112 | cpy = alloc(STRLEN(p) + STRLEN(buf) + 1); |
| 2113 | if (cpy != NULL) |
| 2114 | { |
| 2115 | STRCPY(cpy, p); |
| 2116 | STRCPY(gettail(cpy), buf); |
| 2117 | vim_free(p); |
| 2118 | p = cpy; |
| 2119 | } |
| 2120 | } |
| 2121 | else |
| 2122 | { |
| 2123 | vim_free(p); |
| 2124 | p = vim_strsave(buf); |
| 2125 | } |
| 2126 | } |
| 2127 | |
| 2128 | if (remain == NULL) |
| 2129 | break; |
| 2130 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2131 | // Append the first path component of "remain" to "p". |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2132 | q = getnextcomp(remain + 1); |
| 2133 | len = q - remain - (*q != NUL); |
| 2134 | cpy = vim_strnsave(p, STRLEN(p) + len); |
| 2135 | if (cpy != NULL) |
| 2136 | { |
| 2137 | STRNCAT(cpy, remain, len); |
| 2138 | vim_free(p); |
| 2139 | p = cpy; |
| 2140 | } |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2141 | // Shorten "remain". |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2142 | if (*q != NUL) |
| 2143 | STRMOVE(remain, q - 1); |
| 2144 | else |
| 2145 | VIM_CLEAR(remain); |
| 2146 | } |
| 2147 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2148 | // If the result is a relative path name, make it explicitly relative to |
| 2149 | // the current directory if and only if the argument had this form. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2150 | if (!vim_ispathsep(*p)) |
| 2151 | { |
| 2152 | if (is_relative_to_current |
| 2153 | && *p != NUL |
| 2154 | && !(p[0] == '.' |
| 2155 | && (p[1] == NUL |
| 2156 | || vim_ispathsep(p[1]) |
| 2157 | || (p[1] == '.' |
| 2158 | && (p[2] == NUL |
| 2159 | || vim_ispathsep(p[2])))))) |
| 2160 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2161 | // Prepend "./". |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2162 | cpy = concat_str((char_u *)"./", p); |
| 2163 | if (cpy != NULL) |
| 2164 | { |
| 2165 | vim_free(p); |
| 2166 | p = cpy; |
| 2167 | } |
| 2168 | } |
| 2169 | else if (!is_relative_to_current) |
| 2170 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2171 | // Strip leading "./". |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2172 | q = p; |
| 2173 | while (q[0] == '.' && vim_ispathsep(q[1])) |
| 2174 | q += 2; |
| 2175 | if (q > p) |
| 2176 | STRMOVE(p, p + 2); |
| 2177 | } |
| 2178 | } |
| 2179 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2180 | // Ensure that the result will have no trailing path separator |
| 2181 | // if the argument had none. But keep "/" or "//". |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2182 | if (!has_trailing_pathsep) |
| 2183 | { |
| 2184 | q = p + STRLEN(p); |
| 2185 | if (after_pathsep(p, q)) |
| 2186 | *gettail_sep(p) = NUL; |
| 2187 | } |
| 2188 | |
| 2189 | rettv->vval.v_string = p; |
| 2190 | } |
| 2191 | # else |
| 2192 | rettv->vval.v_string = vim_strsave(p); |
| 2193 | # endif |
| 2194 | #endif |
| 2195 | |
| 2196 | simplify_filename(rettv->vval.v_string); |
| 2197 | |
| 2198 | #ifdef HAVE_READLINK |
| 2199 | fail: |
| 2200 | vim_free(buf); |
| 2201 | #endif |
| 2202 | rettv->v_type = VAR_STRING; |
| 2203 | } |
| 2204 | |
| 2205 | /* |
| 2206 | * "tempname()" function |
| 2207 | */ |
| 2208 | void |
| 2209 | f_tempname(typval_T *argvars UNUSED, typval_T *rettv) |
| 2210 | { |
| 2211 | static int x = 'A'; |
| 2212 | |
| 2213 | rettv->v_type = VAR_STRING; |
| 2214 | rettv->vval.v_string = vim_tempname(x, FALSE); |
| 2215 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2216 | // Advance 'x' to use A-Z and 0-9, so that there are at least 34 different |
| 2217 | // names. Skip 'I' and 'O', they are used for shell redirection. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2218 | do |
| 2219 | { |
| 2220 | if (x == 'Z') |
| 2221 | x = '0'; |
| 2222 | else if (x == '9') |
| 2223 | x = 'A'; |
| 2224 | else |
Bram Moolenaar | 424bcae | 2022-01-31 14:59:41 +0000 | [diff] [blame] | 2225 | ++x; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2226 | } while (x == 'I' || x == 'O'); |
| 2227 | } |
| 2228 | |
| 2229 | /* |
| 2230 | * "writefile()" function |
| 2231 | */ |
| 2232 | void |
| 2233 | f_writefile(typval_T *argvars, typval_T *rettv) |
| 2234 | { |
| 2235 | int binary = FALSE; |
| 2236 | int append = FALSE; |
| 2237 | #ifdef HAVE_FSYNC |
| 2238 | int do_fsync = p_fs; |
| 2239 | #endif |
| 2240 | char_u *fname; |
| 2241 | FILE *fd; |
| 2242 | int ret = 0; |
| 2243 | listitem_T *li; |
| 2244 | list_T *list = NULL; |
| 2245 | blob_T *blob = NULL; |
| 2246 | |
| 2247 | rettv->vval.v_number = -1; |
| 2248 | if (check_secure()) |
| 2249 | return; |
| 2250 | |
Yegappan Lakshmanan | 0ad871d | 2021-07-23 20:37:56 +0200 | [diff] [blame] | 2251 | if (in_vim9script() |
| 2252 | && (check_for_list_or_blob_arg(argvars, 0) == FAIL |
| 2253 | || check_for_string_arg(argvars, 1) == FAIL |
| 2254 | || check_for_opt_string_arg(argvars, 2) == FAIL)) |
| 2255 | return; |
| 2256 | |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2257 | if (argvars[0].v_type == VAR_LIST) |
| 2258 | { |
| 2259 | list = argvars[0].vval.v_list; |
| 2260 | if (list == NULL) |
| 2261 | return; |
Bram Moolenaar | 7e9f351 | 2020-05-13 22:44:22 +0200 | [diff] [blame] | 2262 | CHECK_LIST_MATERIALIZE(list); |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 2263 | FOR_ALL_LIST_ITEMS(list, li) |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2264 | if (tv_get_string_chk(&li->li_tv) == NULL) |
| 2265 | return; |
| 2266 | } |
| 2267 | else if (argvars[0].v_type == VAR_BLOB) |
| 2268 | { |
| 2269 | blob = argvars[0].vval.v_blob; |
| 2270 | if (blob == NULL) |
| 2271 | return; |
| 2272 | } |
| 2273 | else |
| 2274 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 2275 | semsg(_(e_invalid_argument_str), |
Bram Moolenaar | 18a2b87 | 2020-03-19 13:08:45 +0100 | [diff] [blame] | 2276 | _("writefile() first argument must be a List or a Blob")); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2277 | return; |
| 2278 | } |
| 2279 | |
| 2280 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 2281 | { |
| 2282 | char_u *arg2 = tv_get_string_chk(&argvars[2]); |
| 2283 | |
| 2284 | if (arg2 == NULL) |
| 2285 | return; |
| 2286 | if (vim_strchr(arg2, 'b') != NULL) |
| 2287 | binary = TRUE; |
| 2288 | if (vim_strchr(arg2, 'a') != NULL) |
| 2289 | append = TRUE; |
| 2290 | #ifdef HAVE_FSYNC |
| 2291 | if (vim_strchr(arg2, 's') != NULL) |
| 2292 | do_fsync = TRUE; |
| 2293 | else if (vim_strchr(arg2, 'S') != NULL) |
| 2294 | do_fsync = FALSE; |
| 2295 | #endif |
| 2296 | } |
| 2297 | |
| 2298 | fname = tv_get_string_chk(&argvars[1]); |
| 2299 | if (fname == NULL) |
| 2300 | return; |
| 2301 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2302 | // Always open the file in binary mode, library functions have a mind of |
| 2303 | // their own about CR-LF conversion. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2304 | if (*fname == NUL || (fd = mch_fopen((char *)fname, |
| 2305 | append ? APPENDBIN : WRITEBIN)) == NULL) |
| 2306 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 2307 | semsg(_(e_cant_create_file_str), *fname == NUL ? (char_u *)_("<empty>") : fname); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2308 | ret = -1; |
| 2309 | } |
| 2310 | else if (blob) |
| 2311 | { |
| 2312 | if (write_blob(fd, blob) == FAIL) |
| 2313 | ret = -1; |
| 2314 | #ifdef HAVE_FSYNC |
| 2315 | else if (do_fsync) |
| 2316 | // Ignore the error, the user wouldn't know what to do about it. |
| 2317 | // May happen for a device. |
| 2318 | vim_ignored = vim_fsync(fileno(fd)); |
| 2319 | #endif |
| 2320 | fclose(fd); |
| 2321 | } |
| 2322 | else |
| 2323 | { |
| 2324 | if (write_list(fd, list, binary) == FAIL) |
| 2325 | ret = -1; |
| 2326 | #ifdef HAVE_FSYNC |
| 2327 | else if (do_fsync) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2328 | // Ignore the error, the user wouldn't know what to do about it. |
| 2329 | // May happen for a device. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2330 | vim_ignored = vim_fsync(fileno(fd)); |
| 2331 | #endif |
| 2332 | fclose(fd); |
| 2333 | } |
| 2334 | |
| 2335 | rettv->vval.v_number = ret; |
| 2336 | } |
| 2337 | |
| 2338 | #endif // FEAT_EVAL |
| 2339 | |
| 2340 | #if defined(FEAT_BROWSE) || defined(PROTO) |
| 2341 | /* |
| 2342 | * Generic browse function. Calls gui_mch_browse() when possible. |
| 2343 | * Later this may pop-up a non-GUI file selector (external command?). |
| 2344 | */ |
| 2345 | char_u * |
| 2346 | do_browse( |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2347 | int flags, // BROWSE_SAVE and BROWSE_DIR |
| 2348 | char_u *title, // title for the window |
| 2349 | char_u *dflt, // default file name (may include directory) |
| 2350 | char_u *ext, // extension added |
| 2351 | char_u *initdir, // initial directory, NULL for current dir or |
| 2352 | // when using path from "dflt" |
| 2353 | char_u *filter, // file name filter |
| 2354 | buf_T *buf) // buffer to read/write for |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2355 | { |
| 2356 | char_u *fname; |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2357 | static char_u *last_dir = NULL; // last used directory |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2358 | char_u *tofree = NULL; |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 2359 | int save_cmod_flags = cmdmod.cmod_flags; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2360 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2361 | // Must turn off browse to avoid that autocommands will get the |
| 2362 | // flag too! |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 2363 | cmdmod.cmod_flags &= ~CMOD_BROWSE; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2364 | |
| 2365 | if (title == NULL || *title == NUL) |
| 2366 | { |
| 2367 | if (flags & BROWSE_DIR) |
| 2368 | title = (char_u *)_("Select Directory dialog"); |
| 2369 | else if (flags & BROWSE_SAVE) |
| 2370 | title = (char_u *)_("Save File dialog"); |
| 2371 | else |
| 2372 | title = (char_u *)_("Open File dialog"); |
| 2373 | } |
| 2374 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2375 | // When no directory specified, use default file name, default dir, buffer |
| 2376 | // dir, last dir or current dir |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2377 | if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL) |
| 2378 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2379 | if (mch_isdir(dflt)) // default file name is a directory |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2380 | { |
| 2381 | initdir = dflt; |
| 2382 | dflt = NULL; |
| 2383 | } |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2384 | else if (gettail(dflt) != dflt) // default file name includes a path |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2385 | { |
| 2386 | tofree = vim_strsave(dflt); |
| 2387 | if (tofree != NULL) |
| 2388 | { |
| 2389 | initdir = tofree; |
| 2390 | *gettail(initdir) = NUL; |
| 2391 | dflt = gettail(dflt); |
| 2392 | } |
| 2393 | } |
| 2394 | } |
| 2395 | |
| 2396 | if (initdir == NULL || *initdir == NUL) |
| 2397 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2398 | // When 'browsedir' is a directory, use it |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2399 | if (STRCMP(p_bsdir, "last") != 0 |
| 2400 | && STRCMP(p_bsdir, "buffer") != 0 |
| 2401 | && STRCMP(p_bsdir, "current") != 0 |
| 2402 | && mch_isdir(p_bsdir)) |
| 2403 | initdir = p_bsdir; |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2404 | // When saving or 'browsedir' is "buffer", use buffer fname |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2405 | else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b') |
| 2406 | && buf != NULL && buf->b_ffname != NULL) |
| 2407 | { |
| 2408 | if (dflt == NULL || *dflt == NUL) |
| 2409 | dflt = gettail(curbuf->b_ffname); |
| 2410 | tofree = vim_strsave(curbuf->b_ffname); |
| 2411 | if (tofree != NULL) |
| 2412 | { |
| 2413 | initdir = tofree; |
| 2414 | *gettail(initdir) = NUL; |
| 2415 | } |
| 2416 | } |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2417 | // When 'browsedir' is "last", use dir from last browse |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2418 | else if (*p_bsdir == 'l') |
| 2419 | initdir = last_dir; |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2420 | // When 'browsedir is "current", use current directory. This is the |
| 2421 | // default already, leave initdir empty. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | # ifdef FEAT_GUI |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2425 | if (gui.in_use) // when this changes, also adjust f_has()! |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2426 | { |
| 2427 | if (filter == NULL |
| 2428 | # ifdef FEAT_EVAL |
| 2429 | && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL |
| 2430 | && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL |
| 2431 | # endif |
| 2432 | ) |
| 2433 | filter = BROWSE_FILTER_DEFAULT; |
| 2434 | if (flags & BROWSE_DIR) |
| 2435 | { |
| 2436 | # if defined(FEAT_GUI_GTK) || defined(MSWIN) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2437 | // For systems that have a directory dialog. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2438 | fname = gui_mch_browsedir(title, initdir); |
| 2439 | # else |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2440 | // Generic solution for selecting a directory: select a file and |
| 2441 | // remove the file name. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2442 | fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)""); |
| 2443 | # endif |
| 2444 | # if !defined(FEAT_GUI_GTK) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2445 | // Win32 adds a dummy file name, others return an arbitrary file |
| 2446 | // name. GTK+ 2 returns only the directory, |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2447 | if (fname != NULL && *fname != NUL && !mch_isdir(fname)) |
| 2448 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2449 | // Remove the file name. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2450 | char_u *tail = gettail_sep(fname); |
| 2451 | |
| 2452 | if (tail == fname) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2453 | *tail++ = '.'; // use current dir |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2454 | *tail = NUL; |
| 2455 | } |
| 2456 | # endif |
| 2457 | } |
| 2458 | else |
| 2459 | fname = gui_mch_browse(flags & BROWSE_SAVE, |
| 2460 | title, dflt, ext, initdir, (char_u *)_(filter)); |
| 2461 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2462 | // We hang around in the dialog for a while, the user might do some |
| 2463 | // things to our files. The Win32 dialog allows deleting or renaming |
| 2464 | // a file, check timestamps. |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2465 | need_check_timestamps = TRUE; |
| 2466 | did_check_timestamps = FALSE; |
| 2467 | } |
| 2468 | else |
| 2469 | # endif |
| 2470 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2471 | // TODO: non-GUI file selector here |
Bram Moolenaar | eaaac01 | 2022-01-02 17:00:40 +0000 | [diff] [blame] | 2472 | emsg(_(e_sorry_no_file_browser_in_console_mode)); |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2473 | fname = NULL; |
| 2474 | } |
| 2475 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2476 | // keep the directory for next time |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2477 | if (fname != NULL) |
| 2478 | { |
| 2479 | vim_free(last_dir); |
| 2480 | last_dir = vim_strsave(fname); |
| 2481 | if (last_dir != NULL && !(flags & BROWSE_DIR)) |
| 2482 | { |
| 2483 | *gettail(last_dir) = NUL; |
| 2484 | if (*last_dir == NUL) |
| 2485 | { |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2486 | // filename only returned, must be in current dir |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2487 | vim_free(last_dir); |
| 2488 | last_dir = alloc(MAXPATHL); |
| 2489 | if (last_dir != NULL) |
| 2490 | mch_dirname(last_dir, MAXPATHL); |
| 2491 | } |
| 2492 | } |
| 2493 | } |
| 2494 | |
| 2495 | vim_free(tofree); |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 2496 | cmdmod.cmod_flags = save_cmod_flags; |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2497 | |
| 2498 | return fname; |
| 2499 | } |
| 2500 | #endif |
| 2501 | |
| 2502 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 2503 | |
| 2504 | /* |
| 2505 | * "browse(save, title, initdir, default)" function |
| 2506 | */ |
| 2507 | void |
| 2508 | f_browse(typval_T *argvars UNUSED, typval_T *rettv) |
| 2509 | { |
| 2510 | # ifdef FEAT_BROWSE |
| 2511 | int save; |
| 2512 | char_u *title; |
| 2513 | char_u *initdir; |
| 2514 | char_u *defname; |
| 2515 | char_u buf[NUMBUFLEN]; |
| 2516 | char_u buf2[NUMBUFLEN]; |
| 2517 | int error = FALSE; |
| 2518 | |
Bram Moolenaar | f28f2ac | 2021-03-22 22:21:26 +0100 | [diff] [blame] | 2519 | if (in_vim9script() |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 2520 | && (check_for_bool_arg(argvars, 0) == FAIL |
| 2521 | || check_for_string_arg(argvars, 1) == FAIL |
Bram Moolenaar | 32105ae | 2021-03-27 18:59:25 +0100 | [diff] [blame] | 2522 | || check_for_string_arg(argvars, 2) == FAIL |
| 2523 | || check_for_string_arg(argvars, 3) == FAIL)) |
Bram Moolenaar | f28f2ac | 2021-03-22 22:21:26 +0100 | [diff] [blame] | 2524 | return; |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 2525 | |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2526 | save = (int)tv_get_number_chk(&argvars[0], &error); |
| 2527 | title = tv_get_string_chk(&argvars[1]); |
| 2528 | initdir = tv_get_string_buf_chk(&argvars[2], buf); |
| 2529 | defname = tv_get_string_buf_chk(&argvars[3], buf2); |
| 2530 | |
| 2531 | if (error || title == NULL || initdir == NULL || defname == NULL) |
| 2532 | rettv->vval.v_string = NULL; |
| 2533 | else |
| 2534 | rettv->vval.v_string = |
| 2535 | do_browse(save ? BROWSE_SAVE : 0, |
| 2536 | title, defname, NULL, initdir, NULL, curbuf); |
| 2537 | # else |
| 2538 | rettv->vval.v_string = NULL; |
| 2539 | # endif |
| 2540 | rettv->v_type = VAR_STRING; |
| 2541 | } |
| 2542 | |
| 2543 | /* |
| 2544 | * "browsedir(title, initdir)" function |
| 2545 | */ |
| 2546 | void |
| 2547 | f_browsedir(typval_T *argvars UNUSED, typval_T *rettv) |
| 2548 | { |
| 2549 | # ifdef FEAT_BROWSE |
| 2550 | char_u *title; |
| 2551 | char_u *initdir; |
| 2552 | char_u buf[NUMBUFLEN]; |
| 2553 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 2554 | if (in_vim9script() |
| 2555 | && (check_for_string_arg(argvars, 0) == FAIL |
| 2556 | || check_for_string_arg(argvars, 1) == FAIL)) |
| 2557 | return; |
| 2558 | |
Bram Moolenaar | b005cd8 | 2019-09-04 15:54:55 +0200 | [diff] [blame] | 2559 | title = tv_get_string_chk(&argvars[0]); |
| 2560 | initdir = tv_get_string_buf_chk(&argvars[1], buf); |
| 2561 | |
| 2562 | if (title == NULL || initdir == NULL) |
| 2563 | rettv->vval.v_string = NULL; |
| 2564 | else |
| 2565 | rettv->vval.v_string = do_browse(BROWSE_DIR, |
| 2566 | title, NULL, NULL, initdir, NULL, curbuf); |
| 2567 | # else |
| 2568 | rettv->vval.v_string = NULL; |
| 2569 | # endif |
| 2570 | rettv->v_type = VAR_STRING; |
| 2571 | } |
| 2572 | |
| 2573 | #endif // FEAT_EVAL |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2574 | |
| 2575 | /* |
| 2576 | * Replace home directory by "~" in each space or comma separated file name in |
| 2577 | * 'src'. |
| 2578 | * If anything fails (except when out of space) dst equals src. |
| 2579 | */ |
| 2580 | void |
| 2581 | home_replace( |
| 2582 | buf_T *buf, // when not NULL, check for help files |
| 2583 | char_u *src, // input file name |
| 2584 | char_u *dst, // where to put the result |
| 2585 | int dstlen, // maximum length of the result |
| 2586 | int one) // if TRUE, only replace one file name, include |
| 2587 | // spaces and commas in the file name. |
| 2588 | { |
| 2589 | size_t dirlen = 0, envlen = 0; |
| 2590 | size_t len; |
| 2591 | char_u *homedir_env, *homedir_env_orig; |
| 2592 | char_u *p; |
| 2593 | |
| 2594 | if (src == NULL) |
| 2595 | { |
| 2596 | *dst = NUL; |
| 2597 | return; |
| 2598 | } |
| 2599 | |
| 2600 | /* |
| 2601 | * If the file is a help file, remove the path completely. |
| 2602 | */ |
| 2603 | if (buf != NULL && buf->b_help) |
| 2604 | { |
| 2605 | vim_snprintf((char *)dst, dstlen, "%s", gettail(src)); |
| 2606 | return; |
| 2607 | } |
| 2608 | |
| 2609 | /* |
| 2610 | * We check both the value of the $HOME environment variable and the |
| 2611 | * "real" home directory. |
| 2612 | */ |
| 2613 | if (homedir != NULL) |
| 2614 | dirlen = STRLEN(homedir); |
| 2615 | |
| 2616 | #ifdef VMS |
| 2617 | homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN"); |
| 2618 | #else |
| 2619 | homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME"); |
| 2620 | #endif |
| 2621 | #ifdef MSWIN |
| 2622 | if (homedir_env == NULL) |
| 2623 | homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE"); |
| 2624 | #endif |
| 2625 | // Empty is the same as not set. |
| 2626 | if (homedir_env != NULL && *homedir_env == NUL) |
| 2627 | homedir_env = NULL; |
| 2628 | |
| 2629 | if (homedir_env != NULL && *homedir_env == '~') |
| 2630 | { |
| 2631 | int usedlen = 0; |
| 2632 | int flen; |
| 2633 | char_u *fbuf = NULL; |
| 2634 | |
| 2635 | flen = (int)STRLEN(homedir_env); |
| 2636 | (void)modify_fname((char_u *)":p", FALSE, &usedlen, |
| 2637 | &homedir_env, &fbuf, &flen); |
| 2638 | flen = (int)STRLEN(homedir_env); |
| 2639 | if (flen > 0 && vim_ispathsep(homedir_env[flen - 1])) |
| 2640 | // Remove the trailing / that is added to a directory. |
| 2641 | homedir_env[flen - 1] = NUL; |
| 2642 | } |
| 2643 | |
| 2644 | if (homedir_env != NULL) |
| 2645 | envlen = STRLEN(homedir_env); |
| 2646 | |
| 2647 | if (!one) |
| 2648 | src = skipwhite(src); |
| 2649 | while (*src && dstlen > 0) |
| 2650 | { |
| 2651 | /* |
| 2652 | * Here we are at the beginning of a file name. |
| 2653 | * First, check to see if the beginning of the file name matches |
| 2654 | * $HOME or the "real" home directory. Check that there is a '/' |
| 2655 | * after the match (so that if e.g. the file is "/home/pieter/bla", |
| 2656 | * and the home directory is "/home/piet", the file does not end up |
| 2657 | * as "~er/bla" (which would seem to indicate the file "bla" in user |
| 2658 | * er's home directory)). |
| 2659 | */ |
| 2660 | p = homedir; |
| 2661 | len = dirlen; |
| 2662 | for (;;) |
| 2663 | { |
| 2664 | if ( len |
| 2665 | && fnamencmp(src, p, len) == 0 |
| 2666 | && (vim_ispathsep(src[len]) |
| 2667 | || (!one && (src[len] == ',' || src[len] == ' ')) |
| 2668 | || src[len] == NUL)) |
| 2669 | { |
| 2670 | src += len; |
| 2671 | if (--dstlen > 0) |
| 2672 | *dst++ = '~'; |
| 2673 | |
Bram Moolenaar | 0e390f4 | 2020-06-10 13:12:28 +0200 | [diff] [blame] | 2674 | // Do not add directory separator into dst, because dst is |
| 2675 | // expected to just return the directory name without the |
| 2676 | // directory separator '/'. |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2677 | break; |
| 2678 | } |
| 2679 | if (p == homedir_env) |
| 2680 | break; |
| 2681 | p = homedir_env; |
| 2682 | len = envlen; |
| 2683 | } |
| 2684 | |
| 2685 | // if (!one) skip to separator: space or comma |
| 2686 | while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0) |
| 2687 | *dst++ = *src++; |
| 2688 | // skip separator |
| 2689 | while ((*src == ' ' || *src == ',') && --dstlen > 0) |
| 2690 | *dst++ = *src++; |
| 2691 | } |
| 2692 | // if (dstlen == 0) out of space, what to do??? |
| 2693 | |
| 2694 | *dst = NUL; |
| 2695 | |
| 2696 | if (homedir_env != homedir_env_orig) |
| 2697 | vim_free(homedir_env); |
| 2698 | } |
| 2699 | |
| 2700 | /* |
| 2701 | * Like home_replace, store the replaced string in allocated memory. |
| 2702 | * When something fails, NULL is returned. |
| 2703 | */ |
| 2704 | char_u * |
| 2705 | home_replace_save( |
| 2706 | buf_T *buf, // when not NULL, check for help files |
| 2707 | char_u *src) // input file name |
| 2708 | { |
| 2709 | char_u *dst; |
| 2710 | unsigned len; |
| 2711 | |
| 2712 | len = 3; // space for "~/" and trailing NUL |
| 2713 | if (src != NULL) // just in case |
| 2714 | len += (unsigned)STRLEN(src); |
| 2715 | dst = alloc(len); |
| 2716 | if (dst != NULL) |
| 2717 | home_replace(buf, src, dst, len, TRUE); |
| 2718 | return dst; |
| 2719 | } |
| 2720 | |
| 2721 | /* |
| 2722 | * Compare two file names and return: |
| 2723 | * FPC_SAME if they both exist and are the same file. |
| 2724 | * FPC_SAMEX if they both don't exist and have the same file name. |
| 2725 | * FPC_DIFF if they both exist and are different files. |
| 2726 | * FPC_NOTX if they both don't exist. |
| 2727 | * FPC_DIFFX if one of them doesn't exist. |
| 2728 | * For the first name environment variables are expanded if "expandenv" is |
| 2729 | * TRUE. |
| 2730 | */ |
| 2731 | int |
| 2732 | fullpathcmp( |
| 2733 | char_u *s1, |
| 2734 | char_u *s2, |
| 2735 | int checkname, // when both don't exist, check file names |
| 2736 | int expandenv) |
| 2737 | { |
| 2738 | #ifdef UNIX |
| 2739 | char_u exp1[MAXPATHL]; |
| 2740 | char_u full1[MAXPATHL]; |
| 2741 | char_u full2[MAXPATHL]; |
| 2742 | stat_T st1, st2; |
| 2743 | int r1, r2; |
| 2744 | |
| 2745 | if (expandenv) |
| 2746 | expand_env(s1, exp1, MAXPATHL); |
| 2747 | else |
| 2748 | vim_strncpy(exp1, s1, MAXPATHL - 1); |
| 2749 | r1 = mch_stat((char *)exp1, &st1); |
| 2750 | r2 = mch_stat((char *)s2, &st2); |
| 2751 | if (r1 != 0 && r2 != 0) |
| 2752 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2753 | // if mch_stat() doesn't work, may compare the names |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2754 | if (checkname) |
| 2755 | { |
| 2756 | if (fnamecmp(exp1, s2) == 0) |
| 2757 | return FPC_SAMEX; |
| 2758 | r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); |
| 2759 | r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); |
| 2760 | if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0) |
| 2761 | return FPC_SAMEX; |
| 2762 | } |
| 2763 | return FPC_NOTX; |
| 2764 | } |
| 2765 | if (r1 != 0 || r2 != 0) |
| 2766 | return FPC_DIFFX; |
| 2767 | if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) |
| 2768 | return FPC_SAME; |
| 2769 | return FPC_DIFF; |
| 2770 | #else |
| 2771 | char_u *exp1; // expanded s1 |
| 2772 | char_u *full1; // full path of s1 |
| 2773 | char_u *full2; // full path of s2 |
| 2774 | int retval = FPC_DIFF; |
| 2775 | int r1, r2; |
| 2776 | |
| 2777 | // allocate one buffer to store three paths (alloc()/free() is slow!) |
| 2778 | if ((exp1 = alloc(MAXPATHL * 3)) != NULL) |
| 2779 | { |
| 2780 | full1 = exp1 + MAXPATHL; |
| 2781 | full2 = full1 + MAXPATHL; |
| 2782 | |
| 2783 | if (expandenv) |
| 2784 | expand_env(s1, exp1, MAXPATHL); |
| 2785 | else |
| 2786 | vim_strncpy(exp1, s1, MAXPATHL - 1); |
| 2787 | r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); |
| 2788 | r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); |
| 2789 | |
| 2790 | // If vim_FullName() fails, the file probably doesn't exist. |
| 2791 | if (r1 != OK && r2 != OK) |
| 2792 | { |
| 2793 | if (checkname && fnamecmp(exp1, s2) == 0) |
| 2794 | retval = FPC_SAMEX; |
| 2795 | else |
| 2796 | retval = FPC_NOTX; |
| 2797 | } |
| 2798 | else if (r1 != OK || r2 != OK) |
| 2799 | retval = FPC_DIFFX; |
| 2800 | else if (fnamecmp(full1, full2)) |
| 2801 | retval = FPC_DIFF; |
| 2802 | else |
| 2803 | retval = FPC_SAME; |
| 2804 | vim_free(exp1); |
| 2805 | } |
| 2806 | return retval; |
| 2807 | #endif |
| 2808 | } |
| 2809 | |
| 2810 | /* |
| 2811 | * Get the tail of a path: the file name. |
| 2812 | * When the path ends in a path separator the tail is the NUL after it. |
| 2813 | * Fail safe: never returns NULL. |
| 2814 | */ |
| 2815 | char_u * |
| 2816 | gettail(char_u *fname) |
| 2817 | { |
| 2818 | char_u *p1, *p2; |
| 2819 | |
| 2820 | if (fname == NULL) |
| 2821 | return (char_u *)""; |
| 2822 | for (p1 = p2 = get_past_head(fname); *p2; ) // find last part of path |
| 2823 | { |
| 2824 | if (vim_ispathsep_nocolon(*p2)) |
| 2825 | p1 = p2 + 1; |
| 2826 | MB_PTR_ADV(p2); |
| 2827 | } |
| 2828 | return p1; |
| 2829 | } |
| 2830 | |
| 2831 | /* |
| 2832 | * Get pointer to tail of "fname", including path separators. Putting a NUL |
| 2833 | * here leaves the directory name. Takes care of "c:/" and "//". |
| 2834 | * Always returns a valid pointer. |
| 2835 | */ |
| 2836 | char_u * |
| 2837 | gettail_sep(char_u *fname) |
| 2838 | { |
| 2839 | char_u *p; |
| 2840 | char_u *t; |
| 2841 | |
| 2842 | p = get_past_head(fname); // don't remove the '/' from "c:/file" |
| 2843 | t = gettail(fname); |
| 2844 | while (t > p && after_pathsep(fname, t)) |
| 2845 | --t; |
| 2846 | #ifdef VMS |
| 2847 | // path separator is part of the path |
| 2848 | ++t; |
| 2849 | #endif |
| 2850 | return t; |
| 2851 | } |
| 2852 | |
| 2853 | /* |
| 2854 | * get the next path component (just after the next path separator). |
| 2855 | */ |
| 2856 | char_u * |
| 2857 | getnextcomp(char_u *fname) |
| 2858 | { |
| 2859 | while (*fname && !vim_ispathsep(*fname)) |
| 2860 | MB_PTR_ADV(fname); |
| 2861 | if (*fname) |
| 2862 | ++fname; |
| 2863 | return fname; |
| 2864 | } |
| 2865 | |
| 2866 | /* |
| 2867 | * Get a pointer to one character past the head of a path name. |
| 2868 | * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head. |
| 2869 | * If there is no head, path is returned. |
| 2870 | */ |
| 2871 | char_u * |
| 2872 | get_past_head(char_u *path) |
| 2873 | { |
| 2874 | char_u *retval; |
| 2875 | |
| 2876 | #if defined(MSWIN) |
| 2877 | // may skip "c:" |
| 2878 | if (isalpha(path[0]) && path[1] == ':') |
| 2879 | retval = path + 2; |
| 2880 | else |
| 2881 | retval = path; |
| 2882 | #else |
| 2883 | # if defined(AMIGA) |
| 2884 | // may skip "label:" |
| 2885 | retval = vim_strchr(path, ':'); |
| 2886 | if (retval == NULL) |
| 2887 | retval = path; |
| 2888 | # else // Unix |
| 2889 | retval = path; |
| 2890 | # endif |
| 2891 | #endif |
| 2892 | |
| 2893 | while (vim_ispathsep(*retval)) |
| 2894 | ++retval; |
| 2895 | |
| 2896 | return retval; |
| 2897 | } |
| 2898 | |
| 2899 | /* |
| 2900 | * Return TRUE if 'c' is a path separator. |
| 2901 | * Note that for MS-Windows this includes the colon. |
| 2902 | */ |
| 2903 | int |
| 2904 | vim_ispathsep(int c) |
| 2905 | { |
| 2906 | #ifdef UNIX |
| 2907 | return (c == '/'); // UNIX has ':' inside file names |
| 2908 | #else |
| 2909 | # ifdef BACKSLASH_IN_FILENAME |
| 2910 | return (c == ':' || c == '/' || c == '\\'); |
| 2911 | # else |
| 2912 | # ifdef VMS |
| 2913 | // server"user passwd"::device:[full.path.name]fname.extension;version" |
| 2914 | return (c == ':' || c == '[' || c == ']' || c == '/' |
| 2915 | || c == '<' || c == '>' || c == '"' ); |
| 2916 | # else |
| 2917 | return (c == ':' || c == '/'); |
| 2918 | # endif // VMS |
| 2919 | # endif |
| 2920 | #endif |
| 2921 | } |
| 2922 | |
| 2923 | /* |
| 2924 | * Like vim_ispathsep(c), but exclude the colon for MS-Windows. |
| 2925 | */ |
| 2926 | int |
| 2927 | vim_ispathsep_nocolon(int c) |
| 2928 | { |
| 2929 | return vim_ispathsep(c) |
| 2930 | #ifdef BACKSLASH_IN_FILENAME |
| 2931 | && c != ':' |
| 2932 | #endif |
| 2933 | ; |
| 2934 | } |
| 2935 | |
| 2936 | /* |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2937 | * Return TRUE if the directory of "fname" exists, FALSE otherwise. |
| 2938 | * Also returns TRUE if there is no directory name. |
| 2939 | * "fname" must be writable!. |
| 2940 | */ |
| 2941 | int |
| 2942 | dir_of_file_exists(char_u *fname) |
| 2943 | { |
| 2944 | char_u *p; |
| 2945 | int c; |
| 2946 | int retval; |
| 2947 | |
| 2948 | p = gettail_sep(fname); |
| 2949 | if (p == fname) |
| 2950 | return TRUE; |
| 2951 | c = *p; |
| 2952 | *p = NUL; |
| 2953 | retval = mch_isdir(fname); |
| 2954 | *p = c; |
| 2955 | return retval; |
| 2956 | } |
| 2957 | |
| 2958 | /* |
| 2959 | * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally |
| 2960 | * and deal with 'fileignorecase'. |
| 2961 | */ |
| 2962 | int |
| 2963 | vim_fnamecmp(char_u *x, char_u *y) |
| 2964 | { |
| 2965 | #ifdef BACKSLASH_IN_FILENAME |
| 2966 | return vim_fnamencmp(x, y, MAXPATHL); |
| 2967 | #else |
| 2968 | if (p_fic) |
| 2969 | return MB_STRICMP(x, y); |
| 2970 | return STRCMP(x, y); |
| 2971 | #endif |
| 2972 | } |
| 2973 | |
| 2974 | int |
| 2975 | vim_fnamencmp(char_u *x, char_u *y, size_t len) |
| 2976 | { |
| 2977 | #ifdef BACKSLASH_IN_FILENAME |
| 2978 | char_u *px = x; |
| 2979 | char_u *py = y; |
| 2980 | int cx = NUL; |
| 2981 | int cy = NUL; |
| 2982 | |
| 2983 | while (len > 0) |
| 2984 | { |
| 2985 | cx = PTR2CHAR(px); |
| 2986 | cy = PTR2CHAR(py); |
| 2987 | if (cx == NUL || cy == NUL |
| 2988 | || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy) |
| 2989 | && !(cx == '/' && cy == '\\') |
| 2990 | && !(cx == '\\' && cy == '/'))) |
| 2991 | break; |
Bram Moolenaar | 1614a14 | 2019-10-06 22:00:13 +0200 | [diff] [blame] | 2992 | len -= mb_ptr2len(px); |
| 2993 | px += mb_ptr2len(px); |
| 2994 | py += mb_ptr2len(py); |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 2995 | } |
| 2996 | if (len == 0) |
| 2997 | return 0; |
| 2998 | return (cx - cy); |
| 2999 | #else |
| 3000 | if (p_fic) |
| 3001 | return MB_STRNICMP(x, y, len); |
| 3002 | return STRNCMP(x, y, len); |
| 3003 | #endif |
| 3004 | } |
| 3005 | |
| 3006 | /* |
| 3007 | * Concatenate file names fname1 and fname2 into allocated memory. |
| 3008 | * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary. |
| 3009 | */ |
| 3010 | char_u * |
| 3011 | concat_fnames(char_u *fname1, char_u *fname2, int sep) |
| 3012 | { |
| 3013 | char_u *dest; |
| 3014 | |
| 3015 | dest = alloc(STRLEN(fname1) + STRLEN(fname2) + 3); |
| 3016 | if (dest != NULL) |
| 3017 | { |
| 3018 | STRCPY(dest, fname1); |
| 3019 | if (sep) |
| 3020 | add_pathsep(dest); |
| 3021 | STRCAT(dest, fname2); |
| 3022 | } |
| 3023 | return dest; |
| 3024 | } |
| 3025 | |
| 3026 | /* |
| 3027 | * Add a path separator to a file name, unless it already ends in a path |
| 3028 | * separator. |
| 3029 | */ |
| 3030 | void |
| 3031 | add_pathsep(char_u *p) |
| 3032 | { |
| 3033 | if (*p != NUL && !after_pathsep(p, p + STRLEN(p))) |
| 3034 | STRCAT(p, PATHSEPSTR); |
| 3035 | } |
| 3036 | |
| 3037 | /* |
| 3038 | * FullName_save - Make an allocated copy of a full file name. |
| 3039 | * Returns NULL when out of memory. |
| 3040 | */ |
| 3041 | char_u * |
| 3042 | FullName_save( |
| 3043 | char_u *fname, |
| 3044 | int force) // force expansion, even when it already looks |
| 3045 | // like a full path name |
| 3046 | { |
| 3047 | char_u *buf; |
| 3048 | char_u *new_fname = NULL; |
| 3049 | |
| 3050 | if (fname == NULL) |
| 3051 | return NULL; |
| 3052 | |
| 3053 | buf = alloc(MAXPATHL); |
| 3054 | if (buf != NULL) |
| 3055 | { |
| 3056 | if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL) |
| 3057 | new_fname = vim_strsave(buf); |
| 3058 | else |
| 3059 | new_fname = vim_strsave(fname); |
| 3060 | vim_free(buf); |
| 3061 | } |
| 3062 | return new_fname; |
| 3063 | } |
| 3064 | |
| 3065 | /* |
| 3066 | * return TRUE if "fname" exists. |
| 3067 | */ |
| 3068 | int |
| 3069 | vim_fexists(char_u *fname) |
| 3070 | { |
| 3071 | stat_T st; |
| 3072 | |
| 3073 | if (mch_stat((char *)fname, &st)) |
| 3074 | return FALSE; |
| 3075 | return TRUE; |
| 3076 | } |
| 3077 | |
| 3078 | /* |
| 3079 | * Invoke expand_wildcards() for one pattern. |
| 3080 | * Expand items like "%:h" before the expansion. |
| 3081 | * Returns OK or FAIL. |
| 3082 | */ |
| 3083 | int |
| 3084 | expand_wildcards_eval( |
| 3085 | char_u **pat, // pointer to input pattern |
| 3086 | int *num_file, // resulting number of files |
| 3087 | char_u ***file, // array of resulting files |
| 3088 | int flags) // EW_DIR, etc. |
| 3089 | { |
| 3090 | int ret = FAIL; |
| 3091 | char_u *eval_pat = NULL; |
| 3092 | char_u *exp_pat = *pat; |
| 3093 | char *ignored_msg; |
| 3094 | int usedlen; |
| 3095 | |
| 3096 | if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<') |
| 3097 | { |
| 3098 | ++emsg_off; |
| 3099 | eval_pat = eval_vars(exp_pat, exp_pat, &usedlen, |
Bram Moolenaar | a96edb7 | 2022-04-28 17:52:24 +0100 | [diff] [blame] | 3100 | NULL, &ignored_msg, NULL, TRUE); |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3101 | --emsg_off; |
| 3102 | if (eval_pat != NULL) |
| 3103 | exp_pat = concat_str(eval_pat, exp_pat + usedlen); |
| 3104 | } |
| 3105 | |
| 3106 | if (exp_pat != NULL) |
| 3107 | ret = expand_wildcards(1, &exp_pat, num_file, file, flags); |
| 3108 | |
| 3109 | if (eval_pat != NULL) |
| 3110 | { |
| 3111 | vim_free(exp_pat); |
| 3112 | vim_free(eval_pat); |
| 3113 | } |
| 3114 | |
| 3115 | return ret; |
| 3116 | } |
| 3117 | |
| 3118 | /* |
| 3119 | * Expand wildcards. Calls gen_expand_wildcards() and removes files matching |
| 3120 | * 'wildignore'. |
| 3121 | * Returns OK or FAIL. When FAIL then "num_files" won't be set. |
| 3122 | */ |
| 3123 | int |
| 3124 | expand_wildcards( |
| 3125 | int num_pat, // number of input patterns |
| 3126 | char_u **pat, // array of input patterns |
| 3127 | int *num_files, // resulting number of files |
| 3128 | char_u ***files, // array of resulting files |
| 3129 | int flags) // EW_DIR, etc. |
| 3130 | { |
| 3131 | int retval; |
| 3132 | int i, j; |
| 3133 | char_u *p; |
| 3134 | int non_suf_match; // number without matching suffix |
| 3135 | |
| 3136 | retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags); |
| 3137 | |
| 3138 | // When keeping all matches, return here |
| 3139 | if ((flags & EW_KEEPALL) || retval == FAIL) |
| 3140 | return retval; |
| 3141 | |
| 3142 | #ifdef FEAT_WILDIGN |
| 3143 | /* |
| 3144 | * Remove names that match 'wildignore'. |
| 3145 | */ |
| 3146 | if (*p_wig) |
| 3147 | { |
| 3148 | char_u *ffname; |
| 3149 | |
| 3150 | // check all files in (*files)[] |
| 3151 | for (i = 0; i < *num_files; ++i) |
| 3152 | { |
| 3153 | ffname = FullName_save((*files)[i], FALSE); |
| 3154 | if (ffname == NULL) // out of memory |
| 3155 | break; |
| 3156 | # ifdef VMS |
| 3157 | vms_remove_version(ffname); |
| 3158 | # endif |
| 3159 | if (match_file_list(p_wig, (*files)[i], ffname)) |
| 3160 | { |
| 3161 | // remove this matching file from the list |
| 3162 | vim_free((*files)[i]); |
| 3163 | for (j = i; j + 1 < *num_files; ++j) |
| 3164 | (*files)[j] = (*files)[j + 1]; |
| 3165 | --*num_files; |
| 3166 | --i; |
| 3167 | } |
| 3168 | vim_free(ffname); |
| 3169 | } |
| 3170 | |
| 3171 | // If the number of matches is now zero, we fail. |
| 3172 | if (*num_files == 0) |
| 3173 | { |
| 3174 | VIM_CLEAR(*files); |
| 3175 | return FAIL; |
| 3176 | } |
| 3177 | } |
| 3178 | #endif |
| 3179 | |
| 3180 | /* |
| 3181 | * Move the names where 'suffixes' match to the end. |
| 3182 | */ |
| 3183 | if (*num_files > 1) |
| 3184 | { |
| 3185 | non_suf_match = 0; |
| 3186 | for (i = 0; i < *num_files; ++i) |
| 3187 | { |
| 3188 | if (!match_suffix((*files)[i])) |
| 3189 | { |
| 3190 | /* |
| 3191 | * Move the name without matching suffix to the front |
| 3192 | * of the list. |
| 3193 | */ |
| 3194 | p = (*files)[i]; |
| 3195 | for (j = i; j > non_suf_match; --j) |
| 3196 | (*files)[j] = (*files)[j - 1]; |
| 3197 | (*files)[non_suf_match++] = p; |
| 3198 | } |
| 3199 | } |
| 3200 | } |
| 3201 | |
| 3202 | return retval; |
| 3203 | } |
| 3204 | |
| 3205 | /* |
| 3206 | * Return TRUE if "fname" matches with an entry in 'suffixes'. |
| 3207 | */ |
| 3208 | int |
| 3209 | match_suffix(char_u *fname) |
| 3210 | { |
| 3211 | int fnamelen, setsuflen; |
| 3212 | char_u *setsuf; |
| 3213 | #define MAXSUFLEN 30 // maximum length of a file suffix |
| 3214 | char_u suf_buf[MAXSUFLEN]; |
| 3215 | |
| 3216 | fnamelen = (int)STRLEN(fname); |
| 3217 | setsuflen = 0; |
| 3218 | for (setsuf = p_su; *setsuf; ) |
| 3219 | { |
| 3220 | setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,"); |
| 3221 | if (setsuflen == 0) |
| 3222 | { |
| 3223 | char_u *tail = gettail(fname); |
| 3224 | |
| 3225 | // empty entry: match name without a '.' |
| 3226 | if (vim_strchr(tail, '.') == NULL) |
| 3227 | { |
| 3228 | setsuflen = 1; |
| 3229 | break; |
| 3230 | } |
| 3231 | } |
| 3232 | else |
| 3233 | { |
| 3234 | if (fnamelen >= setsuflen |
| 3235 | && fnamencmp(suf_buf, fname + fnamelen - setsuflen, |
| 3236 | (size_t)setsuflen) == 0) |
| 3237 | break; |
| 3238 | setsuflen = 0; |
| 3239 | } |
| 3240 | } |
| 3241 | return (setsuflen != 0); |
| 3242 | } |
| 3243 | |
| 3244 | #ifdef VIM_BACKTICK |
| 3245 | |
| 3246 | /* |
| 3247 | * Return TRUE if we can expand this backtick thing here. |
| 3248 | */ |
| 3249 | static int |
| 3250 | vim_backtick(char_u *p) |
| 3251 | { |
| 3252 | return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`'); |
| 3253 | } |
| 3254 | |
| 3255 | /* |
| 3256 | * Expand an item in `backticks` by executing it as a command. |
| 3257 | * Currently only works when pat[] starts and ends with a `. |
| 3258 | * Returns number of file names found, -1 if an error is encountered. |
| 3259 | */ |
| 3260 | static int |
| 3261 | expand_backtick( |
| 3262 | garray_T *gap, |
| 3263 | char_u *pat, |
| 3264 | int flags) // EW_* flags |
| 3265 | { |
| 3266 | char_u *p; |
| 3267 | char_u *cmd; |
| 3268 | char_u *buffer; |
| 3269 | int cnt = 0; |
| 3270 | int i; |
| 3271 | |
| 3272 | // Create the command: lop off the backticks. |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 3273 | cmd = vim_strnsave(pat + 1, STRLEN(pat) - 2); |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3274 | if (cmd == NULL) |
| 3275 | return -1; |
| 3276 | |
| 3277 | #ifdef FEAT_EVAL |
| 3278 | if (*cmd == '=') // `={expr}`: Expand expression |
Bram Moolenaar | b171fb1 | 2020-06-24 20:34:03 +0200 | [diff] [blame] | 3279 | buffer = eval_to_string(cmd + 1, TRUE); |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3280 | else |
| 3281 | #endif |
| 3282 | buffer = get_cmd_output(cmd, NULL, |
| 3283 | (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL); |
| 3284 | vim_free(cmd); |
| 3285 | if (buffer == NULL) |
| 3286 | return -1; |
| 3287 | |
| 3288 | cmd = buffer; |
| 3289 | while (*cmd != NUL) |
| 3290 | { |
| 3291 | cmd = skipwhite(cmd); // skip over white space |
| 3292 | p = cmd; |
| 3293 | while (*p != NUL && *p != '\r' && *p != '\n') // skip over entry |
| 3294 | ++p; |
| 3295 | // add an entry if it is not empty |
| 3296 | if (p > cmd) |
| 3297 | { |
| 3298 | i = *p; |
| 3299 | *p = NUL; |
| 3300 | addfile(gap, cmd, flags); |
| 3301 | *p = i; |
| 3302 | ++cnt; |
| 3303 | } |
| 3304 | cmd = p; |
| 3305 | while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n')) |
| 3306 | ++cmd; |
| 3307 | } |
| 3308 | |
| 3309 | vim_free(buffer); |
| 3310 | return cnt; |
| 3311 | } |
| 3312 | #endif // VIM_BACKTICK |
| 3313 | |
| 3314 | #if defined(MSWIN) |
| 3315 | /* |
| 3316 | * File name expansion code for MS-DOS, Win16 and Win32. It's here because |
| 3317 | * it's shared between these systems. |
| 3318 | */ |
| 3319 | |
| 3320 | /* |
| 3321 | * comparison function for qsort in dos_expandpath() |
| 3322 | */ |
| 3323 | static int |
| 3324 | pstrcmp(const void *a, const void *b) |
| 3325 | { |
| 3326 | return (pathcmp(*(char **)a, *(char **)b, -1)); |
| 3327 | } |
| 3328 | |
| 3329 | /* |
| 3330 | * Recursively expand one path component into all matching files and/or |
| 3331 | * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. |
| 3332 | * Return the number of matches found. |
| 3333 | * "path" has backslashes before chars that are not to be expanded, starting |
| 3334 | * at "path[wildoff]". |
| 3335 | * Return the number of matches found. |
| 3336 | * NOTE: much of this is identical to unix_expandpath(), keep in sync! |
| 3337 | */ |
| 3338 | static int |
| 3339 | dos_expandpath( |
| 3340 | garray_T *gap, |
| 3341 | char_u *path, |
| 3342 | int wildoff, |
| 3343 | int flags, // EW_* flags |
| 3344 | int didstar) // expanded "**" once already |
| 3345 | { |
| 3346 | char_u *buf; |
| 3347 | char_u *path_end; |
| 3348 | char_u *p, *s, *e; |
| 3349 | int start_len = gap->ga_len; |
| 3350 | char_u *pat; |
| 3351 | regmatch_T regmatch; |
| 3352 | int starts_with_dot; |
| 3353 | int matches; |
| 3354 | int len; |
| 3355 | int starstar = FALSE; |
| 3356 | static int stardepth = 0; // depth for "**" expansion |
| 3357 | HANDLE hFind = INVALID_HANDLE_VALUE; |
| 3358 | WIN32_FIND_DATAW wfb; |
| 3359 | WCHAR *wn = NULL; // UCS-2 name, NULL when not used. |
| 3360 | char_u *matchname; |
| 3361 | int ok; |
Bram Moolenaar | c74fbfe | 2020-04-06 22:56:28 +0200 | [diff] [blame] | 3362 | char_u *p_alt; |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3363 | |
| 3364 | // Expanding "**" may take a long time, check for CTRL-C. |
| 3365 | if (stardepth > 0) |
| 3366 | { |
| 3367 | ui_breakcheck(); |
| 3368 | if (got_int) |
| 3369 | return 0; |
| 3370 | } |
| 3371 | |
| 3372 | // Make room for file name. When doing encoding conversion the actual |
| 3373 | // length may be quite a bit longer, thus use the maximum possible length. |
| 3374 | buf = alloc(MAXPATHL); |
| 3375 | if (buf == NULL) |
| 3376 | return 0; |
| 3377 | |
| 3378 | /* |
| 3379 | * Find the first part in the path name that contains a wildcard or a ~1. |
| 3380 | * Copy it into buf, including the preceding characters. |
| 3381 | */ |
| 3382 | p = buf; |
| 3383 | s = buf; |
| 3384 | e = NULL; |
| 3385 | path_end = path; |
| 3386 | while (*path_end != NUL) |
| 3387 | { |
| 3388 | // May ignore a wildcard that has a backslash before it; it will |
| 3389 | // be removed by rem_backslash() or file_pat_to_reg_pat() below. |
| 3390 | if (path_end >= path + wildoff && rem_backslash(path_end)) |
| 3391 | *p++ = *path_end++; |
| 3392 | else if (*path_end == '\\' || *path_end == ':' || *path_end == '/') |
| 3393 | { |
| 3394 | if (e != NULL) |
| 3395 | break; |
| 3396 | s = p + 1; |
| 3397 | } |
| 3398 | else if (path_end >= path + wildoff |
| 3399 | && vim_strchr((char_u *)"*?[~", *path_end) != NULL) |
| 3400 | e = p; |
| 3401 | if (has_mbyte) |
| 3402 | { |
| 3403 | len = (*mb_ptr2len)(path_end); |
| 3404 | STRNCPY(p, path_end, len); |
| 3405 | p += len; |
| 3406 | path_end += len; |
| 3407 | } |
| 3408 | else |
| 3409 | *p++ = *path_end++; |
| 3410 | } |
| 3411 | e = p; |
| 3412 | *e = NUL; |
| 3413 | |
| 3414 | // now we have one wildcard component between s and e |
| 3415 | // Remove backslashes between "wildoff" and the start of the wildcard |
| 3416 | // component. |
| 3417 | for (p = buf + wildoff; p < s; ++p) |
| 3418 | if (rem_backslash(p)) |
| 3419 | { |
| 3420 | STRMOVE(p, p + 1); |
| 3421 | --e; |
| 3422 | --s; |
| 3423 | } |
| 3424 | |
| 3425 | // Check for "**" between "s" and "e". |
| 3426 | for (p = s; p < e; ++p) |
| 3427 | if (p[0] == '*' && p[1] == '*') |
| 3428 | starstar = TRUE; |
| 3429 | |
| 3430 | starts_with_dot = *s == '.'; |
| 3431 | pat = file_pat_to_reg_pat(s, e, NULL, FALSE); |
| 3432 | if (pat == NULL) |
| 3433 | { |
| 3434 | vim_free(buf); |
| 3435 | return 0; |
| 3436 | } |
| 3437 | |
| 3438 | // compile the regexp into a program |
| 3439 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
| 3440 | ++emsg_silent; |
| 3441 | regmatch.rm_ic = TRUE; // Always ignore case |
| 3442 | regmatch.regprog = vim_regcomp(pat, RE_MAGIC); |
| 3443 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
| 3444 | --emsg_silent; |
| 3445 | vim_free(pat); |
| 3446 | |
| 3447 | if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) |
| 3448 | { |
| 3449 | vim_free(buf); |
| 3450 | return 0; |
| 3451 | } |
| 3452 | |
| 3453 | // remember the pattern or file name being looked for |
| 3454 | matchname = vim_strsave(s); |
| 3455 | |
| 3456 | // If "**" is by itself, this is the first time we encounter it and more |
| 3457 | // is following then find matches without any directory. |
| 3458 | if (!didstar && stardepth < 100 && starstar && e - s == 2 |
| 3459 | && *path_end == '/') |
| 3460 | { |
| 3461 | STRCPY(s, path_end + 1); |
| 3462 | ++stardepth; |
| 3463 | (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE); |
| 3464 | --stardepth; |
| 3465 | } |
| 3466 | |
| 3467 | // Scan all files in the directory with "dir/ *.*" |
| 3468 | STRCPY(s, "*.*"); |
| 3469 | wn = enc_to_utf16(buf, NULL); |
| 3470 | if (wn != NULL) |
| 3471 | hFind = FindFirstFileW(wn, &wfb); |
| 3472 | ok = (hFind != INVALID_HANDLE_VALUE); |
| 3473 | |
| 3474 | while (ok) |
| 3475 | { |
| 3476 | p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here |
Bram Moolenaar | c74fbfe | 2020-04-06 22:56:28 +0200 | [diff] [blame] | 3477 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3478 | if (p == NULL) |
| 3479 | break; // out of memory |
| 3480 | |
Bram Moolenaar | 0dc5f60 | 2021-02-07 14:01:35 +0100 | [diff] [blame] | 3481 | // Do not use the alternate filename when the file name ends in '~', |
| 3482 | // because it picks up backup files: short name for "foo.vim~" is |
| 3483 | // "foo~1.vim", which matches "*.vim". |
| 3484 | if (*wfb.cAlternateFileName == NUL || p[STRLEN(p) - 1] == '~') |
Bram Moolenaar | 40655d5 | 2020-04-06 23:49:50 +0200 | [diff] [blame] | 3485 | p_alt = NULL; |
Bram Moolenaar | c74fbfe | 2020-04-06 22:56:28 +0200 | [diff] [blame] | 3486 | else |
| 3487 | p_alt = utf16_to_enc(wfb.cAlternateFileName, NULL); |
| 3488 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3489 | // Ignore entries starting with a dot, unless when asked for. Accept |
| 3490 | // all entries found with "matchname". |
| 3491 | if ((p[0] != '.' || starts_with_dot |
| 3492 | || ((flags & EW_DODOT) |
| 3493 | && p[1] != NUL && (p[1] != '.' || p[2] != NUL))) |
| 3494 | && (matchname == NULL |
| 3495 | || (regmatch.regprog != NULL |
Bram Moolenaar | 40655d5 | 2020-04-06 23:49:50 +0200 | [diff] [blame] | 3496 | && (vim_regexec(®match, p, (colnr_T)0) |
Bram Moolenaar | c74fbfe | 2020-04-06 22:56:28 +0200 | [diff] [blame] | 3497 | || (p_alt != NULL |
Bram Moolenaar | 40655d5 | 2020-04-06 23:49:50 +0200 | [diff] [blame] | 3498 | && vim_regexec(®match, p_alt, (colnr_T)0)))) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3499 | || ((flags & EW_NOTWILD) |
| 3500 | && fnamencmp(path + (s - buf), p, e - s) == 0))) |
| 3501 | { |
| 3502 | STRCPY(s, p); |
| 3503 | len = (int)STRLEN(buf); |
| 3504 | |
Bram Moolenaar | c74fbfe | 2020-04-06 22:56:28 +0200 | [diff] [blame] | 3505 | if (starstar && stardepth < 100 |
| 3506 | && (wfb.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3507 | { |
| 3508 | // For "**" in the pattern first go deeper in the tree to |
| 3509 | // find matches. |
| 3510 | STRCPY(buf + len, "/**"); |
| 3511 | STRCPY(buf + len + 3, path_end); |
| 3512 | ++stardepth; |
| 3513 | (void)dos_expandpath(gap, buf, len + 1, flags, TRUE); |
| 3514 | --stardepth; |
| 3515 | } |
| 3516 | |
| 3517 | STRCPY(buf + len, path_end); |
| 3518 | if (mch_has_exp_wildcard(path_end)) |
| 3519 | { |
| 3520 | // need to expand another component of the path |
| 3521 | // remove backslashes for the remaining components only |
| 3522 | (void)dos_expandpath(gap, buf, len + 1, flags, FALSE); |
| 3523 | } |
| 3524 | else |
| 3525 | { |
| 3526 | // no more wildcards, check if there is a match |
| 3527 | // remove backslashes for the remaining components only |
| 3528 | if (*path_end != 0) |
| 3529 | backslash_halve(buf + len + 1); |
| 3530 | if (mch_getperm(buf) >= 0) // add existing file |
| 3531 | addfile(gap, buf, flags); |
| 3532 | } |
| 3533 | } |
| 3534 | |
Bram Moolenaar | c74fbfe | 2020-04-06 22:56:28 +0200 | [diff] [blame] | 3535 | vim_free(p_alt); |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3536 | vim_free(p); |
| 3537 | ok = FindNextFileW(hFind, &wfb); |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3538 | } |
| 3539 | |
| 3540 | FindClose(hFind); |
| 3541 | vim_free(wn); |
| 3542 | vim_free(buf); |
| 3543 | vim_regfree(regmatch.regprog); |
| 3544 | vim_free(matchname); |
| 3545 | |
| 3546 | matches = gap->ga_len - start_len; |
| 3547 | if (matches > 0) |
| 3548 | qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches, |
| 3549 | sizeof(char_u *), pstrcmp); |
| 3550 | return matches; |
| 3551 | } |
| 3552 | |
| 3553 | int |
| 3554 | mch_expandpath( |
| 3555 | garray_T *gap, |
| 3556 | char_u *path, |
| 3557 | int flags) // EW_* flags |
| 3558 | { |
| 3559 | return dos_expandpath(gap, path, 0, flags, FALSE); |
| 3560 | } |
| 3561 | #endif // MSWIN |
| 3562 | |
| 3563 | #if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \ |
| 3564 | || defined(PROTO) |
| 3565 | /* |
| 3566 | * Unix style wildcard expansion code. |
| 3567 | * It's here because it's used both for Unix and Mac. |
| 3568 | */ |
| 3569 | static int |
| 3570 | pstrcmp(const void *a, const void *b) |
| 3571 | { |
| 3572 | return (pathcmp(*(char **)a, *(char **)b, -1)); |
| 3573 | } |
| 3574 | |
| 3575 | /* |
| 3576 | * Recursively expand one path component into all matching files and/or |
| 3577 | * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. |
| 3578 | * "path" has backslashes before chars that are not to be expanded, starting |
| 3579 | * at "path + wildoff". |
| 3580 | * Return the number of matches found. |
| 3581 | * NOTE: much of this is identical to dos_expandpath(), keep in sync! |
| 3582 | */ |
| 3583 | int |
| 3584 | unix_expandpath( |
| 3585 | garray_T *gap, |
| 3586 | char_u *path, |
| 3587 | int wildoff, |
| 3588 | int flags, // EW_* flags |
| 3589 | int didstar) // expanded "**" once already |
| 3590 | { |
| 3591 | char_u *buf; |
Bram Moolenaar | 386c24c | 2022-05-16 12:37:36 +0100 | [diff] [blame] | 3592 | size_t buflen; |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3593 | char_u *path_end; |
| 3594 | char_u *p, *s, *e; |
| 3595 | int start_len = gap->ga_len; |
| 3596 | char_u *pat; |
| 3597 | regmatch_T regmatch; |
| 3598 | int starts_with_dot; |
| 3599 | int matches; |
| 3600 | int len; |
| 3601 | int starstar = FALSE; |
| 3602 | static int stardepth = 0; // depth for "**" expansion |
| 3603 | |
| 3604 | DIR *dirp; |
| 3605 | struct dirent *dp; |
| 3606 | |
| 3607 | // Expanding "**" may take a long time, check for CTRL-C. |
| 3608 | if (stardepth > 0) |
| 3609 | { |
| 3610 | ui_breakcheck(); |
| 3611 | if (got_int) |
| 3612 | return 0; |
| 3613 | } |
| 3614 | |
| 3615 | // make room for file name |
Bram Moolenaar | 386c24c | 2022-05-16 12:37:36 +0100 | [diff] [blame] | 3616 | buflen = STRLEN(path) + BASENAMELEN + 5; |
| 3617 | buf = alloc(buflen); |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3618 | if (buf == NULL) |
| 3619 | return 0; |
| 3620 | |
| 3621 | /* |
| 3622 | * Find the first part in the path name that contains a wildcard. |
| 3623 | * When EW_ICASE is set every letter is considered to be a wildcard. |
| 3624 | * Copy it into "buf", including the preceding characters. |
| 3625 | */ |
| 3626 | p = buf; |
| 3627 | s = buf; |
| 3628 | e = NULL; |
| 3629 | path_end = path; |
| 3630 | while (*path_end != NUL) |
| 3631 | { |
| 3632 | // May ignore a wildcard that has a backslash before it; it will |
| 3633 | // be removed by rem_backslash() or file_pat_to_reg_pat() below. |
| 3634 | if (path_end >= path + wildoff && rem_backslash(path_end)) |
| 3635 | *p++ = *path_end++; |
| 3636 | else if (*path_end == '/') |
| 3637 | { |
| 3638 | if (e != NULL) |
| 3639 | break; |
| 3640 | s = p + 1; |
| 3641 | } |
| 3642 | else if (path_end >= path + wildoff |
| 3643 | && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL |
| 3644 | || (!p_fic && (flags & EW_ICASE) |
Bram Moolenaar | 5921aeb | 2022-02-19 11:20:12 +0000 | [diff] [blame] | 3645 | && vim_isalpha(PTR2CHAR(path_end))))) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3646 | e = p; |
| 3647 | if (has_mbyte) |
| 3648 | { |
| 3649 | len = (*mb_ptr2len)(path_end); |
| 3650 | STRNCPY(p, path_end, len); |
| 3651 | p += len; |
| 3652 | path_end += len; |
| 3653 | } |
| 3654 | else |
| 3655 | *p++ = *path_end++; |
| 3656 | } |
| 3657 | e = p; |
| 3658 | *e = NUL; |
| 3659 | |
| 3660 | // Now we have one wildcard component between "s" and "e". |
| 3661 | // Remove backslashes between "wildoff" and the start of the wildcard |
| 3662 | // component. |
| 3663 | for (p = buf + wildoff; p < s; ++p) |
| 3664 | if (rem_backslash(p)) |
| 3665 | { |
| 3666 | STRMOVE(p, p + 1); |
| 3667 | --e; |
| 3668 | --s; |
| 3669 | } |
| 3670 | |
| 3671 | // Check for "**" between "s" and "e". |
| 3672 | for (p = s; p < e; ++p) |
| 3673 | if (p[0] == '*' && p[1] == '*') |
| 3674 | starstar = TRUE; |
| 3675 | |
| 3676 | // convert the file pattern to a regexp pattern |
| 3677 | starts_with_dot = *s == '.'; |
| 3678 | pat = file_pat_to_reg_pat(s, e, NULL, FALSE); |
| 3679 | if (pat == NULL) |
| 3680 | { |
| 3681 | vim_free(buf); |
| 3682 | return 0; |
| 3683 | } |
| 3684 | |
| 3685 | // compile the regexp into a program |
| 3686 | if (flags & EW_ICASE) |
| 3687 | regmatch.rm_ic = TRUE; // 'wildignorecase' set |
| 3688 | else |
| 3689 | regmatch.rm_ic = p_fic; // ignore case when 'fileignorecase' is set |
| 3690 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
| 3691 | ++emsg_silent; |
| 3692 | regmatch.regprog = vim_regcomp(pat, RE_MAGIC); |
| 3693 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
| 3694 | --emsg_silent; |
| 3695 | vim_free(pat); |
| 3696 | |
| 3697 | if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) |
| 3698 | { |
| 3699 | vim_free(buf); |
| 3700 | return 0; |
| 3701 | } |
| 3702 | |
| 3703 | // If "**" is by itself, this is the first time we encounter it and more |
| 3704 | // is following then find matches without any directory. |
| 3705 | if (!didstar && stardepth < 100 && starstar && e - s == 2 |
| 3706 | && *path_end == '/') |
| 3707 | { |
| 3708 | STRCPY(s, path_end + 1); |
| 3709 | ++stardepth; |
| 3710 | (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE); |
| 3711 | --stardepth; |
| 3712 | } |
| 3713 | |
| 3714 | // open the directory for scanning |
| 3715 | *s = NUL; |
| 3716 | dirp = opendir(*buf == NUL ? "." : (char *)buf); |
| 3717 | |
| 3718 | // Find all matching entries |
| 3719 | if (dirp != NULL) |
| 3720 | { |
| 3721 | for (;;) |
| 3722 | { |
| 3723 | dp = readdir(dirp); |
| 3724 | if (dp == NULL) |
| 3725 | break; |
| 3726 | if ((dp->d_name[0] != '.' || starts_with_dot |
| 3727 | || ((flags & EW_DODOT) |
| 3728 | && dp->d_name[1] != NUL |
| 3729 | && (dp->d_name[1] != '.' || dp->d_name[2] != NUL))) |
| 3730 | && ((regmatch.regprog != NULL && vim_regexec(®match, |
| 3731 | (char_u *)dp->d_name, (colnr_T)0)) |
| 3732 | || ((flags & EW_NOTWILD) |
| 3733 | && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0))) |
| 3734 | { |
| 3735 | STRCPY(s, dp->d_name); |
| 3736 | len = STRLEN(buf); |
| 3737 | |
| 3738 | if (starstar && stardepth < 100) |
| 3739 | { |
| 3740 | // For "**" in the pattern first go deeper in the tree to |
| 3741 | // find matches. |
Bram Moolenaar | 386c24c | 2022-05-16 12:37:36 +0100 | [diff] [blame] | 3742 | vim_snprintf((char *)buf + len, buflen - len, |
| 3743 | "/**%s", path_end); |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3744 | ++stardepth; |
| 3745 | (void)unix_expandpath(gap, buf, len + 1, flags, TRUE); |
| 3746 | --stardepth; |
| 3747 | } |
| 3748 | |
Bram Moolenaar | 386c24c | 2022-05-16 12:37:36 +0100 | [diff] [blame] | 3749 | vim_snprintf((char *)buf + len, buflen - len, "%s", path_end); |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3750 | if (mch_has_exp_wildcard(path_end)) // handle more wildcards |
| 3751 | { |
| 3752 | // need to expand another component of the path |
| 3753 | // remove backslashes for the remaining components only |
| 3754 | (void)unix_expandpath(gap, buf, len + 1, flags, FALSE); |
| 3755 | } |
| 3756 | else |
| 3757 | { |
| 3758 | stat_T sb; |
| 3759 | |
| 3760 | // no more wildcards, check if there is a match |
| 3761 | // remove backslashes for the remaining components only |
| 3762 | if (*path_end != NUL) |
| 3763 | backslash_halve(buf + len + 1); |
| 3764 | // add existing file or symbolic link |
| 3765 | if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0 |
| 3766 | : mch_getperm(buf) >= 0) |
| 3767 | { |
| 3768 | #ifdef MACOS_CONVERT |
| 3769 | size_t precomp_len = STRLEN(buf)+1; |
| 3770 | char_u *precomp_buf = |
| 3771 | mac_precompose_path(buf, precomp_len, &precomp_len); |
| 3772 | |
| 3773 | if (precomp_buf) |
| 3774 | { |
| 3775 | mch_memmove(buf, precomp_buf, precomp_len); |
| 3776 | vim_free(precomp_buf); |
| 3777 | } |
| 3778 | #endif |
| 3779 | addfile(gap, buf, flags); |
| 3780 | } |
| 3781 | } |
| 3782 | } |
| 3783 | } |
| 3784 | |
| 3785 | closedir(dirp); |
| 3786 | } |
| 3787 | |
| 3788 | vim_free(buf); |
| 3789 | vim_regfree(regmatch.regprog); |
| 3790 | |
| 3791 | matches = gap->ga_len - start_len; |
| 3792 | if (matches > 0) |
| 3793 | qsort(((char_u **)gap->ga_data) + start_len, matches, |
| 3794 | sizeof(char_u *), pstrcmp); |
| 3795 | return matches; |
| 3796 | } |
| 3797 | #endif |
| 3798 | |
| 3799 | /* |
| 3800 | * Return TRUE if "p" contains what looks like an environment variable. |
| 3801 | * Allowing for escaping. |
| 3802 | */ |
| 3803 | static int |
| 3804 | has_env_var(char_u *p) |
| 3805 | { |
| 3806 | for ( ; *p; MB_PTR_ADV(p)) |
| 3807 | { |
| 3808 | if (*p == '\\' && p[1] != NUL) |
| 3809 | ++p; |
| 3810 | else if (vim_strchr((char_u *) |
| 3811 | #if defined(MSWIN) |
| 3812 | "$%" |
| 3813 | #else |
| 3814 | "$" |
| 3815 | #endif |
| 3816 | , *p) != NULL) |
| 3817 | return TRUE; |
| 3818 | } |
| 3819 | return FALSE; |
| 3820 | } |
| 3821 | |
| 3822 | #ifdef SPECIAL_WILDCHAR |
| 3823 | /* |
| 3824 | * Return TRUE if "p" contains a special wildcard character, one that Vim |
| 3825 | * cannot expand, requires using a shell. |
| 3826 | */ |
| 3827 | static int |
| 3828 | has_special_wildchar(char_u *p) |
| 3829 | { |
| 3830 | for ( ; *p; MB_PTR_ADV(p)) |
| 3831 | { |
| 3832 | // Disallow line break characters. |
| 3833 | if (*p == '\r' || *p == '\n') |
| 3834 | break; |
| 3835 | // Allow for escaping. |
| 3836 | if (*p == '\\' && p[1] != NUL && p[1] != '\r' && p[1] != '\n') |
| 3837 | ++p; |
| 3838 | else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL) |
| 3839 | { |
| 3840 | // A { must be followed by a matching }. |
| 3841 | if (*p == '{' && vim_strchr(p, '}') == NULL) |
| 3842 | continue; |
| 3843 | // A quote and backtick must be followed by another one. |
| 3844 | if ((*p == '`' || *p == '\'') && vim_strchr(p, *p) == NULL) |
| 3845 | continue; |
| 3846 | return TRUE; |
| 3847 | } |
| 3848 | } |
| 3849 | return FALSE; |
| 3850 | } |
| 3851 | #endif |
| 3852 | |
| 3853 | /* |
| 3854 | * Generic wildcard expansion code. |
| 3855 | * |
| 3856 | * Characters in "pat" that should not be expanded must be preceded with a |
| 3857 | * backslash. E.g., "/path\ with\ spaces/my\*star*" |
| 3858 | * |
| 3859 | * Return FAIL when no single file was found. In this case "num_file" is not |
| 3860 | * set, and "file" may contain an error message. |
| 3861 | * Return OK when some files found. "num_file" is set to the number of |
| 3862 | * matches, "file" to the array of matches. Call FreeWild() later. |
| 3863 | */ |
| 3864 | int |
| 3865 | gen_expand_wildcards( |
| 3866 | int num_pat, // number of input patterns |
| 3867 | char_u **pat, // array of input patterns |
| 3868 | int *num_file, // resulting number of files |
| 3869 | char_u ***file, // array of resulting files |
| 3870 | int flags) // EW_* flags |
| 3871 | { |
| 3872 | int i; |
| 3873 | garray_T ga; |
| 3874 | char_u *p; |
| 3875 | static int recursive = FALSE; |
| 3876 | int add_pat; |
| 3877 | int retval = OK; |
| 3878 | #if defined(FEAT_SEARCHPATH) |
| 3879 | int did_expand_in_path = FALSE; |
| 3880 | #endif |
| 3881 | |
| 3882 | /* |
| 3883 | * expand_env() is called to expand things like "~user". If this fails, |
| 3884 | * it calls ExpandOne(), which brings us back here. In this case, always |
| 3885 | * call the machine specific expansion function, if possible. Otherwise, |
| 3886 | * return FAIL. |
| 3887 | */ |
| 3888 | if (recursive) |
| 3889 | #ifdef SPECIAL_WILDCHAR |
| 3890 | return mch_expand_wildcards(num_pat, pat, num_file, file, flags); |
| 3891 | #else |
| 3892 | return FAIL; |
| 3893 | #endif |
| 3894 | |
| 3895 | #ifdef SPECIAL_WILDCHAR |
| 3896 | /* |
| 3897 | * If there are any special wildcard characters which we cannot handle |
| 3898 | * here, call machine specific function for all the expansion. This |
| 3899 | * avoids starting the shell for each argument separately. |
| 3900 | * For `=expr` do use the internal function. |
| 3901 | */ |
| 3902 | for (i = 0; i < num_pat; i++) |
| 3903 | { |
| 3904 | if (has_special_wildchar(pat[i]) |
| 3905 | # ifdef VIM_BACKTICK |
| 3906 | && !(vim_backtick(pat[i]) && pat[i][1] == '=') |
| 3907 | # endif |
| 3908 | ) |
| 3909 | return mch_expand_wildcards(num_pat, pat, num_file, file, flags); |
| 3910 | } |
| 3911 | #endif |
| 3912 | |
| 3913 | recursive = TRUE; |
| 3914 | |
| 3915 | /* |
| 3916 | * The matching file names are stored in a growarray. Init it empty. |
| 3917 | */ |
Bram Moolenaar | 04935fb | 2022-01-08 16:19:22 +0000 | [diff] [blame] | 3918 | ga_init2(&ga, sizeof(char_u *), 30); |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3919 | |
| 3920 | for (i = 0; i < num_pat; ++i) |
| 3921 | { |
| 3922 | add_pat = -1; |
| 3923 | p = pat[i]; |
| 3924 | |
| 3925 | #ifdef VIM_BACKTICK |
| 3926 | if (vim_backtick(p)) |
| 3927 | { |
| 3928 | add_pat = expand_backtick(&ga, p, flags); |
| 3929 | if (add_pat == -1) |
| 3930 | retval = FAIL; |
| 3931 | } |
| 3932 | else |
| 3933 | #endif |
| 3934 | { |
| 3935 | /* |
| 3936 | * First expand environment variables, "~/" and "~user/". |
| 3937 | */ |
| 3938 | if ((has_env_var(p) && !(flags & EW_NOTENV)) || *p == '~') |
| 3939 | { |
| 3940 | p = expand_env_save_opt(p, TRUE); |
| 3941 | if (p == NULL) |
| 3942 | p = pat[i]; |
| 3943 | #ifdef UNIX |
| 3944 | /* |
| 3945 | * On Unix, if expand_env() can't expand an environment |
| 3946 | * variable, use the shell to do that. Discard previously |
| 3947 | * found file names and start all over again. |
| 3948 | */ |
| 3949 | else if (has_env_var(p) || *p == '~') |
| 3950 | { |
| 3951 | vim_free(p); |
| 3952 | ga_clear_strings(&ga); |
| 3953 | i = mch_expand_wildcards(num_pat, pat, num_file, file, |
| 3954 | flags|EW_KEEPDOLLAR); |
| 3955 | recursive = FALSE; |
| 3956 | return i; |
| 3957 | } |
| 3958 | #endif |
| 3959 | } |
| 3960 | |
| 3961 | /* |
LemonBoy | a3157a4 | 2022-04-03 11:58:31 +0100 | [diff] [blame] | 3962 | * If there are wildcards or case-insensitive expansion is |
| 3963 | * required: Expand file names and add each match to the list. If |
| 3964 | * there is no match, and EW_NOTFOUND is given, add the pattern. |
| 3965 | * Otherwise: Add the file name if it exists or when EW_NOTFOUND is |
| 3966 | * given. |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3967 | */ |
LemonBoy | a3157a4 | 2022-04-03 11:58:31 +0100 | [diff] [blame] | 3968 | if (mch_has_exp_wildcard(p) || (flags & EW_ICASE)) |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 3969 | { |
| 3970 | #if defined(FEAT_SEARCHPATH) |
| 3971 | if ((flags & EW_PATH) |
| 3972 | && !mch_isFullName(p) |
| 3973 | && !(p[0] == '.' |
| 3974 | && (vim_ispathsep(p[1]) |
| 3975 | || (p[1] == '.' && vim_ispathsep(p[2])))) |
| 3976 | ) |
| 3977 | { |
| 3978 | // :find completion where 'path' is used. |
| 3979 | // Recursiveness is OK here. |
| 3980 | recursive = FALSE; |
| 3981 | add_pat = expand_in_path(&ga, p, flags); |
| 3982 | recursive = TRUE; |
| 3983 | did_expand_in_path = TRUE; |
| 3984 | } |
| 3985 | else |
| 3986 | #endif |
| 3987 | add_pat = mch_expandpath(&ga, p, flags); |
| 3988 | } |
| 3989 | } |
| 3990 | |
| 3991 | if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND))) |
| 3992 | { |
| 3993 | char_u *t = backslash_halve_save(p); |
| 3994 | |
| 3995 | // When EW_NOTFOUND is used, always add files and dirs. Makes |
| 3996 | // "vim c:/" work. |
| 3997 | if (flags & EW_NOTFOUND) |
| 3998 | addfile(&ga, t, flags | EW_DIR | EW_FILE); |
| 3999 | else |
| 4000 | addfile(&ga, t, flags); |
| 4001 | |
| 4002 | if (t != p) |
| 4003 | vim_free(t); |
| 4004 | } |
| 4005 | |
| 4006 | #if defined(FEAT_SEARCHPATH) |
| 4007 | if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH)) |
| 4008 | uniquefy_paths(&ga, p); |
| 4009 | #endif |
| 4010 | if (p != pat[i]) |
| 4011 | vim_free(p); |
| 4012 | } |
| 4013 | |
Bram Moolenaar | 566cc8c | 2020-06-29 21:14:51 +0200 | [diff] [blame] | 4014 | // When returning FAIL the array must be freed here. |
| 4015 | if (retval == FAIL) |
Yegappan Lakshmanan | 2b74b68 | 2022-04-03 21:30:32 +0100 | [diff] [blame] | 4016 | ga_clear_strings(&ga); |
Bram Moolenaar | 566cc8c | 2020-06-29 21:14:51 +0200 | [diff] [blame] | 4017 | |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 4018 | *num_file = ga.ga_len; |
Bram Moolenaar | 566cc8c | 2020-06-29 21:14:51 +0200 | [diff] [blame] | 4019 | *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data |
| 4020 | : (char_u **)_("no matches"); |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 4021 | |
| 4022 | recursive = FALSE; |
| 4023 | |
| 4024 | return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL; |
| 4025 | } |
| 4026 | |
| 4027 | /* |
| 4028 | * Add a file to a file list. Accepted flags: |
| 4029 | * EW_DIR add directories |
| 4030 | * EW_FILE add files |
| 4031 | * EW_EXEC add executable files |
| 4032 | * EW_NOTFOUND add even when it doesn't exist |
| 4033 | * EW_ADDSLASH add slash after directory name |
| 4034 | * EW_ALLLINKS add symlink also when the referred file does not exist |
| 4035 | */ |
| 4036 | void |
| 4037 | addfile( |
| 4038 | garray_T *gap, |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4039 | char_u *f, // filename |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 4040 | int flags) |
| 4041 | { |
| 4042 | char_u *p; |
| 4043 | int isdir; |
| 4044 | stat_T sb; |
| 4045 | |
| 4046 | // if the file/dir/link doesn't exist, may not add it |
| 4047 | if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS) |
| 4048 | ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0)) |
| 4049 | return; |
| 4050 | |
| 4051 | #ifdef FNAME_ILLEGAL |
| 4052 | // if the file/dir contains illegal characters, don't add it |
| 4053 | if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL) |
| 4054 | return; |
| 4055 | #endif |
| 4056 | |
| 4057 | isdir = mch_isdir(f); |
| 4058 | if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE))) |
| 4059 | return; |
| 4060 | |
| 4061 | // If the file isn't executable, may not add it. Do accept directories. |
| 4062 | // When invoked from expand_shellcmd() do not use $PATH. |
| 4063 | if (!isdir && (flags & EW_EXEC) |
| 4064 | && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD))) |
| 4065 | return; |
| 4066 | |
| 4067 | // Make room for another item in the file list. |
| 4068 | if (ga_grow(gap, 1) == FAIL) |
| 4069 | return; |
| 4070 | |
| 4071 | p = alloc(STRLEN(f) + 1 + isdir); |
| 4072 | if (p == NULL) |
| 4073 | return; |
| 4074 | |
| 4075 | STRCPY(p, f); |
| 4076 | #ifdef BACKSLASH_IN_FILENAME |
| 4077 | slash_adjust(p); |
| 4078 | #endif |
| 4079 | /* |
| 4080 | * Append a slash or backslash after directory names if none is present. |
| 4081 | */ |
| 4082 | #ifndef DONT_ADD_PATHSEP_TO_DIR |
| 4083 | if (isdir && (flags & EW_ADDSLASH)) |
| 4084 | add_pathsep(p); |
| 4085 | #endif |
| 4086 | ((char_u **)gap->ga_data)[gap->ga_len++] = p; |
| 4087 | } |
| 4088 | |
| 4089 | /* |
| 4090 | * Free the list of files returned by expand_wildcards() or other expansion |
| 4091 | * functions. |
| 4092 | */ |
| 4093 | void |
| 4094 | FreeWild(int count, char_u **files) |
| 4095 | { |
| 4096 | if (count <= 0 || files == NULL) |
| 4097 | return; |
| 4098 | while (count--) |
| 4099 | vim_free(files[count]); |
| 4100 | vim_free(files); |
| 4101 | } |
| 4102 | |
| 4103 | /* |
| 4104 | * Compare path "p[]" to "q[]". |
| 4105 | * If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]" |
| 4106 | * Return value like strcmp(p, q), but consider path separators. |
| 4107 | */ |
| 4108 | int |
| 4109 | pathcmp(const char *p, const char *q, int maxlen) |
| 4110 | { |
| 4111 | int i, j; |
| 4112 | int c1, c2; |
| 4113 | const char *s = NULL; |
| 4114 | |
| 4115 | for (i = 0, j = 0; maxlen < 0 || (i < maxlen && j < maxlen);) |
| 4116 | { |
| 4117 | c1 = PTR2CHAR((char_u *)p + i); |
| 4118 | c2 = PTR2CHAR((char_u *)q + j); |
| 4119 | |
| 4120 | // End of "p": check if "q" also ends or just has a slash. |
| 4121 | if (c1 == NUL) |
| 4122 | { |
| 4123 | if (c2 == NUL) // full match |
| 4124 | return 0; |
| 4125 | s = q; |
| 4126 | i = j; |
| 4127 | break; |
| 4128 | } |
| 4129 | |
| 4130 | // End of "q": check if "p" just has a slash. |
| 4131 | if (c2 == NUL) |
| 4132 | { |
| 4133 | s = p; |
| 4134 | break; |
| 4135 | } |
| 4136 | |
| 4137 | if ((p_fic ? MB_TOUPPER(c1) != MB_TOUPPER(c2) : c1 != c2) |
| 4138 | #ifdef BACKSLASH_IN_FILENAME |
| 4139 | // consider '/' and '\\' to be equal |
| 4140 | && !((c1 == '/' && c2 == '\\') |
| 4141 | || (c1 == '\\' && c2 == '/')) |
| 4142 | #endif |
| 4143 | ) |
| 4144 | { |
| 4145 | if (vim_ispathsep(c1)) |
| 4146 | return -1; |
| 4147 | if (vim_ispathsep(c2)) |
| 4148 | return 1; |
| 4149 | return p_fic ? MB_TOUPPER(c1) - MB_TOUPPER(c2) |
| 4150 | : c1 - c2; // no match |
| 4151 | } |
| 4152 | |
Bram Moolenaar | 1614a14 | 2019-10-06 22:00:13 +0200 | [diff] [blame] | 4153 | i += mb_ptr2len((char_u *)p + i); |
| 4154 | j += mb_ptr2len((char_u *)q + j); |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 4155 | } |
| 4156 | if (s == NULL) // "i" or "j" ran into "maxlen" |
| 4157 | return 0; |
| 4158 | |
| 4159 | c1 = PTR2CHAR((char_u *)s + i); |
Bram Moolenaar | 1614a14 | 2019-10-06 22:00:13 +0200 | [diff] [blame] | 4160 | c2 = PTR2CHAR((char_u *)s + i + mb_ptr2len((char_u *)s + i)); |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 4161 | // ignore a trailing slash, but not "//" or ":/" |
| 4162 | if (c2 == NUL |
| 4163 | && i > 0 |
| 4164 | && !after_pathsep((char_u *)s, (char_u *)s + i) |
| 4165 | #ifdef BACKSLASH_IN_FILENAME |
| 4166 | && (c1 == '/' || c1 == '\\') |
| 4167 | #else |
| 4168 | && c1 == '/' |
| 4169 | #endif |
| 4170 | ) |
| 4171 | return 0; // match with trailing slash |
| 4172 | if (s == q) |
| 4173 | return -1; // no match |
| 4174 | return 1; |
| 4175 | } |
| 4176 | |
| 4177 | /* |
| 4178 | * Return TRUE if "name" is a full (absolute) path name or URL. |
| 4179 | */ |
| 4180 | int |
| 4181 | vim_isAbsName(char_u *name) |
| 4182 | { |
| 4183 | return (path_with_url(name) != 0 || mch_isFullName(name)); |
| 4184 | } |
| 4185 | |
| 4186 | /* |
| 4187 | * Get absolute file name into buffer "buf[len]". |
| 4188 | * |
| 4189 | * return FAIL for failure, OK otherwise |
| 4190 | */ |
| 4191 | int |
| 4192 | vim_FullName( |
| 4193 | char_u *fname, |
| 4194 | char_u *buf, |
| 4195 | int len, |
| 4196 | int force) // force expansion even when already absolute |
| 4197 | { |
| 4198 | int retval = OK; |
| 4199 | int url; |
| 4200 | |
| 4201 | *buf = NUL; |
| 4202 | if (fname == NULL) |
| 4203 | return FAIL; |
| 4204 | |
| 4205 | url = path_with_url(fname); |
| 4206 | if (!url) |
| 4207 | retval = mch_FullName(fname, buf, len, force); |
| 4208 | if (url || retval == FAIL) |
| 4209 | { |
| 4210 | // something failed; use the file name (truncate when too long) |
| 4211 | vim_strncpy(buf, fname, len - 1); |
| 4212 | } |
| 4213 | #if defined(MSWIN) |
| 4214 | slash_adjust(buf); |
| 4215 | #endif |
| 4216 | return retval; |
| 4217 | } |