From a028f24136a062f55408a5fec84c6d31201b2143 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Fri, 4 Dec 2020 11:51:48 +0100 Subject: [PATCH] Backport of the new option parser from 2.3 * configure.ac (GPGRT_ENABLE_ARGPARSE_MACROS): Define. * common/argparse.c, common/argparse.h: Rewrite. * tests/gpgscm/main.c: Switch to the new option parser. * g10/gpg.c: Switch to the new option parser and enable a global conf file. * g10/gpgv.c: Ditto. * agent/gpg-agent.c: Ditto. * agent/preset-passphrase.c: Ditto. * agent/protect-tool.c: Ditto. * scd/scdaemon.c: Ditto. * dirmngr/dirmngr.c: Ditto. * dirmngr/dirmngr_ldap.c: Ditto * dirmngr/dirmngr-client.c: Ditto. * kbx/kbxutil.c: Ditto. * tools/gpg-card.c: Ditto. * tools/gpg-check-pattern.c: Ditto. * tools/gpg-connect-agent.c: Ditto. * tools/gpg-pair-tool.c: Ditto. * tools/gpg-wks-client.c: Ditto. * tools/gpg-wks-server.c: Ditto. * tools/gpgconf.c: Ditto. * tools/gpgsplit.c: Ditto. * tools/gpgtar.c: Ditto. * g13/g13.c: Ditto. * g13/g13-syshelp.c: Ditto. Do not force verbose mode. * sm/gpgsm.c: Ditto. Add option --no-options. -- This is backport from master commit cdbe10b762f38449b86da69076209324b0c99982 commit ba463128ce65a0f347643f7246a8e097c5be19f1 commit 3bc004decd289810bc1b6ad6fb8f47e45c770ce6 commit 2c823bd878fcdbcc4f6c34993e1d0539d9a6b237 commit 0e8f6e2aa98c212442001036fb5178cd6cd8af59 but without changing all functions names to gpgrt. Instead we use wrapper functions which, when building against old Libgpg-error versions, are implemented in argparse.c using code from the current libgpg-error. This allows to keep the dependency requirement at libgpg-error 1.27 to support older distributions. Tested builds against 1.27 and 1.40-beta. Note that g13-syshelp does not anymore default to --verbose because that can now be enabled in /etc/gnupg/g13-syshelp.conf. GnuPG-bug-id: 4788 Signed-off-by: Werner Koch --- agent/gpg-agent.c | 206 ++- agent/preset-passphrase.c | 8 +- agent/protect-tool.c | 10 +- common/argparse.c | 2525 ++++++++++++++++++++++++++----------- common/argparse.h | 114 +- common/init.c | 5 +- configure.ac | 19 + dirmngr/dirmngr-client.c | 14 +- dirmngr/dirmngr.c | 174 ++- dirmngr/dirmngr_ldap.c | 8 +- g10/gpg.c | 280 ++-- g10/gpgv.c | 17 +- g13/g13-syshelp.c | 113 +- g13/g13.c | 150 +-- kbx/kbxutil.c | 9 +- scd/scdaemon.c | 150 +-- sm/gpgsm.c | 168 +-- tests/gpgscm/main.c | 9 +- tools/gpg-check-pattern.c | 10 +- tools/gpg-connect-agent.c | 8 +- tools/gpg-wks-client.c | 8 +- tools/gpg-wks-server.c | 8 +- tools/gpgconf.c | 16 +- tools/gpgsplit.c | 10 +- tools/gpgtar.c | 14 +- 25 files changed, 2590 insertions(+), 1463 deletions(-) diff --git a/agent/gpg-agent.c b/agent/gpg-agent.c index 795f28206..b167c34ea 100644 --- a/agent/gpg-agent.c +++ b/agent/gpg-agent.c @@ -1,6 +1,7 @@ /* gpg-agent.c - The GnuPG Agent - * Copyright (C) 2000-2007, 2009-2010 Free Software Foundation, Inc. - * Copyright (C) 2000-2016 Werner Koch + * Copyright (C) 2000-2020 Free Software Foundation, Inc. + * Copyright (C) 2000-2019 Werner Koch + * Copyright (C) 2015-2020 g10 Code GmbH * * This file is part of GnuPG. * @@ -16,6 +17,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, see . + * SPDX-License-Identifier: GPL-3.0-or-later */ #include @@ -169,7 +171,7 @@ static ARGPARSE_OPTS opts[] = { ARGPARSE_s_n (oQuiet, "quiet", N_("be somewhat more quiet")), ARGPARSE_s_n (oSh, "sh", N_("sh-style command output")), ARGPARSE_s_n (oCsh, "csh", N_("csh-style command output")), - ARGPARSE_s_s (oOptions, "options", N_("|FILE|read options from FILE")), + ARGPARSE_conffile (oOptions, "options", N_("|FILE|read options from FILE")), ARGPARSE_s_s (oDebug, "debug", "@"), ARGPARSE_s_n (oDebugAll, "debug-all", "@"), @@ -264,6 +266,7 @@ static ARGPARSE_OPTS opts[] = { ARGPARSE_op_u (oAutoExpandSecmem, "auto-expand-secmem", "@"), ARGPARSE_s_i (oListenBacklog, "listen-backlog", "@"), + ARGPARSE_noconffile (oNoOptions, "no-options", "@"), /* Dummy options for backward compatibility. */ ARGPARSE_o_s (oWriteEnvFile, "write-env-file", "@"), @@ -397,7 +400,9 @@ static char *default_lc_ctype; static char *default_lc_messages; static char *default_xauthority; -/* Name of a config file, which will be reread on a HUP if it is not NULL. */ +/* Name of a config file which was last read on startup or, if missing, + * the name of the standard config file. Any value here enables the + * rereading of the standard config files on SIGHUP. */ static char *config_filename; /* Helper to implement --debug-level */ @@ -514,9 +519,11 @@ my_strusage (int level) switch (level) { + case 9: p = "GPL-3.0-or-later"; break; case 11: p = "@GPG_AGENT@ (@GNUPG@)"; break; case 13: p = VERSION; break; + case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break; case 17: p = PRINTABLE_OS_NAME; break; /* TRANSLATORS: @EMAIL@ will get replaced by the actual bug reporting address. This is so that we can change the @@ -1007,12 +1014,10 @@ main (int argc, char **argv ) ARGPARSE_ARGS pargs; int orig_argc; char **orig_argv; - FILE *configfp = NULL; - char *configname = NULL; + char *last_configname = NULL; + const char *configname = NULL; + int debug_argparser = 0; const char *shell; - unsigned configlineno; - int parse_debug = 0; - int default_config =1; int pipe_server = 0; int is_daemon = 0; int nodetach = 0; @@ -1111,80 +1116,67 @@ main (int argc, char **argv ) orig_argv = argv; pargs.argc = &argc; pargs.argv = &argv; - pargs.flags= 1|(1<<6); /* do not remove the args, ignore version */ - while (arg_parse( &pargs, opts)) + pargs.flags= (ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION); + while (gnupg_argparse (NULL, &pargs, opts)) { - if (pargs.r_opt == oDebug || pargs.r_opt == oDebugAll) - parse_debug++; - else if (pargs.r_opt == oOptions) - { /* yes there is one, so we do not try the default one, but - read the option file when it is encountered at the - commandline */ - default_config = 0; - } - else if (pargs.r_opt == oNoOptions) - default_config = 0; /* --no-options */ - else if (pargs.r_opt == oHomedir) - gnupg_set_homedir (pargs.r.ret_str); - else if (pargs.r_opt == oDebugQuickRandom) - { - gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0); - } + switch (pargs.r_opt) + { + case oDebug: + case oDebugAll: + debug_argparser++; + break; + case oHomedir: + gnupg_set_homedir (pargs.r.ret_str); + break; + + case oDebugQuickRandom: + gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0); + break; + } } + /* Reset the flags. */ + pargs.flags &= ~(ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION); /* Initialize the secure memory. */ gcry_control (GCRYCTL_INIT_SECMEM, SECMEM_BUFFER_SIZE, 0); maybe_setuid = 0; /* - Now we are now working under our real uid - */ + * Now we are now working under our real uid + */ - if (default_config) - configname = make_filename (gnupg_homedir (), - GPG_AGENT_NAME EXTSEP_S "conf", NULL); + gnupg_set_confdir (GNUPG_CONFDIR_SYS, gnupg_sysconfdir ()); + gnupg_set_confdir (GNUPG_CONFDIR_USER, gnupg_homedir ()); argc = orig_argc; argv = orig_argv; pargs.argc = &argc; pargs.argv = &argv; - pargs.flags= 1; /* do not remove the args */ - next_pass: - if (configname) - { - configlineno = 0; - configfp = gnupg_fopen (configname, "r"); - if (!configfp) - { - if (default_config) - { - if( parse_debug ) - log_info (_("Note: no default option file '%s'\n"), - configname ); - /* Save the default conf file name so that - reread_configuration is able to test whether the - config file has been created in the meantime. */ - xfree (config_filename); - config_filename = configname; - configname = NULL; - } - else - { - log_error (_("option file '%s': %s\n"), - configname, strerror(errno) ); - exit(2); - } - xfree (configname); - configname = NULL; - } - if (parse_debug && configname ) - log_info (_("reading options from '%s'\n"), configname ); - default_config = 0; - } + /* We are re-using the struct, thus the reset flag. We OR the + * flags so that the internal intialized flag won't be cleared. */ + pargs.flags |= (ARGPARSE_FLAG_RESET + | ARGPARSE_FLAG_KEEP + | ARGPARSE_FLAG_SYS + | ARGPARSE_FLAG_USER); - while (optfile_parse( configfp, configname, &configlineno, &pargs, opts) ) + while (gnupg_argparser (&pargs, opts, GPG_AGENT_NAME EXTSEP_S "conf")) { + if (pargs.r_opt == ARGPARSE_CONFFILE) + { + if (debug_argparser) + log_info (_("reading options from '%s'\n"), + pargs.r_type? pargs.r.ret_str: "[cmdline]"); + if (pargs.r_type) + { + xfree (last_configname); + last_configname = xstrdup (pargs.r.ret_str); + configname = last_configname; + } + else + configname = NULL; + continue; + } if (parse_rereadable_options (&pargs, 0)) continue; /* Already handled */ switch (pargs.r_opt) @@ -1196,18 +1188,8 @@ main (int argc, char **argv ) case oDebugWait: debug_wait = pargs.r.ret_int; break; - case oOptions: - /* config files may not be nested (silently ignore them) */ - if (!configfp) - { - xfree(configname); - configname = xstrdup(pargs.r.ret_str); - goto next_pass; - } - break; case oNoGreeting: /* Dummy option. */ break; case oNoVerbose: opt.verbose = 0; break; - case oNoOptions: break; /* no-options */ case oHomedir: gnupg_set_homedir (pargs.r.ret_str); break; case oNoDetach: nodetach = 1; break; case oLogFile: logfile = pargs.r.ret_str; break; @@ -1228,7 +1210,7 @@ main (int argc, char **argv ) case oUseStandardSocket: case oNoUseStandardSocket: - obsolete_option (configname, configlineno, "use-standard-socket"); + obsolete_option (configname, pargs.lineno, "use-standard-socket"); break; case oFakedSystemTime: @@ -1280,28 +1262,29 @@ main (int argc, char **argv ) break; case oWriteEnvFile: - obsolete_option (configname, configlineno, "write-env-file"); + obsolete_option (configname, pargs.lineno, "write-env-file"); break; - default : pargs.err = configfp? 1:2; break; + default: + if (configname) + pargs.err = ARGPARSE_PRINT_WARNING; + else + pargs.err = ARGPARSE_PRINT_ERROR; + break; } } - if (configfp) + gnupg_argparse (NULL, &pargs, NULL); /* Release internal state. */ + + if (!last_configname) + config_filename = make_filename (gnupg_homedir (), + GPG_AGENT_NAME EXTSEP_S "conf", + NULL); + else { - fclose( configfp ); - configfp = NULL; - /* Keep a copy of the name so that it can be read on SIGHUP. */ - if (config_filename != configname) - { - xfree (config_filename); - config_filename = configname; - } - configname = NULL; - goto next_pass; + config_filename = last_configname; + last_configname = NULL; } - xfree (configname); - configname = NULL; if (log_get_errorcount(0)) exit(2); @@ -1398,18 +1381,13 @@ main (int argc, char **argv ) agent_exit (0); else if (gpgconf_list) { - char *filename; char *filename_esc; /* List options and default values in the GPG Conf format. */ - filename = make_filename (gnupg_homedir (), - GPG_AGENT_NAME EXTSEP_S "conf", NULL); - filename_esc = percent_escape (filename, NULL); - + filename_esc = percent_escape (config_filename, NULL); es_printf ("%s-%s.conf:%lu:\"%s\n", GPGCONF_NAME, GPG_AGENT_NAME, GC_OPT_FLAG_DEFAULT, filename_esc); - xfree (filename); xfree (filename_esc); es_printf ("verbose:%lu:\n" @@ -2019,35 +1997,39 @@ static void reread_configuration (void) { ARGPARSE_ARGS pargs; - FILE *fp; - unsigned int configlineno = 0; + char *twopart; int dummy; if (!config_filename) return; /* No config file. */ - fp = gnupg_fopen (config_filename, "r"); - if (!fp) - { - log_info (_("option file '%s': %s\n"), - config_filename, strerror(errno) ); - return; - } + twopart = strconcat (GPG_AGENT_NAME EXTSEP_S "conf" PATHSEP_S, + config_filename, NULL); + if (!twopart) + return; /* Out of core. */ parse_rereadable_options (NULL, 1); /* Start from the default values. */ memset (&pargs, 0, sizeof pargs); dummy = 0; pargs.argc = &dummy; - pargs.flags = 1; /* do not remove the args */ - while (optfile_parse (fp, config_filename, &configlineno, &pargs, opts) ) + pargs.flags = (ARGPARSE_FLAG_KEEP + |ARGPARSE_FLAG_SYS + |ARGPARSE_FLAG_USER); + while (gnupg_argparser (&pargs, opts, twopart)) { - if (pargs.r_opt < -1) - pargs.err = 1; /* Print a warning. */ + if (pargs.r_opt == ARGPARSE_CONFFILE) + { + log_info (_("reading options from '%s'\n"), + pargs.r_type? pargs.r.ret_str: "[cmdline]"); + } + else if (pargs.r_opt < -1) + pargs.err = ARGPARSE_PRINT_WARNING; else /* Try to parse this option - ignore unchangeable ones. */ parse_rereadable_options (&pargs, 1); } - fclose (fp); + gnupg_argparse (NULL, &pargs, NULL); /* Release internal state. */ + xfree (twopart); finalize_rereadable_options (); set_debug (); } diff --git a/agent/preset-passphrase.c b/agent/preset-passphrase.c index 3b402598f..c5aeafe13 100644 --- a/agent/preset-passphrase.c +++ b/agent/preset-passphrase.c @@ -15,6 +15,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, see . + * SPDX-License-Identifier: GPL-3.0-or-later */ #include @@ -90,9 +91,11 @@ my_strusage (int level) const char *p; switch (level) { + case 9: p = "GPL-3.0-or-later"; break; case 11: p = "gpg-preset-passphrase (@GNUPG@)"; break; case 13: p = VERSION; break; + case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break; case 17: p = PRINTABLE_OS_NAME; break; case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break; @@ -218,8 +221,8 @@ main (int argc, char **argv) pargs.argc = &argc; pargs.argv = &argv; - pargs.flags= 1; /* (do not remove the args) */ - while (arg_parse (&pargs, opts) ) + pargs.flags= ARGPARSE_FLAG_KEEP; + while (gnupg_argparse (NULL, &pargs, opts)) { switch (pargs.r_opt) { @@ -233,6 +236,7 @@ main (int argc, char **argv) default : pargs.err = 2; break; } } + gnupg_argparse (NULL, &pargs, NULL); /* Release internal state. */ if (log_get_errorcount(0)) exit(2); diff --git a/agent/protect-tool.c b/agent/protect-tool.c index f55a7354d..b04836aaf 100644 --- a/agent/protect-tool.c +++ b/agent/protect-tool.c @@ -15,6 +15,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, see . + * SPDX-License-Identifier: GPL-3.0-or-later */ #include @@ -145,9 +146,11 @@ my_strusage (int level) const char *p; switch (level) { + case 9: p = "GPL-3.0-or-later"; break; case 11: p = "gpg-protect-tool (" GNUPG_NAME ")"; break; case 13: p = VERSION; break; + case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break; case 17: p = PRINTABLE_OS_NAME; break; case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break; @@ -551,7 +554,6 @@ show_keygrip (const char *fname) putchar ('\n'); } - @@ -577,8 +579,8 @@ main (int argc, char **argv ) pargs.argc = &argc; pargs.argv = &argv; - pargs.flags= 1; /* (do not remove the args) */ - while (arg_parse (&pargs, opts) ) + pargs.flags= ARGPARSE_FLAG_KEEP; + while (gnupg_argparse (NULL, &pargs, opts)) { switch (pargs.r_opt) { @@ -608,6 +610,8 @@ main (int argc, char **argv ) default: pargs.err = ARGPARSE_PRINT_ERROR; break; } } + gnupg_argparse (NULL, &pargs, NULL); /* Release internal state. */ + if (log_get_errorcount (0)) exit (2); diff --git a/common/argparse.c b/common/argparse.c index c04cf830d..fff720c19 100644 --- a/common/argparse.c +++ b/common/argparse.c @@ -29,9 +29,8 @@ * if not, see . */ -/* 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. +/* This is a modified version of gpgrt/libgpg-error src/argparse.c. + * We use this to require a dependency on a newer gpgrt version. */ #ifdef HAVE_CONFIG_H @@ -45,232 +44,115 @@ #include #include #include +#include +#include -#ifdef GNUPG_MAJOR_VERSION -# include "util.h" -# include "common-defs.h" -# include "i18n.h" -# include "mischelp.h" -# include "stringhelp.h" -# include "logging.h" -# include "utf8conv.h" -#endif /*GNUPG_MAJOR_VERSION*/ - +#include "util.h" +#include "common-defs.h" +#include "i18n.h" +#include "mischelp.h" +#include "stringhelp.h" +#include "logging.h" +#include "utf8conv.h" +#include "sysutils.h" #include "argparse.h" -/* 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 - set a copyright string for your usage() you may also hardcode it - here. */ -#ifndef GNUPG_MAJOR_VERSION -# define ARGPARSE_GPL_VERSION 2 -# define ARGPARSE_CRIGHT_STR "Copyright (C) YEAR NAME" - -#else /* Used by GnuPG */ - -/* GnuPG has always been a part of the GNU project and thus we have - * shown the FSF as holder of the copyright. We continue to do so for - * the reason that without the FSF the free software used all over the - * world would not have come into existence. However, under Windows - * we print a different copyright string with --version because the - * copyright assignments of g10 Code and Werner Koch were terminated - * many years ago, g10 Code is still the major contributor to the - * code, and Windows is not an FSF endorsed platform. Note that the - * list of copyright holders can be found in the AUTHORS file. */ - -# define ARGPARSE_GPL_VERSION 3 -# ifdef HAVE_W32_SYSTEM -# define ARGPARSE_CRIGHT_STR "Copyright (C) 2020 g10 Code GmbH" -# else -# define ARGPARSE_CRIGHT_STR "Copyright (C) 2020 Free Software Foundation, Inc." -# endif - -#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 -# 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)) -# define map_static_macro_string(a) (a) -#endif /*!GNUPG_MAJOR_VERSION*/ +/* Optional handler to write strings. See gnupg_set_usage_outfnc. */ +static int (*custom_outfnc) (int, const char *); -#define ARGPARSE_STR(v) #v -#define ARGPARSE_STR2(v) ARGPARSE_STR(v) +#if USE_INTERNAL_ARGPARSE + +/* The almost always needed user handler for strusage. */ +static const char *(*strusage_handler)( int ) = NULL; +/* Optional handler to map strings. See gnupg_set_fixed_string_mapper. */ +static const char *(*fixed_string_mapper)(const char*); -/* Replacements for standalone builds. */ -#ifndef GNUPG_MAJOR_VERSION -static void -my_log_error (const char *fmt, ...) +/* Hidden argparse flag used to mark the object as initialized. */ +#define ARGPARSE_FLAG__INITIALIZED (1u << ((8*4)-1)) + +/* Special short options which are auto-inserterd. Must fit into an + * unsigned short. */ +#define ARGPARSE_SHORTOPT_HELP 32768 +#define ARGPARSE_SHORTOPT_VERSION 32769 +#define ARGPARSE_SHORTOPT_WARRANTY 32770 +#define ARGPARSE_SHORTOPT_DUMP_OPTIONS 32771 +#define ARGPARSE_SHORTOPT_DUMP_OPTTBL 32772 + + +/* The malloced configuration directories or NULL. */ +static struct { - va_list arg_ptr ; + char *user; + char *sys; +} confdir; - 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, ...) +/* The states for the gnupg_argparser machinery. */ +enum argparser_states + { + STATE_init = 0, + STATE_open_sys, + STATE_open_user, + STATE_open_cmdline, + STATE_read_sys, + STATE_read_user, + STATE_read_cmdline, + STATE_finished + }; + + +/* An internal object used to store the user provided option table and + * some meta information. */ +typedef struct { - va_list arg_ptr ; + unsigned short short_opt; + unsigned short ordinal; /* (for --help) */ + unsigned int flags; + const char *long_opt; /* Points into the user provided table. */ + const char *description; /* Points into the user provided table. */ + unsigned int forced:1; /* Forced to use the sysconf value. */ + unsigned int ignore:1; /* Ignore this option everywhere but in + * the sysconf file. */ + unsigned int explicit_ignore:1; /* Ignore was explicitly set. */ +} opttable_t; - va_start (arg_ptr, fmt); - fprintf (stderr, "%s: Ohhhh jeeee: ", strusage (11)); - vfprintf (stderr, fmt, arg_ptr); - va_end (arg_ptr); - abort (); -} -/* Return true if the native charset is utf-8. */ -static int -is_native_utf8 (void) +/* Internal object of the public gnupg_argparse_t object. */ +struct _argparse_internal_s { - return 1; -} + int idx; /* Note that this is saved and restored in gnupg_argparser. */ + int inarg; /* (index into args) */ + unsigned int verbose:1; /* Print diagnostics. */ + unsigned int stopped:1; /* Option processing has stopped. */ + unsigned int in_sysconf:1; /* Processing global config file. */ + unsigned int mark_forced:1; /* Mark options as forced. */ + unsigned int mark_ignore:1; /* Mark options as to be ignored. */ + unsigned int explicit_ignore:1; /* Option has explicitly been set + * to ignore or unignore. */ + unsigned int ignore_all_seen:1; /* [ignore-all] has been seen. */ + unsigned int user_seen:1; /* A [user] has been seen. */ + unsigned int user_wildcard:1; /* A [user *] has been seen. */ + unsigned int user_any_active:1; /* Any user section was active. */ + unsigned int user_active:1; /* User section active. */ + unsigned int explicit_confopt:1; /* A conffile option has been given. */ + char *explicit_conffile; /* Malloced name of an explicit + * conffile. */ + char *username; /* Malloced current user name. */ + unsigned int opt_flags; /* Current option flags. */ + enum argparser_states state; /* State of the gnupg_argparser. */ + const char *last; + void *aliases; + const void *cur_alias; + void *iio_list; + estream_t conffp; + char *confname; + opttable_t *opts; /* Malloced option table. */ + unsigned int nopts; /* Number of items in OPTS. */ +}; -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*/ - - - -/********************************* - * @Summary arg_parse - * #include "argparse.h" - * - * 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. - * Bit 6 : Ignore this option - * Bit 7 : This is a command and not an option - * 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 }, - * { 300, "ignored-long-option, ARGPARSE_OP_IGNORE}, - * { 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 { @@ -289,26 +171,103 @@ struct iio_item_def_s char name[1]; /* String with the long option name. */ }; -static const char *(*strusage_handler)( int ) = NULL; -static int (*custom_outfnc) (int, const char *); -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 int writestrings (int is_error, const char *string, ...) -#if __GNUC__ >= 4 - __attribute__ ((sentinel(0))) -#endif - ; +static int set_opt_arg (gnupg_argparse_t *arg, unsigned int flags, char *s); +static void show_help (opttable_t *opts, unsigned int nopts,unsigned int flags); +static void show_version (void); +static void dump_option_table (gnupg_argparse_t *arg); +static int writestrings (int is_error, const char *string, + ...) GPGRT_ATTR_SENTINEL(0); + +static int arg_parse (gnupg_argparse_t *arg, gnupg_opt_t *opts, int no_init); + +/* Set a function to write strings which is then used instead of + * estream. The first arg of that function is MODE and the second the + * STRING to write. A mode of 1 is used for writing to stdout and a + * mode of 2 to write to stderr. Other modes are reserved and should + * not output anything. A NULL for STRING requests a flush. */ void -argparse_register_outfnc (int (*fnc)(int, const char *)) +gnupg_set_usage_outfnc (int (*f)(int, const char *)) { - custom_outfnc = fnc; + custom_outfnc = f; } +/* Register function F as a string mapper which takes a string as + * argument, replaces known "@FOO@" style macros and returns a new + * fixed string. Warning: The input STRING must have been allocated + * statically. */ +void +gnupg_set_fixed_string_mapper (const char *(*f)(const char*)) +{ + fixed_string_mapper = f; +} + + +/* Register a configuration directory for use by the argparse + * functions. The defined values for WHAT are: + * + * GNUPG_CONFDIR_SYS The systems's configuration dir. + * The default is /etc + * + * GNUPG_CONFDIR_USER The user's configuration directory. + * The default is $HOME. + * + * A trailing slash is ignored; to have the function lookup + * configuration files in the current directory, use ".". There is no + * error return; more configuraion values may be added in future + * revisions of this library. + */ +void +gnupg_set_confdir (int what, const char *name) +{ + char *buf, *p; + + if (what == GNUPG_CONFDIR_SYS) + { + xfree (confdir.sys); + buf = confdir.sys = xtrystrdup (name); + } + else if (what == GNUPG_CONFDIR_USER) + { + xfree (confdir.user); + buf = confdir.user = xtrystrdup (name); + } + else + return; + + if (!buf) + log_fatal ("out of core in %s\n", __func__); +#ifdef HAVE_W32_SYSTEM + for (p=buf; *p; p++) + if (*p == '\\') + *p = '/'; +#endif + /* Strip trailing slashes unless buf is "/" or any other single char + * string. */ + if (*buf) + { + for (p=buf + strlen (buf)-1; p > buf; p--) + if (*p == '/') + *p = 0; + else + break; + } +} + + + +static const char * +map_fixed_string (const char *string) +{ + return fixed_string_mapper? fixed_string_mapper (string) : string; +} + +#endif /* USE_INTERNAL_ARGPARSE */ + + /* 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. */ @@ -324,7 +283,7 @@ writestrings (int is_error, const char *string, ...) s = string; va_start (arg_ptr, string); do - { + { /* Fixme: Swicth to estream? */ if (custom_outfnc) custom_outfnc (is_error? 2:1, s); else @@ -348,32 +307,214 @@ flushstrings (int is_error) } +#if USE_INTERNAL_ARGPARSE + static void -initialize( ARGPARSE_ARGS *arg, const char *filename, unsigned *lineno ) +deinitialize (gnupg_argparse_t *arg) { - if( !(arg->flags & (1<<15)) ) + if (arg->internal) { - /* 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; - arg->internal.iio_list = NULL; - arg->err = 0; - arg->flags |= 1<<15; /* Mark as initialized. */ - if ( *arg->argc < 0 ) - log_bug ("invalid argument for arg_parse\n"); + xfree (arg->internal->username); + xfree (arg->internal->explicit_conffile); + xfree (arg->internal->opts); + xfree (arg->internal); + arg->internal = NULL; } + arg->flags &= ARGPARSE_FLAG__INITIALIZED; + arg->lineno = 0; + arg->err = 0; +} + +/* Our own exit handler to clean up used memory. */ +static void +my_exit (gnupg_argparse_t *arg, int code) +{ + deinitialize (arg); + exit (code); +} + + +static gpg_err_code_t +initialize (gnupg_argparse_t *arg, gnupg_opt_t *opts, estream_t fp) +{ + /* We use a dedicated flag to detect whether *ARG has been + * initialized. This is because the old version of that struct, as + * used in GnuPG, had no requirement to zero out all fields of the + * object and existing code still sets only argc,argv and flags. */ + if (!(arg->flags & ARGPARSE_FLAG__INITIALIZED) + || (arg->flags & ARGPARSE_FLAG_RESET) + || !arg->internal) + { + /* Allocate internal data. */ + if (!(arg->flags & ARGPARSE_FLAG__INITIALIZED) || !arg->internal) + { + arg->internal = xtrymalloc (sizeof *arg->internal); + if (!arg->internal) + return gpg_err_code_from_syserror (); + arg->flags |= ARGPARSE_FLAG__INITIALIZED; /* Mark as initialized. */ + } + else if (arg->internal->opts) + xfree (arg->internal->opts); + arg->internal->opts = NULL; + arg->internal->nopts = 0; + + /* Initialize this instance. */ + arg->internal->idx = 0; + arg->internal->last = NULL; + arg->internal->inarg = 0; + arg->internal->stopped = 0; + arg->internal->in_sysconf = 0; + arg->internal->user_seen = 0; + arg->internal->user_wildcard = 0; + arg->internal->user_any_active = 0; + arg->internal->user_active = 0; + arg->internal->username = NULL; + arg->internal->mark_forced = 0; + arg->internal->mark_ignore = 0; + arg->internal->explicit_ignore = 0; + arg->internal->ignore_all_seen = 0; + arg->internal->explicit_confopt = 0; + arg->internal->explicit_conffile = NULL; + arg->internal->opt_flags = 0; + arg->internal->state = STATE_init; + arg->internal->aliases = NULL; + arg->internal->cur_alias = NULL; + arg->internal->iio_list = NULL; + arg->internal->conffp = NULL; + arg->internal->confname = NULL; + + /* Clear the copy of the option list. */ + /* Clear the error indicator. */ + arg->err = 0; + + /* Usually an option file will be parsed from the start. + * However, we do not open the stream and thus we have no way to + * know the current lineno. Using this flag we can allow the + * user to provide a lineno which we don't reset. */ + if (fp || arg->internal->conffp || !(arg->flags & ARGPARSE_FLAG_NOLINENO)) + arg->lineno = 0; + + /* Need to clear the reset request. */ + arg->flags &= ~ARGPARSE_FLAG_RESET; + + /* Check initial args. */ + if ( *arg->argc < 0 ) + log_bug ("invalid argument passed to gnupg_argparse\n"); + + } + + /* Create an array with pointers to the provided list of options. + * Keeping a copy is useful to sort that array and thus do a binary + * search and to allow for extra space at the end to insert the + * hidden options. An ARGPARSE_FLAG_RESET can be used to reinit + * this array. */ + if (!arg->internal->opts) + { + int seen_help = 0; + int seen_version = 0; + int seen_warranty = 0; + int seen_dump_options = 0; + int seen_dump_option_table = 0; + int i; + + for (i=0; opts[i].short_opt; i++) + { + if (opts[i].long_opt) + { + if (!strcmp(opts[i].long_opt, "help")) + seen_help = 1; + else if (!strcmp(opts[i].long_opt, "version")) + seen_version = 1; + else if (!strcmp(opts[i].long_opt, "warranty")) + seen_warranty = 1; + else if (!strcmp(opts[i].long_opt, "dump-options")) + seen_dump_options = 1; + else if (!strcmp(opts[i].long_opt, "dump-option-table")) + seen_dump_option_table = 1; + } + } + i += 5; /* The number of the above internal options. */ + i++; /* End of list marker. */ + arg->internal->opts = xtrycalloc (i, sizeof *arg->internal->opts); + if (!arg->internal->opts) + return gpg_err_code_from_syserror (); + for(i=0; opts[i].short_opt; i++) + { + arg->internal->opts[i].short_opt = opts[i].short_opt; + arg->internal->opts[i].flags = opts[i].flags; + arg->internal->opts[i].long_opt = opts[i].long_opt; + arg->internal->opts[i].description = opts[i].description; + arg->internal->opts[i].ordinal = i; + } + + if (!seen_help) + { + arg->internal->opts[i].short_opt = ARGPARSE_SHORTOPT_HELP; + arg->internal->opts[i].flags = ARGPARSE_TYPE_NONE; + arg->internal->opts[i].long_opt = "help"; + arg->internal->opts[i].description = "@"; + arg->internal->opts[i].ordinal = i; + i++; + } + if (!seen_version) + { + arg->internal->opts[i].short_opt = ARGPARSE_SHORTOPT_VERSION; + arg->internal->opts[i].flags = ARGPARSE_TYPE_NONE; + arg->internal->opts[i].long_opt = "version"; + arg->internal->opts[i].description = "@"; + arg->internal->opts[i].ordinal = i; + i++; + } + + if (!seen_warranty) + { + arg->internal->opts[i].short_opt = ARGPARSE_SHORTOPT_WARRANTY; + arg->internal->opts[i].flags = ARGPARSE_TYPE_NONE; + arg->internal->opts[i].long_opt = "warranty"; + arg->internal->opts[i].description = "@"; + arg->internal->opts[i].ordinal = i; + i++; + } + + if (!seen_dump_option_table) + { + arg->internal->opts[i].short_opt = ARGPARSE_SHORTOPT_DUMP_OPTTBL; + arg->internal->opts[i].flags = ARGPARSE_TYPE_NONE; + arg->internal->opts[i].long_opt = "dump-option-table"; + arg->internal->opts[i].description = "@"; + arg->internal->opts[i].ordinal = i; + i++; + } + + if (!seen_dump_options) + { + arg->internal->opts[i].short_opt = ARGPARSE_SHORTOPT_DUMP_OPTIONS; + arg->internal->opts[i].flags = ARGPARSE_TYPE_NONE; + arg->internal->opts[i].long_opt = "dump-options"; + arg->internal->opts[i].description = "@"; + arg->internal->opts[i].ordinal = i; + i++; + } + /* Take care: When adding new options remember to increase the + * size of the array. */ + + arg->internal->opts[i].short_opt = 0; + + /* Note that we do not count the end marker but keep it in the + * table anyway as an extra item. */ + arg->internal->nopts = i; + } if (arg->err) { /* Last option was erroneous. */ const char *s; - if (filename) + if (!fp && arg->internal->conffp) + fp = arg->internal->conffp; + + if (fp) { if ( arg->r_opt == ARGPARSE_UNEXPECTED_ARG ) s = _("argument not expected"); @@ -389,22 +530,35 @@ initialize( ARGPARSE_ARGS *arg, const char *filename, unsigned *lineno ) s = _("invalid command"); else if ( arg->r_opt == ARGPARSE_INVALID_ALIAS ) s = _("invalid alias definition"); + else if ( arg->r_opt == ARGPARSE_PERMISSION_ERROR ) + s = _("permission error"); else if ( arg->r_opt == ARGPARSE_OUT_OF_CORE ) s = _("out of core"); + else if ( arg->r_opt == ARGPARSE_NO_CONFFILE ) + s = NULL; /* Error has already been printed. */ + else if ( arg->r_opt == ARGPARSE_INVALID_META ) + s = _("invalid meta command"); + else if ( arg->r_opt == ARGPARSE_UNKNOWN_META ) + s = _("unknown meta command"); + else if ( arg->r_opt == ARGPARSE_UNEXPECTED_META ) + s = _("unexpected meta command"); else s = _("invalid option"); - log_error ("%s:%u: %s\n", filename, *lineno, s); + if (s) + log_error ("%s:%u: %s\n", + gpgrt_fname_get (fp), arg->lineno, s); } else { - s = arg->internal.last? arg->internal.last:"[??]"; + s = arg->internal->last? arg->internal->last:"[??]"; if ( arg->r_opt == ARGPARSE_MISSING_ARG ) log_error (_("missing argument for option \"%.50s\"\n"), s); else if ( arg->r_opt == ARGPARSE_INVALID_ARG ) log_error (_("invalid argument for option \"%.50s\"\n"), s); else if ( arg->r_opt == ARGPARSE_UNEXPECTED_ARG ) - log_error (_("option \"%.50s\" does not expect an argument\n"), s); + log_error (_("option \"%.50s\" does not expect " + "an argument\n"), s); else if ( arg->r_opt == ARGPARSE_INVALID_COMMAND ) log_error (_("invalid command \"%.50s\"\n"), s); else if ( arg->r_opt == ARGPARSE_AMBIGUOUS_OPTION ) @@ -412,18 +566,30 @@ initialize( ARGPARSE_ARGS *arg, const char *filename, unsigned *lineno ) else if ( arg->r_opt == ARGPARSE_AMBIGUOUS_COMMAND ) log_error (_("command \"%.50s\" is ambiguous\n"),s ); else if ( arg->r_opt == ARGPARSE_OUT_OF_CORE ) - log_error ("%s\n", _("out of core\n")); + log_error ("%s\n", _("out of core")); + else if ( arg->r_opt == ARGPARSE_PERMISSION_ERROR ) + log_error ("%s\n", _("permission error")); + else if ( arg->r_opt == ARGPARSE_NO_CONFFILE) + ; /* Error has already been printed. */ + else if ( arg->r_opt == ARGPARSE_INVALID_META ) + log_error ("%s\n", _("invalid meta command")); + else if ( arg->r_opt == ARGPARSE_UNKNOWN_META ) + log_error ("%s\n", _("unknown meta command")); + else if ( arg->r_opt == ARGPARSE_UNEXPECTED_META ) + log_error ("%s\n",_("unexpected meta command")); else log_error (_("invalid option \"%.50s\"\n"), s); } if (arg->err != ARGPARSE_PRINT_WARNING) - exit (2); + my_exit (arg, 2); arg->err = 0; } /* Zero out the return value union. */ arg->r.ret_str = NULL; arg->r.ret_long = 0; + + return 0; } @@ -451,7 +617,7 @@ store_alias( ARGPARSE_ARGS *arg, char *name, char *value ) static int ignore_invalid_option_p (ARGPARSE_ARGS *arg, const char *keyword) { - IIO_ITEM_DEF item = arg->internal.iio_list; + IIO_ITEM_DEF item = arg->internal->iio_list; for (; item; item = item->next) if (!strcmp (item->name, keyword)) @@ -465,7 +631,7 @@ ignore_invalid_option_p (ARGPARSE_ARGS *arg, const char *keyword) 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) +ignore_invalid_option_add (ARGPARSE_ARGS *arg, estream_t fp) { IIO_ITEM_DEF item; int c; @@ -476,7 +642,7 @@ ignore_invalid_option_add (ARGPARSE_ARGS *arg, FILE *fp) while (!ready) { - c = getc (fp); + c = gpgrt_getc (fp); if (c == '\n') ready = 1; else if (c == EOF) @@ -524,8 +690,8 @@ ignore_invalid_option_add (ARGPARSE_ARGS *arg, FILE *fp) if (!item) return 1; strcpy (item->name, name); - item->next = (IIO_ITEM_DEF)arg->internal.iio_list; - arg->internal.iio_list = item; + item->next = (IIO_ITEM_DEF)arg->internal->iio_list; + arg->internal->iio_list = item; } state = skipWS; goto again; @@ -541,15 +707,261 @@ ignore_invalid_option_clear (ARGPARSE_ARGS *arg) { IIO_ITEM_DEF item, tmpitem; - for (item = arg->internal.iio_list; item; item = tmpitem) + for (item = arg->internal->iio_list; item; item = tmpitem) { tmpitem = item->next; xfree (item); } - arg->internal.iio_list = NULL; + arg->internal->iio_list = NULL; } +/* Make sure the username field is filled. Return 0 on success. */ +static int +assure_username (gnupg_argparse_t *arg) +{ + if (!arg->internal->username) + { + arg->internal->username = "dummyuser"; /*FIXMEgpgrt_getusername ();*/ + if (!arg->internal->username) + { + log_error ("%s:%u: error getting current user's name: %s\n", + arg->internal->confname, arg->lineno, + gpg_strerror (gpg_error_from_syserror ())); + /* Not necessary the correct error code but given that we + * either have a malloc error or some internal system error, + * it is the best we can do. */ + return ARGPARSE_PERMISSION_ERROR; + } + } + return 0; +} + + +/* Implementation of the "user" command. ARG is the context. ARGS is + * a non-empty string which this function is allowed to modify. */ +static int +handle_meta_user (gnupg_argparse_t *arg, unsigned int alternate, char *args) +{ + int rc; + + (void)alternate; + + rc = assure_username (arg); + if (rc) + return rc; + + arg->internal->user_seen = 1; + if (*args == '*' && !args[1]) + { + arg->internal->user_wildcard = 1; + arg->internal->user_active = !arg->internal->user_any_active; + } + else if (arg->internal->user_wildcard) + { + /* All other user statements are ignored after a wildcard. */ + arg->internal->user_active = 0; + } + else if (!strcasecmp (args, arg->internal->username)) + { + arg->internal->user_any_active = 1; + arg->internal->user_active = 1; + } + else + { + arg->internal->user_active = 0; + } + + return 0; +} + + +/* Implementation of the "force" command. ARG is the context. A + * value of 0 for ALTERNATE is "force", a value of 1 requests an + * unforce". ARGS is the empty string and not used. */ +static int +handle_meta_force (gnupg_argparse_t *arg, unsigned int alternate, char *args) +{ + (void)args; + + arg->internal->mark_forced = alternate? 0 : 1; + + return 0; +} + + +/* Implementation of the "ignore" command. ARG is the context. A + * value of 0 for ALTERNATE is a plain "ignore", a value of 1 request + * an "unignore, a value of 2 requests an "ignore-all". ARGS is the + * empty string and not used. */ +static int +handle_meta_ignore (gnupg_argparse_t *arg, unsigned int alternate, char *args) +{ + (void)args; + + if (!alternate) + { + arg->internal->mark_ignore = 1; + arg->internal->explicit_ignore = 1; + } + else if (alternate == 1) + { + arg->internal->mark_ignore = 0; + arg->internal->explicit_ignore = 1; + } + else + arg->internal->ignore_all_seen = 1; + + return 0; +} + + +/* Implementation of the "echo" command. ARG is the context. If + * ALTERNATE is true the filename is not printed. ARGS is the string + * to log. */ +static int +handle_meta_echo (gnupg_argparse_t *arg, unsigned int alternate, char *args) +{ + int rc = 0; + char *p, *pend; + + if (alternate) + log_info ("%s", ""); + else + log_info ("%s:%u: ", arg->internal->confname, arg->lineno); + + while (*args) + { + p = strchr (args, '$'); + if (!p) + { + log_printf ("%s", args); + break; + } + *p = 0; + log_printf ("%s", args); + if (p[1] == '$') + { + log_printf ("$"); + args = p+2; + continue; + } + if (p[1] != '{') + { + log_printf ("$"); + args = p+1; + continue; + } + pend = strchr (p+2, '}'); + if (!pend) /* No closing brace. */ + { + log_printf ("$"); + args = p+1; + continue; + } + p += 2; + *pend = 0; + args = pend+1; + if (!strcmp (p, "user")) + { + rc = assure_username (arg); + if (rc) + goto leave; + log_printf ("%s", arg->internal->username); + } + else if (!strcmp (p, "file")) + log_printf ("%s", arg->internal->confname); + else if (!strcmp (p, "line")) + log_printf ("%u", arg->lineno); + else if (!strcmp (p, "epoch")) + log_printf ("%lu", (unsigned long)time (NULL)); + } + + leave: + log_printf ("\n"); + return rc; +} + + +/* Implementation of the "verbose" command. ARG is the context. If + * ALTERNATE is true the verbosity is disabled. ARGS is not used. */ +static int +handle_meta_verbose (gnupg_argparse_t *arg, unsigned int alternate, char *args) +{ + (void)args; + + if (alternate) + arg->internal->verbose = 0; + else + arg->internal->verbose = 1; + return 0; +} + +/* Handle a meta command. KEYWORD has the content inside the brackets + * with leading and trailing spaces removed. The function may modify + * KEYWORD. On success 0 is returned, on error an ARGPARSE_ error + * code is returned. */ +static int +handle_metacmd (gnupg_argparse_t *arg, char *keyword) +{ + static struct { + const char *name; /* Name of the command. */ + unsigned short alternate; /* Use alternate version of the command. */ + unsigned short needarg:1; /* Command requires an argument. */ + unsigned short always:1; /* Command allowed in all conf files. */ + unsigned short noskip:1; /* Even done in non-active [user] mode. */ + int (*func)(gnupg_argparse_t *arg, + unsigned int alternate, char *args); /*handler*/ + } cmds[] = + {{ "user", 0, 1, 0, 1, handle_meta_user }, + { "force", 0, 0, 0, 0, handle_meta_force }, + { "+force", 0, 0, 0, 0, handle_meta_force }, + { "-force", 1, 0, 0, 0, handle_meta_force }, + { "ignore", 0, 0, 0, 0, handle_meta_ignore }, + { "+ignore", 0, 0, 0, 0, handle_meta_ignore }, + { "-ignore", 1, 0, 0, 0, handle_meta_ignore }, + { "ignore-all", 2, 0, 0, 0, handle_meta_ignore }, + { "+ignore-all", 2, 0, 0, 0, handle_meta_ignore }, + { "verbose", 0, 0, 1, 1, handle_meta_verbose }, + { "+verbose", 0, 0, 1, 1, handle_meta_verbose }, + { "-verbose", 1, 0, 1, 1, handle_meta_verbose }, + { "echo", 0, 1, 1, 1, handle_meta_echo }, + { "-echo", 1, 1, 1, 1, handle_meta_echo }, + { "info", 0, 1, 1, 0, handle_meta_echo }, + { "-info", 1, 1, 1, 0, handle_meta_echo } + }; + char *rest; + int i; + + for (rest = keyword; *rest && !(isascii (*rest) && isspace (*rest)); rest++) + ; + if (*rest) + { + *rest++ = 0; + trim_spaces (rest); + } + + for (i=0; i < DIM (cmds); i++) + if (!strcmp (cmds[i].name, keyword)) + break; + if (!(i < DIM (cmds))) + return ARGPARSE_UNKNOWN_META; + if (cmds[i].needarg && !*rest) + return ARGPARSE_MISSING_ARG; + if (!cmds[i].needarg && *rest) + return ARGPARSE_UNEXPECTED_ARG; + if (!arg->internal->in_sysconf && !cmds[i].always) + return ARGPARSE_UNEXPECTED_META; + + if (!cmds[i].noskip + && arg->internal->in_sysconf + && arg->internal->user_seen + && !arg->internal->user_active) + return 0; /* Skip this meta command. */ + + return cmds[i].func (arg, cmds[i].alternate, rest); +} + /**************** * Get options from a file. @@ -573,11 +985,25 @@ ignore_invalid_option_clear (ARGPARSE_ARGS *arg) * Note: Abbreviation of options is here not allowed. */ int -optfile_parse (FILE *fp, const char *filename, unsigned *lineno, - ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts) +gnupg_argparse (estream_t fp, gnupg_argparse_t *arg, gnupg_opt_t *opts_orig) { - int state, i, c; - int idx=0; + enum { Ainit, + Acomment, /* In a comment line. */ + Acopykeyword, /* Collecting a keyword. */ + Awaitarg, /* Wait for an argument. */ + Acopyarg, /* Copy the argument. */ + Akeyword_eol, /* Got keyword at end of line. */ + Akeyword_spc, /* Got keyword at space. */ + Acopymetacmd, /* Copy a meta command. */ + Askipmetacmd, /* Skip spaces after metacmd. */ + Askipmetacmd2,/* Skip comment after metacmd. */ + Ametacmd, /* Process the metacmd. */ + Askipandleave /* Skip the rest of the line and then leave. */ + } state; + opttable_t *opts; + unsigned int nopts; + int i, c; + int idx = 0; char keyword[100]; char *buffer = NULL; size_t buflen = 0; @@ -585,97 +1011,246 @@ optfile_parse (FILE *fp, const char *filename, unsigned *lineno, int unread_buf[3]; /* We use an int so that we can store EOF. */ int unread_buf_count = 0; - if (!fp) /* Divert to arg_parse() in this case. */ - return arg_parse (arg, opts); + if (arg && !opts_orig) + { + deinitialize (arg); + return 0; + } - initialize (arg, filename, lineno); + if (!fp) /* Divert to arg_parse() in this case. */ + return arg_parse (arg, opts_orig, 0); + + if (initialize (arg, opts_orig, fp)) + return (arg->r_opt = ARGPARSE_OUT_OF_CORE); + + opts = arg->internal->opts; + nopts = arg->internal->nopts; /* 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) + if (!arg->lineno) { - unread_buf[0] = getc (fp); - unread_buf[1] = getc (fp); - unread_buf[2] = getc (fp); + unread_buf[0] = gpgrt_fgetc (fp); + unread_buf[1] = gpgrt_fgetc (fp); + unread_buf[2] = gpgrt_fgetc (fp); if (unread_buf[0] != 0xef || unread_buf[1] != 0xbb || unread_buf[2] != 0xbf) unread_buf_count = 3; } + arg->internal->opt_flags = 0; + /* Find the next keyword. */ - state = i = 0; + state = Ainit; + i = 0; for (;;) { - if (unread_buf_count) - c = unread_buf[3 - unread_buf_count--]; - else - c = getc (fp); - if (c == '\n' || c== EOF ) + nextstate: + /* Before scanning the next char handle the keyword seen states. */ + if (state == Akeyword_eol || state == Akeyword_spc) { - if ( c != EOF ) - ++*lineno; - if (state == -1) - break; - else if (state == 2) + /* We are either at the end of a line or right after a + * keyword. In the latter case we need to find the keyword + * so that we can decide whether an argument is required. */ + + /* Check the keyword. */ + for (idx=0; idx < nopts; idx++ ) { - keyword[i] = 0; - for (i=0; opts[i].short_opt; i++ ) + if (opts[idx].long_opt && !strcmp (opts[idx].long_opt, keyword)) + break; + } + arg->r_opt = opts[idx].short_opt; + if (!(idx < nopts)) + { + /* The option (keyword) is not known - check for + * internal keywords before returning an error. */ + if (state == Akeyword_spc && !strcmp (keyword, "alias")) { - if (opts[i].long_opt && !strcmp (opts[i].long_opt, keyword)) - break; + in_alias = 1; + state = Awaitarg; } - idx = i; - arg->r_opt = opts[idx].short_opt; - if ((opts[idx].flags & ARGPARSE_OPT_IGNORE)) + else if (!strcmp (keyword, "ignore-invalid-option")) { - state = i = 0; - continue; + /* We might have keywords as argument - add them to + * the list of ignored keywords. Note that we + * ignore empty argument lists and thus do not to + * call the function in the Akeyword_eol state. */ + if (state == Akeyword_spc) + { + if (ignore_invalid_option_add (arg, fp)) + { + arg->r_opt = ARGPARSE_OUT_OF_CORE; + goto leave; + } + arg->lineno++; + } + state = Ainit; + i = 0; } - else if (!opts[idx].short_opt ) + else if (ignore_invalid_option_p (arg, keyword)) + { + /* This invalid option is already in the iio list. */ + state = state == Akeyword_eol? Ainit : Acomment; + i = 0; + } + else { - 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); + if (state == Akeyword_spc) + state = Askipandleave; + else + goto leave; } - else if (!(opts[idx].flags & ARGPARSE_TYPE_MASK)) - arg->r_type = 0; /* Does not take an arg. */ + } + else if (state == Akeyword_spc) + { + /* Known option but need to scan for args. */ + state = Awaitarg; + } + else if (arg->internal->in_sysconf + && arg->internal->user_seen + && !arg->internal->user_active) + { + /* We are in a [user] meta command and it is not active. + * Skip the command. */ + state = state == Akeyword_eol? Ainit : Acomment; + i = 0; + } + else if ((opts[idx].flags & ARGPARSE_OPT_IGNORE)) + { + /* Known option is configured to be ignored. Start from + * scratch (new line) or process like a comment. */ + state = state == Akeyword_eol? Ainit : Acomment; + i = 0; + } + else /* Known option */ + { + int set_ignore = 0; + + if (arg->internal->in_sysconf) + { + /* Set the current forced and ignored attributes. */ + if (arg->internal->mark_forced) + opts[idx].forced = 1; + if (arg->internal->mark_ignore) + opts[idx].ignore = 1; + if (arg->internal->explicit_ignore) + opts[idx].explicit_ignore = 1; + } + else /* Non-sysconf file */ + { /* Act upon the forced and ignored attributes. */ + if (opts[idx].ignore || opts[idx].forced) + { + if (arg->internal->verbose) + log_info ("%s:%u: ignoring option \"--%s\"" + " due to attributes:%s%s\n", + arg->internal->confname, + arg->lineno, + opts[idx].long_opt, + opts[idx].forced? " forced":"", + opts[idx].ignore? " ignore":""); + if ((arg->flags & ARGPARSE_FLAG_WITHATTR)) + set_ignore = 1; + else + { + state = Ainit; + i = 0; + goto nextstate; /* Ignore this one. */ + } + } + } + + arg->r_opt = opts[idx].short_opt; + if (!(opts[idx].flags & ARGPARSE_TYPE_MASK)) + arg->r_type = ARGPARSE_TYPE_NONE; /* Does not take an arg. */ else if ((opts[idx].flags & ARGPARSE_OPT_OPTIONAL) ) - arg->r_type = 0; /* Arg is optional. */ + arg->r_type = ARGPARSE_TYPE_NONE; /* Arg is optional. */ else arg->r_opt = ARGPARSE_MISSING_ARG; - break; - } - else if (state == 3) + /* If the caller wants us to return the attributes or + * ignored options, or the flags in. */ + if ((arg->flags & ARGPARSE_FLAG_WITHATTR)) + { + if (opts[idx].ignore) + arg->r_type |= ARGPARSE_ATTR_IGNORE; + if (opts[idx].forced) + arg->r_type |= ARGPARSE_ATTR_FORCE; + if (set_ignore) + arg->r_type |= ARGPARSE_OPT_IGNORE; + } + + goto leave; + } + } /* (end state Akeyword_eol/Akeyword_spc) */ + else if (state == Ametacmd) + { + /* We are at the end of a line. */ + log_assert (*keyword == '['); + trim_spaces (keyword+1); + if (!keyword[1]) { - /* No argument found. */ + arg->r_opt = ARGPARSE_INVALID_META; /* Empty. */ + goto leave; + } + c = handle_metacmd (arg, keyword+1); + if (c) + { + arg->r_opt = c; /* Return error. */ + goto leave; + } + state = Ainit; + i = 0; + } + + /* Get the next character from the line. */ + if (unread_buf_count) + c = unread_buf[3 - unread_buf_count--]; + else + c = gpgrt_fgetc (fp); + + if (c == '\n' || c== EOF ) + { /* Handle end of line. */ + if ( c != EOF ) + arg->lineno++; + if (state == Askipandleave) + goto leave; + else if (state == Acopykeyword) + { + keyword[i] = 0; + state = Akeyword_eol; + goto nextstate; + } + else if (state == Acopymetacmd) + { + arg->r_opt = ARGPARSE_INVALID_META; /* "]" missing */ + goto leave; + } + else if (state == Askipmetacmd || state == Askipmetacmd2) + { + state = Ametacmd; + goto nextstate; + } + else if (state == Awaitarg) + { + /* No argument found at the end of the line. */ if (in_alias) arg->r_opt = ARGPARSE_MISSING_ARG; else if (!(opts[idx].flags & ARGPARSE_TYPE_MASK)) - arg->r_type = 0; /* Does not take an arg. */ + arg->r_type = ARGPARSE_TYPE_NONE; /* Does not take an arg. */ else if ((opts[idx].flags & ARGPARSE_OPT_OPTIONAL)) - arg->r_type = 0; /* No optional argument. */ + arg->r_type = ARGPARSE_TYPE_NONE; /* No optional argument. */ else arg->r_opt = ARGPARSE_MISSING_ARG; - break; + goto leave; } - else if (state == 4) + else if (state == Acopyarg) { - /* Has an argument. */ + /* Has an argument at the end of a line. */ if (in_alias) { if (!buffer) @@ -735,82 +1310,61 @@ optfile_parse (FILE *fp, const char *filename, unsigned *lineno, gpgrt_annotate_leaked_object (buffer); } } - break; + goto leave; } else if (c == EOF) { ignore_invalid_option_clear (arg); - if (ferror (fp)) + if (gpgrt_ferror (fp)) arg->r_opt = ARGPARSE_READ_ERROR; else arg->r_opt = 0; /* EOF. */ - break; + goto leave; } - state = 0; + state = Ainit; i = 0; - } - else if (state == -1) + } /* (end handle end of line) */ + else if (state == Askipandleave) ; /* Skip. */ - else if (state == 0 && isascii (c) && isspace(c)) + else if (state == Ainit && isascii (c) && isspace(c)) ; /* Skip leading white space. */ - else if (state == 0 && c == '#' ) - state = 1; /* Start of a comment. */ - else if (state == 1) + else if (state == Ainit && c == '#' ) + state = Acomment; /* Start of a comment. */ + else if (state == Acomment || state == Askipmetacmd2) ; /* Skip comments. */ - else if (state == 2 && isascii (c) && isspace(c)) + else if (state == Askipmetacmd) { - /* 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; - if ((opts[idx].flags & ARGPARSE_OPT_IGNORE)) + if (c == '#') + state = Askipmetacmd2; + else if (!(isascii (c) && isspace(c))) { - state = 1; /* Process like a comment. */ + arg->r_opt = ARGPARSE_INVALID_META; + state = Askipandleave; } - else if (!opts[idx].short_opt) - { - if (!strcmp (keyword, "alias")) - { - in_alias = 1; - state = 3; - } - 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. */ - else - { - arg->r_opt = ((opts[idx].flags & ARGPARSE_OPT_COMMAND) - ? ARGPARSE_INVALID_COMMAND - : ARGPARSE_INVALID_OPTION); - state = -1; /* Skip rest of line and leave. */ - } - } - else - state = 3; } - else if (state == 3) + else if (state == Acopykeyword && isascii (c) && isspace(c)) + { + keyword[i] = 0; + state = Akeyword_spc; + goto nextstate; + } + else if (state == Acopymetacmd && c == ']') + { + keyword[i] = 0; + state = Askipmetacmd; + goto nextstate; + } + else if (state == Awaitarg) { /* Skip leading spaces of the argument. */ if (!isascii (c) || !isspace(c)) { i = 0; keyword[i++] = c; - state = 4; + state = Acopyarg; } } - else if (state == 4) + else if (state == Acopyarg) { /* Collect the argument. */ if (buffer) @@ -833,7 +1387,7 @@ optfile_parse (FILE *fp, const char *filename, unsigned *lineno, { xfree (buffer); arg->r_opt = ARGPARSE_OUT_OF_CORE; - break; + goto leave; } } } @@ -852,130 +1406,554 @@ optfile_parse (FILE *fp, const char *filename, unsigned *lineno, else { arg->r_opt = ARGPARSE_OUT_OF_CORE; - break; + goto leave; } } } else if (i >= DIM(keyword)-1) { arg->r_opt = ARGPARSE_KEYWORD_TOO_LONG; - state = -1; /* Skip rest of line and leave. */ + state = Askipandleave; /* Skip rest of line and leave. */ + } + else if (!i) + { + state = c == '[' ? Acopymetacmd : Acopykeyword; + keyword[i++] = c; } else { keyword[i++] = c; - state = 2; } } + leave: + return arg->r_opt; +} + + +/* Return true if the list of options OPTS has any option marked with + * ARGPARSE_OPT_CONFFILE. */ +static int +any_opt_conffile (opttable_t *opts, unsigned int nopts) +{ + int i; + + for (i=0; i < nopts; i++ ) + if ((opts[i].flags & ARGPARSE_OPT_CONFFILE)) + return 1; + return 0; +} + + +/* Return true if FNAME is an absolute filename. */ +static int +is_absfname (const char *fname) +{ + const char *s; + +#ifdef HAVE_W32_SYSTEM + s = strchr (fname, ':'); + if (s) + s++; + else + s = fname; +#else + s = fname; +#endif + + return (*s == '/' +#ifdef HAVE_W32_SYSTEM + || *s == DIRSEP_C +#endif + ); +} + + +/* If FNAME specifies two files of the form + * NAME1:/NAME2 (Unix) + * or + * NAME1;[x:]/NAME2 (Windows) + * return a pointer to the delimiter or NULL if there is none. + */ +static const char * +is_twopartfname (const char *fname) +{ + const char *s; + + if ((s = strchr (fname, PATHSEP_C)) && is_absfname (s+1) && s != fname) + return s; + return NULL; +} + + +/* Try to use a version-ed config file name. A version-ed config file + * name is one which has the packages version number appended. For + * example if the standard config file name is "foo.conf" and the + * version of the foo program is 1.2.3-beta1 the following config + * files are tried in order until one is readable: + * + * foo.conf-1.2.3-beta1 + * foo.conf-1.2.3 + * foo.conf-1.2 + * foo.conf-1 + * foo.conf + * + * The argument CONFIGNAME should already be expanded. On success a + * newly allocated file name is returned. On error NULL is returned. + */ +static char * +try_versioned_conffile (const char *configname) +{ + const char *version = strusage (13); + char *name; + char *dash, *endp; + + if (!version || !*version) + return NULL; /* No program version known. */ + + name = strconcat (configname, "-", version, NULL); + if (!name) + return NULL; /* Oops: Out of core - ignore. */ + dash = name + strlen (configname); + + endp = dash + strlen (dash) - 1; + while (endp > dash) + { + if (!gnupg_access (name, R_OK)) + { + return name; + } + for (; endp > dash; endp--) + { + if (*endp == '-' || *endp == '.') + { + *endp = 0; + break; + } + } + } + + xfree (name); + return NULL; +} + + +/* This function is called after a sysconf file has been read. */ +static void +finish_read_sys (gnupg_argparse_t *arg) +{ + opttable_t *opts = arg->internal->opts; + unsigned int nopts = arg->internal->nopts; + int i; + + if (arg->internal->ignore_all_seen) + { + /* [ignore-all] was used: Set all options which have not + * explictly been set as ignore or not ignore to ignore. */ + for (i = 0; i < nopts; i++) + { + if (!opts[i].explicit_ignore) + opts[i].ignore = 1; + } + } + + /* Reset all flags which pertain only to sysconf files. */ + arg->internal->in_sysconf = 0; + arg->internal->user_active = 0; + arg->internal->mark_forced = 0; + arg->internal->mark_ignore = 0; + arg->internal->explicit_ignore = 0; + arg->internal->ignore_all_seen = 0; +} + +/* The full arg parser which handles option files and command line + * arguments. The behaviour depends on the combinations of CONFNAME + * and the ARGPARSE_FLAG_xxx values: + * + * | CONFNAME | SYS | USER | Action | + * |----------+-----+------+--------------------| + * | NULL | - | - | cmdline | + * | string | 0 | 1 | user, cmdline | + * | string | 1 | 0 | sys, cmdline | + * | string | 1 | 1 | sys, user, cmdline | + * + * Note that if an option has been flagged with ARGPARSE_OPT_CONFFILE + * and a type of ARGPARSE_TYPE_STRING that option is not returned but + * the specified configuration file is processed directly; if + * ARGPARSE_TYPE_NONE is used no user configuration files are + * processed and from the system configuration files only those which + * are immutable are processed. The string values for CONFNAME shall + * not include a directory part because that is taken from the values + * set by gnupg_set_confdir. However, if CONFNAME is a twopart + * filename delimited by a colon (semicolon on Windows) with the + * second part being an absolute filename, the first part is used for + * the SYS file and the the entire second part for the USER file. + */ +int +gnupg_argparser (gnupg_argparse_t *arg, gnupg_opt_t *opts, + const char *confname) +{ + /* First check whether releasing the resources has been requested. */ + if (arg && !opts) + { + deinitialize (arg); + return 0; + } + + /* Make sure that the internal data object is ready and also print + * warnings or errors from the last iteration. */ + if (initialize (arg, opts, NULL)) + return (arg->r_opt = ARGPARSE_OUT_OF_CORE); + + next_state: + switch (arg->internal->state) + { + case STATE_init: + if (arg->argc && arg->argv && *arg->argc + && any_opt_conffile (arg->internal->opts, arg->internal->nopts)) + { + /* The list of option allow for conf files + * (e.g. gpg's "--option FILE" and "--no-options") + * Now check whether one was really given on the command + * line. Note that we don't need to run this code if no + * argument array was provided. */ + int save_argc = *arg->argc; + char **save_argv = *arg->argv; + unsigned int save_flags = arg->flags; + int save_idx = arg->internal->idx; + int any_no_conffile = 0; + + arg->flags = (ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION + | ARGPARSE_FLAG__INITIALIZED); + while (arg_parse (arg, opts, 1)) + { + if ((arg->internal->opt_flags & ARGPARSE_OPT_CONFFILE)) + { + arg->internal->explicit_confopt = 1; + if ((arg->r_type & ARGPARSE_TYPE_MASK) == ARGPARSE_TYPE_STRING + && !arg->internal->explicit_conffile) + { + /* Store the first conffile name. All further + * conf file options are not handled. */ + arg->internal->explicit_conffile + = xtrystrdup (arg->r.ret_str); + if (!arg->internal->explicit_conffile) + return (arg->r_opt = ARGPARSE_OUT_OF_CORE); + + } + else if ((arg->r_type & ARGPARSE_TYPE_MASK) + == ARGPARSE_TYPE_NONE) + any_no_conffile = 1; + } + } + if (any_no_conffile) + { + /* A NoConffile option overrides any other conf file option. */ + xfree (arg->internal->explicit_conffile); + arg->internal->explicit_conffile = NULL; + } + /* Restore parser. */ + *arg->argc = save_argc; + *arg->argv = save_argv; + arg->flags = save_flags; + arg->internal->idx = save_idx; + } + + if (confname && *confname) + { + if ((arg->flags & ARGPARSE_FLAG_SYS)) + arg->internal->state = STATE_open_sys; + else if ((arg->flags & ARGPARSE_FLAG_USER)) + arg->internal->state = STATE_open_user; + else + return (arg->r_opt = ARGPARSE_INVALID_ARG); + } + else + arg->internal->state = STATE_open_cmdline; + goto next_state; + + case STATE_open_sys: + { + /* If it is a two part name take the first part. */ + const char *s; + char *tmpname = NULL; + + if ((s = is_twopartfname (confname))) + { + tmpname = xtrymalloc (s - confname + 1); + if (!tmpname) + return (arg->r_opt = ARGPARSE_OUT_OF_CORE); + memcpy (tmpname, confname, s-confname); + tmpname[s-confname] = 0; + s = tmpname; + } + else + s = confname; + xfree (arg->internal->confname); + arg->internal->confname = make_filename_try + (confdir.sys? confdir.sys : "/etc", s, NULL); + xfree (tmpname); + if (!arg->internal->confname) + return (arg->r_opt = ARGPARSE_OUT_OF_CORE); + } + arg->lineno = 0; + arg->internal->idx = 0; + arg->internal->verbose = 0; + arg->internal->stopped = 0; + arg->internal->inarg = 0; + gpgrt_fclose (arg->internal->conffp); + arg->internal->conffp = gpgrt_fopen (arg->internal->confname, "r"); + if (!arg->internal->conffp) + { + if ((arg->flags & ARGPARSE_FLAG_VERBOSE) || arg->internal->verbose) + log_info (_("Note: no default option file '%s'\n"), + arg->internal->confname); + if ((arg->flags & ARGPARSE_FLAG_USER)) + arg->internal->state = STATE_open_user; + else + arg->internal->state = STATE_open_cmdline; + goto next_state; + } + + if ((arg->flags & ARGPARSE_FLAG_VERBOSE) || arg->internal->verbose) + log_info (_("reading options from '%s'\n"), + arg->internal->confname); + arg->internal->state = STATE_read_sys; + arg->internal->in_sysconf = 1; + arg->r.ret_str = xtrystrdup (arg->internal->confname); + if (!arg->r.ret_str) + arg->r_opt = ARGPARSE_OUT_OF_CORE; + else + { + gpgrt_annotate_leaked_object (arg->r.ret_str); + arg->r_opt = ARGPARSE_CONFFILE; + arg->r_type = ARGPARSE_TYPE_STRING; + } + break; + + case STATE_open_user: + if (arg->internal->explicit_confopt + && arg->internal->explicit_conffile) + { + /* An explict option to use a specific configuration file + * has been given - use that one. */ + xfree (arg->internal->confname); + arg->internal->confname + = xtrystrdup (arg->internal->explicit_conffile); + if (!arg->internal->confname) + return (arg->r_opt = ARGPARSE_OUT_OF_CORE); + } + else if (arg->internal->explicit_confopt) + { + /* An explict option not to use a configuration file has + * been given - leap direct to command line reading. */ + arg->internal->state = STATE_open_cmdline; + goto next_state; + } + else + { + /* Use the standard configure file. If it is a two part + * name take the second part. If it is the standard name + * and ARGPARSE_FLAG_USERVERS is set try versioned config + * files. */ + const char *s; + char *nconf; + + xfree (arg->internal->confname); + if ((s = is_twopartfname (confname))) + { + arg->internal->confname = make_filename_try (s + 1, NULL); + if (!arg->internal->confname) + return (arg->r_opt = ARGPARSE_OUT_OF_CORE); + } + else + { + arg->internal->confname = make_filename_try + (confdir.user? confdir.user : "~/.config", confname, NULL); + if (!arg->internal->confname) + return (arg->r_opt = ARGPARSE_OUT_OF_CORE); + if ((arg->flags & ARGPARSE_FLAG_USERVERS) + && (nconf = try_versioned_conffile (arg->internal->confname))) + { + xfree (arg->internal->confname); + arg->internal->confname = nconf; + } + } + } + arg->lineno = 0; + arg->internal->idx = 0; + arg->internal->verbose = 0; + arg->internal->stopped = 0; + arg->internal->inarg = 0; + arg->internal->in_sysconf = 0; + gpgrt_fclose (arg->internal->conffp); + arg->internal->conffp = gpgrt_fopen (arg->internal->confname, "r"); + if (!arg->internal->conffp) + { + arg->internal->state = STATE_open_cmdline; + if (arg->internal->explicit_confopt) + { + log_error (_("option file '%s': %s\n"), + arg->internal->confname, strerror (errno)); + return (arg->r_opt = ARGPARSE_NO_CONFFILE); + } + else + { + if ((arg->flags & ARGPARSE_FLAG_VERBOSE) + || arg->internal->verbose) + log_info (_("Note: no default option file '%s'\n"), + arg->internal->confname); + goto next_state; + } + } + + if ((arg->flags & ARGPARSE_FLAG_VERBOSE) || arg->internal->verbose) + log_info (_("reading options from '%s'\n"), + arg->internal->confname); + arg->internal->state = STATE_read_user; + arg->r.ret_str = xtrystrdup (arg->internal->confname); + if (!arg->r.ret_str) + arg->r_opt = ARGPARSE_OUT_OF_CORE; + else + { + gpgrt_annotate_leaked_object (arg->r.ret_str); + arg->r_opt = ARGPARSE_CONFFILE; + arg->r_type = ARGPARSE_TYPE_STRING; + } + break; + + case STATE_open_cmdline: + gpgrt_fclose (arg->internal->conffp); + arg->internal->conffp = NULL; + xfree (arg->internal->confname); + arg->internal->confname = NULL; + arg->internal->idx = 0; + arg->internal->verbose = 0; + arg->internal->stopped = 0; + arg->internal->inarg = 0; + arg->internal->in_sysconf = 0; + if (!arg->argc || !arg->argv || !*arg->argv) + { + /* No or empty argument vector - don't bother to parse things. */ + arg->internal->state = STATE_finished; + goto next_state; + } + arg->r_opt = ARGPARSE_CONFFILE; + arg->r_type = ARGPARSE_TYPE_NONE; + arg->r.ret_str = NULL; + arg->internal->state = STATE_read_cmdline; + break; + + case STATE_read_sys: + arg->r_opt = gnupg_argparse (arg->internal->conffp, arg, opts); + if (!arg->r_opt) + { + finish_read_sys (arg); + arg->internal->state = STATE_open_user; + goto next_state; + } + if ((arg->internal->opt_flags & ARGPARSE_OPT_CONFFILE)) + goto next_state; /* Already handled - again. */ + break; + + case STATE_read_user: + arg->r_opt = gnupg_argparse (arg->internal->conffp, arg, opts); + if (!arg->r_opt) + { + arg->internal->state = STATE_open_cmdline; + goto next_state; + } + if ((arg->internal->opt_flags & ARGPARSE_OPT_CONFFILE)) + goto next_state; /* Already handled - again. */ + break; + + case STATE_read_cmdline: + arg->r_opt = arg_parse (arg, opts, 1); + if (!arg->r_opt) + { + arg->internal->state = STATE_finished; + goto next_state; + } + if ((arg->internal->opt_flags & ARGPARSE_OPT_CONFFILE)) + goto next_state; /* Already handled - again. */ + break; + + case STATE_finished: + arg->r_opt = 0; + break; + } + return arg->r_opt; } +/* Given the list of options in ARG and a keyword, return the index of + * the long option matching KEYWORD. On error -1 is returned for not + * found or -2 for ambigious keyword. */ static int -find_long_option( ARGPARSE_ARGS *arg, - ARGPARSE_OPTS *opts, const char *keyword ) +find_long_option (gnupg_argparse_t *arg, const char *keyword) { - int i; - size_t n; + int i; + size_t n; + opttable_t *opts = arg->internal->opts; + unsigned int nopts = arg->internal->nopts; - (void)arg; - - /* 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 - lookup table when this function is first invoked */ - 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; -#if 0 + /* 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 an option lookup table + * when this function is first invoked. The latter has already been + * done. */ + if (!*keyword) + return -1; + for (i=0; i < nopts; i++ ) + if (opts[i].long_opt && !strcmp (opts[i].long_opt, keyword)) + return i; + /* Not found. See whether it is an abbreviation. Aliases may not + * be abbreviated, though. */ + n = strlen (keyword); + for (i=0; i < nopts; i++) { - 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 */ + if (opts[i].long_opt && !strncmp (opts[i].long_opt, keyword, n)) + { + int j; + for (j=i+1; j < nopts; j++) + { + if (opts[j].long_opt + && !strncmp (opts[j].long_opt, keyword, n) + && !(opts[j].short_opt == opts[i].short_opt + && opts[j].flags == opts[i].flags ) ) + return -2; /* Abbreviation is ambiguous. */ } + return i; } } -#endif - /* 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 - && !strncmp( opts[j].long_opt, keyword, n ) - && !(opts[j].short_opt == opts[i].short_opt - && opts[j].flags == opts[i].flags ) ) - return -2; /* abbreviation is ambiguous */ - } - return i; - } - } - return -1; /* Not found. */ + return -1; /* Not found. */ } -int -arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts) + +/* The option parser for command line options. */ +static int +arg_parse (gnupg_argparse_t *arg, gnupg_opt_t *opts_orig, int no_init) { int idx; + opttable_t *opts; + unsigned int nopts; int argc; char **argv; char *s, *s2; int i; - /* 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", "@"); - int seen_help = 0; - int seen_version = 0; - int seen_warranty = 0; - int seen_dump_options = 0; + if (no_init) + ; + else if (initialize (arg, opts_orig, NULL)) + return (arg->r_opt = ARGPARSE_OUT_OF_CORE); - 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; - - initialize( arg, NULL, NULL ); + opts = arg->internal->opts; + nopts = arg->internal->nopts; argc = *arg->argc; argv = *arg->argv; - idx = arg->internal.idx; + idx = arg->internal->idx; if (!idx && argc && !(arg->flags & ARGPARSE_FLAG_ARG0)) { @@ -984,24 +1962,24 @@ arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts) } next_one: - if (!argc) + if (!argc || (s = *argv) == NULL) { /* No more args. */ arg->r_opt = 0; goto leave; /* Ready. */ } - s = *argv; - arg->internal.last = s; + arg->internal->last = s; + arg->internal->opt_flags = 0; - if (arg->internal.stopped && (arg->flags & ARGPARSE_FLAG_ALL)) + if (arg->internal->stopped && (arg->flags & ARGPARSE_FLAG_ALL)) { arg->r_opt = ARGPARSE_IS_ARG; /* Not an option but an argument. */ - arg->r_type = 2; + arg->r_type = ARGPARSE_TYPE_STRING; arg->r.ret_str = s; argc--; argv++; idx++; /* set to next one */ } - else if( arg->internal.stopped ) + else if (arg->internal->stopped) { arg->r_opt = 0; goto leave; /* Ready. */ @@ -1011,11 +1989,11 @@ arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts) /* Long option. */ char *argpos; - arg->internal.inarg = 0; + arg->internal->inarg = 0; if (!s[2] && !(arg->flags & ARGPARSE_FLAG_NOSTOP)) { /* Stop option processing. */ - arg->internal.stopped = 1; + arg->internal->stopped = 1; arg->flags |= ARGPARSE_FLAG_STOP_SEEN; argc--; argv++; idx++; goto next_one; @@ -1024,33 +2002,38 @@ arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts) argpos = strchr( s+2, '=' ); if ( argpos ) *argpos = 0; - i = find_long_option ( arg, opts, s+2 ); + i = find_long_option (arg, s+2); if ( argpos ) *argpos = '='; if (i > 0 && opts[i].short_opt == ARGPARSE_SHORTOPT_HELP) - show_help (opts, arg->flags); + { + show_help (opts, nopts, arg->flags); + my_exit (arg, 0); + } else if (i > 0 && opts[i].short_opt == ARGPARSE_SHORTOPT_VERSION) { if (!(arg->flags & ARGPARSE_FLAG_NOVERSION)) { show_version (); - exit(0); + my_exit (arg, 0); } } else if (i > 0 && opts[i].short_opt == ARGPARSE_SHORTOPT_WARRANTY) { writestrings (0, strusage (16), "\n", NULL); - exit (0); + my_exit (arg, 0); } + else if (i > 0 && opts[i].short_opt == ARGPARSE_SHORTOPT_DUMP_OPTTBL) + dump_option_table (arg); else if (i > 0 && opts[i].short_opt == ARGPARSE_SHORTOPT_DUMP_OPTIONS) { - for (i=0; opts[i].short_opt; i++ ) + for (i=0; i < nopts; i++ ) { if (opts[i].long_opt && !(opts[i].flags & ARGPARSE_OPT_IGNORE)) writestrings (0, "--", opts[i].long_opt, "\n", NULL); } - exit (0); + my_exit (arg, 0); } if ( i == -2 ) @@ -1062,6 +2045,7 @@ arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts) } else arg->r_opt = opts[i].short_opt; + if ( i < 0 ) ; else if ( (opts[i].flags & ARGPARSE_TYPE_MASK) ) @@ -1074,6 +2058,7 @@ arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts) } else s2 = argv[1]; + if ( !s2 && (opts[i].flags & ARGPARSE_OPT_OPTIONAL) ) { arg->r_type = ARGPARSE_TYPE_NONE; /* Argument is optional. */ @@ -1105,125 +2090,157 @@ arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts) if ( argpos ) arg->r_type = ARGPARSE_UNEXPECTED_ARG; else - arg->r_type = 0; + { + arg->internal->opt_flags = opts[i].flags; + arg->r_type = ARGPARSE_TYPE_NONE; + } } argc--; argv++; idx++; /* Set to next one. */ } - else if ( (*s == '-' && s[1]) || arg->internal.inarg ) - { - /* Short option. */ - int dash_kludge = 0; + else if ( (*s == '-' && s[1]) || arg->internal->inarg ) + { + /* Short option. */ + int dash_kludge = 0; - i = 0; - if ( !arg->internal.inarg ) - { - arg->internal.inarg++; - 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; - } - } - } - s += arg->internal.inarg; + i = 0; + if ( !arg->internal->inarg ) + { + arg->internal->inarg++; + if ( (arg->flags & ARGPARSE_FLAG_ONEDASH) ) + { + for (i=0; i < nopts; i++ ) + if ( opts[i].long_opt && !strcmp (opts[i].long_opt, s+1)) + { + dash_kludge = 1; + break; + } + } + } + s += arg->internal->inarg; - if (!dash_kludge ) - { - for (i=0; opts[i].short_opt; i++ ) - if ( opts[i].short_opt == *s ) - break; - } + if (!dash_kludge ) + { + for (i=0; i < nopts; i++ ) + if ( opts[i].short_opt == *s ) + break; + } - if ( !opts[i].short_opt && ( *s == 'h' || *s == '?' ) ) - show_help (opts, arg->flags); + if ( !opts[i].short_opt && ( *s == 'h' || *s == '?' ) ) + { + show_help (opts, nopts, arg->flags); + my_exit (arg, 0); + } - arg->r_opt = opts[i].short_opt; - if (!opts[i].short_opt ) - { - arg->r_opt = (opts[i].flags & ARGPARSE_OPT_COMMAND)? - ARGPARSE_INVALID_COMMAND:ARGPARSE_INVALID_OPTION; - arg->internal.inarg++; /* Point to the next arg. */ - arg->r.ret_str = s; - } - else if ( (opts[i].flags & ARGPARSE_TYPE_MASK) ) - { - if ( s[1] && !dash_kludge ) - { - s2 = s+1; - set_opt_arg (arg, opts[i].flags, s2); - } - else - { - s2 = argv[1]; - if ( !s2 && (opts[i].flags & ARGPARSE_OPT_OPTIONAL) ) - { - arg->r_type = ARGPARSE_TYPE_NONE; - } - else if ( !s2 ) - { - arg->r_opt = ARGPARSE_MISSING_ARG; - } - 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. */ - arg->internal.inarg = 0; - argc--; argv++; idx++; - } - } + arg->r_opt = opts[i].short_opt; + if (!opts[i].short_opt ) + { + arg->r_opt = (opts[i].flags & ARGPARSE_OPT_COMMAND)? + ARGPARSE_INVALID_COMMAND:ARGPARSE_INVALID_OPTION; + arg->internal->inarg++; /* Point to the next arg. */ + arg->r.ret_str = s; + } + else if ( (opts[i].flags & ARGPARSE_TYPE_MASK) ) + { + if ( s[1] && !dash_kludge ) + { + s2 = s+1; + set_opt_arg (arg, opts[i].flags, s2); + } + else + { + s2 = argv[1]; + if ( !s2 && (opts[i].flags & ARGPARSE_OPT_OPTIONAL) ) + { + arg->r_type = ARGPARSE_TYPE_NONE; + arg->internal->opt_flags = opts[i].flags; + } + else if ( !s2 ) + { + arg->r_opt = ARGPARSE_MISSING_ARG; + } + 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; + arg->internal->opt_flags = opts[i].flags; + } + 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->opt_flags = opts[i].flags; + arg->internal->inarg++; /* Point to the next arg. */ + } + if ( !s[1] || dash_kludge ) + { + /* No more concatenated short options. */ + arg->internal->inarg = 0; + argc--; argv++; idx++; + } + } else if ( arg->flags & ARGPARSE_FLAG_MIXED ) { arg->r_opt = ARGPARSE_IS_ARG; - arg->r_type = 2; + arg->r_type = ARGPARSE_TYPE_STRING; arg->r.ret_str = s; argc--; argv++; idx++; /* Set to next one. */ } else { - arg->internal.stopped = 1; /* Stop option processing. */ + arg->internal->stopped = 1; /* Stop option processing. */ goto next_one; } + if (arg->r_opt > 0 && i >= 0 && i < nopts + && ((opts[i].ignore && opts[i].explicit_ignore) || opts[i].forced)) + { + + if ((arg->flags & ARGPARSE_FLAG_WITHATTR)) + { + if (opts[i].ignore) + arg->r_type |= ARGPARSE_ATTR_IGNORE; + if (opts[i].forced) + arg->r_type |= ARGPARSE_ATTR_FORCE; + arg->r_type |= ARGPARSE_OPT_IGNORE; + } + else + { + log_info (_("Note: ignoring option \"--%s\"" + " due to global config\n"), + opts[i].long_opt); + goto next_one; /* Skip ignored/forced option. */ + } + } + leave: *arg->argc = argc; *arg->argv = argv; - arg->internal.idx = idx; + arg->internal->idx = idx; return arg->r_opt; } + /* Returns: -1 on error, 0 for an integer type and 1 for a non integer type argument. */ static int -set_opt_arg (ARGPARSE_ARGS *arg, unsigned flags, char *s) +set_opt_arg (gnupg_argparse_t *arg, unsigned flags, char *s) { int base = (flags & ARGPARSE_OPT_PREFIX)? 0 : 10; long l; + arg->internal->opt_flags = flags; switch ( (arg->r_type = (flags & ARGPARSE_TYPE_MASK)) ) { case ARGPARSE_TYPE_LONG: @@ -1272,8 +2289,10 @@ set_opt_arg (ARGPARSE_ARGS *arg, unsigned flags, char *s) } +/* Return the length of the option O. This needs to consider the + * description as well as the option name. */ static size_t -long_opt_strlen( ARGPARSE_OPTS *o ) +long_opt_strlen (opttable_t *o) { size_t n = strlen (o->long_opt); @@ -1286,8 +2305,8 @@ long_opt_strlen( ARGPARSE_OPTS *o ) if ( *s != '=' ) n++; /* For a (mostly) correct length calculation we exclude - continuation bytes (10xxxxxx) if we are on a native utf8 - terminal. */ + * continuation bytes (10xxxxxx) if we are on a native utf8 + * terminal. */ for (; *s && *s != '|'; s++ ) if ( is_utf8 && (*s&0xc0) != 0x80 ) n++; @@ -1296,22 +2315,35 @@ long_opt_strlen( ARGPARSE_OPTS *o ) } +/* Qsort compare for show_help. */ +static int +cmp_ordtbl (const void *a_v, const void *b_v) +{ + const unsigned short *a = a_v; + const unsigned short *b = b_v; + + return *a - *b; +} + + /**************** * 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 + * - a description which starts with a '@' and is followed by * any other characters is printed as is; this may be used for examples - * ans such. + * and such. This is a legacy methiod, moder codes uses the flags + * ARGPARSE_OPT_VERBATIM or ARGPARSE_OPT_HEADER. * - A description which starts with a '|' outputs the string between this * bar and the next one as arguments of the long option. */ static void -show_help (ARGPARSE_OPTS *opts, unsigned int flags) +show_help (opttable_t *opts, unsigned int nopts, unsigned int flags) { const char *s; char tmp[2]; + unsigned int *ordtbl = NULL; show_version (); writestrings (0, "\n", NULL); @@ -1325,30 +2357,68 @@ show_help (ARGPARSE_OPTS *opts, unsigned int flags) } s = strusage(41); writestrings (0, s, "\n", NULL); - if ( opts[0].description ) + if ( nopts ) { /* Auto format the option description. */ - int i,j, indent; + int i,j,indent; + const char *last_header = NULL; + + ordtbl = xtrycalloc (nopts, sizeof *ordtbl); + if (!ordtbl) + { + writestrings (1, "\nOoops: Out of memory whilst printing the help.\n", + NULL); + goto leave; + } /* Get max. length of long options. */ - for (i=indent=0; opts[i].short_opt; i++ ) + for (i=indent=0; i < nopts; 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; + ordtbl[i] = opts[i].ordinal; } + qsort (ordtbl, nopts, sizeof *ordtbl, cmp_ordtbl); + + /* The first option needs to have a description; if not do not + * print the help at all. */ + if (!opts[ordtbl[0]].description) + goto leave; + /* Example: " -v, --verbose Viele Sachen ausgeben" */ indent += 10; - if ( *opts[0].description != '@' ) + if ( *opts[ordtbl[0]].description != '@' + && !(opts[ordtbl[0]].flags + & (ARGPARSE_OPT_VERBATIM|ARGPARSE_OPT_HEADER))) writestrings (0, "Options:", "\n", NULL); - for (i=0; opts[i].short_opt; i++ ) + for (i=0; i < nopts; i++ ) { - s = map_static_macro_string (_( opts[i].description )); + s = map_fixed_string (_( opts[ordtbl[i]].description )); if ( s && *s== '@' && !s[1] ) /* Hide this line. */ continue; - if ( s && *s == '@' ) /* Unindented comment only line. */ + if ( s && (opts[ordtbl[i]].flags & ARGPARSE_OPT_HEADER)) + { + /* We delay printing until we have found one real output + * line. This avoids having a header above an empty + * section. */ + last_header = s; + continue; + } + if (last_header) + { + if (*last_header) + writestrings (0, "\n", last_header, ":\n", NULL); + last_header = NULL; + } + if ( s && (opts[ordtbl[i]].flags & ARGPARSE_OPT_VERBATIM)) + { + writestrings (0, s, NULL); + continue; + } + if ( s && *s == '@' ) /* Unindented legacy comment only line. */ { for (s++; *s; s++ ) { @@ -1369,12 +2439,12 @@ show_help (ARGPARSE_OPTS *opts, unsigned int flags) } j = 3; - if ( opts[i].short_opt < 256 ) + if ( opts[ordtbl[i]].short_opt < 256 ) { - tmp[0] = opts[i].short_opt; + tmp[0] = opts[ordtbl[i]].short_opt; tmp[1] = 0; writestrings (0, " -", tmp, NULL ); - if ( !opts[i].long_opt ) + if ( !opts[ordtbl[i]].long_opt ) { if (s && *s == '|' ) { @@ -1392,11 +2462,11 @@ show_help (ARGPARSE_OPTS *opts, unsigned int flags) } else writestrings (0, " ", NULL); - if ( opts[i].long_opt ) + if ( opts[ordtbl[i]].long_opt ) { - tmp[0] = opts[i].short_opt < 256?',':' '; + tmp[0] = opts[ordtbl[i]].short_opt < 256?',':' '; tmp[1] = 0; - j += writestrings (0, tmp, " --", opts[i].long_opt, NULL); + j += writestrings (0, tmp, " --", opts[ordtbl[i]].long_opt, NULL); if (s && *s == '|' ) { if ( *++s != '=' ) @@ -1456,10 +2526,13 @@ show_help (ARGPARSE_OPTS *opts, unsigned int flags) writestrings (0, "\n", NULL); writestrings (0, s, NULL); } + + leave: flushstrings (0); - exit(0); + xfree (ordtbl); } + static void show_version () { @@ -1495,6 +2568,165 @@ show_version () } +/* Print the table of options with flags etc. */ +static void +dump_option_table (gnupg_argparse_t *arg) +{ + opttable_t *opts; + unsigned int nopts; + const char *s; + char tmp[50]; + unsigned int *ordtbl = NULL; + int i; + + opts = arg->internal->opts; + nopts = arg->internal->nopts; + if (!nopts) + return; + + ordtbl = xtrycalloc (nopts, sizeof *ordtbl); + if (!ordtbl) + { + writestrings (1, "\nOoops: Out of memory whilst dumping the table.\n", + NULL); + flushstrings (1); + my_exit (arg, 2); + } + for (i=0; i < nopts; i++ ) + ordtbl[i] = opts[i].ordinal; + qsort (ordtbl, nopts, sizeof *ordtbl, cmp_ordtbl); + for (i=0; i < nopts; i++ ) + { + if (!opts[ordtbl[i]].long_opt) + continue; + writestrings (0, opts[ordtbl[i]].long_opt, ":", NULL); + snprintf (tmp, sizeof tmp, "%u:%u:", + opts[ordtbl[i]].short_opt, + opts[ordtbl[i]].flags); + writestrings (0, tmp, NULL); + s = opts[ordtbl[i]].description; + if (s) + { + for (; *s; s++) + { + if (*s == '%' || *s == ':' || *s == '\n') + snprintf (tmp, sizeof tmp, "%%%02X", *s); + else + { + tmp[0] = *s; + tmp[1] = 0; + } + writestrings (0, tmp, NULL); + } + } + writestrings (0, ":\n", NULL); + } + + flushstrings (0); + xfree (ordtbl); + my_exit (arg, 0); +} + + + +/* Level + * 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 + * 8: Return NULL for UTF-8 or string with the native charset. + * 9: Return the SPDX License tag. + * 10: Return license info string + * 11: Return the name of the program + * 12: Return optional name of package which includes this program. + * 13: version string + * 14: copyright string + * 15: Short copying conditions (with LFs) + * 16: Long copying conditions (with LFs) + * 17: Optional printable OS name + * 18: Optional thanks list (with LFs) + * 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) + * 42: Flag string: + * First char is '1': + * The short usage notes needs to be printed + * before the long usage note. + */ +const char * +strusage( int level ) +{ + const char *p = strusage_handler? strusage_handler(level) : NULL; + const char *tmp; + + if ( p ) + return map_static_macro_string (p); + + switch ( level ) + { + + case 8: break; /* Default to utf-8. */ + case 9: p = "GPL-3.0-or-later"; break; + case 10: + tmp = strusage (9); + if (tmp && !strcmp (tmp, "LGPL-2.1-or-later")) + p = ("License GNU LGPL-2.1-or-later "); + else /* Default to GPLv3+. */ + p =("License GNU GPL-3.0-or-later "); + break; + case 11: p = "foo"; break; + case 13: p = "0.0"; break; + case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break; + case 15: p = +"This is free software: you are free to change and redistribute it.\n" +"There is NO WARRANTY, to the extent permitted by law.\n"; + break; + case 16: + tmp = strusage (9); + if (tmp && !strcmp (tmp, "LGPL-2.1-or-later")) + p = +"This is free software; you can redistribute it and/or modify\n" +"it under the terms of the GNU Lesser General Public License as\n" +"published by the Free Software Foundation; either version 2.1 of\n" +"the License, or (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 Lesser General Public License for more details.\n\n" +"You should have received a copy of the GNU Lesser General Public License\n" +"along with this software. If not, see .\n"; + else /* Default */ + p = +"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" +"the Free Software Foundation; either version 3 of the License, or\n" +"(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" +"along with this software. If not, see .\n"; + break; + case 40: /* short and long usage */ + case 41: p = ""; break; + } + + return p; +} + + +/* Set the usage handler. This function is basically a constructor. */ +void +set_strusage ( const char *(*f)( int ) ) +{ + strusage_handler = f; +} + +#endif /* USE_INTERNAL_ARGPARSE */ + + void usage (int level) { @@ -1528,156 +2760,3 @@ usage (int level) exit (0); } } - -/* Level - * 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. - * 13: version string - * 14: copyright string - * 15: Short copying conditions (with LFs) - * 16: Long copying conditions (with LFs) - * 17: Optional printable OS name - * 18: Optional thanks list (with LFs) - * 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) - * 42: Flag string: - * First char is '1': - * The short usage notes needs to be printed - * before the long usage note. - */ -const char * -strusage( int level ) -{ - const char *p = strusage_handler? strusage_handler(level) : NULL; - - if ( p ) - return map_static_macro_string (p); - - switch ( level ) - { - - case 10: -#if ARGPARSE_GPL_VERSION == 3 - p = ("License GPLv3+: GNU GPL version 3 or later " - ""); -#else - p = ("License GPLv2+: GNU GPL version 2 or later " - ""); -#endif - break; - case 11: p = "foo"; break; - case 13: p = "0.0"; break; - case 14: p = ARGPARSE_CRIGHT_STR; break; - case 15: p = -"This is free software: you are free to change and redistribute it.\n" -"There is NO WARRANTY, to the extent permitted by law.\n"; - break; - case 16: p = -"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" -"the Free Software Foundation; either version " -ARGPARSE_STR2(ARGPARSE_GPL_VERSION) -" of the License, or\n" -"(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" -"along with this software. If not, see .\n"; - break; - case 40: /* short and long usage */ - case 41: p = ""; break; - } - - return p; -} - - -/* Set the usage handler. This function is basically a constructor. */ -void -set_strusage ( const char *(*f)( int ) ) -{ - strusage_handler = f; -} - - -#ifdef TEST -static struct { - int verbose; - int debug; - char *outfile; - char *crf; - int myopt; - int echo; - int a_long_one; -} opt; - -int -main(int argc, char **argv) -{ - ARGPARSE_OPTS opts[] = { - ARGPARSE_x('v', "verbose", NONE, 0, "Laut sein"), - ARGPARSE_s_n('e', "echo" , ("Zeile ausgeben, damit wir sehen, " - "was wir eingegeben haben")), - 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" ), - /* Note that on a non-utf8 terminal the ß might garble the output. */ - 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 ), - ARGPARSE_end() - }; - ARGPARSE_ARGS pargs = { &argc, &argv, (ARGPARSE_FLAG_ALL - | ARGPARSE_FLAG_MIXED - | ARGPARSE_FLAG_ONEDASH) }; - int i; - - 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; - } - } - 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; -} -#endif /*TEST*/ - -/**** bottom of file ****/ diff --git a/common/argparse.h b/common/argparse.h index cdd18d9a2..282aaea32 100644 --- a/common/argparse.h +++ b/common/argparse.h @@ -32,7 +32,15 @@ #define GNUPG_COMMON_ARGPARSE_H #include +#include +#if GPGRT_VERSION_NUMBER < 0x012600 /* 1.38 */ + +#define USE_INTERNAL_ARGPARSE 1 + +/* We use a copy of the code from the new gpgrt parser. */ + +struct _argparse_internal_s; typedef struct { int *argc; /* Pointer to ARGC (value subject to change). */ @@ -42,7 +50,7 @@ typedef struct int err; /* Print error description for last option. Either 0, ARGPARSE_PRINT_WARNING or ARGPARSE_PRINT_ERROR. */ - + unsigned int lineno;/* The current line number. */ int r_opt; /* Returns option code. */ int r_type; /* Returns type of option value. */ union { @@ -52,16 +60,9 @@ typedef struct char *ret_str; } r; /* Return values */ - struct { - int idx; - int inarg; - int stopped; - const char *last; - void *aliases; - const void *cur_alias; - void *iio_list; - } internal; /* Private - do not change. */ -} ARGPARSE_ARGS; + struct _argparse_internal_s *internal; +} gnupg_argparse_t; + typedef struct { @@ -69,7 +70,11 @@ typedef struct const char *long_opt; unsigned int flags; const char *description; /* Optional option description. */ -} ARGPARSE_OPTS; +} gnupg_opt_t; + + +typedef gnupg_argparse_t ARGPARSE_ARGS; +typedef gnupg_opt_t ARGPARSE_OPTS; /* Short options. */ #define ARGPARSE_SHORTOPT_HELP 32768 @@ -87,8 +92,14 @@ typedef struct #define ARGPARSE_FLAG_ARG0 16 /* Do not skip the first arg. */ #define ARGPARSE_FLAG_ONEDASH 32 /* Allow long options with one dash. */ #define ARGPARSE_FLAG_NOVERSION 64 /* No output for "--version". */ - +#define ARGPARSE_FLAG_RESET 128 /* Request to reset the internal state. */ #define ARGPARSE_FLAG_STOP_SEEN 256 /* Set to true if a "--" has been seen. */ +#define ARGPARSE_FLAG_NOLINENO 512 /* Do not zero the lineno field. */ +#define ARGPARSE_FLAG_SYS 1024 /* Use system config file. */ +#define ARGPARSE_FLAG_USER 2048 /* Use user config file. */ +#define ARGPARSE_FLAG_VERBOSE 4096 /* Print additional argparser info. */ +#define ARGPARSE_FLAG_USERVERS 8192 /* Try version-ed user config files. */ +#define ARGPARSE_FLAG_WITHATTR 16384 /* Return attribute bits. */ /* Flags for each option (ARGPARSE_OPTS). The type code may be ORed with the OPT flags. */ @@ -101,6 +112,11 @@ typedef struct #define ARGPARSE_OPT_PREFIX (1<<4) /* Allow 0x etc. prefixed values. */ #define ARGPARSE_OPT_IGNORE (1<<6) /* Ignore command or option. */ #define ARGPARSE_OPT_COMMAND (1<<7) /* The argument is a command. */ +#define ARGPARSE_OPT_CONFFILE (1<<8) /* The value is a conffile. */ +#define ARGPARSE_OPT_HEADER (1<<9) /* The value is printed as a header. */ +#define ARGPARSE_OPT_VERBATIM (1<<10)/* The value is printed verbatim. */ +#define ARGPARSE_ATTR_FORCE (1<<14)/* Attribute force is set. */ +#define ARGPARSE_ATTR_IGNORE (1<<15)/* Attribute ignore is set. */ #define ARGPARSE_TYPE_MASK 7 /* Mask for the type values (internal). */ @@ -169,19 +185,32 @@ typedef struct #define ARGPARSE_c(s,l,d) \ { (s), (l), (ARGPARSE_TYPE_NONE | ARGPARSE_OPT_COMMAND), (d) } +#define ARGPARSE_conffile(s,l,d) \ + { (s), (l), (ARGPARSE_TYPE_STRING|ARGPARSE_OPT_CONFFILE), (d) } + +#define ARGPARSE_noconffile(s,l,d) \ + { (s), (l), (ARGPARSE_TYPE_NONE|ARGPARSE_OPT_CONFFILE), (d) } + #define ARGPARSE_ignore(s,l) \ { (s), (l), (ARGPARSE_OPT_IGNORE), "@" } #define ARGPARSE_group(s,d) \ { (s), NULL, 0, (d) } -/* Placeholder options for help, version, warranty and dump-options. See arg_parse(). */ +/* Verbatim print the string D in the help output. It does not make + * use of the "@" hack as ARGPARSE_group does. */ +#define ARGPARSE_verbatim(d) \ + { 1, NULL, (ARGPARSE_OPT_VERBATIM), (d) } + +/* Same as ARGPARSE_verbatim but also print a colon and a LF. N can + * be used give a symbolic name to the header. Nothing is printed if + * D is the empty string. */ +#define ARGPARSE_header(n,d) \ + { 1, (n), (ARGPARSE_OPT_HEADER), (d) } + +/* Mark the end of the list (mandatory). */ #define ARGPARSE_end() \ - { 0, NULL, 0, NULL }, \ - { 0, NULL, 0, NULL }, \ - { 0, NULL, 0, NULL }, \ - { 0, NULL, 0, NULL }, \ - { 0, NULL, 0, NULL } + { 0, NULL, 0, NULL } /* Other constants. */ @@ -202,14 +231,51 @@ typedef struct #define ARGPARSE_INVALID_ALIAS (-10) #define ARGPARSE_OUT_OF_CORE (-11) #define ARGPARSE_INVALID_ARG (-12) +#define ARGPARSE_PERMISSION_ERROR (-13) +#define ARGPARSE_NO_CONFFILE (-14) +#define ARGPARSE_CONFFILE (-15) +#define ARGPARSE_INVALID_META (-16) +#define ARGPARSE_UNKNOWN_META (-17) +#define ARGPARSE_UNEXPECTED_META (-18) +/* Values used for gnupg_set_confdir. */ +#define GNUPG_CONFDIR_USER 1 /* The user's configuration dir. */ +#define GNUPG_CONFDIR_SYS 2 /* The systems's configuration dir. */ + +/* Take care: gpgrt_argparse keeps state in ARG and requires that + * either ARGPARSE_FLAG_RESET is used after OPTS has been changed or + * gpgrt_argparse (NULL, ARG, NULL) is called first. */ +int gnupg_argparse (gpgrt_stream_t fp, + gnupg_argparse_t *arg, gnupg_opt_t *opts); +int gnupg_argparser (gnupg_argparse_t *arg, gnupg_opt_t *opts, + const char *confname); -int arg_parse (ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts); -int optfile_parse (FILE *fp, const char *filename, unsigned *lineno, - ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts); -void usage (int level); const char *strusage (int level); void set_strusage (const char *(*f)( int )); -void argparse_register_outfnc (int (*fnc)(int, const char *)); +void gnupg_set_usage_outfnc (int (*f)(int, const char *)); +void gnupg_set_fixed_string_mapper (const char *(*f)(const char*)); +void gnupg_set_confdir (int what, const char *name); + +#else /* !USE_INTERNAL_ARGPARSE */ + +#define GNUPG_CONFDIR_USER GPGRT_CONFDIR_USER +#define GNUPG_CONFDIR_SYS GPGRT_CONFDIR_SYS + +typedef gpgrt_argparse_t gnupg_argparse_t; +typedef gpgrt_opt_t gnupg_opt_t; +typedef gpgrt_argparse_t ARGPARSE_ARGS; +typedef gpgrt_opt_t ARGPARSE_OPTS; + +#define gnupg_argparse(a,b,c) gpgrt_argparse ((a),(b),(c)) +#define gnupg_argparser(a,b,c) gpgrt_argparser ((a),(b),(c)) +#define strusage(a) gpgrt_strusage (a) +#define set_strusage(a) gpgrt_set_strusage (a) +#define gnupg_set_usage_outfnc(a) gpgrt_set_usage_outfnc ((a)) +#define gnupg_set_fixed_string_mapper(a) gpgrt_set_fixed_string_mapper ((a)) +#define gnupg_set_confdir(a,b) gpgrt_set_confdir ((a),(b)) + +#endif /* !USE_INTERNAL_ARGPARSE */ + +void usage (int level); #endif /*GNUPG_COMMON_ARGPARSE_H*/ diff --git a/common/init.c b/common/init.c index 86b71e5ee..bcd023676 100644 --- a/common/init.c +++ b/common/init.c @@ -210,7 +210,10 @@ _init_common_subsystems (gpg_err_source_t errsource, int *argcp, char ***argvp) } /* --version et al shall use estream as well. */ - argparse_register_outfnc (writestring_via_estream); + gnupg_set_usage_outfnc (writestring_via_estream); + + /* Register our string mapper with gpgrt. */ + gnupg_set_fixed_string_mapper (map_static_macro_string); /* Logging shall use the standard socket directory as fallback. */ log_set_socket_dir_cb (gnupg_socketdir); diff --git a/configure.ac b/configure.ac index 4234b0928..6835a4681 100644 --- a/configure.ac +++ b/configure.ac @@ -510,6 +510,22 @@ AH_BOTTOM([ #define GNUPG_PRIVATE_KEYS_DIR "private-keys-v1.d" #define GNUPG_OPENPGP_REVOC_DIR "openpgp-revocs.d" +/* GnuPG has always been a part of the GNU project and thus we have + * shown the FSF as holder of the copyright. We continue to do so for + * the reason that without the FSF the free software used all over the + * world would not have come into existence. However, under Windows + * we print a different copyright string with --version because the + * copyright assignments of g10 Code and Werner Koch were terminated + * many years ago, g10 Code is still the major contributor to the + * code, and Windows is not an FSF endorsed platform. Note that the + * actual list of copyright holders can be found in the AUTHORS file. */ +#ifdef HAVE_W32_SYSTEM +#define GNUPG_DEF_COPYRIGHT_LINE "Copyright (C) 2020 g10 Code GmbH" +#else +#define GNUPG_DEF_COPYRIGHT_LINE "Copyright (C) 2020 Free Software Foundation, Inc." +#endif + + /* For some systems (DOS currently), we hardcode the path here. For POSIX systems the values are constructed by the Makefiles, so that the values may be overridden by the make invocations; this is to @@ -563,6 +579,9 @@ AH_BOTTOM([ /* Provide the es_ macro for estream. */ #define GPGRT_ENABLE_ES_MACROS 1 +/* We want the argparse macros from gpgrt. */ +#define GPGRT_ENABLE_ARGPARSE_MACROS 1 + /* Tell libgcrypt not to use its own libgpg-error implementation. */ #define USE_LIBGPG_ERROR 1 diff --git a/dirmngr/dirmngr-client.c b/dirmngr/dirmngr-client.c index 0d714d485..e4df476d7 100644 --- a/dirmngr/dirmngr-client.c +++ b/dirmngr/dirmngr-client.c @@ -16,6 +16,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, see . + * SPDX-License-Identifier: GPL-3.0-or-later */ #include @@ -158,9 +159,11 @@ my_strusage (int level) switch(level) { + case 9: p = "GPL-3.0-or-later"; break; case 11: p = "dirmngr-client (@GNUPG@)"; break; case 13: p = VERSION; break; + case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break; case 17: p = PRINTABLE_OS_NAME; break; case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break; case 49: p = PACKAGE_BUGREPORT; break; @@ -202,6 +205,9 @@ main (int argc, char **argv ) set_strusage (my_strusage); log_set_prefix ("dirmngr-client", GPGRT_LOG_WITH_PREFIX); + /* Register our string mapper. Usually done in + * init_common_subsystems, but we don't use that here. */ + gnupg_set_fixed_string_mapper (map_static_macro_string); /* For W32 we need to initialize the socket subsystem. Because we don't use Pth we need to do this explicit. */ @@ -223,8 +229,8 @@ main (int argc, char **argv ) /* Parse the command line. */ pargs.argc = &argc; pargs.argv = &argv; - pargs.flags= 1; /* Do not remove the args. */ - while (arg_parse (&pargs, opts) ) + pargs.flags= ARGPARSE_FLAG_KEEP; + while (gnupg_argparse (NULL, &pargs, opts)) { switch (pargs.r_opt) { @@ -247,9 +253,11 @@ main (int argc, char **argv ) break; case oForceDefaultResponder: opt.force_default_responder = 1; break; - default : pargs.err = 2; break; + default : pargs.err = ARGPARSE_PRINT_ERROR; break; } } + gnupg_argparse (NULL, &pargs, NULL); /* Release internal state. */ + if (log_get_errorcount (0)) exit (2); diff --git a/dirmngr/dirmngr.c b/dirmngr/dirmngr.c index 509cb9e3e..dcfc7a8ff 100644 --- a/dirmngr/dirmngr.c +++ b/dirmngr/dirmngr.c @@ -1,6 +1,6 @@ /* dirmngr.c - Keyserver and X.509 LDAP access * Copyright (C) 2002 Klarälvdalens Datakonsult AB - * Copyright (C) 2003, 2004, 2006, 2007, 2008, 2010, 2011 g10 Code GmbH + * Copyright (C) 2003, 2004, 2006, 2007, 2008, 2010, 2011, 2020 g10 Code GmbH * Copyright (C) 2014 Werner Koch * * This file is part of GnuPG. @@ -17,8 +17,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, see . - * - * SPDX-License-Identifier: GPL-3.0+ + * SPDX-License-Identifier: GPL-3.0-or-later */ #include @@ -183,7 +182,7 @@ static ARGPARSE_OPTS opts[] = { ARGPARSE_s_n (oQuiet, "quiet", N_("be somewhat more quiet")), ARGPARSE_s_n (oSh, "sh", N_("sh-style command output")), ARGPARSE_s_n (oCsh, "csh", N_("csh-style command output")), - ARGPARSE_s_s (oOptions, "options", N_("|FILE|read options from FILE")), + ARGPARSE_conffile (oOptions, "options", N_("|FILE|read options from FILE")), ARGPARSE_s_s (oDebugLevel, "debug-level", N_("|LEVEL|set the debugging level to LEVEL")), ARGPARSE_s_n (oNoDetach, "no-detach", N_("do not detach from the console")), @@ -261,6 +260,7 @@ static ARGPARSE_OPTS opts[] = { ARGPARSE_s_i (oConnectTimeout, "connect-timeout", "@"), ARGPARSE_s_i (oConnectQuickTimeout, "connect-quick-timeout", "@"), ARGPARSE_s_i (oListenBacklog, "listen-backlog", "@"), + ARGPARSE_noconffile (oNoOptions, "no-options", "@"), ARGPARSE_group (302,N_("@\n(See the \"info\" manual for a complete listing " "of all commands and options)\n")), @@ -392,9 +392,11 @@ my_strusage( int level ) const char *p; switch ( level ) { + case 9: p = "GPL-3.0-or-later"; break; case 11: p = "@DIRMNGR@ (@GNUPG@)"; break; case 13: p = VERSION; break; + case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break; case 17: p = PRINTABLE_OS_NAME; break; /* TRANSLATORS: @EMAIL@ will get replaced by the actual bug reporting address. This is so that we can change the @@ -828,12 +830,10 @@ main (int argc, char **argv) ARGPARSE_ARGS pargs; int orig_argc; char **orig_argv; - FILE *configfp = NULL; - char *configname = NULL; + char *last_configname = NULL; + const char *configname = NULL; const char *shell; - unsigned configlineno; - int parse_debug = 0; - int default_config =1; + int debug_argparser = 0; int greeting = 0; int nogreeting = 0; int nodetach = 0; @@ -915,63 +915,56 @@ main (int argc, char **argv) orig_argv = argv; pargs.argc = &argc; pargs.argv = &argv; - pargs.flags= 1|(1<<6); /* do not remove the args, ignore version */ - while (arg_parse( &pargs, opts)) + pargs.flags= (ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION); + while (gnupg_argparse (NULL, &pargs, opts)) { - if (pargs.r_opt == oDebug || pargs.r_opt == oDebugAll) - parse_debug++; - else if (pargs.r_opt == oOptions) - { /* Yes there is one, so we do not try the default one, but - read the option file when it is encountered at the - commandline */ - default_config = 0; - } - else if (pargs.r_opt == oNoOptions) - default_config = 0; /* --no-options */ - else if (pargs.r_opt == oHomedir) + switch (pargs.r_opt) { + case oDebug: + case oDebugAll: + debug_argparser++; + break; + case oHomedir: gnupg_set_homedir (pargs.r.ret_str); + break; } } + /* Reset the flags. */ + pargs.flags &= ~(ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION); socket_name = dirmngr_socket_name (); - if (default_config) - configname = make_filename (gnupg_homedir (), DIRMNGR_NAME".conf", NULL ); + /* The configuraton directories for use by gpgrt_argparser. */ + gnupg_set_confdir (GNUPG_CONFDIR_SYS, gnupg_sysconfdir ()); + gnupg_set_confdir (GNUPG_CONFDIR_USER, gnupg_homedir ()); + + /* We are re-using the struct, thus the reset flag. We OR the + * flags so that the internal intialized flag won't be cleared. */ argc = orig_argc; argv = orig_argv; pargs.argc = &argc; pargs.argv = &argv; - pargs.flags= 1; /* do not remove the args */ - next_pass: - if (configname) + pargs.flags |= (ARGPARSE_FLAG_RESET + | ARGPARSE_FLAG_KEEP + | ARGPARSE_FLAG_SYS + | ARGPARSE_FLAG_USER); + while (gnupg_argparser (&pargs, opts, DIRMNGR_NAME EXTSEP_S "conf")) { - configlineno = 0; - configfp = gnupg_fopen (configname, "r"); - if (!configfp) + if (pargs.r_opt == ARGPARSE_CONFFILE) { - if (default_config) + if (debug_argparser) + log_info (_("reading options from '%s'\n"), + pargs.r_type? pargs.r.ret_str: "[cmdline]"); + if (pargs.r_type) { - if( parse_debug ) - log_info (_("Note: no default option file '%s'\n"), - configname ); - } + xfree (last_configname); + last_configname = xstrdup (pargs.r.ret_str); + configname = last_configname; + } else - { - log_error (_("option file '%s': %s\n"), - configname, strerror(errno) ); - exit(2); - } - xfree (configname); - configname = NULL; - } - if (parse_debug && configname ) - log_info (_("reading options from '%s'\n"), configname ); - default_config = 0; - } - - while (optfile_parse( configfp, configname, &configlineno, &pargs, opts) ) - { + configname = NULL; + continue; + } if (parse_rereadable_options (&pargs, 0)) continue; /* Already handled */ switch (pargs.r_opt) @@ -996,18 +989,8 @@ main (int argc, char **argv) case oDebugWait: debug_wait = pargs.r.ret_int; break; - case oOptions: - /* Config files may not be nested (silently ignore them) */ - if (!configfp) - { - xfree(configname); - configname = xstrdup(pargs.r.ret_str); - goto next_pass; - } - break; case oNoGreeting: nogreeting = 1; break; case oNoVerbose: opt.verbose = 0; break; - case oNoOptions: break; /* no-options */ case oHomedir: /* Ignore this option here. */; break; case oNoDetach: nodetach = 1; break; case oLogFile: logfile = pargs.r.ret_str; break; @@ -1035,20 +1018,26 @@ main (int argc, char **argv) listen_backlog = pargs.r.ret_int; break; - default : pargs.err = configfp? 1:2; break; + default: + if (configname) + pargs.err = ARGPARSE_PRINT_WARNING; + else + pargs.err = ARGPARSE_PRINT_ERROR; + break; } } - if (configfp) + gnupg_argparse (NULL, &pargs, NULL); /* Release internal state. */ + + if (!last_configname) + opt.config_filename = make_filename (gnupg_homedir (), + DIRMNGR_NAME EXTSEP_S "conf", + NULL); + else { - fclose (configfp); - configfp = NULL; - /* Keep a copy of the name so that it can be read on SIGHUP. */ - opt.config_filename = configname; - configname = NULL; - goto next_pass; + opt.config_filename = last_configname; + last_configname = NULL; } - xfree (configname); - configname = NULL; + if (log_get_errorcount(0)) exit(2); if (nogreeting ) @@ -1479,17 +1468,6 @@ main (int argc, char **argv) char *filename; char *filename_esc; - /* First the configuration file. This is not an option, but it - is vital information for GPG Conf. */ - if (!opt.config_filename) - opt.config_filename = make_filename (gnupg_homedir (), - "dirmngr.conf", NULL ); - - filename = percent_escape (opt.config_filename, NULL); - es_printf ("gpgconf-dirmngr.conf:%lu:\"%s\n", - GC_OPT_FLAG_DEFAULT, filename); - xfree (filename); - es_printf ("verbose:%lu:\n", flags | GC_OPT_FLAG_NONE); es_printf ("quiet:%lu:\n", flags | GC_OPT_FLAG_NONE); es_printf ("debug-level:%lu:\"none\n", flags | GC_OPT_FLAG_DEFAULT); @@ -1543,7 +1521,6 @@ main (int argc, char **argv) filename_esc); xfree (filename_esc); - es_printf ("nameserver:%lu:\n", flags | GC_OPT_FLAG_NONE); es_printf ("resolver-timeout:%lu:%u\n", flags | GC_OPT_FLAG_DEFAULT, 0); @@ -1833,36 +1810,39 @@ static void reread_configuration (void) { ARGPARSE_ARGS pargs; - FILE *fp; - unsigned int configlineno = 0; + char *twopart; int dummy; if (!opt.config_filename) return; /* No config file. */ - fp = gnupg_fopen (opt.config_filename, "r"); - if (!fp) - { - log_error (_("option file '%s': %s\n"), - opt.config_filename, strerror(errno) ); - return; - } + twopart = strconcat (DIRMNGR_NAME EXTSEP_S "conf" PATHSEP_S, + opt.config_filename, NULL); + if (!twopart) + return; /* Out of core. */ parse_rereadable_options (NULL, 1); /* Start from the default values. */ memset (&pargs, 0, sizeof pargs); dummy = 0; pargs.argc = &dummy; - pargs.flags = 1; /* do not remove the args */ - while (optfile_parse (fp, opt.config_filename, &configlineno, &pargs, opts) ) + pargs.flags = (ARGPARSE_FLAG_KEEP + |ARGPARSE_FLAG_SYS + |ARGPARSE_FLAG_USER); + while (gnupg_argparser (&pargs, opts, twopart)) { - if (pargs.r_opt < -1) - pargs.err = 1; /* Print a warning. */ + if (pargs.r_opt == ARGPARSE_CONFFILE) + { + log_info (_("reading options from '%s'\n"), + pargs.r_type? pargs.r.ret_str: "[cmdline]"); + } + else if (pargs.r_opt < -1) + pargs.err = ARGPARSE_PRINT_WARNING; else /* Try to parse this option - ignore unchangeable ones. */ parse_rereadable_options (&pargs, 1); } - fclose (fp); - + gnupg_argparse (NULL, &pargs, NULL); /* Release internal state. */ + xfree (twopart); post_option_parsing (); } diff --git a/dirmngr/dirmngr_ldap.c b/dirmngr/dirmngr_ldap.c index 8452c3ba0..804959008 100644 --- a/dirmngr/dirmngr_ldap.c +++ b/dirmngr/dirmngr_ldap.c @@ -16,6 +16,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, see . + * SPDX-License-Identifier: GPL-3.0-or-later */ #include @@ -197,9 +198,11 @@ my_strusage (int level) switch(level) { + case 9: p = "GPL-3.0-or-later"; break; case 11: p = "dirmngr_ldap (@GNUPG@)"; break; case 13: p = VERSION; break; + case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break; case 17: p = PRINTABLE_OS_NAME; break; case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break; case 49: p = PACKAGE_BUGREPORT; break; @@ -267,8 +270,8 @@ ldap_wrapper_main (char **argv, estream_t outstream) /* Parse the command line. */ pargs.argc = &argc; pargs.argv = &argv; - pargs.flags= 1; /* Do not remove the args. */ - while (arg_parse (&pargs, opts) ) + pargs.flags= ARGPARSE_FLAG_KEEP; + while (gnupg_argparse (NULL, &pargs, opts)) { switch (pargs.r_opt) { @@ -309,6 +312,7 @@ ldap_wrapper_main (char **argv, estream_t outstream) break; } } + gnupg_argparse (NULL, &pargs, NULL); /* Release internal state. */ if (only_search_timeout) myopt->alarm_timeout = 0; diff --git a/g10/gpg.c b/g10/gpg.c index a3ada64c8..431c516b9 100644 --- a/g10/gpg.c +++ b/g10/gpg.c @@ -1,7 +1,7 @@ /* gpg.c - The GnuPG utility (main for gpg) - * Copyright (C) 1998-2019 Free Software Foundation, Inc. + * Copyright (C) 1998-2020 Free Software Foundation, Inc. * Copyright (C) 1997-2019 Werner Koch - * Copyright (C) 2015-2019 g10 Code GmbH + * Copyright (C) 2015-2020 g10 Code GmbH * * This file is part of GnuPG. * @@ -17,6 +17,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, see . + * SPDX-License-Identifier: GPL-3.0-or-later */ #include @@ -634,7 +635,7 @@ static ARGPARSE_OPTS opts[] = { ARGPARSE_s_s (oDisplayCharset, "display-charset", "@"), ARGPARSE_s_s (oDisplayCharset, "charset", "@"), - ARGPARSE_s_s (oOptions, "options", "@"), + ARGPARSE_conffile (oOptions, "options", "@"), ARGPARSE_s_s (oDebug, "debug", "@"), ARGPARSE_s_s (oDebugLevel, "debug-level", "@"), @@ -737,7 +738,7 @@ static ARGPARSE_OPTS opts[] = { ARGPARSE_s_n (oNoDefKeyring, "no-default-keyring", "@"), ARGPARSE_s_n (oNoKeyring, "no-keyring", "@"), ARGPARSE_s_n (oNoGreeting, "no-greeting", "@"), - ARGPARSE_s_n (oNoOptions, "no-options", "@"), + ARGPARSE_noconffile (oNoOptions, "no-options", "@"), ARGPARSE_s_s (oHomedir, "homedir", "@"), ARGPARSE_s_n (oNoBatch, "no-batch", "@"), ARGPARSE_s_n (oWithColons, "with-colons", "@"), @@ -1058,10 +1059,13 @@ my_strusage( int level ) static char *digests, *pubkeys, *ciphers, *zips, *ver_gcry; const char *p; - switch( level ) { + switch (level) + { + case 9: p = "GPL-3.0-or-later"; break; case 11: p = "@GPG@ (@GNUPG@)"; break; case 13: p = VERSION; break; + case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break; case 17: p = PRINTABLE_OS_NAME; break; case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break; @@ -2301,13 +2305,11 @@ main (int argc, char **argv) strlist_t nrings = NULL; armor_filter_context_t *afx = NULL; int detached_sig = 0; - FILE *configfp = NULL; - char *configname = NULL; - char *save_configname = NULL; - char *default_configname = NULL; - unsigned configlineno; - int parse_debug = 0; - int default_config = 1; + char *last_configname = NULL; + const char *configname = NULL; /* NULL or points to last_configname. + * NULL also indicates that we are + * processing options from the cmdline. */ + int debug_argparser = 0; int default_keyring = 1; int greeting = 0; int nogreeting = 0; @@ -2430,41 +2432,42 @@ main (int argc, char **argv) opt.emit_version = 0; opt.weak_digests = NULL; - /* Check whether we have a config file on the command line. */ + /* Check special options given on the command line. */ orig_argc = argc; orig_argv = argv; pargs.argc = &argc; pargs.argv = &argv; pargs.flags= (ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION); - while( arg_parse( &pargs, opts) ) { - if( pargs.r_opt == oDebug || pargs.r_opt == oDebugAll ) - parse_debug++; - else if (pargs.r_opt == oDebugIOLBF) - es_setvbuf (es_stdout, NULL, _IOLBF, 0); - else if( pargs.r_opt == oOptions ) { - /* yes there is one, so we do not try the default one, but - * read the option file when it is encountered at the commandline - */ - default_config = 0; - } - else if( pargs.r_opt == oNoOptions ) + while (gnupg_argparse (NULL, &pargs, opts)) + { + switch (pargs.r_opt) { - default_config = 0; /* --no-options */ + case oDebug: + case oDebugAll: + debug_argparser++; + break; + + case oDebugIOLBF: + es_setvbuf (es_stdout, NULL, _IOLBF, 0); + break; + + case oNoOptions: + /* Set here here because the homedir would otherwise be + * created before main option parsing starts. */ opt.no_homedir_creation = 1; + break; + + case oHomedir: + gnupg_set_homedir (pargs.r.ret_str); + break; + + case oNoPermissionWarn: + opt.no_perm_warn = 1; + break; } - else if( pargs.r_opt == oHomedir ) - gnupg_set_homedir (pargs.r.ret_str); - else if( pargs.r_opt == oNoPermissionWarn ) - opt.no_perm_warn=1; - else if (pargs.r_opt == oStrict ) - { - /* Not used */ - } - else if (pargs.r_opt == oNoStrict ) - { - /* Not used */ - } - } + } + /* Reset the flags. */ + pargs.flags &= ~(ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION); #ifdef HAVE_DOSISH_SYSTEM if ( strchr (gnupg_homedir (), '\\') ) { @@ -2508,64 +2511,67 @@ main (int argc, char **argv) additional_weak_digest ("MD5"); parse_auto_key_locate (DEFAULT_AKL_LIST); - /* Try for a version specific config file first */ - default_configname = get_default_configname (); - if (default_config) - configname = xstrdup (default_configname); - argc = orig_argc; argv = orig_argv; pargs.argc = &argc; pargs.argv = &argv; - pargs.flags= ARGPARSE_FLAG_KEEP; + /* We are re-using the struct, thus the reset flag. We OR the + * flags so that the internal intialized flag won't be cleared. */ + pargs.flags |= (ARGPARSE_FLAG_RESET + | ARGPARSE_FLAG_KEEP + | ARGPARSE_FLAG_SYS + | ARGPARSE_FLAG_USER + | ARGPARSE_FLAG_USERVERS); /* By this point we have a homedir, and cannot change it. */ check_permissions (gnupg_homedir (), 0); - next_pass: - if( configname ) { - if(check_permissions(configname,1)) - { - /* If any options file is unsafe, then disable any external - programs for keyserver calls or photo IDs. Since the - external program to call is set in the options file, a - unsafe options file can lead to an arbitrary program - being run. */ + /* The configuraton directories for use by gpgrt_argparser. */ + gnupg_set_confdir (GNUPG_CONFDIR_SYS, gnupg_sysconfdir ()); + gnupg_set_confdir (GNUPG_CONFDIR_USER, gnupg_homedir ()); - opt.exec_disable=1; - } - - configlineno = 0; - configfp = gnupg_fopen( configname, "r" ); - if (configfp && is_secured_file (fileno (configfp))) - { - fclose (configfp); - configfp = NULL; - gpg_err_set_errno (EPERM); - } - if( !configfp ) { - if( default_config ) { - if( parse_debug ) - log_info(_("Note: no default option file '%s'\n"), - configname ); - } - else { - log_error(_("option file '%s': %s\n"), - configname, strerror(errno) ); - g10_exit(2); - } - xfree(configname); configname = NULL; - } - if( parse_debug && configname ) - log_info(_("reading options from '%s'\n"), configname ); - default_config = 0; - } - - while( optfile_parse( configfp, configname, &configlineno, - &pargs, opts) ) + while (gnupg_argparser (&pargs, opts, GPG_NAME EXTSEP_S "conf")) { - switch( pargs.r_opt ) + switch (pargs.r_opt) { + case ARGPARSE_CONFFILE: + if (debug_argparser) + log_info (_("reading options from '%s'\n"), + pargs.r_type? pargs.r.ret_str: "[cmdline]"); + if (pargs.r_type) + { + xfree (last_configname); + last_configname = xstrdup (pargs.r.ret_str); + configname = last_configname; + if (is_secured_filename (configname)) + { + pargs.r_opt = ARGPARSE_PERMISSION_ERROR; + pargs.err = ARGPARSE_PRINT_ERROR; + } + else if (strncmp (configname, gnupg_sysconfdir (), + strlen (gnupg_sysconfdir ()))) + { + /* This is not the global config file and thus we + * need to check the permissions: If the file is + * unsafe, then disable any external programs for + * keyserver calls or photo IDs. Since the + * external program to call is set in the options + * file, a unsafe options file can lead to an + * arbitrary program being run. */ + if (check_permissions (configname, 1)) + opt.exec_disable=1; + } + } + else + configname = NULL; + break; + + /* case oOptions: + * case oNoOptions: + * We will never see these options here because + * gpgrt_argparse handles them for us. + */ + case aListConfig: case aListGcryptConfig: case aGPGConfList: @@ -2702,25 +2708,25 @@ main (int argc, char **argv) break; case oNoUseAgent: - obsolete_option (configname, configlineno, "no-use-agent"); + obsolete_option (configname, pargs.lineno, "no-use-agent"); break; case oGpgAgentInfo: - obsolete_option (configname, configlineno, "gpg-agent-info"); + obsolete_option (configname, pargs.lineno, "gpg-agent-info"); break; case oReaderPort: - obsolete_scdaemon_option (configname, configlineno, "reader-port"); + obsolete_scdaemon_option (configname, pargs.lineno, "reader-port"); break; case octapiDriver: - obsolete_scdaemon_option (configname, configlineno, "ctapi-driver"); + obsolete_scdaemon_option (configname, pargs.lineno, "ctapi-driver"); break; case opcscDriver: - obsolete_scdaemon_option (configname, configlineno, "pcsc-driver"); + obsolete_scdaemon_option (configname, pargs.lineno, "pcsc-driver"); break; case oDisableCCID: - obsolete_scdaemon_option (configname, configlineno, "disable-ccid"); + obsolete_scdaemon_option (configname, pargs.lineno, "disable-ccid"); break; case oHonorHttpProxy: - obsolete_option (configname, configlineno, "honor-http-proxy"); + obsolete_option (configname, pargs.lineno, "honor-http-proxy"); break; case oAnswerYes: opt.answer_yes = 1; break; @@ -2731,7 +2737,7 @@ main (int argc, char **argv) sl->flags = KEYDB_RESOURCE_FLAG_PRIMARY; break; case oShowKeyring: - deprecated_warning(configname,configlineno,"--show-keyring", + deprecated_warning(configname,pargs.lineno,"--show-keyring", "--list-options ","show-keyring"); opt.list_options|=LIST_SHOW_KEYRING; break; @@ -2803,14 +2809,6 @@ main (int argc, char **argv) /* Ignore this old option. */ break; - case oOptions: - /* config files may not be nested (silently ignore them) */ - if( !configfp ) { - xfree(configname); - configname = xstrdup(pargs.r.ret_str); - goto next_pass; - } - break; case oNoArmor: opt.no_armor=1; opt.armor=0; break; case oNoDefKeyring: @@ -2843,7 +2841,7 @@ main (int argc, char **argv) case oDefaultKey: sl = add_to_strlist (&opt.def_secret_key, pargs.r.ret_str); sl->flags = (pargs.r_opt << PK_LIST_SHIFT); - if (configfp) + if (configname) sl->flags |= PK_LIST_CONFIG; break; case oDefRecipient: @@ -2861,7 +2859,6 @@ main (int argc, char **argv) xfree(opt.def_recipient); opt.def_recipient = NULL; opt.def_recipient_self = 0; break; - case oNoOptions: opt.no_homedir_creation = 1; break; /* no-options */ case oHomedir: break; case oNoBatch: opt.batch = 0; break; @@ -2893,7 +2890,7 @@ main (int argc, char **argv) opt.tofu_default_policy = parse_tofu_policy (pargs.r.ret_str); break; case oTOFUDBFormat: - obsolete_option (configname, configlineno, "tofu-db-format"); + obsolete_option (configname, pargs.lineno, "tofu-db-format"); break; case oForceOwnertrust: @@ -2951,17 +2948,17 @@ main (int argc, char **argv) case oSigPolicyURL: add_policy_url(pargs.r.ret_str,0); break; case oCertPolicyURL: add_policy_url(pargs.r.ret_str,1); break; case oShowPolicyURL: - deprecated_warning(configname,configlineno,"--show-policy-url", + deprecated_warning(configname,pargs.lineno,"--show-policy-url", "--list-options ","show-policy-urls"); - deprecated_warning(configname,configlineno,"--show-policy-url", + deprecated_warning(configname,pargs.lineno,"--show-policy-url", "--verify-options ","show-policy-urls"); opt.list_options|=LIST_SHOW_POLICY_URLS; opt.verify_options|=VERIFY_SHOW_POLICY_URLS; break; case oNoShowPolicyURL: - deprecated_warning(configname,configlineno,"--no-show-policy-url", + deprecated_warning(configname,pargs.lineno,"--no-show-policy-url", "--list-options ","no-show-policy-urls"); - deprecated_warning(configname,configlineno,"--no-show-policy-url", + deprecated_warning(configname,pargs.lineno,"--no-show-policy-url", "--verify-options ","no-show-policy-urls"); opt.list_options&=~LIST_SHOW_POLICY_URLS; opt.verify_options&=~VERIFY_SHOW_POLICY_URLS; @@ -2978,7 +2975,7 @@ main (int argc, char **argv) append_to_strlist(&opt.comments,pargs.r.ret_str); break; case oDefaultComment: - deprecated_warning(configname,configlineno, + deprecated_warning(configname,pargs.lineno, "--default-comment","--no-comments",""); /* fall through */ case oNoComments: @@ -2988,17 +2985,17 @@ main (int argc, char **argv) case oThrowKeyids: opt.throw_keyids = 1; break; case oNoThrowKeyids: opt.throw_keyids = 0; break; case oShowPhotos: - deprecated_warning(configname,configlineno,"--show-photos", + deprecated_warning(configname,pargs.lineno,"--show-photos", "--list-options ","show-photos"); - deprecated_warning(configname,configlineno,"--show-photos", + deprecated_warning(configname,pargs.lineno,"--show-photos", "--verify-options ","show-photos"); opt.list_options|=LIST_SHOW_PHOTOS; opt.verify_options|=VERIFY_SHOW_PHOTOS; break; case oNoShowPhotos: - deprecated_warning(configname,configlineno,"--no-show-photos", + deprecated_warning(configname,pargs.lineno,"--no-show-photos", "--list-options ","no-show-photos"); - deprecated_warning(configname,configlineno,"--no-show-photos", + deprecated_warning(configname,pargs.lineno,"--no-show-photos", "--verify-options ","no-show-photos"); opt.list_options&=~LIST_SHOW_PHOTOS; opt.verify_options&=~VERIFY_SHOW_PHOTOS; @@ -3029,7 +3026,7 @@ main (int argc, char **argv) * enough space for the flags. */ sl = add_to_strlist2( &remusr, pargs.r.ret_str, utf8_strings ); sl->flags = (pargs.r_opt << PK_LIST_SHIFT); - if (configfp) + if (configname) sl->flags |= PK_LIST_CONFIG; if (pargs.r_opt == oHiddenRecipient || pargs.r_opt == oHiddenRecipientFile) @@ -3045,7 +3042,7 @@ main (int argc, char **argv) /* Store an additional recipient. */ sl = add_to_strlist2( &remusr, pargs.r.ret_str, utf8_strings ); sl->flags = ((pargs.r_opt << PK_LIST_SHIFT) | PK_LIST_ENCRYPT_TO); - if (configfp) + if (configname) sl->flags |= PK_LIST_CONFIG; if (pargs.r_opt == oHiddenEncryptTo) sl->flags |= PK_LIST_HIDDEN; @@ -3055,7 +3052,7 @@ main (int argc, char **argv) opt.no_encrypt_to = 1; break; case oEncryptToDefaultKey: - opt.encrypt_to_default_key = configfp ? 2 : 1; + opt.encrypt_to_default_key = configname ? 2 : 1; break; case oTrySecretKey: @@ -3101,7 +3098,7 @@ main (int argc, char **argv) case oLocalUser: /* store the local users */ sl = add_to_strlist2( &locusr, pargs.r.ret_str, utf8_strings ); sl->flags = (pargs.r_opt << PK_LIST_SHIFT); - if (configfp) + if (configname) sl->flags |= PK_LIST_CONFIG; break; case oSender: @@ -3241,7 +3238,7 @@ main (int argc, char **argv) { if(configname) log_error(_("%s:%d: invalid keyserver options\n"), - configname,configlineno); + configname,pargs.lineno); else log_error(_("invalid keyserver options\n")); } @@ -3251,7 +3248,7 @@ main (int argc, char **argv) { if(configname) log_error(_("%s:%d: invalid import options\n"), - configname,configlineno); + configname,pargs.lineno); else log_error(_("invalid import options\n")); } @@ -3266,7 +3263,7 @@ main (int argc, char **argv) { if(configname) log_error(_("%s:%d: invalid export options\n"), - configname,configlineno); + configname,pargs.lineno); else log_error(_("invalid export options\n")); } @@ -3281,7 +3278,7 @@ main (int argc, char **argv) { if(configname) log_error(_("%s:%d: invalid list options\n"), - configname,configlineno); + configname,pargs.lineno); else log_error(_("invalid list options\n")); } @@ -3321,7 +3318,7 @@ main (int argc, char **argv) { if(configname) log_error(_("%s:%d: invalid verify options\n"), - configname,configlineno); + configname,pargs.lineno); else log_error(_("invalid verify options\n")); } @@ -3342,17 +3339,17 @@ main (int argc, char **argv) case oCertNotation: add_notation_data( pargs.r.ret_str, 1 ); break; case oKnownNotation: register_known_notation (pargs.r.ret_str); break; case oShowNotation: - deprecated_warning(configname,configlineno,"--show-notation", + deprecated_warning(configname,pargs.lineno,"--show-notation", "--list-options ","show-notations"); - deprecated_warning(configname,configlineno,"--show-notation", + deprecated_warning(configname,pargs.lineno,"--show-notation", "--verify-options ","show-notations"); opt.list_options|=LIST_SHOW_NOTATIONS; opt.verify_options|=VERIFY_SHOW_NOTATIONS; break; case oNoShowNotation: - deprecated_warning(configname,configlineno,"--no-show-notation", + deprecated_warning(configname,pargs.lineno,"--no-show-notation", "--list-options ","no-show-notations"); - deprecated_warning(configname,configlineno,"--no-show-notation", + deprecated_warning(configname,pargs.lineno,"--no-show-notation", "--verify-options ","no-show-notations"); opt.list_options&=~LIST_SHOW_NOTATIONS; opt.verify_options&=~VERIFY_SHOW_NOTATIONS; @@ -3408,7 +3405,7 @@ main (int argc, char **argv) ovrseskeyfd = translate_sys2libc_fd_int (pargs.r.ret_int, 0); break; case oMergeOnly: - deprecated_warning(configname,configlineno,"--merge-only", + deprecated_warning(configname,pargs.lineno,"--merge-only", "--import-options ","merge-only"); opt.import_options|=IMPORT_MERGE_ONLY; break; @@ -3536,7 +3533,7 @@ main (int argc, char **argv) { if(configname) log_error(_("%s:%d: invalid auto-key-locate list\n"), - configname,configlineno); + configname,pargs.lineno); else log_error(_("invalid auto-key-locate list\n")); } @@ -3558,7 +3555,7 @@ main (int argc, char **argv) if (configname) log_info("%s:%d: WARNING: gpg not built with large secure " "memory buffer. Ignoring enable-large-rsa\n", - configname,configlineno); + configname,pargs.lineno); else log_info("WARNING: gpg not built with large secure " "memory buffer. Ignoring --enable-large-rsa\n"); @@ -3620,7 +3617,7 @@ main (int argc, char **argv) case oNoop: break; default: - if (configfp) + if (configname) pargs.err = ARGPARSE_PRINT_WARNING; else { @@ -3634,19 +3631,8 @@ main (int argc, char **argv) } } - if (configfp) - { - fclose( configfp ); - configfp = NULL; - /* Remember the first config file name. */ - if (!save_configname) - save_configname = configname; - else - xfree(configname); - configname = NULL; - goto next_pass; - } - xfree(configname); configname = NULL; + gnupg_argparse (NULL, &pargs, NULL); /* Release internal state. */ + if (log_get_errorcount (0)) { write_status_failure ("option-parser", gpg_error(GPG_ERR_GENERAL)); @@ -3657,11 +3643,11 @@ main (int argc, char **argv) directly after the option parsing. */ if (cmd == aGPGConfList) { - gpgconf_list (save_configname ? save_configname : default_configname); + gpgconf_list (last_configname ? last_configname : "UNKNOWN"); g10_exit (0); } - xfree (save_configname); - xfree (default_configname); + xfree (last_configname); + last_configname = NULL; if (print_dane_records) log_error ("invalid option \"%s\"; use \"%s\" instead\n", diff --git a/g10/gpgv.c b/g10/gpgv.c index 9be3eff0b..6b9c88d40 100644 --- a/g10/gpgv.c +++ b/g10/gpgv.c @@ -1,6 +1,7 @@ /* gpgv.c - The GnuPG signature verify utility - * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2005, 2006, - * 2008, 2009, 2012 Free Software Foundation, Inc. + * Copyright (C) 1998-2020 Free Software Foundation, Inc. + * Copyright (C) 1998-2019 Werner Koch + * Copyright (C) 2015-2020 g10 Code GmbH * * This file is part of GnuPG. * @@ -16,6 +17,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, see . + * SPDX-License-Identifier: GPL-3.0-or-later */ #include @@ -138,9 +140,11 @@ my_strusage( int level ) switch (level) { + case 9: p = "GPL-3.0-or-later"; break; case 11: p = "@GPG@v (GnuPG)"; break; case 13: p = VERSION; break; + case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break; case 17: p = PRINTABLE_OS_NAME; break; case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break; @@ -172,7 +176,6 @@ main( int argc, char **argv ) int rc=0; strlist_t sl; strlist_t nrings = NULL; - unsigned configlineno; ctrl_t ctrl; early_system_init (); @@ -206,11 +209,13 @@ main( int argc, char **argv ) pargs.argc = &argc; pargs.argv = &argv; - pargs.flags= 1; /* do not remove the args */ - while (optfile_parse( NULL, NULL, &configlineno, &pargs, opts)) + pargs.flags= ARGPARSE_FLAG_KEEP; + while (gnupg_argparser (&pargs, opts, NULL)) { switch (pargs.r_opt) { + case ARGPARSE_CONFFILE: break; + case oQuiet: opt.quiet = 1; break; case oVerbose: opt.verbose++; @@ -250,6 +255,8 @@ main( int argc, char **argv ) } } + gnupg_argparse (NULL, &pargs, NULL); /* Release internal state. */ + if (log_get_errorcount (0)) g10_exit(2); diff --git a/g13/g13-syshelp.c b/g13/g13-syshelp.c index e6755d575..65d5c25ae 100644 --- a/g13/g13-syshelp.c +++ b/g13/g13-syshelp.c @@ -15,6 +15,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, see . + * SPDX-License-Identifier: GPL-3.0-or-later */ #include @@ -145,9 +146,11 @@ my_strusage( int level ) switch (level) { + case 9: p = "GPL-3.0-or-later"; break; case 11: p = "@G13@-syshelp (@GNUPG@)"; break; case 13: p = VERSION; break; + case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break; case 17: p = PRINTABLE_OS_NAME; break; case 19: p = _("Please report bugs to <" PACKAGE_BUGREPORT ">.\n"); break; @@ -225,12 +228,10 @@ main ( int argc, char **argv) gpg_error_t err = 0; /* const char *fname; */ int may_coredump; - FILE *configfp = NULL; - char *configname = NULL; - unsigned configlineno; - int parse_debug = 0; + char *last_configname = NULL; + const char *configname = NULL; + int debug_argparser = 0; int no_more_options = 0; - int default_config =1; char *logfile = NULL; /* int debug_wait = 0; */ int use_random_seed = 1; @@ -265,30 +266,32 @@ main ( int argc, char **argv) log_fatal ("error allocating session environment block: %s\n", strerror (errno)); - /* Fixme: We enable verbose mode here because there is currently no - way to do this when starting g13-syshelp. To fix that we should - add a g13-syshelp.conf file in /etc/gnupg. */ - opt.verbose = 1; - /* First check whether we have a debug option on the commandline. */ orig_argc = argc; orig_argv = argv; pargs.argc = &argc; pargs.argv = &argv; pargs.flags= (ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION); - while (arg_parse( &pargs, opts)) + while (gnupg_argparse (NULL, &pargs, opts)) { - if (pargs.r_opt == oDebug || pargs.r_opt == oDebugAll) - parse_debug++; + switch (pargs.r_opt) + { + case oDebug: + case oDebugAll: + debug_argparser++; + break; + } } + /* Reset the flags. */ + pargs.flags &= ~(ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION); /* Initialize the secure memory. */ gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0); maybe_setuid = 0; /* - Now we are now working under our real uid - */ + * Now we are now working under our real uid + */ /* Setup malloc hooks. */ { @@ -311,47 +314,40 @@ main ( int argc, char **argv) ctrl.no_server = 1; ctrl.status_fd = -1; /* No status output. */ - if (default_config ) - configname = make_filename (gnupg_sysconfdir (), - G13_NAME"-syshelp.conf", NULL); + /* The configuraton directories for use by gpgrt_argparser. */ + gnupg_set_confdir (GNUPG_CONFDIR_SYS, gnupg_sysconfdir ()); + gnupg_set_confdir (GNUPG_CONFDIR_USER, gnupg_homedir ()); argc = orig_argc; argv = orig_argv; pargs.argc = &argc; pargs.argv = &argv; - pargs.flags = 1; /* Do not remove the args. */ - - next_pass: - if (configname) - { - configlineno = 0; - configfp = gnupg_fopen (configname, "r"); - if (!configfp) - { - if (default_config) - { - if (parse_debug) - log_info (_("NOTE: no default option file '%s'\n"), configname); - } - else - { - log_error (_("option file '%s': %s\n"), - configname, strerror(errno)); - g13_exit(2); - } - xfree (configname); - configname = NULL; - } - if (parse_debug && configname) - log_info (_("reading options from '%s'\n"), configname); - default_config = 0; - } + pargs.flags |= (ARGPARSE_FLAG_RESET + | ARGPARSE_FLAG_KEEP + | ARGPARSE_FLAG_SYS + | ARGPARSE_FLAG_USER); while (!no_more_options - && optfile_parse (configfp, configname, &configlineno, &pargs, opts)) + && gnupg_argparser (&pargs, opts, G13_NAME"-syshelp" EXTSEP_S "conf")) { switch (pargs.r_opt) { + case ARGPARSE_CONFFILE: + { + if (debug_argparser) + log_info (_("reading options from '%s'\n"), + pargs.r_type? pargs.r.ret_str: "[cmdline]"); + if (pargs.r_type) + { + xfree (last_configname); + last_configname = xstrdup (pargs.r.ret_str); + configname = last_configname; + } + else + configname = NULL; + } + break; + case oQuiet: opt.quiet = 1; break; case oDryRun: opt.dry_run = 1; break; @@ -404,26 +400,21 @@ main ( int argc, char **argv) case oNoRandomSeedFile: use_random_seed = 0; break; default: - pargs.err = configfp? ARGPARSE_PRINT_WARNING:ARGPARSE_PRINT_ERROR; + pargs.err = configname? ARGPARSE_PRINT_WARNING:ARGPARSE_PRINT_ERROR; break; } } + gnupg_argparse (NULL, &pargs, NULL); /* Release internal state. */ - if (configfp) - { - fclose (configfp); - configfp = NULL; - /* Keep a copy of the config filename. */ - opt.config_filename = configname; - configname = NULL; - goto next_pass; - } - xfree (configname); - configname = NULL; - - if (!opt.config_filename) + if (!last_configname) opt.config_filename = make_filename (gnupg_homedir (), - G13_NAME".conf", NULL); + G13_NAME"-syshelp" EXTSEP_S "conf", + NULL); + else + { + opt.config_filename = last_configname; + last_configname = NULL; + } if (log_get_errorcount(0)) g13_exit(2); diff --git a/g13/g13.c b/g13/g13.c index c4cd34b2a..6500916cc 100644 --- a/g13/g13.c +++ b/g13/g13.c @@ -15,6 +15,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, see . + * SPDX-License-Identifier: GPL-3.0-or-later */ #include @@ -138,7 +139,7 @@ static ARGPARSE_OPTS opts[] = { ARGPARSE_s_n (oDryRun, "dry-run", N_("do not make any changes")), - ARGPARSE_s_s (oOptions, "options", N_("|FILE|read options from FILE")), + ARGPARSE_conffile (oOptions, "options", N_("|FILE|read options from FILE")), ARGPARSE_s_s (oDebug, "debug", "@"), ARGPARSE_s_s (oDebugLevel, "debug-level", @@ -163,7 +164,7 @@ static ARGPARSE_OPTS opts[] = { ARGPARSE_s_n (oNoVerbose, "no-verbose", "@"), ARGPARSE_s_n (oNoSecmemWarn, "no-secmem-warning", "@"), ARGPARSE_s_n (oNoGreeting, "no-greeting", "@"), - ARGPARSE_s_n (oNoOptions, "no-options", "@"), + ARGPARSE_noconffile (oNoOptions, "no-options", "@"), ARGPARSE_s_s (oHomedir, "homedir", "@"), ARGPARSE_s_s (oAgentProgram, "agent-program", "@"), ARGPARSE_s_s (oGpgProgram, "gpg-program", "@"), @@ -235,9 +236,11 @@ my_strusage( int level ) switch (level) { + case 9: p = "GPL-3.0-or-later"; break; case 11: p = "@G13@ (@GNUPG@)"; break; case 13: p = VERSION; break; + case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break; case 17: p = PRINTABLE_OS_NAME; break; case 19: p = _("Please report bugs to <" PACKAGE_BUGREPORT ">.\n"); break; @@ -343,12 +346,10 @@ main ( int argc, char **argv) gpg_error_t err = 0; /* const char *fname; */ int may_coredump; - FILE *configfp = NULL; - char *configname = NULL; - unsigned configlineno; - int parse_debug = 0; + char *last_configname = NULL; + const char *configname = NULL; + int debug_argparser = 0; int no_more_options = 0; - int default_config =1; char *logfile = NULL; int greeting = 0; int nogreeting = 0; @@ -360,8 +361,6 @@ main ( int argc, char **argv) struct server_control_s ctrl; strlist_t recipients = NULL; - /*mtrace();*/ - early_system_init (); gnupg_reopen_std (G13_NAME); set_strusage (my_strusage); @@ -395,29 +394,31 @@ main ( int argc, char **argv) pargs.argc = &argc; pargs.argv = &argv; pargs.flags= 1|(1<<6); /* Do not remove the args, ignore version. */ - while (arg_parse( &pargs, opts)) + pargs.flags= (ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION); + while (gnupg_argparse (NULL, &pargs, opts)) { - if (pargs.r_opt == oDebug || pargs.r_opt == oDebugAll) - parse_debug++; - else if (pargs.r_opt == oOptions) - { /* Yes, there is one, so we do not try the default one but - read the config file when it is encountered at the - commandline. */ - default_config = 0; - } - else if (pargs.r_opt == oNoOptions) - default_config = 0; /* --no-options */ - else if (pargs.r_opt == oHomedir) - gnupg_set_homedir (pargs.r.ret_str); + switch (pargs.r_opt) + { + case oDebug: + case oDebugAll: + debug_argparser++; + break; + + case oHomedir: + gnupg_set_homedir (pargs.r.ret_str); + break; + } } + /* Reset the flags. */ + pargs.flags &= ~(ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION); /* Initialize the secure memory. */ gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0); maybe_setuid = 0; /* - Now we are now working under our real uid - */ + * Now we are now working under our real uid + */ /* Setup malloc hooks. */ { @@ -440,47 +441,39 @@ main ( int argc, char **argv) ctrl.no_server = 1; ctrl.status_fd = -1; /* No status output. */ - /* Set the default option file */ - if (default_config ) - configname = make_filename (gnupg_homedir (), G13_NAME".conf", NULL); + /* The configuraton directories for use by gpgrt_argparser. */ + gnupg_set_confdir (GNUPG_CONFDIR_SYS, gnupg_sysconfdir ()); + gnupg_set_confdir (GNUPG_CONFDIR_USER, gnupg_homedir ()); argc = orig_argc; argv = orig_argv; pargs.argc = &argc; pargs.argv = &argv; - pargs.flags = 1; /* Do not remove the args. */ - - next_pass: - if (configname) - { - configlineno = 0; - configfp = gnupg_fopen (configname, "r"); - if (!configfp) - { - if (default_config) - { - if (parse_debug) - log_info (_("Note: no default option file '%s'\n"), configname); - } - else - { - log_error (_("option file '%s': %s\n"), - configname, strerror(errno)); - g13_exit(2); - } - xfree (configname); - configname = NULL; - } - if (parse_debug && configname) - log_info (_("reading options from '%s'\n"), configname); - default_config = 0; - } - + pargs.flags |= (ARGPARSE_FLAG_RESET + | ARGPARSE_FLAG_KEEP + | ARGPARSE_FLAG_SYS + | ARGPARSE_FLAG_USER); while (!no_more_options - && optfile_parse (configfp, configname, &configlineno, &pargs, opts)) + && gnupg_argparser (&pargs, opts, G13_NAME EXTSEP_S "conf")) { switch (pargs.r_opt) { + case ARGPARSE_CONFFILE: + { + if (debug_argparser) + log_info (_("reading options from '%s'\n"), + pargs.r_type? pargs.r.ret_str: "[cmdline]"); + if (pargs.r_type) + { + xfree (last_configname); + last_configname = xstrdup (pargs.r.ret_str); + configname = last_configname; + } + else + configname = NULL; + } + break; + case aGPGConfList: case aGPGConfTest: set_cmd (&cmd, pargs.r_opt); @@ -538,17 +531,6 @@ main ( int argc, char **argv) case oStatusFD: ctrl.status_fd = pargs.r.ret_int; break; case oLoggerFD: log_set_fd (pargs.r.ret_int ); break; - case oNoOptions: break; /* no-options */ - case oOptions: - /* Config files may not be nested (silently ignore them). */ - if (!configfp) - { - xfree(configname); - configname = xstrdup (pargs.r.ret_str); - goto next_pass; - } - break; - case oHomedir: gnupg_set_homedir (pargs.r.ret_str); break; case oAgentProgram: opt.agent_program = pargs.r.ret_str; break; @@ -592,12 +574,16 @@ main ( int argc, char **argv) break; default: - pargs.err = configfp? ARGPARSE_PRINT_WARNING:ARGPARSE_PRINT_ERROR; + if (configname) + pargs.err = ARGPARSE_PRINT_WARNING; + else + pargs.err = ARGPARSE_PRINT_ERROR; break; } } + gnupg_argparse (NULL, &pargs, NULL); /* Release internal state. */ - /* XXX Construct GPG arguments. */ + /* Construct GPG arguments. */ { strlist_t last; last = append_to_strlist (&opt.gpg_arguments, "-z"); @@ -607,21 +593,15 @@ main ( int argc, char **argv) (void) last; } - if (configfp) - { - fclose (configfp); - configfp = NULL; - /* Keep a copy of the config filename. */ - opt.config_filename = configname; - configname = NULL; - goto next_pass; - } - xfree (configname); - configname = NULL; - - if (!opt.config_filename) + if (!last_configname) opt.config_filename = make_filename (gnupg_homedir (), - G13_NAME".conf", NULL); + G13_NAME EXTSEP_S "conf", + NULL); + else + { + opt.config_filename = last_configname; + last_configname = NULL; + } if (log_get_errorcount(0)) g13_exit(2); @@ -659,7 +639,9 @@ main ( int argc, char **argv) if (logfile) { log_set_file (logfile); - log_set_prefix (NULL, GPGRT_LOG_WITH_PREFIX | GPGRT_LOG_WITH_TIME | GPGRT_LOG_WITH_PID); + log_set_prefix (NULL, (GPGRT_LOG_WITH_PREFIX + | GPGRT_LOG_WITH_TIME + | GPGRT_LOG_WITH_PID )); } if (gnupg_faked_time_p ()) diff --git a/kbx/kbxutil.c b/kbx/kbxutil.c index ec5a43b5d..9e05de42c 100644 --- a/kbx/kbxutil.c +++ b/kbx/kbxutil.c @@ -465,7 +465,8 @@ main( int argc, char **argv ) { ARGPARSE_ARGS pargs; enum cmd_and_opt_values cmd = 0; - unsigned long from = 0, to = ULONG_MAX; + unsigned long from = 0; + unsigned long to = ULONG_MAX; int dry_run = 0; early_system_init (); @@ -487,8 +488,8 @@ main( int argc, char **argv ) pargs.argc = &argc; pargs.argv = &argv; - pargs.flags= 1; /* do not remove the args */ - while (arg_parse( &pargs, opts) ) + pargs.flags= ARGPARSE_FLAG_KEEP; + while (gnupg_argparse (NULL, &pargs, opts)) { switch (pargs.r_opt) { @@ -524,6 +525,8 @@ main( int argc, char **argv ) } } + gnupg_argparse (NULL, &pargs, NULL); + if (to < from) log_error ("record number of \"--to\" is lower than \"--from\" one\n"); diff --git a/scd/scdaemon.c b/scd/scdaemon.c index c1a3f438b..5c519f8a0 100644 --- a/scd/scdaemon.c +++ b/scd/scdaemon.c @@ -16,6 +16,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, see . + * SPDX-License-Identifier: GPL-3.0-or-later */ #include @@ -121,7 +122,7 @@ static ARGPARSE_OPTS opts[] = { ARGPARSE_s_n (oQuiet, "quiet", N_("be somewhat more quiet")), ARGPARSE_s_n (oSh, "sh", N_("sh-style command output")), ARGPARSE_s_n (oCsh, "csh", N_("csh-style command output")), - ARGPARSE_s_s (oOptions, "options", N_("|FILE|read options from FILE")), + ARGPARSE_conffile (oOptions, "options", N_("|FILE|read options from FILE")), ARGPARSE_s_s (oDebug, "debug", "@"), ARGPARSE_s_n (oDebugAll, "debug-all", "@"), ARGPARSE_s_s (oDebugLevel, "debug-level" , @@ -161,6 +162,7 @@ static ARGPARSE_OPTS opts[] = { N_("use variable length input for pinpad")), ARGPARSE_s_s (oHomedir, "homedir", "@"), ARGPARSE_s_i (oListenBacklog, "listen-backlog", "@"), + ARGPARSE_noconffile (oNoOptions, "no-options", "@"), /* Stubs for options which are implemented by 2.3 or later. */ ARGPARSE_s_s (oNoop, "application-priority", "@"), @@ -287,9 +289,11 @@ my_strusage (int level) switch (level) { + case 9: p = "GPL-3.0-or-later"; break; case 11: p = "@SCDAEMON@ (@GNUPG@)"; break; case 13: p = VERSION; break; + case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break; case 17: p = PRINTABLE_OS_NAME; break; case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break; @@ -421,13 +425,11 @@ main (int argc, char **argv ) ARGPARSE_ARGS pargs; int orig_argc; char **orig_argv; - FILE *configfp = NULL; - char *configname = NULL; + char *last_configname = NULL; + const char *configname = NULL; const char *shell; - unsigned int configlineno; - int parse_debug = 0; + int debug_argparser = 0; const char *debug_level = NULL; - int default_config =1; int greeting = 0; int nogreeting = 0; int multi_server = 0; @@ -437,7 +439,7 @@ main (int argc, char **argv ) char *logfile = NULL; int debug_wait = 0; int gpgconf_list = 0; - const char *config_filename = NULL; + char *config_filename = NULL; int allow_coredump = 0; struct assuan_malloc_hooks malloc_hooks; int res; @@ -484,73 +486,61 @@ main (int argc, char **argv ) orig_argv = argv; pargs.argc = &argc; pargs.argv = &argv; - pargs.flags= 1|(1<<6); /* do not remove the args, ignore version */ - while (arg_parse( &pargs, opts)) + pargs.flags= (ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION); + while (gnupg_argparse (NULL, &pargs, opts)) { - if (pargs.r_opt == oDebug || pargs.r_opt == oDebugAll) - parse_debug++; - else if (pargs.r_opt == oOptions) - { /* yes there is one, so we do not try the default one, but - read the option file when it is encountered at the - commandline */ - default_config = 0; - } - else if (pargs.r_opt == oNoOptions) - default_config = 0; /* --no-options */ - else if (pargs.r_opt == oHomedir) + switch (pargs.r_opt) + { + case oDebug: + case oDebugAll: + debug_argparser++; + break; + case oHomedir: gnupg_set_homedir (pargs.r.ret_str); + break; + } } + /* Reset the flags. */ + pargs.flags &= ~(ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION); - /* initialize the secure memory. */ + /* Initialize the secure memory. */ gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0); maybe_setuid = 0; /* - Now we are working under our real uid - */ - - - if (default_config) - configname = make_filename (gnupg_homedir (), SCDAEMON_NAME EXTSEP_S "conf", - NULL ); + * Now we are working under our real uid + */ + /* The configuraton directories for use by gpgrt_argparser. */ + gnupg_set_confdir (GNUPG_CONFDIR_SYS, gnupg_sysconfdir ()); + gnupg_set_confdir (GNUPG_CONFDIR_USER, gnupg_homedir ()); argc = orig_argc; argv = orig_argv; pargs.argc = &argc; pargs.argv = &argv; - pargs.flags= 1; /* do not remove the args */ - next_pass: - if (configname) - { - configlineno = 0; - configfp = gnupg_fopen (configname, "r"); - if (!configfp) - { - if (default_config) - { - if( parse_debug ) - log_info (_("Note: no default option file '%s'\n"), - configname ); - } - else - { - log_error (_("option file '%s': %s\n"), - configname, strerror(errno) ); - exit(2); - } - xfree (configname); - configname = NULL; - } - if (parse_debug && configname ) - log_info (_("reading options from '%s'\n"), configname ); - default_config = 0; - } - - while (optfile_parse( configfp, configname, &configlineno, &pargs, opts) ) + pargs.flags |= (ARGPARSE_FLAG_RESET + | ARGPARSE_FLAG_KEEP + | ARGPARSE_FLAG_SYS + | ARGPARSE_FLAG_USER); + while (gnupg_argparser (&pargs, opts, SCDAEMON_NAME EXTSEP_S "conf")) { switch (pargs.r_opt) { + case ARGPARSE_CONFFILE: + if (debug_argparser) + log_info (_("reading options from '%s'\n"), + pargs.r_type? pargs.r.ret_str: "[cmdline]"); + if (pargs.r_type) + { + xfree (last_configname); + last_configname = xstrdup (pargs.r.ret_str); + configname = last_configname; + } + else + configname = NULL; + break; + case aGPGConfList: gpgconf_list = 1; break; case aGPGConfTest: gpgconf_list = 2; break; case oQuiet: opt.quiet = 1; break; @@ -583,18 +573,8 @@ main (int argc, char **argv ) set_libassuan_log_cats (pargs.r.ret_ulong); break; - case oOptions: - /* config files may not be nested (silently ignore them) */ - if (!configfp) - { - xfree(configname); - configname = xstrdup(pargs.r.ret_str); - goto next_pass; - } - break; case oNoGreeting: nogreeting = 1; break; case oNoVerbose: opt.verbose = 0; break; - case oNoOptions: break; /* no-options */ case oHomedir: gnupg_set_homedir (pargs.r.ret_str); break; case oNoDetach: nodetach = 1; break; case oLogFile: logfile = pargs.r.ret_str; break; @@ -631,21 +611,25 @@ main (int argc, char **argv ) case oNoop: break; default: - pargs.err = configfp? ARGPARSE_PRINT_WARNING:ARGPARSE_PRINT_ERROR; + if (configname) + pargs.err = ARGPARSE_PRINT_WARNING; + else + pargs.err = ARGPARSE_PRINT_ERROR; break; } } - if (configfp) + gnupg_argparse (NULL, &pargs, NULL); /* Release internal state. */ + + if (!last_configname) + config_filename = make_filename (gnupg_homedir (), + SCDAEMON_NAME EXTSEP_S "conf", + NULL); + else { - fclose( configfp ); - configfp = NULL; - /* Keep a copy of the config name for use by --gpgconf-list. */ - config_filename = configname; - configname = NULL; - goto next_pass; + config_filename = last_configname; + last_configname = NULL; } - xfree (configname); - configname = NULL; + if (log_get_errorcount(0)) exit(2); if (nogreeting ) @@ -692,21 +676,13 @@ main (int argc, char **argv ) if (gpgconf_list) { /* List options and default values in the GPG Conf format. */ - char *filename = NULL; char *filename_esc; - if (config_filename) - filename = xstrdup (config_filename); - else - filename = make_filename (gnupg_homedir (), - SCDAEMON_NAME EXTSEP_S "conf", NULL); - filename_esc = percent_escape (filename, NULL); - + filename_esc = percent_escape (config_filename, NULL); es_printf ("%s-%s.conf:%lu:\"%s\n", GPGCONF_NAME, SCDAEMON_NAME, GC_OPT_FLAG_DEFAULT, filename_esc); xfree (filename_esc); - xfree (filename); es_printf ("verbose:%lu:\n" "quiet:%lu:\n" @@ -960,9 +936,11 @@ main (int argc, char **argv ) close (fd); } + xfree (config_filename); return 0; } + void scd_exit (int rc) { diff --git a/sm/gpgsm.c b/sm/gpgsm.c index 321826145..55a92c629 100644 --- a/sm/gpgsm.c +++ b/sm/gpgsm.c @@ -1,6 +1,7 @@ /* gpgsm.c - GnuPG for S/MIME - * Copyright (C) 2001-2008, 2010 Free Software Foundation, Inc. - * Copyright (C) 2001-2008, 2010 Werner Koch + * Copyright (C) 2001-2020 Free Software Foundation, Inc. + * Copyright (C) 2001-2019 Werner Koch + * Copyright (C) 2015-2020 g10 Code GmbH * * This file is part of GnuPG. * @@ -16,6 +17,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, see . + * SPDX-License-Identifier: GPL-3.0-or-later */ #include @@ -26,7 +28,6 @@ #include #include #include -/*#include */ #define INCLUDED_BY_MAIN_MODULE 1 @@ -337,7 +338,7 @@ static ARGPARSE_OPTS opts[] = { ARGPARSE_s_s (oKeyServer, "keyserver", N_("|SPEC|use this keyserver to lookup keys")), - ARGPARSE_s_s (oOptions, "options", N_("|FILE|read options from FILE")), + ARGPARSE_conffile (oOptions, "options", N_("|FILE|read options from FILE")), ARGPARSE_s_s (oDebug, "debug", "@"), ARGPARSE_s_s (oDebugLevel, "debug-level", @@ -373,7 +374,7 @@ static ARGPARSE_OPTS opts[] = { ARGPARSE_s_n (oNoArmor, "no-armour", "@"), ARGPARSE_s_n (oNoDefKeyring, "no-default-keyring", "@"), ARGPARSE_s_n (oNoGreeting, "no-greeting", "@"), - ARGPARSE_s_n (oNoOptions, "no-options", "@"), + ARGPARSE_noconffile (oNoOptions, "no-options", "@"), ARGPARSE_s_s (oHomedir, "homedir", "@"), ARGPARSE_s_s (oAgentProgram, "agent-program", "@"), ARGPARSE_s_s (oDisplay, "display", "@"), @@ -557,9 +558,11 @@ my_strusage( int level ) switch (level) { + case 9: p = "GPL-3.0-or-later"; break; case 11: p = "@GPGSM@ (@GNUPG@)"; break; case 13: p = VERSION; break; + case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break; case 17: p = PRINTABLE_OS_NAME; break; case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break; @@ -893,12 +896,12 @@ main ( int argc, char **argv) strlist_t sl, remusr= NULL, locusr=NULL; strlist_t nrings=NULL; int detached_sig = 0; - FILE *configfp = NULL; - char *configname = NULL; - unsigned configlineno; - int parse_debug = 0; + char *last_configname = NULL; + const char *configname = NULL; /* NULL or points to last_configname. + * NULL also indicates that we are + * processing options from the cmdline. */ + int debug_argparser = 0; int no_more_options = 0; - int default_config =1; int default_keyring = 1; char *logfile = NULL; char *auditlog = NULL; @@ -921,7 +924,8 @@ main ( int argc, char **argv) estream_t htmlauditfp = NULL; struct assuan_malloc_hooks malloc_hooks; int pwfd = -1; - /*mtrace();*/ + + static const char *homedirvalue; early_system_init (); gnupg_reopen_std (GPGSM_NAME); @@ -973,28 +977,35 @@ main ( int argc, char **argv) orig_argv = argv; pargs.argc = &argc; pargs.argv = &argv; - pargs.flags= 1|(1<<6); /* do not remove the args, ignore version */ - while (arg_parse( &pargs, opts)) + pargs.flags= (ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION); + while (gnupg_argparse (NULL, &pargs, opts)) { - if (pargs.r_opt == oDebug || pargs.r_opt == oDebugAll) - parse_debug++; - else if (pargs.r_opt == oOptions) - { /* yes there is one, so we do not try the default one but - read the config file when it is encountered at the - commandline */ - default_config = 0; - } - else if (pargs.r_opt == oNoOptions) + switch (pargs.r_opt) { - default_config = 0; /* --no-options */ + case oDebug: + case oDebugAll: + debug_argparser++; + break; + + case oNoOptions: + /* Set here here because the homedir would otherwise be + * created before main option parsing starts. */ opt.no_homedir_creation = 1; + break; + + case oHomedir: + homedirvalue = pargs.r.ret_str; + break; + + case aCallProtectTool: + /* Make sure that --version and --help are passed to the + * protect-tool. */ + goto leave_cmdline_parser; } - else if (pargs.r_opt == oHomedir) - gnupg_set_homedir (pargs.r.ret_str); - else if (pargs.r_opt == aCallProtectTool) - break; /* This break makes sure that --version and --help are - passed to the protect-tool. */ } + leave_cmdline_parser: + /* Reset the flags. */ + pargs.flags &= ~(ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION); /* Initialize the secure memory. */ @@ -1002,8 +1013,8 @@ main ( int argc, char **argv) maybe_setuid = 0; /* - Now we are now working under our real uid - */ + * Now we are now working under our real uid + */ ksba_set_malloc_hooks (gcry_malloc, gcry_realloc, gcry_free ); @@ -1014,6 +1025,9 @@ main ( int argc, char **argv) assuan_set_gpg_err_source (GPG_ERR_SOURCE_DEFAULT); setup_libassuan_logging (&opt.debug, NULL); + /* Set homedir. */ + gnupg_set_homedir (homedirvalue); + /* Setup a default control structure for command line mode */ memset (&ctrl, 0, sizeof ctrl); gpgsm_init_default_ctrl (&ctrl); @@ -1021,48 +1035,43 @@ main ( int argc, char **argv) ctrl.status_fd = -1; /* No status output. */ ctrl.autodetect_encoding = 1; - /* Set the default option file */ - if (default_config ) - configname = make_filename (gnupg_homedir (), - GPGSM_NAME EXTSEP_S "conf", NULL); /* Set the default policy file */ opt.policy_file = make_filename (gnupg_homedir (), "policies.txt", NULL); + /* The configuraton directories for use by gpgrt_argparser. */ + gnupg_set_confdir (GNUPG_CONFDIR_SYS, gnupg_sysconfdir ()); + gnupg_set_confdir (GNUPG_CONFDIR_USER, gnupg_homedir ()); + + /* We are re-using the struct, thus the reset flag. We OR the + * flags so that the internal intialized flag won't be cleared. */ argc = orig_argc; argv = orig_argv; pargs.argc = &argc; pargs.argv = &argv; - pargs.flags = 1; /* do not remove the args */ - - next_pass: - if (configname) { - configlineno = 0; - configfp = gnupg_fopen (configname, "r"); - if (!configfp) - { - if (default_config) - { - if (parse_debug) - log_info (_("Note: no default option file '%s'\n"), configname); - } - else - { - log_error (_("option file '%s': %s\n"), configname, strerror(errno)); - gpgsm_exit(2); - } - xfree(configname); - configname = NULL; - } - if (parse_debug && configname) - log_info (_("reading options from '%s'\n"), configname); - default_config = 0; - } + pargs.flags |= (ARGPARSE_FLAG_RESET + | ARGPARSE_FLAG_KEEP + | ARGPARSE_FLAG_SYS + | ARGPARSE_FLAG_USER); while (!no_more_options - && optfile_parse (configfp, configname, &configlineno, &pargs, opts)) + && gnupg_argparser (&pargs, opts, GPGSM_NAME EXTSEP_S "conf")) { switch (pargs.r_opt) { + case ARGPARSE_CONFFILE: + if (debug_argparser) + log_info (_("reading options from '%s'\n"), + pargs.r_type? pargs.r.ret_str: "[cmdline]"); + if (pargs.r_type) + { + xfree (last_configname); + last_configname = xstrdup (pargs.r.ret_str); + configname = last_configname; + } + else + configname = NULL; + break; + case aGPGConfList: case aGPGConfTest: set_cmd (&cmd, pargs.r_opt); @@ -1308,16 +1317,6 @@ main ( int argc, char **argv) opt.with_keygrip = 1; break; - case oOptions: - /* config files may not be nested (silently ignore them) */ - if (!configfp) - { - xfree(configname); - configname = xstrdup (pargs.r.ret_str); - goto next_pass; - } - break; - case oNoOptions: opt.no_homedir_creation = 1; break; /* no-options */ case oHomedir: gnupg_set_homedir (pargs.r.ret_str); break; case oAgentProgram: opt.agent_program = pargs.r.ret_str; break; @@ -1445,7 +1444,7 @@ main ( int argc, char **argv) { struct keyserver_spec *keyserver; keyserver = parse_keyserver_line (pargs.r.ret_str, - configname, configlineno); + configname, pargs.lineno); if (! keyserver) log_error (_("could not parse keyserver\n")); else @@ -1483,27 +1482,28 @@ main ( int argc, char **argv) break; default: - pargs.err = configfp? ARGPARSE_PRINT_WARNING:ARGPARSE_PRINT_ERROR; + if (configname) + pargs.err = ARGPARSE_PRINT_WARNING; + else + { + pargs.err = ARGPARSE_PRINT_ERROR; + /* The argparse function calls a plain exit and thus we + * need to print a status here. */ + gpgsm_status_with_error (&ctrl, STATUS_FAILURE, "option-parser", + gpg_error (GPG_ERR_GENERAL)); + } break; } } - if (configfp) - { - fclose (configfp); - configfp = NULL; - /* Keep a copy of the config filename. */ - opt.config_filename = configname; - configname = NULL; - goto next_pass; - } - xfree (configname); - configname = NULL; + gnupg_argparse (NULL, &pargs, NULL); /* Release internal state. */ - if (!opt.config_filename) + if (!last_configname) opt.config_filename = make_filename (gnupg_homedir (), GPGSM_NAME EXTSEP_S "conf", NULL); + else + opt.config_filename = last_configname; if (log_get_errorcount(0)) { diff --git a/tests/gpgscm/main.c b/tests/gpgscm/main.c index 5540ac365..f2b12aaaf 100644 --- a/tests/gpgscm/main.c +++ b/tests/gpgscm/main.c @@ -16,6 +16,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, see . + * SPDX-License-Identifier: GPL-3.0-or-later */ #include @@ -78,9 +79,8 @@ size_t scmpath_len = 0; static void parse_arguments (ARGPARSE_ARGS *pargs, ARGPARSE_OPTS *popts) { - int no_more_options = 0; - while (!no_more_options && optfile_parse (NULL, NULL, NULL, pargs, popts)) + while (gnupg_argparse (NULL, pargs, popts)) { switch (pargs->r_opt) { @@ -89,7 +89,7 @@ parse_arguments (ARGPARSE_ARGS *pargs, ARGPARSE_OPTS *popts) break; default: - pargs->err = 2; + pargs->err = ARGPARSE_PRINT_ERROR; break; } } @@ -103,9 +103,11 @@ my_strusage( int level ) switch (level) { + case 9: p = "GPL-3.0-or-later"; break; case 11: p = "gpgscm (@GNUPG@)"; break; case 13: p = VERSION; break; + case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break; case 17: p = PRINTABLE_OS_NAME; break; case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break; @@ -297,6 +299,7 @@ main (int argc, char **argv) pargs.argv = &argv; pargs.flags = 0; parse_arguments (&pargs, opts); + gnupg_argparse (NULL, &pargs, NULL); if (log_get_errorcount (0)) exit (2); diff --git a/tools/gpg-check-pattern.c b/tools/gpg-check-pattern.c index 46b9e589d..69248a3a3 100644 --- a/tools/gpg-check-pattern.c +++ b/tools/gpg-check-pattern.c @@ -15,6 +15,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, see . + * SPDX-License-Identifier: GPL-3.0-or-later */ #include @@ -133,9 +134,11 @@ my_strusage (int level) const char *p; switch (level) { + case 9: p = "GPL-3.0-or-later"; break; case 11: p = "gpg-check-pattern (@GnuPG@)"; break; case 13: p = VERSION; break; + case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break; case 17: p = PRINTABLE_OS_NAME; break; case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break; @@ -176,8 +179,8 @@ main (int argc, char **argv ) pargs.argc = &argc; pargs.argv = &argv; - pargs.flags= 1; /* (do not remove the args) */ - while (arg_parse (&pargs, opts) ) + pargs.flags= ARGPARSE_FLAG_KEEP; + while (gnupg_argparse (NULL, &pargs, opts)) { switch (pargs.r_opt) { @@ -189,6 +192,8 @@ main (int argc, char **argv ) default : pargs.err = 2; break; } } + gnupg_argparse (NULL, &pargs, NULL); /* Release internal state. */ + if (log_get_errorcount(0)) exit (2); @@ -489,4 +494,3 @@ process (FILE *fp, pattern_t *patarray) if (opt.verbose) log_info ("no input line matches the pattern - accepted\n"); } - diff --git a/tools/gpg-connect-agent.c b/tools/gpg-connect-agent.c index 0dabdad16..250d6caa3 100644 --- a/tools/gpg-connect-agent.c +++ b/tools/gpg-connect-agent.c @@ -16,6 +16,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, see . + * SPDX-License-Identifier: GPL-3.0-or-later */ #include @@ -195,9 +196,11 @@ my_strusage( int level ) switch (level) { + case 9: p = "GPL-3.0-or-later"; break; case 11: p = "@GPG@-connect-agent (@GNUPG@)"; break; case 13: p = VERSION; break; + case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break; case 17: p = PRINTABLE_OS_NAME; break; case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break; @@ -1190,8 +1193,8 @@ main (int argc, char **argv) /* Parse the command line. */ pargs.argc = &argc; pargs.argv = &argv; - pargs.flags = 1; /* Do not remove the args. */ - while (!no_more_options && optfile_parse (NULL, NULL, NULL, &pargs, opts)) + pargs.flags = ARGPARSE_FLAG_KEEP; + while (!no_more_options && gnupg_argparse (NULL, &pargs, opts)) { switch (pargs.r_opt) { @@ -1219,6 +1222,7 @@ main (int argc, char **argv) default: pargs.err = 2; break; } } + gnupg_argparse (NULL, &pargs, NULL); /* Release internal state. */ if (log_get_errorcount (0)) exit (2); diff --git a/tools/gpg-wks-client.c b/tools/gpg-wks-client.c index 56768aa7e..4ebf50f39 100644 --- a/tools/gpg-wks-client.c +++ b/tools/gpg-wks-client.c @@ -16,6 +16,7 @@ * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ #include @@ -159,9 +160,11 @@ my_strusage( int level ) switch (level) { + case 9: p = "LGPL-2.1-or-later"; break; case 11: p = "gpg-wks-client"; break; case 12: p = "@GNUPG@"; break; case 13: p = VERSION; break; + case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break; case 17: p = PRINTABLE_OS_NAME; break; case 19: p = ("Please report bugs to <@EMAIL@>.\n"); break; @@ -196,7 +199,7 @@ parse_arguments (ARGPARSE_ARGS *pargs, ARGPARSE_OPTS *popts) enum cmd_and_opt_values cmd = 0; int no_more_options = 0; - while (!no_more_options && optfile_parse (NULL, NULL, NULL, pargs, popts)) + while (!no_more_options && gnupg_argparse (NULL, pargs, popts)) { switch (pargs->r_opt) { @@ -244,7 +247,7 @@ parse_arguments (ARGPARSE_ARGS *pargs, ARGPARSE_OPTS *popts) cmd = pargs->r_opt; break; - default: pargs->err = 2; break; + default: pargs->err = ARGPARSE_PRINT_ERROR; break; } } @@ -277,6 +280,7 @@ main (int argc, char **argv) pargs.argv = &argv; pargs.flags = ARGPARSE_FLAG_KEEP; cmd = parse_arguments (&pargs, opts); + gnupg_argparse (NULL, &pargs, NULL); if (log_get_errorcount (0)) exit (2); diff --git a/tools/gpg-wks-server.c b/tools/gpg-wks-server.c index addafa27d..2ff53a27f 100644 --- a/tools/gpg-wks-server.c +++ b/tools/gpg-wks-server.c @@ -16,6 +16,7 @@ * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ /* The Web Key Service I-D defines an update protocol to store a @@ -172,9 +173,11 @@ my_strusage( int level ) switch (level) { + case 9: p = "LGPL-2.1-or-later"; break; case 11: p = "gpg-wks-server"; break; case 12: p = "@GNUPG@"; break; case 13: p = VERSION; break; + case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break; case 17: p = PRINTABLE_OS_NAME; break; case 19: p = ("Please report bugs to <@EMAIL@>.\n"); break; @@ -209,7 +212,7 @@ parse_arguments (ARGPARSE_ARGS *pargs, ARGPARSE_OPTS *popts) enum cmd_and_opt_values cmd = 0; int no_more_options = 0; - while (!no_more_options && optfile_parse (NULL, NULL, NULL, pargs, popts)) + while (!no_more_options && gnupg_argparse (NULL, pargs, popts)) { switch (pargs->r_opt) { @@ -258,7 +261,7 @@ parse_arguments (ARGPARSE_ARGS *pargs, ARGPARSE_OPTS *popts) cmd = pargs->r_opt; break; - default: pargs->err = 2; break; + default: pargs->err = ARGPARSE_PRINT_ERROR; break; } } @@ -287,6 +290,7 @@ main (int argc, char **argv) pargs.argv = &argv; pargs.flags = ARGPARSE_FLAG_KEEP; cmd = parse_arguments (&pargs, opts); + gnupg_argparse (NULL, &pargs, NULL); /* Release internal state. */ if (log_get_errorcount (0)) exit (2); diff --git a/tools/gpgconf.c b/tools/gpgconf.c index b6afc8b0e..6b499b3f9 100644 --- a/tools/gpgconf.c +++ b/tools/gpgconf.c @@ -1,6 +1,6 @@ /* gpgconf.c - Configuration utility for GnuPG * Copyright (C) 2003, 2007, 2009, 2011 Free Software Foundation, Inc. - * Copyright (C) 2016 g10 Code GmbH. + * Copyright (C) 2016, 2020 g10 Code GmbH. * * This file is part of GnuPG. * @@ -16,6 +16,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, see . + * SPDX-License-Identifier: GPL-3.0-or-later */ #include @@ -136,9 +137,11 @@ my_strusage( int level ) switch (level) { + case 9: p = "GPL-3.0-or-later"; break; case 11: p = "@GPGCONF@ (@GNUPG@)"; break; case 13: p = VERSION; break; + case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break; case 17: p = PRINTABLE_OS_NAME; break; case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break; @@ -564,17 +567,15 @@ main (int argc, char **argv) /* Parse the command line. */ pargs.argc = &argc; pargs.argv = &argv; - pargs.flags = 1; /* Do not remove the args. */ - while (!no_more_options && optfile_parse (NULL, NULL, NULL, &pargs, opts)) + pargs.flags = ARGPARSE_FLAG_KEEP; + while (!no_more_options && gnupg_argparse (NULL, &pargs, opts)) { switch (pargs.r_opt) { case oOutput: opt.outfile = pargs.r.ret_str; break; case oQuiet: opt.quiet = 1; break; case oDryRun: opt.dry_run = 1; break; - case oRuntime: - opt.runtime = 1; - break; + case oRuntime: opt.runtime = 1; break; case oVerbose: opt.verbose++; break; case oNoVerbose: opt.verbose = 0; break; case oHomedir: gnupg_set_homedir (pargs.r.ret_str); break; @@ -605,9 +606,10 @@ main (int argc, char **argv) cmd = pargs.r_opt; break; - default: pargs.err = 2; break; + default: pargs.err = ARGPARSE_PRINT_ERROR; break; } } + gnupg_argparse (NULL, &pargs, NULL); /* Release internal state. */ if (log_get_errorcount (0)) gpgconf_failure (GPG_ERR_USER_2); diff --git a/tools/gpgsplit.c b/tools/gpgsplit.c index e84400854..d65348c7d 100644 --- a/tools/gpgsplit.c +++ b/tools/gpgsplit.c @@ -15,6 +15,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, see . + * SPDX-License-Identifier: GPL-3.0-or-later */ #include @@ -86,9 +87,11 @@ my_strusage (int level) const char *p; switch (level) { + case 9: p = "GPL-3.0-or-later"; break; case 11: p = "gpgsplit (@GNUPG@)"; break; case 13: p = VERSION; break; + case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break; case 17: p = PRINTABLE_OS_NAME; break; case 19: p = "Please report bugs to <@EMAIL@>.\n"; break; @@ -122,8 +125,8 @@ main (int argc, char **argv) pargs.argc = &argc; pargs.argv = &argv; - pargs.flags= 1; /* do not remove the args */ - while (optfile_parse( NULL, NULL, NULL, &pargs, opts)) + pargs.flags= ARGPARSE_FLAG_KEEP; + while (gnupg_argparse (NULL, &pargs, opts)) { switch (pargs.r_opt) { @@ -132,9 +135,10 @@ main (int argc, char **argv) case oUncompress: opt_uncompress = 1; break; case oSecretToPublic: opt_secret_to_public = 1; break; case oNoSplit: opt_no_split = 1; break; - default : pargs.err = 2; break; + default : pargs.err = ARGPARSE_PRINT_ERROR; break; } } + gnupg_argparse (NULL, &pargs, NULL); /* Release internal state. */ if (log_get_errorcount(0)) g10_exit (2); diff --git a/tools/gpgtar.c b/tools/gpgtar.c index dfb2df029..c2f24ecab 100644 --- a/tools/gpgtar.c +++ b/tools/gpgtar.c @@ -1,5 +1,6 @@ /* gpgtar.c - A simple TAR implementation mainly useful for Windows. * Copyright (C) 2010 Free Software Foundation, Inc. + * Copyright (C) 2020 g10 Code GmbH * * This file is part of GnuPG. * @@ -15,6 +16,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, see . + * SPDX-License-Identifier: GPL-3.0-or-later */ /* GnuPG comes with a shell script gpg-zip which creates archive files @@ -161,9 +163,11 @@ my_strusage( int level ) switch (level) { + case 9: p = "GPL-3.0-or-later"; break; case 11: p = "@GPGTAR@ (@GNUPG@)"; break; case 13: p = VERSION; break; + case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break; case 17: p = PRINTABLE_OS_NAME; break; case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break; @@ -314,7 +318,7 @@ parse_arguments (ARGPARSE_ARGS *pargs, ARGPARSE_OPTS *popts) { int no_more_options = 0; - while (!no_more_options && optfile_parse (NULL, NULL, NULL, pargs, popts)) + while (!no_more_options && gnupg_argparse (NULL, pargs, popts)) { switch (pargs->r_opt) { @@ -385,7 +389,7 @@ parse_arguments (ARGPARSE_ARGS *pargs, ARGPARSE_OPTS *popts) } break; - case oTarArgs:; + case oTarArgs: { int tar_argc; char **tar_argv; @@ -400,6 +404,7 @@ parse_arguments (ARGPARSE_ARGS *pargs, ARGPARSE_OPTS *popts) tar_args.argv = &tar_argv; tar_args.flags = ARGPARSE_FLAG_ARG0; parse_arguments (&tar_args, tar_opts); + gnupg_argparse (NULL, &tar_args, NULL); if (tar_args.err) log_error ("unsupported tar arguments '%s'\n", pargs->r.ret_str); @@ -426,8 +431,6 @@ main (int argc, char **argv) const char *fname; ARGPARSE_ARGS pargs; - assert (sizeof (struct ustar_raw_header) == 512); - gnupg_reopen_std (GPGTAR_NAME); set_strusage (my_strusage); log_set_prefix (GPGTAR_NAME, GPGRT_LOG_WITH_PREFIX); @@ -436,11 +439,14 @@ main (int argc, char **argv) i18n_init(); init_common_subsystems (&argc, &argv); + log_assert (sizeof (struct ustar_raw_header) == 512); + /* Parse the command line. */ pargs.argc = &argc; pargs.argv = &argv; pargs.flags = ARGPARSE_FLAG_KEEP; parse_arguments (&pargs, opts); + gnupg_argparse (NULL, &pargs, NULL); if (log_get_errorcount (0)) exit (2);