2 #-------------------------------------------------------------------------
5 # perl script which generates .bki files from specially formatted .h
6 # files. These .bki files are used to initialize the postgres template
9 # Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
10 # Portions Copyright (c) 1994, Regents of the University of California
16 #-------------------------------------------------------------------------
24 our (@ISA, @EXPORT_OK);
26 @EXPORT_OK = qw(genbki);
33 $version =~ /^(\d+\.\d+)/ || die "Bad format verison $version\n";
34 my $majorversion = $1;
36 my $pgauthid = read_file
("src/include/catalog/pg_authid.h");
37 $pgauthid =~ /^#define\s+BOOTSTRAP_SUPERUSERID\s+(\d+)$/mg
38 || die "Could not read BOOTSTRAUP_SUPERUSERID from pg_authid.h\n";
39 my $bootstrapsuperuserid = $1;
41 my $pgnamespace = read_file
("src/include/catalog/pg_namespace.h");
42 $pgnamespace =~ /^#define\s+PG_CATALOG_NAMESPACE\s+(\d+)$/mg
43 || die "Could not read PG_CATALOG_NAMESPACE from pg_namespace.h\n";
44 my $pgcatalognamespace = $1;
52 $indata .= read_file
($f);
56 # Strip C comments, from perl FAQ 4.27
57 $indata =~ s{/\*.*?\*/}{}gs;
59 $indata =~ s{;\s*$}{}gm;
60 $indata =~ s{^\s+}{}gm;
61 $indata =~ s{^Oid}{oid}gm;
62 $indata =~ s{\(Oid}{(oid}gm;
63 $indata =~ s{^NameData}{name}gm;
64 $indata =~ s{\(NameData}{(name}g;
65 $indata =~ s{^TransactionId}{xid}gm;
66 $indata =~ s{\(TransactionId}{(xid}g;
67 $indata =~ s{PGUID}{$bootstrapsuperuserid}g;
68 $indata =~ s{PGNSP}{$pgcatalognamespace}g;
80 my $shared_relation = "";
81 my $without_oids = "";
87 foreach my $line (split /\n/, $indata)
89 if ($line =~ /^DATA\((.*)\)$/m)
92 my @fields = split /\s+/,$data;
93 if ($#fields >=4 && $fields[0] eq "insert" && $fields[1] eq "OID" && $fields[2] eq "=")
101 $data =~ s/\s{2,}/ /g;
102 $bki .= $data . "\n";
104 elsif ($line =~ /^DESCR\("(.*)"\)$/m)
108 $desc .= sprintf("%d\t%s\t0\t%s\n", $oid, $catalog, $1);
111 elsif ($line =~ /^SHDESCR\("(.*)"\)$/m)
115 $shdesc .= sprintf("%d\t%s\t%s\n", $oid, $catalog, $1);
118 elsif ($line =~ /^DECLARE_(UNIQUE_)?INDEX\((.*)\)$/m)
122 $bki .= "close $catalog\n";
125 my $u = $1?
" unique":"";
126 my @fields = split /,/,$2,3;
127 $fields[2] =~ s/\s{2,}/ /g;
128 $bki .= "declare$u index $fields[0] $fields[1] $fields[2]\n";
130 elsif ($line =~ /^DECLARE_TOAST\((.*)\)$/m)
134 $bki .= "close $catalog\n";
137 my @fields = split /,/,$1;
138 $bki .= "declare toast $fields[1] $fields[2] on $fields[0]\n";
140 elsif ($line =~ /^BUILD_INDICES/)
142 $bki .= "build indices\n";
144 elsif ($line =~ /^CATALOG\((.*)\)(.*)$/m)
148 $bki .= "close $catalog\n";
152 my @fields = split /,/,$1;
153 $catalog = $fields[0];
155 $bootstrap=$shared_relation=$without_oids="";
156 if ($rest =~ /BKI_BOOTSTRAP/)
158 $bootstrap = "bootstrap ";
160 if ($rest =~ /BKI_SHARED_RELATION/)
162 $shared_relation = "shared_relation ";
164 if ($rest =~ /BKI_WITHOUT_OIDS/)
166 $without_oids = "without_oids ";
174 next if ($line =~ /{/);
179 $bki .= "create $bootstrap$shared_relation$without_oids$catalog $oid\n (\n";
181 for (my $i = 0; $i <= $#attr; $i++)
191 $bki .= " " . $attr[$i] . " = " . $types[$i];
198 if ($bootstrap eq "")
200 $bki .= "open $catalog\n";
205 # inside catalog definition, so keep sucking up attributes
206 my @fields = split /\s+/,$line;
207 if ($fields[1] =~ /(.*)\[.*\]/)
210 push @types, $fields[0] . '[]';
214 push @attr, $fields[1];
215 push @types, $fields[0];
222 $bki .= "close $catalog\n";
225 open(O
,">$prefix.bki") || die "Could not write $prefix.bki\n";
226 print O
"# PostgreSQL $majorversion\n";
229 open(O
,">$prefix.description") || die "Could not write $prefix.description\n";
232 open(O
,">$prefix.shdescription") || die "Could not write $prefix.shdescription\n";
239 my $filename = shift;
244 open($F, $filename) || die "Could not open file $filename\n";