From: Richard Jones Date: Fri, 3 Apr 2009 22:47:47 +0000 (+0100) Subject: Added outline of shell command, added generator support. X-Git-Tag: 0.4~24 X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=commitdiff_plain;h=bc0b3d6f473e607972d29be27f7ae6b1d0489c3b Added outline of shell command, added generator support. --- diff --git a/.gitignore b/.gitignore index 3b1d2f0..f3d3dce 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 621df7b..9dc45a3 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 69a55f2..ef95cdd 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 0000000..b8f07ed --- /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 0000000..d0ab20c --- /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 +#include +#include + +#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 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 \n\nMount a guest disk at a position in the filesystem. Block devices\nare named C, C and so on, as they were added to\nthe guest. If those block devices contain partitions, they will have\nthe usual names (eg. C). Also LVM C-style\nnames can be used.\n\nThe rules are the same as for L: 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 and C 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."); + else + if (strcasecmp (cmd, "touch") == 0) + pod2text ("touch - Update file timestamps or create a new file", " touch \n\nTouch acts like the L 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 0000000..8554300 --- /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 + +#include +#include +#include +#include +#include + +#include + +#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 < 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 5868e17..b26ea3c 100755 --- a/src/generator.ml +++ b/src/generator.ml @@ -74,7 +74,7 @@ calling C."); "Update file timestamps or create a new file", "\ Touch acts like the L 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 \n"; + pr "#include \n"; + pr "#include \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 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 ()