aboutsummaryrefslogtreecommitdiffstats
path: root/jnlib/argparse.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--jnlib/argparse.c (renamed from util/argparse.c)149
1 files changed, 73 insertions, 76 deletions
diff --git a/util/argparse.c b/jnlib/argparse.c
index cdc56bf3a..15a7c546e 100644
--- a/util/argparse.c
+++ b/jnlib/argparse.c
@@ -1,25 +1,22 @@
/* [argparse.c wk 17.06.97] Argument Parser for option handling
- * Copyright (C) 1998, 1999, 2000, 2001, 2003,
- * 2004 Free Software Foundation, Inc.
+ * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
*
- * This file is part of GnuPG.
+ * This file is part of GnuPG.
*
- * GnuPG is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * GnuPG is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
*
- * GnuPG is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
+ * GnuPG is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
*
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
- *
- *
- * Note: This is an independent version of the one in WkLib
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+ * USA.
*/
#include <config.h>
@@ -28,8 +25,11 @@
#include <ctype.h>
#include <string.h>
-#include "util.h"
-#include "i18n.h"
+#include "libjnlib-config.h"
+#include "mischelp.h"
+#include "stringhelp.h"
+#include "logging.h"
+#include "argparse.h"
/*********************************
@@ -75,7 +75,7 @@
* 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 and --help
+ * 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):
@@ -137,10 +137,13 @@ struct alias_def_s {
const char *value; /* ptr into name */
};
+static const char *(*strusage_handler)( int ) = NULL;
+
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);
+
static void
initialize( ARGPARSE_ARGS *arg, const char *filename, unsigned *lineno )
{
@@ -154,49 +157,48 @@ initialize( ARGPARSE_ARGS *arg, const char *filename, unsigned *lineno )
arg->err = 0;
arg->flags |= 1<<15; /* mark initialized */
if( *arg->argc < 0 )
- log_bug("Invalid argument for ArgParse\n");
+ jnlib_log_bug("Invalid argument for ArgParse\n");
}
if( arg->err ) { /* last option was erroneous */
+ const char *s;
if( filename ) {
if( arg->r_opt == -6 )
- log_error("%s:%u: argument not expected\n", filename, *lineno );
+ s = "argument not expected\n";
else if( arg->r_opt == -5 )
- log_error("%s:%u: read error\n", filename, *lineno );
+ s = "read error\n";
else if( arg->r_opt == -4 )
- log_error("%s:%u: keyword too long\n", filename, *lineno );
+ s = "keyword too long\n";
else if( arg->r_opt == -3 )
- log_error("%s:%u: missing argument\n", filename, *lineno );
+ s = "missing argument\n";
else if( arg->r_opt == -7 )
- log_error("%s:%u: invalid command\n", filename, *lineno );
+ s = "invalid command\n";
else if( arg->r_opt == -10 )
- log_error("%s:%u: invalid alias definition\n",filename,*lineno);
+ s = "invalid alias definition\n";
else
- log_error("%s:%u: invalid option\n", filename, *lineno );
+ s = "invalid option\n";
+ jnlib_log_error("%s:%u: %s\n", filename, *lineno, s);
}
else {
+ s = arg->internal.last? arg->internal.last:"[??]";
+
if( arg->r_opt == -3 )
- log_error("Missing argument for option \"%.50s\"\n",
- arg->internal.last? arg->internal.last:"[??]" );
+ jnlib_log_error ("Missing argument for option \"%.50s\"\n", s);
else if( arg->r_opt == -6 )
- log_error("Option \"%.50s\" does not expect an argument\n",
- arg->internal.last? arg->internal.last:"[??]" );
+ jnlib_log_error ("Option \"%.50s\" does not expect an argument\n",
+ s );
else if( arg->r_opt == -7 )
- log_error("Invalid command \"%.50s\"\n",
- arg->internal.last? arg->internal.last:"[??]" );
+ jnlib_log_error ("Invalid command \"%.50s\"\n", s);
else if( arg->r_opt == -8 )
- log_error("Option \"%.50s\" is ambiguous\n",
- arg->internal.last? arg->internal.last:"[??]" );
+ jnlib_log_error ("Option \"%.50s\" is ambiguous\n", s);
else if( arg->r_opt == -9 )
- log_error("Command \"%.50s\" is ambiguous\n",
- arg->internal.last? arg->internal.last:"[??]" );
+ jnlib_log_error ("Command \"%.50s\" is ambiguous\n",s );
else
- log_error("Invalid option \"%.50s\"\n",
- arg->internal.last? arg->internal.last:"[??]" );
+ jnlib_log_error ("Invalid option \"%.50s\"\n", s);
}
- if( arg->err != 1 || arg->r_opt == -5 )
+ if( arg->err != 1 )
exit(2);
arg->err = 0;
}
@@ -215,7 +217,7 @@ store_alias( ARGPARSE_ARGS *arg, char *name, char *value )
* used as lvalue
*/
#if 0
- ALIAS_DEF a = xmalloc( sizeof *a );
+ ALIAS_DEF a = jnlib_xmalloc( sizeof *a );
a->name = name;
a->value = value;
a->next = (ALIAS_DEF)arg->internal.aliases;
@@ -310,7 +312,7 @@ optfile_parse( FILE *fp, const char *filename, unsigned *lineno,
trim_spaces( p );
}
if( !p || !*p ) {
- xfree( buffer );
+ jnlib_free( buffer );
arg->r_opt = -10;
}
else {
@@ -324,29 +326,20 @@ optfile_parse( FILE *fp, const char *filename, unsigned *lineno,
char *p;
if( !buffer ) {
keyword[i] = 0;
- buffer = xstrdup(keyword);
+ buffer = jnlib_xstrdup(keyword);
}
else
buffer[i] = 0;
trim_spaces( buffer );
p = buffer;
- /* remove quotes if they totally enclose the
- string, and do not occur within the string */
- if( *p == '"' && p[strlen(p)-1]=='"') {
- char *p2=p;
-
- while(*(++p2))
- if(*p2=='"')
- break;
-
- if(*p2=='"' && *(p2+1)=='\0') {
- p[strlen(p)-1] = 0;
- p++;
- }
+ if( *p == '"' ) { /* remove quotes */
+ p++;
+ if( *p && p[strlen(p)-1] == '"' )
+ p[strlen(p)-1] = 0;
}
if( !set_opt_arg(arg, opts[idx].flags, p) )
- xfree(buffer);
+ jnlib_free(buffer);
}
break;
}
@@ -401,7 +394,7 @@ optfile_parse( FILE *fp, const char *filename, unsigned *lineno,
buffer[i++] = c;
else {
buflen += 50;
- buffer = xrealloc(buffer, buflen);
+ buffer = jnlib_xrealloc(buffer, buflen);
buffer[i++] = c;
}
}
@@ -409,7 +402,7 @@ optfile_parse( FILE *fp, const char *filename, unsigned *lineno,
keyword[i++] = c;
else {
buflen = DIM(keyword)+50;
- buffer = xmalloc(buflen);
+ buffer = jnlib_xmalloc(buflen);
memcpy(buffer, keyword, i);
buffer[i++] = c;
}
@@ -445,7 +438,7 @@ find_long_option( ARGPARSE_ARGS *arg,
for(i=0; opts[i].short_opt; i++ )
if( opts[i].long_opt && !strcmp( opts[i].long_opt, keyword) )
return i;
-#if 0
+ #if 0
{
ALIAS_DEF a;
/* see whether it is an alias */
@@ -457,7 +450,7 @@ find_long_option( ARGPARSE_ARGS *arg,
}
}
}
-#endif
+ #endif
/* not found, see whether it is an abbreviation */
/* aliases may not be abbreviated */
n = strlen( keyword );
@@ -529,11 +522,8 @@ arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts)
if( argpos )
*argpos = '=';
- if( i < 0 && !strcmp( "help", s+2) ) {
- if( !(arg->flags & (1<<6)) ) {
- show_help(opts, arg->flags);
- }
- }
+ if( i < 0 && !strcmp( "help", s+2) )
+ show_help(opts, arg->flags);
else if( i < 0 && !strcmp( "version", s+2) ) {
if( !(arg->flags & (1<<6)) ) {
show_version();
@@ -619,11 +609,8 @@ arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts)
break;
}
- if( !opts[i].short_opt && ( *s == 'h' || *s == '?' ) ) {
- if( !(arg->flags & (1<<6)) ) {
- show_help(opts, arg->flags);
- }
- }
+ if( !opts[i].short_opt && ( *s == 'h' || *s == '?' ) )
+ show_help(opts, arg->flags);
arg->r_opt = opts[i].short_opt;
if( !opts[i].short_opt ) {
@@ -866,7 +853,7 @@ show_version()
/* additional program info */
for(i=30; i < 40; i++ )
if( (s=strusage(i)) )
- fputs( (const byte*)s, stdout);
+ fputs (s, stdout);
fflush(stdout);
}
@@ -908,9 +895,13 @@ usage( int level )
* 41: long usage note (with LF)
*/
const char *
-default_strusage( int level )
+strusage( int level )
{
- const char *p = NULL;
+ const char *p = strusage_handler? strusage_handler(level) : NULL;
+
+ if( p )
+ return p;
+
switch( level ) {
case 11: p = "foo"; break;
case 13: p = "0.0"; break;
@@ -930,7 +921,8 @@ default_strusage( int level )
"GNU General Public License for more details.\n\n"
"You should have received a copy of the GNU General Public License\n"
"along with this program; if not, write to the Free Software\n"
-"Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n";
+"Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,\n"
+"USA.\n";
break;
case 40: /* short and long usage */
case 41: p = ""; break;
@@ -939,6 +931,11 @@ default_strusage( int level )
return p;
}
+void
+set_strusage( const char *(*f)( int ) )
+{
+ strusage_handler = f;
+}
#ifdef TEST