Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
dynamic-graph
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Stack Of Tasks
dynamic-graph
Commits
7887e86e
Commit
7887e86e
authored
14 years ago
by
Francois Bleibel
Browse files
Options
Downloads
Patches
Plain Diff
Re-added shell functions and procedures.
parent
057fb587
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/plugins/shell-functions.cpp
+517
-0
517 additions, 0 deletions
src/plugins/shell-functions.cpp
src/plugins/shell-procedure.cpp
+278
-0
278 additions, 0 deletions
src/plugins/shell-procedure.cpp
with
795 additions
and
0 deletions
src/plugins/shell-functions.cpp
0 → 100644
+
517
−
0
View file @
7887e86e
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* Copyright Projet JRL-Japan, 2007
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* File: shell-functions.cpp
* Project: DYNAMIC-GRAPH
* Author: François Bleibel, Nicolas Mansard
*
* Version control
* ===============
*
* $Id$
*
* Description
* ============
*
*
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#include
<dynamic-graph/shell-functions.h>
#include
<dynamic-graph/plugin-loader.h>
#include
<dynamic-graph/factory.h>
#include
<dynamic-graph/debug.h>
#include
<dynamic-graph/entity.h>
#include
<dynamic-graph/signal.h>
#include
<dynamic-graph/exception-signal.h>
#ifdef WIN32
#include
<Windows.h>
#endif
#include
<fstream>
using
namespace
std
;
using
namespace
dynamicgraph
;
void
ShellFunctions
::
cmdTry
(
const
std
::
string
cmdLine
,
istringstream
&
cmdArg
,
std
::
ostream
&
os
)
{
if
(
cmdLine
==
"help"
)
{
os
<<
" - try <cmd...>"
<<
"
\t\t\t\t
Try <cmd> and display the caught exception."
<<
endl
;
return
;
}
string
cmdLine2
;
cmdArg
>>
cmdLine2
;
dgDEBUG
(
5
)
<<
"Try <"
<<
cmdLine2
<<
">"
<<
endl
;
try
{
g_shell
.
cmd
(
cmdLine2
,
cmdArg
,
os
);
}
catch
(
const
ExceptionAbstract
&
e
)
{
os
<<
"dgERROR catch: "
<<
endl
<<
e
<<
endl
;
}
catch
(
...
)
{
os
<<
"Unknown error catch."
<<
endl
;
}
}
void
ShellFunctions
::
cmdLoadPlugins
(
const
std
::
string
cmdLine
,
std
::
istringstream
&
cmdArg
,
std
::
ostream
&
os
)
{
if
(
cmdLine
==
"help"
)
{
os
<<
" - loadPlugins <file.txt> <directory>"
<<
"
\t\t
Load the plugins listed in the file."
<<
endl
;
return
;
}
if
(
NULL
!=
g_shell
.
dlPtr
)
{
string
pluginName
,
directory
;
cmdArg
>>
pluginName
;
cmdArg
>>
directory
;
dgDEBUG
(
15
)
<<
"Load plugin list <"
<<
pluginName
<<
"> from dir <"
<<
directory
<<
">."
<<
endl
;
if
(
directory
.
length
()
!=
0
)
g_shell
.
dlPtr
->
setDirectory
(
directory
);
g_shell
.
dlPtr
->
loadPluginList
(
pluginName
);
g_shell
.
dlPtr
->
loadPlugins
();
}
else
{
os
<<
"!! Dynamic loading functionalities not accessible through the shell."
<<
endl
;
}
}
void
ShellFunctions
::
cmdClearPlugin
(
const
std
::
string
cmdLine
,
std
::
istringstream
&
cmdArg
,
std
::
ostream
&
os
)
{
if
(
cmdLine
==
"help"
)
{
os
<<
" - clearPlugin <className>"
<<
"
\t\t
Destroy all the objects of type <className>."
<<
endl
;
return
;
}
string
pluginName
;
cmdArg
>>
pluginName
;
g_pool
.
clearPlugin
(
pluginName
);
}
void
ShellFunctions
::
cmdDisplayPlugins
(
const
std
::
string
cmdLine
,
std
::
istringstream
&
cmdArg
,
std
::
ostream
&
os
)
{
if
(
cmdLine
==
"help"
)
{
os
<<
" - displayPlugins "
<<
"
\t\t\t\t
Display the name of the loaded plugins."
<<
endl
;
return
;
}
if
(
NULL
!=
g_shell
.
dlPtr
)
{
std
::
map
<
std
::
string
,
std
::
string
>
m
=
g_shell
.
dlPtr
->
getLoadedPluginNames
();
for
(
std
::
map
<
std
::
string
,
std
::
string
>::
const_iterator
iter
=
m
.
begin
();
iter
!=
m
.
end
();
++
iter
)
{
os
<<
" - <"
<<
iter
->
first
<<
">:
\t
"
<<
iter
->
second
<<
endl
;
}
}
else
{
os
<<
"!! Dynamic loading functionalities not accessible through the shell."
<<
endl
;
}
}
void
ShellFunctions
::
cmdDisplayFactory
(
const
std
::
string
cmdLine
,
std
::
istringstream
&
cmdArg
,
std
::
ostream
&
os
)
{
if
(
cmdLine
==
"help"
)
{
os
<<
" - "
;
g_factory
.
commandLine
(
cmdLine
,
cmdArg
,
os
);
return
;
}
string
cmd2
;
cmdArg
>>
cmd2
;
g_factory
.
commandLine
(
cmd2
,
cmdArg
,
os
);
}
void
ShellFunctions
::
cmdCommentary
(
const
std
::
string
cmdLine
,
std
::
istringstream
&
cmdArg
,
std
::
ostream
&
os
)
{
if
(
cmdLine
==
"help"
)
{
os
<<
" # comment with '#': ignore the end of the line."
<<
endl
;
}
return
;
}
void
ShellFunctions
::
cmdUnplug
(
const
std
::
string
cmdLine
,
istringstream
&
cmdArg
,
std
::
ostream
&
os
)
{
if
(
cmdLine
==
"help"
)
{
os
<<
" - unplug <obj2.sig2>"
<<
"
\t\t\t\t
Plug on sig2 (consumer) in sig1->sig2."
<<
endl
;
return
;
}
string
ssig
;
cmdArg
>>
ssig
;
string
obj2
,
fun2
;
istringstream
str2
(
ssig
);
if
(
!
Interpreter
::
objectNameParser
(
str2
,
obj2
,
fun2
)
)
{
DG_THROW
ExceptionFactory
(
ExceptionFactory
::
SYNTAX_ERROR
,
"Plug function: syntax is unplug OBJ2.SIG2."
,
"(while calling plug %s %s)."
,
ssig
.
c_str
()
);
}
dgDEBUG
(
20
)
<<
"Get Ent2 <"
<<
obj2
<<
"> ."
<<
endl
;
Entity
&
ent2
=
g_pool
.
getEntity
(
obj2
);
dgDEBUG
(
20
)
<<
"Get Sig2 <"
<<
fun2
<<
"> ."
<<
endl
;
SignalBase
<
int
>
&
sig2
=
ent2
.
getSignal
(
fun2
);
dgDEBUG
(
25
)
<<
"Unplug..."
<<
endl
;
sig2
.
unplug
();
}
void
ShellFunctions
::
cmdSignalTime
(
const
std
::
string
cmdLine
,
istringstream
&
cmdArg
,
std
::
ostream
&
os
)
{
if
(
cmdLine
==
"help"
)
{
os
<<
" - signalTime <obj.sig>"
<<
"
\t\t\t\t
Display the time of sig."
<<
endl
;
return
;
}
string
ssig
;
cmdArg
>>
ssig
;
string
obj2
,
fun2
;
istringstream
str2
(
ssig
);
if
(
!
Interpreter
::
objectNameParser
(
str2
,
obj2
,
fun2
)
)
{
DG_THROW
ExceptionFactory
(
ExceptionFactory
::
SYNTAX_ERROR
,
"Plug function: syntax is unplug OBJ2.SIG2."
,
"(while calling plug %s %s)."
,
ssig
.
c_str
()
);
}
dgDEBUG
(
20
)
<<
"Get Ent2 <"
<<
obj2
<<
"> ."
<<
endl
;
Entity
&
ent2
=
g_pool
.
getEntity
(
obj2
);
dgDEBUG
(
20
)
<<
"Get Sig2 <"
<<
fun2
<<
"> ."
<<
endl
;
SignalBase
<
int
>
&
sig2
=
ent2
.
getSignal
(
fun2
);
os
<<
sig2
.
getTime
()
<<
endl
;
}
void
ShellFunctions
::
cmdSynchroSignal
(
const
std
::
string
cmdLine
,
std
::
istringstream
&
cmdArg
,
std
::
ostream
&
os
)
{
if
(
cmdLine
==
"help"
)
{
os
<<
" - synchro <obj.sig> [<period>]"
<<
"
\t\t\t\t
Get/Set the syncho of a signal <sig>."
<<
endl
;
return
;
}
SignalBase
<
int
>
&
sig
=
g_pool
.
getSignal
(
cmdArg
);
cmdArg
>>
ws
;
if
(
cmdArg
.
good
()
)
{
int
period
;
cmdArg
>>
period
;
sig
.
setPeriodTime
(
period
);
cmdArg
>>
ws
;
if
(
cmdArg
.
good
()
)
{
/* The sig is recomputed at the given period from the
* current time, at any time T so that T%p==0, p the period.
* By modifying the current time, the sig reomputation is done
* at T s.t. T%p=d, d the desynchro. */
int
currTime
=
sig
.
getTime
();
int
desynchro
;
cmdArg
>>
desynchro
;
sig
.
setTime
(
currTime
+
desynchro
);
}
}
else
{
os
<<
"period = "
<<
sig
.
getPeriodTime
()
<<
std
::
endl
;
}
}
void
ShellFunctions
::
cmdEcho
(
const
std
::
string
cmdLine
,
std
::
istringstream
&
cmdArg
,
std
::
ostream
&
os
)
{
if
(
cmdLine
==
"help"
)
{
os
<<
" - echo <string>"
<<
"
\t\t\t\t
Print <string. on the standard output."
<<
endl
;
return
;
}
cmdArg
>>
ws
;
while
(
cmdArg
.
good
()
)
{
std
::
string
toPrint
;
cmdArg
>>
toPrint
;
os
<<
toPrint
<<
" "
;
}
os
<<
std
::
endl
;
}
void
ShellFunctions
::
cmdCopy
(
const
std
::
string
cmdLine
,
istringstream
&
cmdArg
,
std
::
ostream
&
os
)
{
if
(
cmdLine
==
"help"
)
{
os
<<
" - copy <obj1.sig1> <obj2.sig2>"
<<
"
\t\t
Copy the value of sig1 to constant value in sig2."
<<
endl
;
return
;
}
string
ssig1
,
ssig2
;
cmdArg
>>
ssig1
>>
ssig2
;
istringstream
str1
(
ssig1
),
str2
(
ssig2
);
try
{
SignalBase
<
int
>
&
sig1
=
g_pool
.
getSignal
(
str1
);
SignalBase
<
int
>
&
sig2
=
g_pool
.
getSignal
(
str2
);
dgDEBUG
(
25
)
<<
"Copy..."
<<
endl
;
sig2
.
plug
(
&
sig1
);
sig2
.
setConstantDefault
();
sig2
.
plug
(
&
sig2
);
}
catch
(
ExceptionAbstract
&
err
)
{
throw
;
}
catch
(
...
)
{
DG_THROW
ExceptionFactory
(
ExceptionFactory
::
SYNTAX_ERROR
,
"Copy: syntax is copy OBJ1.SIG1 OBJ2.SIG2."
,
"(while calling copy %s %s)."
,
ssig1
.
c_str
(),
ssig2
.
c_str
()
);
}
}
void
ShellFunctions
::
cmdFreeze
(
const
std
::
string
cmdLine
,
istringstream
&
cmdArg
,
std
::
ostream
&
os
)
{
if
(
cmdLine
==
"help"
)
{
os
<<
" - freeze <obj.sig> "
<<
"
\t\t
On a ptr-sig: save the current value from the source "
<<
"and unplug the signal."
<<
endl
;
return
;
}
string
ssig1
;
cmdArg
>>
ssig1
;
istringstream
str1
(
ssig1
);
try
{
SignalBase
<
int
>
&
sig1
=
g_pool
.
getSignal
(
str1
);
dgDEBUG
(
25
)
<<
"Unplug..."
<<
endl
;
sig1
.
setConstantDefault
();
sig1
.
plug
(
&
sig1
);
}
catch
(
...
)
{
DG_THROW
ExceptionFactory
(
ExceptionFactory
::
SYNTAX_ERROR
,
"Freeze: syntax is freeze OBJ.SIG."
,
"(while calling freeze %s )."
,
ssig1
.
c_str
());
}
}
void
ShellFunctions
::
cmdSqueeze
(
const
std
::
string
cmdLine
,
istringstream
&
cmdArg
,
std
::
ostream
&
os
)
{
if
(
cmdLine
==
"help"
)
{
os
<<
" - squeeze <mainObj.mainSig> <squeezeObj.sigIn> <squeezeObj.sigOut>"
<<
"
\t\t
Intercalate squeezeObj between mainObj and its source."
<<
endl
;
return
;
}
string
ssigMain
,
ssigIn
,
ssigOut
;
cmdArg
>>
ssigMain
>>
ssigIn
>>
ssigOut
;
istringstream
strMain
(
ssigMain
);
istringstream
strIn
(
ssigIn
);
istringstream
strOut
(
ssigOut
);
try
{
SignalBase
<
int
>
&
sigMain
=
g_pool
.
getSignal
(
strMain
);
SignalBase
<
int
>
&
sigIn
=
g_pool
.
getSignal
(
strIn
);
SignalBase
<
int
>
&
sigOut
=
g_pool
.
getSignal
(
strOut
);
SignalBase
<
int
>
*
sigMainSource
=
sigMain
.
getPluged
();
if
(
sigMainSource
==&
sigMain
)
{
DG_THROW
ExceptionFactory
(
ExceptionFactory
::
SYNTAX_ERROR
,
"The main signal is autopluged (or set constant). "
,
"(while calling freeze %s )."
,
ssigMain
.
c_str
());
}
sigMain
.
plug
(
&
sigOut
);
sigIn
.
plug
(
sigMainSource
);
}
catch
(
ExceptionFactory
&
exc
)
{
switch
(
exc
.
getCode
()
)
{
case
ExceptionFactory
::
UNREFERED_SIGNAL
:
DG_THROW
ExceptionFactory
(
ExceptionFactory
::
SYNTAX_ERROR
,
"Sqeeze: unknown signal. "
,
"(error while searching signal: %s )."
,
exc
.
getMessage
()
);
break
;
default:
throw
;
}
}
}
void
ShellFunctions
::
cmdEnableTrace
(
const
std
::
string
cmdLine
,
istringstream
&
cmdArg
,
std
::
ostream
&
os
)
{
if
(
cmdLine
==
"help"
)
{
os
<<
" - debugtrace [{true|false}] [<filename>="
<<
DebugTrace
::
DEBUG_FILENAME_DEFAULT
<<
"]"
<<
"
\t\t
Open/close the file <filename> for debug tracing."
<<
endl
;
return
;
}
string
opt
,
filename
;
cmdArg
>>
ws
;
if
(
cmdArg
.
good
()
)
{
cmdArg
>>
opt
>>
ws
;
if
(
opt
==
"true"
)
if
(
cmdArg
.
good
()
)
{
cmdArg
>>
filename
;
DebugTrace
::
openFile
(
filename
.
c_str
()
);
}
else
{
DebugTrace
::
openFile
();
}
else
DebugTrace
::
closeFile
();
}
else
{
if
(
dgDEBUGFLOW
.
outputbuffer
.
good
()
)
os
<<
"true"
<<
endl
;
else
os
<<
"false"
<<
endl
;
}
}
void
ShellFunctions
::
cmdSetPrompt
(
const
std
::
string
cmdLine
,
istringstream
&
cmdArg
,
std
::
ostream
&
os
)
{
if
(
cmdLine
==
"help"
)
{
os
<<
" - prompt [<string>] "
<<
"
\t\t
Set/get the default prompt."
<<
endl
;
return
;
}
string
opt
;
cmdArg
>>
ws
;
if
(
cmdArg
.
good
()
)
{
char
buffer
[
80
];
cmdArg
.
getline
(
buffer
,
80
);
g_shell
.
prompt
=
buffer
;
}
else
{
os
<<
"Current prompt is <"
<<
g_shell
.
prompt
<<
">."
<<
endl
;
}
}
void
ShellFunctions
::
cmdSleep
(
const
std
::
string
cmdLine
,
istringstream
&
cmdArg
,
std
::
ostream
&
os
)
{
if
(
cmdLine
==
"help"
)
{
os
<<
" - sleep [<float> secs] "
<<
"
\t\t
Sleep (time in secs)."
<<
endl
;
return
;
}
string
opt
;
cmdArg
>>
ws
;
if
(
cmdArg
.
good
()
)
{
double
secs
;
cmdArg
>>
secs
;
if
(
secs
>
0
)
#ifndef WIN32
usleep
(
int
(
secs
*
1000000
)
);
#else
Sleep
(
int
(
secs
*
1000
)
);
#endif
}
else
{
/* TODO ERROR */
}
}
void
ShellFunctions
::
cmdBeep
(
const
std
::
string
cmdLine
,
istringstream
&
cmdArg
,
std
::
ostream
&
os
)
{
if
(
cmdLine
==
"help"
)
{
os
<<
" - beep [<float> secs] "
<<
"
\t\t
Send a bip to the std::cout."
<<
endl
;
return
;
}
os
<<
char
(
7
)
<<
"Beep!"
<<
std
::
endl
;
}
void
ShellFunctions
::
cmdCompletionList
(
const
std
::
string
cmdLine
,
istringstream
&
cmdArg
,
std
::
ostream
&
os
)
{
if
(
cmdLine
==
"help"
)
{
os
<<
" - completion <filename>"
<<
"
\t\t
Generate the completion list for current graph."
<<
endl
;
return
;
}
try
{
std
::
string
aFileName
;
cmdArg
>>
aFileName
;
std
::
ofstream
completionFile
((
char
*
)
aFileName
.
c_str
());
g_pool
.
writeCompletionList
(
completionFile
);
}
catch
(
ExceptionAbstract
&
err
)
{
throw
;
}
catch
(
...
)
{
DG_THROW
ExceptionFactory
(
ExceptionFactory
::
SYNTAX_ERROR
,
"setflag: sig should be of flag type. "
,
"(while calling setflag)."
);
}
}
extern
"C"
{
ShellFunctionRegisterer
regFun1
(
"try"
,
boost
::
bind
(
ShellFunctions
::
cmdTry
,
_1
,
_2
,
_3
)
);
ShellFunctionRegisterer
regFun2
(
"loadPlugins"
,
boost
::
bind
(
ShellFunctions
::
cmdLoadPlugins
,
_1
,
_2
,
_3
)
);
ShellFunctionRegisterer
regFun3
(
"displayPlugins"
,
boost
::
bind
(
ShellFunctions
::
cmdDisplayPlugins
,
_1
,
_2
,
_3
)
);
ShellFunctionRegisterer
regFun4
(
"factory"
,
boost
::
bind
(
ShellFunctions
::
cmdDisplayFactory
,
_1
,
_2
,
_3
)
);
ShellFunctionRegisterer
regFun5
(
"#"
,
boost
::
bind
(
ShellFunctions
::
cmdCommentary
,
_1
,
_2
,
_3
)
);
ShellFunctionRegisterer
regFun7
(
"unplug"
,
boost
::
bind
(
ShellFunctions
::
cmdUnplug
,
_1
,
_2
,
_3
)
);
ShellFunctionRegisterer
regFun8
(
"clearPlugin"
,
boost
::
bind
(
ShellFunctions
::
cmdClearPlugin
,
_1
,
_2
,
_3
)
);
ShellFunctionRegisterer
regFun9
(
"signalTime"
,
boost
::
bind
(
ShellFunctions
::
cmdSignalTime
,
_1
,
_2
,
_3
)
);
ShellFunctionRegisterer
regFun10
(
"synchro"
,
boost
::
bind
(
ShellFunctions
::
cmdSynchroSignal
,
_1
,
_2
,
_3
)
);
ShellFunctionRegisterer
regFun11
(
"echo"
,
boost
::
bind
(
ShellFunctions
::
cmdEcho
,
_1
,
_2
,
_3
)
);
ShellFunctionRegisterer
regFun12
(
"copy"
,
boost
::
bind
(
ShellFunctions
::
cmdCopy
,
_1
,
_2
,
_3
)
);
ShellFunctionRegisterer
regFun13
(
"freeze"
,
boost
::
bind
(
ShellFunctions
::
cmdFreeze
,
_1
,
_2
,
_3
)
);
ShellFunctionRegisterer
regFun13b
(
"squeeze"
,
boost
::
bind
(
ShellFunctions
::
cmdSqueeze
,
_1
,
_2
,
_3
)
);
ShellFunctionRegisterer
regFun14
(
"debugtrace"
,
boost
::
bind
(
ShellFunctions
::
cmdEnableTrace
,
_1
,
_2
,
_3
)
);
ShellFunctionRegisterer
regFun15
(
"prompt"
,
boost
::
bind
(
ShellFunctions
::
cmdSetPrompt
,
_1
,
_2
,
_3
)
);
ShellFunctionRegisterer
regFun16
(
"sleep"
,
boost
::
bind
(
ShellFunctions
::
cmdSleep
,
_1
,
_2
,
_3
)
);
ShellFunctionRegisterer
regFun17
(
"beep"
,
boost
::
bind
(
ShellFunctions
::
cmdBeep
,
_1
,
_2
,
_3
)
);
ShellFunctionRegisterer
regFun19
(
"completion"
,
boost
::
bind
(
ShellFunctions
::
cmdCompletionList
,
_1
,
_2
,
_3
)
);
}
This diff is collapsed.
Click to expand it.
src/plugins/shell-procedure.cpp
0 → 100644
+
278
−
0
View file @
7887e86e
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* Copyright Projet JRL-Japan, 2007
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* File: shell-procedure.cpp
* Project: DYNAMIC-GRAPH
* Author: François Bleibel, Nicolas Mansard
*
* Version control
* ===============
*
* $Id$
*
* Description
* ============
*
*
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#include
<dynamic-graph/shell-procedure.h>
#include
<dynamic-graph/plugin-loader.h>
#include
<dynamic-graph/factory.h>
#include
<dynamic-graph/debug.h>
#include
<dynamic-graph/entity.h>
#include
<fstream>
using
namespace
std
;
using
namespace
dynamicgraph
;
void
ShellProcedure
::
cmdStartProcedure
(
const
std
::
string
&
cmdLine
,
std
::
istringstream
&
args
,
std
::
ostream
&
os
)
{
if
(
cmdLine
==
"help"
)
{
os
<<
" - proc <name>"
<<
"
\t\t\t\t
Procedure header."
<<
endl
;
return
;
}
args
>>
procName
;
dgDEBUG
(
5
)
<<
"Proc <"
<<
procName
<<
">"
<<
endl
;
currentProc
.
clear
();
args
>>
ws
;
while
(
args
.
good
()
)
{
std
::
string
next
;
args
>>
next
>>
ws
;
currentProc
.
params
.
push_back
(
next
);
}
}
void
ShellProcedure
::
cmdContinueProcedure
(
const
std
::
string
&
cmdLine
,
std
::
istringstream
&
args
,
std
::
ostream
&
os
)
{
if
(
cmdLine
==
"help"
)
{
os
<<
" - -> cmd args..."
<<
"
\t\t\t\t
Procedure body."
<<
endl
;
return
;
}
std
::
string
cmd2
;
args
>>
ws
>>
cmd2
;
dgDEBUG
(
5
)
<<
"Proc <"
<<
procName
<<
">: "
<<
cmd2
<<
endl
;
Instruction
ins
;
ins
.
cmd
=
cmd2
;
args
>>
ws
;
while
(
args
.
good
()
)
{
std
::
string
next
;
int
param
=-
1
;
args
>>
next
>>
ws
;
for
(
unsigned
int
i
=
0
;
i
<
currentProc
.
params
.
size
();
++
i
)
{
if
(
next
==
currentProc
.
params
[
i
]
)
{
param
=
i
;
break
;
}
}
ins
.
args
.
push_back
(
next
);
ins
.
params
.
push_back
(
param
);
}
currentProc
.
instructions
.
push_back
(
ins
);
}
void
ShellProcedure
::
cmdEndProcedure
(
const
std
::
string
&
cmdLine
,
std
::
istringstream
&
args
,
std
::
ostream
&
os
)
{
if
(
cmdLine
==
"help"
)
{
os
<<
" - endproc..."
<<
"
\t\t\t\t
Procedure end."
<<
endl
;
return
;
}
dgDEBUG
(
5
)
<<
"Proc <"
<<
procName
<<
">: endproc"
<<
endl
;
procedureList
[
procName
]
=
currentProc
;
// std::string toto="toto";
// for( Procedure::iterator ins=procedureList[ toto ].begin();
// ins!=procedureList[ toto ].end(); ++ins )
// {
// dgDEBUG(15) << "Proc <" << procName << "> : "
// << ins->cmd << " -> " << ins->args <<endl;
// }
currentProc
.
clear
();
if
(
g_shell
.
deregisterFunction
(
procName
))
{
os
<<
"Redefining proc <"
<<
procName
<<
">: procedure already defined. "
<<
"Overwrite it."
<<
endl
;
}
ShellFunctionRegisterer
registration
(
procName
.
c_str
(),
boost
::
bind
(
&
ShellProcedure
::
cmdProcedure
,
this
,
procName
,
_1
,
_2
,
_3
)
);
}
void
ShellProcedure
::
cmdProcedure
(
const
std
::
string
&
procname
,
const
std
::
string
&
cmdLine
,
std
::
istringstream
&
args
,
std
::
ostream
&
os
)
{
if
(
cmdLine
==
"help"
)
{
os
<<
" - "
<<
procname
<<
"
\t\t\t\t\t
User-defined procedure"
<<
endl
;
args
>>
ws
;
if
(
args
.
good
()
)
{
std
::
string
argname
;
const
unsigned
int
gc
=
args
.
tellg
();
args
>>
argname
;
args
.
seekg
(
gc
);
args
.
clear
();
if
(
procname
==
argname
)
{
/* if cmdline = "Help <procname>", then display
* the proc instruction. */
ProcedureList
::
iterator
pair
=
procedureList
.
find
(
argname
);
if
(
pair
==
procedureList
.
end
()
)
{
DG_THROW
ExceptionFactory
(
ExceptionFactory
::
UNREFERED_FUNCTION
,
"Undefined procedure"
,
": procedure <%s> not defined."
,
argname
.
c_str
()
);
}
Procedure
&
proc
=
pair
->
second
;
unsigned
int
cmdnum
=
1
;
for
(
std
::
list
<
Instruction
>::
iterator
ins
=
proc
.
instructions
.
begin
();
ins
!=
proc
.
instructions
.
end
();
++
ins
)
{
os
<<
"
\t
#"
<<
cmdnum
++<<
" "
<<
ins
->
cmd
;
// <<" "<<ins->args <<endl;
for
(
unsigned
int
i
=
0
;
i
<
ins
->
args
.
size
();
++
i
)
{
os
<<
" "
<<
ins
->
args
[
i
];
}
os
<<
endl
;
}
}
}
return
;
}
dgDEBUG
(
15
)
<<
" Calling procedure <"
<<
cmdLine
<<
"> "
<<
endl
;
ProcedureList
::
iterator
pair
=
procedureList
.
find
(
cmdLine
);
if
(
pair
==
procedureList
.
end
()
)
{
DG_THROW
ExceptionFactory
(
ExceptionFactory
::
UNREFERED_FUNCTION
,
"Undefined procedure"
,
": procedure <%s> not defined."
,
cmdLine
.
c_str
()
);
}
/* You need a copy here, in case the proc is removed from the
* list by itself:
* % proc next
* % -> proc next
* % -> -> echo TOTO
* % -> endproc
* % endproc
*/
Procedure
proc
=
pair
->
second
;
std
::
vector
<
std
::
string
>
paramValue
;
for
(
unsigned
int
i
=
0
;
i
<
proc
.
params
.
size
();
++
i
)
{
args
>>
ws
;
if
(
args
.
good
()
)
{
std
::
string
next
;
args
>>
next
>>
ws
;
paramValue
.
push_back
(
next
);
dgDEBUG
(
25
)
<<
"Args : <"
<<
next
<<
">"
<<
endl
;
}
else
{
paramValue
.
push_back
(
""
);
}
}
istringstream
iss
;
ostringstream
oss
;
for
(
std
::
list
<
Instruction
>::
iterator
ins
=
proc
.
instructions
.
begin
();
ins
!=
proc
.
instructions
.
end
();
++
ins
)
{
dgDEBUG
(
15
)
<<
"Proc <"
<<
cmdLine
<<
"> : "
<<
ins
->
cmd
<<
endl
;
oss
.
clear
();
oss
.
str
(
""
);
for
(
unsigned
int
i
=
0
;
i
<
ins
->
params
.
size
();
++
i
)
{
int
paramArg
=
ins
->
params
[
i
];
if
(
paramArg
==-
1
)
oss
<<
ins
->
args
[
i
]
<<
" "
;
else
oss
<<
paramValue
[
paramArg
]
<<
" "
;
}
dgDEBUG
(
15
)
<<
" Args = "
<<
oss
.
str
()
<<
endl
;
iss
.
str
(
oss
.
str
());
iss
.
clear
();
g_shell
.
cmd
(
ins
->
cmd
,
iss
,
os
);
}
}
void
ShellProcedure
::
cmdFor
(
const
std
::
string
&
cmdLine
,
std
::
istringstream
&
args
,
std
::
ostream
&
os
)
{
if
(
cmdLine
==
"help"
)
{
os
<<
" - for 1 5 instruction "
<<
endl
;
return
;
}
std
::
string
cmd2
,
idx
;
int
istart
,
iend
;
{
stringstream
oss
;
args
>>
cmd2
;
oss
.
str
(
cmd2
);
const
unsigned
int
SIZE
=
32
;
char
b1
[
SIZE
],
b2
[
SIZE
],
b3
[
SIZE
];
oss
.
getline
(
b1
,
SIZE
,
'='
);
oss
.
getline
(
b2
,
SIZE
,
':'
);
oss
.
getline
(
b3
,
SIZE
);
dgDEBUG
(
15
)
<<
b1
<<
"/"
<<
b2
<<
"/"
<<
b3
<<
endl
;
idx
=
b1
;
istart
=
atoi
(
b2
);
iend
=
atoi
(
b3
);
args
>>
cmd2
;
dgDEBUG
(
15
)
<<
"FOR <"
<<
idx
<<
"> = "
<<
istart
<<
" TO "
<<
iend
<<
" DO "
<<
cmd2
<<
endl
;
}
string
argsstr
;
{
const
unsigned
int
SIZE
=
1024
;
char
buffer
[
SIZE
];
args
.
getline
(
buffer
,
SIZE
);
argsstr
=
buffer
;
}
for
(
int
i
=
istart
;
i
<=
iend
;
++
i
)
{
istringstream
iss
;
stringstream
oss
;
std
::
string
insp
;
istringstream
issargs
(
argsstr
);
while
(
issargs
.
good
()
)
{
issargs
>>
insp
;
if
(
insp
==
idx
)
{
oss
<<
i
<<
" "
;
}
else
{
oss
<<
insp
<<
" "
;
}
}
iss
.
str
(
oss
.
str
()
);
g_shell
.
cmd
(
cmd2
,
iss
,
os
);
}
}
ShellProcedure
sotShellProceduror
;
extern
"C"
{
ShellFunctionRegisterer
regFun1
(
"proc"
,
boost
::
bind
(
&
ShellProcedure
::
cmdStartProcedure
,
&
sotShellProceduror
,
_1
,
_2
,
_3
)
);
ShellFunctionRegisterer
regFun2
(
"->"
,
boost
::
bind
(
&
ShellProcedure
::
cmdContinueProcedure
,
&
sotShellProceduror
,
_1
,
_2
,
_3
)
);
ShellFunctionRegisterer
regFun3
(
"endproc"
,
boost
::
bind
(
&
ShellProcedure
::
cmdEndProcedure
,
&
sotShellProceduror
,
_1
,
_2
,
_3
)
);
ShellFunctionRegisterer
regFun4
(
"for"
,
boost
::
bind
(
&
ShellProcedure
::
cmdFor
,
_1
,
_2
,
_3
)
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment