slightly optimised and, more importantly, much more consistent (use only
one set of CTRL, UNCTRL, and new ISCTRL macros) C0 and DEL handling; the
optimisation only works for 7-bit ASCII, so those places 8-bit must pass
intact have an added check
also, while here, remove an editor oops (‘;’), oksh rcsid sync (they did
accept I was right wrt. set -e), int → bool, and code merge/cleanup
2013-09-24 22:19:45 +02:00
|
|
|
/* $OpenBSD: eval.c,v 1.40 2013/09/14 20:09:30 millert Exp $ */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-05-16 18:59:42 +02:00
|
|
|
/*-
|
2012-03-24 00:25:30 +01:00
|
|
|
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
|
2017-02-17 21:52:15 +01:00
|
|
|
* 2011, 2012, 2013, 2014, 2015, 2016, 2017
|
2015-10-09 19:48:53 +02:00
|
|
|
* mirabilos <m@mirbsd.org>
|
2009-05-16 18:59:42 +02:00
|
|
|
*
|
|
|
|
* Provided that these terms and disclaimer and all copyright notices
|
|
|
|
* are retained or reproduced in an accompanying document, permission
|
|
|
|
* is granted to deal in this work without restriction, including un-
|
|
|
|
* limited rights to use, publicly perform, distribute, sell, modify,
|
|
|
|
* merge, give away, or sublicence.
|
|
|
|
*
|
|
|
|
* This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
|
|
|
|
* the utmost extent permitted by applicable law, neither express nor
|
|
|
|
* implied; without malicious intent or gross negligence. In no event
|
|
|
|
* may a licensor, author or contributor be held liable for indirect,
|
|
|
|
* direct, other damage, loss, or other issues arising in any way out
|
|
|
|
* of dealing in the work, even if advised of the possibility of such
|
|
|
|
* damage or existence of a defect, except proven that it results out
|
|
|
|
* of said person's immediate fault when using the work as intended.
|
|
|
|
*/
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
#include "sh.h"
|
|
|
|
|
2017-05-06 00:53:32 +02:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/eval.c,v 1.214 2017/05/05 22:53:27 tg Exp $");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* string expansion
|
|
|
|
*
|
|
|
|
* first pass: quoting, IFS separation, ~, ${}, $() and $(()) substitution.
|
|
|
|
* second pass: alternation ({,}), filename expansion (*?[]).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* expansion generator state */
|
2013-02-23 21:03:31 +01:00
|
|
|
typedef struct {
|
|
|
|
/* not including an "int type;" member, see expand() */
|
|
|
|
/* string */
|
|
|
|
const char *str;
|
|
|
|
/* source */
|
2005-05-23 05:06:10 +02:00
|
|
|
union {
|
2013-02-23 21:03:31 +01:00
|
|
|
/* string[] */
|
|
|
|
const char **strv;
|
|
|
|
/* file */
|
|
|
|
struct shf *shf;
|
|
|
|
} u;
|
|
|
|
/* variable in ${var...} */
|
|
|
|
struct tbl *var;
|
|
|
|
/* split "$@" / call waitlast in $() */
|
|
|
|
bool split;
|
2005-05-23 05:06:10 +02:00
|
|
|
} Expand;
|
|
|
|
|
|
|
|
#define XBASE 0 /* scanning original */
|
|
|
|
#define XSUB 1 /* expanding ${} string */
|
|
|
|
#define XARGSEP 2 /* ifs0 between "$*" */
|
|
|
|
#define XARG 3 /* expanding $*, $@ */
|
|
|
|
#define XCOM 4 /* expanding $() */
|
|
|
|
#define XNULLSUB 5 /* "$@" when $# is 0 (don't generate word) */
|
|
|
|
#define XSUBMID 6 /* middle of expanding ${} */
|
|
|
|
|
|
|
|
/* States used for field splitting */
|
2015-02-19 23:01:13 +01:00
|
|
|
#define IFS_WORD 0 /* word has chars (or quotes except "$@") */
|
2005-05-23 05:06:10 +02:00
|
|
|
#define IFS_WS 1 /* have seen IFS white-space */
|
|
|
|
#define IFS_NWS 2 /* have seen IFS non-white-space */
|
2016-09-01 14:59:12 +02:00
|
|
|
#define IFS_IWS 3 /* beginning of word, ignore IFS WS */
|
2015-02-19 23:01:13 +01:00
|
|
|
#define IFS_QUOTE 4 /* beg.w/quote, become IFS_WORD unless "$@" */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2017-05-03 17:36:12 +02:00
|
|
|
#define STYPE_CHAR 0xFF
|
|
|
|
#define STYPE_DBL 0x100
|
|
|
|
#define STYPE_AT 0x200
|
|
|
|
#define STYPE_SINGLE 0x2FF
|
|
|
|
#define STYPE_MASK 0x300
|
2017-05-03 17:33:16 +02:00
|
|
|
|
2007-03-04 01:13:17 +01:00
|
|
|
static int varsub(Expand *, const char *, const char *, int *, int *);
|
2012-10-22 22:19:18 +02:00
|
|
|
static int comsub(Expand *, const char *, int);
|
2013-05-02 23:59:54 +02:00
|
|
|
static char *valsub(struct op *, Area *);
|
2006-05-08 13:07:38 +02:00
|
|
|
static char *trimsub(char *, char *, int);
|
2012-08-24 21:02:57 +02:00
|
|
|
static void glob(char *, XPtrV *, bool);
|
2006-05-08 13:07:38 +02:00
|
|
|
static void globit(XString *, char **, char *, XPtrV *, int);
|
2014-12-05 16:14:23 +01:00
|
|
|
static const char *maybe_expand_tilde(const char *, XString *, char **, bool);
|
2007-01-17 19:01:51 +01:00
|
|
|
#ifndef MKSH_NOPWNAM
|
2006-05-08 13:07:38 +02:00
|
|
|
static char *homedir(char *);
|
2007-01-11 01:32:31 +01:00
|
|
|
#endif
|
2006-05-08 13:07:38 +02:00
|
|
|
static void alt_expand(XPtrV *, char *, char *, char *, int);
|
2014-01-05 22:57:29 +01:00
|
|
|
static int utflen(const char *) MKSH_A_PURE;
|
2009-05-16 17:53:02 +02:00
|
|
|
static void utfincptr(const char *, mksh_ari_t *);
|
|
|
|
|
|
|
|
/* UTFMODE functions */
|
2011-08-27 20:06:52 +02:00
|
|
|
static int
|
2009-05-16 17:53:02 +02:00
|
|
|
utflen(const char *s)
|
|
|
|
{
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
if (UTFMODE) {
|
|
|
|
n = 0;
|
|
|
|
while (*s) {
|
|
|
|
s += utf_ptradj(s);
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
n = strlen(s);
|
2011-08-27 20:06:52 +02:00
|
|
|
|
|
|
|
if (n > 2147483647)
|
|
|
|
n = 2147483647;
|
|
|
|
return ((int)n);
|
2009-05-16 17:53:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
utfincptr(const char *s, mksh_ari_t *lp)
|
|
|
|
{
|
|
|
|
const char *cp = s;
|
|
|
|
|
|
|
|
while ((*lp)--)
|
|
|
|
cp += utf_ptradj(cp);
|
|
|
|
*lp = cp - s;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* compile and expand word */
|
|
|
|
char *
|
|
|
|
substitute(const char *cp, int f)
|
|
|
|
{
|
|
|
|
struct source *s, *sold;
|
|
|
|
|
|
|
|
sold = source;
|
|
|
|
s = pushs(SWSTR, ATEMP);
|
|
|
|
s->start = s->str = cp;
|
|
|
|
source = s;
|
|
|
|
if (yylex(ONEWORD) != LWORD)
|
2016-01-21 19:24:45 +01:00
|
|
|
internal_errorf(Tbadsubst);
|
2005-05-23 05:06:10 +02:00
|
|
|
source = sold;
|
|
|
|
afree(s, ATEMP);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (evalstr(yylval.cp, f));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* expand arg-list
|
|
|
|
*/
|
|
|
|
char **
|
2007-03-04 01:13:17 +01:00
|
|
|
eval(const char **ap, int f)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
XPtrV w;
|
|
|
|
|
2007-03-04 01:13:17 +01:00
|
|
|
if (*ap == NULL) {
|
|
|
|
union mksh_ccphack vap;
|
|
|
|
|
|
|
|
vap.ro = ap;
|
|
|
|
return (vap.rw);
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
XPinit(w, 32);
|
2011-01-21 23:25:34 +01:00
|
|
|
/* space for shell name */
|
|
|
|
XPput(w, NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
while (*ap != NULL)
|
|
|
|
expand(*ap++, &w, f);
|
|
|
|
XPput(w, NULL);
|
2009-06-08 22:06:50 +02:00
|
|
|
return ((char **)XPclose(w) + 1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* expand string
|
|
|
|
*/
|
|
|
|
char *
|
2007-03-04 01:13:17 +01:00
|
|
|
evalstr(const char *cp, int f)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
XPtrV w;
|
2007-07-31 12:42:15 +02:00
|
|
|
char *dp = null;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
XPinit(w, 1);
|
|
|
|
expand(cp, &w, f);
|
2007-07-31 12:42:15 +02:00
|
|
|
if (XPsize(w))
|
|
|
|
dp = *XPptrv(w);
|
2005-05-23 05:06:10 +02:00
|
|
|
XPfree(w);
|
2007-03-04 01:13:17 +01:00
|
|
|
return (dp);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* expand string - return only one component
|
|
|
|
* used from iosetup to expand redirection files
|
|
|
|
*/
|
|
|
|
char *
|
2007-03-04 01:13:17 +01:00
|
|
|
evalonestr(const char *cp, int f)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
XPtrV w;
|
2007-03-04 01:13:17 +01:00
|
|
|
char *rv;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
XPinit(w, 1);
|
|
|
|
expand(cp, &w, f);
|
|
|
|
switch (XPsize(w)) {
|
|
|
|
case 0:
|
2007-03-04 01:13:17 +01:00
|
|
|
rv = null;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case 1:
|
2007-03-04 01:13:17 +01:00
|
|
|
rv = (char *) *XPptrv(w);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
default:
|
2016-08-01 23:38:07 +02:00
|
|
|
rv = evalstr(cp, f & ~DOGLOB);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
XPfree(w);
|
2007-03-04 01:13:17 +01:00
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* for nested substitution: ${var:=$var2} */
|
|
|
|
typedef struct SubType {
|
2009-04-07 20:41:37 +02:00
|
|
|
struct tbl *var; /* variable for ${var..} */
|
|
|
|
struct SubType *prev; /* old type */
|
|
|
|
struct SubType *next; /* poped type (to avoid re-allocating) */
|
2016-09-01 14:59:12 +02:00
|
|
|
size_t base; /* start position of expanded word */
|
2005-05-23 05:06:10 +02:00
|
|
|
short stype; /* [=+-?%#] action after expanded word */
|
|
|
|
short f; /* saved value of f (DOPAT, etc) */
|
2010-04-09 21:16:31 +02:00
|
|
|
uint8_t quotep; /* saved value of quote (for ${..[%#]..}) */
|
|
|
|
uint8_t quotew; /* saved value of quote (for ${..[+-=]..}) */
|
2005-05-23 05:06:10 +02:00
|
|
|
} SubType;
|
|
|
|
|
|
|
|
void
|
2013-02-23 21:03:31 +01:00
|
|
|
expand(
|
|
|
|
/* input word */
|
|
|
|
const char *ccp,
|
|
|
|
/* output words */
|
|
|
|
XPtrV *wp,
|
|
|
|
/* DO* flags */
|
|
|
|
int f)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
int c = 0;
|
2013-02-23 21:03:31 +01:00
|
|
|
/* expansion type */
|
|
|
|
int type;
|
|
|
|
/* quoted */
|
|
|
|
int quote = 0;
|
|
|
|
/* destination string and live pointer */
|
|
|
|
XString ds;
|
|
|
|
char *dp;
|
|
|
|
/* source */
|
|
|
|
const char *sp;
|
|
|
|
/* second pass flags */
|
|
|
|
int fdo;
|
|
|
|
/* have word */
|
|
|
|
int word;
|
|
|
|
/* field splitting of parameter/command substitution */
|
|
|
|
int doblank;
|
|
|
|
/* expansion variables */
|
2011-01-21 23:25:34 +01:00
|
|
|
Expand x = {
|
2005-07-12 15:30:38 +02:00
|
|
|
NULL, { NULL }, NULL, 0
|
|
|
|
};
|
2005-05-23 05:06:10 +02:00
|
|
|
SubType st_head, *st;
|
2013-02-23 21:03:31 +01:00
|
|
|
/* record number of trailing newlines in COMSUB */
|
2011-01-21 23:25:34 +01:00
|
|
|
int newlines = 0;
|
2012-07-30 19:28:21 +02:00
|
|
|
bool saw_eq, make_magic;
|
2014-11-25 22:13:31 +01:00
|
|
|
unsigned int tilde_ok;
|
2005-05-23 05:06:10 +02:00
|
|
|
size_t len;
|
2013-02-23 21:03:31 +01:00
|
|
|
char *cp;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2013-02-23 21:03:31 +01:00
|
|
|
if (ccp == NULL)
|
2007-05-13 19:51:24 +02:00
|
|
|
internal_errorf("expand(NULL)");
|
2005-05-23 05:06:10 +02:00
|
|
|
/* for alias, readonly, set, typeset commands */
|
2013-02-23 21:03:31 +01:00
|
|
|
if ((f & DOVACHECK) && is_wdvarassign(ccp)) {
|
2014-10-03 19:32:12 +02:00
|
|
|
f &= ~(DOVACHECK | DOBLANK | DOGLOB | DOTILDE);
|
2014-10-19 23:53:08 +02:00
|
|
|
f |= DOASNTILDE | DOSCALAR;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (Flag(FNOGLOB))
|
|
|
|
f &= ~DOGLOB;
|
|
|
|
if (Flag(FMARKDIRS))
|
|
|
|
f |= DOMARKDIRS;
|
|
|
|
if (Flag(FBRACEEXPAND) && (f & DOGLOB))
|
2011-09-07 17:24:22 +02:00
|
|
|
f |= DOBRACE;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-01-21 23:25:34 +01:00
|
|
|
/* init destination string */
|
|
|
|
Xinit(ds, dp, 128, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
type = XBASE;
|
2013-02-23 21:03:31 +01:00
|
|
|
sp = ccp;
|
2005-05-23 05:06:10 +02:00
|
|
|
fdo = 0;
|
2012-07-30 19:28:21 +02:00
|
|
|
saw_eq = false;
|
2011-01-21 23:25:34 +01:00
|
|
|
/* must be 1/0 */
|
2014-10-03 19:32:12 +02:00
|
|
|
tilde_ok = (f & (DOTILDE | DOASNTILDE)) ? 1 : 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
doblank = 0;
|
2012-07-30 19:28:21 +02:00
|
|
|
make_magic = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
word = (f&DOBLANK) ? IFS_WS : IFS_WORD;
|
2009-12-05 18:43:50 +01:00
|
|
|
/* clang doesn't know OSUBST comes before CSUBST */
|
|
|
|
memset(&st_head, 0, sizeof(st_head));
|
2005-05-23 05:06:10 +02:00
|
|
|
st = &st_head;
|
|
|
|
|
2011-03-13 02:20:25 +01:00
|
|
|
while (/* CONSTCOND */ 1) {
|
2005-05-23 05:06:10 +02:00
|
|
|
Xcheck(ds, dp);
|
|
|
|
|
|
|
|
switch (type) {
|
2011-01-21 23:25:34 +01:00
|
|
|
case XBASE:
|
|
|
|
/* original prefixed string */
|
2017-05-06 00:53:32 +02:00
|
|
|
c = ord(*sp++);
|
2005-05-23 05:06:10 +02:00
|
|
|
switch (c) {
|
|
|
|
case EOS:
|
|
|
|
c = 0;
|
|
|
|
break;
|
|
|
|
case CHAR:
|
2017-05-06 00:53:32 +02:00
|
|
|
c = ord(*sp++);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case QCHAR:
|
2011-01-21 23:25:34 +01:00
|
|
|
/* temporary quote */
|
|
|
|
quote |= 2;
|
2017-05-06 00:53:32 +02:00
|
|
|
c = ord(*sp++);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case OQUOTE:
|
2015-07-06 19:45:33 +02:00
|
|
|
if (word != IFS_WORD)
|
2015-02-20 08:14:30 +01:00
|
|
|
word = IFS_QUOTE;
|
2005-05-23 05:06:10 +02:00
|
|
|
tilde_ok = 0;
|
|
|
|
quote = 1;
|
|
|
|
continue;
|
|
|
|
case CQUOTE:
|
2015-07-06 19:45:33 +02:00
|
|
|
if (word == IFS_QUOTE)
|
|
|
|
word = IFS_WORD;
|
2010-04-09 21:16:31 +02:00
|
|
|
quote = st->quotew;
|
2005-05-23 05:06:10 +02:00
|
|
|
continue;
|
2017-04-06 03:59:58 +02:00
|
|
|
case COMASUB:
|
2005-05-23 05:06:10 +02:00
|
|
|
case COMSUB:
|
2017-04-06 03:59:58 +02:00
|
|
|
case FUNASUB:
|
2012-10-22 22:19:18 +02:00
|
|
|
case FUNSUB:
|
2013-05-02 23:59:54 +02:00
|
|
|
case VALSUB:
|
2005-05-23 05:06:10 +02:00
|
|
|
tilde_ok = 0;
|
|
|
|
if (f & DONTRUNCOMMAND) {
|
|
|
|
word = IFS_WORD;
|
2012-07-30 23:37:17 +02:00
|
|
|
*dp++ = '$';
|
2017-04-06 03:59:58 +02:00
|
|
|
switch (c) {
|
|
|
|
case COMASUB:
|
|
|
|
case COMSUB:
|
|
|
|
*dp++ = '(';
|
2017-05-06 00:53:32 +02:00
|
|
|
c = ord(')');
|
2017-04-06 03:59:58 +02:00
|
|
|
break;
|
|
|
|
case FUNASUB:
|
|
|
|
case FUNSUB:
|
|
|
|
case VALSUB:
|
|
|
|
*dp++ = '{';
|
|
|
|
*dp++ = c == VALSUB ? '|' : ' ';
|
2017-05-06 00:53:32 +02:00
|
|
|
c = ord('}');
|
2017-04-06 03:59:58 +02:00
|
|
|
break;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
while (*sp != '\0') {
|
|
|
|
Xcheck(ds, dp);
|
|
|
|
*dp++ = *sp++;
|
|
|
|
}
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c == ord('}'))
|
2012-10-22 22:19:18 +02:00
|
|
|
*dp++ = ';';
|
2017-04-06 03:59:58 +02:00
|
|
|
*dp++ = c;
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
2012-10-22 22:19:18 +02:00
|
|
|
type = comsub(&x, sp, c);
|
2013-05-02 23:59:54 +02:00
|
|
|
if (type != XBASE && (f & DOBLANK))
|
2005-05-23 05:06:10 +02:00
|
|
|
doblank++;
|
2007-05-13 21:14:05 +02:00
|
|
|
sp = strnul(sp) + 1;
|
2005-05-23 05:06:10 +02:00
|
|
|
newlines = 0;
|
|
|
|
}
|
|
|
|
continue;
|
2011-03-13 00:04:48 +01:00
|
|
|
case EXPRSUB:
|
2005-05-23 05:06:10 +02:00
|
|
|
tilde_ok = 0;
|
|
|
|
if (f & DONTRUNCOMMAND) {
|
2014-06-09 13:16:07 +02:00
|
|
|
word = IFS_WORD;
|
2005-05-23 05:06:10 +02:00
|
|
|
*dp++ = '$'; *dp++ = '('; *dp++ = '(';
|
2011-03-13 00:04:48 +01:00
|
|
|
while (*sp != '\0') {
|
|
|
|
Xcheck(ds, dp);
|
|
|
|
*dp++ = *sp++;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
*dp++ = ')'; *dp++ = ')';
|
|
|
|
} else {
|
|
|
|
struct tbl v;
|
|
|
|
|
|
|
|
v.flag = DEFINED|ISSET|INTEGER;
|
2011-01-21 23:25:34 +01:00
|
|
|
/* not default */
|
|
|
|
v.type = 10;
|
2005-05-23 05:06:10 +02:00
|
|
|
v.name[0] = '\0';
|
2011-03-13 00:04:48 +01:00
|
|
|
v_evaluate(&v, substitute(sp, 0),
|
2005-05-23 05:06:10 +02:00
|
|
|
KSH_UNWIND_ERROR, true);
|
2011-03-13 00:04:48 +01:00
|
|
|
sp = strnul(sp) + 1;
|
2014-06-09 13:16:07 +02:00
|
|
|
x.str = str_val(&v);
|
|
|
|
type = XSUB;
|
|
|
|
if (f & DOBLANK)
|
|
|
|
doblank++;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
continue;
|
2011-01-21 23:25:34 +01:00
|
|
|
case OSUBST: {
|
|
|
|
/* ${{#}var{:}[=+-?#%]word} */
|
|
|
|
/*-
|
|
|
|
* format is:
|
2009-06-10 20:12:51 +02:00
|
|
|
* OSUBST [{x] plain-variable-part \0
|
|
|
|
* compiled-word-part CSUBST [}x]
|
|
|
|
* This is where all syntax checking gets done...
|
|
|
|
*/
|
2011-01-21 23:25:34 +01:00
|
|
|
/* skip the { or x (}) */
|
|
|
|
const char *varname = ++sp;
|
2005-05-23 05:06:10 +02:00
|
|
|
int stype;
|
2005-07-12 15:24:00 +02:00
|
|
|
int slen = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-01-21 23:25:34 +01:00
|
|
|
/* skip variable */
|
|
|
|
sp = cstrchr(sp, '\0') + 1;
|
2005-05-23 05:06:10 +02:00
|
|
|
type = varsub(&x, varname, sp, &stype, &slen);
|
|
|
|
if (type < 0) {
|
2007-03-04 01:13:17 +01:00
|
|
|
char *beg, *end, *str;
|
2007-07-06 04:22:57 +02:00
|
|
|
unwind_substsyn:
|
2011-01-21 23:25:34 +01:00
|
|
|
/* restore sp */
|
|
|
|
sp = varname - 2;
|
2016-02-26 19:05:10 +01:00
|
|
|
beg = wdcopy(sp, ATEMP);
|
|
|
|
end = (wdscan(cstrchr(sp, '\0') + 1,
|
|
|
|
CSUBST) - sp) + beg;
|
2005-05-23 05:06:10 +02:00
|
|
|
/* ({) the } or x is already skipped */
|
2007-03-14 03:41:09 +01:00
|
|
|
if (end < wdscan(beg, EOS))
|
|
|
|
*end = EOS;
|
2016-07-25 02:04:48 +02:00
|
|
|
str = snptreef(NULL, 64, Tf_S, beg);
|
2007-03-04 01:13:17 +01:00
|
|
|
afree(beg, ATEMP);
|
2016-07-25 02:04:48 +02:00
|
|
|
errorf(Tf_sD_s, str, Tbadsubst);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2007-03-04 01:13:17 +01:00
|
|
|
if (f & DOBLANK)
|
2005-05-23 05:06:10 +02:00
|
|
|
doblank++;
|
|
|
|
tilde_ok = 0;
|
2015-02-19 23:01:13 +01:00
|
|
|
if (word == IFS_QUOTE && type != XNULLSUB)
|
|
|
|
word = IFS_WORD;
|
2011-01-21 23:25:34 +01:00
|
|
|
if (type == XBASE) {
|
|
|
|
/* expand? */
|
2005-05-23 05:06:10 +02:00
|
|
|
if (!st->next) {
|
|
|
|
SubType *newst;
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
newst = alloc(sizeof(SubType), ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
newst->next = NULL;
|
|
|
|
newst->prev = st;
|
|
|
|
st->next = newst;
|
|
|
|
}
|
|
|
|
st = st->next;
|
|
|
|
st->stype = stype;
|
|
|
|
st->base = Xsavepos(ds, dp);
|
|
|
|
st->f = f;
|
2016-02-26 19:48:14 +01:00
|
|
|
if (x.var == vtemp) {
|
|
|
|
st->var = tempvar(vtemp->name);
|
2012-06-28 22:03:20 +02:00
|
|
|
st->var->flag &= ~INTEGER;
|
|
|
|
/* can't fail here */
|
|
|
|
setstr(st->var,
|
|
|
|
str_val(x.var),
|
|
|
|
KSH_RETURN_ERROR | 0x4);
|
|
|
|
} else
|
|
|
|
st->var = x.var;
|
|
|
|
|
2010-04-09 21:16:31 +02:00
|
|
|
st->quotew = st->quotep = quote;
|
2005-05-23 05:06:10 +02:00
|
|
|
/* skip qualifier(s) */
|
|
|
|
if (stype)
|
|
|
|
sp += slen;
|
2017-05-03 17:33:16 +02:00
|
|
|
switch (stype & STYPE_SINGLE) {
|
|
|
|
case ord('#') | STYPE_AT:
|
2012-03-29 21:23:01 +02:00
|
|
|
x.str = shf_smprintf("%08X",
|
2014-01-11 19:09:43 +01:00
|
|
|
(unsigned int)hash(str_val(st->var)));
|
2011-01-21 23:25:34 +01:00
|
|
|
break;
|
2017-05-03 17:33:16 +02:00
|
|
|
case ord('Q') | STYPE_AT: {
|
2012-07-21 01:22:13 +02:00
|
|
|
struct shf shf;
|
|
|
|
|
|
|
|
shf_sopen(NULL, 0, SHF_WR|SHF_DYNAMIC, &shf);
|
|
|
|
print_value_quoted(&shf, str_val(st->var));
|
|
|
|
x.str = shf_sclose(&shf);
|
|
|
|
break;
|
2014-10-03 19:32:12 +02:00
|
|
|
}
|
2017-05-03 17:33:16 +02:00
|
|
|
case ord('0'): {
|
2007-07-06 03:53:36 +02:00
|
|
|
char *beg, *mid, *end, *stg;
|
2009-05-16 17:53:02 +02:00
|
|
|
mksh_ari_t from = 0, num = -1, flen, finc = 0;
|
2007-07-06 03:53:36 +02:00
|
|
|
|
|
|
|
beg = wdcopy(sp, ATEMP);
|
|
|
|
mid = beg + (wdscan(sp, ADELIM) - sp);
|
2007-07-06 04:22:57 +02:00
|
|
|
stg = beg + (wdscan(sp, CSUBST) - sp);
|
2007-07-06 03:53:36 +02:00
|
|
|
mid[-2] = EOS;
|
2017-05-06 00:53:32 +02:00
|
|
|
if (ord(mid[-1]) == ord(/*{*/ '}')) {
|
2007-07-06 03:53:36 +02:00
|
|
|
sp += mid - beg - 1;
|
|
|
|
end = NULL;
|
|
|
|
} else {
|
|
|
|
end = mid +
|
|
|
|
(wdscan(mid, ADELIM) - mid);
|
2017-05-06 00:53:32 +02:00
|
|
|
if (ord(end[-1]) != ord(/*{*/ '}'))
|
2016-01-14 23:30:43 +01:00
|
|
|
/* more than max delimiters */
|
2007-07-06 04:22:57 +02:00
|
|
|
goto unwind_substsyn;
|
2007-07-06 03:53:36 +02:00
|
|
|
end[-2] = EOS;
|
|
|
|
sp += end - beg - 1;
|
|
|
|
}
|
2011-05-03 00:52:54 +02:00
|
|
|
evaluate(substitute(stg = wdstrip(beg, 0), 0),
|
2007-07-06 04:22:57 +02:00
|
|
|
&from, KSH_UNWIND_ERROR, true);
|
2007-07-06 03:53:36 +02:00
|
|
|
afree(stg, ATEMP);
|
|
|
|
if (end) {
|
2011-05-03 00:52:54 +02:00
|
|
|
evaluate(substitute(stg = wdstrip(mid, 0), 0),
|
2007-07-06 03:53:36 +02:00
|
|
|
&num, KSH_UNWIND_ERROR, true);
|
|
|
|
afree(stg, ATEMP);
|
|
|
|
}
|
|
|
|
afree(beg, ATEMP);
|
|
|
|
beg = str_val(st->var);
|
2009-05-16 17:53:02 +02:00
|
|
|
flen = utflen(beg);
|
2007-07-06 03:53:36 +02:00
|
|
|
if (from < 0) {
|
|
|
|
if (-from < flen)
|
2009-05-16 17:53:02 +02:00
|
|
|
finc = flen + from;
|
2007-07-06 03:53:36 +02:00
|
|
|
} else
|
2009-05-16 17:53:02 +02:00
|
|
|
finc = from < flen ? from : flen;
|
2009-08-01 21:31:02 +02:00
|
|
|
if (UTFMODE)
|
2009-05-16 17:53:02 +02:00
|
|
|
utfincptr(beg, &finc);
|
|
|
|
beg += finc;
|
|
|
|
flen = utflen(beg);
|
2007-07-06 03:53:36 +02:00
|
|
|
if (num < 0 || num > flen)
|
|
|
|
num = flen;
|
2009-08-01 21:31:02 +02:00
|
|
|
if (UTFMODE)
|
2009-05-16 17:53:02 +02:00
|
|
|
utfincptr(beg, &num);
|
2008-10-28 15:32:43 +01:00
|
|
|
strndupx(x.str, beg, num, ATEMP);
|
2007-07-06 03:53:36 +02:00
|
|
|
goto do_CSUBST;
|
2014-10-03 19:32:12 +02:00
|
|
|
}
|
2017-05-03 17:33:16 +02:00
|
|
|
case ord('/') | STYPE_AT:
|
|
|
|
case ord('/'): {
|
2008-03-01 22:10:26 +01:00
|
|
|
char *s, *p, *d, *sbeg, *end;
|
2016-08-01 23:38:07 +02:00
|
|
|
char *pat = NULL, *rrep = null;
|
2015-12-12 23:24:07 +01:00
|
|
|
char fpat = 0, *tpat1, *tpat2;
|
2016-08-01 23:38:07 +02:00
|
|
|
char *ws, *wpat, *wrep;
|
2008-03-01 22:10:26 +01:00
|
|
|
|
2016-08-01 23:38:07 +02:00
|
|
|
s = ws = wdcopy(sp, ATEMP);
|
2008-03-01 22:10:26 +01:00
|
|
|
p = s + (wdscan(sp, ADELIM) - sp);
|
|
|
|
d = s + (wdscan(sp, CSUBST) - sp);
|
|
|
|
p[-2] = EOS;
|
2017-05-06 00:53:32 +02:00
|
|
|
if (ord(p[-1]) == ord(/*{*/ '}'))
|
2008-03-01 22:10:26 +01:00
|
|
|
d = NULL;
|
|
|
|
else
|
|
|
|
d[-2] = EOS;
|
|
|
|
sp += (d ? d : p) - s - 1;
|
2017-05-03 17:33:16 +02:00
|
|
|
if (!(stype & STYPE_MASK) &&
|
2015-12-12 23:24:07 +01:00
|
|
|
s[0] == CHAR &&
|
2017-04-28 02:38:33 +02:00
|
|
|
ctype(s[1], C_SUB2))
|
2015-12-12 23:24:07 +01:00
|
|
|
fpat = s[1];
|
2016-08-01 23:38:07 +02:00
|
|
|
wpat = s + (fpat ? 2 : 0);
|
|
|
|
wrep = d ? p : NULL;
|
2017-05-03 17:33:16 +02:00
|
|
|
if (!(stype & STYPE_AT)) {
|
2016-08-01 23:38:07 +02:00
|
|
|
rrep = wrep ? evalstr(wrep,
|
|
|
|
DOTILDE | DOSCALAR) :
|
|
|
|
null;
|
|
|
|
}
|
2008-03-01 22:10:26 +01:00
|
|
|
|
2016-08-01 23:38:07 +02:00
|
|
|
/* prepare string on which to work */
|
|
|
|
strdupx(s, str_val(st->var), ATEMP);
|
|
|
|
sbeg = s;
|
|
|
|
again_search:
|
|
|
|
pat = evalstr(wpat,
|
|
|
|
DOTILDE | DOSCALAR | DOPAT);
|
2011-03-17 22:57:50 +01:00
|
|
|
/* check for special cases */
|
2015-12-12 23:24:07 +01:00
|
|
|
if (!*pat && !fpat) {
|
|
|
|
/*
|
|
|
|
* empty unanchored
|
|
|
|
* pattern => reject
|
|
|
|
*/
|
2008-03-01 22:10:26 +01:00
|
|
|
goto no_repl;
|
2011-03-17 22:57:50 +01:00
|
|
|
}
|
2017-05-03 17:33:16 +02:00
|
|
|
if ((stype & STYPE_MASK) &&
|
2015-12-12 23:24:07 +01:00
|
|
|
gmatchx(null, pat, false)) {
|
2011-03-17 22:57:50 +01:00
|
|
|
/*
|
2015-05-23 19:43:22 +02:00
|
|
|
* pattern matches empty
|
|
|
|
* string => don't loop
|
2011-03-17 22:57:50 +01:00
|
|
|
*/
|
2017-05-03 17:33:16 +02:00
|
|
|
stype &= ~STYPE_MASK;
|
2011-03-17 22:57:50 +01:00
|
|
|
}
|
2008-03-01 22:10:26 +01:00
|
|
|
|
|
|
|
/* first see if we have any match at all */
|
2017-05-03 17:33:16 +02:00
|
|
|
if (ord(fpat) == ord('#')) {
|
2008-03-01 22:10:26 +01:00
|
|
|
/* anchor at the beginning */
|
2015-12-12 23:24:07 +01:00
|
|
|
tpat1 = shf_smprintf("%s%c*", pat, MAGIC);
|
2008-03-01 22:10:26 +01:00
|
|
|
tpat2 = tpat1;
|
2017-05-03 17:33:16 +02:00
|
|
|
} else if (ord(fpat) == ord('%')) {
|
2008-03-01 22:10:26 +01:00
|
|
|
/* anchor at the end */
|
2015-12-12 23:24:07 +01:00
|
|
|
tpat1 = shf_smprintf("%c*%s", MAGIC, pat);
|
|
|
|
tpat2 = pat;
|
2008-03-01 22:10:26 +01:00
|
|
|
} else {
|
|
|
|
/* float */
|
|
|
|
tpat1 = shf_smprintf("%c*%s%c*", MAGIC, pat, MAGIC);
|
|
|
|
tpat2 = tpat1 + 2;
|
|
|
|
}
|
|
|
|
again_repl:
|
2011-01-21 23:25:34 +01:00
|
|
|
/*
|
|
|
|
* this would not be necessary if gmatchx would return
|
2008-03-01 22:10:26 +01:00
|
|
|
* the start and end values of a match found, like re*
|
|
|
|
*/
|
|
|
|
if (!gmatchx(sbeg, tpat1, false))
|
|
|
|
goto end_repl;
|
|
|
|
end = strnul(s);
|
|
|
|
/* now anchor the beginning of the match */
|
2017-05-03 17:33:16 +02:00
|
|
|
if (ord(fpat) != ord('#'))
|
2008-03-01 22:10:26 +01:00
|
|
|
while (sbeg <= end) {
|
|
|
|
if (gmatchx(sbeg, tpat2, false))
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
sbeg++;
|
|
|
|
}
|
|
|
|
/* now anchor the end of the match */
|
|
|
|
p = end;
|
2017-05-03 17:33:16 +02:00
|
|
|
if (ord(fpat) != ord('%'))
|
2008-03-01 22:10:26 +01:00
|
|
|
while (p >= sbeg) {
|
|
|
|
bool gotmatch;
|
|
|
|
|
2011-03-07 21:30:41 +01:00
|
|
|
c = *p;
|
|
|
|
*p = '\0';
|
2015-12-12 23:24:07 +01:00
|
|
|
gotmatch = tobool(gmatchx(sbeg, pat, false));
|
2008-03-01 22:10:26 +01:00
|
|
|
*p = c;
|
|
|
|
if (gotmatch)
|
|
|
|
break;
|
|
|
|
p--;
|
|
|
|
}
|
2016-08-01 23:38:07 +02:00
|
|
|
strndupx(end, sbeg, p - sbeg, ATEMP);
|
|
|
|
record_match(end);
|
|
|
|
afree(end, ATEMP);
|
2017-05-03 17:33:16 +02:00
|
|
|
if (stype & STYPE_AT) {
|
2016-08-01 23:38:07 +02:00
|
|
|
if (rrep != null)
|
|
|
|
afree(rrep, ATEMP);
|
|
|
|
rrep = wrep ? evalstr(wrep,
|
|
|
|
DOTILDE | DOSCALAR) :
|
|
|
|
null;
|
|
|
|
}
|
2008-10-28 15:32:43 +01:00
|
|
|
strndupx(end, s, sbeg - s, ATEMP);
|
2016-07-25 02:04:48 +02:00
|
|
|
d = shf_smprintf(Tf_sss, end, rrep, p);
|
2008-03-01 22:10:26 +01:00
|
|
|
afree(end, ATEMP);
|
|
|
|
sbeg = d + (sbeg - s) + strlen(rrep);
|
|
|
|
afree(s, ATEMP);
|
|
|
|
s = d;
|
2017-05-03 17:33:16 +02:00
|
|
|
if (stype & STYPE_AT) {
|
2016-08-01 23:38:07 +02:00
|
|
|
afree(tpat1, ATEMP);
|
|
|
|
afree(pat, ATEMP);
|
|
|
|
goto again_search;
|
2017-05-03 17:33:16 +02:00
|
|
|
} else if (stype & STYPE_DBL)
|
2008-03-01 22:10:26 +01:00
|
|
|
goto again_repl;
|
|
|
|
end_repl:
|
|
|
|
afree(tpat1, ATEMP);
|
|
|
|
x.str = s;
|
|
|
|
no_repl:
|
|
|
|
afree(pat, ATEMP);
|
|
|
|
if (rrep != null)
|
|
|
|
afree(rrep, ATEMP);
|
2016-08-01 23:38:07 +02:00
|
|
|
afree(ws, ATEMP);
|
2008-03-01 22:10:26 +01:00
|
|
|
goto do_CSUBST;
|
2014-10-03 19:32:12 +02:00
|
|
|
}
|
2017-05-03 17:33:16 +02:00
|
|
|
case ord('#'):
|
|
|
|
case ord('%'):
|
2015-12-12 18:28:10 +01:00
|
|
|
/* ! DOBLANK,DOBRACE */
|
2012-07-30 23:37:17 +02:00
|
|
|
f = (f & DONTRUNCOMMAND) |
|
2015-12-12 18:28:10 +01:00
|
|
|
DOPAT | DOTILDE |
|
|
|
|
DOTEMP | DOSCALAR;
|
|
|
|
tilde_ok = 1;
|
2010-04-09 21:16:31 +02:00
|
|
|
st->quotew = quote = 0;
|
2011-01-21 23:25:34 +01:00
|
|
|
/*
|
|
|
|
* Prepend open pattern (so |
|
2005-05-23 05:06:10 +02:00
|
|
|
* in a trim will work as
|
|
|
|
* expected)
|
|
|
|
*/
|
2011-03-28 10:27:09 +02:00
|
|
|
if (!Flag(FSH)) {
|
|
|
|
*dp++ = MAGIC;
|
2014-10-19 22:56:33 +02:00
|
|
|
*dp++ = 0x80 | '@';
|
2011-03-28 10:27:09 +02:00
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
2017-05-03 17:33:16 +02:00
|
|
|
case ord('='):
|
2011-01-21 23:25:34 +01:00
|
|
|
/*
|
2017-03-11 23:49:56 +01:00
|
|
|
* Tilde expansion for string
|
|
|
|
* variables in POSIX mode is
|
|
|
|
* governed by Austinbug 351.
|
|
|
|
* In non-POSIX mode historic
|
|
|
|
* ksh behaviour (enable it!)
|
|
|
|
* us followed.
|
2005-05-23 05:06:10 +02:00
|
|
|
* Not doing tilde expansion
|
|
|
|
* for integer variables is a
|
|
|
|
* non-POSIX thing - makes
|
|
|
|
* sense though, since ~ is
|
|
|
|
* a arithmetic operator.
|
|
|
|
*/
|
|
|
|
if (!(x.var->flag & INTEGER))
|
2014-10-03 19:32:12 +02:00
|
|
|
f |= DOASNTILDE | DOTILDE;
|
2017-02-17 23:40:13 +01:00
|
|
|
f |= DOTEMP | DOSCALAR;
|
2011-01-21 23:25:34 +01:00
|
|
|
/*
|
|
|
|
* These will be done after the
|
2005-05-23 05:06:10 +02:00
|
|
|
* value has been assigned.
|
|
|
|
*/
|
2011-09-07 17:24:22 +02:00
|
|
|
f &= ~(DOBLANK|DOGLOB|DOBRACE);
|
2005-05-23 05:06:10 +02:00
|
|
|
tilde_ok = 1;
|
|
|
|
break;
|
2017-05-03 17:33:16 +02:00
|
|
|
case ord('?'):
|
2015-10-09 19:48:53 +02:00
|
|
|
if (*sp == CSUBST)
|
|
|
|
errorf("%s: parameter null or not set",
|
|
|
|
st->var->name);
|
2005-05-23 05:06:10 +02:00
|
|
|
f &= ~DOBLANK;
|
2011-09-07 17:24:22 +02:00
|
|
|
f |= DOTEMP;
|
2010-01-29 10:34:31 +01:00
|
|
|
/* FALLTHROUGH */
|
2005-05-23 05:06:10 +02:00
|
|
|
default:
|
2014-10-19 23:39:37 +02:00
|
|
|
/* '-' '+' '?' */
|
|
|
|
if (quote)
|
|
|
|
word = IFS_WORD;
|
|
|
|
else if (dp == Xstring(ds, dp))
|
|
|
|
word = IFS_IWS;
|
2005-05-23 05:06:10 +02:00
|
|
|
/* Enable tilde expansion */
|
|
|
|
tilde_ok = 1;
|
|
|
|
f |= DOTILDE;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
/* skip word */
|
2006-11-12 15:58:16 +01:00
|
|
|
sp += wdscan(sp, CSUBST) - sp;
|
2005-05-23 05:06:10 +02:00
|
|
|
continue;
|
2014-10-03 19:32:12 +02:00
|
|
|
}
|
2011-01-21 23:25:34 +01:00
|
|
|
case CSUBST:
|
|
|
|
/* only get here if expanding word */
|
2007-07-06 03:53:36 +02:00
|
|
|
do_CSUBST:
|
2011-01-21 23:25:34 +01:00
|
|
|
/* ({) skip the } or x */
|
|
|
|
sp++;
|
|
|
|
/* in case of ${unset:-} */
|
|
|
|
tilde_ok = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
*dp = '\0';
|
2010-04-20 11:10:07 +02:00
|
|
|
quote = st->quotep;
|
2005-05-23 05:06:10 +02:00
|
|
|
f = st->f;
|
2014-06-09 13:16:07 +02:00
|
|
|
if (f & DOBLANK)
|
2005-05-23 05:06:10 +02:00
|
|
|
doblank--;
|
2017-05-03 17:33:16 +02:00
|
|
|
switch (st->stype & STYPE_SINGLE) {
|
|
|
|
case ord('#'):
|
|
|
|
case ord('%'):
|
2011-03-28 10:27:09 +02:00
|
|
|
if (!Flag(FSH)) {
|
|
|
|
/* Append end-pattern */
|
|
|
|
*dp++ = MAGIC;
|
|
|
|
*dp++ = ')';
|
|
|
|
}
|
|
|
|
*dp = '\0';
|
2005-05-23 05:06:10 +02:00
|
|
|
dp = Xrestpos(ds, dp, st->base);
|
2011-01-21 23:25:34 +01:00
|
|
|
/*
|
|
|
|
* Must use st->var since calling
|
2005-05-23 05:06:10 +02:00
|
|
|
* global would break things
|
|
|
|
* like x[i+=1].
|
|
|
|
*/
|
|
|
|
x.str = trimsub(str_val(st->var),
|
|
|
|
dp, st->stype);
|
2014-06-09 12:41:03 +02:00
|
|
|
if (x.str[0] != '\0') {
|
2014-10-19 23:04:47 +02:00
|
|
|
word = IFS_IWS;
|
2010-04-09 20:53:29 +02:00
|
|
|
type = XSUB;
|
2015-02-19 23:01:13 +01:00
|
|
|
} else if (quote) {
|
|
|
|
word = IFS_WORD;
|
|
|
|
type = XSUB;
|
|
|
|
} else {
|
|
|
|
if (dp == Xstring(ds, dp))
|
|
|
|
word = IFS_IWS;
|
|
|
|
type = XNULLSUB;
|
|
|
|
}
|
2014-06-09 13:16:07 +02:00
|
|
|
if (f & DOBLANK)
|
2005-05-23 05:06:10 +02:00
|
|
|
doblank++;
|
|
|
|
st = st->prev;
|
|
|
|
continue;
|
2017-05-03 17:33:16 +02:00
|
|
|
case ord('='):
|
2011-01-21 23:25:34 +01:00
|
|
|
/*
|
|
|
|
* Restore our position and substitute
|
2005-05-23 05:06:10 +02:00
|
|
|
* the value of st->var (may not be
|
|
|
|
* the assigned value in the presence
|
|
|
|
* of integer/right-adj/etc attributes).
|
|
|
|
*/
|
|
|
|
dp = Xrestpos(ds, dp, st->base);
|
2011-01-21 23:25:34 +01:00
|
|
|
/*
|
|
|
|
* Must use st->var since calling
|
2005-05-23 05:06:10 +02:00
|
|
|
* global would cause with things
|
|
|
|
* like x[i+=1] to be evaluated twice.
|
|
|
|
*/
|
2011-01-21 23:25:34 +01:00
|
|
|
/*
|
|
|
|
* Note: not exported by FEXPORT
|
2009-06-11 14:42:21 +02:00
|
|
|
* in AT&T ksh.
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
2011-01-21 23:25:34 +01:00
|
|
|
/*
|
|
|
|
* XXX POSIX says readonly is only
|
2005-05-23 05:06:10 +02:00
|
|
|
* fatal for special builtins (setstr
|
|
|
|
* does readonly check).
|
|
|
|
*/
|
|
|
|
len = strlen(dp) + 1;
|
|
|
|
setstr(st->var,
|
2008-12-13 18:02:18 +01:00
|
|
|
debunk(alloc(len, ATEMP),
|
2005-05-23 05:06:10 +02:00
|
|
|
dp, len), KSH_UNWIND_ERROR);
|
|
|
|
x.str = str_val(st->var);
|
|
|
|
type = XSUB;
|
2014-06-09 13:16:07 +02:00
|
|
|
if (f & DOBLANK)
|
2005-05-23 05:06:10 +02:00
|
|
|
doblank++;
|
|
|
|
st = st->prev;
|
2014-10-19 23:53:08 +02:00
|
|
|
word = quote || (!*x.str && (f & DOSCALAR)) ? IFS_WORD : IFS_IWS;
|
2005-05-23 05:06:10 +02:00
|
|
|
continue;
|
2017-05-03 17:33:16 +02:00
|
|
|
case ord('?'):
|
2015-10-09 19:48:53 +02:00
|
|
|
dp = Xrestpos(ds, dp, st->base);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2016-07-25 02:04:48 +02:00
|
|
|
errorf(Tf_sD_s, st->var->name,
|
2015-10-09 19:48:53 +02:00
|
|
|
debunk(dp, dp, strlen(dp) + 1));
|
|
|
|
break;
|
2017-05-03 17:33:16 +02:00
|
|
|
case ord('0'):
|
|
|
|
case ord('/') | STYPE_AT:
|
|
|
|
case ord('/'):
|
|
|
|
case ord('#') | STYPE_AT:
|
|
|
|
case ord('Q') | STYPE_AT:
|
2007-07-06 03:53:36 +02:00
|
|
|
dp = Xrestpos(ds, dp, st->base);
|
|
|
|
type = XSUB;
|
2014-10-19 23:53:08 +02:00
|
|
|
word = quote || (!*x.str && (f & DOSCALAR)) ? IFS_WORD : IFS_IWS;
|
2014-06-09 13:16:07 +02:00
|
|
|
if (f & DOBLANK)
|
2007-07-06 03:53:36 +02:00
|
|
|
doblank++;
|
|
|
|
st = st->prev;
|
|
|
|
continue;
|
2014-10-19 23:39:37 +02:00
|
|
|
/* default: '-' '+' */
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
st = st->prev;
|
|
|
|
type = XBASE;
|
|
|
|
continue;
|
|
|
|
|
2011-01-21 23:25:34 +01:00
|
|
|
case OPAT:
|
|
|
|
/* open pattern: *(foo|bar) */
|
2005-05-23 05:06:10 +02:00
|
|
|
/* Next char is the type of pattern */
|
2012-07-30 19:28:21 +02:00
|
|
|
make_magic = true;
|
2011-08-27 20:06:52 +02:00
|
|
|
c = *sp++ | 0x80;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
2011-01-21 23:25:34 +01:00
|
|
|
case SPAT:
|
|
|
|
/* pattern separator (|) */
|
2012-07-30 19:28:21 +02:00
|
|
|
make_magic = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
c = '|';
|
|
|
|
break;
|
|
|
|
|
2011-01-21 23:25:34 +01:00
|
|
|
case CPAT:
|
|
|
|
/* close pattern */
|
2012-07-30 19:28:21 +02:00
|
|
|
make_magic = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
c = /*(*/ ')';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XNULLSUB:
|
2011-01-21 23:25:34 +01:00
|
|
|
/*
|
|
|
|
* Special case for "$@" (and "${foo[@]}") - no
|
2005-05-23 05:06:10 +02:00
|
|
|
* word is generated if $# is 0 (unless there is
|
|
|
|
* other stuff inside the quotes).
|
|
|
|
*/
|
|
|
|
type = XBASE;
|
2014-06-09 13:16:07 +02:00
|
|
|
if (f & DOBLANK) {
|
2005-05-23 05:06:10 +02:00
|
|
|
doblank--;
|
2015-02-19 23:01:13 +01:00
|
|
|
if (dp == Xstring(ds, dp) && word != IFS_WORD)
|
2014-10-19 23:04:47 +02:00
|
|
|
word = IFS_IWS;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case XSUB:
|
|
|
|
case XSUBMID:
|
|
|
|
if ((c = *x.str++) == 0) {
|
|
|
|
type = XBASE;
|
2014-06-09 13:16:07 +02:00
|
|
|
if (f & DOBLANK)
|
2005-05-23 05:06:10 +02:00
|
|
|
doblank--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XARGSEP:
|
|
|
|
type = XARG;
|
|
|
|
quote = 1;
|
2012-06-24 21:36:27 +02:00
|
|
|
/* FALLTHROUGH */
|
2005-05-23 05:06:10 +02:00
|
|
|
case XARG:
|
|
|
|
if ((c = *x.str++) == '\0') {
|
2011-01-21 23:25:34 +01:00
|
|
|
/*
|
|
|
|
* force null words to be created so
|
2014-10-03 19:32:12 +02:00
|
|
|
* set -- "" 2 ""; echo "$@" will do
|
2005-05-23 05:06:10 +02:00
|
|
|
* the right thing
|
|
|
|
*/
|
|
|
|
if (quote && x.split)
|
|
|
|
word = IFS_WORD;
|
|
|
|
if ((x.str = *x.u.strv++) == NULL) {
|
|
|
|
type = XBASE;
|
2014-06-09 13:16:07 +02:00
|
|
|
if (f & DOBLANK)
|
2005-05-23 05:06:10 +02:00
|
|
|
doblank--;
|
|
|
|
continue;
|
|
|
|
}
|
2017-04-22 02:07:10 +02:00
|
|
|
c = ord(ifs0);
|
2015-02-19 23:26:50 +01:00
|
|
|
if ((f & DOHEREDOC)) {
|
|
|
|
/* pseudo-field-split reliably */
|
|
|
|
if (c == 0)
|
|
|
|
c = ' ';
|
|
|
|
break;
|
|
|
|
}
|
2014-10-19 23:53:08 +02:00
|
|
|
if ((f & DOSCALAR)) {
|
|
|
|
/* do not field-split */
|
2014-10-03 19:32:12 +02:00
|
|
|
if (x.split) {
|
|
|
|
c = ' ';
|
|
|
|
break;
|
|
|
|
}
|
2015-02-19 23:26:50 +01:00
|
|
|
if (c == 0)
|
2014-10-03 19:32:12 +02:00
|
|
|
continue;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
if (c == 0) {
|
|
|
|
if (quote && !x.split)
|
|
|
|
continue;
|
2014-10-03 19:32:12 +02:00
|
|
|
if (!quote && word == IFS_WS)
|
|
|
|
continue;
|
2013-02-23 21:03:31 +01:00
|
|
|
/* this is so we don't terminate */
|
2005-05-23 05:06:10 +02:00
|
|
|
c = ' ';
|
2013-02-23 21:03:31 +01:00
|
|
|
/* now force-emit a word */
|
|
|
|
goto emit_word;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (quote && x.split) {
|
|
|
|
/* terminate word for "$@" */
|
|
|
|
type = XARGSEP;
|
|
|
|
quote = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XCOM:
|
2013-07-21 20:36:03 +02:00
|
|
|
if (x.u.shf == NULL) {
|
|
|
|
/* $(<...) failed */
|
|
|
|
subst_exstat = 1;
|
|
|
|
/* fake EOF */
|
2015-02-06 11:09:07 +01:00
|
|
|
c = -1;
|
2013-07-21 20:36:03 +02:00
|
|
|
} else if (newlines) {
|
2013-02-23 21:03:31 +01:00
|
|
|
/* spit out saved NLs */
|
2005-05-23 05:06:10 +02:00
|
|
|
c = '\n';
|
|
|
|
--newlines;
|
|
|
|
} else {
|
2017-01-10 09:29:41 +01:00
|
|
|
while ((c = shf_getc(x.u.shf)) == 0 ||
|
2017-04-28 02:38:33 +02:00
|
|
|
ctype(c, C_NL)) {
|
2017-04-02 16:41:01 +02:00
|
|
|
#ifdef MKSH_WITH_TEXTMODE
|
2017-01-10 09:29:41 +01:00
|
|
|
if (c == '\r') {
|
|
|
|
c = shf_getc(x.u.shf);
|
2017-03-26 00:09:46 +01:00
|
|
|
switch (c) {
|
|
|
|
case '\n':
|
|
|
|
break;
|
|
|
|
default:
|
2017-01-10 09:29:41 +01:00
|
|
|
shf_ungetc(c, x.u.shf);
|
2017-03-26 00:09:46 +01:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case -1:
|
|
|
|
c = '\r';
|
|
|
|
break;
|
|
|
|
}
|
2017-01-10 09:29:41 +01:00
|
|
|
}
|
|
|
|
#endif
|
2009-06-10 20:12:51 +02:00
|
|
|
if (c == '\n')
|
2013-02-23 21:03:31 +01:00
|
|
|
/* save newlines */
|
2009-06-10 20:12:51 +02:00
|
|
|
newlines++;
|
2017-01-10 09:29:41 +01:00
|
|
|
}
|
2015-02-06 11:09:07 +01:00
|
|
|
if (newlines && c != -1) {
|
2005-05-23 05:06:10 +02:00
|
|
|
shf_ungetc(c, x.u.shf);
|
|
|
|
c = '\n';
|
|
|
|
--newlines;
|
|
|
|
}
|
|
|
|
}
|
2015-02-06 11:09:07 +01:00
|
|
|
if (c == -1) {
|
2005-05-23 05:06:10 +02:00
|
|
|
newlines = 0;
|
2013-07-21 20:36:03 +02:00
|
|
|
if (x.u.shf)
|
|
|
|
shf_close(x.u.shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (x.split)
|
|
|
|
subst_exstat = waitlast();
|
|
|
|
type = XBASE;
|
2014-06-09 13:16:07 +02:00
|
|
|
if (f & DOBLANK)
|
2005-05-23 05:06:10 +02:00
|
|
|
doblank--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check for end of word or IFS separation */
|
|
|
|
if (c == 0 || (!quote && (f & DOBLANK) && doblank &&
|
|
|
|
!make_magic && ctype(c, C_IFS))) {
|
2011-01-21 23:25:34 +01:00
|
|
|
/*-
|
|
|
|
* How words are broken up:
|
2009-06-10 20:12:51 +02:00
|
|
|
* | value of c
|
|
|
|
* word | ws nws 0
|
2005-05-23 05:06:10 +02:00
|
|
|
* -----------------------------------
|
2009-06-10 20:12:51 +02:00
|
|
|
* IFS_WORD w/WS w/NWS w
|
2014-07-29 18:29:11 +02:00
|
|
|
* IFS_WS -/WS -/NWS -
|
|
|
|
* IFS_NWS -/NWS w/NWS -
|
2014-10-07 17:22:17 +02:00
|
|
|
* IFS_IWS -/WS w/NWS -
|
2009-06-10 20:12:51 +02:00
|
|
|
* (w means generate a word)
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
2015-02-19 23:01:13 +01:00
|
|
|
if ((word == IFS_WORD) || (word == IFS_QUOTE) || (c &&
|
2014-10-07 17:22:17 +02:00
|
|
|
(word == IFS_IWS || word == IFS_NWS) &&
|
|
|
|
!ctype(c, C_IFSWS))) {
|
2013-02-23 21:03:31 +01:00
|
|
|
emit_word:
|
2015-09-06 21:47:01 +02:00
|
|
|
if (f & DOHERESTR)
|
|
|
|
*dp++ = '\n';
|
2005-05-23 05:06:10 +02:00
|
|
|
*dp++ = '\0';
|
2013-02-23 21:03:31 +01:00
|
|
|
cp = Xclose(ds, dp);
|
2011-09-07 17:24:22 +02:00
|
|
|
if (fdo & DOBRACE)
|
2005-05-23 05:06:10 +02:00
|
|
|
/* also does globbing */
|
2013-02-23 21:03:31 +01:00
|
|
|
alt_expand(wp, cp, cp,
|
|
|
|
cp + Xlength(ds, (dp - 1)),
|
2005-05-23 05:06:10 +02:00
|
|
|
fdo | (f & DOMARKDIRS));
|
|
|
|
else if (fdo & DOGLOB)
|
2013-02-23 21:03:31 +01:00
|
|
|
glob(cp, wp, tobool(f & DOMARKDIRS));
|
2011-09-07 17:24:22 +02:00
|
|
|
else if ((f & DOPAT) || !(fdo & DOMAGIC))
|
2013-02-23 21:03:31 +01:00
|
|
|
XPput(*wp, cp);
|
2005-05-23 05:06:10 +02:00
|
|
|
else
|
2013-02-23 21:03:31 +01:00
|
|
|
XPput(*wp, debunk(cp, cp,
|
|
|
|
strlen(cp) + 1));
|
2005-05-23 05:06:10 +02:00
|
|
|
fdo = 0;
|
2012-07-30 19:28:21 +02:00
|
|
|
saw_eq = false;
|
2014-10-03 19:32:12 +02:00
|
|
|
/* must be 1/0 */
|
|
|
|
tilde_ok = (f & (DOTILDE | DOASNTILDE)) ? 1 : 0;
|
2011-12-16 21:03:12 +01:00
|
|
|
if (c == 0)
|
|
|
|
return;
|
|
|
|
Xinit(ds, dp, 128, ATEMP);
|
|
|
|
} else if (c == 0) {
|
2005-05-23 05:06:10 +02:00
|
|
|
return;
|
2011-12-16 21:03:12 +01:00
|
|
|
} else if (type == XSUB && ctype(c, C_IFS) &&
|
|
|
|
!ctype(c, C_IFSWS) && Xlength(ds, dp) == 0) {
|
2013-02-23 21:03:31 +01:00
|
|
|
*(cp = alloc(1, ATEMP)) = '\0';
|
|
|
|
XPput(*wp, cp);
|
2011-12-16 21:03:12 +01:00
|
|
|
type = XSUBMID;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
if (word != IFS_NWS)
|
|
|
|
word = ctype(c, C_IFSWS) ? IFS_WS : IFS_NWS;
|
|
|
|
} else {
|
|
|
|
if (type == XSUB) {
|
|
|
|
if (word == IFS_NWS &&
|
|
|
|
Xlength(ds, dp) == 0) {
|
2013-02-23 21:03:31 +01:00
|
|
|
*(cp = alloc(1, ATEMP)) = '\0';
|
|
|
|
XPput(*wp, cp);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
type = XSUBMID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* age tilde_ok info - ~ code tests second bit */
|
|
|
|
tilde_ok <<= 1;
|
|
|
|
/* mark any special second pass chars */
|
|
|
|
if (!quote)
|
2017-05-06 00:53:32 +02:00
|
|
|
switch (ord(c)) {
|
|
|
|
case ord('['):
|
|
|
|
case ord('!'):
|
|
|
|
case ord('-'):
|
|
|
|
case ord(']'):
|
2011-01-21 23:25:34 +01:00
|
|
|
/*
|
|
|
|
* For character classes - doesn't hurt
|
2005-05-23 05:06:10 +02:00
|
|
|
* to have magic !,-,]s outside of
|
|
|
|
* [...] expressions.
|
|
|
|
*/
|
|
|
|
if (f & (DOPAT | DOGLOB)) {
|
2011-09-07 17:24:22 +02:00
|
|
|
fdo |= DOMAGIC;
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c == ord('['))
|
2005-05-23 05:06:10 +02:00
|
|
|
fdo |= f & DOGLOB;
|
|
|
|
*dp++ = MAGIC;
|
|
|
|
}
|
|
|
|
break;
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('*'):
|
|
|
|
case ord('?'):
|
2005-05-23 05:06:10 +02:00
|
|
|
if (f & (DOPAT | DOGLOB)) {
|
2011-09-07 17:24:22 +02:00
|
|
|
fdo |= DOMAGIC | (f & DOGLOB);
|
2005-05-23 05:06:10 +02:00
|
|
|
*dp++ = MAGIC;
|
|
|
|
}
|
|
|
|
break;
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('{'):
|
|
|
|
case ord('}'):
|
|
|
|
case ord(','):
|
|
|
|
if ((f & DOBRACE) &&
|
|
|
|
(ord(c) == ord('{' /*}*/) ||
|
2011-09-07 17:24:22 +02:00
|
|
|
(fdo & DOBRACE))) {
|
|
|
|
fdo |= DOBRACE|DOMAGIC;
|
2005-05-23 05:06:10 +02:00
|
|
|
*dp++ = MAGIC;
|
|
|
|
}
|
|
|
|
break;
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('='):
|
2005-05-23 05:06:10 +02:00
|
|
|
/* Note first unquoted = for ~ */
|
2015-10-09 19:48:53 +02:00
|
|
|
if (!(f & DOTEMP) && (!Flag(FPOSIX) ||
|
|
|
|
(f & DOASNTILDE)) && !saw_eq) {
|
2012-07-30 19:28:21 +02:00
|
|
|
saw_eq = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
tilde_ok = 1;
|
|
|
|
}
|
|
|
|
break;
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord(':'):
|
2011-01-21 23:25:34 +01:00
|
|
|
/* : */
|
2005-05-23 05:06:10 +02:00
|
|
|
/* Note unquoted : for ~ */
|
2011-09-07 17:24:22 +02:00
|
|
|
if (!(f & DOTEMP) && (f & DOASNTILDE))
|
2005-05-23 05:06:10 +02:00
|
|
|
tilde_ok = 1;
|
|
|
|
break;
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('~'):
|
2011-01-21 23:25:34 +01:00
|
|
|
/*
|
|
|
|
* tilde_ok is reset whenever
|
2005-05-23 05:06:10 +02:00
|
|
|
* any of ' " $( $(( ${ } are seen.
|
|
|
|
* Note that tilde_ok must be preserved
|
|
|
|
* through the sequence ${A=a=}~
|
|
|
|
*/
|
|
|
|
if (type == XBASE &&
|
2014-10-03 19:32:12 +02:00
|
|
|
(f & (DOTILDE | DOASNTILDE)) &&
|
2005-05-23 05:06:10 +02:00
|
|
|
(tilde_ok & 2)) {
|
2013-02-23 21:03:31 +01:00
|
|
|
const char *tcp;
|
|
|
|
char *tdp = dp;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2013-02-23 21:03:31 +01:00
|
|
|
tcp = maybe_expand_tilde(sp,
|
|
|
|
&ds, &tdp,
|
2014-12-05 16:14:23 +01:00
|
|
|
tobool(f & DOASNTILDE));
|
2013-02-23 21:03:31 +01:00
|
|
|
if (tcp) {
|
|
|
|
if (dp != tdp)
|
2005-05-23 05:06:10 +02:00
|
|
|
word = IFS_WORD;
|
2013-02-23 21:03:31 +01:00
|
|
|
dp = tdp;
|
|
|
|
sp = tcp;
|
2005-05-23 05:06:10 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-08-24 23:15:42 +02:00
|
|
|
else
|
2011-01-21 23:25:34 +01:00
|
|
|
/* undo temporary */
|
|
|
|
quote &= ~2;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (make_magic) {
|
2012-07-30 19:28:21 +02:00
|
|
|
make_magic = false;
|
2011-09-07 17:24:22 +02:00
|
|
|
fdo |= DOMAGIC | (f & DOGLOB);
|
2005-05-23 05:06:10 +02:00
|
|
|
*dp++ = MAGIC;
|
|
|
|
} else if (ISMAGIC(c)) {
|
2011-09-07 17:24:22 +02:00
|
|
|
fdo |= DOMAGIC;
|
2005-05-23 05:06:10 +02:00
|
|
|
*dp++ = MAGIC;
|
|
|
|
}
|
2011-01-21 23:25:34 +01:00
|
|
|
/* save output char */
|
|
|
|
*dp++ = c;
|
2005-05-23 05:06:10 +02:00
|
|
|
word = IFS_WORD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-12 21:38:56 +01:00
|
|
|
static bool
|
|
|
|
hasnonempty(const char **strv)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
while (strv[i])
|
|
|
|
if (*strv[i++])
|
|
|
|
return (true);
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
/*
|
|
|
|
* Prepare to generate the string returned by ${} substitution.
|
|
|
|
*/
|
|
|
|
static int
|
2007-03-04 01:13:17 +01:00
|
|
|
varsub(Expand *xp, const char *sp, const char *word,
|
2005-05-23 05:06:10 +02:00
|
|
|
int *stypep, /* becomes qualifier type */
|
|
|
|
int *slenp) /* " " len (=, :=, etc.) valid iff *stypep != 0 */
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
int state; /* next state: XBASE, XARG, XSUB, XNULLSUB */
|
|
|
|
int stype; /* substitution type */
|
2016-06-26 01:55:00 +02:00
|
|
|
int slen = 0;
|
2007-03-04 01:13:17 +01:00
|
|
|
const char *p;
|
2005-05-23 05:06:10 +02:00
|
|
|
struct tbl *vp;
|
2009-07-16 17:06:45 +02:00
|
|
|
bool zero_ok = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2017-05-03 16:51:25 +02:00
|
|
|
if ((stype = ord(sp[0])) == '\0')
|
2011-01-21 23:25:34 +01:00
|
|
|
/* Bad variable name */
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
xp->var = NULL;
|
|
|
|
|
2009-11-22 00:23:20 +01:00
|
|
|
/*-
|
|
|
|
* ${#var}, string length (-U: characters, +U: octets) or array size
|
|
|
|
* ${%var}, string width (-U: screen columns, +U: octets)
|
|
|
|
*/
|
2017-05-03 16:51:25 +02:00
|
|
|
c = ord(sp[1]);
|
|
|
|
if (stype == ord('%') && c == '\0')
|
2009-11-22 00:23:20 +01:00
|
|
|
return (-1);
|
2017-04-28 02:38:33 +02:00
|
|
|
if (ctype(stype, C_SUB2) && c != '\0') {
|
2009-11-22 00:23:20 +01:00
|
|
|
/* Can't have any modifiers for ${#...} or ${%...} */
|
2005-05-23 05:06:10 +02:00
|
|
|
if (*word != CSUBST)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
sp++;
|
|
|
|
/* Check for size of array */
|
2017-05-03 16:51:25 +02:00
|
|
|
if ((p = cstrchr(sp, '[')) && (ord(p[1]) == ord('*') ||
|
|
|
|
ord(p[1]) == ord('@')) && ord(p[2]) == ord(']')) {
|
2005-05-23 05:06:10 +02:00
|
|
|
int n = 0;
|
|
|
|
|
2017-05-03 16:51:25 +02:00
|
|
|
if (stype != ord('#'))
|
2009-11-22 00:23:20 +01:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
vp = global(arrayname(sp));
|
|
|
|
if (vp->flag & (ISSET|ARRAY))
|
2009-07-16 17:06:45 +02:00
|
|
|
zero_ok = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
for (; vp; vp = vp->u.array)
|
2007-06-07 01:28:17 +02:00
|
|
|
if (vp->flag & ISSET)
|
2005-05-23 05:06:10 +02:00
|
|
|
n++;
|
2007-06-07 01:28:17 +02:00
|
|
|
c = n;
|
2017-05-03 16:51:25 +02:00
|
|
|
} else if (c == ord('*') || c == ord('@')) {
|
|
|
|
if (stype != ord('#'))
|
2009-11-22 00:23:20 +01:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
c = e->loc->argc;
|
2009-11-22 00:23:20 +01:00
|
|
|
} else {
|
2005-05-23 05:06:10 +02:00
|
|
|
p = str_val(global(sp));
|
|
|
|
zero_ok = p != null;
|
2017-05-03 16:51:25 +02:00
|
|
|
if (stype == ord('#'))
|
2010-01-01 18:44:10 +01:00
|
|
|
c = utflen(p);
|
|
|
|
else {
|
|
|
|
/* partial utf_mbswidth reimplementation */
|
|
|
|
const char *s = p;
|
|
|
|
unsigned int wc;
|
|
|
|
size_t len;
|
|
|
|
int cw;
|
|
|
|
|
|
|
|
c = 0;
|
|
|
|
while (*s) {
|
|
|
|
if (!UTFMODE || (len = utf_mbtowc(&wc,
|
|
|
|
s)) == (size_t)-1)
|
|
|
|
/* not UTFMODE or not UTF-8 */
|
2017-05-05 22:36:03 +02:00
|
|
|
wc = rtt2asc(*s++);
|
2010-01-01 18:44:10 +01:00
|
|
|
else
|
|
|
|
/* UTFMODE and UTF-8 */
|
|
|
|
s += len;
|
|
|
|
/* wc == char or wchar at s++ */
|
|
|
|
if ((cw = utf_wcwidth(wc)) == -1) {
|
|
|
|
/* 646, 8859-1, 10646 C0/C1 */
|
|
|
|
c = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
c += cw;
|
|
|
|
}
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (Flag(FNOUNSET) && c == 0 && !zero_ok)
|
2016-07-25 02:04:48 +02:00
|
|
|
errorf(Tf_parm, sp);
|
2011-01-21 23:25:34 +01:00
|
|
|
/* unqualified variable/string substitution */
|
|
|
|
*stypep = 0;
|
2016-07-25 02:04:48 +02:00
|
|
|
xp->str = shf_smprintf(Tf_d, c);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (XSUB);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2017-05-03 16:51:25 +02:00
|
|
|
if (stype == ord('!') && c != '\0' && *word == CSUBST) {
|
2016-06-26 01:55:00 +02:00
|
|
|
sp++;
|
2017-05-03 16:51:25 +02:00
|
|
|
if ((p = cstrchr(sp, '[')) && (ord(p[1]) == ord('*') ||
|
|
|
|
ord(p[1]) == ord('@')) && ord(p[2]) == ord(']')) {
|
|
|
|
c = ord('!');
|
2016-06-26 01:55:00 +02:00
|
|
|
stype = 0;
|
|
|
|
goto arraynames;
|
|
|
|
}
|
|
|
|
xp->var = global(sp);
|
|
|
|
xp->str = p ? shf_smprintf("%s[%lu]",
|
|
|
|
xp->var->name, arrayindex(xp->var)) : xp->var->name;
|
|
|
|
*stypep = 0;
|
|
|
|
return (XSUB);
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* Check for qualifiers in word part */
|
|
|
|
stype = 0;
|
2017-05-03 16:51:25 +02:00
|
|
|
c = word[slen + 0] == CHAR ? ord(word[slen + 1]) : 0;
|
|
|
|
if (c == ord(':')) {
|
2005-05-23 05:06:10 +02:00
|
|
|
slen += 2;
|
2017-05-03 17:33:16 +02:00
|
|
|
stype = STYPE_DBL;
|
2017-05-03 16:51:25 +02:00
|
|
|
c = word[slen + 0] == CHAR ? ord(word[slen + 1]) : 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2017-05-03 16:51:25 +02:00
|
|
|
if (!stype && c == ord('/')) {
|
2008-03-01 22:10:26 +01:00
|
|
|
slen += 2;
|
|
|
|
stype = c;
|
2017-05-03 16:51:25 +02:00
|
|
|
if (word[slen] == ADELIM &&
|
|
|
|
ord(word[slen + 1]) == (unsigned int)c) {
|
2008-03-01 22:10:26 +01:00
|
|
|
slen += 2;
|
2017-05-03 17:33:16 +02:00
|
|
|
stype |= STYPE_DBL;
|
2008-03-01 22:10:26 +01:00
|
|
|
}
|
2017-05-03 17:33:16 +02:00
|
|
|
} else if (stype == STYPE_DBL && (c == ord(' ') || c == ord('0'))) {
|
|
|
|
stype |= ord('0');
|
2017-04-27 21:16:10 +02:00
|
|
|
} else if (ctype(c, C_SUB1)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
slen += 2;
|
|
|
|
stype |= c;
|
2017-04-27 21:33:53 +02:00
|
|
|
} else if (ctype(c, C_SUB2)) {
|
2011-01-21 23:25:34 +01:00
|
|
|
/* Note: ksh88 allows :%, :%%, etc */
|
2005-05-23 05:06:10 +02:00
|
|
|
slen += 2;
|
|
|
|
stype = c;
|
2017-05-03 16:51:25 +02:00
|
|
|
if (word[slen + 0] == CHAR &&
|
|
|
|
ord(word[slen + 1]) == (unsigned int)c) {
|
2017-05-03 17:33:16 +02:00
|
|
|
stype |= STYPE_DBL;
|
2005-05-23 05:06:10 +02:00
|
|
|
slen += 2;
|
|
|
|
}
|
2017-05-03 16:51:25 +02:00
|
|
|
} else if (c == ord('@')) {
|
2011-01-21 23:25:34 +01:00
|
|
|
/* @x where x is command char */
|
2017-05-03 16:51:25 +02:00
|
|
|
switch (c = ord(word[slen + 2]) == CHAR ?
|
|
|
|
ord(word[slen + 3]) : 0) {
|
|
|
|
case ord('#'):
|
|
|
|
case ord('/'):
|
|
|
|
case ord('Q'):
|
2017-02-17 21:52:15 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (-1);
|
|
|
|
}
|
2017-05-03 17:33:16 +02:00
|
|
|
stype |= STYPE_AT | c;
|
2017-02-17 21:52:15 +01:00
|
|
|
slen += 4;
|
2011-01-21 23:25:34 +01:00
|
|
|
} else if (stype)
|
|
|
|
/* : is not ok */
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (!stype && *word != CSUBST)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2017-05-03 16:51:25 +02:00
|
|
|
c = ord(sp[0]);
|
|
|
|
if (c == ord('*') || c == ord('@')) {
|
2017-05-03 17:33:16 +02:00
|
|
|
switch (stype & STYPE_SINGLE) {
|
2013-02-23 21:03:31 +01:00
|
|
|
/* can't assign to a vector */
|
2017-05-03 16:51:25 +02:00
|
|
|
case ord('='):
|
2013-02-23 21:03:31 +01:00
|
|
|
/* can't trim a vector (yet) */
|
2017-05-03 16:51:25 +02:00
|
|
|
case ord('%'):
|
|
|
|
case ord('#'):
|
|
|
|
case ord('?'):
|
|
|
|
case ord('0'):
|
2017-05-03 17:33:16 +02:00
|
|
|
case ord('/') | STYPE_AT:
|
2017-05-03 16:51:25 +02:00
|
|
|
case ord('/'):
|
2017-05-03 17:33:16 +02:00
|
|
|
case ord('#') | STYPE_AT:
|
|
|
|
case ord('Q') | STYPE_AT:
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (e->loc->argc == 0) {
|
|
|
|
xp->str = null;
|
|
|
|
xp->var = global(sp);
|
2017-05-03 16:51:25 +02:00
|
|
|
state = c == ord('@') ? XNULLSUB : XSUB;
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
2009-08-29 00:39:09 +02:00
|
|
|
xp->u.strv = (const char **)e->loc->argv + 1;
|
2005-05-23 05:06:10 +02:00
|
|
|
xp->str = *xp->u.strv++;
|
2012-07-30 19:28:21 +02:00
|
|
|
/* $@ */
|
2017-05-03 16:51:25 +02:00
|
|
|
xp->split = tobool(c == ord('@'));
|
2005-05-23 05:06:10 +02:00
|
|
|
state = XARG;
|
|
|
|
}
|
2011-01-21 23:25:34 +01:00
|
|
|
/* POSIX 2009? */
|
|
|
|
zero_ok = true;
|
2017-05-03 16:51:25 +02:00
|
|
|
} else if ((p = cstrchr(sp, '[')) && (ord(p[1]) == ord('*') ||
|
|
|
|
ord(p[1]) == ord('@')) && ord(p[2]) == ord(']')) {
|
2016-05-06 00:19:04 +02:00
|
|
|
XPtrV wv;
|
|
|
|
|
2017-05-03 17:33:16 +02:00
|
|
|
switch (stype & STYPE_SINGLE) {
|
2016-05-06 00:19:04 +02:00
|
|
|
/* can't assign to a vector */
|
2017-05-03 16:51:25 +02:00
|
|
|
case ord('='):
|
2016-05-06 00:19:04 +02:00
|
|
|
/* can't trim a vector (yet) */
|
2017-05-03 16:51:25 +02:00
|
|
|
case ord('%'):
|
|
|
|
case ord('#'):
|
|
|
|
case ord('?'):
|
|
|
|
case ord('0'):
|
2017-05-03 17:33:16 +02:00
|
|
|
case ord('/') | STYPE_AT:
|
2017-05-03 16:51:25 +02:00
|
|
|
case ord('/'):
|
2017-05-03 17:33:16 +02:00
|
|
|
case ord('#') | STYPE_AT:
|
|
|
|
case ord('Q') | STYPE_AT:
|
2016-05-06 00:19:04 +02:00
|
|
|
return (-1);
|
|
|
|
}
|
2016-06-26 01:55:00 +02:00
|
|
|
c = 0;
|
|
|
|
arraynames:
|
2016-05-06 00:19:04 +02:00
|
|
|
XPinit(wv, 32);
|
|
|
|
vp = global(arrayname(sp));
|
|
|
|
for (; vp; vp = vp->u.array) {
|
|
|
|
if (!(vp->flag&ISSET))
|
|
|
|
continue;
|
2017-05-03 16:51:25 +02:00
|
|
|
XPput(wv, c == ord('!') ? shf_smprintf(Tf_lu,
|
2016-05-06 00:19:04 +02:00
|
|
|
arrayindex(vp)) :
|
|
|
|
str_val(vp));
|
|
|
|
}
|
|
|
|
if (XPsize(wv) == 0) {
|
|
|
|
xp->str = null;
|
2017-05-03 16:51:25 +02:00
|
|
|
state = ord(p[1]) == ord('@') ? XNULLSUB : XSUB;
|
2016-05-06 00:19:04 +02:00
|
|
|
XPfree(wv);
|
|
|
|
} else {
|
|
|
|
XPput(wv, 0);
|
|
|
|
xp->u.strv = (const char **)XPptrv(wv);
|
|
|
|
xp->str = *xp->u.strv++;
|
|
|
|
/* ${foo[@]} */
|
2017-05-03 16:51:25 +02:00
|
|
|
xp->split = tobool(ord(p[1]) == ord('@'));
|
2016-05-06 00:19:04 +02:00
|
|
|
state = XARG;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
2016-06-26 01:55:00 +02:00
|
|
|
xp->var = global(sp);
|
|
|
|
xp->str = str_val(xp->var);
|
|
|
|
/* can't assign things like $! or $1 */
|
2017-05-03 17:33:16 +02:00
|
|
|
if ((stype & STYPE_SINGLE) == ord('=') && !*xp->str &&
|
2016-05-06 00:19:04 +02:00
|
|
|
ctype(*sp, C_VAR1 | C_DIGIT))
|
|
|
|
return (-1);
|
|
|
|
state = XSUB;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2017-05-03 17:33:16 +02:00
|
|
|
c = stype & STYPE_CHAR;
|
2005-05-23 05:06:10 +02:00
|
|
|
/* test the compiler's code generator */
|
2017-05-03 17:33:16 +02:00
|
|
|
if ((!(stype & STYPE_AT) && (ctype(c, C_SUB2) ||
|
|
|
|
(((stype & STYPE_DBL) ? *xp->str == '\0' : xp->str == null) &&
|
2015-12-12 21:38:56 +01:00
|
|
|
(state != XARG || (ifs0 || xp->split ?
|
|
|
|
(xp->u.strv[0] == NULL) : !hasnonempty(xp->u.strv))) ?
|
2017-05-03 16:51:25 +02:00
|
|
|
ctype(c, C_EQUAL | C_MINUS | C_QUEST) : c == ord('+')))) ||
|
2017-05-03 17:33:16 +02:00
|
|
|
stype == (ord('0') | STYPE_DBL) || stype == (ord('#') | STYPE_AT) ||
|
|
|
|
stype == (ord('Q') | STYPE_AT) || (stype & STYPE_CHAR) == ord('/'))
|
2011-01-21 23:25:34 +01:00
|
|
|
/* expand word instead of variable value */
|
|
|
|
state = XBASE;
|
2009-07-16 17:06:45 +02:00
|
|
|
if (Flag(FNOUNSET) && xp->str == null && !zero_ok &&
|
2017-05-03 16:51:25 +02:00
|
|
|
(ctype(c, C_SUB2) || (state != XBASE && c != ord('+'))))
|
2016-07-25 02:04:48 +02:00
|
|
|
errorf(Tf_parm, sp);
|
2016-06-26 01:55:00 +02:00
|
|
|
*stypep = stype;
|
|
|
|
*slenp = slen;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (state);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run the command in $(...) and read its output.
|
|
|
|
*/
|
|
|
|
static int
|
2017-04-06 03:59:58 +02:00
|
|
|
comsub(Expand *xp, const char *cp, int fn)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
Source *s, *sold;
|
|
|
|
struct op *t;
|
|
|
|
struct shf *shf;
|
2017-04-06 03:59:58 +02:00
|
|
|
bool doalias = false;
|
2011-03-13 17:03:54 +01:00
|
|
|
uint8_t old_utfmode = UTFMODE;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2017-04-06 03:59:58 +02:00
|
|
|
switch (fn) {
|
|
|
|
case COMASUB:
|
|
|
|
fn = COMSUB;
|
|
|
|
if (0)
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case FUNASUB:
|
|
|
|
fn = FUNSUB;
|
|
|
|
doalias = true;
|
|
|
|
}
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
s = pushs(SSTRING, ATEMP);
|
|
|
|
s->start = s->str = cp;
|
|
|
|
sold = source;
|
2017-04-06 03:59:58 +02:00
|
|
|
t = compile(s, true, doalias);
|
2007-09-09 20:06:42 +02:00
|
|
|
afree(s, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
source = sold;
|
|
|
|
|
2012-07-30 23:37:17 +02:00
|
|
|
UTFMODE = old_utfmode;
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
if (t == NULL)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (XBASE);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2012-07-30 23:37:17 +02:00
|
|
|
/* no waitlast() unless specifically enabled later */
|
|
|
|
xp->split = false;
|
|
|
|
|
|
|
|
if (t->type == TCOM &&
|
2005-05-23 05:06:10 +02:00
|
|
|
*t->args == NULL && *t->vars == NULL && t->ioact != NULL) {
|
2011-01-21 23:25:34 +01:00
|
|
|
/* $(<file) */
|
2005-05-23 05:06:10 +02:00
|
|
|
struct ioword *io = *t->ioact;
|
|
|
|
char *name;
|
|
|
|
|
2016-06-26 02:44:59 +02:00
|
|
|
switch (io->ioflag & IOTYPE) {
|
|
|
|
case IOREAD:
|
|
|
|
shf = shf_open(name = evalstr(io->ioname, DOTILDE),
|
|
|
|
O_RDONLY, 0, SHF_MAPHI | SHF_CLEXEC);
|
|
|
|
if (shf == NULL)
|
2016-07-25 02:04:48 +02:00
|
|
|
warningf(!Flag(FTALKING), Tf_sD_s_sD_s,
|
|
|
|
name, Tcant_open, "$(<...) input",
|
2016-06-26 02:44:59 +02:00
|
|
|
cstrerror(errno));
|
|
|
|
break;
|
|
|
|
case IOHERE:
|
|
|
|
if (!herein(io, &name)) {
|
|
|
|
xp->str = name;
|
|
|
|
/* as $(…) requires, trim trailing newlines */
|
2017-04-30 00:04:31 +02:00
|
|
|
name = strnul(name);
|
2016-06-26 02:44:59 +02:00
|
|
|
while (name > xp->str && name[-1] == '\n')
|
|
|
|
--name;
|
|
|
|
*name = '\0';
|
|
|
|
return (XSUB);
|
|
|
|
}
|
|
|
|
shf = NULL;
|
|
|
|
break;
|
|
|
|
default:
|
2016-07-25 02:04:48 +02:00
|
|
|
errorf(Tf_sD_s, T_funny_command,
|
|
|
|
snptreef(NULL, 32, Tft_R, io));
|
2016-06-26 02:44:59 +02:00
|
|
|
}
|
2012-10-22 22:19:18 +02:00
|
|
|
} else if (fn == FUNSUB) {
|
2013-02-11 00:43:59 +01:00
|
|
|
int ofd1;
|
2012-10-22 22:19:18 +02:00
|
|
|
struct temp *tf = NULL;
|
|
|
|
|
2013-03-29 18:33:07 +01:00
|
|
|
/*
|
|
|
|
* create a temporary file, open for reading and writing,
|
|
|
|
* with an shf open for reading (buffered) but yet unused
|
|
|
|
*/
|
2012-10-22 22:19:18 +02:00
|
|
|
maketemp(ATEMP, TT_FUNSUB, &tf);
|
|
|
|
if (!tf->shf) {
|
2016-07-25 02:04:48 +02:00
|
|
|
errorf(Tf_temp,
|
|
|
|
Tcreate, tf->tffn, cstrerror(errno));
|
2012-10-22 22:19:18 +02:00
|
|
|
}
|
2013-03-29 18:33:07 +01:00
|
|
|
/* extract shf from temporary file, unlink and free it */
|
|
|
|
shf = tf->shf;
|
|
|
|
unlink(tf->tffn);
|
|
|
|
afree(tf, ATEMP);
|
|
|
|
/* save stdout and let it point to the tempfile */
|
2012-10-22 22:19:18 +02:00
|
|
|
ofd1 = savefd(1);
|
2013-03-29 18:33:07 +01:00
|
|
|
ksh_dup2(shf_fileno(shf), 1, false);
|
2013-02-11 00:30:47 +01:00
|
|
|
/*
|
|
|
|
* run tree, with output thrown into the tempfile,
|
2013-02-11 00:43:59 +01:00
|
|
|
* in a new function block
|
2013-02-11 00:30:47 +01:00
|
|
|
*/
|
2013-07-24 14:39:28 +02:00
|
|
|
valsub(t, NULL);
|
2013-02-11 00:30:47 +01:00
|
|
|
subst_exstat = exstat & 0xFF;
|
2013-03-29 18:33:07 +01:00
|
|
|
/* rewind the tempfile and restore regular stdout */
|
|
|
|
lseek(shf_fileno(shf), (off_t)0, SEEK_SET);
|
2013-02-11 00:43:59 +01:00
|
|
|
restfd(1, ofd1);
|
2013-05-02 23:59:54 +02:00
|
|
|
} else if (fn == VALSUB) {
|
|
|
|
xp->str = valsub(t, ATEMP);
|
|
|
|
subst_exstat = exstat & 0xFF;
|
|
|
|
return (XSUB);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
2013-02-10 22:42:16 +01:00
|
|
|
int ofd1, pv[2];
|
2012-07-30 23:37:17 +02:00
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
openpipe(pv);
|
|
|
|
shf = shf_fdopen(pv[0], SHF_RD, NULL);
|
2006-05-10 20:54:13 +02:00
|
|
|
ofd1 = savefd(1);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (pv[1] != 1) {
|
|
|
|
ksh_dup2(pv[1], 1, false);
|
|
|
|
close(pv[1]);
|
|
|
|
}
|
2012-08-17 20:34:25 +02:00
|
|
|
execute(t, XXCOM | XPIPEO | XFORK, NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
restfd(1, ofd1);
|
2012-08-17 20:34:25 +02:00
|
|
|
startlast();
|
|
|
|
/* waitlast() */
|
|
|
|
xp->split = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
xp->u.shf = shf;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (XCOM);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* perform #pattern and %pattern substitution in ${}
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
trimsub(char *str, char *pat, int how)
|
|
|
|
{
|
2007-05-13 21:14:05 +02:00
|
|
|
char *end = strnul(str);
|
2005-05-23 05:06:10 +02:00
|
|
|
char *p, c;
|
|
|
|
|
2017-05-03 17:33:16 +02:00
|
|
|
switch (how & (STYPE_CHAR | STYPE_DBL)) {
|
|
|
|
case ord('#'):
|
2011-01-21 23:25:34 +01:00
|
|
|
/* shortest match at beginning */
|
2009-11-21 23:32:08 +01:00
|
|
|
for (p = str; p <= end; p += utf_ptradj(p)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
c = *p; *p = '\0';
|
2005-05-23 17:18:17 +02:00
|
|
|
if (gmatchx(str, pat, false)) {
|
2016-08-01 23:38:07 +02:00
|
|
|
record_match(str);
|
2005-05-23 05:06:10 +02:00
|
|
|
*p = c;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (p);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
*p = c;
|
|
|
|
}
|
|
|
|
break;
|
2017-05-03 17:33:16 +02:00
|
|
|
case ord('#') | STYPE_DBL:
|
2011-01-21 23:25:34 +01:00
|
|
|
/* longest match at beginning */
|
2005-05-23 05:06:10 +02:00
|
|
|
for (p = end; p >= str; p--) {
|
|
|
|
c = *p; *p = '\0';
|
2005-05-23 17:18:17 +02:00
|
|
|
if (gmatchx(str, pat, false)) {
|
2016-08-01 23:38:07 +02:00
|
|
|
record_match(str);
|
2005-05-23 05:06:10 +02:00
|
|
|
*p = c;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (p);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
*p = c;
|
|
|
|
}
|
|
|
|
break;
|
2017-05-03 17:33:16 +02:00
|
|
|
case ord('%'):
|
2011-01-21 23:25:34 +01:00
|
|
|
/* shortest match at end */
|
2009-11-21 23:32:08 +01:00
|
|
|
p = end;
|
|
|
|
while (p >= str) {
|
|
|
|
if (gmatchx(p, pat, false))
|
|
|
|
goto trimsub_match;
|
|
|
|
if (UTFMODE) {
|
|
|
|
char *op = p;
|
2017-05-05 22:36:03 +02:00
|
|
|
while ((p-- > str) && ((rtt2asc(*p) & 0xC0) == 0x80))
|
2009-11-21 23:32:08 +01:00
|
|
|
;
|
|
|
|
if ((p < str) || (p + utf_ptradj(p) != op))
|
|
|
|
p = op - 1;
|
|
|
|
} else
|
|
|
|
--p;
|
|
|
|
}
|
|
|
|
break;
|
2017-05-03 17:33:16 +02:00
|
|
|
case ord('%') | STYPE_DBL:
|
2011-01-21 23:25:34 +01:00
|
|
|
/* longest match at end */
|
2009-11-21 23:32:08 +01:00
|
|
|
for (p = str; p <= end; p++)
|
2008-10-28 15:32:43 +01:00
|
|
|
if (gmatchx(p, pat, false)) {
|
|
|
|
trimsub_match:
|
2016-08-01 23:38:07 +02:00
|
|
|
record_match(p);
|
2008-10-28 15:32:43 +01:00
|
|
|
strndupx(end, str, p - str, ATEMP);
|
|
|
|
return (end);
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-01-21 23:25:34 +01:00
|
|
|
/* no match, return string */
|
|
|
|
return (str);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* glob
|
|
|
|
* Name derived from V6's /etc/glob, the program that expanded filenames.
|
|
|
|
*/
|
|
|
|
|
2009-10-30 01:57:39 +01:00
|
|
|
/* XXX cp not const 'cause slashes are temporarily replaced with NULs... */
|
2005-05-23 05:06:10 +02:00
|
|
|
static void
|
2012-08-24 21:02:57 +02:00
|
|
|
glob(char *cp, XPtrV *wp, bool markdirs)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
int oldsize = XPsize(*wp);
|
|
|
|
|
|
|
|
if (glob_str(cp, wp, markdirs) == 0)
|
|
|
|
XPput(*wp, debunk(cp, cp, strlen(cp) + 1));
|
|
|
|
else
|
2006-11-10 04:23:50 +01:00
|
|
|
qsort(XPptrv(*wp) + oldsize, XPsize(*wp) - oldsize,
|
2017-04-21 22:06:06 +02:00
|
|
|
sizeof(void *), ascpstrcmp);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define GF_NONE 0
|
|
|
|
#define GF_EXCHECK BIT(0) /* do existence check on file */
|
|
|
|
#define GF_GLOBBED BIT(1) /* some globbing has been done */
|
|
|
|
#define GF_MARKDIR BIT(2) /* add trailing / to directories */
|
|
|
|
|
2011-01-21 23:25:34 +01:00
|
|
|
/*
|
|
|
|
* Apply file globbing to cp and store the matching files in wp. Returns
|
2005-05-23 05:06:10 +02:00
|
|
|
* the number of matches found.
|
|
|
|
*/
|
|
|
|
int
|
2012-08-24 21:02:57 +02:00
|
|
|
glob_str(char *cp, XPtrV *wp, bool markdirs)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
int oldsize = XPsize(*wp);
|
|
|
|
XString xs;
|
|
|
|
char *xp;
|
|
|
|
|
|
|
|
Xinit(xs, xp, 256, ATEMP);
|
|
|
|
globit(&xs, &xp, cp, wp, markdirs ? GF_MARKDIR : GF_NONE);
|
|
|
|
Xfree(xs, xp);
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (XPsize(*wp) - oldsize);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
globit(XString *xs, /* dest string */
|
|
|
|
char **xpp, /* ptr to dest end */
|
|
|
|
char *sp, /* source path */
|
|
|
|
XPtrV *wp, /* output list */
|
|
|
|
int check) /* GF_* flags */
|
|
|
|
{
|
|
|
|
char *np; /* next source component */
|
|
|
|
char *xp = *xpp;
|
|
|
|
char *se;
|
|
|
|
char odirsep;
|
|
|
|
|
|
|
|
/* This to allow long expansions to be interrupted */
|
|
|
|
intrcheck();
|
|
|
|
|
2011-01-21 23:25:34 +01:00
|
|
|
if (sp == NULL) {
|
|
|
|
/* end of source path */
|
|
|
|
/*
|
|
|
|
* We only need to check if the file exists if a pattern
|
2005-05-23 05:06:10 +02:00
|
|
|
* is followed by a non-pattern (eg, foo*x/bar; no check
|
|
|
|
* is needed for foo* since the match must exist) or if
|
|
|
|
* any patterns were expanded and the markdirs option is set.
|
|
|
|
* Symlinks make things a bit tricky...
|
|
|
|
*/
|
|
|
|
if ((check & GF_EXCHECK) ||
|
|
|
|
((check & GF_MARKDIR) && (check & GF_GLOBBED))) {
|
2013-02-23 21:03:31 +01:00
|
|
|
#define stat_check() (stat_done ? stat_done : (stat_done = \
|
|
|
|
stat(Xstring(*xs, xp), &statb) < 0 ? -1 : 1))
|
2005-05-23 05:06:10 +02:00
|
|
|
struct stat lstatb, statb;
|
2013-02-23 21:03:31 +01:00
|
|
|
/* -1: failed, 1 ok, 0 not yet done */
|
|
|
|
int stat_done = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2012-05-05 00:05:02 +02:00
|
|
|
if (mksh_lstat(Xstring(*xs, xp), &lstatb) < 0)
|
2005-05-23 05:06:10 +02:00
|
|
|
return;
|
2011-01-21 23:25:34 +01:00
|
|
|
/*
|
|
|
|
* special case for systems which strip trailing
|
2005-05-23 05:06:10 +02:00
|
|
|
* slashes from regular files (eg, /etc/passwd/).
|
|
|
|
* SunOS 4.1.3 does this...
|
|
|
|
*/
|
|
|
|
if ((check & GF_EXCHECK) && xp > Xstring(*xs, xp) &&
|
2016-11-12 00:31:39 +01:00
|
|
|
mksh_cdirsep(xp[-1]) && !S_ISDIR(lstatb.st_mode) &&
|
2005-05-23 05:06:10 +02:00
|
|
|
(!S_ISLNK(lstatb.st_mode) ||
|
|
|
|
stat_check() < 0 || !S_ISDIR(statb.st_mode)))
|
|
|
|
return;
|
2011-01-21 23:25:34 +01:00
|
|
|
/*
|
|
|
|
* Possibly tack on a trailing / if there isn't already
|
2005-05-23 05:06:10 +02:00
|
|
|
* one and if the file is a directory or a symlink to a
|
|
|
|
* directory
|
|
|
|
*/
|
|
|
|
if (((check & GF_MARKDIR) && (check & GF_GLOBBED)) &&
|
2016-11-12 00:31:39 +01:00
|
|
|
xp > Xstring(*xs, xp) && !mksh_cdirsep(xp[-1]) &&
|
2005-05-23 05:06:10 +02:00
|
|
|
(S_ISDIR(lstatb.st_mode) ||
|
|
|
|
(S_ISLNK(lstatb.st_mode) && stat_check() > 0 &&
|
|
|
|
S_ISDIR(statb.st_mode)))) {
|
|
|
|
*xp++ = '/';
|
|
|
|
*xp = '\0';
|
|
|
|
}
|
|
|
|
}
|
2008-10-28 15:32:43 +01:00
|
|
|
strndupx(np, Xstring(*xs, xp), Xlength(*xs, xp), ATEMP);
|
|
|
|
XPput(*wp, np);
|
2005-05-23 05:06:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xp > Xstring(*xs, xp))
|
|
|
|
*xp++ = '/';
|
2016-11-12 00:31:39 +01:00
|
|
|
while (mksh_cdirsep(*sp)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
Xcheck(*xs, xp);
|
|
|
|
*xp++ = *sp++;
|
|
|
|
}
|
2016-11-12 00:31:39 +01:00
|
|
|
np = mksh_sdirsep(sp);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (np != NULL) {
|
|
|
|
se = np;
|
2011-01-21 23:25:34 +01:00
|
|
|
/* don't assume '/', can be multiple kinds */
|
|
|
|
odirsep = *np;
|
2005-05-23 05:06:10 +02:00
|
|
|
*np++ = '\0';
|
|
|
|
} else {
|
|
|
|
odirsep = '\0'; /* keep gcc quiet */
|
2017-04-30 00:04:31 +02:00
|
|
|
se = strnul(sp);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-21 23:25:34 +01:00
|
|
|
/*
|
|
|
|
* Check if sp needs globbing - done to avoid pattern checks for strings
|
2005-05-23 05:06:10 +02:00
|
|
|
* containing MAGIC characters, open [s without the matching close ],
|
|
|
|
* etc. (otherwise opendir() will be called which may fail because the
|
|
|
|
* directory isn't readable - if no globbing is needed, only execute
|
|
|
|
* permission should be required (as per POSIX)).
|
|
|
|
*/
|
2017-04-29 23:49:07 +02:00
|
|
|
if (!has_globbing(sp)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
XcheckN(*xs, xp, se - sp + 1);
|
|
|
|
debunk(xp, sp, Xnleft(*xs, xp));
|
2017-04-30 00:04:31 +02:00
|
|
|
xp = strnul(xp);
|
2005-05-23 05:06:10 +02:00
|
|
|
*xpp = xp;
|
|
|
|
globit(xs, xpp, np, wp, check);
|
|
|
|
} else {
|
|
|
|
DIR *dirp;
|
|
|
|
struct dirent *d;
|
|
|
|
char *name;
|
2011-08-27 20:06:52 +02:00
|
|
|
size_t len, prefix_len;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-06-10 20:12:51 +02:00
|
|
|
/* xp = *xpp; copy_non_glob() may have re-alloc'd xs */
|
2005-05-23 05:06:10 +02:00
|
|
|
*xp = '\0';
|
|
|
|
prefix_len = Xlength(*xs, xp);
|
2016-07-25 02:04:48 +02:00
|
|
|
dirp = opendir(prefix_len ? Xstring(*xs, xp) : Tdot);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (dirp == NULL)
|
|
|
|
goto Nodir;
|
|
|
|
while ((d = readdir(dirp)) != NULL) {
|
|
|
|
name = d->d_name;
|
|
|
|
if (name[0] == '.' &&
|
|
|
|
(name[1] == 0 || (name[1] == '.' && name[2] == 0)))
|
2011-01-21 23:25:34 +01:00
|
|
|
/* always ignore . and .. */
|
|
|
|
continue;
|
2005-05-23 05:06:10 +02:00
|
|
|
if ((*name == '.' && *sp != '.') ||
|
2005-05-23 17:18:17 +02:00
|
|
|
!gmatchx(name, sp, true))
|
2005-05-23 05:06:10 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
len = strlen(d->d_name) + 1;
|
|
|
|
XcheckN(*xs, xp, len);
|
|
|
|
memcpy(xp, name, len);
|
|
|
|
*xpp = xp + len - 1;
|
2017-04-30 00:04:31 +02:00
|
|
|
globit(xs, xpp, np, wp, (check & GF_MARKDIR) |
|
|
|
|
GF_GLOBBED | (np ? GF_EXCHECK : GF_NONE));
|
2005-05-23 05:06:10 +02:00
|
|
|
xp = Xstring(*xs, xp) + prefix_len;
|
|
|
|
}
|
|
|
|
closedir(dirp);
|
2006-08-01 15:43:28 +02:00
|
|
|
Nodir:
|
|
|
|
;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (np != NULL)
|
|
|
|
*--np = odirsep;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove MAGIC from string */
|
|
|
|
char *
|
|
|
|
debunk(char *dp, const char *sp, size_t dlen)
|
|
|
|
{
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
char *d;
|
|
|
|
const char *s;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
if ((s = cstrchr(sp, MAGIC))) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (s - sp >= (ssize_t)dlen)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (dp);
|
2009-12-05 23:24:35 +01:00
|
|
|
memmove(dp, sp, s - sp);
|
2005-05-23 05:06:10 +02:00
|
|
|
for (d = dp + (s - sp); *s && (d - dp < (ssize_t)dlen); s++)
|
|
|
|
if (!ISMAGIC(*s) || !(*++s & 0x80) ||
|
2017-04-28 02:38:33 +02:00
|
|
|
!ctype(*s & 0x7F, C_PATMO | C_SPC))
|
2005-05-23 05:06:10 +02:00
|
|
|
*d++ = *s;
|
|
|
|
else {
|
|
|
|
/* extended pattern operators: *+?@! */
|
|
|
|
if ((*s & 0x7f) != ' ')
|
|
|
|
*d++ = *s & 0x7f;
|
|
|
|
if (d - dp < (ssize_t)dlen)
|
|
|
|
*d++ = '(';
|
|
|
|
}
|
|
|
|
*d = '\0';
|
|
|
|
} else if (dp != sp)
|
|
|
|
strlcpy(dp, sp, dlen);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (dp);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2011-01-21 23:25:34 +01:00
|
|
|
/*
|
|
|
|
* Check if p is an unquoted name, possibly followed by a / or :. If so
|
2005-05-23 05:06:10 +02:00
|
|
|
* puts the expanded version in *dcp,dp and returns a pointer in p just
|
|
|
|
* past the name, otherwise returns 0.
|
|
|
|
*/
|
2007-03-04 01:13:17 +01:00
|
|
|
static const char *
|
2014-12-05 16:14:23 +01:00
|
|
|
maybe_expand_tilde(const char *p, XString *dsp, char **dpp, bool isassign)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
XString ts;
|
|
|
|
char *dp = *dpp;
|
2007-03-04 01:13:17 +01:00
|
|
|
char *tp;
|
|
|
|
const char *r;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
Xinit(ts, tp, 16, ATEMP);
|
|
|
|
/* : only for DOASNTILDE form */
|
2017-03-11 23:49:56 +01:00
|
|
|
while (p[0] == CHAR && /* not cdirsep */ p[1] != '/' &&
|
2016-11-12 00:31:39 +01:00
|
|
|
(!isassign || p[1] != ':')) {
|
2005-05-23 05:06:10 +02:00
|
|
|
Xcheck(ts, tp);
|
|
|
|
*tp++ = p[1];
|
|
|
|
p += 2;
|
|
|
|
}
|
|
|
|
*tp = '\0';
|
|
|
|
r = (p[0] == EOS || p[0] == CHAR || p[0] == CSUBST) ?
|
2014-11-19 22:49:12 +01:00
|
|
|
do_tilde(Xstring(ts, tp)) : NULL;
|
2005-05-23 05:06:10 +02:00
|
|
|
Xfree(ts, tp);
|
|
|
|
if (r) {
|
|
|
|
while (*r) {
|
|
|
|
Xcheck(*dsp, dp);
|
|
|
|
if (ISMAGIC(*r))
|
|
|
|
*dp++ = MAGIC;
|
|
|
|
*dp++ = *r++;
|
|
|
|
}
|
|
|
|
*dpp = dp;
|
|
|
|
r = p;
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (r);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* tilde expansion
|
|
|
|
*
|
|
|
|
* based on a version by Arnold Robbins
|
|
|
|
*/
|
2012-08-24 22:57:46 +02:00
|
|
|
char *
|
2014-11-19 22:49:12 +01:00
|
|
|
do_tilde(char *cp)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2007-01-17 19:01:51 +01:00
|
|
|
char *dp = null;
|
2016-07-25 01:05:52 +02:00
|
|
|
#ifndef MKSH_NOPWNAM
|
|
|
|
bool do_simplify = true;
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (cp[0] == '\0')
|
|
|
|
dp = str_val(global("HOME"));
|
|
|
|
else if (cp[0] == '+' && cp[1] == '\0')
|
2016-07-25 02:04:48 +02:00
|
|
|
dp = str_val(global(TPWD));
|
2015-04-29 20:32:44 +02:00
|
|
|
else if (ksh_isdash(cp))
|
2016-07-25 02:04:48 +02:00
|
|
|
dp = str_val(global(TOLDPWD));
|
2007-01-17 19:01:51 +01:00
|
|
|
#ifndef MKSH_NOPWNAM
|
2016-07-25 01:05:52 +02:00
|
|
|
else {
|
2005-05-23 05:06:10 +02:00
|
|
|
dp = homedir(cp);
|
2016-07-25 01:05:52 +02:00
|
|
|
do_simplify = false;
|
|
|
|
}
|
2007-01-17 19:01:51 +01:00
|
|
|
#endif
|
2016-07-25 01:05:52 +02:00
|
|
|
|
|
|
|
/* if parameters aren't set, don't expand ~ */
|
|
|
|
if (dp == NULL || dp == null)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
/* simplify parameters as if cwd upon entry */
|
|
|
|
#ifndef MKSH_NOPWNAM
|
|
|
|
if (do_simplify)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
strdupx(dp, dp, ATEMP);
|
|
|
|
simplify_path(dp);
|
|
|
|
}
|
|
|
|
return (dp);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2007-01-17 19:01:51 +01:00
|
|
|
#ifndef MKSH_NOPWNAM
|
2005-05-23 05:06:10 +02:00
|
|
|
/*
|
|
|
|
* map userid to user's home directory.
|
|
|
|
* note that 4.3's getpw adds more than 6K to the shell,
|
|
|
|
* and the YP version probably adds much more.
|
|
|
|
* we might consider our own version of getpwnam() to keep the size down.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
homedir(char *name)
|
|
|
|
{
|
|
|
|
struct tbl *ap;
|
|
|
|
|
2009-08-28 22:30:59 +02:00
|
|
|
ap = ktenter(&homedirs, name, hash(name));
|
2005-05-23 05:06:10 +02:00
|
|
|
if (!(ap->flag & ISSET)) {
|
|
|
|
struct passwd *pw;
|
|
|
|
|
|
|
|
pw = getpwnam(name);
|
|
|
|
if (pw == NULL)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (NULL);
|
2008-10-28 15:32:43 +01:00
|
|
|
strdupx(ap->val.s, pw->pw_dir, APERM);
|
2005-05-23 05:06:10 +02:00
|
|
|
ap->flag |= DEFINED|ISSET|ALLOC;
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (ap->val.s);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2007-01-11 01:32:31 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
alt_expand(XPtrV *wp, char *start, char *exp_start, char *end, int fdo)
|
|
|
|
{
|
2015-02-06 10:33:41 +01:00
|
|
|
unsigned int count = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
char *brace_start, *brace_end, *comma = NULL;
|
|
|
|
char *field_start;
|
2015-02-06 10:33:41 +01:00
|
|
|
char *p = exp_start;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* search for open brace */
|
2017-05-06 00:53:32 +02:00
|
|
|
while ((p = strchr(p, MAGIC)) && ord(p[1]) != ord('{' /*}*/))
|
2015-02-06 10:33:41 +01:00
|
|
|
p += 2;
|
2005-05-23 05:06:10 +02:00
|
|
|
brace_start = p;
|
|
|
|
|
|
|
|
/* find matching close brace, if any */
|
|
|
|
if (p) {
|
|
|
|
comma = NULL;
|
|
|
|
count = 1;
|
2015-02-06 10:33:41 +01:00
|
|
|
p += 2;
|
|
|
|
while (*p && count) {
|
|
|
|
if (ISMAGIC(*p++)) {
|
2017-05-06 00:53:32 +02:00
|
|
|
if (ord(*p) == ord('{' /*}*/))
|
2015-02-06 10:33:41 +01:00
|
|
|
++count;
|
2017-05-06 00:53:32 +02:00
|
|
|
else if (ord(*p) == ord(/*{*/ '}'))
|
2005-05-23 05:06:10 +02:00
|
|
|
--count;
|
|
|
|
else if (*p == ',' && count == 1)
|
|
|
|
comma = p;
|
2015-02-06 10:33:41 +01:00
|
|
|
++p;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* no valid expansions... */
|
|
|
|
if (!p || count != 0) {
|
2011-01-21 23:25:34 +01:00
|
|
|
/*
|
|
|
|
* Note that given a{{b,c} we do not expand anything (this is
|
2009-06-11 14:42:21 +02:00
|
|
|
* what AT&T ksh does. This may be changed to do the {b,c}
|
2005-05-23 05:06:10 +02:00
|
|
|
* expansion. }
|
|
|
|
*/
|
|
|
|
if (fdo & DOGLOB)
|
2012-08-24 21:02:57 +02:00
|
|
|
glob(start, wp, tobool(fdo & DOMARKDIRS));
|
2005-05-23 05:06:10 +02:00
|
|
|
else
|
|
|
|
XPput(*wp, debunk(start, start, end - start));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
brace_end = p;
|
|
|
|
if (!comma) {
|
|
|
|
alt_expand(wp, start, brace_end, end, fdo);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* expand expression */
|
|
|
|
field_start = brace_start + 2;
|
|
|
|
count = 1;
|
|
|
|
for (p = brace_start + 2; p != brace_end; p++) {
|
|
|
|
if (ISMAGIC(*p)) {
|
2017-05-06 00:53:32 +02:00
|
|
|
if (ord(*++p) == ord('{' /*}*/))
|
2015-02-06 10:33:41 +01:00
|
|
|
++count;
|
2017-05-06 00:53:32 +02:00
|
|
|
else if ((ord(*p) == ord(/*{*/ '}') && --count == 0) ||
|
2005-05-23 05:06:10 +02:00
|
|
|
(*p == ',' && count == 1)) {
|
2010-07-18 00:09:40 +02:00
|
|
|
char *news;
|
2005-05-23 05:06:10 +02:00
|
|
|
int l1, l2, l3;
|
|
|
|
|
2010-09-14 23:26:19 +02:00
|
|
|
/*
|
|
|
|
* addition safe since these operate on
|
|
|
|
* one string (separate substrings)
|
|
|
|
*/
|
2005-05-23 05:06:10 +02:00
|
|
|
l1 = brace_start - start;
|
|
|
|
l2 = (p - 1) - field_start;
|
|
|
|
l3 = end - brace_end;
|
2010-07-18 00:09:40 +02:00
|
|
|
news = alloc(l1 + l2 + l3 + 1, ATEMP);
|
|
|
|
memcpy(news, start, l1);
|
|
|
|
memcpy(news + l1, field_start, l2);
|
|
|
|
memcpy(news + l1 + l2, brace_end, l3);
|
|
|
|
news[l1 + l2 + l3] = '\0';
|
|
|
|
alt_expand(wp, news, news + l1,
|
|
|
|
news + l1 + l2 + l3, fdo);
|
2005-05-23 05:06:10 +02:00
|
|
|
field_start = p + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2013-02-11 00:30:47 +01:00
|
|
|
|
|
|
|
/* helper function due to setjmp/longjmp woes */
|
2013-05-02 23:59:54 +02:00
|
|
|
static char *
|
|
|
|
valsub(struct op *t, Area *ap)
|
|
|
|
{
|
2013-07-24 20:03:57 +02:00
|
|
|
char * volatile cp = NULL;
|
2013-07-24 14:39:28 +02:00
|
|
|
struct tbl * volatile vp = NULL;
|
2013-05-02 23:59:54 +02:00
|
|
|
|
2013-07-24 14:39:28 +02:00
|
|
|
newenv(E_FUNC);
|
2013-05-02 23:59:54 +02:00
|
|
|
newblock();
|
2013-07-24 14:39:28 +02:00
|
|
|
if (ap)
|
|
|
|
vp = local("REPLY", false);
|
2013-05-02 23:59:54 +02:00
|
|
|
if (!kshsetjmp(e->jbuf))
|
|
|
|
execute(t, XXCOM | XERROK, NULL);
|
2013-07-24 14:39:28 +02:00
|
|
|
if (vp)
|
|
|
|
strdupx(cp, str_val(vp), ap);
|
|
|
|
quitenv(NULL);
|
|
|
|
|
2013-05-02 23:59:54 +02:00
|
|
|
return (cp);
|
|
|
|
}
|