source: SVN/cambria/redboot/host/libcdl/option.cxx @ 1

Last change on this file since 1 was 1, checked in by Tim Harvey, 2 years ago

restored latest version of files from server backup

Signed-off-by: Tim Harvey <tharvey@…>

File size: 14.5 KB
Line 
1//{{{  Banner                           
2
3//============================================================================
4//
5//     option.cxx
6//
7//     Implementation of the CdlOption 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// ----------------------------------------------------------------------------
67CYGDBG_DEFINE_MEMLEAK_COUNTER(CdlOptionBody);
68
69//}}}
70//{{{  Constructor                     
71
72// ----------------------------------------------------------------------------
73CdlOptionBody::CdlOptionBody(std::string name_arg)
74    : CdlNodeBody(name_arg),
75      CdlUserVisibleBody(),
76      CdlValuableBody(),
77      CdlParentableBody(),
78      CdlBuildableBody(),
79      CdlDefinableBody()
80{
81    CYG_REPORT_FUNCNAME("CdlOptionBody:: constructor");
82    CYG_REPORT_FUNCARG1XV(this);
83
84    cdloptionbody_cookie = CdlOptionBody_Magic;
85    CYGDBG_MEMLEAK_CONSTRUCTOR();
86
87    CYG_POSTCONDITION_THISC();
88    CYG_REPORT_RETURN();
89}
90
91//}}}
92//{{{  Destructor                       
93
94// ----------------------------------------------------------------------------
95
96CdlOptionBody::~CdlOptionBody()
97{
98    CYG_REPORT_FUNCNAME("CdlOptionBody:: destructor");
99    CYG_REPORT_FUNCARG1XV(this);
100    CYG_PRECONDITION_THISC();
101
102    cdloptionbody_cookie = CdlOptionBody_Invalid;
103    CYGDBG_MEMLEAK_DESTRUCTOR();
104   
105    CYG_REPORT_RETURN();
106}
107
108//}}}
109//{{{  parse_option()                   
110
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".
115
116int
117CdlOptionBody::parse_option(CdlInterpreter interp, int argc, const char* argv[])
118{
119    CYG_REPORT_FUNCNAMETYPE("CdlOptionBody::parse_option", "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 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;
141    bool         ok             = true;
142    int          result         = TCL_OK;
143    try {
144   
145        // Currently there are no command-line 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        } 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] + "'.");
154            ok = false;
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.");
158            ok = false;
159        } else {
160            new_option = new CdlOptionBody(argv[1]);
161            toplevel->add_node(package, parent, new_option);
162        }
163
164        if (!ok) {
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);
168            return TCL_OK;
169        }
170    } catch(std::bad_alloc e) {
171        interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Out of memory"));
172        result = TCL_ERROR;
173    } catch(CdlParseException e) {
174        interp->set_result(e.get_message());
175        result = TCL_ERROR;
176    } catch(...) {
177        interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Unexpected C++ exception"));
178        result = TCL_ERROR;
179    }
180    if (TCL_OK != result) {
181        CYG_REPORT_RETVAL(result);
182        return result;
183    }
184
185    // At this stage new_option has been created and added to the hierarchy.
186    // The main work now is to add the properties.
187   
188    // Push the option as the current node early on. This aids
189    // diagnostics.
190    CdlNode old_node = interp->push_node(new_option);
191
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[] =
198    {
199        CdlInterpreterCommandEntry("", 0 )
200    };
201    int i;
202   
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.
205    try {
206
207        for (i = 0; 0 != commands[i].command; i++) {
208            new_commands.push_back(commands[i]);
209        }
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);
216   
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);
224       
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);
235        }
236       
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,
240        // just give up.
241        interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Out of memory"));
242        result = TCL_ERROR;
243    } catch (CdlParseException e) {
244        interp->set_result(e.get_message());
245        result = TCL_ERROR;
246    } catch(...) {
247        interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Unexpected C++ exception"));
248        result = TCL_ERROR;
249    }
250
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);
255    }
256   
257    CYG_REPORT_RETVAL(result);
258    return result;
259}
260
261//}}}
262//{{{  Persistence support             
263
264// ----------------------------------------------------------------------------
265void
266CdlOptionBody::initialize_savefile_support(CdlToplevel toplevel)
267{
268    CYG_REPORT_FUNCNAME("CdlOption::initialize_savefile_support");
269
270    toplevel->add_savefile_command("cdl_option", 0, &savefile_option_command);
271    CdlValuableBody::initialize_savefile_support(toplevel, "cdl_option");
272
273    CYG_REPORT_RETURN();
274}
275
276void
277CdlOptionBody::save(CdlInterpreter interp, Tcl_Channel chan, int indentation, bool minimal)
278{
279    CYG_REPORT_FUNCNAME("CdlOption::save");
280    CYG_REPORT_FUNCARG5XV(this, interp, chan, indentation, minimal);
281    CYG_PRECONDITION_THISC();
282    CYG_PRECONDITION_CLASSC(interp);
283
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);
288
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);
294
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);
299
300        // And with any unrecognised data
301        this->CdlNodeBody::save(interp, chan, indentation + 4, minimal);
302   
303        // Close the cdl_option body. A blank line is added here.
304        interp->write_data(chan, "};\n\n");
305    }
306   
307    CYG_REPORT_RETURN();
308}
309
310int
311CdlOptionBody::savefile_option_command(CdlInterpreter interp, int argc, const char* argv[])
312{
313    CYG_REPORT_FUNCNAMETYPE("CdlOption::savefile_option_command", "result %d");
314    CYG_PRECONDITION_CLASSC(interp);
315
316    int result = TCL_OK;
317    CdlToplevel toplevel = interp->get_toplevel();
318    CYG_ASSERT_CLASSC(toplevel);
319    CdlConfiguration config = dynamic_cast<CdlConfiguration>(toplevel);
320    CYG_ASSERT_CLASSC(config);
321
322    std::vector<CdlInterpreterCommandEntry> subcommands;
323    std::vector<CdlInterpreterCommandEntry>* toplevel_commands = 0;
324    CdlNode old_node = 0;
325   
326    try {
327       
328        if (3 != argc) {
329            CdlParse::report_error(interp, "", "Invalid cdl_option command in savefile, expecting two arguments.");
330        } else {
331
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 `")
337                                       + argv[1] + "'");
338            } else {
339                config->get_savefile_subcommands("cdl_option", subcommands);
340                toplevel_commands = interp->push_commands(subcommands);
341                old_node = interp->push_node(current_node);
342               
343                std::string tcl_result;
344                result = interp->eval(argv[2], tcl_result);
345           
346                interp->pop_commands(toplevel_commands);
347                toplevel_commands = 0;
348                interp->pop_node(old_node);
349                old_node = 0;
350            }
351        }
352    } catch(...) {
353        if (0 != old_node) {
354            interp->pop_node(old_node);
355        }
356        if (0 != toplevel_commands) {
357            interp->pop_commands(toplevel_commands);
358        }
359        throw;
360    }
361
362    CYG_REPORT_RETVAL(result);
363    return result;
364}
365
366//}}}
367//{{{  check_this()                     
368
369// ----------------------------------------------------------------------------
370bool
371CdlOptionBody::check_this(cyg_assert_class_zeal zeal) const
372{
373    if (CdlOptionBody_Magic != cdloptionbody_cookie) {
374        return false;
375    }
376    CYGDBG_MEMLEAK_CHECKTHIS();
377       
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);
384}
385
386//}}}
387//{{{  misc                             
388
389// ----------------------------------------------------------------------------
390
391std::string
392CdlOptionBody::get_class_name() const
393{
394    CYG_REPORT_FUNCNAME("CdlOption::get_class_name");
395    CYG_PRECONDITION_THISC();
396    CYG_REPORT_RETURN();
397    return "option";
398}
399
400//}}}
Note: See TracBrowser for help on using the repository browser.