summaryrefslogtreecommitdiffstats
path: root/meta/recipes-core/uclibc/uclibc-git/argp-support.patch
diff options
context:
space:
mode:
authorKhem Raj <raj.khem@gmail.com>2011-05-22 12:02:12 -0700
committerRichard Purdie <richard.purdie@linuxfoundation.org>2011-05-23 15:53:28 +0100
commit4ea8b2fde48134dd58a9876c9ddc21b4b51e78f2 (patch)
treef0bdb225cabf12d58369be1514cd641ebf05341d /meta/recipes-core/uclibc/uclibc-git/argp-support.patch
parented6f039bca9bd8da640ba8c76a4751bc818a091b (diff)
downloadpoky-4ea8b2fde48134dd58a9876c9ddc21b4b51e78f2.tar.gz
uclibc: Upgrade to 0.9.32-rc3
Bring in the uclibc recipes from meta-oe they have been well tested by now. Delete 0.9.30.1 recipes (From OE-Core rev: ac60a936e737680c16b287a3dab6aa285d87c5c0) Signed-off-by: Khem Raj <raj.khem@gmail.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/recipes-core/uclibc/uclibc-git/argp-support.patch')
-rw-r--r--meta/recipes-core/uclibc/uclibc-git/argp-support.patch5189
1 files changed, 5189 insertions, 0 deletions
diff --git a/meta/recipes-core/uclibc/uclibc-git/argp-support.patch b/meta/recipes-core/uclibc/uclibc-git/argp-support.patch
new file mode 100644
index 0000000000..04a3c7824c
--- /dev/null
+++ b/meta/recipes-core/uclibc/uclibc-git/argp-support.patch
@@ -0,0 +1,5189 @@
1From: Salvatore Cro <salvatore.cro at st.com>
2
3Argp is an advanced support for parsing unix-style argument vectors.
4In addition to the common getopt interface, it provides automatic response
5to `--help' and `--version' options and use of custom parser in conjunction
6with argp native option parser, among others.
7Argp support is required by elfutils package and prelink.
8
9In uClibc argp functionalities has been moved from C library to libuargp.so
10Further the libc.so linker script contains an AS_NEEDED entry so that
11it doesn't need to link libuargp.so explicitely.
12
13Signed-off-by: Salvatore Cro <salvatore.cro at st.com>
14Signed-off-by: Filippo Arcidiacono <filippo.arcidiacono at st.com>
15Signed-off-by: Carmelo Amoroso <carmelo.amoroso at st.com>
16---
17 Makefile.in | 8 +
18 Makerules | 8 +-
19 Rules.mak | 7 +
20 extra/Configs/Config.in | 17 +
21 libc/sysdeps/linux/common/bits/getopt_int.h | 136 ++
22 libc/unistd/getopt.c | 18 +-
23 libc/unistd/getopt_int.h | 134 --
24 libuargp/Makefile | 14 +
25 libuargp/Makefile.in | 76 ++
26 libuargp/argp-ba.c | 26 +
27 libuargp/argp-eexst.c | 32 +
28 libuargp/argp-fmtstream.c | 439 +++++++
29 libuargp/argp-fmtstream.h | 314 +++++
30 libuargp/argp-fs-xinl.c | 44 +
31 libuargp/argp-help.c | 1882 +++++++++++++++++++++++++++
32 libuargp/argp-parse.c | 949 ++++++++++++++
33 libuargp/argp-pv.c | 25 +
34 libuargp/argp-pvh.c | 32 +
35 libuargp/argp-xinl.c | 35 +
36 test/argp/Makefile | 7 +
37 test/argp/Makefile.in | 12 +
38 test/argp/argp-ex1.c | 15 +
39 test/argp/argp-ex2.c | 45 +
40 test/argp/argp-ex3.c | 153 +++
41 test/argp/argp-ex4.c | 167 +++
42 test/argp/argp-test.c | 209 +++
43 test/argp/bug-argp1.c | 26 +
44 test/argp/tst-argp1.c | 118 ++
45 test/argp/tst-argp2.c | 101 ++
46 29 files changed, 4911 insertions(+), 138 deletions(-)
47 create mode 100644 libc/sysdeps/linux/common/bits/getopt_int.h
48 delete mode 100644 libc/unistd/getopt_int.h
49 create mode 100644 libuargp/Makefile
50 create mode 100644 libuargp/Makefile.in
51 create mode 100644 libuargp/argp-ba.c
52 create mode 100644 libuargp/argp-eexst.c
53 create mode 100644 libuargp/argp-fmtstream.c
54 create mode 100644 libuargp/argp-fmtstream.h
55 create mode 100644 libuargp/argp-fs-xinl.c
56 create mode 100644 libuargp/argp-help.c
57 create mode 100644 libuargp/argp-parse.c
58 create mode 100644 libuargp/argp-pv.c
59 create mode 100644 libuargp/argp-pvh.c
60 create mode 100644 libuargp/argp-xinl.c
61 create mode 100644 test/argp/Makefile
62 create mode 100644 test/argp/Makefile.in
63 create mode 100644 test/argp/argp-ex1.c
64 create mode 100644 test/argp/argp-ex2.c
65 create mode 100644 test/argp/argp-ex3.c
66 create mode 100644 test/argp/argp-ex4.c
67 create mode 100644 test/argp/argp-test.c
68 create mode 100644 test/argp/bug-argp1.c
69 create mode 100644 test/argp/tst-argp1.c
70 create mode 100644 test/argp/tst-argp2.c
71
72Index: git/Makefile.in
73===================================================================
74--- git.orig/Makefile.in
75+++ git/Makefile.in
76@@ -48,6 +48,7 @@ include $(top_srcdir)libresolv/Makefile.
77 include $(top_srcdir)libutil/Makefile.in
78 include $(top_srcdir)libpthread/Makefile.in
79 include $(top_srcdir)librt/Makefile.in
80+include $(top_srcdir)libuargp/Makefile.in
81 include $(top_srcdir)libubacktrace/Makefile.in
82 include $(top_srcdir)extra/locale/Makefile.in
83
84@@ -230,6 +231,7 @@ HEADERS_RM- += sgtty.h
85 endif
86 HEADERS_RM-$(HAVE_SHARED) += dlfcn.h bits/dlfcn.h
87 HEADERS_RM-$(PTHREADS_DEBUG_SUPPORT) += thread_db.h
88+HEADERS_RM-$(UCLIBC_HAS_ARGP) += argp.h
89 HEADERS_RM-$(UCLIBC_HAS_BSD_ERR) += err.h
90 HEADERS_RM-$(UCLIBC_HAS_CRYPT) += crypt.h
91 HEADERS_RM-$(UCLIBC_HAS_EPOLL) += sys/epoll.h
92@@ -340,6 +342,12 @@ ifeq ($(UCLIBC_HAS_BACKTRACE),y)
93 echo "GROUP ( $(UBACKTRACE_ASNEEDED) )" >> $(PREFIX)$(DEVEL_PREFIX)$(MULTILIB_DIR)/libc.so; \
94 fi
95 endif
96+ifeq ($(UCLIBC_HAS_ARGP),y)
97+# Add the AS_NEEDED entry for libuargp.so
98+ if [ -f $(top_builddir)lib/libc.so -a -f $(PREFIX)$(RUNTIME_PREFIX)lib/$(SHARED_MAJORNAME) ] ; then \
99+ echo "GROUP ( $(UARGP_ASNEEDED) )" >> $(PREFIX)$(DEVEL_PREFIX)lib/libc.so; \
100+ fi
101+endif
102 ifeq ($(UCLIBC_HAS_THREADS),y)
103 ifneq ($(LINUXTHREADS_OLD),y)
104 ifeq ($(HARDWIRED_ABSPATH),y)
105Index: git/Makerules
106===================================================================
107--- git.orig/Makerules
108+++ git/Makerules
109@@ -32,12 +32,12 @@ shared_objs = $(libc-y:.o=.os) $(libc-sh
110 $(libpthread-so-y) $(libpthread-nonshared-y) $(libthread_db-so-y) \
111 $(libresolv-so-y) $(librt-so-y) \
112 $(ldso-y) \
113- $(libutil-so-y) $(libubacktrace-so-y)
114+ $(libutil-so-y) $(libubacktrace-so-y) $(libuargp-so-y)
115
116 ar_objs = $(libc-y) $(libc-static-y) $(libcrypt-a-y) \
117 $(libdl-a-y) $(libintl-a-y) $(libm-a-y) $(libnsl-a-y) \
118 $(libpthread-a-y) $(libthread_db-a-y) \
119- $(libresolv-a-y) $(librt-a-y) $(libutil-a-y) $(libubacktrace-a-y)
120+ $(libresolv-a-y) $(librt-a-y) $(libutil-a-y) $(libubacktrace-a-y) $(libuargp-a-y)
121 ifeq ($(DOPIC),y)
122 ar_objs := $(ar_objs:.o=.os)
123 endif
124@@ -464,7 +464,7 @@ files.dep := $(libc-a-y) $(libc-so-y) $(
125 $(librt-a-y) $(librt-so-y) $(libresolv-a-y) $(libresolv-so-y) \
126 $(libcrypt-a-y) $(libcrypt-so-y) $(libutil-a-y) $(libutil-so-y) \
127 $(libnsl-a-y) $(libnsl-so-y) $(ldso-y) $(libdl-a-y) $(libdl-so-y) \
128- $(libubacktrace-a-y) $(libubacktrace-so-y)
129+ $(libubacktrace-a-y) $(libubacktrace-so-y) $(libuargp-so-y) $(libuargp-a-y)
130 .depends.dep := \
131 $(patsubst %.s,%.s.dep,$(filter %.s,$(files.dep))) \
132 $(patsubst %.o,%.o.dep,$(filter %.o,$(files.dep))) \
133Index: git/Rules.mak
134===================================================================
135--- git.orig/Rules.mak
136+++ git/Rules.mak
137@@ -525,6 +525,13 @@ export UBACKTRACE_ASNEEDED:=$(shell $(LD
138 else
139 export UBACKTRACE_ASNEEDED:=""
140 endif
141+ifeq ($(UCLIBC_HAS_ARGP),y)
142+# Only used in installed libc.so linker script
143+UARGP_FULL_NAME := $(RUNTIME_PREFIX)lib/libuargp.so.$(MAJOR_VERSION)
144+export UARGP_ASNEEDED:=$(shell $(LD) --help 2>/dev/null | grep -q -- --as-needed && echo "AS_NEEDED ( $(UARGP_FULL_NAME) )" || echo "$(UARGP_FULL_NAME)")
145+else
146+export UARGP_ASNEEDED:=""
147+endif
148 endif
149
150 # Add a bunch of extra pedantic annoyingly strict checks
151Index: git/extra/Configs/Config.in
152===================================================================
153--- git.orig/extra/Configs/Config.in
154+++ git/extra/Configs/Config.in
155@@ -1814,6 +1814,23 @@ config UCLIBC_HAS_GNU_GETSUBOPT
156 smaller SUSv3 compatible getsubopt().
157
158 Most people will answer Y.
159+
160+config UCLIBC_HAS_ARGP
161+ bool "Support argp (as standalone shared object)"
162+ default n
163+ help
164+ Argp is an interface for parsing unix-style argument vectors. Unlike
165+ the common getopt interface, it provides many advanced features in
166+ addition to parsing options, such as automatic output in response to
167+ `--help' and `--version' options.
168+ A library can export an argp option parser, which programs can easily
169+ use in conjunction with their own option parser.
170+ A new shared object "libuargp" is created. The "libc.so" linker script
171+ contains the AS_NEEDED entry for getting the libuargp linked automatically.
172+ Argp support is needed by elfutils libdw.
173+
174+ Most people can safely answer N.
175+
176 endmenu
177
178
179Index: git/libc/sysdeps/linux/common/bits/getopt_int.h
180===================================================================
181--- /dev/null
182+++ git/libc/sysdeps/linux/common/bits/getopt_int.h
183@@ -0,0 +1,136 @@
184+/* Internal declarations for getopt.
185+ Copyright (C) 1989-1994,1996-1999,2001,2003,2004
186+ Free Software Foundation, Inc.
187+ This file is part of the GNU C Library.
188+
189+ The GNU C Library is free software; you can redistribute it and/or
190+ modify it under the terms of the GNU Lesser General Public
191+ License as published by the Free Software Foundation; either
192+ version 2.1 of the License, or (at your option) any later version.
193+
194+ The GNU C Library is distributed in the hope that it will be useful,
195+ but WITHOUT ANY WARRANTY; without even the implied warranty of
196+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
197+ Lesser General Public License for more details.
198+
199+ You should have received a copy of the GNU Lesser General Public
200+ License along with the GNU C Library; if not, write to the Free
201+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
202+ 02111-1307 USA. */
203+
204+#ifndef _GETOPT_INT_H
205+#define _GETOPT_INT_H 1
206+
207+extern int _getopt_internal (int ___argc, char *const *___argv,
208+ const char *__shortopts,
209+ const struct option *__longopts, int *__longind,
210+ int __long_only) attribute_hidden;
211+
212+
213+/* Reentrant versions which can handle parsing multiple argument
214+ vectors at the same time. */
215+
216+/* For __ordering member */
217+enum {
218+ REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
219+};
220+
221+/* Data type for reentrant functions. */
222+
223+struct _getopt_data
224+{
225+ /* These have exactly the same meaning as the corresponding global
226+ variables, except that they are used for the reentrant
227+ versions of getopt. */
228+ int optind;
229+ int opterr;
230+ char *optarg;
231+ smalluint optopt; /* we store characters here, a byte is enough */
232+
233+ /* Internal members. */
234+
235+ /* True if the internal members have been initialized. */
236+ smallint __initialized;
237+
238+ /* Describe how to deal with options that follow non-option ARGV-elements.
239+
240+ If the caller did not specify anything,
241+ the default is REQUIRE_ORDER if the environment variable
242+ POSIXLY_CORRECT is defined, PERMUTE otherwise.
243+
244+ REQUIRE_ORDER means don't recognize them as options;
245+ stop option processing when the first non-option is seen.
246+ This is what Unix does.
247+ This mode of operation is selected by either setting the environment
248+ variable POSIXLY_CORRECT, or using `+' as the first character
249+ of the list of option characters.
250+
251+ PERMUTE is the default. We permute the contents of ARGV as we
252+ scan, so that eventually all the non-options are at the end.
253+ This allows options to be given in any order, even with programs
254+ that were not written to expect this.
255+
256+ RETURN_IN_ORDER is an option available to programs that were
257+ written to expect options and other ARGV-elements in any order
258+ and that care about the ordering of the two. We describe each
259+ non-option ARGV-element as if it were the argument of an option
260+ with character code 1. Using `-' as the first character of the
261+ list of option characters selects this mode of operation.
262+
263+ The special argument `--' forces an end of option-scanning regardless
264+ of the value of `ordering'. In the case of RETURN_IN_ORDER, only
265+ `--' can cause `getopt' to return -1 with `optind' != ARGC. */
266+ smallint __ordering;
267+
268+ /* If the POSIXLY_CORRECT environment variable is set. */
269+ smallint __posixly_correct;
270+
271+ /* The next char to be scanned in the option-element
272+ in which the last option character we returned was found.
273+ This allows us to pick up the scan where we left off.
274+
275+ If this is zero, or a null string, it means resume the scan
276+ by advancing to the next ARGV-element. */
277+ char *__nextchar;
278+
279+
280+ /* Handle permutation of arguments. */
281+
282+ /* Describe the part of ARGV that contains non-options that have
283+ been skipped. `first_nonopt' is the index in ARGV of the first
284+ of them; `last_nonopt' is the index after the last of them. */
285+
286+ int __first_nonopt;
287+ int __last_nonopt;
288+
289+#if defined _LIBC && defined USE_NONOPTION_FLAGS
290+ int __nonoption_flags_max_len;
291+ int __nonoption_flags_len;
292+# endif
293+};
294+
295+/* The initializer is necessary to set OPTIND and OPTERR to their
296+ default values and to clear the initialization flag. */
297+#define _GETOPT_DATA_INITIALIZER { 1, 1 }
298+
299+#if 0 /* first is static on uClibc, the others not used */
300+extern int _getopt_internal_r (int ___argc, char *const *___argv,
301+ const char *__shortopts,
302+ const struct option *__longopts, int *__longind,
303+ int __long_only, struct _getopt_data *__data);
304+#endif
305+#if defined __UCLIBC_HAS_GNU_GETOPT__ || defined __UCLIBC_HAS_GETOPT_LONG__
306+#ifndef __need_getopt
307+extern int _getopt_long_r (int ___argc, char *const *___argv,
308+ const char *__shortopts,
309+ const struct option *__longopts, int *__longind,
310+ struct _getopt_data *__data);
311+
312+extern int _getopt_long_only_r (int ___argc, char *const *___argv,
313+ const char *__shortopts,
314+ const struct option *__longopts,
315+ int *__longind,
316+ struct _getopt_data *__data);
317+#endif
318+#endif
319+#endif /* getopt_int.h */
320Index: git/libc/unistd/getopt.c
321===================================================================
322--- git.orig/libc/unistd/getopt.c
323+++ git/libc/unistd/getopt.c
324@@ -119,7 +119,7 @@
325 they can distinguish the relative order of options and other arguments. */
326
327 #include <getopt.h>
328-#include "getopt_int.h"
329+#include <bits/getopt_int.h>
330
331
332 /* For communication from `getopt' to the caller.
333@@ -1183,6 +1183,15 @@ getopt_long (int argc, char *const *argv
334 return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
335 }
336
337+int
338+_getopt_long_r (int argc, char *const *argv, const char *options,
339+ const struct option *long_options, int *opt_index,
340+ struct _getopt_data *d)
341+{
342+ return _getopt_internal_r (argc, argv, options, long_options, opt_index,
343+ 0, d);
344+}
345+
346 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
347 If an option that starts with '-' (not '--') doesn't match a long option,
348 but does match a short option, it is parsed as a short option
349@@ -1195,4 +1204,12 @@ getopt_long_only (int argc, char *const
350 return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
351 }
352
353+int
354+_getopt_long_only_r (int argc, char *const *argv, const char *options,
355+ const struct option *long_options, int *opt_index,
356+ struct _getopt_data *d)
357+{
358+ return _getopt_internal_r (argc, argv, options, long_options, opt_index, 1, d);
359+}
360+
361 #endif /* Not ELIDE_CODE. */
362Index: git/libuargp/Makefile
363===================================================================
364--- /dev/null
365+++ git/libuargp/Makefile
366@@ -0,0 +1,14 @@
367+# Makefile for uClibc (libuargp)
368+#
369+# Copyright (C) 2010 STMicroelectronics Ltd
370+# Author(s): Filippo Arcidiacono <filippo.arcidiacono at st.com>
371+#
372+# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
373+#
374+
375+top_srcdir=../
376+top_builddir=../
377+include $(top_builddir)Rules.mak
378+all: libs
379+include Makefile.in
380+include $(top_srcdir)Makerules
381Index: git/libuargp/Makefile.in
382===================================================================
383--- /dev/null
384+++ git/libuargp/Makefile.in
385@@ -0,0 +1,73 @@
386+# Makefile for uClibc (libuargp)
387+#
388+# Copyright (C) 2009, 2010 STMicroelectronics Ltd.
389+# Author(s): Salvatore Cro <salvatore.cro at st.com>
390+# - First implementation, embedded into libc
391+# Filippo Arcidiacono <filippo.arcidiacono at st.com>
392+# - Reworked for stand-alone libuargp implementation
393+
394+# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
395+#
396+
397+CFLAGS-libuargp := -DNOT_IN_libc -DIS_IN_libuargp $(SSP_ALL_CFLAGS)
398+
399+LDFLAGS-libuargp.so := $(LDFLAGS)
400+
401+LIBS-libuargp.so := $(LIBS)
402+
403+libuargp_FULL_NAME := libuargp-$(VERSION).so
404+
405+libuargp_DIR := $(top_srcdir)libuargp
406+libuargp_OUT := $(top_builddir)libuargp
407+
408+libuargp_SRC-y :=
409+libuargp_SRC-$(UCLIBC_HAS_ARGP) := $(addsuffix .c,$(addprefix argp-, ba \
410+ eexst fmtstream fs-xinl help parse pv pvh xinl))
411+
412+CFLAGS-argp-xinl.c = -fgnu89-inline
413+
414+libuargp_SRC := $(addprefix $(libuargp_DIR)/,$(libuargp_SRC-y))
415+libuargp_OBJ := $(patsubst $(libuargp_DIR)/%.c,$(libuargp_OUT)/%.o,$(libuargp_SRC))
416+
417+libuargp_SRCS := $(libuargp_SRC)
418+libuargp_OBJS := $(libuargp_OBJ)
419+
420+ifeq ($(DOPIC),y)
421+libuargp-a-y := $(libuargp_OBJS:.o=.os)
422+else
423+libuargp-a-y := $(libuargp_OBJS)
424+endif
425+libuargp-so-y := $(libuargp_OBJS:.o=.os)
426+
427+lib-a-$(UCLIBC_HAS_ARGP) += $(top_builddir)lib/libuargp.a
428+lib-so-$(UCLIBC_HAS_ARGP) += $(top_builddir)lib/libuargp.so
429+
430+objclean-y += CLEAN_libuargp
431+
432+ifeq ($(DOMULTI),n)
433+ifeq ($(DOPIC),y)
434+$(top_builddir)lib/libuargp.so: $(top_builddir)lib/libuargp.a $(libc.depend)
435+else
436+$(top_builddir)lib/libuargp.so: $(libuargp_OUT)/libuargp_so.a $(libc.depend)
437+endif
438+ $(call link.so,$(libuargp_FULL_NAME),$(MAJOR_VERSION))
439+else
440+$(top_builddir)lib/libuargp.so: $(libuargp_OUT)/libuargp.oS $(libc.depend)
441+ $(call linkm.so,$(libuargp_FULL_NAME),$(MAJOR_VERSION))
442+endif
443+
444+$(libuargp_OUT)/libuargp_so.a: $(libuargp-so-y)
445+ $(Q)$(RM) $@
446+ $(do_ar)
447+
448+$(libuargp_OUT)/libuargp.oS: $(libuargp_SRCS)
449+ $(Q)$(RM) $@
450+ $(compile-m)
451+
452+$(top_builddir)lib/libuargp.a: $(libuargp-a-y)
453+ $(Q)$(INSTALL) -d $(dir $@)
454+ $(Q)$(RM) $@
455+ $(do_ar)
456+
457+CLEAN_libuargp:
458+ $(do_rm) $(addprefix $(libuargp_OUT)/*., o os oS a)
459Index: git/libuargp/argp-ba.c
460===================================================================
461--- /dev/null
462+++ git/libuargp/argp-ba.c
463@@ -0,0 +1,26 @@
464+/* Default definition for ARGP_PROGRAM_BUG_ADDRESS.
465+ Copyright (C) 1996, 1997, 1999 Free Software Foundation, Inc.
466+ This file is part of the GNU C Library.
467+ Written by Miles Bader <miles at gnu.ai.mit.edu>.
468+
469+ The GNU C Library is free software; you can redistribute it and/or
470+ modify it under the terms of the GNU Lesser General Public
471+ License as published by the Free Software Foundation; either
472+ version 2.1 of the License, or (at your option) any later version.
473+
474+ The GNU C Library is distributed in the hope that it will be useful,
475+ but WITHOUT ANY WARRANTY; without even the implied warranty of
476+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
477+ Lesser General Public License for more details.
478+
479+ You should have received a copy of the GNU Lesser General Public
480+ License along with the GNU C Library; if not, write to the Free
481+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
482+ 02111-1307 USA. */
483+
484+/* If set by the user program, it should point to string that is the
485+ bug-reporting address for the program. It will be printed by argp_help if
486+ the ARGP_HELP_BUG_ADDR flag is set (as it is by various standard help
487+ messages), embedded in a sentence that says something like `Report bugs to
488+ ADDR.'. */
489+const char *argp_program_bug_address;
490Index: git/libuargp/argp-eexst.c
491===================================================================
492--- /dev/null
493+++ git/libuargp/argp-eexst.c
494@@ -0,0 +1,32 @@
495+/* Default definition for ARGP_ERR_EXIT_STATUS
496+ Copyright (C) 1997 Free Software Foundation, Inc.
497+ This file is part of the GNU C Library.
498+ Written by Miles Bader <miles at gnu.ai.mit.edu>.
499+
500+ The GNU C Library is free software; you can redistribute it and/or
501+ modify it under the terms of the GNU Lesser General Public
502+ License as published by the Free Software Foundation; either
503+ version 2.1 of the License, or (at your option) any later version.
504+
505+ The GNU C Library is distributed in the hope that it will be useful,
506+ but WITHOUT ANY WARRANTY; without even the implied warranty of
507+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
508+ Lesser General Public License for more details.
509+
510+ You should have received a copy of the GNU Lesser General Public
511+ License along with the GNU C Library; if not, write to the Free
512+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
513+ 02111-1307 USA. */
514+
515+#ifdef HAVE_CONFIG_H
516+# include <config.h>
517+#endif
518+
519+#include <sysexits.h>
520+
521+#include <argp.h>
522+
523+/* The exit status that argp will use when exiting due to a parsing error.
524+ If not defined or set by the user program, this defaults to EX_USAGE from
525+ <sysexits.h>. */
526+error_t argp_err_exit_status = EX_USAGE;
527Index: git/libuargp/argp-fmtstream.c
528===================================================================
529--- /dev/null
530+++ git/libuargp/argp-fmtstream.c
531@@ -0,0 +1,439 @@
532+/* Word-wrapping and line-truncating streams
533+ Copyright (C) 1997-1999,2001,2002,2003,2005 Free Software Foundation, Inc.
534+ This file is part of the GNU C Library.
535+ Written by Miles Bader <miles at gnu.ai.mit.edu>.
536+
537+ The GNU C Library is free software; you can redistribute it and/or
538+ modify it under the terms of the GNU Lesser General Public
539+ License as published by the Free Software Foundation; either
540+ version 2.1 of the License, or (at your option) any later version.
541+
542+ The GNU C Library is distributed in the hope that it will be useful,
543+ but WITHOUT ANY WARRANTY; without even the implied warranty of
544+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
545+ Lesser General Public License for more details.
546+
547+ You should have received a copy of the GNU Lesser General Public
548+ License along with the GNU C Library; if not, write to the Free
549+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
550+ 02111-1307 USA.
551+
552+ Modified for uClibc by: Salvatore Cro <salvatore.cro at st.com>
553+*/
554+
555+/* This package emulates glibc `line_wrap_stream' semantics for systems that
556+ don't have that. */
557+
558+#ifdef HAVE_CONFIG_H
559+# include <config.h>
560+#endif
561+
562+#include <stdlib.h>
563+#include <string.h>
564+#include <errno.h>
565+#include <stdarg.h>
566+#include <ctype.h>
567+
568+#include "argp-fmtstream.h"
569+
570+#ifndef ARGP_FMTSTREAM_USE_LINEWRAP
571+
572+#ifndef isblank
573+#define isblank(ch) ((ch)==' ' || (ch)=='\t')
574+#endif
575+
576+#if defined _LIBC && defined USE_IN_LIBIO
577+# include <wchar.h>
578+# include <libio/libioP.h>
579+# define __vsnprintf(s, l, f, a) _IO_vsnprintf (s, l, f, a)
580+#else
581+# define __vsnprintf(s, l, f, a) vsnprintf (s, l, f, a)
582+#endif
583+
584+#define INIT_BUF_SIZE 200
585+#define PRINTF_SIZE_GUESS 150
586+
587+/* Return an argp_fmtstream that outputs to STREAM, and which prefixes lines
588+ written on it with LMARGIN spaces and limits them to RMARGIN columns
589+ total. If WMARGIN >= 0, words that extend past RMARGIN are wrapped by
590+ replacing the whitespace before them with a newline and WMARGIN spaces.
591+ Otherwise, chars beyond RMARGIN are simply dropped until a newline.
592+ Returns NULL if there was an error. */
593+argp_fmtstream_t
594+__argp_make_fmtstream (FILE *stream,
595+ size_t lmargin, size_t rmargin, ssize_t wmargin)
596+{
597+ argp_fmtstream_t fs;
598+
599+ fs = (struct argp_fmtstream *) malloc (sizeof (struct argp_fmtstream));
600+ if (fs != NULL)
601+ {
602+ fs->stream = stream;
603+
604+ fs->lmargin = lmargin;
605+ fs->rmargin = rmargin;
606+ fs->wmargin = wmargin;
607+ fs->point_col = 0;
608+ fs->point_offs = 0;
609+
610+ fs->buf = (char *) malloc (INIT_BUF_SIZE);
611+ if (! fs->buf)
612+ {
613+ free (fs);
614+ fs = 0;
615+ }
616+ else
617+ {
618+ fs->p = fs->buf;
619+ fs->end = fs->buf + INIT_BUF_SIZE;
620+ }
621+ }
622+
623+ return fs;
624+}
625+#if 0
626+/* Not exported. */
627+#ifdef weak_alias
628+weak_alias (__argp_make_fmtstream, argp_make_fmtstream)
629+#endif
630+#endif
631+
632+/* Flush FS to its stream, and free it (but don't close the stream). */
633+void
634+__argp_fmtstream_free (argp_fmtstream_t fs)
635+{
636+ __argp_fmtstream_update (fs);
637+ if (fs->p > fs->buf)
638+ {
639+#ifdef USE_IN_LIBIO
640+ __fxprintf (fs->stream, "%.*s", (int) (fs->p - fs->buf), fs->buf);
641+#else
642+ fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
643+#endif
644+ }
645+ free (fs->buf);
646+ free (fs);
647+}
648+#if 0
649+/* Not exported. */
650+#ifdef weak_alias
651+weak_alias (__argp_fmtstream_free, argp_fmtstream_free)
652+#endif
653+#endif
654+
655+/* Process FS's buffer so that line wrapping is done from POINT_OFFS to the
656+ end of its buffer. This code is mostly from glibc stdio/linewrap.c. */
657+void
658+__argp_fmtstream_update (argp_fmtstream_t fs)
659+{
660+ char *buf, *nl;
661+ size_t len;
662+
663+ /* Scan the buffer for newlines. */
664+ buf = fs->buf + fs->point_offs;
665+ while (buf < fs->p)
666+ {
667+ size_t r;
668+
669+ if (fs->point_col == 0 && fs->lmargin != 0)
670+ {
671+ /* We are starting a new line. Print spaces to the left margin. */
672+ const size_t pad = fs->lmargin;
673+ if (fs->p + pad < fs->end)
674+ {
675+ /* We can fit in them in the buffer by moving the
676+ buffer text up and filling in the beginning. */
677+ memmove (buf + pad, buf, fs->p - buf);
678+ fs->p += pad; /* Compensate for bigger buffer. */
679+ memset (buf, ' ', pad); /* Fill in the spaces. */
680+ buf += pad; /* Don't bother searching them. */
681+ }
682+ else
683+ {
684+ /* No buffer space for spaces. Must flush. */
685+ size_t i;
686+ for (i = 0; i < pad; i++)
687+ {
688+#ifdef USE_IN_LIBIO
689+ if (_IO_fwide (fs->stream, 0) > 0)
690+ putwc_unlocked (L' ', fs->stream);
691+ else
692+#endif
693+ putc_unlocked (' ', fs->stream);
694+ }
695+ }
696+ fs->point_col = pad;
697+ }
698+
699+ len = fs->p - buf;
700+ nl = memchr (buf, '\n', len);
701+
702+ if (fs->point_col < 0)
703+ fs->point_col = 0;
704+
705+ if (!nl)
706+ {
707+ /* The buffer ends in a partial line. */
708+
709+ if (fs->point_col + len < fs->rmargin)
710+ {
711+ /* The remaining buffer text is a partial line and fits
712+ within the maximum line width. Advance point for the
713+ characters to be written and stop scanning. */
714+ fs->point_col += len;
715+ break;
716+ }
717+ else
718+ /* Set the end-of-line pointer for the code below to
719+ the end of the buffer. */
720+ nl = fs->p;
721+ }
722+ else if (fs->point_col + (nl - buf) < (ssize_t) fs->rmargin)
723+ {
724+ /* The buffer contains a full line that fits within the maximum
725+ line width. Reset point and scan the next line. */
726+ fs->point_col = 0;
727+ buf = nl + 1;
728+ continue;
729+ }
730+
731+ /* This line is too long. */
732+ r = fs->rmargin - 1;
733+
734+ if (fs->wmargin < 0)
735+ {
736+ /* Truncate the line by overwriting the excess with the
737+ newline and anything after it in the buffer. */
738+ if (nl < fs->p)
739+ {
740+ memmove (buf + (r - fs->point_col), nl, fs->p - nl);
741+ fs->p -= buf + (r - fs->point_col) - nl;
742+ /* Reset point for the next line and start scanning it. */
743+ fs->point_col = 0;
744+ buf += r + 1; /* Skip full line plus \n. */
745+ }
746+ else
747+ {
748+ /* The buffer ends with a partial line that is beyond the
749+ maximum line width. Advance point for the characters
750+ written, and discard those past the max from the buffer. */
751+ fs->point_col += len;
752+ fs->p -= fs->point_col - r;
753+ break;
754+ }
755+ }
756+ else
757+ {
758+ /* Do word wrap. Go to the column just past the maximum line
759+ width and scan back for the beginning of the word there.
760+ Then insert a line break. */
761+
762+ char *p, *nextline;
763+ int i;
764+
765+ p = buf + (r + 1 - fs->point_col);
766+ while (p >= buf && !isblank (*p))
767+ --p;
768+ nextline = p + 1; /* This will begin the next line. */
769+
770+ if (nextline > buf)
771+ {
772+ /* Swallow separating blanks. */
773+ if (p >= buf)
774+ do
775+ --p;
776+ while (p >= buf && isblank (*p));
777+ nl = p + 1; /* The newline will replace the first blank. */
778+ }
779+ else
780+ {
781+ /* A single word that is greater than the maximum line width.
782+ Oh well. Put it on an overlong line by itself. */
783+ p = buf + (r + 1 - fs->point_col);
784+ /* Find the end of the long word. */
785+ do
786+ ++p;
787+ while (p < nl && !isblank (*p));
788+ if (p == nl)
789+ {
790+ /* It already ends a line. No fussing required. */
791+ fs->point_col = 0;
792+ buf = nl + 1;
793+ continue;
794+ }
795+ /* We will move the newline to replace the first blank. */
796+ nl = p;
797+ /* Swallow separating blanks. */
798+ do
799+ ++p;
800+ while (isblank (*p));
801+ /* The next line will start here. */
802+ nextline = p;
803+ }
804+
805+ /* Note: There are a bunch of tests below for
806+ NEXTLINE == BUF + LEN + 1; this case is where NL happens to fall
807+ at the end of the buffer, and NEXTLINE is in fact empty (and so
808+ we need not be careful to maintain its contents). */
809+
810+ if ((nextline == buf + len + 1
811+ ? fs->end - nl < fs->wmargin + 1
812+ : nextline - (nl + 1) < fs->wmargin)
813+ && fs->p > nextline)
814+ {
815+ /* The margin needs more blanks than we removed. */
816+ if (fs->end - fs->p > fs->wmargin + 1)
817+ /* Make some space for them. */
818+ {
819+ size_t mv = fs->p - nextline;
820+ memmove (nl + 1 + fs->wmargin, nextline, mv);
821+ nextline = nl + 1 + fs->wmargin;
822+ len = nextline + mv - buf;
823+ *nl++ = '\n';
824+ }
825+ else
826+ /* Output the first line so we can use the space. */
827+ {
828+#if defined _LIBC && defined USE_IN_LIBIO
829+ __fxprintf (fs->stream, "%.*s\n",
830+ (int) (nl - fs->buf), fs->buf);
831+#else
832+ if (nl > fs->buf)
833+ fwrite_unlocked (fs->buf, 1, nl - fs->buf, fs->stream);
834+ putc_unlocked ('\n', fs->stream);
835+#endif
836+
837+ len += buf - fs->buf;
838+ nl = buf = fs->buf;
839+ }
840+ }
841+ else
842+ /* We can fit the newline and blanks in before
843+ the next word. */
844+ *nl++ = '\n';
845+
846+ if (nextline - nl >= fs->wmargin
847+ || (nextline == buf + len + 1 && fs->end - nextline >= fs->wmargin))
848+ /* Add blanks up to the wrap margin column. */
849+ for (i = 0; i < fs->wmargin; ++i)
850+ *nl++ = ' ';
851+ else
852+ for (i = 0; i < fs->wmargin; ++i)
853+#ifdef USE_IN_LIBIO
854+ if (_IO_fwide (fs->stream, 0) > 0)
855+ putwc_unlocked (L' ', fs->stream);
856+ else
857+#endif
858+ putc_unlocked (' ', fs->stream);
859+
860+ /* Copy the tail of the original buffer into the current buffer
861+ position. */
862+ if (nl < nextline)
863+ memmove (nl, nextline, buf + len - nextline);
864+ len -= nextline - buf;
865+
866+ /* Continue the scan on the remaining lines in the buffer. */
867+ buf = nl;
868+
869+ /* Restore bufp to include all the remaining text. */
870+ fs->p = nl + len;
871+
872+ /* Reset the counter of what has been output this line. If wmargin
873+ is 0, we want to avoid the lmargin getting added, so we set
874+ point_col to a magic value of -1 in that case. */
875+ fs->point_col = fs->wmargin ? fs->wmargin : -1;
876+ }
877+ }
878+
879+ /* Remember that we've scanned as far as the end of the buffer. */
880+ fs->point_offs = fs->p - fs->buf;
881+}
882+
883+/* Ensure that FS has space for AMOUNT more bytes in its buffer, either by
884+ growing the buffer, or by flushing it. True is returned iff we succeed. */
885+int
886+__argp_fmtstream_ensure (struct argp_fmtstream *fs, size_t amount)
887+{
888+ if ((size_t) (fs->end - fs->p) < amount)
889+ {
890+ ssize_t wrote;
891+
892+ /* Flush FS's buffer. */
893+ __argp_fmtstream_update (fs);
894+
895+#if defined _LIBC && defined USE_IN_LIBIO
896+ __fxprintf (fs->stream, "%.*s", (int) (fs->p - fs->buf), fs->buf);
897+ wrote = fs->p - fs->buf;
898+#else
899+ wrote = fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
900+#endif
901+ if (wrote == fs->p - fs->buf)
902+ {
903+ fs->p = fs->buf;
904+ fs->point_offs = 0;
905+ }
906+ else
907+ {
908+ fs->p -= wrote;
909+ fs->point_offs -= wrote;
910+ memmove (fs->buf, fs->buf + wrote, fs->p - fs->buf);
911+ return 0;
912+ }
913+
914+ if ((size_t) (fs->end - fs->buf) < amount)
915+ /* Gotta grow the buffer. */
916+ {
917+ size_t old_size = fs->end - fs->buf;
918+ size_t new_size = old_size + amount;
919+ char *new_buf;
920+
921+ if (new_size < old_size || ! (new_buf = realloc (fs->buf, new_size)))
922+ {
923+ __set_errno (ENOMEM);
924+ return 0;
925+ }
926+
927+ fs->buf = new_buf;
928+ fs->end = new_buf + new_size;
929+ fs->p = fs->buf;
930+ }
931+ }
932+
933+ return 1;
934+}
935+
936+ssize_t
937+__argp_fmtstream_printf (struct argp_fmtstream *fs, const char *fmt, ...)
938+{
939+ int out;
940+ size_t avail;
941+ size_t size_guess = PRINTF_SIZE_GUESS; /* How much space to reserve. */
942+
943+ do
944+ {
945+ va_list args;
946+
947+ if (! __argp_fmtstream_ensure (fs, size_guess))
948+ return -1;
949+
950+ va_start (args, fmt);
951+ avail = fs->end - fs->p;
952+ out = __vsnprintf (fs->p, avail, fmt, args);
953+ va_end (args);
954+ if ((size_t) out >= avail)
955+ size_guess = out + 1;
956+ }
957+ while ((size_t) out >= avail);
958+
959+ fs->p += out;
960+
961+ return out;
962+}
963+#if 0
964+/* Not exported. */
965+#ifdef weak_alias
966+weak_alias (__argp_fmtstream_printf, argp_fmtstream_printf)
967+#endif
968+#endif
969+
970+#endif /* !ARGP_FMTSTREAM_USE_LINEWRAP */
971Index: git/libuargp/argp-fmtstream.h
972===================================================================
973--- /dev/null
974+++ git/libuargp/argp-fmtstream.h
975@@ -0,0 +1,314 @@
976+/* Word-wrapping and line-truncating streams.
977+ Copyright (C) 1997 Free Software Foundation, Inc.
978+ This file is part of the GNU C Library.
979+ Written by Miles Bader <miles at gnu.ai.mit.edu>.
980+
981+ The GNU C Library is free software; you can redistribute it and/or
982+ modify it under the terms of the GNU Lesser General Public
983+ License as published by the Free Software Foundation; either
984+ version 2.1 of the License, or (at your option) any later version.
985+
986+ The GNU C Library is distributed in the hope that it will be useful,
987+ but WITHOUT ANY WARRANTY; without even the implied warranty of
988+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
989+ Lesser General Public License for more details.
990+
991+ You should have received a copy of the GNU Lesser General Public
992+ License along with the GNU C Library; if not, write to the Free
993+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
994+ 02111-1307 USA.
995+
996+ Modified for uClibc by: Salvatore Cro <salvatore.cro at st.com>
997+*/
998+
999+/* This package emulates glibc `line_wrap_stream' semantics for systems that
1000+ don't have that. If the system does have it, it is just a wrapper for
1001+ that. This header file is only used internally while compiling argp, and
1002+ shouldn't be installed. */
1003+
1004+#ifndef _ARGP_FMTSTREAM_H
1005+#define _ARGP_FMTSTREAM_H
1006+
1007+#include <stdio.h>
1008+#include <string.h>
1009+#include <unistd.h>
1010+
1011+#ifndef __attribute__
1012+/* This feature is available in gcc versions 2.5 and later. */
1013+# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || defined(__STRICT_ANSI__)
1014+# define __attribute__(Spec) /* empty */
1015+# endif
1016+/* The __-protected variants of `format' and `printf' attributes
1017+ are accepted by gcc versions 2.6.4 (effectively 2.7) and later. */
1018+# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7) || defined(__STRICT_ANSI__)
1019+# define __format__ format
1020+# define __printf__ printf
1021+# endif
1022+#endif
1023+
1024+#if 0 /* uClibc: disabled */
1025+#if (_LIBC - 0 && !defined (USE_IN_LIBIO)) \
1026+ || (defined (__GNU_LIBRARY__) && defined (HAVE_LINEWRAP_H))
1027+/* line_wrap_stream is available, so use that. */
1028+#define ARGP_FMTSTREAM_USE_LINEWRAP
1029+#endif
1030+#else
1031+/* line_wrap stream NOT available */
1032+# undef ARGP_FMTSTREAM_USE_LINEWRAP
1033+#endif
1034+
1035+#ifdef ARGP_FMTSTREAM_USE_LINEWRAP
1036+/* Just be a simple wrapper for line_wrap_stream; the semantics are
1037+ *slightly* different, as line_wrap_stream doesn't actually make a new
1038+ object, it just modifies the given stream (reversibly) to do
1039+ line-wrapping. Since we control who uses this code, it doesn't matter. */
1040+
1041+#include <linewrap.h>
1042+
1043+typedef FILE *argp_fmtstream_t;
1044+
1045+#define argp_make_fmtstream line_wrap_stream
1046+#define __argp_make_fmtstream line_wrap_stream
1047+#define argp_fmtstream_free line_unwrap_stream
1048+#define __argp_fmtstream_free line_unwrap_stream
1049+
1050+#define __argp_fmtstream_putc(fs,ch) putc(ch,fs)
1051+#define argp_fmtstream_putc(fs,ch) putc(ch,fs)
1052+#define __argp_fmtstream_puts(fs,str) fputs(str,fs)
1053+#define argp_fmtstream_puts(fs,str) fputs(str,fs)
1054+#define __argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
1055+#define argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
1056+#define __argp_fmtstream_printf fprintf
1057+#define argp_fmtstream_printf fprintf
1058+
1059+#define __argp_fmtstream_lmargin line_wrap_lmargin
1060+#define argp_fmtstream_lmargin line_wrap_lmargin
1061+#define __argp_fmtstream_set_lmargin line_wrap_set_lmargin
1062+#define argp_fmtstream_set_lmargin line_wrap_set_lmargin
1063+#define __argp_fmtstream_rmargin line_wrap_rmargin
1064+#define argp_fmtstream_rmargin line_wrap_rmargin
1065+#define __argp_fmtstream_set_rmargin line_wrap_set_rmargin
1066+#define argp_fmtstream_set_rmargin line_wrap_set_rmargin
1067+#define __argp_fmtstream_wmargin line_wrap_wmargin
1068+#define argp_fmtstream_wmargin line_wrap_wmargin
1069+#define __argp_fmtstream_set_wmargin line_wrap_set_wmargin
1070+#define argp_fmtstream_set_wmargin line_wrap_set_wmargin
1071+#define __argp_fmtstream_point line_wrap_point
1072+#define argp_fmtstream_point line_wrap_point
1073+
1074+#else /* !ARGP_FMTSTREAM_USE_LINEWRAP */
1075+/* Guess we have to define our own version. */
1076+
1077+#ifndef __const
1078+#define __const const
1079+#endif
1080+
1081+struct argp_fmtstream
1082+{
1083+ FILE *stream; /* The stream we're outputting to. */
1084+
1085+ size_t lmargin, rmargin; /* Left and right margins. */
1086+ ssize_t wmargin; /* Margin to wrap to, or -1 to truncate. */
1087+
1088+ /* Point in buffer to which we've processed for wrapping, but not output. */
1089+ size_t point_offs;
1090+ /* Output column at POINT_OFFS, or -1 meaning 0 but don't add lmargin. */
1091+ ssize_t point_col;
1092+
1093+ char *buf; /* Output buffer. */
1094+ char *p; /* Current end of text in BUF. */
1095+ char *end; /* Absolute end of BUF. */
1096+};
1097+
1098+typedef struct argp_fmtstream *argp_fmtstream_t;
1099+
1100+/* Return an argp_fmtstream that outputs to STREAM, and which prefixes lines
1101+ written on it with LMARGIN spaces and limits them to RMARGIN columns
1102+ total. If WMARGIN >= 0, words that extend past RMARGIN are wrapped by
1103+ replacing the whitespace before them with a newline and WMARGIN spaces.
1104+ Otherwise, chars beyond RMARGIN are simply dropped until a newline.
1105+ Returns NULL if there was an error. */
1106+extern argp_fmtstream_t __argp_make_fmtstream (FILE *__stream,
1107+ size_t __lmargin,
1108+ size_t __rmargin,
1109+ ssize_t __wmargin);
1110+extern argp_fmtstream_t argp_make_fmtstream (FILE *__stream,
1111+ size_t __lmargin,
1112+ size_t __rmargin,
1113+ ssize_t __wmargin);
1114+
1115+/* Flush __FS to its stream, and free it (but don't close the stream). */
1116+extern void __argp_fmtstream_free (argp_fmtstream_t __fs);
1117+extern void argp_fmtstream_free (argp_fmtstream_t __fs);
1118+
1119+extern ssize_t __argp_fmtstream_printf (argp_fmtstream_t __fs,
1120+ __const char *__fmt, ...)
1121+ __attribute__ ((__format__ (printf, 2, 3)));
1122+extern ssize_t argp_fmtstream_printf (argp_fmtstream_t __fs,
1123+ __const char *__fmt, ...)
1124+ __attribute__ ((__format__ (printf, 2, 3)));
1125+
1126+extern int __argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
1127+extern int argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
1128+
1129+extern int __argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str);
1130+extern int argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str);
1131+
1132+extern size_t __argp_fmtstream_write (argp_fmtstream_t __fs,
1133+ __const char *__str, size_t __len);
1134+extern size_t argp_fmtstream_write (argp_fmtstream_t __fs,
1135+ __const char *__str, size_t __len);
1136+
1137+/* Access macros for various bits of state. */
1138+#define argp_fmtstream_lmargin(__fs) ((__fs)->lmargin)
1139+#define argp_fmtstream_rmargin(__fs) ((__fs)->rmargin)
1140+#define argp_fmtstream_wmargin(__fs) ((__fs)->wmargin)
1141+#define __argp_fmtstream_lmargin argp_fmtstream_lmargin
1142+#define __argp_fmtstream_rmargin argp_fmtstream_rmargin
1143+#define __argp_fmtstream_wmargin argp_fmtstream_wmargin
1144+
1145+/* Set __FS's left margin to LMARGIN and return the old value. */
1146+extern size_t argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
1147+ size_t __lmargin);
1148+extern size_t __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
1149+ size_t __lmargin);
1150+
1151+/* Set __FS's right margin to __RMARGIN and return the old value. */
1152+extern size_t argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
1153+ size_t __rmargin);
1154+extern size_t __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
1155+ size_t __rmargin);
1156+
1157+/* Set __FS's wrap margin to __WMARGIN and return the old value. */
1158+extern size_t argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
1159+ size_t __wmargin);
1160+extern size_t __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
1161+ size_t __wmargin);
1162+
1163+/* Return the column number of the current output point in __FS. */
1164+extern size_t argp_fmtstream_point (argp_fmtstream_t __fs);
1165+extern size_t __argp_fmtstream_point (argp_fmtstream_t __fs);
1166+
1167+/* Internal routines. */
1168+extern void _argp_fmtstream_update (argp_fmtstream_t __fs);
1169+extern void __argp_fmtstream_update (argp_fmtstream_t __fs);
1170+extern int _argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
1171+extern int __argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
1172+
1173+#ifdef __OPTIMIZE__
1174+/* Inline versions of above routines. */
1175+
1176+#if !_LIBC
1177+#define __argp_fmtstream_putc argp_fmtstream_putc
1178+#define __argp_fmtstream_puts argp_fmtstream_puts
1179+#define __argp_fmtstream_write argp_fmtstream_write
1180+#define __argp_fmtstream_set_lmargin argp_fmtstream_set_lmargin
1181+#define __argp_fmtstream_set_rmargin argp_fmtstream_set_rmargin
1182+#define __argp_fmtstream_set_wmargin argp_fmtstream_set_wmargin
1183+#define __argp_fmtstream_point argp_fmtstream_point
1184+#define __argp_fmtstream_update _argp_fmtstream_update
1185+#define __argp_fmtstream_ensure _argp_fmtstream_ensure
1186+#endif
1187+
1188+#ifndef ARGP_FS_EI
1189+#define ARGP_FS_EI __extern_inline
1190+#endif
1191+
1192+ARGP_FS_EI size_t
1193+__argp_fmtstream_write (argp_fmtstream_t __fs,
1194+ __const char *__str, size_t __len)
1195+{
1196+ if (__fs->p + __len <= __fs->end || __argp_fmtstream_ensure (__fs, __len))
1197+ {
1198+ memcpy (__fs->p, __str, __len);
1199+ __fs->p += __len;
1200+ return __len;
1201+ }
1202+ else
1203+ return 0;
1204+}
1205+
1206+ARGP_FS_EI int
1207+__argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str)
1208+{
1209+ size_t __len = strlen (__str);
1210+ if (__len)
1211+ {
1212+ size_t __wrote = __argp_fmtstream_write (__fs, __str, __len);
1213+ return __wrote == __len ? 0 : -1;
1214+ }
1215+ else
1216+ return 0;
1217+}
1218+
1219+ARGP_FS_EI int
1220+__argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch)
1221+{
1222+ if (__fs->p < __fs->end || __argp_fmtstream_ensure (__fs, 1))
1223+ return *__fs->p++ = __ch;
1224+ else
1225+ return EOF;
1226+}
1227+
1228+/* Set __FS's left margin to __LMARGIN and return the old value. */
1229+ARGP_FS_EI size_t
1230+__argp_fmtstream_set_lmargin (argp_fmtstream_t __fs, size_t __lmargin)
1231+{
1232+ size_t __old;
1233+ if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
1234+ __argp_fmtstream_update (__fs);
1235+ __old = __fs->lmargin;
1236+ __fs->lmargin = __lmargin;
1237+ return __old;
1238+}
1239+
1240+/* Set __FS's right margin to __RMARGIN and return the old value. */
1241+ARGP_FS_EI size_t
1242+__argp_fmtstream_set_rmargin (argp_fmtstream_t __fs, size_t __rmargin)
1243+{
1244+ size_t __old;
1245+ if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
1246+ __argp_fmtstream_update (__fs);
1247+ __old = __fs->rmargin;
1248+ __fs->rmargin = __rmargin;
1249+ return __old;
1250+}
1251+
1252+/* Set FS's wrap margin to __WMARGIN and return the old value. */
1253+ARGP_FS_EI size_t
1254+__argp_fmtstream_set_wmargin (argp_fmtstream_t __fs, size_t __wmargin)
1255+{
1256+ size_t __old;
1257+ if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
1258+ __argp_fmtstream_update (__fs);
1259+ __old = __fs->wmargin;
1260+ __fs->wmargin = __wmargin;
1261+ return __old;
1262+}
1263+
1264+/* Return the column number of the current output point in __FS. */
1265+ARGP_FS_EI size_t
1266+__argp_fmtstream_point (argp_fmtstream_t __fs)
1267+{
1268+ if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
1269+ __argp_fmtstream_update (__fs);
1270+ return __fs->point_col >= 0 ? __fs->point_col : 0;
1271+}
1272+
1273+#if !_LIBC
1274+#undef __argp_fmtstream_putc
1275+#undef __argp_fmtstream_puts
1276+#undef __argp_fmtstream_write
1277+#undef __argp_fmtstream_set_lmargin
1278+#undef __argp_fmtstream_set_rmargin
1279+#undef __argp_fmtstream_set_wmargin
1280+#undef __argp_fmtstream_point
1281+#undef __argp_fmtstream_update
1282+#undef __argp_fmtstream_ensure
1283+#endif
1284+
1285+#endif /* __OPTIMIZE__ */
1286+
1287+#endif /* ARGP_FMTSTREAM_USE_LINEWRAP */
1288+
1289+#endif /* argp-fmtstream.h */
1290Index: git/libuargp/argp-fs-xinl.c
1291===================================================================
1292--- /dev/null
1293+++ git/libuargp/argp-fs-xinl.c
1294@@ -0,0 +1,44 @@
1295+/* Real definitions for extern inline functions in argp-fmtstream.h
1296+ Copyright (C) 1997, 2003, 2004 Free Software Foundation, Inc.
1297+ This file is part of the GNU C Library.
1298+ Written by Miles Bader <miles at gnu.ai.mit.edu>.
1299+
1300+ The GNU C Library is free software; you can redistribute it and/or
1301+ modify it under the terms of the GNU Lesser General Public
1302+ License as published by the Free Software Foundation; either
1303+ version 2.1 of the License, or (at your option) any later version.
1304+
1305+ The GNU C Library is distributed in the hope that it will be useful,
1306+ but WITHOUT ANY WARRANTY; without even the implied warranty of
1307+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1308+ Lesser General Public License for more details.
1309+
1310+ You should have received a copy of the GNU Lesser General Public
1311+ License along with the GNU C Library; if not, write to the Free
1312+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1313+ 02111-1307 USA. */
1314+
1315+#ifdef HAVE_CONFIG_H
1316+# include <config.h>
1317+#endif
1318+
1319+#define ARGP_FS_EI
1320+#undef __OPTIMIZE__
1321+#define __OPTIMIZE__ 1
1322+#include "argp-fmtstream.h"
1323+
1324+#if 0
1325+/* Not exported. */
1326+/* Add weak aliases. */
1327+#if _LIBC - 0 && !defined (ARGP_FMTSTREAM_USE_LINEWRAP) && defined (weak_alias)
1328+
1329+weak_alias (__argp_fmtstream_putc, argp_fmtstream_putc)
1330+weak_alias (__argp_fmtstream_puts, argp_fmtstream_puts)
1331+weak_alias (__argp_fmtstream_write, argp_fmtstream_write)
1332+weak_alias (__argp_fmtstream_set_lmargin, argp_fmtstream_set_lmargin)
1333+weak_alias (__argp_fmtstream_set_rmargin, argp_fmtstream_set_rmargin)
1334+weak_alias (__argp_fmtstream_set_wmargin, argp_fmtstream_set_wmargin)
1335+weak_alias (__argp_fmtstream_point, argp_fmtstream_point)
1336+
1337+#endif
1338+#endif
1339Index: git/libuargp/argp-help.c
1340===================================================================
1341--- /dev/null
1342+++ git/libuargp/argp-help.c
1343@@ -0,0 +1,1882 @@
1344+/* Hierarchial argument parsing help output
1345+ Copyright (C) 1995-2003, 2004, 2005, 2006, 2007
1346+ Free Software Foundation, Inc.
1347+ This file is part of the GNU C Library.
1348+ Written by Miles Bader <miles at gnu.ai.mit.edu>.
1349+
1350+ The GNU C Library is free software; you can redistribute it and/or
1351+ modify it under the terms of the GNU Lesser General Public
1352+ License as published by the Free Software Foundation; either
1353+ version 2.1 of the License, or (at your option) any later version.
1354+
1355+ The GNU C Library is distributed in the hope that it will be useful,
1356+ but WITHOUT ANY WARRANTY; without even the implied warranty of
1357+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1358+ Lesser General Public License for more details.
1359+
1360+ You should have received a copy of the GNU Lesser General Public
1361+ License along with the GNU C Library; if not, write to the Free
1362+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1363+ 02111-1307 USA.
1364+
1365+ Modified for uClibc by: Salvatore Cro <salvatore.cro at st.com>
1366+*/
1367+
1368+#ifndef _GNU_SOURCE
1369+# define _GNU_SOURCE 1
1370+#endif
1371+
1372+#ifdef HAVE_CONFIG_H
1373+#include <config.h>
1374+#endif
1375+
1376+/* AIX requires this to be the first thing in the file. */
1377+#ifndef __GNUC__
1378+# if HAVE_ALLOCA_H || defined _LIBC
1379+# include <alloca.h>
1380+# else
1381+# ifdef _AIX
1382+#pragma alloca
1383+# else
1384+# ifndef alloca /* predefined by HP cc +Olibcalls */
1385+char *alloca ();
1386+# endif
1387+# endif
1388+# endif
1389+#endif
1390+
1391+#include <stddef.h>
1392+#include <stdlib.h>
1393+#include <string.h>
1394+#include <assert.h>
1395+#include <stdarg.h>
1396+#include <ctype.h>
1397+#include <limits.h>
1398+#ifdef _LIBC
1399+# include <wchar.h>
1400+#endif
1401+
1402+#include <features.h>
1403+#ifndef _
1404+/* This is for other GNU distributions with internationalized messages. */
1405+# if (defined HAVE_LIBINTL_H || defined _LIBC) && defined __UCLIBC_HAS_GETTEXT_AWARENESS__
1406+# include <libintl.h>
1407+# ifdef _LIBC
1408+# undef dgettext
1409+# define dgettext(domain, msgid) \
1410+ INTUSE(__dcgettext) (domain, msgid, LC_MESSAGES)
1411+# endif
1412+# else
1413+# define dgettext(domain, msgid) (msgid)
1414+# endif
1415+#endif
1416+
1417+#ifndef _LIBC
1418+# if HAVE_STRERROR_R
1419+# if !HAVE_DECL_STRERROR_R
1420+char *strerror_r (int errnum, char *buf, size_t buflen);
1421+# endif
1422+# else
1423+# if !HAVE_DECL_STRERROR
1424+char *strerror (int errnum);
1425+# endif
1426+# endif
1427+#endif
1428+
1429+#include <argp.h>
1430+#include "argp-fmtstream.h"
1431+#include <stdbool.h>
1432+#include <stdint.h>
1433+
1434+#ifndef SIZE_MAX
1435+# define SIZE_MAX ((size_t) -1)
1436+#endif
1437+
1438+/* User-selectable (using an environment variable) formatting parameters.
1439+
1440+ These may be specified in an environment variable called `ARGP_HELP_FMT',
1441+ with a contents like: VAR1=VAL1,VAR2=VAL2,BOOLVAR2,no-BOOLVAR2
1442+ Where VALn must be a positive integer. The list of variables is in the
1443+ UPARAM_NAMES vector, below. */
1444+
1445+/* Default parameters. */
1446+#define DUP_ARGS 0 /* True if option argument can be duplicated. */
1447+#define DUP_ARGS_NOTE 1 /* True to print a note about duplicate args. */
1448+#define SHORT_OPT_COL 2 /* column in which short options start */
1449+#define LONG_OPT_COL 6 /* column in which long options start */
1450+#define DOC_OPT_COL 2 /* column in which doc options start */
1451+#define OPT_DOC_COL 29 /* column in which option text starts */
1452+#define HEADER_COL 1 /* column in which group headers are printed */
1453+#define USAGE_INDENT 12 /* indentation of wrapped usage lines */
1454+#define RMARGIN 79 /* right margin used for wrapping */
1455+
1456+/* User-selectable (using an environment variable) formatting parameters.
1457+ They must all be of type `int' for the parsing code to work. */
1458+struct uparams
1459+{
1460+ /* If true, arguments for an option are shown with both short and long
1461+ options, even when a given option has both, e.g. `-x ARG, --longx=ARG'.
1462+ If false, then if an option has both, the argument is only shown with
1463+ the long one, e.g., `-x, --longx=ARG', and a message indicating that
1464+ this really means both is printed below the options. */
1465+ int dup_args;
1466+
1467+ /* This is true if when DUP_ARGS is false, and some duplicate arguments have
1468+ been suppressed, an explanatory message should be printed. */
1469+ int dup_args_note;
1470+
1471+ /* Various output columns. */
1472+ int short_opt_col;
1473+ int long_opt_col;
1474+ int doc_opt_col;
1475+ int opt_doc_col;
1476+ int header_col;
1477+ int usage_indent;
1478+ int rmargin;
1479+};
1480+
1481+/* This is a global variable, as user options are only ever read once. */
1482+static struct uparams uparams = {
1483+ DUP_ARGS, DUP_ARGS_NOTE,
1484+ SHORT_OPT_COL, LONG_OPT_COL, DOC_OPT_COL, OPT_DOC_COL, HEADER_COL,
1485+ USAGE_INDENT, RMARGIN
1486+};
1487+
1488+/* A particular uparam, and what the user name is. */
1489+struct uparam_name
1490+{
1491+ const char name[14]; /* User name. */
1492+ bool is_bool; /* Whether it's `boolean'. */
1493+ uint8_t uparams_offs; /* Location of the (int) field in UPARAMS. */
1494+};
1495+
1496+/* The name-field mappings we know about. */
1497+static const struct uparam_name uparam_names[] =
1498+{
1499+ { "dup-args", true, offsetof (struct uparams, dup_args) },
1500+ { "dup-args-note", true, offsetof (struct uparams, dup_args_note) },
1501+ { "short-opt-col", false, offsetof (struct uparams, short_opt_col) },
1502+ { "long-opt-col", false, offsetof (struct uparams, long_opt_col) },
1503+ { "doc-opt-col", false, offsetof (struct uparams, doc_opt_col) },
1504+ { "opt-doc-col", false, offsetof (struct uparams, opt_doc_col) },
1505+ { "header-col", false, offsetof (struct uparams, header_col) },
1506+ { "usage-indent", false, offsetof (struct uparams, usage_indent) },
1507+ { "rmargin", false, offsetof (struct uparams, rmargin) }
1508+};
1509+#define nuparam_names (sizeof (uparam_names) / sizeof (uparam_names[0]))
1510+
1511+/* Read user options from the environment, and fill in UPARAMS appropiately. */
1512+static void
1513+fill_in_uparams (const struct argp_state *state)
1514+{
1515+ const char *var = getenv ("ARGP_HELP_FMT");
1516+
1517+#define SKIPWS(p) do { while (isspace (*p)) p++; } while (0);
1518+
1519+ if (var)
1520+ /* Parse var. */
1521+ while (*var)
1522+ {
1523+ SKIPWS (var);
1524+
1525+ if (isalpha (*var))
1526+ {
1527+ size_t var_len;
1528+ const struct uparam_name *un;
1529+ int unspec = 0, val = 0;
1530+ const char *arg = var;
1531+
1532+ while (isalnum (*arg) || *arg == '-' || *arg == '_')
1533+ arg++;
1534+ var_len = arg - var;
1535+
1536+ SKIPWS (arg);
1537+
1538+ if (*arg == '\0' || *arg == ',')
1539+ unspec = 1;
1540+ else if (*arg == '=')
1541+ {
1542+ arg++;
1543+ SKIPWS (arg);
1544+ }
1545+
1546+ if (unspec)
1547+ {
1548+ if (var[0] == 'n' && var[1] == 'o' && var[2] == '-')
1549+ {
1550+ val = 0;
1551+ var += 3;
1552+ var_len -= 3;
1553+ }
1554+ else
1555+ val = 1;
1556+ }
1557+ else if (isdigit (*arg))
1558+ {
1559+ val = atoi (arg);
1560+ while (isdigit (*arg))
1561+ arg++;
1562+ SKIPWS (arg);
1563+ }
1564+
1565+ un = uparam_names;
1566+ size_t u;
1567+ for (u = 0; u < nuparam_names; ++un, ++u)
1568+ if (strlen (un->name) == var_len
1569+ && strncmp (var, un->name, var_len) == 0)
1570+ {
1571+ if (unspec && !un->is_bool)
1572+ argp_failure (state, 0, 0,
1573+ dgettext (state == NULL ? NULL
1574+ : state->root_argp->argp_domain,
1575+ "\
1576+%.*s: ARGP_HELP_FMT parameter requires a value"),
1577+ (int) var_len, var);
1578+ else
1579+ *(int *)((char *)&uparams + un->uparams_offs) = val;
1580+ break;
1581+ }
1582+ if (u == nuparam_names)
1583+ argp_failure (state, 0, 0,
1584+ dgettext (state == NULL ? NULL
1585+ : state->root_argp->argp_domain, "\
1586+%.*s: Unknown ARGP_HELP_FMT parameter"),
1587+ (int) var_len, var);
1588+
1589+ var = arg;
1590+ if (*var == ',')
1591+ var++;
1592+ }
1593+ else if (*var)
1594+ {
1595+ argp_failure (state, 0, 0,
1596+ dgettext (state == NULL ? NULL
1597+ : state->root_argp->argp_domain,
1598+ "Garbage in ARGP_HELP_FMT: %s"), var);
1599+ break;
1600+ }
1601+ }
1602+}
1603+
1604+/* Returns true if OPT hasn't been marked invisible. Visibility only affects
1605+ whether OPT is displayed or used in sorting, not option shadowing. */
1606+#define ovisible(opt) (! ((opt)->flags & OPTION_HIDDEN))
1607+
1608+/* Returns true if OPT is an alias for an earlier option. */
1609+#define oalias(opt) ((opt)->flags & OPTION_ALIAS)
1610+
1611+/* Returns true if OPT is an documentation-only entry. */
1612+#define odoc(opt) ((opt)->flags & OPTION_DOC)
1613+
1614+/* Returns true if OPT is the end-of-list marker for a list of options. */
1615+#define oend(opt) __option_is_end (opt)
1616+
1617+/* Returns true if OPT has a short option. */
1618+#define oshort(opt) __option_is_short (opt)
1619+
1620+/*
1621+ The help format for a particular option is like:
1622+
1623+ -xARG, -yARG, --long1=ARG, --long2=ARG Documentation...
1624+
1625+ Where ARG will be omitted if there's no argument, for this option, or
1626+ will be surrounded by "[" and "]" appropiately if the argument is
1627+ optional. The documentation string is word-wrapped appropiately, and if
1628+ the list of options is long enough, it will be started on a separate line.
1629+ If there are no short options for a given option, the first long option is
1630+ indented slighly in a way that's supposed to make most long options appear
1631+ to be in a separate column.
1632+
1633+ For example, the following output (from ps):
1634+
1635+ -p PID, --pid=PID List the process PID
1636+ --pgrp=PGRP List processes in the process group PGRP
1637+ -P, -x, --no-parent Include processes without parents
1638+ -Q, --all-fields Don't elide unusable fields (normally if there's
1639+ some reason ps can't print a field for any
1640+ process, it's removed from the output entirely)
1641+ -r, --reverse, --gratuitously-long-reverse-option
1642+ Reverse the order of any sort
1643+ --session[=SID] Add the processes from the session SID (which
1644+ defaults to the sid of the current process)
1645+
1646+ Here are some more options:
1647+ -f ZOT, --foonly=ZOT Glork a foonly
1648+ -z, --zaza Snit a zar
1649+
1650+ -?, --help Give this help list
1651+ --usage Give a short usage message
1652+ -V, --version Print program version
1653+
1654+ The struct argp_option array for the above could look like:
1655+
1656+ {
1657+ {"pid", 'p', "PID", 0, "List the process PID"},
1658+ {"pgrp", OPT_PGRP, "PGRP", 0, "List processes in the process group PGRP"},
1659+ {"no-parent", 'P', 0, 0, "Include processes without parents"},
1660+ {0, 'x', 0, OPTION_ALIAS},
1661+ {"all-fields",'Q', 0, 0, "Don't elide unusable fields (normally"
1662+ " if there's some reason ps can't"
1663+ " print a field for any process, it's"
1664+ " removed from the output entirely)" },
1665+ {"reverse", 'r', 0, 0, "Reverse the order of any sort"},
1666+ {"gratuitously-long-reverse-option", 0, 0, OPTION_ALIAS},
1667+ {"session", OPT_SESS, "SID", OPTION_ARG_OPTIONAL,
1668+ "Add the processes from the session"
1669+ " SID (which defaults to the sid of"
1670+ " the current process)" },
1671+
1672+ {0,0,0,0, "Here are some more options:"},
1673+ {"foonly", 'f', "ZOT", 0, "Glork a foonly"},
1674+ {"zaza", 'z', 0, 0, "Snit a zar"},
1675+
1676+ {0}
1677+ }
1678+
1679+ Note that the last three options are automatically supplied by argp_parse,
1680+ unless you tell it not to with ARGP_NO_HELP.
1681+
1682+*/
1683+
1684+/* Returns true if CH occurs between BEG and END. */
1685+static int
1686+find_char (char ch, char *beg, char *end)
1687+{
1688+ while (beg < end)
1689+ if (*beg == ch)
1690+ return 1;
1691+ else
1692+ beg++;
1693+ return 0;
1694+}
1695+
1696+struct hol_cluster; /* fwd decl */
1697+
1698+struct hol_entry
1699+{
1700+ /* First option. */
1701+ const struct argp_option *opt;
1702+ /* Number of options (including aliases). */
1703+ unsigned num;
1704+
1705+ /* A pointers into the HOL's short_options field, to the first short option
1706+ letter for this entry. The order of the characters following this point
1707+ corresponds to the order of options pointed to by OPT, and there are at
1708+ most NUM. A short option recorded in a option following OPT is only
1709+ valid if it occurs in the right place in SHORT_OPTIONS (otherwise it's
1710+ probably been shadowed by some other entry). */
1711+ char *short_options;
1712+
1713+ /* Entries are sorted by their group first, in the order:
1714+ 1, 2, ..., n, 0, -m, ..., -2, -1
1715+ and then alphabetically within each group. The default is 0. */
1716+ int group;
1717+
1718+ /* The cluster of options this entry belongs to, or 0 if none. */
1719+ struct hol_cluster *cluster;
1720+
1721+ /* The argp from which this option came. */
1722+ const struct argp *argp;
1723+};
1724+
1725+/* A cluster of entries to reflect the argp tree structure. */
1726+struct hol_cluster
1727+{
1728+ /* A descriptive header printed before options in this cluster. */
1729+ const char *header;
1730+
1731+ /* Used to order clusters within the same group with the same parent,
1732+ according to the order in which they occurred in the parent argp's child
1733+ list. */
1734+ int index;
1735+
1736+ /* How to sort this cluster with respect to options and other clusters at the
1737+ same depth (clusters always follow options in the same group). */
1738+ int group;
1739+
1740+ /* The cluster to which this cluster belongs, or 0 if it's at the base
1741+ level. */
1742+ struct hol_cluster *parent;
1743+
1744+ /* The argp from which this cluster is (eventually) derived. */
1745+ const struct argp *argp;
1746+
1747+ /* The distance this cluster is from the root. */
1748+ int depth;
1749+
1750+ /* Clusters in a given hol are kept in a linked list, to make freeing them
1751+ possible. */
1752+ struct hol_cluster *next;
1753+};
1754+
1755+/* A list of options for help. */
1756+struct hol
1757+{
1758+ /* An array of hol_entry's. */
1759+ struct hol_entry *entries;
1760+ /* The number of entries in this hol. If this field is zero, the others
1761+ are undefined. */
1762+ unsigned num_entries;
1763+
1764+ /* A string containing all short options in this HOL. Each entry contains
1765+ pointers into this string, so the order can't be messed with blindly. */
1766+ char *short_options;
1767+
1768+ /* Clusters of entries in this hol. */
1769+ struct hol_cluster *clusters;
1770+};
1771+
1772+/* Create a struct hol from the options in ARGP. CLUSTER is the
1773+ hol_cluster in which these entries occur, or 0, if at the root. */
1774+static struct hol *
1775+make_hol (const struct argp *argp, struct hol_cluster *cluster)
1776+{
1777+ char *so;
1778+ const struct argp_option *o;
1779+ const struct argp_option *opts = argp->options;
1780+ struct hol_entry *entry;
1781+ unsigned num_short_options = 0;
1782+ struct hol *hol = malloc (sizeof (struct hol));
1783+
1784+ assert (hol);
1785+
1786+ hol->num_entries = 0;
1787+ hol->clusters = 0;
1788+
1789+ if (opts)
1790+ {
1791+ int cur_group = 0;
1792+
1793+ /* The first option must not be an alias. */
1794+ assert (! oalias (opts));
1795+
1796+ /* Calculate the space needed. */
1797+ for (o = opts; ! oend (o); o++)
1798+ {
1799+ if (! oalias (o))
1800+ hol->num_entries++;
1801+ if (oshort (o))
1802+ num_short_options++; /* This is an upper bound. */
1803+ }
1804+
1805+ hol->entries = malloc (sizeof (struct hol_entry) * hol->num_entries);
1806+ hol->short_options = malloc (num_short_options + 1);
1807+
1808+ assert (hol->entries && hol->short_options);
1809+#if SIZE_MAX <= UINT_MAX
1810+ assert (hol->num_entries <= SIZE_MAX / sizeof (struct hol_entry));
1811+#endif
1812+
1813+ /* Fill in the entries. */
1814+ so = hol->short_options;
1815+ for (o = opts, entry = hol->entries; ! oend (o); entry++)
1816+ {
1817+ entry->opt = o;
1818+ entry->num = 0;
1819+ entry->short_options = so;
1820+ entry->group = cur_group =
1821+ o->group
1822+ ? o->group
1823+ : ((!o->name && !o->key)
1824+ ? cur_group + 1
1825+ : cur_group);
1826+ entry->cluster = cluster;
1827+ entry->argp = argp;
1828+
1829+ do
1830+ {
1831+ entry->num++;
1832+ if (oshort (o) && ! find_char (o->key, hol->short_options, so))
1833+ /* O has a valid short option which hasn't already been used.*/
1834+ *so++ = o->key;
1835+ o++;
1836+ }
1837+ while (! oend (o) && oalias (o));
1838+ }
1839+ *so = '\0'; /* null terminated so we can find the length */
1840+ }
1841+
1842+ return hol;
1843+}
1844+
1845+/* Add a new cluster to HOL, with the given GROUP and HEADER (taken from the
1846+ associated argp child list entry), INDEX, and PARENT, and return a pointer
1847+ to it. ARGP is the argp that this cluster results from. */
1848+static struct hol_cluster *
1849+hol_add_cluster (struct hol *hol, int group, const char *header, int index,
1850+ struct hol_cluster *parent, const struct argp *argp)
1851+{
1852+ struct hol_cluster *cl = malloc (sizeof (struct hol_cluster));
1853+ if (cl)
1854+ {
1855+ cl->group = group;
1856+ cl->header = header;
1857+
1858+ cl->index = index;
1859+ cl->parent = parent;
1860+ cl->argp = argp;
1861+ cl->depth = parent ? parent->depth + 1 : 0;
1862+
1863+ cl->next = hol->clusters;
1864+ hol->clusters = cl;
1865+ }
1866+ return cl;
1867+}
1868+
1869+/* Free HOL and any resources it uses. */
1870+static void
1871+hol_free (struct hol *hol)
1872+{
1873+ struct hol_cluster *cl = hol->clusters;
1874+
1875+ while (cl)
1876+ {
1877+ struct hol_cluster *next = cl->next;
1878+ free (cl);
1879+ cl = next;
1880+ }
1881+
1882+ if (hol->num_entries > 0)
1883+ {
1884+ free (hol->entries);
1885+ free (hol->short_options);
1886+ }
1887+
1888+ free (hol);
1889+}
1890+
1891+static int
1892+hol_entry_short_iterate (const struct hol_entry *entry,
1893+ int (*func)(const struct argp_option *opt,
1894+ const struct argp_option *real,
1895+ const char *domain, void *cookie),
1896+ const char *domain, void *cookie)
1897+{
1898+ unsigned nopts;
1899+ int val = 0;
1900+ const struct argp_option *opt, *real = entry->opt;
1901+ char *so = entry->short_options;
1902+
1903+ for (opt = real, nopts = entry->num; nopts > 0 && !val; opt++, nopts--)
1904+ if (oshort (opt) && *so == opt->key)
1905+ {
1906+ if (!oalias (opt))
1907+ real = opt;
1908+ if (ovisible (opt))
1909+ val = (*func)(opt, real, domain, cookie);
1910+ so++;
1911+ }
1912+
1913+ return val;
1914+}
1915+
1916+static __inline__ int
1917+__attribute__ ((always_inline))
1918+hol_entry_long_iterate (const struct hol_entry *entry,
1919+ int (*func)(const struct argp_option *opt,
1920+ const struct argp_option *real,
1921+ const char *domain, void *cookie),
1922+ const char *domain, void *cookie)
1923+{
1924+ unsigned nopts;
1925+ int val = 0;
1926+ const struct argp_option *opt, *real = entry->opt;
1927+
1928+ for (opt = real, nopts = entry->num; nopts > 0 && !val; opt++, nopts--)
1929+ if (opt->name)
1930+ {
1931+ if (!oalias (opt))
1932+ real = opt;
1933+ if (ovisible (opt))
1934+ val = (*func)(opt, real, domain, cookie);
1935+ }
1936+
1937+ return val;
1938+}
1939+
1940+/* Iterator that returns true for the first short option. */
1941+static __inline__ int
1942+until_short (const struct argp_option *opt, const struct argp_option *real,
1943+ const char *domain, void *cookie)
1944+{
1945+ return oshort (opt) ? opt->key : 0;
1946+}
1947+
1948+/* Returns the first valid short option in ENTRY, or 0 if there is none. */
1949+static char
1950+hol_entry_first_short (const struct hol_entry *entry)
1951+{
1952+ return hol_entry_short_iterate (entry, until_short,
1953+ entry->argp->argp_domain, 0);
1954+}
1955+
1956+/* Returns the first valid long option in ENTRY, or 0 if there is none. */
1957+static const char *
1958+hol_entry_first_long (const struct hol_entry *entry)
1959+{
1960+ const struct argp_option *opt;
1961+ unsigned num;
1962+ for (opt = entry->opt, num = entry->num; num > 0; opt++, num--)
1963+ if (opt->name && ovisible (opt))
1964+ return opt->name;
1965+ return 0;
1966+}
1967+
1968+/* Returns the entry in HOL with the long option name NAME, or 0 if there is
1969+ none. */
1970+static struct hol_entry *
1971+hol_find_entry (struct hol *hol, const char *name)
1972+{
1973+ struct hol_entry *entry = hol->entries;
1974+ unsigned num_entries = hol->num_entries;
1975+
1976+ while (num_entries-- > 0)
1977+ {
1978+ const struct argp_option *opt = entry->opt;
1979+ unsigned num_opts = entry->num;
1980+
1981+ while (num_opts-- > 0)
1982+ if (opt->name && ovisible (opt) && strcmp (opt->name, name) == 0)
1983+ return entry;
1984+ else
1985+ opt++;
1986+
1987+ entry++;
1988+ }
1989+
1990+ return 0;
1991+}
1992+
1993+/* If an entry with the long option NAME occurs in HOL, set it's special
1994+ sort position to GROUP. */
1995+static void
1996+hol_set_group (struct hol *hol, const char *name, int group)
1997+{
1998+ struct hol_entry *entry = hol_find_entry (hol, name);
1999+ if (entry)
2000+ entry->group = group;
2001+}
2002+
2003+/* Order by group: 0, 1, 2, ..., n, -m, ..., -2, -1.
2004+ EQ is what to return if GROUP1 and GROUP2 are the same. */
2005+static int
2006+group_cmp (int group1, int group2, int eq)
2007+{
2008+ if (group1 == group2)
2009+ return eq;
2010+ else if ((group1 < 0 && group2 < 0) || (group1 >= 0 && group2 >= 0))
2011+ return group1 - group2;
2012+ else
2013+ return group2 - group1;
2014+}
2015+
2016+/* Compare clusters CL1 & CL2 by the order that they should appear in
2017+ output. */
2018+static int
2019+hol_cluster_cmp (const struct hol_cluster *cl1, const struct hol_cluster *cl2)
2020+{
2021+ /* If one cluster is deeper than the other, use its ancestor at the same
2022+ level, so that finding the common ancestor is straightforward. */
2023+ while (cl1->depth > cl2->depth)
2024+ cl1 = cl1->parent;
2025+ while (cl2->depth > cl1->depth)
2026+ cl2 = cl2->parent;
2027+
2028+ /* Now reduce both clusters to their ancestors at the point where both have
2029+ a common parent; these can be directly compared. */
2030+ while (cl1->parent != cl2->parent)
2031+ cl1 = cl1->parent, cl2 = cl2->parent;
2032+
2033+ return group_cmp (cl1->group, cl2->group, cl2->index - cl1->index);
2034+}
2035+
2036+/* Return the ancestor of CL that's just below the root (i.e., has a parent
2037+ of 0). */
2038+static struct hol_cluster *
2039+hol_cluster_base (struct hol_cluster *cl)
2040+{
2041+ while (cl->parent)
2042+ cl = cl->parent;
2043+ return cl;
2044+}
2045+
2046+/* Return true if CL1 is a child of CL2. */
2047+static int
2048+hol_cluster_is_child (const struct hol_cluster *cl1,
2049+ const struct hol_cluster *cl2)
2050+{
2051+ while (cl1 && cl1 != cl2)
2052+ cl1 = cl1->parent;
2053+ return cl1 == cl2;
2054+}
2055+
2056+/* Given the name of a OPTION_DOC option, modifies NAME to start at the tail
2057+ that should be used for comparisons, and returns true iff it should be
2058+ treated as a non-option. */
2059+static int
2060+canon_doc_option (const char **name)
2061+{
2062+ int non_opt;
2063+ /* Skip initial whitespace. */
2064+ while (isspace (**name))
2065+ (*name)++;
2066+ /* Decide whether this looks like an option (leading `-') or not. */
2067+ non_opt = (**name != '-');
2068+ /* Skip until part of name used for sorting. */
2069+ while (**name && !isalnum (**name))
2070+ (*name)++;
2071+ return non_opt;
2072+}
2073+
2074+/* Order ENTRY1 & ENTRY2 by the order which they should appear in a help
2075+ listing. */
2076+static int
2077+hol_entry_cmp (const struct hol_entry *entry1,
2078+ const struct hol_entry *entry2)
2079+{
2080+ /* The group numbers by which the entries should be ordered; if either is
2081+ in a cluster, then this is just the group within the cluster. */
2082+ int group1 = entry1->group, group2 = entry2->group;
2083+
2084+ if (entry1->cluster != entry2->cluster)
2085+ {
2086+ /* The entries are not within the same cluster, so we can't compare them
2087+ directly, we have to use the appropiate clustering level too. */
2088+ if (! entry1->cluster)
2089+ /* ENTRY1 is at the `base level', not in a cluster, so we have to
2090+ compare it's group number with that of the base cluster in which
2091+ ENTRY2 resides. Note that if they're in the same group, the
2092+ clustered option always comes laster. */
2093+ return group_cmp (group1, hol_cluster_base (entry2->cluster)->group, -1);
2094+ else if (! entry2->cluster)
2095+ /* Likewise, but ENTRY2's not in a cluster. */
2096+ return group_cmp (hol_cluster_base (entry1->cluster)->group, group2, 1);
2097+ else
2098+ /* Both entries are in clusters, we can just compare the clusters. */
2099+ return hol_cluster_cmp (entry1->cluster, entry2->cluster);
2100+ }
2101+ else if (group1 == group2)
2102+ /* The entries are both in the same cluster and group, so compare them
2103+ alphabetically. */
2104+ {
2105+ int short1 = hol_entry_first_short (entry1);
2106+ int short2 = hol_entry_first_short (entry2);
2107+ int doc1 = odoc (entry1->opt);
2108+ int doc2 = odoc (entry2->opt);
2109+ const char *long1 = hol_entry_first_long (entry1);
2110+ const char *long2 = hol_entry_first_long (entry2);
2111+
2112+ if (doc1)
2113+ doc1 = long1 != NULL && canon_doc_option (&long1);
2114+ if (doc2)
2115+ doc2 = long2 != NULL && canon_doc_option (&long2);
2116+
2117+ if (doc1 != doc2)
2118+ /* `documentation' options always follow normal options (or
2119+ documentation options that *look* like normal options). */
2120+ return doc1 - doc2;
2121+ else if (!short1 && !short2 && long1 && long2)
2122+ /* Only long options. */
2123+ return strcasecmp (long1, long2);
2124+ else
2125+ /* Compare short/short, long/short, short/long, using the first
2126+ character of long options. Entries without *any* valid
2127+ options (such as options with OPTION_HIDDEN set) will be put
2128+ first, but as they're not displayed, it doesn't matter where
2129+ they are. */
2130+ {
2131+ char first1 = short1 ? short1 : long1 ? *long1 : 0;
2132+ char first2 = short2 ? short2 : long2 ? *long2 : 0;
2133+#ifdef _tolower
2134+ int lower_cmp = _tolower (first1) - _tolower (first2);
2135+#else
2136+ int lower_cmp = tolower (first1) - tolower (first2);
2137+#endif
2138+ /* Compare ignoring case, except when the options are both the
2139+ same letter, in which case lower-case always comes first. */
2140+ return lower_cmp ? lower_cmp : first2 - first1;
2141+ }
2142+ }
2143+ else
2144+ /* Within the same cluster, but not the same group, so just compare
2145+ groups. */
2146+ return group_cmp (group1, group2, 0);
2147+}
2148+
2149+/* Version of hol_entry_cmp with correct signature for qsort. */
2150+static int
2151+hol_entry_qcmp (const void *entry1_v, const void *entry2_v)
2152+{
2153+ return hol_entry_cmp (entry1_v, entry2_v);
2154+}
2155+
2156+/* Sort HOL by group and alphabetically by option name (with short options
2157+ taking precedence over long). Since the sorting is for display purposes
2158+ only, the shadowing of options isn't effected. */
2159+static void
2160+hol_sort (struct hol *hol)
2161+{
2162+ if (hol->num_entries > 0)
2163+ qsort (hol->entries, hol->num_entries, sizeof (struct hol_entry),
2164+ hol_entry_qcmp);
2165+}
2166+
2167+/* Append MORE to HOL, destroying MORE in the process. Options in HOL shadow
2168+ any in MORE with the same name. */
2169+static void
2170+hol_append (struct hol *hol, struct hol *more)
2171+{
2172+ struct hol_cluster **cl_end = &hol->clusters;
2173+
2174+ /* Steal MORE's cluster list, and add it to the end of HOL's. */
2175+ while (*cl_end)
2176+ cl_end = &(*cl_end)->next;
2177+ *cl_end = more->clusters;
2178+ more->clusters = 0;
2179+
2180+ /* Merge entries. */
2181+ if (more->num_entries > 0)
2182+ {
2183+ if (hol->num_entries == 0)
2184+ {
2185+ hol->num_entries = more->num_entries;
2186+ hol->entries = more->entries;
2187+ hol->short_options = more->short_options;
2188+ more->num_entries = 0; /* Mark MORE's fields as invalid. */
2189+ }
2190+ else
2191+ /* Append the entries in MORE to those in HOL, taking care to only add
2192+ non-shadowed SHORT_OPTIONS values. */
2193+ {
2194+ unsigned left;
2195+ char *so, *more_so;
2196+ struct hol_entry *e;
2197+ unsigned num_entries = hol->num_entries + more->num_entries;
2198+ struct hol_entry *entries =
2199+ malloc (num_entries * sizeof (struct hol_entry));
2200+ unsigned hol_so_len = strlen (hol->short_options);
2201+ char *short_options =
2202+ malloc (hol_so_len + strlen (more->short_options) + 1);
2203+
2204+ assert (entries && short_options);
2205+#if SIZE_MAX <= UINT_MAX
2206+ assert (num_entries <= SIZE_MAX / sizeof (struct hol_entry));
2207+#endif
2208+
2209+ mempcpy (mempcpy (entries, hol->entries,
2210+ hol->num_entries * sizeof (struct hol_entry)),
2211+ more->entries,
2212+ more->num_entries * sizeof (struct hol_entry));
2213+
2214+ mempcpy (short_options, hol->short_options, hol_so_len);
2215+
2216+ /* Fix up the short options pointers from HOL. */
2217+ for (e = entries, left = hol->num_entries; left > 0; e++, left--)
2218+ e->short_options += (short_options - hol->short_options);
2219+
2220+ /* Now add the short options from MORE, fixing up its entries
2221+ too. */
2222+ so = short_options + hol_so_len;
2223+ more_so = more->short_options;
2224+ for (left = more->num_entries; left > 0; e++, left--)
2225+ {
2226+ int opts_left;
2227+ const struct argp_option *opt;
2228+
2229+ e->short_options = so;
2230+
2231+ for (opts_left = e->num, opt = e->opt; opts_left; opt++, opts_left--)
2232+ {
2233+ int ch = *more_so;
2234+ if (oshort (opt) && ch == opt->key)
2235+ /* The next short option in MORE_SO, CH, is from OPT. */
2236+ {
2237+ if (! find_char (ch, short_options,
2238+ short_options + hol_so_len))
2239+ /* The short option CH isn't shadowed by HOL's options,
2240+ so add it to the sum. */
2241+ *so++ = ch;
2242+ more_so++;
2243+ }
2244+ }
2245+ }
2246+
2247+ *so = '\0';
2248+
2249+ free (hol->entries);
2250+ free (hol->short_options);
2251+
2252+ hol->entries = entries;
2253+ hol->num_entries = num_entries;
2254+ hol->short_options = short_options;
2255+ }
2256+ }
2257+
2258+ hol_free (more);
2259+}
2260+
2261+/* Inserts enough spaces to make sure STREAM is at column COL. */
2262+static void
2263+indent_to (argp_fmtstream_t stream, unsigned col)
2264+{
2265+ int needed = col - __argp_fmtstream_point (stream);
2266+ while (needed-- > 0)
2267+ __argp_fmtstream_putc (stream, ' ');
2268+}
2269+
2270+/* Output to STREAM either a space, or a newline if there isn't room for at
2271+ least ENSURE characters before the right margin. */
2272+static void
2273+space (argp_fmtstream_t stream, size_t ensure)
2274+{
2275+ if (__argp_fmtstream_point (stream) + ensure
2276+ >= __argp_fmtstream_rmargin (stream))
2277+ __argp_fmtstream_putc (stream, '\n');
2278+ else
2279+ __argp_fmtstream_putc (stream, ' ');
2280+}
2281+
2282+/* If the option REAL has an argument, we print it in using the printf
2283+ format REQ_FMT or OPT_FMT depending on whether it's a required or
2284+ optional argument. */
2285+static void
2286+arg (const struct argp_option *real, const char *req_fmt, const char *opt_fmt,
2287+ const char *domain, argp_fmtstream_t stream)
2288+{
2289+ if (real->arg)
2290+ {
2291+ if (real->flags & OPTION_ARG_OPTIONAL)
2292+ __argp_fmtstream_printf (stream, opt_fmt,
2293+ dgettext (domain, real->arg));
2294+ else
2295+ __argp_fmtstream_printf (stream, req_fmt,
2296+ dgettext (domain, real->arg));
2297+ }
2298+}
2299+
2300+/* Helper functions for hol_entry_help. */
2301+
2302+/* State used during the execution of hol_help. */
2303+struct hol_help_state
2304+{
2305+ /* PREV_ENTRY should contain the previous entry printed, or 0. */
2306+ struct hol_entry *prev_entry;
2307+
2308+ /* If an entry is in a different group from the previous one, and SEP_GROUPS
2309+ is true, then a blank line will be printed before any output. */
2310+ int sep_groups;
2311+
2312+ /* True if a duplicate option argument was suppressed (only ever set if
2313+ UPARAMS.dup_args is false). */
2314+ int suppressed_dup_arg;
2315+};
2316+
2317+/* Some state used while printing a help entry (used to communicate with
2318+ helper functions). See the doc for hol_entry_help for more info, as most
2319+ of the fields are copied from its arguments. */
2320+struct pentry_state
2321+{
2322+ const struct hol_entry *entry;
2323+ argp_fmtstream_t stream;
2324+ struct hol_help_state *hhstate;
2325+
2326+ /* True if nothing's been printed so far. */
2327+ int first;
2328+
2329+ /* If non-zero, the state that was used to print this help. */
2330+ const struct argp_state *state;
2331+};
2332+
2333+/* If a user doc filter should be applied to DOC, do so. */
2334+static const char *
2335+filter_doc (const char *doc, int key, const struct argp *argp,
2336+ const struct argp_state *state)
2337+{
2338+ if (argp && argp->help_filter)
2339+ /* We must apply a user filter to this output. */
2340+ {
2341+ void *input = __argp_input (argp, state);
2342+ return (*argp->help_filter) (key, doc, input);
2343+ }
2344+ else
2345+ /* No filter. */
2346+ return doc;
2347+}
2348+
2349+/* Prints STR as a header line, with the margin lines set appropiately, and
2350+ notes the fact that groups should be separated with a blank line. ARGP is
2351+ the argp that should dictate any user doc filtering to take place. Note
2352+ that the previous wrap margin isn't restored, but the left margin is reset
2353+ to 0. */
2354+static void
2355+print_header (const char *str, const struct argp *argp,
2356+ struct pentry_state *pest)
2357+{
2358+ const char *tstr = dgettext (argp->argp_domain, str);
2359+ const char *fstr = filter_doc (tstr, ARGP_KEY_HELP_HEADER, argp, pest->state);
2360+
2361+ if (fstr)
2362+ {
2363+ if (*fstr)
2364+ {
2365+ if (pest->hhstate->prev_entry)
2366+ /* Precede with a blank line. */
2367+ __argp_fmtstream_putc (pest->stream, '\n');
2368+ indent_to (pest->stream, uparams.header_col);
2369+ __argp_fmtstream_set_lmargin (pest->stream, uparams.header_col);
2370+ __argp_fmtstream_set_wmargin (pest->stream, uparams.header_col);
2371+ __argp_fmtstream_puts (pest->stream, fstr);
2372+ __argp_fmtstream_set_lmargin (pest->stream, 0);
2373+ __argp_fmtstream_putc (pest->stream, '\n');
2374+ }
2375+
2376+ pest->hhstate->sep_groups = 1; /* Separate subsequent groups. */
2377+ }
2378+
2379+ if (fstr != tstr)
2380+ free ((char *) fstr);
2381+}
2382+
2383+/* Inserts a comma if this isn't the first item on the line, and then makes
2384+ sure we're at least to column COL. If this *is* the first item on a line,
2385+ prints any pending whitespace/headers that should precede this line. Also
2386+ clears FIRST. */
2387+static void
2388+comma (unsigned col, struct pentry_state *pest)
2389+{
2390+ if (pest->first)
2391+ {
2392+ const struct hol_entry *pe = pest->hhstate->prev_entry;
2393+ const struct hol_cluster *cl = pest->entry->cluster;
2394+
2395+ if (pest->hhstate->sep_groups && pe && pest->entry->group != pe->group)
2396+ __argp_fmtstream_putc (pest->stream, '\n');
2397+
2398+ if (cl && cl->header && *cl->header
2399+ && (!pe
2400+ || (pe->cluster != cl
2401+ && !hol_cluster_is_child (pe->cluster, cl))))
2402+ /* If we're changing clusters, then this must be the start of the
2403+ ENTRY's cluster unless that is an ancestor of the previous one
2404+ (in which case we had just popped into a sub-cluster for a bit).
2405+ If so, then print the cluster's header line. */
2406+ {
2407+ int old_wm = __argp_fmtstream_wmargin (pest->stream);
2408+ print_header (cl->header, cl->argp, pest);
2409+ __argp_fmtstream_set_wmargin (pest->stream, old_wm);
2410+ }
2411+
2412+ pest->first = 0;
2413+ }
2414+ else
2415+ __argp_fmtstream_puts (pest->stream, ", ");
2416+
2417+ indent_to (pest->stream, col);
2418+}
2419+
2420+/* Print help for ENTRY to STREAM. */
2421+static void
2422+hol_entry_help (struct hol_entry *entry, const struct argp_state *state,
2423+ argp_fmtstream_t stream, struct hol_help_state *hhstate)
2424+{
2425+ unsigned num;
2426+ const struct argp_option *real = entry->opt, *opt;
2427+ char *so = entry->short_options;
2428+ int have_long_opt = 0; /* We have any long options. */
2429+ /* Saved margins. */
2430+ int old_lm = __argp_fmtstream_set_lmargin (stream, 0);
2431+ int old_wm = __argp_fmtstream_wmargin (stream);
2432+ /* PEST is a state block holding some of our variables that we'd like to
2433+ share with helper functions. */
2434+ struct pentry_state pest = { entry, stream, hhstate, 1, state };
2435+
2436+ if (! odoc (real))
2437+ for (opt = real, num = entry->num; num > 0; opt++, num--)
2438+ if (opt->name && ovisible (opt))
2439+ {
2440+ have_long_opt = 1;
2441+ break;
2442+ }
2443+
2444+ /* First emit short options. */
2445+ __argp_fmtstream_set_wmargin (stream, uparams.short_opt_col); /* For truly bizarre cases. */
2446+ for (opt = real, num = entry->num; num > 0; opt++, num--)
2447+ if (oshort (opt) && opt->key == *so)
2448+ /* OPT has a valid (non shadowed) short option. */
2449+ {
2450+ if (ovisible (opt))
2451+ {
2452+ comma (uparams.short_opt_col, &pest);
2453+ __argp_fmtstream_putc (stream, '-');
2454+ __argp_fmtstream_putc (stream, *so);
2455+ if (!have_long_opt || uparams.dup_args)
2456+ arg (real, " %s", "[%s]",
2457+ state == NULL ? NULL : state->root_argp->argp_domain,
2458+ stream);
2459+ else if (real->arg)
2460+ hhstate->suppressed_dup_arg = 1;
2461+ }
2462+ so++;
2463+ }
2464+
2465+ /* Now, long options. */
2466+ if (odoc (real))
2467+ /* A `documentation' option. */
2468+ {
2469+ __argp_fmtstream_set_wmargin (stream, uparams.doc_opt_col);
2470+ for (opt = real, num = entry->num; num > 0; opt++, num--)
2471+ if (opt->name && ovisible (opt))
2472+ {
2473+ comma (uparams.doc_opt_col, &pest);
2474+ /* Calling gettext here isn't quite right, since sorting will
2475+ have been done on the original; but documentation options
2476+ should be pretty rare anyway... */
2477+ __argp_fmtstream_puts (stream,
2478+ dgettext (state == NULL ? NULL
2479+ : state->root_argp->argp_domain,
2480+ opt->name));
2481+ }
2482+ }
2483+ else
2484+ /* A real long option. */
2485+ {
2486+ __argp_fmtstream_set_wmargin (stream, uparams.long_opt_col);
2487+ for (opt = real, num = entry->num; num > 0; opt++, num--)
2488+ if (opt->name && ovisible (opt))
2489+ {
2490+ comma (uparams.long_opt_col, &pest);
2491+ __argp_fmtstream_printf (stream, "--%s", opt->name);
2492+ arg (real, "=%s", "[=%s]",
2493+ state == NULL ? NULL : state->root_argp->argp_domain, stream);
2494+ }
2495+ }
2496+
2497+ /* Next, documentation strings. */
2498+ __argp_fmtstream_set_lmargin (stream, 0);
2499+
2500+ if (pest.first)
2501+ {
2502+ /* Didn't print any switches, what's up? */
2503+ if (!oshort (real) && !real->name)
2504+ /* This is a group header, print it nicely. */
2505+ print_header (real->doc, entry->argp, &pest);
2506+ else
2507+ /* Just a totally shadowed option or null header; print nothing. */
2508+ goto cleanup; /* Just return, after cleaning up. */
2509+ }
2510+ else
2511+ {
2512+ const char *tstr = real->doc ? dgettext (state == NULL ? NULL
2513+ : state->root_argp->argp_domain,
2514+ real->doc) : 0;
2515+ const char *fstr = filter_doc (tstr, real->key, entry->argp, state);
2516+ if (fstr && *fstr)
2517+ {
2518+ unsigned int col = __argp_fmtstream_point (stream);
2519+
2520+ __argp_fmtstream_set_lmargin (stream, uparams.opt_doc_col);
2521+ __argp_fmtstream_set_wmargin (stream, uparams.opt_doc_col);
2522+
2523+ if (col > (unsigned int) (uparams.opt_doc_col + 3))
2524+ __argp_fmtstream_putc (stream, '\n');
2525+ else if (col >= (unsigned int) uparams.opt_doc_col)
2526+ __argp_fmtstream_puts (stream, " ");
2527+ else
2528+ indent_to (stream, uparams.opt_doc_col);
2529+
2530+ __argp_fmtstream_puts (stream, fstr);
2531+ }
2532+ if (fstr && fstr != tstr)
2533+ free ((char *) fstr);
2534+
2535+ /* Reset the left margin. */
2536+ __argp_fmtstream_set_lmargin (stream, 0);
2537+ __argp_fmtstream_putc (stream, '\n');
2538+ }
2539+
2540+ hhstate->prev_entry = entry;
2541+
2542+cleanup:
2543+ __argp_fmtstream_set_lmargin (stream, old_lm);
2544+ __argp_fmtstream_set_wmargin (stream, old_wm);
2545+}
2546+
2547+/* Output a long help message about the options in HOL to STREAM. */
2548+static void
2549+hol_help (struct hol *hol, const struct argp_state *state,
2550+ argp_fmtstream_t stream)
2551+{
2552+ unsigned num;
2553+ struct hol_entry *entry;
2554+ struct hol_help_state hhstate = { 0, 0, 0 };
2555+
2556+ for (entry = hol->entries, num = hol->num_entries; num > 0; entry++, num--)
2557+ hol_entry_help (entry, state, stream, &hhstate);
2558+
2559+ if (hhstate.suppressed_dup_arg && uparams.dup_args_note)
2560+ {
2561+ const char *tstr = dgettext (state == NULL ? NULL
2562+ : state->root_argp->argp_domain, "\
2563+Mandatory or optional arguments to long options are also mandatory or \
2564+optional for any corresponding short options.");
2565+ const char *fstr = filter_doc (tstr, ARGP_KEY_HELP_DUP_ARGS_NOTE,
2566+ state ? state->root_argp : 0, state);
2567+ if (fstr && *fstr)
2568+ {
2569+ __argp_fmtstream_putc (stream, '\n');
2570+ __argp_fmtstream_puts (stream, fstr);
2571+ __argp_fmtstream_putc (stream, '\n');
2572+ }
2573+ if (fstr && fstr != tstr)
2574+ free ((char *) fstr);
2575+ }
2576+}
2577+
2578+/* Helper functions for hol_usage. */
2579+
2580+/* If OPT is a short option without an arg, append its key to the string
2581+ pointer pointer to by COOKIE, and advance the pointer. */
2582+static int
2583+add_argless_short_opt (const struct argp_option *opt,
2584+ const struct argp_option *real,
2585+ const char *domain, void *cookie)
2586+{
2587+ char **snao_end = cookie;
2588+ if (!(opt->arg || real->arg)
2589+ && !((opt->flags | real->flags) & OPTION_NO_USAGE))
2590+ *(*snao_end)++ = opt->key;
2591+ return 0;
2592+}
2593+
2594+/* If OPT is a short option with an arg, output a usage entry for it to the
2595+ stream pointed at by COOKIE. */
2596+static int
2597+usage_argful_short_opt (const struct argp_option *opt,
2598+ const struct argp_option *real,
2599+ const char *domain, void *cookie)
2600+{
2601+ argp_fmtstream_t stream = cookie;
2602+ const char *arg = opt->arg;
2603+ int flags = opt->flags | real->flags;
2604+
2605+ if (! arg)
2606+ arg = real->arg;
2607+
2608+ if (arg && !(flags & OPTION_NO_USAGE))
2609+ {
2610+ arg = dgettext (domain, arg);
2611+
2612+ if (flags & OPTION_ARG_OPTIONAL)
2613+ __argp_fmtstream_printf (stream, " [-%c[%s]]", opt->key, arg);
2614+ else
2615+ {
2616+ /* Manually do line wrapping so that it (probably) won't
2617+ get wrapped at the embedded space. */
2618+ space (stream, 6 + strlen (arg));
2619+ __argp_fmtstream_printf (stream, "[-%c %s]", opt->key, arg);
2620+ }
2621+ }
2622+
2623+ return 0;
2624+}
2625+
2626+/* Output a usage entry for the long option opt to the stream pointed at by
2627+ COOKIE. */
2628+static int
2629+usage_long_opt (const struct argp_option *opt,
2630+ const struct argp_option *real,
2631+ const char *domain, void *cookie)
2632+{
2633+ argp_fmtstream_t stream = cookie;
2634+ const char *arg = opt->arg;
2635+ int flags = opt->flags | real->flags;
2636+
2637+ if (! arg)
2638+ arg = real->arg;
2639+
2640+ if (! (flags & OPTION_NO_USAGE))
2641+ {
2642+ if (arg)
2643+ {
2644+ arg = dgettext (domain, arg);
2645+ if (flags & OPTION_ARG_OPTIONAL)
2646+ __argp_fmtstream_printf (stream, " [--%s[=%s]]", opt->name, arg);
2647+ else
2648+ __argp_fmtstream_printf (stream, " [--%s=%s]", opt->name, arg);
2649+ }
2650+ else
2651+ __argp_fmtstream_printf (stream, " [--%s]", opt->name);
2652+ }
2653+
2654+ return 0;
2655+}
2656+
2657+/* Print a short usage description for the arguments in HOL to STREAM. */
2658+static void
2659+hol_usage (struct hol *hol, argp_fmtstream_t stream)
2660+{
2661+ if (hol->num_entries > 0)
2662+ {
2663+ unsigned nentries;
2664+ struct hol_entry *entry;
2665+ char *short_no_arg_opts = alloca (strlen (hol->short_options) + 1);
2666+ char *snao_end = short_no_arg_opts;
2667+
2668+ /* First we put a list of short options without arguments. */
2669+ for (entry = hol->entries, nentries = hol->num_entries
2670+ ; nentries > 0
2671+ ; entry++, nentries--)
2672+ hol_entry_short_iterate (entry, add_argless_short_opt,
2673+ entry->argp->argp_domain, &snao_end);
2674+ if (snao_end > short_no_arg_opts)
2675+ {
2676+ *snao_end++ = 0;
2677+ __argp_fmtstream_printf (stream, " [-%s]", short_no_arg_opts);
2678+ }
2679+
2680+ /* Now a list of short options *with* arguments. */
2681+ for (entry = hol->entries, nentries = hol->num_entries
2682+ ; nentries > 0
2683+ ; entry++, nentries--)
2684+ hol_entry_short_iterate (entry, usage_argful_short_opt,
2685+ entry->argp->argp_domain, stream);
2686+
2687+ /* Finally, a list of long options (whew!). */
2688+ for (entry = hol->entries, nentries = hol->num_entries
2689+ ; nentries > 0
2690+ ; entry++, nentries--)
2691+ hol_entry_long_iterate (entry, usage_long_opt,
2692+ entry->argp->argp_domain, stream);
2693+ }
2694+}
2695+
2696+/* Make a HOL containing all levels of options in ARGP. CLUSTER is the
2697+ cluster in which ARGP's entries should be clustered, or 0. */
2698+static struct hol *
2699+argp_hol (const struct argp *argp, struct hol_cluster *cluster)
2700+{
2701+ const struct argp_child *child = argp->children;
2702+ struct hol *hol = make_hol (argp, cluster);
2703+ if (child)
2704+ while (child->argp)
2705+ {
2706+ struct hol_cluster *child_cluster =
2707+ ((child->group || child->header)
2708+ /* Put CHILD->argp within its own cluster. */
2709+ ? hol_add_cluster (hol, child->group, child->header,
2710+ child - argp->children, cluster, argp)
2711+ /* Just merge it into the parent's cluster. */
2712+ : cluster);
2713+ hol_append (hol, argp_hol (child->argp, child_cluster)) ;
2714+ child++;
2715+ }
2716+ return hol;
2717+}
2718+
2719+/* Calculate how many different levels with alternative args strings exist in
2720+ ARGP. */
2721+static size_t
2722+argp_args_levels (const struct argp *argp)
2723+{
2724+ size_t levels = 0;
2725+ const struct argp_child *child = argp->children;
2726+
2727+ if (argp->args_doc && strchr (argp->args_doc, '\n'))
2728+ levels++;
2729+
2730+ if (child)
2731+ while (child->argp)
2732+ levels += argp_args_levels ((child++)->argp);
2733+
2734+ return levels;
2735+}
2736+
2737+/* Print all the non-option args documented in ARGP to STREAM. Any output is
2738+ preceded by a space. LEVELS is a pointer to a byte vector the length
2739+ returned by argp_args_levels; it should be initialized to zero, and
2740+ updated by this routine for the next call if ADVANCE is true. True is
2741+ returned as long as there are more patterns to output. */
2742+static int
2743+argp_args_usage (const struct argp *argp, const struct argp_state *state,
2744+ char **levels, int advance, argp_fmtstream_t stream)
2745+{
2746+ char *our_level = *levels;
2747+ int multiple = 0;
2748+ const struct argp_child *child = argp->children;
2749+ const char *tdoc = dgettext (argp->argp_domain, argp->args_doc), *nl = 0;
2750+ const char *fdoc = filter_doc (tdoc, ARGP_KEY_HELP_ARGS_DOC, argp, state);
2751+
2752+ if (fdoc)
2753+ {
2754+ const char *cp = fdoc;
2755+ nl = strchrnul (cp, '\n');
2756+ if (*nl != '\0')
2757+ /* This is a `multi-level' args doc; advance to the correct position
2758+ as determined by our state in LEVELS, and update LEVELS. */
2759+ {
2760+ int i;
2761+ multiple = 1;
2762+ for (i = 0; i < *our_level; i++)
2763+ cp = nl + 1, nl = strchrnul (cp, '\n');
2764+ (*levels)++;
2765+ }
2766+
2767+ /* Manually do line wrapping so that it (probably) won't get wrapped at
2768+ any embedded spaces. */
2769+ space (stream, 1 + nl - cp);
2770+
2771+ __argp_fmtstream_write (stream, cp, nl - cp);
2772+ }
2773+ if (fdoc && fdoc != tdoc)
2774+ free ((char *)fdoc); /* Free user's modified doc string. */
2775+
2776+ if (child)
2777+ while (child->argp)
2778+ advance = !argp_args_usage ((child++)->argp, state, levels, advance, stream);
2779+
2780+ if (advance && multiple)
2781+ {
2782+ /* Need to increment our level. */
2783+ if (*nl)
2784+ /* There's more we can do here. */
2785+ {
2786+ (*our_level)++;
2787+ advance = 0; /* Our parent shouldn't advance also. */
2788+ }
2789+ else if (*our_level > 0)
2790+ /* We had multiple levels, but used them up; reset to zero. */
2791+ *our_level = 0;
2792+ }
2793+
2794+ return !advance;
2795+}
2796+
2797+/* Print the documentation for ARGP to STREAM; if POST is false, then
2798+ everything preceeding a `\v' character in the documentation strings (or
2799+ the whole string, for those with none) is printed, otherwise, everything
2800+ following the `\v' character (nothing for strings without). Each separate
2801+ bit of documentation is separated a blank line, and if PRE_BLANK is true,
2802+ then the first is as well. If FIRST_ONLY is true, only the first
2803+ occurrence is output. Returns true if anything was output. */
2804+static int
2805+argp_doc (const struct argp *argp, const struct argp_state *state,
2806+ int post, int pre_blank, int first_only,
2807+ argp_fmtstream_t stream)
2808+{
2809+ const char *text;
2810+ const char *inp_text;
2811+ void *input = 0;
2812+ int anything = 0;
2813+ size_t inp_text_limit = 0;
2814+ const char *doc = dgettext (argp->argp_domain, argp->doc);
2815+ const struct argp_child *child = argp->children;
2816+
2817+ if (doc)
2818+ {
2819+ char *vt = strchr (doc, '\v');
2820+ inp_text = post ? (vt ? vt + 1 : 0) : doc;
2821+ inp_text_limit = (!post && vt) ? (vt - doc) : 0;
2822+ }
2823+ else
2824+ inp_text = 0;
2825+
2826+ if (argp->help_filter)
2827+ /* We have to filter the doc strings. */
2828+ {
2829+ if (inp_text_limit)
2830+ /* Copy INP_TEXT so that it's nul-terminated. */
2831+ inp_text = strndup (inp_text, inp_text_limit);
2832+ input = __argp_input (argp, state);
2833+ text =
2834+ (*argp->help_filter) (post
2835+ ? ARGP_KEY_HELP_POST_DOC
2836+ : ARGP_KEY_HELP_PRE_DOC,
2837+ inp_text, input);
2838+ }
2839+ else
2840+ text = (const char *) inp_text;
2841+
2842+ if (text)
2843+ {
2844+ if (pre_blank)
2845+ __argp_fmtstream_putc (stream, '\n');
2846+
2847+ if (text == inp_text && inp_text_limit)
2848+ __argp_fmtstream_write (stream, inp_text, inp_text_limit);
2849+ else
2850+ __argp_fmtstream_puts (stream, text);
2851+
2852+ if (__argp_fmtstream_point (stream) > __argp_fmtstream_lmargin (stream))
2853+ __argp_fmtstream_putc (stream, '\n');
2854+
2855+ anything = 1;
2856+ }
2857+
2858+ if (text && text != inp_text)
2859+ free ((char *) text); /* Free TEXT returned from the help filter. */
2860+ if (inp_text && inp_text_limit && argp->help_filter)
2861+ free ((char *) inp_text); /* We copied INP_TEXT, so free it now. */
2862+
2863+ if (post && argp->help_filter)
2864+ /* Now see if we have to output a ARGP_KEY_HELP_EXTRA text. */
2865+ {
2866+ text = (*argp->help_filter) (ARGP_KEY_HELP_EXTRA, 0, input);
2867+ if (text)
2868+ {
2869+ if (anything || pre_blank)
2870+ __argp_fmtstream_putc (stream, '\n');
2871+ __argp_fmtstream_puts (stream, text);
2872+ free ((char *) text);
2873+ if (__argp_fmtstream_point (stream)
2874+ > __argp_fmtstream_lmargin (stream))
2875+ __argp_fmtstream_putc (stream, '\n');
2876+ anything = 1;
2877+ }
2878+ }
2879+
2880+ if (child)
2881+ while (child->argp && !(first_only && anything))
2882+ anything |=
2883+ argp_doc ((child++)->argp, state,
2884+ post, anything || pre_blank, first_only,
2885+ stream);
2886+
2887+ return anything;
2888+}
2889+
2890+/* Output a usage message for ARGP to STREAM. If called from
2891+ argp_state_help, STATE is the relevent parsing state. FLAGS are from the
2892+ set ARGP_HELP_*. NAME is what to use wherever a `program name' is
2893+ needed. */
2894+static void
2895+_help (const struct argp *argp, const struct argp_state *state, FILE *stream,
2896+ unsigned flags, char *name)
2897+{
2898+ int anything = 0; /* Whether we've output anything. */
2899+ struct hol *hol = 0;
2900+ argp_fmtstream_t fs;
2901+
2902+ if (! stream)
2903+ return;
2904+
2905+#if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
2906+ flockfile (stream);
2907+#endif
2908+
2909+ fill_in_uparams (state);
2910+
2911+ fs = __argp_make_fmtstream (stream, 0, uparams.rmargin, 0);
2912+ if (! fs)
2913+ {
2914+#if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
2915+ funlockfile (stream);
2916+#endif
2917+ return;
2918+ }
2919+
2920+ if (flags & (ARGP_HELP_USAGE | ARGP_HELP_SHORT_USAGE | ARGP_HELP_LONG))
2921+ {
2922+ hol = argp_hol (argp, 0);
2923+
2924+ /* If present, these options always come last. */
2925+ hol_set_group (hol, "help", -1);
2926+ hol_set_group (hol, "version", -1);
2927+
2928+ hol_sort (hol);
2929+ }
2930+
2931+ if (flags & (ARGP_HELP_USAGE | ARGP_HELP_SHORT_USAGE))
2932+ /* Print a short `Usage:' message. */
2933+ {
2934+ int first_pattern = 1, more_patterns;
2935+ size_t num_pattern_levels = argp_args_levels (argp);
2936+ char *pattern_levels = alloca (num_pattern_levels);
2937+
2938+ memset (pattern_levels, 0, num_pattern_levels);
2939+
2940+ do
2941+ {
2942+ int old_lm;
2943+ int old_wm = __argp_fmtstream_set_wmargin (fs, uparams.usage_indent);
2944+ char *levels = pattern_levels;
2945+
2946+ if (first_pattern)
2947+ __argp_fmtstream_printf (fs, "%s %s",
2948+ dgettext (argp->argp_domain, "Usage:"),
2949+ name);
2950+ else
2951+ __argp_fmtstream_printf (fs, "%s %s",
2952+ dgettext (argp->argp_domain, " or: "),
2953+ name);
2954+
2955+ /* We set the lmargin as well as the wmargin, because hol_usage
2956+ manually wraps options with newline to avoid annoying breaks. */
2957+ old_lm = __argp_fmtstream_set_lmargin (fs, uparams.usage_indent);
2958+
2959+ if (flags & ARGP_HELP_SHORT_USAGE)
2960+ /* Just show where the options go. */
2961+ {
2962+ if (hol->num_entries > 0)
2963+ __argp_fmtstream_puts (fs, dgettext (argp->argp_domain,
2964+ " [OPTION...]"));
2965+ }
2966+ else
2967+ /* Actually print the options. */
2968+ {
2969+ hol_usage (hol, fs);
2970+ flags |= ARGP_HELP_SHORT_USAGE; /* But only do so once. */
2971+ }
2972+
2973+ more_patterns = argp_args_usage (argp, state, &levels, 1, fs);
2974+
2975+ __argp_fmtstream_set_wmargin (fs, old_wm);
2976+ __argp_fmtstream_set_lmargin (fs, old_lm);
2977+
2978+ __argp_fmtstream_putc (fs, '\n');
2979+ anything = 1;
2980+
2981+ first_pattern = 0;
2982+ }
2983+ while (more_patterns);
2984+ }
2985+
2986+ if (flags & ARGP_HELP_PRE_DOC)
2987+ anything |= argp_doc (argp, state, 0, 0, 1, fs);
2988+
2989+ if (flags & ARGP_HELP_SEE)
2990+ {
2991+ __argp_fmtstream_printf (fs, dgettext (argp->argp_domain, "\
2992+Try `%s --help' or `%s --usage' for more information.\n"),
2993+ name, name);
2994+ anything = 1;
2995+ }
2996+
2997+ if (flags & ARGP_HELP_LONG)
2998+ /* Print a long, detailed help message. */
2999+ {
3000+ /* Print info about all the options. */
3001+ if (hol->num_entries > 0)
3002+ {
3003+ if (anything)
3004+ __argp_fmtstream_putc (fs, '\n');
3005+ hol_help (hol, state, fs);
3006+ anything = 1;
3007+ }
3008+ }
3009+
3010+ if (flags & ARGP_HELP_POST_DOC)
3011+ /* Print any documentation strings at the end. */
3012+ anything |= argp_doc (argp, state, 1, anything, 0, fs);
3013+
3014+ if ((flags & ARGP_HELP_BUG_ADDR) && argp_program_bug_address)
3015+ {
3016+ if (anything)
3017+ __argp_fmtstream_putc (fs, '\n');
3018+ __argp_fmtstream_printf (fs, dgettext (argp->argp_domain,
3019+ "Report bugs to %s.\n"),
3020+ argp_program_bug_address);
3021+ anything = 1;
3022+ }
3023+
3024+#if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
3025+ funlockfile (stream);
3026+#endif
3027+
3028+ if (hol)
3029+ hol_free (hol);
3030+
3031+ __argp_fmtstream_free (fs);
3032+}
3033+
3034+/* Output a usage message for ARGP to STREAM. FLAGS are from the set
3035+ ARGP_HELP_*. NAME is what to use wherever a `program name' is needed. */
3036+void argp_help (const struct argp *argp, FILE *stream,
3037+ unsigned flags, char *name)
3038+{
3039+ _help (argp, 0, stream, flags, name);
3040+}
3041+
3042+char *
3043+__argp_short_program_name (void)
3044+{
3045+# ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
3046+/*
3047+ * uClibc provides both program_invocation_name and
3048+ * program_invocation_short_name
3049+ */
3050+ return (char *) program_invocation_short_name;
3051+# else
3052+ /* FIXME: What now? Miles suggests that it is better to use NULL,
3053+ but currently the value is passed on directly to fputs_unlocked,
3054+ so that requires more changes. */
3055+# if __GNUC__
3056+# warning No reasonable value to return
3057+# endif /* __GNUC__ */
3058+ return "";
3059+# endif
3060+}
3061+
3062+/* Output, if appropriate, a usage message for STATE to STREAM. FLAGS are
3063+ from the set ARGP_HELP_*. */
3064+void
3065+argp_state_help (const struct argp_state *state, FILE *stream, unsigned flags)
3066+{
3067+ if ((!state || ! (state->flags & ARGP_NO_ERRS)) && stream)
3068+ {
3069+ if (state && (state->flags & ARGP_LONG_ONLY))
3070+ flags |= ARGP_HELP_LONG_ONLY;
3071+
3072+ _help (state ? state->root_argp : 0, state, stream, flags,
3073+ state ? state->name : __argp_short_program_name ());
3074+
3075+ if (!state || ! (state->flags & ARGP_NO_EXIT))
3076+ {
3077+ if (flags & ARGP_HELP_EXIT_ERR)
3078+ exit (argp_err_exit_status);
3079+ if (flags & ARGP_HELP_EXIT_OK)
3080+ exit (0);
3081+ }
3082+ }
3083+}
3084+
3085+/* If appropriate, print the printf string FMT and following args, preceded
3086+ by the program name and `:', to stderr, and followed by a `Try ... --help'
3087+ message, then exit (1). */
3088+void
3089+argp_error (const struct argp_state *state, const char *fmt, ...)
3090+{
3091+ if (!state || !(state->flags & ARGP_NO_ERRS))
3092+ {
3093+ FILE *stream = state ? state->err_stream : stderr;
3094+
3095+ if (stream)
3096+ {
3097+ va_list ap;
3098+
3099+#if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
3100+ flockfile (stream);
3101+#endif
3102+
3103+ va_start (ap, fmt);
3104+
3105+#if defined _LIBC && defined USE_IN_LIBIO
3106+ char *buf;
3107+
3108+ if (_IO_vasprintf (&buf, fmt, ap) < 0)
3109+ buf = NULL;
3110+
3111+ __fxprintf (stream, "%s: %s\n",
3112+ state ? state->name : __argp_short_program_name (), buf);
3113+
3114+ free (buf);
3115+#else
3116+ fputs_unlocked (state ? state->name : __argp_short_program_name (),
3117+ stream);
3118+ putc_unlocked (':', stream);
3119+ putc_unlocked (' ', stream);
3120+
3121+ vfprintf (stream, fmt, ap);
3122+
3123+ putc_unlocked ('\n', stream);
3124+#endif
3125+
3126+ argp_state_help (state, stream, ARGP_HELP_STD_ERR);
3127+
3128+ va_end (ap);
3129+
3130+#if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
3131+ funlockfile (stream);
3132+#endif
3133+ }
3134+ }
3135+}
3136+
3137+/* Similar to the standard gnu error-reporting function error(), but will
3138+ respect the ARGP_NO_EXIT and ARGP_NO_ERRS flags in STATE, and will print
3139+ to STATE->err_stream. This is useful for argument parsing code that is
3140+ shared between program startup (when exiting is desired) and runtime
3141+ option parsing (when typically an error code is returned instead). The
3142+ difference between this function and argp_error is that the latter is for
3143+ *parsing errors*, and the former is for other problems that occur during
3144+ parsing but don't reflect a (syntactic) problem with the input. */
3145+void
3146+argp_failure (const struct argp_state *state, int status, int errnum,
3147+ const char *fmt, ...)
3148+{
3149+ if (!state || !(state->flags & ARGP_NO_ERRS))
3150+ {
3151+ FILE *stream = state ? state->err_stream : stderr;
3152+
3153+ if (stream)
3154+ {
3155+#if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
3156+ flockfile (stream);
3157+#endif
3158+
3159+#if defined _LIBC && defined USE_IN_LIBIO
3160+ __fxprintf (stream, "%s",
3161+ state ? state->name : __argp_short_program_name ());
3162+#else
3163+ fputs_unlocked (state ? state->name : __argp_short_program_name (),
3164+ stream);
3165+#endif
3166+
3167+ if (fmt)
3168+ {
3169+ va_list ap;
3170+
3171+ va_start (ap, fmt);
3172+#if defined _LIBC && defined USE_IN_LIBIO
3173+ char *buf;
3174+
3175+ if (_IO_vasprintf (&buf, fmt, ap) < 0)
3176+ buf = NULL;
3177+
3178+ __fxprintf (stream, ": %s", buf);
3179+
3180+ free (buf);
3181+#else
3182+ putc_unlocked (':', stream);
3183+ putc_unlocked (' ', stream);
3184+
3185+ vfprintf (stream, fmt, ap);
3186+#endif
3187+
3188+ va_end (ap);
3189+ }
3190+
3191+ if (errnum)
3192+ {
3193+#if (defined _LIBC && defined USE_IN_LIBIO) || defined HAVE_STRERROR_R
3194+ char buf[200];
3195+#endif
3196+#if defined _LIBC && defined USE_IN_LIBIO
3197+ __fxprintf (stream, ": %s",
3198+ strerror_r (errnum, buf, sizeof (buf)));
3199+#else
3200+ putc_unlocked (':', stream);
3201+ putc_unlocked (' ', stream);
3202+# ifdef HAVE_STRERROR_R
3203+ fputs (strerror_r (errnum, buf, sizeof (buf)), stream);
3204+# else
3205+ fputs (strerror (errnum), stream);
3206+# endif
3207+#endif
3208+ }
3209+
3210+#ifdef USE_IN_LIBIO
3211+ if (_IO_fwide (stream, 0) > 0)
3212+ putwc_unlocked (L'\n', stream);
3213+ else
3214+#endif
3215+ putc_unlocked ('\n', stream);
3216+
3217+#if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
3218+ funlockfile (stream);
3219+#endif
3220+
3221+ if (status && (!state || !(state->flags & ARGP_NO_EXIT)))
3222+ exit (status);
3223+ }
3224+ }
3225+}
3226Index: git/libuargp/argp-parse.c
3227===================================================================
3228--- /dev/null
3229+++ git/libuargp/argp-parse.c
3230@@ -0,0 +1,949 @@
3231+/* Hierarchial argument parsing, layered over getopt
3232+ Copyright (C) 1995-2000, 2002, 2003, 2004 Free Software Foundation, Inc.
3233+ This file is part of the GNU C Library.
3234+ Written by Miles Bader <miles at gnu.ai.mit.edu>.
3235+
3236+ The GNU C Library is free software; you can redistribute it and/or
3237+ modify it under the terms of the GNU Lesser General Public
3238+ License as published by the Free Software Foundation; either
3239+ version 2.1 of the License, or (at your option) any later version.
3240+
3241+ The GNU C Library is distributed in the hope that it will be useful,
3242+ but WITHOUT ANY WARRANTY; without even the implied warranty of
3243+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3244+ Lesser General Public License for more details.
3245+
3246+ You should have received a copy of the GNU Lesser General Public
3247+ License along with the GNU C Library; if not, write to the Free
3248+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
3249+ 02111-1307 USA.
3250+
3251+ Modified for uClibc by: Salvatore Cro <salvatore.cro at st.com>
3252+*/
3253+
3254+#ifdef HAVE_CONFIG_H
3255+#include <config.h>
3256+#endif
3257+
3258+/* AIX requires this to be the first thing in the file. */
3259+#ifndef __GNUC__
3260+# if HAVE_ALLOCA_H || defined _LIBC
3261+# include <alloca.h>
3262+# else
3263+# ifdef _AIX
3264+#pragma alloca
3265+# else
3266+# ifndef alloca /* predefined by HP cc +Olibcalls */
3267+char *alloca ();
3268+# endif
3269+# endif
3270+# endif
3271+#endif
3272+
3273+#include <stdlib.h>
3274+#include <string.h>
3275+#include <unistd.h>
3276+#include <limits.h>
3277+#include <getopt.h>
3278+#include <bits/getopt_int.h>
3279+
3280+#include <features.h>
3281+#ifndef _
3282+/* This is for other GNU distributions with internationalized messages.
3283+ When compiling libc, the _ macro is predefined. */
3284+# if (defined HAVE_LIBINTL_H || defined _LIBC) && defined __UCLIBC_HAS_GETTEXT_AWARENESS__
3285+# include <libintl.h>
3286+# ifdef _LIBC
3287+# undef dgettext
3288+# define dgettext(domain, msgid) \
3289+ INTUSE(__dcgettext) (domain, msgid, LC_MESSAGES)
3290+# endif
3291+# else
3292+# define dgettext(domain, msgid) (msgid)
3293+# define gettext(msgid) (msgid)
3294+# endif
3295+#endif
3296+#ifndef N_
3297+# define N_(msgid) (msgid)
3298+#endif
3299+
3300+#include <argp.h>
3301+
3302+/* Getopt return values. */
3303+#define KEY_END (-1) /* The end of the options. */
3304+#define KEY_ARG 1 /* A non-option argument. */
3305+#define KEY_ERR '?' /* An error parsing the options. */
3306+
3307+/* The meta-argument used to prevent any further arguments being interpreted
3308+ as options. */
3309+#define QUOTE "--"
3310+
3311+/* The number of bits we steal in a long-option value for our own use. */
3312+#define GROUP_BITS CHAR_BIT
3313+
3314+/* The number of bits available for the user value. */
3315+#define USER_BITS ((sizeof ((struct option *)0)->val * CHAR_BIT) - GROUP_BITS)
3316+#define USER_MASK ((1 << USER_BITS) - 1)
3317+
3318+/* EZ alias for ARGP_ERR_UNKNOWN. */
3319+#define EBADKEY ARGP_ERR_UNKNOWN
3320+
3321+/* Default options. */
3322+
3323+/* When argp is given the --HANG switch, _ARGP_HANG is set and argp will sleep
3324+ for one second intervals, decrementing _ARGP_HANG until it's zero. Thus
3325+ you can force the program to continue by attaching a debugger and setting
3326+ it to 0 yourself. */
3327+static volatile int _argp_hang;
3328+
3329+#define OPT_PROGNAME -2
3330+#define OPT_USAGE -3
3331+#define OPT_HANG -4
3332+
3333+static const struct argp_option argp_default_options[] =
3334+{
3335+ {"help", '?', 0, 0, N_("Give this help list"), -1},
3336+ {"usage", OPT_USAGE, 0, 0, N_("Give a short usage message")},
3337+ {"program-name",OPT_PROGNAME,"NAME", OPTION_HIDDEN, N_("Set the program name")},
3338+ {"HANG", OPT_HANG, "SECS", OPTION_ARG_OPTIONAL | OPTION_HIDDEN,
3339+ N_("Hang for SECS seconds (default 3600)")},
3340+ {0, 0}
3341+};
3342+
3343+static error_t
3344+argp_default_parser (int key, char *arg, struct argp_state *state)
3345+{
3346+ switch (key)
3347+ {
3348+ case '?':
3349+ argp_state_help (state, state->out_stream, ARGP_HELP_STD_HELP);
3350+ break;
3351+ case OPT_USAGE:
3352+ argp_state_help (state, state->out_stream,
3353+ ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK);
3354+ break;
3355+
3356+ case OPT_PROGNAME: /* Set the program name. */
3357+#if defined _LIBC || HAVE_DECL_PROGRAM_INVOCATION_NAME
3358+ program_invocation_name = arg;
3359+#endif
3360+ /* [Note that some systems only have PROGRAM_INVOCATION_SHORT_NAME (aka
3361+ __PROGNAME), in which case, PROGRAM_INVOCATION_NAME is just defined
3362+ to be that, so we have to be a bit careful here.] */
3363+
3364+ /* Update what we use for messages. */
3365+ state->name = strrchr (arg, '/');
3366+ if (state->name)
3367+ state->name++;
3368+ else
3369+ state->name = arg;
3370+
3371+#if defined _LIBC || HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME
3372+ program_invocation_short_name = state->name;
3373+#endif
3374+
3375+ if ((state->flags & (ARGP_PARSE_ARGV0 | ARGP_NO_ERRS))
3376+ == ARGP_PARSE_ARGV0)
3377+ /* Update what getopt uses too. */
3378+ state->argv[0] = arg;
3379+
3380+ break;
3381+
3382+ case OPT_HANG:
3383+ _argp_hang = atoi (arg ? arg : "3600");
3384+ while (_argp_hang-- > 0)
3385+ sleep (1);
3386+ break;
3387+
3388+ default:
3389+ return EBADKEY;
3390+ }
3391+ return 0;
3392+}
3393+
3394+static const struct argp argp_default_argp =
3395+ {argp_default_options, &argp_default_parser, NULL, NULL, NULL, NULL, "libc"};
3396+
3397+
3398+static const struct argp_option argp_version_options[] =
3399+{
3400+ {"version", 'V', 0, 0, N_("Print program version"), -1},
3401+ {0, 0}
3402+};
3403+
3404+static error_t
3405+argp_version_parser (int key, char *arg, struct argp_state *state)
3406+{
3407+ switch (key)
3408+ {
3409+ case 'V':
3410+ if (argp_program_version_hook)
3411+ (*argp_program_version_hook) (state->out_stream, state);
3412+ else if (argp_program_version)
3413+ fprintf (state->out_stream, "%s\n", argp_program_version);
3414+ else
3415+ argp_error (state, dgettext (state->root_argp->argp_domain,
3416+ "(PROGRAM ERROR) No version known!?"));
3417+ if (! (state->flags & ARGP_NO_EXIT))
3418+ exit (0);
3419+ break;
3420+ default:
3421+ return EBADKEY;
3422+ }
3423+ return 0;
3424+}
3425+
3426+static const struct argp argp_version_argp =
3427+ {argp_version_options, &argp_version_parser, NULL, NULL, NULL, NULL, "libc"};
3428+
3429+/* Returns the offset into the getopt long options array LONG_OPTIONS of a
3430+ long option with called NAME, or -1 if none is found. Passing NULL as
3431+ NAME will return the number of options. */
3432+static int
3433+find_long_option (struct option *long_options, const char *name)
3434+{
3435+ struct option *l = long_options;
3436+ while (l->name != NULL)
3437+ if (name != NULL && strcmp (l->name, name) == 0)
3438+ return l - long_options;
3439+ else
3440+ l++;
3441+ if (name == NULL)
3442+ return l - long_options;
3443+ else
3444+ return -1;
3445+}
3446+
3447+
3448+/* The state of a `group' during parsing. Each group corresponds to a
3449+ particular argp structure from the tree of such descending from the top
3450+ level argp passed to argp_parse. */
3451+struct group
3452+{
3453+ /* This group's parsing function. */
3454+ argp_parser_t parser;
3455+
3456+ /* Which argp this group is from. */
3457+ const struct argp *argp;
3458+
3459+ /* Points to the point in SHORT_OPTS corresponding to the end of the short
3460+ options for this group. We use it to determine from which group a
3461+ particular short options is from. */
3462+ char *short_end;
3463+
3464+ /* The number of non-option args sucessfully handled by this parser. */
3465+ unsigned args_processed;
3466+
3467+ /* This group's parser's parent's group. */
3468+ struct group *parent;
3469+ unsigned parent_index; /* And the our position in the parent. */
3470+
3471+ /* These fields are swapped into and out of the state structure when
3472+ calling this group's parser. */
3473+ void *input, **child_inputs;
3474+ void *hook;
3475+};
3476+
3477+/* Call GROUP's parser with KEY and ARG, swapping any group-specific info
3478+ from STATE before calling, and back into state afterwards. If GROUP has
3479+ no parser, EBADKEY is returned. */
3480+static error_t
3481+group_parse (struct group *group, struct argp_state *state, int key, char *arg)
3482+{
3483+ if (group->parser)
3484+ {
3485+ error_t err;
3486+ state->hook = group->hook;
3487+ state->input = group->input;
3488+ state->child_inputs = group->child_inputs;
3489+ state->arg_num = group->args_processed;
3490+ err = (*group->parser)(key, arg, state);
3491+ group->hook = state->hook;
3492+ return err;
3493+ }
3494+ else
3495+ return EBADKEY;
3496+}
3497+
3498+struct parser
3499+{
3500+ const struct argp *argp;
3501+
3502+ /* SHORT_OPTS is the getopt short options string for the union of all the
3503+ groups of options. */
3504+ char *short_opts;
3505+ /* LONG_OPTS is the array of getop long option structures for the union of
3506+ all the groups of options. */
3507+ struct option *long_opts;
3508+ /* OPT_DATA is the getopt data used for the re-entrant getopt. */
3509+ struct _getopt_data opt_data;
3510+
3511+ /* States of the various parsing groups. */
3512+ struct group *groups;
3513+ /* The end of the GROUPS array. */
3514+ struct group *egroup;
3515+ /* An vector containing storage for the CHILD_INPUTS field in all groups. */
3516+ void **child_inputs;
3517+
3518+ /* True if we think using getopt is still useful; if false, then
3519+ remaining arguments are just passed verbatim with ARGP_KEY_ARG. This is
3520+ cleared whenever getopt returns KEY_END, but may be set again if the user
3521+ moves the next argument pointer backwards. */
3522+ int try_getopt;
3523+
3524+ /* State block supplied to parsing routines. */
3525+ struct argp_state state;
3526+
3527+ /* Memory used by this parser. */
3528+ void *storage;
3529+};
3530+
3531+/* The next usable entries in the various parser tables being filled in by
3532+ convert_options. */
3533+struct parser_convert_state
3534+{
3535+ struct parser *parser;
3536+ char *short_end;
3537+ struct option *long_end;
3538+ void **child_inputs_end;
3539+};
3540+
3541+/* Converts all options in ARGP (which is put in GROUP) and ancestors
3542+ into getopt options stored in SHORT_OPTS and LONG_OPTS; SHORT_END and
3543+ CVT->LONG_END are the points at which new options are added. Returns the
3544+ next unused group entry. CVT holds state used during the conversion. */
3545+static struct group *
3546+convert_options (const struct argp *argp,
3547+ struct group *parent, unsigned parent_index,
3548+ struct group *group, struct parser_convert_state *cvt)
3549+{
3550+ /* REAL is the most recent non-alias value of OPT. */
3551+ const struct argp_option *real = argp->options;
3552+ const struct argp_child *children = argp->children;
3553+
3554+ if (real || argp->parser)
3555+ {
3556+ const struct argp_option *opt;
3557+
3558+ if (real)
3559+ for (opt = real; !__option_is_end (opt); opt++)
3560+ {
3561+ if (! (opt->flags & OPTION_ALIAS))
3562+ /* OPT isn't an alias, so we can use values from it. */
3563+ real = opt;
3564+
3565+ if (! (real->flags & OPTION_DOC))
3566+ /* A real option (not just documentation). */
3567+ {
3568+ if (__option_is_short (opt))
3569+ /* OPT can be used as a short option. */
3570+ {
3571+ *cvt->short_end++ = opt->key;
3572+ if (real->arg)
3573+ {
3574+ *cvt->short_end++ = ':';
3575+ if (real->flags & OPTION_ARG_OPTIONAL)
3576+ *cvt->short_end++ = ':';
3577+ }
3578+ *cvt->short_end = '\0'; /* keep 0 terminated */
3579+ }
3580+
3581+ if (opt->name
3582+ && find_long_option (cvt->parser->long_opts, opt->name) < 0)
3583+ /* OPT can be used as a long option. */
3584+ {
3585+ cvt->long_end->name = opt->name;
3586+ cvt->long_end->has_arg =
3587+ (real->arg
3588+ ? (real->flags & OPTION_ARG_OPTIONAL
3589+ ? optional_argument
3590+ : required_argument)
3591+ : no_argument);
3592+ cvt->long_end->flag = 0;
3593+ /* we add a disambiguating code to all the user's
3594+ values (which is removed before we actually call
3595+ the function to parse the value); this means that
3596+ the user loses use of the high 8 bits in all his
3597+ values (the sign of the lower bits is preserved
3598+ however)... */
3599+ cvt->long_end->val =
3600+ ((opt->key | real->key) & USER_MASK)
3601+ + (((group - cvt->parser->groups) + 1) << USER_BITS);
3602+
3603+ /* Keep the LONG_OPTS list terminated. */
3604+ (++cvt->long_end)->name = NULL;
3605+ }
3606+ }
3607+ }
3608+
3609+ group->parser = argp->parser;
3610+ group->argp = argp;
3611+ group->short_end = cvt->short_end;
3612+ group->args_processed = 0;
3613+ group->parent = parent;
3614+ group->parent_index = parent_index;
3615+ group->input = 0;
3616+ group->hook = 0;
3617+ group->child_inputs = 0;
3618+
3619+ if (children)
3620+ /* Assign GROUP's CHILD_INPUTS field some space from
3621+ CVT->child_inputs_end.*/
3622+ {
3623+ unsigned num_children = 0;
3624+ while (children[num_children].argp)
3625+ num_children++;
3626+ group->child_inputs = cvt->child_inputs_end;
3627+ cvt->child_inputs_end += num_children;
3628+ }
3629+
3630+ parent = group++;
3631+ }
3632+ else
3633+ parent = 0;
3634+
3635+ if (children)
3636+ {
3637+ unsigned index = 0;
3638+ while (children->argp)
3639+ group =
3640+ convert_options (children++->argp, parent, index++, group, cvt);
3641+ }
3642+
3643+ return group;
3644+}
3645+
3646+/* Find the merged set of getopt options, with keys appropiately prefixed. */
3647+static void
3648+parser_convert (struct parser *parser, const struct argp *argp, int flags)
3649+{
3650+ struct parser_convert_state cvt;
3651+
3652+ cvt.parser = parser;
3653+ cvt.short_end = parser->short_opts;
3654+ cvt.long_end = parser->long_opts;
3655+ cvt.child_inputs_end = parser->child_inputs;
3656+
3657+ if (flags & ARGP_IN_ORDER)
3658+ *cvt.short_end++ = '-';
3659+ else if (flags & ARGP_NO_ARGS)
3660+ *cvt.short_end++ = '+';
3661+ *cvt.short_end = '\0';
3662+
3663+ cvt.long_end->name = NULL;
3664+
3665+ parser->argp = argp;
3666+
3667+ if (argp)
3668+ parser->egroup = convert_options (argp, 0, 0, parser->groups, &cvt);
3669+ else
3670+ parser->egroup = parser->groups; /* No parsers at all! */
3671+}
3672+
3673+/* Lengths of various parser fields which we will allocated. */
3674+struct parser_sizes
3675+{
3676+ size_t short_len; /* Getopt short options string. */
3677+ size_t long_len; /* Getopt long options vector. */
3678+ size_t num_groups; /* Group structures we allocate. */
3679+ size_t num_child_inputs; /* Child input slots. */
3680+};
3681+
3682+/* For ARGP, increments the NUM_GROUPS field in SZS by the total number of
3683+ argp structures descended from it, and the SHORT_LEN & LONG_LEN fields by
3684+ the maximum lengths of the resulting merged getopt short options string and
3685+ long-options array, respectively. */
3686+static void
3687+calc_sizes (const struct argp *argp, struct parser_sizes *szs)
3688+{
3689+ const struct argp_child *child = argp->children;
3690+ const struct argp_option *opt = argp->options;
3691+
3692+ if (opt || argp->parser)
3693+ {
3694+ szs->num_groups++;
3695+ if (opt)
3696+ {
3697+ int num_opts = 0;
3698+ while (!__option_is_end (opt++))
3699+ num_opts++;
3700+ szs->short_len += num_opts * 3; /* opt + up to 2 `:'s */
3701+ szs->long_len += num_opts;
3702+ }
3703+ }
3704+
3705+ if (child)
3706+ while (child->argp)
3707+ {
3708+ calc_sizes ((child++)->argp, szs);
3709+ szs->num_child_inputs++;
3710+ }
3711+}
3712+
3713+
3714+extern char * __argp_short_program_name (void);
3715+/* Initializes PARSER to parse ARGP in a manner described by FLAGS. */
3716+static error_t
3717+parser_init (struct parser *parser, const struct argp *argp,
3718+ int argc, char **argv, int flags, void *input)
3719+{
3720+ error_t err = 0;
3721+ struct group *group;
3722+ struct parser_sizes szs;
3723+ struct _getopt_data opt_data = _GETOPT_DATA_INITIALIZER;
3724+
3725+ szs.short_len = (flags & ARGP_NO_ARGS) ? 0 : 1;
3726+ szs.long_len = 0;
3727+ szs.num_groups = 0;
3728+ szs.num_child_inputs = 0;
3729+
3730+ if (argp)
3731+ calc_sizes (argp, &szs);
3732+
3733+ /* Lengths of the various bits of storage used by PARSER. */
3734+#define GLEN (szs.num_groups + 1) * sizeof (struct group)
3735+#define CLEN (szs.num_child_inputs * sizeof (void *))
3736+#define LLEN ((szs.long_len + 1) * sizeof (struct option))
3737+#define SLEN (szs.short_len + 1)
3738+
3739+ parser->storage = malloc (GLEN + CLEN + LLEN + SLEN);
3740+ if (! parser->storage)
3741+ return ENOMEM;
3742+
3743+ parser->groups = parser->storage;
3744+ parser->child_inputs = parser->storage + GLEN;
3745+ parser->long_opts = parser->storage + GLEN + CLEN;
3746+ parser->short_opts = parser->storage + GLEN + CLEN + LLEN;
3747+ parser->opt_data = opt_data;
3748+
3749+ memset (parser->child_inputs, 0, szs.num_child_inputs * sizeof (void *));
3750+ parser_convert (parser, argp, flags);
3751+
3752+ memset (&parser->state, 0, sizeof (struct argp_state));
3753+ parser->state.root_argp = parser->argp;
3754+ parser->state.argc = argc;
3755+ parser->state.argv = argv;
3756+ parser->state.flags = flags;
3757+ parser->state.err_stream = stderr;
3758+ parser->state.out_stream = stdout;
3759+ parser->state.next = 0; /* Tell getopt to initialize. */
3760+ parser->state.pstate = parser;
3761+
3762+ parser->try_getopt = 1;
3763+
3764+ /* Call each parser for the first time, giving it a chance to propagate
3765+ values to child parsers. */
3766+ if (parser->groups < parser->egroup)
3767+ parser->groups->input = input;
3768+ for (group = parser->groups;
3769+ group < parser->egroup && (!err || err == EBADKEY);
3770+ group++)
3771+ {
3772+ if (group->parent)
3773+ /* If a child parser, get the initial input value from the parent. */
3774+ group->input = group->parent->child_inputs[group->parent_index];
3775+
3776+ if (!group->parser
3777+ && group->argp->children && group->argp->children->argp)
3778+ /* For the special case where no parsing function is supplied for an
3779+ argp, propagate its input to its first child, if any (this just
3780+ makes very simple wrapper argps more convenient). */
3781+ group->child_inputs[0] = group->input;
3782+
3783+ err = group_parse (group, &parser->state, ARGP_KEY_INIT, 0);
3784+ }
3785+ if (err == EBADKEY)
3786+ err = 0; /* Some parser didn't understand. */
3787+
3788+ if (err)
3789+ return err;
3790+
3791+ if (parser->state.flags & ARGP_NO_ERRS)
3792+ {
3793+ parser->opt_data.opterr = 0;
3794+ if (parser->state.flags & ARGP_PARSE_ARGV0)
3795+ /* getopt always skips ARGV[0], so we have to fake it out. As long
3796+ as OPTERR is 0, then it shouldn't actually try to access it. */
3797+ parser->state.argv--, parser->state.argc++;
3798+ }
3799+ else
3800+ parser->opt_data.opterr = 1; /* Print error messages. */
3801+
3802+ if (parser->state.argv == argv && argv[0])
3803+ /* There's an argv[0]; use it for messages. */
3804+ {
3805+ char *short_name = strrchr (argv[0], '/');
3806+ parser->state.name = short_name ? short_name + 1 : argv[0];
3807+ }
3808+ else
3809+ parser->state.name = __argp_short_program_name ();
3810+
3811+ return 0;
3812+}
3813+
3814+/* Free any storage consumed by PARSER (but not PARSER itself). */
3815+static error_t
3816+parser_finalize (struct parser *parser,
3817+ error_t err, int arg_ebadkey, int *end_index)
3818+{
3819+ struct group *group;
3820+
3821+ if (err == EBADKEY && arg_ebadkey)
3822+ /* Suppress errors generated by unparsed arguments. */
3823+ err = 0;
3824+
3825+ if (! err)
3826+ {
3827+ if (parser->state.next == parser->state.argc)
3828+ /* We successfully parsed all arguments! Call all the parsers again,
3829+ just a few more times... */
3830+ {
3831+ for (group = parser->groups;
3832+ group < parser->egroup && (!err || err==EBADKEY);
3833+ group++)
3834+ if (group->args_processed == 0)
3835+ err = group_parse (group, &parser->state, ARGP_KEY_NO_ARGS, 0);
3836+ for (group = parser->egroup - 1;
3837+ group >= parser->groups && (!err || err==EBADKEY);
3838+ group--)
3839+ err = group_parse (group, &parser->state, ARGP_KEY_END, 0);
3840+
3841+ if (err == EBADKEY)
3842+ err = 0; /* Some parser didn't understand. */
3843+
3844+ /* Tell the user that all arguments are parsed. */
3845+ if (end_index)
3846+ *end_index = parser->state.next;
3847+ }
3848+ else if (end_index)
3849+ /* Return any remaining arguments to the user. */
3850+ *end_index = parser->state.next;
3851+ else
3852+ /* No way to return the remaining arguments, they must be bogus. */
3853+ {
3854+ if (!(parser->state.flags & ARGP_NO_ERRS)
3855+ && parser->state.err_stream)
3856+ fprintf (parser->state.err_stream,
3857+ dgettext (parser->argp->argp_domain,
3858+ "%s: Too many arguments\n"),
3859+ parser->state.name);
3860+ err = EBADKEY;
3861+ }
3862+ }
3863+
3864+ /* Okay, we're all done, with either an error or success; call the parsers
3865+ to indicate which one. */
3866+
3867+ if (err)
3868+ {
3869+ /* Maybe print an error message. */
3870+ if (err == EBADKEY)
3871+ /* An appropriate message describing what the error was should have
3872+ been printed earlier. */
3873+ argp_state_help (&parser->state, parser->state.err_stream,
3874+ ARGP_HELP_STD_ERR);
3875+
3876+ /* Since we didn't exit, give each parser an error indication. */
3877+ for (group = parser->groups; group < parser->egroup; group++)
3878+ group_parse (group, &parser->state, ARGP_KEY_ERROR, 0);
3879+ }
3880+ else
3881+ /* Notify parsers of success, and propagate back values from parsers. */
3882+ {
3883+ /* We pass over the groups in reverse order so that child groups are
3884+ given a chance to do there processing before passing back a value to
3885+ the parent. */
3886+ for (group = parser->egroup - 1
3887+ ; group >= parser->groups && (!err || err == EBADKEY)
3888+ ; group--)
3889+ err = group_parse (group, &parser->state, ARGP_KEY_SUCCESS, 0);
3890+ if (err == EBADKEY)
3891+ err = 0; /* Some parser didn't understand. */
3892+ }
3893+
3894+ /* Call parsers once more, to do any final cleanup. Errors are ignored. */
3895+ for (group = parser->egroup - 1; group >= parser->groups; group--)
3896+ group_parse (group, &parser->state, ARGP_KEY_FINI, 0);
3897+
3898+ if (err == EBADKEY)
3899+ err = EINVAL;
3900+
3901+ free (parser->storage);
3902+
3903+ return err;
3904+}
3905+
3906+/* Call the user parsers to parse the non-option argument VAL, at the current
3907+ position, returning any error. The state NEXT pointer is assumed to have
3908+ been adjusted (by getopt) to point after this argument; this function will
3909+ adjust it correctly to reflect however many args actually end up being
3910+ consumed. */
3911+static error_t
3912+parser_parse_arg (struct parser *parser, char *val)
3913+{
3914+ /* Save the starting value of NEXT, first adjusting it so that the arg
3915+ we're parsing is again the front of the arg vector. */
3916+ int index = --parser->state.next;
3917+ error_t err = EBADKEY;
3918+ struct group *group;
3919+ int key = 0; /* Which of ARGP_KEY_ARG[S] we used. */
3920+
3921+ /* Try to parse the argument in each parser. */
3922+ for (group = parser->groups
3923+ ; group < parser->egroup && err == EBADKEY
3924+ ; group++)
3925+ {
3926+ parser->state.next++; /* For ARGP_KEY_ARG, consume the arg. */
3927+ key = ARGP_KEY_ARG;
3928+ err = group_parse (group, &parser->state, key, val);
3929+
3930+ if (err == EBADKEY)
3931+ /* This parser doesn't like ARGP_KEY_ARG; try ARGP_KEY_ARGS instead. */
3932+ {
3933+ parser->state.next--; /* For ARGP_KEY_ARGS, put back the arg. */
3934+ key = ARGP_KEY_ARGS;
3935+ err = group_parse (group, &parser->state, key, 0);
3936+ }
3937+ }
3938+
3939+ if (! err)
3940+ {
3941+ if (key == ARGP_KEY_ARGS)
3942+ /* The default for ARGP_KEY_ARGS is to assume that if NEXT isn't
3943+ changed by the user, *all* arguments should be considered
3944+ consumed. */
3945+ parser->state.next = parser->state.argc;
3946+
3947+ if (parser->state.next > index)
3948+ /* Remember that we successfully processed a non-option
3949+ argument -- but only if the user hasn't gotten tricky and set
3950+ the clock back. */
3951+ (--group)->args_processed += (parser->state.next - index);
3952+ else
3953+ /* The user wants to reparse some args, give getopt another try. */
3954+ parser->try_getopt = 1;
3955+ }
3956+
3957+ return err;
3958+}
3959+
3960+/* Call the user parsers to parse the option OPT, with argument VAL, at the
3961+ current position, returning any error. */
3962+static error_t
3963+parser_parse_opt (struct parser *parser, int opt, char *val)
3964+{
3965+ /* The group key encoded in the high bits; 0 for short opts or
3966+ group_number + 1 for long opts. */
3967+ int group_key = opt >> USER_BITS;
3968+ error_t err = EBADKEY;
3969+
3970+ if (group_key == 0)
3971+ /* A short option. By comparing OPT's position in SHORT_OPTS to the
3972+ various starting positions in each group's SHORT_END field, we can
3973+ determine which group OPT came from. */
3974+ {
3975+ struct group *group;
3976+ char *short_index = strchr (parser->short_opts, opt);
3977+
3978+ if (short_index)
3979+ for (group = parser->groups; group < parser->egroup; group++)
3980+ if (group->short_end > short_index)
3981+ {
3982+ err = group_parse (group, &parser->state, opt,
3983+ parser->opt_data.optarg);
3984+ break;
3985+ }
3986+ }
3987+ else
3988+ /* A long option. We use shifts instead of masking for extracting
3989+ the user value in order to preserve the sign. */
3990+ err =
3991+ group_parse (&parser->groups[group_key - 1], &parser->state,
3992+ (opt << GROUP_BITS) >> GROUP_BITS,
3993+ parser->opt_data.optarg);
3994+
3995+ if (err == EBADKEY)
3996+ /* At least currently, an option not recognized is an error in the
3997+ parser, because we pre-compute which parser is supposed to deal
3998+ with each option. */
3999+ {
4000+ static const char bad_key_err[] =
4001+ N_("(PROGRAM ERROR) Option should have been recognized!?");
4002+ if (group_key == 0)
4003+ argp_error (&parser->state, "-%c: %s", opt,
4004+ dgettext (parser->argp->argp_domain, bad_key_err));
4005+ else
4006+ {
4007+ struct option *long_opt = parser->long_opts;
4008+ while (long_opt->val != opt && long_opt->name)
4009+ long_opt++;
4010+ argp_error (&parser->state, "--%s: %s",
4011+ long_opt->name ? long_opt->name : "???",
4012+ dgettext (parser->argp->argp_domain, bad_key_err));
4013+ }
4014+ }
4015+
4016+ return err;
4017+}
4018+
4019+/* Parse the next argument in PARSER (as indicated by PARSER->state.next).
4020+ Any error from the parsers is returned, and *ARGP_EBADKEY indicates
4021+ whether a value of EBADKEY is due to an unrecognized argument (which is
4022+ generally not fatal). */
4023+static error_t
4024+parser_parse_next (struct parser *parser, int *arg_ebadkey)
4025+{
4026+ int opt;
4027+ error_t err = 0;
4028+
4029+ if (parser->state.quoted && parser->state.next < parser->state.quoted)
4030+ /* The next argument pointer has been moved to before the quoted
4031+ region, so pretend we never saw the quoting `--', and give getopt
4032+ another chance. If the user hasn't removed it, getopt will just
4033+ process it again. */
4034+ parser->state.quoted = 0;
4035+
4036+ if (parser->try_getopt && !parser->state.quoted)
4037+ /* Give getopt a chance to parse this. */
4038+ {
4039+ /* Put it back in OPTIND for getopt. */
4040+ parser->opt_data.optind = parser->state.next;
4041+ /* Distinguish KEY_ERR from a real option. */
4042+ parser->opt_data.optopt = KEY_END;
4043+ if (parser->state.flags & ARGP_LONG_ONLY)
4044+ opt = _getopt_long_only_r (parser->state.argc, parser->state.argv,
4045+ parser->short_opts, parser->long_opts, 0,
4046+ &parser->opt_data);
4047+ else
4048+ opt = _getopt_long_r (parser->state.argc, parser->state.argv,
4049+ parser->short_opts, parser->long_opts, 0,
4050+ &parser->opt_data);
4051+ /* And see what getopt did. */
4052+ parser->state.next = parser->opt_data.optind;
4053+
4054+ if (opt == KEY_END)
4055+ /* Getopt says there are no more options, so stop using
4056+ getopt; we'll continue if necessary on our own. */
4057+ {
4058+ parser->try_getopt = 0;
4059+ if (parser->state.next > 1
4060+ && strcmp (parser->state.argv[parser->state.next - 1], QUOTE)
4061+ == 0)
4062+ /* Not only is this the end of the options, but it's a
4063+ `quoted' region, which may have args that *look* like
4064+ options, so we definitely shouldn't try to use getopt past
4065+ here, whatever happens. */
4066+ parser->state.quoted = parser->state.next;
4067+ }
4068+ else if (opt == KEY_ERR && parser->opt_data.optopt != KEY_END)
4069+ /* KEY_ERR can have the same value as a valid user short
4070+ option, but in the case of a real error, getopt sets OPTOPT
4071+ to the offending character, which can never be KEY_END. */
4072+ {
4073+ *arg_ebadkey = 0;
4074+ return EBADKEY;
4075+ }
4076+ }
4077+ else
4078+ opt = KEY_END;
4079+
4080+ if (opt == KEY_END)
4081+ {
4082+ /* We're past what getopt considers the options. */
4083+ if (parser->state.next >= parser->state.argc
4084+ || (parser->state.flags & ARGP_NO_ARGS))
4085+ /* Indicate that we're done. */
4086+ {
4087+ *arg_ebadkey = 1;
4088+ return EBADKEY;
4089+ }
4090+ else
4091+ /* A non-option arg; simulate what getopt might have done. */
4092+ {
4093+ opt = KEY_ARG;
4094+ parser->opt_data.optarg = parser->state.argv[parser->state.next++];
4095+ }
4096+ }
4097+
4098+ if (opt == KEY_ARG)
4099+ /* A non-option argument; try each parser in turn. */
4100+ err = parser_parse_arg (parser, parser->opt_data.optarg);
4101+ else
4102+ err = parser_parse_opt (parser, opt, parser->opt_data.optarg);
4103+
4104+ if (err == EBADKEY)
4105+ *arg_ebadkey = (opt == KEY_END || opt == KEY_ARG);
4106+
4107+ return err;
4108+}
4109+
4110+/* Parse the options strings in ARGC & ARGV according to the argp in ARGP.
4111+ FLAGS is one of the ARGP_ flags above. If END_INDEX is non-NULL, the
4112+ index in ARGV of the first unparsed option is returned in it. If an
4113+ unknown option is present, EINVAL is returned; if some parser routine
4114+ returned a non-zero value, it is returned; otherwise 0 is returned. */
4115+error_t
4116+argp_parse (const struct argp *argp, int argc, char **argv, unsigned flags,
4117+ int *end_index, void *input)
4118+{
4119+ error_t err;
4120+ struct parser parser;
4121+
4122+ /* If true, then err == EBADKEY is a result of a non-option argument failing
4123+ to be parsed (which in some cases isn't actually an error). */
4124+ int arg_ebadkey = 0;
4125+
4126+ if (! (flags & ARGP_NO_HELP))
4127+ /* Add our own options. */
4128+ {
4129+ struct argp_child *child = alloca (4 * sizeof (struct argp_child));
4130+ struct argp *top_argp = alloca (sizeof (struct argp));
4131+
4132+ /* TOP_ARGP has no options, it just serves to group the user & default
4133+ argps. */
4134+ memset (top_argp, 0, sizeof (*top_argp));
4135+ top_argp->children = child;
4136+
4137+ memset (child, 0, 4 * sizeof (struct argp_child));
4138+
4139+ if (argp)
4140+ (child++)->argp = argp;
4141+ (child++)->argp = &argp_default_argp;
4142+ if (argp_program_version || argp_program_version_hook)
4143+ (child++)->argp = &argp_version_argp;
4144+ child->argp = 0;
4145+
4146+ argp = top_argp;
4147+ }
4148+
4149+ /* Construct a parser for these arguments. */
4150+ err = parser_init (&parser, argp, argc, argv, flags, input);
4151+
4152+ if (! err)
4153+ /* Parse! */
4154+ {
4155+ while (! err)
4156+ err = parser_parse_next (&parser, &arg_ebadkey);
4157+ err = parser_finalize (&parser, err, arg_ebadkey, end_index);
4158+ }
4159+
4160+ return err;
4161+}
4162+
4163+/* Return the input field for ARGP in the parser corresponding to STATE; used
4164+ by the help routines. */
4165+void *
4166+__argp_input (const struct argp *argp, const struct argp_state *state)
4167+{
4168+ if (state)
4169+ {
4170+ struct group *group;
4171+ struct parser *parser = state->pstate;
4172+
4173+ for (group = parser->groups; group < parser->egroup; group++)
4174+ if (group->argp == argp)
4175+ return group->input;
4176+ }
4177+
4178+ return 0;
4179+}
4180Index: git/libuargp/argp-pv.c
4181===================================================================
4182--- /dev/null
4183+++ git/libuargp/argp-pv.c
4184@@ -0,0 +1,25 @@
4185+/* Default definition for ARGP_PROGRAM_VERSION.
4186+ Copyright (C) 1996, 1997, 1999 Free Software Foundation, Inc.
4187+ This file is part of the GNU C Library.
4188+ Written by Miles Bader <miles at gnu.ai.mit.edu>.
4189+
4190+ The GNU C Library is free software; you can redistribute it and/or
4191+ modify it under the terms of the GNU Lesser General Public
4192+ License as published by the Free Software Foundation; either
4193+ version 2.1 of the License, or (at your option) any later version.
4194+
4195+ The GNU C Library is distributed in the hope that it will be useful,
4196+ but WITHOUT ANY WARRANTY; without even the implied warranty of
4197+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4198+ Lesser General Public License for more details.
4199+
4200+ You should have received a copy of the GNU Lesser General Public
4201+ License along with the GNU C Library; if not, write to the Free
4202+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
4203+ 02111-1307 USA. */
4204+
4205+/* If set by the user program to a non-zero value, then a default option
4206+ --version is added (unless the ARGP_NO_HELP flag is used), which will
4207+ print this this string followed by a newline and exit (unless the
4208+ ARGP_NO_EXIT flag is used). Overridden by ARGP_PROGRAM_VERSION_HOOK. */
4209+const char *argp_program_version;
4210Index: git/libuargp/argp-pvh.c
4211===================================================================
4212--- /dev/null
4213+++ git/libuargp/argp-pvh.c
4214@@ -0,0 +1,32 @@
4215+/* Default definition for ARGP_PROGRAM_VERSION_HOOK.
4216+ Copyright (C) 1996, 1997, 1999 Free Software Foundation, Inc.
4217+ This file is part of the GNU C Library.
4218+ Written by Miles Bader <miles at gnu.ai.mit.edu>.
4219+
4220+ The GNU C Library is free software; you can redistribute it and/or
4221+ modify it under the terms of the GNU Lesser General Public
4222+ License as published by the Free Software Foundation; either
4223+ version 2.1 of the License, or (at your option) any later version.
4224+
4225+ The GNU C Library is distributed in the hope that it will be useful,
4226+ but WITHOUT ANY WARRANTY; without even the implied warranty of
4227+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4228+ Lesser General Public License for more details.
4229+
4230+ You should have received a copy of the GNU Lesser General Public
4231+ License along with the GNU C Library; if not, write to the Free
4232+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
4233+ 02111-1307 USA. */
4234+
4235+#ifdef HAVE_CONFIG_H
4236+#include <config.h>
4237+#endif
4238+
4239+#include <argp.h>
4240+
4241+/* If set by the user program to a non-zero value, then a default option
4242+ --version is added (unless the ARGP_NO_HELP flag is used), which calls
4243+ this function with a stream to print the version to and a pointer to the
4244+ current parsing state, and then exits (unless the ARGP_NO_EXIT flag is
4245+ used). This variable takes precedent over ARGP_PROGRAM_VERSION. */
4246+void (*argp_program_version_hook) (FILE *stream, struct argp_state *state);
4247Index: git/libuargp/argp-xinl.c
4248===================================================================
4249--- /dev/null
4250+++ git/libuargp/argp-xinl.c
4251@@ -0,0 +1,35 @@
4252+/* Real definitions for extern inline functions in argp.h
4253+ Copyright (C) 1997, 1998, 2004 Free Software Foundation, Inc.
4254+ This file is part of the GNU C Library.
4255+ Written by Miles Bader <miles at gnu.ai.mit.edu>.
4256+
4257+ The GNU C Library is free software; you can redistribute it and/or
4258+ modify it under the terms of the GNU Lesser General Public
4259+ License as published by the Free Software Foundation; either
4260+ version 2.1 of the License, or (at your option) any later version.
4261+
4262+ The GNU C Library is distributed in the hope that it will be useful,
4263+ but WITHOUT ANY WARRANTY; without even the implied warranty of
4264+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4265+ Lesser General Public License for more details.
4266+
4267+ You should have received a copy of the GNU Lesser General Public
4268+ License along with the GNU C Library; if not, write to the Free
4269+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
4270+ 02111-1307 USA. */
4271+
4272+#ifdef HAVE_CONFIG_H
4273+# include <config.h>
4274+#endif
4275+
4276+#if defined _LIBC || defined HAVE_FEATURES_H
4277+# include <features.h>
4278+#endif
4279+
4280+#ifndef __USE_EXTERN_INLINES
4281+# define __USE_EXTERN_INLINES 1
4282+#endif
4283+#define ARGP_EI
4284+#undef __OPTIMIZE__
4285+#define __OPTIMIZE__ 1
4286+#include <argp.h>
4287Index: git/test/argp/Makefile
4288===================================================================
4289--- /dev/null
4290+++ git/test/argp/Makefile
4291@@ -0,0 +1,7 @@
4292+# uClibc argp tests
4293+# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
4294+
4295+top_builddir=../../
4296+include ../Rules.mak
4297+-include Makefile.in
4298+include ../Test.mak
4299Index: git/test/argp/Makefile.in
4300===================================================================
4301--- /dev/null
4302+++ git/test/argp/Makefile.in
4303@@ -0,0 +1,12 @@
4304+# uClibc argp tests
4305+# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
4306+
4307+TESTS := $(addprefix argp-, ex1 ex2 ex3 ex4 test) \
4308+ bug-argp1 tst-argp1 tst-argp2
4309+
4310+EXTRA_LDFLAGS = -luargp
4311+
4312+OPTS_argp-ex3 = ARG1 ARG2
4313+OPTS_argp-ex4 = ARG1 string1 string2 string3
4314+OPTS_bug-argp1 = -- --help
4315+
4316Index: git/test/argp/argp-ex1.c
4317===================================================================
4318--- /dev/null
4319+++ git/test/argp/argp-ex1.c
4320@@ -0,0 +1,15 @@
4321+/* Argp example #1 -- a minimal program using argp */
4322+
4323+/* This is (probably) the smallest possible program that
4324+ uses argp. It won't do much except give an error
4325+ messages and exit when there are any arguments, and print
4326+ a (rather pointless) messages for --help. */
4327+
4328+#include <stdlib.h>
4329+#include <argp.h>
4330+
4331+int main (int argc, char **argv)
4332+{
4333+ argp_parse (0, argc, argv, 0, 0, 0);
4334+ exit (0);
4335+}
4336Index: git/test/argp/argp-ex2.c
4337===================================================================
4338--- /dev/null
4339+++ git/test/argp/argp-ex2.c
4340@@ -0,0 +1,45 @@
4341+/* Argp example #2 -- a pretty minimal program using argp */
4342+
4343+/* This program doesn't use any options or arguments, but uses
4344+ argp to be compliant with the GNU standard command line
4345+ format.
4346+
4347+ In addition to making sure no arguments are given, and
4348+ implementing a --help option, this example will have a
4349+ --version option, and will put the given documentation string
4350+ and bug address in the --help output, as per GNU standards.
4351+
4352+ The variable ARGP contains the argument parser specification;
4353+ adding fields to this structure is the way most parameters are
4354+ passed to argp_parse (the first three fields are usually used,
4355+ but not in this small program). There are also two global
4356+ variables that argp knows about defined here,
4357+ ARGP_PROGRAM_VERSION and ARGP_PROGRAM_BUG_ADDRESS (they are
4358+ global variables because they will almost always be constant
4359+ for a given program, even if it uses different argument
4360+ parsers for various tasks). */
4361+
4362+#include <stdlib.h>
4363+#include <argp.h>
4364+
4365+const char *argp_program_version =
4366+ "argp-ex2 1.0";
4367+const char *argp_program_bug_address =
4368+ "<bug-gnu-utils@@gnu.org>";
4369+
4370+/* Program documentation. */
4371+static char doc[] =
4372+ "Argp example #2 -- a pretty minimal program using argp";
4373+
4374+/* Our argument parser. The @code{options}, @code{parser}, and
4375+ @code{args_doc} fields are zero because we have neither options or
4376+ arguments; @code{doc} and @code{argp_program_bug_address} will be
4377+ used in the output for @samp{--help}, and the @samp{--version}
4378+ option will print out @code{argp_program_version}. */
4379+static struct argp argp = { 0, 0, 0, doc };
4380+
4381+int main (int argc, char **argv)
4382+{
4383+ argp_parse (&argp, argc, argv, 0, 0, 0);
4384+ exit (0);
4385+}
4386Index: git/test/argp/argp-ex3.c
4387===================================================================
4388--- /dev/null
4389+++ git/test/argp/argp-ex3.c
4390@@ -0,0 +1,153 @@
4391+/* Argp example #3 -- a program with options and arguments using argp */
4392+
4393+/* This program uses the same features as example 2, and uses options and
4394+ arguments.
4395+
4396+ We now use the first four fields in ARGP, so here's a description of them:
4397+ OPTIONS -- A pointer to a vector of struct argp_option (see below)
4398+ PARSER -- A function to parse a single option, called by argp
4399+ ARGS_DOC -- A string describing how the non-option arguments should look
4400+ DOC -- A descriptive string about this program; if it contains a
4401+ vertical tab character (\v), the part after it will be
4402+ printed *following* the options
4403+
4404+ The function PARSER takes the following arguments:
4405+ KEY -- An integer specifying which option this is (taken
4406+ from the KEY field in each struct argp_option), or
4407+ a special key specifying something else; the only
4408+ special keys we use here are ARGP_KEY_ARG, meaning
4409+ a non-option argument, and ARGP_KEY_END, meaning
4410+ that all arguments have been parsed
4411+ ARG -- For an option KEY, the string value of its
4412+ argument, or NULL if it has none
4413+ STATE-- A pointer to a struct argp_state, containing
4414+ various useful information about the parsing state; used here
4415+ are the INPUT field, which reflects the INPUT argument to
4416+ argp_parse, and the ARG_NUM field, which is the number of the
4417+ current non-option argument being parsed
4418+ It should return either 0, meaning success, ARGP_ERR_UNKNOWN, meaning the
4419+ given KEY wasn't recognized, or an errno value indicating some other
4420+ error.
4421+
4422+ Note that in this example, main uses a structure to communicate with the
4423+ parse_opt function, a pointer to which it passes in the INPUT argument to
4424+ argp_parse. Of course, it's also possible to use global variables
4425+ instead, but this is somewhat more flexible.
4426+
4427+ The OPTIONS field contains a pointer to a vector of struct argp_option's;
4428+ that structure has the following fields (if you assign your option
4429+ structures using array initialization like this example, unspecified
4430+ fields will be defaulted to 0, and need not be specified):
4431+ NAME -- The name of this option's long option (may be zero)
4432+ KEY -- The KEY to pass to the PARSER function when parsing this option,
4433+ *and* the name of this option's short option, if it is a
4434+ printable ascii character
4435+ ARG -- The name of this option's argument, if any
4436+ FLAGS -- Flags describing this option; some of them are:
4437+ OPTION_ARG_OPTIONAL -- The argument to this option is optional
4438+ OPTION_ALIAS -- This option is an alias for the
4439+ previous option
4440+ OPTION_HIDDEN -- Don't show this option in --help output
4441+ DOC -- A documentation string for this option, shown in --help output
4442+
4443+ An options vector should be terminated by an option with all fields zero. */
4444+
4445+#include <stdlib.h>
4446+#include <argp.h>
4447+
4448+const char *argp_program_version =
4449+ "argp-ex3 1.0";
4450+const char *argp_program_bug_address =
4451+ "<bug-gnu-utils@@gnu.org>";
4452+
4453+/* Program documentation. */
4454+static char doc[] =
4455+ "Argp example #3 -- a program with options and arguments using argp";
4456+
4457+/* A description of the arguments we accept. */
4458+static char args_doc[] = "ARG1 ARG2";
4459+
4460+/* The options we understand. */
4461+static struct argp_option options[] = {
4462+ {"verbose", 'v', 0, 0, "Produce verbose output" },
4463+ {"quiet", 'q', 0, 0, "Don't produce any output" },
4464+ {"silent", 's', 0, OPTION_ALIAS },
4465+ {"output", 'o', "FILE", 0,
4466+ "Output to FILE instead of standard output" },
4467+ { 0 }
4468+};
4469+
4470+/* Used by @code{main} to communicate with @code{parse_opt}. */
4471+struct arguments
4472+{
4473+ char *args[2]; /* @var{arg1} & @var{arg2} */
4474+ int silent, verbose;
4475+ char *output_file;
4476+};
4477+
4478+/* Parse a single option. */
4479+static error_t
4480+parse_opt (int key, char *arg, struct argp_state *state)
4481+{
4482+ /* Get the @var{input} argument from @code{argp_parse}, which we
4483+ know is a pointer to our arguments structure. */
4484+ struct arguments *arguments = state->input;
4485+
4486+ switch (key)
4487+ {
4488+ case 'q': case 's':
4489+ arguments->silent = 1;
4490+ break;
4491+ case 'v':
4492+ arguments->verbose = 1;
4493+ break;
4494+ case 'o':
4495+ arguments->output_file = arg;
4496+ break;
4497+
4498+ case ARGP_KEY_ARG:
4499+ if (state->arg_num >= 2)
4500+ /* Too many arguments. */
4501+ argp_usage (state);
4502+
4503+ arguments->args[state->arg_num] = arg;
4504+
4505+ break;
4506+
4507+ case ARGP_KEY_END:
4508+ if (state->arg_num < 2)
4509+ /* Not enough arguments. */
4510+ argp_usage (state);
4511+ break;
4512+
4513+ default:
4514+ return ARGP_ERR_UNKNOWN;
4515+ }
4516+ return 0;
4517+}
4518+
4519+/* Our argp parser. */
4520+static struct argp argp = { options, parse_opt, args_doc, doc };
4521+
4522+int main (int argc, char **argv)
4523+{
4524+ struct arguments arguments;
4525+
4526+ /* Default values. */
4527+ arguments.silent = 0;
4528+ arguments.verbose = 0;
4529+ arguments.output_file = "-";
4530+
4531+ /* Parse our arguments; every option seen by @code{parse_opt} will
4532+ be reflected in @code{arguments}. */
4533+ argp_parse (&argp, argc, argv, 0, 0, &arguments);
4534+
4535+ printf ("ARG1 = %s\nARG2 = %s\nOUTPUT_FILE = %s\n"
4536+ "VERBOSE = %s\nSILENT = %s\n",
4537+ arguments.args[0], arguments.args[1],
4538+ arguments.output_file,
4539+ arguments.verbose ? "yes" : "no",
4540+ arguments.silent ? "yes" : "no");
4541+
4542+ exit (0);
4543+}
4544Index: git/test/argp/argp-ex4.c
4545===================================================================
4546--- /dev/null
4547+++ git/test/argp/argp-ex4.c
4548@@ -0,0 +1,167 @@
4549+/* Argp example #4 -- a program with somewhat more complicated options */
4550+
4551+/* This program uses the same features as example 3, but has more
4552+ options, and somewhat more structure in the -help output. It
4553+ also shows how you can `steal' the remainder of the input
4554+ arguments past a certain point, for programs that accept a
4555+ list of items. It also shows the special argp KEY value
4556+ ARGP_KEY_NO_ARGS, which is only given if no non-option
4557+ arguments were supplied to the program.
4558+
4559+ For structuring the help output, two features are used,
4560+ *headers* which are entries in the options vector with the
4561+ first four fields being zero, and a two part documentation
4562+ string (in the variable DOC), which allows documentation both
4563+ before and after the options; the two parts of DOC are
4564+ separated by a vertical-tab character ('\v', or '\013'). By
4565+ convention, the documentation before the options is just a
4566+ short string saying what the program does, and that afterwards
4567+ is longer, describing the behavior in more detail. All
4568+ documentation strings are automatically filled for output,
4569+ although newlines may be included to force a line break at a
4570+ particular point. All documentation strings are also passed to
4571+ the `gettext' function, for possible translation into the
4572+ current locale. */
4573+
4574+#include <stdlib.h>
4575+#include <error.h>
4576+#include <argp.h>
4577+
4578+const char *argp_program_version =
4579+ "argp-ex4 1.0";
4580+const char *argp_program_bug_address =
4581+ "<bug-gnu-utils@@prep.ai.mit.edu>";
4582+
4583+/* Program documentation. */
4584+static char doc[] =
4585+ "Argp example #4 -- a program with somewhat more complicated\
4586+options\
4587+\vThis part of the documentation comes *after* the options;\
4588+ note that the text is automatically filled, but it's possible\
4589+ to force a line-break, e.g.\n<-- here.";
4590+
4591+/* A description of the arguments we accept. */
4592+static char args_doc[] = "ARG1 [STRING...]";
4593+
4594+/* Keys for options without short-options. */
4595+#define OPT_ABORT 1 /* --abort */
4596+
4597+/* The options we understand. */
4598+static struct argp_option options[] = {
4599+ {"verbose", 'v', 0, 0, "Produce verbose output" },
4600+ {"quiet", 'q', 0, 0, "Don't produce any output" },
4601+ {"silent", 's', 0, OPTION_ALIAS },
4602+ {"output", 'o', "FILE", 0,
4603+ "Output to FILE instead of standard output" },
4604+
4605+ {0,0,0,0, "The following options should be grouped together:" },
4606+ {"repeat", 'r', "COUNT", OPTION_ARG_OPTIONAL,
4607+ "Repeat the output COUNT (default 10) times"},
4608+ {"abort", OPT_ABORT, 0, 0, "Abort before showing any output"},
4609+
4610+ { 0 }
4611+};
4612+
4613+/* Used by @code{main} to communicate with @code{parse_opt}. */
4614+struct arguments
4615+{
4616+ char *arg1; /* @var{arg1} */
4617+ char **strings; /* [@var{string}@dots{}] */
4618+ int silent, verbose, abort; /* @samp{-s}, @samp{-v}, @samp{--abort} */
4619+ char *output_file; /* @var{file} arg to @samp{--output} */
4620+ int repeat_count; /* @var{count} arg to @samp{--repeat} */
4621+};
4622+
4623+/* Parse a single option. */
4624+static error_t
4625+parse_opt (int key, char *arg, struct argp_state *state)
4626+{
4627+ /* Get the @code{input} argument from @code{argp_parse}, which we
4628+ know is a pointer to our arguments structure. */
4629+ struct arguments *arguments = state->input;
4630+
4631+ switch (key)
4632+ {
4633+ case 'q': case 's':
4634+ arguments->silent = 1;
4635+ break;
4636+ case 'v':
4637+ arguments->verbose = 1;
4638+ break;
4639+ case 'o':
4640+ arguments->output_file = arg;
4641+ break;
4642+ case 'r':
4643+ arguments->repeat_count = arg ? atoi (arg) : 10;
4644+ break;
4645+ case OPT_ABORT:
4646+ arguments->abort = 1;
4647+ break;
4648+
4649+ case ARGP_KEY_NO_ARGS:
4650+ argp_usage (state);
4651+
4652+ case ARGP_KEY_ARG:
4653+ /* Here we know that @code{state->arg_num == 0}, since we
4654+ force argument parsing to end before any more arguments can
4655+ get here. */
4656+ arguments->arg1 = arg;
4657+
4658+ /* Now we consume all the rest of the arguments.
4659+ @code{state->next} is the index in @code{state->argv} of the
4660+ next argument to be parsed, which is the first @var{string}
4661+ we're interested in, so we can just use
4662+ @code{&state->argv[state->next]} as the value for
4663+ arguments->strings.
4664+
4665+ @emph{In addition}, by setting @code{state->next} to the end
4666+ of the arguments, we can force argp to stop parsing here and
4667+ return. */
4668+ arguments->strings = &state->argv[state->next];
4669+ state->next = state->argc;
4670+
4671+ break;
4672+
4673+ default:
4674+ return ARGP_ERR_UNKNOWN;
4675+ }
4676+ return 0;
4677+}
4678+
4679+/* Our argp parser. */
4680+static struct argp argp = { options, parse_opt, args_doc, doc };
4681+
4682+int main (int argc, char **argv)
4683+{
4684+ int i, j;
4685+ struct arguments arguments;
4686+
4687+ /* Default values. */
4688+ arguments.silent = 0;
4689+ arguments.verbose = 0;
4690+ arguments.output_file = "-";
4691+ arguments.repeat_count = 1;
4692+ arguments.abort = 0;
4693+
4694+ /* Parse our arguments; every option seen by @code{parse_opt} will be
4695+ reflected in @code{arguments}. */
4696+ argp_parse (&argp, argc, argv, 0, 0, &arguments);
4697+
4698+ if (arguments.abort)
4699+ error (10, 0, "ABORTED");
4700+
4701+ for (i = 0; i < arguments.repeat_count; i++)
4702+ {
4703+ printf ("ARG1 = %s\n", arguments.arg1);
4704+ printf ("STRINGS = ");
4705+ for (j = 0; arguments.strings[j]; j++)
4706+ printf (j == 0 ? "%s" : ", %s", arguments.strings[j]);
4707+ printf ("\n");
4708+ printf ("OUTPUT_FILE = %s\nVERBOSE = %s\nSILENT = %s\n",
4709+ arguments.output_file,
4710+ arguments.verbose ? "yes" : "no",
4711+ arguments.silent ? "yes" : "no");
4712+ }
4713+
4714+ exit (0);
4715+}
4716Index: git/test/argp/argp-test.c
4717===================================================================
4718--- /dev/null
4719+++ git/test/argp/argp-test.c
4720@@ -0,0 +1,209 @@
4721+/* Test program for argp argument parser
4722+ Copyright (C) 1997 Free Software Foundation, Inc.
4723+ This file is part of the GNU C Library.
4724+ Written by Miles Bader <miles at gnu.ai.mit.edu>.
4725+
4726+ The GNU C Library is free software; you can redistribute it and/or
4727+ modify it under the terms of the GNU Lesser General Public
4728+ License as published by the Free Software Foundation; either
4729+ version 2.1 of the License, or (at your option) any later version.
4730+
4731+ The GNU C Library is distributed in the hope that it will be useful,
4732+ but WITHOUT ANY WARRANTY; without even the implied warranty of
4733+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4734+ Lesser General Public License for more details.
4735+
4736+ You should have received a copy of the GNU Lesser General Public
4737+ License along with the GNU C Library; if not, write to the Free
4738+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
4739+ 02111-1307 USA. */
4740+
4741+#ifdef HAVE_CONFIG_H
4742+#include <config.h>
4743+#endif
4744+
4745+#include <stdlib.h>
4746+#include <time.h>
4747+#include <string.h>
4748+#include <argp.h>
4749+
4750+const char *argp_program_version = "argp-test 1.0";
4751+
4752+struct argp_option sub_options[] =
4753+{
4754+ {"subopt1", 's', 0, 0, "Nested option 1"},
4755+ {"subopt2", 'S', 0, 0, "Nested option 2"},
4756+
4757+ { 0, 0, 0, 0, "Some more nested options:", 10},
4758+ {"subopt3", 'p', 0, 0, "Nested option 3"},
4759+
4760+ {"subopt4", 'q', 0, 0, "Nested option 4", 1},
4761+
4762+ {0}
4763+};
4764+
4765+static const char sub_args_doc[] = "STRING...\n-";
4766+static const char sub_doc[] = "\vThis is the doc string from the sub-arg-parser.";
4767+
4768+static error_t
4769+sub_parse_opt (int key, char *arg, struct argp_state *state)
4770+{
4771+ switch (key)
4772+ {
4773+ case ARGP_KEY_NO_ARGS:
4774+ printf ("NO SUB ARGS\n");
4775+ break;
4776+ case ARGP_KEY_ARG:
4777+ printf ("SUB ARG: %s\n", arg);
4778+ break;
4779+
4780+ case 's' : case 'S': case 'p': case 'q':
4781+ printf ("SUB KEY %c\n", key);
4782+ break;
4783+
4784+ default:
4785+ return ARGP_ERR_UNKNOWN;
4786+ }
4787+ return 0;
4788+}
4789+
4790+static char *
4791+sub_help_filter (int key, const char *text, void *input)
4792+{
4793+ if (key == ARGP_KEY_HELP_EXTRA)
4794+ return strdup ("This is some extra text from the sub parser (note that it \
4795+is preceded by a blank line).");
4796+ else
4797+ return (char *)text;
4798+}
4799+
4800+static struct argp sub_argp = {
4801+ sub_options, sub_parse_opt, sub_args_doc, sub_doc, 0, sub_help_filter
4802+};
4803+
4804+/* Structure used to communicate with the parsing functions. */
4805+struct params
4806+{
4807+ unsigned foonly; /* Value parsed for foonly. */
4808+ unsigned foonly_default; /* Default value for it. */
4809+};
4810+
4811+#define OPT_PGRP 1
4812+#define OPT_SESS 2
4813+
4814+struct argp_option options[] =
4815+{
4816+ {"pid", 'p', "PID", 0, "List the process PID"},
4817+ {"pgrp", OPT_PGRP,"PGRP",0, "List processes in the process group PGRP"},
4818+ {"no-parent", 'P', 0, 0, "Include processes without parents"},
4819+ {0, 'x', 0, OPTION_ALIAS},
4820+ {"all-fields",'Q', 0, 0, "Don't elide unusable fields (normally"
4821+ " if there's some reason ps can't"
4822+ " print a field for any process, it's"
4823+ " removed from the output entirely)" },
4824+ {"reverse", 'r', 0, 0, "Reverse the order of any sort"},
4825+ {"gratuitously-long-reverse-option", 0, 0, OPTION_ALIAS},
4826+ {"session", OPT_SESS,"SID", OPTION_ARG_OPTIONAL,
4827+ "Add the processes from the session"
4828+ " SID (which defaults to the sid of"
4829+ " the current process)" },
4830+
4831+ {0,0,0,0, "Here are some more options:"},
4832+ {"foonly", 'f', "ZOT", OPTION_ARG_OPTIONAL, "Glork a foonly"},
4833+ {"zaza", 'z', 0, 0, "Snit a zar"},
4834+
4835+ {0}
4836+};
4837+
4838+static const char args_doc[] = "STRING";
4839+static const char doc[] = "Test program for argp."
4840+ "\vThis doc string comes after the options."
4841+ "\nHey! Some manual formatting!"
4842+ "\nThe current time is: %s";
4843+
4844+static void
4845+popt (int key, char *arg)
4846+{
4847+ char buf[10];
4848+ if (isprint (key))
4849+ sprintf (buf, "%c", key);
4850+ else
4851+ sprintf (buf, "%d", key);
4852+ if (arg)
4853+ printf ("KEY %s: %s\n", buf, arg);
4854+ else
4855+ printf ("KEY %s\n", buf);
4856+}
4857+
4858+static error_t
4859+parse_opt (int key, char *arg, struct argp_state *state)
4860+{
4861+ struct params *params = state->input;
4862+
4863+ switch (key)
4864+ {
4865+ case ARGP_KEY_NO_ARGS:
4866+ printf ("NO ARGS\n");
4867+ break;
4868+
4869+ case ARGP_KEY_ARG:
4870+ if (state->arg_num > 0)
4871+ return ARGP_ERR_UNKNOWN; /* Leave it for the sub-arg parser. */
4872+ printf ("ARG: %s\n", arg);
4873+ break;
4874+
4875+ case 'f':
4876+ if (arg)
4877+ params->foonly = atoi (arg);
4878+ else
4879+ params->foonly = params->foonly_default;
4880+ popt (key, arg);
4881+ break;
4882+
4883+ case 'p': case 'P': case OPT_PGRP: case 'x': case 'Q':
4884+ case 'r': case OPT_SESS: case 'z':
4885+ popt (key, arg);
4886+ break;
4887+
4888+ default:
4889+ return ARGP_ERR_UNKNOWN;
4890+ }
4891+ return 0;
4892+}
4893+
4894+static char *
4895+help_filter (int key, const char *text, void *input)
4896+{
4897+ char *new_text;
4898+ struct params *params = input;
4899+
4900+ if (key == ARGP_KEY_HELP_POST_DOC && text)
4901+ {
4902+ time_t now = time (0);
4903+ asprintf (&new_text, text, ctime (&now));
4904+ }
4905+ else if (key == 'f')
4906+ /* Show the default for the --foonly option. */
4907+ asprintf (&new_text, "%s (ZOT defaults to %x)",
4908+ text, params->foonly_default);
4909+ else
4910+ new_text = (char *)text;
4911+
4912+ return new_text;
4913+}
4914+
4915+static struct argp_child argp_children[] = { { &sub_argp }, { 0 } };
4916+static struct argp argp = {
4917+ options, parse_opt, args_doc, doc, argp_children, help_filter
4918+};
4919+
4920+int
4921+main (int argc, char **argv)
4922+{
4923+ struct params params;
4924+ params.foonly = 0;
4925+ params.foonly_default = random ();
4926+ argp_parse (&argp, argc, argv, 0, 0, &params);
4927+ printf ("After parsing: foonly = %x\n", params.foonly);
4928+ return 0;
4929+}
4930Index: git/test/argp/bug-argp1.c
4931===================================================================
4932--- /dev/null
4933+++ git/test/argp/bug-argp1.c
4934@@ -0,0 +1,26 @@
4935+#include <argp.h>
4936+
4937+
4938+static const struct argp_option test_options[] =
4939+{
4940+ { NULL, 'a', NULL, OPTION_DOC, NULL },
4941+ { NULL, 'b', NULL, OPTION_DOC, NULL },
4942+ { NULL, 0, NULL, 0, NULL }
4943+};
4944+
4945+static struct argp test_argp =
4946+{
4947+ test_options
4948+};
4949+
4950+
4951+static int
4952+do_test (int argc, char *argv[])
4953+{
4954+ int i;
4955+ argp_parse (&test_argp, argc, argv, 0, &i, NULL);
4956+ return 0;
4957+}
4958+
4959+#define TEST_FUNCTION do_test (argc, argv)
4960+#include "../test-skeleton.c"
4961Index: git/test/argp/tst-argp1.c
4962===================================================================
4963--- /dev/null
4964+++ git/test/argp/tst-argp1.c
4965@@ -0,0 +1,118 @@
4966+/* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
4967+ This file is part of the GNU C Library.
4968+ Contributed by Ulrich Drepper <drepper at redhat.com>, 2002.
4969+
4970+ The GNU C Library is free software; you can redistribute it and/or
4971+ modify it under the terms of the GNU Lesser General Public
4972+ License as published by the Free Software Foundation; either
4973+ version 2.1 of the License, or (at your option) any later version.
4974+
4975+ The GNU C Library is distributed in the hope that it will be useful,
4976+ but WITHOUT ANY WARRANTY; without even the implied warranty of
4977+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4978+ Lesser General Public License for more details.
4979+
4980+ You should have received a copy of the GNU Lesser General Public
4981+ License along with the GNU C Library; if not, write to the Free
4982+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
4983+ 02111-1307 USA. */
4984+
4985+#include <argp.h>
4986+
4987+
4988+
4989+
4990+#define OPT_TO_THREAD 300
4991+#define OPT_TO_PROCESS 301
4992+#define OPT_SYNC_SIGNAL 302
4993+#define OPT_SYNC_JOIN 303
4994+#define OPT_TOPLEVEL 304
4995+
4996+
4997+static const struct argp_option test_options[] =
4998+ {
4999+ { NULL, 0, NULL, 0, "\
5000+This is a test for threads so we allow ther user to selection the number of \
5001+threads which are used at any one time. Independently the total number of \
5002+rounds can be selected. This is the total number of threads which will have \
5003+run when the process terminates:" },
5004+ { "threads", 't', "NUMBER", 0, "Number of threads used at once" },
5005+ { "starts", 's', "NUMBER", 0, "Total number of working threads" },
5006+ { "toplevel", OPT_TOPLEVEL, "NUMBER", 0,
5007+ "Number of toplevel threads which start the other threads; this \
5008+implies --sync-join" },
5009+
5010+ { NULL, 0, NULL, 0, "\
5011+Each thread can do one of two things: sleep or do work. The latter is 100% \
5012+CPU bound. The work load is the probability a thread does work. All values \
5013+from zero to 100 (inclusive) are valid. How often each thread repeats this \
5014+can be determined by the number of rounds. The work cost determines how long \
5015+each work session (not sleeping) takes. If it is zero a thread would \
5016+effectively nothing. By setting the number of rounds to zero the thread \
5017+does no work at all and pure thread creation times can be measured." },
5018+ { "workload", 'w', "PERCENT", 0, "Percentage of time spent working" },
5019+ { "workcost", 'c', "NUMBER", 0,
5020+ "Factor in the cost of each round of working" },
5021+ { "rounds", 'r', "NUMBER", 0, "Number of rounds each thread runs" },
5022+
5023+ { NULL, 0, NULL, 0, "\
5024+There are a number of different methods how thread creation can be \
5025+synchronized. Synchronization is necessary since the number of concurrently \
5026+running threads is limited." },
5027+ { "sync-signal", OPT_SYNC_SIGNAL, NULL, 0,
5028+ "Synchronize using a signal (default)" },
5029+ { "sync-join", OPT_SYNC_JOIN, NULL, 0, "Synchronize using pthread_join" },
5030+
5031+ { NULL, 0, NULL, 0, "\
5032+One parameter for each threads execution is the size of the stack. If this \
5033+parameter is not used the system's default stack size is used. If many \
5034+threads are used the stack size should be chosen quite small." },
5035+ { "stacksize", 'S', "BYTES", 0, "Size of threads stack" },
5036+ { "guardsize", 'g', "BYTES", 0,
5037+ "Size of stack guard area; must fit into the stack" },
5038+
5039+ { NULL, 0, NULL, 0, "Signal options:" },
5040+ { "to-thread", OPT_TO_THREAD, NULL, 0, "Send signal to main thread" },
5041+ { "to-process", OPT_TO_PROCESS, NULL, 0,
5042+ "Send signal to process (default)" },
5043+
5044+ { NULL, 0, NULL, 0, "Administrative options:" },
5045+ { "progress", 'p', NULL, 0, "Show signs of progress" },
5046+ { "timing", 'T', NULL, 0,
5047+ "Measure time from startup to the last thread finishing" },
5048+ { NULL, 0, NULL, 0, NULL }
5049+ };
5050+
5051+/* Prototype for option handler. */
5052+static error_t parse_opt (int key, char *arg, struct argp_state *state);
5053+
5054+/* Data structure to communicate with argp functions. */
5055+static struct argp argp =
5056+{
5057+ test_options, parse_opt
5058+};
5059+
5060+
5061+static int
5062+do_test (void)
5063+{
5064+ int argc = 2;
5065+ char *argv[3] = { (char *) "tst-argp1", (char *) "--help", NULL };
5066+ int remaining;
5067+
5068+ /* Parse and process arguments. */
5069+ argp_parse (&argp, argc, argv, 0, &remaining, NULL);
5070+
5071+ return 0;
5072+}
5073+
5074+
5075+/* Handle program arguments. */
5076+static error_t
5077+parse_opt (int key, char *arg, struct argp_state *state)
5078+{
5079+ return ARGP_ERR_UNKNOWN;
5080+}
5081+
5082+#define TEST_FUNCTION do_test ()
5083+#include "../test-skeleton.c"
5084Index: git/test/argp/tst-argp2.c
5085===================================================================
5086--- /dev/null
5087+++ git/test/argp/tst-argp2.c
5088@@ -0,0 +1,101 @@
5089+/* Copyright (C) 2007 Free Software Foundation, Inc.
5090+ This file is part of the GNU C Library.
5091+ Contributed by Jakub Jelinek <jakub at redhat.com>, 2007.
5092+
5093+ The GNU C Library is free software; you can redistribute it and/or
5094+ modify it under the terms of the GNU Lesser General Public
5095+ License as published by the Free Software Foundation; either
5096+ version 2.1 of the License, or (at your option) any later version.
5097+
5098+ The GNU C Library is distributed in the hope that it will be useful,
5099+ but WITHOUT ANY WARRANTY; without even the implied warranty of
5100+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5101+ Lesser General Public License for more details.
5102+
5103+ You should have received a copy of the GNU Lesser General Public
5104+ License along with the GNU C Library; if not, write to the Free
5105+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
5106+ 02111-1307 USA. */
5107+
5108+#include <argp.h>
5109+
5110+static const struct argp_option opt1[] =
5111+ {
5112+ { "opt1", '1', "NUMBER", 0, "Option 1" },
5113+ { NULL, 0, NULL, 0, NULL }
5114+ };
5115+
5116+static const struct argp_option opt2[] =
5117+ {
5118+ { "opt2", '2', "NUMBER", 0, "Option 2" },
5119+ { NULL, 0, NULL, 0, NULL }
5120+ };
5121+
5122+static const struct argp_option opt3[] =
5123+ {
5124+ { "opt3", '3', "NUMBER", 0, "Option 3" },
5125+ { NULL, 0, NULL, 0, NULL }
5126+ };
5127+
5128+static const struct argp_option opt4[] =
5129+ {
5130+ { "opt4", '4', "NUMBER", 0, "Option 4" },
5131+ { NULL, 0, NULL, 0, NULL }
5132+ };
5133+
5134+static const struct argp_option opt5[] =
5135+ {
5136+ { "opt5", '5', "NUMBER", 0, "Option 5" },
5137+ { NULL, 0, NULL, 0, NULL }
5138+ };
5139+
5140+static struct argp argp5 =
5141+ {
5142+ opt5, NULL, "args doc5", "doc5", NULL, NULL, NULL
5143+ };
5144+
5145+static struct argp argp4 =
5146+ {
5147+ opt4, NULL, "args doc4", "doc4", NULL, NULL, NULL
5148+ };
5149+
5150+static struct argp argp3 =
5151+ {
5152+ opt3, NULL, "args doc3", "doc3", NULL, NULL, NULL
5153+ };
5154+
5155+static struct argp_child children2[] =
5156+ {
5157+ { &argp4, 0, "child3", 3 },
5158+ { &argp5, 0, "child4", 4 },
5159+ { NULL, 0, NULL, 0 }
5160+ };
5161+
5162+static struct argp argp2 =
5163+ {
5164+ opt2, NULL, "args doc2", "doc2", children2, NULL, NULL
5165+ };
5166+
5167+static struct argp_child children1[] =
5168+ {
5169+ { &argp2, 0, "child1", 1 },
5170+ { &argp3, 0, "child2", 2 },
5171+ { NULL, 0, NULL, 0 }
5172+ };
5173+
5174+static struct argp argp1 =
5175+ {
5176+ opt1, NULL, "args doc1", "doc1", children1, NULL, NULL
5177+ };
5178+
5179+
5180+static int
5181+do_test (void)
5182+{
5183+ argp_help (&argp1, stdout, ARGP_HELP_LONG, (char *) "tst-argp2");
5184+ return 0;
5185+}
5186+
5187+
5188+#define TEST_FUNCTION do_test ()
5189+#include "../test-skeleton.c"