3 //============================================================================
7 // Implementation of the CdlPackage 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(CdlPackageBody);
72 // ----------------------------------------------------------------------------
73 // Constructor. The real work is actually done in the base classes
75 CdlPackageBody::CdlPackageBody(std::string name_arg, CdlConfiguration toplevel, std::string dir)
76 : CdlNodeBody(name_arg),
79 CdlValuableBody(CdlValueFlavor_BoolData),
83 CdlLoadableBody(toplevel, dir),
84 CdlBuildLoadableBody(),
85 CdlDefineLoadableBody()
87 CYG_REPORT_FUNCNAME("CdlPackageBody:: constructor");
88 CYG_REPORT_FUNCARG1XV(this);
90 loaded_for_template = false;
91 loaded_for_hardware = false;
92 cdlpackagebody_cookie = CdlPackageBody_Magic;
93 CYGDBG_MEMLEAK_CONSTRUCTOR();
95 CYG_POSTCONDITION_THISC();
102 // ----------------------------------------------------------------------------
103 // Most of the work is done in the base classes.
105 CdlPackageBody::~CdlPackageBody()
107 CYG_REPORT_FUNCNAME("CdlPackageBody:: destructor");
108 CYG_REPORT_FUNCARG1XV(this);
109 CYG_PRECONDITION_THISC();
111 loaded_for_template = false;
112 loaded_for_hardware = false;
113 cdlpackagebody_cookie = CdlPackageBody_Invalid;
114 CYGDBG_MEMLEAK_DESTRUCTOR();
120 //{{{ parse_package()
122 // ----------------------------------------------------------------------------
123 // Parsing a package definition. This routine gets invoked directly from the
124 // Tcl interpreter, when the cdl_package command is encountered. The
125 // command takes two arguments, a name and a body of properties, and there
126 // should only be one cdl_package command per package.
128 // At the point that the cdl_package command is executed the CdlPackage
129 // object should already exist, and in fact it should be the current
130 // interpreter's loadable. Obviously the name should be checked. The
131 // main purpose of the cdl_package command is to fill in some extra
132 // information in the form of properties.
134 // A package is a buildable, valuable, uservisible, ... object so it
135 // inherits properties from all three. In practice some of the
136 // properties from the base classes are not actually legal, but that
137 // will be caught by the validation code. Additional properties
138 // relevant to a package are: parent, license_proc, install_proc, and
139 // wizard. It is harmless (but unnecessary) to allow components etc.
140 // to be defined inside a package definition.
143 CdlPackageBody::parse_package(CdlInterpreter interp, int argc, const char* argv[])
145 CYG_REPORT_FUNCNAMETYPE("CdlPackageBody::parse_package", "result %d");
146 CYG_REPORT_FUNCARG1("argc %d", argc);
147 CYG_PRECONDITION_CLASSC(interp);
149 std::string diag_argv0 = CdlParse::get_tcl_cmd_name(argv[0]);
151 CdlLoadable loadable = interp->get_loadable();
152 CdlPackage package = dynamic_cast<CdlPackage>(loadable);
153 CdlContainer parent = package->get_parent();
154 CdlToplevel toplevel = interp->get_toplevel();
155 std::string filename = interp->get_context();
157 CYG_ASSERT_CLASSC(loadable); // There should always be a loadable during parsing
158 CYG_ASSERT_CLASSC(package); // And packages are the only loadable for software CDL
159 CYG_ASSERT_CLASSC(toplevel);
160 CYG_ASSERTC(dynamic_cast<CdlToplevel>(parent) == toplevel); // The package cannot have been reparented yet.
161 CYG_ASSERTC("" != filename);
162 CYG_UNUSED_PARAM(CdlContainer, parent);
163 CYG_UNUSED_PARAM(CdlToplevel, toplevel);
165 // There should be no current node, in fact the cdl_package command
166 // can only exist at the toplevel of the original script courtesy
167 // of commands being pushed and popped.
168 CYG_ASSERTC(0 == interp->get_node());
170 // Also, the package should be the current container.
171 CYG_ASSERTC(package == dynamic_cast<CdlPackage>(interp->get_container()));
173 // Declare these outside the scope of the try statement, to allow
174 // goto calls for the error handling.
175 const std::vector<CdlProperty>& properties = package->get_properties();
177 CdlInterpreterBody::NodeSupport interp_node(interp, package);
178 static CdlInterpreterCommandEntry commands[] =
180 CdlInterpreterCommandEntry("hardware", &parse_hardware ),
181 CdlInterpreterCommandEntry("license_proc", &parse_license_proc ),
182 CdlInterpreterCommandEntry("install_proc", &parse_install_proc ),
183 CdlInterpreterCommandEntry("cdl_component", &CdlComponentBody::parse_component ),
184 CdlInterpreterCommandEntry("cdl_option", &CdlOptionBody::parse_option ),
185 CdlInterpreterCommandEntry("cdl_interface", &CdlInterfaceBody::parse_interface ),
186 CdlInterpreterCommandEntry("cdl_dialog", &CdlDialogBody::parse_dialog ),
187 CdlInterpreterCommandEntry("cdl_wizard", &CdlWizardBody::parse_wizard ),
188 CdlInterpreterCommandEntry("", 0 )
190 std::vector<CdlInterpreterCommandEntry> new_commands;
193 // All parsing errors may result in an exception, under the control of
194 // application code. This exception must not pass through the Tcl interpreter.
198 // Currently there are no options. This may change in future.
200 CdlParse::report_error(interp, "",
201 std::string("Incorrect number of arguments to `") + diag_argv0 +
202 "'\nExpecting name and properties list.");
203 } else if (argv[1] != loadable->get_name()) {
204 CdlParse::report_error(interp, "",
205 std::string("Incorrect package name in CDL script.\n") +
206 "This package is `" + loadable->get_name() + "'\n" +
207 "The CDL script `" + filename + "' defines a package `" + argv[1] + "'.");
208 } else if (0 != properties.size()) {
209 CdlParse::report_error(interp, "",
210 std::string("Duplicate cdl_package commands for package `") + argv[1] + "'.");
211 } else if (!Tcl_CommandComplete(CDL_TCL_CONST_CAST(char*, argv[2]))) {
212 CdlParse::report_error(interp, "",
213 std::string("Invalid property list for cdl_package `") + argv[1] + "'.");
216 for (i = 0; 0 != commands[i].command; i++) {
217 new_commands.push_back(commands[i]);
220 CdlBuildLoadableBody::add_property_parsers(new_commands);
221 CdlBuildableBody::add_property_parsers(new_commands);
222 CdlDefineLoadableBody::add_property_parsers(new_commands);
223 CdlDefinableBody::add_property_parsers(new_commands);
224 CdlParentableBody::add_property_parsers(new_commands);
225 CdlValuableBody::add_property_parsers(new_commands);
226 CdlUserVisibleBody::add_property_parsers(new_commands);
227 CdlNodeBody::add_property_parsers(new_commands);
229 // Now evaluate the body. If an error occurs then typically
230 // this will be reported via CdlParse::report_error(),
231 // but any exceptions will have been intercepted and
232 // turned into a Tcl error.
233 CdlInterpreterBody::CommandSupport interp_cmds(interp, new_commands);
234 result = interp->eval(argv[2]);
235 if (TCL_OK == result) {
237 // Even if there were errors, they were not fatal. There may
238 // now be a number of properties for this package, and some
239 // validation should take place. Start with the base classes.
240 package->CdlNodeBody::check_properties(interp);
241 package->CdlUserVisibleBody::check_properties(interp);
242 package->CdlValuableBody::check_properties(interp);
243 package->CdlParentableBody::check_properties(interp);
244 package->CdlBuildableBody::check_properties(interp);
245 package->CdlBuildLoadableBody::check_properties(interp);
246 package->CdlDefinableBody::check_properties(interp);
247 package->CdlDefineLoadableBody::check_properties(interp);
249 // Some of the properties in the base classes are not actually
250 // appropriate. A package is valuable, but it can only be
251 // modified by loading and unloading. Many of the value-related
252 // properties do not make sense.
253 if (package->count_properties(CdlPropertyId_Flavor) > 0) {
254 CdlParse::report_error(interp, "", "A package should not have a `flavor' property.");
256 if (package->count_properties(CdlPropertyId_EntryProc) > 0) {
257 CdlParse::report_error(interp, "", "A package should not have an `entry_proc' property.");
259 if (package->count_properties(CdlPropertyId_CheckProc) > 0) {
260 CdlParse::report_error(interp, "", "A package should not have a `check_proc' property.");
262 // BLV: this reasoning is faulty, it should be possible to
263 // control the enabled aspect via an expression. That would
264 // need option processing for the default_value property.
265 if (package->count_properties(CdlPropertyId_DefaultValue) > 0) {
266 CdlParse::report_error(interp, "", "A package should not have a `default_value' property.");
268 if (package->count_properties(CdlPropertyId_LegalValues) > 0) {
269 CdlParse::report_error(interp, "", "A package should not have a `legal_values' property.");
271 if (package->count_properties(CdlPropertyId_Calculated) > 0) {
272 CdlParse::report_error(interp, "", "A package should not have a `calculated' property.");
274 if (package->count_properties(CdlPropertyId_Dialog) > 0) {
275 CdlParse::report_error(interp, "", "A package should not have a `dialog' property.");
278 // There should be at most one each of license_proc, install_proc, include_dir,
279 // export_to, library, makefile, and wizard.
280 if (package->count_properties(CdlPropertyId_LicenseProc) > 1) {
281 CdlParse::report_error(interp, "", "A package should have at most one `license_proc' property.");
283 if (package->count_properties(CdlPropertyId_InstallProc) > 1) {
284 CdlParse::report_error(interp, "", "A package should have at most one `install_proc' property.");
289 } catch (std::bad_alloc e) {
290 // Errors at this stage should be reported via Tcl, not via C++
291 interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Out of memory"));
293 } catch (CdlParseException e) {
294 interp->set_result(e.get_message());
297 interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Unexpected C++ exception"));
301 CYG_REPORT_RETVAL(result);
306 //{{{ Package properties
308 // ----------------------------------------------------------------------------
311 CdlPackageBody::parse_hardware(CdlInterpreter interp, int argc, const char* argv[])
313 CYG_REPORT_FUNCNAMETYPE("parse_hardware", "result %d");
315 int result = CdlParse::parse_minimal_property(interp, argc, argv, CdlPropertyId_Hardware, 0, 0);
317 CYG_REPORT_RETVAL(result);
321 // ----------------------------------------------------------------------------
322 // Syntax: install_proc <tclcode>
324 CdlPackageBody::parse_install_proc(CdlInterpreter interp, int argc, const char* argv[])
326 CYG_REPORT_FUNCNAMETYPE("parse_install_proc", "result %d");
328 int result = CdlParse::parse_tclcode_property(interp, argc, argv, CdlPropertyId_InstallProc, 0, 0);
330 CYG_REPORT_RETVAL(result);
335 // ----------------------------------------------------------------------------
336 // Syntax: license_proc <tclcode>
339 CdlPackageBody::parse_license_proc(CdlInterpreter interp, int argc, const char* argv[])
341 CYG_REPORT_FUNCNAMETYPE("parse_license_proc", "result %d");
343 int result = CdlParse::parse_tclcode_property(interp, argc, argv, CdlPropertyId_LicenseProc, 0, 0);
345 CYG_REPORT_RETVAL(result);
349 // ----------------------------------------------------------------------------
352 CdlPackageBody::is_hardware_package() const
354 CYG_REPORT_FUNCNAMETYPE("CdlPackage::is_hardware_package", "result %d");
355 CYG_REPORT_FUNCARG1XV(this);
356 CYG_PRECONDITION_THISC();
359 if (has_property(CdlPropertyId_Hardware)) {
363 CYG_REPORT_RETVAL(result);
368 CdlPackageBody::has_install_proc() const
370 CYG_REPORT_FUNCNAMETYPE("CdlPackage::has_install_proc", "result 5d");
371 CYG_REPORT_FUNCARG1XV(this);
372 CYG_PRECONDITION_THISC();
375 if (has_property(CdlPropertyId_InstallProc)) {
379 CYG_REPORT_RETVAL(result);
384 CdlPackageBody::get_install_proc() const
386 CYG_REPORT_FUNCNAME("CdlPackage::get_install_proc");
387 CYG_REPORT_FUNCARG1XV(this);
388 CYG_PRECONDITION_THISC();
390 static cdl_tcl_code null_result = "";
391 cdl_tcl_code& result = null_result;
392 CdlProperty prop = get_property(CdlPropertyId_InstallProc);
394 CdlProperty_TclCode tclprop = dynamic_cast<CdlProperty_TclCode>(prop);
395 result = tclprop->get_code();
403 CdlPackageBody::has_license_proc() const
405 CYG_REPORT_FUNCNAMETYPE("CdlPackage::has_install_proc", "result 5d");
406 CYG_REPORT_FUNCARG1XV(this);
407 CYG_PRECONDITION_THISC();
410 if (has_property(CdlPropertyId_LicenseProc)) {
414 CYG_REPORT_RETVAL(result);
419 CdlPackageBody::get_license_proc() const
421 CYG_REPORT_FUNCNAME("CdlPackage::get_install_proc");
422 CYG_REPORT_FUNCARG1XV(this);
423 CYG_PRECONDITION_THISC();
425 static cdl_tcl_code null_result = "";
426 cdl_tcl_code& result = null_result;
427 CdlProperty prop = get_property(CdlPropertyId_LicenseProc);
429 CdlProperty_TclCode tclprop = dynamic_cast<CdlProperty_TclCode>(prop);
430 result = tclprop->get_code();
438 //{{{ Propagation support
440 // ----------------------------------------------------------------------------
442 CdlPackageBody::update(CdlTransaction transaction, CdlUpdate update)
444 CYG_REPORT_FUNCNAME("CdlPackage::update");
446 this->CdlValuableBody::update(transaction, update);
447 this->CdlContainerBody::update(transaction, update);
453 //{{{ Persistence support
455 // ----------------------------------------------------------------------------
458 CdlPackageBody::initialize_savefile_support(CdlToplevel toplevel)
460 CYG_REPORT_FUNCNAME("CdlPackage::initialize_savefile_support");
462 toplevel->add_savefile_command("cdl_package", 0, &savefile_package_command);
463 CdlValuableBody::initialize_savefile_support(toplevel, "cdl_package");
467 CdlPackageBody::save(CdlInterpreter interp, Tcl_Channel chan, int indentation, bool minimal)
469 CYG_REPORT_FUNCNAME("CdlPackage::save");
470 CYG_REPORT_FUNCARG5XV(this, interp, chan, indentation, minimal);
471 CYG_PRECONDITION_THISC();
472 CYG_PRECONDITION_CLASSC(interp);
474 // For a minimal save there is sufficient data in the savefile header
475 // to allow the package to be loaded. It is still necessary to output
476 // a cdl_package command if there were additional savefile strings.
477 if (!minimal || this->has_additional_savefile_information()) {
479 // Start with the UserVisible data, which will result in a suitable set
480 // of comments before the package definition itself.
481 this->CdlUserVisibleBody::save(interp, chan, indentation, minimal);
483 // Now output the line "cdl_package <name> {"
484 // The name is guaranteed to be a valid C preprocessor symbol, so it
485 // is not going to need any quoting.
486 std::string data = std::string(indentation, ' ') + "cdl_package " + get_name() + " {\n";
487 std::string indent_string = std::string(indentation + 4, ' ');
489 // The value associated with a package cannot be changed simply
490 // by editing the savefile. Add a comment to that effect.
492 data += indent_string + "# Packages cannot be added or removed, nor can their version be changed,\n";
493 data += indent_string + "# simply by editing their value. Instead the appropriate configuration\n";
494 data += indent_string + "# should be used to perform these actions.\n\n";
497 // Output the command and the comment.
498 interp->write_data(chan, data);
500 // Deal with the value
501 this->CdlValuableBody::save(interp, chan, indentation + 4, false, minimal);
503 // And with any unrecognised data
504 this->CdlNodeBody::save(interp, chan, indentation + 4, minimal);
506 // Close the cdl_package body. A blank line is added here.
509 interp->write_data(chan, data);
512 // Packages are containers, so dump the contents as well.
513 this->CdlContainerBody::save(interp, chan, indentation, minimal);
519 CdlPackageBody::savefile_package_command(CdlInterpreter interp, int argc, const char* argv[])
521 CYG_REPORT_FUNCNAMETYPE("CdlPackage::savefile_package_command", "result %d");
522 CYG_PRECONDITION_CLASSC(interp);
525 CdlToplevel toplevel = interp->get_toplevel();
526 CYG_ASSERT_CLASSC(toplevel);
527 CdlConfiguration config = dynamic_cast<CdlConfiguration>(toplevel);
528 CYG_ASSERT_CLASSC(config);
530 std::vector<CdlInterpreterCommandEntry> subcommands;
531 std::vector<CdlInterpreterCommandEntry>* toplevel_commands = 0;
532 CdlNode old_node = 0;
537 CdlParse::report_error(interp, "", "Invalid cdl_package command in savefile, expecting two arguments.");
540 CdlNode current_node = config->lookup(argv[1]);
541 if (0 == current_node) {
542 CdlParse::report_error(interp, "",
543 std::string("The savefile contains a cdl_package command for `") +
544 argv[1] + "' which has not been loaded.");
546 config->get_savefile_subcommands("cdl_package", subcommands);
547 toplevel_commands = interp->push_commands(subcommands);
548 old_node = interp->push_node(current_node);
550 std::string tcl_result;
551 result = interp->eval(argv[2], tcl_result);
553 interp->pop_commands(toplevel_commands);
554 toplevel_commands = 0;
555 interp->pop_node(old_node);
561 interp->pop_node(old_node);
563 if (0 != toplevel_commands) {
564 interp->pop_commands(toplevel_commands);
569 CYG_REPORT_RETVAL(result);
576 // ----------------------------------------------------------------------------
579 CdlPackageBody::check_this(cyg_assert_class_zeal zeal) const
581 if (CdlPackageBody_Magic != cdlpackagebody_cookie) {
584 CYGDBG_MEMLEAK_CHECKTHIS();
586 return CdlNodeBody::check_this(zeal) &&
587 CdlContainerBody::check_this(zeal) &&
588 CdlLoadableBody::check_this(zeal) &&
589 CdlUserVisibleBody::check_this(zeal) &&
590 CdlValuableBody::check_this(zeal) &&
591 CdlParentableBody::check_this(zeal) &&
592 CdlBuildableBody::check_this(zeal) &&
593 CdlBuildLoadableBody::check_this(zeal) &&
594 CdlDefinableBody::check_this(zeal) &&
595 CdlDefineLoadableBody::check_this(zeal);
601 // ----------------------------------------------------------------------------
604 CdlPackageBody::get_class_name() const
606 CYG_REPORT_FUNCNAME("CdlPackage::get_class_name");
607 CYG_PRECONDITION_THISC();
612 // BLV: there is an argument for forcing hardware packages to
613 // send their configuration data to a single header file
614 // <pkgconf/hardware.h>, so that any code can #include a
615 // single file to get hold of the hardware details. This
616 // is suppressed for now while the details are sorted out.
619 CdlPackageBody::get_config_header() const
621 CYG_REPORT_FUNCNAME("CdlPackage::get_config_header");
622 CYG_REPORT_FUNCARG1XV(this);
623 CYG_PRECONDITION_CLASSC(this);
625 std::string result = "";
627 if (has_property(CdlPropertyId_Hardware)) {
628 result = "hardware.h";
630 result = CdlDefineLoadableBody::get_config_header();
633 result = CdlDefineLoadableBody::get_config_header();
641 CdlPackageBody::belongs_to_template() const
643 CYG_REPORT_FUNCNAMETYPE("CdlPackage::belongs_to_template", "result %d");
644 CYG_REPORT_FUNCARG1XV(this);
645 CYG_PRECONDITION_THISC();
647 bool result = loaded_for_template;
649 CYG_REPORT_RETVAL(result);
654 CdlPackageBody::belongs_to_hardware() const
656 CYG_REPORT_FUNCNAMETYPE("CdlPackage::belongs_to_hardware", "result %d");
657 CYG_REPORT_FUNCARG1XV(this);
658 CYG_PRECONDITION_THISC();
660 bool result = loaded_for_hardware;
662 CYG_REPORT_RETVAL(result);