Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/sh Allow (but do not require) the magic '--' option term...



details:   https://anonhg.NetBSD.org/src/rev/4ec82798c5b1
branches:  trunk
changeset: 373704:4ec82798c5b1
user:      kre <kre%NetBSD.org@localhost>
date:      Fri Feb 24 19:04:54 2023 +0000

description:
Allow (but do not require) the magic '--' option terminator in
the builtin 'alias' command.   This allows portability (not that
anyone should really care with aliases) for scripts from other
shells in which the alias command has options, and the -- is
required to allow the first alias name to begin with a '-'.

That is, for us, alias -x='echo x'  works fine, always has,
and still does.   But other shells treat that as an attempt
to use the -x option (and maybe -= etc), and require
alias -- -x='echo x'.   For us that variant used to complain
about the alias -- not existing (as an arg with no '=' is
treated as a request to extract the value of the alias).

Posix also generally requires all standard commands (or
which "alias" is one, unfortunately) to support '--' even
if they have no options, for precisely this reason.

diffstat:

 bin/sh/alias.c |  12 +++++++-----
 1 files changed, 7 insertions(+), 5 deletions(-)

diffs (42 lines):

diff -r f9ec32ec1ede -r 4ec82798c5b1 bin/sh/alias.c
--- a/bin/sh/alias.c    Fri Feb 24 17:08:31 2023 +0000
+++ b/bin/sh/alias.c    Fri Feb 24 19:04:54 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: alias.c,v 1.21 2019/02/09 09:11:07 kre Exp $   */
+/*     $NetBSD: alias.c,v 1.22 2023/02/24 19:04:54 kre Exp $   */
 
 /*-
  * Copyright (c) 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)alias.c    8.3 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: alias.c,v 1.21 2019/02/09 09:11:07 kre Exp $");
+__RCSID("$NetBSD: alias.c,v 1.22 2023/02/24 19:04:54 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -253,18 +253,20 @@
 }
 
 int
-aliascmd(int argc, char **argv)
+aliascmd(int argc, char **argv)        /* ARGSUSED */
 {
        char *n, *v;
        int ret = 0;
        struct alias *ap;
 
-       if (argc == 1) {
+       (void) nextopt(NULL);   /* consume possible "--" */
+
+       if (*argptr == NULL) {
                list_aliases();
                return 0;
        }
 
-       while ((n = *++argv) != NULL) {
+       while ((n = *argptr++) != NULL) {
                if ((v = strchr(n+1, '=')) == NULL) { /* n+1: funny ksh stuff */
                        if ((ap = lookupalias(n, 0)) == NULL) {
                                outfmt(out2, "alias: %s not found\n", n);



Home | Main Index | Thread Index | Old Index