Use gpgrt's new option parser for symcryptrun.

* tools/symcryptrun.c: Follow API change of the new option parser.

Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
This commit is contained in:
NIIBE Yutaka 2020-07-17 15:17:43 +09:00
parent 46d185f603
commit f484ac2b2d
1 changed files with 63 additions and 79 deletions

View File

@ -161,32 +161,32 @@ enum cmd_and_opt_values
/* The list of commands and options. */ /* The list of commands and options. */
static ARGPARSE_OPTS opts[] = static gpgrt_opt_t opts[] = {
{
{ 301, NULL, 0, N_("@\nCommands:\n ") },
{ oDecrypt, "decrypt", 0, N_("decryption modus") }, ARGPARSE_group(300, N_("@\nCommands:\n ")),
{ oEncrypt, "encrypt", 0, N_("encryption modus") },
{ 302, NULL, 0, N_("@\nOptions:\n ") }, ARGPARSE_c(oDecrypt, "decrypt", N_("decryption modus")),
ARGPARSE_c(oEncrypt, "encrypt", N_("encryption modus")),
{ oClass, "class", 2, N_("tool class (confucius)") }, ARGPARSE_group(301, N_("@\nOptions:\n ")),
{ oProgram, "program", 2, N_("program filename") },
{ oKeyfile, "keyfile", 2, N_("secret key file (required)") }, ARGPARSE_s_s(oClass, "class", N_("tool class (confucius)")),
{ oInput, "inputfile", 2, N_("input file name (default stdin)") }, ARGPARSE_s_s(oProgram, "program", N_("program filename")),
{ oVerbose, "verbose", 0, N_("verbose") },
{ oQuiet, "quiet", 0, N_("quiet") },
{ oLogFile, "log-file", 2, N_("use a log file for the server") },
{ oOptions, "options" , 2, N_("|FILE|read options from FILE") },
/* Hidden options. */ ARGPARSE_s_s(oKeyfile, "keyfile", N_("secret key file (required)")),
{ oNoVerbose, "no-verbose", 0, "@" }, ARGPARSE_s_s(oInput, "inputfile", N_("input file name (default stdin)")),
{ oHomedir, "homedir", 2, "@" }, ARGPARSE_s_n(oVerbose, "verbose", N_("verbose")),
{ oNoOptions, "no-options", 0, "@" },/* shortcut for --options /dev/null */ ARGPARSE_s_n(oQuiet, "quiet", N_("quiet")),
ARGPARSE_s_s(oLogFile, "log-file", N_("use a log file for the server")),
ARGPARSE_conffile(oOptions, "options", N_("|FILE|read options from FILE")),
ARGPARSE_end () /* Hidden options. */
}; ARGPARSE_s_n(oNoVerbose, "no-verbose", "@"),
ARGPARSE_s_s(oHomedir, "homedir", "@"),
ARGPARSE_noconffile(oNoOptions, "no-options", "@"),
ARGPARSE_end ()
};
/* We keep all global options in the structure OPT. */ /* We keep all global options in the structure OPT. */
@ -211,9 +211,11 @@ my_strusage (int level)
switch (level) switch (level)
{ {
case 9: p = "GPL-3.0-or-later"; break;
case 11: p = "symcryptrun (@GNUPG@)"; case 11: p = "symcryptrun (@GNUPG@)";
break; break;
case 13: p = VERSION; break; case 13: p = VERSION; break;
case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break;
case 17: p = PRINTABLE_OS_NAME; break; case 17: p = PRINTABLE_OS_NAME; break;
case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break; case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break;
@ -885,19 +887,17 @@ confucius_main (int mode, int argc, char *argv[])
int int
main (int argc, char **argv) main (int argc, char **argv)
{ {
ARGPARSE_ARGS pargs; gpgrt_argparse_t pargs;
int orig_argc; int orig_argc;
char **orig_argv; char **orig_argv;
FILE *configfp = NULL; char *last_configname = NULL;
char *configname = NULL; const char *configname = NULL;
unsigned configlineno;
int mode = 0; int mode = 0;
int res; int res;
char *logfile = NULL; char *logfile = NULL;
int default_config = 1;
early_system_init (); early_system_init ();
set_strusage (my_strusage); gpgrt_set_strusage (my_strusage);
log_set_prefix ("symcryptrun", GPGRT_LOG_WITH_PREFIX); log_set_prefix ("symcryptrun", GPGRT_LOG_WITH_PREFIX);
/* Make sure that our subsystems are ready. */ /* Make sure that our subsystems are ready. */
@ -909,53 +909,48 @@ main (int argc, char **argv)
orig_argv = argv; orig_argv = argv;
pargs.argc = &argc; pargs.argc = &argc;
pargs.argv = &argv; pargs.argv = &argv;
pargs.flags= 1|(1<<6); /* do not remove the args, ignore version */ pargs.flags= ARGPARSE_FLAG_KEEP|ARGPARSE_FLAG_NOVERSION;
while (arg_parse( &pargs, opts)) while (gpgrt_argparse (NULL, &pargs, opts))
{ {
if (pargs.r_opt == oOptions) if (pargs.r_opt == oHomedir)
{ /* 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); gnupg_set_homedir (pargs.r.ret_str);
} }
/* Reset the flags. */
pargs.flags &= ~(ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION);
if (default_config) /* The configuraton directories for use by gpgrt_argparser. */
configname = make_filename (gnupg_homedir (), "symcryptrun.conf", NULL ); gpgrt_set_confdir (GPGRT_CONFDIR_SYS, gnupg_sysconfdir ());
gpgrt_set_confdir (GPGRT_CONFDIR_USER, gnupg_homedir ());
argc = orig_argc; argc = orig_argc;
argv = orig_argv; argv = orig_argv;
pargs.argc = &argc; pargs.argc = &argc;
pargs.argv = &argv; pargs.argv = &argv;
pargs.flags= 1; /* do not remove the args */ /* We are re-using the struct, thus the reset flag. We OR the
next_pass: * flags so that the internal intialized flag won't be cleared. */
if (configname) pargs.flags |= (ARGPARSE_FLAG_RESET
{ | ARGPARSE_FLAG_KEEP
configlineno = 0; | ARGPARSE_FLAG_SYS
configfp = fopen (configname, "r"); | ARGPARSE_FLAG_USER);
if (!configfp)
{
if (!default_config)
{
log_error (_("option file '%s': %s\n"),
configname, strerror(errno) );
exit(1);
}
xfree (configname);
configname = NULL;
}
default_config = 0;
}
/* Parse the command line. */ /* Parse the command line. */
while (optfile_parse (configfp, configname, &configlineno, &pargs, opts)) while (gpgrt_argparser (&pargs, opts, "symcryptrun.conf"))
{ {
switch (pargs.r_opt) switch (pargs.r_opt)
{ {
case ARGPARSE_CONFFILE:
{
if (pargs.r_type)
{
xfree (last_configname);
last_configname = xstrdup (pargs.r.ret_str);
configname = last_configname;
}
else
configname = NULL;
}
break;
case oDecrypt: mode = oDecrypt; break; case oDecrypt: mode = oDecrypt; break;
case oEncrypt: mode = oEncrypt; break; case oEncrypt: mode = oEncrypt; break;
@ -970,30 +965,19 @@ main (int argc, char **argv)
case oLogFile: logfile = pargs.r.ret_str; break; case oLogFile: logfile = pargs.r.ret_str; 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: break; /* no-options */
case oHomedir: /* Ignore this option here. */; break; case oHomedir: /* Ignore this option here. */; 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) gpgrt_argparse (NULL, &pargs, NULL); /* Release internal state. */
{
fclose( configfp ); xfree (last_configname);
configfp = NULL;
configname = NULL;
goto next_pass;
}
xfree (configname);
configname = NULL;
if (!mode) if (!mode)
log_error (_("either %s or %s must be given\n"), log_error (_("either %s or %s must be given\n"),