caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Jeffrey Loren Shaw" <shawjef3@msu.edu>
To: lists@philippewang.info
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Dynamic linking
Date: Mon, 18 Jun 2007 00:18:17 -0400	[thread overview]
Message-ID: <E1I08gz-0006sd-QS@sys15.mail.msu.edu> (raw)

Philippe, here's a little program I wrote that you might find useful. 

compile: 

ocamlopt -o keepsource keepsource.ml 

usage: 

./keepsource foo.ml 

creates a new file foo_keepsource.ml. If you compile it to the executable 
foo you can do ./foo -source to get the original source back. 

./keepsource foo.ml -compile 

does the same thing bug gives you an executable named foo in addition to 
foo_keepsource.ml. 

It will only work on single-file programs, and I haven't tested it much. 

(* keepsource.ml *) 

let replace_quote str =
 let result = ref [] in
 for i = 0 to String.length str - 1 do
   match str.[i] with
     '\\' -> result := '\\'::'\\'::!result
   | '\"' -> result := '\"'::'\\'::!result
   | a -> result := a::!result
 done;
 let buffer = " " in
 List.fold_left
   (fun accum a ->
      buffer.[0] <- a;
      buffer ^ accum
   )
   ""
   !result;; 

let print_help () =
   print_endline
   "usage: keepsource infile.ml (-compile)";; 

if Array.length Sys.argv = 1 then print_help ();; 

let sourcefile = Sys.argv.(1);; 

let sourcefile_keepsource = String.sub sourcefile 0 (String.length 
sourcefile - 3) ^ "_keepsource.ml";; 

if Sys.file_exists sourcefile then
 let inchan = open_in sourcefile in
 let source = ref [] in
 (
   try
     while true do
	source := (input_line inchan)::!source
     done
   with
     End_of_file -> ()
 );
 close_in inchan;
 source := List.rev !source;
 let outchan = open_out sourcefile_keepsource in
 let output = output_string outchan in
 output "let sourcecode = \"";
 List.iter (fun s -> output (replace_quote s^"\n")) !source;
 output "\"\n\nlet main () = (";
 List.iter (fun s -> output (s^"\n")) !source;
 output ");; 

if Array.length Sys.argv > 1 then
 if Sys.argv.(1) = \"-source\" then
   (
     print_endline sourcecode;
     exit 0
   )
 else
   main ()
 else
   main ()
";
 close_out outchan;; 

if Array.length Sys.argv > 2 then
 if Sys.argv.(2) = "-compile" then
   let compile_command =
     "ocamlopt -o " ^ String.sub sourcefile 0 (String.length sourcefile - 3) 
^ " "^ sourcefile_keepsource in
   ignore
     (
	Sys.command compile_command
     ) 



             reply	other threads:[~2007-06-18  4:18 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-06-18  4:18 Jeffrey Loren Shaw [this message]
  -- strict thread matches above, loose matches on Subject: below --
2007-06-15 16:03 Alain Frisch
2007-06-15 16:47 ` [Caml-list] " Joel Reymont
2007-06-15 16:52 ` Brian Hurt
2007-06-15 18:24   ` Alain Frisch
2007-06-15 18:59     ` Jon Harrop
2007-06-15 20:57       ` Alain Frisch
2007-06-15 22:38     ` Quôc Peyrot
2007-06-16  7:23       ` Alain Frisch
2007-06-16 17:33     ` Jon Harrop
2007-06-16 17:59       ` skaller
2007-06-16 20:13         ` Christophe TROESTLER
2007-06-16 23:10         ` Philippe Wang
2007-06-15 22:37 ` skaller
2007-06-15 22:45   ` Christophe TROESTLER
2007-06-15 22:58     ` skaller
2007-06-20 13:40 ` Alain Frisch
2007-06-24 11:35 ` Jon Harrop
2005-10-05 13:11 Alexander Bottema
2005-10-04 15:56 Alexander Bottema
2005-10-05  8:14 ` skaller
2005-10-04 12:47 Alexander Bottema
2005-10-04 14:47 ` skaller
2005-09-28 12:44 Alexander Bottema
2005-09-26  3:09 Jonathan Roewen
2005-09-26  4:26 ` skaller
2005-09-26  5:14 ` Jacques Garrigue
2005-09-26  5:20   ` Jonathan Roewen
2005-09-28  9:05   ` Jacques Garrigue
2005-04-05 21:12 Emir Pasalic
2005-04-05 22:02 ` [Caml-list] " Igor Pechtchanski

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=E1I08gz-0006sd-QS@sys15.mail.msu.edu \
    --to=shawjef3@msu.edu \
    --cc=caml-list@yquem.inria.fr \
    --cc=lists@philippewang.info \
    /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;
as well as URLs for NNTP newsgroup(s).