From: Richard Jones Date: Sat, 17 Apr 2010 11:33:31 +0000 (+0100) Subject: fish: Print output from some commands in octal/hex as approp. (RHBZ#583242). X-Git-Tag: 1.3.4~18 X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=commitdiff_plain;h=4f376dbafb78ee9b5ad1652360472a14de64753d fish: Print output from some commands in octal/hex as approp. (RHBZ#583242). --- diff --git a/fish/guestfish.pod b/fish/guestfish.pod index e5f0bba..dfacf6f 100644 --- a/fish/guestfish.pod +++ b/fish/guestfish.pod @@ -313,7 +313,9 @@ the Unix L program): chmod 0777 /public # OK chmod 777 /public # WRONG! This is mode 777 decimal = 01411 octal. -Commands that return numbers currently always print them in decimal. +Commands that return numbers usually print them in decimal, but +some commands print numbers in other radices (eg. C prints +the mode in octal, preceeded by C<0>). =head1 WILDCARDS AND GLOBBING diff --git a/src/generator.ml b/src/generator.ml index 279272e..87855e7 100755 --- a/src/generator.ml +++ b/src/generator.ml @@ -182,11 +182,16 @@ type flags = | DangerWillRobinson (* flags particularly dangerous commands *) | FishAlias of string (* provide an alias for this cmd in guestfish *) | FishAction of string (* call this function in guestfish *) + | FishOutput of fish_output_t (* how to display output in guestfish *) | NotInFish (* do not export via guestfish *) | NotInDocs (* do not add this function to documentation *) | DeprecatedBy of string (* function is deprecated, use .. instead *) | Optional of string (* function is part of an optional group *) +and fish_output_t = + | FishOutputOctal (* for int return, print in octal *) + | FishOutputHexadecimal (* for int return, print in hex *) + (* You can supply zero or as many tests as you want per API call. * * Note that the test environment has 3 block devices, of size 500MB, @@ -2250,7 +2255,7 @@ to return the existing UUID of a filesystem."); This returns the ext2/3/4 filesystem UUID of the filesystem on C."); - ("fsck", (RInt "status", [String "fstype"; Device "device"]), 84, [], + ("fsck", (RInt "status", [String "fstype"; Device "device"]), 84, [FishOutput FishOutputHexadecimal], [InitBasicFS, Always, TestOutputInt ( [["umount"; "/dev/sda1"]; ["fsck"; "ext2"; "/dev/sda1"]], 0); @@ -3012,7 +3017,7 @@ This call creates a char device node called C with mode C and device major/minor C and C. It is just a convenient wrapper around C."); - ("umask", (RInt "oldmask", [Int "mask"]), 137, [], + ("umask", (RInt "oldmask", [Int "mask"]), 137, [FishOutput FishOutputOctal], [], (* XXX umask is one of those stateful things that we should * reset between each test. *) @@ -4419,7 +4424,7 @@ C has the bootable flag set. See also C."); - ("part_get_mbr_id", (RInt "idbyte", [Device "device"; Int "partnum"]), 235, [], + ("part_get_mbr_id", (RInt "idbyte", [Device "device"; Int "partnum"]), 235, [FishOutput FishOutputHexadecimal], [InitEmpty, Always, TestOutputInt ( [["part_init"; "/dev/sda"; "mbr"]; ["part_add"; "/dev/sda"; "primary"; "1"; "-1"]; @@ -7446,16 +7451,39 @@ and generate_fish_cmds () = pr " free_strings (%s);\n" name ) (snd style); + (* Any output flags? *) + let fish_output = + let flags = filter_map ( + function FishOutput flag -> Some flag | _ -> None + ) flags in + match flags with + | [] -> None + | [f] -> Some f + | _ -> + failwithf "%s: more than one FishOutput flag is not allowed" name in + (* Check return value for errors and display command results. *) (match fst style with | RErr -> pr " return r;\n" | RInt _ -> pr " if (r == -1) return -1;\n"; - pr " printf (\"%%d\\n\", r);\n"; + (match fish_output with + | None -> + pr " printf (\"%%d\\n\", r);\n"; + | Some FishOutputOctal -> + pr " printf (\"%%s%%o\\n\", r != 0 ? \"0\" : \"\", r);\n"; + | Some FishOutputHexadecimal -> + pr " printf (\"%%s%%x\\n\", r != 0 ? \"0x\" : \"\", r);\n"); pr " return 0;\n" | RInt64 _ -> pr " if (r == -1) return -1;\n"; - pr " printf (\"%%\" PRIi64 \"\\n\", r);\n"; + (match fish_output with + | None -> + pr " printf (\"%%\" PRIi64 \"\\n\", r);\n"; + | Some FishOutputOctal -> + pr " printf (\"%%s%%\" PRIo64 \"\\n\", r != 0 ? \"0\" : \"\", r);\n"; + | Some FishOutputHexadecimal -> + pr " printf (\"%%s%%\" PRIx64 \"\\n\", r != 0 ? \"0x\" : \"\", r);\n"); pr " return 0;\n" | RBool _ -> pr " if (r == -1) return -1;\n";