Author: lslezak
Date: Tue Feb 10 16:13:51 2009
New Revision: 55395
URL: http://svn.opensuse.org/viewcvs/yast?rev=55395&view=rev
Log:
- DBus service - added NULL checks, string->YCPTerm automatic conversion
Modified:
branches/tmp/lslezak/core/dbus/namespace_service/DBusModulesServer.cc
Modified: branches/tmp/lslezak/core/dbus/namespace_service/DBusModulesServer.cc
URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/lslezak/core/dbus/namespace_service/DBusModulesServer.cc?rev=55395&r1=55394&r2=55395&view=diff
==============================================================================
--- branches/tmp/lslezak/core/dbus/namespace_service/DBusModulesServer.cc (original)
+++ branches/tmp/lslezak/core/dbus/namespace_service/DBusModulesServer.cc Tue Feb 10 16:13:51 2009
@@ -14,6 +14,7 @@
#include
#include
#include
+#include
#include
#include
@@ -187,7 +188,7 @@
DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING DBUS_TYPE_STRING_AS_STRING
DBUS_TYPE_VARIANT_AS_STRING DBUS_DICT_ENTRY_END_CHAR_AS_STRING; break; /* a{sv} */
case(YT_TERM) : ret = DBUS_TYPE_STRING_AS_STRING; break;
- case(YT_CODE) : ret = DBUS_TYPE_STRING_AS_STRING; break;
+// case(YT_CODE) : ret = DBUS_TYPE_STRING_AS_STRING; break;
default : y2warning("Value type %d is not supported", vt); break;
}
@@ -238,7 +239,7 @@
}
/*
- convert string starting with '`' to YCPSymbol
+ convert string starting with '`' to YCPSymbol or YCPTerm
convert string starting with '.' to YCPPath
*/
YCPValue DBusModulesServer::AutoCast(const YCPValue &value, constTypePtr requested_type)
@@ -251,8 +252,11 @@
{
if (!value_str.empty() && value_str[0] == '`')
{
+ // remove the leading backtick
+ value_str.erase(value_str.begin());
+
y2milestone("Automatic cast: string -> YCPSymbol");
- return YCPSymbol(value_str);
+ return YCPSymbol(std::string(value_str));
}
}
else if (requested_type->isPath())
@@ -263,6 +267,40 @@
return YCPPath(value_str);
}
}
+ else if (requested_type->isTerm())
+ {
+ if (!value_str.empty() && value_str[0] == '`')
+ {
+ y2milestone("Automatic cast: string -> YCPTerm");
+
+ // parse the string, recreate the YCPTerm again
+ Parser parser(value_str.c_str());
+ parser.setBuffered();
+ YCodePtr p = parser.parse();
+ YCPValue contents = YCPNull ();
+
+ if (!p)
+ {
+ y2error("Parse error in YCP value: %s", value_str.c_str());
+ }
+ else
+ {
+ // for security reasons do the evaluation only if it's a term!
+ constTypePtr tptr = p->type();
+
+ if (tptr && tptr->isTerm())
+ {
+ return p->evaluate(true);
+ }
+
+ y2error("Not an YCP term: %s", value_str.c_str());
+ }
+ }
+ }
+ else
+ {
+ y2debug("Automatic cast is not defined for the current arguments");
+ }
}
return value;
@@ -289,74 +327,99 @@
if (t)
{
- constFunctionTypePtr fptr = constFunctionTypePtr(t);
- int reqarg = fptr->parameterCount();
+ constFunctionTypePtr fptr(t);
- if (request.arguments() == reqarg)
+ if (fptr)
{
- // TODO improve it, Y2Namespace was already found in searchFuncType()
- Y2Namespace *ns = nsmap[object];
+ int reqarg = fptr->parameterCount();
- if (ns)
+ if (request.arguments() == reqarg)
{
- // create function call
- Y2Function *y2func = ns->createFunctionCall(method.c_str(), t);
+ // TODO improve it, Y2Namespace was already found in searchFuncType()
+ Y2Namespace *ns = nsmap[object];
- if (y2func)
+ if (ns)
{
- int index = 0;
- bool valid_arguments = true;
+ // create function call
+ Y2Function *y2func = ns->createFunctionCall(method.c_str(), t);
- while(index < reqarg)
+ if (y2func)
{
- YCPValue arg = request.getYCPValue(index);
+ int index = 0;
+ bool valid_arguments = true;
- // check the data type compatibility
- constTypePtr argtype(fptr->parameterType(index));
-
- if (argtype->matchvalue(arg) < 0)
+ while(index < reqarg)
{
- if (interface == "org.opensuse.yast.modules.Values")
- {
- // try converting DBus types to YCPValues in the raw interface
- arg = AutoCast(arg, argtype);
- // do the typecheck again
- valid_arguments = argtype->matchvalue(arg) < 0;
- }
- else
+ YCPValue arg = request.getYCPValue(index);
+
+ if (arg.isNull())
{
+ y2error("NULL parameter");
valid_arguments = false;
+ break;
}
- if (!valid_arguments)
+ // check the data type compatibility
+ constTypePtr argtype(fptr->parameterType(index));
+
+ if (argtype->matchvalue(arg) < 0)
{
- y2error("Invalid argument %d (%s): Function %s::%s() requires type '%s' instead of '%s'",
- index, arg->toString().c_str(), object.c_str(), method.c_str(),
- argtype->toString().c_str(), Type::vt2type(arg->valuetype())->toString().c_str()
- );
- break;
+ if (interface == "org.opensuse.yast.modules.Values")
+ {
+ // try converting DBus types to YCPValues in the raw interface
+ YCPValue casted = AutoCast(arg, argtype);
+
+ if (casted.isNull())
+ {
+ y2error("Automatic cast failed (nil returned)");
+ valid_arguments = false;
+ }
+ else
+ {
+ arg = casted;
+ // do the typecheck again
+ valid_arguments = argtype->matchvalue(arg) >= 0;
+ }
+ }
+ else
+ {
+ valid_arguments = false;
+ }
+
+ if (!valid_arguments)
+ {
+ y2error("Invalid argument %d (%s): Function %s::%s() requires type '%s' instead of '%s'",
+ index, arg->toString().c_str(), object.c_str(), method.c_str(),
+ argtype->toString().c_str(), Type::vt2type(arg->valuetype())->toString().c_str()
+ );
+ break;
+ }
}
- }
- y2func->appendParameter(arg);
- index ++;
- }
+ y2func->appendParameter(arg);
+ index ++;
+ }
- if (valid_arguments && y2func->finishParameters())
- {
- // call the function
- ret = y2func->evaluateCall();
- }
- else
- {
- y2error("Wrong parameters to function %s", method.c_str());
+ if (valid_arguments && y2func->finishParameters())
+ {
+ // call the function
+ ret = y2func->evaluateCall();
+ }
+ else
+ {
+ y2error("Wrong parameters to function %s", method.c_str());
+ }
}
}
}
+ else
+ {
+ y2error("Function %s got %d parameters instead of %d", method.c_str(), request.arguments(), fptr->parameterCount());
+ }
}
else
{
- y2error("Function %s got %d parameters instead of %d", method.c_str(), request.arguments(), fptr->parameterCount());
+ y2internal("Function %s was not found although it was registered", method.c_str());
}
}
else
--
To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org
For additional commands, e-mail: yast-commit+help@opensuse.org