Replaced posix regexp strings by sre expressions.

This commit is contained in:
mainzelm 2002-11-27 09:36:21 +00:00
parent 2442e88ab7
commit cbb4609c3a
1 changed files with 50 additions and 49 deletions

View File

@ -264,7 +264,8 @@
(if *debug* (display "name->octets\n"))
(let loop ((s s))
(cond
((regexp-search (posix-string->regexp "^([^.]*)\\.(.*)") s)
((regexp-search (rx (: bos (submatch (* (~ "."))) "." (submatch (* any))))
s)
=> (lambda (match)
(append
(encode-portion (match:substring match 1))
@ -276,6 +277,7 @@
(encode-portion s)
(list *nul*)))))))
;; for tcp: message has to be tagged with its length
(define (add-size-tag m)
(if *debug* (display "add-size-tag\n"))
@ -322,17 +324,25 @@
(char->ascii (list-ref s 2))
(char->ascii (list-ref s 3))))
(define ip-string-regexp (rx (: bos
(submatch (** 1 3 digit)) "."
(submatch (** 1 3 digit)) "."
(submatch (** 1 3 digit)) "."
(submatch (** 1 3 digit))
eos)))
;; converts an ip-string to octets
(define (string->octet-ip s)
(if *debug* (display "string->octet-ip\n"))
(let loop ((s s)
(result '()))
(cond
((regexp-search (posix-string->regexp "^([^.]*)\\.(.*)") s)
((regexp-search ip-string-regexp s)
=> (lambda (match)
(loop (match:substring match 2) (append result (list (ascii->char (string->number (match:substring match 1))))))))
(list
(ascii->char (string->number (match:substring match 1)))
(ascii->char (string->number (match:substring match 2)))
(ascii->char (string->number (match:substring match 3)))
(ascii->char (string->number (match:substring match 4))))))
(else
(append result (list (ascii->char (string->number s))))))))
(error "invalid ip-string" s))))
;; calculates a "random" number, needed for message-ids
(define random
@ -344,23 +354,20 @@
;; checks if a string is a ip
(define (ip-string? s)
(if *debug* (display "ip-string->in-addr\n"))
(let loop ((s s)
(count 0))
(cond
((regexp-search (posix-string->regexp "^([^.]*)\\.(.*)") s)
=> (lambda (match)
(let* ((portion (match:substring match 1))
(number (string->number portion)))
(if (and number (< number 256))
(loop (match:substring match 2) (+ count 1))
#f))))
(else
(let ((number (string->number s)))
(define (byte-as-string? string)
(let ((number (string->number string)))
(and number
(< number 256)
(= count 3)
#t))))))
(>= number 0)
(< number 256))))
(cond
((regexp-search ip-string-regexp s)
=> (lambda (match)
(and (byte-as-string? (match:substring match 1))
(byte-as-string? (match:substring match 2))
(byte-as-string? (match:substring match 3))
(byte-as-string? (match:substring match 4)))))
(else #f)))
;; checks if v is a address32
(define (address32? v)
@ -369,24 +376,16 @@
;; returns a in-addr.arpa name-string or #f (needed to resolve hostname by ip)
(define (ip-string->in-addr s)
(if *debug* (display "ip-string->in-addr\n"))
(let loop ((s s)
(count 0)
(result ""))
(cond
((regexp-search (posix-string->regexp "^([^.]*)\\.(.*)") s)
((regexp-search ip-string-regexp s)
=> (lambda (match)
(let* ((portion (match:substring match 1))
(number (string->number portion)))
(if (and number (< number 256))
(loop (match:substring match 2) (+ count 1) (string-append portion "." result))
#f))))
(else
(let ((number (string->number s)))
(and number
(< number 256)
(= count 3)
(string-append s "." result "in-addr.arpa")))))))
(string-append
(match:substring match 4) "."
(match:substring match 3) "."
(match:substring match 2) "."
(match:substring match 1) "."
"in-addr.arpa")))
(else #f)))
;; filters types in a list of rrs
(define (filter-type list type)
@ -1046,8 +1045,10 @@
(dns-error 'no-nameservers)
ns))
((regexp-search
(posix-string->regexp
"nameserver[ ]+([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+)") l)
(rx (: "nameserver" (+ (| " " "\t")
(submatch (** 1 3 digit)
(= 3 (: "." (** 1 3 digit)))))))
l)
=> (lambda (match)
(loop (append ns (list (ip-string->address32 (match:substring match 1)))))))
(else