PERFORCE change 145556 for review

Anders Nore andenore at FreeBSD.org
Mon Jul 21 11:52:29 UTC 2008


http://perforce.freebsd.org/chv.cgi?CH=145556

Change 145556 by andenore at andenore_laptop on 2008/07/21 11:51:24

	Fixed some functionality in pkg_convert and updated man-page. The -e and
	-p options now support more than one key as argument.

Affected files ...

.. //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/convert/converter.h#3 edit
.. //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/convert/main.c#4 edit
.. //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/convert/perform.c#7 edit
.. //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/convert/pkg_convert.1#4 edit

Differences ...

==== //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/convert/converter.h#3 (text+ko) ====


==== //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/convert/main.c#4 (text+ko) ====

@@ -1,19 +1,30 @@
 #include <stdlib.h>
 #include <lib.h>
-
-static char Options[] = "vthepk:";
+#include <getopt.h>
 
 char *Key		= NULL;
 Boolean CheckExists	= FALSE;
 Boolean Print		= FALSE;
-Boolean	Textual		= TRUE;
+Boolean	Textual		= FALSE;
+
+void usage(void);
+
+static char opts[] = "vthep";
+
+static struct option longopts[] = {
+	{ "verbose",	no_argument,		NULL,		'v' },
+	{ "textual",	no_argument,		NULL,		't' },
+	{ "help",	no_argument,		NULL,		'h' },
+	{ "exist",	no_argument,		NULL,		'e' },
+	{ NULL,		0,			NULL,		0 }
+};
+
 
 int
 main(int argc, char **argv) {
     char **pkgs;	/* null terminated array of installed pkgnames */
     int retval, ch, flags;
 
-
     signal(SIGHUP, cleanup);
     signal(SIGINT, cleanup);
 
@@ -22,7 +33,8 @@
     else
 	flags = O_RDONLY;
 
-    while ((ch = getopt(argc, argv, Options)) != -1) {
+    pkgs = argv;
+    while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
 	switch(ch) {
 	case 'v':
 		Verbose++;
@@ -31,18 +43,15 @@
 		CheckExists = TRUE;
 		break;
 	case 't':
-		Textual = FALSE;
+		Textual = TRUE;
 		break;
 	case 'p':
 		Print = TRUE;
 		break;
 	case '?':
-	case 'k':
-		Key = optarg;
-		break;
 	case 'h':
 	default:
-		usage();	
+		usage();
 		break;
 	}
     }
@@ -52,25 +61,37 @@
     }
     atexit(closeDatabase);
 
-    char **dumb;
-    dumb = malloc(sizeof(char **) * 2);
-    *dumb = NULL;
-    pkgs = matchinstalled(MATCH_ALL, dumb, &retval);
+    /* If we want to convert the whole database */
+    if (!CheckExists && !Print) {
+	char **dumb;
+	dumb = malloc(sizeof(char **) * 2);
+	*dumb = NULL;
+	pkgs = matchinstalled(MATCH_ALL, dumb, &retval);
+	
+	if (pkgs != NULL || retval != 0) {
+	    return pkg_perform(pkgs);
+	} else {
+	    printf("No packages to convert\n");
+	    return 0;
+	}
+    }
+
+    argc -= optind;
+    argv += optind;
 
-    if (pkgs != NULL || retval != 0) {
- 	return pkg_perform(pkgs);
-    } else {
-	printf("No packages to convert\n");
-    }
+    pkgs = argv;
 
-    return 0;
+    if (argc == NULL)
+	return pkg_perform(NULL);
+    else
+	return pkg_perform(pkgs);
 }
 
 void
 usage()
 {
     fprintf(stderr, "usage:\t%s\n\t%s\n", 
-		    "pkg_convert [-vtep] [-k key]",
-		    "pkg_convert");
+		    "pkg_convert [-vtep] [keys ...]",
+		    "pkg_convert [-v]");
     exit(1);
 }

==== //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/convert/perform.c#7 (text+ko) ====

@@ -12,49 +12,65 @@
 int
 pkg_perform(char **pkgs)
 {
-	int i, error;
-	error = 0;
-	DBT data;
+    int i, error;
+    error = 0;
+    DBT data;
 
-	if (CheckExists) {
-		if (!Key) {
-			warnx("You must specify a key with -k\n");
-			usage();
-			exit(1);
-		}
-		Boolean exists = dbKeyExists(Key, &data);
-		if (Textual) {
-			if (Verbose)
-				printf("%s exists:\t", Key);
-			printf("%s\n", exists ? "TRUE" : "FALSE");
-		} else {
-			printf("%s\n", exists ? "1" : "0");
-		}
-		
-		return 0;
+    if (CheckExists) {
+	if (pkgs == NULL) {
+	    warnx("You must specify one or more keys...\n");
+	    usage();
+	    exit(1);
 	}
 
-	if (Print) {
-		if (Key != NULL) {
-			if (dbKeyExists(Key, &data))
-				printf("%s -> %s\n", Key, (char *)data.data);
-			else
-				printf("%s: does not exist\n", Key);
-		 } else {
-			DBT key;
-			while (dbscan(database, &key, &data) == 0) {	
-				printf("%s -> %s\n", (char *)key.data, (char *)data.data);
-			}
-		}
+	if (Verbose)
+	    printf("Exists:\n");	
+
+	while (*pkgs) {	
+	    Boolean exists = dbKeyExists(*pkgs, &data);
+	    if (Verbose)
+		printf("%-12s : ", *pkgs);
 
-		return 0;
+	    if (Textual) 
+		printf("%s\n", exists ? "TRUE" : "FALSE");
+	    else 
+		printf("%s\n", exists ? "1" : "0");
+	
+	    *pkgs++;
 	}
 
-	for (i=0; pkgs[i] != NULL; i++) {
-		error += pkg_do(pkgs[i]);
+	return 0;
+    }
+
+    if (Print) {
+	if (pkgs != NULL) {
+	    if (Verbose)
+		printf("Printing keys:\n");
+	    while (*pkgs) {
+		if (dbKeyExists(*pkgs, &data))
+		    printf("%-18s: %s\n", *pkgs, (char *)data.data);
+		else
+		    printf("%-18s: (null) does not exist\n", *pkgs);
+		
+		*pkgs++;
+	    }
+	
+	} else {
+	    if (Verbose)
+		printf("Printing the whole database\n");
+	    DBT key;
+	    while (dbscan(database, &key, &data) == 0) {	
+		printf("%-18s : %s\n", (char *)key.data, (char *)data.data);
+	    }
 	}
+	return 0;
+    }
+
+    for (i=0; pkgs[i] != NULL; i++) {
+	error += pkg_do(pkgs[i]);
+    }
 
-	return error;
+    return error;
 }
 
 

==== //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/convert/pkg_convert.1#4 (text+ko) ====

@@ -24,6 +24,9 @@
 .Sh SYNOPSIS
 .Nm
 .Op Fl v
+.Op Fl et Ar key
+.Op Fl p Ar key
+
 .Sh DESCRIPTION
 The
 .Nm
@@ -36,8 +39,12 @@
 Turn on verbose output.
 .It Fl e Ar key
 Checks if key is in database.
+.It Fl p Ar key
+Prints the data at the key. If a key is not specified, the whole database is
+printed.
 .It Fl t
-Turns off textual output for key-check (outputs 1 if key exists 0 if not).
+Turns on textual output for key-check (outputs 'true' if key exists 'false' if
+not).
 .El
 .Sh FILES
 .Bl -tag -width /var/db/pkg -compact


More information about the p4-projects mailing list