/* $Id: pkglist.c,v 1.16 2009/05/07 21:22:52 imil Exp $ */ /* * Copyright (c) 2009 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation * by Emile "iMil" Heitor . * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ #include "pkg_dry.h" void free_pkglist(Plisthead *plisthead) { Pkglist *plist; if (plisthead == NULL) return; while (!SLIST_EMPTY(plisthead)) { plist = SLIST_FIRST(plisthead); SLIST_REMOVE_HEAD(plisthead, next); XFREE(plist->pkgname); XFREE(plist->comment); XFREE(plist); } } /* sqlite callback, record package list */ static int ddb_rec_pkglist(void *param, int argc, char **argv, char **colname) { Pkglist *plist; Plisthead *plisthead = (Plisthead *)param; if (argv != NULL) { /* PKGNAME was empty, probably a package installed * from pkgsrc or wip that does not exist in * pkg_summary(5), return */ if (argv[0] == NULL) return DDB_OK; XMALLOC(plist, sizeof(Pkglist)); XSTRDUP(plist->pkgname, argv[0]); plist->size_pkg = 0; plist->file_size = 0; /* classic pkglist, has COMMENT and SIZEs */ if (argc > 1) { if (argv[1] == NULL) { /* COMMENT or SIZEs were empty * not a valid pkg_summary(5) entry, return */ XFREE(plist->pkgname); XFREE(plist); return DDB_OK; } XSTRDUP(plist->comment, argv[1]); if (argv[2] != NULL) plist->file_size = strtol(argv[2], (char **)NULL, 10); if (argv[3] != NULL) plist->size_pkg = strtol(argv[3], (char **)NULL, 10); } else /* conflicts or requires list, only pkgname needed */ plist->comment = NULL; SLIST_INSERT_HEAD(plisthead, plist, next); return DDB_OK; } return DDB_ERR; } Plisthead * rec_pkglist(const char *pkgquery) { Plisthead *plisthead; XMALLOC(plisthead, sizeof(Plisthead)); SLIST_INIT(plisthead); if (drydb_doquery(pkgquery, ddb_rec_pkglist, plisthead) == 0) return plisthead; else return NULL; } void list_pkgs(const char *pkgquery) { Pkglist *plist; Plisthead *plisthead; const char *alnum = ALNUM; char *p; if ((plisthead = rec_pkglist(pkgquery)) != NULL) { for (; *alnum; alnum++) SLIST_FOREACH(plist, plisthead, next) { p = plist->pkgname; if (*p != *alnum) continue; printf("%-20s %s\n", p, plist->comment); } free_pkglist(plisthead); } } void search_pkg(const char *pkgname) { Pkglist *plist; Plisthead *plisthead; if ((plisthead = rec_pkglist(REMOTE_PKGS_QUERY)) != NULL) { SLIST_FOREACH(plist, plisthead, next) if (strcasestr(plist->pkgname, pkgname) != NULL || strcasestr(plist->comment, pkgname) != NULL) printf("%-20s %s\n", plist->pkgname, plist->comment); free_pkglist(plisthead); } }