2003-01-09 13:36:05 +01:00
|
|
|
/* [argparse.c wk 17.06.97] Argument Parser for option handling
|
2015-03-17 09:19:55 +01:00
|
|
|
* Copyright (C) 1998-2001, 2006-2008, 2012 Free Software Foundation, Inc.
|
2017-01-23 19:16:55 +01:00
|
|
|
* Copyright (C) 1997-2001, 2006-2008, 2013-2017 Werner Koch
|
2003-01-09 13:36:05 +01:00
|
|
|
*
|
2015-04-24 16:42:28 +02:00
|
|
|
* This file is part of GnuPG.
|
2003-01-09 13:36:05 +01:00
|
|
|
*
|
2017-02-24 13:48:28 +01:00
|
|
|
* GnuPG is free software; you can redistribute and/or modify this
|
|
|
|
* part of GnuPG under the terms of either
|
2011-09-30 12:52:11 +02:00
|
|
|
*
|
|
|
|
* - the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 3 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or
|
|
|
|
*
|
|
|
|
* - the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or both in parallel, as here.
|
2003-01-09 13:36:05 +01:00
|
|
|
*
|
2015-04-24 16:42:28 +02:00
|
|
|
* GnuPG is distributed in the hope that it will be useful, but
|
2006-10-02 13:54:35 +02:00
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2011-09-30 12:52:11 +02:00
|
|
|
* General Public License for more details.
|
2003-01-09 13:36:05 +01:00
|
|
|
*
|
2011-09-30 12:52:11 +02:00
|
|
|
* You should have received a copies of the GNU General Public License
|
|
|
|
* and the GNU Lesser General Public License along with this program;
|
2016-05-03 15:55:08 +02:00
|
|
|
* if not, see <https://gnu.org/licenses/>.
|
2003-01-09 13:36:05 +01:00
|
|
|
*/
|
|
|
|
|
2015-03-17 09:19:55 +01:00
|
|
|
/* This file may be used as part of GnuPG or standalone. A GnuPG
|
|
|
|
build is detected by the presence of the macro GNUPG_MAJOR_VERSION.
|
|
|
|
Some feature are only availalbe in the GnuPG build mode.
|
|
|
|
*/
|
|
|
|
|
2008-11-12 18:41:49 +01:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2003-01-09 13:36:05 +01:00
|
|
|
#include <config.h>
|
2008-11-12 18:41:49 +01:00
|
|
|
#endif
|
|
|
|
|
2003-01-09 13:36:05 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
2010-05-30 14:06:38 +02:00
|
|
|
#include <stdarg.h>
|
2014-10-29 17:07:51 +01:00
|
|
|
#include <limits.h>
|
|
|
|
#include <errno.h>
|
2003-01-09 13:36:05 +01:00
|
|
|
|
2015-03-17 09:19:55 +01:00
|
|
|
#ifdef GNUPG_MAJOR_VERSION
|
2015-04-24 15:19:10 +02:00
|
|
|
# include "util.h"
|
|
|
|
# include "common-defs.h"
|
|
|
|
# include "i18n.h"
|
2015-03-17 09:19:55 +01:00
|
|
|
# include "mischelp.h"
|
|
|
|
# include "stringhelp.h"
|
|
|
|
# include "logging.h"
|
2015-04-24 15:19:10 +02:00
|
|
|
# include "utf8conv.h"
|
2015-03-17 09:19:55 +01:00
|
|
|
#endif /*GNUPG_MAJOR_VERSION*/
|
|
|
|
|
2003-01-09 13:36:05 +01:00
|
|
|
#include "argparse.h"
|
|
|
|
|
2015-03-17 09:19:55 +01:00
|
|
|
/* GnuPG uses GPLv3+ but a standalone version of this defaults to
|
|
|
|
GPLv2+ because that is the license of this file. Change this if
|
|
|
|
you include it in a program which uses GPLv3. If you don't want to
|
2017-02-20 22:19:50 +01:00
|
|
|
set a copyright string for your usage() you may also hardcode it
|
2015-03-17 09:19:55 +01:00
|
|
|
here. */
|
|
|
|
#ifndef GNUPG_MAJOR_VERSION
|
|
|
|
|
|
|
|
# define ARGPARSE_GPL_VERSION 2
|
|
|
|
# define ARGPARSE_CRIGHT_STR "Copyright (C) YEAR NAME"
|
|
|
|
|
|
|
|
#else /* Used by GnuPG */
|
|
|
|
|
|
|
|
# define ARGPARSE_GPL_VERSION 3
|
2017-01-23 19:16:55 +01:00
|
|
|
# define ARGPARSE_CRIGHT_STR "Copyright (C) 2017 Free Software Foundation, Inc."
|
2015-03-17 09:19:55 +01:00
|
|
|
|
|
|
|
#endif /*GNUPG_MAJOR_VERSION*/
|
|
|
|
|
|
|
|
/* Replacements for standalone builds. */
|
|
|
|
#ifndef GNUPG_MAJOR_VERSION
|
|
|
|
# ifndef _
|
|
|
|
# define _(a) (a)
|
|
|
|
# endif
|
|
|
|
# ifndef DIM
|
|
|
|
# define DIM(v) (sizeof(v)/sizeof((v)[0]))
|
|
|
|
# endif
|
2015-04-24 15:19:10 +02:00
|
|
|
# define xtrymalloc(a) malloc ((a))
|
|
|
|
# define xtryrealloc(a,b) realloc ((a), (b))
|
|
|
|
# define xtrystrdup(a) strdup ((a))
|
|
|
|
# define xfree(a) free ((a))
|
|
|
|
# define log_error my_log_error
|
|
|
|
# define log_bug my_log_bug
|
|
|
|
# define trim_spaces(a) my_trim_spaces ((a))
|
2015-03-17 09:19:55 +01:00
|
|
|
# define map_static_macro_string(a) (a)
|
|
|
|
#endif /*!GNUPG_MAJOR_VERSION*/
|
|
|
|
|
|
|
|
|
|
|
|
#define ARGPARSE_STR(v) #v
|
|
|
|
#define ARGPARSE_STR2(v) ARGPARSE_STR(v)
|
|
|
|
|
|
|
|
|
|
|
|
/* Replacements for standalone builds. */
|
|
|
|
#ifndef GNUPG_MAJOR_VERSION
|
|
|
|
static void
|
|
|
|
my_log_error (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list arg_ptr ;
|
|
|
|
|
|
|
|
va_start (arg_ptr, fmt);
|
|
|
|
fprintf (stderr, "%s: ", strusage (11));
|
|
|
|
vfprintf (stderr, fmt, arg_ptr);
|
|
|
|
va_end (arg_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
my_log_bug (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list arg_ptr ;
|
|
|
|
|
|
|
|
va_start (arg_ptr, fmt);
|
|
|
|
fprintf (stderr, "%s: Ohhhh jeeee: ", strusage (11));
|
|
|
|
vfprintf (stderr, fmt, arg_ptr);
|
|
|
|
va_end (arg_ptr);
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
|
2015-08-31 20:21:43 +02:00
|
|
|
/* Return true if the native charset is utf-8. */
|
|
|
|
static int
|
|
|
|
is_native_utf8 (void)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-03-17 09:19:55 +01:00
|
|
|
static char *
|
|
|
|
my_trim_spaces (char *str)
|
|
|
|
{
|
|
|
|
char *string, *p, *mark;
|
|
|
|
|
|
|
|
string = str;
|
|
|
|
/* Find first non space character. */
|
|
|
|
for (p=string; *p && isspace (*(unsigned char*)p) ; p++)
|
|
|
|
;
|
|
|
|
/* Move characters. */
|
|
|
|
for ((mark = NULL); (*string = *p); string++, p++)
|
|
|
|
if (isspace (*(unsigned char*)p))
|
|
|
|
{
|
|
|
|
if (!mark)
|
|
|
|
mark = string;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mark = NULL;
|
|
|
|
if (mark)
|
|
|
|
*mark = '\0' ; /* Remove trailing spaces. */
|
|
|
|
|
|
|
|
return str ;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /*!GNUPG_MAJOR_VERSION*/
|
|
|
|
|
2003-01-09 13:36:05 +01:00
|
|
|
|
2008-09-29 17:02:55 +02:00
|
|
|
|
2003-01-09 13:36:05 +01:00
|
|
|
/*********************************
|
|
|
|
* @Summary arg_parse
|
2015-03-17 09:19:55 +01:00
|
|
|
* #include "argparse.h"
|
2003-01-09 13:36:05 +01:00
|
|
|
*
|
|
|
|
* typedef struct {
|
|
|
|
* char *argc; pointer to argc (value subject to change)
|
|
|
|
* char ***argv; pointer to argv (value subject to change)
|
|
|
|
* unsigned flags; Global flags (DO NOT CHANGE)
|
|
|
|
* int err; print error about last option
|
|
|
|
* 1 = warning, 2 = abort
|
|
|
|
* int r_opt; return option
|
|
|
|
* int r_type; type of return value (0 = no argument found)
|
|
|
|
* union {
|
|
|
|
* int ret_int;
|
|
|
|
* long ret_long
|
|
|
|
* ulong ret_ulong;
|
|
|
|
* char *ret_str;
|
|
|
|
* } r; Return values
|
|
|
|
* struct {
|
|
|
|
* int idx;
|
|
|
|
* const char *last;
|
|
|
|
* void *aliases;
|
|
|
|
* } internal; DO NOT CHANGE
|
|
|
|
* } ARGPARSE_ARGS;
|
|
|
|
*
|
|
|
|
* typedef struct {
|
|
|
|
* int short_opt;
|
|
|
|
* const char *long_opt;
|
|
|
|
* unsigned flags;
|
|
|
|
* } ARGPARSE_OPTS;
|
|
|
|
*
|
|
|
|
* int arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts );
|
|
|
|
*
|
|
|
|
* @Description
|
|
|
|
* This is my replacement for getopt(). See the example for a typical usage.
|
|
|
|
* Global flags are:
|
|
|
|
* Bit 0 : Do not remove options form argv
|
|
|
|
* Bit 1 : Do not stop at last option but return other args
|
|
|
|
* with r_opt set to -1.
|
|
|
|
* Bit 2 : Assume options and real args are mixed.
|
|
|
|
* Bit 3 : Do not use -- to stop option processing.
|
|
|
|
* Bit 4 : Do not skip the first arg.
|
|
|
|
* Bit 5 : allow usage of long option with only one dash
|
|
|
|
* Bit 6 : ignore --version
|
|
|
|
* all other bits must be set to zero, this value is modified by the
|
|
|
|
* function, so assume this is write only.
|
|
|
|
* Local flags (for each option):
|
|
|
|
* Bit 2-0 : 0 = does not take an argument
|
|
|
|
* 1 = takes int argument
|
|
|
|
* 2 = takes string argument
|
|
|
|
* 3 = takes long argument
|
|
|
|
* 4 = takes ulong argument
|
|
|
|
* Bit 3 : argument is optional (r_type will the be set to 0)
|
|
|
|
* Bit 4 : allow 0x etc. prefixed values.
|
2013-04-18 14:40:43 +02:00
|
|
|
* Bit 6 : Ignore this option
|
|
|
|
* Bit 7 : This is a command and not an option
|
2003-01-09 13:36:05 +01:00
|
|
|
* You stop the option processing by setting opts to NULL, the function will
|
|
|
|
* then return 0.
|
|
|
|
* @Return Value
|
|
|
|
* Returns the args.r_opt or 0 if ready
|
|
|
|
* r_opt may be -2/-7 to indicate an unknown option/command.
|
|
|
|
* @See Also
|
|
|
|
* ArgExpand
|
|
|
|
* @Notes
|
|
|
|
* You do not need to process the options 'h', '--help' or '--version'
|
|
|
|
* because this function includes standard help processing; but if you
|
|
|
|
* specify '-h', '--help' or '--version' you have to do it yourself.
|
|
|
|
* The option '--' stops argument processing; if bit 1 is set the function
|
|
|
|
* continues to return normal arguments.
|
|
|
|
* To process float args or unsigned args you must use a string args and do
|
|
|
|
* the conversion yourself.
|
|
|
|
* @Example
|
|
|
|
*
|
|
|
|
* ARGPARSE_OPTS opts[] = {
|
|
|
|
* { 'v', "verbose", 0 },
|
|
|
|
* { 'd', "debug", 0 },
|
|
|
|
* { 'o', "output", 2 },
|
|
|
|
* { 'c', "cross-ref", 2|8 },
|
|
|
|
* { 'm', "my-option", 1|8 },
|
2013-04-18 14:40:43 +02:00
|
|
|
* { 300, "ignored-long-option, ARGPARSE_OP_IGNORE},
|
2003-01-09 13:36:05 +01:00
|
|
|
* { 500, "have-no-short-option-for-this-long-option", 0 },
|
|
|
|
* {0} };
|
|
|
|
* ARGPARSE_ARGS pargs = { &argc, &argv, 0 }
|
|
|
|
*
|
|
|
|
* while( ArgParse( &pargs, &opts) ) {
|
|
|
|
* switch( pargs.r_opt ) {
|
|
|
|
* case 'v': opt.verbose++; break;
|
|
|
|
* case 'd': opt.debug++; break;
|
|
|
|
* case 'o': opt.outfile = pargs.r.ret_str; break;
|
|
|
|
* case 'c': opt.crf = pargs.r_type? pargs.r.ret_str:"a.crf"; break;
|
|
|
|
* case 'm': opt.myopt = pargs.r_type? pargs.r.ret_int : 1; break;
|
|
|
|
* case 500: opt.a_long_one++; break
|
|
|
|
* default : pargs.err = 1; break; -- force warning output --
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* if( argc > 1 )
|
|
|
|
* log_fatal( "Too many args");
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct alias_def_s *ALIAS_DEF;
|
|
|
|
struct alias_def_s {
|
|
|
|
ALIAS_DEF next;
|
|
|
|
char *name; /* malloced buffer with name, \0, value */
|
|
|
|
const char *value; /* ptr into name */
|
|
|
|
};
|
|
|
|
|
2012-12-18 18:06:41 +01:00
|
|
|
|
|
|
|
/* Object to store the names for the --ignore-invalid-option option.
|
|
|
|
This is a simple linked list. */
|
|
|
|
typedef struct iio_item_def_s *IIO_ITEM_DEF;
|
|
|
|
struct iio_item_def_s
|
|
|
|
{
|
|
|
|
IIO_ITEM_DEF next;
|
|
|
|
char name[1]; /* String with the long option name. */
|
|
|
|
};
|
|
|
|
|
2003-01-09 13:36:05 +01:00
|
|
|
static const char *(*strusage_handler)( int ) = NULL;
|
2010-05-30 14:06:38 +02:00
|
|
|
static int (*custom_outfnc) (int, const char *);
|
2003-01-09 13:36:05 +01:00
|
|
|
|
|
|
|
static int set_opt_arg(ARGPARSE_ARGS *arg, unsigned flags, char *s);
|
|
|
|
static void show_help(ARGPARSE_OPTS *opts, unsigned flags);
|
|
|
|
static void show_version(void);
|
2010-05-30 14:06:38 +02:00
|
|
|
static int writestrings (int is_error, const char *string, ...)
|
2011-02-03 16:50:01 +01:00
|
|
|
#if __GNUC__ >= 4
|
2010-05-30 14:06:38 +02:00
|
|
|
__attribute__ ((sentinel(0)))
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
argparse_register_outfnc (int (*fnc)(int, const char *))
|
|
|
|
{
|
|
|
|
custom_outfnc = fnc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Write STRING and all following const char * arguments either to
|
|
|
|
stdout or, if IS_ERROR is set, to stderr. The list of strings must
|
|
|
|
be terminated by a NULL. */
|
|
|
|
static int
|
|
|
|
writestrings (int is_error, const char *string, ...)
|
|
|
|
{
|
|
|
|
va_list arg_ptr;
|
|
|
|
const char *s;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
if (string)
|
|
|
|
{
|
|
|
|
s = string;
|
|
|
|
va_start (arg_ptr, string);
|
|
|
|
do
|
|
|
|
{
|
2014-02-26 16:50:59 +01:00
|
|
|
if (custom_outfnc)
|
|
|
|
custom_outfnc (is_error? 2:1, s);
|
2010-05-30 14:06:38 +02:00
|
|
|
else
|
2014-02-26 16:50:59 +01:00
|
|
|
fputs (s, is_error? stderr : stdout);
|
|
|
|
count += strlen (s);
|
2010-05-30 14:06:38 +02:00
|
|
|
}
|
|
|
|
while ((s = va_arg (arg_ptr, const char *)));
|
|
|
|
va_end (arg_ptr);
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
flushstrings (int is_error)
|
|
|
|
{
|
|
|
|
if (custom_outfnc)
|
|
|
|
custom_outfnc (is_error? 2:1, NULL);
|
|
|
|
else
|
|
|
|
fflush (is_error? stderr : stdout);
|
|
|
|
}
|
2003-01-09 13:36:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
initialize( ARGPARSE_ARGS *arg, const char *filename, unsigned *lineno )
|
|
|
|
{
|
2011-02-03 16:50:01 +01:00
|
|
|
if( !(arg->flags & (1<<15)) )
|
|
|
|
{
|
2007-08-29 18:59:20 +02:00
|
|
|
/* Initialize this instance. */
|
|
|
|
arg->internal.idx = 0;
|
|
|
|
arg->internal.last = NULL;
|
|
|
|
arg->internal.inarg = 0;
|
|
|
|
arg->internal.stopped = 0;
|
|
|
|
arg->internal.aliases = NULL;
|
|
|
|
arg->internal.cur_alias = NULL;
|
2012-12-18 18:06:41 +01:00
|
|
|
arg->internal.iio_list = NULL;
|
2007-08-29 18:59:20 +02:00
|
|
|
arg->err = 0;
|
|
|
|
arg->flags |= 1<<15; /* Mark as initialized. */
|
|
|
|
if ( *arg->argc < 0 )
|
2015-04-24 15:19:10 +02:00
|
|
|
log_bug ("invalid argument for arg_parse\n");
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2011-02-03 16:50:01 +01:00
|
|
|
|
|
|
|
|
2007-08-29 18:59:20 +02:00
|
|
|
if (arg->err)
|
|
|
|
{
|
|
|
|
/* Last option was erroneous. */
|
|
|
|
const char *s;
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2007-08-29 18:59:20 +02:00
|
|
|
if (filename)
|
|
|
|
{
|
2008-09-29 17:02:55 +02:00
|
|
|
if ( arg->r_opt == ARGPARSE_UNEXPECTED_ARG )
|
2007-08-29 18:59:20 +02:00
|
|
|
s = _("argument not expected");
|
2008-09-29 17:02:55 +02:00
|
|
|
else if ( arg->r_opt == ARGPARSE_READ_ERROR )
|
2007-08-29 18:59:20 +02:00
|
|
|
s = _("read error");
|
2008-09-29 17:02:55 +02:00
|
|
|
else if ( arg->r_opt == ARGPARSE_KEYWORD_TOO_LONG )
|
2007-08-29 18:59:20 +02:00
|
|
|
s = _("keyword too long");
|
2008-09-29 17:02:55 +02:00
|
|
|
else if ( arg->r_opt == ARGPARSE_MISSING_ARG )
|
2007-08-29 18:59:20 +02:00
|
|
|
s = _("missing argument");
|
2014-10-29 17:07:51 +01:00
|
|
|
else if ( arg->r_opt == ARGPARSE_INVALID_ARG )
|
|
|
|
s = _("invalid argument");
|
2008-09-29 17:02:55 +02:00
|
|
|
else if ( arg->r_opt == ARGPARSE_INVALID_COMMAND )
|
2007-08-29 18:59:20 +02:00
|
|
|
s = _("invalid command");
|
2008-09-29 17:02:55 +02:00
|
|
|
else if ( arg->r_opt == ARGPARSE_INVALID_ALIAS )
|
2007-08-29 18:59:20 +02:00
|
|
|
s = _("invalid alias definition");
|
2008-09-29 17:02:55 +02:00
|
|
|
else if ( arg->r_opt == ARGPARSE_OUT_OF_CORE )
|
|
|
|
s = _("out of core");
|
2007-08-29 18:59:20 +02:00
|
|
|
else
|
|
|
|
s = _("invalid option");
|
2015-04-24 15:19:10 +02:00
|
|
|
log_error ("%s:%u: %s\n", filename, *lineno, s);
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2011-02-03 16:50:01 +01:00
|
|
|
else
|
2007-08-29 18:59:20 +02:00
|
|
|
{
|
|
|
|
s = arg->internal.last? arg->internal.last:"[??]";
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2008-09-29 17:02:55 +02:00
|
|
|
if ( arg->r_opt == ARGPARSE_MISSING_ARG )
|
2015-04-24 15:19:10 +02:00
|
|
|
log_error (_("missing argument for option \"%.50s\"\n"), s);
|
2014-10-29 17:07:51 +01:00
|
|
|
else if ( arg->r_opt == ARGPARSE_INVALID_ARG )
|
2015-04-24 15:19:10 +02:00
|
|
|
log_error (_("invalid argument for option \"%.50s\"\n"), s);
|
2008-09-29 17:02:55 +02:00
|
|
|
else if ( arg->r_opt == ARGPARSE_UNEXPECTED_ARG )
|
2015-04-24 15:19:10 +02:00
|
|
|
log_error (_("option \"%.50s\" does not expect an argument\n"), s);
|
2008-09-29 17:02:55 +02:00
|
|
|
else if ( arg->r_opt == ARGPARSE_INVALID_COMMAND )
|
2015-04-24 15:19:10 +02:00
|
|
|
log_error (_("invalid command \"%.50s\"\n"), s);
|
2008-09-29 17:02:55 +02:00
|
|
|
else if ( arg->r_opt == ARGPARSE_AMBIGUOUS_OPTION )
|
2015-04-24 15:19:10 +02:00
|
|
|
log_error (_("option \"%.50s\" is ambiguous\n"), s);
|
2014-11-21 23:04:42 +01:00
|
|
|
else if ( arg->r_opt == ARGPARSE_AMBIGUOUS_COMMAND )
|
2015-04-24 15:19:10 +02:00
|
|
|
log_error (_("command \"%.50s\" is ambiguous\n"),s );
|
2008-09-29 17:02:55 +02:00
|
|
|
else if ( arg->r_opt == ARGPARSE_OUT_OF_CORE )
|
2015-04-24 15:19:10 +02:00
|
|
|
log_error ("%s\n", _("out of core\n"));
|
2007-08-29 18:59:20 +02:00
|
|
|
else
|
2015-04-24 15:19:10 +02:00
|
|
|
log_error (_("invalid option \"%.50s\"\n"), s);
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2010-07-26 16:01:32 +02:00
|
|
|
if (arg->err != ARGPARSE_PRINT_WARNING)
|
2007-08-29 18:59:20 +02:00
|
|
|
exit (2);
|
|
|
|
arg->err = 0;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
|
|
|
|
2007-08-29 18:59:20 +02:00
|
|
|
/* Zero out the return value union. */
|
|
|
|
arg->r.ret_str = NULL;
|
|
|
|
arg->r.ret_long = 0;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
store_alias( ARGPARSE_ARGS *arg, char *name, char *value )
|
|
|
|
{
|
|
|
|
/* TODO: replace this dummy function with a rea one
|
|
|
|
* and fix the probelms IRIX has with (ALIAS_DEV)arg..
|
|
|
|
* used as lvalue
|
|
|
|
*/
|
2008-10-20 15:53:23 +02:00
|
|
|
(void)arg;
|
|
|
|
(void)name;
|
|
|
|
(void)value;
|
2003-01-09 13:36:05 +01:00
|
|
|
#if 0
|
2015-04-24 15:19:10 +02:00
|
|
|
ALIAS_DEF a = xmalloc( sizeof *a );
|
2003-01-09 13:36:05 +01:00
|
|
|
a->name = name;
|
|
|
|
a->value = value;
|
|
|
|
a->next = (ALIAS_DEF)arg->internal.aliases;
|
|
|
|
(ALIAS_DEF)arg->internal.aliases = a;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-12-18 18:06:41 +01:00
|
|
|
|
|
|
|
/* Return true if KEYWORD is in the ignore-invalid-option list. */
|
|
|
|
static int
|
|
|
|
ignore_invalid_option_p (ARGPARSE_ARGS *arg, const char *keyword)
|
|
|
|
{
|
|
|
|
IIO_ITEM_DEF item = arg->internal.iio_list;
|
|
|
|
|
|
|
|
for (; item; item = item->next)
|
|
|
|
if (!strcmp (item->name, keyword))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Add the keywords up to the next LF to the list of to be ignored
|
|
|
|
options. After returning FP will either be at EOF or the next
|
|
|
|
character read wll be the first of a new line. The function
|
|
|
|
returns 0 on success or true on malloc failure. */
|
|
|
|
static int
|
|
|
|
ignore_invalid_option_add (ARGPARSE_ARGS *arg, FILE *fp)
|
|
|
|
{
|
|
|
|
IIO_ITEM_DEF item;
|
|
|
|
int c;
|
|
|
|
char name[100];
|
|
|
|
int namelen = 0;
|
|
|
|
int ready = 0;
|
|
|
|
enum { skipWS, collectNAME, skipNAME, addNAME} state = skipWS;
|
|
|
|
|
|
|
|
while (!ready)
|
|
|
|
{
|
|
|
|
c = getc (fp);
|
|
|
|
if (c == '\n')
|
|
|
|
ready = 1;
|
|
|
|
else if (c == EOF)
|
|
|
|
{
|
|
|
|
c = '\n';
|
|
|
|
ready = 1;
|
|
|
|
}
|
|
|
|
again:
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case skipWS:
|
|
|
|
if (!isascii (c) || !isspace(c))
|
|
|
|
{
|
|
|
|
namelen = 0;
|
|
|
|
state = collectNAME;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case collectNAME:
|
|
|
|
if (isspace (c))
|
|
|
|
{
|
|
|
|
state = addNAME;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
else if (namelen < DIM(name)-1)
|
|
|
|
name[namelen++] = c;
|
|
|
|
else /* Too long. */
|
|
|
|
state = skipNAME;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case skipNAME:
|
|
|
|
if (isspace (c))
|
|
|
|
{
|
|
|
|
state = skipWS;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case addNAME:
|
|
|
|
name[namelen] = 0;
|
|
|
|
if (!ignore_invalid_option_p (arg, name))
|
|
|
|
{
|
2015-04-24 15:19:10 +02:00
|
|
|
item = xtrymalloc (sizeof *item + namelen);
|
2012-12-18 18:06:41 +01:00
|
|
|
if (!item)
|
|
|
|
return 1;
|
|
|
|
strcpy (item->name, name);
|
|
|
|
item->next = (IIO_ITEM_DEF)arg->internal.iio_list;
|
|
|
|
arg->internal.iio_list = item;
|
|
|
|
}
|
|
|
|
state = skipWS;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Clear the entire ignore-invalid-option list. */
|
|
|
|
static void
|
|
|
|
ignore_invalid_option_clear (ARGPARSE_ARGS *arg)
|
|
|
|
{
|
|
|
|
IIO_ITEM_DEF item, tmpitem;
|
|
|
|
|
|
|
|
for (item = arg->internal.iio_list; item; item = tmpitem)
|
|
|
|
{
|
|
|
|
tmpitem = item->next;
|
2015-04-24 15:19:10 +02:00
|
|
|
xfree (item);
|
2012-12-18 18:06:41 +01:00
|
|
|
}
|
|
|
|
arg->internal.iio_list = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-01-09 13:36:05 +01:00
|
|
|
/****************
|
|
|
|
* Get options from a file.
|
|
|
|
* Lines starting with '#' are comment lines.
|
|
|
|
* Syntax is simply a keyword and the argument.
|
|
|
|
* Valid keywords are all keywords from the long_opt list without
|
|
|
|
* the leading dashes. The special keywords "help", "warranty" and "version"
|
|
|
|
* are not valid here.
|
|
|
|
* The special keyword "alias" may be used to store alias definitions,
|
|
|
|
* which are later expanded like long options.
|
2012-12-18 18:06:41 +01:00
|
|
|
* The option
|
|
|
|
* ignore-invalid-option OPTIONNAMEs
|
|
|
|
* is recognized and updates a list of option which should be ignored if they
|
|
|
|
* are not defined.
|
2003-01-09 13:36:05 +01:00
|
|
|
* Caller must free returned strings.
|
|
|
|
* If called with FP set to NULL command line args are parse instead.
|
|
|
|
*
|
|
|
|
* Q: Should we allow the syntax
|
|
|
|
* keyword = value
|
|
|
|
* and accept for boolean options a value of 1/0, yes/no or true/false?
|
|
|
|
* Note: Abbreviation of options is here not allowed.
|
|
|
|
*/
|
|
|
|
int
|
2008-09-29 17:02:55 +02:00
|
|
|
optfile_parse (FILE *fp, const char *filename, unsigned *lineno,
|
2003-01-09 13:36:05 +01:00
|
|
|
ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts)
|
|
|
|
{
|
2008-09-29 17:02:55 +02:00
|
|
|
int state, i, c;
|
|
|
|
int idx=0;
|
|
|
|
char keyword[100];
|
|
|
|
char *buffer = NULL;
|
|
|
|
size_t buflen = 0;
|
|
|
|
int in_alias=0;
|
2016-12-08 20:25:48 +01:00
|
|
|
int unread_buf[3]; /* We use an int so that we can store EOF. */
|
|
|
|
int unread_buf_count = 0;
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2017-02-20 22:19:50 +01:00
|
|
|
if (!fp) /* Divert to arg_parse() in this case. */
|
2008-09-29 17:02:55 +02:00
|
|
|
return arg_parse (arg, opts);
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2008-09-29 17:02:55 +02:00
|
|
|
initialize (arg, filename, lineno);
|
|
|
|
|
2016-12-08 20:25:48 +01:00
|
|
|
/* If the LINENO is zero we assume that we are at the start of a
|
|
|
|
* file and we skip over a possible Byte Order Mark. */
|
|
|
|
if (!*lineno)
|
|
|
|
{
|
|
|
|
unread_buf[0] = getc (fp);
|
|
|
|
unread_buf[1] = getc (fp);
|
|
|
|
unread_buf[2] = getc (fp);
|
|
|
|
if (unread_buf[0] != 0xef
|
|
|
|
|| unread_buf[1] != 0xbb
|
|
|
|
|| unread_buf[2] != 0xbf)
|
|
|
|
unread_buf_count = 3;
|
|
|
|
}
|
|
|
|
|
2008-09-29 17:02:55 +02:00
|
|
|
/* Find the next keyword. */
|
|
|
|
state = i = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
2016-12-08 20:25:48 +01:00
|
|
|
if (unread_buf_count)
|
|
|
|
c = unread_buf[3 - unread_buf_count--];
|
|
|
|
else
|
|
|
|
c = getc (fp);
|
2008-09-29 17:02:55 +02:00
|
|
|
if (c == '\n' || c== EOF )
|
|
|
|
{
|
|
|
|
if ( c != EOF )
|
|
|
|
++*lineno;
|
|
|
|
if (state == -1)
|
|
|
|
break;
|
|
|
|
else if (state == 2)
|
|
|
|
{
|
|
|
|
keyword[i] = 0;
|
|
|
|
for (i=0; opts[i].short_opt; i++ )
|
|
|
|
{
|
|
|
|
if (opts[i].long_opt && !strcmp (opts[i].long_opt, keyword))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
idx = i;
|
|
|
|
arg->r_opt = opts[idx].short_opt;
|
2013-04-18 14:40:43 +02:00
|
|
|
if ((opts[idx].flags & ARGPARSE_OPT_IGNORE))
|
|
|
|
{
|
|
|
|
state = i = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (!opts[idx].short_opt )
|
2012-12-18 18:06:41 +01:00
|
|
|
{
|
|
|
|
if (!strcmp (keyword, "ignore-invalid-option"))
|
|
|
|
{
|
|
|
|
/* No argument - ignore this meta option. */
|
|
|
|
state = i = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (ignore_invalid_option_p (arg, keyword))
|
|
|
|
{
|
|
|
|
/* This invalid option is in the iio list. */
|
|
|
|
state = i = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
arg->r_opt = ((opts[idx].flags & ARGPARSE_OPT_COMMAND)
|
|
|
|
? ARGPARSE_INVALID_COMMAND
|
|
|
|
: ARGPARSE_INVALID_OPTION);
|
|
|
|
}
|
2013-04-18 14:40:43 +02:00
|
|
|
else if (!(opts[idx].flags & ARGPARSE_TYPE_MASK))
|
2008-09-29 17:02:55 +02:00
|
|
|
arg->r_type = 0; /* Does not take an arg. */
|
2013-04-18 14:40:43 +02:00
|
|
|
else if ((opts[idx].flags & ARGPARSE_OPT_OPTIONAL) )
|
2008-09-29 17:02:55 +02:00
|
|
|
arg->r_type = 0; /* Arg is optional. */
|
|
|
|
else
|
|
|
|
arg->r_opt = ARGPARSE_MISSING_ARG;
|
|
|
|
|
|
|
|
break;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2008-09-29 17:02:55 +02:00
|
|
|
else if (state == 3)
|
2011-02-03 16:50:01 +01:00
|
|
|
{
|
2008-09-29 17:02:55 +02:00
|
|
|
/* No argument found. */
|
|
|
|
if (in_alias)
|
|
|
|
arg->r_opt = ARGPARSE_MISSING_ARG;
|
2013-04-18 14:40:43 +02:00
|
|
|
else if (!(opts[idx].flags & ARGPARSE_TYPE_MASK))
|
2008-09-29 17:02:55 +02:00
|
|
|
arg->r_type = 0; /* Does not take an arg. */
|
2013-04-18 14:40:43 +02:00
|
|
|
else if ((opts[idx].flags & ARGPARSE_OPT_OPTIONAL))
|
2008-09-29 17:02:55 +02:00
|
|
|
arg->r_type = 0; /* No optional argument. */
|
|
|
|
else
|
|
|
|
arg->r_opt = ARGPARSE_MISSING_ARG;
|
|
|
|
|
|
|
|
break;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2008-09-29 17:02:55 +02:00
|
|
|
else if (state == 4)
|
|
|
|
{
|
|
|
|
/* Has an argument. */
|
2011-02-03 16:50:01 +01:00
|
|
|
if (in_alias)
|
2008-09-29 17:02:55 +02:00
|
|
|
{
|
|
|
|
if (!buffer)
|
|
|
|
arg->r_opt = ARGPARSE_UNEXPECTED_ARG;
|
2011-02-03 16:50:01 +01:00
|
|
|
else
|
2008-09-29 17:02:55 +02:00
|
|
|
{
|
|
|
|
char *p;
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2008-09-29 17:02:55 +02:00
|
|
|
buffer[i] = 0;
|
|
|
|
p = strpbrk (buffer, " \t");
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
*p++ = 0;
|
|
|
|
trim_spaces (p);
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2008-09-29 17:02:55 +02:00
|
|
|
if (!p || !*p)
|
|
|
|
{
|
2015-04-24 15:19:10 +02:00
|
|
|
xfree (buffer);
|
2008-09-29 17:02:55 +02:00
|
|
|
arg->r_opt = ARGPARSE_INVALID_ALIAS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
store_alias (arg, buffer, p);
|
|
|
|
}
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
|
|
|
}
|
2013-04-18 14:40:43 +02:00
|
|
|
else if (!(opts[idx].flags & ARGPARSE_TYPE_MASK))
|
2008-09-29 17:02:55 +02:00
|
|
|
arg->r_opt = ARGPARSE_UNEXPECTED_ARG;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
keyword[i] = 0;
|
2015-04-24 15:19:10 +02:00
|
|
|
buffer = xtrystrdup (keyword);
|
2008-09-29 17:02:55 +02:00
|
|
|
if (!buffer)
|
|
|
|
arg->r_opt = ARGPARSE_OUT_OF_CORE;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2008-09-29 17:02:55 +02:00
|
|
|
else
|
|
|
|
buffer[i] = 0;
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2008-09-29 17:02:55 +02:00
|
|
|
if (buffer)
|
|
|
|
{
|
|
|
|
trim_spaces (buffer);
|
|
|
|
p = buffer;
|
|
|
|
if (*p == '"')
|
2011-02-03 16:50:01 +01:00
|
|
|
{
|
2008-09-29 17:02:55 +02:00
|
|
|
/* Remove quotes. */
|
|
|
|
p++;
|
|
|
|
if (*p && p[strlen(p)-1] == '\"' )
|
|
|
|
p[strlen(p)-1] = 0;
|
|
|
|
}
|
|
|
|
if (!set_opt_arg (arg, opts[idx].flags, p))
|
2015-04-24 15:19:10 +02:00
|
|
|
xfree (buffer);
|
2016-07-01 14:09:58 +02:00
|
|
|
else
|
|
|
|
gpgrt_annotate_leaked_object (buffer);
|
2008-09-29 17:02:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (c == EOF)
|
|
|
|
{
|
2012-12-18 18:06:41 +01:00
|
|
|
ignore_invalid_option_clear (arg);
|
2008-09-29 17:02:55 +02:00
|
|
|
if (ferror (fp))
|
|
|
|
arg->r_opt = ARGPARSE_READ_ERROR;
|
|
|
|
else
|
|
|
|
arg->r_opt = 0; /* EOF. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
state = 0;
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
else if (state == -1)
|
|
|
|
; /* Skip. */
|
|
|
|
else if (state == 0 && isascii (c) && isspace(c))
|
|
|
|
; /* Skip leading white space. */
|
|
|
|
else if (state == 0 && c == '#' )
|
|
|
|
state = 1; /* Start of a comment. */
|
|
|
|
else if (state == 1)
|
|
|
|
; /* Skip comments. */
|
|
|
|
else if (state == 2 && isascii (c) && isspace(c))
|
|
|
|
{
|
|
|
|
/* Check keyword. */
|
|
|
|
keyword[i] = 0;
|
|
|
|
for (i=0; opts[i].short_opt; i++ )
|
|
|
|
if (opts[i].long_opt && !strcmp (opts[i].long_opt, keyword))
|
|
|
|
break;
|
|
|
|
idx = i;
|
|
|
|
arg->r_opt = opts[idx].short_opt;
|
2013-04-18 14:40:43 +02:00
|
|
|
if ((opts[idx].flags & ARGPARSE_OPT_IGNORE))
|
|
|
|
{
|
|
|
|
state = 1; /* Process like a comment. */
|
|
|
|
}
|
|
|
|
else if (!opts[idx].short_opt)
|
2008-09-29 17:02:55 +02:00
|
|
|
{
|
|
|
|
if (!strcmp (keyword, "alias"))
|
|
|
|
{
|
|
|
|
in_alias = 1;
|
|
|
|
state = 3;
|
|
|
|
}
|
2012-12-18 18:06:41 +01:00
|
|
|
else if (!strcmp (keyword, "ignore-invalid-option"))
|
|
|
|
{
|
|
|
|
if (ignore_invalid_option_add (arg, fp))
|
|
|
|
{
|
|
|
|
arg->r_opt = ARGPARSE_OUT_OF_CORE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
state = i = 0;
|
|
|
|
++*lineno;
|
|
|
|
}
|
|
|
|
else if (ignore_invalid_option_p (arg, keyword))
|
|
|
|
state = 1; /* Process like a comment. */
|
2011-02-03 16:50:01 +01:00
|
|
|
else
|
2008-09-29 17:02:55 +02:00
|
|
|
{
|
2008-11-12 18:41:49 +01:00
|
|
|
arg->r_opt = ((opts[idx].flags & ARGPARSE_OPT_COMMAND)
|
2008-09-29 17:02:55 +02:00
|
|
|
? ARGPARSE_INVALID_COMMAND
|
|
|
|
: ARGPARSE_INVALID_OPTION);
|
|
|
|
state = -1; /* Skip rest of line and leave. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
state = 3;
|
|
|
|
}
|
|
|
|
else if (state == 3)
|
|
|
|
{
|
|
|
|
/* Skip leading spaces of the argument. */
|
|
|
|
if (!isascii (c) || !isspace(c))
|
|
|
|
{
|
|
|
|
i = 0;
|
|
|
|
keyword[i++] = c;
|
|
|
|
state = 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (state == 4)
|
2011-02-03 16:50:01 +01:00
|
|
|
{
|
2008-09-29 17:02:55 +02:00
|
|
|
/* Collect the argument. */
|
|
|
|
if (buffer)
|
|
|
|
{
|
|
|
|
if (i < buflen-1)
|
|
|
|
buffer[i++] = c;
|
2011-02-03 16:50:01 +01:00
|
|
|
else
|
2008-09-29 17:02:55 +02:00
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
size_t tmplen = buflen + 50;
|
|
|
|
|
2015-04-24 15:19:10 +02:00
|
|
|
tmp = xtryrealloc (buffer, tmplen);
|
2008-09-29 17:02:55 +02:00
|
|
|
if (tmp)
|
|
|
|
{
|
|
|
|
buflen = tmplen;
|
|
|
|
buffer = tmp;
|
|
|
|
buffer[i++] = c;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-24 15:19:10 +02:00
|
|
|
xfree (buffer);
|
2008-09-29 17:02:55 +02:00
|
|
|
arg->r_opt = ARGPARSE_OUT_OF_CORE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (i < DIM(keyword)-1)
|
|
|
|
keyword[i++] = c;
|
2011-02-03 16:50:01 +01:00
|
|
|
else
|
2008-09-29 17:02:55 +02:00
|
|
|
{
|
|
|
|
size_t tmplen = DIM(keyword) + 50;
|
2015-04-24 15:19:10 +02:00
|
|
|
buffer = xtrymalloc (tmplen);
|
2008-09-29 17:02:55 +02:00
|
|
|
if (buffer)
|
|
|
|
{
|
|
|
|
buflen = tmplen;
|
|
|
|
memcpy(buffer, keyword, i);
|
|
|
|
buffer[i++] = c;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
arg->r_opt = ARGPARSE_OUT_OF_CORE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (i >= DIM(keyword)-1)
|
|
|
|
{
|
|
|
|
arg->r_opt = ARGPARSE_KEYWORD_TOO_LONG;
|
|
|
|
state = -1; /* Skip rest of line and leave. */
|
|
|
|
}
|
2011-02-03 16:50:01 +01:00
|
|
|
else
|
2008-09-29 17:02:55 +02:00
|
|
|
{
|
|
|
|
keyword[i++] = c;
|
|
|
|
state = 2;
|
|
|
|
}
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2008-09-29 17:02:55 +02:00
|
|
|
return arg->r_opt;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
find_long_option( ARGPARSE_ARGS *arg,
|
|
|
|
ARGPARSE_OPTS *opts, const char *keyword )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
size_t n;
|
|
|
|
|
2008-10-20 15:53:23 +02:00
|
|
|
(void)arg;
|
|
|
|
|
2003-01-09 13:36:05 +01:00
|
|
|
/* Would be better if we can do a binary search, but it is not
|
|
|
|
possible to reorder our option table because we would mess
|
|
|
|
up our help strings - What we can do is: Build a nice option
|
2015-11-16 12:41:46 +01:00
|
|
|
lookup table when this function is first invoked */
|
2003-01-09 13:36:05 +01:00
|
|
|
if( !*keyword )
|
|
|
|
return -1;
|
|
|
|
for(i=0; opts[i].short_opt; i++ )
|
|
|
|
if( opts[i].long_opt && !strcmp( opts[i].long_opt, keyword) )
|
|
|
|
return i;
|
2007-05-04 11:22:18 +02:00
|
|
|
#if 0
|
2003-01-09 13:36:05 +01:00
|
|
|
{
|
|
|
|
ALIAS_DEF a;
|
|
|
|
/* see whether it is an alias */
|
|
|
|
for( a = args->internal.aliases; a; a = a->next ) {
|
|
|
|
if( !strcmp( a->name, keyword) ) {
|
|
|
|
/* todo: must parse the alias here */
|
|
|
|
args->internal.cur_alias = a;
|
|
|
|
return -3; /* alias available */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-05-04 11:22:18 +02:00
|
|
|
#endif
|
2003-01-09 13:36:05 +01:00
|
|
|
/* not found, see whether it is an abbreviation */
|
|
|
|
/* aliases may not be abbreviated */
|
|
|
|
n = strlen( keyword );
|
|
|
|
for(i=0; opts[i].short_opt; i++ ) {
|
|
|
|
if( opts[i].long_opt && !strncmp( opts[i].long_opt, keyword, n ) ) {
|
|
|
|
int j;
|
|
|
|
for(j=i+1; opts[j].short_opt; j++ ) {
|
|
|
|
if( opts[j].long_opt
|
2017-01-10 21:59:36 +01:00
|
|
|
&& !strncmp( opts[j].long_opt, keyword, n )
|
|
|
|
&& !(opts[j].short_opt == opts[i].short_opt
|
|
|
|
&& opts[j].flags == opts[i].flags ) )
|
2003-01-09 13:36:05 +01:00
|
|
|
return -2; /* abbreviation is ambiguous */
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
2012-12-18 18:06:41 +01:00
|
|
|
return -1; /* Not found. */
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts)
|
|
|
|
{
|
2008-11-12 18:41:49 +01:00
|
|
|
int idx;
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
char *s, *s2;
|
|
|
|
int i;
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2017-08-01 08:28:01 +02:00
|
|
|
/* Fill in missing standard options: help, version, warranty and
|
|
|
|
* dump-options. */
|
|
|
|
ARGPARSE_OPTS help_opt
|
|
|
|
= ARGPARSE_s_n (ARGPARSE_SHORTOPT_HELP, "help", "@");
|
|
|
|
ARGPARSE_OPTS version_opt
|
|
|
|
= ARGPARSE_s_n (ARGPARSE_SHORTOPT_VERSION, "version", "@");
|
|
|
|
ARGPARSE_OPTS warranty_opt
|
|
|
|
= ARGPARSE_s_n (ARGPARSE_SHORTOPT_WARRANTY, "warranty", "@");
|
|
|
|
ARGPARSE_OPTS dump_options_opt
|
|
|
|
= ARGPARSE_s_n(ARGPARSE_SHORTOPT_DUMP_OPTIONS, "dump-options", "@");
|
2017-07-18 18:08:25 +02:00
|
|
|
int seen_help = 0;
|
|
|
|
int seen_version = 0;
|
|
|
|
int seen_warranty = 0;
|
|
|
|
int seen_dump_options = 0;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (opts[i].short_opt)
|
|
|
|
{
|
|
|
|
if (opts[i].long_opt)
|
|
|
|
{
|
|
|
|
if (!strcmp(opts[i].long_opt, help_opt.long_opt))
|
|
|
|
seen_help = 1;
|
|
|
|
else if (!strcmp(opts[i].long_opt, version_opt.long_opt))
|
|
|
|
seen_version = 1;
|
|
|
|
else if (!strcmp(opts[i].long_opt, warranty_opt.long_opt))
|
|
|
|
seen_warranty = 1;
|
|
|
|
else if (!strcmp(opts[i].long_opt, dump_options_opt.long_opt))
|
|
|
|
seen_dump_options = 1;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (! seen_help)
|
|
|
|
opts[i++] = help_opt;
|
|
|
|
if (! seen_version)
|
|
|
|
opts[i++] = version_opt;
|
|
|
|
if (! seen_warranty)
|
|
|
|
opts[i++] = warranty_opt;
|
|
|
|
if (! seen_dump_options)
|
|
|
|
opts[i++] = dump_options_opt;
|
|
|
|
|
2008-11-12 18:41:49 +01:00
|
|
|
initialize( arg, NULL, NULL );
|
|
|
|
argc = *arg->argc;
|
|
|
|
argv = *arg->argv;
|
|
|
|
idx = arg->internal.idx;
|
2003-01-09 13:36:05 +01:00
|
|
|
|
2008-11-12 18:41:49 +01:00
|
|
|
if (!idx && argc && !(arg->flags & ARGPARSE_FLAG_ARG0))
|
|
|
|
{
|
|
|
|
/* Skip the first argument. */
|
|
|
|
argc--; argv++; idx++;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2008-11-12 18:41:49 +01:00
|
|
|
next_one:
|
2011-02-03 16:50:01 +01:00
|
|
|
if (!argc)
|
|
|
|
{
|
2008-11-12 18:41:49 +01:00
|
|
|
/* No more args. */
|
|
|
|
arg->r_opt = 0;
|
|
|
|
goto leave; /* Ready. */
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
|
|
|
|
2008-11-12 18:41:49 +01:00
|
|
|
s = *argv;
|
|
|
|
arg->internal.last = s;
|
2003-01-09 13:36:05 +01:00
|
|
|
|
2011-02-03 16:50:01 +01:00
|
|
|
if (arg->internal.stopped && (arg->flags & ARGPARSE_FLAG_ALL))
|
2008-11-12 18:41:49 +01:00
|
|
|
{
|
|
|
|
arg->r_opt = ARGPARSE_IS_ARG; /* Not an option but an argument. */
|
|
|
|
arg->r_type = 2;
|
|
|
|
arg->r.ret_str = s;
|
|
|
|
argc--; argv++; idx++; /* set to next one */
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2011-02-03 16:50:01 +01:00
|
|
|
else if( arg->internal.stopped )
|
2008-11-12 18:41:49 +01:00
|
|
|
{
|
|
|
|
arg->r_opt = 0;
|
|
|
|
goto leave; /* Ready. */
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2008-11-12 18:41:49 +01:00
|
|
|
else if ( *s == '-' && s[1] == '-' )
|
|
|
|
{
|
|
|
|
/* Long option. */
|
|
|
|
char *argpos;
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2008-11-12 18:41:49 +01:00
|
|
|
arg->internal.inarg = 0;
|
2008-11-20 14:08:26 +01:00
|
|
|
if (!s[2] && !(arg->flags & ARGPARSE_FLAG_NOSTOP))
|
2011-02-03 16:50:01 +01:00
|
|
|
{
|
2008-11-12 18:41:49 +01:00
|
|
|
/* Stop option processing. */
|
|
|
|
arg->internal.stopped = 1;
|
2012-03-27 12:35:13 +02:00
|
|
|
arg->flags |= ARGPARSE_FLAG_STOP_SEEN;
|
2008-11-12 18:41:49 +01:00
|
|
|
argc--; argv++; idx++;
|
|
|
|
goto next_one;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
|
|
|
|
2008-11-12 18:41:49 +01:00
|
|
|
argpos = strchr( s+2, '=' );
|
|
|
|
if ( argpos )
|
|
|
|
*argpos = 0;
|
|
|
|
i = find_long_option ( arg, opts, s+2 );
|
|
|
|
if ( argpos )
|
|
|
|
*argpos = '=';
|
2003-01-09 13:36:05 +01:00
|
|
|
|
2017-07-18 18:08:25 +02:00
|
|
|
if (i > 0 && opts[i].short_opt == ARGPARSE_SHORTOPT_HELP)
|
2008-11-12 18:41:49 +01:00
|
|
|
show_help (opts, arg->flags);
|
2017-07-18 18:08:25 +02:00
|
|
|
else if (i > 0 && opts[i].short_opt == ARGPARSE_SHORTOPT_VERSION)
|
2008-11-12 18:41:49 +01:00
|
|
|
{
|
|
|
|
if (!(arg->flags & ARGPARSE_FLAG_NOVERSION))
|
|
|
|
{
|
|
|
|
show_version ();
|
|
|
|
exit(0);
|
|
|
|
}
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2017-07-18 18:08:25 +02:00
|
|
|
else if (i > 0 && opts[i].short_opt == ARGPARSE_SHORTOPT_WARRANTY)
|
2008-11-12 18:41:49 +01:00
|
|
|
{
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, strusage (16), "\n", NULL);
|
2008-11-12 18:41:49 +01:00
|
|
|
exit (0);
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2017-07-18 18:08:25 +02:00
|
|
|
else if (i > 0 && opts[i].short_opt == ARGPARSE_SHORTOPT_DUMP_OPTIONS)
|
2008-11-12 18:41:49 +01:00
|
|
|
{
|
|
|
|
for (i=0; opts[i].short_opt; i++ )
|
|
|
|
{
|
2013-04-18 14:40:43 +02:00
|
|
|
if (opts[i].long_opt && !(opts[i].flags & ARGPARSE_OPT_IGNORE))
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, "--", opts[i].long_opt, "\n", NULL);
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2008-11-12 18:41:49 +01:00
|
|
|
exit (0);
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2008-11-12 18:41:49 +01:00
|
|
|
if ( i == -2 )
|
|
|
|
arg->r_opt = ARGPARSE_AMBIGUOUS_OPTION;
|
|
|
|
else if ( i == -1 )
|
|
|
|
{
|
|
|
|
arg->r_opt = ARGPARSE_INVALID_OPTION;
|
|
|
|
arg->r.ret_str = s+2;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2008-11-12 18:41:49 +01:00
|
|
|
else
|
|
|
|
arg->r_opt = opts[i].short_opt;
|
|
|
|
if ( i < 0 )
|
|
|
|
;
|
2013-04-18 14:40:43 +02:00
|
|
|
else if ( (opts[i].flags & ARGPARSE_TYPE_MASK) )
|
2008-11-12 18:41:49 +01:00
|
|
|
{
|
|
|
|
if ( argpos )
|
|
|
|
{
|
|
|
|
s2 = argpos+1;
|
|
|
|
if ( !*s2 )
|
|
|
|
s2 = NULL;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2008-11-12 18:41:49 +01:00
|
|
|
else
|
|
|
|
s2 = argv[1];
|
|
|
|
if ( !s2 && (opts[i].flags & ARGPARSE_OPT_OPTIONAL) )
|
|
|
|
{
|
|
|
|
arg->r_type = ARGPARSE_TYPE_NONE; /* Argument is optional. */
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2008-11-12 18:41:49 +01:00
|
|
|
else if ( !s2 )
|
|
|
|
{
|
|
|
|
arg->r_opt = ARGPARSE_MISSING_ARG;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2011-02-03 16:50:01 +01:00
|
|
|
else if ( !argpos && *s2 == '-'
|
|
|
|
&& (opts[i].flags & ARGPARSE_OPT_OPTIONAL) )
|
2008-11-12 18:41:49 +01:00
|
|
|
{
|
|
|
|
/* The argument is optional and the next seems to be an
|
|
|
|
option. We do not check this possible option but
|
|
|
|
assume no argument */
|
|
|
|
arg->r_type = ARGPARSE_TYPE_NONE;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2011-02-03 16:50:01 +01:00
|
|
|
else
|
2008-11-12 18:41:49 +01:00
|
|
|
{
|
|
|
|
set_opt_arg (arg, opts[i].flags, s2);
|
2011-02-03 16:50:01 +01:00
|
|
|
if ( !argpos )
|
2008-11-12 18:41:49 +01:00
|
|
|
{
|
|
|
|
argc--; argv++; idx++; /* Skip one. */
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-11-12 18:41:49 +01:00
|
|
|
else
|
2011-02-03 16:50:01 +01:00
|
|
|
{
|
2008-11-12 18:41:49 +01:00
|
|
|
/* Does not take an argument. */
|
|
|
|
if ( argpos )
|
2011-02-03 16:50:01 +01:00
|
|
|
arg->r_type = ARGPARSE_UNEXPECTED_ARG;
|
2008-11-12 18:41:49 +01:00
|
|
|
else
|
|
|
|
arg->r_type = 0;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2008-11-12 18:41:49 +01:00
|
|
|
argc--; argv++; idx++; /* Set to next one. */
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2011-02-03 16:50:01 +01:00
|
|
|
else if ( (*s == '-' && s[1]) || arg->internal.inarg )
|
2008-11-12 18:41:49 +01:00
|
|
|
{
|
|
|
|
/* Short option. */
|
2003-01-09 13:36:05 +01:00
|
|
|
int dash_kludge = 0;
|
2008-11-12 18:41:49 +01:00
|
|
|
|
2003-01-09 13:36:05 +01:00
|
|
|
i = 0;
|
2011-02-03 16:50:01 +01:00
|
|
|
if ( !arg->internal.inarg )
|
2008-11-12 18:41:49 +01:00
|
|
|
{
|
2003-01-09 13:36:05 +01:00
|
|
|
arg->internal.inarg++;
|
2008-11-12 18:41:49 +01:00
|
|
|
if ( (arg->flags & ARGPARSE_FLAG_ONEDASH) )
|
|
|
|
{
|
|
|
|
for (i=0; opts[i].short_opt; i++ )
|
|
|
|
if ( opts[i].long_opt && !strcmp (opts[i].long_opt, s+1))
|
|
|
|
{
|
|
|
|
dash_kludge = 1;
|
|
|
|
break;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2008-11-12 18:41:49 +01:00
|
|
|
}
|
|
|
|
}
|
2003-01-09 13:36:05 +01:00
|
|
|
s += arg->internal.inarg;
|
|
|
|
|
2008-11-12 18:41:49 +01:00
|
|
|
if (!dash_kludge )
|
|
|
|
{
|
|
|
|
for (i=0; opts[i].short_opt; i++ )
|
|
|
|
if ( opts[i].short_opt == *s )
|
|
|
|
break;
|
|
|
|
}
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2008-11-12 18:41:49 +01:00
|
|
|
if ( !opts[i].short_opt && ( *s == 'h' || *s == '?' ) )
|
|
|
|
show_help (opts, arg->flags);
|
2003-01-09 13:36:05 +01:00
|
|
|
|
|
|
|
arg->r_opt = opts[i].short_opt;
|
2008-11-12 18:41:49 +01:00
|
|
|
if (!opts[i].short_opt )
|
|
|
|
{
|
|
|
|
arg->r_opt = (opts[i].flags & ARGPARSE_OPT_COMMAND)?
|
2008-09-29 17:02:55 +02:00
|
|
|
ARGPARSE_INVALID_COMMAND:ARGPARSE_INVALID_OPTION;
|
2008-11-12 18:41:49 +01:00
|
|
|
arg->internal.inarg++; /* Point to the next arg. */
|
2003-01-09 13:36:05 +01:00
|
|
|
arg->r.ret_str = s;
|
2008-11-12 18:41:49 +01:00
|
|
|
}
|
2013-04-18 14:40:43 +02:00
|
|
|
else if ( (opts[i].flags & ARGPARSE_TYPE_MASK) )
|
2008-11-12 18:41:49 +01:00
|
|
|
{
|
|
|
|
if ( s[1] && !dash_kludge )
|
|
|
|
{
|
2003-01-09 13:36:05 +01:00
|
|
|
s2 = s+1;
|
2008-11-12 18:41:49 +01:00
|
|
|
set_opt_arg (arg, opts[i].flags, s2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-01-09 13:36:05 +01:00
|
|
|
s2 = argv[1];
|
2008-11-12 18:41:49 +01:00
|
|
|
if ( !s2 && (opts[i].flags & ARGPARSE_OPT_OPTIONAL) )
|
|
|
|
{
|
|
|
|
arg->r_type = ARGPARSE_TYPE_NONE;
|
|
|
|
}
|
|
|
|
else if ( !s2 )
|
|
|
|
{
|
2008-09-29 17:02:55 +02:00
|
|
|
arg->r_opt = ARGPARSE_MISSING_ARG;
|
2008-11-12 18:41:49 +01:00
|
|
|
}
|
|
|
|
else if ( *s2 == '-' && s2[1]
|
|
|
|
&& (opts[i].flags & ARGPARSE_OPT_OPTIONAL) )
|
|
|
|
{
|
|
|
|
/* The argument is optional and the next seems to
|
|
|
|
be an option. We do not check this possible
|
|
|
|
option but assume no argument. */
|
|
|
|
arg->r_type = ARGPARSE_TYPE_NONE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
set_opt_arg (arg, opts[i].flags, s2);
|
|
|
|
argc--; argv++; idx++; /* Skip one. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s = "x"; /* This is so that !s[1] yields false. */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Does not take an argument. */
|
|
|
|
arg->r_type = ARGPARSE_TYPE_NONE;
|
|
|
|
arg->internal.inarg++; /* Point to the next arg. */
|
|
|
|
}
|
|
|
|
if ( !s[1] || dash_kludge )
|
|
|
|
{
|
|
|
|
/* No more concatenated short options. */
|
2003-01-09 13:36:05 +01:00
|
|
|
arg->internal.inarg = 0;
|
|
|
|
argc--; argv++; idx++;
|
2008-11-12 18:41:49 +01:00
|
|
|
}
|
|
|
|
}
|
2008-11-20 14:08:26 +01:00
|
|
|
else if ( arg->flags & ARGPARSE_FLAG_MIXED )
|
2008-11-12 18:41:49 +01:00
|
|
|
{
|
|
|
|
arg->r_opt = ARGPARSE_IS_ARG;
|
|
|
|
arg->r_type = 2;
|
|
|
|
arg->r.ret_str = s;
|
|
|
|
argc--; argv++; idx++; /* Set to next one. */
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2008-11-12 18:41:49 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
arg->internal.stopped = 1; /* Stop option processing. */
|
|
|
|
goto next_one;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
|
|
|
|
2008-11-12 18:41:49 +01:00
|
|
|
leave:
|
|
|
|
*arg->argc = argc;
|
|
|
|
*arg->argv = argv;
|
|
|
|
arg->internal.idx = idx;
|
|
|
|
return arg->r_opt;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-29 17:07:51 +01:00
|
|
|
/* Returns: -1 on error, 0 for an integer type and 1 for a non integer
|
|
|
|
type argument. */
|
2003-01-09 13:36:05 +01:00
|
|
|
static int
|
2014-10-29 17:07:51 +01:00
|
|
|
set_opt_arg (ARGPARSE_ARGS *arg, unsigned flags, char *s)
|
2003-01-09 13:36:05 +01:00
|
|
|
{
|
2013-04-18 14:40:43 +02:00
|
|
|
int base = (flags & ARGPARSE_OPT_PREFIX)? 0 : 10;
|
2014-10-29 17:07:51 +01:00
|
|
|
long l;
|
2003-01-09 13:36:05 +01:00
|
|
|
|
2013-04-18 14:40:43 +02:00
|
|
|
switch ( (arg->r_type = (flags & ARGPARSE_TYPE_MASK)) )
|
2008-11-12 18:41:49 +01:00
|
|
|
{
|
|
|
|
case ARGPARSE_TYPE_LONG:
|
2014-10-29 17:07:51 +01:00
|
|
|
case ARGPARSE_TYPE_INT:
|
|
|
|
errno = 0;
|
|
|
|
l = strtol (s, NULL, base);
|
|
|
|
if ((l == LONG_MIN || l == LONG_MAX) && errno == ERANGE)
|
|
|
|
{
|
|
|
|
arg->r_opt = ARGPARSE_INVALID_ARG;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (arg->r_type == ARGPARSE_TYPE_LONG)
|
|
|
|
arg->r.ret_long = l;
|
|
|
|
else if ( (l < 0 && l < INT_MIN) || l > INT_MAX )
|
|
|
|
{
|
|
|
|
arg->r_opt = ARGPARSE_INVALID_ARG;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
arg->r.ret_int = (int)l;
|
2008-11-12 18:41:49 +01:00
|
|
|
return 0;
|
2014-10-29 17:07:51 +01:00
|
|
|
|
2008-11-12 18:41:49 +01:00
|
|
|
case ARGPARSE_TYPE_ULONG:
|
2014-10-29 17:07:51 +01:00
|
|
|
while (isascii (*s) && isspace(*s))
|
|
|
|
s++;
|
|
|
|
if (*s == '-')
|
|
|
|
{
|
|
|
|
arg->r.ret_ulong = 0;
|
|
|
|
arg->r_opt = ARGPARSE_INVALID_ARG;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
errno = 0;
|
|
|
|
arg->r.ret_ulong = strtoul (s, NULL, base);
|
|
|
|
if (arg->r.ret_ulong == ULONG_MAX && errno == ERANGE)
|
|
|
|
{
|
|
|
|
arg->r_opt = ARGPARSE_INVALID_ARG;
|
|
|
|
return -1;
|
|
|
|
}
|
2008-11-12 18:41:49 +01:00
|
|
|
return 0;
|
2014-10-29 17:07:51 +01:00
|
|
|
|
2008-11-12 18:41:49 +01:00
|
|
|
case ARGPARSE_TYPE_STRING:
|
|
|
|
default:
|
|
|
|
arg->r.ret_str = s;
|
|
|
|
return 1;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
long_opt_strlen( ARGPARSE_OPTS *o )
|
|
|
|
{
|
2007-05-04 11:22:18 +02:00
|
|
|
size_t n = strlen (o->long_opt);
|
2003-01-09 13:36:05 +01:00
|
|
|
|
2011-02-03 16:50:01 +01:00
|
|
|
if ( o->description && *o->description == '|' )
|
2007-05-04 11:22:18 +02:00
|
|
|
{
|
|
|
|
const char *s;
|
|
|
|
int is_utf8 = is_native_utf8 ();
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2007-05-04 11:22:18 +02:00
|
|
|
s=o->description+1;
|
|
|
|
if ( *s != '=' )
|
|
|
|
n++;
|
|
|
|
/* For a (mostly) correct length calculation we exclude
|
|
|
|
continuation bytes (10xxxxxx) if we are on a native utf8
|
|
|
|
terminal. */
|
|
|
|
for (; *s && *s != '|'; s++ )
|
|
|
|
if ( is_utf8 && (*s&0xc0) != 0x80 )
|
|
|
|
n++;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2007-05-04 11:22:18 +02:00
|
|
|
return n;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
|
|
|
|
2008-11-12 18:41:49 +01:00
|
|
|
|
2003-01-09 13:36:05 +01:00
|
|
|
/****************
|
|
|
|
* Print formatted help. The description string has some special
|
|
|
|
* meanings:
|
|
|
|
* - A description string which is "@" suppresses help output for
|
|
|
|
* this option
|
|
|
|
* - a description,ine which starts with a '@' and is followed by
|
|
|
|
* any other characters is printed as is; this may be used for examples
|
|
|
|
* ans such.
|
|
|
|
* - A description which starts with a '|' outputs the string between this
|
|
|
|
* bar and the next one as arguments of the long option.
|
|
|
|
*/
|
|
|
|
static void
|
2008-11-12 18:41:49 +01:00
|
|
|
show_help (ARGPARSE_OPTS *opts, unsigned int flags)
|
2003-01-09 13:36:05 +01:00
|
|
|
{
|
2008-11-12 18:41:49 +01:00
|
|
|
const char *s;
|
2010-05-30 14:06:38 +02:00
|
|
|
char tmp[2];
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2008-11-12 18:41:49 +01:00
|
|
|
show_version ();
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, "\n", NULL);
|
2015-03-17 11:22:28 +01:00
|
|
|
s = strusage (42);
|
|
|
|
if (s && *s == '1')
|
|
|
|
{
|
|
|
|
s = strusage (40);
|
|
|
|
writestrings (1, s, NULL);
|
|
|
|
if (*s && s[strlen(s)] != '\n')
|
|
|
|
writestrings (1, "\n", NULL);
|
|
|
|
}
|
2008-11-12 18:41:49 +01:00
|
|
|
s = strusage(41);
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, s, "\n", NULL);
|
2008-11-12 18:41:49 +01:00
|
|
|
if ( opts[0].description )
|
|
|
|
{
|
|
|
|
/* Auto format the option description. */
|
|
|
|
int i,j, indent;
|
2003-01-09 13:36:05 +01:00
|
|
|
|
2008-11-12 18:41:49 +01:00
|
|
|
/* Get max. length of long options. */
|
|
|
|
for (i=indent=0; opts[i].short_opt; i++ )
|
|
|
|
{
|
|
|
|
if ( opts[i].long_opt )
|
|
|
|
if ( !opts[i].description || *opts[i].description != '@' )
|
|
|
|
if ( (j=long_opt_strlen(opts+i)) > indent && j < 35 )
|
|
|
|
indent = j;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2008-11-12 18:41:49 +01:00
|
|
|
|
|
|
|
/* Example: " -v, --verbose Viele Sachen ausgeben" */
|
|
|
|
indent += 10;
|
|
|
|
if ( *opts[0].description != '@' )
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, "Options:", "\n", NULL);
|
2008-11-12 18:41:49 +01:00
|
|
|
for (i=0; opts[i].short_opt; i++ )
|
|
|
|
{
|
2014-05-07 18:18:27 +02:00
|
|
|
s = map_static_macro_string (_( opts[i].description ));
|
2008-11-12 18:41:49 +01:00
|
|
|
if ( s && *s== '@' && !s[1] ) /* Hide this line. */
|
|
|
|
continue;
|
|
|
|
if ( s && *s == '@' ) /* Unindented comment only line. */
|
2011-02-03 16:50:01 +01:00
|
|
|
{
|
|
|
|
for (s++; *s; s++ )
|
2008-11-12 18:41:49 +01:00
|
|
|
{
|
|
|
|
if ( *s == '\n' )
|
|
|
|
{
|
|
|
|
if( s[1] )
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, "\n", NULL);
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2008-11-12 18:41:49 +01:00
|
|
|
else
|
2010-05-30 14:06:38 +02:00
|
|
|
{
|
|
|
|
tmp[0] = *s;
|
|
|
|
tmp[1] = 0;
|
|
|
|
writestrings (0, tmp, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writestrings (0, "\n", NULL);
|
2008-11-12 18:41:49 +01:00
|
|
|
continue;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
|
|
|
|
2008-11-12 18:41:49 +01:00
|
|
|
j = 3;
|
|
|
|
if ( opts[i].short_opt < 256 )
|
|
|
|
{
|
2010-05-30 14:06:38 +02:00
|
|
|
tmp[0] = opts[i].short_opt;
|
|
|
|
tmp[1] = 0;
|
|
|
|
writestrings (0, " -", tmp, NULL );
|
2011-02-03 16:50:01 +01:00
|
|
|
if ( !opts[i].long_opt )
|
2008-11-12 18:41:49 +01:00
|
|
|
{
|
2011-02-03 16:50:01 +01:00
|
|
|
if (s && *s == '|' )
|
2008-11-12 18:41:49 +01:00
|
|
|
{
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, " ", NULL); j++;
|
2008-11-12 18:41:49 +01:00
|
|
|
for (s++ ; *s && *s != '|'; s++, j++ )
|
2010-05-30 14:06:38 +02:00
|
|
|
{
|
|
|
|
tmp[0] = *s;
|
|
|
|
tmp[1] = 0;
|
|
|
|
writestrings (0, tmp, NULL);
|
|
|
|
}
|
2008-11-12 18:41:49 +01:00
|
|
|
if ( *s )
|
|
|
|
s++;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-11-12 18:41:49 +01:00
|
|
|
else
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, " ", NULL);
|
2011-02-03 16:50:01 +01:00
|
|
|
if ( opts[i].long_opt )
|
2008-11-12 18:41:49 +01:00
|
|
|
{
|
2010-05-30 14:06:38 +02:00
|
|
|
tmp[0] = opts[i].short_opt < 256?',':' ';
|
|
|
|
tmp[1] = 0;
|
|
|
|
j += writestrings (0, tmp, " --", opts[i].long_opt, NULL);
|
2011-02-03 16:50:01 +01:00
|
|
|
if (s && *s == '|' )
|
2008-11-12 18:41:49 +01:00
|
|
|
{
|
|
|
|
if ( *++s != '=' )
|
|
|
|
{
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, " ", NULL);
|
2008-11-12 18:41:49 +01:00
|
|
|
j++;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2008-11-12 18:41:49 +01:00
|
|
|
for ( ; *s && *s != '|'; s++, j++ )
|
2010-05-30 14:06:38 +02:00
|
|
|
{
|
|
|
|
tmp[0] = *s;
|
|
|
|
tmp[1] = 0;
|
|
|
|
writestrings (0, tmp, NULL);
|
|
|
|
}
|
2008-11-12 18:41:49 +01:00
|
|
|
if ( *s )
|
|
|
|
s++;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, " ", NULL);
|
2008-11-12 18:41:49 +01:00
|
|
|
j += 3;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2008-11-12 18:41:49 +01:00
|
|
|
for (;j < indent; j++ )
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, " ", NULL);
|
2008-11-12 18:41:49 +01:00
|
|
|
if ( s )
|
|
|
|
{
|
|
|
|
if ( *s && j > indent )
|
|
|
|
{
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, "\n", NULL);
|
2008-11-12 18:41:49 +01:00
|
|
|
for (j=0;j < indent; j++ )
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, " ", NULL);
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2008-11-12 18:41:49 +01:00
|
|
|
for (; *s; s++ )
|
|
|
|
{
|
|
|
|
if ( *s == '\n' )
|
|
|
|
{
|
2011-02-03 16:50:01 +01:00
|
|
|
if ( s[1] )
|
2008-11-12 18:41:49 +01:00
|
|
|
{
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, "\n", NULL);
|
2008-11-12 18:41:49 +01:00
|
|
|
for (j=0; j < indent; j++ )
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, " ", NULL);
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
|
|
|
}
|
2008-11-12 18:41:49 +01:00
|
|
|
else
|
2010-05-30 14:06:38 +02:00
|
|
|
{
|
|
|
|
tmp[0] = *s;
|
|
|
|
tmp[1] = 0;
|
|
|
|
writestrings (0, tmp, NULL);
|
|
|
|
}
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
|
|
|
}
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, "\n", NULL);
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2008-11-12 18:41:49 +01:00
|
|
|
if ( (flags & ARGPARSE_FLAG_ONEDASH) )
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, "\n(A single dash may be used "
|
|
|
|
"instead of the double ones)\n", NULL);
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2008-11-12 18:41:49 +01:00
|
|
|
if ( (s=strusage(19)) )
|
2011-02-03 16:50:01 +01:00
|
|
|
{
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, "\n", NULL);
|
2013-11-18 13:46:52 +01:00
|
|
|
writestrings (0, s, NULL);
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2010-05-30 14:06:38 +02:00
|
|
|
flushstrings (0);
|
2008-11-12 18:41:49 +01:00
|
|
|
exit(0);
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-11-12 18:41:49 +01:00
|
|
|
show_version ()
|
2003-01-09 13:36:05 +01:00
|
|
|
{
|
2007-07-04 11:34:28 +02:00
|
|
|
const char *s;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Version line. */
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, strusage (11), NULL);
|
2007-07-04 11:34:28 +02:00
|
|
|
if ((s=strusage (12)))
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, " (", s, ")", NULL);
|
|
|
|
writestrings (0, " ", strusage (13), "\n", NULL);
|
2007-07-04 11:34:28 +02:00
|
|
|
/* Additional version lines. */
|
|
|
|
for (i=20; i < 30; i++)
|
|
|
|
if ((s=strusage (i)))
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, s, "\n", NULL);
|
2007-07-04 11:34:28 +02:00
|
|
|
/* Copyright string. */
|
2010-05-30 14:06:38 +02:00
|
|
|
if ((s=strusage (14)))
|
|
|
|
writestrings (0, s, "\n", NULL);
|
2007-07-04 11:34:28 +02:00
|
|
|
/* Licence string. */
|
|
|
|
if( (s=strusage (10)) )
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, s, "\n", NULL);
|
2007-07-04 11:34:28 +02:00
|
|
|
/* Copying conditions. */
|
|
|
|
if ( (s=strusage(15)) )
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, s, NULL);
|
2007-07-04 11:34:28 +02:00
|
|
|
/* Thanks. */
|
|
|
|
if ((s=strusage(18)))
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, s, NULL);
|
2007-07-04 11:34:28 +02:00
|
|
|
/* Additional program info. */
|
|
|
|
for (i=30; i < 40; i++ )
|
|
|
|
if ( (s=strusage (i)) )
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, s, NULL);
|
|
|
|
flushstrings (0);
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2007-07-04 11:34:28 +02:00
|
|
|
usage (int level)
|
2003-01-09 13:36:05 +01:00
|
|
|
{
|
2008-05-27 14:03:50 +02:00
|
|
|
const char *p;
|
|
|
|
|
2007-07-04 11:34:28 +02:00
|
|
|
if (!level)
|
|
|
|
{
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (1, strusage(11), " ", strusage(13), "; ",
|
|
|
|
strusage (14), "\n", NULL);
|
|
|
|
flushstrings (1);
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2007-07-04 11:34:28 +02:00
|
|
|
else if (level == 1)
|
|
|
|
{
|
2008-05-27 14:03:50 +02:00
|
|
|
p = strusage (40);
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (1, p, NULL);
|
2008-05-27 14:03:50 +02:00
|
|
|
if (*p && p[strlen(p)] != '\n')
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (1, "\n", NULL);
|
2007-07-04 11:34:28 +02:00
|
|
|
exit (2);
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2011-02-03 16:50:01 +01:00
|
|
|
else if (level == 2)
|
2007-07-04 11:34:28 +02:00
|
|
|
{
|
2015-03-17 11:22:28 +01:00
|
|
|
p = strusage (42);
|
|
|
|
if (p && *p == '1')
|
|
|
|
{
|
|
|
|
p = strusage (40);
|
|
|
|
writestrings (1, p, NULL);
|
|
|
|
if (*p && p[strlen(p)] != '\n')
|
|
|
|
writestrings (1, "\n", NULL);
|
|
|
|
}
|
2010-05-30 14:06:38 +02:00
|
|
|
writestrings (0, strusage(41), "\n", NULL);
|
2007-07-04 11:34:28 +02:00
|
|
|
exit (0);
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Level
|
2007-07-04 11:34:28 +02:00
|
|
|
* 0: Print copyright string to stderr
|
|
|
|
* 1: Print a short usage hint to stderr and terminate
|
|
|
|
* 2: Print a long usage hint to stdout and terminate
|
|
|
|
* 10: Return license info string
|
|
|
|
* 11: Return the name of the program
|
|
|
|
* 12: Return optional name of package which includes this program.
|
2003-01-09 13:36:05 +01:00
|
|
|
* 13: version string
|
|
|
|
* 14: copyright string
|
|
|
|
* 15: Short copying conditions (with LFs)
|
|
|
|
* 16: Long copying conditions (with LFs)
|
|
|
|
* 17: Optional printable OS name
|
2007-07-04 11:34:28 +02:00
|
|
|
* 18: Optional thanks list (with LFs)
|
2003-01-09 13:36:05 +01:00
|
|
|
* 19: Bug report info
|
|
|
|
*20..29: Additional lib version strings.
|
|
|
|
*30..39: Additional program info (with LFs)
|
|
|
|
* 40: short usage note (with LF)
|
|
|
|
* 41: long usage note (with LF)
|
2015-03-17 11:22:28 +01:00
|
|
|
* 42: Flag string:
|
|
|
|
* First char is '1':
|
|
|
|
* The short usage notes needs to be printed
|
|
|
|
* before the long usage note.
|
2003-01-09 13:36:05 +01:00
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
strusage( int level )
|
|
|
|
{
|
2008-11-12 18:41:49 +01:00
|
|
|
const char *p = strusage_handler? strusage_handler(level) : NULL;
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2008-11-12 18:41:49 +01:00
|
|
|
if ( p )
|
2014-02-26 16:50:59 +01:00
|
|
|
return map_static_macro_string (p);
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2008-11-12 18:41:49 +01:00
|
|
|
switch ( level )
|
|
|
|
{
|
2015-03-17 09:19:55 +01:00
|
|
|
|
|
|
|
case 10:
|
|
|
|
#if ARGPARSE_GPL_VERSION == 3
|
|
|
|
p = ("License GPLv3+: GNU GPL version 3 or later "
|
2016-05-03 15:55:08 +02:00
|
|
|
"<https://gnu.org/licenses/gpl.html>");
|
2015-03-17 09:19:55 +01:00
|
|
|
#else
|
|
|
|
p = ("License GPLv2+: GNU GPL version 2 or later "
|
2016-05-03 15:55:08 +02:00
|
|
|
"<https://gnu.org/licenses/>");
|
2015-03-17 09:19:55 +01:00
|
|
|
#endif
|
2008-11-12 18:41:49 +01:00
|
|
|
break;
|
|
|
|
case 11: p = "foo"; break;
|
|
|
|
case 13: p = "0.0"; break;
|
2015-03-17 09:19:55 +01:00
|
|
|
case 14: p = ARGPARSE_CRIGHT_STR; break;
|
2008-11-12 18:41:49 +01:00
|
|
|
case 15: p =
|
2007-07-04 11:34:28 +02:00
|
|
|
"This is free software: you are free to change and redistribute it.\n"
|
|
|
|
"There is NO WARRANTY, to the extent permitted by law.\n";
|
2008-11-12 18:41:49 +01:00
|
|
|
break;
|
|
|
|
case 16: p =
|
2003-01-09 13:36:05 +01:00
|
|
|
"This is free software; you can redistribute it and/or modify\n"
|
|
|
|
"it under the terms of the GNU General Public License as published by\n"
|
2015-03-17 09:19:55 +01:00
|
|
|
"the Free Software Foundation; either version "
|
|
|
|
ARGPARSE_STR2(ARGPARSE_GPL_VERSION)
|
|
|
|
" of the License, or\n"
|
2003-01-09 13:36:05 +01:00
|
|
|
"(at your option) any later version.\n\n"
|
|
|
|
"It is distributed in the hope that it will be useful,\n"
|
|
|
|
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
|
|
|
|
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
|
|
|
|
"GNU General Public License for more details.\n\n"
|
|
|
|
"You should have received a copy of the GNU General Public License\n"
|
2016-05-03 15:55:08 +02:00
|
|
|
"along with this software. If not, see <https://gnu.org/licenses/>.\n";
|
2008-11-12 18:41:49 +01:00
|
|
|
break;
|
|
|
|
case 40: /* short and long usage */
|
|
|
|
case 41: p = ""; break;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2008-11-12 18:41:49 +01:00
|
|
|
return p;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
|
|
|
|
2010-03-22 13:46:05 +01:00
|
|
|
|
|
|
|
/* Set the usage handler. This function is basically a constructor. */
|
2003-01-09 13:36:05 +01:00
|
|
|
void
|
2008-11-12 18:41:49 +01:00
|
|
|
set_strusage ( const char *(*f)( int ) )
|
2003-01-09 13:36:05 +01:00
|
|
|
{
|
2008-11-12 18:41:49 +01:00
|
|
|
strusage_handler = f;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef TEST
|
|
|
|
static struct {
|
|
|
|
int verbose;
|
|
|
|
int debug;
|
|
|
|
char *outfile;
|
|
|
|
char *crf;
|
|
|
|
int myopt;
|
|
|
|
int echo;
|
|
|
|
int a_long_one;
|
2015-03-17 09:19:55 +01:00
|
|
|
} opt;
|
2003-01-09 13:36:05 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2008-11-12 18:41:49 +01:00
|
|
|
ARGPARSE_OPTS opts[] = {
|
|
|
|
ARGPARSE_x('v', "verbose", NONE, 0, "Laut sein"),
|
|
|
|
ARGPARSE_s_n('e', "echo" , ("Zeile ausgeben, damit wir sehen, "
|
2015-03-17 09:19:55 +01:00
|
|
|
"was wir eingegeben haben")),
|
2008-11-12 18:41:49 +01:00
|
|
|
ARGPARSE_s_n('d', "debug", "Debug\nfalls mal etwas\nschief geht"),
|
|
|
|
ARGPARSE_s_s('o', "output", 0 ),
|
|
|
|
ARGPARSE_o_s('c', "cross-ref", "cross-reference erzeugen\n" ),
|
2007-05-04 11:22:18 +02:00
|
|
|
/* Note that on a non-utf8 terminal the ß might garble the output. */
|
2008-11-12 18:41:49 +01:00
|
|
|
ARGPARSE_s_n('s', "street","|Straße|set the name of the street to Straße"),
|
|
|
|
ARGPARSE_o_i('m', "my-option", 0),
|
|
|
|
ARGPARSE_s_n(500, "a-long-option", 0 ),
|
2015-03-17 09:19:55 +01:00
|
|
|
ARGPARSE_end()
|
2008-11-12 18:41:49 +01:00
|
|
|
};
|
2015-03-17 09:19:55 +01:00
|
|
|
ARGPARSE_ARGS pargs = { &argc, &argv, (ARGPARSE_FLAG_ALL
|
|
|
|
| ARGPARSE_FLAG_MIXED
|
|
|
|
| ARGPARSE_FLAG_ONEDASH) };
|
|
|
|
int i;
|
2003-01-09 13:36:05 +01:00
|
|
|
|
2015-03-17 09:19:55 +01:00
|
|
|
while (arg_parse (&pargs, opts))
|
|
|
|
{
|
|
|
|
switch (pargs.r_opt)
|
|
|
|
{
|
|
|
|
case ARGPARSE_IS_ARG :
|
|
|
|
printf ("arg='%s'\n", pargs.r.ret_str);
|
|
|
|
break;
|
|
|
|
case 'v': opt.verbose++; break;
|
|
|
|
case 'e': opt.echo++; break;
|
|
|
|
case 'd': opt.debug++; break;
|
|
|
|
case 'o': opt.outfile = pargs.r.ret_str; break;
|
|
|
|
case 'c': opt.crf = pargs.r_type? pargs.r.ret_str:"a.crf"; break;
|
|
|
|
case 'm': opt.myopt = pargs.r_type? pargs.r.ret_int : 1; break;
|
|
|
|
case 500: opt.a_long_one++; break;
|
|
|
|
default : pargs.err = ARGPARSE_PRINT_WARNING; break;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
|
|
|
}
|
2015-03-17 09:19:55 +01:00
|
|
|
for (i=0; i < argc; i++ )
|
|
|
|
printf ("%3d -> (%s)\n", i, argv[i] );
|
|
|
|
puts ("Options:");
|
|
|
|
if (opt.verbose)
|
|
|
|
printf (" verbose=%d\n", opt.verbose );
|
|
|
|
if (opt.debug)
|
|
|
|
printf (" debug=%d\n", opt.debug );
|
|
|
|
if (opt.outfile)
|
|
|
|
printf (" outfile='%s'\n", opt.outfile );
|
|
|
|
if (opt.crf)
|
|
|
|
printf (" crffile='%s'\n", opt.crf );
|
|
|
|
if (opt.myopt)
|
|
|
|
printf (" myopt=%d\n", opt.myopt );
|
|
|
|
if (opt.a_long_one)
|
|
|
|
printf (" a-long-one=%d\n", opt.a_long_one );
|
|
|
|
if (opt.echo)
|
|
|
|
printf (" echo=%d\n", opt.echo );
|
|
|
|
|
|
|
|
return 0;
|
2003-01-09 13:36:05 +01:00
|
|
|
}
|
2015-03-17 09:19:55 +01:00
|
|
|
#endif /*TEST*/
|
2003-01-09 13:36:05 +01:00
|
|
|
|
|
|
|
/**** bottom of file ****/
|