1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
#!/bin/sh
# $PostgreSQL: pgsql/src/tools/pgindent/pgindent,v 1.100 2008/11/03 15:56:47 momjian Exp $
# Known bugs:
#
# Blank line is added after, seen as a function definition, no space
# after *:
# y = (int) x *y;
if [ "$#" -lt 2 ]
then echo "Usage: $(basename $0) typedefs file [...]" 1>&2
exit 1
fi
TYPEDEFS="$1"
shift
trap "rm -f /tmp/$$ /tmp/$$a" 0 1 2 3 15
entab </dev/null >/dev/null
if [ "$?" -ne 0 ]
then echo "Go to the src/tools/entab directory and do a 'make' and 'make install'." >&2
echo "This will put the 'entab' command in your path." >&2
echo "Then run $0 again."
exit 1
fi
indent -? </dev/null >/dev/null 2>&1
if [ "$?" -ne 1 ]
then echo "You do not appear to have 'indent' installed on your system." >&2
exit 1
fi
indent -gnu </dev/null >/dev/null 2>&1
if [ "$?" -eq 0 ]
then echo "You appear to have GNU indent rather than BSD indent." >&2
echo "See the pgindent/README file for a description of its problems." >&2
EXTRA_OPTS="-cdb -bli0 -npcs -cli4 -sc"
else echo "Hope you installed /src/tools/pgindent/indent.bsd.patch." >&2
EXTRA_OPTS="-cli1"
fi
for FILE
do
cat "$FILE" |
# Convert // comments to /* */
sed 's;^\([ ]*\)//\(.*\)$;\1/* \2 */;g' |
# Avoid bug that converts 'x =- 1' to 'x = -1'
sed 's;=- ;-= ;g' |
# Mark some comments for special treatment later
sed 's;/\* *---;/*---X_X;g' |
# 'else' followed by a single-line comment, followed by
# a brace on the next line confuses BSD indent, so we push
# the comment down to the next line, then later pull it
# back up again. Add space before _PGMV or indent will add
# it for us.
sed 's;\([} ]\)else[ ]*\(/\*\)\(.*\*/\)[ ]*$;\1else\
\2 _PGMV\3;g' |
# Indent multi-line after-'else' comment so BSD indent will move it properly.
# We already moved down single-line comments above. Check for '*' to make
# sure we are not in a single-line comment that has other text on the line.
sed 's;\([} ]\)else[ ]*\(/\*[^\*]*\)[ ]*$;\1else\
\2;g' |
detab -t4 -qc |
# Work around bug where function that defines no local variables misindents
# switch() case lines and line after #else. Do not do for struct/enum.
awk ' BEGIN {line1 = ""; line2 = ""}
{
line2 = $0;
if (NR >= 2)
print line1;
if (NR >= 2 &&
line2 ~ /^{[ ]*$/ &&
line1 !~ /^struct/ &&
line1 !~ /^enum/ &&
line1 !~ /^typedef/ &&
line1 !~ /^extern[ ][ ]*"C"/ &&
line1 !~ /=/ &&
line1 ~ /\)/)
print "int pgindent_func_no_var_fix;";
line1 = line2;
}
END {
if (NR >= 1)
print line1;
}' |
# Prevent indenting of code in 'extern "C"' blocks.
awk ' BEGIN {line1 = ""; line2 = ""; skips = 0}
{
line2 = $0;
if (skips > 0)
skips--;
if (line1 ~ /^#ifdef[ ]*__cplusplus/ &&
line2 ~ /^extern[ ]*"C"[ ]*$/)
{
print line1;
print line2;
if (getline && $0 ~ /^{[ ]*$/)
print "/* Open extern \"C\" */";
else print $0;
line2 = "";
skips = 2;
}
else if (line1 ~ /^#ifdef[ ]*__cplusplus/ &&
line2 ~ /^}[ ]*$/)
{
print line1;
print "/* Close extern \"C\" */";
line2 = "";
skips = 2;
}
else
if (skips == 0 && NR >= 2)
print line1;
line1 = line2;
}
END {
if (NR >= 1 && skips <= 1)
print line1;
}' |
# Protect backslashes in DATA().
sed 's;^DATA(.*$;/*&*/;' |
# Protect wrapping in CATALOG().
sed 's;^CATALOG(.*$;/*&*/;' >/tmp/$$a
# We get the list of typedef's from /src/tools/find_typedef
indent -bad -bap -bc -bl -d0 -cdb -nce -nfc1 -di12 -i4 -l79 \
-lp -nip -npro -bbb $EXTRA_OPTS \
`cat "$TYPEDEFS" | sed -e '/^$/d' -e 's/.*/-T& /'` \
/tmp/$$a >/tmp/$$ 2>&1
if [ "$?" -ne 0 -o -s /tmp/$$ ]
then echo
echo "$FILE"
cat /tmp/$$
fi
cat /tmp/$$a |
# Restore DATA/CATALOG lines.
sed 's;^/\*\(DATA(.*\)\*/$;\1;' |
sed 's;^/\*\(CATALOG(.*\)\*/$;\1;' |
# Remove tabs and retab with four spaces.
detab -t8 -qc |
entab -t4 -qc |
sed 's;^/\* Open extern \"C\" \*/$;{;' |
sed 's;^/\* Close extern \"C\" \*/$;};' |
sed 's;/\*---X_X;/* ---;g' |
# Workaround indent bug for 'static'.
sed 's;^static[ ][ ]*;static ;g' |
# Remove too much indenting after closing brace.
sed 's;^} [ ]*;} ;' |
# Indent single-line after-'else' comment by only one tab.
sed 's;\([} ]\)else[ ]*\(/\*.*\*/\)[ ]*$;\1else \2;g' |
# Pull in #endif comments.
sed 's;^#endif[ ][ ]*/\*;#endif /*;' |
# Work around misindenting of function with no variables defined.
awk '
{
if ($0 ~ /^[ ]*int[ ]*pgindent_func_no_var_fix;/)
{
if (getline && $0 != "")
print $0;
}
else print $0;
}' |
# Add space after comments that start on tab stops.
sed 's;\([^ ]\)\(/\*.*\*/\)$;\1 \2;' |
# Move trailing * in function return type.
sed 's;^\([A-Za-z_][^ ]*\)[ ][ ]*\*$;\1 *;' |
# Remove un-needed braces around single statements.
# Do not use because it uglifies PG_TRY/PG_CATCH blocks and probably
# isn't needed for general use.
# awk '
# {
# line3 = $0;
# if (skips > 0)
# skips--;
# if (line1 ~ / *{$/ &&
# line2 ~ / *[^;{}]*;$/ &&
# line3 ~ / *}$/)
# {
# print line2;
# line2 = "";
# line3 = "";
# skips = 3;
# }
# else
# if (skips == 0 && NR >= 3)
# print line1;
# line1 = line2;
# line2 = line3;
# }
# END {
# if (NR >= 2 && skips <= 1)
# print line1;
# if (NR >= 1 && skips <= 2)
# print line2;
# }' |
# Remove blank line between opening brace and block comment.
awk '
{
line3 = $0;
if (skips > 0)
skips--;
if (line1 ~ / *{$/ &&
line2 ~ /^$/ &&
line3 ~ / *\/\*$/)
{
print line1;
print line3;
line2 = "";
line3 = "";
skips = 3;
}
else
if (skips == 0 && NR >= 3)
print line1;
line1 = line2;
line2 = line3;
}
END {
if (NR >= 2 && skips <= 1)
print line1;
if (NR >= 1 && skips <= 2)
print line2;
}' |
# Pull up single-line comment after 'else' that was pulled down above
awk '
{
if (NR != 1)
{
if ($0 ~ "/\* _PGMV")
{
# remove tag
sub(" _PGMV", "", $0);
# remove leading whitespace
sub("^[ ]*", "", $0);
# add comment with single tab prefix
print prev_line" "$0;
# throw away current line
getline;
}
else
print prev_line;
}
prev_line = $0;
}
END {
if (NR >= 1)
print prev_line;
}' |
# Remove trailing blank lines, helps with adding blank before trailing #endif.
awk ' BEGIN {blank_lines = 0;}
{
line1 = $0;
if (line1 ~ /^$/)
blank_lines++;
else
{
for (; blank_lines > 0; blank_lines--)
printf "\n";
print line1;
}
}' |
# Remove blank line before #else, #elif, and #endif.
awk ' BEGIN {line1 = ""; line2 = ""; skips = 0}
{
line2 = $0;
if (skips > 0)
skips--;
if (line1 ~ /^$/ &&
(line2 ~ /^#else/ ||
line2 ~ /^#elif/ ||
line2 ~ /^#endif/))
{
print line2;
line2 = "";
skips = 2;
}
else
if (skips == 0 && NR >= 2)
print line1;
line1 = line2;
}
END {
if (NR >= 1 && skips <= 1)
print line1;
}' |
# Add blank line before #endif if it is the last line in the file.
awk ' BEGIN {line1 = ""; line2 = ""}
{
line2 = $0;
if (NR >= 2)
print line1;
line1 = line2;
}
END {
if (NR >= 1 && line2 ~ /^#endif/)
printf "\n";
print line1;
}' |
# Move prototype names to the same line as return type. Useful for ctags.
# Indent should do this, but it does not. It formats prototypes just
# like real functions.
awk ' BEGIN {paren_level = 0}
{
if ($0 ~ /^[a-zA-Z_][a-zA-Z_0-9]*[^\(]*$/)
{
saved_len = 0;
saved_lines[++saved_len] = $0;
if ((getline saved_lines[++saved_len]) == 0)
print saved_lines[1];
else
if (saved_lines[saved_len] !~ /^[a-zA-Z_][a-zA-Z_0-9]*\(/ ||
saved_lines[saved_len] ~ /^[a-zA-Z_][a-zA-Z_0-9]*\(.*\)$/ ||
saved_lines[saved_len] ~ /^[a-zA-Z_][a-zA-Z_0-9]*\(.*\);$/)
{
print saved_lines[1];
print saved_lines[2];
}
else
{
while (1)
{
if ((getline saved_lines[++saved_len]) == 0)
break;
if (saved_lines[saved_len] ~ /^[^ ]/ ||
saved_lines[saved_len] !~ /,$/)
break;
}
for (i=1; i <= saved_len; i++)
{
if (i == 1 && saved_lines[saved_len] ~ /\);$/)
{
printf "%s", saved_lines[i];
if (substr(saved_lines[i], length(saved_lines[i]),1) != "*")
printf " ";
}
else print saved_lines[i];
}
}
}
else print $0;
}' |
# Fix indenting of typedef caused by __cplusplus in libpq-fe.h.
(
if echo "$FILE" | grep -q 'libpq-fe.h$'
then sed 's/^[ ]*typedef enum/typedef enum/'
else cat
fi
) |
# end
cat >/tmp/$$ && cat /tmp/$$ >"$FILE"
done
# The 'for' loop makes these backup files useless so delete them
rm -f *a.BAK
|