60 #include "rnr/rnrconfig.h" 82 #define SH_RC_OK_NOEXEC 1
84 #define SH_RC_SYS_ERROR 3
96 : m_sShellName(
"dynashell"),
97 m_sPS1(
"dynashell> "),
99 m_ReadLine(m_sShellName)
168 m_mapCmds[sParent].SetMapBrief(strBrief);
199 if( (sParent == NULL) || (*sParent == 0) )
211 (*
m_mapCmds[sParent].GetMap())[sCmdName] = pNewCmd;
212 (*
m_mapCmds[sParent].GetMap())[sCmdName].SetMapBrief(
223 (*
m_mapCmds[sParent].GetMap())[sCmdName] = pNewCmd;
251 ShCmdMap::iterator iter;
255 for(iter=cmdMap.begin(); iter!=cmdMap.end(); iter++)
257 if( iter->second.IsCmd() )
259 delete iter->second.GetCmd();
261 else if( iter->second.IsMap() )
296 const char *sContext,
323 const char *sContext)
326 static ShCmdMap::iterator iterCmd;
328 const char *sCmdName;
339 if( (sContext == NULL) || (*sContext == 0) )
342 iterCmd = pMap->begin();
351 pMap = iterCmd->second.GetMap();
354 iterCmd = pMap->begin();
361 while( (pMap != NULL) && (iterCmd != pMap->end()) )
363 sCmdName = iterCmd->first;
365 if( !strncmp(sCmdName, sText, uTextLen) )
395 const char *sContext,
422 const char *sContext)
424 MapTabCompleteGen::iterator pos;
428 return pos->second->TabCompletion(*
this, sText, uTextLen, nState, sContext);
471 static const char *sPrompt =
m_sPS1;
478 if( isatty(fileno(
InputFp())) )
525 Error(
"%s.", strerror(errno));
531 while(*sLine && isspace(*sLine))
569 ShCmdMap::iterator iter1, iter2;
571 const char *sCmdName1, *sCmdName2;
573 bool_t bIsUnique =
true;
578 Error(
"No Command.");
584 n = strlen(sPattern);
587 for(iter1=cmdMap.begin(); iter1!=cmdMap.end(); iter1++)
589 sCmdName1 = iter1->first;
590 if( !strncmp(sPattern, sCmdName1, n) )
597 if( iter1 == cmdMap.end() )
599 Error(
"%s: Command not found.\n", sPattern);
604 for(iter2=cmdMap.begin(); iter2!=cmdMap.end(); iter2++)
611 sCmdName2 = iter2->first;
614 if( !strncmp(sPattern, sCmdName2, n) )
621 fprintf(stderr,
"%s: Command not unique: Matches %s %s",
622 sPattern, sCmdName1, sCmdName2);
629 fprintf(stderr,
" %s", sCmdName2);
635 fprintf(stderr,
" ...");
643 fprintf(stderr,
"\n");
647 if( iter1->second.IsCmd() )
649 return iter1->second.GetCmd();
651 else if( iter1->second.IsMap() )
653 return MatchCmd(argc-1, argv+1, *iter1->second.GetMap());
675 Error(
"Cannot execute script %s: At maximum script depth of %d",
680 pScript =
new ShScript(sScriptFile, fp);
745 printf(
": %s\n", sLine);
749 printf(
"%s[%d]: %s\n",
787 fprintf(stderr,
"%s[%d]: ",
792 fprintf(stderr,
"Warning: ");
795 vfprintf(stderr, sFmt, ap);
798 fprintf(stderr,
"\n");
815 vsnprintf(buf,
sizeof(buf), sFmt, ap);
816 buf[
sizeof(buf)-1] = 0;
835 Error(
"Servo %d: Alarms: %s.", nServoId, strAlarms.c_str());
853 fprintf(stderr,
"%s[%d]: ",
858 fprintf(stderr,
"Error: ");
861 vfprintf(stderr, sFmt, ap);
864 fprintf(stderr,
"\n");
894 printf(
" Dynamixel Simple Shell\n");
895 printf(
"(enter 'help' for list of commands; " 896 "partial command matching supported)\n\n");
930 pCmd->
Exec(*
this, tokc-nOffset, tokv+nOffset);
RoadNarrows Dynamixel Bus Communications Abstract Base Class Interface.
int RegisterGenerator(const string strRegEx, ReadLineAppGenFunc_T fnAppGen, void *pAppArg)
Register application-specific tab-completion generator associated.
char * GetInputLine()
Get one input line from file stream.
void ScriptBumpLineNum()
Bump the line number of the current executing script, if any, by one.
FILE * InputFp()
Get the current input file pointer.
#define DYNA_OK
not an error, success
bool IsRunningScript()
Test if executing a script.
Dynamixel shell utilities.
void ScriptTrace(const char *sLine)
Trace script execution.
DynaComm * m_pDynaComm
dynamixel bus communication
void ScriptPush(const char *sScriptFile, FILE *fp)
Push new script on the stack.
static char * CmdSpecGeneratorWrap(int nUid, const char *sText, size_t uTextLen, int nState, const char *sContext, void *pArg)
Command spec generator wrapper function.
DynaShellCmd * MatchCmd(int argc, char *argv[], ShCmdMap &cmdMap)
Match command.
#define SH_RC_OK_NOEXEC
shell input ok, but don't execute command
char * CmdListGenerator(int nUid, const char *sText, size_t uTextLen, int nState, const char *sContext)
Command list generator function.
ShCmdMap m_mapCmds
map of published commnds
static char * fReadLine(FILE *fp, const char *sPrompt)
Read one input line from the given input stream.
MapTabCompleteGen m_mapTabGen
command tab-completion generators map
void PublishMap(const char *sParent, const string &strBrief)
Publish new command map to shell.
static const int FIRST
first state
bool IsExecStmt(const char *sLine)
Test if line is a potential executable statement.
static char * dupstr(const string &str)
Duplicate string.
Dynamixel shell command abstract base class.
#define DYNA_ECODE_SYS
system (errno) error
void InputInit()
Initialize input.
static int tokenize(char *s, char *tokv[], size_t tokmax)
Tokenize input.
static char * CmdListGeneratorWrap(int nUid, const char *sText, size_t uTextLen, int nState, const char *sContext, void *pArg)
Command list generator wrapper function.
DynaRecording * m_pRecording
dynamixel recording
vector< ShScript * > m_vecScripts
script stack
const char * DynaStrError(int ecode)
Get the error string describing the Dynamixel error code.
virtual int Stop()
Stop control and monitoring tasks.
#define SH_RC_SYS_ERROR
system error on shell input
int SetPublishedInfo(int nLevel, const char *sParent=NULL)
map< const char *, ShCmdNode, ShCmdNodeKeyCmp > ShCmdMap
Shell command map type.
bool m_bIsInteractive
[not] user interactive
void AddToHistory(char *sLine)
Add command line to history.
void ScriptPop()
Pop the current script from stack.
void Response(const char *sFmt,...)
Print formatted success response.
#define DYNA_ECODE_RUNTIME
Shell run-time error.
void ScriptFlush()
Flush all scripts from stack.
bool m_bXTrace
do [not] trace script
const char * m_sPS1
primary user prompt
void Warning(const char *sFmt,...)
Issue warning.
ReadLine m_ReadLine
readline object
void AddToHistory(const char *sInput)
Add line to history.
RoadNarrows Dynamixel Bus Communication over Serial Interface Class Interface.
bool IsEOF()
Test if input is at End Of File condition.
int GetPublishedLevel() const
const char * GetCmdName()
Get dynamixel shell command name.
char * CmdSpecGenerator(int nUid, const char *sText, size_t uTextLen, int nState, const char *sContext)
Command spec generator function.
static std::string GetAlarmsString(const uint_t uAlarms, const std::string &strSep="; ")
Get a formatted servo alarms string associated with the alarms.
DynaBgThread * m_pDynaBgThread
dynamixel chain
RoadNarrows Dynamixel Archetype Servo Abstract Base Class.
static const int NOT_REG
not registered return value
bool m_bSilent
do [not] silence non-error responses
The Dynamixel Shell ReadLine Class.
void PublishCommand(const char *sParent, DynaShellCmd *pNewCmd)
Publish new command to shell.
RoadNarrows Dynamixel Top-Level Package Header File.
The dynashell Command Class Interface.
const char * GetCmdHelpBrief()
Get shell command name brief description.
void DeleteCommands(ShCmdMap &cmdMap)
Delete all published commands in map.
char * iReadLine(const char *sPrompt)
Interactively read a line of input from standard input.
RoadNarrows Botsene IP Proxied Dynamixel Bus Communication Class Interface.
#define SH_RC_EOF
end of file
bool m_bRun
do [not] run shell
#define SH_RC_OK
shell input ok
const char * GetPublishedName() const
DynaChain * m_pDynaChain
dynamixel chain
void Error(int rc, const char *sFmt,...)
Raise error on dynamixel error code.
int Run()
Command execution loop.
int m_rc
last command executed return code
int GetInput()
Get one, non-empty input line.
static const int m_nMaxScriptDepth
maximum script stack depth
The simple dynashell declarations.
bool m_bNoExec
parse only, no execution
char * m_sInputLine
working, allocated line of input
virtual void Exec(DynaShell &shell, int argc, char *argv[])=0
Execute command abstract function.