fbb26deff81dfa691e532ae1a7ac987f8bd3d5b4
[libguestfs.git] / fish / fish.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 <getopt.h>
26
27 #include <guestfs.h>
28
29 #include "fish.h"
30
31 struct mp {
32   struct mp *next;
33   char *device;
34   char *mountpoint;
35 };
36
37 static void mount_mps (struct mp *mp);
38 static void interactive (void);
39 static void shell_script (void);
40 static void script (int prompt);
41 static void cmdline (char *argv[], int optind, int argc);
42 static int issue_command (const char *cmd, char *argv[]);
43
44 /* Currently open libguestfs handle. */
45 guestfs_h *g;
46 int g_launched = 0;
47
48 int quit = 0;
49 int verbose = 0;
50
51 int
52 launch (void)
53 {
54   if (!g_launched) {
55     if (guestfs_launch (g) == -1)
56       return -1;
57     if (guestfs_wait_ready (g) == -1)
58       return -1;
59     g_launched = 1;
60   }
61   return 0;
62 }
63
64 static void
65 usage (void)
66 {
67   fprintf (stderr,
68            "guestfish: guest filesystem shell\n"
69            "guestfish lets you edit virtual machine filesystems\n"
70            "Copyright (C) 2009 Red Hat Inc.\n"
71            "Usage:\n"
72            "  guestfish [--options] cmd [: cmd : cmd ...]\n"
73            "or for interactive use:\n"
74            "  guestfish\n"
75            "or from a shell script:\n"
76            "  guestfish <<EOF\n"
77            "  cmd\n"
78            "  ...\n"
79            "  EOF\n"
80            "Options:\n"
81            "  -h|--cmd-help       List available commands\n"
82            "  -h|--cmd-help cmd   Display detailed help on 'cmd'\n"
83            "  -a image            Add image\n"
84            "  -m dev[:mnt]        Mount dev on mnt (if omitted, /)\n"
85            /*"  --ro|-r             All mounts are read-only\n"*/
86            "  -v|--verbose        Verbose messages\n"
87            "For more information, see the manpage guestfish(1).\n");
88 }
89
90 int
91 main (int argc, char *argv[])
92 {
93   static const char *options = "a:h::m:v?";
94   static struct option long_options[] = {
95     { "add", 1, 0, 'a' },
96     { "cmd-help", 2, 0, 'h' },
97     { "help", 0, 0, '?' },
98     { "mount", 1, 0, 'm' },
99     { "verbose", 0, 0, 'v' },
100     { 0, 0, 0, 0 }
101   };
102   struct mp *mps = NULL;
103   struct mp *mp;
104   char *p;
105   int c;
106
107   /* guestfs_create is meant to be a lightweight operation, so
108    * it's OK to do it early here.
109    */
110   g = guestfs_create ();
111   if (g == NULL) {
112     fprintf (stderr, "guestfs_create: failed to create handle\n");
113     exit (1);
114   }
115
116   for (;;) {
117     c = getopt_long (argc, argv, options, long_options, NULL);
118     if (c == -1) break;
119
120     switch (c) {
121     case 'a':
122       if (access (optarg, R_OK) != 0) {
123         perror (optarg);
124         exit (1);
125       }
126       if (guestfs_add_drive (g, optarg) == -1)
127         exit (1);
128       break;
129
130     case 'h':
131       if (optarg)
132         display_command (optarg);
133       else if (argv[optind] && argv[optind][0] != '-')
134         display_command (argv[optind++]);
135       else
136         list_commands ();
137       exit (0);
138
139     case 'm':
140       mp = malloc (sizeof (struct mp));
141       if (!mp) {
142         perror ("malloc");
143         exit (1);
144       }
145       p = strchr (optarg, ':');
146       if (p) {
147         *p = '\0';
148         mp->mountpoint = p+1;
149       } else
150         mp->mountpoint = "/";
151       mp->device = optarg;
152       mp->next = mps;
153       mps = mp->next;
154       break;
155
156     case 'v':
157       verbose++;
158       guestfs_set_verbose (g, verbose);
159       break;
160
161     case '?':
162       usage ();
163       exit (0);
164
165     default:
166       fprintf (stderr, "guestfish: unexpected command line option 0x%x\n", c);
167       exit (1);
168     }
169   }
170
171   /* If we've got mountpoints, we must launch the guest and mount them. */
172   if (mps != NULL) {
173     if (launch () == -1) exit (1);
174     mount_mps (mps);
175   }
176
177   /* Interactive, shell script, or command(s) on the command line? */
178   if (optind >= argc) {
179     if (isatty (0))
180       interactive ();
181     else
182       shell_script ();
183   }
184   else
185     cmdline (argv, optind, argc);
186
187   exit (0);
188 }
189
190 void
191 pod2text (const char *heading, const char *str)
192 {
193   FILE *fp;
194
195   fp = popen ("pod2text", "w");
196   if (fp == NULL) {
197     /* pod2text failed, maybe not found, so let's just print the
198      * source instead, since that's better than doing nothing.
199      */
200     printf ("%s\n\n%s\n", heading, str);
201     return;
202   }
203   fputs ("=head1 ", fp);
204   fputs (heading, fp);
205   fputs ("\n\n", fp);
206   fputs (str, fp);
207   pclose (fp);
208 }
209
210 /* List is built in reverse order, so mount them in reverse order. */
211 static void
212 mount_mps (struct mp *mp)
213 {
214   if (mp) {
215     mount_mps (mp->next);
216     if (guestfs_mount (g, mp->device, mp->mountpoint) == -1)
217       exit (1);
218   }
219 }
220
221 static void
222 interactive (void)
223 {
224   script (1);
225 }
226
227 static void
228 shell_script (void)
229 {
230   script (0);
231 }
232
233 static void
234 script (int prompt)
235 {
236   char buf[8192];
237   char *cmd;
238   char *argv[64];
239   int len, i;
240
241   if (prompt)
242     printf ("\n"
243             "Welcome to guestfish, the libguestfs filesystem interactive shell for\n"
244             "editing virtual machine filesystems.\n"
245             "\n"
246             "Type: 'help' for help with commands\n"
247             "      'quit' to quit the shell\n"
248             "\n");
249
250   while (!quit) {
251     if (prompt) printf ("><fs> ");
252     if (fgets (buf, sizeof buf, stdin) == NULL) {
253       quit = 1;
254       break;
255     }
256
257     len = strlen (buf);
258     if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
259
260     /* Split the buffer up at whitespace. */
261     cmd = strtok (buf, " \t");
262     if (cmd == NULL)
263       continue;
264
265     i = 0;
266     while (i < sizeof argv / sizeof argv[0] &&
267            (argv[i] = strtok (NULL, " \t")) != NULL)
268       i++;
269     if (i == sizeof argv / sizeof argv[0]) {
270       fprintf (stderr, "guestfish: too many arguments in command\n");
271       exit (1);
272     }
273
274     (void) issue_command (cmd, argv);
275   }
276   if (prompt) printf ("\n");
277 }
278
279 static void
280 cmdline (char *argv[], int optind, int argc)
281 {
282   const char *cmd;
283   char **params;
284
285   if (optind >= argc) return;
286
287   cmd = argv[optind++];
288   if (strcmp (cmd, ":") == 0) {
289     fprintf (stderr, "guestfish: empty command on command line\n");
290     exit (1);
291   }
292   params = &argv[optind];
293
294   /* Search for end of command list or ":" ... */
295   while (optind < argc && strcmp (argv[optind], ":") != 0)
296     optind++;
297
298   if (optind == argc) {
299     if (issue_command (cmd, params) == -1) exit (1);
300   } else {
301     argv[optind] = NULL;
302     if (issue_command (cmd, params) == -1) exit (1);
303     cmdline (argv, optind+1, argc);
304   }
305 }
306
307 static int
308 issue_command (const char *cmd, char *argv[])
309 {
310   int argc;
311
312   for (argc = 0; argv[argc] != NULL; ++argc)
313     ;
314
315   if (strcasecmp (cmd, "help") == 0) {
316     if (argc == 0)
317       list_commands ();
318     else
319       display_command (argv[0]);
320     return 0;
321   }
322   else if (strcasecmp (cmd, "quit") == 0 ||
323            strcasecmp (cmd, "exit") == 0 ||
324            strcasecmp (cmd, "q") == 0)
325     exit (0);
326   else if (strcasecmp (cmd, "add") == 0 ||
327            strcasecmp (cmd, "drive") == 0 ||
328            strcasecmp (cmd, "add_drive") == 0) {
329     if (argc != 1) {
330       fprintf (stderr, "use 'add image' to add a guest image\n");
331       return -1;
332     }
333     else
334       return guestfs_add_drive (g, argv[0]);
335   }
336   else if (strcasecmp (cmd, "cdrom") == 0) {
337     if (argc != 1) {
338       fprintf (stderr, "use 'cdrom image' to add a CD-ROM image\n");
339       return -1;
340     }
341     else
342       return guestfs_add_cdrom (g, argv[0]);
343   }
344   else if (strcasecmp (cmd, "launch") == 0) {
345     if (argc != 0) {
346       fprintf (stderr, "'launch' command takes no parameters\n");
347       return -1;
348     }
349     else
350       return launch ();
351   }
352   else
353     return run_action (cmd, argc, argv);
354 }
355
356 void
357 list_builtin_commands (void)
358 {
359   printf ("%-20s %s\n",
360           "help", "display a list of commands or help on a command");
361   printf ("%-20s %s\n",
362           "quit", "quit guestfish");
363   printf ("%-20s %s\n",
364           "add", "add a guest image to be examined or modified");
365   printf ("%-20s %s\n",
366           "cdrom", "add a CD-ROM image to be examined");
367   printf ("%-20s %s\n",
368           "launch", "launch the subprocess");
369 }
370
371 void
372 display_builtin_command (const char *cmd)
373 {
374   if (strcasecmp (cmd, "add") == 0)
375     printf ("add - add a guest image to be examined or modified\n"
376             "     add <image>\n");
377   else if (strcasecmp (cmd, "cdrom") == 0)
378     printf ("cdrom - add a CD-ROM image to be examined\n"
379             "     cdrom <iso-file>\n");
380   else if (strcasecmp (cmd, "help") == 0)
381     printf ("help - display a list of commands or help on a command\n"
382             "     help cmd\n"
383             "     help\n");
384   else if (strcasecmp (cmd, "quit") == 0)
385     printf ("quit - quit guestfish\n"
386             "     quit\n");
387   else if (strcasecmp (cmd, "launch") == 0)
388     printf ("launch - launch the subprocess\n"
389             "     launch\n");
390   else
391     fprintf (stderr, "%s: command not known, use -h to list all commands\n",
392              cmd);
393 }