File: dispatch.c

package info (click to toggle)
inn2 2.6.3-1%2Bdeb10u2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,304 kB
  • sloc: ansic: 96,539; sh: 15,562; perl: 13,281; makefile: 3,700; yacc: 842; python: 309; lex: 262
file content (57 lines) | stat: -rw-r--r-- 1,663 bytes parent folder | download | duplicates (6)
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);
}