Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Support the concept of debug level in libpkg. Use this to implement runtime debug checking for printing additional information about the location of an error. Take the opportunity to create a "global" options mechanism; resetting getopt(3) for the sub-command calls.
Will Andrews committed 14 years ago
commit 348010da3b83e83715039bb51d97f751e873c8f1
parent 13f532a
4 files changed +46 -13
modified libpkg/pkg.h
@@ -659,11 +659,14 @@ typedef int(*pkg_event_cb)(pkg_event_t, const char *, int, void **);

struct pkg_handle {
	pkg_event_cb event_cb;
+
	int debug;
};

struct pkg_handle *pkg_get_handle(void);
pkg_event_cb pkg_handle_get_event_callback(struct pkg_handle *);
void pkg_handle_set_event_callback(struct pkg_handle *, pkg_event_cb);
+
void pkg_handle_set_debug(struct pkg_handle *, int);
+
int pkg_handle_get_debug(struct pkg_handle *);

void __pkg_emit_event(struct pkg_handle *, const char *, int, pkg_event_t, int, ...);

modified libpkg/pkg_event.c
@@ -46,7 +46,7 @@ pkg_event_argument_check(pkg_event_t ev, int argc)
 * This function's purpose is to perform global event handling.
 */
static void
-
libpkg_handle_event(pkg_event_t ev, const char *filename, int line, void **argv)
+
libpkg_handle_event(struct pkg_handle *hdl, pkg_event_t ev, const char *filename, int line, void **argv)
{
	pkg_error_t ret = EPKG_FATAL; /* most of these are this code */
	struct sbuf *sb;
@@ -124,7 +124,7 @@ libpkg_handle_event(pkg_event_t ev, const char *filename, int line, void **argv)
		ret = EPKG_OK; /* unhandled error */
		break;
	}
-
	if (filename != NULL && line >= 0)
+
	if (filename != NULL && line >= 0 && pkg_handle_get_debug(hdl) > 0)
		sbuf_printf(sb, " [at %s:%d]", filename, line);
	sbuf_done(sb);
	if (ret != 0)
@@ -151,7 +151,7 @@ __pkg_emit_event(struct pkg_handle *hdl, const char *filename, int line, pkg_eve
		argv[i] = va_arg(ap, void *);
	va_end(ap);

-
	libpkg_handle_event(ev, filename, line, argv);
+
	libpkg_handle_event(hdl, ev, filename, line, argv);

	if (hdl->event_cb != NULL)
		hdl->event_cb(ev, filename, line, argv);
modified libpkg/pkg_handle.c
@@ -19,3 +19,14 @@ pkg_handle_get_event_callback(struct pkg_handle *hdl)
{
	return hdl->event_cb;
}
+

+
void
+
pkg_handle_set_debug(struct pkg_handle *hdl, int debug)
+
{
+
	hdl->debug = debug;
+
}
+
int
+
pkg_handle_get_debug(struct pkg_handle *hdl)
+
{
+
	return hdl->debug;
+
}
modified pkg/main.c
@@ -4,6 +4,7 @@
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
+
#include <unistd.h>

#include "pkg.h"
#include "create.h"
@@ -47,8 +48,10 @@ const unsigned int cmd_len = (sizeof(cmd)/sizeof(cmd[0]));
static void
usage(void)
{
-
	fprintf(stderr, "usage: pkg <command> [<args>]\n\n");
-
	fprintf(stderr, "Where <command> can be:\n");
+
	fprintf(stderr, "usage: pkg [-d] <command> [<args>]\n\n");
+
	fprintf(stderr, "Global options supported:\n");
+
	fprintf(stderr, "  -d: Increment debug level\n\n");
+
	fprintf(stderr, "Commands supported:\n");

	for (unsigned int i = 0; i < cmd_len; i++) 
		fprintf(stderr, "\t%s\n", cmd[i].name);
@@ -107,6 +110,8 @@ main(int argc, char **argv)
	unsigned int ambiguous = 0;
	size_t len;
	struct pkg_handle *hdl;
+
	char ch;
+
	int debug = 0;

	if (argc < 2)
		usage();
@@ -114,9 +119,25 @@ main(int argc, char **argv)
	hdl = pkg_get_handle();
	pkg_handle_set_event_callback(hdl, event_callback);

-
	len = strlen(argv[1]);
+
	while ((ch = getopt(argc, argv, "d")) != -1) {
+
		switch(ch) {
+
			case 'd':
+
				debug++;
+
				break;
+
			default:
+
				break;
+
		}
+
	}
+
	pkg_handle_set_debug(hdl, debug);
+
	argc -= optind;
+
	argv += optind;
+
	/* reset getopt for the next call */
+
	optreset = 1;
+
	optind = 1;
+

+
	len = strlen(argv[0]);
	for (i = 0; i < cmd_len; i++) {
-
		if (strncmp(argv[1], cmd[i].name, len) == 0) {
+
		if (strncmp(argv[0], cmd[i].name, len) == 0) {
			/* if we have the exact cmd */
			if (len == strlen(cmd[i].name)) {
				command = &cmd[i];
@@ -125,7 +146,7 @@ main(int argc, char **argv)
			}

			/*
-
			 * we already found a partial match so `argv[1]' is
+
			 * we already found a partial match so `argv[0]' is
			 * an ambiguous shortcut
			 */
			ambiguous++;
@@ -138,18 +159,16 @@ main(int argc, char **argv)
		usage();

	if (ambiguous <= 1) {
-
		argc--;
-
		argv++;
		assert(command->exec != NULL);
		return (command->exec(argc, argv));
	} else {
-
		warnx("'%s' is not a valid command.\n", argv[1]);
+
		warnx("'%s' is not a valid command.\n", argv[0]);

		fprintf(stderr, "See 'pkg help' for more information on the commands.\n\n");
-
		fprintf(stderr, "Command '%s' could be one of the following:\n", argv[1]);
+
		fprintf(stderr, "Command '%s' could be one of the following:\n", argv[0]);

		for (i = 0; i < cmd_len; i++)
-
			if (strncmp(argv[1], cmd[i].name, len) == 0)
+
			if (strncmp(argv[0], cmd[i].name, len) == 0)
				fprintf(stderr, "\t%s\n",cmd[i].name);
	}