/usr/share/doc/cron/examples/crontab2english.pl is in cron 3.0pl1-124.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 | #!/usr/bin/perl
#Time-stamp: "2001-07-29 16:07:28 MDT"
my $VERSION = '0.71';
require 5.00404; # I don't think there's (?: ... ) before that.
=head1 NAME
crontab2english -- explain crontab commands in English
=head1 SYNOPSIS
Usage:
% crontab2english [-f] files...
Or:
% cat files... | crontab2english
If you do just this:
% crontab2english
then it's the same as crontab -l | crontab2english
Example output:
% crontab2english | less
Setting env var MAILTO to hulahoops@polygon.int
Command: (line 2)
Run: /bin/csh -c 'perl ~/thang.pl | mail -s hujambo root'
At: 8:10am on the 15th of every month
Command: (line 5)
Run: df -k
At: 5:40am every day
Command: (line 7)
Run: ls -l /tmp
At: 6:50am every Monday
Or with the -f ("f" for filter) switch, it just adds comments
to the input file:
% crontab2english -f | less
# My happy crontab file
MAILTO=hulahoops@polygon.int
10 8 15 * * /bin/csh -c 'perl ~/thang.pl | mail -s hujambo root'
#> At: 8:10am on the 15th of every month
40 5 * * * df -k
#> At: 5:40am every day
50 6 * * 1 ls -l /tmp
#> At: 6:50am every Monday
=head1 DESCRIPTION
It's easy to make mistakes in crontab files. Running
C<crontab2english> on your crontab files and reading the
resulting English explanations will help you catch errors.
=head1 SWITCHES
C<-f> puts this in "filter mode" -- the output is just
the input plus commentary.
C<-v> describes the current C<crontab2english>
version to STDOUT and exits.
C<-p> forces POSIX-only mode: anything not allowed in
the POSIX crontab spec won't be understood.
C<-e> (usually default) turns off POSIX-only mode:
i.e., it doesn't feign ignorance of things not in
the POSIX spec.
C<--> signals end of switches.
=head1 ENVIRONMENT
If the evironment variables C<POSIXLY_CORRECT> and/or
C<POSIX_ME_HARDER> are true, then this turns on C<-p> (POSIX-only)
mode by default. That's overrideable with the C<-e> switch.
=head1 CAVEATS
I've tried to make this program understand all the kinds of crontab
lines that are out there. That probably includes a few kinds of lines
that your particular cron daemon doesn't understand, so just because
crontab2english understands something doesn't mean your cron daemon
will.
Pragmatically, however, there seem to be three kinds of cron daemons
around these days:
=over
=item *
Ones based on old (1993ish) Vixie crontab. These understand all of
POSIX, and more. This is what almost almost everyone runs these days.
=item *
Ones that understand I<only> what the POSIX crontab spec allows --
which excludes all sorts of amenities including: stepped ranges
("1-9/2"), "VAR=NAME" lines, English month or day abbreviations ("mon"
or "jan"), day 7 meaning "Sunday", and "*/3" meaning "every third...".
=item *
Even more recent (post-1993ish) Vixies. These seem relatively rare.
They seem to be just like old Vixies, plus they understand at-words
like "@annually", "@reboot", etc. (altho in some cases, those aren't
mentioned in the docs!).
=back
There I<could> be some ancient or demented pre-Vixie non-POSIX crons
running somewhere. One hopes that these would all basically
understand anything that POSIX does (and possibly nothing more?), but
you just might find peculiarities including:
=over
=item *
Rejecting 0 for Sunday, and accepting only 7.
=item *
Rejecting 7 for Sunday, and accepting only 0.
=item *
Accepting lists I<or> ranges, but no lists that include ranges. I.e.,
allowing "7,8,9", allowing "1-3", but I<not> allowing "1-3,7-9".
=item *
Not accepting ranges at all?
=back
Consult your man pages carefully.
Good general advice: keep your crontab lines simple, and that'll
minimize the chances of disagreement between what you indend, what
crontab2english understands (with or without C<-p>), and what your
cron daemon understands.
=head1 SEE ALSO
C<man 1 crontab>
C<man 5 crontab>
C<man 8 cron>
=head1 BUG REPORTS
If this program explains a crontab line wrong, or can't parse
it, then email me the line, and an explanation of how it you
think it should parse.
=head1 DISCLAIMER
C<crontab2english> is distributed in the hope that it will be useful,
but B<without any warranty>; without even the implied warranty of
B<merchantability> or B<fitness for a particular purpose>.
=head1 COPYRIGHT
Copyright 2001 Sean M. Burke.
This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=head1 AUTHOR
Sean M. Burke, E<lt>sburke@cpan.orgE<gt>
=head1 README
Translates crontab notation into English, for sanity checking: For
example, "10 8 15 * * foo bar" into: Run: foo bar with input
"baz\x0a" At: 8:10am on the 15th of every month
=head1 SCRIPT CATEGORIES
UNIX/System_administration
=head1 CHANGE LOG
=over
=item v0.71: 2001-07-29
Now supports the weird new Vixie-isms like "C<@annually>".
Features for feigning ignorance of non-POSIX features: C<-p>, C<-e>,
and the evironment variables C<POSIXLY_CORRECT> and/or
C<POSIX_ME_HARDER>.
=item v0.63: 2001-07-17
Fixed a bug spotted by Greg Wimpey, where leading whitespace wasn't
getting duly ignored.
=item v0.62: 2001-07-14
Added special cases for when minutes field is 0.
Added explicit "require" statement to ensure acceptable Perl version.
Changed "Every Tuesday of May" to "Every Tuesday in May"
Changed qr//'s to just plain strings, for all the 500404 dinosaurs.
=item v0.61 2001-01-23
First public release.
=back
=cut
use strict;
use integer;
use constant 'DEBUG' => 0;
my $filter = ''; # ...which is false.
# Lame switch processing:
my $posix; # whether to be just POSIX
while(@ARGV and $ARGV[0] =~ m<^->s) {
if($ARGV[0] eq '--') { # end of switches.
shift @ARGV;
last;
} elsif($ARGV[0] eq '-f') { # filter mode
shift @ARGV;
$filter = '#> '; # ...which is true!
} elsif($ARGV[0] eq '-p') { # disable extensions
shift @ARGV;
$posix = 1;
} elsif($ARGV[0] eq '-e') { # enable extensions
shift @ARGV;
$posix = 0;
} elsif($ARGV[0] eq '-v') {
print "crontab2english v$VERSION sburke\@cpan.org\n";
exit;
} else {
die "Usage: crontab2english [-f] [files]\n"
. "See 'perldoc crontab2english' for more info.\n";
}
}
$posix = $ENV{'POSIXLY_CORRECT'} || $ENV{'POSIX_ME_HARDER'}
unless defined $posix;
print $filter, " POSIX-only mode\n" if $posix;
my @lines;
if(@ARGV) {
@lines = <>;
} elsif(-t *STDIN) {
@lines = `crontab -l`;
} else {
@lines = <STDIN>;
}
#--------------------------------------------------------------------------
# Build tables.
my @dows = qw(Sun Mon Tue Wed Thu Fri Sat);
my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
my($dow, $month, %dow2num, %month2num, %num2dow, %num2month);
my %mil2ampm;
@mil2ampm{0 .. 23}
= ('midnight', map($_ . 'am', 1 .. 11), 'noon', map($_ . 'pm', 1 .. 11));
@dow2num{map lc($_), @dows} = (0 .. 6);
push @dows, 'Sun' unless $posix;
# POSIX doesn't know about day 7
@num2dow{0 .. $#dows} = @dows;
DEBUG and print "DOWS: @dows\n";
@month2num{map lc($_), @months} = (1 .. 12);
@num2month{1 .. 12} = @months;
unshift @months, '';
{
my $x = join '|', map quotemeta($_), @dows;
$dow = "^($x)\$"; # regexp
$x = join '|', map quotemeta($_), @months;
$month = "^($x)\$"; # regexp
}
my(%num2month_long, %num2dow_long);
@num2month_long{1 .. 12} = qw(
January February March April May June July August September October
November December
);
@num2dow_long{0 .. 6} = qw(
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
);
$num2dow_long{7} = 'Sunday' unless $posix;
my $atom;
if($posix) {
$atom = '\d+|(?:\d+-\d+)'; # will be a RE
# Yes, POSIX allows no stepped ranges.
} else {
$atom = '\d+|(?:\d+-\d+(?:/\d+)?)'; # will be a RE
}
my $atoms = "^(?:$atom)(?:,$atom)*\$"; # well be a RE
print "Atoms RE: $atoms\n" if DEBUG;
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?
);
#--------------------------------------------------------------------------
my $line_number = 0;
{
my(@bits,$k,$v);
foreach (@lines) {
print $_ if $filter;
chomp;
DEBUG > 1 and print "Line: <$_>\n";
++$line_number;
next if m/^[ \t]*#/s or m/^[ \t]*$/s;
s/^[ \t]+//s; # "leading spaces and tabs are ignored"
if(DEBUG > 1) {
@bits = split m/[ \t]+/,$_,6;
print "Bit count: ", scalar(@bits), ".\n";
}
# The POSIX cron spec doesn't seem to mention
# environment-setting lines at all!
if(!$posix and m/^([^= \t]+)[ \t]*=[ \t]*\"(.*)\"[ \t]*$/s ) {
# NAME = "VALUE"
$k =~ s/[ \t]+$//;
$filter or print "Setting env var $k to \"$v\"\n";
} elsif(!$posix and m/^([^= \t]+)[ \t]*=[ \t]*\'(.*)\'[ \t]*$/s ) {
# NAME = 'VALUE'
($k,$v) = ($1,$2);
#$k =~ s/[ \t]+$//;
$filter or print "Setting env var $k to \'$v\'\n";
} elsif(!$posix and m/^([^= \t]+)[ \t]*=(.*)/s ) {
# NAME = VALUE
($k,$v) = ($1,$2);
#$k =~ s/[ \t]+$//;
$v =~ s/^[ \t]+//;
$filter or print "Setting env var $k to $v\n\n";
} elsif(!$posix and m/^\@(\w+)[ \t]+(.*)/s and exists $atword{lc $1}) {
process_command($_, $atword{lc $1}, $2);
} elsif( (@bits = split m/[ \t]+/, $_, 6) and @bits == 6 ) {
DEBUG and print "Bits: ", map("<$_> ", @bits), "\n";
process_command($_, @bits);
} else {
if($filter) {
print $filter, "UNPARSEABLE LINE?!\n";
} else {
print "Unparseable line (#$line_number): \"", esc($_), "\"\n";
}
}
}
exit;
}
#--------------------------------------------------------------------------
sub process_command {
# 0 m, 1 h, 2 day-of-month, 3 month, 4 dow
my $line = shift;
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(@_);
if(@bits == 1) { # signals error condition
my $x = $bits[0];
if($filter) {
print $filter,
"Unparseable ", @$x == 1 ? 'bit' : 'bits',
": ", join(' ', map "\"$_\"", @$x), "\n"
;
} else {
print
"Unparseable ", @$x == 1 ? 'bit' : 'bits',
" in parsing of command ${$x}[-1] at line number $line_number:\n",
map(" $_\n", @$x), "\n"
;
}
return;
}
@time_lines = bits_to_english(@bits);
$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];
$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?)";
}
$command[0] = "Run: $command[0]";
if($filter) {
print
map("$filter $_\n",
(@command == 1) ? () : (@command), # be concise for simple cases
@time_lines
),
;
} else {
print
#was: "Command: (line $line_number) $line\n",
# but that's awful verbose
"Command: (line $line_number)\n",
map(" $_\n", @command, @time_lines ), "\n";
}
return;
}
#--------------------------------------------------------------------------
sub expand_time_bits {
my @bits = @_;
my @unparseable;
# 0 m, 1 h, 2 day-of-month, 3 month, 4 dow
unless($posix) {
if($bits[3] =~ m/($month)/oi) { $bits[3] = $month2num{lc $1} }
if($bits[4] =~ m/($dow)/oi ) { $bits[4] = $dow2num{lc $1} }
}
for(my $i = 0; $i < 5 ; ++$i) {
my @segments;
if($bits[$i] eq '*') {
push @segments, ['*'];
} elsif(!$posix and $bits[$i] =~ m<^\*/(\d+)$>s) {
# a hack for "*/3" etc
push @segments, ['*', 0 + $1];
} elsif($bits[$i] =~ m/$atoms/ois) {
foreach my $thang (split ',', $bits[$i]) {
if($thang =~ m<^(?:(\d+)|(?:(\d+)-(\d+)(?:/(\d+))?))$>s) {
if(defined $1) {
push @segments, [0 + $1]; # "7"
} elsif(defined $4) {
push @segments, [0 + $2, 0 + $3, 0 + $4]; # "3-20/4"
} else {
push @segments, [0 + $2, 0 + $3]; # "3-20"
}
} else {
warn "GWAH? thang \"$thang\"";
}
}
} else {
push @unparseable, sprintf "field %s: \"%s\"", $i + 1, esc($bits[$i]);
next;
}
$bits[$i] = \@segments;
}
return \@unparseable if @unparseable;
return @bits;
}
#--------------------------------------------------------------------------
sub bits_to_english {
# This is the deep ugly scary guts of this program.
# The older and eldritch among you might recognize this as sort of a
# parody of bad old Lisp style of data-structure handling.
my @bits = @_;
my @time_lines;
{ # gratuitous block.
# Render the minutes and hours ########################################
if(@{$bits[0]} == 1 and @{$bits[1]} == 1 and
@{$bits[0][0]} == 1 and @{$bits[1][0]} == 1 and
$bits[0][0][0] ne '*' and $bits[1][0][0] ne '*'
# It's a highly simplifiable time expression!
# This is a very common case. Like "46 13" -> 1:46pm
# Formally: when minute and hour are each a single number.
) {
my $h = $bits[1][0][0];
if($bits[0][0][0] == 0) {
# Simply at the top of the hour, so just call it by the hour name.
push @time_lines, $mil2ampm{$h};
} else {
# Can't say "noon:02", so use an always-numeric time format:
push @time_lines, sprintf '%s:%02d%s',
($h > 12) ? ($h - 12) : $h,
$bits[0][0][0],
($h >= 12) ? 'pm' : 'am';
}
$time_lines[-1] .= ' on';
} else { # It's not a highly simplifiable time expression
# First, minutes:
if($bits[0][0][0] eq '*') {
if(1 == @{$bits[0][0]} or $bits[0][0][1] == 1) {
push @time_lines, 'every minute of';
} else {
push @time_lines, 'every ' . freq($bits[0][0][1]) . ' minute of';
}
} elsif( @{$bits[0]} == 1 and $bits[0][0][0] == 0 ) {
# It's just a '0'. Ignore it -- instead of bothering
# to add a "0 minutes past"
} elsif( !grep @$_ > 1, @{$bits[0]} ) {
# it's all like 7,10,15. conjoinable
push @time_lines, conj_and(map $_->[0], @{$bits[0]}) . (
$bits[0][-1][0] == 1 ? ' minute past' : ' minutes past' );
} else { # it's just gonna be long.
my @hunks;
foreach my $bit (@{$bits[0]}) {
if(@$bit == 1) { #"7"
push @hunks, $bit->[0] == 1 ? '1 minute' : "$bit->[0] minutes";
} elsif(@$bit == 2) { #"7-9"
push @hunks, sprintf "from %d to %d %s", @$bit,
$bit->[1] == 1 ? 'minute' : 'minutes';
} elsif(@$bit == 3) { # "7-20/2"
push @hunks, sprintf "every %d %s from %d to %d",
$bit->[2],
$bit->[2] == 1 ? 'minute' : 'minutes',
$bit->[0], $bit->[1],
;
}
}
push @time_lines, conj_and(@hunks) . ' past';
}
# Now hours
if($bits[1][0][0] eq '*') {
if(1 == @{$bits[1][0]} or $bits[1][0][1] == 1) {
push @time_lines, 'every hour of';
} else {
push @time_lines, 'every ' . freq($bits[1][0][1]) . ' hour of';
}
} else {
my @hunks;
foreach my $bit (@{$bits[1]}) {
if(@$bit == 1) { # "7"
push @hunks, $mil2ampm{$bit->[0]} || "HOUR_$bit->[0]??";
} elsif(@$bit == 2) { # "7-9"
push @hunks, sprintf "from %s to %s",
$mil2ampm{$bit->[0]} || "HOUR_$bit->[0]??",
$mil2ampm{$bit->[1]} || "HOUR_$bit->[1]??",
} elsif(@$bit == 3) { # "7-20/2"
push @hunks, sprintf "every %d %s from %s to %s",
$bit->[2],
$bit->[2] == 1 ? 'hour' : 'hours',
$mil2ampm{$bit->[0]} || "HOUR_$bit->[0]??",
$mil2ampm{$bit->[1]} || "HOUR_$bit->[1]??",
}
}
push @time_lines, conj_and(@hunks) . ' of';
}
# End of hours and minutes
}
# Day-of-month ########################################################
if($bits[2][0][0] eq '*') {
$time_lines[-1] =~ s/ on$//s;
if(1 == @{$bits[2][0]} or $bits[2][0][1] == 1) {
push @time_lines, 'every day of';
} else {
push @time_lines, 'every ' . freq($bits[2][0][1]) . ' day of';
}
} else {
my @hunks;
foreach my $bit (@{$bits[2]}) {
if(@$bit == 1) { # "7"
push @hunks, 'the ' . ordinate($bit->[0]);
} elsif(@$bit == 2) { # "7-9"
push @hunks, sprintf "from the %s to the %s",
ordinate($bit->[0]), ordinate($bit->[1]),
} elsif(@$bit == 3) { # "7-20/2"
push @hunks, sprintf "every %d %s from the %s to the %s",
$bit->[2],
$bit->[2] == 1 ? 'day' : 'days',
ordinate($bit->[0]), ordinate($bit->[1]),
}
}
# collapse the "the"s, if all the elements have one
if(@hunks > 1 and !grep !m/^the /s, @hunks) {
for (@hunks) { s/^the //s; }
$hunks[0] = 'the '. $hunks[0];
}
push @time_lines, conj_and(@hunks) . ' of';
}
# Month ###############################################################
if($bits[3][0][0] eq '*') {
if(1 == @{$bits[3][0]} or $bits[3][0][1] == 1) {
push @time_lines, 'every month';
} else {
push @time_lines, 'every ' . freq($bits[3][0][1]) . ' month';
}
} else {
my @hunks;
foreach my $bit (@{$bits[3]}) {
if(@$bit == 1) { # "7"
push @hunks, $num2month_long{$bit->[0]} || "MONTH_$bit->[0]??"
} elsif(@$bit == 2) { # "7-9"
push @hunks, sprintf "from %s to %s",
$num2month_long{$bit->[0]} || "MONTH_$bit->[0]??",
$num2month_long{$bit->[1]} || "MONTH_$bit->[1]??",
} elsif(@$bit == 3) { # "7-20/2"
push @hunks, sprintf "every %d %s from %s to %s",
$bit->[2],
$bit->[2] == 1 ? 'month' : 'months',
$num2month_long{$bit->[0]} || "MONTH_$bit->[0]??",
$num2month_long{$bit->[1]} || "MONTH_$bit->[1]??",
}
}
push @time_lines, conj_and(@hunks);
# put in semicolons in the case of complex constituency
#if($time_lines[-1] =~ m/every|from/) {
# $time_lines[-1] =~ tr/,/;/;
# s/ (and|or)\b/\; $1/g;
#}
}
# Weekday #############################################################
#
#
#
#
# From man 5 crontab:
# Note: The day of a command's execution can be specified by two fields
# -- day of month, and day of week. If both fields are restricted
# (ie, aren't *), the command will be run when either field matches the
# current time. For example, "30 4 1,15 * 5" would cause a command to
# be run at 4:30 am on the 1st and 15th of each month, plus every Friday.
#
# [But if both fields ARE *, then it just means "every day".
# and if one but not both are *, then ignore the *'d one --
# so "1 2 3 4 *" means just 2:01, April 3rd
# and "1 2 * 4 5" means just 2:01, on every Friday in April
# But "1 2 3 4 5" means 2:01 of every 3rd or Friday in April. ]
#
#
#
#
# And that's a bit tricky.
if($bits[4][0][0] eq '*' and (
@{$bits[4][0]} == 1 or $bits[4][0][1] == 1
)
) {
# Most common case -- any weekday. Do nothing really.
#
# Hm, does "*/1" really mean "*" here, given the above note?
#
# Tidy things up while we're here:
if($time_lines[-2] eq "every day of" and
$time_lines[-1] eq 'every month'
) {
$time_lines[-2] = "every day";
pop @time_lines;
}
} else {
# Ugh, there's some restriction on weekdays.
# Translate the DOW-expression
my $expression;
my @hunks;
foreach my $bit (@{$bits[4]}) {
if(@$bit == 1) {
push @hunks, $num2dow_long{$bit->[0]} || "DOW_$bit->[0]??";
} elsif(@$bit == 2) {
if($bit->[0] eq '*') { # it's like */3
#push @hunks, sprintf "every %s day of the week", freq($bit->[1]);
# the above was ambiguous -- "every third day of the week"
# sounds synonymous with just "3"
if($bit->[1] eq 2) {
# common and unambiguous case.
push @hunks, "every other day of the week";
} else {
# rare cases: N > 2
push @hunks, "every $bit->[1] days of the week";
# sounds clunky, but it's a clunky concept
}
} else {
# it's like "7-9"
push @hunks, sprintf "%s through %s",
$num2dow_long{$bit->[0]} || "DOW_$bit->[0]??",
$num2dow_long{$bit->[1]} || "DOW_$bit->[1]??",
}
} elsif(@$bit == 3) { # "7-20/2"
push @hunks, sprintf "every %s %s from %s through %s",
ordinate_soft($bit->[2]), #$bit->[2],
'day', #$bit->[2] == 1 ? 'days' : 'days',
$num2dow_long{$bit->[0]} || "DOW_$bit->[0]??",
$num2dow_long{$bit->[1]} || "DOW_$bit->[1]??",
}
}
$expression = conj_or(@hunks);
# Now figure where to put it...
#
if($time_lines[-2] eq "every day of") {
# Unrestricted day-of-month, hooray.
#
if($time_lines[-1] eq 'every month') {
# change it to "every Tuesday", killing the "of every month".
$time_lines[-2] = "every $expression";
$time_lines[-2] =~ s/every every /every /g;
pop @time_lines;
} else {
# change it to "every Tuesday in"
$time_lines[-2] = "every $expression in";
$time_lines[-2] =~ s/every every /every /g;
}
} else {
# This is the messy case where there's a DOM and DOW
# restriction
# Was, wrongly:
# $time_lines[-1] .= ',';
# push @time_lines, "if it's also " . $expression;
$time_lines[-2] .= " -- or every $expression in --";
# Yes, dashes look very strange, but then this is a very
# rare case.
$time_lines[-2] =~ s/every every /every /g;
}
}
#######################################################################
}
# TODO: change "3pm" -> "the 3pm hour" or something?
$time_lines[-1] =~ s/ of$//s;
return @time_lines;
}
###########################################################################
# Just utility routines below here.
my %pretty_form;
BEGIN {
%pretty_form = ( '"' => '\"', '\\' => '\\\\', );
}
sub esc {
my $x = $_[0];
$x =~
#s<([^\x20\x21\x23\x27-\x3F\x41-\x5B\x5D-\x7E])>
s<([\x00-\x1F"\\])>
<$pretty_form{$1} || '\\x'.(unpack("H2",$1))>eg;
return $x;
}
#--------------------------------------------------------------------------
# if($time_lines[-1] =~ m/every|from/) {
# $time_lines[-1] =~ tr/,/;/;
# s/ (and|or)\b/\; $1/g;
# }
sub conj_and {
if(grep m/every|from/, @_) {
# put in semicolons in the case of complex constituency
return join('; and ', @_) if @_ < 2;
my $last = pop @_;
return join('; ', @_) . '; and ' . $last;
}
return join(' and ', @_) if @_ < 3;
my $last = pop @_;
return join(', ', @_) . ', and ' . $last;
}
sub conj_or {
if(grep m/every|from/, @_) {
# put in semicolons in the case of complex constituency
return join('; or ', @_) if @_ < 2;
my $last = pop @_;
return join('; ', @_) . '; or ' . $last;
}
return join(' or ', @_) if @_ < 3;
my $last = pop @_;
return join(', ', @_) . ', or ' . $last;
}
#--------------------------------------------------------------------------
my %ordinations;
BEGIN {
# English-language overrides for common ordinals
%ordinations = qw(
1 first 2 second 3 third 4 fourth 5 fifth 6 sixth
7 seventh 8 eighth 9 ninth 10 tenth
);
# 11 eleventh 12 twelfth
# 13 thirteenth 14 fourteen 15 fifteenth 16 sixteenth
# 17 seventeenth 18 eighteenth 19 nineteenth 20 twentieth
}
sub ordsuf {
return 'th' if not(defined($_[0])) or not( 0 + $_[0] );
# 'th' for undef, 0, or anything non-number.
my $n = abs($_[0]); # Throw away the sign.
return 'th' unless $n == int($n); # Best possible, I guess.
$n %= 100;
return 'th' if $n == 11 or $n == 12 or $n == 13;
$n %= 10;
return 'st' if $n == 1;
return 'nd' if $n == 2;
return 'rd' if $n == 3;
return 'th';
}
sub ordinate {
my $i = $_[0] || 0;
$ordinations{$i} || ($i . ordsuf($i));
}
sub freq {
# frequentive form. Like ordinal, except that 2 -> 'other'
# (as in every other)
my $i = $_[0] || 0;
return 'other' if $i == 2; # special case
$ordinations{$i} || ($i . ordsuf($i));
}
sub ordinate_soft {
my $i = $_[0] || 0;
$i . ordsuf($i);
}
#--------------------------------------------------------------------------
sub percent_proc {
# Translated literally from the C, cron/do_command.c.
my($esc,$need_newline);
my $out = '';
my $c;
for(my $i = 0; $i < length($_[0]); $i++) {
$c = substr($_[0],$i,1);
if($esc) {
$out .= "\\" unless $c eq '%';
} else {
$c = "\n" if $c eq '%';
}
unless($esc = ($c eq "\\")) {
# For unescaped characters,
$out .= $c;
$need_newline = ($c ne "\n");
}
}
$out .= "\\" if $esc;
$out .= "\n" if $need_newline;
return $out;
# I think this would do the same thing:
# $x =~ s/((?:\\\\)+) | (\\\%) | (\%) /$3 ? "\n" : $2 ? '%' : $1/xeg;
# But I don't want to think about it, and I need it to work just
# as the original does.
}
#--------------------------------------------------------------------------
__END__
# Test data for crontab parsing:
MAILTO=hulahoops@polygon.int
10 8 15 * * /bin/csh -c 'perl ~/thang.pl | mail -s hujambo root'
40 5 * * * df -k
50 6 * * 1 ls -l /tmp
1 2 * apr mOn foo
foo=bar
foo=bar
foo = "bar"
foo="bar"
foo = 'bar'
foo='bar'
1 2 3 4 7 Foo
1-20/3 * * * * Foo
1,2,3 * * * * Foo
1-9,15-30 * * * * Foo
1-9/3,15-30/4 * * * * Foo
1 2 3 jan mon stuff
1 2 3 4 mON stuff
1 2 3 jan 5 stuff
@reboot xxstartuply foo
@yearly xxx yearlijk%thig%hooboy
@annually xxannuallijk heehoo
@monthly xxx monthlijk
@weekly xXxX weeklijk
@daily xxxdaylijk
@midnight xxxmidnightlijk
@hourly xXxXhourlijk
*/3 * * * * stuff
#End, really.
|