Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
[PATCH] Exit status of "whence" (Re: symlink chain.)
- X-seq: zsh-workers 34093
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: <zsh-workers@xxxxxxx>
- Subject: [PATCH] Exit status of "whence" (Re: symlink chain.)
- Date: Sun, 04 Jan 2015 18:05:39 -0800
- In-reply-to: <150104173448.ZM19453@torch.brasslantern.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: <549E3A7B.9010209@eastlink.ca> <20150102170307.7d2e644a@ntlworld.com> <54A6E6B1.6070201@eastlink.ca> <20150102212422.3a761af5@ntlworld.com> <54A7136C.1060102@eastlink.ca> <20150102222140.1303a633@ntlworld.com> <54A72CEF.9090102@eastlink.ca> <54A740F3.4040902@eastlink.ca> <150102210337.ZM22099@torch.brasslantern.com> <54A783C3.3000006@eastlink.ca> <150102231734.ZM22168@torch.brasslantern.com> <54A82374.1030208@eastlink.ca> <150103120252.ZM23074@torch.brasslantern.com> <54A85B6C.4020103@eastlink.ca> <150103164002.ZM23676@torch.brasslantern.com> <54A8B4EE.30908@eastlink.ca> <150104003130.ZM24261@torch.brasslantern.com> <54A9A76A.7020303@eastlink.ca> <150104173448.ZM19453@torch.brasslantern.com>
On Jan 4, 5:34pm, Bart Schaefer wrote:
}
} whence -m /this/is/a/file/path
} and
} whence -a this_is_not_a_command
}
} both return zero. Other cases in which whence does not find anything
} return nonzero. So you can't successfully write
}
} whence -m $1 || whence -a $1 || whence $1
}
} which would be the "normal" way to do what you want. I think whence
} should return a detectable failure when the pattern doesn't match,
} rather than just output nothing.
I think this does it. I hope nobody objects to my having minimized the
diffs by not re-indenting everything. I also factored out filltable()
because it doesn't seem like it should be necessary to repeat it for
every argument. Yes, this fills the command table even on a bad search
pattern ... is that terrible?
Note that if you do "whence this that those" you get an exit status of
zero if ANY of them are found. I think that was always the case.
diff --git a/Src/builtin.c b/Src/builtin.c
index ebc0654..4a46cbc 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -3173,7 +3173,7 @@ bin_whence(char *nam, char **argv, Options ops, int func)
int printflags = 0;
int aliasflags;
int csh, all, v, wd;
- int informed;
+ int informed = 0;
char *cnam, **allmatched = 0;
/* Check some option information */
@@ -3207,6 +3207,7 @@ bin_whence(char *nam, char **argv, Options ops, int func)
/* With -m option -- treat arguments as a glob patterns */
if (OPT_ISSET(ops,'m')) {
+ cmdnamtab->filltable(cmdnamtab);
if (all) {
pushheap();
matchednodes = newlinklist();
@@ -3225,17 +3226,19 @@ bin_whence(char *nam, char **argv, Options ops, int func)
/* -p option is for path search only. *
* We're not using it, so search for ... */
+ informed = /* logical OR of what follows */
+
/* aliases ... */
scanmatchtable(aliastab, pprog, 1, 0, DISABLED,
- aliastab->printnode, printflags);
+ aliastab->printnode, printflags) ||
/* and reserved words ... */
scanmatchtable(reswdtab, pprog, 1, 0, DISABLED,
- reswdtab->printnode, printflags);
+ reswdtab->printnode, printflags) ||
/* and shell functions... */
scanmatchtable(shfunctab, pprog, 1, 0, DISABLED,
- shfunctab->printnode, printflags);
+ shfunctab->printnode, printflags) ||
/* and builtins. */
scanmatchtable(builtintab, pprog, 1, 0, DISABLED,
@@ -3243,7 +3246,7 @@ bin_whence(char *nam, char **argv, Options ops, int func)
}
/* Done search for `internal' commands, if the -p option *
* was not used. Now search the path. */
- cmdnamtab->filltable(cmdnamtab);
+ informed +=
scanmatchtable(cmdnamtab, pprog, 1, 0, 0,
(all ? fetchcmdnamnode : cmdnamtab->printnode),
printflags);
@@ -3255,61 +3258,59 @@ bin_whence(char *nam, char **argv, Options ops, int func)
matchednodes = NULL;
popheap();
} else
- return returnval;
+ return returnval || !informed;
}
/* Take arguments literally -- do not glob */
queue_signals();
for (; *argv; argv++) {
- informed = 0;
-
if (!OPT_ISSET(ops,'p') && !allmatched) {
char *suf;
/* Look for alias */
if ((hn = aliastab->getnode(aliastab, *argv))) {
aliastab->printnode(hn, aliasflags);
+ informed = 1;
if (!all)
continue;
- informed = 1;
}
/* Look for suffix alias */
if ((suf = strrchr(*argv, '.')) && suf[1] &&
suf > *argv && suf[-1] != Meta &&
(hn = sufaliastab->getnode(sufaliastab, suf+1))) {
sufaliastab->printnode(hn, printflags);
+ informed = 1;
if (!all)
continue;
- informed = 1;
}
/* Look for reserved word */
if ((hn = reswdtab->getnode(reswdtab, *argv))) {
reswdtab->printnode(hn, printflags);
+ informed = 1;
if (!all)
continue;
- informed = 1;
}
/* Look for shell function */
if ((hn = shfunctab->getnode(shfunctab, *argv))) {
shfunctab->printnode(hn, printflags);
+ informed = 1;
if (!all)
continue;
- informed = 1;
}
/* Look for builtin command */
if ((hn = builtintab->getnode(builtintab, *argv))) {
builtintab->printnode(hn, printflags);
+ informed = 1;
if (!all)
continue;
- informed = 1;
}
/* Look for commands that have been added to the *
* cmdnamtab with the builtin `hash foo=bar'. */
if ((hn = cmdnamtab->getnode(cmdnamtab, *argv)) && (hn->flags & HASHED)) {
cmdnamtab->printnode(hn, printflags);
+ informed = 1;
if (!all)
continue;
- informed = 1;
}
}
@@ -3356,6 +3357,7 @@ bin_whence(char *nam, char **argv, Options ops, int func)
print_if_link(cnam, OPT_ISSET(ops,'S'));
fputc('\n', stdout);
}
+ informed = 1;
} else {
/* Not found at all. */
if (v || csh || wd)
@@ -3367,7 +3369,7 @@ bin_whence(char *nam, char **argv, Options ops, int func)
freearray(allmatched);
unqueue_signals();
- return returnval;
+ return returnval || !informed;
}
/**** command & named directory hash table builtins ****/
Messages sorted by:
Reverse Date,
Date,
Thread,
Author