1
0
forked from aniani/vim

patch 8.1.1985: code for dealing with paths is spread out

Problem:    Code for dealing with paths is spread out.
Solution:   Move path related functions from misc1.c to filepath.c.
            Remove NO_EXPANDPATH.
This commit is contained in:
Bram Moolenaar
2019-09-04 20:59:15 +02:00
parent 3f4f3d8e7e
commit 26262f8777
14 changed files with 2152 additions and 2173 deletions

View File

@@ -282,8 +282,6 @@ static void f_synIDattr(typval_T *argvars, typval_T *rettv);
static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
static void f_synstack(typval_T *argvars, typval_T *rettv);
static void f_synconcealed(typval_T *argvars, typval_T *rettv);
static void f_system(typval_T *argvars, typval_T *rettv);
static void f_systemlist(typval_T *argvars, typval_T *rettv);
static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
@@ -9252,205 +9250,6 @@ f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
#endif
}
static void
get_cmd_output_as_rettv(
typval_T *argvars,
typval_T *rettv,
int retlist)
{
char_u *res = NULL;
char_u *p;
char_u *infile = NULL;
int err = FALSE;
FILE *fd;
list_T *list = NULL;
int flags = SHELL_SILENT;
rettv->v_type = VAR_STRING;
rettv->vval.v_string = NULL;
if (check_restricted() || check_secure())
goto errret;
if (argvars[1].v_type != VAR_UNKNOWN)
{
/*
* Write the text to a temp file, to be used for input of the shell
* command.
*/
if ((infile = vim_tempname('i', TRUE)) == NULL)
{
emsg(_(e_notmp));
goto errret;
}
fd = mch_fopen((char *)infile, WRITEBIN);
if (fd == NULL)
{
semsg(_(e_notopen), infile);
goto errret;
}
if (argvars[1].v_type == VAR_NUMBER)
{
linenr_T lnum;
buf_T *buf;
buf = buflist_findnr(argvars[1].vval.v_number);
if (buf == NULL)
{
semsg(_(e_nobufnr), argvars[1].vval.v_number);
fclose(fd);
goto errret;
}
for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
{
for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
{
err = TRUE;
break;
}
if (putc(NL, fd) == EOF)
{
err = TRUE;
break;
}
}
}
else if (argvars[1].v_type == VAR_LIST)
{
if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
err = TRUE;
}
else
{
size_t len;
char_u buf[NUMBUFLEN];
p = tv_get_string_buf_chk(&argvars[1], buf);
if (p == NULL)
{
fclose(fd);
goto errret; /* type error; errmsg already given */
}
len = STRLEN(p);
if (len > 0 && fwrite(p, len, 1, fd) != 1)
err = TRUE;
}
if (fclose(fd) != 0)
err = TRUE;
if (err)
{
emsg(_("E677: Error writing temp file"));
goto errret;
}
}
/* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
* echoes typeahead, that messes up the display. */
if (!msg_silent)
flags += SHELL_COOKED;
if (retlist)
{
int len;
listitem_T *li;
char_u *s = NULL;
char_u *start;
char_u *end;
int i;
res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
if (res == NULL)
goto errret;
list = list_alloc();
if (list == NULL)
goto errret;
for (i = 0; i < len; ++i)
{
start = res + i;
while (i < len && res[i] != NL)
++i;
end = res + i;
s = alloc(end - start + 1);
if (s == NULL)
goto errret;
for (p = s; start < end; ++p, ++start)
*p = *start == NUL ? NL : *start;
*p = NUL;
li = listitem_alloc();
if (li == NULL)
{
vim_free(s);
goto errret;
}
li->li_tv.v_type = VAR_STRING;
li->li_tv.v_lock = 0;
li->li_tv.vval.v_string = s;
list_append(list, li);
}
rettv_list_set(rettv, list);
list = NULL;
}
else
{
res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
#ifdef USE_CRNL
/* translate <CR><NL> into <NL> */
if (res != NULL)
{
char_u *s, *d;
d = res;
for (s = res; *s; ++s)
{
if (s[0] == CAR && s[1] == NL)
++s;
*d++ = *s;
}
*d = NUL;
}
#endif
rettv->vval.v_string = res;
res = NULL;
}
errret:
if (infile != NULL)
{
mch_remove(infile);
vim_free(infile);
}
if (res != NULL)
vim_free(res);
if (list != NULL)
list_free(list);
}
/*
* "system()" function
*/
static void
f_system(typval_T *argvars, typval_T *rettv)
{
get_cmd_output_as_rettv(argvars, rettv, FALSE);
}
/*
* "systemlist()" function
*/
static void
f_systemlist(typval_T *argvars, typval_T *rettv)
{
get_cmd_output_as_rettv(argvars, rettv, TRUE);
}
/*
* "tabpagebuflist()" function
*/

File diff suppressed because it is too large Load Diff

View File

@@ -1183,6 +1183,8 @@ extern char_u *compiled_user;
extern char_u *compiled_sys;
#endif
EXTERN char_u *homedir INIT(= NULL);
// When a window has a local directory, the absolute path of the global
// current directory is stored here (in allocated memory). If the current
// directory is not a local directory, globaldir is NULL.

File diff suppressed because it is too large Load Diff

View File

@@ -3938,82 +3938,6 @@ sort_strings(
qsort((void *)files, (size_t)count, sizeof(char_u *), sort_compare);
}
#if !defined(NO_EXPANDPATH) || defined(PROTO)
/*
* Compare path "p[]" to "q[]".
* If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]"
* Return value like strcmp(p, q), but consider path separators.
*/
int
pathcmp(const char *p, const char *q, int maxlen)
{
int i, j;
int c1, c2;
const char *s = NULL;
for (i = 0, j = 0; maxlen < 0 || (i < maxlen && j < maxlen);)
{
c1 = PTR2CHAR((char_u *)p + i);
c2 = PTR2CHAR((char_u *)q + j);
/* End of "p": check if "q" also ends or just has a slash. */
if (c1 == NUL)
{
if (c2 == NUL) /* full match */
return 0;
s = q;
i = j;
break;
}
/* End of "q": check if "p" just has a slash. */
if (c2 == NUL)
{
s = p;
break;
}
if ((p_fic ? MB_TOUPPER(c1) != MB_TOUPPER(c2) : c1 != c2)
#ifdef BACKSLASH_IN_FILENAME
/* consider '/' and '\\' to be equal */
&& !((c1 == '/' && c2 == '\\')
|| (c1 == '\\' && c2 == '/'))
#endif
)
{
if (vim_ispathsep(c1))
return -1;
if (vim_ispathsep(c2))
return 1;
return p_fic ? MB_TOUPPER(c1) - MB_TOUPPER(c2)
: c1 - c2; /* no match */
}
i += MB_PTR2LEN((char_u *)p + i);
j += MB_PTR2LEN((char_u *)q + j);
}
if (s == NULL) /* "i" or "j" ran into "maxlen" */
return 0;
c1 = PTR2CHAR((char_u *)s + i);
c2 = PTR2CHAR((char_u *)s + i + MB_PTR2LEN((char_u *)s + i));
/* ignore a trailing slash, but not "//" or ":/" */
if (c2 == NUL
&& i > 0
&& !after_pathsep((char_u *)s, (char_u *)s + i)
#ifdef BACKSLASH_IN_FILENAME
&& (c1 == '/' || c1 == '\\')
#else
&& c1 == '/'
#endif
)
return 0; /* match with trailing slash */
if (s == q)
return -1; /* no match */
return 1;
}
#endif
/*
* The putenv() implementation below comes from the "screen" program.
* Included with permission from Juergen Weigert.

View File

@@ -6396,7 +6396,6 @@ select_eintr:
return result;
}
#ifndef NO_EXPANDPATH
/*
* Expand a path into all matching files and/or directories. Handles "*",
* "?", "[a-z]", "**", etc.
@@ -6411,7 +6410,6 @@ mch_expandpath(
{
return unix_expandpath(gap, path, 0, flags, FALSE);
}
#endif
/*
* mch_expand_wildcards() - this code does wild-card pattern matching using

View File

@@ -397,10 +397,6 @@ typedef struct dsc$descriptor DESC;
/* Special wildcards that need to be handled by the shell */
#define SPECIAL_WILDCHAR "`'{"
#ifndef HAVE_OPENDIR
# define NO_EXPANDPATH
#endif
/*
* Unix has plenty of memory, use large buffers
*/

View File

@@ -28,4 +28,31 @@ void f_writefile(typval_T *argvars, typval_T *rettv);
char_u *do_browse(int flags, char_u *title, char_u *dflt, char_u *ext, char_u *initdir, char_u *filter, buf_T *buf);
void f_browse(typval_T *argvars, typval_T *rettv);
void f_browsedir(typval_T *argvars, typval_T *rettv);
void home_replace(buf_T *buf, char_u *src, char_u *dst, int dstlen, int one);
char_u *home_replace_save(buf_T *buf, char_u *src);
int fullpathcmp(char_u *s1, char_u *s2, int checkname, int expandenv);
char_u *gettail(char_u *fname);
char_u *gettail_sep(char_u *fname);
char_u *getnextcomp(char_u *fname);
char_u *get_past_head(char_u *path);
int vim_ispathsep(int c);
int vim_ispathsep_nocolon(int c);
void shorten_dir(char_u *str);
int dir_of_file_exists(char_u *fname);
int vim_fnamecmp(char_u *x, char_u *y);
int vim_fnamencmp(char_u *x, char_u *y, size_t len);
char_u *concat_fnames(char_u *fname1, char_u *fname2, int sep);
void add_pathsep(char_u *p);
char_u *FullName_save(char_u *fname, int force);
int vim_fexists(char_u *fname);
int expand_wildcards_eval(char_u **pat, int *num_file, char_u ***file, int flags);
int expand_wildcards(int num_pat, char_u **pat, int *num_files, char_u ***files, int flags);
int match_suffix(char_u *fname);
int unix_expandpath(garray_T *gap, char_u *path, int wildoff, int flags, int didstar);
int gen_expand_wildcards(int num_pat, char_u **pat, int *num_file, char_u ***file, int flags);
void addfile(garray_T *gap, char_u *f, int flags);
void FreeWild(int count, char_u **files);
int pathcmp(const char *p, const char *q, int maxlen);
int vim_isAbsName(char_u *name);
int vim_FullName(char_u *fname, char_u *buf, int len, int force);
/* vim: set ft=c : */

View File

@@ -43,40 +43,16 @@ void vim_setenv(char_u *name, char_u *val);
char_u *get_env_name(expand_T *xp, int idx);
char_u *get_users(expand_T *xp, int idx);
int match_user(char_u *name);
void home_replace(buf_T *buf, char_u *src, char_u *dst, int dstlen, int one);
char_u *home_replace_save(buf_T *buf, char_u *src);
int fullpathcmp(char_u *s1, char_u *s2, int checkname, int expandenv);
char_u *gettail(char_u *fname);
char_u *gettail_sep(char_u *fname);
char_u *getnextcomp(char_u *fname);
char_u *get_past_head(char_u *path);
int vim_ispathsep(int c);
int vim_ispathsep_nocolon(int c);
void shorten_dir(char_u *str);
int dir_of_file_exists(char_u *fname);
int vim_fnamecmp(char_u *x, char_u *y);
int vim_fnamencmp(char_u *x, char_u *y, size_t len);
char_u *concat_fnames(char_u *fname1, char_u *fname2, int sep);
char_u *concat_str(char_u *str1, char_u *str2);
void add_pathsep(char_u *p);
char_u *FullName_save(char_u *fname, int force);
void preserve_exit(void);
int vim_fexists(char_u *fname);
void line_breakcheck(void);
void fast_breakcheck(void);
int expand_wildcards_eval(char_u **pat, int *num_file, char_u ***file, int flags);
int expand_wildcards(int num_pat, char_u **pat, int *num_files, char_u ***files, int flags);
int match_suffix(char_u *fname);
int unix_expandpath(garray_T *gap, char_u *path, int wildoff, int flags, int didstar);
void remove_duplicates(garray_T *gap);
int gen_expand_wildcards(int num_pat, char_u **pat, int *num_file, char_u ***file, int flags);
void addfile(garray_T *gap, char_u *f, int flags);
char_u *get_cmd_output(char_u *cmd, char_u *infile, int flags, int *ret_len);
void FreeWild(int count, char_u **files);
void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
void f_system(typval_T *argvars, typval_T *rettv);
void f_systemlist(typval_T *argvars, typval_T *rettv);
int goto_im(void);
char_u *get_isolated_shell_name(void);
int path_is_url(char_u *p);
int path_with_url(char_u *fname);
int vim_isAbsName(char_u *name);
int vim_FullName(char_u *fname, char_u *buf, int len, int force);
/* vim: set ft=c : */

View File

@@ -92,7 +92,6 @@ void update_mouseshape(int shape_idx);
int vim_chdir(char_u *new_dir);
int get_user_name(char_u *buf, int len);
void sort_strings(char_u **files, int count);
int pathcmp(const char *p, const char *q, int maxlen);
int filewritable(char_u *fname);
int get2c(FILE *fd);
int get3c(FILE *fd);

View File

@@ -8,6 +8,7 @@ void add_pack_start_dirs(void);
void load_start_packages(void);
void ex_packloadall(exarg_T *eap);
void ex_packadd(exarg_T *eap);
void remove_duplicates(garray_T *gap);
int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirnames[]);
int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
void ex_source(exarg_T *eap);

View File

@@ -605,6 +605,28 @@ ex_packadd(exarg_T *eap)
}
#endif
/*
* Sort "gap" and remove duplicate entries. "gap" is expected to contain a
* list of file names in allocated memory.
*/
void
remove_duplicates(garray_T *gap)
{
int i;
int j;
char_u **fnames = (char_u **)gap->ga_data;
sort_strings(fnames, gap->ga_len);
for (i = gap->ga_len - 1; i > 0; --i)
if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
{
vim_free(fnames[i]);
for (j = i + 1; j < gap->ga_len; ++j)
fnames[j - 1] = fnames[j];
--gap->ga_len;
}
}
/*
* Expand color scheme, compiler or filetype names.
* Search from 'runtimepath':

View File

@@ -757,6 +757,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
1985,
/**/
1984,
/**/

View File

@@ -835,10 +835,6 @@ extern int (*dyn_libintl_wputenv)(const wchar_t *envstring);
# define W_WINROW(wp) (wp->w_winrow)
#endif
#ifdef NO_EXPANDPATH
# define gen_expand_wildcards mch_expand_wildcards
#endif
// Values for the find_pattern_in_path() function args 'type' and 'action':
#define FIND_ANY 1
#define FIND_DEFINE 2