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