3 //============================================================================
7 // Implementation of the CdlComponent 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(CdlComponentBody);
72 // ----------------------------------------------------------------------------
73 CdlComponentBody::CdlComponentBody(std::string name_arg)
74 : CdlNodeBody(name_arg),
82 CYG_REPORT_FUNCNAME("CdlComponentBody:: constructor");
83 CYG_REPORT_FUNCARG1XV(this);
85 cdlcomponentbody_cookie = CdlComponentBody_Magic;
86 CYGDBG_MEMLEAK_CONSTRUCTOR();
88 CYG_POSTCONDITION_THISC();
95 // ----------------------------------------------------------------------------
97 CdlComponentBody::~CdlComponentBody()
99 CYG_REPORT_FUNCNAME("CdlComponentBody:: destructor");
100 CYG_REPORT_FUNCARG1XV(this);
101 CYG_PRECONDITION_THISC();
103 cdlcomponentbody_cookie = CdlComponentBody_Invalid;
104 CYGDBG_MEMLEAK_DESTRUCTOR();
110 //{{{ parse_component()
112 // ----------------------------------------------------------------------------
113 // Parsing a component definition. This routine gets invoked directly from the
117 CdlComponentBody::parse_component(CdlInterpreter interp, int argc, const char* argv[])
119 CYG_REPORT_FUNCNAMETYPE("CdlComponentBody::parse_component", "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 component 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 CdlComponent new_component = 0;
145 // Currently there are no 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.");
153 if (!Tcl_CommandComplete(CDL_TCL_CONST_CAST(char*, argv[2]))) {
154 CdlParse::report_error(interp, "",
155 std::string("Invalid property list for cdl_component `") + argv[1] + "'.");
160 if (0 != toplevel->lookup(argv[1])) {
161 CdlParse::report_error(interp, "",
162 std::string("Component `") + argv[1] +
163 "' cannot be loaded.\nThe name is already in use.");
166 new_component = new CdlComponentBody(argv[1]);
167 toplevel->add_node(package, parent, new_component);
172 // Just because this component cannot be created, that is no
173 // reason to abort the whole parsing process.
174 CYG_REPORT_RETVAL(TCL_OK);
177 } catch(std::bad_alloc e) {
178 interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Out of memory"));
180 } catch(CdlParseException e) {
181 interp->set_result(e.get_message());
184 interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Unexpected C++ exception"));
187 if (TCL_OK != result) {
188 CYG_REPORT_RETVAL(result);
192 // At this stage new_component has been created and added to the hierarchy.
193 // The main work now is to add the properties.
195 // Push the component as the current node early on. This aids
196 // diagnostics. Also make it the new container.
197 CdlNode old_node = interp->push_node(new_component);
198 CdlContainer old_container = interp->push_container(new_component);
199 std::string old_context;
200 CYG_ASSERTC(parent == old_container);
202 // Declare these outside the scope of the try statement, to allow
203 // goto calls for the error handling.
204 std::string tcl_result;
205 std::vector<CdlInterpreterCommandEntry> new_commands;
206 std::vector<CdlInterpreterCommandEntry>* old_commands = 0;
207 static CdlInterpreterCommandEntry commands[] =
209 CdlInterpreterCommandEntry("script", &CdlComponentBody::parse_script ),
210 CdlInterpreterCommandEntry("cdl_component", &CdlComponentBody::parse_component ),
211 CdlInterpreterCommandEntry("cdl_option", &CdlOptionBody::parse_option ),
212 CdlInterpreterCommandEntry("cdl_interface", &CdlInterfaceBody::parse_interface ),
213 CdlInterpreterCommandEntry("cdl_dialog", &CdlDialogBody::parse_dialog ),
214 CdlInterpreterCommandEntry("cdl_wizard", &CdlWizardBody::parse_wizard ),
215 CdlInterpreterCommandEntry("", 0 )
217 static CdlInterpreterCommandEntry script_commands[] =
219 CdlInterpreterCommandEntry("cdl_component", &CdlComponentBody::parse_component ),
220 CdlInterpreterCommandEntry("cdl_option", &CdlOptionBody::parse_option ),
221 CdlInterpreterCommandEntry("cdl_interface", &CdlInterfaceBody::parse_interface ),
222 CdlInterpreterCommandEntry("cdl_dialog", &CdlDialogBody::parse_dialog ),
223 CdlInterpreterCommandEntry("cdl_wizard", &CdlWizardBody::parse_wizard ),
224 CdlInterpreterCommandEntry("", 0 ),
228 // All parsing errors may result in an exception, under the control of
229 // application code. This exception must not pass through the Tcl interpreter.
232 for (i = 0; 0 != commands[i].command; i++) {
233 new_commands.push_back(commands[i]);
235 CdlBuildableBody::add_property_parsers(new_commands);
236 CdlDefinableBody::add_property_parsers(new_commands);
237 CdlParentableBody::add_property_parsers(new_commands);
238 CdlValuableBody::add_property_parsers(new_commands);
239 CdlUserVisibleBody::add_property_parsers(new_commands);
240 CdlNodeBody::add_property_parsers(new_commands);
242 // Now evaluate the body. If an error occurs then typically
243 // this will be reported via CdlParse::report_error(),
244 // but any exceptions will have been intercepted and
245 // turned into a Tcl error.
246 old_commands = interp->push_commands(new_commands);
247 result = interp->eval(argv[2], tcl_result);
248 interp->pop_commands(old_commands);
250 if (TCL_OK != result) {
251 // No point in taking any further action, just go with the flow
255 // Even if there were errors, they were not fatal. There may
256 // now be a number of properties for this component, and some
257 // validation should take place. Start with the base classes.
258 new_component->CdlNodeBody::check_properties(interp);
259 new_component->CdlUserVisibleBody::check_properties(interp);
260 new_component->CdlValuableBody::check_properties(interp);
261 new_component->CdlParentableBody::check_properties(interp);
262 new_component->CdlBuildableBody::check_properties(interp);
263 new_component->CdlDefinableBody::check_properties(interp);
265 // There should be at most one each of wizard and script.
266 if (new_component->count_properties(CdlPropertyId_Wizard) > 1) {
267 CdlParse::report_error(interp, "", "A component should have at most one `wizard' property.");
269 if (new_component->count_properties(CdlPropertyId_Script) > 1) {
270 CdlParse::report_error(interp, "", "A component should have at most one `script' property.");
273 // If there is a script property, life gets more interesting.
274 if (new_component->has_property(CdlPropertyId_Script)) {
275 CdlProperty_String prop = dynamic_cast<CdlProperty_String>(new_component->get_property(CdlPropertyId_Script));
276 CYG_PRECONDITION_CLASSC(prop);
277 std::string script_name = prop->get_string();
279 // Try to locate this script.
280 std::string script_filename = package->find_absolute_file(script_name, "cdl", false);
281 if ("" == script_filename) {
282 CdlParse::report_error(interp, "", "Unable to find script `" + script_name + "'.");
284 // The script exists, so we need to try and execute it.
285 // The current container is still set correctly, but we need
286 // to change the filename and install a different set
288 old_context = interp->push_context(script_filename);
289 new_commands.clear();
290 for (i = 0; 0 != script_commands[i].command; i++) {
291 new_commands.push_back(script_commands[i]);
293 old_commands = interp->push_commands(new_commands);
294 result = interp->eval_file(script_filename, tcl_result);
295 interp->pop_commands(old_commands);
296 interp->pop_context(old_context);
301 // Dummy command just to keep the compiler happy
304 } catch (std::bad_alloc e) {
305 // Errors at this stage should be reported via Tcl, not via C++.
306 // However there is no point in continuing with the parsing operation,
308 interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Out of memory"));
310 } catch (CdlParseException e) {
311 interp->set_result(e.get_message());
314 interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Unexpected C++ exception"));
318 // Restore the interpreter to its prior state.
319 interp->pop_node(old_node);
320 interp->pop_container(old_container);
321 if (0 != old_commands) {
322 interp->pop_commands(old_commands);
325 CYG_REPORT_RETVAL(result);
330 // ----------------------------------------------------------------------------
331 // Syntax: script <filename>
333 CdlComponentBody::parse_script(CdlInterpreter interp, int argc, const char* argv[])
335 CYG_REPORT_FUNCNAMETYPE("parse_script", "result %d");
337 int result = CdlParse::parse_string_property(interp, argc, argv, CdlPropertyId_Script, 0, 0);
339 CYG_REPORT_RETVAL(result);
344 //{{{ Propagation support
346 // ----------------------------------------------------------------------------
348 CdlComponentBody::update(CdlTransaction transaction, CdlUpdate update)
350 CYG_REPORT_FUNCNAME("CdlComponent::update");
352 this->CdlValuableBody::update(transaction, update);
353 this->CdlContainerBody::update(transaction, update);
359 //{{{ Persistence support
361 // ----------------------------------------------------------------------------
363 CdlComponentBody::initialize_savefile_support(CdlToplevel toplevel)
365 CYG_REPORT_FUNCNAME("CdlComponent::initialize_savefile_support");
367 toplevel->add_savefile_command("cdl_component", 0, &savefile_component_command);
368 CdlValuableBody::initialize_savefile_support(toplevel, "cdl_component");
374 CdlComponentBody::save(CdlInterpreter interp, Tcl_Channel chan, int indentation, bool minimal)
376 CYG_REPORT_FUNCNAME("CdlComponent::save");
377 CYG_REPORT_FUNCARG5XV(this, interp, chan, indentation, minimal);
378 CYG_PRECONDITION_THISC();
379 CYG_PRECONDITION_CLASSC(interp);
381 if (!minimal || this->has_additional_savefile_information() || this->value_savefile_entry_needed()) {
382 // Start with the UserVisible data, which will result in a suitable set
383 // of comments before the package definition itself.
384 this->CdlUserVisibleBody::save(interp, chan, indentation, minimal);
386 // Now output the line "cdl_component <name> {"
387 // The name is guaranteed to be a valid C preprocessor symbol, so it
388 // is not going to need any quoting.
389 std::string data = std::string(indentation, ' ') + "cdl_component " + get_name() + " {\n";
390 interp->write_data(chan, data);
392 // Deal with the value
393 bool modifiable = !(CdlValueFlavor_None == this->get_flavor()) &&
394 !this->has_property(CdlPropertyId_Calculated);
395 this->CdlValuableBody::_save(interp, chan, indentation + 4, modifiable, minimal);
397 // And with any unrecognised data
398 this->CdlNodeBody::save(interp, chan, indentation + 4, minimal);
400 // Close the cdl_component body. A blank line is added here.
401 interp->write_data(chan, "};\n\n");
404 // Packages are containers, so dump the contents as well.
405 this->CdlContainerBody::save(interp, chan, indentation, minimal);
411 CdlComponentBody::savefile_component_command(CdlInterpreter interp, int argc, const char* argv[])
413 CYG_REPORT_FUNCNAMETYPE("CdlComponent::savefile_component_command", "result %d");
414 CYG_PRECONDITION_CLASSC(interp);
417 CdlToplevel toplevel = interp->get_toplevel();
418 CYG_ASSERT_CLASSC(toplevel);
419 CdlConfiguration config = dynamic_cast<CdlConfiguration>(toplevel);
420 CYG_ASSERT_CLASSC(config);
422 std::vector<CdlInterpreterCommandEntry> subcommands;
423 std::vector<CdlInterpreterCommandEntry>* toplevel_commands = 0;
424 CdlNode old_node = 0;
429 CdlParse::report_error(interp, "", "Invalid cdl_component command in savefile, expecting two arguments.");
432 CdlNode current_node = config->lookup(argv[1]);
433 if (0 == current_node) {
434 // FIXME: save value in limbo
435 CdlParse::report_error(interp, "",
436 std::string("The savefile contains a cdl_component command for an unknown component `")
439 config->get_savefile_subcommands("cdl_component", subcommands);
440 toplevel_commands = interp->push_commands(subcommands);
441 old_node = interp->push_node(current_node);
443 std::string tcl_result;
444 result = interp->eval(argv[2], tcl_result);
446 interp->pop_commands(toplevel_commands);
447 toplevel_commands = 0;
448 interp->pop_node(old_node);
454 interp->pop_node(old_node);
456 if (0 != toplevel_commands) {
457 interp->pop_commands(toplevel_commands);
462 CYG_REPORT_RETVAL(result);
469 // ----------------------------------------------------------------------------
472 CdlComponentBody::check_this(cyg_assert_class_zeal zeal) const
474 if (CdlComponentBody_Magic != cdlcomponentbody_cookie) {
477 CYGDBG_MEMLEAK_CHECKTHIS();
479 return CdlNodeBody::check_this(zeal) &&
480 CdlContainerBody::check_this(zeal) &&
481 CdlUserVisibleBody::check_this(zeal) &&
482 CdlParentableBody::check_this(zeal) &&
483 CdlValuableBody::check_this(zeal) &&
484 CdlBuildableBody::check_this(zeal) &&
485 CdlDefinableBody::check_this(zeal);
491 // ----------------------------------------------------------------------------
494 CdlComponentBody::get_class_name() const
496 CYG_REPORT_FUNCNAME("CdlComponent::get_class_name");
497 CYG_PRECONDITION_THISC();