Explorar o código

entry: Handle aliases (ref)

Serj Kalichev %!s(int64=2) %!d(string=hai) anos
pai
achega
088cf66f72
Modificáronse 3 ficheiros con 34 adicións e 7 borrados
  1. 0 2
      klish/kentry.h
  2. 21 5
      klish/kscheme/kentry.c
  3. 13 0
      klish/kscheme/kscheme.c

+ 0 - 2
klish/kentry.h

@@ -64,8 +64,6 @@ bool_t kentry_set_ptype(kentry_t *entry, kentry_t *ptype);
 // Ref
 const char *kentry_ref_str(const kentry_t *entry);
 bool_t kentry_set_ref_str(kentry_t *entry, const char *ref_str);
-kentry_t *kentry_ref(const kentry_t *entry);
-bool_t kentry_set_ref(kentry_t *entry, kentry_t *ptype);
 // Value
 const char *kentry_value(const kentry_t *entry);
 bool_t kentry_set_value(kentry_t *entry, const char *value);

+ 21 - 5
klish/kscheme/kentry.c

@@ -22,7 +22,6 @@ struct kentry_s {
 	char *ptype_str; // Text reference to PTYPE
 	kentry_t *ptype; // Resolved entry's PTYPE
 	char *ref_str; // Text reference to aliased ENTRY
-	kentry_t *ref; // Resolved aliased ENTRY
 	char *value; // Additional info
 	bool_t restore; // Should entry restore its depth while execution
 	faux_list_t *entrys; // Nested ENTRYs
@@ -69,9 +68,6 @@ KSET(entry, kentry_t *, ptype);
 // Ref string (must be resolved later)
 KGET_STR(entry, ref_str);
 KSET_STR(entry, ref_str);
-// Ref (resolved)
-KGET(entry, kentry_t *, ref);
-KSET(entry, kentry_t *, ref);
 
 // Value
 KGET_STR(entry, value);
@@ -121,7 +117,6 @@ kentry_t *kentry_new(const char *name)
 	entry->ptype_str = NULL;
 	entry->ptype = NULL;
 	entry->ref_str = NULL;
-	entry->ref = NULL;
 	entry->value = NULL;
 	entry->restore = BOOL_FALSE;
 
@@ -189,5 +184,26 @@ bool_t kentry_link(kentry_t *dst, const kentry_t *src)
 	if (!src)
 		return BOOL_FALSE;
 
+	// Free all fields that will be linker to src later
+	kentry_free_non_link(dst);
+
+	// Copy structure by hand because else some fields must be
+	// returned back anyway and temp memory must be allocated. I think it
+	// worse.
+	// name - orig
+	// help - orig
+	// parent - orig
+	dst->container = src->container;
+	dst->mode = src->mode;
+	dst->min = src->min;
+	dst->max = src->max;
+	dst->ptype_str = src->ptype_str;
+	dst->ptype = src->ptype;
+	// ref_str - orig
+	dst->value = src->value;
+	dst->restore = src->restore;
+	dst->entrys = src->entrys;
+	dst->actions = src->actions;
+
 	return BOOL_TRUE;
 }

+ 13 - 0
klish/kscheme/kscheme.c

@@ -346,6 +346,7 @@ kentry_t *kscheme_find_entry_by_path(const kscheme_t *scheme, const char *name)
 	// within scheme.
 	full_name = faux_str_dup(name);
 	entry_name = strtok_r(full_name, delim, &saveptr);
+printf("ENTRY name=%s\n", entry_name);
 	if (!entry_name) {
 		faux_str_free(full_name);
 		return NULL;
@@ -376,6 +377,7 @@ bool_t kscheme_prepare_entry(kscheme_t *scheme, kentry_t *entry,
 	bool_t retcode = BOOL_TRUE;
 	const char *ref = NULL;
 	kentry_t *ref_entry = NULL;
+	const char *ptype_str = NULL;
 
 	assert(scheme);
 	if (!scheme)
@@ -400,6 +402,17 @@ bool_t kscheme_prepare_entry(kscheme_t *scheme, kentry_t *entry,
 		}
 	}
 
+	// Resolve ptype's ENTRY
+	if ((ptype_str = kentry_ptype_str(entry))) {
+		ref_entry = kscheme_find_entry_by_path(scheme, ptype_str);
+		if (!ref_entry) {
+			faux_error_sprintf(error, "Can't find ENTRY \"%s\" for ptype",
+				ptype_str);
+			return BOOL_FALSE;
+		}
+		kentry_set_ptype(entry, ref_entry);
+	}
+
 	// Process nested ENTRYs
 	iter = kentry_entrys_iter(entry);
 	while ((nested_entry = kentry_entrys_each(&iter))) {