[quagga-dev 11491] Missed patch?: Line number in config reading errors

Paul Jakma paul at jakma.org
Fri Sep 12 11:46:48 BST 2014


Sifting through old branches of mine. I had this but missed merging it. 
Seems very sensible and still applicable. Will merge soonish unless 
there's a 
NACK.

commit 7728d78e3d3dbd34b6f68d36bc2d9e2f33038583
Author: Steve Hill <quagga at cheesy.sackheads.org>
Date:   Tue Jul 28 16:36:14 2009 -0400

     lib: Improve error reporting from broken config files

     * 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.

diff --git a/lib/command.c b/lib/command.c
index 7249c65..1087ceb 100644
--- a/lib/command.c
+++ b/lib/command.c
@@ -2762,13 +2762,15 @@ cmd_execute_command_strict (vector vline, struct vty *vty,

  /* Configration 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;
+  *line_num = 0;
    vector vline;

    while (fgets (vty->buf, VTY_BUFSIZ, fp))
      {
+      ++(*line_num);
        vline = cmd_make_strvec (vty->buf);

        /* In case of comment line */
diff --git a/lib/command.h b/lib/command.h
index e47c425..8dc50d0 100644
--- a/lib/command.h
+++ b/lib/command.h
@@ -518,7 +518,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 **);
diff --git a/lib/vty.c b/lib/vty.c
index 1141357..488f8d5 100644
--- a/lib/vty.c
+++ b/lib/vty.c
@@ -2229,28 +2229,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, "Ambiguous command.\n");
+           fprintf (stderr, "*** Error reading config: Ambiguous command.\n");
             break;
           case CMD_ERR_NO_MATCH:
-           fprintf (stderr, "There is no such command.\n");
+           fprintf (stderr, "*** Error reading config: There is no such command.\n");
             break;
         }
-      fprintf (stderr, "Error occured during reading below line.\n%s\n", 
-	       vty->buf);
+      fprintf (stderr, "*** Error occured processing line %u, below:\n%s\n",
+		       line_num, vty->buf);
        vty_close (vty);
        exit (1);
      }

regards,
-- 
Paul Jakma	paul at jakma.org	@pjakma	Key ID: 64A2FF6A
Fortune:
Volley Theory:
 	It is better to have lobbed and lost than never to have lobbed at all.




More information about the Quagga-dev mailing list