commit:     57e49de5c210c8def6f4957a6f94a62c60f98e52
Author:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Sat Nov  9 10:11:04 2019 +0000
Commit:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Sat Nov  9 10:11:04 2019 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=57e49de5

q: add option to print envvars, much like portageq envvar

Option -e to q will dump the env variables used by portage-utils.

Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org>

 main.c  | 86 +++++++++++++++++++++++++++--------------------------------------
 main.h  | 23 +++++++++++++++---
 man/q.1 |  7 ++++--
 q.c     | 59 +++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 118 insertions(+), 57 deletions(-)

diff --git a/main.c b/main.c
index 0b827fc..f8ba7af 100644
--- a/main.c
+++ b/main.c
@@ -393,19 +393,6 @@ strincr_var(const char *name, const char *s, char **value, 
size_t *value_len)
        /* we should sort here */
 }
 
-typedef enum { _Q_BOOL, _Q_STR, _Q_ISTR } var_types;
-typedef struct {
-       const char *name;
-       const size_t name_len;
-       const var_types type;
-       union {
-               char **s;
-               bool *b;
-       } value;
-       size_t value_len;
-       const char *default_value;
-} env_vars;
-
 static env_vars *
 get_portage_env_var(env_vars *vars, const char *name)
 {
@@ -619,49 +606,48 @@ read_portage_profile(const char *configroot, const char 
*profile, env_vars vars[
        free(profile_file);
 }
 
-static void
-initialize_portage_env(void)
-{
-       size_t i;
-       const char *s;
-
-       bool nocolor = 0;
-
-       env_vars *var;
-       env_vars vars_to_read[] = {
+static bool nocolor = 0;
+env_vars vars_to_read[] = {
 #define _Q_EV(t, V, set, lset, d) \
-       { \
-               .name = #V, \
-               .name_len = strlen(#V), \
-               .type = _Q_##t, \
-               set, \
-               lset, \
-               .default_value = d, \
-       },
+{ \
+       .name = #V, \
+       .name_len = strlen(#V), \
+       .type = _Q_##t, \
+       set, \
+       lset, \
+       .default_value = d, \
+},
 #define _Q_EVS(t, V, v, d) _Q_EV(t, V, .value.s = &v, .value_len = strlen(d), 
d)
 #define _Q_EVB(t, V, v, d) _Q_EV(t, V, .value.b = &v, .value_len = 0, d)
 
-               _Q_EVS(STR,  ROOT,                portroot,            "/")
-               _Q_EVS(STR,  ACCEPT_LICENSE,      accept_license,      "")
-               _Q_EVS(ISTR, INSTALL_MASK,        install_mask,        "")
-               _Q_EVS(ISTR, PKG_INSTALL_MASK,    pkg_install_mask,    "")
-               _Q_EVS(STR,  ARCH,                portarch,            "")
-               _Q_EVS(ISTR, CONFIG_PROTECT,      config_protect,      "/etc")
-               _Q_EVS(ISTR, CONFIG_PROTECT_MASK, config_protect_mask, "")
-               _Q_EVB(BOOL, NOCOLOR,             nocolor,             0)
-               _Q_EVS(ISTR, FEATURES,            features,            "")
-               _Q_EVS(STR,  EPREFIX,             eprefix,             
CONFIG_EPREFIX)
-               _Q_EVS(STR,  EMERGE_LOG_DIR,      portlogdir,          
CONFIG_EPREFIX "var/log")
-               _Q_EVS(STR,  PORTDIR,             main_overlay,        
CONFIG_EPREFIX "var/db/repos/gentoo")
-               _Q_EVS(STR,  PORTAGE_BINHOST,     binhost,             
DEFAULT_PORTAGE_BINHOST)
-               _Q_EVS(STR,  PORTAGE_TMPDIR,      port_tmpdir,         
CONFIG_EPREFIX "var/tmp/portage/")
-               _Q_EVS(STR,  PKGDIR,              pkgdir,              
CONFIG_EPREFIX "var/cache/binpkgs/")
-               _Q_EVS(STR,  Q_VDB,               portvdb,             
CONFIG_EPREFIX "var/db/pkg")
-               _Q_EVS(STR,  Q_EDB,               portedb,             
CONFIG_EPREFIX "var/cache/edb")
-               { NULL, 0, _Q_BOOL, { NULL }, 0, NULL, }
+       _Q_EVS(STR,  ROOT,                portroot,            "/")
+       _Q_EVS(STR,  ACCEPT_LICENSE,      accept_license,      "")
+       _Q_EVS(ISTR, INSTALL_MASK,        install_mask,        "")
+       _Q_EVS(ISTR, PKG_INSTALL_MASK,    pkg_install_mask,    "")
+       _Q_EVS(STR,  ARCH,                portarch,            "")
+       _Q_EVS(ISTR, CONFIG_PROTECT,      config_protect,      "/etc")
+       _Q_EVS(ISTR, CONFIG_PROTECT_MASK, config_protect_mask, "")
+       _Q_EVB(BOOL, NOCOLOR,             nocolor,             0)
+       _Q_EVS(ISTR, FEATURES,            features,            "")
+       _Q_EVS(STR,  EPREFIX,             eprefix,             CONFIG_EPREFIX)
+       _Q_EVS(STR,  EMERGE_LOG_DIR,      portlogdir,          CONFIG_EPREFIX 
"var/log")
+       _Q_EVS(STR,  PORTDIR,             main_overlay,        CONFIG_EPREFIX 
"var/db/repos/gentoo")
+       _Q_EVS(STR,  PORTAGE_BINHOST,     binhost,             
DEFAULT_PORTAGE_BINHOST)
+       _Q_EVS(STR,  PORTAGE_TMPDIR,      port_tmpdir,         CONFIG_EPREFIX 
"var/tmp/portage/")
+       _Q_EVS(STR,  PKGDIR,              pkgdir,              CONFIG_EPREFIX 
"var/cache/binpkgs/")
+       _Q_EVS(STR,  Q_VDB,               portvdb,             CONFIG_EPREFIX 
"var/db/pkg")
+       _Q_EVS(STR,  Q_EDB,               portedb,             CONFIG_EPREFIX 
"var/cache/edb")
+       { NULL, 0, _Q_BOOL, { NULL }, 0, NULL, }
 
 #undef _Q_EV
-       };
+};
+
+static void
+initialize_portage_env(void)
+{
+       size_t i;
+       const char *s;
+       env_vars *var;
 
        /* initialize all the strings with their default value */
        for (i = 0; vars_to_read[i].name; ++i) {

diff --git a/main.h b/main.h
index 88412a8..ad1467b 100644
--- a/main.h
+++ b/main.h
@@ -14,15 +14,16 @@
 # include "config.h"  /* make sure we have EPREFIX, if set */
 #endif
 
+#include <errno.h>
+#include <limits.h>
+#include <stdbool.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <errno.h>
-#include <stdint.h>
-#include <limits.h>
 
-#include "i18n.h"
 #include "colors.h"
+#include "i18n.h"
 
 extern const char *argv0;
 
@@ -134,4 +135,18 @@ extern FILE *warnout;
 #define errp(fmt, args...) _err(warnp, fmt , ## args)
 #define errfp(fmt, args...) _err(warnfp, fmt, ## args)
 
+typedef enum { _Q_BOOL, _Q_STR, _Q_ISTR } var_types;
+typedef struct {
+       const char *name;
+       const size_t name_len;
+       const var_types type;
+       union {
+               char **s;
+               bool *b;
+       } value;
+       size_t value_len;
+       const char *default_value;
+} env_vars;
+extern env_vars vars_to_read[];
+
 #endif

diff --git a/man/q.1 b/man/q.1
index 4e6264c..95caf56 100644
--- a/man/q.1
+++ b/man/q.1
@@ -1,5 +1,5 @@
 .\" generated by mkman.py, please do NOT edit!
-.TH q "1" "Jul 2019" "Gentoo Foundation" "q"
+.TH q "1" "Nov 2019" "Gentoo Foundation" "q"
 .SH NAME
 q \- invoke a portage utility applet
 .SH SYNOPSIS
@@ -22,6 +22,9 @@ Install symlinks for applets.
 \fB\-o\fR, \fB\-\-overlays\fR
 Print available overlays (read from repos.conf).
 .TP
+\fB\-e\fR, \fB\-\-envvar\fR
+Print found environment variables and their values.
+.TP
 \fB\-\-root\fR \fI<arg>\fR
 Set the ROOT env var.
 .TP
@@ -53,7 +56,7 @@ Print version and exit.
      qlop <pkgname>       : emerge log analyzer
  qmanifest <misc args>     : verify or generate thick Manifest files
    qmerge <pkgnames>      : fetch and merge binary package
-     qpkg <misc args>     : manipulate Gentoo binpkgs
+     qpkg <misc args>     : create or manipulate Gentoo binpkgs
   qsearch <regex>         : search pkgname/desc
     qsize <pkgname>       : calculate size usage
     qtbz2 <misc args>     : manipulate tbz2 packages

diff --git a/q.c b/q.c
index c03fd66..2fb406f 100644
--- a/q.c
+++ b/q.c
@@ -23,15 +23,17 @@
 #include "eat_file.h"
 #include "rmspace.h"
 
-#define Q_FLAGS "io" COMMON_FLAGS
+#define Q_FLAGS "ioe" COMMON_FLAGS
 static struct option const q_long_opts[] = {
        {"install",       no_argument, NULL, 'i'},
        {"overlays",      no_argument, NULL, 'o'},
+       {"envvar",        no_argument, NULL, 'e'},
        COMMON_LONG_OPTS
 };
 static const char * const q_opts_help[] = {
        "Install symlinks for applets",
        "Print available overlays (read from repos.conf)",
+       "Print found environment variables and their values",
        COMMON_OPTS_HELP
 };
 #define q_usage(ret) usage(ret, Q_FLAGS, q_long_opts, q_opts_help, NULL, 
lookup_applet_idx("q"))
@@ -78,6 +80,7 @@ int q_main(int argc, char **argv)
        int i;
        bool install;
        bool print_overlays;
+       bool print_vars;
        const char *p;
        APPLET func;
 
@@ -96,11 +99,13 @@ int q_main(int argc, char **argv)
 
        install = false;
        print_overlays = false;
+       print_vars = false;
        while ((i = GETOPT_LONG(Q, q, "+")) != -1) {
                switch (i) {
                COMMON_GETOPTS_CASES(q)
                case 'i': install = true;        break;
                case 'o': print_overlays = true; break;
+               case 'e': print_vars = true;     break;
                }
        }
 
@@ -187,6 +192,58 @@ int q_main(int argc, char **argv)
                return 0;
        }
 
+       if (print_vars) {
+               env_vars *var;
+               int j;
+
+               if (argc == optind || argc - optind > 1) {
+                       for (i = 0; vars_to_read[i].name; i++) {
+                               var = &vars_to_read[i];
+
+                               /* check if we want this variable */
+                               for (j = optind; j < argc; j++)
+                                       if (strcmp(var->name, argv[j]) == 0)
+                                               break;
+                               if (j == argc && optind != argc)
+                                       continue;
+
+                               printf("%s%s%s=", BLUE, var->name, NORM);
+                               switch (var->type) {
+                                       case _Q_BOOL:
+                                               printf("%s%s%s\n",
+                                                               YELLOW, 
*var->value.b ? "1" : "0", NORM);
+                                               break;
+                               case _Q_STR:
+                               case _Q_ISTR:
+                                               printf("%s\"%s\"%s\n", RED, 
*var->value.s, NORM);
+                                               break;
+                               }
+                       }
+               } else {
+                       /* single envvar printing, just output the value, like
+                        * portageq envvar does */
+                       for (i = 0; vars_to_read[i].name; i++) {
+                               var = &vars_to_read[i];
+
+                               if (strcmp(var->name, argv[optind]) != 0)
+                                       continue;
+
+                               switch (var->type) {
+                                       case _Q_BOOL:
+                                               printf("%s%s%s\n",
+                                                               YELLOW, 
*var->value.b ? "1" : "0", NORM);
+                                               break;
+                               case _Q_STR:
+                               case _Q_ISTR:
+                                               printf("%s%s%s\n", RED, 
*var->value.s, NORM);
+                                               break;
+                               }
+                       }
+               }
+
+               return 0;
+       }
+
        if (argc == optind)
                q_usage(EXIT_FAILURE);
        if ((func = lookup_applet(argv[optind])) == NULL)

Reply via email to