Import argp-test.c from glibc 2.33.
This change assumes that asprintf is always available.
This commit is contained in:
parent
06a70d506f
commit
a5fcbcecd9
1 changed files with 31 additions and 73 deletions
104
argp-test.c
104
argp-test.c
|
@ -1,22 +1,21 @@
|
||||||
/* Test program for argp argument parser
|
/* Test program for argp argument parser
|
||||||
Copyright (C) 1997 Free Software Foundation, Inc.
|
Copyright (C) 1997-2021 Free Software Foundation, Inc.
|
||||||
This file is part of the GNU C Library.
|
This file is part of the GNU C Library.
|
||||||
Written by Miles Bader <miles@gnu.ai.mit.edu>.
|
Written by Miles Bader <miles@gnu.ai.mit.edu>.
|
||||||
|
|
||||||
The GNU C Library is free software; you can redistribute it and/or
|
The GNU C Library is free software; you can redistribute it and/or
|
||||||
modify it under the terms of the GNU Library General Public License as
|
modify it under the terms of the GNU Lesser General Public
|
||||||
published by the Free Software Foundation; either version 2 of the
|
License as published by the Free Software Foundation; either
|
||||||
License, or (at your option) any later version.
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
The GNU C Library is distributed in the hope that it will be useful,
|
The GNU C Library is distributed in the hope that it will be useful,
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
Library General Public License for more details.
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
You should have received a copy of the GNU Library General Public
|
You should have received a copy of the GNU Lesser General Public
|
||||||
License along with the GNU C Library; see the file COPYING.LIB. If not,
|
License along with the GNU C Library; if not, see
|
||||||
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
<https://www.gnu.org/licenses/>. */
|
||||||
Boston, MA 02111-1307, USA. */
|
|
||||||
|
|
||||||
#ifndef _GNU_SOURCE
|
#ifndef _GNU_SOURCE
|
||||||
# define _GNU_SOURCE 1
|
# define _GNU_SOURCE 1
|
||||||
|
@ -29,66 +28,28 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <argp.h>
|
||||||
|
|
||||||
#include "argp.h"
|
|
||||||
|
|
||||||
#if !HAVE_ASPRINTF
|
|
||||||
#include <stdarg.h>
|
|
||||||
|
|
||||||
static int
|
|
||||||
asprintf (char **result, const char *format, ...)
|
|
||||||
{
|
|
||||||
size_t size;
|
|
||||||
char *p;
|
|
||||||
|
|
||||||
for (size = 200, p = NULL;; size *= 2)
|
|
||||||
{
|
|
||||||
va_list args;
|
|
||||||
int written;
|
|
||||||
|
|
||||||
p = realloc(p, size + 1);
|
|
||||||
if (!p)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Virtual memory exhausted.\n");
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
p[size] = '\0';
|
|
||||||
|
|
||||||
va_start(args, format);
|
|
||||||
written = vsnprintf(p, size, format, args);
|
|
||||||
va_end(args);
|
|
||||||
|
|
||||||
if (written >= 0)
|
|
||||||
{
|
|
||||||
*result = p;
|
|
||||||
return written;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif /* !HAVE_ASPRINTF */
|
|
||||||
|
|
||||||
const char *argp_program_version = "argp-test 1.0";
|
const char *argp_program_version = "argp-test 1.0";
|
||||||
|
|
||||||
struct argp_option sub_options[] =
|
struct argp_option sub_options[] =
|
||||||
{
|
{
|
||||||
{"subopt1", 's', 0, 0, "Nested option 1", 0},
|
{"subopt1", 's', 0, 0, "Nested option 1"},
|
||||||
{"subopt2", 'S', 0, 0, "Nested option 2", 0},
|
{"subopt2", 'S', 0, 0, "Nested option 2"},
|
||||||
|
|
||||||
{ 0, 0, 0, 0, "Some more nested options:", 10},
|
{ 0, 0, 0, 0, "Some more nested options:", 10},
|
||||||
{"subopt3", 'p', 0, 0, "Nested option 3", 0},
|
{"subopt3", 'p', 0, 0, "Nested option 3"},
|
||||||
|
|
||||||
{"subopt4", 'q', 0, 0, "Nested option 4", 1},
|
{"subopt4", 'q', 0, 0, "Nested option 4", 1},
|
||||||
|
|
||||||
{0, 0, 0, 0, 0, 0}
|
{0}
|
||||||
};
|
};
|
||||||
|
|
||||||
static const char sub_args_doc[] = "STRING...\n-";
|
static const char sub_args_doc[] = "STRING...\n-";
|
||||||
static const char sub_doc[] = "\vThis is the doc string from the sub-arg-parser.";
|
static const char sub_doc[] = "\vThis is the doc string from the sub-arg-parser.";
|
||||||
|
|
||||||
static error_t
|
static error_t
|
||||||
sub_parse_opt (int key, char *arg, struct argp_state *state UNUSED)
|
sub_parse_opt (int key, char *arg, struct argp_state *state)
|
||||||
{
|
{
|
||||||
switch (key)
|
switch (key)
|
||||||
{
|
{
|
||||||
|
@ -110,7 +71,7 @@ sub_parse_opt (int key, char *arg, struct argp_state *state UNUSED)
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
sub_help_filter (int key, const char *text, void *input UNUSED)
|
sub_help_filter (int key, const char *text, void *input)
|
||||||
{
|
{
|
||||||
if (key == ARGP_KEY_HELP_EXTRA)
|
if (key == ARGP_KEY_HELP_EXTRA)
|
||||||
return strdup ("This is some extra text from the sub parser (note that it \
|
return strdup ("This is some extra text from the sub parser (note that it \
|
||||||
|
@ -120,7 +81,7 @@ is preceded by a blank line).");
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct argp sub_argp = {
|
static struct argp sub_argp = {
|
||||||
sub_options, sub_parse_opt, sub_args_doc, sub_doc, 0, sub_help_filter, 0
|
sub_options, sub_parse_opt, sub_args_doc, sub_doc, 0, sub_help_filter
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Structure used to communicate with the parsing functions. */
|
/* Structure used to communicate with the parsing functions. */
|
||||||
|
@ -135,26 +96,26 @@ struct params
|
||||||
|
|
||||||
struct argp_option options[] =
|
struct argp_option options[] =
|
||||||
{
|
{
|
||||||
{"pid", 'p', "PID", 0, "List the process PID", 0},
|
{"pid", 'p', "PID", 0, "List the process PID"},
|
||||||
{"pgrp", OPT_PGRP,"PGRP",0, "List processes in the process group PGRP", 0},
|
{"pgrp", OPT_PGRP,"PGRP",0, "List processes in the process group PGRP"},
|
||||||
{"no-parent", 'P', 0, 0, "Include processes without parents", 0},
|
{"no-parent", 'P', 0, 0, "Include processes without parents"},
|
||||||
{0, 'x', 0, OPTION_ALIAS, NULL, 0},
|
{0, 'x', 0, OPTION_ALIAS},
|
||||||
{"all-fields",'Q', 0, 0, "Don't elide unusable fields (normally"
|
{"all-fields",'Q', 0, 0, "Don't elide unusable fields (normally"
|
||||||
" if there's some reason ps can't"
|
" if there's some reason ps can't"
|
||||||
" print a field for any process, it's"
|
" print a field for any process, it's"
|
||||||
" removed from the output entirely)", 0},
|
" removed from the output entirely)" },
|
||||||
{"reverse", 'r', 0, 0, "Reverse the order of any sort", 0},
|
{"reverse", 'r', 0, 0, "Reverse the order of any sort"},
|
||||||
{"gratuitously-long-reverse-option", 0, 0, OPTION_ALIAS, NULL, 0},
|
{"gratuitously-long-reverse-option", 0, 0, OPTION_ALIAS},
|
||||||
{"session", OPT_SESS,"SID", OPTION_ARG_OPTIONAL,
|
{"session", OPT_SESS,"SID", OPTION_ARG_OPTIONAL,
|
||||||
"Add the processes from the session"
|
"Add the processes from the session"
|
||||||
" SID (which defaults to the sid of"
|
" SID (which defaults to the sid of"
|
||||||
" the current process)", 0},
|
" the current process)" },
|
||||||
|
|
||||||
{0,0,0,0, "Here are some more options:", 0},
|
{0,0,0,0, "Here are some more options:"},
|
||||||
{"foonly", 'f', "ZOT", OPTION_ARG_OPTIONAL, "Glork a foonly", 0},
|
{"foonly", 'f', "ZOT", OPTION_ARG_OPTIONAL, "Glork a foonly"},
|
||||||
{"zaza", 'z', 0, 0, "Snit a zar", 0},
|
{"zaza", 'z', 0, 0, "Snit a zar"},
|
||||||
|
|
||||||
{0, 0, 0, 0, 0, 0}
|
{0}
|
||||||
};
|
};
|
||||||
|
|
||||||
static const char args_doc[] = "STRING";
|
static const char args_doc[] = "STRING";
|
||||||
|
@ -166,7 +127,7 @@ static const char doc[] = "Test program for argp."
|
||||||
static void
|
static void
|
||||||
popt (int key, char *arg)
|
popt (int key, char *arg)
|
||||||
{
|
{
|
||||||
char buf[10];
|
char buf[12];
|
||||||
if (isprint (key))
|
if (isprint (key))
|
||||||
sprintf (buf, "%c", key);
|
sprintf (buf, "%c", key);
|
||||||
else
|
else
|
||||||
|
@ -234,12 +195,9 @@ help_filter (int key, const char *text, void *input)
|
||||||
return new_text;
|
return new_text;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct argp_child argp_children[] = {
|
static struct argp_child argp_children[] = { { &sub_argp }, { 0 } };
|
||||||
{ &sub_argp, 0, 0, 0 }, { 0, 0, 0, 0 }
|
|
||||||
};
|
|
||||||
|
|
||||||
static struct argp argp = {
|
static struct argp argp = {
|
||||||
options, parse_opt, args_doc, doc, argp_children, help_filter, 0
|
options, parse_opt, args_doc, doc, argp_children, help_filter
|
||||||
};
|
};
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
Loading…
Reference in a new issue