]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: Improve error reporting from broken config files
authorSteve Hill <quagga@cheesy.sackheads.org>
Tue, 28 Jul 2009 20:36:14 +0000 (16:36 -0400)
committerDonald Sharp <sharpd@cumulusnetworks.com>
Tue, 1 Sep 2015 19:44:10 +0000 (12:44 -0700)
* command.h: (config_from_file) Add variable to interface for line
      number reporting.
    * command.c: (config_from_file) Set & increment 'line_num' while parsing.
    * vty.c: (vty_read_file) Report parse errors in the correct order to
      stderr, with added line numbers.

Conflicts:
lib/command.c
lib/vty.c

Integrated in changes made to continue loading code even after failure
to read a line in properly as part of this cherry-pick

lib/command.c
lib/command.h
lib/vty.c

index e7027fca26af2e15143a843786d261ff4d24a063..ff3778bd70698beb2aae48a241d31fc650c3070f 100644 (file)
@@ -2777,14 +2777,18 @@ cmd_execute_command_strict (vector vline, struct vty *vty,
 
 /* Configuration make from file. */
 int
-config_from_file (struct vty *vty, FILE *fp)
+config_from_file (struct vty *vty, FILE *fp, unsigned int *line_num)
 {
   int ret, error_ret=0;
   int saved_node;
+  *line_num = 0;
   vector vline;
 
   while (fgets (vty->buf, VTY_BUFSIZ, fp))
     {
+      if (!error_ret)
+       ++(*line_num);
+
       vline = cmd_make_strvec (vty->buf);
 
       /* In case of comment line */
index 9a890f9be394ac1ebb9602d85b12edb67c36c358..e9bfa94e82547b3520113a53a07a79dd0242dd67 100644 (file)
@@ -532,7 +532,7 @@ extern void cmd_free_strvec (vector);
 extern vector cmd_describe_command (vector, struct vty *, int *status);
 extern char **cmd_complete_command (vector, struct vty *, int *status);
 extern const char *cmd_prompt (enum node_type);
-extern int config_from_file (struct vty *, FILE *);
+extern int config_from_file (struct vty *, FILE *, unsigned int *line_num);
 extern enum node_type node_parent (enum node_type);
 extern int cmd_execute_command (vector, struct vty *, struct cmd_element **, int);
 extern int cmd_execute_command_strict (vector, struct vty *, struct cmd_element **);
index 890f47acb6106bdec9cf81d41579ccaee5e22fd4..e617d1f65a10430dff0a450f45d65bb44c04daab 100644 (file)
--- a/lib/vty.c
+++ b/lib/vty.c
@@ -2234,28 +2234,37 @@ vty_read_file (FILE *confp)
 {
   int ret;
   struct vty *vty;
+  unsigned int line_num = 0;
 
   vty = vty_new ();
-  vty->fd = 0;                 /* stdout */
-  vty->type = VTY_TERM;
+  vty->fd = dup(STDERR_FILENO); /* vty_close() will close this */
+  if (vty->fd < 0)
+  {
+    /* Fine, we couldn't make a new fd. vty_close doesn't close stdout. */
+    vty->fd = STDOUT_FILENO;
+  }
+  vty->type = VTY_FILE;
   vty->node = CONFIG_NODE;
   
   /* Execute configuration file */
-  ret = config_from_file (vty, confp);
+  ret = config_from_file (vty, confp, &line_num);
+
+  /* Flush any previous errors before printing messages below */
+  buffer_flush_all (vty->obuf, vty->fd);
 
   if ( !((ret == CMD_SUCCESS) || (ret == CMD_ERR_NOTHING_TODO)) ) 
     {
       switch (ret)
        {
          case CMD_ERR_AMBIGUOUS:
-           fprintf (stderr, "\nAmbiguous command.");
+           fprintf (stderr, "*** Error reading config: Ambiguous command.\n");
            break;
          case CMD_ERR_NO_MATCH:
-           fprintf (stderr, "\nThere is no such command.");
+           fprintf (stderr, "*** Error reading config: There is no such command.\n");
            break;
        }
-      fprintf (stderr, "\nError occured during reading below line.\n%s\n",
-              vty->error_buf);
+      fprintf (stderr, "*** Error occured processing line %u, below:\n%s\n",
+                      line_num, vty->error_buf);
     }
 
   vty_close (vty);