]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - tools/src/libcdl/component.cxx
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / tools / src / libcdl / component.cxx
1 //{{{  Banner                           
2
3 //============================================================================
4 //
5 //     component.cxx
6 //
7 //     Implementation of the CdlComponent class
8 //
9 //============================================================================
10 //####COPYRIGHTBEGIN####
11 //                                                                          
12 // ----------------------------------------------------------------------------
13 // Copyright (C) 2002 Bart Veer
14 // Copyright (C) 1999, 2000 Red Hat, Inc.
15 //
16 // This file is part of the eCos host tools.
17 //
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) 
21 // any later version.
22 // 
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 
26 // more details.
27 // 
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.
31 //
32 // ----------------------------------------------------------------------------
33 //                                                                          
34 //####COPYRIGHTEND####
35 //============================================================================
36 //#####DESCRIPTIONBEGIN####
37 //
38 // Author(s):   bartv
39 // Contact(s):  bartv
40 // Date:        1999/03/01
41 // Version:     0.02
42 //
43 //####DESCRIPTIONEND####
44 //============================================================================
45
46 //}}}
47 //{{{  #include's                       
48
49 // ----------------------------------------------------------------------------
50 #include "cdlconfig.h"
51
52 // Get the infrastructure types, assertions, tracing and similar
53 // facilities.
54 #include <cyg/infra/cyg_ass.h>
55 #include <cyg/infra/cyg_trac.h>
56
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.
60 #include <cdl.hxx>
61
62 //}}}
63
64 //{{{  Statics                          
65
66 // ----------------------------------------------------------------------------
67 CYGDBG_DEFINE_MEMLEAK_COUNTER(CdlComponentBody);
68
69 //}}}
70 //{{{  Constructor                      
71
72 // ----------------------------------------------------------------------------
73 CdlComponentBody::CdlComponentBody(std::string name_arg)
74     : CdlNodeBody(name_arg),
75       CdlContainerBody(),
76       CdlUserVisibleBody(),
77       CdlValuableBody(),
78       CdlParentableBody(),
79       CdlBuildableBody(),
80       CdlDefinableBody()
81 {
82     CYG_REPORT_FUNCNAME("CdlComponentBody:: constructor");
83     CYG_REPORT_FUNCARG1XV(this);
84
85     cdlcomponentbody_cookie = CdlComponentBody_Magic;
86     CYGDBG_MEMLEAK_CONSTRUCTOR();
87
88     CYG_POSTCONDITION_THISC();
89     CYG_REPORT_RETURN();
90 }
91
92 //}}}
93 //{{{  Destructor                       
94
95 // ----------------------------------------------------------------------------
96
97 CdlComponentBody::~CdlComponentBody()
98 {
99     CYG_REPORT_FUNCNAME("CdlComponentBody:: destructor");
100     CYG_REPORT_FUNCARG1XV(this);
101     CYG_PRECONDITION_THISC();
102
103     cdlcomponentbody_cookie = CdlComponentBody_Invalid;
104     CYGDBG_MEMLEAK_DESTRUCTOR();
105     
106     CYG_REPORT_RETURN();
107 }
108
109 //}}}
110 //{{{  parse_component()                
111
112 // ----------------------------------------------------------------------------
113 // Parsing a component definition. This routine gets invoked directly from the
114 // Tcl interpreter.
115
116 int
117 CdlComponentBody::parse_component(CdlInterpreter interp, int argc, const char* argv[])
118 {
119     CYG_REPORT_FUNCNAMETYPE("CdlComponentBody::parse_component", "result %d");
120     CYG_REPORT_FUNCARG1("argc %d", argc);
121     CYG_PRECONDITION_CLASSC(interp);
122     
123     std::string  diag_argv0      = CdlParse::get_tcl_cmd_name(argv[0]);
124
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);
133
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;
141     bool         ok             = true;
142     int          result         = TCL_OK;
143     try {
144     
145         // Currently there are no options. This may change in future.
146         if (3 != argc) {
147             CdlParse::report_error(interp, "",
148                                    std::string("Incorrect number of arguments to `") + diag_argv0 +
149                                          "'\nExpecting name and properties list.");
150             ok = false;
151             goto done;
152         }
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] + "'.");
156             ok = false;
157             goto done;
158         }
159
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.");
164             ok = false;
165         } else {
166             new_component = new CdlComponentBody(argv[1]);
167             toplevel->add_node(package, parent, new_component);
168         }
169
170       done:
171         if (!ok) {
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);
175             return TCL_OK;
176         }
177     } catch(std::bad_alloc e) {
178         interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Out of memory"));
179         result = TCL_ERROR;
180     } catch(CdlParseException e) {
181         interp->set_result(e.get_message());
182         result = TCL_ERROR;
183     } catch(...) {
184         interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Unexpected C++ exception"));
185         result = TCL_ERROR;
186     }
187     if (TCL_OK != result) {
188         CYG_REPORT_RETVAL(result);
189         return result;
190     }
191
192     // At this stage new_component has been created and added to the hierarchy.
193     // The main work now is to add the properties.
194     
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);
201
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[] =
208     {
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                                  )
216     };
217     static CdlInterpreterCommandEntry   script_commands[] =
218     {
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                                  ),
225     };
226     int i;
227     
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.
230     try {
231
232         for (i = 0; 0 != commands[i].command; i++) {
233             new_commands.push_back(commands[i]);
234         }
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);
241     
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);
249         
250         if (TCL_OK != result) {
251             // No point in taking any further action, just go with the flow
252             goto done2;
253         }
254
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);
264
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.");
268         }
269         if (new_component->count_properties(CdlPropertyId_Script) > 1) {
270             CdlParse::report_error(interp, "", "A component should have at most one `script' property.");
271         }
272
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();
278
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 + "'.");
283             } else {
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
287                 // of commands.
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]);
292                 }
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);
297             }
298         }
299
300       done2:
301         // Dummy command just to keep the compiler happy
302         old_context = "";
303         
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,
307         // just give up.
308         interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Out of memory"));
309         result = TCL_ERROR;
310     } catch (CdlParseException e) {
311         interp->set_result(e.get_message());
312         result = TCL_ERROR;
313     } catch(...) {
314         interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Unexpected C++ exception"));
315         result = TCL_ERROR;
316     }
317
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);
323     }
324     
325     CYG_REPORT_RETVAL(result);
326     return result;
327 }
328
329
330 // ----------------------------------------------------------------------------
331 // Syntax: script <filename>
332 int
333 CdlComponentBody::parse_script(CdlInterpreter interp, int argc, const char* argv[])
334 {
335     CYG_REPORT_FUNCNAMETYPE("parse_script", "result %d");
336
337     int result = CdlParse::parse_string_property(interp, argc, argv, CdlPropertyId_Script, 0, 0);
338     
339     CYG_REPORT_RETVAL(result);
340     return result;
341 }
342
343 //}}}
344 //{{{  Propagation support              
345
346 // ----------------------------------------------------------------------------
347 void
348 CdlComponentBody::update(CdlTransaction transaction, CdlUpdate update)
349 {
350     CYG_REPORT_FUNCNAME("CdlComponent::update");
351
352     this->CdlValuableBody::update(transaction, update);
353     this->CdlContainerBody::update(transaction, update);
354     
355     CYG_REPORT_RETURN();
356 }
357
358 //}}}
359 //{{{  Persistence support              
360
361 // ----------------------------------------------------------------------------
362 void
363 CdlComponentBody::initialize_savefile_support(CdlToplevel toplevel)
364 {
365     CYG_REPORT_FUNCNAME("CdlComponent::initialize_savefile_support");
366
367     toplevel->add_savefile_command("cdl_component", 0, &savefile_component_command);
368     CdlValuableBody::initialize_savefile_support(toplevel, "cdl_component");
369
370     CYG_REPORT_RETURN();
371 }
372
373 void
374 CdlComponentBody::save(CdlInterpreter interp, Tcl_Channel chan, int indentation, bool minimal)
375 {
376     CYG_REPORT_FUNCNAME("CdlComponent::save");
377     CYG_REPORT_FUNCARG5XV(this, interp, chan, indentation, minimal);
378     CYG_PRECONDITION_THISC();
379     CYG_PRECONDITION_CLASSC(interp);
380
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);
385
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);
391
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);
396
397         // And with any unrecognised data
398         this->CdlNodeBody::save(interp, chan, indentation + 4, minimal);
399     
400         // Close the cdl_component body. A blank line is added here.
401         interp->write_data(chan, "};\n\n");
402     }
403     
404     // Packages are containers, so dump the contents as well.
405     this->CdlContainerBody::save(interp, chan, indentation, minimal);
406     
407     CYG_REPORT_RETURN();
408 }
409
410 int
411 CdlComponentBody::savefile_component_command(CdlInterpreter interp, int argc, const char* argv[])
412 {
413     CYG_REPORT_FUNCNAMETYPE("CdlComponent::savefile_component_command", "result %d");
414     CYG_PRECONDITION_CLASSC(interp);
415
416     int result = TCL_OK;
417     CdlToplevel toplevel = interp->get_toplevel();
418     CYG_ASSERT_CLASSC(toplevel);
419     CdlConfiguration config = dynamic_cast<CdlConfiguration>(toplevel);
420     CYG_ASSERT_CLASSC(config);
421
422     std::vector<CdlInterpreterCommandEntry> subcommands;
423     std::vector<CdlInterpreterCommandEntry>* toplevel_commands = 0;
424     CdlNode old_node = 0;
425     
426     try {
427         
428         if (3 != argc) {
429             CdlParse::report_error(interp, "", "Invalid cdl_component command in savefile, expecting two arguments.");
430         } else {
431
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 `")
437                                        + argv[1] + "'");
438             } else {
439                 config->get_savefile_subcommands("cdl_component", subcommands);
440                 toplevel_commands = interp->push_commands(subcommands);
441                 old_node = interp->push_node(current_node);
442                 
443                 std::string tcl_result;
444                 result = interp->eval(argv[2], tcl_result);
445             
446                 interp->pop_commands(toplevel_commands);
447                 toplevel_commands = 0;
448                 interp->pop_node(old_node);
449                 old_node = 0;
450             }
451         }
452     } catch(...) {
453         if (0 != old_node) {
454             interp->pop_node(old_node);
455         }
456         if (0 != toplevel_commands) {
457             interp->pop_commands(toplevel_commands);
458         }
459         throw;
460     }
461
462     CYG_REPORT_RETVAL(result);
463     return result;
464 }
465
466 //}}}
467 //{{{  check_this()                     
468
469 // ----------------------------------------------------------------------------
470
471 bool
472 CdlComponentBody::check_this(cyg_assert_class_zeal zeal) const
473 {
474     if (CdlComponentBody_Magic != cdlcomponentbody_cookie) {
475         return false;
476     }
477     CYGDBG_MEMLEAK_CHECKTHIS();
478
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);
486 }
487
488 //}}}
489 //{{{  Misc                             
490
491 // ----------------------------------------------------------------------------
492
493 std::string
494 CdlComponentBody::get_class_name() const
495 {
496     CYG_REPORT_FUNCNAME("CdlComponent::get_class_name");
497     CYG_PRECONDITION_THISC();
498     CYG_REPORT_RETURN();
499     return "component";
500 }
501
502 //}}}