From 8b6e812dcee8a54b4b6ad4116cb0782730079952 Mon Sep 17 00:00:00 2001
From: Alexandre Cassen <acassen@gmail.com>
Date: Tue, 7 Jan 2014 16:10:46 +0100
Subject: [PATCH 6/7] lib: extend vty to support logger
add support to logger and some cosmetics.
---
lib/vty.c | 52 +++++++++++++++++++++++++---------------------------
lib/vty.h | 2 +-
2 files changed, 26 insertions(+), 28 deletions(-)
diff --git a/lib/vty.c b/lib/vty.c
index e8c6dc6..b84b232 100644
--- a/lib/vty.c
+++ b/lib/vty.c
@@ -1600,7 +1600,7 @@ vty_timeout(thread_t *thread)
}
/* Read up configuration file from file_name. */
-static void
+static int
vty_read_file(FILE *confp)
{
int ret;
@@ -1617,19 +1617,20 @@ vty_read_file(FILE *confp)
if (!((ret == CMD_SUCCESS) || (ret == CMD_ERR_NOTHING_TODO))) {
switch (ret) {
case CMD_ERR_AMBIGUOUS:
- fprintf(stderr, "Ambiguous command.\n");
+ log_message(LOG_ERR, "Ambiguous command.\n");
break;
case CMD_ERR_NO_MATCH:
- fprintf(stderr, "There is no such command.\n");
+ log_message(LOG_ERR, "There is no such command.\n");
break;
}
- fprintf(stderr, "Error occured during reading below line.\n%s\n"
- , vty->buf);
+ log_message(LOG_ERR, "Error occured during reading below line.\n%s\n"
+ , vty->buf);
vty_close(vty);
- exit(1);
+ return -1;
}
vty_close(vty);
+ return 0;
}
static FILE *
@@ -1703,7 +1704,7 @@ vty_use_backup_config(char *fullpath)
}
/* Read up configuration file from file_name. */
-void
+int
vty_read_config(char *config_file, char *config_default_dir)
{
char cwd[MAXPATHLEN];
@@ -1717,9 +1718,9 @@ vty_read_config(char *config_file, char *config_default_dir)
if (!IS_DIRECTORY_SEP(config_file[0])) {
retpath = getcwd(cwd, MAXPATHLEN);
if (!retpath) {
- fprintf(stderr, "%s: failed to get current working directory: %s\n"
- , __func__, strerror(errno));
- exit(1);
+ log_message(LOG_ERR, "%s: failed to get current working directory: %s\n"
+ , __func__, strerror(errno));
+ return -1;
}
tmp = MALLOC(strlen(cwd) + strlen(config_file) + 2);
sprintf(tmp, "%s/%s", cwd, config_file);
@@ -1731,32 +1732,32 @@ vty_read_config(char *config_file, char *config_default_dir)
confp = fopen(fullpath, "r");
if (confp == NULL) {
- fprintf(stderr, "%s: failed to open configuration file %s: %s\n"
- , __func__, fullpath, strerror (errno));
+ log_message(LOG_ERR, "%s: failed to open configuration file %s: %s\n"
+ , __func__, fullpath, strerror (errno));
confp = vty_use_backup_config(fullpath);
if (confp) {
- fprintf(stderr, "WARNING: using backup configuration file!\n");
+ log_message(LOG_ERR, "WARNING: using backup configuration file!\n");
} else {
- fprintf(stderr, "can't open configuration file [%s]\n"
- , config_file);
- exit(1);
+ log_message(LOG_ERR, "can't open configuration file [%s]\n"
+ , config_file);
+ return -1;
}
}
} else {
confp = fopen(config_default_dir, "r");
if (confp == NULL) {
- fprintf(stderr, "%s: failed to open configuration file %s: %s\n"
- , __func__, config_default_dir, strerror(errno));
+ log_message(LOG_ERR, "%s: failed to open configuration file %s: %s\n"
+ , __func__, config_default_dir, strerror(errno));
confp = vty_use_backup_config(config_default_dir);
if (confp) {
- fprintf(stderr, "WARNING: using backup configuration file!\n");
+ log_message(LOG_ERR, "WARNING: using backup configuration file!\n");
fullpath = config_default_dir;
} else {
- fprintf(stderr, "can't open configuration file [%s]\n"
- , config_default_dir);
- exit (1);
+ log_message(LOG_ERR, "can't open configuration file [%s]\n"
+ , config_default_dir);
+ return -1;
}
} else {
fullpath = config_default_dir;
@@ -1764,12 +1765,10 @@ vty_read_config(char *config_file, char *config_default_dir)
}
vty_read_file(confp);
-
fclose(confp);
-
host_config_set(fullpath);
-
FREE_PTR(tmp);
+ return 0;
}
int
@@ -2038,8 +2037,7 @@ vty_config_write(vty_t *vty)
return CMD_SUCCESS;
}
-cmd_node_t vty_node =
-{
+cmd_node_t vty_node = {
VTY_NODE,
"%s(config-line)# ",
1,
diff --git a/lib/vty.h b/lib/vty.h
index 6134ad3..2c4b44f 100644
--- a/lib/vty.h
+++ b/lib/vty.h
@@ -181,7 +181,7 @@ extern int vty_listen(struct sockaddr_storage *);
extern void vty_reset(void);
extern vty_t *vty_new(void);
extern int vty_out(vty_t *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
-extern void vty_read_config(char *, char *);
+extern int vty_read_config(char *, char *);
extern void vty_time_print(vty_t *, int);
extern void vty_serv_sock(const char *, unsigned short, const char *);
extern void vty_close(vty_t *);
--
1.8.1.4