Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Reimplement the argument passing mechanism using varargs.
Will Andrews committed 14 years ago
commit 4052cdd7b092db8ed27a1d1cbf8f0fb7628f1c4e
parent cbe6533
4 files changed +37 -16
modified libpkg/pkg.h
@@ -1,6 +1,7 @@
#ifndef _PKG_H
#define _PKG_H

+
#include <stdarg.h>
#include <sys/types.h>
#include <openssl/pem.h>

@@ -591,7 +592,7 @@ typedef enum {
 * Event callback mechanism.  Events will be reported using this callback,
 * providing an event identifier and up to two event-specific pointers.
 */
-
typedef int(*pkg_event_cb)(pkg_event_t, void *, void *);
+
typedef int(*pkg_event_cb)(pkg_event_t, void **);

struct pkg_handle {
	pkg_event_cb event_cb;
@@ -604,9 +605,9 @@ void pkg_handle_set_event_callback(struct pkg_handle *, pkg_event_cb);
/* XXX maybe the event callback should also get a pointer to the handle, and
 * just drop arg1 altogether..? */

-
#define	pkg_emit_event(ev, arg0, arg1) \
-
	__pkg_emit_event(pkg_get_handle(), ev, arg0, arg1)
+
#define	pkg_emit_event(ev, argc, ...) \
+
	__pkg_emit_event(pkg_get_handle(), ev, argc, ...)

-
void __pkg_emit_event(struct pkg_handle *, pkg_event_t, void *, void *);
+
void __pkg_emit_event(struct pkg_handle *, pkg_event_t, int, ...);

#endif
modified libpkg/pkg_handle.c
@@ -1,3 +1,4 @@
+
#include <assert.h>
#include "pkg.h"

struct pkg_handle __pkg_handle_singleton;
@@ -21,9 +22,33 @@ pkg_handle_get_event_callback(struct pkg_handle *hdl)
}

void
-
__pkg_emit_event(struct pkg_handle *hdl, pkg_event_t ev, void *arg0, void *arg1)
+
__pkg_emit_event(struct pkg_handle *hdl, pkg_event_t ev, int argc, ...)
{
+
	va_list ap;
+
	void **argv;
+
	int i;
+

	if (hdl == NULL || hdl->event_cb == NULL)
		return;
-
	hdl->event_cb(ev, arg0, arg1);
+

+
	/* Guard-rail against incorrect number of arguments */
+
	switch(ev) {
+
	case PKG_EVENT_INSTALL_BEGIN:
+
		assert(argc == 1);
+
		break;
+
	case PKG_EVENT_ARCHIVE_ERROR:
+
		assert(argc == 2);
+
		break;
+
	default:
+
		break;
+
	}
+

+
	/* Generate the argument vector to pass in. */
+
	argv = calloc(argc, sizeof(void *));
+
	va_start(ap, argc);
+
	for (i = 0;i < argc; i++)
+
		argv[i] = va_arg(ap, void *);
+
	va_end(ap);
+

+
	hdl->event_cb(ev, argv);
}
modified pkg/event.c
@@ -3,22 +3,17 @@
#include "pkg.h"
#include "event.h"

-
/* XXX: use varargs? */
int
-
event_callback(pkg_event_t ev, void *arg0, void *arg1)
+
event_callback(pkg_event_t ev, void **argv)
{
-
	struct pkg *pkg;
-
	const char *str0, *str1;

	switch(ev) {
	case PKG_EVENT_INSTALL_BEGIN:
-
		pkg = (struct pkg *)arg0;
-
		printf("Installing %s\n", pkg_get(pkg, PKG_NAME));
+
		printf("Installing %s\n", pkg_get((struct pkg *)argv[0], PKG_NAME));
		break;
	case PKG_EVENT_ARCHIVE_ERROR:
-
		str0 = (const char *)arg0; /* file path */
-
		str1 = archive_error_string(arg1);
-
		fprintf(stderr, "archive error on %s: %s\n", str0, str1);
+
		fprintf(stderr, "archive error on %s: %s\n",
+
		    (const char *)argv[0], archive_error_string(argv[1]));
		break;
	default:
		break;
modified pkg/event.h
@@ -1,6 +1,6 @@
#ifndef __PKG_EVENT_H__
#define	__PKG_EVENT_H__

-
int event_callback(pkg_event_t ev, void *arg0, void *arg1);
+
int event_callback(pkg_event_t ev, void **argv);

#endif