]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/crontab/crontab.factor
factor: trim using lists
[factor.git] / extra / crontab / crontab.factor
index 2ea27a821e39859e572dbf2013c679598c0091ff..402ec1073bb957011a13dffa852c4a66da57f913 100644 (file)
@@ -2,11 +2,13 @@
 ! See http://factorcode.org/license.txt for BSD license
 
 USING: accessors arrays ascii assocs calendar calendar.english
-calendar.private combinators io kernel literals locals math
-math.order math.parser math.ranges sequences splitting ;
+calendar.private combinators combinators.short-circuit io kernel
+literals math math.order math.parser ranges sequences splitting ;
 
 IN: crontab
 
+ERROR: invalid-cronentry value ;
+
 :: parse-value ( value quot: ( value -- value' ) seq -- value )
     value {
         { [ CHAR: , over member? ] [
@@ -16,7 +18,7 @@ IN: crontab
             "/" split1 [ quot seq parse-value 0 over length 1 - ] dip
             string>number <range> swap nths ] }
         { [ CHAR: - over member? ] [
-            "-" split1 quot bi@ [a,b] ] }
+            "-" split1 quot bi@ [a..b] ] }
         [ quot call 1array ]
     } cond ; inline recursive
 
@@ -42,6 +44,19 @@ CONSTANT: aliases H{
     { "@hourly"   "0 * * * *" }
 }
 
+: check-cronentry ( cronentry -- cronentry )
+    dup {
+        [ days-of-week>> [ 0 6 between? ] all? ]
+        [ months>> [ 1 12 between? ] all? ]
+        [
+            [ days>> 1 ] [ months>> ] bi [
+                { 0 31 29 31 30 31 30 31 31 30 31 30 31 } nth
+            ] map supremum [ between? ] 2curry all?
+        ]
+        [ minutes>> [ 0 59 between? ] all? ]
+        [ hours>> [ 0 23 between? ] all? ]
+    } 1&& [ invalid-cronentry ] unless ;
+
 : parse-cronentry ( entry -- cronentry )
     " " split1 [ aliases ?at drop ] dip " " glue
     " " split1 " " split1 " " split1 " " split1 " " split1 {
@@ -51,7 +66,7 @@ CONSTANT: aliases H{
         [ [ parse-month ] T{ range f 1 12 1 } parse-value ]
         [ [ parse-day ] T{ range f 0 7 1 } parse-value ]
         [ ]
-    } spread cronentry boa ;
+    } spread cronentry boa check-cronentry ;
 
 <PRIVATE
 
@@ -117,4 +132,4 @@ PRIVATE>
     now next-times-after ;
 
 : read-crontab ( -- entries )
-    lines harvest [ parse-cronentry ] map ;
+    read-lines harvest [ parse-cronentry ] map ;