浏览代码

scheme: Add nested elements

Serj Kalichev 4 年之前
父节点
当前提交
ef628d0253
共有 7 个文件被更改,包括 129 次插入23 次删除
  1. 2 2
      bin/klishd/klishd.c
  2. 1 0
      klish/kcommand.h
  3. 62 10
      klish/kscheme/kcommand.c
  4. 19 1
      klish/kscheme/kparam.c
  5. 26 8
      klish/kscheme/kptype.c
  6. 1 1
      klish/kscheme/kscheme.c
  7. 18 1
      klish/kscheme/kview.c

+ 2 - 2
bin/klishd/klishd.c

@@ -97,8 +97,8 @@ ischeme_t sch = {
       END_COMMAND_LIST,
       END_COMMAND_LIST,
     },
     },
 
 
-    VIEW {
-    },
+//    VIEW {
+//    },
 
 
   END_VIEW_LIST,
   END_VIEW_LIST,
 };
 };

+ 1 - 0
klish/kcommand.h

@@ -41,6 +41,7 @@ bool_t kcommand_set_help(kcommand_t *command, const char *help);
 
 
 bool_t kcommand_add_param(kcommand_t *command, kparam_t *param);
 bool_t kcommand_add_param(kcommand_t *command, kparam_t *param);
 kparam_t *kcommand_find_param(const kcommand_t *command, const char *name);
 kparam_t *kcommand_find_param(const kcommand_t *command, const char *name);
+bool_t kcommand_add_action(kcommand_t *command, kaction_t *action);
 
 
 bool_t kcommand_nested_from_icommand(kcommand_t *kcommand, icommand_t *icommand,
 bool_t kcommand_nested_from_icommand(kcommand_t *kcommand, icommand_t *icommand,
 	faux_error_t *error_stack);
 	faux_error_t *error_stack);

+ 62 - 10
klish/kscheme/kcommand.c

@@ -8,6 +8,7 @@
 #include <faux/error.h>
 #include <faux/error.h>
 #include <klish/khelper.h>
 #include <klish/khelper.h>
 #include <klish/kparam.h>
 #include <klish/kparam.h>
+#include <klish/kaction.h>
 #include <klish/kcommand.h>
 #include <klish/kcommand.h>
 
 
 
 
@@ -15,6 +16,7 @@ struct kcommand_s {
 	char *name;
 	char *name;
 	char *help;
 	char *help;
 	faux_list_t *params;
 	faux_list_t *params;
+	faux_list_t *actions;
 };
 };
 
 
 // Simple attributes
 // Simple attributes
@@ -33,6 +35,9 @@ static KCMP_NESTED_BY_KEY(command, param, name);
 KADD_NESTED(command, param);
 KADD_NESTED(command, param);
 KFIND_NESTED(command, param);
 KFIND_NESTED(command, param);
 
 
+// ACTION list
+KADD_NESTED(command, action);
+
 
 
 static kcommand_t *kcommand_new_empty(void)
 static kcommand_t *kcommand_new_empty(void)
 {
 {
@@ -47,11 +52,17 @@ static kcommand_t *kcommand_new_empty(void)
 	command->name = NULL;
 	command->name = NULL;
 	command->help = NULL;
 	command->help = NULL;
 
 
+	// PARAM list
 	command->params = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
 	command->params = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
 		kcommand_param_compare, kcommand_param_kcompare,
 		kcommand_param_compare, kcommand_param_kcompare,
 		(void (*)(void *))kcommand_free);
 		(void (*)(void *))kcommand_free);
 	assert(command->params);
 	assert(command->params);
 
 
+	// ACTION list
+	command->actions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
+		NULL, NULL, (void (*)(void *))kaction_free);
+	assert(command->actions);
+
 	return command;
 	return command;
 }
 }
 
 
@@ -88,6 +99,7 @@ void kcommand_free(kcommand_t *command)
 	faux_str_free(command->name);
 	faux_str_free(command->name);
 	faux_str_free(command->help);
 	faux_str_free(command->help);
 	faux_list_free(command->params);
 	faux_list_free(command->params);
+	faux_list_free(command->actions);
 
 
 	faux_free(command);
 	faux_free(command);
 }
 }
@@ -161,24 +173,64 @@ bool_t kcommand_nested_from_icommand(kcommand_t *kcommand, icommand_t *icommand,
 		return BOOL_FALSE;
 		return BOOL_FALSE;
 	}
 	}
 
 
-/*
+
+	// PARAM list
+	if (icommand->params) {
+		iparam_t **p_iparam = NULL;
+		for (p_iparam = *icommand->params; *p_iparam; p_iparam++) {
+			kparam_t *kparam = NULL;
+			iparam_t *iparam = *p_iparam;
+
+			kparam = kparam_from_iparam(iparam, error_stack);
+			if (!kparam) {
+				retval = BOOL_FALSE;
+				continue;
+			}
+			if (!kcommand_add_param(kcommand, kparam)) {
+				char *msg = NULL;
+				// Search for PARAM duplicates
+				if (kcommand_find_param(kcommand,
+					kparam_name(kparam))) {
+					msg = faux_str_sprintf("COMMAND: "
+						"Can't add duplicate PARAM "
+						"\"%s\"",
+						kparam_name(kparam));
+				} else {
+					msg = faux_str_sprintf("COMMAND: "
+						"Can't add PARAM \"%s\"",
+						kparam_name(kparam));
+				}
+				faux_error_add(error_stack, msg);
+				faux_str_free(msg);
+				retval = BOOL_FALSE;
+			}
+		}
+	}
+
 	// ACTION list
 	// ACTION list
 	if (icommand->actions) {
 	if (icommand->actions) {
 		iaction_t **p_iaction = NULL;
 		iaction_t **p_iaction = NULL;
 		for (p_iaction = *icommand->actions; *p_iaction; p_iaction++) {
 		for (p_iaction = *icommand->actions; *p_iaction; p_iaction++) {
 			kaction_t *kaction = NULL;
 			kaction_t *kaction = NULL;
 			iaction_t *iaction = *p_iaction;
 			iaction_t *iaction = *p_iaction;
-iaction = iaction;
-printf("action\n");
-//			kaction = kaction_from_iaction(iaction, error_stack);
-//			if (!kaction) {
-//				retval = BOOL_FALSE;
-//				continue;
-//			}
-kaction = kaction;
+
+			kaction = kaction_from_iaction(iaction, error_stack);
+			if (!kaction) {
+				retval = BOOL_FALSE;
+				continue;
+			}
+			if (!kcommand_add_action(kcommand, kaction)) {
+				char *msg = NULL;
+				msg = faux_str_sprintf("COMMAND: "
+					"Can't add ACTION #%d",
+					faux_list_len(kcommand->actions) + 1);
+				faux_error_add(error_stack, msg);
+				faux_str_free(msg);
+				retval = BOOL_FALSE;
+			}
 		}
 		}
 	}
 	}
-*/
+
 	return retval;
 	return retval;
 }
 }
 
 

+ 19 - 1
klish/kscheme/kparam.c

@@ -190,12 +190,30 @@ bool_t kparam_nested_from_iparam(kparam_t *kparam, iparam_t *iparam,
 		for (p_iparam = *iparam->params; *p_iparam; p_iparam++) {
 		for (p_iparam = *iparam->params; *p_iparam; p_iparam++) {
 			kparam_t *nkparam = NULL;
 			kparam_t *nkparam = NULL;
 			iparam_t *niparam = *p_iparam;
 			iparam_t *niparam = *p_iparam;
-printf("subparam\n");
+
 			nkparam = kparam_from_iparam(niparam, error_stack);
 			nkparam = kparam_from_iparam(niparam, error_stack);
 			if (!nkparam) {
 			if (!nkparam) {
 				retval = BOOL_FALSE;
 				retval = BOOL_FALSE;
 				continue;
 				continue;
 			}
 			}
+			if (!kparam_add_param(kparam, nkparam)) {
+				char *msg = NULL;
+				// Search for PARAM duplicates
+				if (kparam_find_param(kparam,
+					kparam_name(nkparam))) {
+					msg = faux_str_sprintf("PARAM: "
+						"Can't add duplicate PARAM "
+						"\"%s\"",
+						kparam_name(nkparam));
+				} else {
+					msg = faux_str_sprintf("PARAM: "
+						"Can't add PARAM \"%s\"",
+						kparam_name(nkparam));
+				}
+				faux_error_add(error_stack, msg);
+				faux_str_free(msg);
+				retval = BOOL_FALSE;
+			}
 		}
 		}
 	}
 	}
 
 

+ 26 - 8
klish/kscheme/kptype.c

@@ -8,11 +8,13 @@
 #include <faux/error.h>
 #include <faux/error.h>
 #include <klish/khelper.h>
 #include <klish/khelper.h>
 #include <klish/kptype.h>
 #include <klish/kptype.h>
+#include <klish/kaction.h>
 
 
 
 
 struct kptype_s {
 struct kptype_s {
 	char *name;
 	char *name;
 	char *help;
 	char *help;
+	faux_list_t *actions;
 };
 };
 
 
 
 
@@ -26,6 +28,9 @@ KSET_STR_ONCE(ptype, name);
 KGET_STR(ptype, help);
 KGET_STR(ptype, help);
 KSET_STR(ptype, help);
 KSET_STR(ptype, help);
 
 
+// ACTION list
+KADD_NESTED(ptype, action);
+
 
 
 static kptype_t *kptype_new_empty(void)
 static kptype_t *kptype_new_empty(void)
 {
 {
@@ -40,6 +45,11 @@ static kptype_t *kptype_new_empty(void)
 	ptype->name = NULL;
 	ptype->name = NULL;
 	ptype->help = NULL;
 	ptype->help = NULL;
 
 
+	// ACTION list
+	ptype->actions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
+		NULL, NULL, (void (*)(void *))kaction_free);
+	assert(ptype->actions);
+
 	return ptype;
 	return ptype;
 }
 }
 
 
@@ -75,6 +85,7 @@ void kptype_free(kptype_t *ptype)
 
 
 	faux_str_free(ptype->name);
 	faux_str_free(ptype->name);
 	faux_str_free(ptype->help);
 	faux_str_free(ptype->help);
+	faux_list_free(ptype->actions);
 
 
 	faux_free(ptype);
 	faux_free(ptype);
 }
 }
@@ -154,14 +165,21 @@ bool_t kptype_nested_from_iptype(kptype_t *kptype, iptype_t *iptype,
 		for (p_iaction = *iptype->actions; *p_iaction; p_iaction++) {
 		for (p_iaction = *iptype->actions; *p_iaction; p_iaction++) {
 			kaction_t *kaction = NULL;
 			kaction_t *kaction = NULL;
 			iaction_t *iaction = *p_iaction;
 			iaction_t *iaction = *p_iaction;
-iaction = iaction;
-printf("action\n");
-//			kaction = kaction_from_iaction(iaction, error_stack);
-//			if (!kaction) {
-//				retval = BOOL_FALSE;
-//				continue;
-//			}
-kaction = kaction;
+
+			kaction = kaction_from_iaction(iaction, error_stack);
+			if (!kaction) {
+				retval = BOOL_FALSE;
+				continue;
+			}
+			if (!kptype_add_action(kptype, kaction)) {
+				char *msg = NULL;
+				msg = faux_str_sprintf("PTYPE: "
+					"Can't add ACTION #%d",
+					faux_list_len(kptype->actions) + 1);
+				faux_error_add(error_stack, msg);
+				faux_str_free(msg);
+				retval = BOOL_FALSE;
+			}
 		}
 		}
 	}
 	}
 
 

+ 1 - 1
klish/kscheme/kscheme.c

@@ -132,8 +132,8 @@ bool_t kscheme_nested_from_ischeme(kscheme_t *kscheme, ischeme_t *ischeme,
 				}
 				}
 				faux_error_add(error_stack, msg);
 				faux_error_add(error_stack, msg);
 				faux_str_free(msg);
 				faux_str_free(msg);
+				retval = BOOL_FALSE;
 			}
 			}
-			retval = BOOL_FALSE;
 		}
 		}
 	}
 	}
 
 

+ 18 - 1
klish/kscheme/kview.c

@@ -148,12 +148,29 @@ bool_t kview_nested_from_iview(kview_t *kview, iview_t *iview,
 		for (p_icommand = *iview->commands; *p_icommand; p_icommand++) {
 		for (p_icommand = *iview->commands; *p_icommand; p_icommand++) {
 			kcommand_t *kcommand = NULL;
 			kcommand_t *kcommand = NULL;
 			icommand_t *icommand = *p_icommand;
 			icommand_t *icommand = *p_icommand;
-printf("command %s\n", icommand->name);
 			kcommand = kcommand_from_icommand(icommand, error_stack);
 			kcommand = kcommand_from_icommand(icommand, error_stack);
 			if (!kcommand) {
 			if (!kcommand) {
 				retval = BOOL_FALSE;
 				retval = BOOL_FALSE;
 				continue;
 				continue;
 			}
 			}
+			if (!kview_add_command(kview, kcommand)) {
+				char *msg = NULL;
+				// Search for COMMAND duplicates
+				if (kview_find_command(kview,
+					kcommand_name(kcommand))) {
+					msg = faux_str_sprintf("VIEW: "
+						"Can't add duplicate COMMAND "
+						"\"%s\"",
+						kcommand_name(kcommand));
+				} else {
+					msg = faux_str_sprintf("VIEW: "
+						"Can't add COMMAND \"%s\"",
+						kcommand_name(kcommand));
+				}
+				faux_error_add(error_stack, msg);
+				faux_str_free(msg);
+				retval = BOOL_FALSE;
+			}
 		}
 		}
 	}
 	}