+ # then read in any local ones
+ if (-e $localfn) {
+ $t = -M $localfn;
+
+ @lcrontab = cread($localfn);
+ $mtime = $t if $t <= $mtime;
+ }
+ @crontab = (@scrontab, @lcrontab);
+ }
+}
+
+# read in a cron file
+sub cread
+{
+ my $fn = shift;
+ my $fh = new IO::File;
+ my $line = 0;
+ my @out;
+
+ dbg("DXCron::cread reading $fn\n") if isdbg('cron');
+ open($fh, $fn) or confess("cron: can't open $fn $!");
+ while (my $l = <$fh>) {
+ $line++;
+ chomp $l;
+ next if $l =~ /^\s*#/ or $l =~ /^\s*$/;
+ if (my ($ts) = $l =~/^\s*LOCALE\s*=\s*(UTC|LOCAL)/i) {
+ $ts = uc $ts;
+ if ($ts eq 'UTC') {
+ $use_localtime = 0;
+ } elsif ($ts eq 'LOCAL') {
+ $use_localtime = 1;
+ }
+ dbg("DXCron: LOCALE set to $ts") if isdbg('cron');
+ }
+ my ($min, $hour, $mday, $month, $wday, $cmd) = $l =~ /^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)$/;
+ next unless defined $min;
+ my $ref = bless {};
+ my $err = '';
+
+ if (defined $min && defined $hour && defined $cmd) { # it isn't all of them, but should be enough to tell if this is a real line
+ $err .= parse($ref, 'min', $min, 0, 60);
+ $err .= parse($ref, 'hour', $hour, 0, 23);
+ $err .= parse($ref, 'mday', $mday, 1, 31);
+ $err .= parse($ref, 'month', $month, 1, 12, "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec");
+ $err .= parse($ref, 'wday', $wday, 0, 6, "sun", "mon", "tue", "wed", "thu", "fri", "sat");
+ if (!$err) {
+ $ref->{cmd} = $cmd;
+ push @out, $ref;
+ dbg("DXCron::cread: adding $l\n") if isdbg('cron');
+ } else {
+ $err =~ s/^, //;
+ LogDbg('cron', "DXCron::cread: error $err on line $line '$l'");
+ }
+ } else {
+ LogDbg('cron', "DXCron::cread error on line $line '$l'");
+ my @s = ($min, $hour, $mday, $month, $wday, $cmd);
+ my $s = "line $line splits as " . join(', ', (map {defined $_ ? qq{$_} : q{'undef'}} @s));
+ LogDbg('cron', $s);
+ }
+ }
+ close($fh);
+ return @out;
+}
+
+sub parse
+{
+ my $ref = shift;
+ my $sort = shift;
+ my $val = shift;
+ my $low = shift;
+ my $high = shift;
+ my @req;
+
+ # handle '*' values
+ if ($val eq '*') {
+ $ref->{$sort} = 0;
+ return '';
+ }
+
+ # handle comma delimited values
+ my @comma = split /,/o, $val;
+ for (@comma) {
+ my @minus = split /-/o;
+ if (@minus == 2) {
+ return ", $sort should be $low >= $minus[0] <= $high" if $minus[0] < $low || $minus[0] > $high;
+ return ", $sort should be $low >= $minus[1] <= $high" if $minus[1] < $low || $minus[1] > $high;
+ my $i;
+ for ($i = $minus[0]; $i <= $minus[1]; ++$i) {
+ push @req, 0 + $i;
+ }
+ } else {
+ return ", $sort should be $low >= $val <= $high" if $_ < $low || $_ > $high;
+ push @req, 0 + $_;
+ }
+ }
+ $ref->{$sort} = \@req;
+
+ return '';