|
| 1 | +#!/usr/bin/perl |
| 2 | +#------------------------------------------------------------------------- |
| 3 | +# |
| 4 | +# Genbki.pm -- |
| 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-2007, 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.pm,v 1.1 2007/03/17 13:50:42 mha Exp $ |
| 15 | +# |
| 16 | +#------------------------------------------------------------------------- |
| 17 | + |
| 18 | +packageGenbki; |
| 19 | + |
| 20 | +use strict; |
| 21 | +use warnings; |
| 22 | + |
| 23 | +use Exporter; |
| 24 | +our (@ISA,@EXPORT_OK); |
| 25 | +@ISA =qw(Exporter); |
| 26 | +@EXPORT_OK =qw(genbki); |
| 27 | + |
| 28 | +subgenbki |
| 29 | +{ |
| 30 | +my$version =shift; |
| 31 | +my$prefix =shift; |
| 32 | + |
| 33 | +$version =~/^(\d+\.\d+)/ ||die"Bad format verison$version\n"; |
| 34 | +my$majorversion =$1; |
| 35 | + |
| 36 | +my$pgext = read_file("src/include/pg_config_manual.h"); |
| 37 | +$pgext =~/^#define\s+NAMEDATALEN\s+(\d+)$/mg |
| 38 | + ||die"Could not read NAMEDATALEN from pg_config_manual.h\n"; |
| 39 | +my$namedatalen =$1; |
| 40 | + |
| 41 | +my$pgauthid = read_file("src/include/catalog/pg_authid.h"); |
| 42 | +$pgauthid =~/^#define\s+BOOTSTRAP_SUPERUSERID\s+(\d+)$/mg |
| 43 | + ||die"Could not read BOOTSTRAUP_SUPERUSERID from pg_authid.h\n"; |
| 44 | +my$bootstrapsuperuserid =$1; |
| 45 | + |
| 46 | +my$pgnamespace = read_file("src/include/catalog/pg_namespace.h"); |
| 47 | +$pgnamespace =~/^#define\s+PG_CATALOG_NAMESPACE\s+(\d+)$/mg |
| 48 | + ||die"Could not read PG_CATALOG_NAMESPACE from pg_namespace.h\n"; |
| 49 | +my$pgcatalognamespace =$1; |
| 50 | + |
| 51 | +my$indata =""; |
| 52 | + |
| 53 | +while (@_) |
| 54 | + { |
| 55 | +my$f =shift; |
| 56 | +nextunless$f; |
| 57 | +$indata .= read_file($f); |
| 58 | +$indata .="\n"; |
| 59 | + } |
| 60 | + |
| 61 | +# Strip C comments, from perl FAQ 4.27 |
| 62 | +$indata =~s{/\*.*?\*/}{}gs; |
| 63 | + |
| 64 | +$indata =~s{;\s*$}{}gm; |
| 65 | +$indata =~s{^\s+}{}gm; |
| 66 | +$indata =~s{^Oid}{oid}gm; |
| 67 | +$indata =~s{\(Oid}{(oid}gm; |
| 68 | +$indata =~s{^NameData}{name}gm; |
| 69 | +$indata =~s{\(NameData}{(name}g; |
| 70 | +$indata =~s{^TransactionId}{xid}gm; |
| 71 | +$indata =~s{\(TransactionId}{(xid}g; |
| 72 | +$indata =~s{PGUID}{$bootstrapsuperuserid}g; |
| 73 | +$indata =~s{NAMEDATALEN}{$namedatalen}g; |
| 74 | +$indata =~s{PGNSP}{$pgcatalognamespace}g; |
| 75 | + |
| 76 | +#print $indata; |
| 77 | + |
| 78 | +my$bki =""; |
| 79 | +my$desc =""; |
| 80 | +my$shdesc =""; |
| 81 | + |
| 82 | +my$oid = 0; |
| 83 | +my$catalog = 0; |
| 84 | +my$reln_open = 0; |
| 85 | +my$bootstrap =""; |
| 86 | +my$shared_relation =""; |
| 87 | +my$without_oids =""; |
| 88 | +my$nc = 0; |
| 89 | +my$inside = 0; |
| 90 | +my@attr; |
| 91 | +my@types; |
| 92 | + |
| 93 | +foreachmy$line (split /\n/,$indata) |
| 94 | + { |
| 95 | +if ($line =~/^DATA\((.*)\)$/m) |
| 96 | + { |
| 97 | +my$data =$1; |
| 98 | +my@fields =split /\s+/,$data; |
| 99 | +if ($#fields >=4 &&$fields[0]eq"insert" &&$fields[1]eq"OID" &&$fields[2]eq"=") |
| 100 | + { |
| 101 | +$oid =$fields[3]; |
| 102 | + } |
| 103 | +else |
| 104 | + { |
| 105 | +$oid = 0; |
| 106 | + } |
| 107 | +$data =~s/\s{2,}//g; |
| 108 | +$bki .=$data ."\n"; |
| 109 | + } |
| 110 | +elsif ($line =~/^DESCR\("(.*)"\)$/m) |
| 111 | + { |
| 112 | +if ($oid != 0) |
| 113 | + { |
| 114 | +$desc .=sprintf("%d\t%s\t0\t%s\n",$oid,$catalog,$1); |
| 115 | + } |
| 116 | + } |
| 117 | +elsif ($line =~/^SHDESCR\("(.*)"\)$/m) |
| 118 | + { |
| 119 | +if ($oid != 0) |
| 120 | + { |
| 121 | +$shdesc .=sprintf("%d\t%s\t%s\n",$oid,$catalog,$1); |
| 122 | + } |
| 123 | + } |
| 124 | +elsif ($line =~/^DECLARE_(UNIQUE_)?INDEX\((.*)\)$/m) |
| 125 | + { |
| 126 | +if ($reln_open) |
| 127 | + { |
| 128 | +$bki .="close$catalog\n"; |
| 129 | +$reln_open = 0; |
| 130 | + } |
| 131 | +my$u =$1?" unique":""; |
| 132 | +my@fields =split /,/,$2,3; |
| 133 | +$fields[2] =~s/\s{2,}//g; |
| 134 | +$bki .="declare$u index$fields[0]$fields[1]$fields[2]\n"; |
| 135 | + } |
| 136 | +elsif ($line =~/^DECLARE_TOAST\((.*)\)$/m) |
| 137 | + { |
| 138 | +if ($reln_open) |
| 139 | + { |
| 140 | +$bki .="close$catalog\n"; |
| 141 | +$reln_open = 0; |
| 142 | + } |
| 143 | +my@fields =split /,/,$1; |
| 144 | +$bki .="declare toast$fields[1]$fields[2] on$fields[0]\n"; |
| 145 | + } |
| 146 | +elsif ($line =~/^BUILD_INDICES/) |
| 147 | + { |
| 148 | +$bki .="build indices\n"; |
| 149 | + } |
| 150 | +elsif ($line =~/^CATALOG\((.*)\)(.*)$/m) |
| 151 | + { |
| 152 | +if ($reln_open) |
| 153 | + { |
| 154 | +$bki .="close$catalog\n"; |
| 155 | +$reln_open = 0; |
| 156 | + } |
| 157 | +my$rest =$2; |
| 158 | +my@fields =split /,/,$1; |
| 159 | +$catalog =$fields[0]; |
| 160 | +$oid =$fields[1]; |
| 161 | +$bootstrap=$shared_relation=$without_oids=""; |
| 162 | +if ($rest =~/BKI_BOOTSTRAP/) |
| 163 | + { |
| 164 | +$bootstrap ="bootstrap"; |
| 165 | + } |
| 166 | +if ($rest =~/BKI_SHARED_RELATION/) |
| 167 | + { |
| 168 | +$shared_relation ="shared_relation"; |
| 169 | + } |
| 170 | +if ($rest =~/BKI_WITHOUT_OIDS/) |
| 171 | + { |
| 172 | +$without_oids ="without_oids"; |
| 173 | + } |
| 174 | +$nc++; |
| 175 | +$inside = 1; |
| 176 | +next; |
| 177 | + } |
| 178 | +if ($inside==1) |
| 179 | + { |
| 180 | +nextif ($line =~/{/); |
| 181 | +if ($line =~/}/) |
| 182 | + { |
| 183 | + |
| 184 | +# Last line |
| 185 | +$bki .="create$bootstrap$shared_relation$without_oids$catalog$oid\n (\n"; |
| 186 | +my$first = 1; |
| 187 | +for (my$i = 0;$i <=$#attr;$i++) |
| 188 | + { |
| 189 | +if ($first == 1) |
| 190 | + { |
| 191 | +$first = 0; |
| 192 | + } |
| 193 | +else |
| 194 | + { |
| 195 | +$bki .=",\n"; |
| 196 | + } |
| 197 | +$bki .="" .$attr[$i] ." =" .$types[$i]; |
| 198 | + } |
| 199 | +$bki .="\n )\n"; |
| 200 | +undef(@attr); |
| 201 | +undef(@types); |
| 202 | +$reln_open = 1; |
| 203 | +$inside = 0; |
| 204 | +if ($bootstrapeq"") |
| 205 | + { |
| 206 | +$bki .="open$catalog\n"; |
| 207 | + } |
| 208 | +next; |
| 209 | + } |
| 210 | + |
| 211 | +# inside catalog definition, so keep sucking up attributes |
| 212 | +my@fields =split /\s+/,$line; |
| 213 | +if ($fields[1] =~/(.*)\[.*\]/) |
| 214 | + {#Array attribute |
| 215 | +push@attr,$1; |
| 216 | +push@types,$fields[0] .'[]'; |
| 217 | + } |
| 218 | +else |
| 219 | + { |
| 220 | +push@attr,$fields[1]; |
| 221 | +push@types,$fields[0]; |
| 222 | + } |
| 223 | +next; |
| 224 | + } |
| 225 | + } |
| 226 | +if ($reln_open == 1) |
| 227 | + { |
| 228 | +$bki .="close$catalog\n"; |
| 229 | + } |
| 230 | + |
| 231 | +open(O,">$prefix.bki") ||die"Could not write$prefix.bki\n"; |
| 232 | +print O"# PostgreSQL$majorversion\n"; |
| 233 | +print O$bki; |
| 234 | +close(O); |
| 235 | +open(O,">$prefix.description") ||die"Could not write$prefix.description\n"; |
| 236 | +print O$desc; |
| 237 | +close(O); |
| 238 | +open(O,">$prefix.shdescription") ||die"Could not write$prefix.shdescription\n"; |
| 239 | +print O$shdesc; |
| 240 | +close(O); |
| 241 | +} |
| 242 | + |
| 243 | +subUsage |
| 244 | +{ |
| 245 | +print"Usage: genbki.pl <version> <prefix> <input1> [<input2> <input3>...]\n"; |
| 246 | +exit(1); |
| 247 | +} |
| 248 | + |
| 249 | +subread_file |
| 250 | +{ |
| 251 | +my$filename =shift; |
| 252 | +my$F; |
| 253 | +my$t =$/; |
| 254 | + |
| 255 | +undef$/; |
| 256 | +open($F,$filename) ||die"Could not open file$filename\n"; |
| 257 | +my$txt = <$F>; |
| 258 | +close($F); |
| 259 | +$/ =$t; |
| 260 | + |
| 261 | +return$txt; |
| 262 | +} |
| 263 | + |
| 264 | +1; |