Generated code for new mount_* commands.
[libguestfs.git] / fish / edit.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 #include <fcntl.h>
26 #include <inttypes.h>
27
28 #include "fish.h"
29
30 /* guestfish edit command, suggested by Ján Ondrej, implemented by RWMJ */
31
32 static int
33 xwrite (int fd, const void *buf, size_t len)
34 {
35   int r;
36
37   while (len > 0) {
38     r = write (fd, buf, len);
39     if (r == -1) {
40       perror ("write");
41       return -1;
42     }
43     buf += r;
44     len -= r;
45   }
46
47   return 0;
48 }
49
50 static char *
51 load_file (const char *filename, int *len_r)
52 {
53   int fd, r, start;
54   char *content = NULL, *p;
55   char buf[65536];
56
57   *len_r = 0;
58
59   fd = open (filename, O_RDONLY);
60   if (fd == -1) {
61     perror (filename);
62     return NULL;
63   }
64
65   while ((r = read (fd, buf, sizeof buf)) > 0) {
66     start = *len_r;
67     *len_r += r;
68     p = realloc (content, *len_r + 1);
69     if (p == NULL) {
70       perror ("realloc");
71       free (content);
72       return NULL;
73     }
74     content = p;
75     memcpy (content + start, buf, r);
76     content[start+r] = '\0';
77   }
78
79   if (r == -1) {
80     perror (filename);
81     free (content);
82     return NULL;
83   }
84
85   if (close (fd) == -1) {
86     perror (filename);
87     free (content);
88     return NULL;
89   }
90
91   return content;
92 }
93
94 int
95 do_edit (const char *cmd, int argc, char *argv[])
96 {
97   char filename[] = "/tmp/guestfishXXXXXX";
98   char buf[256];
99   const char *editor;
100   char *content, *content_new;
101   int r, fd, size;
102
103   if (argc != 1) {
104     fprintf (stderr, "use '%s filename' to edit a file\n", cmd);
105     return -1;
106   }
107
108   /* Choose an editor. */
109   if (strcasecmp (cmd, "vi") == 0)
110     editor = "vi";
111   else if (strcasecmp (cmd, "emacs") == 0)
112     editor = "emacs -nw";
113   else {
114     editor = getenv ("EDITOR");
115     if (editor == NULL)
116       editor = "vi"; /* could be cruel here and choose ed(1) */
117   }
118
119   /* Download the file and write it to a temporary. */
120   fd = mkstemp (filename);
121   if (fd == -1) {
122     perror ("mkstemp");
123     return -1;
124   }
125
126   if ((content = guestfs_cat (g, argv[0])) == NULL) {
127     close (fd);
128     unlink (filename);
129     return -1;
130   }
131
132   if (xwrite (fd, content, strlen (content)) == -1) {
133     close (fd);
134     unlink (filename);
135     free (content);
136     return -1;
137   }
138
139   if (close (fd) == -1) {
140     perror (filename);
141     unlink (filename);
142     free (content);
143     return -1;
144   }
145
146   /* Edit it. */
147   /* XXX Safe? */
148   snprintf (buf, sizeof buf, "%s %s", editor, filename);
149
150   r = system (buf);
151   if (r != 0) {
152     perror (buf);
153     unlink (filename);
154     free (content);
155     return -1;
156   }
157
158   /* Reload it. */
159   content_new = load_file (filename, &size);
160   if (content_new == NULL) {
161     unlink (filename);
162     free (content);
163     return -1;
164   }
165
166   unlink (filename);
167
168   /* Changed? */
169   if (strlen (content) == size && strncmp (content, content_new, size) == 0) {
170     free (content);
171     free (content_new);
172     return 0;
173   }
174
175   /* Write new content. */
176   if (guestfs_write_file (g, argv[0], content_new, size) == -1) {
177     free (content);
178     free (content_new);
179     return -1;
180   }
181
182   free (content);
183   free (content_new);
184   return 0;
185 }