+(* When tools register themselves, they are added to this list.
+ * Later, we will alphabetize the list.
+ *)
+let tools = ref []
+
+(* Registration function used by the tools. *)
+let register ?(external_cmd = true) ?(extra_args = [])
+ ?argcheck ?beforeksyms ?run
+ name summary description =
+ tools :=
+ (name, (name, summary, description, external_cmd, extra_args,
+ argcheck, beforeksyms, run))
+ :: !tools
+
+(* Main program, called from mem/virt_mem_main.ml when all the
+ * tools have had a chance to register themselves.
+ *)
+let main () =
+ (* Get the registered tools, alphabetically. *)
+ let tools = !tools in
+ let tools = List.sort ~cmp:(fun (a,_) (b,_) -> compare a b) tools in
+
+ (* Which tool did the user want to run? Look at the executable
+ * name (eg. 'virt-dmesg' => tool == dmesg). If we don't recognise
+ * the executable name then we must look for the first parameter
+ * which doesn't begin with a '-' character.
+ *
+ * Note that we must do all of this before using the OCaml Arg
+ * module to properly parse the command line (below), so that
+ * we can have a usage message ready.
+ *)
+ let tool, ignore_first_anon_arg =
+ let prog = Sys.executable_name in (* eg. "/usr/bin/virt-dmesg.opt" *)
+ let prog = Filename.basename prog in(* eg. "virt-dmesg.opt" *)
+ let prog = (* eg. "virt-dmesg" *)
+ try Filename.chop_extension prog with Invalid_argument _ -> prog in
+ let prog = (* eg. "dmesg" *)
+ if String.starts_with prog "virt-" then
+ String.sub prog 5 (String.length prog - 5)
+ else prog in
+ try Some (List.assoc prog tools), false
+ with Not_found ->
+ let arg1 = (* First non-option argument. *)
+ match Array.to_list Sys.argv with
+ | [] -> None
+ | _::args ->
+ let rec loop = function
+ | [] -> None
+ | a::args when String.length a > 0 && a.[0] = '-' -> loop args
+ | a::_ -> Some a
+ in
+ loop args in
+ match arg1 with
+ | None -> None, false
+ | Some prog -> (* Recognisable first argument? *)
+ let prog =
+ try Filename.chop_extension prog with Invalid_argument _ -> prog in
+ let prog =
+ if String.starts_with prog "virt-" then
+ String.sub prog 5 (String.length prog - 5)
+ else prog in
+ (try Some (List.assoc prog tools), true
+ with Not_found -> None, false) in
+
+ (* Make a usage message. *)
+ let usage_msg =
+ match tool with
+ | None -> (* Generic usage message. *)
+ let tools = List.map (
+ fun (name, (_, summary, _, external_cmd, _, _, _, _)) ->
+ if external_cmd then "virt-"^name, summary
+ else "virt-mem "^name, summary
+ ) tools in
+ (* Maximum width of field in the left hand column. *)
+ let max_width =
+ List.fold_left max 0 (List.map String.length (List.map fst tools)) in
+ let tools = List.map (fun (l,r) -> pad max_width l, r) tools in
+ let tools = List.map (fun (l,r) -> " " ^ l ^ " - " ^ r) tools in
+ let tools = String.concat "\n" tools in
+
+ sprintf (f_"\
+
+virt-mem: Tools for providing information about virtual machines
+
+Currently available tools include:
+%s
+
+General usage is:
+ <tool> [-options] [domains...]
+
+To display extra help for a single tool, do:
+ virt-mem help <tool>
+
+Options:") tools
+
+ (* Tool-specific usage message. *)
+ | Some (name, summary, description, external_cmd, _, _, _, _) ->
+ let cmd =
+ if external_cmd then "virt-" ^ name else "virt-mem " ^ name in
+
+ sprintf (f_"\
+
+%s: %s
+
+Description:
+%s
+
+Options:") cmd summary description in
+
+ (* Now begin proper parsing of the command line arguments. *)