2 * Copyright (C) 2009-2010 Red Hat Inc.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 (* Please read generator/README first. *)
26 open Generator_docstrings
27 open Generator_optgroups
28 open Generator_actions
29 open Generator_structs
30 open Generator_prepopts
34 let doc_opttype_of = function
35 | Bool n -> "true|false"
41 (* Generate a lot of different functions for guestfish. *)
42 let generate_fish_cmds () =
43 generate_header CStyle GPLv2plus;
47 fun (_, _, _, flags, _, _, _) -> not (List.mem NotInFish flags)
49 let all_functions_sorted =
51 fun (_, _, _, flags, _, _, _) -> not (List.mem NotInFish flags)
52 ) all_functions_sorted in
54 let all_functions_and_fish_commands_sorted =
55 List.sort action_compare (all_functions_sorted @ fish_commands) in
57 pr "#include <config.h>\n";
59 pr "/* It is safe to call deprecated functions from this file. */\n";
60 pr "#undef GUESTFS_WARN_DEPRECATED\n";
62 pr "#include <stdio.h>\n";
63 pr "#include <stdlib.h>\n";
64 pr "#include <string.h>\n";
65 pr "#include <inttypes.h>\n";
67 pr "#include \"c-ctype.h\"\n";
68 pr "#include \"full-write.h\"\n";
69 pr "#include \"xstrtol.h\"\n";
71 pr "#include <guestfs.h>\n";
72 pr "#include \"fish.h\"\n";
73 pr "#include \"fish-cmds.h\"\n";
74 pr "#include \"options.h\"\n";
75 pr "#include \"cmds_gperf.h\"\n";
77 pr "/* Valid suffixes allowed for numbers. See Gnulib xstrtol function. */\n";
78 pr "static const char *xstrtol_suffixes = \"0kKMGTPEZY\";\n";
82 fun (name, _, _, _, _, _, _) ->
83 pr "static int run_%s (const char *cmd, size_t argc, char *argv[]);\n"
89 (* List of command_entry structs. *)
91 fun (name, _, _, flags, _, shortdesc, longdesc) ->
92 let name2 = replace_char name '_' '-' in
94 filter_map (function FishAlias n -> Some n | _ -> None) flags in
97 sprintf "\n\nYou can use %s as an alias for this command."
98 (String.concat " or " (List.map (fun s -> "'" ^ s ^ "'") aliases))
102 sprintf "%s - %s\n\n=head1 DESCRIPTION\n\n%s\n\n%s"
103 name2 shortdesc longdesc describe_alias in
105 String.concat "\n" (pod2text ~trim:false ~discard:false "NAME" pod)
108 pr "struct command_entry %s_cmd_entry = {\n" name;
109 pr " .name = \"%s\",\n" name2;
110 pr " .help = \"%s\",\n" (c_quote text);
111 pr " .run = run_%s\n" name;
117 fun (name, (_, args, optargs), _, flags, _, shortdesc, longdesc) ->
118 let name2 = replace_char name '_' '-' in
120 filter_map (function FishAlias n -> Some n | _ -> None) flags in
122 let longdesc = replace_str longdesc "C<guestfs_" "C<" in
127 let args = List.filter (function Key _ -> false | _ -> true) args in
131 (List.map (fun arg -> " " ^ name_of_argt arg) args))
133 (List.map (fun arg ->
134 sprintf " [%s:%s]" (name_of_argt arg) (doc_opttype_of arg)
138 if List.exists (function Key _ -> true | _ -> false) args then
139 "\n\nThis command has one or more key or passphrase parameters.
140 Guestfish will prompt for these separately."
145 if List.mem ProtocolLimitWarning flags then
146 ("\n\n" ^ protocol_limit_warning)
151 match deprecation_notice flags with
153 | Some txt -> "\n\n" ^ txt in
156 if aliases <> [] then
157 sprintf "\n\nYou can use %s as an alias for this command."
158 (String.concat " or " (List.map (fun s -> "'" ^ s ^ "'") aliases))
162 sprintf "%s - %s\n\n=head1 SYNOPSIS\n\n %s\n\n=head1 DESCRIPTION\n\n%s%s%s"
163 name2 shortdesc synopsis longdesc warnings describe_alias in
165 String.concat "\n" (pod2text ~trim:false ~discard:false "NAME" pod)
168 pr "struct command_entry %s_cmd_entry = {\n" name;
169 pr " .name = \"%s\",\n" name2;
170 pr " .help = \"%s\",\n" (c_quote text);
171 pr " .run = run_%s\n" name;
176 (* list_commands function, which implements guestfish -h *)
178 pr "list_commands (void)\n";
180 pr " printf (\" %%-16s %%s\\n\", _(\"Command\"), _(\"Description\"));\n";
181 pr " list_builtin_commands ();\n";
183 fun (name, _, _, flags, _, shortdesc, _) ->
184 let name = replace_char name '_' '-' in
185 pr " printf (\"%%-20s %%s\\n\", \"%s\", _(\"%s\"));\n"
187 ) all_functions_and_fish_commands_sorted;
188 pr " printf (\" %%s\\n\",";
189 pr " _(\"Use -h <cmd> / help <cmd> to show detailed help for a command.\"));\n";
193 (* display_command function, which implements guestfish -h cmd *)
195 pr "display_command (const char *cmd)\n";
197 pr " const struct command_table *ct;\n";
199 pr " ct = lookup_fish_command (cmd, strlen (cmd));\n";
201 pr " fputs (ct->entry->help, stdout);\n";
205 pr " return display_builtin_command (cmd);\n";
209 let emit_print_list_function typ =
211 pr "print_%s_list (struct guestfs_%s_list *%ss)\n"
214 pr " unsigned int i;\n";
216 pr " for (i = 0; i < %ss->len; ++i) {\n" typ;
217 pr " printf (\"[%%d] = {\\n\", i);\n";
218 pr " print_%s_indent (&%ss->val[i], \" \");\n" typ typ;
219 pr " printf (\"}\\n\");\n";
225 (* print_* functions *)
229 List.exists (function (_, (FUUID|FBuffer)) -> true | _ -> false) cols in
232 pr "print_%s_indent (struct guestfs_%s *%s, const char *indent)\n" typ typ typ;
235 pr " unsigned int i;\n";
241 pr " printf (\"%%s%s: %%s\\n\", indent, %s->%s);\n" name typ name
243 pr " printf (\"%%s%s: \", indent);\n" name;
244 pr " for (i = 0; i < 32; ++i)\n";
245 pr " printf (\"%%c\", %s->%s[i]);\n" typ name;
246 pr " printf (\"\\n\");\n"
248 pr " printf (\"%%s%s: \", indent);\n" name;
249 pr " for (i = 0; i < %s->%s_len; ++i)\n" typ name;
250 pr " if (c_isprint (%s->%s[i]))\n" typ name;
251 pr " printf (\"%%c\", %s->%s[i]);\n" typ name;
253 pr " printf (\"\\\\x%%02x\", %s->%s[i]);\n" typ name;
254 pr " printf (\"\\n\");\n"
255 | name, (FUInt64|FBytes) ->
256 pr " printf (\"%%s%s: %%\" PRIu64 \"\\n\", indent, %s->%s);\n"
259 pr " printf (\"%%s%s: %%\" PRIi64 \"\\n\", indent, %s->%s);\n"
262 pr " printf (\"%%s%s: %%\" PRIu32 \"\\n\", indent, %s->%s);\n"
265 pr " printf (\"%%s%s: %%\" PRIi32 \"\\n\", indent, %s->%s);\n"
268 pr " printf (\"%%s%s: %%c\\n\", indent, %s->%s);\n"
270 | name, FOptPercent ->
271 pr " if (%s->%s >= 0)\n" typ name;
272 pr " printf (\"%%s%s: %%g %%%%\\n\", indent, (double) %s->%s);\n"
275 pr " printf (\"%%s%s: \\n\", indent);\n" name
281 (* Emit a print_TYPE_list function definition only if that function is used. *)
284 | typ, (RStructListOnly | RStructAndList) ->
285 (* generate the function for typ *)
286 emit_print_list_function typ
287 | typ, _ -> () (* empty *)
288 ) (rstructs_used_by all_functions);
290 (* Emit a print_TYPE function definition only if that function is used. *)
293 | typ, (RStructOnly | RStructAndList) ->
295 pr "print_%s (struct guestfs_%s *%s)\n" typ typ typ;
297 pr " print_%s_indent (%s, \"\");\n" typ typ;
300 | typ, _ -> () (* empty *)
301 ) (rstructs_used_by all_functions);
303 (* run_<action> actions *)
305 fun (name, (ret, args, optargs as style), _, flags, _, _, _) ->
307 pr "run_%s (const char *cmd, size_t argc, char *argv[])\n" name;
312 | RBool _ -> pr " int r;\n"
313 | RInt64 _ -> pr " int64_t r;\n"
314 | RConstString _ | RConstOptString _ -> pr " const char *r;\n"
315 | RString _ -> pr " char *r;\n"
316 | RStringList _ | RHashtable _ -> pr " char **r;\n"
317 | RStruct (_, typ) -> pr " struct guestfs_%s *r;\n" typ
318 | RStructList (_, typ) -> pr " struct guestfs_%s_list *r;\n" typ
321 pr " size_t size;\n";
327 | OptString n -> pr " const char *%s;\n" n
332 | Key n -> pr " char *%s;\n" n
334 pr " const char *%s;\n" n;
335 pr " size_t %s_size;\n" n
336 | StringList n | DeviceList n -> pr " char **%s;\n" n
337 | Bool n -> pr " int %s;\n" n
338 | Int n -> pr " int %s;\n" n
339 | Int64 n -> pr " int64_t %s;\n" n
340 | Pointer _ -> assert false
343 if optargs <> [] then (
344 pr " struct guestfs_%s_argv optargs_s = { .bitmask = 0 };\n" name;
345 pr " struct guestfs_%s_argv *optargs = &optargs_s;\n" name
348 if args <> [] || optargs <> [] then
349 pr " size_t i = 0;\n";
353 (* Check and convert parameters. *)
354 let argc_minimum, argc_maximum =
356 List.filter (function Key _ -> false | _ -> true) args in
357 let argc_minimum = List.length args_no_keys in
358 let argc_maximum = argc_minimum + List.length optargs in
359 argc_minimum, argc_maximum in
361 if argc_minimum = argc_maximum then (
362 pr " if (argc != %d) {\n" argc_minimum;
363 pr " fprintf (stderr, _(\"%%s should have %%d parameter(s)\\n\"), cmd, %d);\n"
366 pr " if (argc < %d || argc > %d) {\n" argc_minimum argc_maximum;
367 pr " fprintf (stderr, _(\"%%s should have %%d-%%d parameter(s)\\n\"), cmd, %d, %d);\n"
368 argc_minimum argc_maximum;
370 pr " fprintf (stderr, _(\"type 'help %%s' for help on %%s\\n\"), cmd, cmd);\n";
374 let parse_integer expr fn fntyp rtyp range name =
376 pr " strtol_error xerr;\n";
379 pr " xerr = %s (%s, NULL, 0, &r, xstrtol_suffixes);\n" fn expr;
380 pr " if (xerr != LONGINT_OK) {\n";
381 pr " fprintf (stderr,\n";
382 pr " _(\"%%s: %%s: invalid integer parameter (%%s returned %%d)\\n\"),\n";
383 pr " cmd, \"%s\", \"%s\", xerr);\n" name fn;
388 | Some (min, max, comment) ->
389 pr " /* %s */\n" comment;
390 pr " if (r < %s || r > %s) {\n" min max;
391 pr " fprintf (stderr, _(\"%%s: %%s: integer out of range\\n\"), cmd, \"%s\");\n"
395 pr " /* The check above should ensure this assignment does not overflow. */\n";
397 pr " %s = r;\n" name;
405 pr " %s = argv[i++];\n" name
407 | Dev_or_Path name ->
408 pr " %s = win_prefix (argv[i++]); /* process \"win:\" prefix */\n" name;
409 pr " if (%s == NULL) return -1;\n" name
411 pr " %s = STRNEQ (argv[i], \"\") ? argv[i] : NULL;\n" name;
414 pr " %s = argv[i];\n" name;
415 pr " %s_size = strlen (argv[i]);\n" name;
418 pr " %s = file_in (argv[i++]);\n" name;
419 pr " if (%s == NULL) return -1;\n" name
421 pr " %s = file_out (argv[i++]);\n" name;
422 pr " if (%s == NULL) return -1;\n" name
423 | StringList name | DeviceList name ->
424 pr " %s = parse_string_list (argv[i++]);\n" name;
425 pr " if (%s == NULL) return -1;\n" name
427 pr " %s = read_key (\"%s\");\n" name name;
428 pr " if (keys_from_stdin)\n";
429 pr " input_lineno++;\n";
430 pr " if (%s == NULL) return -1;\n" name
432 pr " %s = is_true (argv[i++]) ? 1 : 0;\n" name
435 let min = "(-(2LL<<30))"
436 and max = "((2LL<<30)-1)"
438 "The Int type in the generator is a signed 31 bit int." in
439 Some (min, max, comment) in
440 parse_integer "argv[i++]" "xstrtoll" "long long" "int" range name
442 parse_integer "argv[i++]" "xstrtoll" "long long" "int64_t" None name
443 | Pointer _ -> assert false
446 (* Optional arguments are prefixed with <argname>:<value> and
447 * may be missing, so we need to parse those until the end of
450 if optargs <> [] then (
451 let uc_name = String.uppercase name in
453 pr " for (; i < argc; ++i) {\n";
454 pr " uint64_t this_mask;\n";
455 pr " const char *this_arg;\n";
460 let n = name_of_argt argt in
461 let uc_n = String.uppercase n in
462 let len = String.length n in
463 pr "if (STRPREFIX (argv[i], \"%s:\")) {\n" n;
466 pr " optargs_s.%s = is_true (&argv[i][%d]) ? 1 : 0;\n"
470 let min = "(-(2LL<<30))"
471 and max = "((2LL<<30)-1)"
473 "The Int type in the generator is a signed 31 bit int." in
474 Some (min, max, comment) in
475 let expr = sprintf "&argv[i][%d]" (len+1) in
476 parse_integer expr "xstrtoll" "long long" "int" range
477 (sprintf "optargs_s.%s" n)
479 let expr = sprintf "&argv[i][%d]" (len+1) in
480 parse_integer expr "xstrtoll" "long long" "int64_t" None
481 (sprintf "optargs_s.%s" n)
483 pr " optargs_s.%s = &argv[i][%d];\n" n (len+1);
486 pr " this_mask = GUESTFS_%s_%s_BITMASK;\n" uc_name uc_n;
487 pr " this_arg = \"%s\";\n" n;
493 pr " fprintf (stderr, _(\"%%s: unknown optional argument \\\"%%s\\\"\\n\"),\n";
494 pr " cmd, argv[i]);\n";
498 pr " if (optargs_s.bitmask & this_mask) {\n";
499 pr " fprintf (stderr, _(\"%%s: optional argument \\\"%%s\\\" given twice\\n\"),\n";
500 pr " cmd, this_arg);\n";
503 pr " optargs_s.bitmask |= this_mask;\n";
508 (* Call C API function. *)
510 pr " r = guestfs_%s " name
512 pr " r = guestfs_%s_argv " name;
513 generate_c_call_args ~handle:"g" style;
518 | Device _ | String _
519 | OptString _ | Bool _
522 | Pathname name | Dev_or_Path name | FileOut name
524 pr " free (%s);\n" name
526 pr " free_file_in (%s);\n" name
527 | StringList name | DeviceList name ->
528 pr " free_strings (%s);\n" name
529 | Pointer _ -> assert false
532 (* Any output flags? *)
534 let flags = filter_map (
535 function FishOutput flag -> Some flag | _ -> None
541 failwithf "%s: more than one FishOutput flag is not allowed" name in
543 (* Check return value for errors and display command results. *)
545 | RErr -> pr " return r;\n"
547 pr " if (r == -1) return -1;\n";
548 (match fish_output with
550 pr " printf (\"%%d\\n\", r);\n";
551 | Some FishOutputOctal ->
552 pr " printf (\"%%s%%o\\n\", r != 0 ? \"0\" : \"\", r);\n";
553 | Some FishOutputHexadecimal ->
554 pr " printf (\"%%s%%x\\n\", r != 0 ? \"0x\" : \"\", r);\n");
557 pr " if (r == -1) return -1;\n";
558 (match fish_output with
560 pr " printf (\"%%\" PRIi64 \"\\n\", r);\n";
561 | Some FishOutputOctal ->
562 pr " printf (\"%%s%%\" PRIo64 \"\\n\", r != 0 ? \"0\" : \"\", r);\n";
563 | Some FishOutputHexadecimal ->
564 pr " printf (\"%%s%%\" PRIx64 \"\\n\", r != 0 ? \"0x\" : \"\", r);\n");
567 pr " if (r == -1) return -1;\n";
568 pr " if (r) printf (\"true\\n\"); else printf (\"false\\n\");\n";
571 pr " if (r == NULL) return -1;\n";
572 pr " printf (\"%%s\\n\", r);\n";
574 | RConstOptString _ ->
575 pr " printf (\"%%s\\n\", r ? : \"(null)\");\n";
578 pr " if (r == NULL) return -1;\n";
579 pr " printf (\"%%s\\n\", r);\n";
583 pr " if (r == NULL) return -1;\n";
584 pr " print_strings (r);\n";
585 pr " free_strings (r);\n";
587 | RStruct (_, typ) ->
588 pr " if (r == NULL) return -1;\n";
589 pr " print_%s (r);\n" typ;
590 pr " guestfs_free_%s (r);\n" typ;
592 | RStructList (_, typ) ->
593 pr " if (r == NULL) return -1;\n";
594 pr " print_%s_list (r);\n" typ;
595 pr " guestfs_free_%s_list (r);\n" typ;
598 pr " if (r == NULL) return -1;\n";
599 pr " print_table (r);\n";
600 pr " free_strings (r);\n";
603 pr " if (r == NULL) return -1;\n";
604 pr " if (full_write (1, r, size) != size) {\n";
605 pr " perror (\"write\");\n";
616 (* run_action function *)
618 pr "run_action (const char *cmd, size_t argc, char *argv[])\n";
620 pr " const struct command_table *ct;\n";
622 pr " ct = lookup_fish_command (cmd, strlen (cmd));\n";
624 pr " return ct->entry->run (cmd, argc, argv);\n";
626 pr " fprintf (stderr, _(\"%%s: unknown command\\n\"), cmd);\n";
627 pr " if (command_num == 1)\n";
628 pr " extended_help_message ();\n";
633 and generate_fish_cmds_h () =
634 generate_header CStyle GPLv2plus;
636 pr "#ifndef FISH_CMDS_H\n";
637 pr "#define FISH_CMDS_H\n";
641 fun (shortname, _, _, _, _, _, _) ->
642 pr "extern int run_%s (const char *cmd, size_t argc, char *argv[]);\n"
647 pr "#endif /* FISH_CMDS_H */\n"
649 (* gperf code to do fast lookups of commands. *)
650 and generate_fish_cmds_gperf () =
651 generate_header CStyle GPLv2plus;
653 let all_functions_sorted =
655 fun (_, _, _, flags, _, _, _) -> not (List.mem NotInFish flags)
656 ) all_functions_sorted in
658 let all_functions_and_fish_commands_sorted =
659 List.sort action_compare (all_functions_sorted @ fish_commands) in
663 %%define lookup-function-name lookup_fish_command
675 #include \"cmds_gperf.h\"
680 fun (name, _, _, _, _, _, _) ->
681 pr "extern struct command_entry %s_cmd_entry;\n" name
682 ) all_functions_and_fish_commands_sorted;
687 struct command_table;
693 fun (name, _, _, flags, _, _, _) ->
694 let name2 = replace_char name '_' '-' in
696 filter_map (function FishAlias n -> Some n | _ -> None) flags in
698 (* The basic command. *)
699 pr "%s, &%s_cmd_entry\n" name name;
701 (* Command with dashes instead of underscores. *)
702 if name <> name2 then
703 pr "%s, &%s_cmd_entry\n" name2 name;
705 (* Aliases for the command. *)
708 pr "%s, &%s_cmd_entry\n" alias name;
710 ) all_functions_and_fish_commands_sorted
712 (* Readline completion for guestfish. *)
713 and generate_fish_completion () =
714 generate_header CStyle GPLv2plus;
718 fun (_, _, _, flags, _, _, _) -> not (List.mem NotInFish flags)
728 #ifdef HAVE_LIBREADLINE
729 #include <readline/readline.h>
734 #ifdef HAVE_LIBREADLINE
736 static const char *const commands[] = {
737 BUILTIN_COMMANDS_FOR_COMPLETION,
740 (* Get the commands, including the aliases. They don't need to be
741 * sorted - the generator() function just does a dumb linear search.
745 fun (name, _, _, flags, _, _, _) ->
746 let name2 = replace_char name '_' '-' in
748 filter_map (function FishAlias n -> Some n | _ -> None) flags in
750 ) (all_functions @ fish_commands) in
751 let commands = List.flatten commands in
753 List.iter (pr " \"%s\",\n") commands;
759 generator (const char *text, int state)
761 static size_t index, len;
769 rl_attempted_completion_over = 1;
771 while ((name = commands[index]) != NULL) {
773 if (STRCASEEQLEN (name, text, len))
774 return strdup (name);
780 #endif /* HAVE_LIBREADLINE */
782 #ifdef HAVE_RL_COMPLETION_MATCHES
783 #define RL_COMPLETION_MATCHES rl_completion_matches
785 #ifdef HAVE_COMPLETION_MATCHES
786 #define RL_COMPLETION_MATCHES completion_matches
788 #endif /* else just fail if we don't have either symbol */
791 do_completion (const char *text, int start, int end)
793 char **matches = NULL;
795 #ifdef HAVE_LIBREADLINE
796 rl_completion_append_character = ' ';
799 matches = RL_COMPLETION_MATCHES (text, generator);
800 else if (complete_dest_paths)
801 matches = RL_COMPLETION_MATCHES (text, complete_dest_paths_generator);
808 (* Generate the POD documentation for guestfish. *)
809 and generate_fish_actions_pod () =
810 let all_functions_sorted =
812 fun (_, _, _, flags, _, _, _) ->
813 not (List.mem NotInFish flags || List.mem NotInDocs flags)
814 ) all_functions_sorted in
816 let rex = Str.regexp "C<guestfs_\\([^>]+\\)>" in
819 fun (name, (_, args, optargs), _, flags, _, _, longdesc) ->
821 Str.global_substitute rex (
824 try Str.matched_group 1 s
826 failwithf "error substituting C<guestfs_...> in longdesc of function %s" name in
827 "L</" ^ replace_char sub '_' '-' ^ ">"
829 let name = replace_char name '_' '-' in
831 filter_map (function FishAlias n -> Some n | _ -> None) flags in
835 pr "=head2 %s\n\n" name
840 | Pathname n | Device n | Dev_or_Path n | String n ->
842 | OptString n -> pr " %s" n
843 | StringList n | DeviceList n -> pr " '%s ...'" n
844 | Bool _ -> pr " true|false"
845 | Int n -> pr " %s" n
846 | Int64 n -> pr " %s" n
847 | FileIn n | FileOut n -> pr " (%s|-)" n
848 | BufferIn n -> pr " %s" n
849 | Key _ -> () (* keys are entered at a prompt *)
850 | Pointer _ -> assert false
854 | (Bool n | Int n | Int64 n | String n) as arg ->
855 pr " [%s:%s]" n (doc_opttype_of arg)
860 pr "%s\n\n" longdesc;
862 if List.exists (function FileIn _ | FileOut _ -> true
863 | _ -> false) args then
864 pr "Use C<-> instead of a filename to read/write from stdin/stdout.\n\n";
866 if List.exists (function Key _ -> true | _ -> false) args then
867 pr "This command has one or more key or passphrase parameters.
868 Guestfish will prompt for these separately.\n\n";
870 if optargs <> [] then
871 pr "This command has one or more optional arguments. See L</OPTIONAL ARGUMENTS>.\n\n";
873 if List.mem ProtocolLimitWarning flags then
874 pr "%s\n\n" protocol_limit_warning;
876 match deprecation_notice flags with
878 | Some txt -> pr "%s\n\n" txt
879 ) all_functions_sorted
881 (* Generate documentation for guestfish-only commands. *)
882 and generate_fish_commands_pod () =
884 fun (name, _, _, flags, _, _, longdesc) ->
885 let name = replace_char name '_' '-' in
887 filter_map (function FishAlias n -> Some n | _ -> None) flags in
891 pr "=head2 %s\n\n" name
893 pr "%s\n\n" longdesc;
896 and generate_fish_prep_options_h () =
897 generate_header CStyle GPLv2plus;
899 pr "#ifndef PREPOPTS_H\n";
904 const char *name; /* eg. \"fs\" */
906 size_t nr_params; /* optional parameters */
907 struct prep_param *params;
909 const char *shortdesc; /* short description */
910 const char *longdesc; /* long description */
912 /* functions to implement it */
913 void (*prelaunch) (const char *filename, prep_data *);
914 void (*postlaunch) (const char *filename, prep_data *, const char *device);
918 const char *pname; /* parameter name */
919 const char *pdefault; /* parameter default */
920 const char *pdesc; /* parameter description */
923 extern const struct prep preps[];
926 " (List.length prepopts);
929 fun (name, shortdesc, args, longdesc) ->
931 extern void prep_prelaunch_%s (const char *filename, prep_data *data);
932 extern void prep_postlaunch_%s (const char *filename, prep_data *data, const char *device);
938 pr "#endif /* PREPOPTS_H */\n"
940 and generate_fish_prep_options_c () =
941 generate_header CStyle GPLv2plus;
947 #include \"prepopts.h\"
952 fun (name, shortdesc, args, longdesc) ->
953 pr "static struct prep_param %s_args[] = {\n" name;
955 fun (n, default, desc) ->
956 pr " { \"%s\", \"%s\", \"%s\" },\n" n default desc
962 pr "const struct prep preps[] = {\n";
964 fun (name, shortdesc, args, longdesc) ->
965 pr " { \"%s\", %d, %s_args,
968 prep_prelaunch_%s, prep_postlaunch_%s },
970 name (List.length args) name
971 (c_quote shortdesc) (c_quote longdesc)
976 and generate_fish_event_names () =
977 generate_header CStyle GPLv2plus;
987 event_name_of_event_bitmask (uint64_t ev)
994 pr " case GUESTFS_EVENT_%s:\n" (String.uppercase name);
995 pr " return \"%s\";\n" name
999 abort (); /* should not happen */
1004 print_event_set (uint64_t event_bitmask, FILE *fp)
1008 if (event_bitmask == GUESTFS_EVENT_ALL) {
1017 pr " if (event_bitmask & GUESTFS_EVENT_%s) {\n" (String.uppercase name);
1018 pr " if (comma) fputc (',', fp);\n";
1020 pr " fputs (\"%s\", fp);\n" name;
1028 event_bitmask_of_event_set (const char *arg, uint64_t *eventset_r)
1032 if (STREQ (arg, \"*\")) {
1033 *eventset_r = GUESTFS_EVENT_ALL;
1040 n = strcspn (arg, \",\");
1046 pr "if (STREQLEN (arg, \"%s\", n))\n" name;
1047 pr " *eventset_r |= GUESTFS_EVENT_%s;\n" (String.uppercase name);
1053 fprintf (stderr, _(\"unknown event name: %%s\\n\"), arg);