-
Notifications
You must be signed in to change notification settings - Fork 256
/
parse_conf_state_machine.cc
304 lines (266 loc) · 10.3 KB
/
parse_conf_state_machine.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
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2022 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
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
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "lib/parse_conf_state_machine.h"
#include "lib/parse_conf.h"
#include "lib/resource_item.h"
#include "lib/lex.h"
#include "lib/qualified_resource_name_type_converter.h"
ConfigParserStateMachine::ConfigParserStateMachine(
const char* config_file_name,
void* caller_ctx,
LEX_ERROR_HANDLER* scan_error,
LEX_WARNING_HANDLER* scan_warning,
ConfigurationParser& my_config)
: config_file_name_(config_file_name)
, caller_ctx_(caller_ctx)
, scan_error_(scan_error)
, scan_warning_(scan_warning)
, my_config_(my_config)
{
return;
};
ConfigParserStateMachine::~ConfigParserStateMachine()
{
while (lexical_parser_) { lexical_parser_ = LexCloseFile(lexical_parser_); }
}
bool ConfigParserStateMachine::ParseAllTokens()
{
int token;
while ((token = LexGetToken(lexical_parser_, BCT_ALL)) != BCT_EOF) {
Dmsg3(900, "parse state=%d parser_pass_number_=%d got token=%s\n", state,
parser_pass_number_, lex_tok_to_str(token));
switch (state) {
case ParseState::kInit:
switch (ParserInitResource(token)) {
case ParseInternalReturnCode::kGetNextToken:
case ParseInternalReturnCode::kNextState:
continue;
case ParseInternalReturnCode::kError:
return false;
default:
ASSERT(false);
}
break;
case ParseState::kResource:
switch (ScanResource(token)) {
case ParseInternalReturnCode::kGetNextToken:
continue;
case ParseInternalReturnCode::kError:
return false;
default:
ASSERT(false);
}
break;
default:
scan_err1(lexical_parser_, _("Unknown parser state %d\n"), state);
return false;
}
}
return true;
}
void ConfigParserStateMachine::FreeUnusedMemoryFromPass2()
{
if (parser_pass_number_ == 2) {
// free all resource memory from second pass
if (currently_parsed_resource_.allocated_resource_) {
if (currently_parsed_resource_.allocated_resource_->resource_name_) {
free(currently_parsed_resource_.allocated_resource_->resource_name_);
}
delete currently_parsed_resource_.allocated_resource_;
}
currently_parsed_resource_.rcode_ = 0;
currently_parsed_resource_.resource_items_ = nullptr;
currently_parsed_resource_.allocated_resource_ = nullptr;
}
}
ConfigParserStateMachine::ParseInternalReturnCode
ConfigParserStateMachine::ScanResource(int token)
{
switch (token) {
case BCT_BOB:
config_level_++;
return ParseInternalReturnCode::kGetNextToken;
case BCT_IDENTIFIER: {
if (config_level_ != 1) {
scan_err1(lexical_parser_, _("not in resource definition: %s"),
lexical_parser_->str);
return ParseInternalReturnCode::kError;
}
int resource_item_index = my_config_.GetResourceItemIndex(
currently_parsed_resource_.resource_items_, lexical_parser_->str);
if (resource_item_index >= 0) {
ResourceItem* item = nullptr;
item = ¤tly_parsed_resource_.resource_items_[resource_item_index];
if (!(item->flags & CFG_ITEM_NO_EQUALS)) {
token = LexGetToken(lexical_parser_, BCT_SKIP_EOL);
Dmsg1(900, "in BCT_IDENT got token=%s\n", lex_tok_to_str(token));
if (token != BCT_EQUALS) {
scan_err1(lexical_parser_, _("expected an equals, got: %s"),
lexical_parser_->str);
return ParseInternalReturnCode::kError;
}
}
if (parser_pass_number_ == 1 && item->flags & CFG_ITEM_DEPRECATED) {
my_config_.AddWarning(std::string("using deprecated keyword ")
+ item->name + " on line "
+ std::to_string(lexical_parser_->line_no)
+ " of file " + lexical_parser_->fname);
}
Dmsg1(800, "calling handler for %s\n", item->name);
if (!my_config_.StoreResource(item->type, lexical_parser_, item,
resource_item_index,
parser_pass_number_)) {
if (my_config_.store_res_) {
my_config_.store_res_(lexical_parser_, item, resource_item_index,
parser_pass_number_,
my_config_.config_resources_container_
->configuration_resources_);
}
}
} else {
Dmsg2(900, "config_level_=%d id=%s\n", config_level_,
lexical_parser_->str);
Dmsg1(900, "Keyword = %s\n", lexical_parser_->str);
scan_err1(lexical_parser_,
_("Keyword \"%s\" not permitted in this resource.\n"
"Perhaps you left the trailing brace off of the "
"previous resource."),
lexical_parser_->str);
return ParseInternalReturnCode::kError;
}
return ParseInternalReturnCode::kGetNextToken;
}
case BCT_EOB:
config_level_--;
state = ParseState::kInit;
Dmsg0(900, "BCT_EOB => define new resource\n");
if (!currently_parsed_resource_.allocated_resource_->resource_name_) {
scan_err0(lexical_parser_, _("Name not specified for resource"));
return ParseInternalReturnCode::kError;
}
/* save resource */
if (!my_config_.SaveResourceCb_(
currently_parsed_resource_.rcode_,
currently_parsed_resource_.resource_items_,
parser_pass_number_)) {
scan_err0(lexical_parser_, _("SaveResource failed"));
return ParseInternalReturnCode::kError;
}
FreeUnusedMemoryFromPass2();
return ParseInternalReturnCode::kGetNextToken;
case BCT_EOL:
return ParseInternalReturnCode::kGetNextToken;
default:
scan_err2(lexical_parser_,
_("unexpected token %d %s in resource definition"), token,
lex_tok_to_str(token));
return ParseInternalReturnCode::kError;
}
return ParseInternalReturnCode::kGetNextToken;
}
ConfigParserStateMachine::ParseInternalReturnCode
ConfigParserStateMachine::ParserInitResource(int token)
{
const char* resource_identifier = lexical_parser_->str;
switch (token) {
case BCT_EOL:
case BCT_UTF8_BOM:
return ParseInternalReturnCode::kGetNextToken;
case BCT_UTF16_BOM:
scan_err0(lexical_parser_,
_("Currently we cannot handle UTF-16 source files. "
"Please convert the conf file to UTF-8\n"));
return ParseInternalReturnCode::kError;
default:
if (token != BCT_IDENTIFIER) {
scan_err1(lexical_parser_,
_("Expected a Resource name identifier, got: %s"),
resource_identifier);
return ParseInternalReturnCode::kError;
}
break;
}
ResourceTable* resource_table;
resource_table = my_config_.GetResourceTable(resource_identifier);
bool init_done = false;
if (resource_table && resource_table->items) {
currently_parsed_resource_.rcode_ = resource_table->rcode;
currently_parsed_resource_.resource_items_ = resource_table->items;
my_config_.InitResource(currently_parsed_resource_.rcode_,
currently_parsed_resource_.resource_items_,
parser_pass_number_,
resource_table->ResourceSpecificInitializer);
ASSERT(resource_table->allocated_resource_);
currently_parsed_resource_.allocated_resource_
= *resource_table->allocated_resource_;
ASSERT(currently_parsed_resource_.allocated_resource_);
currently_parsed_resource_.allocated_resource_->rcode_str_
= my_config_.GetQualifiedResourceNameTypeConverter()
->ResourceTypeToString(resource_table->rcode);
state = ParseState::kResource;
init_done = true;
}
if (!init_done) {
scan_err1(lexical_parser_, _("expected resource identifier, got: %s"),
resource_identifier);
return ParseInternalReturnCode::kError;
}
return ParseInternalReturnCode::kNextState;
}
bool ConfigParserStateMachine::InitParserPass()
{
parser_pass_number_++;
ASSERT(parser_pass_number_ < 3);
// close files from the pass before
while (lexical_parser_) { lexical_parser_ = LexCloseFile(lexical_parser_); }
Dmsg1(900, "ParseConfig parser_pass_number_ %d\n", parser_pass_number_);
lexical_parser_ = lex_open_file(lexical_parser_, config_file_name_.c_str(),
scan_error_, scan_warning_);
if (!lexical_parser_) {
my_config_.lex_error(config_file_name_.c_str(), scan_error_, scan_warning_);
return false;
}
LexSetErrorHandlerErrorType(lexical_parser_, my_config_.err_type_);
lexical_parser_->error_counter = 0;
lexical_parser_->caller_ctx = caller_ctx_;
return true;
}
void ConfigParserStateMachine::DumpResourcesAfterSecondPass()
{
if (debug_level >= 900 && parser_pass_number_ == 2) {
for (int i = 0; i <= my_config_.r_num_ - 1; i++) {
my_config_.DumpResourceCb_(
i,
my_config_.config_resources_container_->configuration_resources_[i],
PrintMessage, nullptr, false, false);
}
}
}
ConfigParserStateMachine::ParserError ConfigParserStateMachine::GetParseError()
const
{
// in this order
if (state != ParseState::kInit) {
return ParserError::kResourceIncomplete;
} else if (lexical_parser_->error_counter > 0) {
return ParserError::kParserError;
} else {
return ParserError::kNoError;
}
}