2 # Copyright (c) The Exim Maintainers 2022
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 # Create cdb file from flat alias file. DPC: 15/10/98.
6 # Args: source (may be relative or absolute)
7 # target (may be relative or absolute. Default = source)
8 # Generates: target.cdb
11 # Little Perl script to convert flat file into CDB file. Two advantages over
12 # cdbmake-12 awk script that is distributed with CDB:
13 # 1) Handles 'dpc22:dpc22@hermes' as well as 'dpc22 dpc22@hermes'
14 # 2) Perl works with arbitrary length strings: awk chokes at 1,024 chars
16 # Cambridge: hermes/src/admin/mkcdb,v 1.9 2005/02/15 18:14:12 fanf2 Exp
20 BEGIN { pop @INC if $INC[-1] eq '.' };
24 my $CDB = '/opt/cdb/bin/cdbmake';
27 $prog =~ s|(.*/)?([^/]+)|$2|;
32 $source = shift(@ARGV);
34 } elsif (@ARGV == 2) {
35 $source = shift(@ARGV);
36 $target = shift(@ARGV);
38 die("$prog: usage: <source> [<target>]\n");
40 # trust the invoker ?!
46 open(SOURCE, "< ${source}")
47 or die("$prog: open < $source: $!\n");
49 open(PIPE, "| $CDB $target.cdb $target.tmp")
50 or die("$prog: open | $CDB $target: $!\n");
55 printf PIPE ("+%d,%d:%s->%s\n", length($key), length($val), $key, $val);
60 if ($line =~ /^([^\s:]+)\s*:\s*(.*)$/s) { # key : values
64 if ($line =~ /^(\S+)\s+(.*)$/s) { # key: values
68 if ($line =~ /^(\S+)$/s) { # key (empty value)
72 warn "$prog: unrecognized item: $line";
77 next if /^#/ or /^\s*$/;
79 if (length($1) == 0) {
80 add_line($data) if defined $data;
86 add_line($data) if defined $data;
90 or die("$prog: close < $source: $!\n");
92 or die($! ? "$prog: close | $CDB $target: $!\n"
93 : "$prog: close | $CDB $target: exited $?\n");