frontend.lisp: Allow command-line overrides of timeout parameters. master
authorMark Wooding <mdw@distorted.org.uk>
Sun, 28 Apr 2024 11:13:18 +0000 (12:13 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 28 Apr 2024 11:13:18 +0000 (12:13 +0100)
frontend.lisp

index e170731..661b80a 100644 (file)
@@ -22,7 +22,7 @@
 ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
 (defpackage #:zone.frontend
-  (:use #:common-lisp #:mdw.sys-base #:optparse #:net #:zone
+  (:use #:common-lisp #:mdw.base #:mdw.sys-base #:optparse #:net #:zone
        #+(or cmu clisp) #:mop
        #+sbcl #:sb-mop)
   (:export #:main))
       (if (and path
               (not (pathname-name path)))
          (setf var path)
-         (option-parse-error "path `~A' doesn't name a directory." arg)))))
+         (option-parse-error "path `~A' doesn't name a directory." arg))))
+  (let ((duration-units (make-hash-table :test #'equal)))
+    (dolist (item '((("Gs") #.(* 1000 1000 1000))
+                   (("Ms") #.(* 1000 1000))
+                   (("ks") 1000)
+                   (("hs") 100)
+                   (("das") 10)
+                   (("yr" "year" "years" "y") #.(* 365 24 60 60))
+                   (("wk" "week" "weeks" "w") #.(* 7 24 60 60))
+                   (("day" "days" "dy" "d") #.(* 24 60 60))
+                   (("hr" "hour" "hours" "h") #.(* 60 60))
+                   (("min" "minute" "minutes" "m") 60)
+                   (("s" "second" "seconds" "sec" "") 1)))
+      (dolist (name (car item))
+       (setf (gethash name duration-units) (cadr item))))
+    (defopthandler dur (var arg) ()
+      (let ((len (length arg)))
+         (multiple-value-bind (n i) (parse-integer arg :junk-allowed t)
+           (unless n
+             (option-parse-error "invalid duration `~A': ~
+                                  integer expected" arg))
+           (loop (cond ((or (>= i len)
+                            (not (whitespace-char-p (char arg i))))
+                        (return))
+                       (t
+                        (incf i))))
+           (let ((u0 i))
+             (loop (cond ((or (>= i len)
+                              (whitespace-char-p (char arg i)))
+                          (return))
+                         (t
+                          (incf i))))
+             (let* ((u1 i)
+                    (unit (subseq arg u0 u1))
+                    (scale (gethash unit duration-units)))
+               (unless scale
+                 (option-parse-error "invalid duration `~A': ~
+                                      unknown unit `~A'"
+                                     arg unit))
+               (setf var (* n scale)))))))))
 
 (define-program
     :version "1.0.0" :usage "ZONEDEF..."
                           "Designate NET as a preferred subnet.")
                      (#\D "debug" (set opt-debug)
                           "Produce stack backtrace on error.")
+                     "Timeout options"
+                     (#\E "expire" (:arg "DURATION")
+                          (dur *default-zone-expire*)
+                          "Set default zone expiry period.")
+                     (#\N "min-ttl" (:arg "DURATION")
+                          (dur *default-zone-min-ttl*)
+                          "Set default zone minimum/negative time-to-live.")
+                     (#\R "refresh" (:arg "DURATION")
+                          (dur *default-zone-refresh*)
+                          "Set default zone refresh period.")
+                     (#\T "ttl" (:arg "DURATION")
+                          (dur *default-zone-ttl*)
+                          "Set default zone time-to-live.")
+                     (#\Y "retry" (:arg "DURATION")
+                          (dur *default-zone-retry*)
+                          "Set default zone retry period.")
                      "Output options"
                      (#\d "directory" (:arg "DIRECTORY")
                           (dir *zone-output-path*)