3 //============================================================================
7 // Implementation of the CdlWizard 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 // <cdlcore.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 <cdlcore.hxx>
66 // ----------------------------------------------------------------------------
67 CYGDBG_DEFINE_MEMLEAK_COUNTER(CdlWizardBody);
72 // ----------------------------------------------------------------------------
73 // Constructor. The real work is actually done in the parse routine.
74 CdlWizardBody::CdlWizardBody(std::string name_arg)
75 : CdlNodeBody(name_arg),
79 CYG_REPORT_FUNCNAME("CdlWizardBody:: constructor");
80 CYG_REPORT_FUNCARG1XV(this);
82 cdlwizardbody_cookie = CdlWizardBody_Magic;
83 CYGDBG_MEMLEAK_CONSTRUCTOR();
85 CYG_POSTCONDITION_THISC();
92 // ----------------------------------------------------------------------------
93 // The real work is done in the base classes.
94 CdlWizardBody::~CdlWizardBody()
96 CYG_REPORT_FUNCNAME("CdlWizardBody:: destructor");
97 CYG_REPORT_FUNCARG1XV(this);
98 CYG_PRECONDITION_THISC();
100 cdlwizardbody_cookie = CdlWizardBody_Invalid;
101 CYGDBG_MEMLEAK_DESTRUCTOR();
109 // ----------------------------------------------------------------------------
110 // Parsing a wizard definition.
113 CdlWizardBody::parse_wizard(CdlInterpreter interp, int argc, const char* argv[])
115 CYG_REPORT_FUNCNAMETYPE("CdlWizard::parse_wizard", "result %d");
116 CYG_REPORT_FUNCARG1("argc %d", argc);
117 CYG_PRECONDITION_CLASSC(interp);
120 std::string diag_argv0 = CdlParse::get_tcl_cmd_name(argv[0]);
122 CdlLoadable loadable = interp->get_loadable();
123 CdlContainer parent = interp->get_container();
124 CdlToplevel toplevel = interp->get_toplevel();
125 CYG_ASSERT_CLASSC(loadable); // There should always be a loadable during parsing
126 CYG_ASSERT_CLASSC(parent);
127 CYG_ASSERT_CLASSC(toplevel);
129 // The new wizard should be created and added to the loadable
130 // early on. If there is a parsing error it will get cleaned up
131 // automatically as a consequence of the loadable destructor.
132 // However it is necessary to validate the name first. Errors
133 // should be reported via CdlParse::report_error(), which
134 // may result in an exception.
135 CdlWizard new_wizard = 0;
138 // Currently there are no command-line options. This may change in future.
140 CdlParse::report_error(interp, "", std::string("Incorrect number of arguments to `") + diag_argv0 +
141 "'\nExpecting name and properties list.");
142 } else if (!Tcl_CommandComplete(CDL_TCL_CONST_CAST(char*, argv[2]))) {
143 CdlParse::report_error(interp, "", std::string("Invalid property list for cdl_wizard `") + argv[1]+ "'.");
144 } else if (0 != toplevel->lookup(argv[1])) {
145 CdlParse::report_error(interp, "", std::string("Wizard `") + argv[1] +
146 "' cannot be loaded.\nThe name is already in use.");
148 new_wizard = new CdlWizardBody(argv[1]);
149 toplevel->add_node(loadable, parent, new_wizard);
151 // At this stage new_wizard has been created and added to the hierarchy.
152 // The main work now is to add the properties.
154 // Push the wizard as the current base object early on.
155 // This aids diagnostics.
156 CdlNode old_node = 0;
158 std::string tcl_result;
159 std::vector<CdlInterpreterCommandEntry> new_commands;
160 std::vector<CdlInterpreterCommandEntry>* old_commands = 0;
161 static CdlInterpreterCommandEntry commands[] =
163 CdlInterpreterCommandEntry("init_proc", &parse_init_proc ),
164 CdlInterpreterCommandEntry("decoration_proc", &parse_decoration_proc ),
165 CdlInterpreterCommandEntry("screen", &parse_screen ),
166 CdlInterpreterCommandEntry("confirm_proc", &parse_confirm_proc ),
167 CdlInterpreterCommandEntry("cancel_proc", &parse_cancel_proc ),
168 CdlInterpreterCommandEntry("", 0 ),
171 for (i = 0; 0 != commands[i].command; i++) {
172 new_commands.push_back(commands[i]);
174 CdlParentableBody::add_property_parsers(new_commands);
175 CdlUserVisibleBody::add_property_parsers(new_commands);
176 CdlNodeBody::add_property_parsers(new_commands);
178 // Now evaluate the body. If an error occurs then typically
179 // this will be reported via CdlParse::report_error(),
180 // but any exceptions will have been intercepted and
181 // turned into a Tcl error.
182 old_node = interp->push_node(new_wizard);
183 old_commands = interp->push_commands(new_commands);
184 result = interp->eval(argv[2], tcl_result);
185 interp->pop_node(old_node);
186 interp->pop_commands(old_commands);
188 if (TCL_OK == result) {
189 // Even if there were errors, they were not fatal. There may
190 // now be a number of properties for this option, and some
191 // validation should take place. Start with the base classes.
192 new_wizard->CdlNodeBody::check_properties(interp);
193 new_wizard->CdlUserVisibleBody::check_properties(interp);
194 new_wizard->CdlParentableBody::check_properties(interp);
196 // The init_proc and decoration_proc properties are
197 // optional. The confirm_proc and cancel_proc properties
198 // are compulsory, and there should be at least one screen
200 if (new_wizard->count_properties(CdlPropertyId_InitProc) > 1) {
201 CdlParse::report_error(interp, "", "A wizard should have only one `init_proc' property.");
203 if (new_wizard->count_properties(CdlPropertyId_DecorationProc) > 1) {
204 CdlParse::report_error(interp, "", "A wizard should have only one `decoration_proc' property.");
206 if (new_wizard->count_properties(CdlPropertyId_ConfirmProc) != 1) {
207 CdlParse::report_error(interp, "", "A wizard should have one `confirm_proc' property.");
209 if (new_wizard->count_properties(CdlPropertyId_CancelProc) != 1) {
210 CdlParse::report_error(interp, "", "A wizard should have one `cancel_proc' property.");
212 if (new_wizard->count_properties(CdlPropertyId_Screen) < 1) {
213 CdlParse::report_error(interp, "", "A wizard should have at least one `screen' property.");
216 // It is necessary to check that all the screen properties have unique numbers
217 const std::vector<CdlProperty>& properties = new_wizard->get_properties();
218 std::vector<CdlProperty>::const_iterator prop_i, prop_j;
219 for (prop_i = properties.begin(); prop_i != properties.end(); prop_i++) {
220 if (CdlPropertyId_Screen != (*prop_i)->get_property_name()) {
223 CdlProperty_TclCode tclprop = dynamic_cast<CdlProperty_TclCode>(*prop_i);
224 CYG_ASSERT_CLASSC(tclprop);
225 cdl_int num1 = tclprop->get_number();
227 for (prop_j = ++prop_i; prop_j != properties.end(); prop_j++) {
228 if (CdlPropertyId_Screen != (*prop_j)->get_property_name()) {
231 CdlProperty_TclCode tclprop2 = dynamic_cast<CdlProperty_TclCode>(*prop_j);
232 CYG_ASSERT_CLASSC(tclprop2);
233 cdl_int num2 = tclprop2->get_number();
236 std::string tmp = "";
237 Cdl::integer_to_string(num1, tmp);
238 CdlParse::report_error(interp, "", "Duplicate definition of screen `" + tmp + "'.");
244 // There is no restriction on what screen numbers can be
245 // defined (screens may appear and disappear during
246 // development). It would be nice to validate that the
247 // Tcl fragments never switch to an invalid screen, but
248 // there is no easy way to do that.
253 if (0 != new_wizard) {
259 CYG_REPORT_RETVAL(result);
263 // ----------------------------------------------------------------------------
264 // Syntax: cancel_proc <tclcode>
267 CdlWizardBody::parse_cancel_proc(CdlInterpreter interp, int argc, const char* argv[])
269 CYG_REPORT_FUNCNAMETYPE("parse_cancel_proc", "result %d");
271 int result = CdlParse::parse_tclcode_property(interp, argc, argv, CdlPropertyId_CancelProc, 0, 0);
273 CYG_REPORT_RETVAL(result);
277 // ----------------------------------------------------------------------------
278 // Syntax: confirm_proc <tclcode>
281 CdlWizardBody::parse_confirm_proc(CdlInterpreter interp, int argc, const char* argv[])
283 CYG_REPORT_FUNCNAMETYPE("parse_confirm_proc", "result %d");
285 int result = CdlParse::parse_tclcode_property(interp, argc, argv, CdlPropertyId_ConfirmProc, 0, 0);
287 CYG_REPORT_RETVAL(result);
291 // ----------------------------------------------------------------------------
292 // syntax: decoration_proc <tclcode>
295 CdlWizardBody::parse_decoration_proc(CdlInterpreter interp, int argc, const char* argv[])
297 CYG_REPORT_FUNCNAMETYPE("parse_decoration_proc", "result %d");
299 int result = CdlParse::parse_tclcode_property(interp, argc, argv, CdlPropertyId_DecorationProc, 0, 0);
301 CYG_REPORT_RETVAL(result);
305 // ----------------------------------------------------------------------------
306 // Syntax: init_proc <tclcode>
309 CdlWizardBody::parse_init_proc(CdlInterpreter interp, int argc, const char* argv[])
311 CYG_REPORT_FUNCNAMETYPE("parse_init_proc", "result %d");
313 int result = CdlParse::parse_tclcode_property(interp, argc, argv, CdlPropertyId_InitProc, 0, 0);
315 CYG_REPORT_RETVAL(result);
319 // ----------------------------------------------------------------------------
320 // The screen property is a little bit special and cannot be handled by the
321 // generic parsers. There are two arguments, a number and some Tcl code.
324 CdlWizardBody::parse_screen(CdlInterpreter interp, int argc, const char* argv[])
326 CYG_REPORT_FUNCNAME("CdlParse::parse_screen");
327 CYG_REPORT_FUNCARG1("argc %d", argc);
328 CYG_PRECONDITION_CLASSC(interp);
330 CdlProperty_TclCode new_property = 0;
334 std::vector<std::pair<std::string,std::string> > options;
335 int data_index = CdlParse::parse_options(interp, std::string("property ") + argv[0], 0, argc, argv, 1, options);
336 if ((data_index + 2) != argc) {
337 CdlParse::report_property_parse_error(interp, argv[0], std::string("Invalid number of arguments.\n") +
338 " Expecting two arguments, a number and some Tcl code.");
339 } else if (!Cdl::string_to_integer(argv[data_index], number)) {
340 CdlParse::report_property_parse_error(interp, argv[0], std::string(argv[data_index]) + " is not a valid number.");
341 } else if (!Tcl_CommandComplete(CDL_TCL_CONST_CAST(char*, argv[data_index + 1]))) {
342 CdlParse::report_property_parse_error(interp, argv[0], "incomplete Tcl code fragment.");
345 CdlNode current_node = interp->get_node();
346 CYG_ASSERTC(0 != current_node);
347 new_property = CdlProperty_TclCodeBody::make(current_node, CdlPropertyId_Screen, number, argv[data_index + 1],
348 argc, argv, options);
351 if (0 != new_property) {
363 // ----------------------------------------------------------------------------
364 // For now there is no information in a wizard that should end up in a
365 // save file, but it is still desirable to override the base class
369 CdlWizardBody::save(CdlInterpreter interp, Tcl_Channel chan, int indentation, bool minimal)
371 CYG_REPORT_FUNCNAME("CdlWizard::save");
372 CYG_REPORT_FUNCARG5XV(this, interp, chan, indentation, minimal);
373 CYG_PRECONDITION_THISC();
375 CYG_UNUSED_PARAM(CdlInterpreter, interp);
376 CYG_UNUSED_PARAM(Tcl_Channel, chan);
377 CYG_UNUSED_PARAM(int, indentation);
378 CYG_UNUSED_PARAM(bool, minimal);
386 // ----------------------------------------------------------------------------
388 CdlWizardBody::has_init_proc() const
390 CYG_REPORT_FUNCNAMETYPE("CdlWizard::has_init_proc", "result %d");
391 CYG_REPORT_FUNCARG1XV(this);
392 CYG_PRECONDITION_THISC();
394 bool result = has_property(CdlPropertyId_InitProc);
395 CYG_REPORT_RETVAL(result);
400 CdlWizardBody::has_decoration_proc() const
402 CYG_REPORT_FUNCNAMETYPE("CdlWizard::has_decoration_proc", "result %d");
403 CYG_REPORT_FUNCARG1XV(this);
404 CYG_PRECONDITION_THISC();
406 bool result = has_property(CdlPropertyId_DecorationProc);
407 CYG_REPORT_RETVAL(result);
412 CdlWizardBody::has_screen(cdl_int screen) const
414 CYG_REPORT_FUNCNAMETYPE("CdlWizard::has_screen", "result %d");
415 CYG_REPORT_FUNCARG2XV(this, (int) screen);
416 CYG_PRECONDITION_THISC();
419 const std::vector<CdlProperty>& properties = get_properties();
420 std::vector<CdlProperty>::const_iterator prop_i;
421 for (prop_i = properties.begin(); prop_i != properties.end(); prop_i++) {
422 if (CdlPropertyId_Screen != (*prop_i)->get_property_name()) {
425 CdlProperty_TclCode tclprop = dynamic_cast<CdlProperty_TclCode>(*prop_i);
426 CYG_ASSERT_CLASSC(tclprop);
427 if (screen == tclprop->get_number()) {
432 CYG_REPORT_RETVAL(result);
437 CdlWizardBody::get_init_proc() const
439 CYG_REPORT_FUNCNAME("CdlWizard::get_init_proc");
440 CYG_REPORT_FUNCARG1XV(this);
441 CYG_PRECONDITION_THISC();
443 static cdl_tcl_code null_result = "";
444 cdl_tcl_code& result = null_result;
445 CdlProperty prop = get_property(CdlPropertyId_InitProc);
447 CdlProperty_TclCode tclprop = dynamic_cast<CdlProperty_TclCode>(prop);
448 CYG_ASSERT_CLASSC(tclprop);
449 result = tclprop->get_code();
457 CdlWizardBody::get_decoration_proc() const
459 CYG_REPORT_FUNCNAME("CdlWizard::get_decoration_proc");
460 CYG_REPORT_FUNCARG1XV(this);
461 CYG_PRECONDITION_THISC();
463 static cdl_tcl_code null_result = "";
464 cdl_tcl_code& result = null_result;
465 CdlProperty prop = get_property(CdlPropertyId_DecorationProc);
467 CdlProperty_TclCode tclprop = dynamic_cast<CdlProperty_TclCode>(prop);
468 CYG_ASSERT_CLASSC(tclprop);
469 result = tclprop->get_code();
477 CdlWizardBody::get_confirm_proc() const
479 CYG_REPORT_FUNCNAME("CdlWizard::get_display_proc");
480 CYG_REPORT_FUNCARG1XV(this);
481 CYG_PRECONDITION_THISC();
483 CdlProperty prop = get_property(CdlPropertyId_ConfirmProc);
484 CYG_ASSERT_CLASSC(prop);
485 CdlProperty_TclCode tclprop = dynamic_cast<CdlProperty_TclCode>(prop);
486 CYG_ASSERT_CLASSC(tclprop);
488 const cdl_tcl_code& result = tclprop->get_code();
495 CdlWizardBody::get_cancel_proc() const
497 CYG_REPORT_FUNCNAME("CdlWizard::get_display_proc");
498 CYG_REPORT_FUNCARG1XV(this);
499 CYG_PRECONDITION_THISC();
501 CdlProperty prop = get_property(CdlPropertyId_CancelProc);
502 CYG_ASSERT_CLASSC(prop);
503 CdlProperty_TclCode tclprop = dynamic_cast<CdlProperty_TclCode>(prop);
504 CYG_ASSERT_CLASSC(tclprop);
506 const cdl_tcl_code& result = tclprop->get_code();
513 CdlWizardBody::get_first_screen_number() const
515 CYG_REPORT_FUNCNAMETYPE("CdlWizard::get_first_screen_number", "result %d");
516 CYG_REPORT_FUNCARG1XV(this);
517 CYG_PRECONDITION_THISC();
519 // The result should really be initialized to MININT, but that is
520 // slightly tricky given that we are dealing with cdl_int's rather
521 // than plain int's. Instead a separate flag gives the same
523 bool result_set = false;
526 const std::vector<CdlProperty>& properties = get_properties();
527 std::vector<CdlProperty>::const_iterator prop_i;
528 for (prop_i = properties.begin(); prop_i != properties.end(); prop_i++) {
529 if (CdlPropertyId_Screen != (*prop_i)->get_property_name()) {
532 CdlProperty_TclCode tclprop = dynamic_cast<CdlProperty_TclCode>(*prop_i);
533 cdl_int its_num = tclprop->get_number();
534 if (!result_set || (its_num < result)) {
540 CYG_REPORT_RETVAL((int) result);
545 CdlWizardBody::get_first_screen() const
547 CYG_REPORT_FUNCNAME("CdlWizard::get_first_screen");
548 CYG_REPORT_FUNCARG1XV(this);
549 CYG_PRECONDITION_THISC();
551 // The result should really be initialized to MININT, but that is
552 // slightly tricky given that we are dealing with cdl_int's rather
553 // than plain int's. Instead a separate flag gives the same
555 static cdl_tcl_code null_result = "";
556 bool result_set = false;
557 cdl_tcl_code& result = null_result;
558 cdl_int result_num = -1;
560 const std::vector<CdlProperty>& properties = get_properties();
561 std::vector<CdlProperty>::const_iterator prop_i;
562 for (prop_i = properties.begin(); prop_i != properties.end(); prop_i++) {
563 if (CdlPropertyId_Screen != (*prop_i)->get_property_name()) {
566 CdlProperty_TclCode tclprop = dynamic_cast<CdlProperty_TclCode>(*prop_i);
567 cdl_int its_num = tclprop->get_number();
568 if (!result_set || (its_num < result_num)) {
569 result = tclprop->get_code();
570 result_num = its_num;
580 CdlWizardBody::get_screen(cdl_int screen) const
582 CYG_REPORT_FUNCNAME("CdlWizard::get_screen");
583 CYG_REPORT_FUNCARG2XV(this, (int)screen);
584 CYG_PRECONDITION_THISC();
586 static cdl_tcl_code null_result = "";
587 cdl_tcl_code& result = null_result;
589 const std::vector<CdlProperty>& properties = get_properties();
590 std::vector<CdlProperty>::const_iterator prop_i;
591 for (prop_i = properties.begin(); prop_i != properties.end(); prop_i++) {
592 if (CdlPropertyId_Screen != (*prop_i)->get_property_name()) {
595 CdlProperty_TclCode tclprop = dynamic_cast<CdlProperty_TclCode>(*prop_i);
596 if (tclprop->get_number() == screen) {
597 result = tclprop->get_code();
609 // ----------------------------------------------------------------------------
610 // check_this(). There is very little data associated with a wizard,
611 // most of the checks happen in the base classes.
613 CdlWizardBody::check_this(cyg_assert_class_zeal zeal) const
615 if (CdlWizardBody_Magic != cdlwizardbody_cookie) {
618 CYGDBG_MEMLEAK_CHECKTHIS();
619 return CdlUserVisibleBody::check_this(zeal);
625 // ----------------------------------------------------------------------------
628 CdlWizardBody::get_class_name() const
630 CYG_REPORT_FUNCNAME("CdlWizard::get_class_name");
631 CYG_PRECONDITION_THISC();