Added outline of shell command, added generator support.
[libguestfs.git] / fish / fish.c
1 /* guestfish - the filesystem 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 static void
32 usage (void)
33 {
34   fprintf (stderr,
35            "guestfish: guest filesystem shell\n"
36            "guestfish lets you edit virtual machine filesystems\n"
37            "Copyright (C) 2009 Red Hat Inc.\n"
38            "Usage:\n"
39            "  guestfish [--options] [cmd]\n"
40            "or for interactive use:\n"
41            "  guestfish\n"
42            "or from a shell script:\n"
43            "  guestfish <<EOF\n"
44            "  cmd\n"
45            "  ...\n"
46            "  EOF\n"
47            "Options:\n"
48            "  -h|--cmd-help       List available commands\n"
49            "  -h|--cmd-help cmd   Display detailed help on 'cmd'\n"
50            "For more information, see the manual page guestfish(1).\n");
51 }
52
53 int
54 main (int argc, char *argv[])
55 {
56   static const char *options = "h::?";
57   static struct option long_options[] = {
58     { "cmd-help", 2, 0, 'h' },
59     { "help", 0, 0, '?' },
60     { 0, 0, 0, 0 }
61   };
62   int c;
63
64   for (;;) {
65     c = getopt_long (argc, argv, options, long_options, NULL);
66     if (c == -1) break;
67
68     switch (c) {
69     case 'h':
70       if (optarg)
71         display_command (optarg);
72       else if (argv[optind] && argv[optind][0] != '-')
73         display_command (argv[optind++]);
74       else
75         list_commands ();
76       exit (0);
77
78     case '?':
79       usage ();
80       exit (0);
81
82     default:
83       fprintf (stderr, "guestfish: unexpected command line option 0x%x\n", c);
84       exit (1);
85     }
86   }
87
88   if (optind < argc) {
89     usage ();
90     exit (1);
91   }
92
93
94
95
96
97
98
99
100
101
102
103
104   exit (0);
105 }
106
107 void
108 pod2text (const char *heading, const char *str)
109 {
110   FILE *fp;
111
112   fp = popen ("pod2text", "w");
113   if (fp == NULL) {
114     /* pod2text failed, maybe not found, so let's just print the
115      * source instead, since that's better than doing nothing.
116      */
117     printf ("%s\n\n%s\n", heading, str);
118     return;
119   }
120   fputs ("=head1 ", fp);
121   fputs (heading, fp);
122   fputs ("\n\n", fp);
123   fputs (str, fp);
124   pclose (fp);
125 }