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