Split code for counting and parsing arguments

This commit is contained in:
Thiago de Arruda
2014-03-29 08:25:18 -03:00
parent 54782ecfe0
commit 1ab6cf47bd

View File

@@ -1721,47 +1721,59 @@ int options; /* SHELL_*, see vim.h */
if (options & SHELL_COOKED) if (options & SHELL_COOKED)
settmode(TMODE_COOK); /* set to normal mode */ settmode(TMODE_COOK); /* set to normal mode */
/* // Count the number of arguments for the shell
* Do this loop twice: p = newcmd;
* 1: find number of arguments inquote = FALSE;
* 2: separate them and build argv[] argc = 0;
*/ while (true) {
for (i = 0; i < 2; ++i) { ++argc;
p = newcmd; // Move `p` to the end of shell word by advancing the pointer it while it's
inquote = FALSE; // inside a quote or it's a non-whitespace character
argc = 0; while (*p && (inquote || (*p != ' ' && *p != TAB))) {
for (;; ) { if (*p == '"')
if (i == 1) // Found a quote character, switch the `inquote` flag
argv[argc] = (char *)p; inquote = !inquote;
++argc; ++p;
while (*p && (inquote || (*p != ' ' && *p != TAB))) {
if (*p == '"')
inquote = !inquote;
++p;
}
if (*p == NUL)
break;
if (i == 1)
*p++ = NUL;
p = skipwhite(p);
} }
if (argv == NULL) { if (*p == NUL)
/* break;
* Account for possible multiple args in p_shcf. // Move to the next word
*/ p = skipwhite(p);
p = p_shcf; }
for (;; ) {
p = skiptowhite(p);
if (*p == NUL)
break;
++argc;
p = skipwhite(p);
}
argv = (char **)alloc((unsigned)((argc + 4) * sizeof(char *))); // Account for multiple args in p_shcf('shellcmdflag' option)
if (argv == NULL) /* out of memory */ p = p_shcf;
goto error; while (true) {
// Same as above, but doesn't need to take quotes into consideration
p = skiptowhite(p);
if (*p == NUL)
break;
++argc;
p = skipwhite(p);
}
// Allocate argv memory
argv = (char **)alloc((unsigned)((argc + 4) * sizeof(char *)));
if (argv == NULL) // out of memory
goto error;
// Build argv[]
p = newcmd;
inquote = FALSE;
argc = 0;
while (true) {
argv[argc] = (char *)p;
++argc;
while (*p && (inquote || (*p != ' ' && *p != TAB))) {
if (*p == '"')
inquote = !inquote;
++p;
} }
if (*p == NUL)
break;
// Terminate the word
*p++ = NUL;
p = skipwhite(p);
} }
if (cmd != NULL) { if (cmd != NULL) {
char_u *s; char_u *s;