/* Hierarchial argument parsing help output
Copyright (C) 1995-2005, 2007, 2009-2010 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Written by Miles Bader <miles@gnu.ai.mit.edu>.
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef _GNU_SOURCE
#endif
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <alloca.h>
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdarg.h>
#include <ctype.h>
#include <limits.h>
#ifdef USE_IN_LIBIO
# include <wchar.h>
#endif
#ifdef _LIBC
# include <libintl.h>
#else
# include "gettext.h"
#endif
#include "argp.h"
#include "argp-fmtstream.h"
#include "argp-namefrob.h"
#ifndef SIZE_MAX
#endif
/* User-selectable (using an environment variable) formatting parameters.
These may be specified in an environment variable called `ARGP_HELP_FMT',
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
UPARAM_NAMES vector, below. */
/* Default parameters. */
/* User-selectable (using an environment variable) formatting parameters.
They must all be of type `int' for the parsing code to work. */
struct uparams
{
/* 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'.
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
this really means both is printed below the options. */
int dup_args;
/* This is true if when DUP_ARGS is false, and some duplicate arguments have
been suppressed, an explanatory message should be printed. */
int dup_args_note;
/* Various output columns. */
};
/* This is a global variable, as user options are only ever read once. */
0
};
/* A particular uparam, and what the user name is. */
struct uparam_name
{
};
/* The name-field mappings we know about. */
{
{ 0 }
};
static void
{
{
continue;
{
__argp_failure (state, 0, 0,
"\
ARGP_HELP_FMT: %s value is less than or equal to %s"),
return;
}
}
}
/* Read user options from the environment, and fill in UPARAMS appropiately. */
static void
{
if (var)
{
/* Parse var. */
while (*var)
{
{
arg++;
unspec = 1;
else if (*arg == '=')
{
arg++;
}
if (unspec)
{
{
val = 0;
var += 3;
var_len -= 3;
}
else
val = 1;
}
{
arg++;
}
{
__argp_failure (state, 0, 0,
"\
%.*s: ARGP_HELP_FMT parameter requires a value"),
else if (val < 0)
__argp_failure (state, 0, 0,
"\
%.*s: ARGP_HELP_FMT parameter must be positive"),
else
break;
}
__argp_failure (state, 0, 0,
%.*s: Unknown ARGP_HELP_FMT parameter"),
if (*var == ',')
var++;
}
else if (*var)
{
__argp_failure (state, 0, 0,
"Garbage in ARGP_HELP_FMT: %s"), var);
break;
}
}
}
}
/* Returns true if OPT hasn't been marked invisible. Visibility only affects
whether OPT is displayed or used in sorting, not option shadowing. */
/* Returns true if OPT is an alias for an earlier option. */
/* Returns true if OPT is an documentation-only entry. */
/* Returns true if OPT should not be translated */
/* Returns true if OPT is the end-of-list marker for a list of options. */
/* Returns true if OPT has a short option. */
/*
The help format for a particular option is like:
-xARG, -yARG, --long1=ARG, --long2=ARG Documentation...
Where ARG will be omitted if there's no argument, for this option, or
will be surrounded by "[" and "]" appropiately if the argument is
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.
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
to be in a separate column.
For example, the following output (from ps):
-p PID, --pid=PID List the process PID
--pgrp=PGRP List processes in the process group PGRP
-P, -x, --no-parent Include processes without parents
-Q, --all-fields Don't elide unusable fields (normally if there's
some reason ps can't print a field for any
process, it's removed from the output entirely)
-r, --reverse, --gratuitously-long-reverse-option
Reverse the order of any sort
--session[=SID] Add the processes from the session SID (which
defaults to the sid of the current process)
Here are some more options:
-f ZOT, --foonly=ZOT Glork a foonly
-z, --zaza Snit a zar
-?, --help Give this help list
--usage Give a short usage message
-V, --version Print program version
The struct argp_option array for the above could look like:
{
{"pid", 'p', "PID", 0, "List the process PID"},
{"pgrp", OPT_PGRP, "PGRP", 0, "List processes in the process group PGRP"},
{"no-parent", 'P', 0, 0, "Include processes without parents"},
{0, 'x', 0, OPTION_ALIAS},
{"all-fields",'Q', 0, 0, "Don't elide unusable fields (normally"
" if there's some reason ps can't"
" print a field for any process, it's"
" removed from the output entirely)" },
{"reverse", 'r', 0, 0, "Reverse the order of any sort"},
{"gratuitously-long-reverse-option", 0, 0, OPTION_ALIAS},
{"session", OPT_SESS, "SID", OPTION_ARG_OPTIONAL,
"Add the processes from the session"
" SID (which defaults to the sid of"
" the current process)" },
{0,0,0,0, "Here are some more options:"},
{"foonly", 'f', "ZOT", 0, "Glork a foonly"},
{"zaza", 'z', 0, 0, "Snit a zar"},
{0}
}
Note that the last three options are automatically supplied by argp_parse,
unless you tell it not to with ARGP_NO_HELP.
*/
/* Returns true if CH occurs between BEG and END. */
static int
{
return 1;
else
beg++;
return 0;
}
struct hol_cluster; /* fwd decl */
struct hol_entry
{
/* First option. */
/* Number of options (including aliases). */
unsigned num;
/* 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
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
valid if it occurs in the right place in SHORT_OPTIONS (otherwise it's
probably been shadowed by some other entry). */
char *short_options;
/* Entries are sorted by their group first, in the order:
1, 2, ..., n, 0, -m, ..., -2, -1
and then alphabetically within each group. The default is 0. */
int group;
/* The cluster of options this entry belongs to, or 0 if none. */
/* The argp from which this option came. */
/* Position in the array */
unsigned ord;
};
/* A cluster of entries to reflect the argp tree structure. */
struct hol_cluster
{
/* A descriptive header printed before options in this cluster. */
const char *header;
/* 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
list. */
int index;
/* How to sort this cluster with respect to options and other clusters at the
same depth (clusters always follow options in the same group). */
int group;
/* The cluster to which this cluster belongs, or 0 if it's at the base
level. */
/* The argp from which this cluster is (eventually) derived. */
/* The distance this cluster is from the root. */
int depth;
/* Clusters in a given hol are kept in a linked list, to make freeing them
possible. */
};
/* A list of options for help. */
struct hol
{
/* An array of hol_entry's. */
/* The number of entries in this hol. If this field is zero, the others
are undefined. */
unsigned num_entries;
/* 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. */
char *short_options;
/* Clusters of entries in this hol. */
};
/* 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. */
static struct hol *
{
char *so;
const struct argp_option *o;
unsigned num_short_options = 0;
hol->num_entries = 0;
if (opts)
{
int cur_group = 0;
/* The first option must not be an alias. */
/* Calculate the space needed. */
{
if (! oalias (o))
hol->num_entries++;
if (oshort (o))
num_short_options++; /* This is an upper bound. */
}
/* Fill in the entries. */
{
o->group
? o->group
? cur_group + 1
: cur_group);
do
{
/* O has a valid short option which hasn't already been used.*/
o++;
}
}
}
return hol;
}
/* 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
to it. ARGP is the argp that this cluster results from. */
static struct hol_cluster *
{
if (cl)
{
}
return cl;
}
/* Free HOL and any resources it uses. */
static void
{
while (cl)
{
}
if (hol->num_entries > 0)
{
}
}
static int
const struct argp_option *real,
{
unsigned nopts;
int val = 0;
{
so++;
}
return val;
}
static inline int
const struct argp_option *real,
{
unsigned nopts;
int val = 0;
{
}
return val;
}
/* Iterator that returns true for the first short option. */
static int
{
}
/* Returns the first valid short option in ENTRY, or 0 if there is none. */
static char
{
}
/* Returns the first valid long option in ENTRY, or 0 if there is none. */
static const char *
{
unsigned num;
return 0;
}
/* Returns the entry in HOL with the long option name NAME, or 0 if there is
none. */
static struct hol_entry *
{
while (num_entries-- > 0)
{
while (num_opts-- > 0)
return entry;
else
opt++;
entry++;
}
return 0;
}
/* If an entry with the long option NAME occurs in HOL, set it's special
sort position to GROUP. */
static void
{
if (entry)
}
/* Order by group: 0, 1, 2, ..., n, -m, ..., -2, -1.
EQ is what to return if GROUP1 and GROUP2 are the same. */
static int
{
return eq;
else
}
/* Compare clusters CL1 & CL2 by the order that they should appear in
output. */
static int
{
/* If one cluster is deeper than the other, use its ancestor at the same
level, so that finding the common ancestor is straightforward.
clN->depth > 0 means that clN->parent != NULL (see hol_add_cluster) */
/* Now reduce both clusters to their ancestors at the point where both have
a common parent; these can be directly compared. */
}
/* Return the ancestor of CL that's just below the root (i.e., has a parent
of 0). */
static struct hol_cluster *
{
return cl;
}
/* Return true if CL1 is a child of CL2. */
static int
const struct hol_cluster *cl2)
{
}
/* 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
treated as a non-option. */
static int
{
int non_opt;
if (!*name)
non_opt = 1;
else
{
/* Skip initial whitespace. */
(*name)++;
/* Decide whether this looks like an option (leading `-') or not. */
/* Skip until part of name used for sorting. */
(*name)++;
}
return non_opt;
}
/* Order ENTRY1 & ENTRY2 by the order which they should appear in a help
listing. */
static int
{
/* 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. */
int rc;
{
/* The entries are not within the same cluster, so we can't compare them
directly, we have to use the appropiate clustering level too. */
/* 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
ENTRY2 resides. Note that if they're in the same group, the
clustered option always comes laster. */
/* Likewise, but ENTRY2's not in a cluster. */
else
/* Both entries are in clusters, we can just compare the clusters. */
}
/* The entries are both in the same cluster and group, so compare them
alphabetically. */
{
if (doc1)
if (doc2)
/* `documentation' options always follow normal options (or
documentation options that *look* like normal options). */
/* Only long options. */
else
character of long options. Entries without *any* valid
options (such as options with OPTION_HIDDEN set) will be put
first, but as they're not displayed, it doesn't matter where
they are. */
{
/* Use tolower, not _tolower, since only the former is
guaranteed to work on something already lower case. */
/* Compare ignoring case, except when the options are both the
same letter, in which case lower-case always comes first. */
}
}
else
/* Within the same cluster, but not the same group, so just compare
groups. */
}
/* Version of hol_entry_cmp with correct signature for qsort. */
static int
{
}
/* Sort HOL by group and alphabetically by option name (with short options
taking precedence over long). Since the sorting is for display purposes
only, the shadowing of options isn't effected. */
static void
{
if (hol->num_entries > 0)
{
unsigned i;
struct hol_entry *e;
e->ord = i;
}
}
/* Append MORE to HOL, destroying MORE in the process. Options in HOL shadow
any in MORE with the same name. */
static void
{
/* Steal MORE's cluster list, and add it to the end of HOL's. */
while (*cl_end)
/* Merge entries. */
if (more->num_entries > 0)
{
if (hol->num_entries == 0)
{
}
else
/* Append the entries in MORE to those in HOL, taking care to only add
non-shadowed SHORT_OPTIONS values. */
{
unsigned left;
struct hol_entry *e;
char *short_options =
/* Fix up the short options pointers from HOL. */
/* Now add the short options from MORE, fixing up its entries
too. */
{
int opts_left;
e->short_options = so;
{
/* The next short option in MORE_SO, CH, is from OPT. */
{
/* The short option CH isn't shadowed by HOL's options,
so add it to the sum. */
more_so++;
}
}
}
*so = '\0';
}
}
}
/* Inserts enough spaces to make sure STREAM is at column COL. */
static void
{
while (needed-- > 0)
}
/* Output to STREAM either a space, or a newline if there isn't room for at
least ENSURE characters before the right margin. */
static void
{
else
}
/* 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
optional argument. */
static void
{
{
else
}
}
/* Helper functions for hol_entry_help. */
/* State used during the execution of hol_help. */
struct hol_help_state
{
/* PREV_ENTRY should contain the previous entry printed, or 0. */
/* 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. */
int sep_groups;
/* True if a duplicate option argument was suppressed (only ever set if
UPARAMS.dup_args is false). */
int suppressed_dup_arg;
};
/* 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
of the fields are copied from its arguments. */
struct pentry_state
{
/* True if nothing's been printed so far. */
int first;
/* If non-zero, the state that was used to print this help. */
};
/* If a user doc filter should be applied to DOC, do so. */
static const char *
const struct argp_state *state)
{
if (argp->help_filter)
/* We must apply a user filter to this output. */
{
}
else
/* No filter. */
return doc;
}
/* 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
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
to 0. */
static void
struct pentry_state *pest)
{
if (fstr)
{
if (*fstr)
{
/* Precede with a blank line. */
}
}
}
/* 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,
prints any pending whitespace/headers that should precede this line. Also
clears FIRST. */
static void
{
{
&& (!pe
/* 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
(in which case we had just popped into a sub-cluster for a bit).
If so, then print the cluster's header line. */
{
}
}
else
}
/* Print help for ENTRY to STREAM. */
static void
{
unsigned num;
/* Saved margins. */
/* PEST is a state block holding some of our variables that we'd like to
share with helper functions. */
{
have_long_opt = 1;
break;
}
/* First emit short options. */
/* OPT has a valid (non shadowed) short option. */
{
{
}
so++;
}
/* Now, long options. */
/* A `documentation' option. */
{
{
/* Calling dgettext here isn't quite right, since sorting will
have been done on the original; but documentation options
should be pretty rare anyway... */
}
}
else
/* A real long option. */
{
{
stream);
}
}
/* Next, documentation strings. */
{
/* Didn't print any switches, what's up? */
/* This is a group header, print it nicely. */
else
/* Just a totally shadowed option or null header; print nothing. */
goto cleanup; /* Just return, after cleaning up. */
}
else
{
{
else
}
/* Reset the left margin. */
}
}
/* Output a long help message about the options in HOL to STREAM. */
static void
{
unsigned num;
{
Mandatory or optional arguments to long options are also mandatory or \
optional for any corresponding short options.");
{
}
}
}
/* Helper functions for hol_usage. */
/* If OPT is a short option without an arg, append its key to the string
pointer pointer to by COOKIE, and advance the pointer. */
static int
const struct argp_option *real,
{
return 0;
}
/* If OPT is a short option with an arg, output a usage entry for it to the
stream pointed at by COOKIE. */
static int
const struct argp_option *real,
{
if (! arg)
{
if (flags & OPTION_ARG_OPTIONAL)
else
{
/* Manually do line wrapping so that it (probably) won't
get wrapped at the embedded space. */
}
}
return 0;
}
/* Output a usage entry for the long option opt to the stream pointed at by
COOKIE. */
static int
const struct argp_option *real,
{
if (! arg)
{
if (arg)
{
if (flags & OPTION_ARG_OPTIONAL)
else
}
else
}
return 0;
}
/* Print a short usage description for the arguments in HOL to STREAM. */
static void
{
if (hol->num_entries > 0)
{
unsigned nentries;
/* First we put a list of short options without arguments. */
; nentries > 0
if (snao_end > short_no_arg_opts)
{
*snao_end++ = 0;
}
/* Now a list of short options *with* arguments. */
; nentries > 0
/* Finally, a list of long options (whew!). */
; nentries > 0
}
}
/* Make a HOL containing all levels of options in ARGP. CLUSTER is the
cluster in which ARGP's entries should be clustered, or 0. */
static struct hol *
{
if (child)
{
/* Put CHILD->argp within its own cluster. */
/* Just merge it into the parent's cluster. */
: cluster);
child++;
}
return hol;
}
/* Calculate how many different levels with alternative args strings exist in
ARGP. */
static size_t
{
levels++;
if (child)
return levels;
}
/* 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
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
returned as long as there are more patterns to output. */
static int
{
int multiple = 0;
if (fdoc)
{
if (*nl != '\0')
/* This is a `multi-level' args doc; advance to the correct position
as determined by our state in LEVELS, and update LEVELS. */
{
int i;
multiple = 1;
for (i = 0; i < *our_level; i++)
(*levels)++;
}
/* Manually do line wrapping so that it (probably) won't get wrapped at
any embedded spaces. */
}
if (child)
{
/* Need to increment our level. */
if (*nl)
/* There's more we can do here. */
{
(*our_level)++;
advance = 0; /* Our parent shouldn't advance also. */
}
else if (*our_level > 0)
/* We had multiple levels, but used them up; reset to zero. */
*our_level = 0;
}
return !advance;
}
/* Print the documentation for ARGP to STREAM; if POST is false, then
everything preceeding a `\v' character in the documentation strings (or
the whole string, for those with none) is printed, otherwise, everything
following the `\v' character (nothing for strings without). Each separate
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
occurrence is output. Returns true if anything was output. */
static int
{
const char *text;
const char *inp_text;
const char *trans_text;
void *input = 0;
int anything = 0;
{
if (vt)
{
if (post)
else
{
}
}
else
}
else
trans_text = inp_text = 0;
if (argp->help_filter)
/* We have to filter the doc strings. */
{
text =
trans_text, input);
}
else
text = (const char *) trans_text;
if (text)
{
if (pre_blank)
anything = 1;
}
if (inp_text && inp_text_len)
/* Now see if we have to output a ARGP_KEY_HELP_EXTRA text. */
{
if (text)
{
if (__argp_fmtstream_point (stream)
anything = 1;
}
}
if (child)
anything |=
stream);
return anything;
}
/* Output a usage message for ARGP to STREAM. If called from
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
needed. */
static void
{
if (! stream)
return;
#endif
if (! fs)
{
#endif
return;
}
{
/* If present, these options always come last. */
}
/* Print a short `Usage:' message. */
{
do
{
int old_lm;
if (first_pattern)
name);
else
name);
/* We set the lmargin as well as the wmargin, because hol_usage
manually wraps options with newline to avoid annoying breaks. */
if (flags & ARGP_HELP_SHORT_USAGE)
/* Just show where the options go. */
{
if (hol->num_entries > 0)
" [OPTION...]"));
}
else
/* Actually print the options. */
{
}
anything = 1;
first_pattern = 0;
}
while (more_patterns);
}
if (flags & ARGP_HELP_PRE_DOC)
if (flags & ARGP_HELP_SEE)
{
Try `%s --help' or `%s --usage' for more information.\n"),
anything = 1;
}
if (flags & ARGP_HELP_LONG)
/* Print a long, detailed help message. */
{
/* Print info about all the options. */
if (hol->num_entries > 0)
{
if (anything)
anything = 1;
}
}
if (flags & ARGP_HELP_POST_DOC)
/* Print any documentation strings at the end. */
{
if (anything)
"Report bugs to %s.\n"),
anything = 1;
}
#endif
if (hol)
}
/* 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. */
{
}
#ifdef weak_alias
#endif
#if ! (defined _LIBC || HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME)
char *
__argp_short_program_name (void)
{
return __argp_base_name (program_invocation_name);
# else
/* FIXME: What now? Miles suggests that it is better to use NULL,
but currently the value is passed on directly to fputs_unlocked,
so that requires more changes. */
# if __GNUC__
# endif /* __GNUC__ */
return "";
# endif
}
#endif
/* Output, if appropriate, a usage message for STATE to STREAM. FLAGS are
from the set ARGP_HELP_*. */
void
{
{
{
if (flags & ARGP_HELP_EXIT_ERR)
if (flags & ARGP_HELP_EXIT_OK)
exit (0);
}
}
}
#ifdef weak_alias
#endif
/* If appropriate, print the printf string FMT and following args, preceded
by the program name and `:', to stderr, and followed by a `Try ... --help'
message, then exit (1). */
void
{
{
if (stream)
{
#endif
#ifdef USE_IN_LIBIO
{
char *buf;
buf);
}
else
#endif
{
stream);
}
#endif
}
}
}
#ifdef weak_alias
#endif
/* 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
to STATE->err_stream. This is useful for argument parsing code that is
shared between program startup (when exiting is desired) and runtime
option parsing (when typically an error code is returned instead). The
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 but don't reflect a (syntactic) problem with the input. */
void
const char *fmt, ...)
{
{
if (stream)
{
#endif
#ifdef USE_IN_LIBIO
else
#endif
stream);
if (fmt)
{
#ifdef USE_IN_LIBIO
{
char *buf;
}
else
#endif
{
}
}
if (errnum)
{
#ifdef USE_IN_LIBIO
else
#endif
{
char const *s = NULL;
s = buf;
#endif
#if !_LIBC
"Unknown system error");
#endif
}
}
#ifdef USE_IN_LIBIO
else
#endif
#endif
}
}
}
#ifdef weak_alias
#endif