]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/pxp/pxp/tools/insert_variant
- the mathql interpreter is not helm-dependent any more
[helm.git] / helm / DEVEL / pxp / pxp / tools / insert_variant
1 #! /bin/sh
2 # (*
3 exec ocaml "$0" "$@"
4 *) directory ".";;
5
6 (* $Id$
7  * ----------------------------------------------------------------------
8  *
9  *)
10
11 let get_arg variant insert_line =
12   (* returns the argument of an "#insert" line *)
13   let s = ref "" in
14   for i = 8 to String.length insert_line - 1 do
15     match insert_line.[i] with
16         ' ' -> ()
17       | '*' ->
18           (* replace '*' with 'variant' *)
19           s := !s ^ variant
20       | c ->
21           s := !s ^ String.make 1 c
22   done;
23   !s
24 ;;
25
26
27 let edit_file variant name =
28   let basename = Filename.chop_suffix name ".src" in
29   let mllname = basename ^ "_" ^ variant ^ ".mll" in
30   let chin = open_in name in
31   let chout = open_out mllname in
32   output_string chout "(* File generated by insert_variant; DO NOT EDIT! *)\n";
33   begin try
34     while true do
35       let line = input_line chin in
36       (* We do not have Str here. *)
37       if String.length line >= 8 & String.sub line 0 8 = "#insert " then begin
38         let insname = get_arg variant line in
39         (* Copy the file 'insname' to chout *)
40         let chcopy = open_in insname in
41         let n = in_channel_length chcopy in
42         let s = String.create n in
43         really_input chcopy s 0 n;
44         close_in chcopy;
45         output_string chout s;
46       end
47       else begin
48         output_string chout line;
49         output_char chout '\n';
50       end
51     done
52   with
53       End_of_file -> ()
54   end;
55   close_in chin;
56   close_out chout
57 ;;
58
59
60 let main() =
61   let variant = ref "" in
62   let files = ref [] in
63   Arg.current := 0;          (* Because of a OCaml-3.00 bug *)
64   Arg.parse
65       [ "-variant", Arg.String (fun s -> variant := s),
66                 "<name>  Set the variant (character encoding)";
67       ]
68       (fun s -> files := !files @ [s])
69       "insert_variant [ options ] file.src ...
70
71 Reads the files, replaces the #insert lines by the referred files, and 
72 writes the file file_variant.mll. 
73
74 The #insert lines include the specified file into the source. The
75 asterisk (*) is replaced by the name of the variant.
76
77 Options:
78 ";
79   
80   if !variant = "" then 
81     failwith "No variant specified!";
82
83   List.iter 
84     (fun name -> edit_file !variant name)
85     !files
86 ;;
87
88
89 main();;
90
91 (* ======================================================================
92  * History:
93  * 
94  * $Log$
95  * Revision 1.1  2000/11/17 09:57:35  lpadovan
96  * Initial revision
97  *
98  * Revision 1.2  2000/05/20 21:14:33  gerd
99  *      Workaround for an OCaml 3.00 bug.
100  *
101  * Revision 1.1  2000/05/20 20:30:15  gerd
102  *      Initial revision.
103  *
104  * 
105  *)