From 948d8a9736dabe9b0d85968fa2d09829b3f94ad8 Mon Sep 17 00:00:00 2001 From: shattered Date: Mon, 7 Nov 2011 20:27:30 +0000 Subject: [PATCH] PR/36986 -- two new options: -D -- print DESCR file of matched package to stdout; -P -- search in PLIST files. --- doc/CHANGES-2011 | 3 +- pkgtools/pkgfind/Makefile | 4 +- pkgtools/pkgfind/files/pkgfind.1 | 12 ++++- pkgtools/pkgfind/files/pkgfind.c | 89 +++++++++++++++++++++++++++++--- 4 files changed, 96 insertions(+), 12 deletions(-) diff --git a/doc/CHANGES-2011 b/doc/CHANGES-2011 index 69841a3b5b3c..04668c230ec4 100644 --- a/doc/CHANGES-2011 +++ b/doc/CHANGES-2011 @@ -1,4 +1,4 @@ -$NetBSD: CHANGES-2011,v 1.2794 2011/11/07 15:37:05 taca Exp $ +$NetBSD: CHANGES-2011,v 1.2795 2011/11/07 20:27:30 shattered Exp $ Changes to the packages collection and infrastructure in 2011: @@ -4400,3 +4400,4 @@ Changes to the packages collection and infrastructure in 2011: Updated devel/p5-Software-License to 0.103.004 [hiramatsu 2011-11-07] Updated www/p5-CSS to 1.09 [hiramatsu 2011-11-07] Updated mail/postfix to 2.8.7 [taca 2011-11-07] + Updated pkgtools/pkgfind to 20111022 [shattered 2011-11-07] diff --git a/pkgtools/pkgfind/Makefile b/pkgtools/pkgfind/Makefile index ae06a7058de3..8100e432850d 100644 --- a/pkgtools/pkgfind/Makefile +++ b/pkgtools/pkgfind/Makefile @@ -1,6 +1,6 @@ -# $NetBSD: Makefile,v 1.22 2009/04/09 00:48:13 joerg Exp $ +# $NetBSD: Makefile,v 1.23 2011/11/07 20:27:30 shattered Exp $ -DISTNAME= pkgfind-20050804 +DISTNAME= pkgfind-20111022 CATEGORIES= pkgtools MASTER_SITES= # empty DISTFILES= # empty diff --git a/pkgtools/pkgfind/files/pkgfind.1 b/pkgtools/pkgfind/files/pkgfind.1 index 6d1cd357972f..1329038956ed 100644 --- a/pkgtools/pkgfind/files/pkgfind.1 +++ b/pkgtools/pkgfind/files/pkgfind.1 @@ -1,4 +1,4 @@ -.\" $NetBSD: pkgfind.1,v 1.8 2005/05/25 16:53:59 wiz Exp $ +.\" $NetBSD: pkgfind.1,v 1.9 2011/11/07 20:27:30 shattered Exp $ .\" .\" Copyright (c) 2004 Peter Postma .\" All rights reserved. @@ -58,10 +58,20 @@ Search in the field, instead of looking at package names. .It Fl c Do case sensitive searches. +.It Fl D +Show the contents of the +.Dq DESCR +file when a match is found. .It Fl M Search in the .Dq MAINTAINER field, instead of looking at package names. +.It Fl P +Search in the +.Dq PLIST +file, instead of looking at package names. +Useful when you only know the name of +a file contained in a package. .It Fl n Ar number Stop searching after .Ar number diff --git a/pkgtools/pkgfind/files/pkgfind.c b/pkgtools/pkgfind/files/pkgfind.c index 8fa0e92c43e7..ef00dfc6ec3a 100644 --- a/pkgtools/pkgfind/files/pkgfind.c +++ b/pkgtools/pkgfind/files/pkgfind.c @@ -29,7 +29,9 @@ * pancake@phreaker.net ** changes 2004/09/14 * * -C search in comments + * -D print DESCR file (if any) to stdout * -c case sensitive + * -P search in PLIST entries * -q quiet, don't output comment * -x exact matches */ @@ -65,11 +67,13 @@ static int checkskip(const struct dirent *); static int partialmatch(const char *, const char *); static int exactmatch(const char *, const char *); static void usage(void); +static void print_file(const char*); +static int find_plist_entry(const char *, const char *); static int (*match)(const char *, const char *); static const char *search; -static int cflag, qflag; +static int cflag, dflag, pflag, qflag; int main(int argc, char *argv[]) @@ -86,11 +90,14 @@ main(int argc, char *argv[]) cflag = qflag = 0; - while ((ch = getopt(argc, argv, "Ccn:Mqx")) != -1) { + while ((ch = getopt(argc, argv, "CDcn:MPqx")) != -1) { switch (ch) { case 'C': /* search in comments */ search = "COMMENT"; break; + case 'D': /* print DESCR file to stdout */ + dflag = 1; + break; case 'c': /* case sensitive */ cflag = 1; break; @@ -100,6 +107,9 @@ main(int argc, char *argv[]) case 'M': /* search for maintainer */ search = "MAINTAINER"; break; + case 'P': /* search in PLIST file */ + pflag = 1; + break; case 'q': /* quiet, don't output comment */ qflag = 1; break; @@ -130,7 +140,7 @@ static void pkgfind(const char *path, const char *pkg, int count) { struct dirent **cat, **list = NULL; - int ncat, nlist, i, j; + int ncat, nlist, i, j, plistfound = 0; char tmp[PATH_MAX]; char *text = NULL; struct stat sb; @@ -166,16 +176,20 @@ pkgfind(const char *path, const char *pkg, int count) if (getstring(tmp, search, &text) == 0) continue; } + } else if (pflag) { + (void)strncat(tmp, "/PLIST", sizeof(tmp)); + plistfound = find_plist_entry(tmp, pkg); + text = ""; } else { text = list[j]->d_name; } - if ((*match)(text, pkg)) { + if (plistfound || (*match)(text, pkg)) { showpkg(path, cat[i]->d_name, list[j]->d_name); if (count != 0 && --count < 1) { i = ncat; break; } - } + } free(list[j]); } free(cat[i]); @@ -187,12 +201,15 @@ pkgfind(const char *path, const char *pkg, int count) static void showpkg(const char *path, const char *cat, const char *pkg) { - char *mk, *comment = NULL; - size_t len; + char *mk, *desc, *comment = NULL; + size_t len, desclen; len = strlen(path) + strlen(cat) + strlen(pkg) + strlen("Makefile") + 3 + 1; + desclen = strlen(path) + strlen(cat) + strlen(pkg) + + strlen("DESCR") + 4; + if (!qflag) { if ((mk = malloc(len)) == NULL) err(EXIT_FAILURE, "malloc"); @@ -212,6 +229,62 @@ showpkg(const char *path, const char *cat, const char *pkg) (void)printf("%s/%s: %s\n", cat, pkg, comment); else (void)printf("%s/%s\n", cat, pkg); + + if (dflag) { + if ((desc = malloc(len)) == NULL) { + err(EXIT_FAILURE, "malloc"); + } + (void) snprintf(desc, desclen, "%s/%s/%s/DESCR", path, cat, pkg); + print_file(desc); + free(desc); + } +} + +static void +print_file(const char *file) +{ + FILE* fp; + char data[BUFSIZ+1]; + ssize_t nread = 0; + + if ( (fp = fopen(file, "r")) == NULL ) { + warnx("Couldn't open %s for reading\n", file); + return; + } + while ((nread = fread(data, 1, BUFSIZ, fp))) { + data[nread] = 0; + fprintf(stdout, "%s", data); + } + if (ferror(fp)) { + warnx("Couldn't finish reading %s\n", file); + } else { + fprintf(stdout, "\n"); + } + fclose(fp); +} + +static int +find_plist_entry(const char *file, const char *string) +{ + char line[BUFSIZ]; + FILE *fp; + int found = 0;; + + if ((fp = fopen(file, "r")) == NULL) { + return 0; + } + while (fgets(line, BUFSIZ, fp) != NULL) { + if ((*match)(line, string)) { + found = 1; + break; + } + } + if (ferror(fp)) { + warnx("Couldn't finish reading %s\n", file); + } + fclose(fp); + + return found; } static int @@ -287,7 +360,7 @@ exactmatch(const char *s, const char *find) static void usage(void) { - (void)fprintf(stderr, "Usage: %s [-CcMqx] [-n number] keyword [...]\n", + (void)fprintf(stderr, "Usage: %s [-CcMPqxD] [-n number] keyword [...]\n", getprogname()); exit(EXIT_FAILURE); }