summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Jones <rjones@redhat.com>2009-04-03 23:47:47 +0100
committerRichard Jones <rjones@redhat.com>2009-04-03 23:47:47 +0100
commitbc0b3d6f473e607972d29be27f7ae6b1d0489c3b (patch)
treea2129e7328c1adff8c32890d57a86c36b908ecff
parentb72ec21d24824720385c9cd024b7f1eae734f4a4 (diff)
downloadlibguestfs-bc0b3d6f473e607972d29be27f7ae6b1d0489c3b.tar.gz
libguestfs-bc0b3d6f473e607972d29be27f7ae6b1d0489c3b.tar.xz
libguestfs-bc0b3d6f473e607972d29be27f7ae6b1d0489c3b.zip
Added outline of shell command, added generator support.
-rw-r--r--.gitignore1
-rw-r--r--Makefile.am2
-rw-r--r--configure.ac4
-rw-r--r--fish/Makefile.am25
-rw-r--r--fish/cmds.c53
-rw-r--r--fish/fish.c125
-rw-r--r--fish/fish.h29
-rw-r--r--guestfs-actions.pod2
-rwxr-xr-xsrc/generator.ml64
9 files changed, 300 insertions, 5 deletions
diff --git a/.gitignore b/.gitignore
index 3b1d2f0e..f3d3dced 100644
--- a/.gitignore
+++ b/.gitignore
@@ -23,6 +23,7 @@ depcomp
emptydisk
examples/df
examples/hello
+fish/guestfish
guestfs.3
initramfs
initramfs.timestamp
diff --git a/Makefile.am b/Makefile.am
index 621df7be..9dc45a31 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -17,7 +17,7 @@
ACLOCAL_AMFLAGS = -I m4
-SUBDIRS = src daemon examples
+SUBDIRS = src daemon fish examples
EXTRA_DIST = make-initramfs.sh
diff --git a/configure.ac b/configure.ac
index 69a55f2e..ef95cdde 100644
--- a/configure.ac
+++ b/configure.ac
@@ -91,9 +91,9 @@ AC_CONFIG_SUBDIRS([daemon])
dnl Produce output files.
AC_CONFIG_HEADERS([config.h])
-AC_CONFIG_FILES([Makefile src/Makefile examples/Makefile
+AC_CONFIG_FILES([Makefile src/Makefile fish/Makefile examples/Makefile
make-initramfs.sh update-initramfs.sh])
AC_OUTPUT
dnl WTF?
-chmod +x make-initramfs.sh \ No newline at end of file
+chmod +x make-initramfs.sh update-initramfs.sh
diff --git a/fish/Makefile.am b/fish/Makefile.am
new file mode 100644
index 00000000..b8f07ed8
--- /dev/null
+++ b/fish/Makefile.am
@@ -0,0 +1,25 @@
+# libguestfs
+# Copyright (C) 2009 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+bin_PROGRAMS = guestfish
+
+guestfish_SOURCES = \
+ cmds.c \
+ fish.c \
+ fish.h
+guestfish_CFLAGS = -I$(top_builddir)/src -Wall
+guestfish_LDADD = $(top_builddir)/src/libguestfs.la
diff --git a/fish/cmds.c b/fish/cmds.c
new file mode 100644
index 00000000..d0ab20c7
--- /dev/null
+++ b/fish/cmds.c
@@ -0,0 +1,53 @@
+/* libguestfs generated file
+ * WARNING: THIS FILE IS GENERATED BY 'src/generator.ml'.
+ * ANY CHANGES YOU MAKE TO THIS FILE WILL BE LOST.
+ *
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "fish.h"
+
+void list_commands (void)
+{
+ printf ("%-20s %s\n", "Command", "Description");
+ printf ("%-20s %s\n", "mount", "Mount a guest disk at a position in the filesystem");
+ printf ("%-20s %s\n", "sync", "Sync disks, writes are flushed through to the disk image");
+ printf ("%-20s %s\n", "touch", "Update file timestamps or create a new file");
+ printf ("Use -h <cmd> to show detailed help for a command.\n");
+}
+
+void display_command (const char *cmd)
+{
+ if (strcasecmp (cmd, "mount") == 0)
+ pod2text ("mount - Mount a guest disk at a position in the filesystem", " mount <device> <mountpoint>\n\nMount a guest disk at a position in the filesystem. Block devices\nare named C</dev/sda>, C</dev/sdb> and so on, as they were added to\nthe guest. If those block devices contain partitions, they will have\nthe usual names (eg. C</dev/sda1>). Also LVM C</dev/VG/LV>-style\nnames can be used.\n\nThe rules are the same as for L<mount(2)>: A filesystem must\nfirst be mounted on C</> before others can be mounted. Other\nfilesystems can only be mounted on directories which already\nexist.\n\nThe mounted filesystem is writable, if we have sufficient permissions\non the underlying device.\n\nThe filesystem options C<sync> and C<noatime> are set with this\ncall, in order to improve reliability.");
+ else
+ if (strcasecmp (cmd, "sync") == 0)
+ pod2text ("sync - Sync disks, writes are flushed through to the disk image", " sync\n\nThis syncs the disk, so that any writes are flushed through to the\nunderlying disk image.\n\nYou should always call this if you have modified a disk image, before\ncalling C<guestfs_close>.");
+ else
+ if (strcasecmp (cmd, "touch") == 0)
+ pod2text ("touch - Update file timestamps or create a new file", " touch <path>\n\nTouch acts like the L<touch(1)> command. It can be used to\nupdate the timestamps on a file, or, if the file does not exist,\nto create a new zero-length file.");
+ else
+ {
+ fprintf (stderr, "%s: command not known, use -h to list all commands\n", cmd);
+ exit (1);
+ }
+}
+
diff --git a/fish/fish.c b/fish/fish.c
new file mode 100644
index 00000000..85543004
--- /dev/null
+++ b/fish/fish.c
@@ -0,0 +1,125 @@
+/* guestfish - the filesystem shell
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <getopt.h>
+
+#include <guestfs.h>
+
+#include "fish.h"
+
+static void
+usage (void)
+{
+ fprintf (stderr,
+ "guestfish: guest filesystem shell\n"
+ "guestfish lets you edit virtual machine filesystems\n"
+ "Copyright (C) 2009 Red Hat Inc.\n"
+ "Usage:\n"
+ " guestfish [--options] [cmd]\n"
+ "or for interactive use:\n"
+ " guestfish\n"
+ "or from a shell script:\n"
+ " guestfish <<EOF\n"
+ " cmd\n"
+ " ...\n"
+ " EOF\n"
+ "Options:\n"
+ " -h|--cmd-help List available commands\n"
+ " -h|--cmd-help cmd Display detailed help on 'cmd'\n"
+ "For more information, see the manual page guestfish(1).\n");
+}
+
+int
+main (int argc, char *argv[])
+{
+ static const char *options = "h::?";
+ static struct option long_options[] = {
+ { "cmd-help", 2, 0, 'h' },
+ { "help", 0, 0, '?' },
+ { 0, 0, 0, 0 }
+ };
+ int c;
+
+ for (;;) {
+ c = getopt_long (argc, argv, options, long_options, NULL);
+ if (c == -1) break;
+
+ switch (c) {
+ case 'h':
+ if (optarg)
+ display_command (optarg);
+ else if (argv[optind] && argv[optind][0] != '-')
+ display_command (argv[optind++]);
+ else
+ list_commands ();
+ exit (0);
+
+ case '?':
+ usage ();
+ exit (0);
+
+ default:
+ fprintf (stderr, "guestfish: unexpected command line option 0x%x\n", c);
+ exit (1);
+ }
+ }
+
+ if (optind < argc) {
+ usage ();
+ exit (1);
+ }
+
+
+
+
+
+
+
+
+
+
+
+
+ exit (0);
+}
+
+void
+pod2text (const char *heading, const char *str)
+{
+ FILE *fp;
+
+ fp = popen ("pod2text", "w");
+ if (fp == NULL) {
+ /* pod2text failed, maybe not found, so let's just print the
+ * source instead, since that's better than doing nothing.
+ */
+ printf ("%s\n\n%s\n", heading, str);
+ return;
+ }
+ fputs ("=head1 ", fp);
+ fputs (heading, fp);
+ fputs ("\n\n", fp);
+ fputs (str, fp);
+ pclose (fp);
+}
diff --git a/fish/fish.h b/fish/fish.h
new file mode 100644
index 00000000..3e811703
--- /dev/null
+++ b/fish/fish.h
@@ -0,0 +1,29 @@
+/* libguestfs - the guestfsd daemon
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef FISH_H
+#define FISH_H
+
+/* in fish.c */
+extern void pod2text (const char *heading, const char *body);
+
+/* in cmds.c (auto-generated) */
+extern void list_commands (void);
+extern void display_command (const char *cmd);
+
+#endif /* FISH_H */
diff --git a/guestfs-actions.pod b/guestfs-actions.pod
index f1ef3320..2a3d62db 100644
--- a/guestfs-actions.pod
+++ b/guestfs-actions.pod
@@ -41,7 +41,7 @@ This function return 0 on success or -1 on error.
const char *path);
Touch acts like the L<touch(1)> command. It can be used to
-update the filesystems on a file, or, if the file does not exist,
+update the timestamps on a file, or, if the file does not exist,
to create a new zero-length file.
This function return 0 on success or -1 on error.
diff --git a/src/generator.ml b/src/generator.ml
index 5868e170..b26ea3c1 100755
--- a/src/generator.ml
+++ b/src/generator.ml
@@ -74,7 +74,7 @@ calling C<guestfs_close>.");
"Update file timestamps or create a new file",
"\
Touch acts like the L<touch(1)> command. It can be used to
-update the filesystems on a file, or, if the file does not exist,
+update the timestamps on a file, or, if the file does not exist,
to create a new zero-length file.");
]
@@ -87,6 +87,11 @@ let iter_args f = function
| P1 arg1 -> f arg1
| P2 (arg1, arg2) -> f arg1; f arg2
+let map_args f = function
+ | P0 -> []
+ | P1 arg1 -> [f arg1]
+ | P2 (arg1, arg2) -> [f arg1; f arg2]
+
type comment_style = CStyle | HashStyle | OCamlStyle
type license = GPLv2 | LGPLv2
@@ -446,7 +451,60 @@ and generate_daemon_actions () =
pr " default:\n";
pr " reply_with_error (\"dispatch_incoming_message: unknown procedure number %%d\", proc_nr);\n";
pr " }\n";
+ pr "}\n"
+
+and generate_fish_cmds () =
+ generate_header CStyle GPLv2;
+
+ pr "#include <stdio.h>\n";
+ pr "#include <stdlib.h>\n";
+ pr "#include <string.h>\n";
+ pr "\n";
+ pr "#include \"fish.h\"\n";
+ pr "\n";
+
+ (* list_commands function, which implements guestfish -h *)
+ pr "void list_commands (void)\n";
+ pr "{\n";
+ pr " printf (\"%%-20s %%s\\n\", \"Command\", \"Description\");\n";
+ List.iter (
+ fun (name, _, _, shortdesc, _) ->
+ pr " printf (\"%%-20s %%s\\n\", \"%s\", \"%s\");\n"
+ name shortdesc
+ ) functions;
+ pr " printf (\"Use -h <cmd> to show detailed help for a command.\\n\");\n";
pr "}\n";
+ pr "\n";
+
+ (* display_command function, which implements guestfish -h cmd *)
+ pr "void display_command (const char *cmd)\n";
+ pr "{\n";
+ List.iter (
+ fun (name, style, _, shortdesc, longdesc) ->
+ let synopsis =
+ match style with
+ | (Err, P0) -> name
+ | (Err, args) ->
+ sprintf "%s <%s>"
+ name (
+ String.concat "> <" (
+ map_args (function
+ | String n -> n) args
+ )
+ ) in
+
+ pr " if (strcasecmp (cmd, \"%s\") == 0)\n" name;
+ pr " pod2text (\"%s - %s\", %S);\n"
+ name shortdesc
+ (" " ^ synopsis ^ "\n\n" ^ longdesc);
+ pr " else\n"
+ ) functions;
+ pr " {\n";
+ pr " fprintf (stderr, \"%%s: command not known, use -h to list all commands\\n\", cmd);\n";
+ pr " exit (1);\n";
+ pr " }\n";
+ pr "}\n";
+ pr "\n"
(* Generate a C function prototype. *)
and generate_prototype ?(extern = true) ?(static = false) ?(semicolon = true)
@@ -527,6 +585,10 @@ let () =
generate_daemon_actions ();
close ();
+ let close = output_to "fish/cmds.c" in
+ generate_fish_cmds ();
+ close ();
+
let close = output_to "guestfs-actions.pod" in
generate_pod ();
close ()