3 //============================================================================
7 // Implementation of the CdlOption class
9 //============================================================================
10 //####COPYRIGHTBEGIN####
12 // ----------------------------------------------------------------------------
13 // Copyright (C) 2002 Bart Veer
14 // Copyright (C) 1999, 2000 Red Hat, Inc.
16 // This file is part of the eCos host tools.
18 // This program is free software; you can redistribute it and/or modify it
19 // under the terms of the GNU General Public License as published by the Free
20 // Software Foundation; either version 2 of the License, or (at your option)
23 // This program is distributed in the hope that it will be useful, but WITHOUT
24 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
25 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
28 // You should have received a copy of the GNU General Public License along with
29 // this program; if not, write to the Free Software Foundation, Inc.,
30 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 // ----------------------------------------------------------------------------
34 //####COPYRIGHTEND####
35 //============================================================================
36 //#####DESCRIPTIONBEGIN####
43 //####DESCRIPTIONEND####
44 //============================================================================
49 // ----------------------------------------------------------------------------
50 #include "cdlconfig.h"
52 // Get the infrastructure types, assertions, tracing and similar
54 #include <cyg/infra/cyg_ass.h>
55 #include <cyg/infra/cyg_trac.h>
57 // <cdl.hxx> defines everything implemented in this module.
58 // It implicitly supplies <string>, <vector> and <map> because
59 // the class definitions rely on these headers.
66 // ----------------------------------------------------------------------------
67 CYGDBG_DEFINE_MEMLEAK_COUNTER(CdlOptionBody);
72 // ----------------------------------------------------------------------------
73 CdlOptionBody::CdlOptionBody(std::string name_arg)
74 : CdlNodeBody(name_arg),
81 CYG_REPORT_FUNCNAME("CdlOptionBody:: constructor");
82 CYG_REPORT_FUNCARG1XV(this);
84 cdloptionbody_cookie = CdlOptionBody_Magic;
85 CYGDBG_MEMLEAK_CONSTRUCTOR();
87 CYG_POSTCONDITION_THISC();
94 // ----------------------------------------------------------------------------
96 CdlOptionBody::~CdlOptionBody()
98 CYG_REPORT_FUNCNAME("CdlOptionBody:: destructor");
99 CYG_REPORT_FUNCARG1XV(this);
100 CYG_PRECONDITION_THISC();
102 cdloptionbody_cookie = CdlOptionBody_Invalid;
103 CYGDBG_MEMLEAK_DESTRUCTOR();
111 // ----------------------------------------------------------------------------
112 // Parsing an option definition. This is basically the same as
113 // CdlComponent::parse_component(), but simpler: an option is not
114 // a container, and the only non-standard property is "parent".
117 CdlOptionBody::parse_option(CdlInterpreter interp, int argc, const char* argv[])
119 CYG_REPORT_FUNCNAMETYPE("CdlOptionBody::parse_option", "result %d");
120 CYG_REPORT_FUNCARG1("argc %d", argc);
121 CYG_PRECONDITION_CLASSC(interp);
123 std::string diag_argv0 = CdlParse::get_tcl_cmd_name(argv[0]);
125 CdlLoadable loadable = interp->get_loadable();
126 CdlPackage package = dynamic_cast<CdlPackage>(loadable);
127 CdlContainer parent = interp->get_container();
128 CdlToplevel toplevel = interp->get_toplevel();
129 CYG_ASSERT_CLASSC(loadable); // There should always be a loadable during parsing
130 CYG_ASSERT_CLASSC(package); // And packages are the only loadable for software CDL
131 CYG_ASSERT_CLASSC(parent);
132 CYG_ASSERT_CLASSC(toplevel);
134 // The new option should be created and added to the package
135 // early on. If there is a parsing error it will get cleaned up
136 // automatically as a consequence of the package destructor.
137 // However it is necessary to validate the name first. Errors
138 // should be reported via CdlParse::report_error(),
139 // which may result in an exception.
140 CdlOption new_option = 0;
145 // Currently there are no command-line options. This may change in future.
147 CdlParse::report_error(interp, "",
148 std::string("Incorrect number of arguments to `") + diag_argv0 +
149 "'\nExpecting name and properties list.");
151 } else if (!Tcl_CommandComplete(CDL_TCL_CONST_CAST(char*, argv[2]))) {
152 CdlParse::report_error(interp, "",
153 std::string("Invalid property list for cdl_option `") + argv[1] + "'.");
155 } else if (0 != toplevel->lookup(argv[1])) {
156 CdlParse::report_error(interp, "",
157 std::string("Option `") + argv[1] + "' cannot be loaded.\nThe name is already in use.");
160 new_option = new CdlOptionBody(argv[1]);
161 toplevel->add_node(package, parent, new_option);
165 // Just because this component cannot be created, that is no
166 // reason to abort the whole parsing process.
167 CYG_REPORT_RETVAL(TCL_OK);
170 } catch(std::bad_alloc e) {
171 interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Out of memory"));
173 } catch(CdlParseException e) {
174 interp->set_result(e.get_message());
177 interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Unexpected C++ exception"));
180 if (TCL_OK != result) {
181 CYG_REPORT_RETVAL(result);
185 // At this stage new_option has been created and added to the hierarchy.
186 // The main work now is to add the properties.
188 // Push the option as the current node early on. This aids
190 CdlNode old_node = interp->push_node(new_option);
192 // Declare these outside the scope of the try statement, to allow
193 // goto calls for the error handling.
194 std::string tcl_result;
195 std::vector<CdlInterpreterCommandEntry> new_commands;
196 std::vector<CdlInterpreterCommandEntry>* old_commands = 0;
197 static CdlInterpreterCommandEntry commands[] =
199 CdlInterpreterCommandEntry("", 0 )
203 // All parsing errors may result in an exception, under the control of
204 // application code. This exception must not pass through the Tcl interpreter.
207 for (i = 0; 0 != commands[i].command; i++) {
208 new_commands.push_back(commands[i]);
210 CdlDefinableBody::add_property_parsers(new_commands);
211 CdlBuildableBody::add_property_parsers(new_commands);
212 CdlParentableBody::add_property_parsers(new_commands);
213 CdlValuableBody::add_property_parsers(new_commands);
214 CdlUserVisibleBody::add_property_parsers(new_commands);
215 CdlNodeBody::add_property_parsers(new_commands);
217 // Now evaluate the body. If an error occurs then typically
218 // this will be reported via CdlParse::report_error(),
219 // but any exceptions will have been intercepted and
220 // turned into a Tcl error.
221 old_commands = interp->push_commands(new_commands);
222 result = interp->eval(argv[2], tcl_result);
223 interp->pop_commands(old_commands);
225 if (TCL_OK == result) {
226 // Even if there were errors, they were not fatal. There may
227 // now be a number of properties for this option, and some
228 // validation should take place. Start with the base classes.
229 new_option->CdlNodeBody::check_properties(interp);
230 new_option->CdlUserVisibleBody::check_properties(interp);
231 new_option->CdlValuableBody::check_properties(interp);
232 new_option->CdlParentableBody::check_properties(interp);
233 new_option->CdlBuildableBody::check_properties(interp);
234 new_option->CdlDefinableBody::check_properties(interp);
237 } catch (std::bad_alloc e) {
238 // Errors at this stage should be reported via Tcl, not via C++.
239 // However there is no point in continuing with the parsing operation,
241 interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Out of memory"));
243 } catch (CdlParseException e) {
244 interp->set_result(e.get_message());
247 interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Unexpected C++ exception"));
251 // Restore the interpreter to its prior state.
252 interp->pop_node(old_node);
253 if (0 != old_commands) {
254 interp->pop_commands(old_commands);
257 CYG_REPORT_RETVAL(result);
262 //{{{ Persistence support
264 // ----------------------------------------------------------------------------
266 CdlOptionBody::initialize_savefile_support(CdlToplevel toplevel)
268 CYG_REPORT_FUNCNAME("CdlOption::initialize_savefile_support");
270 toplevel->add_savefile_command("cdl_option", 0, &savefile_option_command);
271 CdlValuableBody::initialize_savefile_support(toplevel, "cdl_option");
277 CdlOptionBody::save(CdlInterpreter interp, Tcl_Channel chan, int indentation, bool minimal)
279 CYG_REPORT_FUNCNAME("CdlOption::save");
280 CYG_REPORT_FUNCARG5XV(this, interp, chan, indentation, minimal);
281 CYG_PRECONDITION_THISC();
282 CYG_PRECONDITION_CLASSC(interp);
284 if (!minimal || this->has_additional_savefile_information() || this->value_savefile_entry_needed()) {
285 // Start with the UserVisible data, which will result in a suitable set
286 // of comments before the package definition itself.
287 this->CdlUserVisibleBody::save(interp, chan, indentation, minimal);
289 // Now output the line "cdl_option <name> {"
290 // The name is guaranteed to be a valid C preprocessor symbol, so it
291 // is not going to need any quoting.
292 std::string data = std::string(indentation, ' ') + "cdl_option " + get_name() + " {\n";
293 interp->write_data(chan, data);
295 // Deal with the value
296 bool modifiable = !(CdlValueFlavor_None == this->get_flavor()) &&
297 !this->has_property(CdlPropertyId_Calculated);
298 this->CdlValuableBody::save(interp, chan, indentation + 4, modifiable, minimal);
300 // And with any unrecognised data
301 this->CdlNodeBody::save(interp, chan, indentation + 4, minimal);
303 // Close the cdl_option body. A blank line is added here.
304 interp->write_data(chan, "};\n\n");
311 CdlOptionBody::savefile_option_command(CdlInterpreter interp, int argc, const char* argv[])
313 CYG_REPORT_FUNCNAMETYPE("CdlOption::savefile_option_command", "result %d");
314 CYG_PRECONDITION_CLASSC(interp);
317 CdlToplevel toplevel = interp->get_toplevel();
318 CYG_ASSERT_CLASSC(toplevel);
319 CdlConfiguration config = dynamic_cast<CdlConfiguration>(toplevel);
320 CYG_ASSERT_CLASSC(config);
322 std::vector<CdlInterpreterCommandEntry> subcommands;
323 std::vector<CdlInterpreterCommandEntry>* toplevel_commands = 0;
324 CdlNode old_node = 0;
329 CdlParse::report_error(interp, "", "Invalid cdl_option command in savefile, expecting two arguments.");
332 CdlNode current_node = config->lookup(argv[1]);
333 if (0 == current_node) {
334 // FIXME: save value in limbo
335 CdlParse::report_error(interp, "",
336 std::string("The savefile contains a cdl_option for an unknown option `")
339 config->get_savefile_subcommands("cdl_option", subcommands);
340 toplevel_commands = interp->push_commands(subcommands);
341 old_node = interp->push_node(current_node);
343 std::string tcl_result;
344 result = interp->eval(argv[2], tcl_result);
346 interp->pop_commands(toplevel_commands);
347 toplevel_commands = 0;
348 interp->pop_node(old_node);
354 interp->pop_node(old_node);
356 if (0 != toplevel_commands) {
357 interp->pop_commands(toplevel_commands);
362 CYG_REPORT_RETVAL(result);
369 // ----------------------------------------------------------------------------
371 CdlOptionBody::check_this(cyg_assert_class_zeal zeal) const
373 if (CdlOptionBody_Magic != cdloptionbody_cookie) {
376 CYGDBG_MEMLEAK_CHECKTHIS();
378 return CdlNodeBody::check_this(zeal) &&
379 CdlUserVisibleBody::check_this(zeal) &&
380 CdlValuableBody::check_this(zeal) &&
381 CdlParentableBody::check_this(zeal) &&
382 CdlBuildableBody::check_this(zeal) &&
383 CdlDefinableBody::check_this(zeal);
389 // ----------------------------------------------------------------------------
392 CdlOptionBody::get_class_name() const
394 CYG_REPORT_FUNCNAME("CdlOption::get_class_name");
395 CYG_PRECONDITION_THISC();