Skip to content

Instantly share code, notes, and snippets.

@yamasushi
Created July 22, 2012 06:41
Show Gist options
  • Save yamasushi/3158712 to your computer and use it in GitHub Desktop.
Save yamasushi/3158712 to your computer and use it in GitHub Desktop.
Gaucheマニュアルのgrepサンプルを改造してみる。
#!/usr/bin/env gosh
(use util.match)
; マニュアルからコピー
(define (usage)
(format (current-error-port)
"Usage: ~a regexp file ...\n" *program-name*)
(exit 2))
(define (grep rx port)
(with-input-from-port port
(lambda ()
(port-for-each
(lambda (line)
(when (rxmatch rx line)
(format #t "~a:~a: ~a\n"
(port-name port)
(- (port-current-line port) 1)
line)))
read-line))))
(define (call-with-<> files proc :optional (reader #f))
(cond
[ (input-port? files)
(if reader
(port-for-each proc (cut reader files) )
(proc files) ) ]
[ (string? files)
(call-with-input-file files (cut call-with-<> <> proc reader) ) ]
[ (list? files)
(for-each (cut call-with-<> <> proc reader) files) ]
[else (error "error")]
))
(define (main args)
(if (null? (cdr args))
(usage)
(let ((rx (string->regexp (cadr args))))
(call-with-<>
(match (cddr args)
[() (current-input-port)] [x x] )
(cut grep rx <>))))
0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment