summaryrefslogtreecommitdiff
path: root/libshouldbeinlibc/argp-parse.c
diff options
context:
space:
mode:
authorMiles Bader <miles@gnu.org>1996-05-02 05:54:02 +0000
committerMiles Bader <miles@gnu.org>1996-05-02 05:54:02 +0000
commita2dbc963b927c995ea02ba83c197de2a465f6215 (patch)
tree6ffa8fe61e68d04e8820b9a4911cfda742380c73 /libshouldbeinlibc/argp-parse.c
parent1d14e9fea5d9f375df9bc9f068d8c0b808ec13e7 (diff)
(_argp_hang): New variable.
(OPT_HANG): New macro. (argp_default_options, argp_default_parser): Add hidden --HANG option. (argp_default_parser): New function. (argp_version_options, argp_version_argp): New variables. (argp_parse): Use ARGP_VERSION_ARGP when appropiate.
Diffstat (limited to 'libshouldbeinlibc/argp-parse.c')
-rw-r--r--libshouldbeinlibc/argp-parse.c50
1 files changed, 49 insertions, 1 deletions
diff --git a/libshouldbeinlibc/argp-parse.c b/libshouldbeinlibc/argp-parse.c
index daae3f28..6f7a1279 100644
--- a/libshouldbeinlibc/argp-parse.c
+++ b/libshouldbeinlibc/argp-parse.c
@@ -22,7 +22,8 @@
#include <stdlib.h>
#include <string.h>
-#include <limits.h> /* for CHAR_BIT */
+#include <unistd.h>
+#include <limits.h>
#include <getopt.h>
#include <cthreads.h>
@@ -45,15 +46,25 @@
#define USER_MASK ((1 << USER_BITS) - 1)
/* ---------------------------------------------------------------- */
+/* Default options. */
+
+/* When argp is given the --HANG switch, _ARGP_HANG is set and argp will sleep
+ for one second intervals, decrementing _ARGP_HANG until it's zero. Thus
+ you can force the program to continue by attaching a debugger and setting
+ it to 0 yourself. */
+volatile int _argp_hang = 0;
#define OPT_PROGNAME -2
#define OPT_USAGE -3
+#define OPT_HANG -4
static const struct argp_option argp_default_options[] =
{
{"help", '?', 0, 0, "Give this help list", -1},
{"usage", OPT_USAGE, 0, 0, "Give a short usage message"},
{"program-name",OPT_PROGNAME,"NAME", OPTION_HIDDEN, "Set the program name"},
+ {"HANG", OPT_HANG, "SECS", OPTION_ARG_OPTIONAL | OPTION_HIDDEN,
+ "Hang for SECS seconds (default 3600)"},
{0, 0}
};
@@ -83,6 +94,11 @@ argp_default_parser (int key, char *arg, struct argp_state *state)
break;
+ case OPT_HANG:
+ _argp_hang = atoi (arg ?: "3600");
+ while (_argp_hang-- > 0)
+ sleep (1);
+
default:
return EINVAL;
}
@@ -93,6 +109,36 @@ static const struct argp argp_default_argp =
{argp_default_options, &argp_default_parser};
+static const struct argp_option argp_version_options[] =
+{
+ {"version", 'v', 0, 0, "Print program version", -1},
+ {0, 0}
+};
+
+static error_t
+argp_version_parser (int key, char *arg, struct argp_state *state)
+{
+ switch (key)
+ {
+ case 'v':
+ if (argp_program_version_hook)
+ (*argp_program_version_hook) ();
+ else if (argp_program_version)
+ puts (argp_program_version);
+ else
+ argp_error (state, "No version known!?");
+ if (! (state->flags & ARGP_NO_EXIT))
+ exit (0);
+ break;
+ default:
+ return EINVAL;
+ }
+ return 0;
+}
+
+static const struct argp argp_version_argp =
+ {argp_version_options, &argp_version_parser};
+
/* ---------------------------------------------------------------- */
/* Returns the offset into the getopt long options array LONG_OPTIONS of a
@@ -283,6 +329,8 @@ argp_parse (const struct argp *argp, int argc, char **argv, unsigned flags,
if (state.argp)
*plist++ = state.argp;
*plist++ = &argp_default_argp;
+ if (argp_program_version || argp_program_version_hook)
+ *plist++ = &argp_version_argp;
*plist = 0;
state.argp = top_argp;