/
ModulesAgent.cc
361 lines (291 loc) · 9.76 KB
/
ModulesAgent.cc
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
/* ModulesAgent.cc
*
* An agent for reading the modules.conf configuration file.
*
* Author: Michal Svec <msvec@suse.cz>
* Daniel Vesely <dan@suse.cz>
*
* $Id$
*
*/
#include "Y2Logger.h"
#include "ModulesAgent.h"
#include "ModulesConf.h"
#define MAGIC_DIRECTIVE "extra"
#define PC(n) (path->component_str(n))
#define VAL2STR(v) ((v)->asString()->value())
#define VAL2CSTR(v) ((v)->asString()->value_cstr())
/*
PATH:
MAP[STRING].MAP[STRING].MAP[STRING].(MAP[STRING].STRING|STRING)
READ:
.modules.options.<module>.<option>
"options".STRING.MAP
.modules.options.<module>.<option>.<parameters>
"options".STRING.STRING.STRING
.modules.<directive>.<module>.<argument>
STRING.STRING.STRING
.modules.<directive>.<module>.comment.<comment>
STRING.STRING."comment".STRING
.modules.<directive>.<module>
STRING.STRING.LIST
.modules.<directive>
STRING.LIST
.modules
LIST
*/
/**
* Constructor
*/
ModulesAgent::ModulesAgent() : SCRAgent(), modules_conf(NULL) {
}
/**
* Destructor
*/
ModulesAgent::~ModulesAgent() {
if (modules_conf != NULL)
delete modules_conf;
}
/**
* Simple template function for converting c++ map into YCP list
*/
template <class T> YCPList map2list(const T &m) {
YCPList list;
typename T::const_iterator it = m.begin ();
for (; it != m.end (); ++it)
/* Preserve listing of the final comment */
if(it->first != "YaST2_final_modules_conf_comment")
list->add(YCPString (it->first));
return list;
}
/**
* Simple template function for converting c++ map into YCP map
*/
template <class T> YCPMap map2ycpmap(const T &m) {
YCPMap ret_map;
typename T::const_iterator it = m.begin ();
for (; it != m.end(); ++it)
ret_map->add (YCPString (it->first), YCPString (it->second));
return ret_map;
}
/**
* Simple function for converting YCP map into c++ map
*/
ModuleEntry::EntryArg ycpmap2map (const YCPMap &m) {
ModuleEntry::EntryArg ret_map;
YCPMap::const_iterator it = m->begin ();
for (; it != m->end(); ++it)
if (it->first->isString () && it->second->isString ())
ret_map[VAL2STR(it->first)] = VAL2STR(it->second);
else {
y2error("Map element must be string!");
return ModuleEntry::EntryArg();
}
return ret_map;
}
/**
* Dir
*/
YCPList ModulesAgent::Dir(const YCPPath& path) {
YCPList list;
string elem;
if (modules_conf == NULL) {
ycp2error("Can't execute Dir before being mounted.");
return YCPNull();
}
switch (path->length ()) {
case 0:
/* .modules -> ["alias","options"] */
return YCPList (map2list (modules_conf->getDirectives ()));
case 1:
if(PC(0) == MAGIC_DIRECTIVE)
Y2_RETURN_VOID("Dir() doesn't support the .%s directive", MAGIC_DIRECTIVE);
/* .modules.options -> ["eth0","eth1"] */
return YCPList (map2list (modules_conf->getModules(PC(0))));
case 2:
/* .modules.options.eth0 -> ["irq","io"] */
if(PC(0) == "options")
return YCPList (map2list (modules_conf->getOptions(PC(1))));
}
Y2_RETURN_VOID("Wrong path '%s' in Dir().", path->toString().c_str());
}
/**
* Read
*/
YCPValue ModulesAgent::Read(const YCPPath &path, const YCPValue& arg, const YCPValue& optarg) {
if (modules_conf == NULL)
Y2_RETURN_VOID("Can't execute Read before being mounted.");
y2debug("Read(%s)", path->toString().c_str());
switch (path->length ()) {
case 0:
/* .modules -> ["alias","options"] */
return YCPList (map2list (modules_conf->getDirectives()));
case 1:
/* FIXME: remove */
if (!arg.isNull() && arg->isString ()) {
y2error("Obsolete interface, don't use any more!");
if (PC(0) == "options")
return YCPMap (map2ycpmap (modules_conf->getOptions(VAL2STR(arg))));
else
return YCPString (modules_conf->getArgument(PC(0), VAL2STR(arg)));
}
/* .modules.options -> ["eth0","eth1"] */
return YCPList (map2list (modules_conf->getModules(PC(0))));
#if 0 /* some attempts to support other directives */
/* .modules.keep -> "" */
/* .modules.prune -> "filename" */
if (PC(0) == "keep" || PC(0) == "prune") {
YCPPath newpath;
newpath->append(PC(0));
newpath->append(string(""));
//newpath->append(string("\"\""));
return Read(newpath, arg);
}
/* .modules.keep -> "" */
/* .modules.prune -> "filename" */
if (PC(0) != "keep" && PC(0) != "prune")
Y2_RETURN_VOID("Unsupported simple directive: %s,"
"please inform maintainer", PC(0).c_str());
/* .modules.keep -> "" [zero args directives -> ignore 2nd arg] */
if(PC(0) == "keep")
return YCPString ("");
/* .modules.prune, "filename" */
return YCPString (modules_conf->getArgument(PC(0), ""));
#endif
Y2_RETURN_VOID("Wrong path '%s' in Read().", path->toString().c_str());
case 2:
/* FIXME: remove */
if (!arg.isNull () && arg->isString ()) {
y2error("Obsolete interface, don't use any more!");
if (PC(1) == "comment")
return YCPString (modules_conf->getComment(PC(0), VAL2STR(arg)));
if (PC(0) == "options")
return YCPString (modules_conf->getOption(VAL2STR(arg), PC(1)));
}
/* .modules.options.eth0 -> $["irq":"7"] */
if(PC(0) == "options")
return YCPMap (map2ycpmap (modules_conf->getOptions(PC(1))));
/* .modules.alias.eth0 -> "off" */
else
return YCPString (modules_conf->getArgument(PC(0), PC(1)));
case 3:
/* .modules.alias.eth0.comment -> "# comment\n" */
if (PC(2) == "comment")
return YCPString (modules_conf->getComment(PC(0), PC(1)));
/* .modules.options.eth0.irq -> "7" */
if (PC(0) == "options")
return YCPString (modules_conf->getOption(PC(1), PC(2)));
}
Y2_RETURN_VOID("Wrong path '%s' in Read().", path->toString().c_str());
}
/**
* Write
*/
YCPBoolean ModulesAgent::Write(const YCPPath &path, const YCPValue& value, const YCPValue& arg) {
if (modules_conf == NULL)
Y2_RETURN_VOID("Can't execute Write before being mounted.");
if (path->isRoot() && value->isVoid ())
return YCPBoolean(modules_conf->writeFile());
switch (path->length ()) {
case 1:
/* FIXME: remove */
if (!arg.isNull () && arg->isString ()) {
y2error("Obsolete interface, don't use any more!");
if (value->isVoid ())
return YCPBoolean (modules_conf->removeEntry (PC(0), VAL2STR(arg)));
if (PC(0) == "options") {
if (value->isMap ())
return YCPBoolean (modules_conf->setOptions(VAL2STR(arg),
ycpmap2map (value->asMap ()),
ModuleEntry::SET));
else
Y2_RETURN_YCP_FALSE("Argument for Write () not map.");
}
return YCPBoolean (modules_conf->setArgument (PC(0), VAL2STR(arg),
VAL2STR(value),
ModuleEntry::SET));
}
#if 0 /* some attempts to support other directives */
/* .modules.keep, nil */
/* .modules.prune, nil */
if (PC(0) != "keep" && PC(0) != "prune")
Y2_RETURN_YCP_FALSE("Unsupported simple directive: %s,"
"please inform maintainer", PC(0).c_str());
if (value->isVoid ())
return YCPBoolean (modules_conf->removeEntry (PC(0), ""));
/* .modules.keep, "" [zero args directives -> ignore 2nd arg] */
if (PC(0) == "keep")
return YCPBoolean (modules_conf->setArgument (PC(0), "",
"", ModuleEntry::SET));
if (!value->isString())
Y2_RETURN_YCP_FALSE("Argument for Write() is not string: %s.",
value->toString().c_str());
/* .modules.prune, "filename" */
return YCPBoolean (modules_conf->setArgument (PC(0), "",
VAL2STR(value), ModuleEntry::SET));
#endif
Y2_RETURN_YCP_FALSE("Argument (2nd) for Write() is not string.");
case 2:
/* FIXME: remove */
if (value->isString () && !arg.isNull () && arg->isString ()) {
y2error("Obsolete interface, don't use any more!");
if (PC(0) == "options")
return YCPBoolean (modules_conf->setOption (VAL2STR(arg), PC(1),
VAL2STR(value),
ModuleEntry::SET));
if (PC(1) == "comment")
return YCPBoolean (modules_conf->setComment (PC(0), VAL2STR(arg),
VAL2STR(value),
ModuleEntry::SET));
}
/* .modules.alias.eth0, nil */
if (value->isVoid ())
return YCPBoolean (modules_conf->removeEntry (PC(0), PC(1)));
/* .modules.options.eth0, $["irq":"7"] */
if (PC(0) == "options") {
if (value->isMap ())
return YCPBoolean (modules_conf->setOptions(PC(1),
ycpmap2map (value->asMap ()), ModuleEntry::SET));
else
Y2_RETURN_YCP_FALSE("Argument for Write(.options) not map: %s.",
value->toString().c_str());
}
if (!value->isString())
Y2_RETURN_YCP_FALSE("Argument for Write() is not string: %s.",
value->toString().c_str());
/* .modules.alias.eth0, "off" */
return YCPBoolean (modules_conf->setArgument (PC(0), PC(1),
VAL2STR(value), ModuleEntry::SET));
case 3:
if (!value->isString())
Y2_RETURN_YCP_FALSE("Argument for Write() is not string: %s.",
value->toString().c_str());
/* .modules.alias.eth0.comment, "# First tulip\n" */
if (PC(2) == "comment") {
return YCPBoolean (modules_conf->setComment (PC(0), PC(1),
VAL2STR(value), ModuleEntry::SET));
}
/* .modules.options.eth0.irq, "7" */
if (PC(0) == "options")
return YCPBoolean (modules_conf->setOption (PC(1), PC(2),
VAL2STR(value), ModuleEntry::SET));
}
Y2_RETURN_VOID("Wrong path '%s' in Write().", path->toString().c_str());
}
/**
* otherCommand
*/
YCPValue ModulesAgent::otherCommand(const YCPTerm& term) {
string sym = term->name();
if (sym == "ModulesConf" && term->size() == 1) {
if (term->value(0)->isString()) {
YCPString s = term->value(0)->asString();
if (modules_conf != NULL)
delete modules_conf;
modules_conf = new ModulesConf(s->value());
return YCPVoid();
} else
Y2_RETURN_VOID("Bad first arg of ModulesConf(): is not a string.");
}
return YCPNull();
}