| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Treex::PML::Backend::CSTS; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 605 | use vars qw($VERSION); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 36 |  | 
| 4 |  |  |  |  |  |  | BEGIN { | 
| 5 | 1 |  |  | 1 |  | 13 | $VERSION='2.21'; # version template | 
| 6 |  |  |  |  |  |  | } | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 1 |  |  | 1 |  | 28 | use Treex::PML; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | use Treex::PML::IO qw(set_encoding); | 
| 10 |  |  |  |  |  |  | use Treex::PML::Backend::CSTS::Csts2fs; | 
| 11 |  |  |  |  |  |  | use Treex::PML::Backend::CSTS::Fs2csts; | 
| 12 |  |  |  |  |  |  | use Fcntl qw(SEEK_SET); | 
| 13 |  |  |  |  |  |  | use File::ShareDir; | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | use vars qw($sgmls $sgmlsopts $doctype $csts_encoding); | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | sub default_settings { | 
| 18 |  |  |  |  |  |  | $sgmls = "nsgmls" unless $sgmls; | 
| 19 |  |  |  |  |  |  | $sgmlsopts = "-i preserve.gen.entities" unless $sgmlsopts; | 
| 20 |  |  |  |  |  |  | unless ($doctype and -f $doctype) { | 
| 21 |  |  |  |  |  |  | $doctype = eval { File::ShareDir::module_file(__PACKAGE__,'csts.doctype') }; | 
| 22 |  |  |  |  |  |  | unless (defined($doctype) and -f $doctype) { | 
| 23 |  |  |  |  |  |  | $doctype = Treex::PML::IO::CallerDir(File::Spec->catfile(qw(CSTS share csts.doctype))); | 
| 24 |  |  |  |  |  |  | } | 
| 25 |  |  |  |  |  |  | unless (-f $doctype) { | 
| 26 |  |  |  |  |  |  | $doctype = Treex::PML::FindInResources("csts.doctype"); | 
| 27 |  |  |  |  |  |  | } | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  | $sgmls_command='%s %o %d %f' unless $sgmls_command; | 
| 30 |  |  |  |  |  |  | $csts_encoding = 'iso-8859-2'; # this the encoding of CSTS by definition | 
| 31 |  |  |  |  |  |  | } | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | my %stderr_pool; | 
| 34 |  |  |  |  |  |  | sub open_backend { | 
| 35 |  |  |  |  |  |  | my ($filename, $mode, $encoding)=@_; | 
| 36 |  |  |  |  |  |  | if ($mode eq 'w') { | 
| 37 |  |  |  |  |  |  | return Treex::PML::IO::open_backend($filename,$mode,$csts_encoding); | 
| 38 |  |  |  |  |  |  | } elsif ($mode eq 'r') { | 
| 39 |  |  |  |  |  |  | my $fh = undef; | 
| 40 |  |  |  |  |  |  | my $cmd = $sgmls_command; | 
| 41 |  |  |  |  |  |  | $doctype = Treex::PML::FindInResources($doctype) unless -f $doctype; | 
| 42 |  |  |  |  |  |  | print STDERR "$cmd\n" if $Treex::PML::Debug; | 
| 43 |  |  |  |  |  |  | $cmd=~s/\%s/$sgmls/g; | 
| 44 |  |  |  |  |  |  | $cmd=~s/\%o/$sgmlsopts/g; | 
| 45 |  |  |  |  |  |  | $cmd=~s/\%d/$doctype/g; | 
| 46 |  |  |  |  |  |  | $cmd=~s/\%f/-/g; | 
| 47 |  |  |  |  |  |  | warn "[r $cmd]\n" if $Treex::PML::Debug; | 
| 48 |  |  |  |  |  |  | no integer; | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | { | 
| 51 |  |  |  |  |  |  | my $err = File::Temp->new(UNLINK => 1); | 
| 52 |  |  |  |  |  |  | $err->autoflush(1); | 
| 53 |  |  |  |  |  |  | open my $olderr, ">&", \*STDERR or die "Can't dup STDERR: $!"; | 
| 54 |  |  |  |  |  |  | open STDERR, ">&", $err or die "Can't dup temporary filehandle as STDERR: $!"; | 
| 55 |  |  |  |  |  |  | eval { | 
| 56 |  |  |  |  |  |  | $fh = set_encoding(Treex::PML::IO::open_pipe($filename,'r',$cmd),$csts_encoding); | 
| 57 |  |  |  |  |  |  | binmode $fh,':crlf' if $fh and $^O eq 'MSWin32'; | 
| 58 |  |  |  |  |  |  | }; | 
| 59 |  |  |  |  |  |  | close(STDERR); | 
| 60 |  |  |  |  |  |  | open STDERR, ">&", $olderr or die "Can't dup old STDERR: $!"; | 
| 61 |  |  |  |  |  |  | if ($@) { | 
| 62 |  |  |  |  |  |  | close $err; | 
| 63 |  |  |  |  |  |  | die $@; | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  | $stderr_pool{$fh} = $err; | 
| 66 |  |  |  |  |  |  | return $fh; | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  | } else { | 
| 69 |  |  |  |  |  |  | die "unknown mode $mode\n"; | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | sub close_backend { | 
| 74 |  |  |  |  |  |  | my ($fh)=@_; | 
| 75 |  |  |  |  |  |  | if (exists $stderr_pool{$fh}) { | 
| 76 |  |  |  |  |  |  | my $err = delete $stderr_pool{$fh}; | 
| 77 |  |  |  |  |  |  | seek($err,0,SEEK_SET); | 
| 78 |  |  |  |  |  |  | local $/; | 
| 79 |  |  |  |  |  |  | my $warnings = <$err>; | 
| 80 |  |  |  |  |  |  | close($err); | 
| 81 |  |  |  |  |  |  | if (defined $warnings and length $warnings) { | 
| 82 |  |  |  |  |  |  | warn $warnings; | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  | unless (Treex::PML::IO::close_backend($fh)) { | 
| 86 |  |  |  |  |  |  | die "$sgmls ended with error code $?\n"; | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  | return 1; | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | sub read { | 
| 92 |  |  |  |  |  |  | Treex::PML::Backend::CSTS::Csts2fs::read(@_); | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | sub write { | 
| 96 |  |  |  |  |  |  | Treex::PML::Backend::CSTS::Fs2csts::write(@_); | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | sub test_nsgmls { | 
| 100 |  |  |  |  |  |  | return 1 if (-x $sgmls); | 
| 101 |  |  |  |  |  |  | foreach (split(($^O eq 'MSWin32' ? ';' : ':'),$ENV{PATH})) { | 
| 102 |  |  |  |  |  |  | if (-x "$_".($^O eq 'MSWin32' ? "\\" : "/")."$sgmls") { | 
| 103 |  |  |  |  |  |  | unless (-f $doctype) { | 
| 104 |  |  |  |  |  |  | warn("CSTS doctype not found: $doctype\n") if $Treex::PML::Debug; | 
| 105 |  |  |  |  |  |  | return 0; | 
| 106 |  |  |  |  |  |  | } | 
| 107 |  |  |  |  |  |  | return 1; | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  | } | 
| 110 |  |  |  |  |  |  | warn("nsgmls not found at $sgmls\n") if $Treex::PML::Debug; | 
| 111 |  |  |  |  |  |  | return 0; | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | sub test { | 
| 115 |  |  |  |  |  |  | my ($f,$encoding)=@_; | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | return 0 unless test_nsgmls(); | 
| 118 |  |  |  |  |  |  | if (ref($f)) { | 
| 119 |  |  |  |  |  |  | my $line=$f->getline(); | 
| 120 |  |  |  |  |  |  | return $line=~/^\s*]|^ | 
| 121 |  |  |  |  |  |  | } else { | 
| 122 |  |  |  |  |  |  | my $fh = Treex::PML::IO::open_backend($f,"r"); | 
| 123 |  |  |  |  |  |  | my $test = $fh && test($fh); | 
| 124 |  |  |  |  |  |  | Treex::PML::IO::close_backend($fh); | 
| 125 |  |  |  |  |  |  | return $test; | 
| 126 |  |  |  |  |  |  | } | 
| 127 |  |  |  |  |  |  | } | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | BEGIN { | 
| 130 |  |  |  |  |  |  | default_settings(); | 
| 131 |  |  |  |  |  |  | } | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | 1; | 
| 134 |  |  |  |  |  |  | __END__ |