1994-08-16 23:36:45 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1996-11-01 06:45:43 +00:00
|
|
|
* Copyright (c) 1993, 1994, 1995, 1996
|
|
|
|
* Keith Bostic. All rights reserved.
|
1994-08-16 23:36:45 +00:00
|
|
|
*
|
1996-11-01 06:45:43 +00:00
|
|
|
* See the LICENSE file for redistribution information.
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
1994-08-16 23:36:45 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/queue.h>
|
2013-08-11 09:44:58 +00:00
|
|
|
#include <sys/time.h>
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
#include <bitstring.h>
|
|
|
|
#include <ctype.h>
|
1996-11-01 06:45:43 +00:00
|
|
|
#include <dirent.h>
|
1994-08-16 23:36:45 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
2013-08-11 09:44:58 +00:00
|
|
|
#include <pwd.h>
|
1994-08-16 23:36:45 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
#include "../common/common.h"
|
1994-08-16 23:36:45 +00:00
|
|
|
|
2015-04-10 13:26:05 +00:00
|
|
|
static int argv_alloc(SCR *, size_t);
|
|
|
|
static int argv_comp(const void *, const void *);
|
|
|
|
static int argv_fexp(SCR *, EXCMD *,
|
|
|
|
CHAR_T *, size_t, CHAR_T *, size_t *, CHAR_T **, size_t *, int);
|
|
|
|
static int argv_sexp(SCR *, CHAR_T **, size_t *, size_t *);
|
|
|
|
static int argv_flt_user(SCR *, EXCMD *, CHAR_T *, size_t);
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* argv_init --
|
|
|
|
* Build a prototype arguments list.
|
1996-11-01 06:45:43 +00:00
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: int argv_init(SCR *, EXCMD *);
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
int
|
2013-08-11 09:44:58 +00:00
|
|
|
argv_init(SCR *sp, EXCMD *excp)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
|
|
|
EX_PRIVATE *exp;
|
|
|
|
|
|
|
|
exp = EXP(sp);
|
|
|
|
exp->argsoff = 0;
|
|
|
|
argv_alloc(sp, 1);
|
|
|
|
|
|
|
|
excp->argv = exp->args;
|
|
|
|
excp->argc = exp->argsoff;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* argv_exp0 --
|
|
|
|
* Append a string to the argument list.
|
1996-11-01 06:45:43 +00:00
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: int argv_exp0(SCR *, EXCMD *, CHAR_T *, size_t);
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
int
|
2013-08-11 09:44:58 +00:00
|
|
|
argv_exp0(SCR *sp, EXCMD *excp, CHAR_T *cmd, size_t cmdlen)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
|
|
|
EX_PRIVATE *exp;
|
|
|
|
|
|
|
|
exp = EXP(sp);
|
|
|
|
argv_alloc(sp, cmdlen);
|
2013-08-11 09:44:58 +00:00
|
|
|
MEMCPY(exp->args[exp->argsoff]->bp, cmd, cmdlen);
|
1994-08-16 23:36:45 +00:00
|
|
|
exp->args[exp->argsoff]->bp[cmdlen] = '\0';
|
|
|
|
exp->args[exp->argsoff]->len = cmdlen;
|
|
|
|
++exp->argsoff;
|
|
|
|
excp->argv = exp->args;
|
|
|
|
excp->argc = exp->argsoff;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* argv_exp1 --
|
|
|
|
* Do file name expansion on a string, and append it to the
|
|
|
|
* argument list.
|
1996-11-01 06:45:43 +00:00
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: int argv_exp1(SCR *, EXCMD *, CHAR_T *, size_t, int);
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
int
|
2013-08-11 09:44:58 +00:00
|
|
|
argv_exp1(SCR *sp, EXCMD *excp, CHAR_T *cmd, size_t cmdlen, int is_bang)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
|
|
|
EX_PRIVATE *exp;
|
|
|
|
size_t blen, len;
|
2013-08-11 09:44:58 +00:00
|
|
|
CHAR_T *p, *t, *bp;
|
1994-08-16 23:36:45 +00:00
|
|
|
|
2013-08-11 09:44:58 +00:00
|
|
|
GET_SPACE_RETW(sp, bp, blen, 512);
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
len = 0;
|
|
|
|
exp = EXP(sp);
|
|
|
|
if (argv_fexp(sp, excp, cmd, cmdlen, bp, &len, &bp, &blen, is_bang)) {
|
2013-08-11 09:44:58 +00:00
|
|
|
FREE_SPACEW(sp, bp, blen);
|
1994-08-16 23:36:45 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If it's empty, we're done. */
|
|
|
|
if (len != 0) {
|
|
|
|
for (p = bp, t = bp + len; p < t; ++p)
|
2013-08-11 09:44:58 +00:00
|
|
|
if (!cmdskip(*p))
|
1994-08-16 23:36:45 +00:00
|
|
|
break;
|
|
|
|
if (p == t)
|
|
|
|
goto ret;
|
|
|
|
} else
|
|
|
|
goto ret;
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
(void)argv_exp0(sp, excp, bp, len);
|
|
|
|
|
2013-08-11 09:44:58 +00:00
|
|
|
ret: FREE_SPACEW(sp, bp, blen);
|
1994-08-16 23:36:45 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* argv_exp2 --
|
|
|
|
* Do file name and shell expansion on a string, and append it to
|
|
|
|
* the argument list.
|
1996-11-01 06:45:43 +00:00
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: int argv_exp2(SCR *, EXCMD *, CHAR_T *, size_t);
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
int
|
2013-08-11 09:44:58 +00:00
|
|
|
argv_exp2(SCR *sp, EXCMD *excp, CHAR_T *cmd, size_t cmdlen)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
|
|
|
size_t blen, len, n;
|
|
|
|
int rval;
|
2013-08-11 09:44:58 +00:00
|
|
|
CHAR_T *bp, *p;
|
1994-08-16 23:36:45 +00:00
|
|
|
|
2013-08-11 09:44:58 +00:00
|
|
|
GET_SPACE_RETW(sp, bp, blen, 512);
|
1994-08-16 23:36:45 +00:00
|
|
|
|
2013-08-11 09:44:58 +00:00
|
|
|
#define SHELLECHO L("echo ")
|
|
|
|
#define SHELLOFFSET (SIZE(SHELLECHO) - 1)
|
|
|
|
MEMCPY(bp, SHELLECHO, SHELLOFFSET);
|
1994-08-16 23:36:45 +00:00
|
|
|
p = bp + SHELLOFFSET;
|
|
|
|
len = SHELLOFFSET;
|
|
|
|
|
|
|
|
#if defined(DEBUG) && 0
|
|
|
|
TRACE(sp, "file_argv: {%.*s}\n", (int)cmdlen, cmd);
|
|
|
|
#endif
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
if (argv_fexp(sp, excp, cmd, cmdlen, p, &len, &bp, &blen, 0)) {
|
1994-08-16 23:36:45 +00:00
|
|
|
rval = 1;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(DEBUG) && 0
|
|
|
|
TRACE(sp, "before shell: %d: {%s}\n", len, bp);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do shell word expansion -- it's very, very hard to figure out what
|
|
|
|
* magic characters the user's shell expects. Historically, it was a
|
|
|
|
* union of v7 shell and csh meta characters. We match that practice
|
|
|
|
* by default, so ":read \%" tries to read a file named '%'. It would
|
|
|
|
* make more sense to pass any special characters through the shell,
|
|
|
|
* but then, if your shell was csh, the above example will behave
|
|
|
|
* differently in nvi than in vi. If you want to get other characters
|
|
|
|
* passed through to your shell, change the "meta" option.
|
|
|
|
*/
|
1996-11-01 06:45:43 +00:00
|
|
|
if (opts_empty(sp, O_SHELL, 1) || opts_empty(sp, O_SHELLMETA, 1))
|
|
|
|
n = 0;
|
|
|
|
else {
|
|
|
|
p = bp + SHELLOFFSET;
|
|
|
|
n = len - SHELLOFFSET;
|
2013-08-11 09:44:58 +00:00
|
|
|
for (; n > 0; --n, ++p)
|
|
|
|
if (IS_SHELLMETA(sp, *p))
|
|
|
|
break;
|
1996-11-01 06:45:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we found a meta character in the string, fork a shell to expand
|
|
|
|
* it. Unfortunately, this is comparatively slow. Historically, it
|
|
|
|
* didn't matter much, since users don't enter meta characters as part
|
|
|
|
* of pathnames that frequently. The addition of filename completion
|
2013-08-11 09:44:58 +00:00
|
|
|
* broke that assumption because it's easy to use. To increase the
|
|
|
|
* completion performance, nvi used to have an internal routine to
|
|
|
|
* handle "filename*". However, the shell special characters does not
|
|
|
|
* limit to "shellmeta", so such a hack breaks historic practice.
|
|
|
|
* After it all, we split the completion logic out from here.
|
1996-11-01 06:45:43 +00:00
|
|
|
*/
|
|
|
|
switch (n) {
|
|
|
|
case 0:
|
|
|
|
p = bp + SHELLOFFSET;
|
|
|
|
len -= SHELLOFFSET;
|
|
|
|
rval = argv_exp3(sp, excp, p, len);
|
|
|
|
break;
|
|
|
|
default:
|
1994-08-16 23:36:45 +00:00
|
|
|
if (argv_sexp(sp, &bp, &blen, &len)) {
|
|
|
|
rval = 1;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
p = bp;
|
1996-11-01 06:45:43 +00:00
|
|
|
rval = argv_exp3(sp, excp, p, len);
|
|
|
|
break;
|
1994-08-16 23:36:45 +00:00
|
|
|
}
|
|
|
|
|
2013-08-11 09:44:58 +00:00
|
|
|
err: FREE_SPACEW(sp, bp, blen);
|
1994-08-16 23:36:45 +00:00
|
|
|
return (rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* argv_exp3 --
|
|
|
|
* Take a string and break it up into an argv, which is appended
|
|
|
|
* to the argument list.
|
1996-11-01 06:45:43 +00:00
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: int argv_exp3(SCR *, EXCMD *, CHAR_T *, size_t);
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
int
|
2013-08-11 09:44:58 +00:00
|
|
|
argv_exp3(SCR *sp, EXCMD *excp, CHAR_T *cmd, size_t cmdlen)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
|
|
|
EX_PRIVATE *exp;
|
|
|
|
size_t len;
|
|
|
|
int ch, off;
|
2013-08-11 09:44:58 +00:00
|
|
|
CHAR_T *ap, *p;
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
for (exp = EXP(sp); cmdlen > 0; ++exp->argsoff) {
|
|
|
|
/* Skip any leading whitespace. */
|
|
|
|
for (; cmdlen > 0; --cmdlen, ++cmd) {
|
|
|
|
ch = *cmd;
|
2013-08-11 09:44:58 +00:00
|
|
|
if (!cmdskip(ch))
|
1994-08-16 23:36:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (cmdlen == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine the length of this whitespace delimited
|
|
|
|
* argument.
|
|
|
|
*
|
|
|
|
* QUOTING NOTE:
|
|
|
|
*
|
|
|
|
* Skip any character preceded by the user's quoting
|
|
|
|
* character.
|
|
|
|
*/
|
|
|
|
for (ap = cmd, len = 0; cmdlen > 0; ++cmd, --cmdlen, ++len) {
|
|
|
|
ch = *cmd;
|
1996-11-01 06:45:43 +00:00
|
|
|
if (IS_ESCAPE(sp, excp, ch) && cmdlen > 1) {
|
1994-08-16 23:36:45 +00:00
|
|
|
++cmd;
|
|
|
|
--cmdlen;
|
2013-08-11 09:44:58 +00:00
|
|
|
} else if (cmdskip(ch))
|
1994-08-16 23:36:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy the argument into place.
|
|
|
|
*
|
|
|
|
* QUOTING NOTE:
|
|
|
|
*
|
|
|
|
* Lose quote chars.
|
|
|
|
*/
|
|
|
|
argv_alloc(sp, len);
|
|
|
|
off = exp->argsoff;
|
|
|
|
exp->args[off]->len = len;
|
|
|
|
for (p = exp->args[off]->bp; len > 0; --len, *p++ = *ap++)
|
1996-11-01 06:45:43 +00:00
|
|
|
if (IS_ESCAPE(sp, excp, *ap))
|
1994-08-16 23:36:45 +00:00
|
|
|
++ap;
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
excp->argv = exp->args;
|
|
|
|
excp->argc = exp->argsoff;
|
|
|
|
|
|
|
|
#if defined(DEBUG) && 0
|
|
|
|
for (cnt = 0; cnt < exp->argsoff; ++cnt)
|
|
|
|
TRACE(sp, "arg %d: {%s}\n", cnt, exp->argv[cnt]);
|
|
|
|
#endif
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2013-08-11 09:44:58 +00:00
|
|
|
/*
|
|
|
|
* argv_flt_ex --
|
|
|
|
* Filter the ex commands with a prefix, and append the results to
|
|
|
|
* the argument list.
|
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: int argv_flt_ex(SCR *, EXCMD *, CHAR_T *, size_t);
|
2013-08-11 09:44:58 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
argv_flt_ex(SCR *sp, EXCMD *excp, CHAR_T *cmd, size_t cmdlen)
|
|
|
|
{
|
|
|
|
EX_PRIVATE *exp;
|
|
|
|
EXCMDLIST const *cp;
|
|
|
|
int off;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
exp = EXP(sp);
|
|
|
|
|
|
|
|
for (off = exp->argsoff, cp = cmds; cp->name != NULL; ++cp) {
|
|
|
|
len = STRLEN(cp->name);
|
|
|
|
if (cmdlen > 0 &&
|
|
|
|
(cmdlen > len || MEMCMP(cmd, cp->name, cmdlen)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Copy the matched ex command name. */
|
|
|
|
argv_alloc(sp, len + 1);
|
|
|
|
MEMCPY(exp->args[exp->argsoff]->bp, cp->name, len + 1);
|
|
|
|
exp->args[exp->argsoff]->len = len;
|
|
|
|
++exp->argsoff;
|
|
|
|
excp->argv = exp->args;
|
|
|
|
excp->argc = exp->argsoff;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* argv_flt_user --
|
|
|
|
* Filter the ~user list on the system with a prefix, and append
|
|
|
|
* the results to the argument list.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
argv_flt_user(SCR *sp, EXCMD *excp, CHAR_T *uname, size_t ulen)
|
|
|
|
{
|
|
|
|
EX_PRIVATE *exp;
|
|
|
|
struct passwd *pw;
|
|
|
|
int off;
|
|
|
|
char *np;
|
|
|
|
size_t len, nlen;
|
|
|
|
|
|
|
|
exp = EXP(sp);
|
|
|
|
off = exp->argsoff;
|
|
|
|
|
|
|
|
/* The input must come with a leading '~'. */
|
|
|
|
INT2CHAR(sp, uname + 1, ulen - 1, np, nlen);
|
|
|
|
if ((np = v_strdup(sp, np, nlen)) == NULL)
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
setpwent();
|
|
|
|
while ((pw = getpwent()) != NULL) {
|
|
|
|
len = strlen(pw->pw_name);
|
|
|
|
if (nlen > 0 &&
|
|
|
|
(nlen > len || memcmp(np, pw->pw_name, nlen)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Copy '~' + the matched user name. */
|
|
|
|
CHAR2INT(sp, pw->pw_name, len + 1, uname, ulen);
|
|
|
|
argv_alloc(sp, ulen + 1);
|
|
|
|
exp->args[exp->argsoff]->bp[0] = '~';
|
|
|
|
MEMCPY(exp->args[exp->argsoff]->bp + 1, uname, ulen);
|
|
|
|
exp->args[exp->argsoff]->len = ulen;
|
|
|
|
++exp->argsoff;
|
|
|
|
excp->argv = exp->args;
|
|
|
|
excp->argc = exp->argsoff;
|
|
|
|
}
|
|
|
|
endpwent();
|
|
|
|
free(np);
|
|
|
|
|
|
|
|
qsort(exp->args + off, exp->argsoff - off, sizeof(ARGS *), argv_comp);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1994-08-16 23:36:45 +00:00
|
|
|
/*
|
|
|
|
* argv_fexp --
|
|
|
|
* Do file name and bang command expansion.
|
|
|
|
*/
|
|
|
|
static int
|
2013-08-11 09:44:58 +00:00
|
|
|
argv_fexp(SCR *sp, EXCMD *excp, CHAR_T *cmd, size_t cmdlen, CHAR_T *p, size_t *lenp, CHAR_T **bpp, size_t *blenp, int is_bang)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
|
|
|
EX_PRIVATE *exp;
|
2013-08-11 09:44:58 +00:00
|
|
|
char *t;
|
1996-11-01 06:45:43 +00:00
|
|
|
size_t blen, len, off, tlen;
|
2013-08-11 09:44:58 +00:00
|
|
|
CHAR_T *bp;
|
|
|
|
CHAR_T *wp;
|
|
|
|
size_t wlen;
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
/* Replace file name characters. */
|
|
|
|
for (bp = *bpp, blen = *blenp, len = *lenp; cmdlen > 0; --cmdlen, ++cmd)
|
|
|
|
switch (*cmd) {
|
|
|
|
case '!':
|
|
|
|
if (!is_bang)
|
|
|
|
goto ins_ch;
|
|
|
|
exp = EXP(sp);
|
|
|
|
if (exp->lastbcomm == NULL) {
|
|
|
|
msgq(sp, M_ERR,
|
1996-11-01 06:45:43 +00:00
|
|
|
"115|No previous command to replace \"!\"");
|
1994-08-16 23:36:45 +00:00
|
|
|
return (1);
|
|
|
|
}
|
2013-08-11 09:44:58 +00:00
|
|
|
len += tlen = STRLEN(exp->lastbcomm);
|
1996-11-01 06:45:43 +00:00
|
|
|
off = p - bp;
|
2013-08-11 09:44:58 +00:00
|
|
|
ADD_SPACE_RETW(sp, bp, blen, len);
|
1996-11-01 06:45:43 +00:00
|
|
|
p = bp + off;
|
2013-08-11 09:44:58 +00:00
|
|
|
MEMCPY(p, exp->lastbcomm, tlen);
|
1994-08-16 23:36:45 +00:00
|
|
|
p += tlen;
|
|
|
|
F_SET(excp, E_MODIFY);
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
if ((t = sp->frp->name) == NULL) {
|
|
|
|
msgq(sp, M_ERR,
|
1996-11-01 06:45:43 +00:00
|
|
|
"116|No filename to substitute for %%");
|
1994-08-16 23:36:45 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
tlen = strlen(t);
|
|
|
|
len += tlen;
|
1996-11-01 06:45:43 +00:00
|
|
|
off = p - bp;
|
2013-08-11 09:44:58 +00:00
|
|
|
ADD_SPACE_RETW(sp, bp, blen, len);
|
1996-11-01 06:45:43 +00:00
|
|
|
p = bp + off;
|
2013-08-11 09:44:58 +00:00
|
|
|
CHAR2INT(sp, t, tlen, wp, wlen);
|
|
|
|
MEMCPY(p, wp, wlen);
|
|
|
|
p += wlen;
|
1994-08-16 23:36:45 +00:00
|
|
|
F_SET(excp, E_MODIFY);
|
|
|
|
break;
|
|
|
|
case '#':
|
|
|
|
if ((t = sp->alt_name) == NULL) {
|
|
|
|
msgq(sp, M_ERR,
|
1996-11-01 06:45:43 +00:00
|
|
|
"117|No filename to substitute for #");
|
1994-08-16 23:36:45 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
len += tlen = strlen(t);
|
1996-11-01 06:45:43 +00:00
|
|
|
off = p - bp;
|
2013-08-11 09:44:58 +00:00
|
|
|
ADD_SPACE_RETW(sp, bp, blen, len);
|
1996-11-01 06:45:43 +00:00
|
|
|
p = bp + off;
|
2013-08-11 09:44:58 +00:00
|
|
|
CHAR2INT(sp, t, tlen, wp, wlen);
|
|
|
|
MEMCPY(p, wp, wlen);
|
|
|
|
p += wlen;
|
1994-08-16 23:36:45 +00:00
|
|
|
F_SET(excp, E_MODIFY);
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
/*
|
|
|
|
* QUOTING NOTE:
|
|
|
|
*
|
|
|
|
* Strip any backslashes that protected the file
|
|
|
|
* expansion characters.
|
|
|
|
*/
|
1996-11-01 06:45:43 +00:00
|
|
|
if (cmdlen > 1 &&
|
|
|
|
(cmd[1] == '%' || cmd[1] == '#' || cmd[1] == '!')) {
|
1994-08-16 23:36:45 +00:00
|
|
|
++cmd;
|
|
|
|
--cmdlen;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
default:
|
|
|
|
ins_ch: ++len;
|
1996-11-01 06:45:43 +00:00
|
|
|
off = p - bp;
|
2013-08-11 09:44:58 +00:00
|
|
|
ADD_SPACE_RETW(sp, bp, blen, len);
|
1996-11-01 06:45:43 +00:00
|
|
|
p = bp + off;
|
1994-08-16 23:36:45 +00:00
|
|
|
*p++ = *cmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Nul termination. */
|
|
|
|
++len;
|
1996-11-01 06:45:43 +00:00
|
|
|
off = p - bp;
|
2013-08-11 09:44:58 +00:00
|
|
|
ADD_SPACE_RETW(sp, bp, blen, len);
|
1996-11-01 06:45:43 +00:00
|
|
|
p = bp + off;
|
1994-08-16 23:36:45 +00:00
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
/* Return the new string length, buffer, buffer length. */
|
|
|
|
*lenp = len - 1;
|
|
|
|
*bpp = bp;
|
|
|
|
*blenp = blen;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* argv_alloc --
|
|
|
|
* Make more space for arguments.
|
|
|
|
*/
|
|
|
|
static int
|
2013-08-11 09:44:58 +00:00
|
|
|
argv_alloc(SCR *sp, size_t len)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
|
|
|
ARGS *ap;
|
|
|
|
EX_PRIVATE *exp;
|
|
|
|
int cnt, off;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate room for another argument, always leaving
|
|
|
|
* enough room for an ARGS structure with a length of 0.
|
|
|
|
*/
|
|
|
|
#define INCREMENT 20
|
|
|
|
exp = EXP(sp);
|
|
|
|
off = exp->argsoff;
|
|
|
|
if (exp->argscnt == 0 || off + 2 >= exp->argscnt - 1) {
|
|
|
|
cnt = exp->argscnt + INCREMENT;
|
|
|
|
REALLOC(sp, exp->args, ARGS **, cnt * sizeof(ARGS *));
|
|
|
|
if (exp->args == NULL) {
|
|
|
|
(void)argv_free(sp);
|
|
|
|
goto mem;
|
|
|
|
}
|
1996-11-01 06:45:43 +00:00
|
|
|
memset(&exp->args[exp->argscnt], 0, INCREMENT * sizeof(ARGS *));
|
1994-08-16 23:36:45 +00:00
|
|
|
exp->argscnt = cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First argument. */
|
|
|
|
if (exp->args[off] == NULL) {
|
2020-09-09 07:57:59 +00:00
|
|
|
CALLOC(sp, exp->args[off], 1, sizeof(ARGS));
|
1994-08-16 23:36:45 +00:00
|
|
|
if (exp->args[off] == NULL)
|
|
|
|
goto mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First argument buffer. */
|
|
|
|
ap = exp->args[off];
|
|
|
|
ap->len = 0;
|
|
|
|
if (ap->blen < len + 1) {
|
|
|
|
ap->blen = len + 1;
|
|
|
|
REALLOC(sp, ap->bp, CHAR_T *, ap->blen * sizeof(CHAR_T));
|
|
|
|
if (ap->bp == NULL) {
|
|
|
|
ap->bp = NULL;
|
|
|
|
ap->blen = 0;
|
|
|
|
F_CLR(ap, A_ALLOCATED);
|
|
|
|
mem: msgq(sp, M_SYSERR, NULL);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
F_SET(ap, A_ALLOCATED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Second argument. */
|
|
|
|
if (exp->args[++off] == NULL) {
|
2020-09-09 07:57:59 +00:00
|
|
|
CALLOC(sp, exp->args[off], 1, sizeof(ARGS));
|
1994-08-16 23:36:45 +00:00
|
|
|
if (exp->args[off] == NULL)
|
|
|
|
goto mem;
|
|
|
|
}
|
|
|
|
/* 0 length serves as end-of-argument marker. */
|
|
|
|
exp->args[off]->len = 0;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* argv_free --
|
|
|
|
* Free up argument structures.
|
1996-11-01 06:45:43 +00:00
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: int argv_free(SCR *);
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
int
|
2013-08-11 09:44:58 +00:00
|
|
|
argv_free(SCR *sp)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
|
|
|
EX_PRIVATE *exp;
|
|
|
|
int off;
|
|
|
|
|
|
|
|
exp = EXP(sp);
|
|
|
|
if (exp->args != NULL) {
|
|
|
|
for (off = 0; off < exp->argscnt; ++off) {
|
|
|
|
if (exp->args[off] == NULL)
|
|
|
|
continue;
|
|
|
|
if (F_ISSET(exp->args[off], A_ALLOCATED))
|
|
|
|
free(exp->args[off]->bp);
|
1996-11-01 06:45:43 +00:00
|
|
|
free(exp->args[off]);
|
1994-08-16 23:36:45 +00:00
|
|
|
}
|
1996-11-01 06:45:43 +00:00
|
|
|
free(exp->args);
|
1994-08-16 23:36:45 +00:00
|
|
|
}
|
|
|
|
exp->args = NULL;
|
|
|
|
exp->argscnt = 0;
|
|
|
|
exp->argsoff = 0;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
/*
|
2013-08-11 09:44:58 +00:00
|
|
|
* argv_flt_path --
|
1996-11-01 06:45:43 +00:00
|
|
|
* Find all file names matching the prefix and append them to the
|
2013-08-11 09:44:58 +00:00
|
|
|
* argument list.
|
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: int argv_flt_path(SCR *, EXCMD *, CHAR_T *, size_t);
|
1996-11-01 06:45:43 +00:00
|
|
|
*/
|
2013-08-11 09:44:58 +00:00
|
|
|
int
|
|
|
|
argv_flt_path(SCR *sp, EXCMD *excp, CHAR_T *path, size_t plen)
|
1996-11-01 06:45:43 +00:00
|
|
|
{
|
|
|
|
struct dirent *dp;
|
|
|
|
DIR *dirp;
|
|
|
|
EX_PRIVATE *exp;
|
|
|
|
int off;
|
|
|
|
size_t dlen, len, nlen;
|
2013-08-11 09:44:58 +00:00
|
|
|
CHAR_T *dname;
|
|
|
|
CHAR_T *p, *np, *n;
|
|
|
|
char *name, *tp, *epd = NULL;
|
|
|
|
CHAR_T *wp;
|
|
|
|
size_t wlen;
|
1996-11-01 06:45:43 +00:00
|
|
|
|
|
|
|
exp = EXP(sp);
|
|
|
|
|
|
|
|
/* Set up the name and length for comparison. */
|
2013-08-11 09:44:58 +00:00
|
|
|
if ((path = v_wstrdup(sp, path, plen)) == NULL)
|
|
|
|
return (1);
|
|
|
|
if ((p = STRRCHR(path, '/')) == NULL) {
|
|
|
|
if (*path == '~') {
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Filter ~user list instead. */
|
|
|
|
rc = argv_flt_user(sp, excp, path, plen);
|
|
|
|
free(path);
|
|
|
|
return (rc);
|
|
|
|
}
|
|
|
|
dname = L(".");
|
1996-11-01 06:45:43 +00:00
|
|
|
dlen = 0;
|
2013-08-11 09:44:58 +00:00
|
|
|
np = path;
|
|
|
|
} else {
|
1996-11-01 06:45:43 +00:00
|
|
|
if (p == path) {
|
2013-08-11 09:44:58 +00:00
|
|
|
dname = L("/");
|
1996-11-01 06:45:43 +00:00
|
|
|
dlen = 1;
|
|
|
|
} else {
|
|
|
|
*p = '\0';
|
|
|
|
dname = path;
|
2013-08-11 09:44:58 +00:00
|
|
|
dlen = p - path;
|
1996-11-01 06:45:43 +00:00
|
|
|
}
|
2013-08-11 09:44:58 +00:00
|
|
|
np = p + 1;
|
1996-11-01 06:45:43 +00:00
|
|
|
}
|
|
|
|
|
2013-08-11 09:44:58 +00:00
|
|
|
INT2CHAR(sp, dname, dlen + 1, tp, nlen);
|
|
|
|
if ((epd = expanduser(tp)) != NULL)
|
|
|
|
tp = epd;
|
|
|
|
if ((dirp = opendir(tp)) == NULL) {
|
|
|
|
free(epd);
|
|
|
|
free(path);
|
1996-11-01 06:45:43 +00:00
|
|
|
return (1);
|
|
|
|
}
|
2013-08-11 09:44:58 +00:00
|
|
|
free(epd);
|
|
|
|
|
|
|
|
INT2CHAR(sp, np, STRLEN(np), tp, nlen);
|
|
|
|
if ((name = v_strdup(sp, tp, nlen)) == NULL) {
|
|
|
|
free(path);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
for (off = exp->argsoff; (dp = readdir(dirp)) != NULL;) {
|
|
|
|
if (nlen == 0) {
|
|
|
|
if (dp->d_name[0] == '.')
|
|
|
|
continue;
|
2020-10-01 04:25:54 +00:00
|
|
|
#ifdef HAVE_DIRENT_D_NAMLEN
|
2013-08-11 09:44:58 +00:00
|
|
|
len = dp->d_namlen;
|
2020-10-01 04:25:54 +00:00
|
|
|
#else
|
|
|
|
len = strlen(dp->d_name);
|
|
|
|
#endif
|
1996-11-01 06:45:43 +00:00
|
|
|
} else {
|
2020-10-01 04:25:54 +00:00
|
|
|
#ifdef HAVE_DIRENT_D_NAMLEN
|
2013-08-11 09:44:58 +00:00
|
|
|
len = dp->d_namlen;
|
2020-10-01 04:25:54 +00:00
|
|
|
#else
|
|
|
|
len = strlen(dp->d_name);
|
|
|
|
#endif
|
1996-11-01 06:45:43 +00:00
|
|
|
if (len < nlen || memcmp(dp->d_name, name, nlen))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Directory + name + slash + null. */
|
2013-08-11 09:44:58 +00:00
|
|
|
CHAR2INT(sp, dp->d_name, len + 1, wp, wlen);
|
|
|
|
argv_alloc(sp, dlen + wlen + 1);
|
|
|
|
n = exp->args[exp->argsoff]->bp;
|
1996-11-01 06:45:43 +00:00
|
|
|
if (dlen != 0) {
|
2013-08-11 09:44:58 +00:00
|
|
|
MEMCPY(n, dname, dlen);
|
|
|
|
n += dlen;
|
1996-11-01 06:45:43 +00:00
|
|
|
if (dlen > 1 || dname[0] != '/')
|
2013-08-11 09:44:58 +00:00
|
|
|
*n++ = '/';
|
|
|
|
exp->args[exp->argsoff]->len = dlen + 1;
|
1996-11-01 06:45:43 +00:00
|
|
|
}
|
2013-08-11 09:44:58 +00:00
|
|
|
MEMCPY(n, wp, wlen);
|
|
|
|
exp->args[exp->argsoff]->len += wlen - 1;
|
1996-11-01 06:45:43 +00:00
|
|
|
++exp->argsoff;
|
|
|
|
excp->argv = exp->args;
|
|
|
|
excp->argc = exp->argsoff;
|
|
|
|
}
|
|
|
|
closedir(dirp);
|
2013-08-11 09:44:58 +00:00
|
|
|
free(name);
|
|
|
|
free(path);
|
1996-11-01 06:45:43 +00:00
|
|
|
|
|
|
|
qsort(exp->args + off, exp->argsoff - off, sizeof(ARGS *), argv_comp);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* argv_comp --
|
|
|
|
* Alphabetic comparison.
|
|
|
|
*/
|
|
|
|
static int
|
2013-08-11 09:44:58 +00:00
|
|
|
argv_comp(const void *a, const void *b)
|
1996-11-01 06:45:43 +00:00
|
|
|
{
|
2013-08-11 09:44:58 +00:00
|
|
|
return (STRCMP((*(ARGS **)a)->bp, (*(ARGS **)b)->bp));
|
1996-11-01 06:45:43 +00:00
|
|
|
}
|
|
|
|
|
1994-08-16 23:36:45 +00:00
|
|
|
/*
|
|
|
|
* argv_sexp --
|
|
|
|
* Fork a shell, pipe a command through it, and read the output into
|
|
|
|
* a buffer.
|
|
|
|
*/
|
|
|
|
static int
|
2013-08-11 09:44:58 +00:00
|
|
|
argv_sexp(SCR *sp, CHAR_T **bpp, size_t *blenp, size_t *lenp)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
1996-11-01 06:45:43 +00:00
|
|
|
enum { SEXP_ERR, SEXP_EXPANSION_ERR, SEXP_OK } rval;
|
1994-08-16 23:36:45 +00:00
|
|
|
FILE *ifp;
|
|
|
|
pid_t pid;
|
|
|
|
size_t blen, len;
|
1996-11-01 06:45:43 +00:00
|
|
|
int ch, std_output[2];
|
2013-08-11 09:44:58 +00:00
|
|
|
CHAR_T *bp, *p;
|
|
|
|
char *sh, *sh_path;
|
|
|
|
char *np;
|
|
|
|
size_t nlen;
|
1994-08-16 23:36:45 +00:00
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
/* Secure means no shell access. */
|
|
|
|
if (O_ISSET(sp, O_SECURE)) {
|
|
|
|
msgq(sp, M_ERR,
|
|
|
|
"289|Shell expansions not supported when the secure edit option is set");
|
|
|
|
return (1);
|
|
|
|
}
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
sh_path = O_STR(sp, O_SHELL);
|
|
|
|
if ((sh = strrchr(sh_path, '/')) == NULL)
|
|
|
|
sh = sh_path;
|
|
|
|
else
|
|
|
|
++sh;
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
/* Local copies of the buffer variables. */
|
|
|
|
bp = *bpp;
|
|
|
|
blen = *blenp;
|
|
|
|
|
1994-08-16 23:36:45 +00:00
|
|
|
/*
|
1996-11-01 06:45:43 +00:00
|
|
|
* There are two different processes running through this code, named
|
|
|
|
* the utility (the shell) and the parent. The utility reads standard
|
|
|
|
* input and writes standard output and standard error output. The
|
|
|
|
* parent writes to the utility, reads its standard output and ignores
|
|
|
|
* its standard error output. Historically, the standard error output
|
|
|
|
* was discarded by vi, as it produces a lot of noise when file patterns
|
|
|
|
* don't match.
|
|
|
|
*
|
|
|
|
* The parent reads std_output[0], and the utility writes std_output[1].
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
1996-11-01 06:45:43 +00:00
|
|
|
ifp = NULL;
|
|
|
|
std_output[0] = std_output[1] = -1;
|
|
|
|
if (pipe(std_output) < 0) {
|
1994-08-16 23:36:45 +00:00
|
|
|
msgq(sp, M_SYSERR, "pipe");
|
|
|
|
return (1);
|
|
|
|
}
|
1996-11-01 06:45:43 +00:00
|
|
|
if ((ifp = fdopen(std_output[0], "r")) == NULL) {
|
1994-08-16 23:36:45 +00:00
|
|
|
msgq(sp, M_SYSERR, "fdopen");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-11-01 06:45:43 +00:00
|
|
|
* Do the minimal amount of work possible, the shell is going to run
|
|
|
|
* briefly and then exit. We sincerely hope.
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
switch (pid = vfork()) {
|
|
|
|
case -1: /* Error. */
|
|
|
|
msgq(sp, M_SYSERR, "vfork");
|
1996-11-01 06:45:43 +00:00
|
|
|
err: if (ifp != NULL)
|
|
|
|
(void)fclose(ifp);
|
|
|
|
else if (std_output[0] != -1)
|
|
|
|
close(std_output[0]);
|
|
|
|
if (std_output[1] != -1)
|
|
|
|
close(std_output[0]);
|
1994-08-16 23:36:45 +00:00
|
|
|
return (1);
|
|
|
|
case 0: /* Utility. */
|
1996-11-01 06:45:43 +00:00
|
|
|
/* Redirect stdout to the write end of the pipe. */
|
|
|
|
(void)dup2(std_output[1], STDOUT_FILENO);
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
/* Close the utility's file descriptors. */
|
1996-11-01 06:45:43 +00:00
|
|
|
(void)close(std_output[0]);
|
|
|
|
(void)close(std_output[1]);
|
|
|
|
(void)close(STDERR_FILENO);
|
1994-08-16 23:36:45 +00:00
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
/*
|
|
|
|
* XXX
|
|
|
|
* Assume that all shells have -c.
|
|
|
|
*/
|
2013-08-11 09:44:58 +00:00
|
|
|
INT2CHAR(sp, bp, STRLEN(bp)+1, np, nlen);
|
|
|
|
execl(sh_path, sh, "-c", np, (char *)NULL);
|
1996-11-01 06:45:43 +00:00
|
|
|
msgq_str(sp, M_SYSERR, sh_path, "118|Error: execl: %s");
|
1994-08-16 23:36:45 +00:00
|
|
|
_exit(127);
|
|
|
|
default: /* Parent. */
|
1996-11-01 06:45:43 +00:00
|
|
|
/* Close the pipe ends the parent won't use. */
|
|
|
|
(void)close(std_output[1]);
|
1994-08-16 23:36:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-11-01 06:45:43 +00:00
|
|
|
* Copy process standard output into a buffer.
|
1994-08-16 23:36:45 +00:00
|
|
|
*
|
|
|
|
* !!!
|
|
|
|
* Historic vi apparently discarded leading \n and \r's from
|
|
|
|
* the shell output stream. We don't on the grounds that any
|
|
|
|
* shell that does that is broken.
|
|
|
|
*/
|
|
|
|
for (p = bp, len = 0, ch = EOF;
|
2013-08-11 09:44:58 +00:00
|
|
|
(ch = GETC(ifp)) != EOF; *p++ = ch, blen-=sizeof(CHAR_T), ++len)
|
1994-08-16 23:36:45 +00:00
|
|
|
if (blen < 5) {
|
2013-08-11 09:44:58 +00:00
|
|
|
ADD_SPACE_GOTOW(sp, bp, *blenp, *blenp * 2);
|
1994-08-16 23:36:45 +00:00
|
|
|
p = bp + len;
|
|
|
|
blen = *blenp - len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete the final newline, nul terminate the string. */
|
|
|
|
if (p > bp && (p[-1] == '\n' || p[-1] == '\r')) {
|
1996-11-01 06:45:43 +00:00
|
|
|
--p;
|
1994-08-16 23:36:45 +00:00
|
|
|
--len;
|
1996-11-01 06:45:43 +00:00
|
|
|
}
|
|
|
|
*p = '\0';
|
1994-08-16 23:36:45 +00:00
|
|
|
*lenp = len;
|
1996-11-01 06:45:43 +00:00
|
|
|
*bpp = bp; /* *blenp is already updated. */
|
1994-08-16 23:36:45 +00:00
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
if (ferror(ifp))
|
|
|
|
goto ioerr;
|
|
|
|
if (fclose(ifp)) {
|
|
|
|
ioerr: msgq_str(sp, M_ERR, sh, "119|I/O error: %s");
|
|
|
|
alloc_err: rval = SEXP_ERR;
|
|
|
|
} else
|
|
|
|
rval = SEXP_OK;
|
1994-08-16 23:36:45 +00:00
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
/*
|
|
|
|
* Wait for the process. If the shell process fails (e.g., "echo $q"
|
|
|
|
* where q wasn't a defined variable) or if the returned string has
|
|
|
|
* no characters or only blank characters, (e.g., "echo $5"), complain
|
|
|
|
* that the shell expansion failed. We can't know for certain that's
|
|
|
|
* the error, but it's a good guess, and it matches historic practice.
|
|
|
|
* This won't catch "echo foo_$5", but that's not a common error and
|
|
|
|
* historic vi didn't catch it either.
|
|
|
|
*/
|
|
|
|
if (proc_wait(sp, (long)pid, sh, 1, 0))
|
|
|
|
rval = SEXP_EXPANSION_ERR;
|
|
|
|
|
|
|
|
for (p = bp; len; ++p, --len)
|
2013-08-11 09:44:58 +00:00
|
|
|
if (!cmdskip(*p))
|
1996-11-01 06:45:43 +00:00
|
|
|
break;
|
|
|
|
if (len == 0)
|
|
|
|
rval = SEXP_EXPANSION_ERR;
|
|
|
|
|
|
|
|
if (rval == SEXP_EXPANSION_ERR)
|
|
|
|
msgq(sp, M_ERR, "304|Shell expansion failed");
|
1994-08-16 23:36:45 +00:00
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
return (rval == SEXP_OK ? 0 : 1);
|
1994-08-16 23:36:45 +00:00
|
|
|
}
|
2013-08-11 09:44:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* argv_esc --
|
|
|
|
* Escape a string into an ex and shell argument.
|
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: CHAR_T *argv_esc(SCR *, EXCMD *, CHAR_T *, size_t);
|
2013-08-11 09:44:58 +00:00
|
|
|
*/
|
|
|
|
CHAR_T *
|
|
|
|
argv_esc(SCR *sp, EXCMD *excp, CHAR_T *str, size_t len)
|
|
|
|
{
|
|
|
|
size_t blen, off;
|
|
|
|
CHAR_T *bp, *p;
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
GET_SPACE_GOTOW(sp, bp, blen, len + 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Leaving the first '~' unescaped causes the user to need a
|
|
|
|
* "./" prefix to edit a file which really starts with a '~'.
|
|
|
|
* However, the file completion happens to not work for these
|
|
|
|
* files without the prefix.
|
|
|
|
*
|
|
|
|
* All ex expansion characters, "!%#", are double escaped.
|
|
|
|
*/
|
|
|
|
for (p = bp; len > 0; ++str, --len) {
|
|
|
|
ch = *str;
|
|
|
|
off = p - bp;
|
|
|
|
if (blen / sizeof(CHAR_T) - off < 3) {
|
|
|
|
ADD_SPACE_GOTOW(sp, bp, blen, off + 3);
|
|
|
|
p = bp + off;
|
|
|
|
}
|
|
|
|
if (cmdskip(ch) || ch == '\n' ||
|
|
|
|
IS_ESCAPE(sp, excp, ch)) /* Ex. */
|
|
|
|
*p++ = CH_LITERAL;
|
|
|
|
else switch (ch) {
|
|
|
|
case '~': /* ~user. */
|
|
|
|
if (p != bp)
|
|
|
|
*p++ = '\\';
|
|
|
|
break;
|
|
|
|
case '+': /* Ex +cmd. */
|
|
|
|
if (p == bp)
|
|
|
|
*p++ = '\\';
|
|
|
|
break;
|
|
|
|
case '!': case '%': case '#': /* Ex exp. */
|
|
|
|
*p++ = '\\';
|
|
|
|
*p++ = '\\';
|
|
|
|
break;
|
|
|
|
case ',': case '-': case '.': case '/': /* Safe. */
|
|
|
|
case ':': case '=': case '@': case '_':
|
|
|
|
break;
|
|
|
|
default: /* Unsafe. */
|
|
|
|
if (isascii(ch) && !isalnum(ch))
|
|
|
|
*p++ = '\\';
|
|
|
|
}
|
|
|
|
*p++ = ch;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
return bp;
|
|
|
|
|
|
|
|
alloc_err:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* argv_uesc --
|
|
|
|
* Unescape an escaped ex and shell argument.
|
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: CHAR_T *argv_uesc(SCR *, EXCMD *, CHAR_T *, size_t);
|
2013-08-11 09:44:58 +00:00
|
|
|
*/
|
|
|
|
CHAR_T *
|
|
|
|
argv_uesc(SCR *sp, EXCMD *excp, CHAR_T *str, size_t len)
|
|
|
|
{
|
|
|
|
size_t blen;
|
|
|
|
CHAR_T *bp, *p;
|
|
|
|
|
|
|
|
GET_SPACE_GOTOW(sp, bp, blen, len + 1);
|
|
|
|
|
|
|
|
for (p = bp; len > 0; ++str, --len) {
|
|
|
|
if (IS_ESCAPE(sp, excp, *str)) {
|
|
|
|
if (--len < 1)
|
|
|
|
break;
|
|
|
|
++str;
|
|
|
|
} else if (*str == '\\') {
|
|
|
|
if (--len < 1)
|
|
|
|
break;
|
|
|
|
++str;
|
|
|
|
|
|
|
|
/* Check for double escaping. */
|
|
|
|
if (*str == '\\' && len > 1)
|
|
|
|
switch (str[1]) {
|
|
|
|
case '!': case '%': case '#':
|
|
|
|
++str;
|
|
|
|
--len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*p++ = *str;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
return bp;
|
|
|
|
|
|
|
|
alloc_err:
|
|
|
|
return NULL;
|
|
|
|
}
|