forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc.c
288 lines (239 loc) · 7.02 KB
/
doc.c
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "doc.h"
//! [plugin include]
#include <kdbplugin.h>
//! [plugin include]
//
//! [plugin errors include]
// using namespace ckdb; // for C++
#include <kdberrors.h>
//! [plugin errors include]
#include <stdio.h>
#include <stdlib.h>
#ifndef HAVE_KDBCONFIG
#include "kdbconfig.h"
#endif
//! [global data]
typedef struct
{
int global;
} GlobalData;
//! [global data]
#define DOC_PLUGIN_NAME "doc"
#define DOC_PLUGIN_VERSION "1.0.0"
//! [doc open]
int elektraDocOpen (Plugin * handle, Key * warningsKey ELEKTRA_UNUSED)
{
GlobalData * data;
KeySet * config = elektraPluginGetConfig (handle);
Key * kg = ksLookupByName (config, "/global", 0);
data = elektraMalloc (sizeof (GlobalData));
data->global = 0;
if (kg) data->global = atoi (keyString (kg));
elektraPluginSetData (handle, data);
//! [doc open]
//! [doc module]
if (ksLookupByName (config, "/module", 0))
{
return 0;
}
// do some setup that will fail without configuration
//! [doc module]
return 0; /* success */
}
//! [doc close]
int elektraDocClose (Plugin * handle, Key * warningsKey ELEKTRA_UNUSED)
{
elektraFree (elektraPluginGetData (handle));
return 0; /* success */
}
//! [doc close]
static int parseKey (FILE * fp ELEKTRA_UNUSED, char ** key ELEKTRA_UNUSED, char ** value ELEKTRA_UNUSED)
{
return 0;
}
static void doAction (Key * k ELEKTRA_UNUSED)
{
}
/*
//! [plugin errors spec]
number:60
description:Invalid Line encountered
severity:error
macro:NOEOF
module:simpleini
//! [plugin errors spec]
//! [plugin errors usage]
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERROR ( parentKey, "Not at the end of file");
//! [plugin errors usage]
*/
//![get contract]
int elektraDocGet (Plugin * plugin ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
{
if (!strcmp (keyName (parentKey), "system:/elektra/modules/doc"))
{
KeySet * contract =
ksNew (30, keyNew ("system:/elektra/modules/doc", KEY_VALUE, "doc plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/doc/exports", KEY_END),
keyNew ("system:/elektra/modules/doc/exports/open", KEY_FUNC, elektraDocOpen, KEY_END),
keyNew ("system:/elektra/modules/doc/exports/close", KEY_FUNC, elektraDocClose, KEY_END),
keyNew ("system:/elektra/modules/doc/exports/get", KEY_FUNC, elektraDocGet, KEY_END),
keyNew ("system:/elektra/modules/doc/exports/set", KEY_FUNC, elektraDocSet, KEY_END),
keyNew ("system:/elektra/modules/doc/exports/commit", KEY_FUNC, elektraDocCommit, KEY_END),
keyNew ("system:/elektra/modules/doc/exports/error", KEY_FUNC, elektraDocError, KEY_END),
keyNew ("system:/elektra/modules/doc/exports/checkconf", KEY_FUNC, elektraDocCheckConf, KEY_END),
#include ELEKTRA_README
keyNew ("system:/elektra/modules/doc/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
ksAppend (returned, contract);
ksDel (contract);
return 1; /* success */
}
//![get contract]
//![get storage]
FILE * fp = fopen (keyString (parentKey), "r");
char * key = 0;
char * value = 0;
while (parseKey (fp, &key, &value) >= 1)
{
Key * read = keyNew (keyName (parentKey), KEY_END);
if (keyAddName (read, key) == -1)
{
ELEKTRA_ADD_VALIDATION_SYNTACTIC_WARNINGF (parentKey, "Key name %s is not valid, discarding key", key);
keyDel (read);
continue;
}
keySetString (read, value);
ksAppendKey (returned, read);
}
if (feof (fp) == 0)
{
fclose (fp);
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERROR (parentKey, "Invalid line encountered: not at the end of file");
return -1;
}
fclose (fp);
//![get storage]
//![get global keyset]
KeySet * globalKS = elektraPluginGetGlobalKeySet (plugin);
// now we can read something from the global keyset
// or add something for us or others to read
Key * important = keyNew ("user:/global/myDocKey", KEY_VALUE, "global plugins can see me", KEY_END);
ksAppendKey (globalKS, important);
//![get global keyset]
//![get global keyset cleanup]
// clean up parts of the global keyset which we do not need
Key * cutKey = keyNew ("user:/global/myDocKey", KEY_END);
KeySet * notNeeded = ksCut (globalKS, cutKey);
ksDel (notNeeded);
//![get global keyset cleanup]
//![get filter]
Key * k;
ksRewind (returned);
while ((k = ksNext (returned)) != 0)
{
doAction (k);
}
return 1; // success
}
//![get filter]
int elektraDocSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey)
{
ssize_t nr_keys = 0;
/* set all keys below parentKey and count them with nr_keys */
//![opening files]
FILE * fp = fopen (keyString (parentKey), "w");
if (!fp)
{
ELEKTRA_SET_ERROR_SET (parentKey);
return -1;
}
//![opening files]
fclose (fp);
return nr_keys;
}
int elektraDocError (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey ELEKTRA_UNUSED)
{
return 0;
}
int elektraDocCommit (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey ELEKTRA_UNUSED)
{
return 0;
}
static Plugin * findPlugin (KDB * handle ELEKTRA_UNUSED)
{
return 0;
}
static void saveToDisc (Key * k ELEKTRA_UNUSED)
{
}
//![validate configuration]
int elektraDocCheckConf (Key * errorKey ELEKTRA_UNUSED, KeySet * conf ELEKTRA_UNUSED)
{
/* validate plugin configuration */
// the return codes have the following meaning:
// 0: The configuration was OK and has not been changed
// 1: The configuration has been changed and now it is OK
// -1: The configuration was not OK and could not be fixed. An error has to be set to errorKey.
return 0;
}
//![validate configuration]
//![set full]
static void usercode (KDB * handle, KeySet * keyset, Key * key)
{
// some more user code
keySetString (key, "mycomment"); // the user changes the key
ksAppendKey (keyset, key); // append the key to the keyset
kdbSet (handle, keyset, 0); // and syncs it to disc
}
// so now kdbSet is called
int elektraKdbSet (KDB * handle, KeySet * keyset, Key * parentKey)
{
int ret = 0;
// find appropriate plugin and then call it:
Plugin * plugin = findPlugin (handle);
ret = elektraDocSet (plugin, keyset, parentKey);
// the keyset with the key (and others for this plugin)
// will be passed to this function
return ret;
}
// so now elektraPluginSet(), which is the function described here,
// is called:
int elektraPluginSet (Plugin * plugin ELEKTRA_UNUSED, KeySet * returned, Key * parentKey ELEKTRA_UNUSED)
{
// the task of elektraPluginSet is now to store the keys
Key * k;
ksRewind (returned);
while ((k = ksNext (returned)) != 0)
{
saveToDisc (k);
}
return 1; /* success */
}
//![set full]
void elektraUsercodeUselessSymbol (void)
{
usercode (0, 0, 0);
}
//![export]
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport(DOC_PLUGIN_NAME,
ELEKTRA_PLUGIN_OPEN, &elektraDocOpen,
ELEKTRA_PLUGIN_CLOSE, &elektraDocClose,
ELEKTRA_PLUGIN_GET, &elektraDocGet,
ELEKTRA_PLUGIN_SET, &elektraDocSet,
ELEKTRA_PLUGIN_ERROR, &elektraDocError,
ELEKTRA_PLUGIN_COMMIT, &elektraDocCommit,
ELEKTRA_PLUGIN_END);
}
//![export]
/**
* @}
*/