Commit | Line | Data |
---|---|---|
21d59bc5 | 1 | #!/usr/bin/perl -wT |
21d59bc5 PH |
2 | # |
3 | # Create cdb file from flat alias file. DPC: 15/10/98. | |
4 | # Args: source (may be relative or absolute) | |
5 | # target (may be relative or absolute. Default = source) | |
6 | # Generates: target.cdb | |
7 | # target.tmp | |
8 | # | |
9 | # Little Perl script to convert flat file into CDB file. Two advantages over | |
10 | # cdbmake-12 awk script that is distributed with CDB: | |
11 | # 1) Handles 'dpc22:dpc22@hermes' as well as 'dpc22 dpc22@hermes' | |
12 | # 2) Perl works with arbitary length strings: awk chokes at 1,024 chars | |
13 | # | |
14 | # Cambridge: hermes/src/admin/mkcdb,v 1.9 2005/02/15 18:14:12 fanf2 Exp | |
15 | ||
16 | use strict; | |
17 | ||
18 | $ENV{'PATH'} = ""; | |
19 | umask(022); | |
20 | ||
21 | my $CDB = '/opt/cdb/bin/cdbmake'; | |
22 | ||
23 | my $prog = $0; | |
24 | $prog =~ s|(.*/)?([^/]+)|$2|; | |
25 | ||
26 | my $source; | |
27 | my $target; | |
28 | if (@ARGV == 1) { | |
29 | $source = shift(@ARGV); | |
30 | $target = $source; | |
31 | } elsif (@ARGV == 2) { | |
32 | $source = shift(@ARGV); | |
33 | $target = shift(@ARGV); | |
34 | } else { | |
35 | die("$prog: usage: <source> [<target>]\n"); | |
36 | } | |
37 | # trust the invoker ?! | |
38 | $source =~ /(.*)/; | |
39 | $source = $1; | |
40 | $target =~ /(.*)/; | |
41 | $target = $1; | |
42 | ||
43 | open(SOURCE, "< ${source}") | |
44 | or die("$prog: open < $source: $!\n"); | |
45 | ||
46 | open(PIPE, "| $CDB $target.cdb $target.tmp") | |
47 | or die("$prog: open | $CDB $target: $!\n"); | |
48 | ||
49 | sub add_item ($$) { | |
50 | my $key = shift; | |
51 | my $val = shift; | |
52 | printf PIPE ("+%d,%d:%s->%s\n", length($key), length($val), $key, $val); | |
53 | } | |
54 | ||
55 | sub add_line ($) { | |
56 | my $line = shift; | |
57 | if ($line =~ /^([^\s:]+)\s*:\s*(.*)$/s) { # key : values | |
58 | add_item($1,$2); | |
59 | return; | |
60 | } | |
61 | if ($line =~ /^(\S+)\s+(.*)$/s) { # key: values | |
62 | add_item($1,$2); | |
63 | return; | |
64 | } | |
65 | if ($line =~ /^(\S+)$/s) { # key (empty value) | |
66 | add_item($1,''); | |
67 | return; | |
68 | } | |
69 | warn "$prog: unrecognized item: $line"; | |
70 | } | |
71 | ||
72 | my $data; | |
73 | while(<SOURCE>) { | |
74 | next if /^#/ or /^\s*$/; | |
75 | m/^(\s*)(\S.*)\s+$/s; | |
76 | if (length($1) == 0) { | |
77 | add_line($data) if defined $data; | |
78 | $data = $2; | |
79 | } else { | |
80 | $data .= " $2"; | |
81 | } | |
82 | } | |
83 | add_line($data) if defined $data; | |
84 | print PIPE "\n"; | |
85 | ||
86 | close(SOURCE) | |
87 | or die("$prog: close < $source: $!\n"); | |
88 | close(PIPE) | |
89 | or die($! ? "$prog: close | $CDB $target: $!\n" | |
90 | : "$prog: close | $CDB $target: exited $?\n"); | |
91 | ||
92 | exit 0; |