3 # $PostgreSQL: pgsql/src/tools/pgindent/pgindent,v 1.98 2008/01/16 20:13:44 momjian Exp $
7 # Blank line is added after, seen as a function definition, no space
12 then echo "Usage: `basename $0` typedefs file [...]" 1>&2
19 trap "rm -f /tmp/$$ /tmp/$$a" 0 1 2 3 15
20 entab </dev/null >/dev/null
22 then echo "Go to the src/tools/entab directory and do a 'make' and 'make install'." >&2
23 echo "This will put the 'entab' command in your path." >&2
24 echo "Then run $0 again."
27 indent -? </dev/null >/dev/null 2>&1
29 then echo "You do not appear to have 'indent' installed on your system." >&2
32 indent -gnu </dev/null >/dev/null 2>&1
34 then echo "You appear to have GNU indent rather than BSD indent." >&2
35 echo "See the pgindent/README file for a description of its problems." >&2
36 EXTRA_OPTS="-cdb -bli0 -npcs -cli4 -sc"
37 else echo "Hope you installed /src/tools/pgindent/indent.bsd.patch." >&2
45 # Convert // comments to /* */
46 sed 's;^\([ ]*\)//\(.*\)$;\1/* \2 */;g' |
48 # Avoid bug that converts 'x =- 1' to 'x = -1'
51 # Mark some comments for special treatment later
52 sed 's;/\* *---;/*---X_X;g' |
54 # 'else' followed by a single-line comment, followed by
55 # a brace on the next line confuses BSD indent, so we push
56 # the comment down to the next line, then later pull it
57 # back up again. Add space before _PGMV or indent will add
59 sed 's;\([} ]\)else[ ]*\(/\*\)\(.*\*/\)[ ]*$;\1else\
62 # Indent multi-line after-'else' comment so BSD indent will move it properly.
63 # We already moved down single-line comments above. Check for '*' to make
64 # sure we are not in a single-line comment that has other text on the line.
65 sed 's;\([} ]\)else[ ]*\(/\*[^\*]*\)[ ]*$;\1else\
69 # Work around bug where function that defines no local variables misindents
70 # switch() case lines and line after #else. Do not do for struct/enum.
71 awk ' BEGIN {line1 = ""; line2 = ""}
80 line1 !~ /^typedef/ &&
81 line1 !~ /^extern[ ][ ]*"C"/ &&
84 print "int pgindent_func_no_var_fix;";
92 # Prevent indenting of code in 'extern "C"' blocks.
93 awk ' BEGIN {line1 = ""; line2 = ""; skips = 0}
98 if (line1 ~ /^#ifdef[ ]*__cplusplus/ &&
99 line2 ~ /^extern[ ]*"C"[ ]*$/)
103 if (getline && $0 ~ /^{[ ]*$/)
104 print "/* Open extern \"C\" */";
109 else if (line1 ~ /^#ifdef[ ]*__cplusplus/ &&
113 print "/* Close extern \"C\" */";
118 if (skips == 0 && NR >= 2)
123 if (NR >= 1 && skips <= 1)
127 # Protect backslashes in DATA().
128 sed 's;^DATA(.*$;/*&*/;' |
130 # Protect wrapping in CATALOG().
131 sed 's;^CATALOG(.*$;/*&*/;' >/tmp/$$a
133 # We get the list of typedef's from /src/tools/find_typedef
134 indent -bad -bap -bc -bl -d0 -cdb -nce -nfc1 -di12 -i4 -l79 \
135 -lp -nip -npro -bbb $EXTRA_OPTS \
136 `cat "$TYPEDEFS" | sed 's/.*/-T& /'` \
137 /tmp/$$a >/tmp/$$ 2>&1
139 if [ "$?" -ne 0 -o -s /tmp/$$ ]
146 # Restore DATA/CATALOG lines.
147 sed 's;^/\*\(DATA(.*\)\*/$;\1;' |
148 sed 's;^/\*\(CATALOG(.*\)\*/$;\1;' |
150 # Remove tabs and retab with four spaces.
153 sed 's;^/\* Open extern \"C\" \*/$;{;' |
154 sed 's;^/\* Close extern \"C\" \*/$;};' |
155 sed 's;/\*---X_X;/* ---;g' |
157 # Workaround indent bug for 'static'.
158 sed 's;^static[ ][ ]*;static ;g' |
160 # Remove too much indenting after closing brace.
161 sed 's;^} [ ]*;} ;' |
163 # Indent single-line after-'else' comment by only one tab.
164 sed 's;\([} ]\)else[ ]*\(/\*.*\*/\)[ ]*$;\1else \2;g' |
166 # Pull in #endif comments.
167 sed 's;^#endif[ ][ ]*/\*;#endif /*;' |
169 # Work around misindenting of function with no variables defined.
172 if ($0 ~ /^[ ]*int[ ]*pgindent_func_no_var_fix;/)
174 if (getline && $0 != "")
180 # Add space after comments that start on tab stops.
181 sed 's;\([^ ]\)\(/\*.*\*/\)$;\1 \2;' |
183 # Move trailing * in function return type.
184 sed 's;^\([A-Za-z_][^ ]*\)[ ][ ]*\*$;\1 *;' |
186 # Remove un-needed braces around single statements.
187 # Do not use because it uglifies PG_TRY/PG_CATCH blocks and probably
188 # isn't needed for general use.
194 # if (line1 ~ / *{$/ &&
195 # line2 ~ / *[^;{}]*;$/ &&
204 # if (skips == 0 && NR >= 3)
210 # if (NR >= 2 && skips <= 1)
212 # if (NR >= 1 && skips <= 2)
216 # Remove blank line between opening brace and block comment.
222 if (line1 ~ / *{$/ &&
233 if (skips == 0 && NR >= 3)
239 if (NR >= 2 && skips <= 1)
241 if (NR >= 1 && skips <= 2)
245 # Pull up single-line comment after 'else' that was pulled down above
250 if ($0 ~ "/\* _PGMV")
253 sub(" _PGMV", "", $0);
254 # remove leading whitespace
255 sub("^[ ]*", "", $0);
256 # add comment with single tab prefix
257 print prev_line" "$0;
258 # throw away current line
271 # Remove trailing blank lines, helps with adding blank before trailing #endif.
272 awk ' BEGIN {blank_lines = 0;}
279 for (; blank_lines > 0; blank_lines--)
285 # Remove blank line before #else, #elif, and #endif.
286 awk ' BEGIN {line1 = ""; line2 = ""; skips = 0}
301 if (skips == 0 && NR >= 2)
306 if (NR >= 1 && skips <= 1)
310 # Add blank line before #endif if it is the last line in the file.
311 awk ' BEGIN {line1 = ""; line2 = ""}
319 if (NR >= 1 && line2 ~ /^#endif/)
324 # Move prototype names to the same line as return type. Useful for ctags.
325 # Indent should do this, but it does not. It formats prototypes just
326 # like real functions.
327 awk ' BEGIN {paren_level = 0}
329 if ($0 ~ /^[a-zA-Z_][a-zA-Z_0-9]*[^\(]*$/)
332 saved_lines[++saved_len] = $0;
333 if ((getline saved_lines[++saved_len]) == 0)
334 print saved_lines[1];
336 if (saved_lines[saved_len] !~ /^[a-zA-Z_][a-zA-Z_0-9]*\(/ ||
337 saved_lines[saved_len] ~ /^[a-zA-Z_][a-zA-Z_0-9]*\(.*\)$/ ||
338 saved_lines[saved_len] ~ /^[a-zA-Z_][a-zA-Z_0-9]*\(.*\);$/)
340 print saved_lines[1];
341 print saved_lines[2];
347 if ((getline saved_lines[++saved_len]) == 0)
349 if (saved_lines[saved_len] ~ /^[^ ]/ ||
350 saved_lines[saved_len] !~ /,$/)
353 for (i=1; i <= saved_len; i++)
355 if (i == 1 && saved_lines[saved_len] ~ /\);$/)
357 printf "%s", saved_lines[i];
358 if (substr(saved_lines[i], length(saved_lines[i]),1) != "*")
361 else print saved_lines[i];
368 # Fix indenting of typedef caused by __cplusplus in libpq-fe.h.
370 if echo "$FILE" | grep -q 'libpq-fe.h$'
371 then sed 's/^[ ]*typedef enum/typedef enum/'
376 cat >/tmp/$$ && cat /tmp/$$ >"$FILE"
379 # The 'for' loop makes these backup files useless so delete them