2006-05-10 20:54:13 +02:00
|
|
|
/* $OpenBSD: eval.c,v 1.30 2006/04/10 14:38:59 jaredy Exp $ */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
#include "sh.h"
|
|
|
|
|
2007-07-31 12:42:15 +02:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/eval.c,v 1.34 2007/07/31 10:42:15 tg Exp $");
|
2007-01-17 19:01:51 +01:00
|
|
|
|
|
|
|
#ifdef MKSH_SMALL
|
|
|
|
#define MKSH_NOPWNAM
|
|
|
|
#endif
|
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 */
|
|
|
|
typedef struct Expand {
|
• 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
|
|
|
/* int type; */ /* see expand() */
|
2005-05-23 05:06:10 +02:00
|
|
|
const char *str; /* string */
|
|
|
|
union {
|
|
|
|
const char **strv;/* string[] */
|
• 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
|
|
|
struct shf *shf; /* file */
|
2005-05-23 05:06:10 +02:00
|
|
|
} u; /* source */
|
|
|
|
struct tbl *var; /* variable in ${var..} */
|
• 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
|
|
|
short split; /* split "$@" / call waitlast $() */
|
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 */
|
|
|
|
#define IFS_WORD 0 /* word has chars (or quotes) */
|
|
|
|
#define IFS_WS 1 /* have seen IFS white-space */
|
|
|
|
#define IFS_NWS 2 /* have seen IFS non-white-space */
|
|
|
|
|
2007-03-04 01:13:17 +01:00
|
|
|
static int varsub(Expand *, const char *, const char *, int *, int *);
|
|
|
|
static int comsub(Expand *, const char *);
|
2006-05-08 13:07:38 +02:00
|
|
|
static char *trimsub(char *, char *, int);
|
|
|
|
static void glob(char *, XPtrV *, int);
|
|
|
|
static void globit(XString *, char **, char *, XPtrV *, int);
|
2007-03-04 01:13:17 +01:00
|
|
|
static const char *maybe_expand_tilde(const char *, XString *, char **, int);
|
2006-05-08 13:07:38 +02:00
|
|
|
static char *tilde(char *);
|
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);
|
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)
|
2007-05-13 19:51:24 +02:00
|
|
|
internal_errorf("substitute");
|
2005-05-23 05:06:10 +02:00
|
|
|
source = sold;
|
|
|
|
afree(s, ATEMP);
|
|
|
|
return evalstr(yylval.cp, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
XPput(w, NULL); /* space for shell name */
|
|
|
|
while (*ap != NULL)
|
|
|
|
expand(*ap++, &w, f);
|
|
|
|
XPput(w, NULL);
|
|
|
|
return (char **) XPclose(w) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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:
|
2007-03-04 01:13:17 +01: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 {
|
|
|
|
short stype; /* [=+-?%#] action after expanded word */
|
|
|
|
short base; /* begin position of expanded word */
|
|
|
|
short f; /* saved value of f (DOPAT, etc) */
|
|
|
|
short quote; /* saved value of quote (for ${..[%#]..}) */
|
2007-07-01 17:39:22 +02:00
|
|
|
struct tbl *var; /* variable for ${var..} */
|
2005-05-23 05:06:10 +02:00
|
|
|
struct SubType *prev; /* old type */
|
|
|
|
struct SubType *next; /* poped type (to avoid re-allocating) */
|
|
|
|
} SubType;
|
|
|
|
|
|
|
|
void
|
2007-03-04 01:13:17 +01:00
|
|
|
expand(const char *cp, /* input word */
|
2005-05-23 05:06:10 +02:00
|
|
|
XPtrV *wp, /* output words */
|
|
|
|
int f) /* DO* flags */
|
|
|
|
{
|
|
|
|
int c = 0;
|
|
|
|
int type; /* expansion type */
|
|
|
|
int quote = 0; /* quoted */
|
|
|
|
XString ds; /* destination string */
|
2007-03-04 01:13:17 +01:00
|
|
|
char *dp; /* destination */
|
|
|
|
const char *sp; /* source */
|
2005-05-23 05:06:10 +02:00
|
|
|
int fdo, word; /* second pass flags; have word */
|
|
|
|
int doblank; /* field splitting of parameter/command subst */
|
2005-07-12 15:30:38 +02:00
|
|
|
Expand x = { /* expansion variables */
|
|
|
|
NULL, { NULL }, NULL, 0
|
|
|
|
};
|
2005-05-23 05:06:10 +02:00
|
|
|
SubType st_head, *st;
|
|
|
|
int newlines = 0; /* For trailing newlines in COMSUB */
|
|
|
|
int saw_eq, tilde_ok;
|
|
|
|
int make_magic;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (cp == 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 */
|
|
|
|
if ((f & DOVACHECK) && is_wdvarassign(cp)) {
|
|
|
|
f &= ~(DOVACHECK|DOBLANK|DOGLOB|DOTILDE);
|
|
|
|
f |= DOASNTILDE;
|
|
|
|
}
|
|
|
|
if (Flag(FNOGLOB))
|
|
|
|
f &= ~DOGLOB;
|
|
|
|
if (Flag(FMARKDIRS))
|
|
|
|
f |= DOMARKDIRS;
|
|
|
|
if (Flag(FBRACEEXPAND) && (f & DOGLOB))
|
|
|
|
f |= DOBRACE_;
|
|
|
|
|
|
|
|
Xinit(ds, dp, 128, ATEMP); /* init dest. string */
|
|
|
|
type = XBASE;
|
|
|
|
sp = cp;
|
|
|
|
fdo = 0;
|
|
|
|
saw_eq = 0;
|
|
|
|
tilde_ok = (f & (DOTILDE|DOASNTILDE)) ? 1 : 0; /* must be 1/0 */
|
|
|
|
doblank = 0;
|
|
|
|
make_magic = 0;
|
|
|
|
word = (f&DOBLANK) ? IFS_WS : IFS_WORD;
|
|
|
|
st_head.next = NULL;
|
|
|
|
st = &st_head;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
Xcheck(ds, dp);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case XBASE: /* original prefixed string */
|
|
|
|
c = *sp++;
|
|
|
|
switch (c) {
|
|
|
|
case EOS:
|
|
|
|
c = 0;
|
|
|
|
break;
|
|
|
|
case CHAR:
|
|
|
|
c = *sp++;
|
|
|
|
break;
|
|
|
|
case QCHAR:
|
|
|
|
quote |= 2; /* temporary quote */
|
|
|
|
c = *sp++;
|
|
|
|
break;
|
|
|
|
case OQUOTE:
|
|
|
|
word = IFS_WORD;
|
|
|
|
tilde_ok = 0;
|
|
|
|
quote = 1;
|
|
|
|
continue;
|
|
|
|
case CQUOTE:
|
|
|
|
quote = 0;
|
|
|
|
continue;
|
|
|
|
case COMSUB:
|
|
|
|
tilde_ok = 0;
|
|
|
|
if (f & DONTRUNCOMMAND) {
|
|
|
|
word = IFS_WORD;
|
|
|
|
*dp++ = '$'; *dp++ = '(';
|
|
|
|
while (*sp != '\0') {
|
|
|
|
Xcheck(ds, dp);
|
|
|
|
*dp++ = *sp++;
|
|
|
|
}
|
|
|
|
*dp++ = ')';
|
|
|
|
} else {
|
|
|
|
type = comsub(&x, sp);
|
|
|
|
if (type == XCOM && (f&DOBLANK))
|
|
|
|
doblank++;
|
2007-05-13 21:14:05 +02:00
|
|
|
sp = strnul(sp) + 1;
|
2005-05-23 05:06:10 +02:00
|
|
|
newlines = 0;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
case EXPRSUB:
|
|
|
|
word = IFS_WORD;
|
|
|
|
tilde_ok = 0;
|
|
|
|
if (f & DONTRUNCOMMAND) {
|
|
|
|
*dp++ = '$'; *dp++ = '('; *dp++ = '(';
|
|
|
|
while (*sp != '\0') {
|
|
|
|
Xcheck(ds, dp);
|
|
|
|
*dp++ = *sp++;
|
|
|
|
}
|
|
|
|
*dp++ = ')'; *dp++ = ')';
|
|
|
|
} else {
|
|
|
|
struct tbl v;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
v.flag = DEFINED|ISSET|INTEGER;
|
|
|
|
v.type = 10; /* not default */
|
|
|
|
v.name[0] = '\0';
|
|
|
|
v_evaluate(&v, substitute(sp, 0),
|
|
|
|
KSH_UNWIND_ERROR, true);
|
2007-05-13 21:14:05 +02:00
|
|
|
sp = strnul(sp) + 1;
|
2005-05-23 05:06:10 +02:00
|
|
|
for (p = str_val(&v); *p; ) {
|
|
|
|
Xcheck(ds, dp);
|
|
|
|
*dp++ = *p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
case OSUBST: /* ${{#}var{:}[=+-?#%]word} */
|
|
|
|
/* format is:
|
|
|
|
* OSUBST [{x] plain-variable-part \0
|
|
|
|
* compiled-word-part CSUBST [}x]
|
|
|
|
* This is where all syntax checking gets done...
|
|
|
|
*/
|
|
|
|
{
|
2007-03-04 01:13:17 +01:00
|
|
|
const char *varname = ++sp; /* skip the { or x (}) */
|
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
|
|
|
|
• 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
|
|
|
sp = cstrchr(sp, '\0') + 1; /* skip variable */
|
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;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2007-07-06 04:22:57 +02:00
|
|
|
unwind_substsyn:
|
2005-05-23 05:06:10 +02:00
|
|
|
sp = varname - 2; /* restore sp */
|
2007-03-14 03:41:09 +01:00
|
|
|
end = (beg = wdcopy(sp, ATEMP)) +
|
2007-03-04 01:13:17 +01:00
|
|
|
(wdscan(sp, CSUBST) - sp);
|
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;
|
2007-03-04 01:13:17 +01:00
|
|
|
str = snptreef(NULL, 64, "%S", beg);
|
|
|
|
afree(beg, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
errorf("%s: bad substitution", str);
|
|
|
|
}
|
2007-03-04 01:13:17 +01:00
|
|
|
if (f & DOBLANK)
|
2005-05-23 05:06:10 +02:00
|
|
|
doblank++;
|
|
|
|
tilde_ok = 0;
|
|
|
|
if (type == XBASE) { /* expand? */
|
|
|
|
if (!st->next) {
|
|
|
|
SubType *newst;
|
|
|
|
|
|
|
|
newst = (SubType *) alloc(
|
|
|
|
sizeof(SubType), ATEMP);
|
|
|
|
newst->next = NULL;
|
|
|
|
newst->prev = st;
|
|
|
|
st->next = newst;
|
|
|
|
}
|
|
|
|
st = st->next;
|
|
|
|
st->stype = stype;
|
|
|
|
st->base = Xsavepos(ds, dp);
|
|
|
|
st->f = f;
|
|
|
|
st->var = x.var;
|
|
|
|
st->quote = quote;
|
|
|
|
/* skip qualifier(s) */
|
|
|
|
if (stype)
|
|
|
|
sp += slen;
|
|
|
|
switch (stype & 0x7f) {
|
2007-07-06 03:53:36 +02:00
|
|
|
case '0': {
|
|
|
|
char *beg, *mid, *end, *stg;
|
|
|
|
long from = 0, num = -1, flen;
|
|
|
|
|
|
|
|
/* ! DOBLANK,DOBRACE_,DOTILDE */
|
|
|
|
f = DOPAT | (f&DONTRUNCOMMAND) |
|
|
|
|
DOTEMP_;
|
|
|
|
quote = 0;
|
|
|
|
beg = wdcopy(sp, ATEMP);
|
|
|
|
mid = beg + (wdscan(sp, ADELIM) - sp);
|
2007-07-06 04:22:57 +02:00
|
|
|
stg = beg + (wdscan(sp, CSUBST) - sp);
|
|
|
|
if (mid >= stg)
|
|
|
|
goto unwind_substsyn;
|
2007-07-06 03:53:36 +02:00
|
|
|
mid[-2] = EOS;
|
|
|
|
if (mid[-1] == /*{*/'}') {
|
|
|
|
sp += mid - beg - 1;
|
|
|
|
end = NULL;
|
|
|
|
} else {
|
|
|
|
end = mid +
|
|
|
|
(wdscan(mid, ADELIM) - mid);
|
2007-07-06 04:22:57 +02:00
|
|
|
if (end >= stg)
|
|
|
|
goto unwind_substsyn;
|
2007-07-06 03:53:36 +02:00
|
|
|
end[-2] = EOS;
|
|
|
|
sp += end - beg - 1;
|
|
|
|
}
|
2007-07-06 04:22:57 +02:00
|
|
|
evaluate(substitute(stg = wdstrip(beg), 0),
|
|
|
|
&from, KSH_UNWIND_ERROR, true);
|
2007-07-06 03:53:36 +02:00
|
|
|
afree(stg, ATEMP);
|
|
|
|
if (end) {
|
|
|
|
evaluate(stg = wdstrip(mid),
|
|
|
|
&num, KSH_UNWIND_ERROR, true);
|
|
|
|
afree(stg, ATEMP);
|
|
|
|
}
|
|
|
|
afree(beg, ATEMP);
|
|
|
|
beg = str_val(st->var);
|
|
|
|
flen = strlen(beg);
|
|
|
|
if (from < 0) {
|
|
|
|
if (-from < flen)
|
|
|
|
beg += flen + from;
|
|
|
|
} else
|
|
|
|
beg += from < flen ? from : flen;
|
|
|
|
flen = strlen(beg);
|
|
|
|
if (num < 0 || num > flen)
|
|
|
|
num = flen;
|
|
|
|
x.str = str_nsave(beg, num, ATEMP);
|
|
|
|
goto do_CSUBST;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
case '#':
|
|
|
|
case '%':
|
|
|
|
/* ! DOBLANK,DOBRACE_,DOTILDE */
|
|
|
|
f = DOPAT | (f&DONTRUNCOMMAND) |
|
|
|
|
DOTEMP_;
|
|
|
|
quote = 0;
|
|
|
|
/* Prepend open pattern (so |
|
|
|
|
* in a trim will work as
|
|
|
|
* expected)
|
|
|
|
*/
|
|
|
|
*dp++ = MAGIC;
|
|
|
|
*dp++ = (char)('@' | 0x80);
|
|
|
|
break;
|
|
|
|
case '=':
|
|
|
|
/* Enabling tilde expansion
|
|
|
|
* after :s here is
|
|
|
|
* non-standard ksh, but is
|
|
|
|
* consistent with rules for
|
|
|
|
* other assignments. Not
|
|
|
|
* sure what POSIX thinks of
|
|
|
|
* this.
|
|
|
|
* 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))
|
|
|
|
f |= DOASNTILDE|DOTILDE;
|
|
|
|
f |= DOTEMP_;
|
|
|
|
/* These will be done after the
|
|
|
|
* value has been assigned.
|
|
|
|
*/
|
|
|
|
f &= ~(DOBLANK|DOGLOB|DOBRACE_);
|
|
|
|
tilde_ok = 1;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
f &= ~DOBLANK;
|
|
|
|
f |= DOTEMP_;
|
2005-06-09 00:34:03 +02:00
|
|
|
/* FALLTHRU */
|
2005-05-23 05:06:10 +02:00
|
|
|
default:
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
case CSUBST: /* only get here if expanding word */
|
2007-07-06 03:53:36 +02:00
|
|
|
do_CSUBST:
|
2005-05-23 05:06:10 +02:00
|
|
|
sp++; /* ({) skip the } or x */
|
|
|
|
tilde_ok = 0; /* in case of ${unset:-} */
|
|
|
|
*dp = '\0';
|
|
|
|
quote = st->quote;
|
|
|
|
f = st->f;
|
|
|
|
if (f&DOBLANK)
|
|
|
|
doblank--;
|
|
|
|
switch (st->stype&0x7f) {
|
|
|
|
case '#':
|
|
|
|
case '%':
|
|
|
|
/* Append end-pattern */
|
|
|
|
*dp++ = MAGIC; *dp++ = ')'; *dp = '\0';
|
|
|
|
dp = Xrestpos(ds, dp, st->base);
|
|
|
|
/* Must use st->var since calling
|
|
|
|
* global would break things
|
|
|
|
* like x[i+=1].
|
|
|
|
*/
|
|
|
|
x.str = trimsub(str_val(st->var),
|
|
|
|
dp, st->stype);
|
|
|
|
type = XSUB;
|
|
|
|
if (f&DOBLANK)
|
|
|
|
doblank++;
|
|
|
|
st = st->prev;
|
|
|
|
continue;
|
|
|
|
case '=':
|
|
|
|
/* Restore our position and substitute
|
|
|
|
* 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);
|
|
|
|
/* Must use st->var since calling
|
|
|
|
* global would cause with things
|
|
|
|
* like x[i+=1] to be evaluated twice.
|
|
|
|
*/
|
|
|
|
/* Note: not exported by FEXPORT
|
|
|
|
* in at&t ksh.
|
|
|
|
*/
|
|
|
|
/* XXX POSIX says readonly is only
|
|
|
|
* fatal for special builtins (setstr
|
|
|
|
* does readonly check).
|
|
|
|
*/
|
|
|
|
len = strlen(dp) + 1;
|
|
|
|
setstr(st->var,
|
|
|
|
debunk((char *) alloc(len, ATEMP),
|
|
|
|
dp, len), KSH_UNWIND_ERROR);
|
|
|
|
x.str = str_val(st->var);
|
|
|
|
type = XSUB;
|
|
|
|
if (f&DOBLANK)
|
|
|
|
doblank++;
|
|
|
|
st = st->prev;
|
|
|
|
continue;
|
|
|
|
case '?':
|
|
|
|
{
|
|
|
|
char *s = Xrestpos(ds, dp, st->base);
|
|
|
|
|
|
|
|
errorf("%s: %s", st->var->name,
|
|
|
|
dp == s ?
|
|
|
|
"parameter null or not set" :
|
|
|
|
(debunk(s, s, strlen(s) + 1), s));
|
|
|
|
}
|
2007-06-28 01:12:59 +02:00
|
|
|
case '0':
|
2007-07-06 03:53:36 +02:00
|
|
|
dp = Xrestpos(ds, dp, st->base);
|
|
|
|
type = XSUB;
|
|
|
|
if (f&DOBLANK)
|
|
|
|
doblank++;
|
|
|
|
st = st->prev;
|
|
|
|
continue;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
st = st->prev;
|
|
|
|
type = XBASE;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case OPAT: /* open pattern: *(foo|bar) */
|
|
|
|
/* Next char is the type of pattern */
|
|
|
|
make_magic = 1;
|
|
|
|
c = *sp++ + 0x80;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SPAT: /* pattern separator (|) */
|
|
|
|
make_magic = 1;
|
|
|
|
c = '|';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CPAT: /* close pattern */
|
|
|
|
make_magic = 1;
|
|
|
|
c = /*(*/ ')';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XNULLSUB:
|
|
|
|
/* Special case for "$@" (and "${foo[@]}") - no
|
|
|
|
* word is generated if $# is 0 (unless there is
|
|
|
|
* other stuff inside the quotes).
|
|
|
|
*/
|
|
|
|
type = XBASE;
|
|
|
|
if (f&DOBLANK) {
|
|
|
|
doblank--;
|
|
|
|
/* not really correct: x=; "$x$@" should
|
|
|
|
* generate a null argument and
|
|
|
|
* set A; "${@:+}" shouldn't.
|
|
|
|
*/
|
|
|
|
if (dp == Xstring(ds, dp))
|
|
|
|
word = IFS_WS;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case XSUB:
|
|
|
|
case XSUBMID:
|
|
|
|
if ((c = *x.str++) == 0) {
|
|
|
|
type = XBASE;
|
|
|
|
if (f&DOBLANK)
|
|
|
|
doblank--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XARGSEP:
|
|
|
|
type = XARG;
|
|
|
|
quote = 1;
|
|
|
|
case XARG:
|
|
|
|
if ((c = *x.str++) == '\0') {
|
|
|
|
/* force null words to be created so
|
|
|
|
* set -- '' 2 ''; foo "$@" will do
|
|
|
|
* the right thing
|
|
|
|
*/
|
|
|
|
if (quote && x.split)
|
|
|
|
word = IFS_WORD;
|
|
|
|
if ((x.str = *x.u.strv++) == NULL) {
|
|
|
|
type = XBASE;
|
|
|
|
if (f&DOBLANK)
|
|
|
|
doblank--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
c = ifs0;
|
|
|
|
if (c == 0) {
|
|
|
|
if (quote && !x.split)
|
|
|
|
continue;
|
|
|
|
c = ' ';
|
|
|
|
}
|
|
|
|
if (quote && x.split) {
|
|
|
|
/* terminate word for "$@" */
|
|
|
|
type = XARGSEP;
|
|
|
|
quote = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XCOM:
|
|
|
|
if (newlines) { /* Spit out saved NLs */
|
|
|
|
c = '\n';
|
|
|
|
--newlines;
|
|
|
|
} else {
|
|
|
|
while ((c = shf_getc(x.u.shf)) == 0 || c == '\n')
|
|
|
|
if (c == '\n')
|
|
|
|
newlines++; /* Save newlines */
|
|
|
|
if (newlines && c != EOF) {
|
|
|
|
shf_ungetc(c, x.u.shf);
|
|
|
|
c = '\n';
|
|
|
|
--newlines;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (c == EOF) {
|
|
|
|
newlines = 0;
|
|
|
|
shf_close(x.u.shf);
|
|
|
|
if (x.split)
|
|
|
|
subst_exstat = waitlast();
|
|
|
|
type = XBASE;
|
|
|
|
if (f&DOBLANK)
|
|
|
|
doblank--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check for end of word or IFS separation */
|
|
|
|
if (c == 0 || (!quote && (f & DOBLANK) && doblank &&
|
|
|
|
!make_magic && ctype(c, C_IFS))) {
|
|
|
|
/* How words are broken up:
|
|
|
|
* | value of c
|
|
|
|
* word | ws nws 0
|
|
|
|
* -----------------------------------
|
|
|
|
* IFS_WORD w/WS w/NWS w
|
|
|
|
* IFS_WS -/WS w/NWS -
|
|
|
|
* IFS_NWS -/NWS w/NWS w
|
|
|
|
* (w means generate a word)
|
|
|
|
* Note that IFS_NWS/0 generates a word (at&t ksh
|
|
|
|
* doesn't do this, but POSIX does).
|
|
|
|
*/
|
|
|
|
if (word == IFS_WORD ||
|
|
|
|
(!ctype(c, C_IFSWS) && c && word == IFS_NWS)) {
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
*dp++ = '\0';
|
|
|
|
p = Xclose(ds, dp);
|
|
|
|
if (fdo & DOBRACE_)
|
|
|
|
/* also does globbing */
|
|
|
|
alt_expand(wp, p, p,
|
|
|
|
p + Xlength(ds, (dp - 1)),
|
|
|
|
fdo | (f & DOMARKDIRS));
|
|
|
|
else if (fdo & DOGLOB)
|
|
|
|
glob(p, wp, f & DOMARKDIRS);
|
|
|
|
else if ((f & DOPAT) || !(fdo & DOMAGIC_))
|
|
|
|
XPput(*wp, p);
|
|
|
|
else
|
|
|
|
XPput(*wp, debunk(p, p, strlen(p) + 1));
|
|
|
|
fdo = 0;
|
|
|
|
saw_eq = 0;
|
|
|
|
tilde_ok = (f & (DOTILDE|DOASNTILDE)) ? 1 : 0;
|
|
|
|
if (c != 0)
|
|
|
|
Xinit(ds, dp, 128, ATEMP);
|
|
|
|
}
|
|
|
|
if (c == 0)
|
|
|
|
return;
|
|
|
|
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) {
|
|
|
|
char *p;
|
|
|
|
|
2006-11-10 07:40:05 +01:00
|
|
|
if ((p = str_nsave(null, 0, ATEMP))
|
|
|
|
== NULL)
|
2007-05-13 19:51:24 +02:00
|
|
|
internal_errorf("unable "
|
2005-05-23 05:06:10 +02:00
|
|
|
"to allocate memory");
|
|
|
|
XPput(*wp, p);
|
|
|
|
}
|
|
|
|
type = XSUBMID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* age tilde_ok info - ~ code tests second bit */
|
|
|
|
tilde_ok <<= 1;
|
|
|
|
/* mark any special second pass chars */
|
|
|
|
if (!quote)
|
|
|
|
switch (c) {
|
|
|
|
case '[':
|
|
|
|
case NOT:
|
|
|
|
case '-':
|
|
|
|
case ']':
|
|
|
|
/* For character classes - doesn't hurt
|
|
|
|
* to have magic !,-,]s outside of
|
|
|
|
* [...] expressions.
|
|
|
|
*/
|
|
|
|
if (f & (DOPAT | DOGLOB)) {
|
|
|
|
fdo |= DOMAGIC_;
|
|
|
|
if (c == '[')
|
|
|
|
fdo |= f & DOGLOB;
|
|
|
|
*dp++ = MAGIC;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
case '?':
|
|
|
|
if (f & (DOPAT | DOGLOB)) {
|
|
|
|
fdo |= DOMAGIC_ | (f & DOGLOB);
|
|
|
|
*dp++ = MAGIC;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OBRACE:
|
|
|
|
case ',':
|
|
|
|
case CBRACE:
|
|
|
|
if ((f & DOBRACE_) && (c == OBRACE ||
|
|
|
|
(fdo & DOBRACE_))) {
|
|
|
|
fdo |= DOBRACE_|DOMAGIC_;
|
|
|
|
*dp++ = MAGIC;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '=':
|
|
|
|
/* Note first unquoted = for ~ */
|
2005-07-04 14:27:28 +02:00
|
|
|
if (!(f & DOTEMP_) && !saw_eq &&
|
|
|
|
(Flag(FBRACEEXPAND) ||
|
|
|
|
(f & DOASNTILDE))) {
|
2005-05-23 05:06:10 +02:00
|
|
|
saw_eq = 1;
|
|
|
|
tilde_ok = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ':': /* : */
|
|
|
|
/* Note unquoted : for ~ */
|
|
|
|
if (!(f & DOTEMP_) && (f & DOASNTILDE))
|
|
|
|
tilde_ok = 1;
|
|
|
|
break;
|
|
|
|
case '~':
|
|
|
|
/* tilde_ok is reset whenever
|
|
|
|
* any of ' " $( $(( ${ } are seen.
|
|
|
|
* Note that tilde_ok must be preserved
|
|
|
|
* through the sequence ${A=a=}~
|
|
|
|
*/
|
|
|
|
if (type == XBASE &&
|
|
|
|
(f & (DOTILDE|DOASNTILDE)) &&
|
|
|
|
(tilde_ok & 2)) {
|
2007-03-04 01:13:17 +01:00
|
|
|
const char *p;
|
|
|
|
char *dp_x;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
dp_x = dp;
|
|
|
|
p = maybe_expand_tilde(sp,
|
|
|
|
&ds, &dp_x,
|
|
|
|
f & DOASNTILDE);
|
|
|
|
if (p) {
|
|
|
|
if (dp != dp_x)
|
|
|
|
word = IFS_WORD;
|
|
|
|
dp = dp_x;
|
|
|
|
sp = p;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
quote &= ~2; /* undo temporary */
|
|
|
|
|
|
|
|
if (make_magic) {
|
|
|
|
make_magic = 0;
|
|
|
|
fdo |= DOMAGIC_ | (f & DOGLOB);
|
|
|
|
*dp++ = MAGIC;
|
|
|
|
} else if (ISMAGIC(c)) {
|
|
|
|
fdo |= DOMAGIC_;
|
|
|
|
*dp++ = MAGIC;
|
|
|
|
}
|
|
|
|
*dp++ = c; /* save output char */
|
|
|
|
word = IFS_WORD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 */
|
|
|
|
int slen;
|
2007-03-04 01:13:17 +01:00
|
|
|
const char *p;
|
2005-05-23 05:06:10 +02:00
|
|
|
struct tbl *vp;
|
|
|
|
|
|
|
|
if (sp[0] == '\0') /* Bad variable name */
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
xp->var = NULL;
|
|
|
|
|
|
|
|
/* ${#var}, string length or array size */
|
|
|
|
if (sp[0] == '#' && (c = sp[1]) != '\0') {
|
|
|
|
int zero_ok = 0;
|
|
|
|
|
|
|
|
/* Can't have any modifiers for ${#...} */
|
|
|
|
if (*word != CSUBST)
|
|
|
|
return -1;
|
|
|
|
sp++;
|
|
|
|
/* Check for size of array */
|
2007-03-04 01:13:17 +01:00
|
|
|
if ((p = cstrchr(sp, '[')) && (p[1] == '*' || p[1] == '@') &&
|
|
|
|
p[2] == ']') {
|
2005-05-23 05:06:10 +02:00
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
vp = global(arrayname(sp));
|
|
|
|
if (vp->flag & (ISSET|ARRAY))
|
|
|
|
zero_ok = 1;
|
|
|
|
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;
|
2005-05-23 05:06:10 +02:00
|
|
|
} else if (c == '*' || c == '@')
|
|
|
|
c = e->loc->argc;
|
|
|
|
else {
|
|
|
|
p = str_val(global(sp));
|
|
|
|
zero_ok = p != null;
|
|
|
|
c = strlen(p);
|
|
|
|
}
|
|
|
|
if (Flag(FNOUNSET) && c == 0 && !zero_ok)
|
|
|
|
errorf("%s: parameter not set", sp);
|
|
|
|
*stypep = 0; /* unqualified variable/string substitution */
|
2006-11-10 01:09:27 +01:00
|
|
|
xp->str = shf_smprintf("%lu", (unsigned long)c);
|
2005-05-23 05:06:10 +02:00
|
|
|
return XSUB;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for qualifiers in word part */
|
|
|
|
stype = 0;
|
|
|
|
c = word[slen = 0] == CHAR ? word[1] : 0;
|
|
|
|
if (c == ':') {
|
|
|
|
slen += 2;
|
|
|
|
stype = 0x80;
|
|
|
|
c = word[slen + 0] == CHAR ? word[slen + 1] : 0;
|
|
|
|
}
|
2007-07-06 04:39:37 +02:00
|
|
|
if (stype == 0x80 && (c == ' ' || c == '0')) {
|
2007-06-28 01:12:59 +02:00
|
|
|
stype |= '0';
|
|
|
|
} else if (ctype(c, C_SUBOP1)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
slen += 2;
|
|
|
|
stype |= c;
|
|
|
|
} else if (ctype(c, C_SUBOP2)) { /* Note: ksh88 allows :%, :%%, etc */
|
|
|
|
slen += 2;
|
|
|
|
stype = c;
|
|
|
|
if (word[slen + 0] == CHAR && c == word[slen + 1]) {
|
|
|
|
stype |= 0x80;
|
|
|
|
slen += 2;
|
|
|
|
}
|
|
|
|
} else if (stype) /* : is not ok */
|
|
|
|
return -1;
|
|
|
|
if (!stype && *word != CSUBST)
|
|
|
|
return -1;
|
|
|
|
*stypep = stype;
|
|
|
|
*slenp = slen;
|
|
|
|
|
|
|
|
c = sp[0];
|
|
|
|
if (c == '*' || c == '@') {
|
|
|
|
switch (stype & 0x7f) {
|
|
|
|
case '=': /* can't assign to a vector */
|
|
|
|
case '%': /* can't trim a vector (yet) */
|
|
|
|
case '#':
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (e->loc->argc == 0) {
|
|
|
|
xp->str = null;
|
|
|
|
xp->var = global(sp);
|
|
|
|
state = c == '@' ? XNULLSUB : XSUB;
|
|
|
|
} else {
|
|
|
|
xp->u.strv = (const char **) e->loc->argv + 1;
|
|
|
|
xp->str = *xp->u.strv++;
|
|
|
|
xp->split = c == '@'; /* $@ */
|
|
|
|
state = XARG;
|
|
|
|
}
|
|
|
|
} else {
|
2007-03-04 01:13:17 +01:00
|
|
|
if ((p = cstrchr(sp, '[')) && (p[1] == '*' || p[1] == '@') &&
|
|
|
|
p[2] == ']') {
|
2005-05-23 05:06:10 +02:00
|
|
|
XPtrV wv;
|
|
|
|
|
|
|
|
switch (stype & 0x7f) {
|
|
|
|
case '=': /* can't assign to a vector */
|
|
|
|
case '%': /* can't trim a vector (yet) */
|
|
|
|
case '#':
|
|
|
|
case '?':
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
XPinit(wv, 32);
|
|
|
|
vp = global(arrayname(sp));
|
|
|
|
for (; vp; vp = vp->u.array) {
|
|
|
|
if (!(vp->flag&ISSET))
|
|
|
|
continue;
|
|
|
|
XPput(wv, str_val(vp));
|
|
|
|
}
|
|
|
|
if (XPsize(wv) == 0) {
|
|
|
|
xp->str = null;
|
|
|
|
state = p[1] == '@' ? XNULLSUB : XSUB;
|
|
|
|
XPfree(wv);
|
|
|
|
} else {
|
|
|
|
XPput(wv, 0);
|
|
|
|
xp->u.strv = (const char **) XPptrv(wv);
|
|
|
|
xp->str = *xp->u.strv++;
|
|
|
|
xp->split = p[1] == '@'; /* ${foo[@]} */
|
|
|
|
state = XARG;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Can't assign things like $! or $1 */
|
|
|
|
if ((stype & 0x7f) == '=' &&
|
2006-11-10 08:52:04 +01:00
|
|
|
ctype(*sp, C_VAR1 | C_DIGIT))
|
2005-05-23 05:06:10 +02:00
|
|
|
return -1;
|
|
|
|
xp->var = global(sp);
|
|
|
|
xp->str = str_val(xp->var);
|
|
|
|
state = XSUB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c = stype&0x7f;
|
|
|
|
/* test the compiler's code generator */
|
2007-06-28 01:12:59 +02:00
|
|
|
if (ctype(c, C_SUBOP2) || stype == (0x80 | '0') ||
|
2005-05-23 05:06:10 +02:00
|
|
|
(((stype&0x80) ? *xp->str=='\0' : xp->str==null) ? /* undef? */
|
|
|
|
c == '=' || c == '-' || c == '?' : c == '+'))
|
|
|
|
state = XBASE; /* expand word instead of variable value */
|
|
|
|
if (Flag(FNOUNSET) && xp->str == null &&
|
|
|
|
(ctype(c, C_SUBOP2) || (state != XBASE && c != '+')))
|
|
|
|
errorf("%s: parameter not set", sp);
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run the command in $(...) and read its output.
|
|
|
|
*/
|
|
|
|
static int
|
2007-03-04 01:13:17 +01:00
|
|
|
comsub(Expand *xp, const char *cp)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
Source *s, *sold;
|
|
|
|
struct op *t;
|
|
|
|
struct shf *shf;
|
|
|
|
|
|
|
|
s = pushs(SSTRING, ATEMP);
|
|
|
|
s->start = s->str = cp;
|
|
|
|
sold = source;
|
|
|
|
t = compile(s);
|
|
|
|
source = sold;
|
|
|
|
|
|
|
|
if (t == NULL)
|
|
|
|
return XBASE;
|
|
|
|
|
|
|
|
if (t != NULL && t->type == TCOM && /* $(<file) */
|
|
|
|
*t->args == NULL && *t->vars == NULL && t->ioact != NULL) {
|
|
|
|
struct ioword *io = *t->ioact;
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
if ((io->flag&IOTYPE) != IOREAD)
|
|
|
|
errorf("funny $() command: %s",
|
|
|
|
snptreef(NULL, 32, "%R", io));
|
|
|
|
shf = shf_open(name = evalstr(io->name, DOTILDE), O_RDONLY, 0,
|
|
|
|
SHF_MAPHI|SHF_CLEXEC);
|
|
|
|
if (shf == NULL)
|
|
|
|
errorf("%s: cannot open $() input", name);
|
|
|
|
xp->split = 0; /* no waitlast() */
|
|
|
|
} else {
|
|
|
|
int ofd1, pv[2];
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
execute(t, XFORK|XXCOM|XPIPEO);
|
|
|
|
restfd(1, ofd1);
|
|
|
|
startlast();
|
|
|
|
xp->split = 1; /* waitlast() */
|
|
|
|
}
|
|
|
|
|
|
|
|
xp->u.shf = shf;
|
|
|
|
return XCOM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
switch (how&0xff) { /* UCHAR_MAX maybe? */
|
|
|
|
case '#': /* shortest at beginning */
|
|
|
|
for (p = str; p <= end; p++) {
|
|
|
|
c = *p; *p = '\0';
|
2005-05-23 17:18:17 +02:00
|
|
|
if (gmatchx(str, pat, false)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
*p = c;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
*p = c;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '#'|0x80: /* longest match at beginning */
|
|
|
|
for (p = end; p >= str; p--) {
|
|
|
|
c = *p; *p = '\0';
|
2005-05-23 17:18:17 +02:00
|
|
|
if (gmatchx(str, pat, false)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
*p = c;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
*p = c;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '%': /* shortest match at end */
|
|
|
|
for (p = end; p >= str; p--) {
|
2005-05-23 17:18:17 +02:00
|
|
|
if (gmatchx(p, pat, false))
|
2005-05-23 05:06:10 +02:00
|
|
|
return str_nsave(str, p - str, ATEMP);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '%'|0x80: /* longest match at end */
|
|
|
|
for (p = str; p <= end; p++) {
|
2005-05-23 17:18:17 +02:00
|
|
|
if (gmatchx(p, pat, false))
|
2005-05-23 05:06:10 +02:00
|
|
|
return str_nsave(str, p - str, ATEMP);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str; /* no match, return string */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* glob
|
|
|
|
* Name derived from V6's /etc/glob, the program that expanded filenames.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* XXX cp not const 'cause slashes are temporarily replaced with nulls... */
|
|
|
|
static void
|
|
|
|
glob(char *cp, XPtrV *wp, int markdirs)
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
sizeof (void *), xstrcmp);
|
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 */
|
|
|
|
|
|
|
|
/* Apply file globbing to cp and store the matching files in wp. Returns
|
|
|
|
* the number of matches found.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
glob_str(char *cp, XPtrV *wp, int markdirs)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
return XPsize(*wp) - oldsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
if (sp == NULL) { /* end of source path */
|
|
|
|
/* We only need to check if the file exists if a pattern
|
|
|
|
* 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))) {
|
|
|
|
#define stat_check() (stat_done ? stat_done : \
|
|
|
|
(stat_done = stat(Xstring(*xs, xp), &statb) < 0 \
|
|
|
|
? -1 : 1))
|
|
|
|
struct stat lstatb, statb;
|
|
|
|
int stat_done = 0; /* -1: failed, 1 ok */
|
|
|
|
|
|
|
|
if (lstat(Xstring(*xs, xp), &lstatb) < 0)
|
|
|
|
return;
|
|
|
|
/* special case for systems which strip trailing
|
|
|
|
* slashes from regular files (eg, /etc/passwd/).
|
|
|
|
* SunOS 4.1.3 does this...
|
|
|
|
*/
|
|
|
|
if ((check & GF_EXCHECK) && xp > Xstring(*xs, xp) &&
|
|
|
|
xp[-1] == '/' && !S_ISDIR(lstatb.st_mode) &&
|
|
|
|
(!S_ISLNK(lstatb.st_mode) ||
|
|
|
|
stat_check() < 0 || !S_ISDIR(statb.st_mode)))
|
|
|
|
return;
|
|
|
|
/* Possibly tack on a trailing / if there isn't already
|
|
|
|
* one and if the file is a directory or a symlink to a
|
|
|
|
* directory
|
|
|
|
*/
|
|
|
|
if (((check & GF_MARKDIR) && (check & GF_GLOBBED)) &&
|
|
|
|
xp > Xstring(*xs, xp) && xp[-1] != '/' &&
|
|
|
|
(S_ISDIR(lstatb.st_mode) ||
|
|
|
|
(S_ISLNK(lstatb.st_mode) && stat_check() > 0 &&
|
|
|
|
S_ISDIR(statb.st_mode)))) {
|
|
|
|
*xp++ = '/';
|
|
|
|
*xp = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
XPput(*wp, str_nsave(Xstring(*xs, xp), Xlength(*xs, xp), ATEMP));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xp > Xstring(*xs, xp))
|
|
|
|
*xp++ = '/';
|
|
|
|
while (*sp == '/') {
|
|
|
|
Xcheck(*xs, xp);
|
|
|
|
*xp++ = *sp++;
|
|
|
|
}
|
|
|
|
np = strchr(sp, '/');
|
|
|
|
if (np != NULL) {
|
|
|
|
se = np;
|
|
|
|
odirsep = *np; /* don't assume '/', can be multiple kinds */
|
|
|
|
*np++ = '\0';
|
|
|
|
} else {
|
|
|
|
odirsep = '\0'; /* keep gcc quiet */
|
|
|
|
se = sp + strlen(sp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Check if sp needs globbing - done to avoid pattern checks for strings
|
|
|
|
* 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)).
|
|
|
|
*/
|
|
|
|
if (!has_globbing(sp, se)) {
|
|
|
|
XcheckN(*xs, xp, se - sp + 1);
|
|
|
|
debunk(xp, sp, Xnleft(*xs, xp));
|
|
|
|
xp += strlen(xp);
|
|
|
|
*xpp = xp;
|
|
|
|
globit(xs, xpp, np, wp, check);
|
|
|
|
} else {
|
|
|
|
DIR *dirp;
|
|
|
|
struct dirent *d;
|
|
|
|
char *name;
|
|
|
|
int len;
|
|
|
|
int prefix_len;
|
|
|
|
|
|
|
|
/* xp = *xpp; copy_non_glob() may have re-alloc'd xs */
|
|
|
|
*xp = '\0';
|
|
|
|
prefix_len = Xlength(*xs, xp);
|
|
|
|
dirp = opendir(prefix_len ? Xstring(*xs, xp) : ".");
|
|
|
|
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)))
|
|
|
|
continue; /* always ignore . and .. */
|
|
|
|
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;
|
|
|
|
globit(xs, xpp, np, wp,
|
|
|
|
(check & GF_MARKDIR) | GF_GLOBBED
|
|
|
|
| (np ? GF_EXCHECK : GF_NONE));
|
|
|
|
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)
|
|
|
|
return dp;
|
|
|
|
memcpy(dp, sp, s - sp);
|
|
|
|
for (d = dp + (s - sp); *s && (d - dp < (ssize_t)dlen); s++)
|
|
|
|
if (!ISMAGIC(*s) || !(*++s & 0x80) ||
|
• 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
|
|
|
!vstrchr("*+?@! ", *s & 0x7f))
|
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);
|
|
|
|
return dp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if p is an unquoted name, possibly followed by a / or :. If so
|
|
|
|
* 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 *
|
|
|
|
maybe_expand_tilde(const char *p, XString *dsp, char **dpp, int 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 */
|
|
|
|
while (p[0] == CHAR && p[1] != '/' && (!isassign || p[1] != ':'))
|
|
|
|
{
|
|
|
|
Xcheck(ts, tp);
|
|
|
|
*tp++ = p[1];
|
|
|
|
p += 2;
|
|
|
|
}
|
|
|
|
*tp = '\0';
|
|
|
|
r = (p[0] == EOS || p[0] == CHAR || p[0] == CSUBST) ?
|
|
|
|
tilde(Xstring(ts, tp)) : NULL;
|
|
|
|
Xfree(ts, tp);
|
|
|
|
if (r) {
|
|
|
|
while (*r) {
|
|
|
|
Xcheck(*dsp, dp);
|
|
|
|
if (ISMAGIC(*r))
|
|
|
|
*dp++ = MAGIC;
|
|
|
|
*dp++ = *r++;
|
|
|
|
}
|
|
|
|
*dpp = dp;
|
|
|
|
r = p;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* tilde expansion
|
|
|
|
*
|
|
|
|
* based on a version by Arnold Robbins
|
|
|
|
*/
|
|
|
|
|
|
|
|
static char *
|
|
|
|
tilde(char *cp)
|
|
|
|
{
|
2007-01-17 19:01:51 +01:00
|
|
|
char *dp = null;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (cp[0] == '\0')
|
|
|
|
dp = str_val(global("HOME"));
|
|
|
|
else if (cp[0] == '+' && cp[1] == '\0')
|
|
|
|
dp = str_val(global("PWD"));
|
|
|
|
else if (cp[0] == '-' && cp[1] == '\0')
|
|
|
|
dp = str_val(global("OLDPWD"));
|
2007-01-17 19:01:51 +01:00
|
|
|
#ifndef MKSH_NOPWNAM
|
2005-05-23 05:06:10 +02:00
|
|
|
else
|
|
|
|
dp = homedir(cp);
|
2007-01-17 19:01:51 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
/* If HOME, PWD or OLDPWD are not set, don't expand ~ */
|
|
|
|
if (dp == null)
|
|
|
|
dp = NULL;
|
|
|
|
return dp;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2006-01-29 21:04:54 +01: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)
|
|
|
|
return NULL;
|
|
|
|
ap->val.s = str_save(pw->pw_dir, APERM);
|
|
|
|
ap->flag |= DEFINED|ISSET|ALLOC;
|
|
|
|
}
|
|
|
|
return ap->val.s;
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
char *brace_start, *brace_end, *comma = NULL;
|
|
|
|
char *field_start;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
/* search for open brace */
|
|
|
|
for (p = exp_start; (p = strchr(p, MAGIC)) && p[1] != OBRACE; p += 2)
|
|
|
|
;
|
|
|
|
brace_start = p;
|
|
|
|
|
|
|
|
/* find matching close brace, if any */
|
|
|
|
if (p) {
|
|
|
|
comma = NULL;
|
|
|
|
count = 1;
|
|
|
|
for (p += 2; *p && count; p++) {
|
|
|
|
if (ISMAGIC(*p)) {
|
|
|
|
if (*++p == OBRACE)
|
|
|
|
count++;
|
|
|
|
else if (*p == CBRACE)
|
|
|
|
--count;
|
|
|
|
else if (*p == ',' && count == 1)
|
|
|
|
comma = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* no valid expansions... */
|
|
|
|
if (!p || count != 0) {
|
|
|
|
/* Note that given a{{b,c} we do not expand anything (this is
|
|
|
|
* what at&t ksh does. This may be changed to do the {b,c}
|
|
|
|
* expansion. }
|
|
|
|
*/
|
|
|
|
if (fdo & DOGLOB)
|
|
|
|
glob(start, wp, fdo & DOMARKDIRS);
|
|
|
|
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)) {
|
|
|
|
if (*++p == OBRACE)
|
|
|
|
count++;
|
|
|
|
else if ((*p == CBRACE && --count == 0) ||
|
|
|
|
(*p == ',' && count == 1)) {
|
|
|
|
char *new;
|
|
|
|
int l1, l2, l3;
|
|
|
|
|
|
|
|
l1 = brace_start - start;
|
|
|
|
l2 = (p - 1) - field_start;
|
|
|
|
l3 = end - brace_end;
|
|
|
|
new = (char *) alloc(l1 + l2 + l3 + 1, ATEMP);
|
|
|
|
memcpy(new, start, l1);
|
|
|
|
memcpy(new + l1, field_start, l2);
|
|
|
|
memcpy(new + l1 + l2, brace_end, l3);
|
|
|
|
new[l1 + l2 + l3] = '\0';
|
|
|
|
alt_expand(wp, new, new + l1,
|
|
|
|
new + l1 + l2 + l3, fdo);
|
|
|
|
field_start = p + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|