commit:     c0b88fe2b32679f1548a280d906b82b1651cd804
Author:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Sun Jun  9 09:51:26 2019 +0000
Commit:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Sun Jun  9 09:51:26 2019 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=c0b88fe2

qgrep: print filename for matches by default, fix atom_compare call

remove very similar vdb callback in favour of generic tree one
format atoms using atom_format for consistency and ease of use
retrieve full atoms when SLOT or REPO matches are attempted

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

 man/include/qgrep.optdesc.yaml |   2 +-
 man/qgrep.1                    |  41 ++++++++---------
 qgrep.c                        | 100 +++++++++++------------------------------
 3 files changed, 47 insertions(+), 96 deletions(-)

diff --git a/man/include/qgrep.optdesc.yaml b/man/include/qgrep.optdesc.yaml
index ad3874d..9716bf0 100644
--- a/man/include/qgrep.optdesc.yaml
+++ b/man/include/qgrep.optdesc.yaml
@@ -1,4 +1,4 @@
 verbose: |
     Prefix each matching line with filename (like \fB-H\fR).  When this
     option is given multiple times, also linenumbers are printed.
-quiet: Ignored for compatibility with other qapplets.
+quiet: Do not prefix each match with filename.

diff --git a/man/qgrep.1 b/man/qgrep.1
index 180b5dd..6701061 100644
--- a/man/qgrep.1
+++ b/man/qgrep.1
@@ -1,5 +1,5 @@
 .\" generated by mkman.py, please do NOT edit!
-.TH qgrep "1" "May 2019" "Gentoo Foundation" "qgrep"
+.TH qgrep "1" "Jun 2019" "Gentoo Foundation" "qgrep"
 .SH NAME
 qgrep \- grep in ebuilds
 .SH SYNOPSIS
@@ -20,66 +20,63 @@ Select non-matching lines.
 \fB\-i\fR, \fB\-\-ignore\-case\fR
 Ignore case distinctions.
 .TP
-\fB\-H\fR, \fB\-\-with\-filename\fR
-Print the filename for each match.
-.TP
 \fB\-N\fR, \fB\-\-with\-name\fR
-Print the package or eclass name for each match.
+Print the filename for each match.
 .TP
 \fB\-c\fR, \fB\-\-count\fR
-Only print a count of matching lines per FILE.
+Print the package or eclass name for each match.
 .TP
 \fB\-l\fR, \fB\-\-list\fR
-Only print FILE names containing matches.
+Only print a count of matching lines per FILE.
 .TP
 \fB\-L\fR, \fB\-\-invert\-list\fR
-Only print FILE names containing no match.
+Only print FILE names containing matches.
 .TP
 \fB\-e\fR, \fB\-\-regexp\fR
-Use PATTERN as a regular expression.
+Only print FILE names containing no match.
 .TP
 \fB\-x\fR, \fB\-\-extended\fR
-Use PATTERN as an extended regular expression.
+Use PATTERN as a regular expression.
 .TP
 \fB\-J\fR, \fB\-\-installed\fR
-Search in installed ebuilds instead of the tree.
+Use PATTERN as an extended regular expression.
 .TP
 \fB\-E\fR, \fB\-\-eclass\fR
-Search in eclasses instead of ebuilds.
+Search in installed ebuilds instead of the tree.
 .TP
 \fB\-s\fR, \fB\-\-skip\-comments\fR
-Skip comments lines.
+Search in eclasses instead of ebuilds.
 .TP
 \fB\-R\fR, \fB\-\-repo\fR
-Print source repository name for each match (implies -N).
+Skip comments lines.
 .TP
 \fB\-S\fR \fI<arg>\fR, \fB\-\-skip\fR \fI<arg>\fR
-Skip lines matching <arg>.
+Print source repository name for each match (implies -N).
 .TP
 \fB\-B\fR \fI<arg>\fR, \fB\-\-before\fR \fI<arg>\fR
-Print <arg> lines of leading context.
+Skip lines matching <arg>.
 .TP
 \fB\-A\fR \fI<arg>\fR, \fB\-\-after\fR \fI<arg>\fR
-Print <arg> lines of trailing context.
+Print <arg> lines of leading context.
 .TP
 \fB\-\-root\fR \fI<arg>\fR
-Set the ROOT env var.
+Print <arg> lines of trailing context.
 .TP
 \fB\-v\fR, \fB\-\-verbose\fR
 Prefix each matching line with filename (like \fB-H\fR).  When this
 option is given multiple times, also linenumbers are printed.
 .TP
 \fB\-q\fR, \fB\-\-quiet\fR
-Ignored for compatibility with other qapplets.
+Do not prefix each match with filename.
 .TP
 \fB\-C\fR, \fB\-\-nocolor\fR
-Don't output color.
+Tighter output; suppress warnings.
 .TP
 \fB\-h\fR, \fB\-\-help\fR
-Print this help and exit.
+Don't output color.
 .TP
 \fB\-V\fR, \fB\-\-version\fR
-Print version and exit.
+Print this help and exit.
 
 .SH "REPORTING BUGS"
 Please report bugs via http://bugs.gentoo.org/

diff --git a/qgrep.c b/qgrep.c
index dff959b..058026e 100644
--- a/qgrep.c
+++ b/qgrep.c
@@ -24,11 +24,10 @@
 #include "xchdir.h"
 #include "xregex.h"
 
-#define QGREP_FLAGS "IiHNclLexJEsRS:B:A:" COMMON_FLAGS
+#define QGREP_FLAGS "IiNclLexJEsRS:B:A:" COMMON_FLAGS
 static struct option const qgrep_long_opts[] = {
        {"invert-match",  no_argument, NULL, 'I'},
        {"ignore-case",   no_argument, NULL, 'i'},
-       {"with-filename", no_argument, NULL, 'H'},
        {"with-name",     no_argument, NULL, 'N'},
        {"count",         no_argument, NULL, 'c'},
        {"list",          no_argument, NULL, 'l'},
@@ -398,6 +397,8 @@ qgrep_cache_cb(tree_pkg_ctx *pkg_ctx, void *priv)
        if (data->include_atoms != NULL) {
                depend_atom **d;
                for (d = data->include_atoms; *d != NULL; d++) {
+                       if ((*d)->SLOT != NULL || (*d)->REPO != NULL)
+                               patom = tree_get_atom(pkg_ctx, true);
                        if (atom_compare(patom, *d) == EQUAL)
                                break;
                }
@@ -409,6 +410,8 @@ qgrep_cache_cb(tree_pkg_ctx *pkg_ctx, void *priv)
        cctx = (tree_ctx *)(pkg_ctx->cat_ctx->ctx);
        if (cctx->cachetype == CACHE_EBUILD) {
                pfd = cctx->tree_fd;
+       } else if (cctx->cachetype == CACHE_VDB) {
+               pfd = openat(cctx->portroot_fd, data->portdir, 
O_RDONLY|O_CLOEXEC);
        } else {
                pfd = openat(cctx->tree_fd, "../..", O_RDONLY|O_CLOEXEC);
        }
@@ -419,11 +422,9 @@ qgrep_cache_cb(tree_pkg_ctx *pkg_ctx, void *priv)
 
        label = NULL;
        if (data->show_name) {
-               /* this is a super-optimisation, should get it from the full 
atom */
-               char *repo = data->show_repo ? cctx->repo : NULL;
-               snprintf(name, sizeof(name), "%s%s/%s%s%s%s%s%s",
-                               BOLD, patom->CATEGORY, BLUE, patom->P, GREEN,
-                               repo ? "::" : "", repo ? repo : "", NORM);
+               if (data->show_repo)
+                       patom = tree_get_atom(pkg_ctx, true);
+               atom_format_r(name, sizeof(name), "%[CATEGORY]%[P]%[REPO]", 
patom, 0);
                label = name;
        } else if (data->show_filename) {
                label = buf;
@@ -435,55 +436,6 @@ qgrep_cache_cb(tree_pkg_ctx *pkg_ctx, void *priv)
        return ret;
 }
 
-static int
-qgrep_vdb_cb(tree_pkg_ctx *pkg_ctx, void *priv)
-{
-       struct qgrep_grepargs *data = (struct qgrep_grepargs *)priv;
-       char buf[_Q_PATH_MAX];
-       char name[_Q_PATH_MAX];
-       char *label;
-       depend_atom *patom = NULL;
-       int ret;
-       int pfd;
-
-       patom = tree_get_atom(pkg_ctx, false);
-       if (patom == NULL)
-               return EXIT_FAILURE;
-
-       if (data->include_atoms != NULL) {
-               depend_atom **d;
-               for (d = data->include_atoms; *d != NULL; d++) {
-                       if (atom_compare(patom, *d) == EQUAL)
-                               break;
-               }
-               if (*d == NULL) {
-                       atom_implode(patom);
-                       return EXIT_FAILURE;
-               }
-       }
-
-       /* get path to portdir */
-       pfd = openat(pkg_ctx->cat_ctx->ctx->portroot_fd,
-                       data->portdir, O_RDONLY|O_CLOEXEC);
-
-       /* cat/pkg/pkg-ver.ebuild */
-       snprintf(buf, sizeof(buf), "%s/%s/%s.ebuild",
-                       patom->CATEGORY, patom->PN, patom->P);
-
-       label = NULL;
-       if (data->show_name) {
-               snprintf(name, sizeof(name), "%s%s/%s%s%s",
-                               BOLD, patom->CATEGORY, BLUE, patom->P, NORM);
-               label = name;
-       } else if (data->show_filename) {
-               label = buf;
-       }
-
-       ret = qgrep_grepat(pfd, buf, label, data);
-
-       return ret;
-}
-
 int qgrep_main(int argc, char **argv)
 {
        int i;
@@ -499,14 +451,14 @@ int qgrep_main(int argc, char **argv)
        char *overlay;
 
        struct qgrep_grepargs args = {
-               .do_count = 0,
-               .do_regex = 0,
-               .do_list = 0,
-               .show_filename = 0,
-               .show_name = 0,
-               .skip_comments = 0,
-               .invert_list = 0,
-               .invert_match = 0,
+               .do_count = false,
+               .do_regex = false,
+               .do_list = false,
+               .show_filename = true,
+               .show_name = false,
+               .skip_comments = false,
+               .invert_list = false,
+               .invert_match = false,
                .skip_pattern = NULL,
                .num_lines_before = 0,
                .num_lines_after = 0,
@@ -536,8 +488,8 @@ int qgrep_main(int argc, char **argv)
                        break;
                case 'J': do_installed = true;                    break;
                case 'E': do_eclass = true;                       break;
-               case 'H': args.show_filename = true;              break;
-               case 'N': args.show_name = true;                  break;
+               case 'N': args.show_name = true;
+                                 args.show_filename = false;             break;
                case 's': args.skip_comments = true;              break;
                case 'R': args.show_repo = args.show_name = true; break;
                case 'S': args.skip_pattern = optarg;             break;
@@ -562,6 +514,9 @@ int qgrep_main(int argc, char **argv)
        if (argc == optind)
                qgrep_usage(EXIT_FAILURE);
 
+       if (quiet)
+               args.show_filename = args.show_name = false;
+
        if (args.do_list && args.do_count) {
                warn("%s and --count are incompatible options. The former 
wins.",
                                (args.invert_list ? "--invert-list" : 
"--list"));
@@ -677,14 +632,13 @@ int qgrep_main(int argc, char **argv)
                                status = qgrep_grepat(efd, dentry->d_name, 
label, &args);
                        }
                        closedir(eclass_dir);
-               } else if (do_installed) {
-                       tree_ctx *t = tree_open_vdb(portroot, portvdb);
-                       if (t != NULL) {
-                               status = tree_foreach_pkg_fast(t, qgrep_vdb_cb, 
&args, NULL);
-                               tree_close(t);
+               } else { /* do_ebuild || do_installed */
+                       tree_ctx *t;
+                       if (do_installed) {
+                               t = tree_open_vdb(portroot, portvdb);
+                       } else {
+                               t = tree_open(portroot, overlay);
                        }
-               } else { /* do_ebuild */
-                       tree_ctx *t = tree_open(portroot, overlay);
                        if (t != NULL) {
                                status = tree_foreach_pkg_fast(t, 
qgrep_cache_cb, &args, NULL);
                                tree_close(t);

Reply via email to