aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--qlist.c71
-rw-r--r--qmerge.c91
2 files changed, 79 insertions, 83 deletions
diff --git a/qlist.c b/qlist.c
index 24e59bc4..41e39b14 100644
--- a/qlist.c
+++ b/qlist.c
@@ -1,7 +1,7 @@
/*
* Copyright 2005-2010 Gentoo Foundation
* Distributed under the terms of the GNU General Public License v2
- * $Header: /var/cvsroot/gentoo-projects/portage-utils/qlist.c,v 1.65 2011/12/18 07:27:37 vapier Exp $
+ * $Header: /var/cvsroot/gentoo-projects/portage-utils/qlist.c,v 1.66 2011/12/18 20:23:34 vapier Exp $
*
* Copyright 2005-2010 Ned Ludd - <solar@gentoo.org>
* Copyright 2005-2010 Mike Frysinger - <vapier@gentoo.org>
@@ -41,7 +41,7 @@ static const char * const qlist_opts_help[] = {
/* "query filename for pkgname", */
COMMON_OPTS_HELP
};
-static const char qlist_rcsid[] = "$Id: qlist.c,v 1.65 2011/12/18 07:27:37 vapier Exp $";
+static const char qlist_rcsid[] = "$Id: qlist.c,v 1.66 2011/12/18 20:23:34 vapier Exp $";
#define qlist_usage(ret) usage(ret, QLIST_FLAGS, qlist_long_opts, qlist_opts_help, lookup_applet_idx("qlist"))
queue *filter_dups(queue *sets);
@@ -151,6 +151,47 @@ static const char *umapstr(char display, const char *cat, const char *name)
return buf;
}
+_q_static bool qlist_match(q_vdb_pkg_ctx *pkg_ctx, const char *name, bool exact)
+{
+ const char *catname = pkg_ctx->cat_ctx->name;
+ const char *pkgname = pkg_ctx->name;
+ char buf[_Q_PATH_MAX];
+ char swap[_Q_PATH_MAX];
+ depend_atom *atom;
+
+ snprintf(buf, sizeof(buf), "%s/%s", catname, pkgname);
+ /* printf("buf=%s:%s\n", buf,grab_vdb_item("SLOT", catname, pkgname)); */
+ if (exact) {
+ if (strcmp(name, buf) == 0)
+ return true;
+ if (strcmp(name, strstr(buf, "/") + 1) == 0)
+ return true;
+
+ if ((atom = atom_explode(buf)) == NULL) {
+ warn("invalid atom %s", buf);
+ return false;
+ }
+ snprintf(swap, sizeof(swap), "%s/%s",
+ atom->CATEGORY, atom->PN);
+ atom_implode(atom);
+ if (strcmp(name, swap) == 0)
+ return true;
+ if (strcmp(name, strstr(swap, "/") + 1) == 0)
+ return true;
+ } else {
+ if (charmatch(name, buf) == 0)
+ return true;
+ if (charmatch(name, pkgname) == 0)
+ return true;
+ if (rematch(name, buf, REG_EXTENDED) == 0)
+ return true;
+ if (rematch(name, pkgname, REG_EXTENDED) == 0)
+ return true;
+ }
+
+ return false;
+}
+
struct qlist_opt_state {
int argc;
char **argv;
@@ -186,30 +227,8 @@ _q_static int qlist_cb(q_vdb_pkg_ctx *pkg_ctx, void *priv)
/* see if this cat/pkg is requested */
for (i = optind; i < state->argc; ++i) {
char *name = pkg_name(state->argv[i]);
- snprintf(state->buf, state->buflen, "%s/%s", catname, pkgname);
- /* printf("buf=%s:%s\n", buf,grab_vdb_item("SLOT", catname, pkgname)); */
- if (state->exact) {
- if ((atom = atom_explode(state->buf)) == NULL) {
- warn("invalid atom %s", state->buf);
- continue;
- }
- snprintf(swap, sizeof(swap), "%s/%s",
- atom->CATEGORY, atom->PN);
- atom_implode(atom);
- if ((strcmp(name, swap) == 0) || (strcmp(name, state->buf) == 0))
- break;
- if ((strcmp(name, strstr(swap, "/") + 1) == 0) || (strcmp(name, strstr(state->buf, "/") + 1) == 0))
- break;
- } else {
- if (charmatch(name, state->buf) == 0)
- break;
- if (charmatch(name, pkgname) == 0)
- break;
- if (rematch(name, state->buf, REG_EXTENDED) == 0)
- break;
- if (rematch(name, pkgname, REG_EXTENDED) == 0)
- break;
- }
+ if (qlist_match(pkg_ctx, name, state->exact))
+ break;
}
if ((i == state->argc) && (state->argc != optind))
return 0;
diff --git a/qmerge.c b/qmerge.c
index fe74551b..3d1d2a15 100644
--- a/qmerge.c
+++ b/qmerge.c
@@ -1,7 +1,7 @@
/*
* Copyright 2005-2010 Gentoo Foundation
* Distributed under the terms of the GNU General Public License v2
- * $Header: /var/cvsroot/gentoo-projects/portage-utils/qmerge.c,v 1.111 2011/12/12 20:47:00 grobian Exp $
+ * $Header: /var/cvsroot/gentoo-projects/portage-utils/qmerge.c,v 1.112 2011/12/18 20:23:34 vapier Exp $
*
* Copyright 2005-2010 Ned Ludd - <solar@gentoo.org>
* Copyright 2005-2010 Mike Frysinger - <vapier@gentoo.org>
@@ -65,7 +65,7 @@ static const char * const qmerge_opts_help[] = {
COMMON_OPTS_HELP
};
-static const char qmerge_rcsid[] = "$Id: qmerge.c,v 1.111 2011/12/12 20:47:00 grobian Exp $";
+static const char qmerge_rcsid[] = "$Id: qmerge.c,v 1.112 2011/12/18 20:23:34 vapier Exp $";
#define qmerge_usage(ret) usage(ret, QMERGE_FLAGS, qmerge_long_opts, qmerge_opts_help, lookup_applet_idx("qmerge"))
char search_pkgs = 0;
@@ -198,64 +198,41 @@ _q_static void qmerge_initialize(void)
}
}
-_q_static char *best_version(const char *CATEGORY, const char *PN)
-{
- /* XXX: The qlist code should be refactored to avoid this fork and I/O */
- static char buf[4096];
- ssize_t rret;
- int status, pipefd[2];
- pid_t p;
- FILE *fp;
-
- buf[0] = '\0';
- if (pipe(pipefd))
- return buf;
-
- snprintf(buf, sizeof(buf), "%s%s%s", (CATEGORY != NULL ? CATEGORY : ""),
- (CATEGORY != NULL ? "/" : ""), PN);
-
- fflush(stdout);
-
- switch ((p = fork())) {
- /* Stupid -Cv screws up global state */
- case 0: {
- fclose(stdout);
- close(pipefd[0]);
- if (dup2(pipefd[1], STDOUT_FILENO) != STDOUT_FILENO)
- exit(1);
- stdout = fdopen(pipefd[1], "w");
- if (!stdout)
- exit(1);
- run_applet_l("qlist", "-CIev", buf, NULL);
- exit(0);
- }
- default:
- close(pipefd[1]);
- waitpid(p, &status, 0);
- }
-
- fp = fdopen(pipefd[0], "r");
- if (!fp) {
- close(pipefd[0]);
- return buf;
- }
-
- rret = fread(buf, 1, sizeof(buf), fp);
- if (rret > 0) {
- char *s;
-
- buf[rret] = '\0';
+struct qmerge_bv_state {
+ const char *catname;
+ const char *pkgname;
+ char buf[4096];
+ char *retbuf;
+};
- s = buf;
- while ((s = strchr(s, '\n')) != NULL)
- *s = ' ';
- rmspace(buf);
- }
+_q_static int qmerge_filter_cat(q_vdb_cat_ctx *cat_ctx, void *priv)
+{
+ struct qmerge_bv_state *state = priv;
+ return strcmp(cat_ctx->name, state->catname) == 0;
+}
- /* takes care of close(pipefd[0]) */
- fclose(fp);
+_q_static int qmerge_best_version_cb(q_vdb_pkg_ctx *pkg_ctx, void *priv)
+{
+ struct qmerge_bv_state *state = priv;
+ if (qlist_match(pkg_ctx, state->buf, true))
+ strcpy(state->retbuf, state->buf);
+ return 0;
+}
- return buf;
+_q_static char *best_version(const char *catname, const char *pkgname)
+{
+ static char retbuf[4096];
+ struct qmerge_bv_state state = {
+ .catname = catname,
+ .pkgname = pkgname,
+ .retbuf = retbuf,
+ };
+
+ snprintf(state.buf, sizeof(state.buf), "%s/%s", catname, pkgname);
+ retbuf[0] = '\0';
+ q_vdb_foreach_pkg(qmerge_best_version_cb, &state, qmerge_filter_cat);
+
+ return retbuf;
}
_q_static int