-
Notifications
You must be signed in to change notification settings - Fork 3
/
kconfig.cpp
471 lines (448 loc) · 17.9 KB
/
kconfig.cpp
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/********************************************************************
KConfig - a CLI config tool
This file is part of the KDE project.
Copyright (C) 2013 Thomas Lübking <thomas.luebking@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include <iostream>
#include <KConfig>
#include <KConfigGroup>
#include <QFile>
#include <QtDebug>
// #include <QSettings>
#ifdef Q_OS_WIN32 // windows of course needs an extra invitation.
#include <io.h>
#define IS_A_TTY(_I_) _isatty(_I_)
#else
#include <unistd.h>
#define IS_A_TTY(_I_) isatty(_I_)
#endif
#define CHAR(_S_) _S_.toLocal8Bit().data()
static const char *gs_separator = "\n ---";
void usage() {
std::cout << "kconfig <component>[/<group>[/<subgroup>[...]]] read|write|delete|list|replace [<key>] [<value>]\n"
"* read|get <key>\n"
" prints <key> in this (sub)group\n"
"* write|set <key> <value>\n"
" sets <key> to <value>, informs if the key did not exist before\n"
"* delete <key>\n"
" removes the <key> from the (sub)group and informs whether there was such\n"
"* deletegroup <key>\n"
" removes the group <key> from the (sub)group and informs whether there was such\n"
"* list [<key>]\n"
" lists all keys in the (sub)group matching the regular expression <key> (if provided)\n"
"* listkeys [<key>]\n"
" like list, but does not show values (for autocompletion)\n"
"* listgroups [<key>]\n"
" like list, but only shows (sub)groups (for autocompletion)\n"
"* replace <key> <value>\n"
" replaces regular expression <key> with <value>, eg. \n"
" kconfig MyApp/Group replace Item(.*)=Old(.*) Item\\1=New\\2\n";
}
enum Mode { Invalid = 0, Read, Write, List, ListKeys, ListGroups, Replace, Delete, DeleteGroup };
QString path(KConfigGroup group)
{
QString ret;
while (group.exists()) {
ret.prepend( "/" + group.name());
group = group.parent();
}
ret.prepend(group.config()->name());
return ret;
}
void process(Mode mode, KConfigGroup &grp, QString key, QString value)
{
switch (mode) {
case Read:
if (IS_A_TTY(1))
std::cout << CHAR(key) << ": " << CHAR(grp.readEntry(key, "does not exist")) << " (" << CHAR(path(grp)) << ")" << std::endl;
else
std::cout << CHAR(grp.readEntry(key, ""));
break;
case Write: {
if (grp.isImmutable()) {
std::cout << "The component/group " << CHAR(path(grp)) << " cannot be modified" << std::endl;
exit(1);
}
bool added = !grp.hasKey(key);
QString oldv;
if (!added) oldv = grp.readEntry(key);
grp.writeEntry(key, QString(value));
grp.sync();
if (added)
std::cout << "New " << CHAR(key) << ": " << CHAR(grp.readEntry(key)) << std::endl;
else
std::cout << CHAR(key) << ": " << CHAR(oldv) << " -> " << CHAR(grp.readEntry(key)) << std::endl;
break;
}
case Delete: {
if (grp.isImmutable()) {
std::cout << "The component/group " << CHAR(path(grp)) << " cannot be modified" << std::endl;
exit(1);
}
if (grp.hasKey(key)) {
std::cout << "Removed " << CHAR(key) << ": " << CHAR(grp.readEntry(key)) << std::endl;
grp.deleteEntry(key);
grp.sync();
} else if (grp.hasGroup(key)) {
std::cout << "There's a group, but no key: " << CHAR(key) << "\nPlease explicitly use deletegroup" << std::endl;
exit(1);
} else {
std::cout << "There's no key " << CHAR(key) << " in " << CHAR(path(grp)) << std::endl;
exit(1);
}
break;
}
case DeleteGroup: {
if (grp.hasGroup(key)) {
grp = grp.group(key);
if (grp.isImmutable()) {
std::cout << "The component/group " << CHAR(path(grp)) << " cannot be modified" << std::endl;
exit(1);
}
QMap<QString, QString> map = grp.entryMap();
std::cout << "Removed " << CHAR(key) << gs_separator << std::endl;
for (QMap<QString, QString>::const_iterator it = map.constBegin(), end = map.constEnd(); it != end; ++it) {
std::cout << CHAR(it.key()) << ": " << CHAR(it.value()) << std::endl;
}
grp.deleteGroup();
grp.sync();
} else {
std::cout << "There's no group " << CHAR(key) << " in " << CHAR(path(grp)) << std::endl;
exit(1);
}
break;
}
case List:
case ListKeys: {
if (!grp.exists()) { // could be parent group
if (mode == ListKeys)
exit(1);
QStringList groups = grp.parent().exists() ? grp.parent().groupList() : grp.config()->groupList();
if (groups.isEmpty()) {
std::cout << "The component/group " << CHAR(path(grp)) << " does not exist" << std::endl;
exit(1);
}
std::cout << "Groups in " << CHAR(path(grp)) << gs_separator << std::endl;
foreach (const QString &s, groups)
if (key.isEmpty() || s.contains(key, Qt::CaseInsensitive))
std::cout << CHAR(s) << std::endl;
exit(0);
}
QMap<QString, QString> map = grp.entryMap();
if (map.isEmpty()) {
std::cout << "The group " << CHAR(path(grp)) << " is empty" << std::endl;
break;
}
if (mode == List) {
bool matchFound = false;
for (QMap<QString, QString>::const_iterator it = map.constBegin(), end = map.constEnd(); it != end; ++it) {
if (key.isEmpty() || it.key().contains(key, Qt::CaseInsensitive)) {
if (!matchFound)
std::cout << std::endl << CHAR(path(grp)) << gs_separator << std::endl;
matchFound = true;
std::cout << CHAR(it.key()) << ": " << CHAR(it.value()) << std::endl;
}
}
if (!matchFound)
std::cout << "No present key matches \"" << CHAR(key) << "\" in " << CHAR(path(grp));
std::cout << std::endl;
} else {
for (QMap<QString, QString>::const_iterator it = map.constBegin(), end = map.constEnd(); it != end; ++it) {
if (key.isEmpty() || it.key().contains(key, Qt::CaseInsensitive)) {
std::cout << CHAR(it.key()) << std::endl;
}
}
}
break;
}
case ListGroups: {
QStringList groups = grp.parent().exists() ? grp.parent().groupList() : grp.config()->groupList();
foreach (const QString &s, groups)
if (key.isEmpty() || s.contains(key, Qt::CaseInsensitive))
std::cout << CHAR(s) << std::endl;
exit(0);
}
case Replace: {
if (grp.isImmutable()) {
std::cout << "The component/group " << CHAR(path(grp)) << " cannot be modified" << std::endl;
exit(1);
}
QStringList match = key.split("=");
if (match.count() != 2) {
std::cout << "The match sequence must be of the form <key regexp>=<value regexp>" << std::endl;
exit(1);
}
QRegExp keyMatch(match.at(0), Qt::CaseInsensitive);
QRegExp valueMatch(match.at(1), Qt::CaseInsensitive);
QStringList replace = value.split("=");
if (replace.count() != 2) {
std::cout << "The replace sequence must be of the form <key string>=<value string>" << std::endl;
exit(1);
}
QMap<QString, QString> map = grp.entryMap();
QStringList keys;
for (QMap<QString, QString>::const_iterator it = map.constBegin(), end = map.constEnd(); it != end; ++it) {
if (keyMatch.exactMatch(it.key()) && valueMatch.exactMatch(it.value())) {
keys << it.key();
}
}
foreach (const QString &key, keys) {
QString newKey = key;
newKey.replace(keyMatch, replace.at(0));
QString newValue = grp.readEntry(key);
const QString oldValue = newValue;
newValue.replace(valueMatch, replace.at(1));
if (key != newKey)
grp.deleteEntry(key);
grp.writeEntry(newKey, newValue);
std::cout << CHAR(key) << ": " << CHAR(oldValue) << " -> " << CHAR(newKey) << ": " << CHAR(grp.readEntry(newKey)) << std::endl;
grp.sync();
}
break;
}
Invalid:
default:
break;
}
}
#if 0
void process(Mode mode, QSettings &set, QString key, QString value)
{
switch (mode) {
case Read:
if (IS_A_TTY(1))
std::cout << CHAR(key) << ": " << CHAR(set.value(key, "does not exist").toString()) << " (" << CHAR(path(grp)) << ")" << std::endl;
else
std::cout << CHAR(set.value(key, "").toString());
break;
case Write: {
bool added = !set.hasKey(key);
QString oldv;
if (!added) oldv = set.value(key).toString();
set.setValue(key, QString(value));
grp.sync();
if (added)
std::cout << "New " << CHAR(key) << ": " << CHAR(grp.value(key).toString()) << std::endl;
else
std::cout << CHAR(key) << ": " << CHAR(oldv) << " -> " << CHAR(grp.value(key).toString()) << std::endl;
break;
}
case Delete: {
if (grp.hasKey(key)) {
std::cout << "Removed " << CHAR(key) << ": " << CHAR(grp.value(key).toString()) << std::endl;
grp.deleteEntry(key);
grp.sync();
} else if (grp.hasGroup(key)) {
std::cout << "There's a group, but no key: " << CHAR(key) << "\nPlease explicitly use deletegroup" << std::endl;
exit(1);
} else {
std::cout << "There's no key " << CHAR(key) << " in " << CHAR(path(grp)) << std::endl;
exit(1);
}
break;
}
case DeleteGroup: {
if (grp.hasGroup(key)) {
grp = grp.group(key);
if (grp.isImmutable()) {
std::cout << "The component/group " << CHAR(path(grp)) << "/" << CHAR(key) << " cannot be modified" << std::endl;
exit(1);
}
QMap<QString, QString> map = grp.entryMap();
std::cout << "Removed " << CHAR(key) << gs_separator << std::endl;
for (QMap<QString, QString>::const_iterator it = map.constBegin(), end = map.constEnd(); it != end; ++it) {
std::cout << CHAR(it.key()) << ": " << CHAR(it.value()) << std::endl;
}
grp.deleteGroup();
grp.sync();
} else {
std::cout << "There's no group " << CHAR(key) << " in " << CHAR(path(grp)) << std::endl;
exit(1);
}
break;
}
case List: {
if (!grp.exists()) { // could be parent group
QStringList groups = grp.parent().exists() ? grp.parent().groupList() : grp.config()->groupList();
if (groups.isEmpty()) {
std::cout << "The component/group " << CHAR(path(grp)) << " does not exist" << std::endl;
exit(1);
}
std::cout << "Groups in " << CHAR(path(grp)) << gs_separator << std::endl;
foreach (const QString &s, groups)
if (key.isEmpty() || s.contains(key, Qt::CaseInsensitive))
std::cout << CHAR(s) << std::endl;
exit(0);
}
QMap<QString, QString> map = grp.entryMap();
if (map.isEmpty()) {
std::cout << "The group is empty" << std::endl;
exit(1);
}
bool matchFound = false;
for (QMap<QString, QString>::const_iterator it = map.constBegin(), end = map.constEnd(); it != end; ++it) {
if (key.isEmpty() || it.key().contains(key, Qt::CaseInsensitive)) {
matchFound = true;
std::cout << CHAR(it.key()) << ": " << CHAR(it.value()) << std::endl;
}
}
if (!matchFound)
std::cout << "No present key matches \"" << CHAR(key) << "\" in " << CHAR(path(grp)) << std::endl;
break;
}
case Replace: {
QStringList match = key.split("=");
if (match.count() != 2) {
std::cout << "The match sequence must be of the form <key regexp>=<value regexp>" << std::endl;
exit(1);
}
QRegExp keyMatch(match.at(0), Qt::CaseInsensitive);
QRegExp valueMatch(match.at(1), Qt::CaseInsensitive);
QStringList replace = value.split("=");
if (replace.count() != 2) {
std::cout << "The replace sequence must be of the form <key string>=<value string>" << std::endl;
exit(1);
}
QMap<QString, QString> map = grp.entryMap();
QStringList keys;
for (QMap<QString, QString>::const_iterator it = map.constBegin(), end = map.constEnd(); it != end; ++it) {
if (keyMatch.exactMatch(it.key()) && valueMatch.exactMatch(it.value())) {
keys << it.key();
}
}
foreach (const QString &key, keys) {
QString newKey = key;
newKey.replace(keyMatch, replace.at(0));
QString newValue = grp.value(key);
const QString oldValue = newValue;
newValue.replace(valueMatch, replace.at(1));
if (key != newKey)
grp.deleteEntry(key);
grp.writeEntry(newKey, newValue);
std::cout << CHAR(key) << ": " << CHAR(oldValue) << " -> " << CHAR(newKey) << ": " << CHAR(grp.value(newKey).toString()) << std::endl;
grp.sync();
}
break;
}
Invalid:
default:
break;
}
}
#endif
Mode checkMode(QString modekey, int argc) {
Mode mode(Invalid);
if (modekey == "read" || modekey == "get") {
if (argc < 4) {
std::cout << "You must say what <key> to read" << std::endl;
exit(1);
}
mode = Read;
} else if (modekey == "write" || modekey == "set") {
if (argc < 5) {
std::cout << "You must say what <key> to write to what <value>" << std::endl;
exit(1);
}
mode = Write;
} else if (modekey == "delete") {
if (argc < 4) {
std::cout << "You must say what key to delete" << std::endl;
exit(1);
}
mode = Delete;
} else if (modekey == "deletegroup") {
if (argc < 4) {
std::cout << "You must say what group to delete" << std::endl;
exit(1);
}
mode = DeleteGroup;
} else if (modekey == "list") {
mode = List;
} else if (modekey == "listkeys") {
mode = ListKeys;
} else if (modekey == "listgroups") {
mode = ListGroups;
} else if (modekey == "replace") {
if (argc < 5) {
std::cout << "You must say <what regexp> to replace by <what string>" << std::endl;
exit(1);
}
mode = Replace;
}
return mode;
}
void processGroup(KConfigGroup grp, QStringList component, int next, Mode mode, QString key, QString value) {
if (next == component.count()) {
process(mode, grp, key, value);
return;
}
const QStringList groups = grp.exists() ? grp.groupList() : grp.config()->groupList();
const QRegExp groupMatch(component.at(next), Qt::CaseInsensitive);
int hits = 0;
foreach (const QString &g, groups) {
if (groupMatch.exactMatch(g)) {
++hits;
processGroup(grp.group(g), component, next + 1, mode, key, value);
}
}
if (!hits) {
std::cout << "No existing group matches " << CHAR(component.at(next)) << std::endl;
}
}
// void processGroup(QSettings &set, QStringList component, int next, Mode mode, QString key, QString value) {
// if (next == component.count()) {
// process(mode, set, key, value);
// return;
// }
//
// const QStringList groups = set.childGroups();
// const QRegExp groupMatch(component.at(next), Qt::CaseInsensitive);
// foreach (const QString &g, groups) {
// if (groupMatch.exactMatch(g)) {
// set.beginGroup(g);
// processGroup(set, component, next + 1, mode, key, value);
// set.endGroup();
// }
// }
// }
int main (int argc, char **argv)
{
if (argc < 3) {
usage();
exit(1);
}
Mode mode = checkMode(QString::fromLocal8Bit(argv[2]), argc);
if (mode == Invalid) {
std::cout << "Unknown command: " << argv[2] << std::endl;
exit(1);
}
QString file = QString::fromLocal8Bit(argv[1]);
QStringList component = file.split('/', QString::SkipEmptyParts);
int firstGroupIndex = 1;
if (file.startsWith('/')) {
file = '/' + component.at(firstGroupIndex - 1);
while (firstGroupIndex < component.count() && QFile::exists(file + '/' + component.at(firstGroupIndex)))
file += '/' + component.at(firstGroupIndex++);
} else {
file = component.at(0);
}
QString key, value;
if (argc > 2)
key = QString::fromLocal8Bit(argv[3]);
if (argc > 3)
value = QString::fromLocal8Bit(argv[4]);
KConfig cfg(file);
KConfigGroup grp = cfg.group(QString());
processGroup(grp, component, firstGroupIndex, mode, key, value);
exit(0);
}