commit:     b1558916d2ca76d7cd4c81248d5b220aaa46a728
Author:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
AuthorDate: Sat Nov 26 23:15:22 2016 +0000
Commit:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
CommitDate: Sat Nov 26 23:15:22 2016 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=b1558916

--quiet: do not suppress fatal error messages

Rework how we write to stderr so that --quiet will automatically
suppress warnings but not any fatal error messages.

URL: https://bugs.gentoo.org/585248
Reported-by: Ulrich Müller <ulm <AT> gentoo.org>

 libq/libq.h |  4 +++-
 main.c      | 34 +++++++++++++++++-----------------
 qdepends.c  |  2 +-
 3 files changed, 21 insertions(+), 19 deletions(-)

diff --git a/libq/libq.h b/libq/libq.h
index 5cc3689..6a74a48 100644
--- a/libq/libq.h
+++ b/libq/libq.h
@@ -1,9 +1,10 @@
 /* we need the space before the last comma or we trigger a bug in gcc-2 :( */
+FILE *warnout;
 #if defined OPTIMIZE_FOR_SIZE && (OPTIMIZE_FOR_SIZE > 1)
 #define warn(fmt, args...)
 #else
 #define warn(fmt, args...) \
-       fprintf(stderr, _("%s%s%s: " fmt "\n"), RED, argv0, NORM , ## args)
+       fprintf(warnout, _("%s%s%s: " fmt "\n"), RED, argv0, NORM , ## args)
 #endif
 #define warnf(fmt, args...) warn("%s%s()%s: " fmt, YELLOW, __func__, NORM , ## 
args)
 #define warnl(fmt, args...) warn("%s%i()%s: " fmt, YELLOW, __LINE__, NORM , ## 
args)
@@ -11,6 +12,7 @@
 #define warnfp(fmt, args...) warnf(fmt ": %s" , ## args , strerror(errno))
 #define _err(wfunc, fmt, args...) \
        do { \
+       warnout = stderr; \
        wfunc(fmt , ## args); \
        exit(EXIT_FAILURE); \
        } while (0)

diff --git a/main.c b/main.c
index 76e5bad..8e0e2ad 100644
--- a/main.c
+++ b/main.c
@@ -107,7 +107,7 @@ void no_colors(void)
 #define COMMON_GETOPTS_CASES(applet) \
        case 0x1: portroot = optarg; break; \
        case 'v': ++verbose; break; \
-       case 'q': ++quiet; if (freopen("/dev/null", "w", stderr)) { /* ignore 
errors */ } break; \
+       case 'q': ++quiet; warnout = fopen("/dev/null", "we"); break; \
        case 'V': version_barf(); break; \
        case 'h': applet ## _usage(EXIT_SUCCESS); break; \
        case 'C': no_colors(); break; \
@@ -121,31 +121,31 @@ static void usage(int status, const char *flags, struct 
option const opts[],
        const char a_arg[] = "<arg>";
        size_t a_arg_len = strlen(a_arg) + 1;
        size_t i, optlen;
+       FILE *fp = status == EXIT_SUCCESS ? stdout : warnout;
 
-       if (status != EXIT_SUCCESS)
-               dup2(STDERR_FILENO, STDOUT_FILENO);
        if (blabber == 0) {
-               printf("%sUsage:%s %sq%s %s<applet> <args>%s  : %s"
+               fprintf(fp, "%sUsage:%s %sq%s %s<applet> <args>%s  : %s"
                        "invoke a portage utility applet\n\n", GREEN,
                        NORM, YELLOW, NORM, DKBLUE, RED, NORM);
-               printf("%sCurrently defined applets:%s\n", GREEN, NORM);
+               fprintf(fp, "%sCurrently defined applets:%s\n", GREEN, NORM);
                for (i = 0; applets[i].desc; ++i)
                        if (applets[i].func)
-                               printf(" %s%8s%s %s%-16s%s%s:%s %s\n",
+                               fprintf(fp, " %s%8s%s %s%-16s%s%s:%s %s\n",
                                        YELLOW, applets[i].name, NORM,
                                        DKBLUE, applets[i].opts, NORM,
                                        RED, NORM, _(applets[i].desc));
        } else if (blabber > 0) {
-               printf("%sUsage:%s %s%s%s [opts] %s%s%s %s:%s %s\n",
+               fprintf(fp, "%sUsage:%s %s%s%s [opts] %s%s%s %s:%s %s\n",
                        GREEN, NORM,
                        YELLOW, applets[blabber].name, NORM,
                        DKBLUE, applets[blabber].opts, NORM,
                        RED, NORM, _(applets[blabber].desc));
                if (desc)
-                       printf("\n%s\n", desc);
+                       fprintf(fp, "\n%s\n", desc);
        }
        if (module_name != NULL)
-               printf("%sLoaded module:%s\n%s%8s%s %s<args>%s\n", GREEN, NORM, 
YELLOW, module_name, NORM, DKBLUE, NORM);
+               fprintf(fp, "%sLoaded module:%s\n%s%8s%s %s<args>%s\n",
+                       GREEN, NORM, YELLOW, module_name, NORM, DKBLUE, NORM);
 
        /* Prescan the --long opt length to auto-align. */
        optlen = 0;
@@ -156,23 +156,23 @@ static void usage(int status, const char *flags, struct 
option const opts[],
                optlen = MAX(l, optlen);
        }
 
-       printf("\n%sOptions:%s -[%s]\n", GREEN, NORM, flags);
+       fprintf(fp, "\n%sOptions:%s -[%s]\n", GREEN, NORM, flags);
        for (i = 0; opts[i].name; ++i) {
                /* this assert is a life saver when adding new applets. */
                assert(help[i] != NULL);
 
                /* first output the short flag if it has one */
                if (opts[i].val > '~' || opts[i].val < ' ')
-                       printf("      ");
+                       fprintf(fp, "      ");
                else
-                       printf("  -%c, ", opts[i].val);
+                       fprintf(fp, "  -%c, ", opts[i].val);
 
                /* then the long flag + help text */
                if (opts[i].has_arg == no_argument)
-                       printf("--%-*s %s*%s %s\n", (int)optlen, opts[i].name,
+                       fprintf(fp, "--%-*s %s*%s %s\n", (int)optlen, 
opts[i].name,
                                RED, NORM, _(help[i]));
                else
-                       printf("--%s %s%s%s%*s %s*%s %s\n",
+                       fprintf(fp, "--%s %s%s%s%*s %s*%s %s\n",
                                opts[i].name,
                                DKBLUE, (opts[i].has_arg == a_argument ? a_arg 
: opt_arg), NORM,
                                (int)(optlen - strlen(opts[i].name) - 
a_arg_len), "",
@@ -985,9 +985,8 @@ initialize_flat(const char *overlay, int cache_type, bool 
force)
                                return cache_file;
        }
 
-       if (!quiet)
-               warn("Updating ebuild %scache in %s ... ",
-                       cache_type == CACHE_EBUILD ? "" : "meta", overlay);
+       warn("Updating ebuild %scache in %s ... ",
+               cache_type == CACHE_EBUILD ? "" : "meta", overlay);
 
        count = frac = secs = 0;
 
@@ -1421,6 +1420,7 @@ void cleanup(void)
 int main(int argc, char **argv)
 {
        struct stat st;
+       warnout = stderr;
        IF_DEBUG(init_coredumps());
        argv0 = argv[0];
 

diff --git a/qdepends.c b/qdepends.c
index 7337c61..3c90863 100644
--- a/qdepends.c
+++ b/qdepends.c
@@ -596,7 +596,7 @@ int qdepends_main(int argc, char **argv)
                atom_implode(atom);
        xarrayfree_int(atoms);
 
-       if (!ret && !quiet)
+       if (!ret)
                warn("no matches found for your query");
        return ret ? EXIT_SUCCESS : EXIT_FAILURE;
 }

Reply via email to