Browse Source

Implement a simple trim function. Use trim to get only the config string without any leading/trailing whitespace

Stanislav Galabov 11 years ago
parent
commit
013f60c901
4 changed files with 35 additions and 6 deletions
  1. 3 1
      plugins/lua/lua_init.c
  2. 1 0
      plugins/lua/module.am
  3. 28 3
      plugins/lua/plugin_init.c
  4. 3 2
      xml-examples/lua/startup.xml

+ 3 - 1
plugins/lua/lua_init.c

@@ -6,7 +6,7 @@
 
 #include "private.h"
 
-char *scripts_path = NULL;
+extern char *scripts_path = NULL;
 
 static bool_t
 load_scripts(lua_State *L, char *path)
@@ -71,8 +71,10 @@ int clish_plugin_init_lua(clish_shell_t *shell)
 	luaL_openlibs(L);
 
 	if (scripts_path && !load_scripts(L, scripts_path)) {
+		free(scripts_path);
 		return (-1);
 	}
+	free(scripts_path);
 
 	clish_shell__set_udata(shell, LUA_UDATA, L);
 

+ 1 - 0
plugins/lua/module.am

@@ -21,5 +21,6 @@ nobase_include_HEADERS		+= \
 clish_plugin_lua_la_SOURCES	+= \
 	plugins/lua/plugin_init.c \
 	plugins/lua/plugin_fini.c \
+	plugins/lua/lua_init.c \
 	plugins/lua/lua_action.c \
 	plugins/lua/lua_print_error.c

+ 28 - 3
plugins/lua/plugin_init.c

@@ -1,13 +1,38 @@
+#include <string.h>
+
 #include "private.h"
 
+static char * trim(char *str)
+{
+	size_t len = 0;
+	const char *first = str;
+	char *last = str + strlen(str) - 1;
+	char *new = NULL;
+	int i = 0;
+
+	while (first < last && isspace(*first))
+		++first;
+	while (first < last && isspace(*last))
+		--last;
+
+	len = last - first + 1;
+	new = malloc(len + 1);
+	bcopy(first, new, len);
+	new[len] = '\0';
+
+	return new;
+}
+
 CLISH_PLUGIN_INIT
 {
 	char *conf = clish_plugin__get_conf(plugin);
 
-	if (conf)
-		scripts_path = conf;
+	if (conf) {
+		scripts_path = trim(conf);
+	}
 
-	clish_plugin_init_lua((clish_shell_t *)clish_shell);
+	if(clish_plugin_init_lua((clish_shell_t *)clish_shell))
+		return (-1);
 
 	clish_plugin__set_name(plugin, "lua_hooks");
 

+ 3 - 2
xml-examples/lua/startup.xml

@@ -4,8 +4,9 @@
  xsi:schemaLocation="http://clish.sourceforge.net/XMLSchema
                      http://clish.sourceforge.net/XMLSchema/clish.xsd">
 
-     <PLUGIN name="lua_hooks"
-	     file="clish_plugin_lua.so">/usr2/klish-rw/xml-examples/lua</PLUGIN>
+     <PLUGIN name="lua_hooks" file="clish_plugin_lua.so">
+	 /usr2/klish-rw/xml-examples/lua
+     </PLUGIN>
      <STARTUP view="test-view" default_builtin="hook_action@lua_hooks"/>
 
 </CLISH_MODULE>