1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
/* $Id: dispatch.c 6376 2003-06-01 23:24:34Z rra $
**
** Dispatch cvectors of commands to functions.
**
** This is a generic command dispatching system designed primary to handle
** dispatching NNTP commands to functions that handle them, for NNTP server
** software.
*/
#include "config.h"
#include "clibrary.h"
#include "inn/dispatch.h"
#include "inn/vector.h"
/*
** Comparison function for bsearch on the table of dispatch instructions.
*/
static int
compare_dispatch(const void *key, const void *element)
{
const char *command = key;
const struct dispatch *rule = element;
return strcasecmp(command, rule->command);
}
/*
** The dispatch function. Takes a command (as a struct cvector), the
** dispatch table (a SORTED array of dispatch structs), the number of
** elements in the table, the callback function for unknown commands, the
** callback function for syntax errors (commands called with the wrong number
** of arguments), and an opaque void * that will be passed to the callback
** functions.
*/
void
dispatch(struct cvector *command, const struct dispatch *table, size_t count,
dispatch_func unknown, dispatch_func syntax, void *cookie)
{
struct dispatch *rule;
int argc = command->count - 1;
if (argc < 0) {
(*unknown)(command, cookie);
return;
}
rule = bsearch(command->strings[0], table, count, sizeof(struct dispatch),
compare_dispatch);
if (rule == NULL)
(*unknown)(command, cookie);
else if (argc < rule->min_args || argc > rule->max_args)
(*syntax)(command, cookie);
else
(*rule->callback)(command, cookie);
}
|