]> granicus.if.org Git - postgresql/blob - src/backend/utils/Gen_fmgrtab.sh
cf649ff3cd6403d9ff23eb9ab68367c4e7776dc7
[postgresql] / src / backend / utils / Gen_fmgrtab.sh
1 #! /bin/sh
2 #-------------------------------------------------------------------------
3 #
4 # Gen_fmgrtab.sh
5 #    shell script to generate fmgroids.h and fmgrtab.c from pg_proc.h
6 #
7 # Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
8 # Portions Copyright (c) 1994, Regents of the University of California
9 #
10 #
11 # IDENTIFICATION
12 #    $Header: /cvsroot/pgsql/src/backend/utils/Attic/Gen_fmgrtab.sh,v 1.20 2001/05/22 12:06:51 momjian Exp $
13 #
14 #-------------------------------------------------------------------------
15
16 CMDNAME=`basename $0`
17
18 : ${AWK='awk'}
19 : ${CPP='cc -E'}
20
21 cleanup(){
22     [ x"$noclean" != x"t" ] && rm -f "$CPPTMPFILE" "$RAWFILE" "$$-$OIDSFILE" "$$-$TABLEFILE"
23 }
24
25 BKIOPTS=
26 noclean=
27
28 #
29 # Process command line switches.
30 #
31 while [ $# -gt 0 ]
32 do
33     case $1 in
34         -D)
35             BKIOPTS="$BKIOPTS -D$2"
36             shift;;
37         -D*)
38             BKIOPTS="$BKIOPTS $1"
39             ;;
40         --noclean)
41             noclean=t
42             ;;
43         --help)
44             echo "$CMDNAME generates fmgroids.h and fmgrtab.c from pg_proc.h."
45             echo
46             echo "Usage:"
47             echo "  $CMDNAME [ -D define [...] ]"
48             echo
49             echo "The environment variables CPP and AWK determine which C"
50             echo "preprocessor and Awk program to use. The defaults are"
51             echo "\`cc -E' and \`awk'."
52             echo
53             echo "Report bugs to <pgsql-bugs@postgresql.org>."
54             exit 0
55             ;;
56         --) shift; break;;
57         -*)
58             echo "$CMDNAME: invalid option: $1"
59             exit 1
60             ;;
61         *)
62             INFILE=$1
63             ;;
64     esac
65     shift
66 done
67
68
69 if [ x"$INFILE" = x ] ; then
70     echo "$CMDNAME: no input file"
71     exit 1
72 fi
73
74 CPPTMPFILE="$$-fmgrtmp.c"
75 RAWFILE="$$-fmgr.raw"
76 OIDSFILE=fmgroids.h
77 TABLEFILE=fmgrtab.c
78
79
80 trap 'echo "Caught signal." ; cleanup ; exit 1' 1 2 15
81
82
83 #
84 # Generate the file containing raw pg_proc tuple data
85 # (but only for "internal" language procedures...).
86 #
87 # Unlike genbki.sh, which can run through cpp last, we have to
88 # deal with preprocessor statements first (before we sort the
89 # function table by oid).
90 #
91 $AWK '
92 BEGIN           { raw = 0; }
93 /^DATA/         { print; next; }
94 /^BKI_BEGIN/    { raw = 1; next; }
95 /^BKI_END/      { raw = 0; next; }
96 raw == 1        { print; next; }' $INFILE | \
97 sed     -e 's/^.*OID[^=]*=[^0-9]*//' \
98         -e 's/(//g' \
99         -e 's/[         ]*).*$//' | \
100 $AWK '
101 /^#/            { print; next; }
102 $4 == "12"      { print; next; }' > $CPPTMPFILE
103
104 if [ $? -ne 0 ]; then
105     cleanup
106     echo "$CMDNAME failed"
107     exit 1
108 fi
109
110 $CPP $BKIOPTS $CPPTMPFILE | \
111 egrep '^[ ]*[0-9]' | \
112 sort -n > $RAWFILE
113
114 if [ $? -ne 0 ]; then
115     cleanup
116     echo "$CMDNAME failed"
117     exit 1
118 fi
119
120
121 cpp_define=`echo $OIDSFILE | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ | sed -e 's/[^A-Z]/_/g'`
122
123 #
124 # Generate fmgroids.h
125 #
126 cat > "$$-$OIDSFILE" <<FuNkYfMgRsTuFf
127 /*-------------------------------------------------------------------------
128  *
129  * $OIDSFILE
130  *    Macros that define the OIDs of built-in functions.
131  *
132  * These macros can be used to avoid a catalog lookup when a specific
133  * fmgr-callable function needs to be referenced.
134  *
135  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
136  * Portions Copyright (c) 1994, Regents of the University of California
137  *
138  * NOTES
139  *      ******************************
140  *      *** DO NOT EDIT THIS FILE! ***
141  *      ******************************
142  *
143  *      It has been GENERATED by $CMDNAME
144  *      from $INFILE
145  *
146  *-------------------------------------------------------------------------
147  */
148 #ifndef $cpp_define
149 #define $cpp_define
150
151 /*
152  *      Constant macros for the OIDs of entries in pg_proc.
153  *
154  *      NOTE: macros are named after the prosrc value, ie the actual C name
155  *      of the implementing function, not the proname which may be overloaded.
156  *      For example, we want to be able to assign different macro names to both
157  *      char_text() and int4_text() even though these both appear with proname
158  *      'text'.  If the same C function appears in more than one pg_proc entry,
159  *      its equivalent macro will be defined with the lowest OID among those
160  *      entries.
161  */
162 FuNkYfMgRsTuFf
163
164 tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' < $RAWFILE | \
165 $AWK '
166 BEGIN   { OFS = ""; }
167         { if (seenit[$(NF-1)]++ == 0) print "#define F_", $(NF-1), " ", $1; }' >> "$$-$OIDSFILE"
168
169 if [ $? -ne 0 ]; then
170     cleanup
171     echo "$CMDNAME failed"
172     exit 1
173 fi
174
175 cat >> "$$-$OIDSFILE" <<FuNkYfMgRsTuFf
176
177 #endif  /* $cpp_define */
178 FuNkYfMgRsTuFf
179
180 #
181 # Generate fmgr's built-in-function table.
182 #
183 # Print out the function declarations, then the table that refers to them.
184 #
185 cat > "$$-$TABLEFILE" <<FuNkYfMgRtAbStUfF
186 /*-------------------------------------------------------------------------
187  *
188  * $TABLEFILE
189  *    The function manager's table of internal functions.
190  *
191  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
192  * Portions Copyright (c) 1994, Regents of the University of California
193  *
194  * NOTES
195  *
196  *      ******************************
197  *      *** DO NOT EDIT THIS FILE! ***
198  *      ******************************
199  *
200  *      It has been GENERATED by $CMDNAME
201  *      from $INFILE
202  *
203  *-------------------------------------------------------------------------
204  */
205
206 #include "postgres.h"
207
208 #include "utils/fmgrtab.h"
209
210 FuNkYfMgRtAbStUfF
211
212 $AWK '{ print "extern Datum", $(NF-1), "(PG_FUNCTION_ARGS);"; }' $RAWFILE >> "$$-$TABLEFILE"
213
214 if [ $? -ne 0 ]; then
215     cleanup
216     echo "$CMDNAME failed"
217     exit 1
218 fi
219
220
221 cat >> "$$-$TABLEFILE" <<FuNkYfMgRtAbStUfF
222
223 const FmgrBuiltin fmgr_builtins[] = {
224 FuNkYfMgRtAbStUfF
225
226 # Note: using awk arrays to translate from pg_proc values to fmgrtab values
227 # may seem tedious, but avoid the temptation to write a quick x?y:z
228 # conditional expression instead.  Not all awks have conditional expressions.
229
230 $AWK 'BEGIN {
231     Bool["t"] = "true"
232     Bool["f"] = "false"
233 }
234 { printf ("  { %d, \"%s\", %d, %s, %s, %s },\n"), \
235         $1, $(NF-1), $9, Bool[$8], Bool[$10], $(NF-1)
236 }' $RAWFILE >> "$$-$TABLEFILE"
237
238 if [ $? -ne 0 ]; then
239     cleanup
240     echo "$CMDNAME failed"
241     exit 1
242 fi
243
244 cat >> "$$-$TABLEFILE" <<FuNkYfMgRtAbStUfF
245   /* dummy entry is easier than getting rid of comma after last real one */
246   /* (not that there has ever been anything wrong with *having* a
247      comma after the last field in an array initializer) */
248   { 0, NULL, 0, false, false, (PGFunction) NULL }
249 };
250
251 /* Note fmgr_nbuiltins excludes the dummy entry */
252 const int fmgr_nbuiltins = (sizeof(fmgr_builtins) / sizeof(FmgrBuiltin)) - 1;
253
254 FuNkYfMgRtAbStUfF
255
256 # We use the temporary files to avoid problems with concurrent runs
257 # (which can happen during parallel make).
258 mv "$$-$OIDSFILE" $OIDSFILE
259 mv "$$-$TABLEFILE" $TABLEFILE
260
261 cleanup
262 exit 0