summaryrefslogtreecommitdiff
path: root/libshouldbeinlibc/argp-help.c
blob: 3a5d4ae03cf49853eedeb4b993e988fdff72df1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
/* Hierarchial argument parsing help output

   Copyright (C) 1995, 1996 Free Software Foundation, Inc.

   Written by Miles Bader <miles@gnu.ai.mit.edu>

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   The GNU Hurd 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdarg.h>
#include <malloc.h>
#include <ctype.h>
#include <linewrap.h>

#include "argp.h"

#define SHORT_OPT_COL 2		/* column in which short options start */
#define LONG_OPT_COL  6		/* column in which long options start */
#define OPT_DOC_COL  29		/* column in which option text starts */
#define HEADER_COL    1		/* column in which group headers are printed */
#define USAGE_INDENT 12		/* indentation of wrapped usage lines */
#define RMARGIN      79		/* right margin used for wrapping */

/* Returns true if OPT hasn't been marked invisible.  Visibility only affects
   whether OPT is displayed or used in sorting, not option shadowing.  */
#define ovisible(opt) (! ((opt)->flags & OPTION_HIDDEN))

/* Returns true if OPT is an alias for an earlier option.  */
#define oalias(opt) ((opt)->flags & OPTION_ALIAS)

/* Returns true if OPT is the end-of-list marker for a list of options.  */
#define oend(opt) _option_is_end (opt)

/* Returns true if OPT has a short option.  */
#define oshort(opt) _option_is_short (opt)

/*
   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 (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)

   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)"},
   }

*/

/* Returns true if CH occurs between BEG and END.  */
static int
find_char (char ch, char *beg, char *end)
{
  while (beg < end)
    if (*beg == ch)
      return 1;
    else
      beg++;
  return 0;
}

struct hol_entry
{
  /* First option.  */
  const struct argp_option *opt;
  /* 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;
};

/* A list of options for help.  */
struct hol
{
  /* The number of entries in this hol.  If this field is zero, the others
     are undefined.  */
  unsigned num_entries;
  /* An array of hol_entry's.  */
  struct hol_entry *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;
};

/* Create a struct hol from an array of struct argp_option.  */
struct hol *make_hol (const struct argp_option *opt)
{
  char *so;
  const struct argp_option *o;
  struct hol_entry *entry;
  unsigned num_short_options = 0;
  struct hol *hol = malloc (sizeof (struct hol));

  assert (hol);

  hol->num_entries = 0;

  if (opt)
    {
      int cur_group = 0;

      /* The first option must not be an alias.  */
      assert (! oalias (opt));

      /* Calculate the space needed.  */
      for (o = opt; ! oend (o); o++)
	{
	  if (! oalias (o))
	    hol->num_entries++;
	  if (oshort (o))
	    num_short_options++;	/* This is an upper bound.  */
	}

      hol->entries = malloc (sizeof (struct hol_entry) * hol->num_entries);
      hol->short_options = malloc (num_short_options + 1);

      assert (hol->entries && hol->short_options);

      /* Fill in the entries.  */
      so = hol->short_options;
      for (o = opt, entry = hol->entries; ! oend (o); entry++)
	{
	  entry->opt = o;
	  entry->num = 0;
	  entry->short_options = so;
	  entry->group = cur_group = o->group ?: cur_group;

	  do
	    {
	      entry->num++;
	      if (oshort (o) && ! find_char (o->key, hol->short_options, so))
		/* O has a valid short option which hasn't already been used.*/
		*so++ = o->key;
	      o++;
	    }
	  while (! oend (o) && oalias (o));
	}
      *so = '\0';		/* null terminated so we can find the length */
    }

  return hol;
}

/* Free HOL and any resources it uses.  */
static void
hol_free (struct hol *hol)
{
  if (hol->num_entries > 0)
    {
      free (hol->entries);
      free (hol->short_options);
    }
  free (hol);
}

static inline int
hol_entry_short_iterate (const struct hol_entry *entry,
			 int (*func)(const struct argp_option *opt,
				     const struct argp_option *real))
{
  unsigned nopts;
  int val = 0;
  const struct argp_option *opt, *real = entry->opt;
  char *so = entry->short_options;

  for (opt = real, nopts = entry->num; nopts > 0 && !val; opt++, nopts--)
    if (oshort (opt) && *so == opt->key)
      {
	if (!oalias (opt))
	  real = opt;
	if (ovisible (opt))
	  val = (*func)(opt, real);
	so++;
      }

  return val;
}

static inline int
hol_entry_long_iterate (const struct hol_entry *entry,
			int (*func)(const struct argp_option *opt,
				    const struct argp_option *real))
{
  unsigned nopts;
  int val = 0;
  const struct argp_option *opt, *real = entry->opt;

  for (opt = real, nopts = entry->num; nopts > 0 && !val; opt++, nopts--)
    if (opt->name)
      {
	if (!oalias (opt))
	  real = opt;
	if (ovisible (opt))
	  val = (*func)(opt, real);
      }

  return val;
}

/* Returns the first valid short option in ENTRY, or 0 if there is none.  */
static char
hol_entry_first_short (const struct hol_entry *entry)
{
  inline int func1 (const struct argp_option *opt,
		    const struct argp_option *real)
    {
      return opt->key;
    }
  return hol_entry_short_iterate (entry, func1);
}

/* Returns the first valid long option in ENTRY, or 0 if there is none.  */
static const char *
hol_entry_first_long (const struct hol_entry *entry)
{
  const struct argp_option *opt;
  unsigned num;
  for (opt = entry->opt, num = entry->num; num > 0; opt++, num--)
    if (opt->name && ovisible (opt))
      return opt->name;
  return 0;
}

/* Returns the entry in HOL with the long option name NAME, or 0 if there is
   none.  */
static struct hol_entry *hol_find_entry (struct hol *hol, char *name)
{
  struct hol_entry *entry = hol->entries;
  unsigned num_entries = hol->num_entries;

  while (num_entries-- > 0)
    {
      const struct argp_option *opt = entry->opt;
      unsigned num_opts = entry->num;

      while (num_opts-- > 0)
	if (opt->name && ovisible (opt) && strcmp (opt->name, name) == 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
hol_set_group (struct hol *hol, char *name, int group)
{
  struct hol_entry *entry = hol_find_entry (hol, name);
  if (entry)
    entry->group = group;
}

/* 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
hol_sort (struct hol *hol)
{
  int entry_cmp (const void *entry1_v, const void *entry2_v)
    {
      const struct hol_entry *entry1 = entry1_v, *entry2 = entry2_v;
      int group1 = entry1->group, group2 = entry2->group;

      if (group1 == group2)
	/* Normal comparison.  */
	{
	  int short1 = hol_entry_first_short (entry1);
	  int short2 = hol_entry_first_short (entry2);
	  const char *long1 = hol_entry_first_long (entry1);
	  const char *long2 = hol_entry_first_long (entry2);

	  if (!short1 && !short2 && long1 && long2)
	    /* Only long options.  */
	    return strcasecmp (long1, long2);
	  else
	    /* Compare short/short, long/short, short/long, using the first
	       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.  */
	    {
	      char first1 = short1 ?: long1 ? *long1 : 0;
	      char first2 = short2 ?: long2 ? *long2 : 0;
	      /* Compare ignoring case, except when the options are both the
		 same letter, in which case lower-case always comes first.  */
	      return (tolower (first1) - tolower (first2)) ?: first2 - first1;
	    }
	}
      else
	/* Order by group:  1, 2, ..., n, 0, -m, ..., -2, -1  */
	if ((group1 < 0 && group2 < 0) || (group1 > 0 && group2 > 0))
	  return group1 - group2;
	else
	  return group2 - group1;
    }

  if (hol->num_entries > 0)
    qsort (hol->entries, hol->num_entries, sizeof (struct hol_entry),
	   entry_cmp);
}

/* Append MORE to HOL, destroying MORE in the process.  Options in HOL shadow
   any in MORE with the same name.  */
static void
hol_append (struct hol *hol, struct hol *more)
{
  if (more->num_entries == 0)
    hol_free (more);
  else if (hol->num_entries == 0)
    {
      hol->num_entries = more->num_entries;
      hol->entries = more->entries;
      hol->short_options = more->short_options;
      /* We've stolen everything MORE from more.  Destroy the empty shell. */
      free (more);		
    }
  else
    /* append the entries in MORE to those in HOL, taking care to only add
       non-shadowed SHORT_OPTIONS values.  */
    {
      unsigned left;
      char *so, *more_so;
      struct hol_entry *e;
      unsigned num_entries = hol->num_entries + more->num_entries;
      struct hol_entry *entries =
	malloc (num_entries * sizeof (struct hol_entry));
      unsigned hol_so_len = strlen (hol->short_options);
      char *short_options =
	malloc (hol_so_len + strlen (more->short_options) + 1);

      bcopy (hol->entries, entries,
	     hol->num_entries * sizeof (struct hol_entry));
      bcopy (more->entries, entries + hol->num_entries,
	     more->num_entries * sizeof (struct hol_entry));

      bcopy (hol->short_options, short_options, hol_so_len);

      /* Fix up the short options pointers from HOL.  */
      for (e = entries, left = hol->num_entries; left > 0; e++, left--)
	e->short_options += (short_options - hol->short_options);

      /* Now add the short options from MORE, fixing up its entries too.  */
      so = short_options + hol_so_len;
      more_so = more->short_options;
      for (left = more->num_entries; left > 0; e++, left--)
	{
	  int opts_left;
	  const struct argp_option *opt;

	  e->short_options = so;

	  for (opts_left = e->num, opt = e->opt; opts_left; opt++, opts_left--)
	    {
	      int ch = *more_so;
	      if (oshort (opt) && ch == opt->key)
		/* The next short option in MORE_SO, CH, is from OPT.  */
		{
		  if (! find_char (ch,
				   short_options, short_options + hol_so_len))
		    /* The short option CH isn't shadowed by HOL's options,
		       so add it to the sum.  */
		    *so++ = ch;
		  more_so++;
		}
	    }
	}

      *so = '\0';

      free (hol->entries);
      free (hol->short_options);

      hol->entries = entries;
      hol->num_entries = num_entries;
      hol->short_options = short_options;

      hol_free (more);
    }
}

/* Inserts enough spaces to make sure STREAM is at column COL.  */
static void
indent_to (FILE *stream, unsigned col)
{
  int needed = col - line_wrap_point (stream);
  while (needed > 0)
    putc (' ', stream);
}

/* Print help for ENTRY to STREAM.  *LAST_ENTRY should contain the last entry
   printed before this, or null if it's the first, and if ENTRY is in a
   different group, and *SEP_GROUPS is true, then a blank line will be
   printed before any output.  *SEP_GROUPS is also set to true if a
   user-specified group header is printed.  */
static void
hol_entry_help (struct hol_entry *entry, FILE *stream,
		struct hol_entry **prev_entry, int *sep_groups)
{
  unsigned num;
  int first = 1;		/* True if nothing's been printed so far.  */
  const struct argp_option *real = entry->opt, *opt;
  char *so = entry->short_options;
  int old_lm = line_wrap_set_lmargin (stream, 0);
  int old_wm = line_wrap_set_wmargin (stream, 0);

  /* Inserts a comma if this isn't the first item on the line, and then makes
     sure we're at least to column COL.  Also clears FIRST.  */
  void comma (unsigned col)
    {
      if (first)
	{
	  if (sep_groups && *sep_groups
	      && prev_entry && *prev_entry
	      && entry->group != (*prev_entry)->group)
	    putc ('\n', stream);
	  first = 0;
	}
      else
	fputs (", ", stream);
      indent_to (stream, col);
    }

  /* 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.  */
  void arg (char *req_fmt, char *opt_fmt)
    {
      if (real->arg)
	if (real->flags & OPTION_ARG_OPTIONAL)
	  fprintf (stream, opt_fmt, real->arg);
	else
	  fprintf (stream, req_fmt, real->arg);
    }

  /* First emit short options.  */
  line_wrap_set_wmargin (stream, SHORT_OPT_COL); /* For truly bizarre cases. */
  for (opt = real, num = entry->num; num > 0; opt++, num--)
    if (oshort (opt) && opt->key == *so)
      /* OPT has a valid (non shadowed) short option.  */
      {
	if (ovisible (opt))
	  {
	    comma (SHORT_OPT_COL);
	    putc ('-', stream);
	    putc (*so, stream);
	    arg (" %s", "[%s]");
	  }
	so++;
      }

  /* Now, long options.  */
  line_wrap_set_wmargin (stream, LONG_OPT_COL);
  for (opt = real, num = entry->num; num > 0; opt++, num--)
    if (opt->name && ovisible (opt))
      {
	comma (LONG_OPT_COL);
	fprintf (stream, "--%s", opt->name);
	arg ("=%s", "[=%s]");
      }

  line_wrap_set_lmargin (stream, 0);
  if (first)
    /* Didn't print any switches, what's up?  */
    if (!oshort (real) && !real->name && real->doc)
      /* This is a group header, print it nicely.  */
      {
	if (*real->doc)
	  {
	    if (prev_entry && *prev_entry)
	      putc ('\n', stream); /* Precede with a blank line.  */
	    indent_to (stream, HEADER_COL);
	    line_wrap_set_lmargin (stream, HEADER_COL);
	    line_wrap_set_wmargin (stream, HEADER_COL);
	    fputs (real->doc, stream);
	  }
	if (sep_groups)
	  *sep_groups = 1;	/* Separate subsequent groups. */
      }
    else
      /* Just a totally shadowed option or null header; print nothing.  */
      return;			
  else if (real->doc)
    /* Now the option documentation.  */
    {
      unsigned col = line_wrap_point (stream);
      const char *doc = real->doc;

      line_wrap_set_lmargin (stream, OPT_DOC_COL);
      line_wrap_set_wmargin (stream, OPT_DOC_COL);

      if (col > OPT_DOC_COL + 3)
	putc ('\n', stream);
      else if (col >= OPT_DOC_COL)
	fprintf (stream, "   ");
      else
	indent_to (stream, OPT_DOC_COL);

      fputs (doc, stream);
    }

  line_wrap_set_lmargin (stream, 0); /* Don't follow the nl with spaces. */
  putc ('\n', stream);
  line_wrap_set_lmargin (stream, old_lm);
  line_wrap_set_wmargin (stream, old_wm);

  if (prev_entry)
    *prev_entry = entry;
}

/* Output a long help message about the options in HOL to STREAM.  */
static void
hol_help (struct hol *hol, FILE *stream)
{
  unsigned num;
  struct hol_entry *entry;
  struct hol_entry *last_entry = 0;
  int sep_groups = 0;		/* True if we should separate different
				   sections with blank lines.   */
  for (entry = hol->entries, num = hol->num_entries; num > 0; entry++, num--)
    hol_entry_help (entry, stream, &last_entry, &sep_groups);
}

/* Print a short usage description for the arguments in HOL to STREAM.  */
static void
hol_usage (struct hol *hol, FILE *stream)
{
  int old_wm = line_wrap_set_wmargin (stream, USAGE_INDENT);

  if (hol->num_entries > 0)
    {
      unsigned nentries;
      struct hol_entry *entry;
      char *short_no_arg_opts = alloca (strlen (hol->short_options));
      char *snao_end = short_no_arg_opts;

      /* First we put a list of short options without arguments.  */
      for (entry = hol->entries, nentries = hol->num_entries
	   ; nentries > 0
	   ; entry++, nentries--)
	{
	  inline int func2 (const struct argp_option *opt,
			    const struct argp_option *real)
	    {
	      if (! (opt->arg || real->arg))
		*snao_end++ = opt->key;
	      return 0;
	    }
	  hol_entry_short_iterate (entry, func2);
	}
      if (snao_end > short_no_arg_opts)
	{
	  *snao_end++ = 0;
	  fprintf (stream, "[-%s]", short_no_arg_opts);
	}

      /* Now a list of short options *with* arguments.  */
      for (entry = hol->entries, nentries = hol->num_entries
	   ; nentries > 0
	   ; entry++, nentries--)
	{
	  inline int func3 (const struct argp_option *opt,
			    const struct argp_option *real)
	    {
	      if (opt->arg || real->arg)
		if ((opt->flags | real->flags) & OPTION_ARG_OPTIONAL)
		  fprintf (stream, "[-%c[%s]]",
			   opt->key, opt->arg ?: real->arg);
		else
		  fprintf (stream, "[-%c %s]",
			   opt->key, opt->arg ?: real->arg);
	      return 0;
	    }
	  hol_entry_short_iterate (entry, func3);
	}

      /* Finally, a list of long options (whew!).  */
      for (entry = hol->entries, nentries = hol->num_entries
	   ; nentries > 0
	   ; entry++, nentries--)
	{
	  int func4 (const struct argp_option *opt,
		     const struct argp_option *real)
	    {
	      if (opt->arg || real->arg)
		if ((opt->flags | real->flags) & OPTION_ARG_OPTIONAL)
		  fprintf (stream, "[--%s[=%s]]",
			   opt->name, opt->arg ?: real->arg);
		else
		  fprintf (stream, "[--%s=%s]",
			   opt->name, opt->arg ?: real->arg);
	      else
		fprintf (stream, "[--%s]", opt->name);
	      return 0;
	    }
	  hol_entry_long_iterate (entry, func4);
	}
    }
  line_wrap_set_wmargin (stream, old_wm);
}

/* Make a HOL containing all levels of options in ARGP.  */
static struct hol *
argp_hol (const struct argp *argp)
{
  const struct argp **children = argp->children;
  struct hol *hol = make_hol (argp->options);
  if (children)
    while (*children)
      hol_append (hol, argp_hol (*children++));
  return hol;
}

/* Print all the non-option args documented in ARGP to STREAM.  Any output is
   preceded by a space.  */
static void
argp_args_usage (const struct argp *argp, FILE *stream)
{
  const struct argp **children = argp->children;
  const char *doc = argp->args_doc;
  if (doc)
    fprintf (stream, "%s", doc);
  if (children)
    while (*children)
      argp_args_usage (*children++, stream);
}

/* Print the documentation for ARGP to STREAM.  Each separate bit of
   documentation is preceded by a blank line.  */
static void
argp_doc (const struct argp *argp, FILE *stream)
{
  const struct argp **children = argp->children;
  const char *doc = argp->doc;
  if (doc)
    {
      putc ('\n', stream);
      fputs (doc, stream);
      if (line_wrap_point (stream) > line_wrap_lmargin (stream))
	putc ('\n', stream);
    }
  if (children)
    while (*children)
      argp_doc (*children++, stream);
}

/* Output a usage message for ARGP to STREAM.  FLAGS are from the set
   ARGP_HELP_*.  */
void argp_help (const struct argp *argp, FILE *stream, unsigned flags)
{
  int first = 1;
  struct hol *hol = 0;

  stream = line_wrap_stream (stream, 0, RMARGIN, 0);
  assert (stream);

  if (flags & (ARGP_HELP_USAGE | ARGP_HELP_SHORT_USAGE | ARGP_HELP_LONG))
    {
      hol = argp_hol (argp);

      /* If present, these options always come last.  */
      hol_set_group (hol, "help", -1);
      hol_set_group (hol, "version", -1);

      hol_sort (hol);
    }

  if (flags & (ARGP_HELP_USAGE | ARGP_HELP_SHORT_USAGE))
    /* Print a short `Usage:' message.  */
    {
      fprintf (stream, "Usage: %s", program_invocation_name);
      if (flags & ARGP_HELP_SHORT_USAGE)
	/* Just show where the options go.  */
	{
	  if (hol->num_entries > 0)
	    fputs (" [OPTIONS...]", stream);
	}
      else
	/* Actually print the options.  */
	hol_usage (hol, stream);
      argp_args_usage (argp, stream);
      putc ('\n', stream);
      first = 0;
    }

  if (flags & ARGP_HELP_SEE)
    {
      fprintf (stream, "Try `%s --help' for more information.\n",
	       program_invocation_name);
      first = 0;
    }

  if (flags & ARGP_HELP_LONG)
    /* Print a long, detailed help message.  */
    {
      /* Print info about all the options.  */
      if (hol->num_entries > 0)
	{
	  if (! first)
	    putc ('\n', stream);
	  hol_help (hol, stream);
	  first = 0;
	}

      /* Finally, print any documentation strings at the end.  */
      argp_doc (argp, stream);
    }

  if (hol)
    hol_free (hol);

  line_unwrap_stream (stream);
}

/* Output, if appropriate, a usage message for STATE to STREAM.  FLAGS are
   from the set ARGP_HELP_*.  */
void
argp_state_help (struct argp_state *state, FILE *stream, unsigned flags)
{
  if (!state || ! (state->flags & ARGP_NO_ERRS))
    {
      argp_help (state ? state->argp : 0, stream, flags);

      if (!state || ! (state->flags & ARGP_NO_EXIT))
	{
	  if (flags & ARGP_HELP_EXIT_ERR)
	    exit (1);
	  if (flags & ARGP_HELP_EXIT_OK)
	    exit (0);
	}
  }
}

/* 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
argp_error (struct argp_state *state, const char *fmt, ...)
{
  if (!state || ! (state->flags & ARGP_NO_ERRS))
    {
      va_list ap;

      fputs (program_invocation_name, stderr);
      putc (':', stderr);
      putc (' ', stderr);

      va_start (ap, fmt);
      vfprintf (stderr, fmt, ap);
      va_end (ap);

      putc ('\n', stderr);

      argp_state_help (state, stderr, ARGP_HELP_STD_ERR);
    }
}