Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: globbing in conditional expressions
- X-seq: zsh-workers 32631
- From: Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
- To: "Zsh Hackers' List" <zsh-workers@xxxxxxx>
- Subject: Re: globbing in conditional expressions
- Date: Fri, 30 May 2014 20:19:42 +0100
- In-reply-to: <20140530195734.1d9c5310@pws-pc.ntlworld.com>
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <20140507154407.660eb500@pwslap01u.europe.root.pri> <20140508105522.GE2052@tarsus.local2> <20140508122045.3c68c3fa@pwslap01u.europe.root.pri> <140508083418.ZM14713@torch.brasslantern.com> <20140508201936.GB53652@isis.sigpipe.cz> <140513084117.ZM22925@torch.brasslantern.com> <20140514041908.GF2471@tarsus.local2> <140514001819.ZM23478@torch.brasslantern.com> <20140515092901.GC2174@tarsus.local2> <140515075003.ZM28035@torch.brasslantern.com> <20140526235216.GC1920@tarsus.local2> <140529205956.ZM17410@torch.brasslantern.com> <20140530094752.4a116629@pwslap01u.europe.root.pri> <140530085542.ZM18304@torch.brasslantern.com> <20140530195734.1d9c5310@pws-pc.ntlworld.com>
On Fri, 30 May 2014 19:57:34 +0100
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx> wrote:
> One thing we could do is make [[ ... = ... ]] expand its right hand
> argument the same way as normal command arguments when told to do so, so
> there's no retokenization.
No reason not to do this for all string arguments, I suppose --- there's
nothig here that's specific to pattern matching. The function
cond_match() appears not to be used.
[[ -z nonexistent*(#qN) ]]
[[ -n glob*(#qN) ]]
diff --git a/Src/cond.c b/Src/cond.c
index c673542..6e9b558 100644
--- a/Src/cond.c
+++ b/Src/cond.c
@@ -37,6 +37,21 @@ static char *condstr[COND_MOD] = {
"-ne", "-lt", "-gt", "-le", "-ge", "=~"
};
+static void cond_subst(char **strp, int glob_ok)
+{
+ if (glob_ok &&
+ checkglobqual(*strp, strlen(*strp), 1, NULL)) {
+ LinkList args = newlinklist();
+ addlinknode(args, *strp);
+ prefork(args, 0);
+ while (!errflag && args && nonempty(args) &&
+ has_token((char *)peekfirst(args)))
+ zglob(args, firstnode(args), 0);
+ *strp = sepjoin(hlinklist2array(args, 0), NULL, 1);
+ } else
+ singsub(strp);
+}
+
/*
* Evaluate a conditional expression given the arguments.
* If fromtest is set, the caller is the test or [ builtin;
@@ -177,13 +192,13 @@ evalcond(Estate state, char *fromtest)
}
left = ecgetstr(state, EC_DUPTOK, &htok);
if (htok) {
- singsub(&left);
+ cond_subst(&left, !fromtest);
untokenize(left);
}
if (ctype <= COND_GE && ctype != COND_STREQ && ctype != COND_STRNEQ) {
right = ecgetstr(state, EC_DUPTOK, &htok);
if (htok) {
- singsub(&right);
+ cond_subst(&right, !fromtest);
untokenize(right);
}
}
@@ -194,7 +209,7 @@ evalcond(Estate state, char *fromtest)
fprintf(xtrerr, " %s ", condstr[ctype]);
if (ctype == COND_STREQ || ctype == COND_STRNEQ) {
char *rt = dupstring(ecrawstr(state->prog, state->pc, NULL));
- singsub(&rt);
+ cond_subst(&rt, !fromtest);
quote_tokenized_output(rt, xtrerr);
}
else
@@ -283,7 +298,7 @@ evalcond(Estate state, char *fromtest)
right = dupstring(opat = ecrawstr(state->prog, state->pc,
&htok));
if (htok)
- singsub(&right);
+ cond_subst(&right, !fromtest);
save = (!(state->prog->flags & EF_HEAP) &&
!strcmp(opat, right) && pprog != dummy_patprog2);
@@ -517,17 +532,6 @@ cond_val(char **args, int num)
}
/**/
-mod_export int
-cond_match(char **args, int num, char *str)
-{
- char *s = args[num];
-
- singsub(&s);
-
- return matchpat(str, s);
-}
-
-/**/
static void
tracemodcond(char *name, char **args, int inf)
{
diff --git a/Src/glob.c b/Src/glob.c
index 07dd7c2..01f8a6e 100644
--- a/Src/glob.c
+++ b/Src/glob.c
@@ -1061,6 +1061,65 @@ insert_glob_match(LinkList list, LinkNode next, char *data)
insertlinknode(list, next, data);
}
+/*
+ * Return
+ * 1 if str ends in bare glob qualifiers
+ * 2 if str ends in non-bare glob qualifiers (#q)
+ * 0 otherwise.
+ *
+ * str is the string to check.
+ * sl is it's length (to avoid recalculation).
+ * nobareglob is 1 if bare glob qualifiers are not allowed.
+ * *sp, if sp is not null, wil be a pointer to the start parentheses.
+ */
+
+/**/
+int
+checkglobqual(char *str, int sl, int nobareglob, char **sp)
+{
+ char *s;
+ int paren, ret = 1;
+
+ if (str[sl - 1] != Outpar)
+ return 0;
+
+ /* Check these are really qualifiers, not a set of *
+ * alternatives or exclusions. We can be more *
+ * lenient with an explicit (#q) than with a bare *
+ * set of qualifiers. */
+ paren = 0;
+ for (s = str + sl - 2; *s && (*s != Inpar || paren); s--) {
+ switch (*s) {
+ case Outpar:
+ paren++; /*FALLTHROUGH*/
+ case Bar:
+ if (!zpc_disables[ZPC_BAR])
+ nobareglob = 1;
+ break;
+ case Tilde:
+ if (isset(EXTENDEDGLOB) && !zpc_disables[ZPC_TILDE])
+ nobareglob = 1;
+ break;
+ case Inpar:
+ paren--;
+ break;
+ }
+ }
+ if (*s != Inpar)
+ return 0;
+ if (isset(EXTENDEDGLOB) && !zpc_disables[ZPC_HASH] && s[1] == Pound) {
+ if (s[2] != 'q')
+ return 0;
+ ret = 2;
+ } else if (nobareglob)
+ return 0;
+
+ if (sp)
+ *sp = s;
+
+ return ret;
+}
+
/* Main entry point to the globbing code for filename globbing. *
* np points to a node in the list list which will be expanded *
* into a series of nodes. */
@@ -1118,7 +1177,7 @@ zglob(LinkList list, LinkNode np, int nountok)
(isset(EXTENDEDGLOB) && !zpc_disables[ZPC_HASH])) {
struct qual *newquals;
char *s;
- int sense, paren;
+ int sense, qualsfound;
off_t data;
char *sdata, *newcolonmod;
int (*func) _((char *, Statptr, off_t, char *));
@@ -1148,40 +1207,7 @@ zglob(LinkList list, LinkNode np, int nountok)
newquals = qo = qn = ql = NULL;
sl = strlen(str);
- if (str[sl - 1] != Outpar)
- break;
-
- /* Check these are really qualifiers, not a set of *
- * alternatives or exclusions. We can be more *
- * lenient with an explicit (#q) than with a bare *
- * set of qualifiers. */
- paren = 0;
- for (s = str + sl - 2; *s && (*s != Inpar || paren); s--) {
- switch (*s) {
- case Outpar:
- paren++; /*FALLTHROUGH*/
- case Bar:
- if (!zpc_disables[ZPC_BAR])
- nobareglob = 1;
- break;
- case Tilde:
- if (isset(EXTENDEDGLOB) && !zpc_disables[ZPC_TILDE])
- nobareglob = 1;
- break;
- case Inpar:
- paren--;
- break;
- }
- }
- if (*s != Inpar)
- break;
- if (isset(EXTENDEDGLOB) && !zpc_disables[ZPC_HASH] && s[1] == Pound) {
- if (s[2] == 'q') {
- *s = 0;
- s += 2;
- } else
- break;
- } else if (nobareglob)
+ if (!(qualsfound = checkglobqual(str, sl, nobareglob, &s)))
break;
/* Real qualifiers found. */
@@ -1194,6 +1220,8 @@ zglob(LinkList list, LinkNode np, int nountok)
str[sl-1] = 0;
*s++ = 0;
+ if (qualsfound == 2)
+ s += 2;
while (*s && !newcolonmod) {
func = (int (*) _((char *, Statptr, off_t, char *)))0;
if (idigit(*s)) {
--
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/
Messages sorted by:
Reverse Date,
Date,
Thread,
Author