commit:     8ca9d1625448544f72aa0d45a1cbbdfa8dcfb22e
Author:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Thu May 26 09:27:11 2022 +0000
Commit:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Thu May 26 09:27:11 2022 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=8ca9d162

libq/dep: add dep_resolve_tree function

allow resolving a dep-specification to atoms found in a tree, e.g. see
what would get selected

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

 libq/dep.c | 77 +++++++++++++++++++++++++++++++++++++++-----------------------
 libq/dep.h | 12 ++++++----
 2 files changed, 56 insertions(+), 33 deletions(-)

diff --git a/libq/dep.c b/libq/dep.c
index 99629e7..4138a1c 100644
--- a/libq/dep.c
+++ b/libq/dep.c
@@ -1,5 +1,5 @@
 /*
- * Copyright 2005-2019 Gentoo Foundation
+ * Copyright 2005-2022 Gentoo Foundation
  * Distributed under the terms of the GNU General Public License v2
  *
  * Copyright 2005-2010 Ned Ludd        - <[email protected]>
@@ -20,6 +20,7 @@
 #include "atom.h"
 #include "dep.h"
 #include "set.h"
+#include "tree.h"
 #include "xarray.h"
 #include "xasprintf.h"
 
@@ -62,8 +63,6 @@ static void
 _dep_burn_node(dep_node *node)
 {
        assert(node);
-       if (node->info_on_heap)
-               free(node->info);
        if (node->atom)
                atom_implode(node->atom);
        free(node);
@@ -237,7 +236,6 @@ dep_print_tree(
 {
        size_t s;
        int indent = 4;  /* Gentoo 4-wide indent standard */
-       depend_atom *d = NULL;
        bool singlechild = false;
        bool nonewline = false;
 
@@ -260,33 +258,31 @@ dep_print_tree(
        if (root->type == DEP_OR)
                fprintf(fp, "|| (");
        if (root->info) {
-               if (hlatoms != NULL && array_cnt(hlatoms) > 0 &&
-                               root->type == DEP_NORM)
-               {
-                       size_t i;
-                       depend_atom *m;
-                       char *oslot;
-
-                       d = root->atom;
-                       d->pfx_op = d->sfx_op = ATOM_OP_NONE;
-
-                       array_for_each(hlatoms, i, m) {
-                               oslot = d->SLOT;
-                               if (m->SLOT == NULL)
-                                       d->SLOT = NULL;
-
-                               if (atom_compare(m, d) == EQUAL) {
-                                       m = NULL;
-                                       break;
+               if (root->type == DEP_NORM) {
+                       bool dohl = false;
+
+                       if (hlatoms != NULL && array_cnt(hlatoms) > 0)
+                       {
+                               size_t       i;
+                               depend_atom *m;
+
+                               array_for_each(hlatoms, i, m) {
+                                       /* make m query, such that any 
specifics (SLOT,
+                                        * pfx/sfx) from the depstring are 
ignored while
+                                        * highlighting */
+                                       if (atom_compare(root->atom, m) == 
EQUAL) {
+                                               dohl = true;
+                                               break;
+                                       }
                                }
-                               d->SLOT = oslot;
                        }
 
-                       if (m == NULL) { /* match found */
-                               fprintf(fp, "%s%s%s", hlcolor, root->info, 
NORM);
-                       } else {
-                               fprintf(fp, "%s", root->info);
-                       }
+                       fprintf(fp, "%s%s%s",
+                                       dohl ? hlcolor : "",
+                                       atom_to_string(root->atom),
+                                       dohl ? NORM : "");
+                       if (root->atom_resolved && verbose > 0)
+                               fprintf(fp, "  # %s", root->info);
                } else {
                        fprintf(fp, "%s", root->info);
                }
@@ -355,6 +351,31 @@ dep_prune_use(dep_node *root, set *use)
                dep_prune_use(root->children, use);
 }
 
+void
+dep_resolve_tree(dep_node *root, tree_ctx *t)
+{
+       if (root->type != DEP_NULL) {
+               if (root->type == DEP_NORM && root->atom) {
+                       depend_atom    *d = root->atom;
+                       tree_match_ctx *r = tree_match_atom(t, d,
+                                                                               
                TREE_MATCH_DEFAULT |
+                                                                               
                TREE_MATCH_LATEST);
+                       if (r != NULL) {
+                               atom_implode(d);
+                               root->atom = atom_clone(r->atom);
+                               root->atom_resolved = 1;
+                               tree_match_close(r);
+                       }
+               }
+
+               if (root->children)
+                       dep_resolve_tree(root->children, t);
+       }
+
+       if (root->neighbor)
+               dep_resolve_tree(root->neighbor, t);
+}
+
 void
 dep_flatten_tree(const dep_node *root, array_t *out)
 {

diff --git a/libq/dep.h b/libq/dep.h
index 1055d29..68d5c75 100644
--- a/libq/dep.h
+++ b/libq/dep.h
@@ -1,5 +1,5 @@
 /*
- * Copyright 2005-2019 Gentoo Foundation
+ * Copyright 2005-2022 Gentoo Foundation
  * Distributed under the terms of the GNU General Public License v2
  */
 
@@ -9,6 +9,7 @@
 #include "atom.h"
 #include "colors.h"
 #include "set.h"
+#include "tree.h"
 #include "xarray.h"
 
 typedef enum {
@@ -28,10 +29,10 @@ static const char * const _dep_names[] = {
 };
 
 struct _dep_node {
-       dep_type type;
-       char *info;
-       char info_on_heap;
-       depend_atom *atom;
+       dep_type          type;
+       char             *info;
+       char              atom_resolved:1;
+       depend_atom      *atom;
        struct _dep_node *parent;
        struct _dep_node *neighbor;
        struct _dep_node *children;
@@ -47,6 +48,7 @@ typedef struct _dep_node dep_node;
 
 dep_node *dep_grow_tree(const char *depend);
 void dep_print_tree(FILE *fp, const dep_node *root, size_t space, array_t *m, 
const char *c, int verbose);
+void dep_resolve_tree(dep_node *root, tree_ctx *t);
 void dep_burn_tree(dep_node *root);
 void dep_prune_use(dep_node *root, set *use);
 void dep_flatten_tree(const dep_node *root, array_t *out);

Reply via email to