1 /*=========================================================================
4 Module: $RCSfile: bbtkInterpreter.cxx,v $ $
6 Date: $Date: 2008/01/29 14:31:02 $
7 Version: $Revision: 1.13 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
20 * \brief class Interpreter :
23 #include "bbtkInterpreter.h"
24 #include "bbtkMessageManager.h"
25 #include "bbtkConfigurationFile.h"
26 #include "bbtkWxConsole.h"
27 #include "bbtkUtilities.h"
29 #ifdef CMAKE_HAVE_TERMIOS_H
31 #define BBTK_USE_TERMIOS_BASED_PROMPT
39 Interpreter* Interpreter::mGlobalInterpreter = NULL;
41 //=======================================================================
45 Interpreter::Interpreter()
47 mCommandLine(false), verbose(false)
49 bbtk::MessageManager::RegisterMessageType("Echo","Level>0 : Prints the 'echo' commands of the user.\n\tLevel>1 : Prints the command being interpreted",1);
50 bbtk::MessageManager::RegisterMessageType("Interpreter","Messages of the interpreter",0);
51 bbtkDebugMessageInc("Interpreter",9,"Interpreter::Interpreter()" <<std::endl);
53 mGlobalInterpreter = this;
55 // mFactory = new bbtk::Factory();
56 mExecuter = new bbtk::Executer();
57 //mExecuter->SetFactory(mFactory);
59 // Builds the commands dict
66 info.syntax = "new <type> <name>";
67 info.help = "Creates a new black box of type <type> with name <name>";
68 mCommandDict[info.keyword] = info;
70 info.keyword = "delete";
74 info.syntax = "delete <box>";
75 info.help = "Deletes the black box of name <box>";
76 mCommandDict[info.keyword] = info;
78 info.keyword = "connect";
82 info.syntax = "connect <box1.output> <box2.input>";
83 info.help = "Connects the ouput <output> of black box <box1> to the input <input> of black box <box2>";
84 mCommandDict[info.keyword] = info;
86 info.keyword = "print";
90 info.syntax = "print <string>";
91 info.help = "Prints the string. Substitutes any token of the form '$box.output$' by the string adaptation of the output of the box (requires the right adaptor). No carriage return is issued at the end, use '\\n' to add carriage returns. The level of 'Echo' messages must be greater than 1 (see the command 'message').";
92 mCommandDict[info.keyword] = info;
94 info.keyword = "exec";
98 info.syntax = "exec <box | 'freeze' | 'unfreeze' >";
99 info.help = "Executes the black box of name <box> (and connected boxes if needed). If the special keyword 'freeze' is given then freezes any further execution command. 'unfreeze' reverts to normal execution mode.";
100 mCommandDict[info.keyword] = info;
102 info.keyword = "package";
105 info.code = cPackage;
106 info.syntax = "package <name>";
107 info.help = "Begins the definition of a package.";
108 mCommandDict[info.keyword] = info;
110 info.keyword = "endpackage";
113 info.code = cEndPackage;
114 info.syntax = "endpackage";
115 info.help = "Ends the definition of a package.";
116 mCommandDict[info.keyword] = info;
118 info.keyword = "define";
122 info.syntax = "define <type> [<package>]";
123 info.help = "Begins the definition of a new type of complex black box called <type>. If <package> if provided will create it in the given package.";
124 mCommandDict[info.keyword] = info;
126 info.keyword = "endefine";
129 info.code = cEndDefine;
130 info.syntax = "endefine";
131 info.help = "Ends the definition of a new type of complex black box";
132 mCommandDict[info.keyword] = info;
134 info.keyword = "input";
138 info.syntax = "input <name> <box.input> <help>";
139 info.help = "Defines the input <name> of the current working black box as being an alias for the input <input> of the black box <box>. <help> defines the help string for the newly created input";
140 mCommandDict[info.keyword] = info;
142 info.keyword = "output";
146 info.syntax = "output <name> <box.output> <help>";
147 info.help = "Defines the output <name> of the current working black box as being an alias for the output <output> of the black box <box>. <help> defines the help string for the newly created output";
148 mCommandDict[info.keyword] = info;
150 info.keyword = "set";
154 info.syntax = "set <box.input> <value>";
155 info.help = "Sets the value of the input <input> of the black box <box> to <value>. There must exist a string to the value type adaptor";
156 mCommandDict[info.keyword] = info;
158 info.keyword = "config"; // JPR
162 info.syntax = "config [<verbose>|<v>]";
163 info.help = "Prints the value of all configuration parameters";
164 mCommandDict[info.keyword] = info;
166 info.keyword = "reset"; //EED
170 info.syntax = "reset";
171 info.help = "Deletes all boxes and unloads all packages (bbi is reset to its start state)";
172 mCommandDict[info.keyword] = info;
174 info.keyword = "author";
178 info.syntax = "author <string>";
179 info.help = "Adds the string <string> to the author information of the black box being defined";
180 mCommandDict[info.keyword] = info;
182 info.keyword = "description";
185 info.code = cDescription;
186 info.syntax = "description <string>";
187 info.help = "Adds the string <string> to the descriptive information of the black box being defined";
188 mCommandDict[info.keyword] = info;
190 info.keyword = "help";
194 info.syntax = "help";
195 info.syntax = "\n (1) help \n (2) help <command name> \n (3) help packages [all]\n (4) help <package name> [all]\n (5) help <black box type> \n (6) help <black box name>";
196 info.help = "Effect :\n (1) Lists all available commands;\n (2) Prints help on a particular command; \n (3) Lists the packages loaded and their black boxes.\n Add 'all' to list adaptors; \n (4) Prints short help on the black boxes of a package.\n Add 'all' to include adaptors; \n (5) Prints full help on a black box type; \n (6) Prints information on the inputs, outputs and connections of a black box instance.";
197 mCommandDict[info.keyword] = info;
199 info.keyword = "message";
202 info.code = cMessage;
203 info.syntax = "message <category> <level>";
204 info.help = "Sets the level of the category of messages <category> to <level>.\n If category='All' then sets the level for all categories. If no category nor level is passed then prints info on available categories of messages and their current level.";
205 mCommandDict[info.keyword] = info;
207 info.keyword = "include";
210 info.code = cInclude;
211 info.syntax = "include <filename>";
212 info.help = "Includes the file <filename>";
213 mCommandDict[info.keyword] = info;
215 info.keyword = "quit";
219 info.syntax = "quit";
220 info.help = "Quits the program (during script execution it stops the complete execution)";
221 mCommandDict[info.keyword] = info;
223 info.keyword = "load";
227 info.syntax = "load <packagename>";
228 info.help = "Loads the black box package <packagename>";
229 mCommandDict[info.keyword] = info;
231 info.keyword = "unload";
235 info.syntax = "unload <packagename>";
236 info.help = "Unloads the black box package <packagename>";
237 mCommandDict[info.keyword] = info;
239 info.keyword = "graph";
243 info.syntax = "graph [ BlackBoxName [ Detail 0..1 [ Level 0..99999 [ Output html file [ Custom header [ Custom title ]]]]]] \n graph [ BlackBoxNameType [ Detail 0..1 [ Level 0..99999 [ Output html file [ Custom header [ Custom title ]]]]]]";
244 info.help = "Shows a graphical view of a bbtk pipeline.\n- BlackBoxName : name of the box to view. Default '.' : current box.\n- BlackBoxNameType : name of the type of box to view, ex : 'workspace')";
245 mCommandDict[info.keyword] = info;
248 info.keyword = "workspace";
251 info.code = cWorkspace;
252 info.syntax = "workspace < ( freeze | unfreeze ) | ( rename <newname> ) >";
253 info.help = "Configures the workspace.\n 'freeze' allow to block execution commands while keeping definition commands active. 'unfreeze' turns back the worspace in 'normal' mode.\n 'rename' allow to set a new name to the workspace.";
254 mCommandDict[info.keyword] = info;
257 bbtkDebugDecTab("Interpreter",9);
260 //=======================================================================
264 //=======================================================================
268 Interpreter::~Interpreter()
270 bbtkDebugMessageInc("Interpreter",9,"Interpreter::~Interpreter()" <<std::endl);
274 // std::cout <<"EO Interpreter::~Interpreter()"<<std::endl;
275 bbtkDebugDecTab("Interpreter",9);
277 //=======================================================================
280 //=======================================================================
284 void Interpreter::InterpretFile( const std::string& filename, bool use_configuration_file, bool verbose)
286 bbtkDebugMessageInc("Interpreter",9,"Interpreter::InterpretFile(\""<<filename<<"\")"<<std::endl);
288 bool exm = mCommandLine;
289 mCommandLine = false;
293 SwitchToFile(filename, use_configuration_file, verbose);
294 bool insideComment = false; // for multiline comment
295 while (mFile.size()>0)
297 while ((mFile.size()>0) &&
298 (!mFile.back()->eof()))
302 mFile.back()->getline(buf,500);
304 std::string str(buf);
305 int size=str.length();
306 if ( str[ size-1 ]==13 )
311 InterpretLine(str, insideComment);
313 //if (mFile.size()>0)
317 catch (QuitException e)
320 catch (bbtk::Exception e)
322 std::cerr << "* ERROR : "<<e.GetMessage()<<std::endl;
323 if (mFileName.size()) {
324 std::cerr << "* FILE : \""<<mFileName.back()<<"\""<<std::endl;
325 std::cerr << "* LINE : "<<mLine.back()<<std::endl;
328 catch (std::exception& e)
330 std::cerr << "* ERROR : "<<e.what()<<" (not in bbtk)"<<std::endl;
331 if (mFileName.size()) {
332 std::cerr << "* FILE : \""<<mFileName.back()<<"\""<<std::endl;
333 std::cerr << "* LINE : "<<mLine.back()<<std::endl;
338 std::cout << "* UNDEFINED ERROR (not a bbtk nor a std exception)"<<std::endl;
339 if (mFileName.size()) {
340 std::cout << "* FILE : \""<<mFileName.back()<<"\""<<std::endl;
341 std::cout << "* LINE : "<<mLine.back()<<std::endl;
346 bbtkDebugMessage("Interpreter",9,"EO Interpreter::InterpretFile(\""<<filename<<"\")"<<std::endl);
347 bbtkDecTab("Interpreter",9);
351 //=======================================================================
355 //=======================================================================
359 void Interpreter::InterpretLine( const std::string& line, bool &insideComment )
362 bbtkDebugMessageInc("Interpreter",9,"Interpreter::InterpretLine(\""<<line<<"\")"<<std::endl);
363 bbtkMessage("Echo",2,"\""<<line<<"\""<<std::endl);
365 std::vector<std::string> words;
366 SplitLine(line,words);
371 bbtkDebugDecTab("Interpreter",9);
375 // Single line comment : # or //
376 if ( words[0][0]=='#' || (words[0][0]=='/' && words[0][1]=='/') )
378 bbtkDebugDecTab("Interpreter",9);
379 bbtkMessage("Interpreter",9,"Comment"<<std::endl);
383 // Multi line comment ( /* ... */ ) -delimiters on different lines !-
385 if (words[0][0]=='/' && words[0][1]=='*')
387 bbtkDebugDecTab("Interpreter",9);
388 bbtkMessage("Interpreter",9,"In multiline comment"<<std::endl);
389 insideComment = true;
393 if (words[0][0]=='*' && words[0][1]=='/')
395 bbtkDebugDecTab("Interpreter",9);
396 bbtkMessage("Interpreter",9,"Out multiline comment"<<std::endl);
397 if ( !insideComment ) {
398 bbtkDebugDecTab("Interpreter",9);
399 bbtkMessage("Interpreter",9,"Comment mismatch : '*/' with no matching '/*'"<<std::endl);
401 insideComment = false;
407 bbtkDebugDecTab("Interpreter",9);
408 bbtkMessage("Interpreter",9,"Multiline Comment"<<std::endl);
413 CommandInfoType command;
414 InterpretCommand(words,command);
416 bbtkDebugMessage("Interpreter",9,
417 "Command='"<<command.keyword
418 <<"' code="<<command.code<<std::endl);
420 std::string left,right,left2,right2;
421 std::string filename;
422 switch (command.code)
425 mExecuter->Create(words[1],words[2]);
430 // mExecuter->Remove(words[1]);
434 Utilities::SplitAroundFirstDot(words[1],left,right);
435 Utilities::SplitAroundFirstDot(words[2],left2,right2);
436 mExecuter->Connect(left,right,left2,right2);
440 mExecuter->BeginPackage(words[1]);
444 mExecuter->EndPackage();
448 if (mFileName.size()>0)
450 filename = Utilities::get_file_name(mFileName.back());
454 mExecuter->Define(words[1],"",filename);
458 mExecuter->Define(words[1],words[2],filename);
463 mExecuter->EndDefine();
467 Print(words[1]); /// \todo use mExecuter
471 if (words[1]=="freeze")
472 mExecuter->SetNoExecMode(true);
473 else if (words[1]=="unfreeze")
474 mExecuter->SetNoExecMode(false);
476 mExecuter->Update(words[1]);
480 Utilities::SplitAroundFirstDot(words[2],left,right);
481 mExecuter->DefineInput(words[1],left,right,words[3]);
485 Utilities::SplitAroundFirstDot(words[2],left,right);
486 mExecuter->DefineOutput(words[1],left,right,words[3]);
490 Utilities::SplitAroundFirstDot(words[1],left,right);
491 mExecuter->Set(left,right,words[2]);
495 mExecuter->Author(words[1]);
499 mExecuter->Description(words[1]);
509 bbtk::MessageManager::PrintInfo();
513 sscanf(words[2].c_str(),"%d",&level);
514 bbtk::MessageManager::SetMessageLevel(words[1],level);
523 if (words.size()>1) // any param for config means verbose = true
531 this->mExecuter->Reset();
537 InterpretFile(words[1], true, verbose); // true : better pass use_config_file
541 SwitchToFile(words[1], true, verbose); // true : better pass use_config_file
546 LoadPackage(words[1], true, verbose); // true : better pass use_config_file
550 UnLoadPackage(words[1]);
554 throw QuitException();
558 if (words.size() == 2)
560 if (words[1]=="freeze") mExecuter->SetNoExecMode(true);
561 else if (words[1]=="unfreeze") mExecuter->SetNoExecMode(false);
565 mExecuter->SetWorkspaceName(words[2]);
570 bbtkInternalError("should not reach here !!!");
573 bbtkDecTab("Interpreter",9);
575 //=======================================================================
579 //=======================================================================
580 void SplitString ( const std::string& str, const std::string& delimiters,
581 std::vector<std::string>& tokens)
583 // Skip delimiters at beginning.
584 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
585 // Find first delimiter.
586 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
588 while (std::string::npos != pos || std::string::npos != lastPos)
590 // Found a token, add it to the vector.
591 tokens.push_back(str.substr(lastPos, pos - lastPos));
592 // Skip delimiters. Note the "not_of"
593 lastPos = str.find_first_not_of(delimiters, pos);
594 // Find next delimiter
595 pos = str.find_first_of(delimiters, lastPos);
599 //=======================================================================
603 //=======================================================================
607 void Interpreter::SplitLine ( const std::string& str, std::vector<std::string>& tokens)
609 bbtkDebugMessageInc("Interpreter",9,"Interpreter::SplitLine(\""<<str<<"\")"<<std::endl);
611 std::string delimiters = "\"";
612 std::vector<std::string> quote;
613 SplitString(str,delimiters,quote);
616 std::vector<std::string>::iterator i;
617 for (i=quote.begin(); i!=quote.end(); )
619 SplitString(*i,delimiters,tokens);
623 // bbtkDebugMessage("Interpreter",0,"\""<<*i<<"\""<<std::endl);
624 tokens.push_back(*i);
629 for (i=tokens.begin(); i!=tokens.end(); ++i)
631 bbtkDebugMessage("Interpreter",9,"["<<*i<<"] ");
633 bbtkDebugMessageCont("Interpreter",9,std::endl);
635 bbtkDebugDecTab("Interpreter",9);
637 //=======================================================================
640 //=======================================================================
641 // Replaces substrings "\\n" by a real carriage return "\n"
642 void SubsBackslashN ( std::string& s )
644 // std::cout << "BEFORE=["<<s<<"]"<<std::endl;
645 std::string ss("\\n");
646 std::string::size_type pos = 0;
649 while ( pos != std::string::npos )
651 // std::cout << "*** find one "<<std::endl;
652 s.replace(pos,2,cr,1);
653 pos = s.find(ss, pos-1);
655 // std::cout << "AFTER=["<<s<<"]"<<std::endl;
657 //=======================================================================
660 //=======================================================================
664 void Interpreter::Print( const std::string& str)
666 if (mExecuter->GetNoExecMode()) return;
668 bbtkDebugMessageInc("Interpreter",9,"Interpreter::SplitLine(\""<<str<<"\")"<<std::endl);
670 std::vector<std::string> chains;
671 std::string delimiters("$");
673 // Skip delimiters at beginning.
674 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
676 if (lastPos>0) is_text = false;
678 // Find first delimiter.
679 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
681 while (std::string::npos != pos || std::string::npos != lastPos)
685 // Found a text token, add it to the vector.
686 chains.push_back(str.substr(lastPos, pos - lastPos));
687 // std::cout << "text='"<<chains.back()<<"'"<<std::endl;
691 // is an output (between $$) : decode
692 std::string tok,box,output;
693 tok = str.substr(lastPos, pos - lastPos);
694 Utilities::SplitAroundFirstDot(tok,box,output);
695 chains.push_back( mExecuter->Get(box,output) );
696 // std::cout << "outp='"<<chains.back()<<"'"<<std::endl;
698 // Skip delimiters. Note the "not_of"
699 lastPos = str.find_first_not_of(delimiters, pos);
700 // Find next delimiter
701 pos = str.find_first_of(delimiters, lastPos);
705 // std::cout << "nb="<<chains.size()<<std::endl;
706 std::vector<std::string>::iterator i;
707 for (i= chains.begin(); i!=chains.end(); ++i)
709 // bbtkMessage("Echo",1,*i);
713 std::cout << std::endl;
714 bbtkDebugDecTab("Interpreter",9);
717 //=======================================================================
722 // ===================================================================================
724 void Interpreter::SwitchToFile( const std::string& name,
725 bool use_configuration_file, bool verbose)
727 // Note : in the following :
728 // name : the user supplied name
729 // - abreviated name e.g. scr scr.bbs
730 // - relative full name e.g. ./scr.bbs ../../scr.bbs
731 // - absolute full name e.g. /home/usrname/proj/dir/scr.bbs
732 // same for Windows, with c:, d: ...
734 // expression like directory/subdir/scrname.bbs is FORBIDDEN (*)
735 // use ./directory/subdir/scrname.bbs
737 // (*) except when using packagename/boxes/*
739 bbtkDebugMessageInc("Interpreter",9,"Interpreter::SwitchToFile( \""
740 <<name<<"\")"<<std::endl);
742 // to be remove in final version
745 std::vector<std::string> script_paths;
746 std::string fullPathScriptName; // full path script name
747 std::string pkgname; // e.g. <scriptname>.bbs
749 if (use_configuration_file)
751 // The following is *NOT* a debug time message :
752 // It's a user intended message.
753 // Please don't remove it.
755 std::cout << "look for : [" << name << "] (use_configuration_file == TRUE)" << std::endl;
756 script_paths = ConfigurationFile::GetInstance().Get_bbs_paths();
759 pkgname = Utilities::ExtractScriptName(name,upath);
761 bool fullnameGiven = false;
762 bool foundFile = false;
764 if(pkgname == "*") // =========================================== load all boxes (e.g. std/boxes/*)
767 std::vector<std::string>::iterator i;
768 std::string fullDirectoryName;
769 for (i=script_paths.begin();i!=script_paths.end();++i)
773 // we *really* want '.' to be the current working directory
775 char buf[2048]; // for getcwd
776 char * currentDir = getcwd(buf, 2048);
777 std::string cwd(currentDir);
781 fullDirectoryName = Utilities::MakePkgnameFromPath(path, upath, false);
782 //std::cout <<"fullpath [" << fullDirectoryName << "]" <<std::endl;
783 // Check if library exists
784 if ( ! Utilities::IsDirectory(fullDirectoryName) )
786 // The following is *NOT* a debug time message :
787 // It's a user intended message.
788 // Please don't remove it.
790 std::cout <<" [" <<fullDirectoryName <<"] : doesn't exist" <<std::endl;
791 continue; // try next path
795 std::vector<std::string> Filenames;
796 int nbFiles = Utilities::Explore(fullDirectoryName, false, Filenames);
797 // std::cout << "=================nbFiles " << nbFiles << std::endl;
799 for (std::vector<std::string>::iterator i = Filenames.begin(); i!=Filenames.end(); ++i)
801 if ((*i).substr((*i).size()-4, 4) != ".bbs")
802 continue; // ignore non .bbs files
808 std::cout << "WARNING : No '.bbs' file found in [" << fullDirectoryName << "]" << std::endl;
810 break; // a directory was found; we stop iterating
815 std::string::size_type slash_position = name.find_last_of("/\\");
817 // if name starts with a / or a . or contains : user is assumed to have passed a relative/absolute name
818 // (not only a plain script name)
819 // we trust him, and try to expland the directory name
820 // WARNING : starting from current local directory : ./whatYouWant (./ mandatory!)
822 // if (slash_position != std::string::npos)
823 if (name[0]=='/' || name[1] == ':' || name[0]=='.') // absolute path (linux/windows) or relative path
826 // ===========================================================check user supplied location
827 fullnameGiven = true;
829 fullPathScriptName = Utilities::ExpandLibName(name, verbose);
831 // allow user to always forget ".bbs"
832 int l = fullPathScriptName.size();
838 if (fullPathScriptName.substr(l-4, 4) != ".bbs")
840 fullPathScriptName = fullPathScriptName + ".bbs";
845 fullPathScriptName = fullPathScriptName + ".bbs";
848 if ( Utilities::FileExists(fullPathScriptName))
857 // =============================================================== iterate on the paths
860 std::vector<std::string>::iterator i;
861 for (i=script_paths.begin();i!=script_paths.end();++i)
865 // we *really* want '.' to be the current working directory
867 char buf[2048]; // for getcwd
868 char * currentDir = getcwd(buf, 2048);
869 std::string cwd(currentDir);
873 // fullPathScriptName = Utilities::MakePkgnameFromPath(path, name, true); //pkgname);
875 fullPathScriptName = Utilities::MakePkgnameFromPath(path, name, true);
876 //std::cout << "FULL PATH = "<<fullPathScriptName<<std::endl;
878 // Check if library exists
879 if ( ! Utilities::FileExists(fullPathScriptName) )
881 // The following is *NOT* a debug time message :
882 // It's a user intended message.
883 // Please don't remove it.
885 std::cout <<" [" <<fullPathScriptName <<"] : doesn't exist" <<std::endl;
886 continue; // try next path
889 std::cout <<" [" <<fullPathScriptName <<"] : found" <<std::endl;
891 break; // a script was found; we stop iterating
893 } //------------------ // end for ( package_paths.begin();i!=package_paths.end() )
899 if(fullPathScriptName == "")
900 bbtkError("Path ["<<upath<<"] doesn't exist");
902 bbtkError("Script ["<<fullPathScriptName<<"] not found");
904 bbtkError("No ["<<pkgname<<".bbs] script found");
908 LoadScript(fullPathScriptName);
914 //=======================================================================
916 void Interpreter::LoadScript( std::string fullPathScriptName)
918 bbtkMessage("Interpreter",1,fullPathScriptName<<" found"<<std::endl);
921 s = new std::ifstream;
922 s->open(fullPathScriptName.c_str());
925 bbtkError("Could not open file ["<<fullPathScriptName<<"]");
930 std::cout << " -->[" << fullPathScriptName << "] found" << std::endl;
933 mFileName.push_back(fullPathScriptName);
938 //=======================================================================
942 void Interpreter::CloseCurrentFile()
944 bbtkDebugMessage("Interpreter",9,"Interpreter::CloseCurrentFile()"
949 bbtkDebugMessage("Interpreter",9," -> no file left open"<<std::endl);
953 mFile.back()->close();
956 bbtkDebugMessage("Interpreter",9,
957 " Closing file '"<<mFileName.back()<<"'"<<std::endl);
959 mFileName.pop_back();
961 bbtkDebugMessage("Interpreter",9," Remains "
963 <<" open"<<std::endl);
964 bbtkDebugMessage("Interpreter",9,"EO Interpreter::CloseCurrentFile()"
967 //=======================================================================
969 //=======================================================================
973 void Interpreter::CloseAllFiles()
975 bbtkDebugMessage("Interpreter",9,"Interpreter::CloseAllFiles()"
978 while (mFile.size() != 0)
980 mFile.back()->close();
983 bbtkDebugMessage("Interpreter",9,
984 " Closing file '"<<mFileName.back()<<"'"<<std::endl);
985 mFileName.pop_back();
988 bbtkDebugMessage("Interpreter",9,"EO Interpreter::CloseAllFiles()"
991 //=======================================================================
995 //=======================================================================
999 void Interpreter::InterpretCommand( const std::vector<std::string>& words,
1000 CommandInfoType& info )
1002 bbtkDebugMessageInc("Interpreter",9,"Interpreter::InterpretCommand(...)"<<std::endl);
1004 // searches the command keyword
1005 CommandDictType::iterator c;
1006 c = mCommandDict.find(words[0]);
1007 if ( c == mCommandDict.end() ) {
1008 bbtkError(words[0]<<" : unknown command");
1011 // tests the number of args
1012 if ( ( words.size()-1 < c->second.argmin ) ||
1013 ( words.size()-1 > c->second.argmax ) )
1015 HelpCommand(words[0]);
1016 bbtkError(words[0]<<" : wrong number of arguments");
1020 bbtkDecTab("Interpreter",9);
1022 //=======================================================================
1025 //=======================================================================
1026 /// Displays help on all the commands
1027 void Interpreter::Help(const std::vector<std::string>& words)
1029 unsigned int nbarg = words.size()-1;
1037 if (words[1]=="packages")
1039 PrintPackages(true);
1044 HelpCommand(words[1]);
1046 catch (bbtk::Exception e)
1050 HelpPackage(words[1]);
1052 catch (bbtk::Exception f)
1056 HelpBlackBox(words[1]);
1058 catch (bbtk::Exception g)
1062 this->mExecuter->ShowRelations(words[1],"0","9999");
1064 catch (bbtk::Exception h){
1065 bbtkError("\""<<words[1].c_str()
1066 <<"\" is not a known command, package, black box type or black box name");
1074 if (words[2]=="all")
1076 if ( words[1]=="packages" )
1078 PrintPackages(true,true);
1083 HelpPackage(words[1],true);
1085 catch (bbtk::Exception f)
1091 HelpCommand(words[0]);
1092 bbtkError(words[0]<<" : syntax error");
1097 bbtkError("Should not reach here !!!");
1100 //=======================================================================
1102 //===================================================================
1103 /// Displays the Configuration
1104 void Interpreter::Config( bool verbose ) const
1106 bbtkDebugMessageInc("Core",9,"Factory::Config"<<std::endl);
1108 ConfigurationFile cf = ConfigurationFile::GetInstance();
1110 const std::string config_xml_full_path = cf.Get_config_xml_full_path();
1111 const std::string description = cf.Get_description();
1112 const std::string url = cf.Get_url();
1113 const std::string data_path = cf.Get_data_path();
1114 const std::string default_doc_tmp = cf.Get_default_doc_tmp();
1115 const std::string file_separator = cf.Get_file_separator();
1116 const std::vector<std::string>bbs_paths = cf.Get_bbs_paths();
1117 const std::vector<std::string>package_paths = cf.Get_package_paths();
1119 bbtkMessage("Help",1, "=============" << std::endl);
1120 bbtkMessage("Help",1, "Configuration" << std::endl);
1121 bbtkMessage("Help",1, "=============" << std::endl);
1122 bbtkMessage("Help",1, "bbtk_config.xml : [" << config_xml_full_path << "]" << std::endl);
1123 bbtkMessage("Help",1, "Documentation Url : [" << url << "]" << std::endl);
1124 bbtkMessage("Help",1, "Data Path : [" << data_path << "]" << std::endl);
1125 bbtkMessage("Help",1, "Default Doc_tmp : [" << default_doc_tmp << "]" << std::endl);
1126 bbtkMessage("Help",1, "File Separator : [" << file_separator << "]" << std::endl);
1128 std::vector<std::string>::const_iterator i;
1130 bbtkMessage("Help",1, "BBS Paths " << std::endl);
1131 for (i = bbs_paths.begin(); i!=bbs_paths.end(); ++i )
1133 bbtkMessage("Help",1,"--- ["<<*i<<"]"<<std::endl);
1136 bbtkMessage("Help",1, "PACKAGE Paths : " << std::endl);
1137 for (i = package_paths.begin(); i!=package_paths.end(); ++i )
1139 bbtkMessage("Help",1,"--- ["<<*i<<"]"<<std::endl);
1142 bbtkDebugDecTab("Core",9);
1145 //=======================================================================
1146 /// Displays help on all the commands
1147 void Interpreter::HelpCommands()
1149 std::cout << "Available commands :" << std::endl;
1150 CommandDictType::iterator i;
1151 for ( i = mCommandDict.begin();
1152 i != mCommandDict.end();
1154 std::cout << " " << i->first << std::endl;
1155 // std::cout << " usage : " << i->second.syntax << std::endl;
1156 // std::cout << " " << i->second.help << std::endl;
1160 //=======================================================================
1163 //=======================================================================
1164 /// Displays help on a particular commands
1165 void Interpreter::HelpCommand(const std::string& s)
1167 CommandDictType::iterator c;
1168 c = mCommandDict.find(s);
1169 if ( c == mCommandDict.end() ) {
1170 bbtkError(s<<" : Unknown command");
1172 // std::cout << " " << s << " : "<< std::endl;
1173 // CommandParamDictType::iterator i;
1174 // for ( i = c->second.begin();
1175 // i != c->second.end();
1177 std::cout << " usage : " << c->second.syntax << std::endl;
1178 std::cout << " " << c->second.help << std::endl;
1181 //=======================================================================
1184 //=======================================================================
1185 /// Fills the vector commands with the commands which
1186 /// have the first n chars of buf for prefix
1187 /// TODO : skip initial spaces in buf and also return the position of first
1188 /// non blank char in buf
1189 void Interpreter::FindCommandsWithPrefix( char* buf,
1191 std::vector<std::string>& commands )
1193 CommandDictType::const_iterator i;
1194 for (i=mCommandDict.begin(); i!=mCommandDict.end(); ++i)
1196 if ((i->first).find(buf,0,n) == 0)
1197 commands.push_back(i->first);
1200 //=======================================================================
1204 //=======================================================================
1205 #ifdef BBTK_USE_TERMIOS_BASED_PROMPT
1207 inline void PrintChar(char c) { write(STDOUT_FILENO,&c,1); }
1208 inline void BackSpace() { write(STDOUT_FILENO,"\b \b",3); }
1210 // LG : KEYBOARD CODES AS SCANNED ON MY TTY : UNIVERSAL ?
1211 // IF NOT THE USER SHOULD BE ABLE TO CONFIGURE IT
1212 // E.G. STORE THIS IN bbtk_config.xml
1213 #define BBTK_UP_ARROW_KBCODE 0x00415B1B
1214 #define BBTK_DOWN_ARROW_KBCODE 0x00425B1B
1215 #define BBTK_RIGHT_ARROW_KBCODE 0x00435B1B
1216 #define BBTK_LEFT_ARROW_KBCODE 0x00445B1B
1217 #define BBTK_BACKSPACE_KBCODE 0x00000008
1218 #define BBTK_DEL_KBCODE 0x0000007F
1219 #define BBTK_SPACE_KBCODE 0x00000020
1221 //=======================================================================
1222 void Interpreter::GetLineFromPrompt(std::string& s)
1227 int MAX_LINE_SIZE = 160;
1228 int MAX_HISTORY_SIZE = 100;
1230 char* newline = new char[MAX_LINE_SIZE];
1231 memset(newline,0,MAX_LINE_SIZE);
1232 char* histline = new char[MAX_LINE_SIZE];
1233 memset(histline,0,MAX_LINE_SIZE);
1235 char* line = newline;
1236 int hist = mHistory.size();
1243 read ( STDIN_FILENO, &c, 4) ;
1245 bbtkDebugMessage("Debug",9,"[0x"<<std::hex<<c<<"]\n");
1247 // Printable character
1248 if ( (ind<MAX_LINE_SIZE-1) &&
1249 ( c >= BBTK_SPACE_KBCODE ) &&
1250 ( c < BBTK_DEL_KBCODE ))
1258 // delete the unused line
1264 // empty lines are not stored in from history
1267 // if history too long : delete oldest command
1268 if (mHistory.size()>MAX_HISTORY_SIZE)
1270 delete mHistory.front();
1271 mHistory.pop_front();
1273 mHistory.push_back(line);
1278 else if ( (ind>0) &&
1279 ((c == BBTK_BACKSPACE_KBCODE) ||
1280 (c == BBTK_DEL_KBCODE)) )
1288 // TODO : Command completion
1289 std::vector<std::string> commands;
1290 FindCommandsWithPrefix( line,ind,commands);
1291 if (commands.size()==1)
1293 std::string com = *commands.begin();
1294 for (; ind<com.size(); ++ind)
1296 PrintChar(com[ind]);
1302 else if (commands.size()>1)
1304 std::vector<std::string>::iterator i;
1306 for (i=commands.begin();i!=commands.end();++i)
1308 write(STDOUT_FILENO,(*i).c_str(),strlen((*i).c_str()));
1311 write(STDOUT_FILENO,"\n> ",3);
1312 //for (int j=0;j<ind;++j)
1314 write(STDOUT_FILENO,line,ind);
1318 // Arrow up : back in history
1319 else if (c==BBTK_UP_ARROW_KBCODE)
1323 // erase current line
1324 while (ind--) BackSpace();
1328 strcpy(histline,mHistory[hist]);
1332 write(STDOUT_FILENO,line,ind);
1335 // Arrow down : down in history
1336 else if (c==BBTK_DOWN_ARROW_KBCODE)
1338 if (hist<mHistory.size()-1)
1340 // erase current line
1341 while (ind--) BackSpace();
1345 strcpy(histline,mHistory[hist]);
1349 write(STDOUT_FILENO,line,ind);
1351 // end of history : switch back to newline
1352 else if (hist==mHistory.size()-1)
1354 // erase current line
1355 while (ind--) BackSpace();
1362 write(STDOUT_FILENO,line,ind);
1366 else if (line[ind]!=0 && c==BBTK_RIGHT_ARROW_KBCODE)
1368 PrintChar(line[ind]);
1373 else if (ind>0 && c==BBTK_LEFT_ARROW_KBCODE)
1381 write(STDOUT_FILENO,"\n\r",2);
1389 //=======================================================================
1390 void Interpreter::GetLineFromPrompt(std::string& s)
1416 //=======================================================================
1422 //=======================================================================
1423 void Interpreter::CommandLineInterpreter()
1425 bbtkDebugMessageInc("Interpreter",9,
1426 "Interpreter::CommandLineInterpreter()"<<std::endl);
1428 #ifdef BBTK_USE_TERMIOS_BASED_PROMPT
1429 // Initialise the tty in non canonical mode with no echo
1430 // oter remembers the previous settings to restore them after
1431 struct termios ter,oter;
1434 ter.c_lflag &= ~ECHO;
1435 ter.c_lflag &= ~ICANON;
1438 tcsetattr(0,TCSANOW,&ter);
1441 mCommandLine = true;
1443 bool insideComment = false; // for multiline comment
1449 GetLineFromPrompt(line);
1450 InterpretLine(line, insideComment);
1452 catch (QuitException e)
1456 catch (bbtk::Exception e)
1460 catch (std::exception& e)
1462 std::cerr << "* ERROR :: "<<e.what()<<" (not in bbtk)"<<std::endl;
1466 std::cerr << "* UNDEFINED ERROR (not a bbtk nor a std exception)"<<std::endl;
1471 #ifdef BBTK_USE_TERMIOS_BASED_PROMPT
1472 tcsetattr(0,TCSANOW,&oter);
1475 std::cout << "Good bye !" << std::endl;
1477 bbtkDebugDecTab("Interpreter",9);
1480 //=======================================================================
1481 void Interpreter::Graph(const std::vector<std::string>& words)
1484 bool system_display = true;
1486 #ifdef _USE_WXWIDGETS_
1487 if ( WxConsole::GetInstance() != 0 ) system_display = false;
1490 if (words.size()==1)
1492 page = mExecuter->ShowGraph(".","0","0","","","",system_display);
1494 else if (words.size()==2)
1496 page = mExecuter->ShowGraph(words[1],"0","0","","","",system_display);
1498 else if (words.size()==3)
1500 page = mExecuter->ShowGraph(words[1],words[2],"0","","","",system_display);
1502 else if (words.size()==4)
1504 page = mExecuter->ShowGraph(words[1],words[2],words[3],"","","",system_display);
1506 else if (words.size()==5)
1508 page = mExecuter->ShowGraph(words[1],words[2],words[3],words[4],"","",system_display);
1510 else if (words.size()==6)
1512 page = mExecuter->ShowGraph(words[1],words[2],words[3],words[4],words[5],"",system_display);
1514 else if (words.size()==7)
1516 page = mExecuter->ShowGraph(words[1],words[2],words[3],words[4],words[5],words[6],system_display);
1519 #ifdef _USE_WXWIDGETS_
1520 if ( WxConsole::GetInstance() != 0 )
1521 WxConsole::GetInstance()->ShowHtmlPage(page);
1524 //=======================================================================