OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.17.0/] [newlib/] [libc/] [sys/] [linux/] [argp/] [argp-help.c] - Diff between revs 148 and 158

Only display areas with differences | Details | Blame | View Log

Rev 148 Rev 158
/* Hierarchial argument parsing help output
/* Hierarchial argument parsing help output
   Copyright (C) 1995-2000, 2001 Free Software Foundation, Inc.
   Copyright (C) 1995-2000, 2001 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   This file is part of the GNU C Library.
   Written by Miles Bader <miles@gnu.ai.mit.edu>.
   Written by Miles Bader <miles@gnu.ai.mit.edu>.
 
 
   The GNU C Library is free software; you can redistribute it and/or
   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.
   version 2.1 of the License, or (at your option) any later version.
 
 
   The GNU C Library is distributed in the hope that it will be useful,
   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.
   Lesser General Public License for more details.
 
 
   You should have received a copy of the GNU Lesser General Public
   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, write to the Free
   License along with the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  */
   02111-1307 USA.  */
 
 
#ifndef _GNU_SOURCE
#ifndef _GNU_SOURCE
# define _GNU_SOURCE    1
# define _GNU_SOURCE    1
#endif
#endif
 
 
#ifdef HAVE_CONFIG_H
#ifdef HAVE_CONFIG_H
#include <config.h>
#include <config.h>
#endif
#endif
 
 
#ifndef alloca
#ifndef alloca
# ifdef __GNUC__
# ifdef __GNUC__
#  define alloca __builtin_alloca
#  define alloca __builtin_alloca
#  define HAVE_ALLOCA 1
#  define HAVE_ALLOCA 1
# else
# else
#  if defined HAVE_ALLOCA_H || defined _LIBC
#  if defined HAVE_ALLOCA_H || defined _LIBC
#   include <alloca.h>
#   include <alloca.h>
#  else
#  else
#   ifdef _AIX
#   ifdef _AIX
 #pragma alloca
 #pragma alloca
#   else
#   else
#    ifndef alloca
#    ifndef alloca
char *alloca ();
char *alloca ();
#    endif
#    endif
#   endif
#   endif
#  endif
#  endif
# endif
# endif
#endif
#endif
 
 
#include <stddef.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#include <assert.h>
#include <assert.h>
#include <stdarg.h>
#include <stdarg.h>
#include <malloc.h>
#include <malloc.h>
#include <ctype.h>
#include <ctype.h>
#ifdef USE_IN_LIBIO
#ifdef USE_IN_LIBIO
# include <wchar.h>
# include <wchar.h>
#endif
#endif
 
 
#ifndef _
#ifndef _
/* This is for other GNU distributions with internationalized messages.  */
/* This is for other GNU distributions with internationalized messages.  */
# if defined HAVE_LIBINTL_H || defined _LIBC
# if defined HAVE_LIBINTL_H || defined _LIBC
#  include <libintl.h>
#  include <libintl.h>
#  ifdef _LIBC
#  ifdef _LIBC
#   undef dgettext
#   undef dgettext
#   define dgettext(domain, msgid) __dcgettext (domain, msgid, LC_MESSAGES)
#   define dgettext(domain, msgid) __dcgettext (domain, msgid, LC_MESSAGES)
#  endif
#  endif
# else
# else
#  define dgettext(domain, msgid) (msgid)
#  define dgettext(domain, msgid) (msgid)
# endif
# endif
#endif
#endif
 
 
#include <argp.h>
#include <argp.h>
#include "argp-fmtstream.h"
#include "argp-fmtstream.h"
#include "argp-namefrob.h"
#include "argp-namefrob.h"


/* User-selectable (using an environment variable) formatting parameters.
/* User-selectable (using an environment variable) formatting parameters.
 
 
   These may be specified in an environment variable called `ARGP_HELP_FMT',
   These may be specified in an environment variable called `ARGP_HELP_FMT',
   with a contents like:  VAR1=VAL1,VAR2=VAL2,BOOLVAR2,no-BOOLVAR2
   with a contents like:  VAR1=VAL1,VAR2=VAL2,BOOLVAR2,no-BOOLVAR2
   Where VALn must be a positive integer.  The list of variables is in the
   Where VALn must be a positive integer.  The list of variables is in the
   UPARAM_NAMES vector, below.  */
   UPARAM_NAMES vector, below.  */
 
 
/* Default parameters.  */
/* Default parameters.  */
#define DUP_ARGS      0         /* True if option argument can be duplicated. */
#define DUP_ARGS      0         /* True if option argument can be duplicated. */
#define DUP_ARGS_NOTE 1         /* True to print a note about duplicate args. */
#define DUP_ARGS_NOTE 1         /* True to print a note about duplicate args. */
#define SHORT_OPT_COL 2         /* column in which short options start */
#define SHORT_OPT_COL 2         /* column in which short options start */
#define LONG_OPT_COL  6         /* column in which long options start */
#define LONG_OPT_COL  6         /* column in which long options start */
#define DOC_OPT_COL   2         /* column in which doc options start */
#define DOC_OPT_COL   2         /* column in which doc options start */
#define OPT_DOC_COL  29         /* column in which option text starts */
#define OPT_DOC_COL  29         /* column in which option text starts */
#define HEADER_COL    1         /* column in which group headers are printed */
#define HEADER_COL    1         /* column in which group headers are printed */
#define USAGE_INDENT 12         /* indentation of wrapped usage lines */
#define USAGE_INDENT 12         /* indentation of wrapped usage lines */
#define RMARGIN      79         /* right margin used for wrapping */
#define RMARGIN      79         /* right margin used for wrapping */
 
 
/* User-selectable (using an environment variable) formatting parameters.
/* User-selectable (using an environment variable) formatting parameters.
   They must all be of type `int' for the parsing code to work.  */
   They must all be of type `int' for the parsing code to work.  */
struct uparams
struct uparams
{
{
  /* If true, arguments for an option are shown with both short and long
  /* If true, arguments for an option are shown with both short and long
     options, even when a given option has both, e.g. `-x ARG, --longx=ARG'.
     options, even when a given option has both, e.g. `-x ARG, --longx=ARG'.
     If false, then if an option has both, the argument is only shown with
     If false, then if an option has both, the argument is only shown with
     the long one, e.g., `-x, --longx=ARG', and a message indicating that
     the long one, e.g., `-x, --longx=ARG', and a message indicating that
     this really means both is printed below the options.  */
     this really means both is printed below the options.  */
  int dup_args;
  int dup_args;
 
 
  /* This is true if when DUP_ARGS is false, and some duplicate arguments have
  /* This is true if when DUP_ARGS is false, and some duplicate arguments have
     been suppressed, an explanatory message should be printed.  */
     been suppressed, an explanatory message should be printed.  */
  int dup_args_note;
  int dup_args_note;
 
 
  /* Various output columns.  */
  /* Various output columns.  */
  int short_opt_col;
  int short_opt_col;
  int long_opt_col;
  int long_opt_col;
  int doc_opt_col;
  int doc_opt_col;
  int opt_doc_col;
  int opt_doc_col;
  int header_col;
  int header_col;
  int usage_indent;
  int usage_indent;
  int rmargin;
  int rmargin;
 
 
  int valid;                    /* True when the values in here are valid.  */
  int valid;                    /* True when the values in here are valid.  */
};
};
 
 
/* This is a global variable, as user options are only ever read once.  */
/* This is a global variable, as user options are only ever read once.  */
static struct uparams uparams = {
static struct uparams uparams = {
  DUP_ARGS, DUP_ARGS_NOTE,
  DUP_ARGS, DUP_ARGS_NOTE,
  SHORT_OPT_COL, LONG_OPT_COL, DOC_OPT_COL, OPT_DOC_COL, HEADER_COL,
  SHORT_OPT_COL, LONG_OPT_COL, DOC_OPT_COL, OPT_DOC_COL, HEADER_COL,
  USAGE_INDENT, RMARGIN,
  USAGE_INDENT, RMARGIN,
  0
  0
};
};
 
 
/* A particular uparam, and what the user name is.  */
/* A particular uparam, and what the user name is.  */
struct uparam_name
struct uparam_name
{
{
  const char *name;             /* User name.  */
  const char *name;             /* User name.  */
  int is_bool;                  /* Whether it's `boolean'.  */
  int is_bool;                  /* Whether it's `boolean'.  */
  size_t uparams_offs;          /* Location of the (int) field in UPARAMS.  */
  size_t uparams_offs;          /* Location of the (int) field in UPARAMS.  */
};
};
 
 
/* The name-field mappings we know about.  */
/* The name-field mappings we know about.  */
static const struct uparam_name uparam_names[] =
static const struct uparam_name uparam_names[] =
{
{
  { "dup-args",       1, offsetof (struct uparams, dup_args) },
  { "dup-args",       1, offsetof (struct uparams, dup_args) },
  { "dup-args-note",  1, offsetof (struct uparams, dup_args_note) },
  { "dup-args-note",  1, offsetof (struct uparams, dup_args_note) },
  { "short-opt-col",  0, offsetof (struct uparams, short_opt_col) },
  { "short-opt-col",  0, offsetof (struct uparams, short_opt_col) },
  { "long-opt-col",   0, offsetof (struct uparams, long_opt_col) },
  { "long-opt-col",   0, offsetof (struct uparams, long_opt_col) },
  { "doc-opt-col",    0, offsetof (struct uparams, doc_opt_col) },
  { "doc-opt-col",    0, offsetof (struct uparams, doc_opt_col) },
  { "opt-doc-col",    0, offsetof (struct uparams, opt_doc_col) },
  { "opt-doc-col",    0, offsetof (struct uparams, opt_doc_col) },
  { "header-col",     0, offsetof (struct uparams, header_col) },
  { "header-col",     0, offsetof (struct uparams, header_col) },
  { "usage-indent",   0, offsetof (struct uparams, usage_indent) },
  { "usage-indent",   0, offsetof (struct uparams, usage_indent) },
  { "rmargin",        0, offsetof (struct uparams, rmargin) },
  { "rmargin",        0, offsetof (struct uparams, rmargin) },
  { 0 }
  { 0 }
};
};
 
 
/* Read user options from the environment, and fill in UPARAMS appropiately.  */
/* Read user options from the environment, and fill in UPARAMS appropiately.  */
static void
static void
fill_in_uparams (const struct argp_state *state)
fill_in_uparams (const struct argp_state *state)
{
{
  const char *var = getenv ("ARGP_HELP_FMT");
  const char *var = getenv ("ARGP_HELP_FMT");
 
 
#define SKIPWS(p) do { while (isspace (*p)) p++; } while (0);
#define SKIPWS(p) do { while (isspace (*p)) p++; } while (0);
 
 
  if (var)
  if (var)
    /* Parse var. */
    /* Parse var. */
    while (*var)
    while (*var)
      {
      {
        SKIPWS (var);
        SKIPWS (var);
 
 
        if (isalpha (*var))
        if (isalpha (*var))
          {
          {
            size_t var_len;
            size_t var_len;
            const struct uparam_name *un;
            const struct uparam_name *un;
            int unspec = 0, val = 0;
            int unspec = 0, val = 0;
            const char *arg = var;
            const char *arg = var;
 
 
            while (isalnum (*arg) || *arg == '-' || *arg == '_')
            while (isalnum (*arg) || *arg == '-' || *arg == '_')
              arg++;
              arg++;
            var_len = arg - var;
            var_len = arg - var;
 
 
            SKIPWS (arg);
            SKIPWS (arg);
 
 
            if (*arg == '\0' || *arg == ',')
            if (*arg == '\0' || *arg == ',')
              unspec = 1;
              unspec = 1;
            else if (*arg == '=')
            else if (*arg == '=')
              {
              {
                arg++;
                arg++;
                SKIPWS (arg);
                SKIPWS (arg);
              }
              }
 
 
            if (unspec)
            if (unspec)
              {
              {
                if (var[0] == 'n' && var[1] == 'o' && var[2] == '-')
                if (var[0] == 'n' && var[1] == 'o' && var[2] == '-')
                  {
                  {
                    val = 0;
                    val = 0;
                    var += 3;
                    var += 3;
                    var_len -= 3;
                    var_len -= 3;
                  }
                  }
                else
                else
                  val = 1;
                  val = 1;
              }
              }
            else if (isdigit (*arg))
            else if (isdigit (*arg))
              {
              {
                val = atoi (arg);
                val = atoi (arg);
                while (isdigit (*arg))
                while (isdigit (*arg))
                  arg++;
                  arg++;
                SKIPWS (arg);
                SKIPWS (arg);
              }
              }
 
 
            for (un = uparam_names; un->name; un++)
            for (un = uparam_names; un->name; un++)
              if (strlen (un->name) == var_len
              if (strlen (un->name) == var_len
                  && strncmp (var, un->name, var_len) == 0)
                  && strncmp (var, un->name, var_len) == 0)
                {
                {
                  if (unspec && !un->is_bool)
                  if (unspec && !un->is_bool)
                    __argp_failure (state, 0, 0,
                    __argp_failure (state, 0, 0,
                                    dgettext (state->root_argp->argp_domain, "\
                                    dgettext (state->root_argp->argp_domain, "\
%.*s: ARGP_HELP_FMT parameter requires a value"),
%.*s: ARGP_HELP_FMT parameter requires a value"),
                                    (int) var_len, var);
                                    (int) var_len, var);
                  else
                  else
                    *(int *)((char *)&uparams + un->uparams_offs) = val;
                    *(int *)((char *)&uparams + un->uparams_offs) = val;
                  break;
                  break;
                }
                }
            if (! un->name)
            if (! un->name)
              __argp_failure (state, 0, 0,
              __argp_failure (state, 0, 0,
                              dgettext (state->root_argp->argp_domain, "\
                              dgettext (state->root_argp->argp_domain, "\
%.*s: Unknown ARGP_HELP_FMT parameter"),
%.*s: Unknown ARGP_HELP_FMT parameter"),
                              (int) var_len, var);
                              (int) var_len, var);
 
 
            var = arg;
            var = arg;
            if (*var == ',')
            if (*var == ',')
              var++;
              var++;
          }
          }
        else if (*var)
        else if (*var)
          {
          {
            __argp_failure (state, 0, 0,
            __argp_failure (state, 0, 0,
                            dgettext (state->root_argp->argp_domain,
                            dgettext (state->root_argp->argp_domain,
                                      "Garbage in ARGP_HELP_FMT: %s"), var);
                                      "Garbage in ARGP_HELP_FMT: %s"), var);
            break;
            break;
          }
          }
      }
      }
}
}


/* Returns true if OPT hasn't been marked invisible.  Visibility only affects
/* Returns true if OPT hasn't been marked invisible.  Visibility only affects
   whether OPT is displayed or used in sorting, not option shadowing.  */
   whether OPT is displayed or used in sorting, not option shadowing.  */
#define ovisible(opt) (! ((opt)->flags & OPTION_HIDDEN))
#define ovisible(opt) (! ((opt)->flags & OPTION_HIDDEN))
 
 
/* Returns true if OPT is an alias for an earlier option.  */
/* Returns true if OPT is an alias for an earlier option.  */
#define oalias(opt) ((opt)->flags & OPTION_ALIAS)
#define oalias(opt) ((opt)->flags & OPTION_ALIAS)
 
 
/* Returns true if OPT is an documentation-only entry.  */
/* Returns true if OPT is an documentation-only entry.  */
#define odoc(opt) ((opt)->flags & OPTION_DOC)
#define odoc(opt) ((opt)->flags & OPTION_DOC)
 
 
/* Returns true if OPT is the end-of-list marker for a list of options.  */
/* Returns true if OPT is the end-of-list marker for a list of options.  */
#define oend(opt) __option_is_end (opt)
#define oend(opt) __option_is_end (opt)
 
 
/* Returns true if OPT has a short option.  */
/* Returns true if OPT has a short option.  */
#define oshort(opt) __option_is_short (opt)
#define oshort(opt) __option_is_short (opt)


/*
/*
   The help format for a particular option is like:
   The help format for a particular option is like:
 
 
     -xARG, -yARG, --long1=ARG, --long2=ARG        Documentation...
     -xARG, -yARG, --long1=ARG, --long2=ARG        Documentation...
 
 
   Where ARG will be omitted if there's no argument, for this option, or
   Where ARG will be omitted if there's no argument, for this option, or
   will be surrounded by "[" and "]" appropiately if the argument is
   will be surrounded by "[" and "]" appropiately if the argument is
   optional.  The documentation string is word-wrapped appropiately, and if
   optional.  The documentation string is word-wrapped appropiately, and if
   the list of options is long enough, it will be started on a separate line.
   the list of options is long enough, it will be started on a separate line.
   If there are no short options for a given option, the first long option is
   If there are no short options for a given option, the first long option is
   indented slighly in a way that's supposed to make most long options appear
   indented slighly in a way that's supposed to make most long options appear
   to be in a separate column.
   to be in a separate column.
 
 
   For example, the following output (from ps):
   For example, the following output (from ps):
 
 
     -p PID, --pid=PID          List the process PID
     -p PID, --pid=PID          List the process PID
         --pgrp=PGRP            List processes in the process group PGRP
         --pgrp=PGRP            List processes in the process group PGRP
     -P, -x, --no-parent        Include processes without parents
     -P, -x, --no-parent        Include processes without parents
     -Q, --all-fields           Don't elide unusable fields (normally if there's
     -Q, --all-fields           Don't elide unusable fields (normally if there's
                                some reason ps can't print a field for any
                                some reason ps can't print a field for any
                                process, it's removed from the output entirely)
                                process, it's removed from the output entirely)
     -r, --reverse, --gratuitously-long-reverse-option
     -r, --reverse, --gratuitously-long-reverse-option
                                Reverse the order of any sort
                                Reverse the order of any sort
         --session[=SID]        Add the processes from the session SID (which
         --session[=SID]        Add the processes from the session SID (which
                                defaults to the sid of the current process)
                                defaults to the sid of the current process)
 
 
    Here are some more options:
    Here are some more options:
     -f ZOT, --foonly=ZOT       Glork a foonly
     -f ZOT, --foonly=ZOT       Glork a foonly
     -z, --zaza                 Snit a zar
     -z, --zaza                 Snit a zar
 
 
     -?, --help                 Give this help list
     -?, --help                 Give this help list
         --usage                Give a short usage message
         --usage                Give a short usage message
     -V, --version              Print program version
     -V, --version              Print program version
 
 
   The struct argp_option array for the above could look like:
   The struct argp_option array for the above could look like:
 
 
   {
   {
     {"pid",       'p',      "PID",  0, "List the process PID"},
     {"pid",       'p',      "PID",  0, "List the process PID"},
     {"pgrp",      OPT_PGRP, "PGRP", 0, "List processes in the process group PGRP"},
     {"pgrp",      OPT_PGRP, "PGRP", 0, "List processes in the process group PGRP"},
     {"no-parent", 'P',       0,     0, "Include processes without parents"},
     {"no-parent", 'P',       0,     0, "Include processes without parents"},
     {0,           'x',       0,     OPTION_ALIAS},
     {0,           'x',       0,     OPTION_ALIAS},
     {"all-fields",'Q',       0,     0, "Don't elide unusable fields (normally"
     {"all-fields",'Q',       0,     0, "Don't elide unusable fields (normally"
                                        " if there's some reason ps can't"
                                        " if there's some reason ps can't"
                                        " print a field for any process, it's"
                                        " print a field for any process, it's"
                                        " removed from the output entirely)" },
                                        " removed from the output entirely)" },
     {"reverse",   'r',       0,     0, "Reverse the order of any sort"},
     {"reverse",   'r',       0,     0, "Reverse the order of any sort"},
     {"gratuitously-long-reverse-option", 0, 0, OPTION_ALIAS},
     {"gratuitously-long-reverse-option", 0, 0, OPTION_ALIAS},
     {"session",   OPT_SESS,  "SID", OPTION_ARG_OPTIONAL,
     {"session",   OPT_SESS,  "SID", OPTION_ARG_OPTIONAL,
                                        "Add the processes from the session"
                                        "Add the processes from the session"
                                        " SID (which defaults to the sid of"
                                        " SID (which defaults to the sid of"
                                        " the current process)" },
                                        " the current process)" },
 
 
     {0,0,0,0, "Here are some more options:"},
     {0,0,0,0, "Here are some more options:"},
     {"foonly", 'f', "ZOT", 0, "Glork a foonly"},
     {"foonly", 'f', "ZOT", 0, "Glork a foonly"},
     {"zaza", 'z', 0, 0, "Snit a zar"},
     {"zaza", 'z', 0, 0, "Snit a zar"},
 
 
     {0}
     {0}
   }
   }
 
 
   Note that the last three options are automatically supplied by argp_parse,
   Note that the last three options are automatically supplied by argp_parse,
   unless you tell it not to with ARGP_NO_HELP.
   unless you tell it not to with ARGP_NO_HELP.
 
 
*/
*/


/* Returns true if CH occurs between BEG and END.  */
/* Returns true if CH occurs between BEG and END.  */
static int
static int
find_char (char ch, char *beg, char *end)
find_char (char ch, char *beg, char *end)
{
{
  while (beg < end)
  while (beg < end)
    if (*beg == ch)
    if (*beg == ch)
      return 1;
      return 1;
    else
    else
      beg++;
      beg++;
  return 0;
  return 0;
}
}


struct hol_cluster;             /* fwd decl */
struct hol_cluster;             /* fwd decl */
 
 
struct hol_entry
struct hol_entry
{
{
  /* First option.  */
  /* First option.  */
  const struct argp_option *opt;
  const struct argp_option *opt;
  /* Number of options (including aliases).  */
  /* Number of options (including aliases).  */
  unsigned num;
  unsigned num;
 
 
  /* A pointers into the HOL's short_options field, to the first short option
  /* A pointers into the HOL's short_options field, to the first short option
     letter for this entry.  The order of the characters following this point
     letter for this entry.  The order of the characters following this point
     corresponds to the order of options pointed to by OPT, and there are at
     corresponds to the order of options pointed to by OPT, and there are at
     most NUM.  A short option recorded in a option following OPT is only
     most NUM.  A short option recorded in a option following OPT is only
     valid if it occurs in the right place in SHORT_OPTIONS (otherwise it's
     valid if it occurs in the right place in SHORT_OPTIONS (otherwise it's
     probably been shadowed by some other entry).  */
     probably been shadowed by some other entry).  */
  char *short_options;
  char *short_options;
 
 
  /* Entries are sorted by their group first, in the order:
  /* Entries are sorted by their group first, in the order:
       1, 2, ..., n, 0, -m, ..., -2, -1
       1, 2, ..., n, 0, -m, ..., -2, -1
     and then alphabetically within each group.  The default is 0.  */
     and then alphabetically within each group.  The default is 0.  */
  int group;
  int group;
 
 
  /* The cluster of options this entry belongs to, or 0 if none.  */
  /* The cluster of options this entry belongs to, or 0 if none.  */
  struct hol_cluster *cluster;
  struct hol_cluster *cluster;
 
 
  /* The argp from which this option came.  */
  /* The argp from which this option came.  */
  const struct argp *argp;
  const struct argp *argp;
};
};
 
 
/* A cluster of entries to reflect the argp tree structure.  */
/* A cluster of entries to reflect the argp tree structure.  */
struct hol_cluster
struct hol_cluster
{
{
  /* A descriptive header printed before options in this cluster.  */
  /* A descriptive header printed before options in this cluster.  */
  const char *header;
  const char *header;
 
 
  /* Used to order clusters within the same group with the same parent,
  /* Used to order clusters within the same group with the same parent,
     according to the order in which they occurred in the parent argp's child
     according to the order in which they occurred in the parent argp's child
     list.  */
     list.  */
  int index;
  int index;
 
 
  /* How to sort this cluster with respect to options and other clusters at the
  /* How to sort this cluster with respect to options and other clusters at the
     same depth (clusters always follow options in the same group).  */
     same depth (clusters always follow options in the same group).  */
  int group;
  int group;
 
 
  /* The cluster to which this cluster belongs, or 0 if it's at the base
  /* The cluster to which this cluster belongs, or 0 if it's at the base
     level.  */
     level.  */
  struct hol_cluster *parent;
  struct hol_cluster *parent;
 
 
  /* The argp from which this cluster is (eventually) derived.  */
  /* The argp from which this cluster is (eventually) derived.  */
  const struct argp *argp;
  const struct argp *argp;
 
 
  /* The distance this cluster is from the root.  */
  /* The distance this cluster is from the root.  */
  int depth;
  int depth;
 
 
  /* Clusters in a given hol are kept in a linked list, to make freeing them
  /* Clusters in a given hol are kept in a linked list, to make freeing them
     possible.  */
     possible.  */
  struct hol_cluster *next;
  struct hol_cluster *next;
};
};
 
 
/* A list of options for help.  */
/* A list of options for help.  */
struct hol
struct hol
{
{
  /* An array of hol_entry's.  */
  /* An array of hol_entry's.  */
  struct hol_entry *entries;
  struct hol_entry *entries;
  /* The number of entries in this hol.  If this field is zero, the others
  /* The number of entries in this hol.  If this field is zero, the others
     are undefined.  */
     are undefined.  */
  unsigned num_entries;
  unsigned num_entries;
 
 
  /* A string containing all short options in this HOL.  Each entry contains
  /* A string containing all short options in this HOL.  Each entry contains
     pointers into this string, so the order can't be messed with blindly.  */
     pointers into this string, so the order can't be messed with blindly.  */
  char *short_options;
  char *short_options;
 
 
  /* Clusters of entries in this hol.  */
  /* Clusters of entries in this hol.  */
  struct hol_cluster *clusters;
  struct hol_cluster *clusters;
};
};


/* Create a struct hol from the options in ARGP.  CLUSTER is the
/* Create a struct hol from the options in ARGP.  CLUSTER is the
   hol_cluster in which these entries occur, or 0, if at the root.  */
   hol_cluster in which these entries occur, or 0, if at the root.  */
static struct hol *
static struct hol *
make_hol (const struct argp *argp, struct hol_cluster *cluster)
make_hol (const struct argp *argp, struct hol_cluster *cluster)
{
{
  char *so;
  char *so;
  const struct argp_option *o;
  const struct argp_option *o;
  const struct argp_option *opts = argp->options;
  const struct argp_option *opts = argp->options;
  struct hol_entry *entry;
  struct hol_entry *entry;
  unsigned num_short_options = 0;
  unsigned num_short_options = 0;
  struct hol *hol = malloc (sizeof (struct hol));
  struct hol *hol = malloc (sizeof (struct hol));
 
 
  assert (hol);
  assert (hol);
 
 
  hol->num_entries = 0;
  hol->num_entries = 0;
  hol->clusters = 0;
  hol->clusters = 0;
 
 
  if (opts)
  if (opts)
    {
    {
      int cur_group = 0;
      int cur_group = 0;
 
 
      /* The first option must not be an alias.  */
      /* The first option must not be an alias.  */
      assert (! oalias (opts));
      assert (! oalias (opts));
 
 
      /* Calculate the space needed.  */
      /* Calculate the space needed.  */
      for (o = opts; ! oend (o); o++)
      for (o = opts; ! oend (o); o++)
        {
        {
          if (! oalias (o))
          if (! oalias (o))
            hol->num_entries++;
            hol->num_entries++;
          if (oshort (o))
          if (oshort (o))
            num_short_options++;        /* This is an upper bound.  */
            num_short_options++;        /* This is an upper bound.  */
        }
        }
 
 
      hol->entries = malloc (sizeof (struct hol_entry) * hol->num_entries);
      hol->entries = malloc (sizeof (struct hol_entry) * hol->num_entries);
      hol->short_options = malloc (num_short_options + 1);
      hol->short_options = malloc (num_short_options + 1);
 
 
      assert (hol->entries && hol->short_options);
      assert (hol->entries && hol->short_options);
 
 
      /* Fill in the entries.  */
      /* Fill in the entries.  */
      so = hol->short_options;
      so = hol->short_options;
      for (o = opts, entry = hol->entries; ! oend (o); entry++)
      for (o = opts, entry = hol->entries; ! oend (o); entry++)
        {
        {
          entry->opt = o;
          entry->opt = o;
          entry->num = 0;
          entry->num = 0;
          entry->short_options = so;
          entry->short_options = so;
          entry->group = cur_group =
          entry->group = cur_group =
            o->group
            o->group
            ? o->group
            ? o->group
            : ((!o->name && !o->key)
            : ((!o->name && !o->key)
               ? cur_group + 1
               ? cur_group + 1
               : cur_group);
               : cur_group);
          entry->cluster = cluster;
          entry->cluster = cluster;
          entry->argp = argp;
          entry->argp = argp;
 
 
          do
          do
            {
            {
              entry->num++;
              entry->num++;
              if (oshort (o) && ! find_char (o->key, hol->short_options, so))
              if (oshort (o) && ! find_char (o->key, hol->short_options, so))
                /* O has a valid short option which hasn't already been used.*/
                /* O has a valid short option which hasn't already been used.*/
                *so++ = o->key;
                *so++ = o->key;
              o++;
              o++;
            }
            }
          while (! oend (o) && oalias (o));
          while (! oend (o) && oalias (o));
        }
        }
      *so = '\0';               /* null terminated so we can find the length */
      *so = '\0';               /* null terminated so we can find the length */
    }
    }
 
 
  return hol;
  return hol;
}
}


/* Add a new cluster to HOL, with the given GROUP and HEADER (taken from the
/* Add a new cluster to HOL, with the given GROUP and HEADER (taken from the
   associated argp child list entry), INDEX, and PARENT, and return a pointer
   associated argp child list entry), INDEX, and PARENT, and return a pointer
   to it.  ARGP is the argp that this cluster results from.  */
   to it.  ARGP is the argp that this cluster results from.  */
static struct hol_cluster *
static struct hol_cluster *
hol_add_cluster (struct hol *hol, int group, const char *header, int index,
hol_add_cluster (struct hol *hol, int group, const char *header, int index,
                 struct hol_cluster *parent, const struct argp *argp)
                 struct hol_cluster *parent, const struct argp *argp)
{
{
  struct hol_cluster *cl = malloc (sizeof (struct hol_cluster));
  struct hol_cluster *cl = malloc (sizeof (struct hol_cluster));
  if (cl)
  if (cl)
    {
    {
      cl->group = group;
      cl->group = group;
      cl->header = header;
      cl->header = header;
 
 
      cl->index = index;
      cl->index = index;
      cl->parent = parent;
      cl->parent = parent;
      cl->argp = argp;
      cl->argp = argp;
      cl->depth = parent ? parent->depth + 1 : 0;
      cl->depth = parent ? parent->depth + 1 : 0;
 
 
      cl->next = hol->clusters;
      cl->next = hol->clusters;
      hol->clusters = cl;
      hol->clusters = cl;
    }
    }
  return cl;
  return cl;
}
}


/* Free HOL and any resources it uses.  */
/* Free HOL and any resources it uses.  */
static void
static void
hol_free (struct hol *hol)
hol_free (struct hol *hol)
{
{
  struct hol_cluster *cl = hol->clusters;
  struct hol_cluster *cl = hol->clusters;
 
 
  while (cl)
  while (cl)
    {
    {
      struct hol_cluster *next = cl->next;
      struct hol_cluster *next = cl->next;
      free (cl);
      free (cl);
      cl = next;
      cl = next;
    }
    }
 
 
  if (hol->num_entries > 0)
  if (hol->num_entries > 0)
    {
    {
      free (hol->entries);
      free (hol->entries);
      free (hol->short_options);
      free (hol->short_options);
    }
    }
 
 
  free (hol);
  free (hol);
}
}


static inline int
static inline int
hol_entry_short_iterate (const struct hol_entry *entry,
hol_entry_short_iterate (const struct hol_entry *entry,
                         int (*func)(const struct argp_option *opt,
                         int (*func)(const struct argp_option *opt,
                                     const struct argp_option *real,
                                     const struct argp_option *real,
                                     const char *domain, void *cookie),
                                     const char *domain, void *cookie),
                         const char *domain, void *cookie)
                         const char *domain, void *cookie)
{
{
  unsigned nopts;
  unsigned nopts;
  int val = 0;
  int val = 0;
  const struct argp_option *opt, *real = entry->opt;
  const struct argp_option *opt, *real = entry->opt;
  char *so = entry->short_options;
  char *so = entry->short_options;
 
 
  for (opt = real, nopts = entry->num; nopts > 0 && !val; opt++, nopts--)
  for (opt = real, nopts = entry->num; nopts > 0 && !val; opt++, nopts--)
    if (oshort (opt) && *so == opt->key)
    if (oshort (opt) && *so == opt->key)
      {
      {
        if (!oalias (opt))
        if (!oalias (opt))
          real = opt;
          real = opt;
        if (ovisible (opt))
        if (ovisible (opt))
          val = (*func)(opt, real, domain, cookie);
          val = (*func)(opt, real, domain, cookie);
        so++;
        so++;
      }
      }
 
 
  return val;
  return val;
}
}
 
 
static inline int
static inline int
hol_entry_long_iterate (const struct hol_entry *entry,
hol_entry_long_iterate (const struct hol_entry *entry,
                        int (*func)(const struct argp_option *opt,
                        int (*func)(const struct argp_option *opt,
                                    const struct argp_option *real,
                                    const struct argp_option *real,
                                    const char *domain, void *cookie),
                                    const char *domain, void *cookie),
                        const char *domain, void *cookie)
                        const char *domain, void *cookie)
{
{
  unsigned nopts;
  unsigned nopts;
  int val = 0;
  int val = 0;
  const struct argp_option *opt, *real = entry->opt;
  const struct argp_option *opt, *real = entry->opt;
 
 
  for (opt = real, nopts = entry->num; nopts > 0 && !val; opt++, nopts--)
  for (opt = real, nopts = entry->num; nopts > 0 && !val; opt++, nopts--)
    if (opt->name)
    if (opt->name)
      {
      {
        if (!oalias (opt))
        if (!oalias (opt))
          real = opt;
          real = opt;
        if (ovisible (opt))
        if (ovisible (opt))
          val = (*func)(opt, real, domain, cookie);
          val = (*func)(opt, real, domain, cookie);
      }
      }
 
 
  return val;
  return val;
}
}


/* Iterator that returns true for the first short option.  */
/* Iterator that returns true for the first short option.  */
static inline int
static inline int
until_short (const struct argp_option *opt, const struct argp_option *real,
until_short (const struct argp_option *opt, const struct argp_option *real,
             const char *domain, void *cookie)
             const char *domain, void *cookie)
{
{
  return oshort (opt) ? opt->key : 0;
  return oshort (opt) ? opt->key : 0;
}
}
 
 
/* Returns the first valid short option in ENTRY, or 0 if there is none.  */
/* Returns the first valid short option in ENTRY, or 0 if there is none.  */
static char
static char
hol_entry_first_short (const struct hol_entry *entry)
hol_entry_first_short (const struct hol_entry *entry)
{
{
  return hol_entry_short_iterate (entry, until_short,
  return hol_entry_short_iterate (entry, until_short,
                                  entry->argp->argp_domain, 0);
                                  entry->argp->argp_domain, 0);
}
}
 
 
/* Returns the first valid long option in ENTRY, or 0 if there is none.  */
/* Returns the first valid long option in ENTRY, or 0 if there is none.  */
static const char *
static const char *
hol_entry_first_long (const struct hol_entry *entry)
hol_entry_first_long (const struct hol_entry *entry)
{
{
  const struct argp_option *opt;
  const struct argp_option *opt;
  unsigned num;
  unsigned num;
  for (opt = entry->opt, num = entry->num; num > 0; opt++, num--)
  for (opt = entry->opt, num = entry->num; num > 0; opt++, num--)
    if (opt->name && ovisible (opt))
    if (opt->name && ovisible (opt))
      return opt->name;
      return opt->name;
  return 0;
  return 0;
}
}
 
 
/* Returns the entry in HOL with the long option name NAME, or 0 if there is
/* Returns the entry in HOL with the long option name NAME, or 0 if there is
   none.  */
   none.  */
static struct hol_entry *
static struct hol_entry *
hol_find_entry (struct hol *hol, const char *name)
hol_find_entry (struct hol *hol, const char *name)
{
{
  struct hol_entry *entry = hol->entries;
  struct hol_entry *entry = hol->entries;
  unsigned num_entries = hol->num_entries;
  unsigned num_entries = hol->num_entries;
 
 
  while (num_entries-- > 0)
  while (num_entries-- > 0)
    {
    {
      const struct argp_option *opt = entry->opt;
      const struct argp_option *opt = entry->opt;
      unsigned num_opts = entry->num;
      unsigned num_opts = entry->num;
 
 
      while (num_opts-- > 0)
      while (num_opts-- > 0)
        if (opt->name && ovisible (opt) && strcmp (opt->name, name) == 0)
        if (opt->name && ovisible (opt) && strcmp (opt->name, name) == 0)
          return entry;
          return entry;
        else
        else
          opt++;
          opt++;
 
 
      entry++;
      entry++;
    }
    }
 
 
  return 0;
  return 0;
}
}


/* If an entry with the long option NAME occurs in HOL, set it's special
/* If an entry with the long option NAME occurs in HOL, set it's special
   sort position to GROUP.  */
   sort position to GROUP.  */
static void
static void
hol_set_group (struct hol *hol, const char *name, int group)
hol_set_group (struct hol *hol, const char *name, int group)
{
{
  struct hol_entry *entry = hol_find_entry (hol, name);
  struct hol_entry *entry = hol_find_entry (hol, name);
  if (entry)
  if (entry)
    entry->group = group;
    entry->group = group;
}
}


/* Order by group:  0, 1, 2, ..., n, -m, ..., -2, -1.
/* Order by group:  0, 1, 2, ..., n, -m, ..., -2, -1.
   EQ is what to return if GROUP1 and GROUP2 are the same.  */
   EQ is what to return if GROUP1 and GROUP2 are the same.  */
static int
static int
group_cmp (int group1, int group2, int eq)
group_cmp (int group1, int group2, int eq)
{
{
  if (group1 == group2)
  if (group1 == group2)
    return eq;
    return eq;
  else if ((group1 < 0 && group2 < 0) || (group1 >= 0 && group2 >= 0))
  else if ((group1 < 0 && group2 < 0) || (group1 >= 0 && group2 >= 0))
    return group1 - group2;
    return group1 - group2;
  else
  else
    return group2 - group1;
    return group2 - group1;
}
}
 
 
/* Compare clusters CL1 & CL2 by the order that they should appear in
/* Compare clusters CL1 & CL2 by the order that they should appear in
   output.  */
   output.  */
static int
static int
hol_cluster_cmp (const struct hol_cluster *cl1, const struct hol_cluster *cl2)
hol_cluster_cmp (const struct hol_cluster *cl1, const struct hol_cluster *cl2)
{
{
  /* If one cluster is deeper than the other, use its ancestor at the same
  /* If one cluster is deeper than the other, use its ancestor at the same
     level, so that finding the common ancestor is straightforward.  */
     level, so that finding the common ancestor is straightforward.  */
  while (cl1->depth < cl2->depth)
  while (cl1->depth < cl2->depth)
    cl1 = cl1->parent;
    cl1 = cl1->parent;
  while (cl2->depth < cl1->depth)
  while (cl2->depth < cl1->depth)
    cl2 = cl2->parent;
    cl2 = cl2->parent;
 
 
  /* Now reduce both clusters to their ancestors at the point where both have
  /* Now reduce both clusters to their ancestors at the point where both have
     a common parent; these can be directly compared.  */
     a common parent; these can be directly compared.  */
  while (cl1->parent != cl2->parent)
  while (cl1->parent != cl2->parent)
    cl1 = cl1->parent, cl2 = cl2->parent;
    cl1 = cl1->parent, cl2 = cl2->parent;
 
 
  return group_cmp (cl1->group, cl2->group, cl2->index - cl1->index);
  return group_cmp (cl1->group, cl2->group, cl2->index - cl1->index);
}
}
 
 
/* Return the ancestor of CL that's just below the root (i.e., has a parent
/* Return the ancestor of CL that's just below the root (i.e., has a parent
   of 0).  */
   of 0).  */
static struct hol_cluster *
static struct hol_cluster *
hol_cluster_base (struct hol_cluster *cl)
hol_cluster_base (struct hol_cluster *cl)
{
{
  while (cl->parent)
  while (cl->parent)
    cl = cl->parent;
    cl = cl->parent;
  return cl;
  return cl;
}
}
 
 
/* Return true if CL1 is a child of CL2.  */
/* Return true if CL1 is a child of CL2.  */
static int
static int
hol_cluster_is_child (const struct hol_cluster *cl1,
hol_cluster_is_child (const struct hol_cluster *cl1,
                      const struct hol_cluster *cl2)
                      const struct hol_cluster *cl2)
{
{
  while (cl1 && cl1 != cl2)
  while (cl1 && cl1 != cl2)
    cl1 = cl1->parent;
    cl1 = cl1->parent;
  return cl1 == cl2;
  return cl1 == cl2;
}
}


/* Given the name of a OPTION_DOC option, modifies NAME to start at the tail
/* Given the name of a OPTION_DOC option, modifies NAME to start at the tail
   that should be used for comparisons, and returns true iff it should be
   that should be used for comparisons, and returns true iff it should be
   treated as a non-option.  */
   treated as a non-option.  */
static int
static int
canon_doc_option (const char **name)
canon_doc_option (const char **name)
{
{
  int non_opt;
  int non_opt;
  /* Skip initial whitespace.  */
  /* Skip initial whitespace.  */
  while (isspace (**name))
  while (isspace (**name))
    (*name)++;
    (*name)++;
  /* Decide whether this looks like an option (leading `-') or not.  */
  /* Decide whether this looks like an option (leading `-') or not.  */
  non_opt = (**name != '-');
  non_opt = (**name != '-');
  /* Skip until part of name used for sorting.  */
  /* Skip until part of name used for sorting.  */
  while (**name && !isalnum (**name))
  while (**name && !isalnum (**name))
    (*name)++;
    (*name)++;
  return non_opt;
  return non_opt;
}
}
 
 
/* Order ENTRY1 & ENTRY2 by the order which they should appear in a help
/* Order ENTRY1 & ENTRY2 by the order which they should appear in a help
   listing.  */
   listing.  */
static int
static int
hol_entry_cmp (const struct hol_entry *entry1,
hol_entry_cmp (const struct hol_entry *entry1,
               const struct hol_entry *entry2)
               const struct hol_entry *entry2)
{
{
  /* The group numbers by which the entries should be ordered; if either is
  /* The group numbers by which the entries should be ordered; if either is
     in a cluster, then this is just the group within the cluster.  */
     in a cluster, then this is just the group within the cluster.  */
  int group1 = entry1->group, group2 = entry2->group;
  int group1 = entry1->group, group2 = entry2->group;
 
 
  if (entry1->cluster != entry2->cluster)
  if (entry1->cluster != entry2->cluster)
    {
    {
      /* The entries are not within the same cluster, so we can't compare them
      /* The entries are not within the same cluster, so we can't compare them
         directly, we have to use the appropiate clustering level too.  */
         directly, we have to use the appropiate clustering level too.  */
      if (! entry1->cluster)
      if (! entry1->cluster)
        /* ENTRY1 is at the `base level', not in a cluster, so we have to
        /* ENTRY1 is at the `base level', not in a cluster, so we have to
           compare it's group number with that of the base cluster in which
           compare it's group number with that of the base cluster in which
           ENTRY2 resides.  Note that if they're in the same group, the
           ENTRY2 resides.  Note that if they're in the same group, the
           clustered option always comes laster.  */
           clustered option always comes laster.  */
        return group_cmp (group1, hol_cluster_base (entry2->cluster)->group, -1);
        return group_cmp (group1, hol_cluster_base (entry2->cluster)->group, -1);
      else if (! entry2->cluster)
      else if (! entry2->cluster)
        /* Likewise, but ENTRY2's not in a cluster.  */
        /* Likewise, but ENTRY2's not in a cluster.  */
        return group_cmp (hol_cluster_base (entry1->cluster)->group, group2, 1);
        return group_cmp (hol_cluster_base (entry1->cluster)->group, group2, 1);
      else
      else
        /* Both entries are in clusters, we can just compare the clusters.  */
        /* Both entries are in clusters, we can just compare the clusters.  */
        return hol_cluster_cmp (entry1->cluster, entry2->cluster);
        return hol_cluster_cmp (entry1->cluster, entry2->cluster);
    }
    }
  else if (group1 == group2)
  else if (group1 == group2)
    /* The entries are both in the same cluster and group, so compare them
    /* The entries are both in the same cluster and group, so compare them
       alphabetically.  */
       alphabetically.  */
    {
    {
      int short1 = hol_entry_first_short (entry1);
      int short1 = hol_entry_first_short (entry1);
      int short2 = hol_entry_first_short (entry2);
      int short2 = hol_entry_first_short (entry2);
      int doc1 = odoc (entry1->opt);
      int doc1 = odoc (entry1->opt);
      int doc2 = odoc (entry2->opt);
      int doc2 = odoc (entry2->opt);
      const char *long1 = hol_entry_first_long (entry1);
      const char *long1 = hol_entry_first_long (entry1);
      const char *long2 = hol_entry_first_long (entry2);
      const char *long2 = hol_entry_first_long (entry2);
 
 
      if (doc1)
      if (doc1)
        doc1 = canon_doc_option (&long1);
        doc1 = canon_doc_option (&long1);
      if (doc2)
      if (doc2)
        doc2 = canon_doc_option (&long2);
        doc2 = canon_doc_option (&long2);
 
 
      if (doc1 != doc2)
      if (doc1 != doc2)
        /* `documentation' options always follow normal options (or
        /* `documentation' options always follow normal options (or
           documentation options that *look* like normal options).  */
           documentation options that *look* like normal options).  */
        return doc1 - doc2;
        return doc1 - doc2;
      else if (!short1 && !short2 && long1 && long2)
      else if (!short1 && !short2 && long1 && long2)
        /* Only long options.  */
        /* Only long options.  */
        return __strcasecmp (long1, long2);
        return __strcasecmp (long1, long2);
      else
      else
        /* Compare short/short, long/short, short/long, using the first
        /* Compare short/short, long/short, short/long, using the first
           character of long options.  Entries without *any* valid
           character of long options.  Entries without *any* valid
           options (such as options with OPTION_HIDDEN set) will be put
           options (such as options with OPTION_HIDDEN set) will be put
           first, but as they're not displayed, it doesn't matter where
           first, but as they're not displayed, it doesn't matter where
           they are.  */
           they are.  */
        {
        {
          char first1 = short1 ? short1 : long1 ? *long1 : 0;
          char first1 = short1 ? short1 : long1 ? *long1 : 0;
          char first2 = short2 ? short2 : long2 ? *long2 : 0;
          char first2 = short2 ? short2 : long2 ? *long2 : 0;
#ifdef _tolower
#ifdef _tolower
          int lower_cmp = _tolower (first1) - _tolower (first2);
          int lower_cmp = _tolower (first1) - _tolower (first2);
#else
#else
          int lower_cmp = tolower (first1) - tolower (first2);
          int lower_cmp = tolower (first1) - tolower (first2);
#endif
#endif
          /* Compare ignoring case, except when the options are both the
          /* Compare ignoring case, except when the options are both the
             same letter, in which case lower-case always comes first.  */
             same letter, in which case lower-case always comes first.  */
          return lower_cmp ? lower_cmp : first2 - first1;
          return lower_cmp ? lower_cmp : first2 - first1;
        }
        }
    }
    }
  else
  else
    /* Within the same cluster, but not the same group, so just compare
    /* Within the same cluster, but not the same group, so just compare
       groups.  */
       groups.  */
    return group_cmp (group1, group2, 0);
    return group_cmp (group1, group2, 0);
}
}
 
 
/* Version of hol_entry_cmp with correct signature for qsort.  */
/* Version of hol_entry_cmp with correct signature for qsort.  */
static int
static int
hol_entry_qcmp (const void *entry1_v, const void *entry2_v)
hol_entry_qcmp (const void *entry1_v, const void *entry2_v)
{
{
  return hol_entry_cmp (entry1_v, entry2_v);
  return hol_entry_cmp (entry1_v, entry2_v);
}
}
 
 
/* Sort HOL by group and alphabetically by option name (with short options
/* Sort HOL by group and alphabetically by option name (with short options
   taking precedence over long).  Since the sorting is for display purposes
   taking precedence over long).  Since the sorting is for display purposes
   only, the shadowing of options isn't effected.  */
   only, the shadowing of options isn't effected.  */
static void
static void
hol_sort (struct hol *hol)
hol_sort (struct hol *hol)
{
{
  if (hol->num_entries > 0)
  if (hol->num_entries > 0)
    qsort (hol->entries, hol->num_entries, sizeof (struct hol_entry),
    qsort (hol->entries, hol->num_entries, sizeof (struct hol_entry),
           hol_entry_qcmp);
           hol_entry_qcmp);
}
}


/* Append MORE to HOL, destroying MORE in the process.  Options in HOL shadow
/* Append MORE to HOL, destroying MORE in the process.  Options in HOL shadow
   any in MORE with the same name.  */
   any in MORE with the same name.  */
static void
static void
hol_append (struct hol *hol, struct hol *more)
hol_append (struct hol *hol, struct hol *more)
{
{
  struct hol_cluster **cl_end = &hol->clusters;
  struct hol_cluster **cl_end = &hol->clusters;
  char *tmp;
  char *tmp;
 
 
  /* Steal MORE's cluster list, and add it to the end of HOL's.  */
  /* Steal MORE's cluster list, and add it to the end of HOL's.  */
  while (*cl_end)
  while (*cl_end)
    cl_end = &(*cl_end)->next;
    cl_end = &(*cl_end)->next;
  *cl_end = more->clusters;
  *cl_end = more->clusters;
  more->clusters = 0;
  more->clusters = 0;
 
 
  /* Merge entries.  */
  /* Merge entries.  */
  if (more->num_entries > 0)
  if (more->num_entries > 0)
    {
    {
      if (hol->num_entries == 0)
      if (hol->num_entries == 0)
        {
        {
          hol->num_entries = more->num_entries;
          hol->num_entries = more->num_entries;
          hol->entries = more->entries;
          hol->entries = more->entries;
          hol->short_options = more->short_options;
          hol->short_options = more->short_options;
          more->num_entries = 0; /* Mark MORE's fields as invalid.  */
          more->num_entries = 0; /* Mark MORE's fields as invalid.  */
        }
        }
      else
      else
        /* Append the entries in MORE to those in HOL, taking care to only add
        /* Append the entries in MORE to those in HOL, taking care to only add
           non-shadowed SHORT_OPTIONS values.  */
           non-shadowed SHORT_OPTIONS values.  */
        {
        {
          unsigned left;
          unsigned left;
          char *so, *more_so;
          char *so, *more_so;
          struct hol_entry *e;
          struct hol_entry *e;
          unsigned num_entries = hol->num_entries + more->num_entries;
          unsigned num_entries = hol->num_entries + more->num_entries;
          struct hol_entry *entries =
          struct hol_entry *entries =
            malloc (num_entries * sizeof (struct hol_entry));
            malloc (num_entries * sizeof (struct hol_entry));
          unsigned hol_so_len = strlen (hol->short_options);
          unsigned hol_so_len = strlen (hol->short_options);
          char *short_options =
          char *short_options =
            malloc (hol_so_len + strlen (more->short_options) + 1);
            malloc (hol_so_len + strlen (more->short_options) + 1);
 
 
          tmp = memcpy (entries, hol->entries,
          tmp = memcpy (entries, hol->entries,
                        hol->num_entries * sizeof (struct hol_entry));
                        hol->num_entries * sizeof (struct hol_entry));
          tmp += hol->num_entries * sizeof (struct hol_entry);
          tmp += hol->num_entries * sizeof (struct hol_entry);
          memcpy (tmp,
          memcpy (tmp,
                  more->entries,
                  more->entries,
                  more->num_entries * sizeof (struct hol_entry));
                  more->num_entries * sizeof (struct hol_entry));
 
 
          memcpy (short_options, hol->short_options, hol_so_len);
          memcpy (short_options, hol->short_options, hol_so_len);
 
 
          /* Fix up the short options pointers from HOL.  */
          /* Fix up the short options pointers from HOL.  */
          for (e = entries, left = hol->num_entries; left > 0; e++, left--)
          for (e = entries, left = hol->num_entries; left > 0; e++, left--)
            e->short_options += (short_options - hol->short_options);
            e->short_options += (short_options - hol->short_options);
 
 
          /* Now add the short options from MORE, fixing up its entries
          /* Now add the short options from MORE, fixing up its entries
             too.  */
             too.  */
          so = short_options + hol_so_len;
          so = short_options + hol_so_len;
          more_so = more->short_options;
          more_so = more->short_options;
          for (left = more->num_entries; left > 0; e++, left--)
          for (left = more->num_entries; left > 0; e++, left--)
            {
            {
              int opts_left;
              int opts_left;
              const struct argp_option *opt;
              const struct argp_option *opt;
 
 
              e->short_options = so;
              e->short_options = so;
 
 
              for (opts_left = e->num, opt = e->opt; opts_left; opt++, opts_left--)
              for (opts_left = e->num, opt = e->opt; opts_left; opt++, opts_left--)
                {
                {
                  int ch = *more_so;
                  int ch = *more_so;
                  if (oshort (opt) && ch == opt->key)
                  if (oshort (opt) && ch == opt->key)
                    /* The next short option in MORE_SO, CH, is from OPT.  */
                    /* The next short option in MORE_SO, CH, is from OPT.  */
                    {
                    {
                      if (! find_char (ch, short_options,
                      if (! find_char (ch, short_options,
                                       short_options + hol_so_len))
                                       short_options + hol_so_len))
                        /* The short option CH isn't shadowed by HOL's options,
                        /* The short option CH isn't shadowed by HOL's options,
                           so add it to the sum.  */
                           so add it to the sum.  */
                        *so++ = ch;
                        *so++ = ch;
                      more_so++;
                      more_so++;
                    }
                    }
                }
                }
            }
            }
 
 
          *so = '\0';
          *so = '\0';
 
 
          free (hol->entries);
          free (hol->entries);
          free (hol->short_options);
          free (hol->short_options);
 
 
          hol->entries = entries;
          hol->entries = entries;
          hol->num_entries = num_entries;
          hol->num_entries = num_entries;
          hol->short_options = short_options;
          hol->short_options = short_options;
        }
        }
    }
    }
 
 
  hol_free (more);
  hol_free (more);
}
}


/* Inserts enough spaces to make sure STREAM is at column COL.  */
/* Inserts enough spaces to make sure STREAM is at column COL.  */
static void
static void
indent_to (argp_fmtstream_t stream, unsigned col)
indent_to (argp_fmtstream_t stream, unsigned col)
{
{
  int needed = col - __argp_fmtstream_point (stream);
  int needed = col - __argp_fmtstream_point (stream);
  while (needed-- > 0)
  while (needed-- > 0)
    __argp_fmtstream_putc (stream, ' ');
    __argp_fmtstream_putc (stream, ' ');
}
}
 
 
/* Output to STREAM either a space, or a newline if there isn't room for at
/* Output to STREAM either a space, or a newline if there isn't room for at
   least ENSURE characters before the right margin.  */
   least ENSURE characters before the right margin.  */
static void
static void
space (argp_fmtstream_t stream, size_t ensure)
space (argp_fmtstream_t stream, size_t ensure)
{
{
  if (__argp_fmtstream_point (stream) + ensure
  if (__argp_fmtstream_point (stream) + ensure
      >= __argp_fmtstream_rmargin (stream))
      >= __argp_fmtstream_rmargin (stream))
    __argp_fmtstream_putc (stream, '\n');
    __argp_fmtstream_putc (stream, '\n');
  else
  else
    __argp_fmtstream_putc (stream, ' ');
    __argp_fmtstream_putc (stream, ' ');
}
}
 
 
/* If the option REAL has an argument, we print it in using the printf
/* If the option REAL has an argument, we print it in using the printf
   format REQ_FMT or OPT_FMT depending on whether it's a required or
   format REQ_FMT or OPT_FMT depending on whether it's a required or
   optional argument.  */
   optional argument.  */
static void
static void
arg (const struct argp_option *real, const char *req_fmt, const char *opt_fmt,
arg (const struct argp_option *real, const char *req_fmt, const char *opt_fmt,
     const char *domain, argp_fmtstream_t stream)
     const char *domain, argp_fmtstream_t stream)
{
{
  if (real->arg)
  if (real->arg)
    {
    {
      if (real->flags & OPTION_ARG_OPTIONAL)
      if (real->flags & OPTION_ARG_OPTIONAL)
        __argp_fmtstream_printf (stream, opt_fmt,
        __argp_fmtstream_printf (stream, opt_fmt,
                                 dgettext (domain, real->arg));
                                 dgettext (domain, real->arg));
      else
      else
        __argp_fmtstream_printf (stream, req_fmt,
        __argp_fmtstream_printf (stream, req_fmt,
                                 dgettext (domain, real->arg));
                                 dgettext (domain, real->arg));
    }
    }
}
}


/* Helper functions for hol_entry_help.  */
/* Helper functions for hol_entry_help.  */
 
 
/* State used during the execution of hol_help.  */
/* State used during the execution of hol_help.  */
struct hol_help_state
struct hol_help_state
{
{
  /* PREV_ENTRY should contain the previous entry printed, or 0.  */
  /* PREV_ENTRY should contain the previous entry printed, or 0.  */
  struct hol_entry *prev_entry;
  struct hol_entry *prev_entry;
 
 
  /* If an entry is in a different group from the previous one, and SEP_GROUPS
  /* If an entry is in a different group from the previous one, and SEP_GROUPS
     is true, then a blank line will be printed before any output. */
     is true, then a blank line will be printed before any output. */
  int sep_groups;
  int sep_groups;
 
 
  /* True if a duplicate option argument was suppressed (only ever set if
  /* True if a duplicate option argument was suppressed (only ever set if
     UPARAMS.dup_args is false).  */
     UPARAMS.dup_args is false).  */
  int suppressed_dup_arg;
  int suppressed_dup_arg;
};
};
 
 
/* Some state used while printing a help entry (used to communicate with
/* Some state used while printing a help entry (used to communicate with
   helper functions).  See the doc for hol_entry_help for more info, as most
   helper functions).  See the doc for hol_entry_help for more info, as most
   of the fields are copied from its arguments.  */
   of the fields are copied from its arguments.  */
struct pentry_state
struct pentry_state
{
{
  const struct hol_entry *entry;
  const struct hol_entry *entry;
  argp_fmtstream_t stream;
  argp_fmtstream_t stream;
  struct hol_help_state *hhstate;
  struct hol_help_state *hhstate;
 
 
  /* True if nothing's been printed so far.  */
  /* True if nothing's been printed so far.  */
  int first;
  int first;
 
 
  /* If non-zero, the state that was used to print this help.  */
  /* If non-zero, the state that was used to print this help.  */
  const struct argp_state *state;
  const struct argp_state *state;
};
};
 
 
/* If a user doc filter should be applied to DOC, do so.  */
/* If a user doc filter should be applied to DOC, do so.  */
static const char *
static const char *
filter_doc (const char *doc, int key, const struct argp *argp,
filter_doc (const char *doc, int key, const struct argp *argp,
            const struct argp_state *state)
            const struct argp_state *state)
{
{
  if (argp->help_filter)
  if (argp->help_filter)
    /* We must apply a user filter to this output.  */
    /* We must apply a user filter to this output.  */
    {
    {
      void *input = __argp_input (argp, state);
      void *input = __argp_input (argp, state);
      return (*argp->help_filter) (key, doc, input);
      return (*argp->help_filter) (key, doc, input);
    }
    }
  else
  else
    /* No filter.  */
    /* No filter.  */
    return doc;
    return doc;
}
}
 
 
/* Prints STR as a header line, with the margin lines set appropiately, and
/* Prints STR as a header line, with the margin lines set appropiately, and
   notes the fact that groups should be separated with a blank line.  ARGP is
   notes the fact that groups should be separated with a blank line.  ARGP is
   the argp that should dictate any user doc filtering to take place.  Note
   the argp that should dictate any user doc filtering to take place.  Note
   that the previous wrap margin isn't restored, but the left margin is reset
   that the previous wrap margin isn't restored, but the left margin is reset
   to 0.  */
   to 0.  */
static void
static void
print_header (const char *str, const struct argp *argp,
print_header (const char *str, const struct argp *argp,
              struct pentry_state *pest)
              struct pentry_state *pest)
{
{
  const char *tstr = dgettext (argp->argp_domain, str);
  const char *tstr = dgettext (argp->argp_domain, str);
  const char *fstr = filter_doc (tstr, ARGP_KEY_HELP_HEADER, argp, pest->state);
  const char *fstr = filter_doc (tstr, ARGP_KEY_HELP_HEADER, argp, pest->state);
 
 
  if (fstr)
  if (fstr)
    {
    {
      if (*fstr)
      if (*fstr)
        {
        {
          if (pest->hhstate->prev_entry)
          if (pest->hhstate->prev_entry)
            /* Precede with a blank line.  */
            /* Precede with a blank line.  */
            __argp_fmtstream_putc (pest->stream, '\n');
            __argp_fmtstream_putc (pest->stream, '\n');
          indent_to (pest->stream, uparams.header_col);
          indent_to (pest->stream, uparams.header_col);
          __argp_fmtstream_set_lmargin (pest->stream, uparams.header_col);
          __argp_fmtstream_set_lmargin (pest->stream, uparams.header_col);
          __argp_fmtstream_set_wmargin (pest->stream, uparams.header_col);
          __argp_fmtstream_set_wmargin (pest->stream, uparams.header_col);
          __argp_fmtstream_puts (pest->stream, fstr);
          __argp_fmtstream_puts (pest->stream, fstr);
          __argp_fmtstream_set_lmargin (pest->stream, 0);
          __argp_fmtstream_set_lmargin (pest->stream, 0);
          __argp_fmtstream_putc (pest->stream, '\n');
          __argp_fmtstream_putc (pest->stream, '\n');
        }
        }
 
 
      pest->hhstate->sep_groups = 1; /* Separate subsequent groups. */
      pest->hhstate->sep_groups = 1; /* Separate subsequent groups. */
    }
    }
 
 
  if (fstr != tstr)
  if (fstr != tstr)
    free ((char *) fstr);
    free ((char *) fstr);
}
}
 
 
/* Inserts a comma if this isn't the first item on the line, and then makes
/* Inserts a comma if this isn't the first item on the line, and then makes
   sure we're at least to column COL.  If this *is* the first item on a line,
   sure we're at least to column COL.  If this *is* the first item on a line,
   prints any pending whitespace/headers that should precede this line. Also
   prints any pending whitespace/headers that should precede this line. Also
   clears FIRST.  */
   clears FIRST.  */
static void
static void
comma (unsigned col, struct pentry_state *pest)
comma (unsigned col, struct pentry_state *pest)
{
{
  if (pest->first)
  if (pest->first)
    {
    {
      const struct hol_entry *pe = pest->hhstate->prev_entry;
      const struct hol_entry *pe = pest->hhstate->prev_entry;
      const struct hol_cluster *cl = pest->entry->cluster;
      const struct hol_cluster *cl = pest->entry->cluster;
 
 
      if (pest->hhstate->sep_groups && pe && pest->entry->group != pe->group)
      if (pest->hhstate->sep_groups && pe && pest->entry->group != pe->group)
        __argp_fmtstream_putc (pest->stream, '\n');
        __argp_fmtstream_putc (pest->stream, '\n');
 
 
      if (cl && cl->header && *cl->header
      if (cl && cl->header && *cl->header
          && (!pe
          && (!pe
              || (pe->cluster != cl
              || (pe->cluster != cl
                  && !hol_cluster_is_child (pe->cluster, cl))))
                  && !hol_cluster_is_child (pe->cluster, cl))))
        /* If we're changing clusters, then this must be the start of the
        /* If we're changing clusters, then this must be the start of the
           ENTRY's cluster unless that is an ancestor of the previous one
           ENTRY's cluster unless that is an ancestor of the previous one
           (in which case we had just popped into a sub-cluster for a bit).
           (in which case we had just popped into a sub-cluster for a bit).
           If so, then print the cluster's header line.  */
           If so, then print the cluster's header line.  */
        {
        {
          int old_wm = __argp_fmtstream_wmargin (pest->stream);
          int old_wm = __argp_fmtstream_wmargin (pest->stream);
          print_header (cl->header, cl->argp, pest);
          print_header (cl->header, cl->argp, pest);
          __argp_fmtstream_set_wmargin (pest->stream, old_wm);
          __argp_fmtstream_set_wmargin (pest->stream, old_wm);
        }
        }
 
 
      pest->first = 0;
      pest->first = 0;
    }
    }
  else
  else
    __argp_fmtstream_puts (pest->stream, ", ");
    __argp_fmtstream_puts (pest->stream, ", ");
 
 
  indent_to (pest->stream, col);
  indent_to (pest->stream, col);
}
}


/* Print help for ENTRY to STREAM.  */
/* Print help for ENTRY to STREAM.  */
static void
static void
hol_entry_help (struct hol_entry *entry, const struct argp_state *state,
hol_entry_help (struct hol_entry *entry, const struct argp_state *state,
                argp_fmtstream_t stream, struct hol_help_state *hhstate)
                argp_fmtstream_t stream, struct hol_help_state *hhstate)
{
{
  unsigned num;
  unsigned num;
  const struct argp_option *real = entry->opt, *opt;
  const struct argp_option *real = entry->opt, *opt;
  char *so = entry->short_options;
  char *so = entry->short_options;
  int have_long_opt = 0; /* We have any long options.  */
  int have_long_opt = 0; /* We have any long options.  */
  /* Saved margins.  */
  /* Saved margins.  */
  int old_lm = __argp_fmtstream_set_lmargin (stream, 0);
  int old_lm = __argp_fmtstream_set_lmargin (stream, 0);
  int old_wm = __argp_fmtstream_wmargin (stream);
  int old_wm = __argp_fmtstream_wmargin (stream);
  /* PEST is a state block holding some of our variables that we'd like to
  /* PEST is a state block holding some of our variables that we'd like to
     share with helper functions.  */
     share with helper functions.  */
  struct pentry_state pest = { entry, stream, hhstate, 1, state };
  struct pentry_state pest = { entry, stream, hhstate, 1, state };
 
 
  if (! odoc (real))
  if (! odoc (real))
    for (opt = real, num = entry->num; num > 0; opt++, num--)
    for (opt = real, num = entry->num; num > 0; opt++, num--)
      if (opt->name && ovisible (opt))
      if (opt->name && ovisible (opt))
        {
        {
          have_long_opt = 1;
          have_long_opt = 1;
          break;
          break;
        }
        }
 
 
  /* First emit short options.  */
  /* First emit short options.  */
  __argp_fmtstream_set_wmargin (stream, uparams.short_opt_col); /* For truly bizarre cases. */
  __argp_fmtstream_set_wmargin (stream, uparams.short_opt_col); /* For truly bizarre cases. */
  for (opt = real, num = entry->num; num > 0; opt++, num--)
  for (opt = real, num = entry->num; num > 0; opt++, num--)
    if (oshort (opt) && opt->key == *so)
    if (oshort (opt) && opt->key == *so)
      /* OPT has a valid (non shadowed) short option.  */
      /* OPT has a valid (non shadowed) short option.  */
      {
      {
        if (ovisible (opt))
        if (ovisible (opt))
          {
          {
            comma (uparams.short_opt_col, &pest);
            comma (uparams.short_opt_col, &pest);
            __argp_fmtstream_putc (stream, '-');
            __argp_fmtstream_putc (stream, '-');
            __argp_fmtstream_putc (stream, *so);
            __argp_fmtstream_putc (stream, *so);
            if (!have_long_opt || uparams.dup_args)
            if (!have_long_opt || uparams.dup_args)
              arg (real, " %s", "[%s]", state->root_argp->argp_domain, stream);
              arg (real, " %s", "[%s]", state->root_argp->argp_domain, stream);
            else if (real->arg)
            else if (real->arg)
              hhstate->suppressed_dup_arg = 1;
              hhstate->suppressed_dup_arg = 1;
          }
          }
        so++;
        so++;
      }
      }
 
 
  /* Now, long options.  */
  /* Now, long options.  */
  if (odoc (real))
  if (odoc (real))
    /* A `documentation' option.  */
    /* A `documentation' option.  */
    {
    {
      __argp_fmtstream_set_wmargin (stream, uparams.doc_opt_col);
      __argp_fmtstream_set_wmargin (stream, uparams.doc_opt_col);
      for (opt = real, num = entry->num; num > 0; opt++, num--)
      for (opt = real, num = entry->num; num > 0; opt++, num--)
        if (opt->name && ovisible (opt))
        if (opt->name && ovisible (opt))
          {
          {
            comma (uparams.doc_opt_col, &pest);
            comma (uparams.doc_opt_col, &pest);
            /* Calling gettext here isn't quite right, since sorting will
            /* Calling gettext here isn't quite right, since sorting will
               have been done on the original; but documentation options
               have been done on the original; but documentation options
               should be pretty rare anyway...  */
               should be pretty rare anyway...  */
            __argp_fmtstream_puts (stream,
            __argp_fmtstream_puts (stream,
                                   dgettext (state->root_argp->argp_domain,
                                   dgettext (state->root_argp->argp_domain,
                                             opt->name));
                                             opt->name));
          }
          }
    }
    }
  else
  else
    /* A real long option.  */
    /* A real long option.  */
    {
    {
      int first_long_opt = 1;
      int first_long_opt = 1;
 
 
      __argp_fmtstream_set_wmargin (stream, uparams.long_opt_col);
      __argp_fmtstream_set_wmargin (stream, uparams.long_opt_col);
      for (opt = real, num = entry->num; num > 0; opt++, num--)
      for (opt = real, num = entry->num; num > 0; opt++, num--)
        if (opt->name && ovisible (opt))
        if (opt->name && ovisible (opt))
          {
          {
            comma (uparams.long_opt_col, &pest);
            comma (uparams.long_opt_col, &pest);
            __argp_fmtstream_printf (stream, "--%s", opt->name);
            __argp_fmtstream_printf (stream, "--%s", opt->name);
            if (first_long_opt || uparams.dup_args)
            if (first_long_opt || uparams.dup_args)
              arg (real, "=%s", "[=%s]", state->root_argp->argp_domain,
              arg (real, "=%s", "[=%s]", state->root_argp->argp_domain,
                   stream);
                   stream);
            else if (real->arg)
            else if (real->arg)
              hhstate->suppressed_dup_arg = 1;
              hhstate->suppressed_dup_arg = 1;
          }
          }
    }
    }
 
 
  /* Next, documentation strings.  */
  /* Next, documentation strings.  */
  __argp_fmtstream_set_lmargin (stream, 0);
  __argp_fmtstream_set_lmargin (stream, 0);
 
 
  if (pest.first)
  if (pest.first)
    {
    {
      /* Didn't print any switches, what's up?  */
      /* Didn't print any switches, what's up?  */
      if (!oshort (real) && !real->name)
      if (!oshort (real) && !real->name)
        /* This is a group header, print it nicely.  */
        /* This is a group header, print it nicely.  */
        print_header (real->doc, entry->argp, &pest);
        print_header (real->doc, entry->argp, &pest);
      else
      else
        /* Just a totally shadowed option or null header; print nothing.  */
        /* Just a totally shadowed option or null header; print nothing.  */
        goto cleanup;           /* Just return, after cleaning up.  */
        goto cleanup;           /* Just return, after cleaning up.  */
    }
    }
  else
  else
    {
    {
      const char *tstr = real->doc ? dgettext (state->root_argp->argp_domain,
      const char *tstr = real->doc ? dgettext (state->root_argp->argp_domain,
                                               real->doc) : 0;
                                               real->doc) : 0;
      const char *fstr = filter_doc (tstr, real->key, entry->argp, state);
      const char *fstr = filter_doc (tstr, real->key, entry->argp, state);
      if (fstr && *fstr)
      if (fstr && *fstr)
        {
        {
          unsigned int col = __argp_fmtstream_point (stream);
          unsigned int col = __argp_fmtstream_point (stream);
 
 
          __argp_fmtstream_set_lmargin (stream, uparams.opt_doc_col);
          __argp_fmtstream_set_lmargin (stream, uparams.opt_doc_col);
          __argp_fmtstream_set_wmargin (stream, uparams.opt_doc_col);
          __argp_fmtstream_set_wmargin (stream, uparams.opt_doc_col);
 
 
          if (col > (unsigned int) (uparams.opt_doc_col + 3))
          if (col > (unsigned int) (uparams.opt_doc_col + 3))
            __argp_fmtstream_putc (stream, '\n');
            __argp_fmtstream_putc (stream, '\n');
          else if (col >= (unsigned int) uparams.opt_doc_col)
          else if (col >= (unsigned int) uparams.opt_doc_col)
            __argp_fmtstream_puts (stream, "   ");
            __argp_fmtstream_puts (stream, "   ");
          else
          else
            indent_to (stream, uparams.opt_doc_col);
            indent_to (stream, uparams.opt_doc_col);
 
 
          __argp_fmtstream_puts (stream, fstr);
          __argp_fmtstream_puts (stream, fstr);
        }
        }
      if (fstr && fstr != tstr)
      if (fstr && fstr != tstr)
        free ((char *) fstr);
        free ((char *) fstr);
 
 
      /* Reset the left margin.  */
      /* Reset the left margin.  */
      __argp_fmtstream_set_lmargin (stream, 0);
      __argp_fmtstream_set_lmargin (stream, 0);
      __argp_fmtstream_putc (stream, '\n');
      __argp_fmtstream_putc (stream, '\n');
    }
    }
 
 
  hhstate->prev_entry = entry;
  hhstate->prev_entry = entry;
 
 
cleanup:
cleanup:
  __argp_fmtstream_set_lmargin (stream, old_lm);
  __argp_fmtstream_set_lmargin (stream, old_lm);
  __argp_fmtstream_set_wmargin (stream, old_wm);
  __argp_fmtstream_set_wmargin (stream, old_wm);
}
}


/* Output a long help message about the options in HOL to STREAM.  */
/* Output a long help message about the options in HOL to STREAM.  */
static void
static void
hol_help (struct hol *hol, const struct argp_state *state,
hol_help (struct hol *hol, const struct argp_state *state,
          argp_fmtstream_t stream)
          argp_fmtstream_t stream)
{
{
  unsigned num;
  unsigned num;
  struct hol_entry *entry;
  struct hol_entry *entry;
  struct hol_help_state hhstate = { 0, 0, 0 };
  struct hol_help_state hhstate = { 0, 0, 0 };
 
 
  for (entry = hol->entries, num = hol->num_entries; num > 0; entry++, num--)
  for (entry = hol->entries, num = hol->num_entries; num > 0; entry++, num--)
    hol_entry_help (entry, state, stream, &hhstate);
    hol_entry_help (entry, state, stream, &hhstate);
 
 
  if (hhstate.suppressed_dup_arg && uparams.dup_args_note)
  if (hhstate.suppressed_dup_arg && uparams.dup_args_note)
    {
    {
      const char *tstr = dgettext (state->root_argp->argp_domain, "\
      const char *tstr = dgettext (state->root_argp->argp_domain, "\
Mandatory or optional arguments to long options are also mandatory or \
Mandatory or optional arguments to long options are also mandatory or \
optional for any corresponding short options.");
optional for any corresponding short options.");
      const char *fstr = filter_doc (tstr, ARGP_KEY_HELP_DUP_ARGS_NOTE,
      const char *fstr = filter_doc (tstr, ARGP_KEY_HELP_DUP_ARGS_NOTE,
                                     state ? state->root_argp : 0, state);
                                     state ? state->root_argp : 0, state);
      if (fstr && *fstr)
      if (fstr && *fstr)
        {
        {
          __argp_fmtstream_putc (stream, '\n');
          __argp_fmtstream_putc (stream, '\n');
          __argp_fmtstream_puts (stream, fstr);
          __argp_fmtstream_puts (stream, fstr);
          __argp_fmtstream_putc (stream, '\n');
          __argp_fmtstream_putc (stream, '\n');
        }
        }
      if (fstr && fstr != tstr)
      if (fstr && fstr != tstr)
        free ((char *) fstr);
        free ((char *) fstr);
    }
    }
}
}


/* Helper functions for hol_usage.  */
/* Helper functions for hol_usage.  */
 
 
/* If OPT is a short option without an arg, append its key to the string
/* If OPT is a short option without an arg, append its key to the string
   pointer pointer to by COOKIE, and advance the pointer.  */
   pointer pointer to by COOKIE, and advance the pointer.  */
static int
static int
add_argless_short_opt (const struct argp_option *opt,
add_argless_short_opt (const struct argp_option *opt,
                       const struct argp_option *real,
                       const struct argp_option *real,
                       const char *domain, void *cookie)
                       const char *domain, void *cookie)
{
{
  char **snao_end = cookie;
  char **snao_end = cookie;
  if (!(opt->arg || real->arg)
  if (!(opt->arg || real->arg)
      && !((opt->flags | real->flags) & OPTION_NO_USAGE))
      && !((opt->flags | real->flags) & OPTION_NO_USAGE))
    *(*snao_end)++ = opt->key;
    *(*snao_end)++ = opt->key;
  return 0;
  return 0;
}
}
 
 
/* If OPT is a short option with an arg, output a usage entry for it to the
/* If OPT is a short option with an arg, output a usage entry for it to the
   stream pointed at by COOKIE.  */
   stream pointed at by COOKIE.  */
static int
static int
usage_argful_short_opt (const struct argp_option *opt,
usage_argful_short_opt (const struct argp_option *opt,
                        const struct argp_option *real,
                        const struct argp_option *real,
                        const char *domain, void *cookie)
                        const char *domain, void *cookie)
{
{
  argp_fmtstream_t stream = cookie;
  argp_fmtstream_t stream = cookie;
  const char *arg = opt->arg;
  const char *arg = opt->arg;
  int flags = opt->flags | real->flags;
  int flags = opt->flags | real->flags;
 
 
  if (! arg)
  if (! arg)
    arg = real->arg;
    arg = real->arg;
 
 
  if (arg && !(flags & OPTION_NO_USAGE))
  if (arg && !(flags & OPTION_NO_USAGE))
    {
    {
      arg = dgettext (domain, arg);
      arg = dgettext (domain, arg);
 
 
      if (flags & OPTION_ARG_OPTIONAL)
      if (flags & OPTION_ARG_OPTIONAL)
        __argp_fmtstream_printf (stream, " [-%c[%s]]", opt->key, arg);
        __argp_fmtstream_printf (stream, " [-%c[%s]]", opt->key, arg);
      else
      else
        {
        {
          /* Manually do line wrapping so that it (probably) won't
          /* Manually do line wrapping so that it (probably) won't
             get wrapped at the embedded space.  */
             get wrapped at the embedded space.  */
          space (stream, 6 + strlen (arg));
          space (stream, 6 + strlen (arg));
          __argp_fmtstream_printf (stream, "[-%c %s]", opt->key, arg);
          __argp_fmtstream_printf (stream, "[-%c %s]", opt->key, arg);
        }
        }
    }
    }
 
 
  return 0;
  return 0;
}
}
 
 
/* Output a usage entry for the long option opt to the stream pointed at by
/* Output a usage entry for the long option opt to the stream pointed at by
   COOKIE.  */
   COOKIE.  */
static int
static int
usage_long_opt (const struct argp_option *opt,
usage_long_opt (const struct argp_option *opt,
                const struct argp_option *real,
                const struct argp_option *real,
                const char *domain, void *cookie)
                const char *domain, void *cookie)
{
{
  argp_fmtstream_t stream = cookie;
  argp_fmtstream_t stream = cookie;
  const char *arg = opt->arg;
  const char *arg = opt->arg;
  int flags = opt->flags | real->flags;
  int flags = opt->flags | real->flags;
 
 
  if (! arg)
  if (! arg)
    arg = real->arg;
    arg = real->arg;
 
 
  if (! (flags & OPTION_NO_USAGE))
  if (! (flags & OPTION_NO_USAGE))
    {
    {
      if (arg)
      if (arg)
        {
        {
          arg = dgettext (domain, arg);
          arg = dgettext (domain, arg);
          if (flags & OPTION_ARG_OPTIONAL)
          if (flags & OPTION_ARG_OPTIONAL)
            __argp_fmtstream_printf (stream, " [--%s[=%s]]", opt->name, arg);
            __argp_fmtstream_printf (stream, " [--%s[=%s]]", opt->name, arg);
          else
          else
            __argp_fmtstream_printf (stream, " [--%s=%s]", opt->name, arg);
            __argp_fmtstream_printf (stream, " [--%s=%s]", opt->name, arg);
        }
        }
      else
      else
        __argp_fmtstream_printf (stream, " [--%s]", opt->name);
        __argp_fmtstream_printf (stream, " [--%s]", opt->name);
    }
    }
 
 
  return 0;
  return 0;
}
}


/* Print a short usage description for the arguments in HOL to STREAM.  */
/* Print a short usage description for the arguments in HOL to STREAM.  */
static void
static void
hol_usage (struct hol *hol, argp_fmtstream_t stream)
hol_usage (struct hol *hol, argp_fmtstream_t stream)
{
{
  if (hol->num_entries > 0)
  if (hol->num_entries > 0)
    {
    {
      unsigned nentries;
      unsigned nentries;
      struct hol_entry *entry;
      struct hol_entry *entry;
      char *short_no_arg_opts = alloca (strlen (hol->short_options) + 1);
      char *short_no_arg_opts = alloca (strlen (hol->short_options) + 1);
      char *snao_end = short_no_arg_opts;
      char *snao_end = short_no_arg_opts;
 
 
      /* First we put a list of short options without arguments.  */
      /* First we put a list of short options without arguments.  */
      for (entry = hol->entries, nentries = hol->num_entries
      for (entry = hol->entries, nentries = hol->num_entries
           ; nentries > 0
           ; nentries > 0
           ; entry++, nentries--)
           ; entry++, nentries--)
        hol_entry_short_iterate (entry, add_argless_short_opt,
        hol_entry_short_iterate (entry, add_argless_short_opt,
                                 entry->argp->argp_domain, &snao_end);
                                 entry->argp->argp_domain, &snao_end);
      if (snao_end > short_no_arg_opts)
      if (snao_end > short_no_arg_opts)
        {
        {
          *snao_end++ = 0;
          *snao_end++ = 0;
          __argp_fmtstream_printf (stream, " [-%s]", short_no_arg_opts);
          __argp_fmtstream_printf (stream, " [-%s]", short_no_arg_opts);
        }
        }
 
 
      /* Now a list of short options *with* arguments.  */
      /* Now a list of short options *with* arguments.  */
      for (entry = hol->entries, nentries = hol->num_entries
      for (entry = hol->entries, nentries = hol->num_entries
           ; nentries > 0
           ; nentries > 0
           ; entry++, nentries--)
           ; entry++, nentries--)
        hol_entry_short_iterate (entry, usage_argful_short_opt,
        hol_entry_short_iterate (entry, usage_argful_short_opt,
                                 entry->argp->argp_domain, stream);
                                 entry->argp->argp_domain, stream);
 
 
      /* Finally, a list of long options (whew!).  */
      /* Finally, a list of long options (whew!).  */
      for (entry = hol->entries, nentries = hol->num_entries
      for (entry = hol->entries, nentries = hol->num_entries
           ; nentries > 0
           ; nentries > 0
           ; entry++, nentries--)
           ; entry++, nentries--)
        hol_entry_long_iterate (entry, usage_long_opt,
        hol_entry_long_iterate (entry, usage_long_opt,
                                entry->argp->argp_domain, stream);
                                entry->argp->argp_domain, stream);
    }
    }
}
}


/* Make a HOL containing all levels of options in ARGP.  CLUSTER is the
/* Make a HOL containing all levels of options in ARGP.  CLUSTER is the
   cluster in which ARGP's entries should be clustered, or 0.  */
   cluster in which ARGP's entries should be clustered, or 0.  */
static struct hol *
static struct hol *
argp_hol (const struct argp *argp, struct hol_cluster *cluster)
argp_hol (const struct argp *argp, struct hol_cluster *cluster)
{
{
  const struct argp_child *child = argp->children;
  const struct argp_child *child = argp->children;
  struct hol *hol = make_hol (argp, cluster);
  struct hol *hol = make_hol (argp, cluster);
  if (child)
  if (child)
    while (child->argp)
    while (child->argp)
      {
      {
        struct hol_cluster *child_cluster =
        struct hol_cluster *child_cluster =
          ((child->group || child->header)
          ((child->group || child->header)
           /* Put CHILD->argp within its own cluster.  */
           /* Put CHILD->argp within its own cluster.  */
           ? hol_add_cluster (hol, child->group, child->header,
           ? hol_add_cluster (hol, child->group, child->header,
                              child - argp->children, cluster, argp)
                              child - argp->children, cluster, argp)
           /* Just merge it into the parent's cluster.  */
           /* Just merge it into the parent's cluster.  */
           : cluster);
           : cluster);
        hol_append (hol, argp_hol (child->argp, child_cluster)) ;
        hol_append (hol, argp_hol (child->argp, child_cluster)) ;
        child++;
        child++;
      }
      }
  return hol;
  return hol;
}
}


/* Calculate how many different levels with alternative args strings exist in
/* Calculate how many different levels with alternative args strings exist in
   ARGP.  */
   ARGP.  */
static size_t
static size_t
argp_args_levels (const struct argp *argp)
argp_args_levels (const struct argp *argp)
{
{
  size_t levels = 0;
  size_t levels = 0;
  const struct argp_child *child = argp->children;
  const struct argp_child *child = argp->children;
 
 
  if (argp->args_doc && strchr (argp->args_doc, '\n'))
  if (argp->args_doc && strchr (argp->args_doc, '\n'))
    levels++;
    levels++;
 
 
  if (child)
  if (child)
    while (child->argp)
    while (child->argp)
      levels += argp_args_levels ((child++)->argp);
      levels += argp_args_levels ((child++)->argp);
 
 
  return levels;
  return levels;
}
}
 
 
/* Print all the non-option args documented in ARGP to STREAM.  Any output is
/* Print all the non-option args documented in ARGP to STREAM.  Any output is
   preceded by a space.  LEVELS is a pointer to a byte vector the length
   preceded by a space.  LEVELS is a pointer to a byte vector the length
   returned by argp_args_levels; it should be initialized to zero, and
   returned by argp_args_levels; it should be initialized to zero, and
   updated by this routine for the next call if ADVANCE is true.  True is
   updated by this routine for the next call if ADVANCE is true.  True is
   returned as long as there are more patterns to output.  */
   returned as long as there are more patterns to output.  */
static int
static int
argp_args_usage (const struct argp *argp, const struct argp_state *state,
argp_args_usage (const struct argp *argp, const struct argp_state *state,
                 char **levels, int advance, argp_fmtstream_t stream)
                 char **levels, int advance, argp_fmtstream_t stream)
{
{
  char *our_level = *levels;
  char *our_level = *levels;
  int multiple = 0;
  int multiple = 0;
  const struct argp_child *child = argp->children;
  const struct argp_child *child = argp->children;
  const char *tdoc = dgettext (argp->argp_domain, argp->args_doc), *nl = 0;
  const char *tdoc = dgettext (argp->argp_domain, argp->args_doc), *nl = 0;
  const char *fdoc = filter_doc (tdoc, ARGP_KEY_HELP_ARGS_DOC, argp, state);
  const char *fdoc = filter_doc (tdoc, ARGP_KEY_HELP_ARGS_DOC, argp, state);
 
 
  if (fdoc)
  if (fdoc)
    {
    {
      const char *cp = fdoc;
      const char *cp = fdoc;
      nl = strchr (cp, '\n');
      nl = strchr (cp, '\n');
      if (!nl)
      if (!nl)
        {
        {
          nl = cp;
          nl = cp;
          while (*nl != '\0') nl++;
          while (*nl != '\0') nl++;
        }
        }
 
 
      if (*nl != '\0')
      if (*nl != '\0')
        /* This is a `multi-level' args doc; advance to the correct position
        /* This is a `multi-level' args doc; advance to the correct position
           as determined by our state in LEVELS, and update LEVELS.  */
           as determined by our state in LEVELS, and update LEVELS.  */
        {
        {
          int i;
          int i;
          multiple = 1;
          multiple = 1;
          for (i = 0; i < *our_level; i++)
          for (i = 0; i < *our_level; i++)
            {
            {
              cp = nl + 1;
              cp = nl + 1;
              nl = strchr (cp, '\n');
              nl = strchr (cp, '\n');
              if (!nl)
              if (!nl)
                {
                {
                  nl = cp;
                  nl = cp;
                  while (*nl != '\0') nl++;
                  while (*nl != '\0') nl++;
                }
                }
            }
            }
          (*levels)++;
          (*levels)++;
        }
        }
 
 
      /* Manually do line wrapping so that it (probably) won't get wrapped at
      /* Manually do line wrapping so that it (probably) won't get wrapped at
         any embedded spaces.  */
         any embedded spaces.  */
      space (stream, 1 + nl - cp);
      space (stream, 1 + nl - cp);
 
 
      __argp_fmtstream_write (stream, cp, nl - cp);
      __argp_fmtstream_write (stream, cp, nl - cp);
    }
    }
  if (fdoc && fdoc != tdoc)
  if (fdoc && fdoc != tdoc)
    free ((char *)fdoc);        /* Free user's modified doc string.  */
    free ((char *)fdoc);        /* Free user's modified doc string.  */
 
 
  if (child)
  if (child)
    while (child->argp)
    while (child->argp)
      advance = !argp_args_usage ((child++)->argp, state, levels, advance, stream);
      advance = !argp_args_usage ((child++)->argp, state, levels, advance, stream);
 
 
  if (advance && multiple)
  if (advance && multiple)
    {
    {
      /* Need to increment our level.  */
      /* Need to increment our level.  */
      if (*nl)
      if (*nl)
        /* There's more we can do here.  */
        /* There's more we can do here.  */
        {
        {
          (*our_level)++;
          (*our_level)++;
          advance = 0;           /* Our parent shouldn't advance also. */
          advance = 0;           /* Our parent shouldn't advance also. */
        }
        }
      else if (*our_level > 0)
      else if (*our_level > 0)
        /* We had multiple levels, but used them up; reset to zero.  */
        /* We had multiple levels, but used them up; reset to zero.  */
        *our_level = 0;
        *our_level = 0;
    }
    }
 
 
  return !advance;
  return !advance;
}
}


/* Print the documentation for ARGP to STREAM; if POST is false, then
/* Print the documentation for ARGP to STREAM; if POST is false, then
   everything preceeding a `\v' character in the documentation strings (or
   everything preceeding a `\v' character in the documentation strings (or
   the whole string, for those with none) is printed, otherwise, everything
   the whole string, for those with none) is printed, otherwise, everything
   following the `\v' character (nothing for strings without).  Each separate
   following the `\v' character (nothing for strings without).  Each separate
   bit of documentation is separated a blank line, and if PRE_BLANK is true,
   bit of documentation is separated a blank line, and if PRE_BLANK is true,
   then the first is as well.  If FIRST_ONLY is true, only the first
   then the first is as well.  If FIRST_ONLY is true, only the first
   occurrence is output.  Returns true if anything was output.  */
   occurrence is output.  Returns true if anything was output.  */
static int
static int
argp_doc (const struct argp *argp, const struct argp_state *state,
argp_doc (const struct argp *argp, const struct argp_state *state,
          int post, int pre_blank, int first_only,
          int post, int pre_blank, int first_only,
          argp_fmtstream_t stream)
          argp_fmtstream_t stream)
{
{
  const char *text;
  const char *text;
  const char *inp_text;
  const char *inp_text;
  char *tmp_text;
  char *tmp_text;
  void *input = 0;
  void *input = 0;
  int anything = 0;
  int anything = 0;
  size_t inp_text_limit = 0;
  size_t inp_text_limit = 0;
  const char *doc = dgettext (argp->argp_domain, argp->doc);
  const char *doc = dgettext (argp->argp_domain, argp->doc);
  const struct argp_child *child = argp->children;
  const struct argp_child *child = argp->children;
 
 
  if (doc)
  if (doc)
    {
    {
      char *vt = strchr (doc, '\v');
      char *vt = strchr (doc, '\v');
      inp_text = post ? (vt ? vt + 1 : 0) : doc;
      inp_text = post ? (vt ? vt + 1 : 0) : doc;
      inp_text_limit = (!post && vt) ? (vt - doc) : 0;
      inp_text_limit = (!post && vt) ? (vt - doc) : 0;
    }
    }
  else
  else
    inp_text = 0;
    inp_text = 0;
 
 
  if (argp->help_filter)
  if (argp->help_filter)
    /* We have to filter the doc strings.  */
    /* We have to filter the doc strings.  */
    {
    {
      if (inp_text_limit)
      if (inp_text_limit)
        {
        {
          /* Copy INP_TEXT so that it's nul-terminated. */
          /* Copy INP_TEXT so that it's nul-terminated. */
          tmp_text = _malloc_r (_REENT, inp_text_limit);
          tmp_text = _malloc_r (_REENT, inp_text_limit);
          strncpy (tmp_text, inp_text, inp_text_limit);
          strncpy (tmp_text, inp_text, inp_text_limit);
          _free_r (_REENT, inp_text);
          _free_r (_REENT, inp_text);
          inp_text = tmp_text;
          inp_text = tmp_text;
        }
        }
      input = __argp_input (argp, state);
      input = __argp_input (argp, state);
      text =
      text =
        (*argp->help_filter) (post
        (*argp->help_filter) (post
                              ? ARGP_KEY_HELP_POST_DOC
                              ? ARGP_KEY_HELP_POST_DOC
                              : ARGP_KEY_HELP_PRE_DOC,
                              : ARGP_KEY_HELP_PRE_DOC,
                              inp_text, input);
                              inp_text, input);
    }
    }
  else
  else
    text = (const char *) inp_text;
    text = (const char *) inp_text;
 
 
  if (text)
  if (text)
    {
    {
      if (pre_blank)
      if (pre_blank)
        __argp_fmtstream_putc (stream, '\n');
        __argp_fmtstream_putc (stream, '\n');
 
 
      if (text == inp_text && inp_text_limit)
      if (text == inp_text && inp_text_limit)
        __argp_fmtstream_write (stream, inp_text, inp_text_limit);
        __argp_fmtstream_write (stream, inp_text, inp_text_limit);
      else
      else
        __argp_fmtstream_puts (stream, text);
        __argp_fmtstream_puts (stream, text);
 
 
      if (__argp_fmtstream_point (stream) > __argp_fmtstream_lmargin (stream))
      if (__argp_fmtstream_point (stream) > __argp_fmtstream_lmargin (stream))
        __argp_fmtstream_putc (stream, '\n');
        __argp_fmtstream_putc (stream, '\n');
 
 
      anything = 1;
      anything = 1;
    }
    }
 
 
  if (text && text != inp_text)
  if (text && text != inp_text)
    free ((char *) text);       /* Free TEXT returned from the help filter.  */
    free ((char *) text);       /* Free TEXT returned from the help filter.  */
  if (inp_text && inp_text_limit && argp->help_filter)
  if (inp_text && inp_text_limit && argp->help_filter)
    free ((char *) inp_text);   /* We copied INP_TEXT, so free it now.  */
    free ((char *) inp_text);   /* We copied INP_TEXT, so free it now.  */
 
 
  if (post && argp->help_filter)
  if (post && argp->help_filter)
    /* Now see if we have to output a ARGP_KEY_HELP_EXTRA text.  */
    /* Now see if we have to output a ARGP_KEY_HELP_EXTRA text.  */
    {
    {
      text = (*argp->help_filter) (ARGP_KEY_HELP_EXTRA, 0, input);
      text = (*argp->help_filter) (ARGP_KEY_HELP_EXTRA, 0, input);
      if (text)
      if (text)
        {
        {
          if (anything || pre_blank)
          if (anything || pre_blank)
            __argp_fmtstream_putc (stream, '\n');
            __argp_fmtstream_putc (stream, '\n');
          __argp_fmtstream_puts (stream, text);
          __argp_fmtstream_puts (stream, text);
          free ((char *) text);
          free ((char *) text);
          if (__argp_fmtstream_point (stream)
          if (__argp_fmtstream_point (stream)
              > __argp_fmtstream_lmargin (stream))
              > __argp_fmtstream_lmargin (stream))
            __argp_fmtstream_putc (stream, '\n');
            __argp_fmtstream_putc (stream, '\n');
          anything = 1;
          anything = 1;
        }
        }
    }
    }
 
 
  if (child)
  if (child)
    while (child->argp && !(first_only && anything))
    while (child->argp && !(first_only && anything))
      anything |=
      anything |=
        argp_doc ((child++)->argp, state,
        argp_doc ((child++)->argp, state,
                  post, anything || pre_blank, first_only,
                  post, anything || pre_blank, first_only,
                  stream);
                  stream);
 
 
  return anything;
  return anything;
}
}


/* Output a usage message for ARGP to STREAM.  If called from
/* Output a usage message for ARGP to STREAM.  If called from
   argp_state_help, STATE is the relevent parsing state.  FLAGS are from the
   argp_state_help, STATE is the relevent parsing state.  FLAGS are from the
   set ARGP_HELP_*.  NAME is what to use wherever a `program name' is
   set ARGP_HELP_*.  NAME is what to use wherever a `program name' is
   needed. */
   needed. */
static void
static void
_help (const struct argp *argp, const struct argp_state *state, FILE *stream,
_help (const struct argp *argp, const struct argp_state *state, FILE *stream,
       unsigned flags, char *name)
       unsigned flags, char *name)
{
{
  int anything = 0;              /* Whether we've output anything.  */
  int anything = 0;              /* Whether we've output anything.  */
  struct hol *hol = 0;
  struct hol *hol = 0;
  argp_fmtstream_t fs;
  argp_fmtstream_t fs;
 
 
  if (! stream)
  if (! stream)
    return;
    return;
 
 
  _flockfile (stream);
  _flockfile (stream);
 
 
  if (! uparams.valid)
  if (! uparams.valid)
    fill_in_uparams (state);
    fill_in_uparams (state);
 
 
  fs = __argp_make_fmtstream (stream, 0, uparams.rmargin, 0);
  fs = __argp_make_fmtstream (stream, 0, uparams.rmargin, 0);
  if (! fs)
  if (! fs)
    {
    {
      _funlockfile (stream);
      _funlockfile (stream);
      return;
      return;
    }
    }
 
 
  if (flags & (ARGP_HELP_USAGE | ARGP_HELP_SHORT_USAGE | ARGP_HELP_LONG))
  if (flags & (ARGP_HELP_USAGE | ARGP_HELP_SHORT_USAGE | ARGP_HELP_LONG))
    {
    {
      hol = argp_hol (argp, 0);
      hol = argp_hol (argp, 0);
 
 
      /* If present, these options always come last.  */
      /* If present, these options always come last.  */
      hol_set_group (hol, "help", -1);
      hol_set_group (hol, "help", -1);
      hol_set_group (hol, "version", -1);
      hol_set_group (hol, "version", -1);
 
 
      hol_sort (hol);
      hol_sort (hol);
    }
    }
 
 
  if (flags & (ARGP_HELP_USAGE | ARGP_HELP_SHORT_USAGE))
  if (flags & (ARGP_HELP_USAGE | ARGP_HELP_SHORT_USAGE))
    /* Print a short `Usage:' message.  */
    /* Print a short `Usage:' message.  */
    {
    {
      int first_pattern = 1, more_patterns;
      int first_pattern = 1, more_patterns;
      size_t num_pattern_levels = argp_args_levels (argp);
      size_t num_pattern_levels = argp_args_levels (argp);
      char *pattern_levels = alloca (num_pattern_levels);
      char *pattern_levels = alloca (num_pattern_levels);
 
 
      memset (pattern_levels, 0, num_pattern_levels);
      memset (pattern_levels, 0, num_pattern_levels);
 
 
      do
      do
        {
        {
          int old_lm;
          int old_lm;
          int old_wm = __argp_fmtstream_set_wmargin (fs, uparams.usage_indent);
          int old_wm = __argp_fmtstream_set_wmargin (fs, uparams.usage_indent);
          char *levels = pattern_levels;
          char *levels = pattern_levels;
 
 
          if (first_pattern)
          if (first_pattern)
            __argp_fmtstream_printf (fs, "%s %s",
            __argp_fmtstream_printf (fs, "%s %s",
                                     dgettext (argp->argp_domain, "Usage:"),
                                     dgettext (argp->argp_domain, "Usage:"),
                                     name);
                                     name);
          else
          else
            __argp_fmtstream_printf (fs, "%s %s",
            __argp_fmtstream_printf (fs, "%s %s",
                                     dgettext (argp->argp_domain, "  or: "),
                                     dgettext (argp->argp_domain, "  or: "),
                                     name);
                                     name);
 
 
          /* We set the lmargin as well as the wmargin, because hol_usage
          /* We set the lmargin as well as the wmargin, because hol_usage
             manually wraps options with newline to avoid annoying breaks.  */
             manually wraps options with newline to avoid annoying breaks.  */
          old_lm = __argp_fmtstream_set_lmargin (fs, uparams.usage_indent);
          old_lm = __argp_fmtstream_set_lmargin (fs, uparams.usage_indent);
 
 
          if (flags & ARGP_HELP_SHORT_USAGE)
          if (flags & ARGP_HELP_SHORT_USAGE)
            /* Just show where the options go.  */
            /* Just show where the options go.  */
            {
            {
              if (hol->num_entries > 0)
              if (hol->num_entries > 0)
                __argp_fmtstream_puts (fs, dgettext (argp->argp_domain,
                __argp_fmtstream_puts (fs, dgettext (argp->argp_domain,
                                                     " [OPTION...]"));
                                                     " [OPTION...]"));
            }
            }
          else
          else
            /* Actually print the options.  */
            /* Actually print the options.  */
            {
            {
              hol_usage (hol, fs);
              hol_usage (hol, fs);
              flags |= ARGP_HELP_SHORT_USAGE; /* But only do so once.  */
              flags |= ARGP_HELP_SHORT_USAGE; /* But only do so once.  */
            }
            }
 
 
          more_patterns = argp_args_usage (argp, state, &levels, 1, fs);
          more_patterns = argp_args_usage (argp, state, &levels, 1, fs);
 
 
          __argp_fmtstream_set_wmargin (fs, old_wm);
          __argp_fmtstream_set_wmargin (fs, old_wm);
          __argp_fmtstream_set_lmargin (fs, old_lm);
          __argp_fmtstream_set_lmargin (fs, old_lm);
 
 
          __argp_fmtstream_putc (fs, '\n');
          __argp_fmtstream_putc (fs, '\n');
          anything = 1;
          anything = 1;
 
 
          first_pattern = 0;
          first_pattern = 0;
        }
        }
      while (more_patterns);
      while (more_patterns);
    }
    }
 
 
  if (flags & ARGP_HELP_PRE_DOC)
  if (flags & ARGP_HELP_PRE_DOC)
    anything |= argp_doc (argp, state, 0, 0, 1, fs);
    anything |= argp_doc (argp, state, 0, 0, 1, fs);
 
 
  if (flags & ARGP_HELP_SEE)
  if (flags & ARGP_HELP_SEE)
    {
    {
      __argp_fmtstream_printf (fs, dgettext (argp->argp_domain, "\
      __argp_fmtstream_printf (fs, dgettext (argp->argp_domain, "\
Try `%s --help' or `%s --usage' for more information.\n"),
Try `%s --help' or `%s --usage' for more information.\n"),
                               name, name);
                               name, name);
      anything = 1;
      anything = 1;
    }
    }
 
 
  if (flags & ARGP_HELP_LONG)
  if (flags & ARGP_HELP_LONG)
    /* Print a long, detailed help message.  */
    /* Print a long, detailed help message.  */
    {
    {
      /* Print info about all the options.  */
      /* Print info about all the options.  */
      if (hol->num_entries > 0)
      if (hol->num_entries > 0)
        {
        {
          if (anything)
          if (anything)
            __argp_fmtstream_putc (fs, '\n');
            __argp_fmtstream_putc (fs, '\n');
          hol_help (hol, state, fs);
          hol_help (hol, state, fs);
          anything = 1;
          anything = 1;
        }
        }
    }
    }
 
 
  if (flags & ARGP_HELP_POST_DOC)
  if (flags & ARGP_HELP_POST_DOC)
    /* Print any documentation strings at the end.  */
    /* Print any documentation strings at the end.  */
    anything |= argp_doc (argp, state, 1, anything, 0, fs);
    anything |= argp_doc (argp, state, 1, anything, 0, fs);
 
 
  if ((flags & ARGP_HELP_BUG_ADDR) && argp_program_bug_address)
  if ((flags & ARGP_HELP_BUG_ADDR) && argp_program_bug_address)
    {
    {
      if (anything)
      if (anything)
        __argp_fmtstream_putc (fs, '\n');
        __argp_fmtstream_putc (fs, '\n');
      __argp_fmtstream_printf (fs, dgettext (argp->argp_domain,
      __argp_fmtstream_printf (fs, dgettext (argp->argp_domain,
                                             "Report bugs to %s.\n"),
                                             "Report bugs to %s.\n"),
                               argp_program_bug_address);
                               argp_program_bug_address);
      anything = 1;
      anything = 1;
    }
    }
 
 
  _funlockfile (stream);
  _funlockfile (stream);
 
 
  if (hol)
  if (hol)
    hol_free (hol);
    hol_free (hol);
 
 
  __argp_fmtstream_free (fs);
  __argp_fmtstream_free (fs);
}
}


/* Output a usage message for ARGP to STREAM.  FLAGS are from the set
/* Output a usage message for ARGP to STREAM.  FLAGS are from the set
   ARGP_HELP_*.  NAME is what to use wherever a `program name' is needed. */
   ARGP_HELP_*.  NAME is what to use wherever a `program name' is needed. */
void __argp_help (const struct argp *argp, FILE *stream,
void __argp_help (const struct argp *argp, FILE *stream,
                  unsigned flags, char *name)
                  unsigned flags, char *name)
{
{
  _help (argp, 0, stream, flags, name);
  _help (argp, 0, stream, flags, name);
}
}
#ifdef weak_alias
#ifdef weak_alias
weak_alias (__argp_help, argp_help)
weak_alias (__argp_help, argp_help)
#endif
#endif
 
 
/* Output, if appropriate, a usage message for STATE to STREAM.  FLAGS are
/* Output, if appropriate, a usage message for STATE to STREAM.  FLAGS are
   from the set ARGP_HELP_*.  */
   from the set ARGP_HELP_*.  */
void
void
__argp_state_help (const struct argp_state *state, FILE *stream, unsigned flags)
__argp_state_help (const struct argp_state *state, FILE *stream, unsigned flags)
{
{
  if ((!state || ! (state->flags & ARGP_NO_ERRS)) && stream)
  if ((!state || ! (state->flags & ARGP_NO_ERRS)) && stream)
    {
    {
      if (state && (state->flags & ARGP_LONG_ONLY))
      if (state && (state->flags & ARGP_LONG_ONLY))
        flags |= ARGP_HELP_LONG_ONLY;
        flags |= ARGP_HELP_LONG_ONLY;
 
 
      _help (state ? state->root_argp : 0, state, stream, flags,
      _help (state ? state->root_argp : 0, state, stream, flags,
             state ? state->name : program_invocation_short_name);
             state ? state->name : program_invocation_short_name);
 
 
      if (!state || ! (state->flags & ARGP_NO_EXIT))
      if (!state || ! (state->flags & ARGP_NO_EXIT))
        {
        {
          if (flags & ARGP_HELP_EXIT_ERR)
          if (flags & ARGP_HELP_EXIT_ERR)
            exit (argp_err_exit_status);
            exit (argp_err_exit_status);
          if (flags & ARGP_HELP_EXIT_OK)
          if (flags & ARGP_HELP_EXIT_OK)
            exit (0);
            exit (0);
        }
        }
  }
  }
}
}
#ifdef weak_alias
#ifdef weak_alias
weak_alias (__argp_state_help, argp_state_help)
weak_alias (__argp_state_help, argp_state_help)
#endif
#endif


/* If appropriate, print the printf string FMT and following args, preceded
/* If appropriate, print the printf string FMT and following args, preceded
   by the program name and `:', to stderr, and followed by a `Try ... --help'
   by the program name and `:', to stderr, and followed by a `Try ... --help'
   message, then exit (1).  */
   message, then exit (1).  */
void
void
__argp_error (const struct argp_state *state, const char *fmt, ...)
__argp_error (const struct argp_state *state, const char *fmt, ...)
{
{
  if (!state || !(state->flags & ARGP_NO_ERRS))
  if (!state || !(state->flags & ARGP_NO_ERRS))
    {
    {
      FILE *stream = state ? state->err_stream : stderr;
      FILE *stream = state ? state->err_stream : stderr;
 
 
      if (stream)
      if (stream)
        {
        {
          va_list ap;
          va_list ap;
 
 
          _flockfile (stream);
          _flockfile (stream);
 
 
          va_start (ap, fmt);
          va_start (ap, fmt);
 
 
#ifdef USE_IN_LIBIO
#ifdef USE_IN_LIBIO
          if (_IO_fwide (stream, 0) > 0)
          if (_IO_fwide (stream, 0) > 0)
            {
            {
              char *buf;
              char *buf;
 
 
              __asprintf (&buf, fmt, ap);
              __asprintf (&buf, fmt, ap);
 
 
              __fwprintf (stream, L"%s: %s\n",
              __fwprintf (stream, L"%s: %s\n",
                          state ? state->name : program_invocation_short_name,
                          state ? state->name : program_invocation_short_name,
                          buf);
                          buf);
 
 
              free (buf);
              free (buf);
            }
            }
          else
          else
#endif
#endif
            {
            {
              fputs (state
              fputs (state
                              ? state->name : program_invocation_short_name,
                              ? state->name : program_invocation_short_name,
                              stream);
                              stream);
              putc_unlocked (':', stream);
              putc_unlocked (':', stream);
              putc_unlocked (' ', stream);
              putc_unlocked (' ', stream);
 
 
              vfprintf (stream, fmt, ap);
              vfprintf (stream, fmt, ap);
 
 
              putc_unlocked ('\n', stream);
              putc_unlocked ('\n', stream);
            }
            }
 
 
          __argp_state_help (state, stream, ARGP_HELP_STD_ERR);
          __argp_state_help (state, stream, ARGP_HELP_STD_ERR);
 
 
          va_end (ap);
          va_end (ap);
 
 
          _funlockfile (stream);
          _funlockfile (stream);
        }
        }
    }
    }
}
}
#ifdef weak_alias
#ifdef weak_alias
weak_alias (__argp_error, argp_error)
weak_alias (__argp_error, argp_error)
#endif
#endif


/* Similar to the standard gnu error-reporting function error(), but will
/* Similar to the standard gnu error-reporting function error(), but will
   respect the ARGP_NO_EXIT and ARGP_NO_ERRS flags in STATE, and will print
   respect the ARGP_NO_EXIT and ARGP_NO_ERRS flags in STATE, and will print
   to STATE->err_stream.  This is useful for argument parsing code that is
   to STATE->err_stream.  This is useful for argument parsing code that is
   shared between program startup (when exiting is desired) and runtime
   shared between program startup (when exiting is desired) and runtime
   option parsing (when typically an error code is returned instead).  The
   option parsing (when typically an error code is returned instead).  The
   difference between this function and argp_error is that the latter is for
   difference between this function and argp_error is that the latter is for
   *parsing errors*, and the former is for other problems that occur during
   *parsing errors*, and the former is for other problems that occur during
   parsing but don't reflect a (syntactic) problem with the input.  */
   parsing but don't reflect a (syntactic) problem with the input.  */
void
void
__argp_failure (const struct argp_state *state, int status, int errnum,
__argp_failure (const struct argp_state *state, int status, int errnum,
                const char *fmt, ...)
                const char *fmt, ...)
{
{
  if (!state || !(state->flags & ARGP_NO_ERRS))
  if (!state || !(state->flags & ARGP_NO_ERRS))
    {
    {
      FILE *stream = state ? state->err_stream : stderr;
      FILE *stream = state ? state->err_stream : stderr;
 
 
      if (stream)
      if (stream)
        {
        {
          _flockfile (stream);
          _flockfile (stream);
 
 
#ifdef USE_IN_LIBIO
#ifdef USE_IN_LIBIO
          if (_IO_fwide (stream, 0) > 0)
          if (_IO_fwide (stream, 0) > 0)
            __fwprintf (stream, L"%s",
            __fwprintf (stream, L"%s",
                        state ? state->name : program_invocation_short_name);
                        state ? state->name : program_invocation_short_name);
          else
          else
#endif
#endif
            fputs (state
            fputs (state
                            ? state->name : program_invocation_short_name,
                            ? state->name : program_invocation_short_name,
                            stream);
                            stream);
 
 
          if (fmt)
          if (fmt)
            {
            {
              va_list ap;
              va_list ap;
 
 
              va_start (ap, fmt);
              va_start (ap, fmt);
#ifdef USE_IN_LIBIO
#ifdef USE_IN_LIBIO
              if (_IO_fwide (stream, 0) > 0)
              if (_IO_fwide (stream, 0) > 0)
                {
                {
                  char *buf;
                  char *buf;
 
 
                  __asprintf (&buf, fmt, ap);
                  __asprintf (&buf, fmt, ap);
 
 
                  __fwprintf (stream, L": %s", buf);
                  __fwprintf (stream, L": %s", buf);
 
 
                  free (buf);
                  free (buf);
                }
                }
              else
              else
#endif
#endif
                {
                {
                  putc_unlocked (':', stream);
                  putc_unlocked (':', stream);
                  putc_unlocked (' ', stream);
                  putc_unlocked (' ', stream);
 
 
                  vfprintf (stream, fmt, ap);
                  vfprintf (stream, fmt, ap);
                }
                }
 
 
              va_end (ap);
              va_end (ap);
            }
            }
 
 
          if (errnum)
          if (errnum)
            {
            {
              char buf[200];
              char buf[200];
 
 
#ifdef USE_IN_LIBIO
#ifdef USE_IN_LIBIO
              if (_IO_fwide (stream, 0) > 0)
              if (_IO_fwide (stream, 0) > 0)
                __fwprintf (stream, L": %s",
                __fwprintf (stream, L": %s",
                            strerror_r (errnum, buf, sizeof (buf)));
                            strerror_r (errnum, buf, sizeof (buf)));
              else
              else
#endif
#endif
                {
                {
                  putc_unlocked (':', stream);
                  putc_unlocked (':', stream);
                  putc_unlocked (' ', stream);
                  putc_unlocked (' ', stream);
                  fputs (strerror_r (errnum, buf, sizeof (buf)), stream);
                  fputs (strerror_r (errnum, buf, sizeof (buf)), stream);
                }
                }
            }
            }
 
 
#ifdef USE_IN_LIBIO
#ifdef USE_IN_LIBIO
          if (_IO_fwide (stream, 0) > 0)
          if (_IO_fwide (stream, 0) > 0)
            putwc_unlocked (L'\n', stream);
            putwc_unlocked (L'\n', stream);
          else
          else
#endif
#endif
            putc_unlocked ('\n', stream);
            putc_unlocked ('\n', stream);
 
 
          _funlockfile (stream);
          _funlockfile (stream);
 
 
          if (status && (!state || !(state->flags & ARGP_NO_EXIT)))
          if (status && (!state || !(state->flags & ARGP_NO_EXIT)))
            exit (status);
            exit (status);
        }
        }
    }
    }
}
}
#ifdef weak_alias
#ifdef weak_alias
weak_alias (__argp_failure, argp_failure)
weak_alias (__argp_failure, argp_failure)
#endif
#endif
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.