mirror of
https://github.com/neovim/neovim.git
synced 2025-09-18 17:28:23 +00:00
@@ -1307,52 +1307,58 @@ do_shell (
|
|||||||
apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf);
|
apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/// Create a shell command from a command string, input redirection file and
|
||||||
* Create a shell command from a command string, input redirection file and
|
/// output redirection file.
|
||||||
* output redirection file.
|
///
|
||||||
* Returns an allocated string with the shell command.
|
/// @param cmd Command to execute.
|
||||||
*/
|
/// @param itmp NULL or the input file.
|
||||||
char_u *
|
/// @param otmp NULL or the output file.
|
||||||
make_filter_cmd (
|
/// @returns an allocated string with the shell command.
|
||||||
char_u *cmd, /* command */
|
char_u *make_filter_cmd(char_u *cmd, char_u *itmp, char_u *otmp)
|
||||||
char_u *itmp, /* NULL or name of input file */
|
|
||||||
char_u *otmp /* NULL or name of output file */
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
size_t len = STRLEN(cmd) + 3; /* "()" + NUL */
|
bool is_fish_shell =
|
||||||
|
#if defined(UNIX)
|
||||||
|
STRNCMP(invocation_path_tail(p_sh, NULL), "fish", 4) == 0;
|
||||||
|
#else
|
||||||
|
false;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
size_t len = STRLEN(cmd) + 1; // At least enough space for cmd + NULL.
|
||||||
|
|
||||||
|
len += is_fish_shell ? sizeof("begin; ""; end") - 1
|
||||||
|
: sizeof("("")") - 1;
|
||||||
|
|
||||||
if (itmp != NULL)
|
if (itmp != NULL)
|
||||||
len += STRLEN(itmp) + 9; /* " { < " + " } " */
|
len += STRLEN(itmp) + sizeof(" { "" < "" } ") - 1;
|
||||||
if (otmp != NULL)
|
if (otmp != NULL)
|
||||||
len += STRLEN(otmp) + STRLEN(p_srr) + 2; /* " " */
|
len += STRLEN(otmp) + STRLEN(p_srr) + 2; // two extra spaces (" "),
|
||||||
char_u *buf = xmalloc(len);
|
char_u *buf = xmalloc(len);
|
||||||
|
|
||||||
#if defined(UNIX)
|
#if defined(UNIX)
|
||||||
/*
|
// Put delimiters around the command (for concatenated commands) when
|
||||||
* Put braces around the command (for concatenated commands) when
|
// redirecting input and/or output.
|
||||||
* redirecting input and/or output.
|
if (itmp != NULL || otmp != NULL) {
|
||||||
*/
|
char *fmt = is_fish_shell ? "begin; %s; end"
|
||||||
if (itmp != NULL || otmp != NULL)
|
: "(%s)";
|
||||||
vim_snprintf((char *)buf, len, "(%s)", (char *)cmd);
|
vim_snprintf((char *)buf, len, fmt, (char *)cmd);
|
||||||
else
|
} else {
|
||||||
STRCPY(buf, cmd);
|
STRCPY(buf, cmd);
|
||||||
|
}
|
||||||
|
|
||||||
if (itmp != NULL) {
|
if (itmp != NULL) {
|
||||||
STRCAT(buf, " < ");
|
STRCAT(buf, " < ");
|
||||||
STRCAT(buf, itmp);
|
STRCAT(buf, itmp);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
/*
|
// For shells that don't understand braces around commands, at least allow
|
||||||
* for shells that don't understand braces around commands, at least allow
|
// the use of commands in a pipe.
|
||||||
* the use of commands in a pipe.
|
|
||||||
*/
|
|
||||||
STRCPY(buf, cmd);
|
STRCPY(buf, cmd);
|
||||||
if (itmp != NULL) {
|
if (itmp != NULL) {
|
||||||
char_u *p;
|
char_u *p;
|
||||||
|
|
||||||
/*
|
// If there is a pipe, we have to put the '<' in front of it.
|
||||||
* If there is a pipe, we have to put the '<' in front of it.
|
// Don't do this when 'shellquote' is not empty, otherwise the
|
||||||
* Don't do this when 'shellquote' is not empty, otherwise the
|
// redirection would be inside the quotes.
|
||||||
* redirection would be inside the quotes.
|
|
||||||
*/
|
|
||||||
if (*p_shq == NUL) {
|
if (*p_shq == NUL) {
|
||||||
p = vim_strchr(buf, '|');
|
p = vim_strchr(buf, '|');
|
||||||
if (p != NULL)
|
if (p != NULL)
|
||||||
@@ -1363,7 +1369,7 @@ make_filter_cmd (
|
|||||||
if (*p_shq == NUL) {
|
if (*p_shq == NUL) {
|
||||||
p = vim_strchr(cmd, '|');
|
p = vim_strchr(cmd, '|');
|
||||||
if (p != NULL) {
|
if (p != NULL) {
|
||||||
STRCAT(buf, " "); /* insert a space before the '|' for DOS */
|
STRCAT(buf, " "); // Insert a space before the '|' for DOS
|
||||||
STRCAT(buf, p);
|
STRCAT(buf, p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2359,7 +2359,6 @@ void set_init_3(void)
|
|||||||
* This is done after other initializations, where 'shell' might have been
|
* This is done after other initializations, where 'shell' might have been
|
||||||
* set, but only if they have not been set before.
|
* set, but only if they have not been set before.
|
||||||
*/
|
*/
|
||||||
char_u *p;
|
|
||||||
int idx_srr;
|
int idx_srr;
|
||||||
int do_srr;
|
int do_srr;
|
||||||
int idx_sp;
|
int idx_sp;
|
||||||
@@ -2376,28 +2375,10 @@ void set_init_3(void)
|
|||||||
else
|
else
|
||||||
do_sp = !(options[idx_sp].flags & P_WAS_SET);
|
do_sp = !(options[idx_sp].flags & P_WAS_SET);
|
||||||
|
|
||||||
/*
|
size_t len = 0;
|
||||||
* Isolate the name of the shell:
|
char_u *p = (char_u *)invocation_path_tail(p_sh, &len);
|
||||||
* - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
|
p = vim_strnsave(p, len);
|
||||||
* - Remove any argument. E.g., "csh -f" -> "csh".
|
|
||||||
* But don't allow a space in the path, so that this works:
|
|
||||||
* "/usr/bin/csh --rcfile ~/.cshrc"
|
|
||||||
* But don't do that for Windows, it's common to have a space in the path.
|
|
||||||
*/
|
|
||||||
p = skiptowhite(p_sh);
|
|
||||||
if (*p == NUL) {
|
|
||||||
/* No white space, use the tail. */
|
|
||||||
p = vim_strsave(path_tail(p_sh));
|
|
||||||
} else {
|
|
||||||
char_u *p1, *p2;
|
|
||||||
|
|
||||||
/* Find the last path separator before the space. */
|
|
||||||
p1 = p_sh;
|
|
||||||
for (p2 = p_sh; p2 < p; mb_ptr_adv(p2))
|
|
||||||
if (vim_ispathsep(*p2))
|
|
||||||
p1 = p2 + 1;
|
|
||||||
p = vim_strnsave(p1, (int)(p - p1));
|
|
||||||
}
|
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* Default for p_sp is "| tee", for p_srr is ">".
|
* Default for p_sp is "| tee", for p_srr is ">".
|
||||||
@@ -2421,6 +2402,7 @@ void set_init_3(void)
|
|||||||
|| fnamecmp(p, "zsh") == 0
|
|| fnamecmp(p, "zsh") == 0
|
||||||
|| fnamecmp(p, "zsh-beta") == 0
|
|| fnamecmp(p, "zsh-beta") == 0
|
||||||
|| fnamecmp(p, "bash") == 0
|
|| fnamecmp(p, "bash") == 0
|
||||||
|
|| fnamecmp(p, "fish") == 0
|
||||||
) {
|
) {
|
||||||
if (do_sp) {
|
if (do_sp) {
|
||||||
p_sp = (char_u *)"2>&1| tee";
|
p_sp = (char_u *)"2>&1| tee";
|
||||||
|
@@ -131,6 +131,35 @@ char_u *path_tail_with_sep(char_u *fname)
|
|||||||
return tail;
|
return tail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Finds the path tail (or executable) in an invocation.
|
||||||
|
///
|
||||||
|
/// @param[in] invocation A program invocation in the form:
|
||||||
|
/// "path/to/exe [args]".
|
||||||
|
/// @param[out] len Stores the length of the executable name.
|
||||||
|
///
|
||||||
|
/// @post if `len` is not null, stores the length of the executable name.
|
||||||
|
///
|
||||||
|
/// @return The position of the last path separator + 1.
|
||||||
|
const char_u *invocation_path_tail(const char_u *invocation, size_t *len)
|
||||||
|
FUNC_ATTR_NONNULL_RET FUNC_ATTR_NONNULL_ARG(1)
|
||||||
|
{
|
||||||
|
const char_u *tail = get_past_head((char_u *) invocation);
|
||||||
|
const char_u *p = tail;
|
||||||
|
while (*p != NUL && *p != ' ') {
|
||||||
|
bool was_sep = vim_ispathsep_nocolon(*p);
|
||||||
|
mb_ptr_adv(p);
|
||||||
|
if (was_sep) {
|
||||||
|
tail = p; // Now tail points one past the separator.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (len != NULL) {
|
||||||
|
*len = (size_t)(p - tail);
|
||||||
|
}
|
||||||
|
|
||||||
|
return tail;
|
||||||
|
}
|
||||||
|
|
||||||
/// Get the next path component of a path name.
|
/// Get the next path component of a path name.
|
||||||
///
|
///
|
||||||
/// @param fname A file path. (Must be != NULL.)
|
/// @param fname A file path. (Must be != NULL.)
|
||||||
|
@@ -319,7 +319,7 @@ static int included_patches[] = {
|
|||||||
//279,
|
//279,
|
||||||
//278,
|
//278,
|
||||||
277,
|
277,
|
||||||
//276,
|
276,
|
||||||
275,
|
275,
|
||||||
274,
|
274,
|
||||||
//273 NA
|
//273 NA
|
||||||
|
@@ -114,6 +114,65 @@ describe 'path function', ->
|
|||||||
it 'returns the whole file name if there is no separator', ->
|
it 'returns the whole file name if there is no separator', ->
|
||||||
eq 'file.txt', path_tail_with_sep 'file.txt'
|
eq 'file.txt', path_tail_with_sep 'file.txt'
|
||||||
|
|
||||||
|
describe 'invocation_path_tail', ->
|
||||||
|
-- Returns the path tail and length (out param) of the tail.
|
||||||
|
-- Does not convert the tail from C-pointer to lua string for use with
|
||||||
|
-- strcmp.
|
||||||
|
invocation_path_tail = (invk) ->
|
||||||
|
plen = ffi.new 'size_t[?]', 1
|
||||||
|
ptail = path.invocation_path_tail (to_cstr invk), plen
|
||||||
|
neq NULL, ptail
|
||||||
|
|
||||||
|
-- it does not change the output if len==NULL
|
||||||
|
tail2 = path.invocation_path_tail (to_cstr invk), NULL
|
||||||
|
neq NULL, tail2
|
||||||
|
eq (ffi.string ptail), (ffi.string tail2)
|
||||||
|
|
||||||
|
ptail, plen[0]
|
||||||
|
|
||||||
|
-- This test mimics the intended use in C.
|
||||||
|
compare = (base, pinvk, len) ->
|
||||||
|
eq 0, (ffi.C.strncmp (to_cstr base), pinvk, len)
|
||||||
|
|
||||||
|
it 'returns the executable name of an invocation given a relative invocation', ->
|
||||||
|
invk, len = invocation_path_tail 'directory/exe a b c'
|
||||||
|
compare "exe a b c", invk, len
|
||||||
|
eq 3, len
|
||||||
|
|
||||||
|
it 'returns the executable name of an invocation given an absolute invocation', ->
|
||||||
|
if ffi.os == 'Windows'
|
||||||
|
invk, len = invocation_path_tail 'C:\\Users\\anyone\\Program Files\\z a b'
|
||||||
|
compare 'z a b', invk, len
|
||||||
|
eq 1, len
|
||||||
|
else
|
||||||
|
invk, len = invocation_path_tail '/usr/bin/z a b'
|
||||||
|
compare 'z a b', invk, len
|
||||||
|
eq 1, len
|
||||||
|
|
||||||
|
it 'does not count arguments to the executable as part of its path', ->
|
||||||
|
invk, len = invocation_path_tail 'exe a/b\\c'
|
||||||
|
compare "exe a/b\\c", invk, len
|
||||||
|
eq 3, len
|
||||||
|
|
||||||
|
it 'only accepts whitespace as a terminator for the executable name', ->
|
||||||
|
invk, len = invocation_path_tail 'exe-a+b_c[]()|#!@$%^&*'
|
||||||
|
eq 'exe-a+b_c[]()|#!@$%^&*', (ffi.string invk)
|
||||||
|
|
||||||
|
it 'is equivalent to path_tail when args do not contain a path separator', ->
|
||||||
|
ptail = path.path_tail to_cstr "a/b/c x y z"
|
||||||
|
neq NULL, ptail
|
||||||
|
tail = ffi.string ptail
|
||||||
|
|
||||||
|
invk, len = invocation_path_tail "a/b/c x y z"
|
||||||
|
eq tail, ffi.string invk
|
||||||
|
|
||||||
|
it 'is not equivalent to path_tail when args contain a path separator', ->
|
||||||
|
ptail = path.path_tail to_cstr "a/b/c x y/z"
|
||||||
|
neq NULL, ptail
|
||||||
|
|
||||||
|
invk, len = invocation_path_tail "a/b/c x y/z"
|
||||||
|
neq (ffi.string ptail), (ffi.string invk)
|
||||||
|
|
||||||
describe 'path_next_component', ->
|
describe 'path_next_component', ->
|
||||||
path_next_component = (file) ->
|
path_next_component = (file) ->
|
||||||
res = path.path_next_component (to_cstr file)
|
res = path.path_next_component (to_cstr file)
|
||||||
|
Reference in New Issue
Block a user