22 # define _GNU_SOURCE 1
58 # if defined HAVE_LIBINTL_H || defined _LIBC
62 # define dgettext(domain, msgid) __dcgettext (domain, msgid, LC_MESSAGES)
65 # define dgettext(domain, msgid) (msgid)
70 #include "argp-fmtstream.h"
71 #include "argp-namefrob.h"
76 # define __strchrnul strchrnul
79 # define __mempcpy mempcpy
82 # define STRNDUP strndup
84 # define STRERROR strerror
86 # define STRERROR(x) (sys_errlist[x])
89 # define FLOCKFILE __flockfile
90 # define FUNLOCKFILE __funlockfile
91 # define STRNDUP __strndup
92 # define STRERROR strerror
97 char *strndup (
const char *s,
size_t size);
101 void *mempcpy (
void *to,
const void *from,
size_t size);
105 char *strchrnul(
const char *s,
int c);
120 #define DUP_ARGS_NOTE 1
121 #define SHORT_OPT_COL 2
122 #define LONG_OPT_COL 6
123 #define DOC_OPT_COL 2
124 #define OPT_DOC_COL 29
126 #define USAGE_INDENT 12
158 DUP_ARGS, DUP_ARGS_NOTE,
159 SHORT_OPT_COL, LONG_OPT_COL, DOC_OPT_COL, OPT_DOC_COL, HEADER_COL,
160 USAGE_INDENT, RMARGIN,
175 {
"dup-args", 1, offsetof (
struct uparams, dup_args) },
176 {
"dup-args-note", 1, offsetof (
struct uparams, dup_args_note) },
177 {
"short-opt-col", 0, offsetof (
struct uparams, short_opt_col) },
178 {
"long-opt-col", 0, offsetof (
struct uparams, long_opt_col) },
179 {
"doc-opt-col", 0, offsetof (
struct uparams, doc_opt_col) },
180 {
"opt-doc-col", 0, offsetof (
struct uparams, opt_doc_col) },
181 {
"header-col", 0, offsetof (
struct uparams, header_col) },
182 {
"usage-indent", 0, offsetof (
struct uparams, usage_indent) },
183 {
"rmargin", 0, offsetof (
struct uparams, rmargin) },
189 fill_in_uparams (
const struct argp_state *state)
192 const unsigned char *var = (
unsigned char *) getenv (
"ARGP_HELP_FMT");
194 #define SKIPWS(p) do { while (isspace (*p)) p++; } while (0);
206 int unspec = 0, val = 0;
207 const unsigned char *arg = var;
209 while (isalnum (*arg) || *arg ==
'-' || *arg ==
'_')
215 if (*arg ==
'\0' || *arg ==
',')
217 else if (*arg ==
'=')
225 if (var[0] ==
'n' && var[1] ==
'o' && var[2] ==
'-')
234 else if (isdigit (*arg))
237 while (isdigit (*arg))
242 for (un = uparam_names; un->name; un++)
243 if (strlen (un->name) == var_len
244 && strncmp (var, un->name, var_len) == 0)
246 if (unspec && !un->is_bool)
247 __argp_failure (state, 0, 0,
248 dgettext (state->root_argp->argp_domain,
"\
249 %.*s: ARGP_HELP_FMT parameter requires a value"),
252 *(
int *)((
char *)&
uparams + un->uparams_offs) = val;
256 __argp_failure (state, 0, 0,
257 dgettext (state->root_argp->argp_domain,
"\
258 %.*s: Unknown ARGP_HELP_FMT parameter"),
267 __argp_failure (state, 0, 0,
268 dgettext (state->root_argp->argp_domain,
269 "Garbage in ARGP_HELP_FMT: %s"), var);
277 #define ovisible(opt) (! ((opt)->flags & OPTION_HIDDEN))
280 #define oalias(opt) ((opt)->flags & OPTION_ALIAS)
283 #define odoc(opt) ((opt)->flags & OPTION_DOC)
286 #define oend(opt) __option_is_end (opt)
289 #define oshort(opt) __option_is_short (opt)
357 find_char (
char ch,
char *beg,
char *end)
433 unsigned num_entries;
452 unsigned num_short_options = 0;
453 struct hol *
hol = malloc (
sizeof (
struct hol));
457 hol->num_entries = 0;
465 assert (! oalias (opts));
468 for (o = opts; ! oend (o); o++)
477 hol->short_options = malloc (num_short_options + 1);
479 assert (
hol->entries &&
hol->short_options);
482 so =
hol->short_options;
483 for (o = opts, entry =
hol->entries; ! oend (o); entry++)
487 entry->short_options = so;
488 entry->group = cur_group =
491 : ((!o->name && !o->key)
494 entry->cluster = cluster;
500 if (oshort (o) && ! find_char (o->key,
hol->short_options, so))
505 while (! oend (o) && oalias (o));
517 hol_add_cluster (
struct hol *
hol,
int group,
const char *header,
int index,
529 cl->depth = parent ? parent->depth + 1 : 0;
531 cl->next =
hol->clusters;
539 hol_free (
struct hol *
hol)
550 if (
hol->num_entries > 0)
553 free (
hol->short_options);
560 hol_entry_short_iterate (
const struct hol_entry *entry,
563 const char *domain,
void *cookie),
564 const char *domain,
void *cookie)
569 char *so = entry->short_options;
571 for (opt = real, nopts = entry->num; nopts > 0 && !val; opt++, nopts--)
572 if (oshort (opt) && *so == opt->key)
577 val = (*func)(opt, real, domain, cookie);
585 hol_entry_long_iterate (
const struct hol_entry *entry,
588 const char *domain,
void *cookie),
589 const char *domain,
void *cookie)
595 for (opt = real, nopts = entry->num; nopts > 0 && !val; opt++, nopts--)
601 val = (*func)(opt, real, domain, cookie);
610 const char *domain UNUSED,
void *cookie UNUSED)
612 return oshort (opt) ? opt->key : 0;
617 hol_entry_first_short (
const struct hol_entry *entry)
619 return hol_entry_short_iterate (entry, until_short,
620 entry->argp->argp_domain, 0);
625 hol_entry_first_long (
const struct hol_entry *entry)
629 for (opt = entry->opt, num = entry->num; num > 0; opt++, num--)
630 if (opt->name && ovisible (opt))
638 hol_find_entry (
struct hol *
hol,
const char *name)
641 unsigned num_entries =
hol->num_entries;
643 while (num_entries-- > 0)
646 unsigned num_opts = entry->num;
648 while (num_opts-- > 0)
649 if (opt->name && ovisible (opt) && strcmp (opt->name, name) == 0)
663 hol_set_group (
struct hol *
hol,
const char *name,
int group)
667 entry->group =
group;
673 group_cmp (
int group1,
int group2,
int eq)
675 if (group1 == group2)
677 else if ((group1 < 0 && group2 < 0) || (group1 >= 0 && group2 >= 0))
678 return group1 - group2;
680 return group2 - group1;
690 while (cl1->depth < cl2->depth)
692 while (cl2->depth < cl1->depth)
697 while (cl1->parent != cl2->parent)
698 cl1 = cl1->parent, cl2 = cl2->parent;
700 return group_cmp (cl1->group, cl2->group, cl2->index - cl1->index);
715 hol_cluster_is_child (
const struct hol_cluster *cl1,
718 while (cl1 && cl1 != cl2)
729 canon_doc_option (
const char **name)
733 while (isspace ( (
unsigned char) **name))
736 non_opt = (**name !=
'-');
738 while (**name && !isalnum ( (
unsigned char) **name))
746 hol_entry_cmp (
const struct hol_entry *entry1,
751 int group1 = entry1->group, group2 = entry2->group;
753 if (entry1->cluster != entry2->cluster)
757 if (! entry1->cluster)
762 return group_cmp (group1, hol_cluster_base (entry2->cluster)->group, -1);
763 else if (! entry2->cluster)
765 return group_cmp (hol_cluster_base (entry1->cluster)->group, group2, 1);
768 return hol_cluster_cmp (entry1->cluster, entry2->cluster);
770 else if (group1 == group2)
774 int short1 = hol_entry_first_short (entry1);
775 int short2 = hol_entry_first_short (entry2);
776 int doc1 = odoc (entry1->opt);
777 int doc2 = odoc (entry2->opt);
779 const char *long1 = hol_entry_first_long (entry1);
780 const char *long2 = hol_entry_first_long (entry2);
783 doc1 = canon_doc_option (&long1);
785 doc2 = canon_doc_option (&long2);
791 else if (!short1 && !short2 && long1 && long2)
793 return __strcasecmp (long1, long2);
801 unsigned char first1 = short1 ? short1 : long1 ? *long1 : 0;
802 unsigned char first2 = short2 ? short2 : long2 ? *long2 : 0;
804 int lower_cmp = _tolower (first1) - _tolower (first2);
806 int lower_cmp = tolower (first1) - tolower (first2);
813 return lower_cmp ? lower_cmp : first2 - first1;
819 return group_cmp (group1, group2, 0);
824 hol_entry_qcmp (
const void *entry1_v,
const void *entry2_v)
826 return hol_entry_cmp (entry1_v, entry2_v);
833 hol_sort (
struct hol *
hol)
835 if (
hol->num_entries > 0)
843 hol_append (
struct hol *
hol,
struct hol *more)
849 cl_end = &(*cl_end)->next;
850 *cl_end = more->clusters;
854 if (more->num_entries > 0)
856 if (
hol->num_entries == 0)
858 hol->num_entries = more->num_entries;
859 hol->entries = more->entries;
860 hol->short_options = more->short_options;
861 more->num_entries = 0;
870 unsigned num_entries =
hol->num_entries + more->num_entries;
872 malloc (num_entries *
sizeof (
struct hol_entry));
873 unsigned hol_so_len = strlen (
hol->short_options);
874 char *short_options =
875 malloc (hol_so_len + strlen (more->short_options) + 1);
877 __mempcpy (__mempcpy (entries,
hol->entries,
880 more->num_entries * sizeof (
struct hol_entry));
882 __mempcpy (short_options,
hol->short_options, hol_so_len);
885 for (e = entries, left =
hol->num_entries; left > 0; e++, left--)
886 e->short_options += (short_options -
hol->short_options);
890 so = short_options + hol_so_len;
891 more_so = more->short_options;
892 for (left = more->num_entries; left > 0; e++, left--)
897 e->short_options = so;
899 for (opts_left = e->num, opt = e->opt; opts_left; opt++, opts_left--)
902 if (oshort (opt) && ch == opt->key)
905 if (! find_char (ch, short_options,
906 short_options + hol_so_len))
918 free (
hol->short_options);
920 hol->entries = entries;
921 hol->num_entries = num_entries;
922 hol->short_options = short_options;
933 int needed = col - __argp_fmtstream_point (stream);
935 __argp_fmtstream_putc (stream,
' ');
943 if (__argp_fmtstream_point (stream) + ensure
944 >= __argp_fmtstream_rmargin (stream))
945 __argp_fmtstream_putc (stream,
'\n');
947 __argp_fmtstream_putc (stream,
' ');
954 arg (
const struct argp_option *real,
const char *req_fmt,
const char *opt_fmt,
959 if (real->flags & OPTION_ARG_OPTIONAL)
960 __argp_fmtstream_printf (stream, opt_fmt,
961 dgettext (domain, real->arg));
963 __argp_fmtstream_printf (stream, req_fmt,
964 dgettext (domain, real->arg));
982 int suppressed_dup_arg;
1003 filter_doc (
const char *doc,
int key,
const struct argp *
argp,
1006 if (
argp->help_filter)
1009 void *input = __argp_input (
argp, state);
1010 return (*
argp->help_filter) (key, doc, input);
1023 print_header (
const char *str,
const struct argp *
argp,
1026 const char *tstr = dgettext (
argp->argp_domain, str);
1027 const char *fstr = filter_doc (tstr, ARGP_KEY_HELP_HEADER,
argp, pest->state);
1033 if (pest->hhstate->prev_entry)
1035 __argp_fmtstream_putc (pest->stream,
'\n');
1036 indent_to (pest->stream,
uparams.header_col);
1037 __argp_fmtstream_set_lmargin (pest->stream,
uparams.header_col);
1038 __argp_fmtstream_set_wmargin (pest->stream,
uparams.header_col);
1039 __argp_fmtstream_puts (pest->stream, fstr);
1040 __argp_fmtstream_set_lmargin (pest->stream, 0);
1041 __argp_fmtstream_putc (pest->stream,
'\n');
1044 pest->hhstate->sep_groups = 1;
1048 free ((
char *) fstr);
1060 const struct hol_entry *pe = pest->hhstate->prev_entry;
1061 const struct hol_cluster *cl = pest->entry->cluster;
1063 if (pest->hhstate->sep_groups && pe && pest->entry->group != pe->group)
1064 __argp_fmtstream_putc (pest->stream,
'\n');
1066 if (cl && cl->header && *cl->header
1068 || (pe->cluster != cl
1069 && !hol_cluster_is_child (pe->cluster, cl))))
1075 int old_wm = __argp_fmtstream_wmargin (pest->stream);
1076 print_header (cl->header, cl->argp, pest);
1077 __argp_fmtstream_set_wmargin (pest->stream, old_wm);
1083 __argp_fmtstream_puts (pest->stream,
", ");
1085 indent_to (pest->stream, col);
1094 const struct argp_option *real = entry->opt, *opt;
1095 char *so = entry->short_options;
1096 int have_long_opt = 0;
1098 int old_lm = __argp_fmtstream_set_lmargin (stream, 0);
1099 int old_wm = __argp_fmtstream_wmargin (stream);
1106 pest.stream = stream;
1107 pest.hhstate = hhstate;
1112 for (opt = real, num = entry->num; num > 0; opt++, num--)
1113 if (opt->name && ovisible (opt))
1120 __argp_fmtstream_set_wmargin (stream,
uparams.short_opt_col);
1121 for (opt = real, num = entry->num; num > 0; opt++, num--)
1122 if (oshort (opt) && opt->key == *so)
1127 comma (
uparams.short_opt_col, &pest);
1128 __argp_fmtstream_putc (stream,
'-');
1129 __argp_fmtstream_putc (stream, *so);
1130 if (!have_long_opt ||
uparams.dup_args)
1131 arg (real,
" %s",
"[%s]", state->root_argp->argp_domain, stream);
1133 hhstate->suppressed_dup_arg = 1;
1142 __argp_fmtstream_set_wmargin (stream,
uparams.doc_opt_col);
1143 for (opt = real, num = entry->num; num > 0; opt++, num--)
1144 if (opt->name && ovisible (opt))
1146 comma (
uparams.doc_opt_col, &pest);
1150 __argp_fmtstream_puts (stream,
1151 dgettext (state->root_argp->argp_domain,
1158 int first_long_opt = 1;
1160 __argp_fmtstream_set_wmargin (stream,
uparams.long_opt_col);
1161 for (opt = real, num = entry->num; num > 0; opt++, num--)
1162 if (opt->name && ovisible (opt))
1164 comma (
uparams.long_opt_col, &pest);
1165 __argp_fmtstream_printf (stream,
"--%s", opt->name);
1166 if (first_long_opt ||
uparams.dup_args)
1167 arg (real,
"=%s",
"[=%s]", state->root_argp->argp_domain,
1170 hhstate->suppressed_dup_arg = 1;
1175 __argp_fmtstream_set_lmargin (stream, 0);
1180 if (!oshort (real) && !real->name)
1182 print_header (real->doc, entry->argp, &pest);
1189 const char *tstr = real->doc ? dgettext (state->root_argp->argp_domain,
1191 const char *fstr = filter_doc (tstr, real->key, entry->argp, state);
1194 unsigned int col = __argp_fmtstream_point (stream);
1196 __argp_fmtstream_set_lmargin (stream,
uparams.opt_doc_col);
1197 __argp_fmtstream_set_wmargin (stream,
uparams.opt_doc_col);
1199 if (col > (
unsigned int) (
uparams.opt_doc_col + 3))
1200 __argp_fmtstream_putc (stream,
'\n');
1201 else if (col >= (
unsigned int)
uparams.opt_doc_col)
1202 __argp_fmtstream_puts (stream,
" ");
1204 indent_to (stream,
uparams.opt_doc_col);
1206 __argp_fmtstream_puts (stream, fstr);
1208 if (fstr && fstr != tstr)
1209 free ((
char *) fstr);
1212 __argp_fmtstream_set_lmargin (stream, 0);
1213 __argp_fmtstream_putc (stream,
'\n');
1216 hhstate->prev_entry = entry;
1219 __argp_fmtstream_set_lmargin (stream, old_lm);
1220 __argp_fmtstream_set_wmargin (stream, old_wm);
1232 for (entry =
hol->entries, num =
hol->num_entries; num > 0; entry++, num--)
1233 hol_entry_help (entry, state, stream, &hhstate);
1235 if (hhstate.suppressed_dup_arg &&
uparams.dup_args_note)
1237 const char *tstr = dgettext (state->root_argp->argp_domain,
"\
1238 Mandatory or optional arguments to long options are also mandatory or \
1239 optional for any corresponding short options.");
1240 const char *fstr = filter_doc (tstr, ARGP_KEY_HELP_DUP_ARGS_NOTE,
1241 state ? state->root_argp : 0, state);
1244 __argp_fmtstream_putc (stream,
'\n');
1245 __argp_fmtstream_puts (stream, fstr);
1246 __argp_fmtstream_putc (stream,
'\n');
1248 if (fstr && fstr != tstr)
1249 free ((
char *) fstr);
1258 add_argless_short_opt (
const struct argp_option *opt,
1260 const char *domain UNUSED,
void *cookie)
1262 char **snao_end = cookie;
1263 if (!(opt->arg || real->arg)
1264 && !((opt->flags | real->flags) & OPTION_NO_USAGE))
1265 *(*snao_end)++ = opt->key;
1272 usage_argful_short_opt (
const struct argp_option *opt,
1274 const char *domain UNUSED,
void *cookie)
1277 const char *arg = opt->arg;
1278 int flags = opt->flags | real->flags;
1283 if (arg && !(flags & OPTION_NO_USAGE))
1285 arg = dgettext (domain, arg);
1287 if (flags & OPTION_ARG_OPTIONAL)
1288 __argp_fmtstream_printf (stream,
" [-%c[%s]]", opt->key, arg);
1293 space (stream, 6 + strlen (arg));
1294 __argp_fmtstream_printf (stream,
"[-%c %s]", opt->key, arg);
1306 const char *domain UNUSED,
void *cookie)
1309 const char *arg = opt->arg;
1310 int flags = opt->flags | real->flags;
1315 if (! (flags & OPTION_NO_USAGE))
1319 arg = dgettext (domain, arg);
1320 if (flags & OPTION_ARG_OPTIONAL)
1321 __argp_fmtstream_printf (stream,
" [--%s[=%s]]", opt->name, arg);
1323 __argp_fmtstream_printf (stream,
" [--%s=%s]", opt->name, arg);
1326 __argp_fmtstream_printf (stream,
" [--%s]", opt->name);
1336 if (
hol->num_entries > 0)
1340 char *short_no_arg_opts = alloca (strlen (
hol->short_options) + 1);
1341 char *snao_end = short_no_arg_opts;
1344 for (entry =
hol->entries, nentries =
hol->num_entries
1346 ; entry++, nentries--)
1347 hol_entry_short_iterate (entry, add_argless_short_opt,
1348 entry->argp->argp_domain, &snao_end);
1349 if (snao_end > short_no_arg_opts)
1352 __argp_fmtstream_printf (stream,
" [-%s]", short_no_arg_opts);
1356 for (entry =
hol->entries, nentries =
hol->num_entries
1358 ; entry++, nentries--)
1359 hol_entry_short_iterate (entry, usage_argful_short_opt,
1360 entry->argp->argp_domain, stream);
1363 for (entry =
hol->entries, nentries =
hol->num_entries
1365 ; entry++, nentries--)
1366 hol_entry_long_iterate (entry, usage_long_opt,
1367 entry->argp->argp_domain, stream);
1382 ((child->group || child->header)
1384 ? hol_add_cluster (
hol, child->group, child->header,
1385 child -
argp->children, cluster,
argp)
1388 hol_append (
hol, argp_hol (child->argp, child_cluster)) ;
1397 argp_args_levels (
const struct argp *
argp)
1402 if (
argp->args_doc && strchr (
argp->args_doc,
'\n'))
1407 levels += argp_args_levels ((child++)->
argp);
1421 char *our_level = *levels;
1424 const char *tdoc = dgettext (
argp->argp_domain,
argp->args_doc), *nl = 0;
1425 const char *fdoc = filter_doc (tdoc, ARGP_KEY_HELP_ARGS_DOC,
argp, state);
1429 const char *cp = fdoc;
1430 nl = __strchrnul (cp,
'\n');
1437 for (i = 0; i < *our_level; i++)
1438 cp = nl + 1, nl = __strchrnul (cp,
'\n');
1444 space (stream, 1 + nl - cp);
1446 __argp_fmtstream_write (stream, cp, nl - cp);
1448 if (fdoc && fdoc != tdoc)
1449 free ((
char *)fdoc);
1453 advance = !argp_args_usage ((child++)->
argp, state, levels, advance, stream);
1455 if (advance && multiple)
1464 else if (*our_level > 0)
1481 int post,
int pre_blank,
int first_only,
1485 const char *inp_text;
1488 size_t inp_text_limit = 0;
1489 const char *doc = dgettext (
argp->argp_domain,
argp->doc);
1494 char *vt = strchr (doc,
'\v');
1495 inp_text = post ? (vt ? vt + 1 : 0) : doc;
1496 inp_text_limit = (!post && vt) ? (vt - doc) : 0;
1501 if (
argp->help_filter)
1506 inp_text = STRNDUP (inp_text, inp_text_limit);
1507 input = __argp_input (
argp, state);
1509 (*
argp->help_filter) (post
1510 ? ARGP_KEY_HELP_POST_DOC
1511 : ARGP_KEY_HELP_PRE_DOC,
1515 text = (
const char *) inp_text;
1520 __argp_fmtstream_putc (stream,
'\n');
1522 if (text == inp_text && inp_text_limit)
1523 __argp_fmtstream_write (stream, inp_text, inp_text_limit);
1525 __argp_fmtstream_puts (stream, text);
1527 if (__argp_fmtstream_point (stream) > __argp_fmtstream_lmargin (stream))
1528 __argp_fmtstream_putc (stream,
'\n');
1533 if (text && text != inp_text)
1534 free ((
char *) text);
1535 if (inp_text && inp_text_limit &&
argp->help_filter)
1536 free ((
char *) inp_text);
1538 if (post &&
argp->help_filter)
1541 text = (*
argp->help_filter) (ARGP_KEY_HELP_EXTRA, 0, input);
1544 if (anything || pre_blank)
1545 __argp_fmtstream_putc (stream,
'\n');
1546 __argp_fmtstream_puts (stream, text);
1547 free ((
char *) text);
1548 if (__argp_fmtstream_point (stream)
1549 > __argp_fmtstream_lmargin (stream))
1550 __argp_fmtstream_putc (stream,
'\n');
1556 while (child->argp && !(first_only && anything))
1558 argp_doc ((child++)->
argp, state,
1559 post, anything || pre_blank, first_only,
1572 unsigned flags,
const char *name)
1584 fill_in_uparams (state);
1586 fs = __argp_make_fmtstream (stream, 0,
uparams.rmargin, 0);
1589 FUNLOCKFILE (stream);
1593 if (flags & (ARGP_HELP_USAGE | ARGP_HELP_SHORT_USAGE | ARGP_HELP_LONG))
1598 hol_set_group (
hol,
"help", -1);
1599 hol_set_group (
hol,
"version", -1);
1604 if (flags & (ARGP_HELP_USAGE | ARGP_HELP_SHORT_USAGE))
1607 int first_pattern = 1, more_patterns;
1608 size_t num_pattern_levels = argp_args_levels (
argp);
1609 char *pattern_levels = alloca (num_pattern_levels);
1611 memset (pattern_levels, 0, num_pattern_levels);
1616 int old_wm = __argp_fmtstream_set_wmargin (fs,
uparams.usage_indent);
1617 char *levels = pattern_levels;
1620 __argp_fmtstream_printf (fs,
"%s %s",
1621 dgettext (
argp->argp_domain,
"Usage:"),
1624 __argp_fmtstream_printf (fs,
"%s %s",
1625 dgettext (
argp->argp_domain,
" or: "),
1630 old_lm = __argp_fmtstream_set_lmargin (fs,
uparams.usage_indent);
1632 if (flags & ARGP_HELP_SHORT_USAGE)
1635 if (
hol->num_entries > 0)
1636 __argp_fmtstream_puts (fs, dgettext (
argp->argp_domain,
1642 hol_usage (
hol, fs);
1643 flags |= ARGP_HELP_SHORT_USAGE;
1646 more_patterns = argp_args_usage (
argp, state, &levels, 1, fs);
1648 __argp_fmtstream_set_wmargin (fs, old_wm);
1649 __argp_fmtstream_set_lmargin (fs, old_lm);
1651 __argp_fmtstream_putc (fs,
'\n');
1656 while (more_patterns);
1659 if (flags & ARGP_HELP_PRE_DOC)
1660 anything |= argp_doc (
argp, state, 0, 0, 1, fs);
1662 if (flags & ARGP_HELP_SEE)
1664 __argp_fmtstream_printf (fs, dgettext (
argp->argp_domain,
"\
1665 Try `%s --help' or `%s --usage' for more information.\n"),
1670 if (flags & ARGP_HELP_LONG)
1674 if (
hol->num_entries > 0)
1677 __argp_fmtstream_putc (fs,
'\n');
1678 hol_help (
hol, state, fs);
1683 if (flags & ARGP_HELP_POST_DOC)
1685 anything |= argp_doc (
argp, state, 1, anything, 0, fs);
1687 if ((flags & ARGP_HELP_BUG_ADDR) && argp_program_bug_address)
1690 __argp_fmtstream_putc (fs,
'\n');
1691 __argp_fmtstream_printf (fs, dgettext (
argp->argp_domain,
1692 "Report bugs to %s.\n"),
1693 argp_program_bug_address);
1697 FUNLOCKFILE (stream);
1702 __argp_fmtstream_free (fs);
1707 void __argp_help (
const struct argp *
argp, FILE *stream,
1708 unsigned flags,
char *name)
1710 _help (
argp, 0, stream, flags, name);
1713 weak_alias (__argp_help, argp_help)
1716 char *__argp_basename(
char *name)
1718 char *short_name = strrchr(name,
'/');
1719 return short_name ? short_name + 1 : name;
1723 __argp_short_program_name(
const struct argp_state *state)
1727 #if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME
1728 return program_invocation_short_name;
1729 #elif HAVE_DECL_PROGRAM_INVOCATION_NAME
1730 return __argp_basename(program_invocation_name);
1736 # warning No reasonable value to return
1745 __argp_state_help (
const struct argp_state *state, FILE *stream,
unsigned flags)
1747 if ((!state || ! (state->flags & ARGP_NO_ERRS)) && stream)
1749 if (state && (state->flags & ARGP_LONG_ONLY))
1750 flags |= ARGP_HELP_LONG_ONLY;
1752 _help (state ? state->root_argp : 0, state, stream, flags,
1753 __argp_short_program_name(state));
1755 if (!state || ! (state->flags & ARGP_NO_EXIT))
1757 if (flags & ARGP_HELP_EXIT_ERR)
1758 exit (argp_err_exit_status);
1759 if (flags & ARGP_HELP_EXIT_OK)
1765 weak_alias (__argp_state_help, argp_state_help)
1772 __argp_error (
const struct argp_state *state,
const char *fmt, ...)
1774 if (!state || !(state->flags & ARGP_NO_ERRS))
1776 FILE *stream = state ? state->err_stream : stderr;
1784 FPUTS_UNLOCKED (__argp_short_program_name(state),
1786 PUTC_UNLOCKED (
':', stream);
1787 PUTC_UNLOCKED (
' ', stream);
1790 vfprintf (stream, fmt, ap);
1793 PUTC_UNLOCKED (
'\n', stream);
1795 __argp_state_help (state, stream, ARGP_HELP_STD_ERR);
1797 FUNLOCKFILE (stream);
1802 weak_alias (__argp_error, argp_error)
1814 __argp_failure (
const struct argp_state *state,
int status,
int errnum,
1815 const char *fmt, ...)
1817 if (!state || !(state->flags & ARGP_NO_ERRS))
1819 FILE *stream = state ? state->err_stream : stderr;
1825 FPUTS_UNLOCKED (__argp_short_program_name(state),
1832 PUTC_UNLOCKED (
':', stream);
1833 PUTC_UNLOCKED (
' ', stream);
1836 vfprintf (stream, fmt, ap);
1842 PUTC_UNLOCKED (
':', stream);
1843 PUTC_UNLOCKED (
' ', stream);
1844 fputs (STRERROR (errnum), stream);
1847 PUTC_UNLOCKED (
'\n', stream);
1849 FUNLOCKFILE (stream);
1851 if (status && (!state || !(state->flags & ARGP_NO_EXIT)))
1857 weak_alias (__argp_failure, argp_failure)