2 * Copyright 2015, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
7 #include "SettingsParser.h"
11 #include <DriverSettingsMessageAdapter.h>
14 class AbstractArgsConverter
: public DriverSettingsConverter
{
16 status_t
ConvertEmptyFromDriverSettings(
17 const driver_parameter
& parameter
, const char* name
, uint32 type
,
20 if (parameter
.parameter_count
!= 0 || strcmp(name
, Name()) == 0)
24 return target
.AddMessage(name
, &message
);
28 status_t
AddSubMessage(const driver_parameter
& parameter
, int32 index
,
31 const char* condition
= parameter
.values
[index
];
33 for (index
++; index
< parameter
.value_count
; index
++) {
34 status_t status
= args
.AddString("args",
35 parameter
.values
[index
]);
39 return target
.AddMessage(condition
, &args
);
42 virtual const char* Name() = 0;
46 class ConditionConverter
: public AbstractArgsConverter
{
48 status_t
ConvertFromDriverSettings(const driver_parameter
& parameter
,
49 const char* name
, int32 index
, uint32 type
, BMessage
& target
)
52 if (strcmp(parameter
.name
, "if") == 0) {
53 // Parse values directly following "if", with special
54 // handling for the "not" operator.
58 BMessage
* add
= &target
;
59 bool notOperator
= parameter
.value_count
> 1
60 && strcmp(parameter
.values
[0], "not") == 0;
66 status_t status
= AddSubMessage(parameter
, index
, *add
);
67 if (status
== B_OK
&& notOperator
)
68 status
= target
.AddMessage("not", &message
);
72 if (strcmp(parameter
.name
, "not") == 0) {
76 return AddSubMessage(parameter
, index
, target
);
79 message
.AddString("args", parameter
.values
[index
]);
80 return target
.AddMessage(parameter
.name
, &message
);
90 class EventConverter
: public AbstractArgsConverter
{
92 status_t
ConvertFromDriverSettings(const driver_parameter
& parameter
,
93 const char* name
, int32 index
, uint32 type
, BMessage
& target
)
96 if (strcmp(parameter
.name
, "on") == 0) {
97 // Parse values directly following "on"
101 return AddSubMessage(parameter
, index
, target
);
104 message
.AddString("args", parameter
.values
[index
]);
105 return target
.AddMessage(parameter
.name
, &message
);
115 class RunConverter
: public DriverSettingsConverter
{
117 status_t
ConvertFromDriverSettings(const driver_parameter
& parameter
,
118 const char* name
, int32 index
, uint32 type
, BMessage
& target
)
120 if (parameter
.parameter_count
== 0)
121 return target
.AddString("target", parameter
.values
[index
]);
123 return B_NOT_SUPPORTED
;
126 status_t
ConvertEmptyFromDriverSettings(
127 const driver_parameter
& parameter
, const char* name
, uint32 type
,
130 if (parameter
.parameter_count
!= 0)
133 return target
.AddString("target", name
);
138 const static settings_template kConditionTemplate
[] = {
139 {B_STRING_TYPE
, NULL
, NULL
, true, new ConditionConverter()},
140 {B_MESSAGE_TYPE
, "not", kConditionTemplate
},
141 {B_MESSAGE_TYPE
, "and", kConditionTemplate
},
142 {B_MESSAGE_TYPE
, "or", kConditionTemplate
},
146 const static settings_template kEventTemplate
[] = {
147 {B_STRING_TYPE
, NULL
, NULL
, true, new EventConverter()},
148 {B_MESSAGE_TYPE
, "and", kEventTemplate
},
149 {B_MESSAGE_TYPE
, "or", kEventTemplate
},
153 const static settings_template kPortTemplate
[] = {
154 {B_STRING_TYPE
, "name", NULL
, true},
155 {B_INT32_TYPE
, "capacity", NULL
},
158 const static settings_template kEnvTemplate
[] = {
159 {B_STRING_TYPE
, "from_script", NULL
, true},
160 {B_STRING_TYPE
, NULL
, NULL
},
163 const static settings_template kJobTemplate
[] = {
164 {B_STRING_TYPE
, "name", NULL
, true},
165 {B_BOOL_TYPE
, "disabled", NULL
},
166 {B_STRING_TYPE
, "launch", NULL
},
167 {B_STRING_TYPE
, "requires", NULL
},
168 {B_BOOL_TYPE
, "legacy", NULL
},
169 {B_MESSAGE_TYPE
, "port", kPortTemplate
},
170 {B_MESSAGE_TYPE
, "on", kEventTemplate
},
171 {B_MESSAGE_TYPE
, "if", kConditionTemplate
},
172 {B_BOOL_TYPE
, "no_safemode", NULL
},
173 {B_BOOL_TYPE
, "on_demand", NULL
},
174 {B_MESSAGE_TYPE
, "env", kEnvTemplate
},
178 const static settings_template kTargetTemplate
[] = {
179 {B_STRING_TYPE
, "name", NULL
, true},
180 {B_BOOL_TYPE
, "reset", NULL
},
181 {B_MESSAGE_TYPE
, "on", kEventTemplate
},
182 {B_MESSAGE_TYPE
, "if", kConditionTemplate
},
183 {B_BOOL_TYPE
, "no_safemode", NULL
},
184 {B_MESSAGE_TYPE
, "env", kEnvTemplate
},
185 {B_MESSAGE_TYPE
, "job", kJobTemplate
},
186 {B_MESSAGE_TYPE
, "service", kJobTemplate
},
190 const static settings_template kRunConditionalTemplate
[] = {
191 {B_STRING_TYPE
, NULL
, NULL
, true, new RunConverter()},
195 const static settings_template kRunTemplate
[] = {
196 {B_STRING_TYPE
, NULL
, NULL
, true, new RunConverter()},
197 {B_MESSAGE_TYPE
, "if", kConditionTemplate
},
198 {B_MESSAGE_TYPE
, "then", kRunConditionalTemplate
},
199 {B_MESSAGE_TYPE
, "else", kRunConditionalTemplate
},
203 const static settings_template kSettingsTemplate
[] = {
204 {B_MESSAGE_TYPE
, "target", kTargetTemplate
},
205 {B_MESSAGE_TYPE
, "job", kJobTemplate
},
206 {B_MESSAGE_TYPE
, "service", kJobTemplate
},
207 {B_MESSAGE_TYPE
, "run", kRunTemplate
},
212 SettingsParser::SettingsParser()
218 SettingsParser::ParseFile(const char* path
, BMessage
& settings
)
220 DriverSettingsMessageAdapter adapter
;
221 return adapter
.ConvertFromDriverSettings(path
, kSettingsTemplate
, settings
);
229 SettingsParser::Parse(const char* text
, BMessage
& settings
)
231 void* driverSettings
= parse_driver_settings_string(text
);
232 if (driverSettings
== NULL
)
235 DriverSettingsMessageAdapter adapter
;
236 status_t status
= adapter
.ConvertFromDriverSettings(
237 *get_driver_settings(driverSettings
), kSettingsTemplate
, settings
);
239 delete_driver_settings(driverSettings
);