]> git.puffer.fish Git - matthieu/frr.git/commitdiff
vtysh: Set an erroneous exit code if dry run fails because of syntax error
authorDonald Sharp <sharpd@cumulusnetworks.com>
Wed, 9 Mar 2016 12:25:02 +0000 (07:25 -0500)
committerDonald Sharp <sharpd@cumulusnetworks.com>
Wed, 9 Mar 2016 12:25:02 +0000 (07:25 -0500)
vtysh has a -C option to do a dry run of the quagga commands. However, the
program always returns 0 even when there's an error detected in the command.
Furthermore, it only parses vtysh.conf, not Quagga.conf.

This patch makes vtysh -C parse Quagga.conf also and return a non-zero
exit code so that network automation tools can catch this to flag errors in
syntax. This non-zero exit code along with printing the exact error with the
line number and offending line itself should help in fixing the error. But
this lack of proper error code requires the automation tools to go through
an additional hoop to validate the syntax.

Signed-off-by: Dinesh G Dutt <ddutt@cumulusnetworks.com>
vtysh/vtysh.c
vtysh/vtysh.h
vtysh/vtysh_config.c
vtysh/vtysh_main.c

index 9af2461448fa2ce86019db0f864a5e47b55e7741..2d84b020f6de8f71db5eac941961a54ed0b09a11 100644 (file)
@@ -675,12 +675,13 @@ vtysh_mark_file (const char *filename)
 }
 
 /* Configration make from file. */
-void
+int
 vtysh_config_from_file (struct vty *vty, FILE *fp)
 {
   int ret;
   struct cmd_element *cmd;
   int lineno = 0;
+  int retcode = CMD_SUCCESS;
 
   while (fgets (vty->buf, VTY_BUFSIZ, fp))
     {
@@ -693,15 +694,19 @@ vtysh_config_from_file (struct vty *vty, FILE *fp)
        case CMD_WARNING:
          if (vty->type == VTY_FILE)
            fprintf (stdout,"line %d: Warning...: %s\n", lineno, vty->buf);
+         retcode = 1;          /* once we have an error, we remember & return that */
          break;
        case CMD_ERR_AMBIGUOUS:
          fprintf (stdout,"line %d: %% Ambiguous command: %s\n", lineno, vty->buf);
+         retcode = 1;          /* once we have an error, we remember & return that */
          break;
        case CMD_ERR_NO_MATCH:
          fprintf (stdout,"line %d: %% Unknown command: %s", lineno, vty->buf);
+         retcode = 1;          /* once we have an error, we remember & return that */
          break;
        case CMD_ERR_INCOMPLETE:
          fprintf (stdout,"line %d: %% Command incomplete: %s\n", lineno, vty->buf);
+         retcode = 1;          /* once we have an error, we remember & return that */
          break;
        case CMD_SUCCESS_DAEMON:
          {
@@ -726,6 +731,8 @@ vtysh_config_from_file (struct vty *vty, FILE *fp)
          }
        }
     }
+
+  return (retcode);
 }
 
 /* We don't care about the point of the cursor when '?' is typed. */
index a24d615c3fffd2ef3198b66cc11be8692cf86dab..da6dd2a06718f47f4675b9b9eb3a3bacc0602419 100644 (file)
@@ -36,6 +36,7 @@
 
 /* vtysh local configuration file. */
 #define VTYSH_DEFAULT_CONFIG "vtysh.conf"
+#define QUAGGA_DEFAULT_CONFIG "Quagga.conf"
 
 void vtysh_init_vty (void);
 void vtysh_init_cmd (void);
@@ -50,7 +51,7 @@ char *vtysh_prompt (void);
 
 void vtysh_config_write (void);
 
-void vtysh_config_from_file (struct vty *, FILE *);
+int vtysh_config_from_file (struct vty *, FILE *);
 
 int vtysh_mark_file(const char *filename);
 
index f435cf8719f40d650b55377a30c2f9ed18b65ae6..6bb8fad42b0673c37cb23fc3d2f50368f198feef 100644 (file)
@@ -365,10 +365,11 @@ vtysh_config_dump (FILE *fp)
 }
 
 /* Read up configuration file from file_name. */
-static void
+static int
 vtysh_read_file (FILE *confp)
 {
   struct vty *vty;
+  int ret;
 
   vty = vty_new ();
   vty->fd = 0;                 /* stdout */
@@ -379,12 +380,14 @@ vtysh_read_file (FILE *confp)
   vtysh_execute_no_pager ("configure terminal");
 
   /* Execute configuration file. */
-  vtysh_config_from_file (vty, confp);
+  ret = vtysh_config_from_file (vty, confp);
 
   vtysh_execute_no_pager ("end");
   vtysh_execute_no_pager ("disable");
 
   vty_close (vty);
+
+  return (ret);
 }
 
 /* Read up configuration file from config_default_dir. */
@@ -392,16 +395,17 @@ int
 vtysh_read_config (const char *config_default_dir)
 {
   FILE *confp = NULL;
+  int ret;
 
   host_config_set (config_default_dir);
   confp = fopen (config_default_dir, "r");
   if (confp == NULL)
     return (1);
 
-  vtysh_read_file (confp);
+  ret = vtysh_read_file (confp);
   fclose (confp);
 
-  return (0);
+  return (ret);
 }
 
 /* We don't write vtysh specific into file from vtysh. vtysh.conf should
index befc86e6e228d0d6196e92c54502af5be654b657..d8b769ba206aa780945940203012d25deb4846f3 100644 (file)
@@ -42,6 +42,7 @@ char *progname;
 
 /* Configuration file name and directory. */
 char config_default[] = SYSCONFDIR VTYSH_DEFAULT_CONFIG;
+char quagga_config_default[] = SYSCONFDIR QUAGGA_DEFAULT_CONFIG;
 char history_file[MAXPATHLEN];
 
 /* Flag for indicate executing child command. */
@@ -232,6 +233,7 @@ main (int argc, char **argv, char **env)
   int echo_command = 0;
   int no_error = 0;
   int markfile = 0;
+  int ret = 0;
   char *homedir = NULL;
 
   /* Preserve name of myself. */
@@ -330,9 +332,13 @@ main (int argc, char **argv, char **env)
     {
       if (inputfile)
        {
-         vtysh_read_config(inputfile);
+         ret = vtysh_read_config(inputfile);
        }
-      return(0);
+      else
+       {
+         ret = vtysh_read_config(quagga_config_default);
+       }
+      exit(ret);
     }
 
   /* Ignore error messages */
@@ -357,8 +363,8 @@ main (int argc, char **argv, char **env)
 
   if (inputfile)
     {
-      vtysh_read_config(inputfile);
-      exit(0);
+      ret = vtysh_read_config(inputfile);
+      exit(ret);
     }
 
   /*