Put DPC's mkcdb script (with tabs removed) into the util directory, and
[exim.git] / src / util / mkcdb.pl
1 #!/usr/bin/perl -wT
2 # $Cambridge: exim/src/util/mkcdb.pl,v 1.1 2005/05/24 11:20:40 ph10 Exp $
3 #
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
8 # target.tmp
9 #
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
14 #
15 # Cambridge: hermes/src/admin/mkcdb,v 1.9 2005/02/15 18:14:12 fanf2 Exp
16
17 use strict;
18
19 $ENV{'PATH'} = "";
20 umask(022);
21
22 my $CDB = '/opt/cdb/bin/cdbmake';
23
24 my $prog = $0;
25 $prog =~ s|(.*/)?([^/]+)|$2|;
26
27 my $source;
28 my $target;
29 if (@ARGV == 1) {
30 $source = shift(@ARGV);
31 $target = $source;
32 } elsif (@ARGV == 2) {
33 $source = shift(@ARGV);
34 $target = shift(@ARGV);
35 } else {
36 die("$prog: usage: <source> [<target>]\n");
37 }
38 # trust the invoker ?!
39 $source =~ /(.*)/;
40 $source = $1;
41 $target =~ /(.*)/;
42 $target = $1;
43
44 open(SOURCE, "< ${source}")
45 or die("$prog: open < $source: $!\n");
46
47 open(PIPE, "| $CDB $target.cdb $target.tmp")
48 or die("$prog: open | $CDB $target: $!\n");
49
50 sub add_item ($$) {
51 my $key = shift;
52 my $val = shift;
53 printf PIPE ("+%d,%d:%s->%s\n", length($key), length($val), $key, $val);
54 }
55
56 sub add_line ($) {
57 my $line = shift;
58 if ($line =~ /^([^\s:]+)\s*:\s*(.*)$/s) { # key : values
59 add_item($1,$2);
60 return;
61 }
62 if ($line =~ /^(\S+)\s+(.*)$/s) { # key: values
63 add_item($1,$2);
64 return;
65 }
66 if ($line =~ /^(\S+)$/s) { # key (empty value)
67 add_item($1,'');
68 return;
69 }
70 warn "$prog: unrecognized item: $line";
71 }
72
73 my $data;
74 while(<SOURCE>) {
75 next if /^#/ or /^\s*$/;
76 m/^(\s*)(\S.*)\s+$/s;
77 if (length($1) == 0) {
78 add_line($data) if defined $data;
79 $data = $2;
80 } else {
81 $data .= " $2";
82 }
83 }
84 add_line($data) if defined $data;
85 print PIPE "\n";
86
87 close(SOURCE)
88 or die("$prog: close < $source: $!\n");
89 close(PIPE)
90 or die($! ? "$prog: close | $CDB $target: $!\n"
91 : "$prog: close | $CDB $target: exited $?\n");
92
93 exit 0;