|
| 1 | +#!/usr/bin/perl |
| 2 | +#------------------------------------------------------------------------- |
| 3 | +# |
| 4 | +# genbki.pl-- |
| 5 | +# perl script which generates .bki files from specially formatted .h |
| 6 | +# files. These .bki files are used to initialize the postgres template |
| 7 | +# database. |
| 8 | +# |
| 9 | +# Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group |
| 10 | +# Portions Copyright (c) 1994, Regents of the University of California |
| 11 | +# |
| 12 | +# |
| 13 | +# IDENTIFICATION |
| 14 | +# $PostgreSQL: pgsql/src/tools/msvc/genbki.pl,v 1.1 2006/11/29 19:49:31 tgl Exp $ |
| 15 | +# |
| 16 | +#------------------------------------------------------------------------- |
| 17 | + |
| 18 | +use strict; |
| 19 | +use warnings; |
| 20 | + |
| 21 | +my $version = shift || Usage(); |
| 22 | +my $prefix = shift || Usage(); |
| 23 | + |
| 24 | +$version =~ /^(\d+\.\d+)/ || die "Bad format verison $version\n"; |
| 25 | +my $majorversion = $1; |
| 26 | + |
| 27 | +my $pgext = read_file("src/include/postgres_ext.h"); |
| 28 | +$pgext =~ /^#define\s+NAMEDATALEN\s+(\d+)$/mg || die "Could not read NAMEDATALEN from postgres_ext.h\n"; |
| 29 | +my $namedatalen = $1; |
| 30 | + |
| 31 | +my $pgauthid = read_file("src/include/catalog/pg_authid.h"); |
| 32 | +$pgauthid =~ /^#define\s+BOOTSTRAP_SUPERUSERID\s+(\d+)$/mg || die "Could not read BOOTSTRAUP_SUPERUSERID from pg_authid.h\n"; |
| 33 | +my $bootstrapsuperuserid = $1; |
| 34 | + |
| 35 | +my $pgnamespace = read_file("src/include/catalog/pg_namespace.h"); |
| 36 | +$pgnamespace =~ /^#define\s+PG_CATALOG_NAMESPACE\s+(\d+)$/mg || die "Could not read PG_CATALOG_NAMESPACE from pg_namespace.h\n"; |
| 37 | +my $pgcatalognamespace = $1; |
| 38 | + |
| 39 | +my $indata = ""; |
| 40 | +while (my $f = shift) { |
| 41 | + $indata .= read_file($f); |
| 42 | + $indata .= "\n"; |
| 43 | +} |
| 44 | + |
| 45 | +# Strip C comments, from perl FAQ 4.27 |
| 46 | +$indata =~ s{/\*.*?\*/}{}gs; |
| 47 | + |
| 48 | +$indata =~ s{;\s*$}{}gm; |
| 49 | +$indata =~ s{^\s+}{}gm; |
| 50 | +$indata =~ s{^Oid}{oid}gm; |
| 51 | +$indata =~ s{\(Oid}{(oid}gm; |
| 52 | +$indata =~ s{^NameData}{name}gm; |
| 53 | +$indata =~ s{\(NameData}{(name}g; |
| 54 | +$indata =~ s{^TransactionId}{xid}gm; |
| 55 | +$indata =~ s{\(TransactionId}{(xid}g; |
| 56 | +$indata =~ s{PGUID}{$bootstrapsuperuserid}g; |
| 57 | +$indata =~ s{NAMEDATALEN}{$namedatalen}g; |
| 58 | +$indata =~ s{PGNSP}{$pgcatalognamespace}g; |
| 59 | + |
| 60 | +#print $indata; |
| 61 | + |
| 62 | +my $bki = ""; |
| 63 | +my $desc = ""; |
| 64 | +my $shdesc = ""; |
| 65 | + |
| 66 | +my $oid = 0; |
| 67 | +my $catalog = 0; |
| 68 | +my $reln_open = 0; |
| 69 | +my $bootstrap = ""; |
| 70 | +my $shared_relation = ""; |
| 71 | +my $without_oids = ""; |
| 72 | +my $nc = 0; |
| 73 | +my $inside = 0; |
| 74 | +my @attr; |
| 75 | +my @types; |
| 76 | +foreach my $line (split /\n/, $indata) { |
| 77 | + if ($line =~ /^DATA\((.*)\)$/m) { |
| 78 | + my $data = $1; |
| 79 | + my @fields = split /\s+/,$data; |
| 80 | + if ($#fields >=4 && $fields[0] eq "insert" && $fields[1] eq "OID" && $fields[2] eq "=") { |
| 81 | + $oid = $fields[3]; |
| 82 | + } |
| 83 | + $data =~ s/\s{2,}/ /g; |
| 84 | + $bki .= $data . "\n"; |
| 85 | + } |
| 86 | + elsif ($line =~ /^DESCR\("(.*)"\)$/m){ |
| 87 | + if ($oid != 0) { |
| 88 | + $desc .= sprintf("%d\t%s\t0\t%s\n", $oid, $catalog, $1); |
| 89 | + } |
| 90 | + } |
| 91 | + elsif ($line =~ /^SHDESCR\("(.*)"\)$/m) { |
| 92 | + if ($oid != 0) { |
| 93 | + $shdesc .= sprintf("%d\t%s\t%s\n", $oid, $catalog, $1); |
| 94 | + } |
| 95 | + } |
| 96 | + elsif ($line =~ /^DECLARE_(UNIQUE_)?INDEX\((.*)\)$/m) { |
| 97 | + if ($reln_open) { |
| 98 | + $bki .= "close $catalog\n"; |
| 99 | + $reln_open = 0; |
| 100 | + } |
| 101 | + my $u = $1?" unique":""; |
| 102 | + my @fields = split /,/,$2,3; |
| 103 | + $fields[2] =~ s/\s{2,}/ /g; |
| 104 | + $bki .= "declare$u index $fields[0] $fields[1] $fields[2]\n"; |
| 105 | + } |
| 106 | + elsif ($line =~ /^DECLARE_TOAST\((.*)\)$/m) { |
| 107 | + if ($reln_open) { |
| 108 | + $bki .= "close $catalog\n"; |
| 109 | + $reln_open = 0; |
| 110 | + } |
| 111 | + my @fields = split /,/,$1; |
| 112 | + $bki .= "declare toast $fields[1] $fields[2] on $fields[0]\n"; |
| 113 | + } |
| 114 | + elsif ($line =~ /^BUILD_INDICES/) { |
| 115 | + $bki .= "build indices\n"; |
| 116 | + } |
| 117 | + elsif ($line =~ /^CATALOG\((.*)\)(.*)$/m) { |
| 118 | + if ($reln_open) { |
| 119 | + $bki .= "close $catalog\n"; |
| 120 | + $reln_open = 0; |
| 121 | + } |
| 122 | + my $rest = $2; |
| 123 | + my @fields = split /,/,$1; |
| 124 | + $catalog = $fields[0]; |
| 125 | + $oid = $fields[1]; |
| 126 | + $bootstrap=$shared_relation=$without_oids=""; |
| 127 | + if ($rest =~ /BKI_BOOTSTRAP/) { |
| 128 | + $bootstrap = "bootstrap "; |
| 129 | + } |
| 130 | + if ($rest =~ /BKI_SHARED_RELATION/) { |
| 131 | + $shared_relation = "shared_relation "; |
| 132 | + } |
| 133 | + if ($rest =~ /BKI_WITHOUT_OIDS/) { |
| 134 | + $without_oids = "without_oids "; |
| 135 | + } |
| 136 | + $nc++; |
| 137 | + $inside = 1; |
| 138 | + next; |
| 139 | + } |
| 140 | + if ($inside==1) { |
| 141 | + next if ($line =~ /{/); |
| 142 | + if ($line =~ /}/) { |
| 143 | +# Last line |
| 144 | + $bki .= "create $bootstrap$shared_relation$without_oids$catalog $oid\n (\n"; |
| 145 | + my $first = 1; |
| 146 | + for (my $i = 0; $i <= $#attr; $i++) { |
| 147 | + if ($first == 1) { |
| 148 | + $first = 0; |
| 149 | + } else { |
| 150 | + $bki .= ",\n"; |
| 151 | + } |
| 152 | + $bki .= " " . $attr[$i] . " = " . $types[$i]; |
| 153 | + } |
| 154 | + $bki .= "\n )\n"; |
| 155 | + undef(@attr); |
| 156 | + undef(@types); |
| 157 | + $reln_open = 1; |
| 158 | + $inside = 0; |
| 159 | + if ($bootstrap eq "") { |
| 160 | + $bki .= "open $catalog\n"; |
| 161 | + } |
| 162 | + next; |
| 163 | + } |
| 164 | +# inside catalog definition, so keep sucking up attributes |
| 165 | + my @fields = split /\s+/,$line; |
| 166 | + if ($fields[1] =~ /(.*)\[.*\]/) { #Array attribute |
| 167 | + push @attr, $1; |
| 168 | + push @types, $fields[0] . '[]'; |
| 169 | + } |
| 170 | + else { |
| 171 | + push @attr, $fields[1]; |
| 172 | + push @types, $fields[0]; |
| 173 | + } |
| 174 | + next; |
| 175 | + } |
| 176 | +} |
| 177 | +if ($reln_open == 1) { |
| 178 | + $bki .= "close $catalog\n"; |
| 179 | +} |
| 180 | + |
| 181 | +open(O,">$prefix.bki") || die "Could not write $prefix.bki\n"; |
| 182 | +print O "# PostgreSQL $majorversion\n"; |
| 183 | +print O $bki; |
| 184 | +close(O); |
| 185 | +open(O,">$prefix.description") || die "Could not write $prefix.description\n"; |
| 186 | +print O $desc; |
| 187 | +close(O); |
| 188 | +open(O,">$prefix.shdescription") || die "Could not write $prefix.shdescription\n"; |
| 189 | +print O $shdesc; |
| 190 | +close(O); |
| 191 | + |
| 192 | +sub Usage { |
| 193 | + print "Usage: genbki.pl <version> <prefix> <input1> [<input2> <input3>...]\n"; |
| 194 | + exit(1); |
| 195 | +} |
| 196 | + |
| 197 | +sub read_file { |
| 198 | + my $filename = shift; |
| 199 | + my $F; |
| 200 | + my $t = $/; |
| 201 | + |
| 202 | + undef $/; |
| 203 | + open($F, $filename) || die "Could not open file $filename\n"; |
| 204 | + my $txt = <$F>; |
| 205 | + close($F); |
| 206 | + $/ = $t; |
| 207 | + |
| 208 | + return $txt; |
| 209 | +} |
| 210 | + |
0 commit comments