Update the documentation to match the new Program Arguments dialog.
[kdbg.git] / kdbg / programconfig.cpp
blob755ac02eefca492a91ee29e198870ea88102428a
1 // $Id$
3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
6 #include "programconfig.h"
7 #include <kconfigbackend.h>
8 #include <qfile.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
13 struct ProgramConfig::MyBackend : KConfigINIBackEnd
15 MyBackend(KConfigBase* cfg, const QString& fn) :
16 KConfigINIBackEnd(cfg, fn, "", false)
17 { }
18 // need the following public
19 using KConfigINIBackEnd::parseSingleConfigFile;
22 ProgramConfig::ProgramConfig(const QString& fileName) :
23 m_fileName(fileName)
25 m_iniBackend = new MyBackend(this, fileName);
26 backEnd = m_iniBackend;
27 reparseConfiguration();
30 QStringList ProgramConfig::groupList() const
32 // unused
33 return QStringList();
36 QMap<QString, QString> ProgramConfig::entryMap(const QString&) const
38 // unused
39 return QMap<QString, QString>();
42 void ProgramConfig::reparseConfiguration()
44 m_entryMap.clear();
46 // add the "default group" marker to the map
47 KEntryKey groupKey("<default>", 0);
48 m_entryMap.insert(groupKey, KEntry());
50 QFile file(m_fileName);
51 bool readonly = true;
52 bool useit = true;
53 if (file.open(IO_ReadWrite)) { /* don't truncate! */
54 readonly = false;
55 // the file exists now
56 } else if (!file.open(IO_ReadOnly)) {
57 /* file does not exist and cannot be created: don't use it */
58 useit = false;
61 if (useit)
63 // Check ownership
64 // Important: This must be done using fstat on the opened file
65 // to avoid race conditions.
66 struct stat s;
67 memset(&s, 0, sizeof(s));
68 useit =
69 fstat(file.handle(), &s) == 0 &&
70 s.st_uid == getuid();
73 if (useit)
75 m_iniBackend->parseSingleConfigFile(file, 0, false, false);
77 else
80 * The program specific config file is not ours, so we do not trust
81 * it for the following reason: Should the debuggee be located in a
82 * world-writable directory somebody else may have created it where
83 * the entry DebuggerCmdStr contains a malicious command, or may
84 * have created a (possibly huge) file containing nonsense, which
85 * leads to a DoS.
89 // don't write the file if we don't own it
90 setReadOnly(readonly || !useit);
93 KEntryMap ProgramConfig::internalEntryMap(const QString& group) const
95 QCString group_utf = group.utf8();
96 KEntryMap tmpEntryMap;
98 // copy the whole group starting at the special group marker
99 KEntryKey key(group_utf, 0);
100 KEntryMapConstIterator it = m_entryMap.find(key);
101 for (; it != m_entryMap.end() && it.key().mGroup == group_utf; ++it)
103 tmpEntryMap.insert(it.key(), *it);
106 return tmpEntryMap;
109 KEntryMap ProgramConfig::internalEntryMap() const
111 return m_entryMap;
114 void ProgramConfig::putData(const KEntryKey& key, const KEntry& data, bool checkGroup)
116 if (checkGroup)
118 // make sure the special group marker is present
119 m_entryMap[KEntryKey(key.mGroup, 0)];
121 m_entryMap[key] = data;
124 KEntry ProgramConfig::lookupData(const KEntryKey& key) const
126 KEntryMapConstIterator it;
128 it = m_entryMap.find(key);
129 if (it != m_entryMap.end())
131 const KEntry& entry = *it;
132 if (entry.bDeleted)
133 return KEntry();
134 else
135 return entry;
137 else {
138 return KEntry();
142 bool ProgramConfig::internalHasGroup(const QCString&) const
144 // unused
145 return false;