Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jonathan Roewen <jonathan.roewen@gmail.com>
To: caml-list@yquem.inria.fr
Subject: [Caml-list] HELP! In-place modification of caml globals...
Date: Thu, 22 Dec 2005 17:03:48 +1300	[thread overview]
Message-ID: <ad8cfe7e0512212003v29fd0a97q47c813df78c9fea0@mail.gmail.com> (raw)

Hi,

I need some help, badly ;-) This is the one thing getting in my way at
present, and I don't know how to fix it.

I have two types:

type in_channel = {
	mutable in_read : unit -> char;
	mutable in_input : string -> int -> int -> int;
	mutable in_seek : int -> unit;
	mutable in_tell : unit -> int;
	mutable in_close : unit -> unit;
	mutable in_length : unit -> int;
}

type 'a out_channel = {
	mutable out_write : char -> unit;
	mutable out_output : string -> int -> int -> unit;
	mutable out_seek : int -> unit;
	mutable out_tell : unit -> int;
	mutable out_close : unit -> 'a;
	mutable out_flush : unit -> unit;
	mutable out_length : unit -> int;
}

and three globals:

let stdin =
	let read = (fun _ -> raise Input_closed)
	and input = (fun _ _ _ -> raise Input_closed)
	and seek = (fun _ -> raise Input_closed)
	and tell = (fun _ -> raise Input_closed)
	in create_in read input seek tell default_close default_length

let stdout =
	let write = (fun _ -> raise Output_closed)
	and output = (fun _ _ _ -> raise Output_closed)
	and seek = (fun _ -> raise Output_closed)
	and tell = (fun _ -> raise Output_closed)
	in create_out write output seek tell default_close default_flush default_length

let stderr =
	let write = (fun _ -> raise Output_closed)
	and output = (fun _ _ _ -> raise Output_closed)
	and seek = (fun _ -> raise Output_closed)
	and tell = (fun _ -> raise Output_closed)
	in create_out write output seek tell default_close default_flush default_length

By default, they're all closed. What I want to do is swap them with
other instances at kernel startup.

So I defined two external functions:

external swap_input : in_channel -> in_channel -> unit = "caml_ml_swap_input";;
external swap_output : unit out_channel -> unit out_channel -> unit =
"caml_ml_swap_output";;

Which are both defined as:

value caml_ml_swap_input(value to, value from) {
	/* record has 6 fields */
	Store_field(to, 0, Field(from, 0));
	Store_field(to, 1, Field(from, 1));
	Store_field(to, 2, Field(from, 2));
	Store_field(to, 3, Field(from, 3));
	Store_field(to, 4, Field(from, 4));
	Store_field(to, 5, Field(from, 5));
	return Val_unit;
}

value caml_ml_swap_output(value to, value from) {
	/* record has 7 fields */
	Store_field(to, 0, Field(from, 0));
	Store_field(to, 1, Field(from, 1));
	Store_field(to, 2, Field(from, 2));
	Store_field(to, 3, Field(from, 3));
	Store_field(to, 4, Field(from, 4));
	Store_field(to, 5, Field(from, 5));
	Store_field(to, 6, Field(from, 6));
	return Val_unit;
}

Then initialise with:

(* init the standard IO descriptors *)
swap_input stdin kernel_stdin;;
swap_output stdout kernel_stdout;;
swap_output stderr kernel_stderr;;

However, as soon as I try to print, I get a fatal error that output
stream is closed =/

What's the correct way to do this?

Jonathan


             reply	other threads:[~2005-12-22  4:03 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-22  4:03 Jonathan Roewen [this message]
2005-12-22  4:23 ` Jonathan Roewen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ad8cfe7e0512212003v29fd0a97q47c813df78c9fea0@mail.gmail.com \
    --to=jonathan.roewen@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox