ecpg -? option doesn't work in windows

Started by Haribabu Kommiover 9 years ago2 messages
#1Haribabu Kommi
kommi.haribabu@gmail.com
1 attachment(s)

ecpg option --help alternative -? doesn't work in windows.
In windows, the PG provided getopt_long function is used
for reading the provided options.

The getopt_long function returns '?' for invalid characters
also but it sets optopt option to 0 in case if the character
itself is a '?'. But this works for Linux and others, whereas
for windows, optopt is not 0. Because of this reason it is
failing.

I feel, from this commit 5b88b85c on wards, it is not working.
I feel instead of fixing the getopt_long function to reset optopt
parameter to zero whenever it is returning '?', I prefer fixing
the ecpg in handling the version and help options seperate.

Patch is attached. Any one prefers the getopt_long function
fix, I can produce the patch for the same.

Regards,
Hari Babu
Fujitsu Australia

Attachments:

ecpg_bugfix.patchapplication/octet-stream; name=ecpg_bugfix.patchDownload
diff --git a/src/interfaces/ecpg/preproc/ecpg.c b/src/interfaces/ecpg/preproc/ecpg.c
index 3ce9d04..d7411dc 100644
--- a/src/interfaces/ecpg/preproc/ecpg.c
+++ b/src/interfaces/ecpg/preproc/ecpg.c
@@ -54,7 +54,7 @@ help(const char *progname)
 	 "                 \"no_indicator\", \"prepare\", \"questionmarks\"\n"));
 	printf(_("  --regression   run in regression testing mode\n"));
 	printf(_("  -t             turn on autocommit of transactions\n"));
-	printf(_("  --version      output version information, then exit\n"));
+	printf(_("  -V, --version  output version information, then exit\n"));
 	printf(_("  -?, --help     show this help, then exit\n"));
 	printf(_("\nIf no output file is specified, the name is formed by adding .c to the\n"
 			 "input file name, after stripping off .pgc if present.\n"));
@@ -111,15 +111,13 @@ add_preprocessor_define(char *define)
 	defines->next = pd;
 }
 
-#define ECPG_GETOPT_LONG_HELP			1
-#define ECPG_GETOPT_LONG_VERSION		2
-#define ECPG_GETOPT_LONG_REGRESSION		3
+#define ECPG_GETOPT_LONG_REGRESSION		1
 int
 main(int argc, char *const argv[])
 {
 	static struct option ecpg_options[] = {
-		{"help", no_argument, NULL, ECPG_GETOPT_LONG_HELP},
-		{"version", no_argument, NULL, ECPG_GETOPT_LONG_VERSION},
+		{"help", no_argument, NULL, '?'},
+		{"version", no_argument, NULL, 'V'},
 		{"regression", no_argument, NULL, ECPG_GETOPT_LONG_REGRESSION},
 		{NULL, 0, NULL, 0}
 	};
@@ -144,32 +142,25 @@ main(int argc, char *const argv[])
 		return (ILLEGAL_OPTION);
 	}
 
+	if (argc > 1)
+	{
+		if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
+		{
+			help(progname);
+			exit(0);
+		}
+		if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
+		{
+			printf("ecpg %s\n", PG_VERSION);
+			exit(0);
+		}
+	}
+
 	output_filename = NULL;
-	while ((c = getopt_long(argc, argv, "vcio:I:tD:dC:r:h?", ecpg_options, NULL)) != -1)
+	while ((c = getopt_long(argc, argv, "vcio:I:tD:dC:r:h", ecpg_options, NULL)) != -1)
 	{
 		switch (c)
 		{
-			case ECPG_GETOPT_LONG_VERSION:
-				printf("ecpg %s\n", PG_VERSION);
-				exit(0);
-			case ECPG_GETOPT_LONG_HELP:
-				help(progname);
-				exit(0);
-
-				/*
-				 * -? is an alternative spelling of --help. However it is also
-				 * returned by getopt_long for unknown options. We can
-				 * distinguish both cases by means of the optopt variable
-				 * which is set to 0 if it was really -? and not an unknown
-				 * option character.
-				 */
-			case '?':
-				if (optopt == 0)
-				{
-					help(progname);
-					exit(0);
-				}
-				break;
 			case ECPG_GETOPT_LONG_REGRESSION:
 				regression_mode = true;
 				break;
#2Heikki Linnakangas
hlinnaka@iki.fi
In reply to: Haribabu Kommi (#1)
Re: ecpg -? option doesn't work in windows

On 08/29/2016 09:10 AM, Haribabu Kommi wrote:

ecpg option --help alternative -? doesn't work in windows.
In windows, the PG provided getopt_long function is used
for reading the provided options.

The getopt_long function returns '?' for invalid characters
also but it sets optopt option to 0 in case if the character
itself is a '?'. But this works for Linux and others, whereas
for windows, optopt is not 0. Because of this reason it is
failing.

I feel, from this commit 5b88b85c on wards, it is not working.
I feel instead of fixing the getopt_long function to reset optopt
parameter to zero whenever it is returning '?', I prefer fixing
the ecpg in handling the version and help options seperate.

Agreed. This does have one annoying consequence, though: --help and
--version are now only accepted as the first argument. But that's
consistent with most of our binaries. psql does this slightly
differently, though, so e.g. "psql --t --help" works. It might be worth
changing all our binaries to follow psql's example, but that's another
story.

Patch is attached. Any one prefers the getopt_long function
fix, I can produce the patch for the same.

Committed, thanks!

- Heikki

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers