mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-22 07:03:38 +00:00
This change was done almost entirely automatically using the script
below. This uses the OCaml lexer to read the source files and extract
the strings and locations. Strings which are "candidates" (in this
case, longer than 3 lines) are replaced in the output with quoted
string literals.
Since the OCaml lexer is used, it already substitutes all escape
sequences correctly. I diffed the output of the generator and it is
identical after this change, except for UUIDs, which change because of
how Utils.stable_uuid is implemented.
Thanks: Nicolas Ojeda Bar
$ ocamlfind opt -package unix,compiler-libs.common find_strings.ml \
-o find_strings.opt -linkpkg
$ for f in $( git ls-files -- \*.ml ) ; do ./find_strings.opt $f ; done
open Printf
let read_whole_file path =
let buf = Buffer.create 16384 in
let chan = open_in path in
let maxlen = 16384 in
let b = Bytes.create maxlen in
let rec loop () =
let r = input chan b 0 maxlen in
if r > 0 then (
Buffer.add_substring buf (Bytes.to_string b) 0 r;
loop ()
)
in
loop ();
close_in chan;
Buffer.contents buf
let count_chars c str =
let count = ref 0 in
for i = 0 to String.length str - 1 do
if c = String.unsafe_get str i then incr count
done;
!count
let subs = ref []
let consider_string str loc =
let nr_lines = count_chars '\n' str in
if nr_lines > 3 then
subs := (str, loc) :: !subs
let () =
Lexer.init ();
let filename = Sys.argv.(1) in
let content = read_whole_file filename in
let lexbuf = Lexing.from_string content in
let rec loop () =
let token = Lexer.token lexbuf in
(match token with
| Parser.EOF -> ();
| STRING (s, loc, sopt) ->
consider_string s loc; (* sopt? *)
loop ();
| token ->
loop ();
)
in
loop ();
(* The list of subs is already reversed, which is convenient
* because we must the file substitutions in reverse order.
*)
let subs = !subs in
let new_content = ref content in
List.iter (
fun (str, loc) ->
let { Location.loc_start = { pos_cnum = p1 };
loc_end = { pos_cnum = p2 } } = loc in
let len = String.length !new_content in
let before = String.sub !new_content 0 (p1-1) in
let after = String.sub !new_content (p2+1) (len - p2 - 1) in
new_content := before ^ "{|" ^ str ^ "|}" ^ after
) subs;
let new_content = !new_content in
if content <> new_content then (
(* Update the file in place. *)
let new_filename = filename ^ ".new"
and backup_filename = filename ^ ".bak" in
let chan = open_out new_filename in
fprintf chan "%s" new_content;
close_out chan;
Unix.rename filename backup_filename;
Unix.rename new_filename filename
)
290 lines
8.7 KiB
OCaml
290 lines
8.7 KiB
OCaml
(* libguestfs
|
|
* Copyright (C) 2009-2025 Red Hat Inc.
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*)
|
|
|
|
(* Please read generator/README first. *)
|
|
|
|
open Printf
|
|
|
|
open Std_utils
|
|
open Types
|
|
open Utils
|
|
open Pr
|
|
open Docstrings
|
|
open Optgroups
|
|
open Actions
|
|
open Structs
|
|
|
|
let generate_header = generate_header ~inputs:["generator/haskell.ml"]
|
|
|
|
let rec generate_haskell_hs () =
|
|
generate_header HaskellStyle LGPLv2plus;
|
|
|
|
(* See guestfs(3)/Haskell for limitations of the current Haskell
|
|
* bindings. Please help out! XXX
|
|
*)
|
|
let can_generate = function
|
|
| _, _, (_::_) -> false (* no optional args yet *)
|
|
| RErr, _, []
|
|
| RInt _, _, []
|
|
| RInt64 _, _, []
|
|
| RBool _, _, []
|
|
| RConstString _, _, []
|
|
| RString _, _, []
|
|
| RStringList _, _, []
|
|
| RHashtable _, _, [] -> true
|
|
| RStruct _, _, []
|
|
| RStructList _, _, []
|
|
| RBufferOut _, _, []
|
|
| RConstOptString _, _, [] -> false
|
|
in
|
|
|
|
pr "\
|
|
{-# LANGUAGE ForeignFunctionInterface #-}
|
|
|
|
module Guestfs (
|
|
create";
|
|
|
|
(* List out the names of the actions we want to export. *)
|
|
List.iter (
|
|
fun { name; style } ->
|
|
if can_generate style then pr ",\n %s" name
|
|
) (actions |> external_functions |> sort);
|
|
|
|
pr {|
|
|
) where
|
|
|
|
-- Unfortunately some symbols duplicate ones already present
|
|
-- in Prelude. We don't know which, so we hard-code a list
|
|
-- here.
|
|
import Prelude hiding (head, tail, truncate)
|
|
|
|
import Foreign
|
|
import Foreign.C
|
|
import Foreign.C.Types
|
|
import System.IO
|
|
import Control.Exception
|
|
import Data.Typeable
|
|
|
|
data GuestfsS = GuestfsS -- represents the opaque C struct
|
|
type GuestfsP = Ptr GuestfsS -- guestfs_h *
|
|
type GuestfsH = ForeignPtr GuestfsS -- guestfs_h * with attached finalizer
|
|
|
|
foreign import ccall unsafe "guestfs.h guestfs_create" c_create
|
|
:: IO GuestfsP
|
|
foreign import ccall unsafe "guestfs.h &guestfs_close" c_close
|
|
:: FunPtr (GuestfsP -> IO ())
|
|
foreign import ccall unsafe "guestfs.h guestfs_set_error_handler" c_set_error_handler
|
|
:: GuestfsP -> Ptr CInt -> Ptr CInt -> IO ()
|
|
|
|
create :: IO GuestfsH
|
|
create = do
|
|
p <- c_create
|
|
c_set_error_handler p nullPtr nullPtr
|
|
h <- newForeignPtr c_close p
|
|
return h
|
|
|
|
foreign import ccall unsafe "guestfs.h guestfs_last_error" c_last_error
|
|
:: GuestfsP -> IO CString
|
|
|
|
-- last_error :: GuestfsH -> IO (Maybe String)
|
|
-- last_error h = do
|
|
-- str <- withForeignPtr h (\p -> c_last_error p)
|
|
-- maybePeek peekCString str
|
|
|
|
last_error :: GuestfsH -> IO String
|
|
last_error h = do
|
|
str <- withForeignPtr h (\p -> c_last_error p)
|
|
if (str == nullPtr)
|
|
then return "no error"
|
|
else peekCString str
|
|
|
|
assocListOfHashtable :: Eq a => [a] -> [(a,a)]
|
|
assocListOfHashtable [] = []
|
|
assocListOfHashtable [a] =
|
|
fail "RHashtable returned an odd number of elements"
|
|
assocListOfHashtable (a:b:rest) = (a,b) : assocListOfHashtable rest
|
|
|
|
|};
|
|
|
|
(* Generate wrappers for each foreign function. *)
|
|
List.iter (
|
|
fun { name; style = (ret, args, optargs as style);
|
|
c_function = c_function } ->
|
|
if can_generate style then (
|
|
pr "foreign import ccall unsafe \"guestfs.h %s\" c_%s\n"
|
|
c_function name;
|
|
pr " :: ";
|
|
generate_haskell_prototype ~handle:"GuestfsP" style;
|
|
pr "\n";
|
|
pr "\n";
|
|
pr "%s :: " name;
|
|
generate_haskell_prototype ~handle:"GuestfsH" ~hs:true style;
|
|
pr "\n";
|
|
pr "%s %s = do\n" name
|
|
(String.concat " " ("h" :: List.map name_of_argt args));
|
|
pr " r <- ";
|
|
(* Convert pointer arguments using with* functions. *)
|
|
List.iter (
|
|
function
|
|
| String (_, n) ->
|
|
pr "withCString %s $ \\%s -> " n n
|
|
| BufferIn n ->
|
|
pr "withCStringLen %s $ \\(%s, %s_size) -> " n n n
|
|
| OptString n -> pr "maybeWith withCString %s $ \\%s -> " n n
|
|
| StringList (_, n) ->
|
|
pr "withMany withCString %s $ \\%s -> withArray0 nullPtr %s $ \\%s -> " n n n n
|
|
| Bool _ | Int _ | Int64 _ | Pointer _ -> ()
|
|
) args;
|
|
(* Convert integer arguments. *)
|
|
let args =
|
|
List.map (
|
|
function
|
|
| Bool n -> sprintf "(fromBool %s)" n
|
|
| Int n -> sprintf "(fromIntegral %s)" n
|
|
| Int64 n | Pointer (_, n) -> sprintf "(fromIntegral %s)" n
|
|
| String (_, n)
|
|
| OptString n
|
|
| StringList (_, n) -> n
|
|
| BufferIn n -> sprintf "%s (fromIntegral %s_size)" n n
|
|
) args in
|
|
pr "withForeignPtr h (\\p -> c_%s %s)\n" name
|
|
(String.concat " " ("p" :: args));
|
|
(match ret with
|
|
| RErr | RInt _ | RInt64 _ | RBool _ ->
|
|
pr " if (r == -1)\n";
|
|
pr " then do\n";
|
|
pr " err <- last_error h\n";
|
|
pr " fail err\n";
|
|
| RConstString _ | RConstOptString _ | RString _
|
|
| RStringList _ | RStruct _
|
|
| RStructList _ | RHashtable _ | RBufferOut _ ->
|
|
pr " if (r == nullPtr)\n";
|
|
pr " then do\n";
|
|
pr " err <- last_error h\n";
|
|
pr " fail err\n";
|
|
);
|
|
(match ret with
|
|
| RErr ->
|
|
pr " else return ()\n"
|
|
| RInt _ ->
|
|
pr " else return (fromIntegral r)\n"
|
|
| RInt64 _ ->
|
|
pr " else return (fromIntegral r)\n"
|
|
| RBool _ ->
|
|
pr " else return (toBool r)\n"
|
|
| RConstString _
|
|
| RString _ ->
|
|
pr " else peekCString r\n"
|
|
| RStringList _ ->
|
|
pr " else peekArray0 nullPtr r >>= mapM peekCString\n"
|
|
| RHashtable _ ->
|
|
pr " else do\n";
|
|
pr " arr <- peekArray0 nullPtr r\n";
|
|
pr " arr <- mapM peekCString arr\n";
|
|
pr " return (assocListOfHashtable arr)\n"
|
|
| RStruct _
|
|
| RStructList _
|
|
| RBufferOut _
|
|
| RConstOptString _ ->
|
|
pr " else return ()\n" (* XXXXXXXXXXXXXXXXXXXX *)
|
|
);
|
|
pr "\n";
|
|
)
|
|
) (actions |> external_functions |> sort)
|
|
|
|
and generate_haskell_prototype ~handle ?(hs = false) (ret, args, optargs) =
|
|
pr "%s -> " handle;
|
|
if not hs then (
|
|
List.iter (
|
|
fun arg ->
|
|
(match arg with
|
|
| String _ ->
|
|
pr "CString"
|
|
| BufferIn _ ->
|
|
pr "CString -> CInt"
|
|
| OptString _ ->
|
|
pr "CString"
|
|
| StringList _ ->
|
|
pr "Ptr CString"
|
|
| Bool _ -> pr "CInt"
|
|
| Int _ -> pr "CInt"
|
|
| Int64 _ -> pr "Int64"
|
|
| Pointer _ -> pr "CInt"
|
|
);
|
|
pr " -> ";
|
|
) args;
|
|
pr "IO ";
|
|
(match ret with
|
|
| RErr -> pr "CInt"
|
|
| RInt _ -> pr "CInt"
|
|
| RInt64 _ -> pr "Int64"
|
|
| RBool _ -> pr "CInt"
|
|
| RConstString _ -> pr "CString"
|
|
| RConstOptString _ -> pr "(Maybe CString)"
|
|
| RString _ -> pr "CString"
|
|
| RStringList _ -> pr "(Ptr CString)"
|
|
| RStruct (_, typ) ->
|
|
let name = camel_name_of_struct typ in
|
|
pr "%s" name
|
|
| RStructList (_, typ) ->
|
|
let name = camel_name_of_struct typ in
|
|
pr "[%s]" name
|
|
| RHashtable _ -> pr "(Ptr CString)"
|
|
| RBufferOut _ -> pr "CString"
|
|
)
|
|
)
|
|
else (* hs *) (
|
|
List.iter (
|
|
fun arg ->
|
|
(match arg with
|
|
| String _ ->
|
|
pr "String"
|
|
| BufferIn _ ->
|
|
pr "String"
|
|
| OptString _ ->
|
|
pr "Maybe String"
|
|
| StringList _ ->
|
|
pr "[String]"
|
|
| Bool _ -> pr "Bool"
|
|
| Int _ -> pr "Int"
|
|
| Int64 _ -> pr "Integer"
|
|
| Pointer _ -> pr "Int"
|
|
);
|
|
pr " -> ";
|
|
) args;
|
|
pr "IO ";
|
|
(match ret with
|
|
| RErr -> pr "()"
|
|
| RInt _ -> pr "Int"
|
|
| RInt64 _ -> pr "Int64"
|
|
| RBool _ -> pr "Bool"
|
|
| RConstString _ -> pr "String"
|
|
| RConstOptString _ -> pr "(Maybe String)"
|
|
| RString _ -> pr "String"
|
|
| RStringList _ -> pr "[String]"
|
|
| RStruct (_, typ) ->
|
|
let name = camel_name_of_struct typ in
|
|
pr "%s" name
|
|
| RStructList (_, typ) ->
|
|
let name = camel_name_of_struct typ in
|
|
pr "[%s]" name
|
|
| RHashtable _ -> pr "[(String, String)]"
|
|
| RBufferOut _ -> pr "String"
|
|
)
|
|
)
|