diff mbox series

Cosmetic: move lua handler search path to log

Message ID 20201111212810.825205-1-sbabic@denx.de
State Accepted
Headers show
Series Cosmetic: move lua handler search path to log | expand

Commit Message

Stefano Babic Nov. 11, 2020, 9:28 p.m. UTC
Signed-off-by: Stefano Babic <sbabic@denx.de>
---
 corelib/lua_interface.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

Comments

Storm, Christian Nov. 13, 2020, 1:24 p.m. UTC | #1
Hi Stefano,

> Signed-off-by: Stefano Babic <sbabic@denx.de>
> ---
>  corelib/lua_interface.c | 14 +++++++++++++-
>  1 file changed, 13 insertions(+), 1 deletion(-)
> 
> diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
> index 192563b..0f1b4f5 100644
> --- a/corelib/lua_interface.c
> +++ b/corelib/lua_interface.c
> @@ -1114,8 +1114,20 @@ int lua_handlers_init(void)
>  		if ((ret = luaL_dostring(gL, "require (\"swupdate_handlers\")")) != 0) {
>  			INFO("No Lua handler(s) found.");
>  			if (luaL_dostring(gL, "return package.path:gsub(';','\\n'):gsub('?','swupdate_handlers')") == 0) {
> +				TRACE("Lua handler search path:");
>  				lua_pop(gL, 1);
> -				TRACE("Lua handler search path:\n%s", lua_tostring(gL, -1));
> +				char *s = strdupa(lua_tostring(gL, -1));
> +				char *s1 = s;
> +				char *lf;
> +
> +				do {
> +					lf = strchr(s1, '\n');
> +					if (lf)
> +						*lf = '\0';
> +					TRACE("\t%s", s1);
> +					if (lf)
> +						s1 = ++lf;
> +				} while (lf);

Hm, why not make use of gsub()'s 2nd return value being the number of
items returned, like the following?:


if (luaL_dostring(gL, "return package.path:gsub('?','swupdate_handlers'):gsub(';','\\0')") == 0) {
	const char *paths = lua_tostring(gL, -2);
	for (int i=lua_tonumber(gL, -1); i >= 0; i--) {
		TRACE("\t%s", paths);
		paths += strlen(paths) + 1;
	}
	lua_pop(gL, 2);
}


Kind regards,
   Christian
Stefano Babic Nov. 13, 2020, 3:39 p.m. UTC | #2
Hi Christian,

On 13.11.20 14:24, Christian Storm wrote:
> Hi Stefano,
> 
>> Signed-off-by: Stefano Babic <sbabic@denx.de>
>> ---
>>  corelib/lua_interface.c | 14 +++++++++++++-
>>  1 file changed, 13 insertions(+), 1 deletion(-)
>>
>> diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
>> index 192563b..0f1b4f5 100644
>> --- a/corelib/lua_interface.c
>> +++ b/corelib/lua_interface.c
>> @@ -1114,8 +1114,20 @@ int lua_handlers_init(void)
>>  		if ((ret = luaL_dostring(gL, "require (\"swupdate_handlers\")")) != 0) {
>>  			INFO("No Lua handler(s) found.");
>>  			if (luaL_dostring(gL, "return package.path:gsub(';','\\n'):gsub('?','swupdate_handlers')") == 0) {
>> +				TRACE("Lua handler search path:");
>>  				lua_pop(gL, 1);
>> -				TRACE("Lua handler search path:\n%s", lua_tostring(gL, -1));
>> +				char *s = strdupa(lua_tostring(gL, -1));
>> +				char *s1 = s;
>> +				char *lf;
>> +
>> +				do {
>> +					lf = strchr(s1, '\n');
>> +					if (lf)
>> +						*lf = '\0';
>> +					TRACE("\t%s", s1);
>> +					if (lf)
>> +						s1 = ++lf;
>> +				} while (lf);
> 
> Hm, why not make use of gsub()'s 2nd return value being the number of
> items returned, like the following?:
> 

This is a great idea instead of doing this in code, many thanks !

> 
> if (luaL_dostring(gL, "return package.path:gsub('?','swupdate_handlers'):gsub(';','\\0')") == 0) {
> 	const char *paths = lua_tostring(gL, -2);
> 	for (int i=lua_tonumber(gL, -1); i >= 0; i--) {
> 		TRACE("\t%s", paths);
> 		paths += strlen(paths) + 1;
> 	}
> 	lua_pop(gL, 2);
> }

Nice !

I fix it !

Best regards,
Stefano
diff mbox series

Patch

diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
index 192563b..0f1b4f5 100644
--- a/corelib/lua_interface.c
+++ b/corelib/lua_interface.c
@@ -1114,8 +1114,20 @@  int lua_handlers_init(void)
 		if ((ret = luaL_dostring(gL, "require (\"swupdate_handlers\")")) != 0) {
 			INFO("No Lua handler(s) found.");
 			if (luaL_dostring(gL, "return package.path:gsub(';','\\n'):gsub('?','swupdate_handlers')") == 0) {
+				TRACE("Lua handler search path:");
 				lua_pop(gL, 1);
-				TRACE("Lua handler search path:\n%s", lua_tostring(gL, -1));
+				char *s = strdupa(lua_tostring(gL, -1));
+				char *s1 = s;
+				char *lf;
+
+				do {
+					lf = strchr(s1, '\n');
+					if (lf)
+						*lf = '\0';
+					TRACE("\t%s", s1);
+					if (lf)
+						s1 = ++lf;
+				} while (lf);
 				lua_pop(gL, 1);
 			}
 		} else {