Browse Source

Another syntax '|' for the short commands.

Now the syntax to define short command is value="env|ironment" i.e.
'|' delimeter that saparates minimal command name from the rest.
The old syntax with the "minimal length" is not working now.
Serj Kalichev 4 months ago
parent
commit
2effa671fe
3 changed files with 33 additions and 15 deletions
  1. 1 1
      klish/kentry.h
  2. 3 3
      klish/kscheme/kscheme.c
  3. 29 11
      plugins/klish/ptype_command.c

+ 1 - 1
klish/kentry.h

@@ -51,7 +51,7 @@ typedef enum {
 } kentry_occurs_e;
 
 
-typedef bool_t (*kentry_udata_free_fn)(void *data);
+typedef void (*kentry_udata_free_fn)(void *data);
 
 
 C_DECL_BEGIN

+ 3 - 3
klish/kscheme/kscheme.c

@@ -78,11 +78,11 @@ void kscheme_free(kscheme_t *scheme)
 	if (!scheme)
 		return;
 
-	// kustore_free() must be before plugin_free() because plugin_free()
-	// does dlclose() and ustore free function is will not be accessible
 	kustore_free(scheme->ustore);
-	faux_list_free(scheme->plugins);
 	faux_list_free(scheme->entrys);
+	// The plugin_free() must be after all other free functions because
+	// plugins contain free callback function for the other components.
+	faux_list_free(scheme->plugins);
 
 	faux_free(scheme);
 }

+ 29 - 11
plugins/klish/ptype_command.c

@@ -36,30 +36,45 @@
 
 // User data structure to pre-parse config setting.
 typedef struct {
-	const char *cmd; // command (not null-terminated) with "len" length
+	char *cmd; // command (not null-terminated) with "len" length
 	size_t len; // length of command string
 	size_t min_len; // minimal length to satisfy string comparison
 } klish_ptype_COMMAND_t;
 
 
+static void klish_ptype_COMMAND_free(void *data)
+{
+	klish_ptype_COMMAND_t *udata = (klish_ptype_COMMAND_t *)data;
+
+	faux_str_free(udata->cmd);
+	faux_free(udata);
+}
+
+
 klish_ptype_COMMAND_t *klish_ptype_COMMAND_init(kentry_t *entry)
 {
 	klish_ptype_COMMAND_t *udata = NULL;
 	const char *cmd = NULL;
+	char *dupcmd = NULL;
 	size_t len = 0;
 	size_t min_len = 0;
-	char *space = NULL;
+	char *delim = NULL;
 
 	cmd = kentry_value(entry);
 	if (cmd) {
-		space = strchr(cmd, ' ');
-		if (space) {
-			unsigned char val = 0;
-			if (!faux_conv_atouc((char *)(space + 1), &val, 10))
+		delim = strchr(cmd, '|');
+		if (delim) {
+			min_len = delim - cmd;
+			len = strlen(cmd);
+			dupcmd = faux_malloc(len);
+			assert(dupcmd);
+			if (!dupcmd)
 				return NULL;
-			len = space - cmd;
-			min_len = (val < len) ? val : len;
+			memcpy(dupcmd, cmd, min_len);
+			memcpy(dupcmd + min_len, delim + 1, len - min_len);
+			len--; // exclude trailing '\0'
 		} else {
+			dupcmd = faux_str_dup(cmd);
 			len = strlen(cmd);
 			min_len = len;
 		}
@@ -67,20 +82,23 @@ klish_ptype_COMMAND_t *klish_ptype_COMMAND_init(kentry_t *entry)
 		cmd = kentry_name(entry);
 		if (!cmd)
 			return NULL;
+		dupcmd = faux_str_dup(cmd);
 		len = strlen(cmd);
 		min_len = len;
 	}
 
 	udata = faux_malloc(sizeof(*udata));
 	assert(udata);
-	if (!udata)
+	if (!udata) {
+		faux_free(dupcmd);
 		return NULL;
+	}
 
-	udata->cmd = cmd;
+	udata->cmd = dupcmd;
 	udata->len = len;
 	udata->min_len = min_len;
 
-	kentry_set_udata(entry, udata, (kentry_udata_free_fn)faux_free);
+	kentry_set_udata(entry, udata, klish_ptype_COMMAND_free);
 
 	return udata;
 }