Skip to content

Commit

Permalink
Reimplement the argument passing mechanism using varargs.
Browse files Browse the repository at this point in the history
  • Loading branch information
wca committed May 25, 2011
1 parent cbe6533 commit 4052cdd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
9 changes: 5 additions & 4 deletions libpkg/pkg.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef _PKG_H
#define _PKG_H

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

Expand Down Expand Up @@ -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;
Expand All @@ -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
29 changes: 27 additions & 2 deletions libpkg/pkg_handle.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <assert.h>
#include "pkg.h"

struct pkg_handle __pkg_handle_singleton;
Expand All @@ -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);
}
13 changes: 4 additions & 9 deletions pkg/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion pkg/event.h
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4052cdd

Please sign in to comment.