File: moduleconfigs.c

package info (click to toggle)
redict 7.3.6%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,112 kB
  • sloc: ansic: 129,434; tcl: 46,164; makefile: 930; python: 815; ruby: 572; sh: 482; javascript: 30
file content (201 lines) | stat: -rw-r--r-- 8,336 bytes parent folder | download | duplicates (3)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// SPDX-FileCopyrightText: 2024 Redict Contributors
// SPDX-FileCopyrightText: 2024 Salvatore Sanfilippo <antirez at gmail dot com>
//
// SPDX-License-Identifier: BSD-3-Clause
// SPDX-License-Identifier: LGPL-3.0-only

#include "redictmodule.h"
#include <strings.h>
int mutable_bool_val;
int immutable_bool_val;
long long longval;
long long memval;
RedictModuleString *strval = NULL;
int enumval;
int flagsval;

/* Series of get and set callbacks for each type of config, these rely on the privdata ptr
 * to point to the config, and they register the configs as such. Note that one could also just
 * use names if they wanted, and store anything in privdata. */
int getBoolConfigCommand(const char *name, void *privdata) {
    REDICTMODULE_NOT_USED(name);
    return (*(int *)privdata);
}

int setBoolConfigCommand(const char *name, int new, void *privdata, RedictModuleString **err) {
    REDICTMODULE_NOT_USED(name);
    REDICTMODULE_NOT_USED(err);
    *(int *)privdata = new;
    return REDICTMODULE_OK;
}

long long getNumericConfigCommand(const char *name, void *privdata) {
    REDICTMODULE_NOT_USED(name);
    return (*(long long *) privdata);
}

int setNumericConfigCommand(const char *name, long long new, void *privdata, RedictModuleString **err) {
    REDICTMODULE_NOT_USED(name);
    REDICTMODULE_NOT_USED(err);
    *(long long *)privdata = new;
    return REDICTMODULE_OK;
}

RedictModuleString *getStringConfigCommand(const char *name, void *privdata) {
    REDICTMODULE_NOT_USED(name);
    REDICTMODULE_NOT_USED(privdata);
    return strval;
}
int setStringConfigCommand(const char *name, RedictModuleString *new, void *privdata, RedictModuleString **err) {
    REDICTMODULE_NOT_USED(name);
    REDICTMODULE_NOT_USED(err);
    REDICTMODULE_NOT_USED(privdata);
    size_t len;
    if (!strcasecmp(RedictModule_StringPtrLen(new, &len), "rejectisfreed")) {
        *err = RedictModule_CreateString(NULL, "Cannot set string to 'rejectisfreed'", 36);
        return REDICTMODULE_ERR;
    }
    if (strval) RedictModule_FreeString(NULL, strval);
    RedictModule_RetainString(NULL, new);
    strval = new;
    return REDICTMODULE_OK;
}

int getEnumConfigCommand(const char *name, void *privdata) {
    REDICTMODULE_NOT_USED(name);
    REDICTMODULE_NOT_USED(privdata);
    return enumval;
}

int setEnumConfigCommand(const char *name, int val, void *privdata, RedictModuleString **err) {
    REDICTMODULE_NOT_USED(name);
    REDICTMODULE_NOT_USED(err);
    REDICTMODULE_NOT_USED(privdata);
    enumval = val;
    return REDICTMODULE_OK;
}

int getFlagsConfigCommand(const char *name, void *privdata) {
    REDICTMODULE_NOT_USED(name);
    REDICTMODULE_NOT_USED(privdata);
    return flagsval;
}

int setFlagsConfigCommand(const char *name, int val, void *privdata, RedictModuleString **err) {
    REDICTMODULE_NOT_USED(name);
    REDICTMODULE_NOT_USED(err);
    REDICTMODULE_NOT_USED(privdata);
    flagsval = val;
    return REDICTMODULE_OK;
}

int boolApplyFunc(RedictModuleCtx *ctx, void *privdata, RedictModuleString **err) {
    REDICTMODULE_NOT_USED(ctx);
    REDICTMODULE_NOT_USED(privdata);
    if (mutable_bool_val && immutable_bool_val) {
        *err = RedictModule_CreateString(NULL, "Bool configs cannot both be yes.", 32);
        return REDICTMODULE_ERR;
    }
    return REDICTMODULE_OK;
}

int longlongApplyFunc(RedictModuleCtx *ctx, void *privdata, RedictModuleString **err) {
    REDICTMODULE_NOT_USED(ctx);
    REDICTMODULE_NOT_USED(privdata);
    if (longval == memval) {
        *err = RedictModule_CreateString(NULL, "These configs cannot equal each other.", 38);
        return REDICTMODULE_ERR;
    }
    return REDICTMODULE_OK;
}

int registerBlockCheck(RedictModuleCtx *ctx, RedictModuleString **argv, int argc) {
    REDICTMODULE_NOT_USED(argv);
    REDICTMODULE_NOT_USED(argc);
    int response_ok = 0;
    int result = RedictModule_RegisterBoolConfig(ctx, "mutable_bool", 1, REDICTMODULE_CONFIG_DEFAULT, getBoolConfigCommand, setBoolConfigCommand, boolApplyFunc, &mutable_bool_val);
    response_ok |= (result == REDICTMODULE_OK);

    result = RedictModule_RegisterStringConfig(ctx, "string", "secret password", REDICTMODULE_CONFIG_DEFAULT, getStringConfigCommand, setStringConfigCommand, NULL, NULL);
    response_ok |= (result == REDICTMODULE_OK);

    const char *enum_vals[] = {"none", "five", "one", "two", "four"};
    const int int_vals[] = {0, 5, 1, 2, 4};
    result = RedictModule_RegisterEnumConfig(ctx, "enum", 1, REDICTMODULE_CONFIG_DEFAULT, enum_vals, int_vals, 5, getEnumConfigCommand, setEnumConfigCommand, NULL, NULL);
    response_ok |= (result == REDICTMODULE_OK);

    result = RedictModule_RegisterNumericConfig(ctx, "numeric", -1, REDICTMODULE_CONFIG_DEFAULT, -5, 2000, getNumericConfigCommand, setNumericConfigCommand, longlongApplyFunc, &longval);
    response_ok |= (result == REDICTMODULE_OK);

    result = RedictModule_LoadConfigs(ctx);
    response_ok |= (result == REDICTMODULE_OK);
    
    /* This validates that it's not possible to register/load configs outside OnLoad,
     * thus returns an error if they succeed. */
    if (response_ok) {
        RedictModule_ReplyWithError(ctx, "UNEXPECTEDOK");
    } else {
        RedictModule_ReplyWithSimpleString(ctx, "OK");
    }
    return REDICTMODULE_OK;
}

int RedictModule_OnLoad(RedictModuleCtx *ctx, RedictModuleString **argv, int argc) {
    REDICTMODULE_NOT_USED(argv);
    REDICTMODULE_NOT_USED(argc);

    if (RedictModule_Init(ctx, "moduleconfigs", 1, REDICTMODULE_APIVER_1) == REDICTMODULE_ERR) return REDICTMODULE_ERR;

    if (RedictModule_RegisterBoolConfig(ctx, "mutable_bool", 1, REDICTMODULE_CONFIG_DEFAULT, getBoolConfigCommand, setBoolConfigCommand, boolApplyFunc, &mutable_bool_val) == REDICTMODULE_ERR) {
        return REDICTMODULE_ERR;
    }
    /* Immutable config here. */
    if (RedictModule_RegisterBoolConfig(ctx, "immutable_bool", 0, REDICTMODULE_CONFIG_IMMUTABLE, getBoolConfigCommand, setBoolConfigCommand, boolApplyFunc, &immutable_bool_val) == REDICTMODULE_ERR) {
        return REDICTMODULE_ERR;
    }
    if (RedictModule_RegisterStringConfig(ctx, "string", "secret password", REDICTMODULE_CONFIG_DEFAULT, getStringConfigCommand, setStringConfigCommand, NULL, NULL) == REDICTMODULE_ERR) {
        return REDICTMODULE_ERR;
    }

    /* On the stack to make sure we're copying them. */
    const char *enum_vals[] = {"none", "five", "one", "two", "four"};
    const int int_vals[] = {0, 5, 1, 2, 4};

    if (RedictModule_RegisterEnumConfig(ctx, "enum", 1, REDICTMODULE_CONFIG_DEFAULT, enum_vals, int_vals, 5, getEnumConfigCommand, setEnumConfigCommand, NULL, NULL) == REDICTMODULE_ERR) {
        return REDICTMODULE_ERR;
    }
    if (RedictModule_RegisterEnumConfig(ctx, "flags", 3, REDICTMODULE_CONFIG_DEFAULT | REDICTMODULE_CONFIG_BITFLAGS, enum_vals, int_vals, 5, getFlagsConfigCommand, setFlagsConfigCommand, NULL, NULL) == REDICTMODULE_ERR) {
        return REDICTMODULE_ERR;
    }
    /* Memory config here. */
    if (RedictModule_RegisterNumericConfig(ctx, "memory_numeric", 1024, REDICTMODULE_CONFIG_DEFAULT | REDICTMODULE_CONFIG_MEMORY, 0, 3000000, getNumericConfigCommand, setNumericConfigCommand, longlongApplyFunc, &memval) == REDICTMODULE_ERR) {
        return REDICTMODULE_ERR;
    }
    if (RedictModule_RegisterNumericConfig(ctx, "numeric", -1, REDICTMODULE_CONFIG_DEFAULT, -5, 2000, getNumericConfigCommand, setNumericConfigCommand, longlongApplyFunc, &longval) == REDICTMODULE_ERR) {
        return REDICTMODULE_ERR;
    }
    size_t len;
    if (argc && !strcasecmp(RedictModule_StringPtrLen(argv[0], &len), "noload")) {
        return REDICTMODULE_OK;
    } else if (RedictModule_LoadConfigs(ctx) == REDICTMODULE_ERR) {
        if (strval) {
            RedictModule_FreeString(ctx, strval);
            strval = NULL;
        }
        return REDICTMODULE_ERR;
    }
    /* Creates a command which registers configs outside OnLoad() function. */
    if (RedictModule_CreateCommand(ctx,"block.register.configs.outside.onload", registerBlockCheck, "write", 0, 0, 0) == REDICTMODULE_ERR)
        return REDICTMODULE_ERR;
  
    return REDICTMODULE_OK;
}

int RedictModule_OnUnload(RedictModuleCtx *ctx) {
    REDICTMODULE_NOT_USED(ctx);
    if (strval) {
        RedictModule_FreeString(ctx, strval);
        strval = NULL;
    }
    return REDICTMODULE_OK;
}