diff options
-rw-r--r-- | src/argparse.c | 83 | ||||
-rw-r--r-- | tests/etc/t-argparse.conf | 11 | ||||
-rw-r--r-- | tests/t-argparse.conf | 2 |
3 files changed, 84 insertions, 12 deletions
diff --git a/src/argparse.c b/src/argparse.c index ef744be..88e75c2 100644 --- a/src/argparse.c +++ b/src/argparse.c @@ -34,6 +34,7 @@ #include <limits.h> #include <errno.h> #include <unistd.h> +#include <time.h> #include "gpgrt-int.h" @@ -663,13 +664,10 @@ ignore_invalid_option_clear (gpgrt_argparse_t *arg) } -/* Implementation of the "user" command. ARG is the context. ARGS is - * a non-empty string which this function is allowed to modify. */ +/* Make sure the username field is filled. Return 0 on success. */ static int -handle_meta_user (gpgrt_argparse_t *arg, unsigned int alternate, char *args) +assure_username (gpgrt_argparse_t *arg) { - (void)alternate; - if (!arg->internal->username) { arg->internal->username = _gpgrt_getusername (); @@ -684,7 +682,22 @@ handle_meta_user (gpgrt_argparse_t *arg, unsigned int alternate, char *args) 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 (gpgrt_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]) @@ -757,12 +770,64 @@ handle_meta_ignore (gpgrt_argparse_t *arg, unsigned int alternate, char *args) static int handle_meta_echo (gpgrt_argparse_t *arg, unsigned int alternate, char *args) { + int rc = 0; + char *p, *pend; + if (alternate) - _gpgrt_log_info ("%s\n", args); + _gpgrt_log_info ("%s", ""); else - _gpgrt_log_info ("%s:%u: %s\n", - arg->internal->confname, arg->lineno, args); - return 0; + _gpgrt_log_info ("%s:%u: ", arg->internal->confname, arg->lineno); + + while (*args) + { + p = strchr (args, '$'); + if (!p) + { + _gpgrt_log_printf ("%s", args); + break; + } + *p = 0; + _gpgrt_log_printf ("%s", args); + if (p[1] == '$') + { + _gpgrt_log_printf ("$"); + args = p+2; + continue; + } + if (p[1] != '{') + { + _gpgrt_log_printf ("$"); + args = p+1; + continue; + } + pend = strchr (p+2, '}'); + if (!pend) /* No closing brace. */ + { + _gpgrt_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; + _gpgrt_log_printf ("%s", arg->internal->username); + } + else if (!strcmp (p, "file")) + _gpgrt_log_printf ("%s", arg->internal->confname); + else if (!strcmp (p, "line")) + _gpgrt_log_printf ("%u", arg->lineno); + else if (!strcmp (p, "epoch")) + _gpgrt_log_printf ("%lu", (unsigned long)time (NULL)); + } + + leave: + _gpgrt_log_printf ("\n"); + return rc; } diff --git a/tests/etc/t-argparse.conf b/tests/etc/t-argparse.conf index 4562c7c..863f74e 100644 --- a/tests/etc/t-argparse.conf +++ b/tests/etc/t-argparse.conf @@ -3,10 +3,17 @@ # Options applied to all user's config files #verbose -# The meta comman decho simply prints the argument. With a hash + +# The meta command echo simply prints the argument. With a hash # prefix it does not prepend the file name and line number. For # future compatibility do not use doallar signs. [-echo Begin global config] + +[-echo use $${user} to echo the current user (${user})] +[-echo use $${file} to echo the current file (${file})] +[-echo use $${line} to echo the current line (${line})] +[-echo use $${epoch} to echo a timestamp (${epoch})] + [verbose] [user joy] @@ -16,7 +23,7 @@ # info is an alias for echo but only executed if a user secion is active. # For future compatibility do not use percent or dollar signs. -[-info In user Joy] +[-info In user Joy but real user id ${user}] [-verbose] [+force] diff --git a/tests/t-argparse.conf b/tests/t-argparse.conf index c689a63..f50712e 100644 --- a/tests/t-argparse.conf +++ b/tests/t-argparse.conf @@ -1,6 +1,6 @@ # User test config file for t-argparse -[-echo begin of user config] +[-echo begin of user config (user=${user})] [+verbose] # Options applied to all user's config files echo |