forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mozprefs.c
265 lines (243 loc) · 6.49 KB
/
mozprefs.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
/**
* @file
*
* @brief Source for mozprefs plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#include "mozprefs.h"
#include <kdbhelper.h>
#include <kdbutility.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef enum
{
PREF = 0,
USER,
LOCK,
STICKY,
PREF_END,
} PrefType;
const char * function[] = { "pref", "user_pref", "lockPref", "sticky_pref" };
const char * prefix[] = { "pref", "user", "lock", "sticky" };
static Key * prefToKey (Key * parentKey, PrefType type, const char * pref)
{
Key * key = keyNew (keyName (parentKey), KEY_END);
keyAddBaseName (key, prefix[type]);
char * localString = elektraStrDup (pref);
char * cPtr = strstr (localString, ",");
*cPtr = '\0';
char * sPtr = localString;
++sPtr;
*sPtr++ = '\0';
char * ePtr = cPtr - 1;
elektraRstrip (sPtr, &ePtr);
size_t keyLen = ePtr - sPtr;
char * prefKey = elektraMalloc (keyLen + 1);
snprintf (prefKey, keyLen + 1, "%s", sPtr);
char * tPtr = strtok (prefKey, ".");
if (tPtr) keyAddBaseName (key, tPtr);
while ((tPtr = strtok (NULL, ".")) != NULL)
{
keyAddBaseName (key, tPtr);
}
elektraFree (prefKey);
sPtr = cPtr + 1;
sPtr = elektraLskip (sPtr);
ePtr = strrchr (sPtr, ')');
*ePtr-- = '\0';
elektraRstrip (sPtr, &ePtr);
size_t argLen = ePtr - sPtr + 1;
char * prefArg = elektraMalloc (argLen + 1);
snprintf (prefArg, argLen + 1, "%s", sPtr);
if (!strcmp (prefArg, "true") || !(strcmp (prefArg, "false")))
{
keySetMeta (key, "type", "boolean");
keySetString (key, prefArg);
}
else if (prefArg[0] == '"' && prefArg[strlen (prefArg) - 1] == '"')
{
// TODO: else if list
keySetMeta (key, "type", "string");
*prefArg = '\0';
*(prefArg + (strlen (prefArg + 1))) = '\0';
keySetString (key, (prefArg + 1));
}
else
{
keySetMeta (key, "type", "integer");
keySetString (key, prefArg);
}
elektraFree (prefArg);
elektraFree (localString);
return key;
}
int elektraMozprefsGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
{
if (!elektraStrCmp (keyName (parentKey), "system:/elektra/modules/mozprefs"))
{
KeySet * contract =
ksNew (30, keyNew ("system:/elektra/modules/mozprefs", KEY_VALUE, "mozprefs plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/mozprefs/exports", KEY_END),
keyNew ("system:/elektra/modules/mozprefs/exports/get", KEY_FUNC, elektraMozprefsGet, KEY_END),
keyNew ("system:/elektra/modules/mozprefs/exports/set", KEY_FUNC, elektraMozprefsSet, KEY_END),
#include ELEKTRA_README
keyNew ("system:/elektra/modules/mozprefs/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
ksAppend (returned, contract);
ksDel (contract);
return 1; // success
}
// get all keys
const char * fileName = keyString (parentKey);
FILE * fp = fopen (fileName, "r");
int len = 1024;
char * buffer = elektraMalloc (len * sizeof (char));
Key * key;
while (fgets (buffer, len, fp))
{
if (buffer[strlen (buffer) - 1] != '\n')
{
fseek (fp, ((len - 1) * (-1)), SEEK_CUR);
len *= 2;
elektraRealloc ((void **) &buffer, len * sizeof (char));
continue;
}
else
{
buffer[strlen (buffer) - 1] = '\0';
}
char * ptr = buffer;
ptr = elektraLskip (ptr);
if (!strncmp (buffer, "//", 2)) continue;
for (PrefType p = PREF; p < PREF_END; ++p)
{
if (!strncmp (ptr, function[p], strlen (function[p])))
{
key = prefToKey (parentKey, p, ptr + strlen (function[p]));
ksAppendKey (returned, key);
goto loop_end;
}
}
loop_end:
continue;
}
elektraFree (buffer);
fclose (fp);
return 1; // success
}
static char * keyNameToPrefName (const char * prefName)
{
char * buffer = elektraCalloc (strlen (prefName) + 1);
char * src = (char *) prefName;
char * dst = buffer;
unsigned short flag = 0;
while (*src)
{
switch (*src)
{
case '\\':
if (flag)
{
*dst++ = *src;
flag = 0;
}
else
flag = 1;
break;
case '/':
*dst++ = '.';
break;
default:
*dst++ = *src;
break;
}
++src;
}
return buffer;
}
static inline const char * prefTypToFunction (PrefType pref)
{
if (pref >= PREF_END) return NULL;
return function[pref];
}
// returns a string representing the preference value depending on
// it's type as a quoted string, integer or boolean value
static char * prefArgToString (const Key * key)
{
const Key * typeMeta = keyGetMeta (key, "type");
char * buffer = NULL;
if (!strcmp (keyString (typeMeta), "boolean"))
{
buffer = elektraStrDup (keyString (key));
}
else if (!strcmp (keyString (typeMeta), "string"))
{
ssize_t len = keyGetValueSize (key) + 2; // size of string + leading and trailing '"'
buffer = elektraCalloc (len);
snprintf (buffer, len, "\"%s\"", keyString (key));
}
else if (!strcmp (keyString (typeMeta), "integer"))
{
buffer = elektraStrDup (keyString (key));
}
else
{
ssize_t len = keyGetValueSize (key) + 2;
buffer = elektraCalloc (len);
snprintf (buffer, len, "\"%s\"", keyString (key));
}
return buffer;
}
static void writeKey (FILE * fp, const Key * parentKey, const Key * key)
{
char * prefName = (char *) keyName (key) + strlen (keyName (parentKey)) + 1; // skip parentKey name + '/'
unsigned short flag = 0;
PrefType pref = PREF;
for (; pref < PREF_END; ++pref)
{
if (!strncmp (prefName, prefix[pref], strlen (prefix[pref])))
{
flag = 1;
prefName += strlen (prefix[pref]) + 1; // skip prefix len + '/'
break;
}
}
if (!flag) return;
char * realPrefName = keyNameToPrefName (prefName);
if (!realPrefName) return;
const char * functionName = prefTypToFunction (pref);
char * argString = NULL;
if (!functionName) goto write_cleanup;
argString = prefArgToString (key);
if (!argString) goto write_cleanup;
fprintf (fp, "%s(\"%s\", %s);\n", functionName, realPrefName, argString);
write_cleanup:
if (realPrefName) elektraFree (realPrefName);
if (argString) elektraFree (argString);
}
int elektraMozprefsSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
{
// get all keys
// this function is optional
FILE * fp = fopen (keyString (parentKey), "w");
if (!fp) return -1;
for (elektraCursor it = 0; it < ksGetSize (returned); ++it)
{
Key * cur = ksAtCursor (returned, it);
if (!strcmp (keyName (parentKey), keyName (cur))) continue;
writeKey (fp, parentKey, cur);
}
fclose (fp);
return 1; // success
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport ("mozprefs",
ELEKTRA_PLUGIN_GET, &elektraMozprefsGet,
ELEKTRA_PLUGIN_SET, &elektraMozprefsSet,
ELEKTRA_PLUGIN_END);
}