debian: Fix misspelling in debian/control.
[libguestfs.git] / fish / fish.c
index 4e45cea..2dbcdf0 100644 (file)
@@ -53,6 +53,7 @@ struct parsed_command {
   char *argv[64];
 };
 
+static void user_cancel (int);
 static void set_up_terminal (void);
 static void prepare_drives (struct drv *drv);
 static int launch (void);
@@ -72,7 +73,7 @@ static void add_history_line (const char *);
 static int override_progress_bars = -1;
 
 /* Currently open libguestfs handle. */
-guestfs_h *g;
+guestfs_h *g = NULL;
 
 int read_only = 0;
 int live = 0;
@@ -89,6 +90,7 @@ int inspector = 0;
 int utf8_mode = 0;
 int have_terminfo = 0;
 int progress_bars = 0;
+int is_interactive = 0;
 
 static void __attribute__((noreturn))
 usage (int status)
@@ -385,6 +387,24 @@ main (int argc, char *argv[])
     }
   }
 
+  /* Decide here if this will be an interactive session.  We have to
+   * do this as soon as possible after processing the command line
+   * args.
+   */
+  is_interactive = !file && isatty (0);
+
+  /* Register a ^C handler.  We have to do this before launch could
+   * possibly be called below.
+   */
+  if (is_interactive) {
+    memset (&sa, 0, sizeof sa);
+    sa.sa_handler = user_cancel;
+    sa.sa_flags = SA_RESTART;
+    sigaction (SIGINT, &sa, NULL);
+
+    guestfs_set_pgroup (g, 1);
+  }
+
   /* Old-style -i syntax?  Since -a/-d/-N and -i was disallowed
    * previously, if we have -i without any drives but with something
    * on the command line, it must be old-style syntax.
@@ -487,7 +507,7 @@ main (int argc, char *argv[])
   progress_bars =
     override_progress_bars >= 0
     ? override_progress_bars
-    : (optind >= argc && isatty (0));
+    : (optind >= argc && is_interactive);
 
   if (progress_bars)
     guestfs_set_event_callback (g, progress_callback,
@@ -495,7 +515,7 @@ main (int argc, char *argv[])
 
   /* Interactive, shell script, or command(s) on the command line? */
   if (optind >= argc) {
-    if (isatty (0))
+    if (is_interactive)
       interactive ();
     else
       shell_script ();
@@ -508,6 +528,13 @@ main (int argc, char *argv[])
   exit (EXIT_SUCCESS);
 }
 
+static void
+user_cancel (int sig)
+{
+  if (g)
+    guestfs_user_cancel (g);
+}
+
 /* The <term.h> header file which defines this has "issues". */
 extern int tgetent (char *, const char *);