00001 #ifdef _USE_WXWIDGETS_
00002
00003
00004
00005
00006 #include "bbtkObject.h"
00007 #include "bbtkInterpreter.h"
00008 #include "bbtkWxBlackBox.h"
00009 #include "bbtkWxGUIConsole.h"
00010
00011 #include <wx/cmdline.h>
00012 #include <vector>
00013 #include <map>
00014
00015
00016
00017 static const wxCmdLineEntryDesc cmdLineDesc[] =
00018 {
00019 { wxCMD_LINE_SWITCH, _T("h"), _T("help"), _T("print this help or help on the application defined in input bbs file if any") },
00020 { wxCMD_LINE_SWITCH, _T("g"), _T("graphical-dialog"), _T("prompt the input parameter values using graphical dialog") },
00021 { wxCMD_LINE_SWITCH, _T("t"), _T("text-dialog"), _T("prompt the input parameter values in text mode") },
00022 { wxCMD_LINE_SWITCH, _T("c"), _T("console"), _T("open bbi console") },
00023 { wxCMD_LINE_SWITCH, _T("N"), _T("no-console"), _T("never open bbi console even on error") },
00024 { wxCMD_LINE_SWITCH, _T("q"), _T("quiet"), _T("be quiet (='message max 0')") },
00025 { wxCMD_LINE_SWITCH, _T("d"), _T("debug"), _T("turn all messages on (='message all 9')") },
00026 { wxCMD_LINE_SWITCH, _T("D"), _T("Debug"), _T("memory debug on exit (='debug -D')") },
00027 { wxCMD_LINE_PARAM, NULL, NULL, _T("file [file [...]]"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
00028 { wxCMD_LINE_NONE }
00029 };
00030
00031
00032
00033
00034 struct ProcessCmdLine
00035 {
00036 ProcessCmdLine() {}
00037 void Process(wxCmdLineParser& parser);
00038
00039
00040 std::vector<std::string> argv;
00041 bool console;
00042 bool debug;
00043 bool quiet;
00044 bool help;
00045 bool graphical_dialog;
00046 bool text_dialog;
00047 bool no_console;
00048 bool proceed;
00049 std::map<std::string,std::string> param_map;
00050 std::vector<std::string> input_file;
00051
00052 };
00053
00054
00055
00056 void ProcessCmdLine::Process(wxCmdLineParser& parser)
00057 {
00058 proceed = true;
00059 if (parser.Found(_T("D")))
00060 {
00061 bbtk::StaticInitTime::PrintObjectListInfo = true;
00062 }
00063
00064 debug = ( parser.Found(_T("d")) );
00065 quiet = ( parser.Found(_T("q")) );
00066 help = ( parser.Found(_T("h")) );
00067 graphical_dialog = ( parser.Found(_T("g")) );
00068 text_dialog = ( parser.Found(_T("t")) );
00069 no_console = ( parser.Found(_T("N")) );
00070
00071 if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
00072 if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
00073
00074
00075
00076 int pargc = parser.GetParamCount();
00077 for (int i=0; i<pargc; ++i)
00078 {
00079 std::string s = bbtk::wx2std(parser.GetParam(i));
00080 std::string::size_type pos = s.find_first_of("=");
00081 if (std::string::npos != pos)
00082 {
00083 std::string left = s.substr(0,pos);
00084 std::string right = s.substr(pos+1,s.size());
00085 param_map[left]=right;
00086 }
00087 else
00088 {
00089 input_file.push_back(s);
00090 }
00091 }
00092
00093 bool usage = (help && (input_file.size()==0));
00094 if (usage) {
00095 std::cout << "BBI (The Black Box Interpreter) - bbtk "
00096 << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
00097 << std::endl;
00098 parser.Usage();
00099 proceed = false;
00100 }
00101
00102 console = ( parser.Found(_T("c")) ||
00103 ((input_file.size() == 0) &&
00104 (!no_console) &&
00105 (!usage) ) );
00106
00107 }
00108
00109
00110
00111
00112 class wxBBIApp : public wxApp
00113 {
00114 public:
00115 bool OnInit( );
00116 int OnExit() {
00117
00118
00119 return true; }
00120 void OnInitCmdLine(wxCmdLineParser& parser);
00121 bool OnCmdLineParsed(wxCmdLineParser& parser);
00122
00123 ProcessCmdLine cmd;
00124 };
00125
00126
00127
00128
00129 void wxBBIApp::OnInitCmdLine(wxCmdLineParser& parser)
00130 {
00131 parser.SetDesc(cmdLineDesc);
00132 }
00133
00134
00135
00136 bool wxBBIApp::OnCmdLineParsed(wxCmdLineParser& parser)
00137 {
00138 cmd.Process(parser);
00139
00140 return true;
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 bool wxBBIApp::OnInit( )
00153 {
00154
00155 wxApp::OnInit();
00156 #ifdef __WXGTK__
00157
00158 setlocale(LC_NUMERIC, "C");
00159 #endif
00160
00161
00162 if (cmd.quiet) bbtk::MessageManager::SetMessageLevel("max",0);
00163 if (cmd.debug) bbtk::MessageManager::SetMessageLevel("all",9);
00164
00165
00166 bbtk::WxGUIConsole *I = new bbtk::WxGUIConsole(0,_T("bbi"),wxSize(800,600));
00167 SetTopWindow(I);
00168 if (cmd.console) I->Show(true);
00169
00170
00171 I->SetInputs(cmd.param_map);
00172
00173 bool help_on_script = cmd.help && (cmd.input_file.size() > 0);
00174 if (help_on_script)
00175 I->SetNoExecMode(true);
00176 if (cmd.graphical_dialog)
00177 I->SetDialogMode(bbtk::VirtualExec::GraphicalDialog);
00178 if (cmd.text_dialog)
00179 I->SetDialogMode(bbtk::VirtualExec::TextDialog);
00180
00181 std::vector<std::string>::const_iterator i;
00182 bool error = false;
00183
00184 for (i=cmd.input_file.begin(); i!=cmd.input_file.end(); ++i)
00185 {
00186 error = ! I->InterpretFile(*i);
00187 if (error) break;
00188 }
00189 bool show_on_error = error && ! cmd.no_console;
00190 if (show_on_error) I->Show();
00191
00192 I->SetNoExecMode(false);
00193
00194 if (help_on_script)
00195 {
00196 std::string package;
00197 I->GetInterpreter()->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",package,false);
00198 }
00199
00200
00201
00202
00203
00204
00205 if (!(show_on_error || cmd.console || bbtk::Wx::IsSomeWindowAlive() ))
00206 {
00207 I->Close();
00208
00209 }
00210 else
00211 {
00212
00213 }
00214
00215
00216
00217 return true;
00218
00219 }
00220
00221
00222
00223 #if defined(_WIN32)
00224
00225
00226
00227 IMPLEMENT_APP(wxBBIApp);
00228
00229
00230
00231
00232
00233
00234 int main(int argc, char* argv[])
00235 {
00236 return WinMain(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), SW_SHOWNORMAL);
00237 }
00238
00239 #else
00240
00241
00242
00243
00244 IMPLEMENT_APP_NO_MAIN(wxBBIApp);
00245
00246
00247 int main(int argc, char* argv[])
00248 {
00249 wxMessageOutput::Set( new wxMessageOutputBest );
00250
00251 wxCmdLineParser parser(cmdLineDesc,argc,argv);
00252 int val = parser.Parse(false);
00253 if (val>0)
00254 {
00255 parser.Usage();
00256 return 0;
00257 }
00258 ProcessCmdLine cmdline;
00259 cmdline.Process(parser);
00260
00261 if (!cmdline.proceed) return 0;
00262
00263 if (cmdline.no_console)
00264 {
00265
00266
00267 bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
00268 I->SetInputs(cmdline.param_map);
00269 bool help_on_script = cmdline.help && (cmdline.input_file.size() > 0);
00270 if (help_on_script) I->SetNoExecMode(true);
00271 if (cmdline.text_dialog) I->SetDialogMode(bbtk::VirtualExec::TextDialog);
00272 std::vector<std::string>::const_iterator i;
00273 bool error = false;
00274 for (i=cmdline.input_file.begin();
00275 i!=cmdline.input_file.end(); ++i)
00276 {
00277 error = ! I->InterpretFile(*i);
00278 if (error) break;
00279 }
00280 if (help_on_script)
00281 {
00282 I->SetNoExecMode(false);
00283 std::string package;
00284 I->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",
00285 package,
00286 false);
00287 }
00288 if (cmdline.input_file.size()==0)
00289 I->CommandLineInterpreter();
00290
00291
00292 }
00293 else
00294 {
00295 wxEntry(argc, argv);
00296 }
00297
00298 }
00299
00300
00301 #endif // defined(_WIN32)
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311 #else
00312
00313
00314
00315
00316 #include "bbtkInterpreter.h"
00317
00318
00319 int main(int argc, char* argv[])
00320 {
00321 if (argc>2)
00322 {
00323 std::cout << "usage : "<<argv[0]<<" [filename]"<<std::endl;
00324 return 0;
00325 }
00326
00327 std::cout << "BBI (Black Box Interpreter) - bbtk "
00328 << bbtk::GetVersion()<< " - (c) Creatis 2007"
00329 << std::endl;
00330
00331 bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
00332 if (argc==1)
00333 {
00334 I->CommandLineInterpreter();
00335 }
00336 else
00337
00338 {
00339 std::string f(argv[1]);
00340 I->InterpretFile(f);
00341 }
00342
00343
00344
00345 return 0;
00346
00347 }
00348
00349
00350 #endif //#ifdef _USE_WXWIDGETS_
00351
00352
00353