bug-coreutils
[Top][All Lists]
Advanced

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

Re: option abbreviation exceptions


From: Eric Blake
Subject: Re: option abbreviation exceptions
Date: Wed, 14 Jan 2009 06:31:05 -0700
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.19) Gecko/20081209 Thunderbird/2.0.0.19 Mnenhy/0.7.5.666

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Jim Meyering on 1/10/2009 2:32 PM:
>>> "[" feels like it should be the exception.
>>> I see treating --v like --version as a feature.  a typing saver.
>>> But with "[", it's good to minimize the number of strings that
>>> make it act differently.
>>>
>>
>> Or, for echo and [, we could skip parse_long_options altogether, and
>> hardcode this instead:
>>
>> argc == 2 && (STREQ (argv[1], "--help") || STREQ (argv[1], "--version"))
> 
> One of those approaches would be good.
> Whichever you prefer.

How about this patch for test/[, echo, and printf?  Note that true (and
false) already did this exact approach.  And for all other applications,
it makes sense to use parse_long_options (and allow abbreviations).

I went ahead and pushed this to the next branch, to make it easier to
test.  Since that branch is subject to rebasing, we can remove (or rework)
this patch as needed before making it official.

- --
Don't work too hard, make some time for fun as well!

Eric Blake             address@hidden
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAklt6RgACgkQ84KuGfSFAYDyYwCfZhI6SpvterrL+kGAag/Bd79T
IGoAoKIdjUe0UfpSWUnflR0GQ52sSo6I
=uEoe
-----END PGP SIGNATURE-----
>From 051319cc37fda1ca25d1db56c93ccc531e868496 Mon Sep 17 00:00:00 2001
From: Eric Blake <address@hidden>
Date: Tue, 13 Jan 2009 21:59:35 -0700
Subject: [PATCH] test, echo, printf: don't accept option abbreviation

* src/test.c (main): Directly parse accepted options, thus
avoiding abbreviations.
* src/echo.c (main): Likewise.
* src/printf.c (main): Likewise.
---
 src/echo.c   |   19 +++++++++++++++----
 src/printf.c |   18 +++++++++++++++---
 src/test.c   |   27 ++++++++++++++++++---------
 3 files changed, 48 insertions(+), 16 deletions(-)

diff --git a/src/echo.c b/src/echo.c
index d549cda..caa231b 100644
--- a/src/echo.c
+++ b/src/echo.c
@@ -1,5 +1,5 @@
 /* echo.c, derived from code echo.c in Bash.
-   Copyright (C) 87,89, 1991-1997, 1999-2005, 2007-2008 Free Software
+   Copyright (C) 87,89, 1991-1997, 1999-2005, 2007-2009 Free Software
    Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
@@ -121,9 +121,20 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  if (allow_options)
-    parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
-                       usage, AUTHORS, (char const *) NULL);
+  /* We directly parse options, rather than use parse_long_options, in
+     order to avoid accepting abbreviations.  */
+  if (allow_options && argc == 2)
+    {
+      if (STREQ (argv[1], "--help"))
+       usage (EXIT_SUCCESS);
+
+      if (STREQ (argv[1], "--version"))
+       {
+         version_etc (stdout, PROGRAM_NAME, PACKAGE_NAME, Version, AUTHORS,
+                      (char *) NULL);
+         exit (EXIT_SUCCESS);
+       }
+    }
 
   --argc;
   ++argv;
diff --git a/src/printf.c b/src/printf.c
index c509951..63351f0 100644
--- a/src/printf.c
+++ b/src/printf.c
@@ -1,5 +1,5 @@
 /* printf - format and print data
-   Copyright (C) 1990-2008 Free Software Foundation, Inc.
+   Copyright (C) 1990-2009 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -645,8 +645,20 @@ main (int argc, char **argv)
 
   posixly_correct = (getenv ("POSIXLY_CORRECT") != NULL);
 
-  parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
-                     usage, AUTHORS, (char const *) NULL);
+  /* We directly parse options, rather than use parse_long_options, in
+     order to avoid accepting abbreviations.  */
+  if (argc == 2)
+    {
+      if (STREQ (argv[1], "--help"))
+       usage (EXIT_SUCCESS);
+
+      if (STREQ (argv[1], "--version"))
+       {
+         version_etc (stdout, PROGRAM_NAME, PACKAGE_NAME, Version, AUTHORS,
+                      (char *) NULL);
+         exit (EXIT_SUCCESS);
+       }
+    }
 
   /* The above handles --help and --version.
      Since there is no other invocation of getopt, handle `--' here.  */
diff --git a/src/test.c b/src/test.c
index 14d20fd..c56ab9e 100644
--- a/src/test.c
+++ b/src/test.c
@@ -2,7 +2,7 @@
 
 /* Modified to run with the GNU shell by bfox. */
 
-/* Copyright (C) 1987-2005, 2007-2008 Free Software Foundation, Inc.
+/* Copyright (C) 1987-2005, 2007-2009 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -819,16 +819,25 @@ main (int margc, char **margv)
   if (LBRACKET)
     {
       /* Recognize --help or --version, but only when invoked in the
-        "[" form, and when the last argument is not "]".  POSIX
-        allows "[ --help" and "[ --version" to have the usual GNU
-        behavior, but it requires "test --help" and "test --version"
-        to exit silently with status 0.  */
-      if (margc < 2 || !STREQ (margv[margc - 1], "]"))
+        "[" form, when the last argument is not "]".  Use direct
+        parsing, rather than parse_long_options, to avoid accepting
+        abbreviations.  POSIX allows "[ --help" and "[ --version" to
+        have the usual GNU behavior, but it requires "test --help"
+        and "test --version" to exit silently with status 0.  */
+      if (margc == 2)
        {
-         parse_long_options (margc, margv, PROGRAM_NAME, PACKAGE_NAME, Version,
-                             usage, AUTHORS, (char const *) NULL);
-         test_syntax_error (_("missing `]'"), NULL);
+         if (STREQ (margv[1], "--help"))
+           usage (EXIT_SUCCESS);
+
+         if (STREQ (margv[1], "--version"))
+           {
+             version_etc (stdout, PROGRAM_NAME, PACKAGE_NAME, Version, AUTHORS,
+                          (char *) NULL);
+             test_exit (EXIT_SUCCESS);
+           }
        }
+      if (margc < 2 || !STREQ (margv[margc - 1], "]"))
+       test_syntax_error (_("missing `]'"), NULL);
 
       --margc;
     }
-- 
1.6.0.4


reply via email to

[Prev in Thread] Current Thread [Next in Thread]