Codebase list libgpg-error / 98d11ef
core: Improve the echo and info meta commands of the arg parser * src/argparse.c (handle_meta_echo): Substitue some vars. (handle_meta_user): Factor some code out to ... (assure_username): new. -- Yeah, that is not really needed but might sometimes be helpful. Signed-off-by: Werner Koch <wk@gnupg.org> Werner Koch 4 years ago
3 changed file(s) with 86 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
3333 #include <limits.h>
3434 #include <errno.h>
3535 #include <unistd.h>
36 #include <time.h>
3637
3738 #include "gpgrt-int.h"
3839
662663 }
663664
664665
665 /* Implementation of the "user" command. ARG is the context. ARGS is
666 * a non-empty string which this function is allowed to modify. */
666 /* Make sure the username field is filled. Return 0 on success. */
667667 static int
668 handle_meta_user (gpgrt_argparse_t *arg, unsigned int alternate, char *args)
669 {
670 (void)alternate;
671
668 assure_username (gpgrt_argparse_t *arg)
669 {
672670 if (!arg->internal->username)
673671 {
674672 arg->internal->username = _gpgrt_getusername ();
683681 return ARGPARSE_PERMISSION_ERROR;
684682 }
685683 }
686
684 return 0;
685 }
686
687
688 /* Implementation of the "user" command. ARG is the context. ARGS is
689 * a non-empty string which this function is allowed to modify. */
690 static int
691 handle_meta_user (gpgrt_argparse_t *arg, unsigned int alternate, char *args)
692 {
693 int rc;
694
695 (void)alternate;
696
697 rc = assure_username (arg);
698 if (rc)
699 return rc;
687700
688701 arg->internal->user_seen = 1;
689702 if (*args == '*' && !args[1])
756769 static int
757770 handle_meta_echo (gpgrt_argparse_t *arg, unsigned int alternate, char *args)
758771 {
772 int rc = 0;
773 char *p, *pend;
774
759775 if (alternate)
760 _gpgrt_log_info ("%s\n", args);
776 _gpgrt_log_info ("%s", "");
761777 else
762 _gpgrt_log_info ("%s:%u: %s\n",
763 arg->internal->confname, arg->lineno, args);
764 return 0;
778 _gpgrt_log_info ("%s:%u: ", arg->internal->confname, arg->lineno);
779
780 while (*args)
781 {
782 p = strchr (args, '$');
783 if (!p)
784 {
785 _gpgrt_log_printf ("%s", args);
786 break;
787 }
788 *p = 0;
789 _gpgrt_log_printf ("%s", args);
790 if (p[1] == '$')
791 {
792 _gpgrt_log_printf ("$");
793 args = p+2;
794 continue;
795 }
796 if (p[1] != '{')
797 {
798 _gpgrt_log_printf ("$");
799 args = p+1;
800 continue;
801 }
802 pend = strchr (p+2, '}');
803 if (!pend) /* No closing brace. */
804 {
805 _gpgrt_log_printf ("$");
806 args = p+1;
807 continue;
808 }
809 p += 2;
810 *pend = 0;
811 args = pend+1;
812 if (!strcmp (p, "user"))
813 {
814 rc = assure_username (arg);
815 if (rc)
816 goto leave;
817 _gpgrt_log_printf ("%s", arg->internal->username);
818 }
819 else if (!strcmp (p, "file"))
820 _gpgrt_log_printf ("%s", arg->internal->confname);
821 else if (!strcmp (p, "line"))
822 _gpgrt_log_printf ("%u", arg->lineno);
823 else if (!strcmp (p, "epoch"))
824 _gpgrt_log_printf ("%lu", (unsigned long)time (NULL));
825 }
826
827 leave:
828 _gpgrt_log_printf ("\n");
829 return rc;
765830 }
766831
767832
22 # Options applied to all user's config files
33 #verbose
44
5 # The meta comman decho simply prints the argument. With a hash
5
6 # The meta command echo simply prints the argument. With a hash
67 # prefix it does not prepend the file name and line number. For
78 # future compatibility do not use doallar signs.
89 [-echo Begin global config]
10
11 [-echo use $${user} to echo the current user (${user})]
12 [-echo use $${file} to echo the current file (${file})]
13 [-echo use $${line} to echo the current line (${line})]
14 [-echo use $${epoch} to echo a timestamp (${epoch})]
15
916 [verbose]
1017
1118 [user joy]
1522
1623 # info is an alias for echo but only executed if a user secion is active.
1724 # For future compatibility do not use percent or dollar signs.
18 [-info In user Joy]
25 [-info In user Joy but real user id ${user}]
1926
2027 [-verbose]
2128 [+force]
00 # User test config file for t-argparse
11
2 [-echo begin of user config]
2 [-echo begin of user config (user=${user})]
33 [+verbose]
44 # Options applied to all user's config files
55 echo