diff options
Diffstat (limited to 'src/misc1.c')
-rw-r--r-- | src/misc1.c | 1912 |
1 files changed, 236 insertions, 1676 deletions
diff --git a/src/misc1.c b/src/misc1.c index ed3f57e110..45dcc327b7 100644 --- a/src/misc1.c +++ b/src/misc1.c @@ -18,9 +18,6 @@ # include <lm.h> #endif -static char_u *vim_version_dir(char_u *vimdir); -static char_u *remove_tail(char_u *p, char_u *pend, char_u *name); - #define URL_SLASH 1 /* path_is_url() has found "://" */ #define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */ @@ -1593,8 +1590,6 @@ vim_beep( * For Windows: * This code is duplicated in init_homedir() in dosinst.c. Keep in sync! */ -static char_u *homedir = NULL; - void init_homedir(void) { @@ -2046,6 +2041,45 @@ expand_env_esc( } /* + * If the string between "p" and "pend" ends in "name/", return "pend" minus + * the length of "name/". Otherwise return "pend". + */ + static char_u * +remove_tail(char_u *p, char_u *pend, char_u *name) +{ + int len = (int)STRLEN(name) + 1; + char_u *newend = pend - len; + + if (newend >= p + && fnamencmp(newend, name, len - 1) == 0 + && (newend == p || after_pathsep(p, newend))) + return newend; + return pend; +} + +/* + * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists. + * Return NULL if not, return its name in allocated memory otherwise. + */ + static char_u * +vim_version_dir(char_u *vimdir) +{ + char_u *p; + + if (vimdir == NULL || *vimdir == NUL) + return NULL; + p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE); + if (p != NULL && mch_isdir(p)) + return p; + vim_free(p); + p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE); + if (p != NULL && mch_isdir(p)) + return p; + vim_free(p); + return NULL; +} + +/* * Vim's version of getenv(). * Special handling of $HOME, $VIM and $VIMRUNTIME. * Also does ACP to 'enc' conversion for Win32. @@ -2270,45 +2304,6 @@ vim_getenv(char_u *name, int *mustfree) return p; } -/* - * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists. - * Return NULL if not, return its name in allocated memory otherwise. - */ - static char_u * -vim_version_dir(char_u *vimdir) -{ - char_u *p; - - if (vimdir == NULL || *vimdir == NUL) - return NULL; - p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE); - if (p != NULL && mch_isdir(p)) - return p; - vim_free(p); - p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE); - if (p != NULL && mch_isdir(p)) - return p; - vim_free(p); - return NULL; -} - -/* - * If the string between "p" and "pend" ends in "name/", return "pend" minus - * the length of "name/". Otherwise return "pend". - */ - static char_u * -remove_tail(char_u *p, char_u *pend, char_u *name) -{ - int len = (int)STRLEN(name) + 1; - char_u *newend = pend - len; - - if (newend >= p - && fnamencmp(newend, name, len - 1) == 0 - && (newend == p || after_pathsep(p, newend))) - return newend; - return pend; -} - #if defined(FEAT_EVAL) || defined(PROTO) void vim_unsetenv(char_u *var) @@ -2529,500 +2524,6 @@ match_user(char_u *name) } /* - * Replace home directory by "~" in each space or comma separated file name in - * 'src'. - * If anything fails (except when out of space) dst equals src. - */ - void -home_replace( - buf_T *buf, /* when not NULL, check for help files */ - char_u *src, /* input file name */ - char_u *dst, /* where to put the result */ - int dstlen, /* maximum length of the result */ - int one) /* if TRUE, only replace one file name, include - spaces and commas in the file name. */ -{ - size_t dirlen = 0, envlen = 0; - size_t len; - char_u *homedir_env, *homedir_env_orig; - char_u *p; - - if (src == NULL) - { - *dst = NUL; - return; - } - - /* - * If the file is a help file, remove the path completely. - */ - if (buf != NULL && buf->b_help) - { - vim_snprintf((char *)dst, dstlen, "%s", gettail(src)); - return; - } - - /* - * We check both the value of the $HOME environment variable and the - * "real" home directory. - */ - if (homedir != NULL) - dirlen = STRLEN(homedir); - -#ifdef VMS - homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN"); -#else - homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME"); -#endif -#ifdef MSWIN - if (homedir_env == NULL) - homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE"); -#endif - /* Empty is the same as not set. */ - if (homedir_env != NULL && *homedir_env == NUL) - homedir_env = NULL; - - if (homedir_env != NULL && *homedir_env == '~') - { - int usedlen = 0; - int flen; - char_u *fbuf = NULL; - - flen = (int)STRLEN(homedir_env); - (void)modify_fname((char_u *)":p", FALSE, &usedlen, - &homedir_env, &fbuf, &flen); - flen = (int)STRLEN(homedir_env); - if (flen > 0 && vim_ispathsep(homedir_env[flen - 1])) - /* Remove the trailing / that is added to a directory. */ - homedir_env[flen - 1] = NUL; - } - - if (homedir_env != NULL) - envlen = STRLEN(homedir_env); - - if (!one) - src = skipwhite(src); - while (*src && dstlen > 0) - { - /* - * Here we are at the beginning of a file name. - * First, check to see if the beginning of the file name matches - * $HOME or the "real" home directory. Check that there is a '/' - * after the match (so that if e.g. the file is "/home/pieter/bla", - * and the home directory is "/home/piet", the file does not end up - * as "~er/bla" (which would seem to indicate the file "bla" in user - * er's home directory)). - */ - p = homedir; - len = dirlen; - for (;;) - { - if ( len - && fnamencmp(src, p, len) == 0 - && (vim_ispathsep(src[len]) - || (!one && (src[len] == ',' || src[len] == ' ')) - || src[len] == NUL)) - { - src += len; - if (--dstlen > 0) - *dst++ = '~'; - - /* - * If it's just the home directory, add "/". - */ - if (!vim_ispathsep(src[0]) && --dstlen > 0) - *dst++ = '/'; - break; - } - if (p == homedir_env) - break; - p = homedir_env; - len = envlen; - } - - /* if (!one) skip to separator: space or comma */ - while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0) - *dst++ = *src++; - /* skip separator */ - while ((*src == ' ' || *src == ',') && --dstlen > 0) - *dst++ = *src++; - } - /* if (dstlen == 0) out of space, what to do??? */ - - *dst = NUL; - - if (homedir_env != homedir_env_orig) - vim_free(homedir_env); -} - -/* - * Like home_replace, store the replaced string in allocated memory. - * When something fails, NULL is returned. - */ - char_u * -home_replace_save( - buf_T *buf, /* when not NULL, check for help files */ - char_u *src) /* input file name */ -{ - char_u *dst; - unsigned len; - - len = 3; /* space for "~/" and trailing NUL */ - if (src != NULL) /* just in case */ - len += (unsigned)STRLEN(src); - dst = alloc(len); - if (dst != NULL) - home_replace(buf, src, dst, len, TRUE); - return dst; -} - -/* - * Compare two file names and return: - * FPC_SAME if they both exist and are the same file. - * FPC_SAMEX if they both don't exist and have the same file name. - * FPC_DIFF if they both exist and are different files. - * FPC_NOTX if they both don't exist. - * FPC_DIFFX if one of them doesn't exist. - * For the first name environment variables are expanded if "expandenv" is - * TRUE. - */ - int -fullpathcmp( - char_u *s1, - char_u *s2, - int checkname, // when both don't exist, check file names - int expandenv) -{ -#ifdef UNIX - char_u exp1[MAXPATHL]; - char_u full1[MAXPATHL]; - char_u full2[MAXPATHL]; - stat_T st1, st2; - int r1, r2; - - if (expandenv) - expand_env(s1, exp1, MAXPATHL); - else - vim_strncpy(exp1, s1, MAXPATHL - 1); - r1 = mch_stat((char *)exp1, &st1); - r2 = mch_stat((char *)s2, &st2); - if (r1 != 0 && r2 != 0) - { - /* if mch_stat() doesn't work, may compare the names */ - if (checkname) - { - if (fnamecmp(exp1, s2) == 0) - return FPC_SAMEX; - r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); - r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); - if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0) - return FPC_SAMEX; - } - return FPC_NOTX; - } - if (r1 != 0 || r2 != 0) - return FPC_DIFFX; - if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) - return FPC_SAME; - return FPC_DIFF; -#else - char_u *exp1; /* expanded s1 */ - char_u *full1; /* full path of s1 */ - char_u *full2; /* full path of s2 */ - int retval = FPC_DIFF; - int r1, r2; - - /* allocate one buffer to store three paths (alloc()/free() is slow!) */ - if ((exp1 = alloc(MAXPATHL * 3)) != NULL) - { - full1 = exp1 + MAXPATHL; - full2 = full1 + MAXPATHL; - - if (expandenv) - expand_env(s1, exp1, MAXPATHL); - else - vim_strncpy(exp1, s1, MAXPATHL - 1); - r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); - r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); - - /* If vim_FullName() fails, the file probably doesn't exist. */ - if (r1 != OK && r2 != OK) - { - if (checkname && fnamecmp(exp1, s2) == 0) - retval = FPC_SAMEX; - else - retval = FPC_NOTX; - } - else if (r1 != OK || r2 != OK) - retval = FPC_DIFFX; - else if (fnamecmp(full1, full2)) - retval = FPC_DIFF; - else - retval = FPC_SAME; - vim_free(exp1); - } - return retval; -#endif -} - -/* - * Get the tail of a path: the file name. - * When the path ends in a path separator the tail is the NUL after it. - * Fail safe: never returns NULL. - */ - char_u * -gettail(char_u *fname) -{ - char_u *p1, *p2; - - if (fname == NULL) - return (char_u *)""; - for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */ - { - if (vim_ispathsep_nocolon(*p2)) - p1 = p2 + 1; - MB_PTR_ADV(p2); - } - return p1; -} - -/* - * Get pointer to tail of "fname", including path separators. Putting a NUL - * here leaves the directory name. Takes care of "c:/" and "//". - * Always returns a valid pointer. - */ - char_u * -gettail_sep(char_u *fname) -{ - char_u *p; - char_u *t; - - p = get_past_head(fname); /* don't remove the '/' from "c:/file" */ - t = gettail(fname); - while (t > p && after_pathsep(fname, t)) - --t; -#ifdef VMS - /* path separator is part of the path */ - ++t; -#endif - return t; -} - -/* - * get the next path component (just after the next path separator). - */ - char_u * -getnextcomp(char_u *fname) -{ - while (*fname && !vim_ispathsep(*fname)) - MB_PTR_ADV(fname); - if (*fname) - ++fname; - return fname; -} - -/* - * Get a pointer to one character past the head of a path name. - * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head. - * If there is no head, path is returned. - */ - char_u * -get_past_head(char_u *path) -{ - char_u *retval; - -#if defined(MSWIN) - /* may skip "c:" */ - if (isalpha(path[0]) && path[1] == ':') - retval = path + 2; - else - retval = path; -#else -# if defined(AMIGA) - /* may skip "label:" */ - retval = vim_strchr(path, ':'); - if (retval == NULL) - retval = path; -# else /* Unix */ - retval = path; -# endif -#endif - - while (vim_ispathsep(*retval)) - ++retval; - - return retval; -} - -/* - * Return TRUE if 'c' is a path separator. - * Note that for MS-Windows this includes the colon. - */ - int -vim_ispathsep(int c) -{ -#ifdef UNIX - return (c == '/'); /* UNIX has ':' inside file names */ -#else -# ifdef BACKSLASH_IN_FILENAME - return (c == ':' || c == '/' || c == '\\'); -# else -# ifdef VMS - /* server"user passwd"::device:[full.path.name]fname.extension;version" */ - return (c == ':' || c == '[' || c == ']' || c == '/' - || c == '<' || c == '>' || c == '"' ); -# else - return (c == ':' || c == '/'); -# endif /* VMS */ -# endif -#endif -} - -/* - * Like vim_ispathsep(c), but exclude the colon for MS-Windows. - */ - int -vim_ispathsep_nocolon(int c) -{ - return vim_ispathsep(c) -#ifdef BACKSLASH_IN_FILENAME - && c != ':' -#endif - ; -} - -/* - * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname" - * It's done in-place. - */ - void -shorten_dir(char_u *str) -{ - char_u *tail, *s, *d; - int skip = FALSE; - - tail = gettail(str); - d = str; - for (s = str; ; ++s) - { - if (s >= tail) /* copy the whole tail */ - { - *d++ = *s; - if (*s == NUL) - break; - } - else if (vim_ispathsep(*s)) /* copy '/' and next char */ - { - *d++ = *s; - skip = FALSE; - } - else if (!skip) - { - *d++ = *s; /* copy next char */ - if (*s != '~' && *s != '.') /* and leading "~" and "." */ - skip = TRUE; - if (has_mbyte) - { - int l = mb_ptr2len(s); - - while (--l > 0) - *d++ = *++s; - } - } - } -} - -/* - * Return TRUE if the directory of "fname" exists, FALSE otherwise. - * Also returns TRUE if there is no directory name. - * "fname" must be writable!. - */ - int -dir_of_file_exists(char_u *fname) -{ - char_u *p; - int c; - int retval; - - p = gettail_sep(fname); - if (p == fname) - return TRUE; - c = *p; - *p = NUL; - retval = mch_isdir(fname); - *p = c; - return retval; -} - -/* - * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally - * and deal with 'fileignorecase'. - */ - int -vim_fnamecmp(char_u *x, char_u *y) -{ -#ifdef BACKSLASH_IN_FILENAME - return vim_fnamencmp(x, y, MAXPATHL); -#else - if (p_fic) - return MB_STRICMP(x, y); - return STRCMP(x, y); -#endif -} - - int -vim_fnamencmp(char_u *x, char_u *y, size_t len) -{ -#ifdef BACKSLASH_IN_FILENAME - char_u *px = x; - char_u *py = y; - int cx = NUL; - int cy = NUL; - - while (len > 0) - { - cx = PTR2CHAR(px); - cy = PTR2CHAR(py); - if (cx == NUL || cy == NUL - || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy) - && !(cx == '/' && cy == '\\') - && !(cx == '\\' && cy == '/'))) - break; - len -= MB_PTR2LEN(px); - px += MB_PTR2LEN(px); - py += MB_PTR2LEN(py); - } - if (len == 0) - return 0; - return (cx - cy); -#else - if (p_fic) - return MB_STRNICMP(x, y, len); - return STRNCMP(x, y, len); -#endif -} - -/* - * Concatenate file names fname1 and fname2 into allocated memory. - * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary. - */ - char_u * -concat_fnames(char_u *fname1, char_u *fname2, int sep) -{ - char_u *dest; - - dest = alloc(STRLEN(fname1) + STRLEN(fname2) + 3); - if (dest != NULL) - { - STRCPY(dest, fname1); - if (sep) - add_pathsep(dest); - STRCAT(dest, fname2); - } - return dest; -} - -/* * Concatenate two strings and return the result in allocated memory. * Returns NULL when out of memory. */ @@ -3041,45 +2542,6 @@ concat_str(char_u *str1, char_u *str2) return dest; } -/* - * Add a path separator to a file name, unless it already ends in a path - * separator. - */ - void -add_pathsep(char_u *p) -{ - if (*p != NUL && !after_pathsep(p, p + STRLEN(p))) - STRCAT(p, PATHSEPSTR); -} - -/* - * FullName_save - Make an allocated copy of a full file name. - * Returns NULL when out of memory. - */ - char_u * -FullName_save( - char_u *fname, - int force) /* force expansion, even when it already looks - * like a full path name */ -{ - char_u *buf; - char_u *new_fname = NULL; - - if (fname == NULL) - return NULL; - - buf = alloc(MAXPATHL); - if (buf != NULL) - { - if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL) - new_fname = vim_strsave(buf); - else - new_fname = vim_strsave(fname); - vim_free(buf); - } - return new_fname; -} - static void prepare_to_exit(void) { @@ -3154,19 +2616,6 @@ preserve_exit(void) } /* - * return TRUE if "fname" exists. - */ - int -vim_fexists(char_u *fname) -{ - stat_T st; - - if (mch_stat((char *)fname, &st)) - return FALSE; - return TRUE; -} - -/* * Check for CTRL-C pressed, but only once in a while. * Should be used instead of ui_breakcheck() for functions that check for * each line in the file. Calling ui_breakcheck() each time takes too much @@ -3202,1042 +2651,6 @@ fast_breakcheck(void) } } -/* - * Invoke expand_wildcards() for one pattern. - * Expand items like "%:h" before the expansion. - * Returns OK or FAIL. - */ - int -expand_wildcards_eval( - char_u **pat, /* pointer to input pattern */ - int *num_file, /* resulting number of files */ - char_u ***file, /* array of resulting files */ - int flags) /* EW_DIR, etc. */ -{ - int ret = FAIL; - char_u *eval_pat = NULL; - char_u *exp_pat = *pat; - char *ignored_msg; - int usedlen; - - if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<') - { - ++emsg_off; - eval_pat = eval_vars(exp_pat, exp_pat, &usedlen, - NULL, &ignored_msg, NULL); - --emsg_off; - if (eval_pat != NULL) - exp_pat = concat_str(eval_pat, exp_pat + usedlen); - } - - if (exp_pat != NULL) - ret = expand_wildcards(1, &exp_pat, num_file, file, flags); - - if (eval_pat != NULL) - { - vim_free(exp_pat); - vim_free(eval_pat); - } - - return ret; -} - -/* - * Expand wildcards. Calls gen_expand_wildcards() and removes files matching - * 'wildignore'. - * Returns OK or FAIL. When FAIL then "num_files" won't be set. - */ - int -expand_wildcards( - int num_pat, /* number of input patterns */ - char_u **pat, /* array of input patterns */ - int *num_files, /* resulting number of files */ - char_u ***files, /* array of resulting files */ - int flags) /* EW_DIR, etc. */ -{ - int retval; - int i, j; - char_u *p; - int non_suf_match; /* number without matching suffix */ - - retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags); - - /* When keeping all matches, return here */ - if ((flags & EW_KEEPALL) || retval == FAIL) - return retval; - -#ifdef FEAT_WILDIGN - /* - * Remove names that match 'wildignore'. - */ - if (*p_wig) - { - char_u *ffname; - - /* check all files in (*files)[] */ - for (i = 0; i < *num_files; ++i) - { - ffname = FullName_save((*files)[i], FALSE); - if (ffname == NULL) /* out of memory */ - break; -# ifdef VMS - vms_remove_version(ffname); -# endif - if (match_file_list(p_wig, (*files)[i], ffname)) - { - /* remove this matching file from the list */ - vim_free((*files)[i]); - for (j = i; j + 1 < *num_files; ++j) - (*files)[j] = (*files)[j + 1]; - --*num_files; - --i; - } - vim_free(ffname); - } - - /* If the number of matches is now zero, we fail. */ - if (*num_files == 0) - { - VIM_CLEAR(*files); - return FAIL; - } - } -#endif - - /* - * Move the names where 'suffixes' match to the end. - */ - if (*num_files > 1) - { - non_suf_match = 0; - for (i = 0; i < *num_files; ++i) - { - if (!match_suffix((*files)[i])) - { - /* - * Move the name without matching suffix to the front - * of the list. - */ - p = (*files)[i]; - for (j = i; j > non_suf_match; --j) - (*files)[j] = (*files)[j - 1]; - (*files)[non_suf_match++] = p; - } - } - } - - return retval; -} - -/* - * Return TRUE if "fname" matches with an entry in 'suffixes'. - */ - int -match_suffix(char_u *fname) -{ - int fnamelen, setsuflen; - char_u *setsuf; -#define MAXSUFLEN 30 /* maximum length of a file suffix */ - char_u suf_buf[MAXSUFLEN]; - - fnamelen = (int)STRLEN(fname); - setsuflen = 0; - for (setsuf = p_su; *setsuf; ) - { - setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,"); - if (setsuflen == 0) - { - char_u *tail = gettail(fname); - - /* empty entry: match name without a '.' */ - if (vim_strchr(tail, '.') == NULL) - { - setsuflen = 1; - break; - } - } - else - { - if (fnamelen >= setsuflen - && fnamencmp(suf_buf, fname + fnamelen - setsuflen, - (size_t)setsuflen) == 0) - break; - setsuflen = 0; - } - } - return (setsuflen != 0); -} - -#if !defined(NO_EXPANDPATH) || defined(PROTO) - -# ifdef VIM_BACKTICK -static int vim_backtick(char_u *p); -static int expand_backtick(garray_T *gap, char_u *pat, int flags); -# endif - -# if defined(MSWIN) -/* - * File name expansion code for MS-DOS, Win16 and Win32. It's here because - * it's shared between these systems. - */ - -/* - * comparison function for qsort in dos_expandpath() - */ - static int -pstrcmp(const void *a, const void *b) -{ - return (pathcmp(*(char **)a, *(char **)b, -1)); -} - -/* - * Recursively expand one path component into all matching files and/or - * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. - * Return the number of matches found. - * "path" has backslashes before chars that are not to be expanded, starting - * at "path[wildoff]". - * Return the number of matches found. - * NOTE: much of this is identical to unix_expandpath(), keep in sync! - */ - static int -dos_expandpath( - garray_T *gap, - char_u *path, - int wildoff, - int flags, /* EW_* flags */ - int didstar) /* expanded "**" once already */ -{ - char_u *buf; - char_u *path_end; - char_u *p, *s, *e; - int start_len = gap->ga_len; - char_u *pat; - regmatch_T regmatch; - int starts_with_dot; - int matches; - int len; - int starstar = FALSE; - static int stardepth = 0; // depth for "**" expansion - HANDLE hFind = INVALID_HANDLE_VALUE; - WIN32_FIND_DATAW wfb; - WCHAR *wn = NULL; // UCS-2 name, NULL when not used. - char_u *matchname; - int ok; - - /* Expanding "**" may take a long time, check for CTRL-C. */ - if (stardepth > 0) - { - ui_breakcheck(); - if (got_int) - return 0; - } - - // Make room for file name. When doing encoding conversion the actual - // length may be quite a bit longer, thus use the maximum possible length. - buf = alloc(MAXPATHL); - if (buf == NULL) - return 0; - - /* - * Find the first part in the path name that contains a wildcard or a ~1. - * Copy it into buf, including the preceding characters. - */ - p = buf; - s = buf; - e = NULL; - path_end = path; - while (*path_end != NUL) - { - /* May ignore a wildcard that has a backslash before it; it will - * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ - if (path_end >= path + wildoff && rem_backslash(path_end)) - *p++ = *path_end++; - else if (*path_end == '\\' || *path_end == ':' || *path_end == '/') - { - if (e != NULL) - break; - s = p + 1; - } - else if (path_end >= path + wildoff - && vim_strchr((char_u *)"*?[~", *path_end) != NULL) - e = p; - if (has_mbyte) - { - len = (*mb_ptr2len)(path_end); - STRNCPY(p, path_end, len); - p += len; - path_end += len; - } - else - *p++ = *path_end++; - } - e = p; - *e = NUL; - - /* now we have one wildcard component between s and e */ - /* Remove backslashes between "wildoff" and the start of the wildcard - * component. */ - for (p = buf + wildoff; p < s; ++p) - if (rem_backslash(p)) - { - STRMOVE(p, p + 1); - --e; - --s; - } - - /* Check for "**" between "s" and "e". */ - for (p = s; p < e; ++p) - if (p[0] == '*' && p[1] == '*') - starstar = TRUE; - - starts_with_dot = *s == '.'; - pat = file_pat_to_reg_pat(s, e, NULL, FALSE); - if (pat == NULL) - { - vim_free(buf); - return 0; - } - - /* compile the regexp into a program */ - if (flags & (EW_NOERROR | EW_NOTWILD)) - ++emsg_silent; - regmatch.rm_ic = TRUE; /* Always ignore case */ - regmatch.regprog = vim_regcomp(pat, RE_MAGIC); - if (flags & (EW_NOERROR | EW_NOTWILD)) - --emsg_silent; - vim_free(pat); - - if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) - { - vim_free(buf); - return 0; - } - - /* remember the pattern or file name being looked for */ - matchname = vim_strsave(s); - - /* If "**" is by itself, this is the first time we encounter it and more - * is following then find matches without any directory. */ - if (!didstar && stardepth < 100 && starstar && e - s == 2 - && *path_end == '/') - { - STRCPY(s, path_end + 1); - ++stardepth; - (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE); - --stardepth; - } - - /* Scan all files in the directory with "dir/ *.*" */ - STRCPY(s, "*.*"); - wn = enc_to_utf16(buf, NULL); - if (wn != NULL) - hFind = FindFirstFileW(wn, &wfb); - ok = (hFind != INVALID_HANDLE_VALUE); - - while (ok) - { - p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here - if (p == NULL) - break; // out of memory - - // Ignore entries starting with a dot, unless when asked for. Accept - // all entries found with "matchname". - if ((p[0] != '.' || starts_with_dot - || ((flags & EW_DODOT) - && p[1] != NUL && (p[1] != '.' || p[2] != NUL))) - && (matchname == NULL - || (regmatch.regprog != NULL - && vim_regexec(®match, p, (colnr_T)0)) - || ((flags & EW_NOTWILD) - && fnamencmp(path + (s - buf), p, e - s) == 0))) - { - STRCPY(s, p); - len = (int)STRLEN(buf); - - if (starstar && stardepth < 100) - { - /* For "**" in the pattern first go deeper in the tree to - * find matches. */ - STRCPY(buf + len, "/**"); - STRCPY(buf + len + 3, path_end); - ++stardepth; - (void)dos_expandpath(gap, buf, len + 1, flags, TRUE); - --stardepth; - } - - STRCPY(buf + len, path_end); - if (mch_has_exp_wildcard(path_end)) - { - /* need to expand another component of the path */ - /* remove backslashes for the remaining components only */ - (void)dos_expandpath(gap, buf, len + 1, flags, FALSE); - } - else - { - /* no more wildcards, check if there is a match */ - /* remove backslashes for the remaining components only */ - if (*path_end != 0) - backslash_halve(buf + len + 1); - if (mch_getperm(buf) >= 0) /* add existing file */ - addfile(gap, buf, flags); - } - } - - vim_free(p); - ok = FindNextFileW(hFind, &wfb); - - /* If no more matches and no match was used, try expanding the name - * itself. Finds the long name of a short filename. */ - if (!ok && matchname != NULL && gap->ga_len == start_len) - { - STRCPY(s, matchname); - FindClose(hFind); - vim_free(wn); - wn = enc_to_utf16(buf, NULL); - if (wn != NULL) - hFind = FindFirstFileW(wn, &wfb); - else - hFind = INVALID_HANDLE_VALUE; - ok = (hFind != INVALID_HANDLE_VALUE); - VIM_CLEAR(matchname); - } - } - - FindClose(hFind); - vim_free(wn); - vim_free(buf); - vim_regfree(regmatch.regprog); - vim_free(matchname); - - matches = gap->ga_len - start_len; - if (matches > 0) - qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches, - sizeof(char_u *), pstrcmp); - return matches; -} - - int -mch_expandpath( - garray_T *gap, - char_u *path, - int flags) /* EW_* flags */ -{ - return dos_expandpath(gap, path, 0, flags, FALSE); -} -# endif // MSWIN - -#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \ - || defined(PROTO) -/* - * Unix style wildcard expansion code. - * It's here because it's used both for Unix and Mac. - */ - static int -pstrcmp(const void *a, const void *b) -{ - return (pathcmp(*(char **)a, *(char **)b, -1)); -} - -/* - * Recursively expand one path component into all matching files and/or - * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. - * "path" has backslashes before chars that are not to be expanded, starting - * at "path + wildoff". - * Return the number of matches found. - * NOTE: much of this is identical to dos_expandpath(), keep in sync! - */ - int -unix_expandpath( - garray_T *gap, - char_u *path, - int wildoff, - int flags, /* EW_* flags */ - int didstar) /* expanded "**" once already */ -{ - char_u *buf; - char_u *path_end; - char_u *p, *s, *e; - int start_len = gap->ga_len; - char_u *pat; - regmatch_T regmatch; - int starts_with_dot; - int matches; - int len; - int starstar = FALSE; - static int stardepth = 0; /* depth for "**" expansion */ - - DIR *dirp; - struct dirent *dp; - - /* Expanding "**" may take a long time, check for CTRL-C. */ - if (stardepth > 0) - { - ui_breakcheck(); - if (got_int) - return 0; - } - - /* make room for file name */ - buf = alloc(STRLEN(path) + BASENAMELEN + 5); - if (buf == NULL) - return 0; - - /* - * Find the first part in the path name that contains a wildcard. - * When EW_ICASE is set every letter is considered to be a wildcard. - * Copy it into "buf", including the preceding characters. - */ - p = buf; - s = buf; - e = NULL; - path_end = path; - while (*path_end != NUL) - { - /* May ignore a wildcard that has a backslash before it; it will - * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ - if (path_end >= path + wildoff && rem_backslash(path_end)) - *p++ = *path_end++; - else if (*path_end == '/') - { - if (e != NULL) - break; - s = p + 1; - } - else if (path_end >= path + wildoff - && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL - || (!p_fic && (flags & EW_ICASE) - && isalpha(PTR2CHAR(path_end))))) - e = p; - if (has_mbyte) - { - len = (*mb_ptr2len)(path_end); - STRNCPY(p, path_end, len); - p += len; - path_end += len; - } - else - *p++ = *path_end++; - } - e = p; - *e = NUL; - - /* Now we have one wildcard component between "s" and "e". */ - /* Remove backslashes between "wildoff" and the start of the wildcard - * component. */ - for (p = buf + wildoff; p < s; ++p) - if (rem_backslash(p)) - { - STRMOVE(p, p + 1); - --e; - --s; - } - - /* Check for "**" between "s" and "e". */ - for (p = s; p < e; ++p) - if (p[0] == '*' && p[1] == '*') - starstar = TRUE; - - /* convert the file pattern to a regexp pattern */ - starts_with_dot = *s == '.'; - pat = file_pat_to_reg_pat(s, e, NULL, FALSE); - if (pat == NULL) - { - vim_free(buf); - return 0; - } - - /* compile the regexp into a program */ - if (flags & EW_ICASE) - regmatch.rm_ic = TRUE; /* 'wildignorecase' set */ - else - regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */ - if (flags & (EW_NOERROR | EW_NOTWILD)) - ++emsg_silent; - regmatch.regprog = vim_regcomp(pat, RE_MAGIC); - if (flags & (EW_NOERROR | EW_NOTWILD)) - --emsg_silent; - vim_free(pat); - - if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) - { - vim_free(buf); - return 0; - } - - /* If "**" is by itself, this is the first time we encounter it and more - * is following then find matches without any directory. */ - if (!didstar && stardepth < 100 && starstar && e - s == 2 - && *path_end == '/') - { - STRCPY(s, path_end + 1); - ++stardepth; - (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE); - --stardepth; - } - - /* open the directory for scanning */ - *s = NUL; - dirp = opendir(*buf == NUL ? "." : (char *)buf); - - /* Find all matching entries */ - if (dirp != NULL) - { - for (;;) - { - dp = readdir(dirp); - if (dp == NULL) - break; |