commit:     16215c71c61da9cb44868d58b4c3ce0529c5d4ac
Author:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Sun Jan 19 09:48:50 2020 +0000
Commit:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Sun Jan 19 09:48:50 2020 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=16215c71

qatom: add -l option to match an atom against the tree

returns the latest available version of the atom requested, or nothing
when not found

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

 man/qatom.1 |  5 ++++-
 qatom.c     | 33 +++++++++++++++++++++++++++------
 2 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/man/qatom.1 b/man/qatom.1
index 6254912..4f6ccf2 100644
--- a/man/qatom.1
+++ b/man/qatom.1
@@ -1,5 +1,5 @@
 .\" generated by mkman.py, please do NOT edit!
-.TH qatom "1" "Nov 2019" "Gentoo Foundation" "qatom"
+.TH qatom "1" "Jan 2020" "Gentoo Foundation" "qatom"
 .SH NAME
 qatom \- split atom strings
 .SH SYNOPSIS
@@ -70,6 +70,9 @@ Compare two atoms.
 \fB\-p\fR, \fB\-\-print\fR
 Print reconstructed atom.
 .TP
+\fB\-l\fR, \fB\-\-lookup\fR
+Lookup atom in tree.
+.TP
 \fB\-\-root\fR \fI<arg>\fR
 Set the ROOT env var.
 .TP

diff --git a/qatom.c b/qatom.c
index 23d10d8..59f7392 100644
--- a/qatom.c
+++ b/qatom.c
@@ -8,36 +8,37 @@
  */
 
 #include "main.h"
-
-#include <stdlib.h>
-#include <stdbool.h>
+#include "applets.h"
 
 #include "atom.h"
-#include "applets.h"
+#include "tree.h"
 
 #define QATOM_FORMAT "%{CATEGORY} %{PN} %{PV} %[PR] %[SLOT] %[pfx] %[sfx]"
 
-#define QATOM_FLAGS "F:cp" COMMON_FLAGS
+#define QATOM_FLAGS "F:cpl" COMMON_FLAGS
 static struct option const qatom_long_opts[] = {
        {"format",     a_argument, NULL, 'F'},
        {"compare",   no_argument, NULL, 'c'},
        {"print",     no_argument, NULL, 'p'},
+       {"lookup",    no_argument, NULL, 'l'},
        COMMON_LONG_OPTS
 };
 static const char * const qatom_opts_help[] = {
        "Custom output format (default: " QATOM_FORMAT ")",
        "Compare two atoms",
        "Print reconstructed atom",
+       "Lookup atom in tree",
        COMMON_OPTS_HELP
 };
 #define qatom_usage(ret) usage(ret, QATOM_FLAGS, qatom_long_opts, 
qatom_opts_help, NULL, lookup_applet_idx("qatom"))
 
 int qatom_main(int argc, char **argv)
 {
-       enum qatom_atom { _EXPLODE=0, _COMPARE, _PRINT } action = _EXPLODE;
+       enum qatom_atom { _EXPLODE=0, _COMPARE, _PRINT, _LOOKUP } action = 
_EXPLODE;
        const char *format = QATOM_FORMAT;
        depend_atom *atom;
        depend_atom *atomc;
+       tree_ctx *tree = NULL;
        int i;
 
        while ((i = GETOPT_LONG(QATOM, qatom, "")) != -1) {
@@ -45,6 +46,7 @@ int qatom_main(int argc, char **argv)
                case 'F': format = optarg;   break;
                case 'c': action = _COMPARE; break;
                case 'p': action = _PRINT;   break;
+               case 'l': action = _LOOKUP;  break;
                COMMON_GETOPTS_CASES(qatom)
                }
        }
@@ -55,6 +57,12 @@ int qatom_main(int argc, char **argv)
        if (action == _COMPARE && (argc - optind) % 2)
                err("compare needs even number of arguments");
 
+       if (action == _LOOKUP) {
+               tree = tree_open(portroot, main_overlay);
+               if (tree == NULL)
+                       err("failed to open tree");
+       }
+
        for (i = optind; i < argc; i++) {
                atom = atom_explode(argv[i]);
                if (atom == NULL) {
@@ -101,10 +109,23 @@ int qatom_main(int argc, char **argv)
                case _PRINT:
                        printf("%s\n", atom_to_string(atom));
                        break;
+               case _LOOKUP:
+                       {
+                               tree_pkg_ctx *pkg = tree_match_atom(tree, atom);
+                               if (pkg != NULL) {
+                                       atomc = tree_get_atom(pkg, true);
+                                       if (!quiet)
+                                               printf("%s: ", 
atom_to_string(atom));
+                                       printf("%s\n", atom_format(format, 
atomc));
+                               }
+                       }
                }
 
                atom_implode(atom);
        }
 
+       if (action == _LOOKUP)
+               tree_close(tree);
+
        return EXIT_SUCCESS;
 }

Reply via email to