(validate_uparams): Fix typo
[gnulib.git] / lib / argp-help.c
index 61acecd..0dce126 100644 (file)
@@ -1,5 +1,5 @@
 /* Hierarchial argument parsing help output
-   Copyright (C) 1995-2003, 2004 Free Software Foundation, Inc.
+   Copyright (C) 1995-2003, 2004, 2005 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Written by Miles Bader <miles@gnu.ai.mit.edu>.
 
 
    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation,
-   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 #ifndef _GNU_SOURCE
 # define _GNU_SOURCE   1
 #endif
 
 #ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-/* AIX requires this to be the first thing in the file.  */
-#ifndef __GNUC__
-# if HAVE_ALLOCA_H || defined _LIBC
-#  include <alloca.h>
-# else
-#  ifdef _AIX
-#pragma alloca
-#  else
-#   ifndef alloca /* predefined by HP cc +Olibcalls */
-char *alloca ();
-#   endif
-#  endif
-# endif
+# include <config.h>
 #endif
 
+#include <alloca.h>
+#include <errno.h>
 #include <stddef.h>
 #include <stdlib.h>
 #include <string.h>
@@ -51,30 +38,13 @@ char *alloca ();
 # include <wchar.h>
 #endif
 
-#ifndef _
-/* This is for other GNU distributions with internationalized messages.  */
-# if defined HAVE_LIBINTL_H || defined _LIBC
-#  include <libintl.h>
-#  ifdef _LIBC
-#   undef dgettext
-#   define dgettext(domain, msgid) \
-  INTUSE(__dcgettext) (domain, msgid, LC_MESSAGES)
-#  endif
-# else
-#  define dgettext(domain, msgid) (msgid)
-# endif
-#endif
-
-#ifndef _LIBC
-# if HAVE_STRERROR_R
-#  if !HAVE_DECL_STRERROR_R
-char *strerror_r (int errnum, char *buf, size_t buflen);
-#  endif
-# else
-#  if !HAVE_DECL_STRERROR
-char *strerror (int errnum);
-#  endif
-# endif
+#ifdef _LIBC
+# include <libintl.h>
+# undef dgettext
+# define dgettext(domain, msgid) \
+   INTUSE(__dcgettext) (domain, msgid, LC_MESSAGES)
+#else
+# include "gettext.h"
 #endif
 
 #include "argp.h"
@@ -119,15 +89,15 @@ struct uparams
   int dup_args_note;
 
   /* Various output columns.  */
-  int short_opt_col;
-  int long_opt_col;
-  int doc_opt_col;
-  int opt_doc_col;
-  int header_col;
-  int usage_indent;
-  int rmargin;
-
-  int valid;                   /* True when the values in here are valid.  */
+  int short_opt_col;      /* column in which short options start */   
+  int long_opt_col;       /* column in which long options start */ 
+  int doc_opt_col;        /* column in which doc options start */
+  int opt_doc_col;        /* column in which option text starts */
+  int header_col;         /* column in which group headers are printed */ 
+  int usage_indent;       /* indentation of wrapped usage lines */
+  int rmargin;            /* right margin used for wrapping */
+
+  int valid;             /* True when the values in here are valid.  */
 };
 
 /* This is a global variable, as user options are only ever read once.  */
@@ -161,91 +131,126 @@ static const struct uparam_name uparam_names[] =
   { 0 }
 };
 
-/* Read user options from the environment, and fill in UPARAMS appropiately.  */
+static void
+validate_uparams (const struct argp_state *state, struct uparams *upptr)
+{
+  const struct uparam_name *up;
+
+  for (up = uparam_names; up->name; up++)
+    {
+      if (up->is_bool
+         || up->uparams_offs == offsetof (struct uparams, rmargin))
+       continue;
+      if (*(int *)((char *)upptr + up->uparams_offs) >= upptr->rmargin)
+       {
+         __argp_failure (state, 0, 0,
+                         dgettext (state->root_argp->argp_domain,
+                                   "\
+ARGP_HELP_FMT: %s value is less than or equal to %s"),
+                         "rmargin", up->name);
+         return;
+       }
+    }
+  uparams = *upptr;
+  uparams.valid = 1;
+}
+
+/* Read user options from the environment, and fill in UPARAMS appropiately. */
 static void
 fill_in_uparams (const struct argp_state *state)
 {
   const char *var = getenv ("ARGP_HELP_FMT");
-
+  struct uparams new_params = uparams;
+  
 #define SKIPWS(p) do { while (isspace (*p)) p++; } while (0);
 
   if (var)
-    /* Parse var. */
-    while (*var)
-      {
-       SKIPWS (var);
-
-       if (isalpha (*var))
-         {
-           size_t var_len;
-           const struct uparam_name *un;
-           int unspec = 0, val = 0;
-           const char *arg = var;
-
-           while (isalnum (*arg) || *arg == '-' || *arg == '_')
-             arg++;
-           var_len = arg - var;
-
-           SKIPWS (arg);
+    {
+      /* Parse var. */
+      while (*var)
+       {
+         SKIPWS (var);
+         
+         if (isalpha (*var))
+           {
+             size_t var_len;
+             const struct uparam_name *un;
+             int unspec = 0, val = 0;
+             const char *arg = var;
 
-           if (*arg == '\0' || *arg == ',')
-             unspec = 1;
-           else if (*arg == '=')
-             {
+             while (isalnum (*arg) || *arg == '-' || *arg == '_')
                arg++;
-               SKIPWS (arg);
-             }
-
-           if (unspec)
-             {
-               if (var[0] == 'n' && var[1] == 'o' && var[2] == '-')
-                 {
-                   val = 0;
-                   var += 3;
-                   var_len -= 3;
-                 }
-               else
-                 val = 1;
-             }
-           else if (isdigit (*arg))
-             {
-               val = atoi (arg);
-               while (isdigit (*arg))
+             var_len = arg - var;
+             
+             SKIPWS (arg);
+             
+             if (*arg == '\0' || *arg == ',')
+               unspec = 1;
+             else if (*arg == '=')
+               {
                  arg++;
-               SKIPWS (arg);
-             }
-
-           for (un = uparam_names; un->name; un++)
-             if (strlen (un->name) == var_len
-                 && strncmp (var, un->name, var_len) == 0)
+                 SKIPWS (arg);
+               }
+             
+             if (unspec)
                {
-                 if (unspec && !un->is_bool)
-                   __argp_failure (state, 0, 0,
-                                   dgettext (state->root_argp->argp_domain, "\
-%.*s: ARGP_HELP_FMT parameter requires a value"),
-                                   (int) var_len, var);
+                 if (var[0] == 'n' && var[1] == 'o' && var[2] == '-')
+                   {
+                     val = 0;
+                     var += 3;
+                     var_len -= 3;
+                   }
                  else
-                   *(int *)((char *)&uparams + un->uparams_offs) = val;
-                 break;
+                   val = 1;
                }
-           if (! un->name)
-             __argp_failure (state, 0, 0,
-                             dgettext (state->root_argp->argp_domain, "\
+             else if (isdigit (*arg))
+               {
+                 val = atoi (arg);
+                 while (isdigit (*arg))
+                   arg++;
+                 SKIPWS (arg);
+               }
+             
+             for (un = uparam_names; un->name; un++)
+               if (strlen (un->name) == var_len
+                   && strncmp (var, un->name, var_len) == 0)
+                 {
+                   if (unspec && !un->is_bool)
+                     __argp_failure (state, 0, 0,
+                                     dgettext (state->root_argp->argp_domain,
+                                               "\
+%.*s: ARGP_HELP_FMT parameter requires a value"),
+                                     (int) var_len, var);
+                   else if (val < 0)
+                     __argp_failure (state, 0, 0,
+                                     dgettext (state->root_argp->argp_domain,
+                                               "\
+%.*s: ARGP_HELP_FMT parameter must be positive"),
+                                     (int) var_len, var);
+                   else
+                     *(int *)((char *)&new_params + un->uparams_offs) = val;
+                   break;
+                 }
+             if (! un->name)
+               __argp_failure (state, 0, 0,
+                               dgettext (state->root_argp->argp_domain, "\
 %.*s: Unknown ARGP_HELP_FMT parameter"),
-                             (int) var_len, var);
+                               (int) var_len, var);
 
-           var = arg;
-           if (*var == ',')
-             var++;
-         }
-       else if (*var)
-         {
-           __argp_failure (state, 0, 0,
-                           dgettext (state->root_argp->argp_domain,
-                                     "Garbage in ARGP_HELP_FMT: %s"), var);
-           break;
-         }
-      }
+             var = arg;
+             if (*var == ',')
+               var++;
+           }
+         else if (*var)
+           {
+             __argp_failure (state, 0, 0,
+                             dgettext (state->root_argp->argp_domain,
+                                       "Garbage in ARGP_HELP_FMT: %s"), var);
+             break;
+           }
+       }
+      validate_uparams (state, &new_params);
+    }
 }
 \f
 /* Returns true if OPT hasn't been marked invisible.  Visibility only affects
@@ -258,6 +263,9 @@ fill_in_uparams (const struct argp_state *state)
 /* Returns true if OPT is an documentation-only entry.  */
 #define odoc(opt) ((opt)->flags & OPTION_DOC)
 
+/* Returns true if OPT should not be translated */
+#define onotrans(opt) ((opt)->flags & OPTION_NO_TRANS)
+
 /* Returns true if OPT is the end-of-list marker for a list of options.  */
 #define oend(opt) __option_is_end (opt)
 
@@ -453,9 +461,8 @@ make_hol (const struct argp *argp, struct hol_cluster *cluster)
       hol->short_options = malloc (num_short_options + 1);
 
       assert (hol->entries && hol->short_options);
-#if SIZE_MAX <= UINT_MAX
-      assert (hol->num_entries <= SIZE_MAX / sizeof (struct hol_entry));
-#endif
+      if (SIZE_MAX <= UINT_MAX)
+       assert (hol->num_entries <= SIZE_MAX / sizeof (struct hol_entry));
 
       /* Fill in the entries.  */
       so = hol->short_options;
@@ -707,14 +714,20 @@ static int
 canon_doc_option (const char **name)
 {
   int non_opt;
-  /* Skip initial whitespace.  */
-  while (isspace (**name))
-    (*name)++;
-  /* Decide whether this looks like an option (leading `-') or not.  */
-  non_opt = (**name != '-');
-  /* Skip until part of name used for sorting.  */
-  while (**name && !isalnum (**name))
-    (*name)++;
+
+  if (!*name)
+    non_opt = 1;
+  else
+    {
+      /* Skip initial whitespace.  */
+      while (isspace (**name))
+       (*name)++;
+      /* Decide whether this looks like an option (leading `-') or not.  */
+      non_opt = (**name != '-');
+      /* Skip until part of name used for sorting.  */
+      while (**name && !isalnum (**name))
+       (*name)++;
+    }
   return non_opt;
 }
 
@@ -849,9 +862,8 @@ hol_append (struct hol *hol, struct hol *more)
            malloc (hol_so_len + strlen (more->short_options) + 1);
 
          assert (entries && short_options);
-#if SIZE_MAX <= UINT_MAX
-         assert (num_entries <= SIZE_MAX / sizeof (struct hol_entry));
-#endif
+         if (SIZE_MAX <= UINT_MAX)
+           assert (num_entries <= SIZE_MAX / sizeof (struct hol_entry));
 
          __mempcpy (__mempcpy (entries, hol->entries,
                                hol->num_entries * sizeof (struct hol_entry)),
@@ -1078,7 +1090,13 @@ hol_entry_help (struct hol_entry *entry, const struct argp_state *state,
   int old_wm = __argp_fmtstream_wmargin (stream);
   /* PEST is a state block holding some of our variables that we'd like to
      share with helper functions.  */
-  struct pentry_state pest = { entry, stream, hhstate, 1, state };
+  struct pentry_state pest;
+
+  pest.entry = entry;
+  pest.stream = stream;
+  pest.hhstate = hhstate;
+  pest.first = 1;
+  pest.state = state;
 
   if (! odoc (real))
     for (opt = real, num = entry->num; num > 0; opt++, num--)
@@ -1113,13 +1131,15 @@ hol_entry_help (struct hol_entry *entry, const struct argp_state *state,
     {
       __argp_fmtstream_set_wmargin (stream, uparams.doc_opt_col);
       for (opt = real, num = entry->num; num > 0; opt++, num--)
-       if (opt->name && ovisible (opt))
+       if (opt->name && *opt->name && ovisible (opt))
          {
            comma (uparams.doc_opt_col, &pest);
-           /* Calling gettext here isn't quite right, since sorting will
+           /* Calling dgettext here isn't quite right, since sorting will
               have been done on the original; but documentation options
               should be pretty rare anyway...  */
            __argp_fmtstream_puts (stream,
+                                  onotrans (opt) ?
+                                            opt->name :
                                   dgettext (state->root_argp->argp_domain,
                                             opt->name));
          }
@@ -1284,7 +1304,7 @@ usage_long_opt (const struct argp_option *opt,
   if (! arg)
     arg = real->arg;
 
-  if (! (flags & OPTION_NO_USAGE))
+  if (! (flags & OPTION_NO_USAGE) && !odoc (opt))
     {
       if (arg)
        {
@@ -1684,26 +1704,21 @@ Try `%s --help' or `%s --usage' for more information.\n"),
 void __argp_help (const struct argp *argp, FILE *stream,
                  unsigned flags, char *name)
 {
-  _help (argp, 0, stream, flags, name);
+  struct argp_state state;
+  memset (&state, 0, sizeof state);
+  state.root_argp = argp;
+  _help (argp, &state, stream, flags, name);
 }
 #ifdef weak_alias
 weak_alias (__argp_help, argp_help)
 #endif
 
-#ifndef _LIBC
-char *__argp_basename (char *name)
-{
-  char *short_name = strrchr (name, '/');
-  return short_name ? short_name + 1 : name;
-}
-
+#if ! (defined _LIBC || HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME)
 char *
 __argp_short_program_name (void)
 {
-# if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME
-  return program_invocation_short_name;
-# elif HAVE_DECL_PROGRAM_INVOCATION_NAME
-  return __argp_basename (program_invocation_name);
+# if HAVE_DECL_PROGRAM_INVOCATION_NAME
+  return __argp_base_name (program_invocation_name);
 # else
   /* FIXME: What now? Miles suggests that it is better to use NULL,
      but currently the value is passed on directly to fputs_unlocked,
@@ -1767,7 +1782,8 @@ __argp_error (const struct argp_state *state, const char *fmt, ...)
            {
              char *buf;
 
-             __asprintf (&buf, fmt, ap);
+             if (__asprintf (&buf, fmt, ap) < 0)
+               buf = NULL;
 
              __fwprintf (stream, L"%s: %s\n",
                          state ? state->name : __argp_short_program_name (),
@@ -1845,7 +1861,8 @@ __argp_failure (const struct argp_state *state, int status, int errnum,
                {
                  char *buf;
 
-                 __asprintf (&buf, fmt, ap);
+                 if (__asprintf (&buf, fmt, ap) < 0)
+                   buf = NULL;
 
                  __fwprintf (stream, L": %s", buf);
 
@@ -1874,13 +1891,21 @@ __argp_failure (const struct argp_state *state, int status, int errnum,
              else
 #endif
                {
+                 char const *s = NULL;
                  putc_unlocked (':', stream);
                  putc_unlocked (' ', stream);
-#if defined _LIBC || defined HAVE_STRERROR_R
-                 fputs (__strerror_r (errnum, buf, sizeof (buf)), stream);
-#else
-                 fputs (strerror (errnum), stream);
+#if _LIBC || (HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P)
+                 s = __strerror_r (errnum, buf, sizeof buf);
+#elif HAVE_DECL_STRERROR_R
+                 if (__strerror_r (errnum, buf, sizeof buf) == 0)
+                   s = buf;
+#endif
+#if !_LIBC
+                 if (! s && ! (s = strerror (errnum)))
+                   s = dgettext (state->root_argp->argp_domain,
+                                 "Unknown system error");
 #endif
+                 fputs (s, stream);
                }
            }