aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-light/src/hicn/config/command.c
blob: 4c89d668097764b192322ab6fc0c3b2cae0db0ba (plain)
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
58
59
60
61
/**
 * @file command.c
 * @brief Implementation of commands.
 */

#include <search.h> /* tfind, tdestroy, twalk */
#include <stdio.h>

#include "command.h"

/* Commands are registered in the following tree. */
static void * commands_root    = NULL;  /**< Tree ordered by name */

static void nothing_to_free() {}

#ifdef __linux__
__attribute__((destructor))
static
void
command_clear()
{
    tdestroy(commands_root, nothing_to_free);
}
#endif /* __linux__ */

static
int
_command_compare(const command_parser_t * c1,
        const command_parser_t * c2)
{
    if (c1->action != c2->action)
        return c2->action - c1->action;
    if (c1->object != c2->object)
        return c2->object - c1->object;
    if (c1->nparams != c2->nparams)
        return c2->nparams - c1->nparams;
    return 0;
}

#define command_compare (int (*)(const void *, const void *))(_command_compare)

void
command_register(const command_parser_t * command)
{
    // Insert the command in the tree if the keys does not exist yet
    tsearch(command, &commands_root, command_compare);
}

const command_parser_t *
command_search(hc_action_t action, hc_object_type_t object, unsigned nparams)
{
    command_parser_t ** command, search;

    search.action = action;
    search.object = object;
    search.nparams = nparams;
    command = tfind(&search, &commands_root, command_compare);

    return command ? *command : NULL;
}