~ubuntu-branches/ubuntu/hardy/ocaml-doc/hardy

« back to all changes in this revision

Viewing changes to examples/grep/grep.ml

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Mimram
  • Date: 2007-09-08 01:49:22 UTC
  • mfrom: (0.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070908014922-lvihyehz0ndq7suu
Tags: 3.10-1
* New upstream release.
* Removed camlp4 documentation since it is not up-to-date.
* Updated to standards version 3.7.2, no changes needed.
* Updated my email address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
(***********************************************************************)
2
 
(*                                                                     *)
3
 
(*                           Objective Caml                            *)
4
 
(*                                                                     *)
5
 
(*               Pierre Weis, projet Cristal, INRIA Rocquencourt       *)
6
 
(*                                                                     *)
7
 
(*  Copyright 2001 Institut National de Recherche en Informatique et   *)
8
 
(*  en Automatique.  All rights reserved.  This file is distributed    *)
9
 
(*  only by permission.                                                *)
10
 
(*                                                                     *)
11
 
(***********************************************************************)
12
 
open Expr;;
13
 
open Auto;;
14
 
open Determ;;
15
 
 
16
 
let ligne_trouv�e = ref false;;
17
 
 
18
 
let grep_sur_canal auto nom_fich canal =
19
 
  try
20
 
    while true do
21
 
      let ligne = input_line canal in
22
 
      if reconna�t auto ligne then begin
23
 
        ligne_trouv�e := true;
24
 
        print_string nom_fich;
25
 
        print_string": ";
26
 
        print_endline ligne
27
 
      end
28
 
    done
29
 
  with End_of_file -> ();;
30
 
 
31
 
let grep_sur_fichier auto nom_fich =
32
 
  try
33
 
    let canal = open_in nom_fich in
34
 
    try grep_sur_canal auto nom_fich canal; close_in canal
35
 
    with exc -> close_in canal; raise exc
36
 
  with Sys_error message ->
37
 
    prerr_string "Erreur sur le fichier ";
38
 
    prerr_string nom_fich;
39
 
    prerr_string ": ";
40
 
    prerr_endline message;;
41
 
 
42
 
let construire_auto expr =
43
 
  d�terminise(expr_vers_automate(lire(Stream.of_string expr)));;
44
 
 
45
 
let grep expr fichier =
46
 
  grep_sur_fichier (construire_auto expr) fichier;;
47
 
 
48
 
if !Sys.interactive then () else
49
 
  if Array.length Sys.argv < 2 then begin
50
 
    prerr_endline "Utilisation: grep <motif> <fichiers>";
51
 
    exit 2
52
 
  end else begin
53
 
    let auto =
54
 
      try construire_auto Sys.argv.(1) with
55
 
      | Stream.Error s ->
56
 
          prerr_endline ("Erreur de syntaxe dans l'expression: " ^ s);
57
 
          exit 2
58
 
      | Stream.Failure ->
59
 
          prerr_endline "Erreur de syntaxe dans l'expression";
60
 
          exit 2 in
61
 
    if Array.length Sys.argv >= 3 then
62
 
      for i = 2 to Array.length Sys.argv - 1 do
63
 
        grep_sur_fichier auto Sys.argv.(i)
64
 
      done
65
 
    else
66
 
      grep_sur_canal auto "(entr�e standard)" stdin;
67
 
    exit (if !ligne_trouv�e then 0 else 1)
68
 
  end;;