Fix failure to double-quote function argument names when needed, in
[PostgreSQL.git] / src / backend / utils / Gen_fmgrtab.sh
blob9cbe5d23370f3e70ee80e69a22ae95bf374a03b6
1 #! /bin/sh
2 #-------------------------------------------------------------------------
4 # Gen_fmgrtab.sh
5 # shell script to generate fmgroids.h and fmgrtab.c from pg_proc.h
7 # NOTE: if you change this, you need to fix Gen_fmgrtab.pl too!
9 # Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
10 # Portions Copyright (c) 1994, Regents of the University of California
13 # IDENTIFICATION
14 # $PostgreSQL$
16 #-------------------------------------------------------------------------
18 CMDNAME=`basename $0`
20 if [ x"$AWK" = x"" ]; then
21 AWK=awk
24 cleanup(){
25 [ x"$noclean" != x"t" ] && rm -f "$SORTEDFILE" "$$-$OIDSFILE" "$$-$TABLEFILE"
28 noclean=
31 # Process command line switches.
33 while [ $# -gt 0 ]
35 case $1 in
36 --noclean)
37 noclean=t
39 --help)
40 echo "$CMDNAME generates fmgroids.h and fmgrtab.c from pg_proc.h."
41 echo
42 echo "Usage:"
43 echo " $CMDNAME inputfile"
44 echo
45 echo "The environment variable AWK determines which Awk program"
46 echo "to use. The default is \`awk'."
47 echo
48 echo "Report bugs to <pgsql-bugs@postgresql.org>."
49 exit 0
51 -*)
52 echo "$CMDNAME: invalid option: $1"
53 exit 1
56 INFILE=$1
58 esac
59 shift
60 done
63 if [ x"$INFILE" = x ] ; then
64 echo "$CMDNAME: no input file"
65 exit 1
68 SORTEDFILE="$$-fmgr.data"
69 OIDSFILE=fmgroids.h
70 TABLEFILE=fmgrtab.c
73 trap 'echo "Caught signal." ; cleanup ; exit 1' 1 2 15
76 # Collect the column numbers of the pg_proc columns we need. Because we will
77 # be looking at data that includes the OID as the first column, add one to
78 # each column number.
80 proname=`egrep '^#define Anum_pg_proc_proname[ ]' $INFILE | $AWK '{print $3+1}'`
81 prolang=`egrep '^#define Anum_pg_proc_prolang[ ]' $INFILE | $AWK '{print $3+1}'`
82 proisstrict=`egrep '^#define Anum_pg_proc_proisstrict[ ]' $INFILE | $AWK '{print $3+1}'`
83 proretset=`egrep '^#define Anum_pg_proc_proretset[ ]' $INFILE | $AWK '{print $3+1}'`
84 pronargs=`egrep '^#define Anum_pg_proc_pronargs[ ]' $INFILE | $AWK '{print $3+1}'`
85 prosrc=`egrep '^#define Anum_pg_proc_prosrc[ ]' $INFILE | $AWK '{print $3+1}'`
88 # Generate the file containing raw pg_proc data. We do three things here:
89 # 1. Strip off the DATA macro call, leaving procedure OID as $1
90 # and all the pg_proc field values as $2, $3, etc on each line.
91 # 2. Fold quoted fields to simple "xxx". We need this because such fields
92 # may contain whitespace, which would confuse awk's counting of fields.
93 # Fortunately, this script doesn't need to look at any fields that might
94 # need quoting, so this simple hack is sufficient.
95 # 3. Select out just the rows for internal-language procedures.
97 # Note assumption here that INTERNALlanguageId == 12.
99 egrep '^DATA' $INFILE | \
100 sed -e 's/^[^O]*OID[^=]*=[ ]*//' \
101 -e 's/(//' \
102 -e 's/"[^"]*"/"xxx"/g' | \
103 $AWK "\$$prolang == \"12\" { print }" | \
104 sort -n > $SORTEDFILE
106 if [ $? -ne 0 ]; then
107 cleanup
108 echo "$CMDNAME failed"
109 exit 1
113 cpp_define=`echo $OIDSFILE | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ | sed -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
116 # Generate fmgroids.h
118 cat > "$$-$OIDSFILE" <<FuNkYfMgRsTuFf
119 /*-------------------------------------------------------------------------
121 * $OIDSFILE
122 * Macros that define the OIDs of built-in functions.
124 * These macros can be used to avoid a catalog lookup when a specific
125 * fmgr-callable function needs to be referenced.
127 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
128 * Portions Copyright (c) 1994, Regents of the University of California
130 * NOTES
131 * ******************************
132 * *** DO NOT EDIT THIS FILE! ***
133 * ******************************
135 * It has been GENERATED by $CMDNAME
136 * from $INFILE
138 *-------------------------------------------------------------------------
140 #ifndef $cpp_define
141 #define $cpp_define
144 * Constant macros for the OIDs of entries in pg_proc.
146 * NOTE: macros are named after the prosrc value, ie the actual C name
147 * of the implementing function, not the proname which may be overloaded.
148 * For example, we want to be able to assign different macro names to both
149 * char_text() and name_text() even though these both appear with proname
150 * 'text'. If the same C function appears in more than one pg_proc entry,
151 * its equivalent macro will be defined with the lowest OID among those
152 * entries.
154 FuNkYfMgRsTuFf
156 tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' < $SORTEDFILE | \
157 $AWK "{ if (seenit[\$$prosrc]++ == 0)
158 printf \"#define F_%s %s\\n\", \$$prosrc, \$1; }" >> "$$-$OIDSFILE"
160 if [ $? -ne 0 ]; then
161 cleanup
162 echo "$CMDNAME failed"
163 exit 1
166 cat >> "$$-$OIDSFILE" <<FuNkYfMgRsTuFf
168 #endif /* $cpp_define */
169 FuNkYfMgRsTuFf
172 # Generate fmgr's built-in-function table.
174 # Print out the function declarations, then the table that refers to them.
176 cat > "$$-$TABLEFILE" <<FuNkYfMgRtAbStUfF
177 /*-------------------------------------------------------------------------
179 * $TABLEFILE
180 * The function manager's table of internal functions.
182 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
183 * Portions Copyright (c) 1994, Regents of the University of California
185 * NOTES
187 * ******************************
188 * *** DO NOT EDIT THIS FILE! ***
189 * ******************************
191 * It has been GENERATED by $CMDNAME
192 * from $INFILE
194 *-------------------------------------------------------------------------
197 #include "postgres.h"
199 #include "utils/fmgrtab.h"
201 FuNkYfMgRtAbStUfF
203 $AWK "{ if (seenit[\$$prosrc]++ == 0)
204 print \"extern Datum\", \$$prosrc, \"(PG_FUNCTION_ARGS);\"; }" $SORTEDFILE >> "$$-$TABLEFILE"
206 if [ $? -ne 0 ]; then
207 cleanup
208 echo "$CMDNAME failed"
209 exit 1
213 cat >> "$$-$TABLEFILE" <<FuNkYfMgRtAbStUfF
215 const FmgrBuiltin fmgr_builtins[] = {
216 FuNkYfMgRtAbStUfF
218 # Note: using awk arrays to translate from pg_proc values to fmgrtab values
219 # may seem tedious, but avoid the temptation to write a quick x?y:z
220 # conditional expression instead. Not all awks have conditional expressions.
222 $AWK "BEGIN {
223 Bool[\"t\"] = \"true\";
224 Bool[\"f\"] = \"false\";
226 { printf (\" { %d, \\\"%s\\\", %d, %s, %s, %s },\\n\"),
227 \$1, \$$prosrc, \$$pronargs, Bool[\$$proisstrict], Bool[\$$proretset], \$$prosrc ;
228 }" $SORTEDFILE >> "$$-$TABLEFILE"
230 if [ $? -ne 0 ]; then
231 cleanup
232 echo "$CMDNAME failed"
233 exit 1
236 cat >> "$$-$TABLEFILE" <<FuNkYfMgRtAbStUfF
237 /* dummy entry is easier than getting rid of comma after last real one */
238 /* (not that there has ever been anything wrong with *having* a
239 comma after the last field in an array initializer) */
240 { 0, NULL, 0, false, false, NULL }
243 /* Note fmgr_nbuiltins excludes the dummy entry */
244 const int fmgr_nbuiltins = (sizeof(fmgr_builtins) / sizeof(FmgrBuiltin)) - 1;
245 FuNkYfMgRtAbStUfF
247 # We use the temporary files to avoid problems with concurrent runs
248 # (which can happen during parallel make).
249 mv "$$-$OIDSFILE" $OIDSFILE
250 mv "$$-$TABLEFILE" $TABLEFILE
252 cleanup
253 exit 0