From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 24952 invoked by alias); 18 May 2015 15:30:36 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 35193 Received: (qmail 13587 invoked from network); 18 May 2015 15:30:33 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS autolearn=ham autolearn_force=no version=3.4.0 X-AuditID: cbfec7f5-f794b6d000001495-59-555a059307c1 Date: Mon, 18 May 2015 16:30:25 +0100 From: Peter Stephenson To: zsh-workers@zsh.org Subject: Re: zsh doesn't support standard "unalias -a" Message-id: <20150518163025.579481a9@pwslap01u.europe.root.pri> In-reply-to: <5559E8F1.6070803@inlv.org> References: <5559E8F1.6070803@inlv.org> Organization: Samsung Cambridge Solution Centre X-Mailer: Claws Mail 3.7.9 (GTK+ 2.22.0; i386-redhat-linux-gnu) MIME-version: 1.0 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7bit X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrCLMWRmVeSWpSXmKPExsVy+t/xy7qTWaNCDSYtMrc42PyQyYHRY9XB D0wBjFFcNimpOZllqUX6dglcGWe/N7AWXFev+LV6H2MD4xe5LkZODgkBE4mb2+4xQdhiEhfu rWfrYuTiEBJYyijx8ew8FghnBpPErZkboJxtjBKbVkxmBmlhEVCVeHbuIhuIzSZgKDF102xG EFtEQFzi7NrzLCC2sICZxMOD+8BqeAXsJZ5enwrWyymgIfFqVivYaiEBdYktc76zgtj8AvoS V/9+gjrJXmLmlTOMEL2CEj8m3wObySygJbF5WxMrhC0vsXnNW2aYOTfu7mafwCg0C0nLLCQt s5C0LGBkXsUomlqaXFCclJ5rpFecmFtcmpeul5yfu4kRErZfdzAuPWZ1iFGAg1GJh3eFX2So EGtiWXFl7iFGCQ5mJRFe399AId6UxMqq1KL8+KLSnNTiQ4zSHCxK4rwzd70PERJITyxJzU5N LUgtgskycXBKNTCazVGbu5pFKnbdr+h/j21eHzp77+2aNeaTVpdu3l2cHnqgpeXUm6MpDAde xhaqtOwQmPd/km/1LIuLaxpkfzZ1PqqOyJg3e4uFREvu3ckfTVzPbX1ifeXIldfMZxvqJiZ9 PsypEja1yrQrrtomae9Uhz69KhMbznbJN4dCotYbycSvZX+hp1mhxFKckWioxVxUnAgA1FZC hlcCAAA= On Mon, 18 May 2015 14:28:17 +0100 Martijn Dekker wrote: > In a cross-platform shell library I'm writing, I would like to start by > removing all aliases from the current execution environment, to help > assure a consistent command language. The standard command to remove all > aliases is "unalias -a". This, at least, is easy. It's slightly annoying that we can't pass this via "unhash -a", unlike the other options, but actually there's no particular reason ever to use "unhash -a" in preference to unalias. pws diff --git a/Doc/Zsh/builtins.yo b/Doc/Zsh/builtins.yo index 5327789..1b8729d 100644 --- a/Doc/Zsh/builtins.yo +++ b/Doc/Zsh/builtins.yo @@ -2056,7 +2056,14 @@ the symbolic form the permissions you specify are those which are to be allowed (not denied) to the users specified. ) cindex(aliases, removing) -alias(unalias)(unhash -a) +item(tt(unalias) [ tt(-ams) ] var(name) ...)( +Removes aliases. This command works the same as tt(unhash -a), except that +the tt(-a) option removes all regular or global aliases, or with tt(-s) +all suffix aliases: in this case no var(name) arguments may appear. The +options tt(-m) (remove by pattern) and tt(-s) without tt(-a) (remove +listed suffix aliases) behave as for tt(unhash -a). Note that +the meaning of tt(-a) is different between tt(unalias) and tt(unhash). +) cindex(functions, removing) alias(unfunction)(unhash -f) findex(unhash) diff --git a/Src/builtin.c b/Src/builtin.c index dbcd022..a9afb45 100644 --- a/Src/builtin.c +++ b/Src/builtin.c @@ -122,9 +122,9 @@ static struct builtin builtins[] = BUILTIN("type", 0, bin_whence, 0, -1, 0, "ampfsSw", "v"), BUILTIN("typeset", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL, bin_typeset, 0, -1, 0, "AE:%F:%HL:%R:%TUZ:%afghi:%klprtuxmz", NULL), BUILTIN("umask", 0, bin_umask, 0, 1, 0, "S", NULL), - BUILTIN("unalias", 0, bin_unhash, 1, -1, 0, "ms", "a"), - BUILTIN("unfunction", 0, bin_unhash, 1, -1, 0, "m", "f"), - BUILTIN("unhash", 0, bin_unhash, 1, -1, 0, "adfms", NULL), + BUILTIN("unalias", 0, bin_unhash, 0, -1, BIN_UNALIAS, "ams", NULL), + BUILTIN("unfunction", 0, bin_unhash, 1, -1, BIN_UNFUNCTION, "m", "f"), + BUILTIN("unhash", 0, bin_unhash, 1, -1, BIN_UNHASH, "adfms", NULL), BUILTIN("unset", BINF_PSPECIAL, bin_unset, 1, -1, 0, "fmv", NULL), BUILTIN("unsetopt", 0, bin_setopt, 0, -1, BIN_UNSETOPT, NULL, NULL), BUILTIN("wait", 0, bin_fg, 0, -1, BIN_WAIT, NULL, NULL), @@ -3557,26 +3557,54 @@ bin_hash(char *name, char **argv, Options ops, UNUSED(int func)) /**/ int -bin_unhash(char *name, char **argv, Options ops, UNUSED(int func)) +bin_unhash(char *name, char **argv, Options ops, int func) { HashTable ht; HashNode hn, nhn; Patprog pprog; - int match = 0, returnval = 0; + int match = 0, returnval = 0, all = 0; int i; /* Check which hash table we are working with. */ - if (OPT_ISSET(ops,'d')) + if (func == BIN_UNALIAS) { + if (OPT_ISSET(ops,'s')) + ht = sufaliastab; /* suffix aliases */ + else + ht = aliastab; /* aliases */ + if (OPT_ISSET(ops, 'a')) { + if (*argv) { + zwarnnam(name, "-a: too many arguments"); + return 1; + } + all = 1; + } else if (!*argv) { + zwarnnam(name, "not enough arguments"); + return 1; + } + } else if (OPT_ISSET(ops,'d')) ht = nameddirtab; /* named directories */ else if (OPT_ISSET(ops,'f')) ht = shfunctab; /* shell functions */ else if (OPT_ISSET(ops,'s')) ht = sufaliastab; /* suffix aliases, must precede aliases */ - else if (OPT_ISSET(ops,'a')) + else if (func == BIN_UNHASH && (OPT_ISSET(ops,'a'))) ht = aliastab; /* aliases */ else ht = cmdnamtab; /* external commands */ + if (all) { + queue_signals(); + for (i = 0; i < ht->hsize; i++) { + for (hn = ht->nodes[i]; hn; hn = nhn) { + /* record pointer to next, since we may free this one */ + nhn = hn->next; + ht->freenode(ht->removenode(ht, hn->nam)); + } + } + unqueue_signals(); + return 0; + } + /* With -m option, treat arguments as glob patterns. * * "unhash -m '*'" is legal, but not recommended. */ if (OPT_ISSET(ops,'m')) { diff --git a/Src/hashtable.h b/Src/hashtable.h index 131c15c..b6346bb 100644 --- a/Src/hashtable.h +++ b/Src/hashtable.h @@ -59,6 +59,9 @@ #define BIN_ENABLE 25 #define BIN_PRINTF 26 #define BIN_COMMAND 27 +#define BIN_UNHASH 28 +#define BIN_UNALIAS 29 +#define BIN_UNFUNCTION 30 /* These currently depend on being 0 and 1. */ #define BIN_SETOPT 0 diff --git a/Test/A02alias.ztst b/Test/A02alias.ztst index cace2a4..b294e65 100644 --- a/Test/A02alias.ztst +++ b/Test/A02alias.ztst @@ -83,3 +83,13 @@ > a string S *>*5*echo S a string S " # Note there is a trailing space on the "> a string S " line + + ( + unalias -a + alias + ) +0:unalias -a + + alias -s foo=print + unalias -as +0:unalias -as