1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
(* libguestfs
* Copyright (C) 2009-2023 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/csharp.ml"]
let rec generate_csharp () =
generate_header CPlusPlusStyle LGPLv2plus;
(* XXX Make this configurable by the C# assembly users. *)
let library = "libguestfs.so.0" in
pr "\
// These C# bindings are highly experimental at present.
//
// Firstly they only work on Linux (ie. Mono). In order to get them
// to work on Windows (ie. .Net) you would need to port the library
// itself to Windows first.
//
// The second issue is that some calls are known to be incorrect and
// can cause Mono to segfault. Particularly: calls which pass or
// return string[], or return any structure value. This is because
// we haven't worked out the correct way to do this from C#. Also
// we don't handle functions that take optional arguments at all.
//
// The third issue is that when compiling you get a lot of warnings.
// We are not sure whether the warnings are important or not.
//
// Fourthly we do not routinely build or test these bindings as part
// of the make && make check cycle, which means that regressions might
// go unnoticed.
//
// Suggestions and patches are welcome.
// To compile:
//
// gmcs Libguestfs.cs
// mono Libguestfs.exe
//
// (You'll probably want to add a Test class / static main function
// otherwise this won't do anything useful).
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Collections;
namespace Guestfs
{
class Error : System.ApplicationException
{
public Error (string message) : base (message) {}
protected Error (SerializationInfo info, StreamingContext context) {}
}
class Guestfs
{
IntPtr _handle;
[DllImport (\"%s\")]
static extern IntPtr guestfs_create ();
public Guestfs ()
{
_handle = guestfs_create ();
if (_handle == IntPtr.Zero)
throw new Error (\"could not create guestfs handle\");
}
[DllImport (\"%s\")]
static extern void guestfs_close (IntPtr h);
~Guestfs ()
{
guestfs_close (_handle);
}
[DllImport (\"%s\")]
static extern string guestfs_last_error (IntPtr h);
" library library library;
(* Generate C# structure bindings. We prefix struct names with
* underscore because C# cannot have conflicting struct names and
* method names (eg. "class stat" and "stat").
*)
List.iter (
fun { s_name = typ; s_cols = cols } ->
pr " [StructLayout (LayoutKind.Sequential)]\n";
pr " public class _%s {\n" typ;
List.iter (
function
| name, FChar -> pr " char %s;\n" name
| name, FString -> pr " string %s;\n" name
| name, FBuffer ->
pr " uint %s_len;\n" name;
pr " string %s;\n" name
| name, FUUID ->
pr " [MarshalAs (UnmanagedType.ByValTStr, SizeConst=16)]\n";
pr " string %s;\n" name
| name, FUInt32 -> pr " uint %s;\n" name
| name, FInt32 -> pr " int %s;\n" name
| name, (FUInt64|FBytes) -> pr " ulong %s;\n" name
| name, FInt64 -> pr " long %s;\n" name
| name, FOptPercent -> pr " float %s; /* [0..100] or -1 */\n" name
) cols;
pr " }\n";
pr "\n"
) external_structs;
(* Generate C# function bindings. *)
List.iter (
fun { name; style = ret, args, optargs; c_function;
shortdesc; non_c_aliases } ->
let rec csharp_return_type () =
match ret with
| RErr -> "void"
| RBool n -> "bool"
| RInt n -> "int"
| RInt64 n -> "long"
| RConstString n
| RConstOptString n
| RString (_, n)
| RBufferOut n -> "string"
| RStruct (_,n) -> "_" ^ n
| RHashtable _ -> "Hashtable"
| RStringList _ -> "string[]"
| RStructList (_,n) -> sprintf "_%s[]" n
and c_return_type () =
match ret with
| RErr
| RBool _
| RInt _ -> "int"
| RInt64 _ -> "long"
| RConstString _
| RConstOptString _
| RString _
| RBufferOut _ -> "string"
| RStruct (_,n) -> "_" ^ n
| RHashtable _
| RStringList _ -> "string[]"
| RStructList (_,n) -> sprintf "_%s[]" n
and c_error_comparison () =
match ret with
| RErr
| RBool _
| RInt _
| RInt64 _ -> "== -1"
| RConstString _
| RConstOptString _
| RString _
| RBufferOut _
| RStruct (_,_)
| RHashtable _
| RStringList _
| RStructList (_,_) -> "== null"
and generate_extern_prototype () =
pr " static extern %s %s (IntPtr h"
(c_return_type ()) c_function;
List.iter (
function
| String (_, n)
| OptString n
| BufferIn n ->
pr ", [In] string %s" n
| StringList (_, n) ->
pr ", [In] string[] %s" n
| Bool n ->
pr ", bool %s" n
| Int n ->
pr ", int %s" n
| Int64 n | Pointer (_, n) ->
pr ", long %s" n
) args;
if optargs <> [] then pr ", void *";
pr ");\n"
and generate_public_prototype name =
pr " public %s %s (" (csharp_return_type ()) name;
let comma = ref false in
let next () =
if !comma then pr ", ";
comma := true
in
List.iter (
function
| String (_, n)
| OptString n
| BufferIn n ->
next (); pr "string %s" n
| StringList (_, n) ->
next (); pr "string[] %s" n
| Bool n ->
next (); pr "bool %s" n
| Int n ->
next (); pr "int %s" n
| Int64 n | Pointer (_, n) ->
next (); pr "long %s" n
) args;
pr ")\n"
and generate_call () =
pr "%s (_handle" c_function;
List.iter (fun arg -> pr ", %s" (name_of_argt arg)) args;
(* C# bindings don't deal with optargs at all, so just pass
* NULL for the structure here.
*)
if optargs <> [] then pr ", NULL";
pr ");\n";
and generate_alias alias =
generate_public_prototype alias;
pr " {\n";
(match ret with
| RErr -> pr " ";
| _ -> pr " return "
);
pr "%s (%s);\n" name (String.concat ", " (List.map name_of_argt args));
pr " }\n";
pr "\n";
in
pr " [DllImport (\"%s\")]\n" library;
generate_extern_prototype ();
pr "\n";
pr " /// <summary>\n";
pr " /// %s\n" shortdesc;
pr " /// </summary>\n";
generate_public_prototype name;
pr " {\n";
pr " %s r;\n" (c_return_type ());
pr " r = ";
generate_call ();
pr " if (r %s)\n" (c_error_comparison ());
pr " throw new Error (guestfs_last_error (_handle));\n";
(match ret with
| RErr -> ()
| RBool _ ->
pr " return r != 0 ? true : false;\n"
| RHashtable _ ->
pr " Hashtable rr = new Hashtable ();\n";
pr " for (size_t i = 0; i < r.Length; i += 2)\n";
pr " rr.Add (r[i], r[i+1]);\n";
pr " return rr;\n"
| RInt _ | RInt64 _ | RConstString _ | RConstOptString _
| RString _ | RBufferOut _ | RStruct _ | RStringList _
| RStructList _ ->
pr " return r;\n"
);
pr " }\n";
pr "\n";
List.iter generate_alias non_c_aliases
) (actions |> external_functions |> sort);
pr " }
}
"
|