fish: Fix 'edit' command to work with any file.
[libguestfs.git] / fish / reopen.c
1 /* guestfish - the filesystem interactive shell
2  * Copyright (C) 2009 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "fish.h"
27
28 int
29 do_reopen (const char *cmd, int argc, char *argv[])
30 {
31   guestfs_h *g2;
32   int r;
33   const char *p;
34
35   if (argc > 0) {
36     fprintf (stderr, _("'reopen' command takes no parameters\n"));
37     return -1;
38   }
39
40   /* Open the new handle first, so we can copy the settings from the
41    * old one to the new one, and also so if it fails we still have an
42    * open handle.
43    */
44   g2 = guestfs_create ();
45   if (g2 == NULL) {
46     fprintf (stderr, _("reopen: guestfs_create: failed to create handle\n"));
47     return -1;
48   }
49
50   /* Now copy some of the settings from the old handle.  The settings
51    * we copy are those which are set by guestfish itself.
52    */
53   r = guestfs_get_verbose (g);
54   if (r >= 0)
55     guestfs_set_verbose (g2, r);
56
57   r = guestfs_get_trace (g);
58   if (r >= 0)
59     guestfs_set_trace (g2, r);
60
61   r = guestfs_get_autosync (g);
62   if (r >= 0)
63     guestfs_set_autosync (g2, r);
64
65   p = guestfs_get_path (g);
66   if (p)
67     guestfs_set_path (g2, p);
68
69   if (progress_bars)
70     guestfs_set_progress_callback (g2, progress_callback, NULL);
71
72   /* Close the original handle. */
73   guestfs_close (g);
74   g = g2;
75
76   return 0;
77 }