caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] HELP! In-place modification of caml globals...
@ 2005-12-22  4:03 Jonathan Roewen
  2005-12-22  4:23 ` Jonathan Roewen
  0 siblings, 1 reply; 2+ messages in thread
From: Jonathan Roewen @ 2005-12-22  4:03 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] HELP! In-place modification of caml globals...
  2005-12-22  4:03 [Caml-list] HELP! In-place modification of caml globals Jonathan Roewen
@ 2005-12-22  4:23 ` Jonathan Roewen
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Roewen @ 2005-12-22  4:23 UTC (permalink / raw)
  To: caml-list

Nevermind :-)

Turns out it was an exception being raised and tried to be printed at
caml init, before the descriptors were swapped...

Jonathan


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-12-22  4:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-12-22  4:03 [Caml-list] HELP! In-place modification of caml globals Jonathan Roewen
2005-12-22  4:23 ` Jonathan Roewen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).