2 # $Cambridge: exim/src/util/mkcdb.pl,v 1.1 2005/05/24 11:20:40 ph10 Exp $
4 # Create cdb file from flat alias file. DPC: 15/10/98.
5 # Args: source (may be relative or absolute)
6 # target (may be relative or absolute. Default = source)
7 # Generates: target.cdb
10 # Little Perl script to convert flat file into CDB file. Two advantages over
11 # cdbmake-12 awk script that is distributed with CDB:
12 # 1) Handles 'dpc22:dpc22@hermes' as well as 'dpc22 dpc22@hermes'
13 # 2) Perl works with arbitary length strings: awk chokes at 1,024 chars
15 # Cambridge: hermes/src/admin/mkcdb,v 1.9 2005/02/15 18:14:12 fanf2 Exp
22 my $CDB = '/opt/cdb/bin/cdbmake';
25 $prog =~ s|(.*/)?([^/]+)|$2|;
30 $source = shift(@ARGV);
32 } elsif (@ARGV == 2) {
33 $source = shift(@ARGV);
34 $target = shift(@ARGV);
36 die("$prog: usage: <source> [<target>]\n");
38 # trust the invoker ?!
44 open(SOURCE, "< ${source}")
45 or die("$prog: open < $source: $!\n");
47 open(PIPE, "| $CDB $target.cdb $target.tmp")
48 or die("$prog: open | $CDB $target: $!\n");
53 printf PIPE ("+%d,%d:%s->%s\n", length($key), length($val), $key, $val);
58 if ($line =~ /^([^\s:]+)\s*:\s*(.*)$/s) { # key : values
62 if ($line =~ /^(\S+)\s+(.*)$/s) { # key: values
66 if ($line =~ /^(\S+)$/s) { # key (empty value)
70 warn "$prog: unrecognized item: $line";
75 next if /^#/ or /^\s*$/;
77 if (length($1) == 0) {
78 add_line($data) if defined $data;
84 add_line($data) if defined $data;
88 or die("$prog: close < $source: $!\n");
90 or die($! ? "$prog: close | $CDB $target: $!\n"
91 : "$prog: close | $CDB $target: exited $?\n");