~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/backend/catalog/genbki.sh

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /bin/sh
 
2
#-------------------------------------------------------------------------
 
3
#
 
4
# genbki.sh--
 
5
#    shell 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
# Copyright (c) 1994, Regents of the University of California
 
10
#
 
11
#
 
12
# IDENTIFICATION
 
13
#    $PostgreSQL: pgsql/src/backend/catalog/genbki.sh,v 1.32 2004-01-04 05:57:21 tgl Exp $
 
14
#
 
15
# NOTES
 
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.
 
19
#
 
20
#-------------------------------------------------------------------------
 
21
 
 
22
: ${AWK='awk'}
 
23
 
 
24
CMDNAME=`basename $0`
 
25
 
 
26
INCLUDE_DIRS=
 
27
OUTPUT_PREFIX=
 
28
INFILES=
 
29
major_version=
 
30
 
 
31
#
 
32
# Process command line switches.
 
33
#
 
34
while [ $# -gt 0 ]
 
35
do
 
36
    case $1 in
 
37
        -I)
 
38
            INCLUDE_DIRS="$INCLUDE_DIRS $2"
 
39
            shift;;
 
40
        -I*)
 
41
            arg=`echo $1 | sed -e 's/^-I//'`
 
42
            INCLUDE_DIRS="$INCLUDE_DIRS $arg"
 
43
            ;;
 
44
        -o)
 
45
            OUTPUT_PREFIX="$2"
 
46
            shift;;
 
47
        -o*)
 
48
            OUTPUT_PREFIX=`echo $1 | sed -e 's/^-o//'`
 
49
            ;;
 
50
        --set-version=*)
 
51
            arg=`expr x"$1" : x"--set-version=\(.*\)"`
 
52
            major_version=`expr x"$arg" : x'\([0-9][0-9]*\.[0-9][0-9]*\)'`
 
53
            ;;
 
54
        --help)
 
55
            echo "$CMDNAME generates system catalog bootstrapping files."
 
56
            echo
 
57
            echo "Usage:"
 
58
            echo "  $CMDNAME [ -I dir ] --set-version=VERSION -o prefix files..."
 
59
            echo
 
60
            echo "Options:"
 
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"
 
64
            echo
 
65
            echo "The environment variable AWK determines which Awk program"
 
66
            echo "to use. The default is \`awk'."
 
67
            echo
 
68
            echo "Report bugs to <pgsql-bugs@postgresql.org>."
 
69
            exit 0
 
70
            ;;
 
71
        -*)
 
72
            echo "$CMDNAME: invalid option: $1"
 
73
            exit 1
 
74
            ;;
 
75
        *)
 
76
            INFILES="$INFILES $1"
 
77
            ;;
 
78
    esac
 
79
    shift
 
80
done
 
81
 
 
82
if [ x"$INFILES" = x"" ] ; then
 
83
    echo "$CMDNAME: no input files" 1>&2
 
84
    exit 1
 
85
fi
 
86
 
 
87
if [ x"$OUTPUT_PREFIX" = x"" ] ; then
 
88
    echo "$CMDNAME: no output prefix specified" 1>&2
 
89
    exit 1
 
90
fi
 
91
 
 
92
if [ x"$INCLUDE_DIRS" = x"" ] ; then
 
93
    echo "$CMDNAME: path to include directory unknown" 1>&2
 
94
    exit 1
 
95
fi
 
96
 
 
97
if [ x"$major_version" = x"" ] ; then
 
98
    echo "$CMDNAME: invalid or no version number specified" 1>&2
 
99
    exit 1
 
100
fi
 
101
 
 
102
 
 
103
TMPFILE="genbkitmp$$.c"
 
104
 
 
105
trap "rm -f $TMPFILE ${OUTPUT_PREFIX}.bki.$$ ${OUTPUT_PREFIX}.description.$$" 0 1 2 3 15
 
106
 
 
107
 
 
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 }'`
 
112
        break
 
113
    fi
 
114
done
 
115
 
 
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 }'`
 
121
        break
 
122
    fi
 
123
done
 
124
 
 
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 }'`
 
129
        break
 
130
    fi
 
131
done
 
132
 
 
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 }'`
 
137
        break
 
138
    fi
 
139
done
 
140
export BKIOBJECTID
 
141
 
 
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.
 
146
 
 
147
INDEXMAXKEYS2=`expr $INDEXMAXKEYS '*' 2` || exit
 
148
INDEXMAXKEYS4=`expr $INDEXMAXKEYS '*' 4` || exit
 
149
 
 
150
touch ${OUTPUT_PREFIX}.description.$$
 
151
 
 
152
# ----------------
 
153
#       Strip comments and other trash from .h
 
154
#
 
155
#       Put multi-line start/end comments on a separate line
 
156
#
 
157
#       Rename datatypes that have different names in .h files than in SQL
 
158
#
 
159
#       Substitute values of configuration constants
 
160
# ----------------
 
161
#
 
162
cat $INFILES | \
 
163
sed -e 's;/\*.*\*/;;g' \
 
164
    -e 's;/\*;\
 
165
/*\
 
166
;g' \
 
167
    -e 's;\*/;\
 
168
*/\
 
169
;g' | # we must run a new sed here to see the newlines we added
 
170
sed -e "s/;[    ]*$//g" \
 
171
    -e "s/^[    ]*//" \
 
172
    -e "s/[     ]Oid/ oid/g" \
 
173
    -e "s/^Oid/oid/g" \
 
174
    -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" \
 
181
    -e "s/PGUID/1/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" \
 
190
| $AWK '
 
191
# ----------------
 
192
#       now use awk to process remaining .h file..
 
193
#
 
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.
 
201
# ----------------
 
202
BEGIN {
 
203
        inside = 0;
 
204
        bootstrap = "";
 
205
        shared_relation = "";
 
206
        without_oids = "";
 
207
        nc = 0;
 
208
        reln_open = 0;
 
209
        comment_level = 0;
 
210
        nextbkioid = ENVIRON["BKIOBJECTID"];
 
211
        oid = 0;
 
212
}
 
213
 
 
214
# ----------------
 
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
 
218
# by the sed above.
 
219
# ----------------
 
220
/^\/\*/           { comment_level += 1; next; }
 
221
/^\*\//           { comment_level -= 1; next; }
 
222
comment_level > 0 { next; }
 
223
 
 
224
/^[     ]*$/      { next; }
 
225
 
 
226
# ----------------
 
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().
 
231
# ----------------
 
232
/^DATA\(/ {
 
233
        data = substr($0, 6, length($0) - 6);
 
234
        oid = 0;
 
235
        nf = split(data, datafields);
 
236
        if (nf >= 4 && datafields[1] == "insert" && datafields[2] == "OID" && datafields[3] == "=")
 
237
        {
 
238
                oid = datafields[4];
 
239
                if (oid == 0)
 
240
                {
 
241
                        oid = nextbkioid;
 
242
                        nextbkioid++;
 
243
                        sub("OID *= *0", "OID = " oid, data);
 
244
                }
 
245
        }
 
246
        print data;
 
247
        next;
 
248
}
 
249
 
 
250
/^DESCR\(/ {
 
251
        if (oid != 0)
 
252
        {
 
253
                data = substr($0, 8, length($0) - 9);
 
254
                if (data != "")
 
255
                        printf "%d\t%s\t0\t%s\n", oid, catalog, data >>descriptionfile;
 
256
        }
 
257
        next;
 
258
}
 
259
 
 
260
/^DECLARE_INDEX\(/ {
 
261
# ----
 
262
#  end any prior catalog data insertions before starting a define index
 
263
# ----
 
264
        if (reln_open == 1) {
 
265
#               print "show";
 
266
                print "close " catalog;
 
267
                reln_open = 0;
 
268
        }
 
269
 
 
270
        data = substr($0, 15, length($0) - 15);
 
271
        print "declare index " data
 
272
}
 
273
 
 
274
/^DECLARE_UNIQUE_INDEX\(/ {
 
275
# ----
 
276
#  end any prior catalog data insertions before starting a define unique index
 
277
# ----
 
278
        if (reln_open == 1) {
 
279
#               print "show";
 
280
                print "close " catalog;
 
281
                reln_open = 0;
 
282
        }
 
283
 
 
284
        data = substr($0, 22, length($0) - 22);
 
285
        print "declare unique index " data
 
286
}
 
287
 
 
288
/^BUILD_INDICES/        { print "build indices"; }
 
289
        
 
290
# ----------------
 
291
#       CATALOG() definitions take some more work.
 
292
# ----------------
 
293
/^CATALOG\(/ { 
 
294
# ----
 
295
#  end any prior catalog data insertions before starting a new one..
 
296
# ----
 
297
        if (reln_open == 1) {
 
298
#               print "show";
 
299
                print "close " catalog;
 
300
                reln_open = 0;
 
301
        }
 
302
 
 
303
# ----
 
304
#  get the name and properties of the new catalog
 
305
# ----
 
306
        pos = index($1,")");
 
307
        catalog = substr($1,9,pos-9); 
 
308
 
 
309
        if ($0 ~ /BOOTSTRAP/) {
 
310
                bootstrap = "bootstrap ";
 
311
        }
 
312
        if ($0 ~ /BKI_SHARED_RELATION/) {
 
313
                shared_relation = "shared_relation ";
 
314
        }
 
315
        if ($0 ~ /BKI_WITHOUT_OIDS/) {
 
316
                without_oids = "without_oids ";
 
317
        }
 
318
 
 
319
        i = 1;
 
320
        inside = 1;
 
321
        nc++;
 
322
        next;
 
323
}
 
324
 
 
325
# ----------------
 
326
#       process the contents of the catalog definition
 
327
#
 
328
#       attname[ x ] contains the attribute name for attribute x
 
329
#       atttype[ x ] contains the attribute type fot attribute x
 
330
# ----------------
 
331
inside == 1 {
 
332
# ----
 
333
#  ignore a leading brace line..
 
334
# ----
 
335
        if ($1 ~ /\{/)
 
336
                next;
 
337
 
 
338
# ----
 
339
#  if this is the last line, then output the bki catalog stuff.
 
340
# ----
 
341
        if ($1 ~ /}/) {
 
342
                print "create " bootstrap shared_relation without_oids catalog;
 
343
                print "\t(";
 
344
 
 
345
                for (j=1; j<i-1; j++) {
 
346
                        print "\t " attname[ j ] " = " atttype[ j ] " ,";
 
347
                }
 
348
                print "\t " attname[ j ] " = " atttype[ j ] ;
 
349
                print "\t)";
 
350
 
 
351
                if (bootstrap == "") {
 
352
                        print "open " catalog;
 
353
                }
 
354
 
 
355
                i = 1;
 
356
                reln_open = 1;
 
357
                inside = 0;
 
358
                bootstrap = "";
 
359
                shared_relation = "";
 
360
                without_oids = "";
 
361
                next;
 
362
        }
 
363
 
 
364
# ----
 
365
#  if we are inside the catalog definition, then keep sucking up
 
366
#  attribute names and types
 
367
# ----
 
368
        if ($2 ~ /\[.*\]/) {                    # array attribute
 
369
                idlen = index($2,"[") - 1;
 
370
                atttype[ i ] = $1 "[]";         # variable-length only..
 
371
                attname[ i ] = substr($2,1,idlen);
 
372
        } else {
 
373
                atttype[ i ] = $1;
 
374
                attname[ i ] = $2;
 
375
        }
 
376
        i++;
 
377
        next;
 
378
}
 
379
 
 
380
END {
 
381
        if (reln_open == 1) {
 
382
#               print "show";
 
383
                print "close " catalog;
 
384
                reln_open = 0;
 
385
        }
 
386
}
 
387
' "descriptionfile=${OUTPUT_PREFIX}.description.$$" > $TMPFILE || exit
 
388
 
 
389
echo "# PostgreSQL $major_version" >${OUTPUT_PREFIX}.bki.$$
 
390
 
 
391
sed -e '/^[     ]*$/d' \
 
392
    -e 's/[     ][      ]*/ /g' $TMPFILE >>${OUTPUT_PREFIX}.bki.$$ || exit
 
393
 
 
394
#
 
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.
 
399
#
 
400
if [ `wc -c < ${OUTPUT_PREFIX}.bki.$$` -lt 100000 ]; then
 
401
    echo "$CMDNAME: something seems to be wrong with the .bki file" >&2
 
402
    exit 1
 
403
fi
 
404
if [ `wc -c < ${OUTPUT_PREFIX}.description.$$` -lt 10000 ]; then
 
405
    echo "$CMDNAME: something seems to be wrong with the .description file" >&2
 
406
    exit 1
 
407
fi
 
408
 
 
409
# Looks good, commit ...
 
410
 
 
411
mv ${OUTPUT_PREFIX}.bki.$$ ${OUTPUT_PREFIX}.bki || exit
 
412
mv ${OUTPUT_PREFIX}.description.$$ ${OUTPUT_PREFIX}.description || exit
 
413
 
 
414
exit 0