Browse Source

scheme: Can build C-compatible source from ischeme

Serj Kalichev 3 years ago
parent
commit
78ae315cbb

+ 51 - 1
bin/klishd/klishd.c

@@ -106,8 +106,56 @@ ischeme_t sch = {
         },
 
         COMMAND {
-          .name = "command5",
+          .name = "command4",
           .help = "help1",
+
+   PARAM_LIST
+    PARAM {
+     .name = "param1",
+     .help = "helpparam1",
+     .ptype = "ptype1",
+    },
+    PARAM {
+     .name = "param2",
+     .help = "helpparam2",
+     .ptype = "ptype2",
+   PARAM_LIST
+    PARAM {
+     .name = "param3",
+     .help = "helpparam1",
+     .ptype = "ptype1",
+    },
+    PARAM {
+     .name = "param4",
+     .help = "helpparam2",
+     .ptype = "ptype2",
+
+
+    },
+
+   END_PARAM_LIST,
+
+
+    },
+
+   END_PARAM_LIST,
+
+   ACTION_LIST
+
+    ACTION {
+     .sym = "internal",
+     .script = "cat /etc/passwd",
+    },
+
+    ACTION {
+     .sym = "internal",
+     .script = "cat /etc/group",
+    },
+
+   END_ACTION_LIST,
+
+
+
         },
 
       END_COMMAND_LIST,
@@ -220,11 +268,13 @@ int main(int argc, char **argv)
 	if (!scheme) {
 		fprintf(stderr, "Scheme errors:\n");
 		faux_error_print(error);
+		faux_error_free(error);
 		goto err;
 	}
 	txt = ischeme_to_text(&sch, 0);
 	printf("%s\n", txt);
 	faux_str_free(txt);
+	faux_error_free(error);
 	}
 
 	// Listen socket

+ 4 - 0
klish/kcommand.h

@@ -28,6 +28,10 @@ typedef enum {
 
 C_DECL_BEGIN
 
+// icommand_t
+char *icommand_to_text(const icommand_t *icommand, int level);
+
+// kcommand_t
 kcommand_t *kcommand_new(const icommand_t *info, kcommand_error_e *error);
 void kcommand_free(kcommand_t *command);
 const char *kcommand_strerror(kcommand_error_e error);

+ 6 - 0
klish/kparam.h

@@ -6,6 +6,8 @@
 #ifndef _klish_kparam_h
 #define _klish_kparam_h
 
+#include <faux/error.h>
+
 typedef struct kparam_s kparam_t;
 
 typedef struct iparam_s iparam_t;
@@ -29,6 +31,10 @@ typedef enum {
 
 C_DECL_BEGIN
 
+// iparam_t
+char *iparam_to_text(const iparam_t *iparam, int level);
+
+// kparam_t
 kparam_t *kparam_new(const iparam_t *info, kparam_error_e *error);
 void kparam_free(kparam_t *param);
 const char *kparam_strerror(kparam_error_e error);

+ 4 - 3
klish/kscheme/Makefile.am

@@ -8,6 +8,7 @@ libklish_la_SOURCES += \
 	klish/kscheme/kscheme.c \
 	klish/kscheme/ischeme.c \
 	klish/kscheme/iptype.c \
-	klish/kscheme/iaction.c
-
-
+	klish/kscheme/iaction.c \
+	klish/kscheme/iview.c \
+	klish/kscheme/icommand.c \
+	klish/kscheme/iparam.c

+ 1 - 1
klish/kscheme/iaction.c

@@ -14,7 +14,7 @@ char *iaction_to_text(const iaction_t *iaction, int level)
 	char *str = NULL;
 	char *tmp = NULL;
 
-	tmp = faux_str_sprintf("%*cACTION = {\n", level, ' ');
+	tmp = faux_str_sprintf("%*cACTION {\n", level, ' ');
 	faux_str_cat(&str, tmp);
 	faux_str_free(tmp);
 

+ 72 - 0
klish/kscheme/icommand.c

@@ -0,0 +1,72 @@
+#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/kcommand.h>
+#include <klish/kaction.h>
+
+
+char *icommand_to_text(const icommand_t *icommand, int level)
+{
+	char *str = NULL;
+	char *tmp = NULL;
+
+	tmp = faux_str_sprintf("%*cCOMMAND {\n", level, ' ');
+	faux_str_cat(&str, tmp);
+	faux_str_free(tmp);
+
+	attr2ctext(&str, "name", icommand->name, level + 1);
+	attr2ctext(&str, "help", icommand->help, level + 1);
+
+	// PARAM list
+	if (icommand->params) {
+		iparam_t **p_iparam = NULL;
+
+		tmp = faux_str_sprintf("\n%*cPARAM_LIST\n\n", level + 1, ' ');
+		faux_str_cat(&str, tmp);
+		faux_str_free(tmp);
+
+		for (p_iparam = *icommand->params; *p_iparam; p_iparam++) {
+			iparam_t *iparam = *p_iparam;
+
+			tmp = iparam_to_text(iparam, level + 2);
+			faux_str_cat(&str, tmp);
+			faux_str_free(tmp);
+		}
+
+		tmp = faux_str_sprintf("%*cEND_PARAM_LIST,\n", level + 1, ' ');
+		faux_str_cat(&str, tmp);
+		faux_str_free(tmp);
+	}
+
+	// ACTION list
+	if (icommand->actions) {
+		iaction_t **p_iaction = NULL;
+
+		tmp = faux_str_sprintf("\n%*cACTION_LIST\n\n", level + 1, ' ');
+		faux_str_cat(&str, tmp);
+		faux_str_free(tmp);
+
+		for (p_iaction = *icommand->actions; *p_iaction; p_iaction++) {
+			iaction_t *iaction = *p_iaction;
+
+			tmp = iaction_to_text(iaction, level + 2);
+			faux_str_cat(&str, tmp);
+			faux_str_free(tmp);
+		}
+
+		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);
+
+	return str;
+}

+ 51 - 0
klish/kscheme/iparam.c

@@ -0,0 +1,51 @@
+#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/kparam.h>
+
+
+char *iparam_to_text(const iparam_t *iparam, int level)
+{
+	char *str = NULL;
+	char *tmp = NULL;
+
+	tmp = faux_str_sprintf("%*cPARAM {\n", level, ' ');
+	faux_str_cat(&str, tmp);
+	faux_str_free(tmp);
+
+	attr2ctext(&str, "name", iparam->name, level + 1);
+	attr2ctext(&str, "help", iparam->help, level + 1);
+	attr2ctext(&str, "ptype", iparam->ptype, level + 1);
+
+	// PARAM list
+	if (iparam->params) {
+		iparam_t **p_iparam = NULL;
+
+		tmp = faux_str_sprintf("\n%*cPARAM_LIST\n\n", level + 1, ' ');
+		faux_str_cat(&str, tmp);
+		faux_str_free(tmp);
+
+		for (p_iparam = *iparam->params; *p_iparam; p_iparam++) {
+			iparam_t *niparam = *p_iparam;
+
+			tmp = iparam_to_text(niparam, level + 2);
+			faux_str_cat(&str, tmp);
+			faux_str_free(tmp);
+		}
+
+		tmp = faux_str_sprintf("%*cEND_PARAM_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);
+
+	return str;
+}

+ 1 - 1
klish/kscheme/iptype.c

@@ -15,7 +15,7 @@ char *iptype_to_text(const iptype_t *iptype, int level)
 	char *str = NULL;
 	char *tmp = NULL;
 
-	tmp = faux_str_sprintf("%*cPTYPE = {\n", level, ' ');
+	tmp = faux_str_sprintf("%*cPTYPE {\n", level, ' ');
 	faux_str_cat(&str, tmp);
 	faux_str_free(tmp);
 

+ 4 - 5
klish/kscheme/ischeme.c

@@ -39,12 +39,12 @@ char *ischeme_to_text(const ischeme_t *ischeme, int level)
 		faux_str_cat(&str, tmp);
 		faux_str_free(tmp);
 	}
-/*
+
 	// VIEW list
 	if (ischeme->views) {
 		iview_t **p_iview = NULL;
 
-		tmp = faux_str_sprintf("\n%*sVIEW_LIST\n\n", level + 1, ISCHEME_TAB);
+		tmp = faux_str_sprintf("\n%*cVIEW_LIST\n\n", level + 1, ' ');
 		faux_str_cat(&str, tmp);
 		faux_str_free(tmp);
 
@@ -56,15 +56,14 @@ char *ischeme_to_text(const ischeme_t *ischeme, int level)
 			faux_str_free(tmp);
 		}
 
-		tmp = faux_str_sprintf("\n%*sEND_VIEW_LIST\n", level + 1, ISCHEME_TAB);
+		tmp = faux_str_sprintf("%*cEND_VIEW_LIST\n", level + 1, ' ');
 		faux_str_cat(&str, tmp);
 		faux_str_free(tmp);
 	}
-*/
+
 	tmp = faux_str_sprintf("};\n");
 	faux_str_cat(&str, tmp);
 	faux_str_free(tmp);
 
 	return str;
 }
-

+ 50 - 0
klish/kscheme/iview.c

@@ -0,0 +1,50 @@
+#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/kview.h>
+#include <klish/kcommand.h>
+
+
+char *iview_to_text(const iview_t *iview, int level)
+{
+	char *str = NULL;
+	char *tmp = NULL;
+
+	tmp = faux_str_sprintf("%*cVIEW {\n", level, ' ');
+	faux_str_cat(&str, tmp);
+	faux_str_free(tmp);
+
+	attr2ctext(&str, "name", iview->name, level + 1);
+
+	// COMMAND list
+	if (iview->commands) {
+		icommand_t **p_icommand = NULL;
+
+		tmp = faux_str_sprintf("\n%*cCOMMAND_LIST\n\n", level + 1, ' ');
+		faux_str_cat(&str, tmp);
+		faux_str_free(tmp);
+
+		for (p_icommand = *iview->commands; *p_icommand; p_icommand++) {
+			icommand_t *icommand = *p_icommand;
+
+			tmp = icommand_to_text(icommand, level + 2);
+			faux_str_cat(&str, tmp);
+			faux_str_free(tmp);
+		}
+
+		tmp = faux_str_sprintf("%*cEND_COMMAND_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);
+
+	return str;
+}

+ 0 - 1
klish/kscheme/kaction.c

@@ -254,7 +254,6 @@ kaction_t *kaction_from_iaction(iaction_t *iaction, faux_error_t *error_stack)
 		faux_str_free(msg);
 		return NULL;
 	}
-	printf("action\n");
 
 	return kaction;
 }

+ 13 - 7
klish/kscheme/kcommand.c

@@ -55,7 +55,7 @@ static kcommand_t *kcommand_new_empty(void)
 	// PARAM list
 	command->params = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
 		kcommand_param_compare, kcommand_param_kcompare,
-		(void (*)(void *))kcommand_free);
+		(void (*)(void *))kparam_free);
 	assert(command->params);
 
 	// ACTION list
@@ -202,7 +202,9 @@ bool_t kcommand_nested_from_icommand(kcommand_t *kcommand, icommand_t *icommand,
 				}
 				faux_error_add(error_stack, msg);
 				faux_str_free(msg);
+				kparam_free(kparam);
 				retval = BOOL_FALSE;
+				continue;
 			}
 		}
 	}
@@ -226,11 +228,21 @@ bool_t kcommand_nested_from_icommand(kcommand_t *kcommand, icommand_t *icommand,
 					faux_list_len(kcommand->actions) + 1);
 				faux_error_add(error_stack, msg);
 				faux_str_free(msg);
+				kaction_free(kaction);
 				retval = BOOL_FALSE;
+				continue;
 			}
 		}
 	}
 
+	if (!retval) {
+		char *msg = NULL;
+		msg = faux_str_sprintf("COMMAND \"%s\": Illegal nested elements",
+			kcommand_name(kcommand));
+		faux_error_add(error_stack, msg);
+		faux_str_free(msg);
+	}
+
 	return retval;
 }
 
@@ -250,15 +262,9 @@ kcommand_t *kcommand_from_icommand(icommand_t *icommand, faux_error_t *error_sta
 		faux_str_free(msg);
 		return NULL;
 	}
-	printf("command %s\n", kcommand_name(kcommand));
 
 	// Parse nested elements
 	if (!kcommand_nested_from_icommand(kcommand, icommand, error_stack)) {
-		char *msg = NULL;
-		msg = faux_str_sprintf("COMMAND \"%s\": Illegal nested elements",
-			kcommand_name(kcommand));
-		faux_error_add(error_stack, msg);
-		faux_str_free(msg);
 		kcommand_free(kcommand);
 		return NULL;
 	}

+ 13 - 8
klish/kscheme/kparam.c

@@ -37,7 +37,8 @@ KGET(param, kptype_t *, ptype);
 KSET(param, kptype_t *, ptype);
 
 // PARAM list
-KCMP_NESTED_BY_KEY(param, param, name);
+static KCMP_NESTED(param, param, name);
+static KCMP_NESTED_BY_KEY(param, param, name);
 KADD_NESTED(param, param);
 KFIND_NESTED(param, param);
 
@@ -58,7 +59,7 @@ static kparam_t *kparam_new_empty(void)
 	param->ptype = NULL;
 
 	param->params = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
-		NULL, kparam_param_kcompare,
+		kparam_param_compare, kparam_param_kcompare,
 		(void (*)(void *))kparam_free);
 	assert(param->params);
 
@@ -212,11 +213,21 @@ bool_t kparam_nested_from_iparam(kparam_t *kparam, iparam_t *iparam,
 				}
 				faux_error_add(error_stack, msg);
 				faux_str_free(msg);
+				kparam_free(nkparam);
 				retval = BOOL_FALSE;
+				continue;
 			}
 		}
 	}
 
+	if (!retval) {
+		char *msg = NULL;
+		msg = faux_str_sprintf("PARAM \"%s\": Illegal nested elements",
+			kparam_name(kparam));
+		faux_error_add(error_stack, msg);
+		faux_str_free(msg);
+	}
+
 	return retval;
 }
 
@@ -236,15 +247,9 @@ kparam_t *kparam_from_iparam(iparam_t *iparam, faux_error_t *error_stack)
 		faux_str_free(msg);
 		return NULL;
 	}
-	printf("param %s\n", kparam_name(kparam));
 
 	// Parse nested elements
 	if (!kparam_nested_from_iparam(kparam, iparam, error_stack)) {
-		char *msg = NULL;
-		msg = faux_str_sprintf("PARAM \"%s\": Illegal nested elements",
-			kparam_name(kparam));
-		faux_error_add(error_stack, msg);
-		faux_str_free(msg);
 		kparam_free(kparam);
 		return NULL;
 	}

+ 10 - 6
klish/kscheme/kptype.c

@@ -178,11 +178,21 @@ bool_t kptype_nested_from_iptype(kptype_t *kptype, iptype_t *iptype,
 					faux_list_len(kptype->actions) + 1);
 				faux_error_add(error_stack, msg);
 				faux_str_free(msg);
+				kaction_free(kaction);
 				retval = BOOL_FALSE;
+				continue;
 			}
 		}
 	}
 
+	if (!retval) {
+		char *msg = NULL;
+		msg = faux_str_sprintf("PTYPE \"%s\": Illegal nested elements",
+			kptype_name(kptype));
+		faux_error_add(error_stack, msg);
+		faux_str_free(msg);
+	}
+
 	return retval;
 }
 
@@ -202,15 +212,9 @@ kptype_t *kptype_from_iptype(iptype_t *iptype, faux_error_t *error_stack)
 		faux_str_free(msg);
 		return NULL;
 	}
-	printf("ptype %s\n", kptype_name(kptype));
 
 	// Parse nested elements
 	if (!kptype_nested_from_iptype(kptype, iptype, error_stack)) {
-		char *msg = NULL;
-		msg = faux_str_sprintf("PTYPE \"%s\": Illegal nested elements",
-			kptype_name(kptype));
-		faux_error_add(error_stack, msg);
-		faux_str_free(msg);
 		kptype_free(kptype);
 		return NULL;
 	}

+ 14 - 1
klish/kscheme/kscheme.c

@@ -132,6 +132,7 @@ bool_t kscheme_nested_from_ischeme(kscheme_t *kscheme, ischeme_t *ischeme,
 				}
 				faux_error_add(error_stack, msg);
 				faux_str_free(msg);
+				kptype_free(kptype);
 				retval = BOOL_FALSE;
 			}
 		}
@@ -185,12 +186,20 @@ bool_t kscheme_nested_from_ischeme(kscheme_t *kscheme, ischeme_t *ischeme,
 					kview_name(kview));
 				faux_error_add(error_stack, msg);
 				faux_str_free(msg);
+				kview_free(kview);
 				retval = BOOL_FALSE;
 				continue;
 			}
 		}
 	}
 
+	if (!retval) {
+		char *msg = NULL;
+		msg = faux_str_sprintf("SCHEME: Illegal nested elements");
+		faux_error_add(error_stack, msg);
+		faux_str_free(msg);
+	}
+
 	return retval;
 }
 
@@ -202,11 +211,15 @@ kscheme_t *kscheme_from_ischeme(ischeme_t *ischeme, faux_error_t *error_stack)
 
 	kscheme = kscheme_new(&kscheme_error);
 	if (!kscheme) {
-		faux_error_add(error_stack,
+		char *msg = NULL;
+		msg = faux_str_sprintf("SCHEME: %s",
 			kscheme_strerror(kscheme_error));
+		faux_error_add(error_stack, msg);
+		faux_str_free(msg);
 		return NULL;
 	}
 
+	// Parse nested elements
 	if (!kscheme_nested_from_ischeme(kscheme, ischeme, error_stack)) {
 		kscheme_free(kscheme);
 		return NULL;

+ 10 - 6
klish/kscheme/kview.c

@@ -169,11 +169,21 @@ bool_t kview_nested_from_iview(kview_t *kview, iview_t *iview,
 				}
 				faux_error_add(error_stack, msg);
 				faux_str_free(msg);
+				kcommand_free(kcommand);
 				retval = BOOL_FALSE;
+				continue;
 			}
 		}
 	}
 
+	if (!retval) {
+		char *msg = NULL;
+		msg = faux_str_sprintf("VIEW \"%s\": Illegal nested elements",
+			kview_name(kview));
+		faux_error_add(error_stack, msg);
+		faux_str_free(msg);
+	}
+
 	return retval;
 }
 
@@ -193,15 +203,9 @@ kview_t *kview_from_iview(iview_t *iview, faux_error_t *error_stack)
 		faux_str_free(msg);
 		return NULL;
 	}
-	printf("view %s\n", kview_name(kview));
 
 	// Parse nested elements
 	if (!kview_nested_from_iview(kview, iview, error_stack)) {
-		char *msg = NULL;
-		msg = faux_str_sprintf("VIEW \"%s\": Illegal nested elements",
-			kview_name(kview));
-		faux_error_add(error_stack, msg);
-		faux_str_free(msg);
 		kview_free(kview);
 		return NULL;
 	}

+ 4 - 0
klish/kview.h

@@ -28,6 +28,10 @@ typedef enum {
 
 C_DECL_BEGIN
 
+// iview_t
+char *iview_to_text(const iview_t *iview, int level);
+
+// kview_t
 kview_t *kview_new(const iview_t *info, kview_error_e *error);
 void kview_free(kview_t *view);
 bool_t kview_parse(kview_t *view, const iview_t *info, kview_error_e *error);