Subject: pkg/26964: pkgfind enhacements
To: None <gnats-bugs@gnats.NetBSD.org>
From: None <pancake@phreaker.net>
List: pkgsrc-bugs
Date: 09/15/2004 21:12:28
>Number:         26964
>Category:       pkg
>Synopsis:       pkgfind patches that gives more flexibility to pkgfind.
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    pkg-manager
>State:          open
>Class:          support
>Submitter-Id:   net
>Arrival-Date:   Wed Sep 15 20:13:00 UTC 2004
>Closed-Date:
>Last-Modified:
>Originator:     
>Release:        NetBSD 2.0G
>Organization:
	
>Environment:
	
	
System: NetBSD pl2 2.0G NetBSD 2.0G (pancake-laptop) #10: Wed Jul 21 20:57:24 CEST 2004 pancake@panser:/usr/src/sys/arch/i386/compile/PANCAKE_LAPTOP i386
Architecture: i386
Machine: i386
>Description:
	I wrote these patches, the author agrees with these changes, and I 
	found it very useful.

	The patch adds 4 new flags to pkgfind:

	-c  * case sensitive search
	-x  * exact match search
	-q  * quite output
	-C  * comment search

	There'r two more ideas that can be added in the future:

	-D  * DESCR search
	-P  * PLIST search (inverse search)

	But i've no free time to code these changes for now.
	
>How-To-Repeat:
	
>Fix:
	Apply these two patches against ${PKGSRC_DIR}/pkgtools/pkgfind/files/:


--- pkgfind.c.orig	2004-09-13 21:54:50.000000000 +0000
+++ pkgfind.c	2004-09-13 23:23:48.000000000 +0000
@@ -25,6 +25,19 @@
  *
  */
 
+/*
+ * pancake@phreaker.net ** changes 2004/09/14
+ * 
+ * '-i' ignore case senseitive
+ * -x exact match
+ * -q quite (drop COMMENT on search)
+ * -C comments
+ *
+ * [TODO]
+ * -D DESCR
+ * -P PLIST
+ */
+
 #include <sys/types.h>
 #include <sys/param.h>
 #include <sys/stat.h>
@@ -51,18 +64,46 @@
 static int		subcasestr(const char *, const char *);
 static void		usage(void);
 
+static int		quite = 0;
+static int		cases = 0;
+static int		exact = 0;
+static int		comme = 0;
+
 int
 main(int argc, char *argv[])
 {
 	const char *path;
+	int ch;
 
-	if (argc < 2)
+	while ((ch = getopt(argc, argv, "xcqC")) != -1) {
+		switch (ch) {
+		case 'x':	/* exact match */
+			exact = 1;
+			break;
+		case 'c':	/* case sensitive */
+			cases = 1;
+			break;
+		case 'q':	/* quite */
+			quite = 1;
+			break;
+		case 'C':   /* comment search */
+			comme = 1;
+			break;
+		default:
+			usage();
+			/* NOTREACHED */
+		}
+	}
+	argc -= optind;
+	argv += optind;
+
+	if (argc < 1)
 		usage();
 
 	if ((path = getenv("PKGSRCDIR")) == NULL)
 		path = PKGSRCDIR;
 
-	for (++argv; *argv != NULL; ++argv)
+	for (; *argv != NULL; ++argv)
 		pkgfind(path, *argv);
 
 	return 0;
@@ -74,6 +115,7 @@
 	struct dirent **cat, **list;
 	int ncat, nlist, i, j;
 	char tmp[PATH_MAX];
+	char *comment = NULL;
 	struct stat sb;
 
 	if ((ncat = scandir(path, &cat, checkskip, alphasort)) < 0)
@@ -99,6 +141,15 @@
 			}
 			if (stat(tmp, &sb) < 0 || !S_ISDIR(sb.st_mode))
 				continue;
+
+			if (comme) {
+				strcat(tmp,"/Makefile");
+				if (getcomment(tmp,&comment) != 0)
+					if (comment!=0)
+					if (subcasestr(comment,pkg))
+						showpkg(path,cat[i]->d_name,list[j]->d_name);
+				continue;
+			}
 			if (subcasestr(list[j]->d_name, pkg))
 				showpkg(path, cat[i]->d_name, list[j]->d_name);
 			free(list[j]);
@@ -112,26 +163,28 @@
 static void
 showpkg(const char *path, const char *cat, const char *pkg)
 {
-	char *mk, *comment;
-
-	(void)asprintf(&mk, "%s/%s/%s/Makefile", path, cat, pkg);
-	if (mk == NULL)
-		err(EXIT_FAILURE, "asprintf");
+	char *mk, *comment = NULL;
 
-	comment = NULL;
-	if (getcomment(mk, &comment) == 0) {
-		free(mk);
-		(void)asprintf(&mk, "%s/%s/%s/Makefile.common", path, cat, pkg);
+	if (!quite) {
+		(void)asprintf(&mk, "%s/%s/%s/Makefile", path, cat, pkg);
 		if (mk == NULL)
 			err(EXIT_FAILURE, "asprintf");
-		(void)getcomment(mk, &comment);
+
+		if (getcomment(mk, &comment) == 0) {
+			free(mk);
+			(void)asprintf(&mk, "%s/%s/%s/Makefile.common",
+			    path, cat, pkg);
+			if (mk == NULL)
+				err(EXIT_FAILURE, "asprintf");
+			(void)getcomment(mk, &comment);
+		}
+		free(mk);
 	}
-	free(mk);
 
 	if (comment != NULL)
 		(void)printf("%s/%s: %s\n", cat, pkg, comment);
 	else
-		(void)printf("%s/%s: no description\n", cat, pkg);
+		(void)printf("%s/%s\n", cat, pkg);
 }
 
 static int
@@ -177,13 +230,27 @@
 subcasestr(const char *s, const char *find)
 {
 	size_t len, n;
+	int match = 0;
 
 	len = strlen(find);
 	n = strlen(s) - len;
 
+	if (exact) {
+		if (cases)
+			match = (strcmp(find, s) == 0);
+		else
+			match = (strcasecmp(find, s) == 0);
+		return match;
+	}
+
 	do {
-		if (strncasecmp(s, find, len) == 0)
-			return 1;
+		if (cases) {
+			if (strncmp(s, find, len) == 0)
+				return 1;
+		} else {
+			if (strncasecmp(s, find, len) == 0)
+				return 1;
+		}
 	} while (*++s != '\0' && n-- > 0);
 
 	return 0;
@@ -194,6 +261,6 @@
 {
 	extern char *__progname;
 
-	(void)fprintf(stderr, "Usage: %s keyword [...]\n", __progname);
+	(void)fprintf(stderr, "Usage: %s [-cqxC] keyword [...]\n", __progname);
 	exit(EXIT_FAILURE);
 }


--- pkgfind.1.orig	2004-09-13 22:54:31.000000000 +0000
+++ pkgfind.1	2004-09-13 23:21:59.000000000 +0000
@@ -30,6 +30,7 @@
 .Nd find packages in pkgsrc
 .Sh SYNOPSIS
 .Nm
+.Ar [-cqxC]
 .Ar keyword
 .Op Ar ...
 .Sh DESCRIPTION
@@ -46,6 +47,19 @@
 You may specify a different path by setting
 the environment variable
 .Pa PKGSRCDIR .
+.Pp
+.Fl c
+Make case sensitive searchs
+.Pp
+.Fl q
+Be quite in output. Only shows pkgnames.
+.Pp
+.Fl x
+Exact word search.
+.Pp
+.Fl C
+Search in COMMENTs.
+.Pp
 .Sh SEE ALSO
 http://www.pkgsrc.org/
 .Sh AUTHOR
	
>Release-Note:
>Audit-Trail:
>Unformatted: