Avoid using non-constant initializers for struct pentry_state.
[gnulib.git] / lib / argp-help.c
index e82f488..c506b57 100644 (file)
@@ -1,5 +1,5 @@
 /* Hierarchial argument parsing help output
-   Copyright (C) 1995-2000, 2001, 2002, 2003 Free Software Foundation, Inc.
+   Copyright (C) 1995-2003, 2004 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Written by Miles Bader <miles@gnu.ai.mit.edu>.
 
 #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
-#endif
-
+#include <alloca.h>
+#include <errno.h>
 #include <stddef.h>
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
 #include <stdarg.h>
 #include <ctype.h>
+#include <limits.h>
 #ifdef USE_IN_LIBIO
 # 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"
@@ -257,6 +228,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)
 
@@ -451,8 +425,9 @@ make_hol (const struct argp *argp, struct hol_cluster *cluster)
       hol->entries = malloc (sizeof (struct hol_entry) * hol->num_entries);
       hol->short_options = malloc (num_short_options + 1);
 
-      assert (hol->entries && hol->short_options
-             && hol->num_entries <= SIZE_MAX / sizeof (struct hol_entry));
+      assert (hol->entries && hol->short_options);
+      if (SIZE_MAX <= UINT_MAX)
+       assert (hol->num_entries <= SIZE_MAX / sizeof (struct hol_entry));
 
       /* Fill in the entries.  */
       so = hol->short_options;
@@ -704,14 +679,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;
 }
 
@@ -845,8 +826,9 @@ hol_append (struct hol *hol, struct hol *more)
          char *short_options =
            malloc (hol_so_len + strlen (more->short_options) + 1);
 
-         assert (entries && short_options
-                 && num_entries <= SIZE_MAX / sizeof (struct hol_entry));
+         assert (entries && short_options);
+         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)),
@@ -1073,7 +1055,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--)
@@ -1108,13 +1096,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));
          }
@@ -1685,20 +1675,13 @@ void __argp_help (const struct argp *argp, FILE *stream,
 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
+  char *name = strrchr (program_invocation_name, '/');
+  return name ? name + 1 : 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,
@@ -1869,13 +1852,20 @@ __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 = "Unknown system error"; /* FIXME: translate this */
 #endif
+                 fputs (s, stream);
                }
            }