Changeset 120

Show
Ignore:
Timestamp:
18.12.2006 15:47:18 (2 years ago)
Author:
decoder
Message:

Patch to work correctly with amavisd and others, contributed by Mark Martinec
Changed substitution rules in FuzzyOcr?.pm slightly

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/devel/FuzzyOcr.pm

    r117 r120  
    798798                        } 
    799799                        if ($conf->{focr_strip_numbers}) { 
    800                             tr/!;|081/iiioal/; 
     800                            tr/!;|(0815/iiicoals/; 
    801801                            s/[0-9]//g; 
    802802                        } else { 
    803                             tr/!;|/iii/; 
     803                            tr/!;|(/iiic/; 
    804804                        } 
    805805                        s/[^a-z0-9 ]//g; 
  • trunk/devel/FuzzyOcr/Misc.pm

    r91 r120  
    66 
    77use lib qw(..); 
    8 use FuzzyOcr::Config qw(set_pid unset_pid get_config get_timeout); 
     8use FuzzyOcr::Config qw(set_pid unset_pid get_config get_timeout kill_pid); 
    99use FuzzyOcr::Logging qw(debuglog errorlog warnlog infolog); 
    1010use Time::HiRes qw( gettimeofday tv_interval ); 
     11use POSIX qw(WIFEXITED WIFSIGNALED WIFSTOPPED WEXITSTATUS WTERMSIG WSTOPSIG 
     12             O_RDONLY O_WRONLY O_RDWR O_APPEND O_CREAT O_EXCL); 
    1113 
    1214# Provide some misc helper functions 
     
    4547        errorlog("Cannot remove DIR: $dir"); 
    4648    } 
     49} 
     50 
     51# map process termination status number to a string, and append optional 
     52# user error mesage, returning the resulting string 
     53sub exit_status_str($;$) { 
     54  my($stat,$err) = @_; my($str); 
     55  if (WIFEXITED($stat)) { 
     56    $str = sprintf("exit %d", WEXITSTATUS($stat)); 
     57  } elsif (WIFSTOPPED($stat)) { 
     58    $str = sprintf("stopped, signal %d", WSTOPSIG($stat)); 
     59  } else { 
     60    $str = sprintf("DIED on signal %d (%04x)", WTERMSIG($stat),$stat); 
     61  } 
     62  $str .= ', '.$err  if defined $err && $err != 0; 
     63  $str; 
     64} 
     65 
     66# POSIX::open a file or dup an existing fd (Perl open syntax), with a 
     67# requirement that it gets opened on a prescribed file descriptor $fd_target; 
     68# this subroutine is usually called from a forked process prior to exec 
     69sub open_on_specific_fd($$) { 
     70  my($fd_target,$fname) = @_; 
     71  my($flags) = 0; my($mode) = 0640; 
     72  $fname =~ s/^< *//; 
     73  $fname =~ s/^>> *// and $flags |= O_CREAT|O_WRONLY|O_APPEND; 
     74  $fname =~ s/^> *//  and $flags |= O_CREAT|O_WRONLY|O_EXCL; 
     75  POSIX::close($fd_target);  # ignore error, we may have just closed a log 
     76  my($fd_got) = POSIX::open($fname,$flags,$mode); 
     77  defined $fd_got or die "Can't open $fname, flags=$flags: $!"; 
     78  $fd_got = 0 + $fd_got;     # turn into numeric, avoid: "0 but true" 
     79  if ($fd_got != $fd_target) {  # dup, ensuring we get a specified descriptor 
     80    # POSIX mandates we got the lowest fd available (but some kernels have 
     81    # bugs), let's be explicit that we require a specified file descriptor 
     82    defined POSIX::dup2($fd_got,$fd_target) 
     83      or "Can't dup2 from $fd_got to $fd_target: $!"; 
     84    if ($fd_got > 2) {  # let's get rid of the original fd, unless 0,1,2 
     85      my($err); defined POSIX::close($fd_got) or $err = $!; 
     86      $err = defined $err ? ": $err" : ''; 
     87    } 
     88  } 
     89  $fd_got; 
    4790} 
    4891 
     
    62105            return -1; 
    63106        } elsif (not $pid) { 
     107          eval {  # must not use die in forked process, or we end up with 
     108                  # two running daemons! 
    64109            debuglog("Exec  : $cmd"); 
    65110            debuglog("Stdin : $stdin")  if (defined $stdin); 
    66111            debuglog("Stdout: $stdout") if (defined $stdout); 
    67112            debuglog("Stderr: $stderr") if (defined $stderr); 
     113            # there is no guarantee that Perl file handles STDIN, STDOUT 
     114            # and STDERR are on file descriptors 0, 1, 2.  Let's make sure 
     115            # the exec'd program receives the right files on file descr 0,1,2 
    68116            if (defined $stdin) { 
    69                 open(STDIN, $stdin); 
     117                open_on_specific_fd(0, $stdin); 
    70118            } 
    71119            if (defined $stdout) { 
    72                 open(STDOUT, $stdout); 
     120                open_on_specific_fd(1, $stdout); 
    73121            } 
    74122            if (defined $stderr) { 
    75                 open(STDERR, $stderr); 
    76             } 
    77             exec($cmd) and exit($?); 
    78             exit(-2); 
     123                open_on_specific_fd(2, $stderr); 
     124            } 
     125            exec($cmd); 
     126            die "failed to exec $cmd: $!"; 
     127          }; 
     128          # couldn't open file descriptors or exec failed 
     129          chomp($@); my($msg) = "save_execute: $@\n"; 
     130          # try to get some attention, log and stderr may be closed 
     131          POSIX::write(2,$msg,length($msg));  print STDERR $msg; 
     132          POSIX::_exit(8);  # must avoid END and destructor processing! 
    79133        } else { 
    80134            set_pid($pid); wait(); $retcode = $?; 
    81             debuglog("Elapsed: ". 
    82                 sprintf ("%.6f",tv_interval($begin, [gettimeofday])) 
    83                 ." sec. ($pgm)($retcode)"); 
     135            debuglog(sprintf("Elapsed [%s]: %.6f sec. (%s: %s)", 
     136                $pid, tv_interval($begin, [gettimeofday]), 
     137                $pgm, exit_status_str($retcode))); 
    84138            unset_pid(); 
    85139            if ($return_stdout and $stdout !~ m,/dev/null,i) { 
     
    101155                return -1; 
    102156            } elsif (not $pid) { 
     157              eval {  # must not use die in forked process, or we end up with 
     158                      # two running daemons! 
    103159                debuglog("Exec  : $cmd"); 
    104160                debuglog("Stdin : $stdin") if (defined $stdin); 
     
    106162                debuglog("Stderr: $stderr") if (defined $stderr); 
    107163                if (defined $stdin) { 
    108                     open(STDIN, $stdin); 
     164                    open_on_specific_fd(0, $stdin); 
    109165                } 
    110166                if (defined $stdout) { 
    111                     open(STDOUT, $stdout); 
     167                    open_on_specific_fd(1, $stdout); 
    112168                } 
    113169                if (defined $stderr) { 
    114                     open(STDERR, $stderr); 
    115                 } 
    116                 exec($cmd) and exit($?); 
    117                 exit(-2); 
     170                    open_on_specific_fd(2, $stderr); 
     171                } 
     172                exec($cmd); 
     173                die "failed to exec $cmd: $!"; 
     174              }; 
     175              # couldn't open file descriptors or exec failed 
     176              chomp($@); my($msg) = "save_execute: $@\n"; 
     177              # try to get some attention, log and stderr may be closed 
     178              POSIX::write(2,$msg,length($msg));  print STDERR $msg; 
     179              POSIX::_exit(8);  # must avoid END and destructor processing! 
    118180            } else { 
    119                 set_pid($pid); wait(); $retcode = $?>>8
    120                 debuglog("Elapsed: ". 
    121                     sprintf ("%.6f",tv_interval($begin, [gettimeofday])) 
    122                     ." sec. ($pgm)($retcode)"); 
     181                set_pid($pid); wait(); $retcode = $?
     182                debuglog(sprintf("Elapsed [%s]: %.6f sec. (%s: %s)", 
     183                    $pid, tv_interval($begin, [gettimeofday]), 
     184                    $pgm, exit_status_str($retcode))); 
    123185                unset_pid(); 
    124186                if ($return_stdout and $stdout !~ m,/dev/null,i) {