Attached is a WIP possible replacement for pgindent. Instead of a shell script invoking a mishmash of awk and sed, some of which is pretty impenetrable, it uses a single engine (perl) to do all the pre and post indent processing. Of course, if your regex-fu and perl-fu is not up the scratch this too might be impenetrable, but all but a couple of the recipes are reduced to single lines, and I'd argue that they are all at least as comprehensible as what they replace.
Attached also is a diff file showing what it does differently from the existing script. I think that these are all things where the new script is more correct than the existing script. Most of the changes come into two categories:
* places where the existing script fails to combine the function return type and the function name on a single line in function prototypes. * places where unwanted blank lines are removed by the new script but not by the existing script. Features include: * command line compatibility with the existing script, so you can do: find ../../.. -name '*.[ch]' -type f -print | egrep -v -f exclude_file_patterns | xargs -n100 ./pgindent.pl typedefs.list * a new way of doing the same thing much more nicely: ./pgindent.pl --search-base=../../.. --typedefs=typedefs.list --excludes=exclude_file_patterns * only passes relevant typedefs to indent, not the whole huge list * should in principle be runnable on Windows, unlike existing script (I haven't tested yet) * no semantic tab literals; tabs are only generated using \t and tested for using \t, \h or \s as appropriate. This makes debugging the script much less frustrating. If something looks like a space it should be a space.In one case I used perl's extended regex mode to comment a fairly hairy regex. This should probably be done a bit more, maybe for all of them.
If anybody is so inclined, this could be used as a basis for removing the use of bsd indent altogether, as has been suggested before, as well as external entab/detab.
Comments welcome. cheers andrew
pgindent.pl
Description: Perl program
diff --git a/contrib/btree_gist/btree_bit.c b/contrib/btree_gist/btree_bit.c index 8675d24..f69faaa 100644 --- a/contrib/btree_gist/btree_bit.c +++ b/contrib/btree_gist/btree_bit.c @@ -95,7 +95,6 @@ gbt_bit_xfrm(bytea *leaf) static GBT_VARKEY * gbt_bit_l2n(GBT_VARKEY *leaf) { - GBT_VARKEY *out = leaf; GBT_VARKEY_R r = gbt_var_key_readable(leaf); bytea *o; diff --git a/contrib/btree_gist/btree_text.c b/contrib/btree_gist/btree_text.c index 3d4f8c1..b55dc7c 100644 --- a/contrib/btree_gist/btree_text.c +++ b/contrib/btree_gist/btree_text.c @@ -119,7 +119,6 @@ gbt_text_compress(PG_FUNCTION_ARGS) Datum gbt_bpchar_compress(PG_FUNCTION_ARGS) { - GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); GISTENTRY *retval; diff --git a/contrib/btree_gist/btree_ts.c b/contrib/btree_gist/btree_ts.c index 9d3a591..185bed0 100644 --- a/contrib/btree_gist/btree_ts.c +++ b/contrib/btree_gist/btree_ts.c @@ -380,7 +380,6 @@ gbt_ts_union(PG_FUNCTION_ARGS) Datum gbt_ts_penalty(PG_FUNCTION_ARGS) { - tsKEY *origentry = (tsKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key); tsKEY *newentry = (tsKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key); float *result = (float *) PG_GETARG_POINTER(2); diff --git a/contrib/btree_gist/btree_utils_num.c b/contrib/btree_gist/btree_utils_num.c index a3da580..8b8cd31 100644 --- a/contrib/btree_gist/btree_utils_num.c +++ b/contrib/btree_gist/btree_utils_num.c @@ -134,7 +134,6 @@ gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_nin bool gbt_num_same(const GBT_NUMKEY *a, const GBT_NUMKEY *b, const gbtree_ninfo *tinfo) { - GBT_NUMKEY_R b1, b2; @@ -156,7 +155,6 @@ gbt_num_same(const GBT_NUMKEY *a, const GBT_NUMKEY *b, const gbtree_ninfo *tinfo void gbt_num_bin_union(Datum *u, GBT_NUMKEY *e, const gbtree_ninfo *tinfo) { - GBT_NUMKEY_R rd; rd.lower = &e[0]; diff --git a/contrib/btree_gist/btree_utils_var.c b/contrib/btree_gist/btree_utils_var.c index e73799b..dc3ad38 100644 --- a/contrib/btree_gist/btree_utils_var.c +++ b/contrib/btree_gist/btree_utils_var.c @@ -54,7 +54,6 @@ gbt_var_decompress(PG_FUNCTION_ARGS) GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k) { - GBT_VARKEY_R r; r.lower = (bytea *) &(((char *) k)[VARHDRSZ]); @@ -106,7 +105,6 @@ gbt_var_leaf2node(GBT_VARKEY *leaf, const gbtree_vinfo *tinfo) static int32 gbt_var_node_cp_len(const GBT_VARKEY *node, const gbtree_vinfo *tinfo) { - GBT_VARKEY_R r = gbt_var_key_readable(node); int32 i = 0; int32 l = 0; @@ -270,7 +268,6 @@ gbt_var_bin_union(Datum *u, GBT_VARKEY *e, Oid collation, GISTENTRY * gbt_var_compress(GISTENTRY *entry, const gbtree_vinfo *tinfo) { - GISTENTRY *retval; if (entry->leafkey) @@ -299,7 +296,6 @@ GBT_VARKEY * gbt_var_union(const GistEntryVector *entryvec, int32 *size, Oid collation, const gbtree_vinfo *tinfo) { - int i = 0, numranges = entryvec->n; GBT_VARKEY *cur; diff --git a/contrib/pg_upgrade/exec.c b/contrib/pg_upgrade/exec.c index b7d8266..51bc63b 100644 --- a/contrib/pg_upgrade/exec.c +++ b/contrib/pg_upgrade/exec.c @@ -94,7 +94,6 @@ is_server_running(const char *datadir) void verify_directories(void) { - prep_status("Checking current, bin, and data directories"); if (access(".", R_OK | W_OK diff --git a/contrib/pg_upgrade/file.c b/contrib/pg_upgrade/file.c index a7e4009..01ebb80 100644 --- a/contrib/pg_upgrade/file.c +++ b/contrib/pg_upgrade/file.c @@ -131,7 +131,6 @@ linkAndUpdateFile(pageCnvCtx *pageConverter, static int copy_file(const char *srcfile, const char *dstfile, bool force) { - #define COPY_BUF_SIZE (50 * BLCKSZ) int src_fd; diff --git a/contrib/pg_upgrade/page.c b/contrib/pg_upgrade/page.c index 22a587f..e84690a 100644 --- a/contrib/pg_upgrade/page.c +++ b/contrib/pg_upgrade/page.c @@ -173,6 +173,4 @@ loadConverterPlugin(uint16 newPageVersion, uint16 oldPageVersion) } } - - #endif diff --git a/contrib/pgcrypto/imath.h b/contrib/pgcrypto/imath.h index f2b02d0..cd48c14 100644 --- a/contrib/pgcrypto/imath.h +++ b/contrib/pgcrypto/imath.h @@ -117,14 +117,11 @@ mp_result mp_int_mul_value(mp_int a, int value, mp_int c); mp_result mp_int_mul_pow2(mp_int a, int p2, mp_int c); mp_result mp_int_sqr(mp_int a, mp_int c); /* c = a * a */ -mp_result -mp_int_div(mp_int a, mp_int b, /* q = a / b */ +mp_result mp_int_div(mp_int a, mp_int b, /* q = a / b */ mp_int q, mp_int r); /* r = a % b */ -mp_result -mp_int_div_value(mp_int a, int value, /* q = a / value */ +mp_result mp_int_div_value(mp_int a, int value, /* q = a / value */ mp_int q, int *r); /* r = a % value */ -mp_result -mp_int_div_pow2(mp_int a, int p2, /* q = a / 2^p2 */ +mp_result mp_int_div_pow2(mp_int a, int p2, /* q = a / 2^p2 */ mp_int q, mp_int r); /* r = q % 2^p2 */ mp_result mp_int_mod(mp_int a, mp_int m, mp_int c); /* c = a % m */ @@ -143,17 +140,13 @@ int mp_int_divisible_value(mp_int a, int v); /* Returns k >= 0 such that z = 2^k, if one exists; otherwise < 0 */ int mp_int_is_pow2(mp_int z); -mp_result -mp_int_exptmod(mp_int a, mp_int b, mp_int m, +mp_result mp_int_exptmod(mp_int a, mp_int b, mp_int m, mp_int c); /* c = a^b (mod m) */ -mp_result -mp_int_exptmod_evalue(mp_int a, int value, +mp_result mp_int_exptmod_evalue(mp_int a, int value, mp_int m, mp_int c); /* c = a^v (mod m) */ -mp_result -mp_int_exptmod_bvalue(int value, mp_int b, +mp_result mp_int_exptmod_bvalue(int value, mp_int b, mp_int m, mp_int c); /* c = v^b (mod m) */ -mp_result -mp_int_exptmod_known(mp_int a, mp_int b, +mp_result mp_int_exptmod_known(mp_int a, mp_int b, mp_int m, mp_int mu, mp_int c); /* c = a^b (mod m) */ mp_result mp_int_redux_const(mp_int m, mp_int c); @@ -162,8 +155,7 @@ mp_result mp_int_invmod(mp_int a, mp_int m, mp_int c); /* c = 1/a (mod m) */ mp_result mp_int_gcd(mp_int a, mp_int b, mp_int c); /* c = gcd(a, b) */ -mp_result -mp_int_egcd(mp_int a, mp_int b, mp_int c, /* c = gcd(a, b) */ +mp_result mp_int_egcd(mp_int a, mp_int b, mp_int c, /* c = gcd(a, b) */ mp_int x, mp_int y); /* c = ax + by */ mp_result mp_int_sqrt(mp_int a, mp_int c); /* c = floor(sqrt(q)) */ diff --git a/contrib/pgcrypto/pgp.h b/contrib/pgcrypto/pgp.h index 7ae01cc..3022abf 100644 --- a/contrib/pgcrypto/pgp.h +++ b/contrib/pgcrypto/pgp.h @@ -265,8 +265,7 @@ int pgp_s2k_read(PullFilter *src, PGP_S2K *s2k); int pgp_s2k_process(PGP_S2K *s2k, int cipher, const uint8 *key, int klen); typedef struct PGP_CFB PGP_CFB; -int -pgp_cfb_create(PGP_CFB **ctx_p, int algo, +int pgp_cfb_create(PGP_CFB **ctx_p, int algo, const uint8 *key, int key_len, int recync, uint8 *iv); void pgp_cfb_free(PGP_CFB *ctx); int pgp_cfb_encrypt(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst); diff --git a/src/backend/access/gin/ginbtree.c b/src/backend/access/gin/ginbtree.c index 739fa8a..3160bce 100644 --- a/src/backend/access/gin/ginbtree.c +++ b/src/backend/access/gin/ginbtree.c @@ -177,7 +177,6 @@ void ginFindParents(GinBtree btree, GinBtreeStack *stack, BlockNumber rootBlkno) { - Page page; Buffer buffer; BlockNumber blkno, diff --git a/src/backend/access/gist/gistutil.c b/src/backend/access/gist/gistutil.c index 1754a10..9736f49 100644 --- a/src/backend/access/gist/gistutil.c +++ b/src/backend/access/gist/gistutil.c @@ -242,7 +242,6 @@ gistMakeUnionKey(GISTSTATE *giststate, int attno, GISTENTRY *entry2, bool isnull2, Datum *dst, bool *dstisnull) { - int dstsize; static char storage[2 * sizeof(GISTENTRY) + GEVHDRSZ]; diff --git a/src/backend/optimizer/geqo/geqo_cx.c b/src/backend/optimizer/geqo/geqo_cx.c index afae948..9f6d5e4 100644 --- a/src/backend/optimizer/geqo/geqo_cx.c +++ b/src/backend/optimizer/geqo/geqo_cx.c @@ -47,7 +47,6 @@ int cx(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table) { - int i, start_pos, curr_pos; diff --git a/src/backend/optimizer/geqo/geqo_px.c b/src/backend/optimizer/geqo/geqo_px.c index 808ff6a..99289bc 100644 --- a/src/backend/optimizer/geqo/geqo_px.c +++ b/src/backend/optimizer/geqo/geqo_px.c @@ -46,7 +46,6 @@ void px(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table) { - int num_positions; int i, pos, diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index 726a1f4..3749668 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -1044,7 +1044,6 @@ suff_search(char *str, KeySuffix *suf, int type) static void NUMDesc_prepare(NUMDesc *num, FormatNode *n) { - if (n->type != NODE_TYPE_ACTION) return; diff --git a/src/backend/utils/adt/tsquery_rewrite.c b/src/backend/utils/adt/tsquery_rewrite.c index e2faed2..b2c61e0 100644 --- a/src/backend/utils/adt/tsquery_rewrite.c +++ b/src/backend/utils/adt/tsquery_rewrite.c @@ -47,7 +47,6 @@ addone(int *counters, int last, int total) static QTNode * findeq(QTNode *node, QTNode *ex, QTNode *subs, bool *isfind) { - if ((node->sign & ex->sign) != ex->sign || node->valnode->type != ex->valnode->type) return node; @@ -197,7 +196,6 @@ dofindsubquery(QTNode *root, QTNode *ex, QTNode *subs, bool *isfind) static QTNode * dropvoidsubtree(QTNode *root) { - if (!root) return NULL; diff --git a/src/bin/pgevent/pgevent.c b/src/bin/pgevent/pgevent.c index 1fcde86..11747ae 100644 --- a/src/bin/pgevent/pgevent.c +++ b/src/bin/pgevent/pgevent.c @@ -20,8 +20,7 @@ HANDLE g_module = NULL; /* hModule of DLL */ /* Prototypes */ -STDAPI -DllRegisterServer(void); +STDAPI DllRegisterServer(void); STDAPI DllUnregisterServer(void); BOOL WINAPI DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved); diff --git a/src/include/access/sysattr.h b/src/include/access/sysattr.h index 1b3e64a..bc52adc 100644 --- a/src/include/access/sysattr.h +++ b/src/include/access/sysattr.h @@ -27,5 +27,4 @@ #define TableOidAttributeNumber (-7) #define FirstLowInvalidHeapAttributeNumber (-8) - #endif /* SYSATTR_H */ diff --git a/src/include/storage/predicate_internals.h b/src/include/storage/predicate_internals.h index 495983f..b89e9bb 100644 --- a/src/include/storage/predicate_internals.h +++ b/src/include/storage/predicate_internals.h @@ -468,5 +468,4 @@ typedef struct TwoPhasePredicateRecord */ extern PredicateLockData *GetPredicateLockStatusData(void); - #endif /* PREDICATE_INTERNALS_H */ diff --git a/src/include/utils/guc_tables.h b/src/include/utils/guc_tables.h index a1ca012..8caddb4 100644 --- a/src/include/utils/guc_tables.h +++ b/src/include/utils/guc_tables.h @@ -256,5 +256,4 @@ extern const char *config_enum_lookup_by_value(struct config_enum * record, int extern bool config_enum_lookup_by_name(struct config_enum * record, const char *value, int *retval); - #endif /* GUC_TABLES_H */ diff --git a/src/interfaces/ecpg/compatlib/informix.c b/src/interfaces/ecpg/compatlib/informix.c index 3b30864..d6de3ea 100644 --- a/src/interfaces/ecpg/compatlib/informix.c +++ b/src/interfaces/ecpg/compatlib/informix.c @@ -311,7 +311,6 @@ deccvlong(long lng, decimal *np) int decdiv(decimal *n1, decimal *n2, decimal *result) { - int i; errno = 0; diff --git a/src/interfaces/ecpg/pgtypeslib/datetime.c b/src/interfaces/ecpg/pgtypeslib/datetime.c index 41377a6..82ce55e 100644 --- a/src/interfaces/ecpg/pgtypeslib/datetime.c +++ b/src/interfaces/ecpg/pgtypeslib/datetime.c @@ -52,7 +52,6 @@ PGTYPESdate_from_timestamp(timestamp dt) date PGTYPESdate_from_asc(char *str, char **endptr) { - date dDate; fsec_t fsec; struct tm tt, diff --git a/src/interfaces/ecpg/pgtypeslib/numeric.c b/src/interfaces/ecpg/pgtypeslib/numeric.c index 7257c81..c56dda0 100644 --- a/src/interfaces/ecpg/pgtypeslib/numeric.c +++ b/src/interfaces/ecpg/pgtypeslib/numeric.c @@ -1362,7 +1362,6 @@ done: int PGTYPESnumeric_cmp(numeric *var1, numeric *var2) { - /* use cmp_abs function to calculate the result */ /* both are positive: normal comparation with cmp_abs */ diff --git a/src/interfaces/ecpg/pgtypeslib/timestamp.c b/src/interfaces/ecpg/pgtypeslib/timestamp.c index 8354e46..9604ee4 100644 --- a/src/interfaces/ecpg/pgtypeslib/timestamp.c +++ b/src/interfaces/ecpg/pgtypeslib/timestamp.c @@ -949,7 +949,6 @@ int PGTYPEStimestamp_add_interval(timestamp * tin, interval * span, timestamp * tout) { - if (TIMESTAMP_NOT_FINITE(*tin)) *tout = *tin; diff --git a/src/interfaces/libpq/fe-print.c b/src/interfaces/libpq/fe-print.c index 5fa3be0..d8c62a1 100644 --- a/src/interfaces/libpq/fe-print.c +++ b/src/interfaces/libpq/fe-print.c @@ -330,7 +330,6 @@ do_field(const PQprintOpt *po, const PGresult *res, unsigned char *fieldNotNum, int *fieldMax, const int fieldMaxLen, FILE *fout) { - const char *pval, *p; int plen; @@ -442,7 +441,6 @@ do_header(FILE *fout, const PQprintOpt *po, const int nFields, int *fieldMax, const char **fieldNames, unsigned char *fieldNotNum, const int fs_len, const PGresult *res) { - int j; /* for loop index */ char *border = NULL; @@ -529,7 +527,6 @@ output_row(FILE *fout, const PQprintOpt *po, const int nFields, char **fields, unsigned char *fieldNotNum, int *fieldMax, char *border, const int row_index) { - int field_index; /* for loop index */ if (po->html3) diff --git a/src/interfaces/libpq/libpq-events.h b/src/interfaces/libpq/libpq-events.h index 05417d0..4a3740c 100644 --- a/src/interfaces/libpq/libpq-events.h +++ b/src/interfaces/libpq/libpq-events.h @@ -24,7 +24,7 @@ extern "C" #endif /* Callback Event Ids */ - typedef enum +typedef enum { PGEVT_REGISTER, PGEVT_CONNRESET, diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h index d780275..80ada5e 100644 --- a/src/interfaces/libpq/libpq-fe.h +++ b/src/interfaces/libpq/libpq-fe.h @@ -504,24 +504,21 @@ extern unsigned char *PQescapeBytea(const unsigned char *from, size_t from_lengt /* === in fe-print.c === */ -extern void -PQprint(FILE *fout, /* output stream */ +extern void PQprint(FILE *fout, /* output stream */ const PGresult *res, const PQprintOpt *ps); /* option structure */ /* * really old printing routines */ -extern void -PQdisplayTuples(const PGresult *res, +extern void PQdisplayTuples(const PGresult *res, FILE *fp, /* where to send the output */ int fillAlign, /* pad the fields with spaces */ const char *fieldSep, /* field separator */ int printHeader, /* display headers? */ int quiet); -extern void -PQprintTuples(const PGresult *res, +extern void PQprintTuples(const PGresult *res, FILE *fout, /* output stream */ int printAttName, /* print attribute names */ int terseOutput, /* delimiter bars */ diff --git a/src/pl/plperl/plperl.h b/src/pl/plperl/plperl.h index c4810cb..07ff81e 100644 --- a/src/pl/plperl/plperl.h +++ b/src/pl/plperl/plperl.h @@ -102,6 +102,4 @@ void plperl_spi_freeplan(char *); void plperl_spi_cursor_close(char *); char *plperl_sv_to_literal(SV *, char *); - - #endif /* PL_PERL_H */
-- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers