ParseCron

 view release on metacpan or  search on metacpan

lib/ParseCron.pm  view on Meta::CPAN


{
  my $x = join '|', map quotemeta($_), @dows;
  $dow = "^($x)\$";    # regexp
  $x = join '|', map quotemeta($_), @months;
  $month = "^($x)\$";  # regexp
}



# SET UP THE ENVIRONMENT ####################################################
#############################################################################

sub new {
    my $class = shift;
    my $self = bless {}, $class;
    
    return $self;
}

sub parse_cron {
    my $self = shift;

    my $crontab = shift;
    
    my (@bits, $k, $v, $english);
    
    my %atword = (  # for latter-day Vixie-isms
      'reboot'   => 'At reboot',
      'yearly'   => 'Yearly (midnight on January 1st)',
      'annually' => 'Yearly (midnight on January 1st)',
      'monthly'  => 'Monthly (midnight on the first of every month)',
      'weekly'   => 'Weekly (midnight every Sunday)',
      'daily'    => 'Daily, at midnight',
      'midnight' => 'Daily, at midnight',
      'hourly'   => 'At the top of every hour',
     # These are no longer documented in Vixie cron 3.0.  Why not?
    );
    
    #next if $crontab =~ m/^[ \t]*#/s or $crontab =~ m/^[ \t]*$/s;
    $crontab =~ s/^[ \t]+//s; # "leading spaces and tabs are ignored"

    # The POSIX cron spec doesn't seem to mention
    #  environment-setting lines at all!

    if (!$posix and $crontab =~ m/^([^= \t]+)[ \t]*=[ \t]*\"(.*)\"[ \t]*$/s) {
        # NAME = "VALUE"
        $k = ($crontab =~ s/[ \t]+$//);
    } 
    elsif (!$posix and $crontab =~ m/^([^= \t]+)[ \t]*=[ \t]*\'(.*)\'[ \t]*$/s) {
        ($k, $v) = ($1, $2);
    } 
    elsif (!$posix and $crontab =~ m/^([^= \t]+)[ \t]*=(.*)/s) {
        ($k,$v) = ($1, $2);
        $v = ($crontab =~ s/^[ \t]+//);
    } 
    elsif (!$posix and $crontab =~ m/^\@(\w+)[ \t]+(.*)/s and exists $atword{lc $1}) {
        $english = process_command($crontab, $atword{lc $1}, $2);
    }
    # for adding commands to be run to cron lines:
    #elsif ((@bits = split m/[ \t]+/, $crontab, 6) and @bits == 6) { 
    elsif ((@bits = split m/[ \t]+/, $crontab, 5) and @bits == 5) {
        $english = process_command($crontab, @bits);
    } 
    else {
        $english = 'ERROR';
    }

    $english = 'ERROR' if scalar (@bits) > 5;
    
    return $english;
}

sub process_command {
  # 0 m,   1 h,   2 day-of-month,  3 month,  4 dow
  ###my $line = shift;
  ###
  ###  my $filter = shift;
#print Dumper(\@_); 

    my $filter = '';
shift @_;
my $res = '';
  my(@time_lines, $command_string);
  if(@_ == 2) { # hack for funky vixieism
    $command_string = $_[1];
    @time_lines = ($_[0]);
  } else {
    # a normal line -- expand and Englishify it
    my(@bits) = expand_time_bits(@_);
#print Dumper(\@bits); 
    @time_lines = bits_to_english(@bits);
#print Dumper(\@time_lines); 
    $time_lines[0] = ucfirst($time_lines[0]);
    if(length(join '    ', @time_lines) <= 75) {
      @time_lines = (join '    ', @time_lines);
    }
    for(@time_lines) { $_ = ' ' . $_ }; # indent over
  #  $time_lines[0] = "At:" . $time_lines[0];
    
        ###$time_lines[0] = ":" . $time_lines[0];
        $time_lines[0] = $time_lines[0];
    $command_string = pop @bits;
  }
 
  my @command = split( "\n", percent_proc($command_string), -1 );
  
  if(@command) {
    pop @command if @command == 2 and $command[1] eq '';
     # Eliminate mention of basically null input
  } else {
    push @command, '';
  }
  
  if(@command > 1) {
    my $x = join "\n", splice @command, 1;
    push @command, " with input \"" . esc($x) . "\"";
  }
  if($command[0] =~ m<^\*>s) {
    push @command, " (Do you really mean the command to start with \"*\"?)";
  } elsif($command[0] eq '') {
    push @command, " (Do you really mean to run a null command?)";



( run in 0.508 second using v1.01-cache-2.11-cpan-71847e10f99 )