From 6e22ba03a9fe6853e812268ef404e45aac4866ab Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Wed, 12 Oct 2011 15:45:46 -0400
Subject: [PATCH] Modify pgindent to use a renamed pg_bsd_indent binary.  New
 features include the ability to supply a typedef file, rather than list them
 on the command line.  Also improve the README.

---
 src/tools/pgindent/README           |  39 ++++--
 src/tools/pgindent/indent.bsd.patch | 198 ++++++++++++++++++++++------
 src/tools/pgindent/pgindent         |  19 ++-
 3 files changed, 194 insertions(+), 62 deletions(-)

diff --git a/src/tools/pgindent/README b/src/tools/pgindent/README
index e81e85d15b8..7504650292b 100644
--- a/src/tools/pgindent/README
+++ b/src/tools/pgindent/README
@@ -6,26 +6,28 @@ pgindent
 This can format all PostgreSQL *.c and *.h files, but excludes *.y, and
 *.l files.
 
-1) Change directory to the top of the build tree.
+1) Install pg_bsd_indent (see below for details)
 
-2) Download the typedef file from the buildfarm:
+2) Change directory to the top of the build tree.
+
+3) Download the typedef file from the buildfarm:
 
 	wget -O src/tools/pgindent/typedefs.list http://buildfarm.postgresql.org/cgi-bin/typedefs.pl
 
-3) Remove all derived files (pgindent has trouble with one of the flex macros):
+4) Remove all derived files (pgindent has trouble with one of the flex macros):
 
 	gmake maintainer-clean
 
-4) Run pgindent:
+5) Run pgindent:
 
 	find . -name '*.[ch]' -type f -print | \
 	egrep -v -f src/tools/pgindent/exclude_file_patterns | \
 	xargs -n100 pgindent src/tools/pgindent/typedefs.list
 
-5) Remove any files that generate errors and restore their original
+6) Remove any files that generate errors and restore their original
    versions.
 
-6) Do a full test build:
+7) Do a full test build:
 
 	run configure
 	# stop is only necessary if it's going to install in a location with an
@@ -38,17 +40,28 @@ This can format all PostgreSQL *.c and *.h files, but excludes *.y, and
 
 ---------------------------------------------------------------------------
 
-We have standardized on NetBSD's indent.  We have fixed a few bugs which
-requre the NetBSD source to be patched with indent.bsd.patch patch.  A
-fully patched version is available at ftp://ftp.postgresql.org/pub/dev.
+BSD indent
+----------
+
+We have standardized on NetBSD's indent, and renamed it pg_bsd_indent. 
+We have fixed a few bugs which requre the NetBSD source to be patched
+with indent.bsd.patch patch.  A fully patched version is available at
+ftp://ftp.postgresql.org/pub/dev.
 
 GNU indent, version 2.2.6, has several problems, and is not recommended.
 These bugs become pretty major when you are doing >500k lines of code.
 If you don't believe me, take a directory and make a copy.  Run pgindent
 on the copy using GNU indent, and do a diff -r. You will see what I
-mean. GNU indent does some things better, but mangles too.
+mean. GNU indent does some things better, but mangles too.  For details,
+see:
+
+	http://archives.postgresql.org/pgsql-hackers/2003-10/msg00374.php
+	http://archives.postgresql.org/pgsql-hackers/2011-04/msg01436.php
+
+---------------------------------------------------------------------------
 
-Notes about excluded files:
+Notes about excluded files
+--------------------------
 
 src/include/storage/s_lock.h is excluded because it contains assembly code
 that pgindent tends to mess up.
@@ -63,8 +76,8 @@ should not be changed.
 
 ---------------------------------------------------------------------------
 
-Obsolete typedef list creation instructions:
---------------------------------------------
+Obsolete typedef list creation instructions
+-------------------------------------------
 
 To use pgindent:
 
diff --git a/src/tools/pgindent/indent.bsd.patch b/src/tools/pgindent/indent.bsd.patch
index e10b9973f64..a7d14533895 100644
--- a/src/tools/pgindent/indent.bsd.patch
+++ b/src/tools/pgindent/indent.bsd.patch
@@ -1,17 +1,45 @@
-src/tools/pgindent/indent.bsd.patch
-
-This patch contains several fixes to NetBSD's indent and should be
-applied before using pgindent.
-
----------------------------------------------------------------------------
-
-Index: README
-===================================================================
-RCS file: /cvsroot/src/usr.bin/indent/README,v
-retrieving revision 1.1
-diff -c -r1.1 README
-*** README	9 Apr 1993 12:59:06 -0000	1.1
---- README	15 Nov 2005 00:25:43 -0000
+diff -c -r bsd_indent/Makefile pg_bsd_indent/Makefile
+*** bsd_indent/Makefile	Mon Nov 14 19:30:11 2005
+--- pg_bsd_indent/Makefile	Wed Oct 12 12:17:12 2011
+***************
+*** 2,10 ****
+  # Makefile
+  #
+  #
+! TARGET = indent
+  XFLAGS = -Wall -D__RCSID="static char *rcsid=" -D__COPYRIGHT="static char *copyright="
+! CFLAGS = -g 
+  LIBS = 
+  
+  $(TARGET) : args.o indent.o io.o lexi.o parse.o pr_comment.o
+--- 2,10 ----
+  # Makefile
+  #
+  #
+! TARGET = pg_bsd_indent
+  XFLAGS = -Wall -D__RCSID="static char *rcsid=" -D__COPYRIGHT="static char *copyright="
+! CFLAGS = -O
+  LIBS = 
+  
+  $(TARGET) : args.o indent.o io.o lexi.o parse.o pr_comment.o
+***************
+*** 31,37 ****
+  clean:
+  	rm -f *.o $(TARGET) log core
+  
+! install:
+! 	make clean
+! 	make CFLAGS=-O
+  	install -s -o bin -g bin $(TARGET) /usr/local/bin
+--- 31,35 ----
+  clean:
+  	rm -f *.o $(TARGET) log core
+  
+! install: $(TARGET)
+  	install -s -o bin -g bin $(TARGET) /usr/local/bin
+diff -c -r bsd_indent/README pg_bsd_indent/README
+*** bsd_indent/README	Wed Oct 12 11:51:58 2011
+--- pg_bsd_indent/README	Mon Nov 14 19:30:24 2005
 ***************
 *** 1,3 ****
 --- 1,13 ----
@@ -28,13 +56,107 @@ diff -c -r1.1 README
   This is the C indenter, it originally came from the University of Illinois
   via some distribution tape for PDP-11 Unix.  It has subsequently been
   hacked upon by James Gosling @ CMU.  It isn't very pretty, and really needs
-Index: indent_globs.h
-===================================================================
-RCS file: /cvsroot/src/usr.bin/indent/indent_globs.h,v
-retrieving revision 1.8
-diff -c -r1.8 indent_globs.h
-*** indent_globs.h	7 Aug 2003 11:14:08 -0000	1.8
---- indent_globs.h	15 Nov 2005 00:25:44 -0000
+diff -c -r bsd_indent/args.c pg_bsd_indent/args.c
+*** bsd_indent/args.c	Mon Nov 14 19:30:00 2005
+--- pg_bsd_indent/args.c	Wed Oct 12 12:30:06 2011
+***************
+*** 83,88 ****
+--- 83,90 ----
+  #include <string.h>
+  #include "indent_globs.h"
+  
++ #define INDENT_PG_VERSION	"1.0"
++ 
+  /* profile types */
+  #define	PRO_SPECIAL	1	/* special case */
+  #define	PRO_BOOL	2	/* boolean */
+***************
+*** 99,106 ****
+--- 101,113 ----
+  #define	STDIN		3	/* use stdin */
+  #define	KEY		4	/* type (keyword) */
+  
++ #define	KEY_FILE		5	/* only used for args */
++ #define VERSION			6	/* only used for args */
++ 
+  char   *option_source = "?";
+  
++ void add_typedefs_from_file(char *str);
++ 
+  /*
+   * N.B.: because of the way the table here is scanned, options whose names are
+   * substrings of other options must occur later; that is, with -lp vs -l, -lp
+***************
+*** 118,123 ****
+--- 125,136 ----
+  		"T", PRO_SPECIAL, 0, KEY, 0
+  	},
+  	{
++ 		"U", PRO_SPECIAL, 0, KEY_FILE, 0
++ 	},
++ 	{
++ 		"V", PRO_SPECIAL, 0, VERSION, 0
++ 	},
++ 	{
+  		"bacc", PRO_BOOL, false, ON, &blanklines_around_conditional_compilation
+  	},
+  	{
+***************
+*** 425,430 ****
+--- 438,456 ----
+  			}
+  			break;
+  
++ 		case KEY_FILE:
++ 			if (*param_start == 0)
++ 				goto need_param;
++ 			add_typedefs_from_file(param_start);
++ 			break;
++ 
++ 		case VERSION:
++ 			{
++ 				printf("pg_bsd_indent %s\n", INDENT_PG_VERSION);
++ 				exit(0);
++ 			}
++ 			break;
++ 
+  		default:
+  			fprintf(stderr, "\
+  indent: set_option: internal error: p_special %d\n", p->p_special);
+***************
+*** 459,461 ****
+--- 485,509 ----
+  		exit(1);
+  	}
+  }
++ 
++ 
++ void
++ add_typedefs_from_file(char *str)
++ {
++ 	FILE *file;
++ 	char line[BUFSIZ];
++ 	
++ 	if ((file = fopen(param_start, "r")) == NULL)
++ 	{
++ 		fprintf(stderr, "indent: cannot open file %s\n", str);
++ 		exit(1);
++ 	}
++ 	while ((fgets(line, BUFSIZ, file)) != NULL)
++ 	{
++ 		/* Remove trailing whitespace */
++ 		if (strstr(line, " \t\n\r") != NULL)
++ 			*strstr(line, " \t\n\r") = '\0';
++ 		addkey(strdup(line), 4);
++ 	}
++ 	fclose(file);
++ }
+Only in pg_bsd_indent/: args.o
+Only in bsd_indent/: indent.bsd.patch
+Only in pg_bsd_indent/: indent.o
+diff -c -r bsd_indent/indent_globs.h pg_bsd_indent/indent_globs.h
+*** bsd_indent/indent_globs.h	Wed Oct 12 11:51:58 2011
+--- pg_bsd_indent/indent_globs.h	Mon Nov 14 19:30:24 2005
 ***************
 *** 239,245 ****
           scomf,			/* Same line comment font */
@@ -56,13 +178,10 @@ diff -c -r1.8 indent_globs.h
   
   EXTERN struct parser_state {
   	int     last_token;
-Index: lexi.c
-===================================================================
-RCS file: /cvsroot/src/usr.bin/indent/lexi.c,v
-retrieving revision 1.12
-diff -c -r1.12 lexi.c
-*** lexi.c	7 Aug 2003 11:14:09 -0000	1.12
---- lexi.c	15 Nov 2005 00:25:44 -0000
+Only in pg_bsd_indent/: io.o
+diff -c -r bsd_indent/lexi.c pg_bsd_indent/lexi.c
+*** bsd_indent/lexi.c	Wed Oct 12 11:51:58 2011
+--- pg_bsd_indent/lexi.c	Mon Nov 14 19:30:24 2005
 ***************
 *** 93,99 ****
   	int     rwcode;
@@ -102,13 +221,10 @@ diff -c -r1.12 lexi.c
   	p->rwd = key;
   	p->rwcode = val;
   	p[1].rwd = 0;
-Index: parse.c
-===================================================================
-RCS file: /cvsroot/src/usr.bin/indent/parse.c,v
-retrieving revision 1.7
-diff -c -r1.7 parse.c
-*** parse.c	7 Aug 2003 11:14:09 -0000	1.7
---- parse.c	15 Nov 2005 00:25:44 -0000
+Only in pg_bsd_indent/: lexi.o
+diff -c -r bsd_indent/parse.c pg_bsd_indent/parse.c
+*** bsd_indent/parse.c	Wed Oct 12 11:51:58 2011
+--- pg_bsd_indent/parse.c	Mon Nov 14 19:30:24 2005
 ***************
 *** 231,236 ****
 --- 231,241 ----
@@ -123,13 +239,10 @@ diff -c -r1.7 parse.c
   	reduce();		/* see if any reduction can be done */
   
   #ifdef debug
-Index: pr_comment.c
-===================================================================
-RCS file: /cvsroot/src/usr.bin/indent/pr_comment.c,v
-retrieving revision 1.9
-diff -c -r1.9 pr_comment.c
-*** pr_comment.c	7 Aug 2003 11:14:09 -0000	1.9
---- pr_comment.c	15 Nov 2005 00:25:44 -0000
+Only in pg_bsd_indent/: parse.o
+diff -c -r bsd_indent/pr_comment.c pg_bsd_indent/pr_comment.c
+*** bsd_indent/pr_comment.c	Wed Oct 12 11:51:58 2011
+--- pg_bsd_indent/pr_comment.c	Mon Nov 14 19:30:24 2005
 ***************
 *** 148,154 ****
   		ps.box_com = true;
@@ -173,3 +286,4 @@ diff -c -r1.9 pr_comment.c
   			} else
   				if (++buf_ptr >= buf_end)
   					fill_buffer();
+Only in pg_bsd_indent/: pr_comment.o
diff --git a/src/tools/pgindent/pgindent b/src/tools/pgindent/pgindent
index 05f69ef1f3a..eeb6f5d2a28 100755
--- a/src/tools/pgindent/pgindent
+++ b/src/tools/pgindent/pgindent
@@ -21,12 +21,12 @@ fi
 TYPEDEFS="$1"
 shift
 
-if [ -z "$INDENT" ]
-then
-	INDENT=indent
-fi
+[ -z "$INDENT" ] && INDENT=pg_bsd_indent
 
 trap "rm -f /tmp/$$ /tmp/$$a" 0 1 2 3 15
+
+# check the environment
+
 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
@@ -36,7 +36,11 @@ then	echo "Go to the src/tools/entab directory and do a 'make' and 'make install
 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
+then	echo "You do not appear to have '$INDENT' installed on your system." >&2
+	exit 1
+fi
+if [ "`$INDENT -V`" != "$INDENT 1.0" ]
+then	echo "You do not appear to have $INDENT version 1.0 installed on your system." >&2
 	exit 1
 fi
 $INDENT -gnu </dev/null >/dev/null 2>&1
@@ -140,10 +144,11 @@ do
 # Protect wrapping in CATALOG().
 	sed 's;^CATALOG(.*$;/*&*/;' >/tmp/$$a
 
+	egrep -v '^(FD_SET|date|interval|timestamp|ANY)$' "$TYPEDEFS" | sed -e '/^$/d' > /tmp/$$b
+
 # 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 \
-		`egrep -v '^(FD_SET|date|interval|timestamp|ANY)$' "$TYPEDEFS" | sed -e '/^$/d' -e 's/.*/-T& /'` \
+		-lp -nip -npro -bbb $EXTRA_OPTS -U/tmp/$$b \
 		/tmp/$$a >/tmp/$$ 2>&1
 
 	if [ "$?" -ne 0 -o -s /tmp/$$ ]
-- 
GitLab