mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-21 22:53:37 +00:00
Implemented running actions in guestfish.
This commit is contained in:
48
fish/cmds.c
48
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);
|
||||
|
||||
@@ -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_<action> 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";
|
||||
|
||||
Reference in New Issue
Block a user