From: Richard Jones Date: Sat, 4 Apr 2009 08:40:44 +0000 (+0100) Subject: Implemented running actions in guestfish. X-Git-Tag: 0.4~20 X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=6d37b7698c1e2add43f0e8908e1bc30700f95f7e;p=libguestfs.git Implemented running actions in guestfish. --- diff --git a/fish/cmds.c b/fish/cmds.c index a181b87..bc9d5a9 100644 --- a/fish/cmds.c +++ b/fish/cmds.c @@ -49,16 +49,58 @@ void display_command (const char *cmd) display_builtin_command (cmd); } +static int run_mount (const char *cmd, int argc, char *argv[]) +{ + int r; + const char *device; + const char *mountpoint; + if (argc != 2) { + fprintf (stderr, "%s should have 2 parameter(s)\n", cmd); + fprintf (stderr, "type 'help %s' for help on %s\n", cmd, cmd); + return -1; + } + device = argv[0]; + mountpoint = argv[1]; + r = guestfs_mount (g, device, mountpoint); + return r; +} + +static int run_sync (const char *cmd, int argc, char *argv[]) +{ + int r; + if (argc != 0) { + fprintf (stderr, "%s should have 0 parameter(s)\n", cmd); + fprintf (stderr, "type 'help %s' for help on %s\n", cmd, cmd); + return -1; + } + r = guestfs_sync (g); + return r; +} + +static int run_touch (const char *cmd, int argc, char *argv[]) +{ + int r; + const char *path; + if (argc != 1) { + fprintf (stderr, "%s should have 1 parameter(s)\n", cmd); + fprintf (stderr, "type 'help %s' for help on %s\n", cmd, cmd); + return -1; + } + path = argv[0]; + r = guestfs_touch (g, path); + return r; +} + int run_action (const char *cmd, int argc, char *argv[]) { if (strcasecmp (cmd, "mount") == 0) - printf ("running mount ...\n"); + return run_mount (cmd, argc, argv); else if (strcasecmp (cmd, "sync") == 0) - printf ("running sync ...\n"); + return run_sync (cmd, argc, argv); else if (strcasecmp (cmd, "touch") == 0) - printf ("running touch ...\n"); + return run_touch (cmd, argc, argv); else { fprintf (stderr, "%s: unknown command\n", cmd); diff --git a/src/generator.ml b/src/generator.ml index fbb4a40..8fa5cbc 100755 --- a/src/generator.ml +++ b/src/generator.ml @@ -87,11 +87,18 @@ let iter_args f = function | P1 arg1 -> f arg1 | P2 (arg1, arg2) -> f arg1; f arg2 +let iteri_args f = function + | P0 -> () + | P1 arg1 -> f 0 arg1 + | P2 (arg1, arg2) -> f 0 arg1; f 1 arg2 + let map_args f = function | P0 -> [] | P1 arg1 -> [f arg1] | P2 (arg1, arg2) -> [f arg1; f arg2] +let nr_args = function | P0 -> 0 | P1 _ -> 1 | P2 _ -> 2 + type comment_style = CStyle | HashStyle | OCamlStyle type license = GPLv2 | LGPLv2 @@ -453,6 +460,7 @@ and generate_daemon_actions () = pr " }\n"; pr "}\n" +(* Generate a lot of different functions for guestfish. *) and generate_fish_cmds () = generate_header CStyle GPLv2; @@ -504,13 +512,53 @@ and generate_fish_cmds () = pr "}\n"; pr "\n"; + (* run_ actions *) + List.iter ( + fun (name, style, _, _, _) -> + pr "static int run_%s (const char *cmd, int argc, char *argv[])\n" name; + pr "{\n"; + (match style with + | (Err, _) -> pr " int r;\n" + ); + iter_args ( + function + | String name -> pr " const char *%s;\n" name + ) (snd style); + + (* Check and convert parameters. *) + let argc_expected = nr_args (snd style) in + pr " if (argc != %d) {\n" argc_expected; + pr " fprintf (stderr, \"%%s should have %d parameter(s)\\n\", cmd);\n" + argc_expected; + pr " fprintf (stderr, \"type 'help %%s' for help on %%s\\n\", cmd, cmd);\n"; + pr " return -1;\n"; + pr " }\n"; + iteri_args ( + fun i -> + function + | String name -> pr " %s = argv[%d];\n" name i + ) (snd style); + + (* Call C API function. *) + pr " r = guestfs_%s " name; + generate_call_args ~handle:"g" style; + pr ";\n"; + + (* Check return value for errors. *) + (match style with + | (Err, _) -> pr " return r;\n" + ); + pr "}\n"; + pr "\n" + ) functions; + (* run_action function *) pr "int run_action (const char *cmd, int argc, char *argv[])\n"; pr "{\n"; List.iter ( - fun (name, style, _, _, _) -> + fun (name, _, _, _, _) -> pr " if (strcasecmp (cmd, \"%s\") == 0)\n" name; - pr " printf (\"running %s ...\\n\");\n" name; + pr " return run_%s (cmd, argc, argv);\n" name; pr " else\n"; ) functions; pr " {\n";