fish: Print input file and line number in error messages.
authorRichard W.M. Jones <rjones@redhat.com>
Fri, 5 Aug 2011 13:58:30 +0000 (14:58 +0100)
committerRichard W.M. Jones <rjones@redhat.com>
Fri, 5 Aug 2011 14:08:44 +0000 (15:08 +0100)
eg:
*stdin*:37: libguestfs: error: luks_close: Device lukstest is busy.

fish/fish.c
fish/fish.h
fish/reopen.c
generator/generator_fish.ml

index 2dbcdf0..efc74b2 100644 (file)
@@ -64,6 +64,7 @@ static void cmdline (char *argv[], int optind, int argc);
 static struct parsed_command parse_command_line (char *buf, int *exit_on_error_rtn);
 static int parse_quoted_string (char *p);
 static int execute_and_inline (const char *cmd, int exit_on_error);
 static struct parsed_command parse_command_line (char *buf, int *exit_on_error_rtn);
 static int parse_quoted_string (char *p);
 static int execute_and_inline (const char *cmd, int exit_on_error);
+static void error_cb (guestfs_h *g, void *data, const char *msg);
 static void initialize_readline (void);
 static void cleanup_readline (void);
 #ifdef HAVE_LIBREADLINE
 static void initialize_readline (void);
 static void cleanup_readline (void);
 #ifdef HAVE_LIBREADLINE
@@ -91,6 +92,8 @@ int utf8_mode = 0;
 int have_terminfo = 0;
 int progress_bars = 0;
 int is_interactive = 0;
 int have_terminfo = 0;
 int progress_bars = 0;
 int is_interactive = 0;
+const char *input_file = NULL;
+int input_lineno = 0;
 
 static void __attribute__((noreturn))
 usage (int status)
 
 static void __attribute__((noreturn))
 usage (int status)
@@ -503,6 +506,18 @@ main (int argc, char *argv[])
     }
   }
 
     }
   }
 
+  /* Get the name of the input file, for error messages, and replace
+   * the default error handler.
+   */
+  if (!is_interactive) {
+    if (file)
+      input_file = file;
+    else
+      input_file = "*stdin*";
+    guestfs_set_error_handler (g, error_cb, NULL);
+  }
+  input_lineno = 0;
+
   /* Decide if we display progress bars. */
   progress_bars =
     override_progress_bars >= 0
   /* Decide if we display progress bars. */
   progress_bars =
     override_progress_bars >= 0
@@ -668,6 +683,8 @@ script (int prompt)
       break;
     }
 
       break;
     }
 
+    input_lineno++;
+
     pcmd = parse_command_line (buf, &exit_on_error);
     if (pcmd.status == -1 && exit_on_error)
       exit (EXIT_FAILURE);
     pcmd = parse_command_line (buf, &exit_on_error);
     if (pcmd.status == -1 && exit_on_error)
       exit (EXIT_FAILURE);
@@ -1192,6 +1209,14 @@ extended_help_message (void)
              "For complete documentation:         man guestfish\n"));
 }
 
              "For complete documentation:         man guestfish\n"));
 }
 
+/* Error callback.  This replaces the standard libguestfs error handler. */
+static void
+error_cb (guestfs_h *g, void *data, const char *msg)
+{
+  fprintf (stderr, _("%s:%d: libguestfs: error: %s\n"),
+          input_file, input_lineno, msg);
+}
+
 void
 free_strings (char **argv)
 {
 void
 free_strings (char **argv)
 {
index 6f242da..6bcdb96 100644 (file)
@@ -63,6 +63,8 @@ extern int have_terminfo;
 extern int progress_bars;
 extern int remote_control_csh;
 extern const char *libvirt_uri;
 extern int progress_bars;
 extern int remote_control_csh;
 extern const char *libvirt_uri;
+extern int input_lineno;
+
 extern int issue_command (const char *cmd, char *argv[], const char *pipe, int rc_exit_on_error_flag);
 extern void list_builtin_commands (void);
 extern int display_builtin_command (const char *cmd);
 extern int issue_command (const char *cmd, char *argv[], const char *pipe, int rc_exit_on_error_flag);
 extern void list_builtin_commands (void);
 extern int display_builtin_command (const char *cmd);
index 959cd2c..c455a91 100644 (file)
@@ -31,6 +31,8 @@ run_reopen (const char *cmd, size_t argc, char *argv[])
   guestfs_h *g2;
   int r;
   const char *p;
   guestfs_h *g2;
   int r;
   const char *p;
+  guestfs_error_handler_cb cb;
+  void *cb_data;
 
   if (argc > 0) {
     fprintf (stderr, _("'reopen' command takes no parameters\n"));
 
   if (argc > 0) {
     fprintf (stderr, _("'reopen' command takes no parameters\n"));
@@ -50,6 +52,9 @@ run_reopen (const char *cmd, size_t argc, char *argv[])
   /* Now copy some of the settings from the old handle.  The settings
    * we copy are those which are set by guestfish itself.
    */
   /* Now copy some of the settings from the old handle.  The settings
    * we copy are those which are set by guestfish itself.
    */
+  cb = guestfs_get_error_handler (g, &cb_data);
+  guestfs_set_error_handler (g2, cb, cb_data);
+
   r = guestfs_get_verbose (g);
   if (r >= 0)
     guestfs_set_verbose (g2, r);
   r = guestfs_get_verbose (g);
   if (r >= 0)
     guestfs_set_verbose (g2, r);
index cdf9171..c5def5c 100644 (file)
@@ -416,6 +416,8 @@ Guestfish will prompt for these separately."
             pr "  if (%s == NULL) return -1;\n" name
         | Key name ->
             pr "  %s = read_key (\"%s\");\n" name name;
             pr "  if (%s == NULL) return -1;\n" name
         | Key name ->
             pr "  %s = read_key (\"%s\");\n" name name;
+            pr "  if (keys_from_stdin)\n";
+            pr "    input_lineno++;\n";
             pr "  if (%s == NULL) return -1;\n" name
         | Bool name ->
             pr "  %s = is_true (argv[i++]) ? 1 : 0;\n" name
             pr "  if (%s == NULL) return -1;\n" name
         | Bool name ->
             pr "  %s = is_true (argv[i++]) ? 1 : 0;\n" name