This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
rename.c
403 lines (353 loc) · 9.76 KB
/
rename.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/**
* @file
*
* @brief Implementation of plugin rename
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#include "rename.h"
#ifndef HAVE_KDBCONFIG
#include "kdbconfig.h"
#endif
#include <ctype.h>
#include <errno.h>
#include <kdbhelper.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <kdbprivate.h> // for access to sync bit (keyClearSync)
#define ELEKTRA_ORIGINAL_NAME_META "origname"
#define TOLOWER (-1)
#define TOUPPER 1
#define UNCHNGD 0
#define KEYNAME 2
static void doConversion (char * newName, int levels, const int toCase)
{
int (*conversion) (int);
if (toCase == TOUPPER)
{
conversion = toupper;
}
else
{
conversion = tolower;
}
char * returnName = elektraCalloc (strlen (newName) + 1);
if (levels == 0)
{
unsigned int i = 0;
for (; i < strlen (newName); ++i)
{
returnName[i] = conversion (newName[i]);
}
}
else
{
short levelCount = 0;
int i = 0;
for (i = strlen (newName); i >= 0 && levelCount < levels; --i)
{
if (newName[i] == '/')
{
++levelCount;
returnName[i] = newName[i];
continue;
}
returnName[i] = conversion (newName[i]);
}
for (; i >= 0; --i)
{
returnName[i] = newName[i];
}
}
strncpy (newName, returnName, strlen (newName));
free (returnName);
}
Key * elektraKeyCreateNewName (const Key * key, const Key * parentKey, const char * cutPath, const char * replaceWith,
const char * toUpperPath, const char * toLowerPath, const int initialConversion)
{
size_t addToLen = 0;
if (replaceWith != NULL) addToLen = strlen (replaceWith);
size_t maxNewLength = strlen (keyName (key)) + addToLen;
char * newName = elektraCalloc (maxNewLength + 1);
short replace = 0;
char * parentKeyName = elektraMalloc (keyGetNameSize (parentKey));
keyGetName (parentKey, parentKeyName, keyGetNameSize (parentKey));
char * curKeyName = elektraMalloc (keyGetNameSize (key));
keyGetName (key, curKeyName, keyGetNameSize (key));
char * afterParentString = curKeyName + (strlen (parentKeyName));
char * ptr;
if (initialConversion != UNCHNGD)
{
doConversion (afterParentString, 0, initialConversion);
replace = 1;
}
if (cutPath && (cutPath[0] != '/') && ((ptr = strstr (afterParentString, cutPath)) != NULL))
{
strncpy (newName, afterParentString, (ptr - afterParentString));
if (replaceWith)
{
strncpy (newName + strlen (newName), replaceWith, elektraStrLen (replaceWith));
}
strncat (newName, ptr + (strlen (cutPath)), strlen (afterParentString) - strlen (cutPath));
replace = 1;
}
else
{
strncpy (newName, afterParentString, elektraStrLen (afterParentString));
}
int toLower = toLowerPath ? atoi (toLowerPath) : 0;
int toUpper = toUpperPath ? atoi (toUpperPath) : 0;
if (strlen (newName) > 0)
{
if (toUpperPath && toLowerPath)
{
if (toUpper < toLower)
{
doConversion (newName, toLower, TOLOWER);
doConversion (newName, toUpper, TOUPPER);
}
else
{
doConversion (newName, toUpper, TOUPPER);
doConversion (newName, toLower, TOLOWER);
}
replace = 1;
}
else if (toUpperPath)
{
doConversion (newName, toUpper, TOUPPER);
replace = 1;
}
else if (toLowerPath)
{
doConversion (newName, toLower, TOLOWER);
replace = 1;
}
}
elektraFree (parentKeyName);
elektraFree (curKeyName);
if (replace)
{
Key * result = keyDup (key, KEY_CP_ALL);
keySetName (result, keyName (parentKey));
keyAddName (result, newName);
elektraFree (newName);
return result;
}
elektraFree (newName);
return 0;
}
static Key * renameGet (Key * key, Key * parentKey, Key * cutConfig, Key * replaceWithConfig, Key * toUpperConfig, Key * toLowerConfig,
Key * getCase)
{
char * cutPath = 0;
char * replaceWith = 0;
char * toUpperPath = 0;
char * toLowerPath = 0;
const Key * cutMeta = keyGetMeta (key, "rename/cut");
const Key * toMeta = keyGetMeta (key, "rename/to");
const Key * toUpperMeta = keyGetMeta (key, "rename/toupper");
const Key * toLowerMeta = keyGetMeta (key, "rename/tolower");
int initialConversion = 0;
if (getCase)
{
const char * str = keyString (getCase);
if (!strcmp (str, "toupper"))
{
initialConversion = TOUPPER;
}
else if (!strcmp (str, "tolower"))
{
initialConversion = TOLOWER;
}
else
{
initialConversion = UNCHNGD;
}
}
/* if the meta config exists, it takes precedence over the global config */
if (cutMeta)
cutPath = (char *) keyString (cutMeta);
else if (cutConfig)
cutPath = (char *) keyString (cutConfig);
if (toMeta)
replaceWith = (char *) keyString (toMeta);
else if (replaceWithConfig)
replaceWith = (char *) keyString (replaceWithConfig);
if (toUpperMeta)
toUpperPath = (char *) keyString (toUpperMeta);
else if (toUpperConfig)
toUpperPath = (char *) keyString (toUpperConfig);
if (toLowerMeta)
toLowerPath = (char *) keyString (toLowerMeta);
else if (toLowerConfig)
toLowerPath = (char *) keyString (toLowerConfig);
return elektraKeyCreateNewName (key, parentKey, cutPath, replaceWith, toUpperPath, toLowerPath, initialConversion);
}
static Key * restoreKeyName (Key * key, const Key * parentKey, const Key * configKey)
{
const Key * origNameKey = keyGetMeta (key, ELEKTRA_ORIGINAL_NAME_META);
if (origNameKey)
{
if (strcmp (keyString (origNameKey), keyName (key)))
{
Key * result = keyDup (key, KEY_CP_ALL);
keySetName (result, keyString (origNameKey));
keySetMeta (result, ELEKTRA_ORIGINAL_NAME_META, 0);
return result;
}
}
else
{
if (configKey)
{
Key * result = keyDup (key, KEY_CP_ALL);
keySetName (result, keyName (parentKey));
keyAddName (result, keyString (configKey));
if (keyGetNameSize (key) > keyGetNameSize (parentKey))
{
/* this calculation does not work for the parent key but is also not needed */
const char * relativePath = keyName (key) + keyGetNameSize (parentKey);
keyAddName (result, relativePath);
}
return result;
}
}
return 0;
}
int elektraRenameGet (Plugin * handle, KeySet * returned, Key * parentKey)
{
/* configuration only */
if (!strcmp (keyName (parentKey), "system:/elektra/modules/rename"))
{
KeySet * info =
#include "contract.h"
ksAppend (returned, info);
ksDel (info);
return 1;
}
KeySet * config = elektraPluginGetConfig (handle);
KeySet * iterateKs = ksDup (returned);
Key * cutConfig = ksLookupByName (config, "/cut", KDB_O_NONE);
Key * toUpper = ksLookupByName (config, "/toupper", KDB_O_NONE);
Key * toLower = ksLookupByName (config, "/tolower", KDB_O_NONE);
Key * replaceWith = ksLookupByName (config, "/replacewith", KDB_O_NONE);
Key * getCase = ksLookupByName (config, "/get/case", KDB_O_NONE);
for (elektraCursor it = 0; it < ksGetSize (iterateKs); ++it)
{
Key * key = ksAtCursor (iterateKs, it);
Key * renamedKey = renameGet (key, parentKey, cutConfig, replaceWith, toUpper, toLower, getCase);
if (renamedKey)
{
keySetMeta (renamedKey, ELEKTRA_ORIGINAL_NAME_META, keyName (key));
ksLookup (returned, key, KDB_O_POP);
keyDel (key);
/*
* if the parentKey is replaced by a rename operation
* make sure that we do not loose its reference (ksAppendKey
* would otherwise delete it)
*/
if (keyCmp (renamedKey, parentKey) == 0)
{
/* make sure the parent key is not deleted */
keyIncRef (parentKey);
ksAppendKey (returned, renamedKey);
keyDecRef (parentKey);
}
else
{
ksAppendKey (returned, renamedKey);
}
}
else
{
keySetMeta (key, ELEKTRA_ORIGINAL_NAME_META, keyName (key));
}
}
/* make sure the parent key is not deleted */
keyIncRef (parentKey);
ksDel (iterateKs);
keyDecRef (parentKey);
return 1; /* success */
}
int elektraRenameSet (Plugin * handle, KeySet * returned, Key * parentKey)
{
KeySet * iterateKs = ksDup (returned);
KeySet * config = elektraPluginGetConfig (handle);
Key * cutConfig = ksLookupByName (config, "/cut", KDB_O_NONE);
Key * setCase = ksLookupByName (config, "/set/case", KDB_O_NONE);
int writeConversion = 0;
if (setCase)
{
const char * str = keyString (setCase);
if (!strcmp (str, "toupper"))
{
writeConversion = TOUPPER;
}
else if (!strcmp (str, "tolower"))
{
writeConversion = TOLOWER;
}
else if (!strcmp (str, "keyname"))
{
writeConversion = KEYNAME;
}
else
{
writeConversion = UNCHNGD;
}
}
char * parentKeyName = elektraMalloc (keyGetNameSize (parentKey));
keyGetName (parentKey, parentKeyName, keyGetNameSize (parentKey));
for (elektraCursor it = 0; it < ksGetSize (iterateKs); ++it)
{
Key * key = ksAtCursor (iterateKs, it);
Key * renamedKey = NULL;
if (writeConversion != KEYNAME)
{
renamedKey = restoreKeyName (key, parentKey, cutConfig);
if (!renamedKey) renamedKey = keyDup (key, KEY_CP_ALL);
if (writeConversion == TOUPPER || writeConversion == TOLOWER)
{
char * curKeyName = elektraMalloc (keyGetNameSize (renamedKey));
keyGetName (renamedKey, curKeyName, keyGetNameSize (renamedKey));
char * afterParentString = curKeyName + (strlen (parentKeyName));
doConversion (afterParentString, 0, writeConversion);
keySetName (renamedKey, curKeyName);
elektraFree (curKeyName);
}
/*
* if something is restored from the parentKey, do
* not delete the parentKey (might cause troubles)
*/
if (keyCmp (key, parentKey) != 0)
{
keyDel (ksLookup (returned, key, KDB_O_POP));
}
ksAppendKey (returned, renamedKey);
}
else
{
if (keyCmp (key, parentKey) != 0)
{
keyDel (ksLookupByName (returned, keyString (keyGetMeta (key, ELEKTRA_ORIGINAL_NAME_META)), KDB_O_POP));
}
ksAppendKey (returned, key);
}
}
keyIncRef (parentKey);
ksDel (iterateKs);
keyDecRef (parentKey);
elektraFree (parentKeyName);
return 1; /* success */
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport("rename",
ELEKTRA_PLUGIN_GET, &elektraRenameGet,
ELEKTRA_PLUGIN_SET, &elektraRenameSet,
ELEKTRA_PLUGIN_END);
}