1/* Read KConfig() entries - for use in shell scripts.
2
3 SPDX-FileCopyrightText: 2001 Red Hat, Inc.
4 SPDX-FileContributor: Programmed by Bernhard Rosenkraenzer <bero@redhat.com>
5
6 SPDX-License-Identifier: GPL-2.0-or-later
7*/
8
9/*
10 * If --type is specified as bool, the return value is 0 if the value
11 * is set, 1 if it isn't set. There is no output.
12 *
13 * If --type is specified as num, the return value matches the value
14 * of the key. There is no output.
15 *
16 * If --type is not set, the value of the key is simply printed to stdout.
17 *
18 * Usage examples:
19 * if kreadconfig6 --group KDE --key macStyle --type bool; then
20 * echo "We're using Mac-Style menus."
21 * else
22 * echo "We're using normal menus."
23 * fi
24 *
25 * TRASH=`kreadconfig6 --group Paths --key Trash`
26 * if test -n "$TRASH"; then
27 * mv someFile "$TRASH"
28 * else
29 * rm someFile
30 * fi
31 */
32
33#include <KConfig>
34#include <KConfigGroup>
35#include <KSharedConfig>
36#include <QCommandLineParser>
37#include <stdio.h>
38
39int main(int argc, char **argv)
40{
41 QCoreApplication app(argc, argv);
42
43 QCommandLineParser parser;
44 parser.addHelpOption();
45 parser.addOption(
46 commandLineOption: QCommandLineOption(QStringLiteral("file"), QCoreApplication::translate(context: "main", key: "Use <file> instead of global config"), QStringLiteral("file")));
47 parser.addOption(
48 commandLineOption: QCommandLineOption(QStringLiteral("group"),
49 QCoreApplication::translate(context: "main", key: "Group to look in. Use \"<default>\" for the root group, or use repeatedly for nested groups."),
50 QStringLiteral("group"),
51 QStringLiteral("KDE")));
52 parser.addOption(commandLineOption: QCommandLineOption(QStringLiteral("key"), QCoreApplication::translate(context: "main", key: "Key to look for"), QStringLiteral("key")));
53 parser.addOption(commandLineOption: QCommandLineOption(QStringLiteral("default"), QCoreApplication::translate(context: "main", key: "Default value"), QStringLiteral("value")));
54 parser.addOption(commandLineOption: QCommandLineOption(QStringLiteral("type"), QCoreApplication::translate(context: "main", key: "Type of variable"), QStringLiteral("type")));
55
56 parser.process(app);
57
58 const QStringList groups = parser.values(QStringLiteral("group"));
59 QString key = parser.value(QStringLiteral("key"));
60 QString file = parser.value(QStringLiteral("file"));
61 QString dflt = parser.value(QStringLiteral("default"));
62 QString type = parser.value(QStringLiteral("type")).toLower();
63
64 if (key.isNull() || !parser.positionalArguments().isEmpty()) {
65 parser.showHelp(exitCode: 1);
66 }
67
68 KSharedConfig::openConfig();
69
70 KConfig *konfig;
71 bool configMustDeleted = false;
72 if (file.isEmpty()) {
73 konfig = KSharedConfig::openConfig().data();
74 } else {
75 konfig = new KConfig(file, KConfig::NoGlobals);
76 configMustDeleted = true;
77 }
78 KConfigGroup cfgGroup = konfig->group(group: QString());
79 for (const QString &grp : groups) {
80 if (grp.isEmpty()) {
81 fprintf(stderr,
82 format: "%s: %s\n",
83 qPrintable(QCoreApplication::applicationName()),
84 qPrintable(QCoreApplication::translate("main", "Group name cannot be empty, use \"<default>\" for the root group")));
85 if (configMustDeleted) {
86 delete konfig;
87 }
88 return 2;
89 }
90 cfgGroup = cfgGroup.group(group: grp);
91 }
92
93 if (type == QLatin1String{"bool"}) {
94 dflt = dflt.toLower();
95 bool def = (dflt == QLatin1String{"true"} || dflt == QLatin1String{"on"} || dflt == QLatin1String{"yes"} || dflt == QLatin1String{"1"});
96 bool retValue = !cfgGroup.readEntry(key, aDefault: def);
97 if (configMustDeleted) {
98 delete konfig;
99 }
100 return retValue;
101 } else if (type == QLatin1String{"num"} || type == QLatin1String{"int"}) {
102 int retValue = cfgGroup.readEntry(key, aDefault: dflt.toInt());
103 if (configMustDeleted) {
104 delete konfig;
105 }
106 return retValue;
107 } else if (type == QLatin1String{"path"}) {
108 fprintf(stdout, format: "%s\n", cfgGroup.readPathEntry(pKey: key, aDefault: dflt).toLocal8Bit().data());
109 if (configMustDeleted) {
110 delete konfig;
111 }
112 return 0;
113 } else {
114 /* Assume it's a string... */
115 fprintf(stdout, format: "%s\n", cfgGroup.readEntry(key, aDefault: dflt).toLocal8Bit().data());
116 if (configMustDeleted) {
117 delete konfig;
118 }
119 return 0;
120 }
121}
122

source code of kconfig/src/kreadconfig/kreadconfig.cpp