| 1 |
;; -*- mode:scheme -*- |
| 2 |
;; |
| 3 |
;; A script to restore dbm content |
| 4 |
;; |
| 5 |
;; Usage: gosh dbm/restore [-i <infile>][-t <type>] <dbmname> |
| 6 |
;; |
| 7 |
|
| 8 |
(use gauche.parseopt) |
| 9 |
(use dbm) |
| 10 |
(use util.match) |
| 11 |
(use file.filter) |
| 12 |
|
| 13 |
(define (main args) |
| 14 |
(let-args (cdr args) ([ifile "i=s" #f] |
| 15 |
[type "t=y" 'gdbm] |
| 16 |
[else _ (usage)] |
| 17 |
. args) |
| 18 |
(let1 class (dbm-type->class type) |
| 19 |
(unless class (exit 1 "dbm type `~a' unknown" type)) |
| 20 |
(match args |
| 21 |
[(dbmname) (do-dump dbmname class (or ifile (current-input-port)))] |
| 22 |
[else (usage)])) |
| 23 |
0)) |
| 24 |
|
| 25 |
(define (usage) |
| 26 |
(print "Usage: gosh dbm/restore [-i infile][-t type] dbmname") |
| 27 |
(exit 0)) |
| 28 |
|
| 29 |
(define (do-dump name class input) |
| 30 |
(let1 dbm (guard (e [else (exit 1 "couldn't create dbm database: ~a" |
| 31 |
(~ e'message))]) |
| 32 |
(dbm-open class :path name :rw-mode :create)) |
| 33 |
(file-filter |
| 34 |
(^(in out) |
| 35 |
(port-for-each |
| 36 |
(^p (if (and (pair? p) |
| 37 |
(string? (car p)) |
| 38 |
(string? (cdr p))) |
| 39 |
(dbm-put! dbm (car p) (cdr p)) |
| 40 |
(warn "invalid entry in input ignored: ~,,,,65s" p))) |
| 41 |
(cut read in))) |
| 42 |
:input input) |
| 43 |
(dbm-close dbm))) |