2
#-------------------------------------------------------------------------
5
# shell script which generates .bki files from specially formatted .h
6
# files. These .bki files are used to initialize the postgres template
9
# Copyright (c) 1994, Regents of the University of California
13
# $PostgreSQL: pgsql/src/backend/catalog/genbki.sh,v 1.32 2004-01-04 05:57:21 tgl Exp $
16
# non-essential whitespace is removed from the generated file.
17
# if this is ever a problem, then the sed script at the very
18
# end can be changed into another awk script or something smarter.
20
#-------------------------------------------------------------------------
32
# Process command line switches.
38
INCLUDE_DIRS="$INCLUDE_DIRS $2"
41
arg=`echo $1 | sed -e 's/^-I//'`
42
INCLUDE_DIRS="$INCLUDE_DIRS $arg"
48
OUTPUT_PREFIX=`echo $1 | sed -e 's/^-o//'`
51
arg=`expr x"$1" : x"--set-version=\(.*\)"`
52
major_version=`expr x"$arg" : x'\([0-9][0-9]*\.[0-9][0-9]*\)'`
55
echo "$CMDNAME generates system catalog bootstrapping files."
58
echo " $CMDNAME [ -I dir ] --set-version=VERSION -o prefix files..."
61
echo " -I path to postgres_ext.h and pg_config_manual.h files"
62
echo " -o prefix of output files"
63
echo " --set-version PostgreSQL version number for initdb cross-check"
65
echo "The environment variable AWK determines which Awk program"
66
echo "to use. The default is \`awk'."
68
echo "Report bugs to <pgsql-bugs@postgresql.org>."
72
echo "$CMDNAME: invalid option: $1"
82
if [ x"$INFILES" = x"" ] ; then
83
echo "$CMDNAME: no input files" 1>&2
87
if [ x"$OUTPUT_PREFIX" = x"" ] ; then
88
echo "$CMDNAME: no output prefix specified" 1>&2
92
if [ x"$INCLUDE_DIRS" = x"" ] ; then
93
echo "$CMDNAME: path to include directory unknown" 1>&2
97
if [ x"$major_version" = x"" ] ; then
98
echo "$CMDNAME: invalid or no version number specified" 1>&2
103
TMPFILE="genbkitmp$$.c"
105
trap "rm -f $TMPFILE ${OUTPUT_PREFIX}.bki.$$ ${OUTPUT_PREFIX}.description.$$" 0 1 2 3 15
108
# Get NAMEDATALEN from postgres_ext.h
109
for dir in $INCLUDE_DIRS; do
110
if [ -f "$dir/postgres_ext.h" ]; then
111
NAMEDATALEN=`grep '^#define[ ]*NAMEDATALEN' $dir/postgres_ext.h | $AWK '{ print $3 }'`
116
# Get INDEX_MAX_KEYS from pg_config_manual.h
117
# (who needs consistency?)
118
for dir in $INCLUDE_DIRS; do
119
if [ -f "$dir/pg_config_manual.h" ]; then
120
INDEXMAXKEYS=`grep '^#define[ ]*INDEX_MAX_KEYS' $dir/pg_config_manual.h | $AWK '{ print $3 }'`
125
# Get PG_CATALOG_NAMESPACE from catalog/pg_namespace.h
126
for dir in $INCLUDE_DIRS; do
127
if [ -f "$dir/catalog/pg_namespace.h" ]; then
128
PG_CATALOG_NAMESPACE=`grep '^#define[ ]*PG_CATALOG_NAMESPACE' $dir/catalog/pg_namespace.h | $AWK '{ print $3 }'`
133
# Get FirstGenBKIObjectId from access/transam.h
134
for dir in $INCLUDE_DIRS; do
135
if [ -f "$dir/access/transam.h" ]; then
136
BKIOBJECTID=`grep '^#define[ ]*FirstGenBKIObjectId' $dir/access/transam.h | $AWK '{ print $3 }'`
142
# NOTE: we assume here that FUNC_MAX_ARGS has the same value as
143
# INDEX_MAX_KEYS, and don't read it separately from
144
# pg_config_manual.h. This is OK because both of them must be equal
145
# to the length of oidvector.
147
INDEXMAXKEYS2=`expr $INDEXMAXKEYS '*' 2` || exit
148
INDEXMAXKEYS4=`expr $INDEXMAXKEYS '*' 4` || exit
150
touch ${OUTPUT_PREFIX}.description.$$
153
# Strip comments and other trash from .h
155
# Put multi-line start/end comments on a separate line
157
# Rename datatypes that have different names in .h files than in SQL
159
# Substitute values of configuration constants
163
sed -e 's;/\*.*\*/;;g' \
169
;g' | # we must run a new sed here to see the newlines we added
170
sed -e "s/;[ ]*$//g" \
172
-e "s/[ ]Oid/ oid/g" \
175
-e "s/[ ]NameData/ name/g" \
176
-e "s/^NameData/name/g" \
177
-e "s/(NameData/(name/g" \
178
-e "s/[ ]TransactionId/ xid/g" \
179
-e "s/^TransactionId/xid/g" \
180
-e "s/(TransactionId/(xid/g" \
182
-e "s/NAMEDATALEN/$NAMEDATALEN/g" \
183
-e "s/PGNSP/$PG_CATALOG_NAMESPACE/g" \
184
-e "s/INDEX_MAX_KEYS\*2/$INDEXMAXKEYS2/g" \
185
-e "s/INDEX_MAX_KEYS\*4/$INDEXMAXKEYS4/g" \
186
-e "s/INDEX_MAX_KEYS/$INDEXMAXKEYS/g" \
187
-e "s/FUNC_MAX_ARGS\*2/$INDEXMAXKEYS2/g" \
188
-e "s/FUNC_MAX_ARGS\*4/$INDEXMAXKEYS4/g" \
189
-e "s/FUNC_MAX_ARGS/$INDEXMAXKEYS/g" \
192
# now use awk to process remaining .h file..
194
# nc is the number of catalogs
195
# inside is a variable set to 1 when we are scanning the
196
# contents of a catalog definition.
197
# reln_open is a flag indicating when we are processing DATA lines.
198
# (i.e. have a relation open and need to close it)
199
# nextbkioid is the next OID available for automatic assignment.
200
# oid is the most recently seen or assigned oid.
205
shared_relation = "";
210
nextbkioid = ENVIRON["BKIOBJECTID"];
215
# Anything in a /* .. */ block should be ignored.
216
# Blank lines also go.
217
# Note that any /* */ comment on a line by itself was removed from the line
220
/^\/\*/ { comment_level += 1; next; }
221
/^\*\// { comment_level -= 1; next; }
222
comment_level > 0 { next; }
227
# DATA() statements are basically passed right through after
228
# stripping off the DATA( and the ) on the end. However,
229
# if we see "OID = 0" then we should assign an oid from nextbkioid.
230
# Remember any explicit or assigned OID for use by DESCR().
233
data = substr($0, 6, length($0) - 6);
235
nf = split(data, datafields);
236
if (nf >= 4 && datafields[1] == "insert" && datafields[2] == "OID" && datafields[3] == "=")
243
sub("OID *= *0", "OID = " oid, data);
253
data = substr($0, 8, length($0) - 9);
255
printf "%d\t%s\t0\t%s\n", oid, catalog, data >>descriptionfile;
262
# end any prior catalog data insertions before starting a define index
264
if (reln_open == 1) {
266
print "close " catalog;
270
data = substr($0, 15, length($0) - 15);
271
print "declare index " data
274
/^DECLARE_UNIQUE_INDEX\(/ {
276
# end any prior catalog data insertions before starting a define unique index
278
if (reln_open == 1) {
280
print "close " catalog;
284
data = substr($0, 22, length($0) - 22);
285
print "declare unique index " data
288
/^BUILD_INDICES/ { print "build indices"; }
291
# CATALOG() definitions take some more work.
295
# end any prior catalog data insertions before starting a new one..
297
if (reln_open == 1) {
299
print "close " catalog;
304
# get the name and properties of the new catalog
307
catalog = substr($1,9,pos-9);
309
if ($0 ~ /BOOTSTRAP/) {
310
bootstrap = "bootstrap ";
312
if ($0 ~ /BKI_SHARED_RELATION/) {
313
shared_relation = "shared_relation ";
315
if ($0 ~ /BKI_WITHOUT_OIDS/) {
316
without_oids = "without_oids ";
326
# process the contents of the catalog definition
328
# attname[ x ] contains the attribute name for attribute x
329
# atttype[ x ] contains the attribute type fot attribute x
333
# ignore a leading brace line..
339
# if this is the last line, then output the bki catalog stuff.
342
print "create " bootstrap shared_relation without_oids catalog;
345
for (j=1; j<i-1; j++) {
346
print "\t " attname[ j ] " = " atttype[ j ] " ,";
348
print "\t " attname[ j ] " = " atttype[ j ] ;
351
if (bootstrap == "") {
352
print "open " catalog;
359
shared_relation = "";
365
# if we are inside the catalog definition, then keep sucking up
366
# attribute names and types
368
if ($2 ~ /\[.*\]/) { # array attribute
369
idlen = index($2,"[") - 1;
370
atttype[ i ] = $1 "[]"; # variable-length only..
371
attname[ i ] = substr($2,1,idlen);
381
if (reln_open == 1) {
383
print "close " catalog;
387
' "descriptionfile=${OUTPUT_PREFIX}.description.$$" > $TMPFILE || exit
389
echo "# PostgreSQL $major_version" >${OUTPUT_PREFIX}.bki.$$
392
-e 's/[ ][ ]*/ /g' $TMPFILE >>${OUTPUT_PREFIX}.bki.$$ || exit
395
# Sanity check: if one of the sed/awk/etc commands fails, we'll probably
396
# end up with a .bki file that is empty or just a few lines. Cross-check
397
# that the files are of reasonable size. The numbers here are arbitrary,
398
# but are much smaller than the actual expected sizes as of Postgres 7.2.
400
if [ `wc -c < ${OUTPUT_PREFIX}.bki.$$` -lt 100000 ]; then
401
echo "$CMDNAME: something seems to be wrong with the .bki file" >&2
404
if [ `wc -c < ${OUTPUT_PREFIX}.description.$$` -lt 10000 ]; then
405
echo "$CMDNAME: something seems to be wrong with the .description file" >&2
409
# Looks good, commit ...
411
mv ${OUTPUT_PREFIX}.bki.$$ ${OUTPUT_PREFIX}.bki || exit
412
mv ${OUTPUT_PREFIX}.description.$$ ${OUTPUT_PREFIX}.description || exit