File: main.ml

package info (click to toggle)
ocamlweb 1.37-13
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 712 kB
  • ctags: 820
  • sloc: ml: 5,758; sh: 1,749; makefile: 253
file content (383 lines) | stat: -rw-r--r-- 12,514 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
(*
 * ocamlweb - A WEB-like tool for ocaml
 * Copyright (C) 1999-2001 Jean-Christophe FILLITRE and Claude MARCH
 * 
 * This software is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License version 2, as published by the Free Software Foundation.
 * 
 * This software 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 Library General Public License version 2 for more details
 * (enclosed in the file LGPL).
 *)

(*i $Id: main.ml,v 1.47 2004/09/23 09:20:16 filliatr Exp $ i*)

(*i*)
open Filename
open Printf
open Output
open Web
open Doclexer
(*i*)


(*s \textbf{Usage.} Printed on error output. *)

let usage () =
  prerr_endline "";
  prerr_endline "Usage: ocamlweb <options and files>";
  prerr_endline "  -o <file>      write output in file <file>";
  prerr_endline "  --dvi          output the DVI";
  prerr_endline "  --ps           output the PostScript";
  prerr_endline "  --html         output the HTML";
  prerr_endline "  --hevea-option <opt>";
  prerr_endline "                 pass an option to hevea (HTML output)";
  prerr_endline "  -s             (short) no titles for files";
  prerr_endline "  --noweb        use manual LaTeX sectioning, not WEB";
  prerr_endline "  --header       do not skip the headers of Caml file";
  prerr_endline "  --no-preamble  suppress LaTeX header and trailer";
  prerr_endline "  --no-index     do not output the index";
  prerr_endline "  --extern-defs  keep external definitions in the index";
  prerr_endline "  --impl <file>  consider <file> as a .ml file";
  prerr_endline "  --intf <file>  consider <file> as a .mli file";
  prerr_endline "  --tex <file>   consider <file> as a .tex file";
  prerr_endline "  --latex-option <opt>";
  prerr_endline "                 pass an option to the LaTeX package ocamlweb.sty";
  prerr_endline "  --class-options <opt>";
  prerr_endline "                 set the document class options (defaults to `12pt')";
  prerr_endline "  --old-fullpage uses LaTeX package fullpage with no option";
  prerr_endline "  -p <string>    insert something in LaTeX preamble";
  prerr_endline "  --files <file> read file names to process in <file>";
  prerr_endline "  --quiet        quiet mode";
  prerr_endline "  --no-greek     disable use of greek letters for type variables";
  prerr_endline "";
  prerr_endline 
    "On-line documentation at http://www.lri.fr/~filliatr/ocamlweb/\n";
  exit 1


(*s \textbf{License informations.} Printed when using the option 
    \verb!--warranty!. *)

let copying () =
  prerr_endline "
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License version 2, as
published by the Free Software Foundation.

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 Library General Public License version 2 for more details
(enclosed in the file LGPL).";
  flush stderr


(*s \textbf{Banner.} Always printed. Notice that it is printed on error
    output, so that when the output of \ocamlweb\ is redirected this header
    is not (unless both standard and error outputs are redirected, of 
    course). *)

let banner () =
  eprintf "This is ocamlweb version %s, compiled on %s\n"
    Version.version Version.date;
  eprintf 
    "Copyright (c) 1999-2000 Jean-Christophe Fillitre and Claude March\n";
  eprintf
  "This is free software with ABSOLUTELY NO WARRANTY (use option -warranty)\n";
  flush stderr
    

(*s \textbf{Separation of files.} Files given on the command line are
    separated according to their type, which is determined by their suffix.
    Implementations and interfaces have respective suffixes \verb!.ml!
    and \verb!.mli! and \LaTeX\ files have suffix \verb!.tex!. *)

let check_if_file_exists f =
  if not (Sys.file_exists f) then begin
    eprintf "\nocamlweb: %s: no such file\n" f;
    exit 1
  end

let what_file f =
  check_if_file_exists f;
  if check_suffix f ".ml" then
    File_impl (make_caml_file f)
  else if check_suffix f ".mli" then
    File_intf (make_caml_file f)
  else if check_suffix f ".mll" then 
    File_lex (make_caml_file f)
  else if check_suffix f ".mly" then 
    File_yacc (make_caml_file f)
  else if check_suffix f ".tex" then
    File_other f
  else begin
    eprintf "\nocamlweb: don't know what to do with %s\n" f;
    exit 1
  end

(*s \textbf{Reading file names from a file.} 
    File names may be given
    in a file instead of being given on the command
    line. [(files_from_file f)] returns the list of file names contained
    in the file named [f]. These file names must be separated by spaces,
    tabulations or newlines.
 *)

let files_from_file f =
  let files_from_channel ch =
    let buf = Buffer.create 80 in
    let l = ref [] in
    try
      while true do
	match input_char ch with
	  | ' ' | '\t' | '\n' ->
	      if Buffer.length buf > 0 then l := (Buffer.contents buf) :: !l;
	      Buffer.clear buf
	  | c -> 
	      Buffer.add_char buf c
      done; []
    with End_of_file ->
      List.rev !l
  in
  try
    check_if_file_exists f;
    let ch = open_in f in
    let l = files_from_channel ch in
    close_in ch;l
  with Sys_error s -> begin
    eprintf "\nocamlweb: cannot read from file %s (%s)\n" f s;
    exit 1
  end

(*s \textbf{Parsing of the command line.} Output file, if specified, is kept
    in [output_file]. *)

let output_file = ref ""
let dvi = ref false
let ps = ref false
let html = ref false
let hevea_options = ref ([] : string list)

let parse () =
  let files = ref [] in
  let add_file f = files := f :: !files in
  let rec parse_rec = function
    | [] -> ()

    | ("-header" | "--header") :: rem ->
	skip_header := false; parse_rec rem
    | ("-noweb" | "--noweb" | "-no-web" | "--no-web") :: rem ->
	web := false; parse_rec rem
    | ("-web" | "--web") :: rem ->
	web := true; parse_rec rem
    | ("-nopreamble" | "--nopreamble" | "--no-preamble") :: rem ->
	set_no_preamble true; parse_rec rem
    | ("-p" | "--preamble") :: s :: rem ->
	push_in_preamble s; parse_rec rem
    | ("-p" | "--preamble") :: [] ->
	usage ()
    | ("-noindex" | "--noindex" | "--no-index") :: rem ->
	index := false; parse_rec rem
    | ("-o" | "--output") :: f :: rem ->
	output_file := f; parse_rec rem
    | ("-o" | "--output") :: [] -> 
	usage ()
    | ("-s" | "--short") :: rem ->
	short := true; parse_rec rem
    | ("-dvi" | "--dvi") :: rem ->
	dvi := true; parse_rec rem
    | ("-ps" | "--ps") :: rem ->
	ps := true; parse_rec rem
    | ("-html" | "--html") :: rem ->
	html := true; parse_rec rem
    | ("-hevea-option" | "--hevea-option") :: [] -> 
	usage ()
    | ("-hevea-option" | "--hevea-option") :: s :: rem -> 
	hevea_options := s :: !hevea_options; parse_rec rem
    | ("-extern-defs" | "--extern-defs") :: rem ->
	extern_defs := true; parse_rec rem
    | ("-q" | "-quiet" | "--quiet") :: rem ->
	quiet := true; parse_rec rem

    | ("--nogreek" | "--no-greek") :: rem ->
	use_greek_letters := false; parse_rec rem

    | ("-h" | "-help" | "-?" | "--help") :: rem ->
	banner (); usage ()
    | ("-v" | "-version" | "--version") :: _ ->
	banner (); exit 0
    | ("-warranty" | "--warranty") :: _ ->
	copying (); exit 0

    | "--class-options" :: s :: rem ->
	class_options := s; parse_rec rem
    | "--class-options" :: [] ->
	usage ()
    | "--latex-option" :: s :: rem ->
	add_latex_option s; parse_rec rem
    | "--latex-option" :: [] ->
	usage ()
    | "--old-fullpage" :: rem ->
	fullpage_headings := false; parse_rec rem

    | ("-impl" | "--impl") :: f :: rem -> 
	check_if_file_exists f;
	let n = 
	  if Filename.check_suffix f ".mll" || Filename.check_suffix f ".mly"
          then Filename.chop_extension f else f
	in
	let m = File_impl { caml_filename = f; caml_module = module_name n } in
	add_file m; parse_rec rem
    | ("-impl" | "--impl") :: [] ->
	usage ()
    | ("-intf" | "--intf") :: f :: rem ->
	check_if_file_exists f;
	let i = File_intf { caml_filename = f; caml_module = module_name f } in
	add_file i; parse_rec rem
    | ("-intf" | "--intf") :: [] ->
	usage ()
    | ("-tex" | "--tex") :: f :: rem -> 
	add_file (File_other f); parse_rec rem
    | ("-tex" | "--tex") :: [] ->
	usage ()
    | ("-files" | "--files") :: f :: rem ->
	List.iter (fun f -> add_file (what_file f)) (files_from_file f); 
	parse_rec rem
    | ("-files" | "--files") :: [] ->
	usage ()
    | f :: rem -> 
	add_file (what_file f); parse_rec rem
  in 
  parse_rec (List.tl (Array.to_list Sys.argv));
  List.rev !files

(*s The following function produces the output. The default output is
    the \LaTeX\ document: in that case, we just call [Web.produce_document]. 
    If option \verb!-dvi!, \verb!-ps! or \verb!-html! is invoked, then
    we make calls to \verb!latex!, \verb!dvips! and/or \verb!hevea!
    accordingly. *)

let locally dir f x =
  let cwd = Sys.getcwd () in
  try
    Sys.chdir dir; let y = f x in Sys.chdir cwd; y
  with e ->
    Sys.chdir cwd; raise e

let clean_temp_files basefile =
  let remove f = try Sys.remove f with _ -> () in
  remove (basefile ^ ".tex");
  remove (basefile ^ ".log");
  remove (basefile ^ ".aux");
  remove (basefile ^ ".dvi");
  remove (basefile ^ ".ps");
  remove (basefile ^ ".haux");
  remove (basefile ^ ".html")

let clean_and_exit basefile res = clean_temp_files basefile; exit res

let cat file =
  let c = open_in file in
  try
    while true do print_char (input_char c) done
  with End_of_file ->
    close_in c

let copy src dst =
  let cin = open_in src 
  and cout = open_out dst in
  try
    while true do Pervasives.output_char cout (input_char cin) done
  with End_of_file ->
    close_in cin; close_out cout

let produce_output fl =
  if not (!dvi || !ps || !html) then begin
    if !output_file <> "" then set_output_to_file !output_file;
    produce_document fl
  end else begin
    let texfile = temp_file "ocamlweb" ".tex" in
    let basefile = chop_suffix texfile ".tex" in
    set_output_to_file texfile;
    produce_document fl;
    let command = 
      let file = basename texfile in
      let file = 
	if !quiet then sprintf "'\\nonstopmode\\input{%s}'" file else file 
      in
      sprintf "(latex %s && latex %s) 1>&2 %s" file file
	(if !quiet then "> /dev/null" else "")
    in
    let res = locally (dirname texfile) Sys.command command in
    if res <> 0 then begin
      eprintf "Couldn't run LaTeX successfully\n"; 
      clean_and_exit basefile res
    end;
    let dvifile = basefile ^ ".dvi" in
    if !dvi then begin
      if !output_file <> "" then 
	(* we cannot use Sys.rename accross file systems *)
	copy dvifile !output_file 
      else 
	cat dvifile
    end;
    if !ps then begin
      let psfile = 
	if !output_file <> "" then !output_file else basefile ^ ".ps" 
      in
      let command = 
	sprintf "dvips %s -o %s %s" dvifile psfile 
	  (if !quiet then "> /dev/null 2>&1" else "")
      in
      let res = Sys.command command in
      if res <> 0 then begin
	eprintf "Couldn't run dvips successfully\n"; 
	clean_and_exit basefile res
      end;
      if !output_file = "" then cat psfile
    end;
    if !html then begin
      let htmlfile = 
	if !output_file <> "" then !output_file else basefile ^ ".html" 
      in
      let options = String.concat " " (List.rev !hevea_options) in
      let command = 
	sprintf "hevea %s ocamlweb.sty %s -o %s %s" options texfile htmlfile
	  (if !quiet then "> /dev/null 2>&1" else "")
      in
      let res = Sys.command command in
      if res <> 0 then begin
	eprintf "Couldn't run hevea successfully\n"; 
	clean_and_exit basefile res
      end;
      if !output_file = "" then cat htmlfile
    end;
    clean_temp_files basefile
  end

(*s \textbf{Main program.} Print the banner, parse the command line,
    read the files and then call [produce_document] from module [Web]. *)

let main () =
  let files = parse() in
  if List.length files > 0 then begin
    let l = List.map read_one_file files in
    if !web_style then begin
      if not !web && not !quiet then begin
	eprintf 
	  "Warning: web sections encountered while in noweb style, ignored.\n";
	flush stderr    
      end
    end else 
      web := false;
    if not !web then add_latex_option "noweb";
    produce_output l
  end

let _ = Printexc.catch main ()