Files
libguestfs/generator/generator_csharp.ml
Richard W.M. Jones 14490c3e1a generator: Optional arguments, add-drive-opts (RHBZ#642934,CVE-2010-3851).
This large commit changes the generator so that optional arguments
can be supported for functions.

The model for arguments (known as the "style") is changed from
(ret, args) to (ret, args, optargs) where optargs is a more limited
list of arguments.

One function has been added which takes optional arguments, it is
"add-drive-opts", modelled as:

  (RErr, [String "filename"], #required
         [Bool "readonly"; String "format"; String "iface"]) #optional

Note that this function is processed in the library (does not go over
the RPC protocol to the daemon).  This has allowed us to simplify
the current implementation by omitting changes related to RPC or the
daemon, although we plan to add these at some point in the future.

From C this function can be called in 3 different ways as in these
examples:

  guestfs_add_drive_opts (g, filename,
                          GUESTFS_ADD_DRIVE_OPTS_READONLY, 1,
			  GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
                          -1);

(the argument(s) between 'filename' and '-1' are the optional ones).

  guestfs_add_drive_opts_va (g, filename, args);

where 'args' is a va_list.  This works like the first version.

  struct guestfs_add_drive_opts_argv optargs = {
    .bitmask = GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK,
    .readonly = 1,
  }
  guestfs_add_drive_opts_argv (g, filename, &optargs);

This last form lets you construct lists of optional arguments, and
is used by guestfish and the language bindings.

In guestfish optional arguments are used like this:

  add-drive-opts filename readonly:true

In OCaml these are mapped naturally to OCaml optional arguments, eg:

  g#add_drive_opts ~readonly:true filename;

In Perl these are mapped to extra arguments, eg:

  $g->add_drive_opts ($filename, readonly => 1);

In Python these are mapped to optional arguments, eg:

  g.add_drive_opts ("file", readonly = 1, format = "qcow2")

In Ruby these are mapped to a final hash argument, eg:

  g.add_drive_opts("file", {})
  g.add_drive_opts("file", :readonly => 1)
  g.add_drive_opts("file", :readonly => 1, :iface => "virtio")

In PHP these are mapped to extra parameters.  This is not quite
accurate since you cannot omit arbitrary optional parameters, but
there's not much than can be done within the limitations of PHP
as a language.

Unimplemented in: Haskell, C#, Java.
2010-10-22 17:45:00 +01:00

270 lines
7.9 KiB
OCaml

(* libguestfs
* Copyright (C) 2009-2010 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 Generator_types
open Generator_utils
open Generator_pr
open Generator_docstrings
open Generator_optgroups
open Generator_actions
open Generator_structs
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 (typ, 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"
) structs;
(* Generate C# function bindings. *)
List.iter (
fun (name, (ret, args, optargs), _, _, _, shortdesc, _) ->
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 n -> "Hashtable"
| RStringList n -> "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 guestfs_%s (IntPtr h"
(c_return_type ()) name;
List.iter (
function
| Pathname n | Device n | Dev_or_Path n | String n | OptString n
| FileIn n | FileOut n
| Key n
| BufferIn n ->
pr ", [In] string %s" n
| StringList n | DeviceList n ->
pr ", [In] string[] %s" n
| Bool n ->
pr ", bool %s" n
| Int n ->
pr ", int %s" n
| Int64 n ->
pr ", long %s" n
) args;
pr ");\n"
and generate_public_prototype () =
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
| Pathname n | Device n | Dev_or_Path n | String n | OptString n
| FileIn n | FileOut n
| Key n
| BufferIn n ->
next (); pr "string %s" n
| StringList n | DeviceList n ->
next (); pr "string[] %s" n
| Bool n ->
next (); pr "bool %s" n
| Int n ->
next (); pr "int %s" n
| Int64 n ->
next (); pr "long %s" n
) args;
pr ")\n"
and generate_call () =
pr "guestfs_%s (_handle" name;
List.iter (fun arg -> pr ", %s" (name_of_argt arg)) args;
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 ();
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";
) all_functions_sorted;
pr " }
}
"