Преглед изворни кода

scheme: ischeme to text for iptype and iaction

Serj Kalichev пре 3 година
родитељ
комит
f670900bd0

+ 19 - 3
bin/klishd/klishd.c

@@ -40,14 +40,30 @@
 
 ischeme_t sch = {
 
-  PTYPE_LIST
+ PTYPE_LIST
 
-    PTYPE {
-      .name = "ptype1",
+  PTYPE {
+   .name = "ptype1",
+   .help = "help1",
+
+   ACTION_LIST
+
+    ACTION {
+     .sym = "internal",
+     .script = "cat /etc/passwd",
     },
 
+    ACTION {
+     .sym = "internal",
+     .script = "cat /etc/group",
+    },
+
+   END_ACTION_LIST,
+  },
+
     PTYPE {
       .name = "ptype2",
+      .help = "help2",
     },
 
   END_PTYPE_LIST,

+ 8 - 0
klish/kaction.h

@@ -17,6 +17,7 @@ typedef struct iaction_s {
 	char *interactive;
 	char *exec_on;
 	char *update_retcode;
+	char *script;
 } iaction_t;
 
 
@@ -30,6 +31,7 @@ typedef enum {
 	KACTION_ERROR_ATTR_INTERACTIVE,
 	KACTION_ERROR_ATTR_EXEC_ON,
 	KACTION_ERROR_ATTR_UPDATE_RETCODE,
+	KACTION_ERROR_ATTR_SCRIPT,
 } kaction_error_e;
 
 
@@ -42,6 +44,10 @@ typedef enum {
 
 C_DECL_BEGIN
 
+// iaction_t
+char *iaction_to_text(const iaction_t *iaction, int level);
+
+// kaction_t
 kaction_t *kaction_new(const iaction_t *info, kaction_error_e *error);
 void kaction_free(kaction_t *action);
 const char *kaction_strerror(kaction_error_e error);
@@ -59,6 +65,8 @@ kaction_cond_e kaction_exec_on(const kaction_t *action);
 bool_t kaction_set_exec_on(kaction_t *action, kaction_cond_e exec_on);
 bool_t kaction_update_retcode(const kaction_t *action);
 bool_t kaction_set_update_retcode(kaction_t *action, bool_t update_retcode);
+const char *kaction_script(const kaction_t *action);
+bool_t kaction_set_script(kaction_t *action, const char *script);
 
 kaction_t *kaction_from_iaction(iaction_t *iaction, faux_error_t *error_stack);
 

+ 6 - 0
klish/khelper.h

@@ -127,4 +127,10 @@
 }
 
 
+C_DECL_BEGIN
+
+bool_t attr2ctext(char **dst, const char *field, const char *value, int level);
+
+C_DECL_END
+
 #endif // _khelper_h

+ 3 - 1
klish/kscheme/Makefile.am

@@ -1,4 +1,5 @@
 libklish_la_SOURCES += \
+	klish/kscheme/khelper.c \
 	klish/kscheme/kptype.c \
 	klish/kscheme/kaction.c \
 	klish/kscheme/kparam.c \
@@ -6,6 +7,7 @@ libklish_la_SOURCES += \
 	klish/kscheme/kview.c \
 	klish/kscheme/kscheme.c \
 	klish/kscheme/ischeme.c \
-	klish/kscheme/iptype.c
+	klish/kscheme/iptype.c \
+	klish/kscheme/iaction.c
 
 

+ 34 - 0
klish/kscheme/iaction.c

@@ -0,0 +1,34 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+
+#include <faux/str.h>
+#include <faux/conv.h>
+#include <klish/khelper.h>
+#include <klish/kaction.h>
+
+
+char *iaction_to_text(const iaction_t *iaction, int level)
+{
+	char *str = NULL;
+	char *tmp = NULL;
+
+	tmp = faux_str_sprintf("%*cACTION = {\n", level, ' ');
+	faux_str_cat(&str, tmp);
+	faux_str_free(tmp);
+
+	attr2ctext(&str, "sym", iaction->sym, level + 1);
+	attr2ctext(&str, "lock", iaction->lock, level + 1);
+	attr2ctext(&str, "interrupt", iaction->interrupt, level + 1);
+	attr2ctext(&str, "interactive", iaction->interactive, level + 1);
+	attr2ctext(&str, "exec_on", iaction->exec_on, level + 1);
+	attr2ctext(&str, "update_retcode", iaction->update_retcode, level + 1);
+	attr2ctext(&str, "script", iaction->script, level + 1);
+
+	tmp = faux_str_sprintf("%*c},\n\n", level, ' ');
+	faux_str_cat(&str, tmp);
+	faux_str_free(tmp);
+
+	return str;
+}

+ 13 - 13
klish/kscheme/iptype.c

@@ -4,11 +4,11 @@
 #include <assert.h>
 
 #include <faux/str.h>
+#include <faux/conv.h>
 #include <klish/khelper.h>
 #include <klish/kptype.h>
 #include <klish/kaction.h>
 
-#define ISCHEME_TAB " "
 
 char *iptype_to_text(const iptype_t *iptype, int level)
 {
@@ -19,33 +19,33 @@ char *iptype_to_text(const iptype_t *iptype, int level)
 	faux_str_cat(&str, tmp);
 	faux_str_free(tmp);
 
+	attr2ctext(&str, "name", iptype->name, level + 1);
+	attr2ctext(&str, "help", iptype->help, level + 1);
 
-/*
-	// PTYPE list
-	if (iptype->ptypes) {
-		iptype_t **p_iptype = NULL;
+	// ACTION list
+	if (iptype->actions) {
+		iaction_t **p_iaction = NULL;
 
-		tmp = faux_str_sprintf("\n%*sPTYPE_LIST\n\n", level + 1, ISCHEME_TAB);
+		tmp = faux_str_sprintf("\n%*cACTION_LIST\n\n", level + 1, ' ');
 		faux_str_cat(&str, tmp);
 		faux_str_free(tmp);
 
-		for (p_iptype = *iptype->ptypes; *p_iptype; p_iptype++) {
-			iptype_t *iptype = *p_iptype;
+		for (p_iaction = *iptype->actions; *p_iaction; p_iaction++) {
+			iaction_t *iaction = *p_iaction;
 
-			tmp = iptype_to_text(iptype, level + 2);
+			tmp = iaction_to_text(iaction, level + 2);
 			faux_str_cat(&str, tmp);
 			faux_str_free(tmp);
 		}
 
-		tmp = faux_str_sprintf("\n%*sEND_PTYPE_LIST,\n", level + 1, ISCHEME_TAB);
+		tmp = faux_str_sprintf("%*cEND_ACTION_LIST,\n", level + 1, ' ');
 		faux_str_cat(&str, tmp);
 		faux_str_free(tmp);
 	}
-*/
+
 	tmp = faux_str_sprintf("%*c},\n\n", level, ' ');
 	faux_str_cat(&str, tmp);
 	faux_str_free(tmp);
-iptype = iptype;
+
 	return str;
 }
-

+ 1 - 1
klish/kscheme/ischeme.c

@@ -35,7 +35,7 @@ char *ischeme_to_text(const ischeme_t *ischeme, int level)
 			faux_str_free(tmp);
 		}
 
-		tmp = faux_str_sprintf("\n%*cEND_PTYPE_LIST,\n", level + 1, ' ');
+		tmp = faux_str_sprintf("%*cEND_PTYPE_LIST,\n", level + 1, ' ');
 		faux_str_cat(&str, tmp);
 		faux_str_free(tmp);
 	}

+ 19 - 0
klish/kscheme/kaction.c

@@ -17,6 +17,7 @@ struct kaction_s {
 	bool_t interactive;
 	kaction_cond_e exec_on;
 	bool_t update_retcode;
+	char *script;
 	//ksym_t *sym; // Symbol
 };
 
@@ -47,6 +48,10 @@ KSET(action, kaction_cond_e, exec_on);
 KGET_BOOL(action, update_retcode);
 KSET_BOOL(action, update_retcode);
 
+// Script
+KGET_STR(action, script);
+KSET_STR(action, script);
+
 
 static kaction_t *kaction_new_empty(void)
 {
@@ -64,6 +69,7 @@ static kaction_t *kaction_new_empty(void)
 	action->interactive = BOOL_FALSE;
 	action->exec_on = KACTION_COND_SUCCESS;
 	action->update_retcode = BOOL_TRUE;
+	action->script = NULL;
 
 	return action;
 }
@@ -100,6 +106,7 @@ void kaction_free(kaction_t *action)
 
 	faux_str_free(action->sym_ref);
 	faux_str_free(action->lock);
+	faux_str_free(action->script);
 
 	faux_free(action);
 }
@@ -134,6 +141,9 @@ const char *kaction_strerror(kaction_error_e error)
 	case KACTION_ERROR_ATTR_UPDATE_RETCODE:
 		str = "Illegal 'update_retcode' attribute";
 		break;
+	case KACTION_ERROR_ATTR_SCRIPT:
+		str = "Illegal script";
+		break;
 	default:
 		str = "Unknown error";
 		break;
@@ -217,6 +227,15 @@ bool_t kaction_parse(kaction_t *action, const iaction_t *info, kaction_error_e *
 		}
 	}
 
+	// Script
+	if (!faux_str_is_empty(info->script)) {
+		if (!kaction_set_script(action, info->script)) {
+			if (error)
+				*error = KACTION_ERROR_ATTR_SCRIPT;
+			return BOOL_FALSE;
+		}
+	}
+
 	return BOOL_TRUE;
 }
 

+ 33 - 0
klish/kscheme/khelper.c

@@ -0,0 +1,33 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+
+#include <faux/str.h>
+#include <faux/conv.h>
+#include <klish/khelper.h>
+#include <klish/kptype.h>
+#include <klish/kaction.h>
+
+
+bool_t attr2ctext(char **dst, const char *field, const char *value, int level)
+{
+	char *tmp = NULL;
+	char *esc = NULL;
+
+	if (!field) // Error
+		return BOOL_FALSE;
+	if (faux_str_is_empty(value)) // Not error. Just empty field.
+		return BOOL_TRUE;
+
+	esc = faux_str_c_esc(value);
+	if (!esc)
+		return BOOL_FALSE;
+	tmp = faux_str_sprintf("%*c.%s = \"%s\",\n",
+		level, ' ', field, esc);
+	faux_str_free(esc);
+	faux_str_cat(dst, tmp);
+	faux_str_free(tmp);
+
+	return BOOL_TRUE;
+}