- use Tcl library for splitting and merging argument lists

This commit is contained in:
zolliker
2011-08-29 14:41:23 +00:00
parent 1e23f3c7b4
commit 56523419b2
5 changed files with 85 additions and 125 deletions

View File

@@ -19,7 +19,7 @@
typedef struct ContextItem {
struct ContextItem *next;
Hdb *node;
Hdb *controllerNode;
SctController *controller;
} ContextItem;
typedef struct ScriptContext {
@@ -55,7 +55,7 @@ static struct {
char *name;
} actionCallback;
void PushContext(Hdb * node, Hdb * controllerNode)
void PushContext(Hdb * node, SctController * controller)
{
ContextItem *new;
@@ -68,7 +68,7 @@ void PushContext(Hdb * node, Hdb * controllerNode)
new->next = sct->nodes;
sct->nodes = new;
new->node = node;
new->controllerNode = controllerNode;
new->controller = controller;
}
void PopContext(void)
@@ -91,8 +91,8 @@ void CleanStack(Hdb * node)
if (s->node == node) {
s->node = NULL;
}
if (s->controllerNode == node) {
s->controllerNode = NULL;
if (s->controller->node == node) {
s->controller = NULL;
}
}
}
@@ -163,17 +163,22 @@ int SctCommand(SConnection * con, SicsInterp * sics, void *object,
Hdb *node = NULL;
Hdb *cNode = NULL;
hdbValue v;
SctController *controller;
assert(sct == object);
if (sct->nodes != NULL) {
node = sct->nodes->node;
cNode = sct->nodes->controllerNode;
controller = sct->nodes->controller;
}
if (node == NULL && cNode == NULL) {
if (node == NULL || controller == NULL || controller->node == NULL) {
SCPrintf(con, eError, "ERROR: %s may be called only in proper context",
argv[0]);
return 0;
}
/*
* get path
*/
if (argc <= 1) {
GetHdbPath(node, value, sizeof value);
SCWrite(con, value, eValue);
@@ -214,7 +219,7 @@ int SctCommand(SConnection * con, SicsInterp * sics, void *object,
* controller
*/
if (strcmp(argv[1], "controller") == 0) {
SCWrite(con, cNode->name, eValue);
SCWrite(con, controller->node->name, eValue);
return 1;
}
@@ -244,11 +249,35 @@ int SctCommand(SConnection * con, SicsInterp * sics, void *object,
/* continue with property handling */
}
/*
* with command (execute a script in the context of an other node)
*/
if (strcmp(argv[1], "with") == 0) {
if (argc < 3) {
SCPrintf(con, eError, "ERROR: syntax must be: %s %s node script",
argv[0], argv[1]);
return 0;
}
GetHdbPath(node, value, sizeof value);
node = FindHdbNode(value, argv[2], con); /* get a relative path */
if (node == NULL) {
SCPrintf(con, eError, "ERROR: %s %s: node %s not found",
argv[0], argv[1], argv[2]);
return 0;
}
if (SctCallInContext(con, argv[3], node, controller, &val) == 0) {
SCPrintf(con, eError, "ERROR: in %s %s %s {%s}: %s",
argv[0], argv[1], argv[2], argv[3], val);
return 0;
}
return 1;
}
/*
* property handling
*/
if (argc == 2) { /* get case */
val = GetProp(node, cNode, argv[1]);
val = GetProp(node, controller->node, argv[1]);
if (val == NULL) {
SCPrintf(con, eError, "ERROR: %s %s not found", argv[0], argv[1]);
return 0;
@@ -256,10 +285,10 @@ int SctCommand(SConnection * con, SicsInterp * sics, void *object,
SCWrite(con, val, eValue);
} else { /* set case */
if (argc == 3) {
SetProp(node, cNode, argv[1], argv[2]);
SetProp(node, controller->node, argv[1], argv[2]);
} else {
val = Arg2Tcl(argc - 2, argv + 2, value, sizeof value);
SetProp(node, cNode, argv[1], val);
SetProp(node, controller->node, argv[1], val);
if (val != NULL && val != value)
free(val);
}
@@ -281,7 +310,7 @@ int SctCallInContext(SConnection * con, char *script, Hdb * node,
int iRet = 1;
int verbose = controller->verbose;
PushContext(node, controller->node);
PushContext(node, controller);
if (verbose) {
SCPrintf(con, eLog, "%6.3f script: %s", secondsOfMinute(), script);
}