aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWerner Koch <[email protected]>2020-02-26 14:36:28 +0000
committerWerner Koch <[email protected]>2020-02-26 14:36:28 +0000
commit2f169b341e410924c5d7ee7d76ac67f85f26ed6a (patch)
tree314a212361cfb5950172dcdda8bfd1a16b304289
parentcore: Prepare argparse.c to track additional info for options. (diff)
downloadlibgpg-error-2f169b341e410924c5d7ee7d76ac67f85f26ed6a.tar.gz
libgpg-error-2f169b341e410924c5d7ee7d76ac67f85f26ed6a.zip
core: Add meta command handler stubs to argparse.c.
* src/argparse.c (handle_meta_user): New stub. (handle_meta_force): New stub. (handle_meta_ignore): New stub. (handle_meta_echo): New. (handle_metacmd): New. (_gpgrt_argparse): Call meta command handler. -- GnuPG-bug-id: 4788 Signed-off-by: Werner Koch <[email protected]>
-rw-r--r--src/argparse.c116
-rw-r--r--tests/etc/t-argparse.conf4
2 files changed, 119 insertions, 1 deletions
diff --git a/src/argparse.c b/src/argparse.c
index c29e2ea..a4ee875 100644
--- a/src/argparse.c
+++ b/src/argparse.c
@@ -622,6 +622,115 @@ ignore_invalid_option_clear (gpgrt_argparse_t *arg)
}
+/* Implementation of the "user" and "group" commands. ARG is the
+ * context. A value of 0 for ALTERNATE requests the "user" command, a
+ * value of "1" the "group" command. ARGS is a non-empty string which
+ * this function is allowed to modify. */
+static int
+handle_meta_user (gpgrt_argparse_t *arg, unsigned int alternate, char *args)
+{
+ (void)args;
+
+ _gpgrt_log_info ("%s:%u: meta command %s is not yet supported\n",
+ arg->internal->confname, arg->lineno,
+ alternate? "group":"user");
+
+ 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 (gpgrt_argparse_t *arg, unsigned int alternate, char *args)
+{
+ (void)arg;
+ (void)alternate;
+ (void)args;
+
+ 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 3 requests an "ignore-all". ARGS is the
+ * empty string and not used. */
+static int
+handle_meta_ignore (gpgrt_argparse_t *arg, unsigned int alternate, char *args)
+{
+ (void)arg;
+ (void)alternate;
+ (void)args;
+
+ return 0;
+}
+
+
+/* Implementation of the "ignore" command. ARG is the context.
+ * ALTERNATE is not used. ARGS is the string to log. */
+static int
+handle_meta_echo (gpgrt_argparse_t *arg, unsigned int alternate, char *args)
+{
+ (void)alternate;
+
+ _gpgrt_log_info ("%s:%u: %s\n",
+ arg->internal->confname, arg->lineno, args);
+ 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 (gpgrt_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; /* Command requires an argument. */
+ int (*func)(gpgrt_argparse_t *arg,
+ unsigned int alternate, char *args); /*handler*/
+ } cmds[] =
+ {{ "user", 0, 1, handle_meta_user },
+ { "group", 1, 1, handle_meta_user },
+ { "force", 0, 0, handle_meta_force },
+ { "+force", 0, 0, handle_meta_force },
+ { "-force", 1, 0, handle_meta_force },
+ { "ignore", 0, 0, handle_meta_ignore },
+ { "+ignore", 0, 0, handle_meta_ignore },
+ { "-ignore", 1, 0, handle_meta_ignore },
+ { "ignore-all", 2, 0, handle_meta_ignore },
+ { "+ignore-all", 2, 0, handle_meta_ignore },
+ { "echo", 0, 1, handle_meta_echo }
+ };
+ char *rest;
+ int i;
+
+ _gpgrt_log_debug ("Handle meta command '%s'\n", keyword);
+
+ 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;
+ return cmds[i].func (arg, cmds[i].alternate, rest);
+}
+
/****************
* Get options from a file.
@@ -799,7 +908,12 @@ _gpgrt_argparse (estream_t fp, gpgrt_argparse_t *arg, gpgrt_opt_t *opts_orig)
arg->r_opt = ARGPARSE_UNEXPECTED_META;
goto leave;
}
- /* _gpgrt_log_debug ("Got meta command '%s'\n", keyword+1); */
+ c = handle_metacmd (arg, keyword+1);
+ if (c)
+ {
+ arg->r_opt = c; /* Return error. */
+ goto leave;
+ }
state = Ainit;
i = 0;
}
diff --git a/tests/etc/t-argparse.conf b/tests/etc/t-argparse.conf
index 0852e71..c556466 100644
--- a/tests/etc/t-argparse.conf
+++ b/tests/etc/t-argparse.conf
@@ -3,6 +3,8 @@
# Options applied to all user's config files
#verbose
+[echo Begin global config]
+
[group :staff]
# These option are applied to all users of the group staff up until
# the next [group] or [user] statement.
@@ -52,3 +54,5 @@ my-option 42
# The default algorithm for new keys is set to this.
a-long-option
+
+[echo End global config]