Browse Source

scheme: Use faux_error_sprintf() for error reporting

Serj Kalichev 3 years ago
parent
commit
6737fe1ea0

+ 2 - 2
bin/klishd/klishd.c

@@ -38,7 +38,7 @@
 
 #include "private.h"
 
-#include "sch_test.c"
+#include "sch.c"
 
 // Local static functions
 static int create_listen_unix_sock(const char *path);
@@ -139,7 +139,7 @@ int main(int argc, char **argv)
 	scheme = kscheme_from_ischeme(&sch, error);
 	if (!scheme) {
 		fprintf(stderr, "Scheme errors:\n");
-		faux_error_print(error);
+		faux_error_show(error);
 		faux_error_free(error);
 		goto err;
 	}

+ 1 - 4
klish/kscheme/kaction.c

@@ -247,11 +247,8 @@ kaction_t *kaction_from_iaction(iaction_t *iaction, faux_error_t *error_stack)
 
 	kaction = kaction_new(iaction, &kaction_error);
 	if (!kaction) {
-		char *msg = NULL;
-		msg = faux_str_sprintf("ACTION : %s",
+		faux_error_sprintf(error_stack, "ACTION : %s",
 			kaction_strerror(kaction_error));
-		faux_error_add(error_stack, msg);
-		faux_str_free(msg);
 		return NULL;
 	}
 

+ 9 - 19
klish/kscheme/kcommand.c

@@ -187,21 +187,20 @@ bool_t kcommand_nested_from_icommand(kcommand_t *kcommand, icommand_t *icommand,
 				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: "
+					faux_error_sprintf(error_stack,
+						"COMMAND: "
 						"Can't add duplicate PARAM "
 						"\"%s\"",
 						kparam_name(kparam));
 				} else {
-					msg = faux_str_sprintf("COMMAND: "
+					faux_error_sprintf(error_stack,
+						"COMMAND: "
 						"Can't add PARAM \"%s\"",
 						kparam_name(kparam));
 				}
-				faux_error_add(error_stack, msg);
-				faux_str_free(msg);
 				kparam_free(kparam);
 				retval = BOOL_FALSE;
 				continue;
@@ -222,12 +221,9 @@ bool_t kcommand_nested_from_icommand(kcommand_t *kcommand, icommand_t *icommand,
 				continue;
 			}
 			if (!kcommand_add_action(kcommand, kaction)) {
-				char *msg = NULL;
-				msg = faux_str_sprintf("COMMAND: "
+				faux_error_sprintf(error_stack, "COMMAND: "
 					"Can't add ACTION #%d",
 					faux_list_len(kcommand->actions) + 1);
-				faux_error_add(error_stack, msg);
-				faux_str_free(msg);
 				kaction_free(kaction);
 				retval = BOOL_FALSE;
 				continue;
@@ -235,13 +231,10 @@ bool_t kcommand_nested_from_icommand(kcommand_t *kcommand, icommand_t *icommand,
 		}
 	}
 
-	if (!retval) {
-		char *msg = NULL;
-		msg = faux_str_sprintf("COMMAND \"%s\": Illegal nested elements",
+	if (!retval)
+		faux_error_sprintf(error_stack,
+			"COMMAND \"%s\": Illegal nested elements",
 			kcommand_name(kcommand));
-		faux_error_add(error_stack, msg);
-		faux_str_free(msg);
-	}
 
 	return retval;
 }
@@ -254,12 +247,9 @@ kcommand_t *kcommand_from_icommand(icommand_t *icommand, faux_error_t *error_sta
 
 	kcommand = kcommand_new(icommand, &kcommand_error);
 	if (!kcommand) {
-		char *msg = NULL;
-		msg = faux_str_sprintf("COMMAND \"%s\": %s",
+		faux_error_sprintf(error_stack, "COMMAND \"%s\": %s",
 			icommand->name ? icommand->name : "(null)",
 			kcommand_strerror(kcommand_error));
-		faux_error_add(error_stack, msg);
-		faux_str_free(msg);
 		return NULL;
 	}
 

+ 8 - 15
klish/kscheme/kparam.c

@@ -198,21 +198,20 @@ bool_t kparam_nested_from_iparam(kparam_t *kparam, iparam_t *iparam,
 				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: "
+					faux_error_sprintf(error_stack,
+						"PARAM: "
 						"Can't add duplicate PARAM "
 						"\"%s\"",
 						kparam_name(nkparam));
 				} else {
-					msg = faux_str_sprintf("PARAM: "
+					faux_error_sprintf(error_stack,
+						"PARAM: "
 						"Can't add PARAM \"%s\"",
 						kparam_name(nkparam));
 				}
-				faux_error_add(error_stack, msg);
-				faux_str_free(msg);
 				kparam_free(nkparam);
 				retval = BOOL_FALSE;
 				continue;
@@ -220,13 +219,10 @@ bool_t kparam_nested_from_iparam(kparam_t *kparam, iparam_t *iparam,
 		}
 	}
 
-	if (!retval) {
-		char *msg = NULL;
-		msg = faux_str_sprintf("PARAM \"%s\": Illegal nested elements",
+	if (!retval)
+		faux_error_sprintf(error_stack,
+			"PARAM \"%s\": Illegal nested elements",
 			kparam_name(kparam));
-		faux_error_add(error_stack, msg);
-		faux_str_free(msg);
-	}
 
 	return retval;
 }
@@ -239,12 +235,9 @@ kparam_t *kparam_from_iparam(iparam_t *iparam, faux_error_t *error_stack)
 
 	kparam = kparam_new(iparam, &kparam_error);
 	if (!kparam) {
-		char *msg = NULL;
-		msg = faux_str_sprintf("PARAM \"%s\": %s",
+		faux_error_sprintf(error_stack, "PARAM \"%s\": %s",
 			iparam->name ? iparam->name : "(null)",
 			kparam_strerror(kparam_error));
-		faux_error_add(error_stack, msg);
-		faux_str_free(msg);
 		return NULL;
 	}
 

+ 5 - 14
klish/kscheme/kptype.c

@@ -172,12 +172,9 @@ bool_t kptype_nested_from_iptype(kptype_t *kptype, iptype_t *iptype,
 				continue;
 			}
 			if (!kptype_add_action(kptype, kaction)) {
-				char *msg = NULL;
-				msg = faux_str_sprintf("PTYPE: "
+				faux_error_sprintf(error_stack, "PTYPE: "
 					"Can't add ACTION #%d",
 					faux_list_len(kptype->actions) + 1);
-				faux_error_add(error_stack, msg);
-				faux_str_free(msg);
 				kaction_free(kaction);
 				retval = BOOL_FALSE;
 				continue;
@@ -185,13 +182,10 @@ bool_t kptype_nested_from_iptype(kptype_t *kptype, iptype_t *iptype,
 		}
 	}
 
-	if (!retval) {
-		char *msg = NULL;
-		msg = faux_str_sprintf("PTYPE \"%s\": Illegal nested elements",
+	if (!retval)
+		faux_error_sprintf(error_stack,
+			"PTYPE \"%s\": Illegal nested elements",
 			kptype_name(kptype));
-		faux_error_add(error_stack, msg);
-		faux_str_free(msg);
-	}
 
 	return retval;
 }
@@ -204,12 +198,9 @@ kptype_t *kptype_from_iptype(iptype_t *iptype, faux_error_t *error_stack)
 
 	kptype = kptype_new(iptype, &kptype_error);
 	if (!kptype) {
-		char *msg = NULL;
-		msg = faux_str_sprintf("PTYPE \"%s\": %s",
+		faux_error_sprintf(error_stack, "PTYPE \"%s\": %s",
 			iptype->name ? iptype->name : "(null)",
 			kptype_strerror(kptype_error));
-		faux_error_add(error_stack, msg);
-		faux_str_free(msg);
 		return NULL;
 	}
 

+ 11 - 21
klish/kscheme/kscheme.c

@@ -117,21 +117,20 @@ bool_t kscheme_nested_from_ischeme(kscheme_t *kscheme, ischeme_t *ischeme,
 				continue;
 			}
 			if (!kscheme_add_ptype(kscheme, kptype)) {
-				char *msg = NULL;
 				// Search for PTYPE duplicates
 				if (kscheme_find_ptype(kscheme,
 					kptype_name(kptype))) {
-					msg = faux_str_sprintf("SCHEME: "
+					faux_error_sprintf(error_stack,
+						"SCHEME: "
 						"Can't add duplicate PTYPE "
 						"\"%s\"",
 						kptype_name(kptype));
 				} else {
-					msg = faux_str_sprintf("SCHEME: "
+					faux_error_sprintf(error_stack,
+						"SCHEME: "
 						"Can't add PTYPE \"%s\"",
 						kptype_name(kptype));
 				}
-				faux_error_add(error_stack, msg);
-				faux_str_free(msg);
 				kptype_free(kptype);
 				retval = BOOL_FALSE;
 			}
@@ -158,11 +157,10 @@ bool_t kscheme_nested_from_ischeme(kscheme_t *kscheme, ischeme_t *ischeme,
 			if (kview) {
 				kview_error_e kview_error = KVIEW_ERROR_OK;
 				if (!kview_parse(kview, iview, &kview_error)) {
-					char *msg = faux_str_sprintf("VIEW \"%s\": %s",
+					faux_error_sprintf(error_stack,
+						"VIEW \"%s\": %s",
 						iview->name ? iview->name : "(null)",
 						kview_strerror(kview_error));
-					faux_error_add(error_stack, msg);
-					faux_str_free(msg);
 					retval = BOOL_FALSE;
 					continue;
 				}
@@ -181,11 +179,10 @@ bool_t kscheme_nested_from_ischeme(kscheme_t *kscheme, ischeme_t *ischeme,
 				continue;
 			}
 			if (!kscheme_add_view(kscheme, kview)) {
-				char *msg = faux_str_sprintf("SCHEME: "
+				faux_error_sprintf(error_stack,
+					"SCHEME: "
 					"Can't add VIEW \"%s\"",
 					kview_name(kview));
-				faux_error_add(error_stack, msg);
-				faux_str_free(msg);
 				kview_free(kview);
 				retval = BOOL_FALSE;
 				continue;
@@ -193,12 +190,8 @@ bool_t kscheme_nested_from_ischeme(kscheme_t *kscheme, ischeme_t *ischeme,
 		}
 	}
 
-	if (!retval) {
-		char *msg = NULL;
-		msg = faux_str_sprintf("SCHEME: Illegal nested elements");
-		faux_error_add(error_stack, msg);
-		faux_str_free(msg);
-	}
+	if (!retval)
+		faux_error_sprintf(error_stack, "SCHEME: Illegal nested elements");
 
 	return retval;
 }
@@ -211,11 +204,8 @@ kscheme_t *kscheme_from_ischeme(ischeme_t *ischeme, faux_error_t *error_stack)
 
 	kscheme = kscheme_new(&kscheme_error);
 	if (!kscheme) {
-		char *msg = NULL;
-		msg = faux_str_sprintf("SCHEME: %s",
+		faux_error_sprintf(error_stack, "SCHEME: %s",
 			kscheme_strerror(kscheme_error));
-		faux_error_add(error_stack, msg);
-		faux_str_free(msg);
 		return NULL;
 	}
 

+ 8 - 15
klish/kscheme/kview.c

@@ -154,21 +154,20 @@ bool_t kview_nested_from_iview(kview_t *kview, iview_t *iview,
 				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: "
+					faux_error_sprintf(error_stack,
+						"VIEW: "
 						"Can't add duplicate COMMAND "
 						"\"%s\"",
 						kcommand_name(kcommand));
 				} else {
-					msg = faux_str_sprintf("VIEW: "
+					faux_error_sprintf(error_stack,
+						"VIEW: "
 						"Can't add COMMAND \"%s\"",
 						kcommand_name(kcommand));
 				}
-				faux_error_add(error_stack, msg);
-				faux_str_free(msg);
 				kcommand_free(kcommand);
 				retval = BOOL_FALSE;
 				continue;
@@ -176,13 +175,10 @@ bool_t kview_nested_from_iview(kview_t *kview, iview_t *iview,
 		}
 	}
 
-	if (!retval) {
-		char *msg = NULL;
-		msg = faux_str_sprintf("VIEW \"%s\": Illegal nested elements",
+	if (!retval)
+		faux_error_sprintf(error_stack,
+			"VIEW \"%s\": Illegal nested elements",
 			kview_name(kview));
-		faux_error_add(error_stack, msg);
-		faux_str_free(msg);
-	}
 
 	return retval;
 }
@@ -195,12 +191,9 @@ kview_t *kview_from_iview(iview_t *iview, faux_error_t *error_stack)
 
 	kview = kview_new(iview, &kview_error);
 	if (!kview) {
-		char *msg = NULL;
-		msg = faux_str_sprintf("VIEW \"%s\": %s",
+		faux_error_sprintf(error_stack, "VIEW \"%s\": %s",
 			iview->name ? iview->name : "(null)",
 			kview_strerror(kview_error));
-		faux_error_add(error_stack, msg);
-		faux_str_free(msg);
 		return NULL;
 	}