ruby: Append newline character after printing exception in callback.
[libguestfs.git] / fish / fish.c
index 2dbcdf0..1bc8447 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 void error_cb (guestfs_h *g, void *data, const char *msg);
 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;
+const char *input_file = NULL;
+int input_lineno = 0;
 
 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
@@ -668,6 +683,8 @@ script (int prompt)
       break;
     }
 
+    input_lineno++;
+
     pcmd = parse_command_line (buf, &exit_on_error);
     if (pcmd.status == -1 && exit_on_error)
       exit (EXIT_FAILURE);
@@ -978,7 +995,7 @@ execute_and_inline (const char *cmd, int global_exit_on_error)
 
   free (line);
 
-  if (pclose (pp) == -1) {
+  if (pclose (pp) != 0) {
     perror ("pclose");
     return -1;
   }
@@ -1192,6 +1209,14 @@ extended_help_message (void)
              "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)
 {
@@ -1345,9 +1370,8 @@ parse_string_list (const char *str)
     /* We've reached the end of a token. We shouldn't still be in quotes. */
     if (in_quote) {
       fprintf (stderr, _("Runaway quote in string \"%s\"\n"), str);
-
       free_n_strings (argv, argv_len);
-
+      free (tok);
       return NULL;
     }