Browse Source

Fix code duplication in config_callback.

git-svn-id: https://klish.googlecode.com/svn/trunk@188 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
e644d23106
1 changed files with 103 additions and 171 deletions
  1. 103 171
      clish/clish_config_callback.c

+ 103 - 171
clish/clish_config_callback.c

@@ -25,224 +25,156 @@ static int receive_data(konf_client_t * client, konf_buf_t *buf, konf_buf_t **da
 
 /*--------------------------------------------------------- */
 bool_t
-clish_config_callback(const clish_shell_t * shell,
+clish_config_callback(const clish_shell_t * this,
 		      const clish_command_t * cmd, clish_pargv_t * pargv)
 {
 	unsigned i;
-	char *line;
 	char *command = NULL;
 	konf_client_t *client;
 	konf_buf_t *buf = NULL;
 	const char *viewid = NULL;
+	char *str = NULL;
+	char tmp[100];
+	clish_config_operation_t op;
 
-	if (shell)
-		viewid = clish_shell__get_viewid(shell);
+	if (!this)
+		return BOOL_TRUE;
+
+	viewid = clish_shell__get_viewid(this);
+	op = clish_command__get_cfg_op(cmd);
 
-	switch (clish_command__get_cfg_op(cmd)) {
+	switch (op) {
 
 	case CLISH_CONFIG_NONE:
 		return BOOL_TRUE;
 
 	case CLISH_CONFIG_SET:
-		{
-			char tmp[100];
-			char *pattern;
-
-			lub_string_cat(&command, "-s");
-			pattern = clish_command__get_pattern(cmd, viewid, pargv);
-			if (!pattern) {
-				lub_string_free(command);
-				return BOOL_FALSE;
-			}
-
-			line = clish_variable__get_line(cmd, pargv);
-			lub_string_cat(&command, " -l \"");
-			lub_string_cat(&command, line);
-			lub_string_cat(&command, "\"");
-			lub_string_free(line);
-
-			lub_string_cat(&command, " -r \"");
-			lub_string_cat(&command, pattern);
-			lub_string_cat(&command, "\"");
-			lub_string_free(pattern);
+		/* Add set operation */
+		lub_string_cat(&command, "-s");
 
-			if (clish_command__get_splitter(cmd) == BOOL_FALSE)
-				lub_string_cat(&command, " -i");
+		/* Add entered line */
+		str = clish_variable__get_line(cmd, pargv);
+		lub_string_cat(&command, " -l \"");
+		lub_string_cat(&command, str);
+		lub_string_cat(&command, "\"");
+		lub_string_free(str);
 
-			if (clish_command__get_unique(cmd) == BOOL_FALSE)
-				lub_string_cat(&command, " -n");
+		/* Add splitter */
+		if (clish_command__get_splitter(cmd) == BOOL_FALSE)
+			lub_string_cat(&command, " -i");
 
-			if (clish_command__get_priority(cmd) != 0) {
-				snprintf(tmp, sizeof(tmp) - 1, " -p 0x%x",
-					clish_command__get_priority(cmd));
-				tmp[sizeof(tmp) - 1] = '\0';
-				lub_string_cat(&command, tmp);
-			}
-
-			if (clish_command__is_seq(cmd)) {
-				snprintf(tmp, sizeof(tmp) - 1, " -q %u",
-					clish_command__get_seq(cmd,
-						viewid, pargv));
-				tmp[sizeof(tmp) - 1] = '\0';
-				lub_string_cat(&command, tmp);
-			}
+		/* Add unique */
+		if (clish_command__get_unique(cmd) == BOOL_FALSE)
+			lub_string_cat(&command, " -n");
 
-			for (i = 0; i < clish_command__get_cfg_depth(cmd, viewid, pargv); i++) {
-				const char *str =
-				    clish_shell__get_pwd_line(shell, i);
-				if (!str)
-					return BOOL_FALSE;
-				lub_string_cat(&command, " \"");
-				lub_string_cat(&command, str);
-				lub_string_cat(&command, "\"");
-			}
-			break;
-		}
+		break;
 
 	case CLISH_CONFIG_UNSET:
-		{
-			char tmp[100];
-			char *pattern;
-
-			lub_string_cat(&command, "-u");
-			pattern = clish_command__get_pattern(cmd, viewid, pargv);
-			if (!pattern) {
-				lub_string_free(command);
-				return BOOL_FALSE;
-			}
-
-			lub_string_cat(&command, " -r \"");
-			lub_string_cat(&command, pattern);
-			lub_string_cat(&command, "\"");
-			lub_string_free(pattern);
-
-			if (clish_command__get_priority(cmd) != 0) {
-				snprintf(tmp, sizeof(tmp) - 1, " -p 0x%x",
-					clish_command__get_priority(cmd));
-				tmp[sizeof(tmp) - 1] = '\0';
-				lub_string_cat(&command, tmp);
-			}
-
-			if (clish_command__is_seq(cmd)) {
-				snprintf(tmp, sizeof(tmp) - 1, " -q %u",
-					clish_command__get_seq(cmd,
-						viewid, pargv));
-				tmp[sizeof(tmp) - 1] = '\0';
-				lub_string_cat(&command, tmp);
-			}
-
-			for (i = 0; i < clish_command__get_cfg_depth(cmd, viewid, pargv); i++) {
-				const char *str =
-				    clish_shell__get_pwd_line(shell, i);
-				if (!str)
-					return BOOL_FALSE;
-				lub_string_cat(&command, " \"");
-				lub_string_cat(&command, str);
-				lub_string_cat(&command, "\"");
-			}
-			break;
-		}
+		/* Add unset operation */
+		lub_string_cat(&command, "-u");
+		break;
 
 	case CLISH_CONFIG_DUMP:
-		{
-			char *file;
-			char tmp[100];
-
-			lub_string_cat(&command, "-d");
-
-			file = clish_command__get_file(cmd, viewid, pargv);
-			if (file) {
-				lub_string_cat(&command, " -f \"");
-				if (file[0] != '\0')
-					lub_string_cat(&command, file);
-				else
-					lub_string_cat(&command, "/tmp/running-config");
-				lub_string_cat(&command, "\"");
-			}
-
-			if (clish_command__is_seq(cmd)) {
-				snprintf(tmp, sizeof(tmp) - 1, " -q %u",
-					clish_command__get_seq(cmd,
-						viewid, pargv));
-				tmp[sizeof(tmp) - 1] = '\0';
-				lub_string_cat(&command, tmp);
-			}
-
-			for (i = 0; i < clish_command__get_cfg_depth(cmd, viewid, pargv); i++) {
-				const char *str =
-				    clish_shell__get_pwd_line(shell, i);
-				if (!str)
-					return BOOL_FALSE;
-				lub_string_cat(&command, " \"");
+		/* Add dump operation */
+		lub_string_cat(&command, "-d");
+
+		/* Add filename */
+		str = clish_command__get_file(cmd, viewid, pargv);
+		if (str) {
+			lub_string_cat(&command, " -f \"");
+			if (str[0] != '\0')
 				lub_string_cat(&command, str);
-				lub_string_cat(&command, "\"");
-			}
-			break;
-		}
-
-	case CLISH_CONFIG_COPY:
-		{
-			char *file;
-			lub_string_cat(&command, "-d -f ");
-			file = clish_command__get_file(cmd, viewid, pargv);
-			lub_string_cat(&command, file);
-			lub_string_free(file);
-			break;
+			else
+				lub_string_cat(&command, "/tmp/running-config");
+			lub_string_cat(&command, "\"");
 		}
+		break;
 
 	default:
 		return BOOL_FALSE;
 	};
 
-	client = clish_shell__get_client(shell);
+	/* Add pattern */
+	if ((CLISH_CONFIG_SET == op) || (CLISH_CONFIG_UNSET == op)) {
+		str = clish_command__get_pattern(cmd, viewid, pargv);
+		if (!str) {
+			lub_string_free(command);
+			return BOOL_FALSE;
+		}
+		lub_string_cat(&command, " -r \"");
+		lub_string_cat(&command, str);
+		lub_string_cat(&command, "\"");
+		lub_string_free(str);
+	}
+
+	/* Add priority */
+	if (clish_command__get_priority(cmd) != 0) {
+		snprintf(tmp, sizeof(tmp) - 1, " -p 0x%x",
+			clish_command__get_priority(cmd));
+		tmp[sizeof(tmp) - 1] = '\0';
+		lub_string_cat(&command, tmp);
+	}
+
+	/* Add sequence */
+	if (clish_command__is_seq(cmd)) {
+		snprintf(tmp, sizeof(tmp) - 1, " -q %u",
+			clish_command__get_seq(cmd,
+			viewid, pargv));
+		tmp[sizeof(tmp) - 1] = '\0';
+		lub_string_cat(&command, tmp);
+	}
 
+	/* Add pwd */
+	str = clish_shell__get_pwd_full(this,
+		clish_command__get_cfg_depth(cmd, viewid, pargv));
+	if (str) {
+		lub_string_cat(&command, " ");
+		lub_string_cat(&command, str);
+		lub_string_free(str);
+	}
+
+	client = clish_shell__get_client(this);
 #ifdef DEBUG
 	fprintf(stderr, "CONFIG request: %s\n", command);
 #endif
-
 	if (send_request(client, command) < 0) {
 		fprintf(stderr, "Cannot write to the running-config.\n");
 	}
-
 	if (receive_answer(client, &buf) < 0) {
 		fprintf(stderr, "Cannot get answer from config daemon.\n");
 	}
-
 	lub_string_free(command);
 
-	switch (clish_command__get_cfg_op(cmd)) {
+	/* Postprocessing. Get data from daemon etc. */
+	switch (op) {
 
 	case CLISH_CONFIG_DUMP:
-		{
-			char *filename;
-
-			filename = clish_command__get_file(cmd, viewid, pargv);
-			if (filename) {
-				FILE *fd;
-				char str[1024];
-				if (!(fd = fopen(filename, "r")))
-					break;
-				while (fgets(str, sizeof(str), fd))
-					fprintf(clish_shell__get_ostream(shell),
-						"%s", str);
-				fclose(fd);
-			}
-			if (buf) {
-				char *str;
-				konf_buf_lseek(buf, 0);
-				while ((str = konf_buf_preparse(buf))) {
-					if (strlen(str) == 0) {
-						lub_string_free(str);
-						break;
-					}
-					fprintf(clish_shell__get_ostream(shell),
-						"%s\n", str);
+		str = clish_command__get_file(cmd, viewid, pargv);
+		if (str) {
+			FILE *fd;
+			char str[1024];
+			if (!(fd = fopen(str, "r")))
+				break;
+			while (fgets(str, sizeof(str), fd))
+				fprintf(clish_shell__get_ostream(this),
+					"%s", str);
+			fclose(fd);
+		}
+		if (buf) {
+			char *str;
+			konf_buf_lseek(buf, 0);
+			while ((str = konf_buf_preparse(buf))) {
+				if (strlen(str) == 0) {
 					lub_string_free(str);
+					break;
 				}
-				konf_buf_delete(buf);
+				fprintf(clish_shell__get_ostream(this),
+					"%s\n", str);
+				lub_string_free(str);
 			}
-			break;
+			konf_buf_delete(buf);
 		}
+		break;
 
 	default:
 		break;