Don't override arguments set via options with positional arguments.

A number of utility programs were rather careless about paremeters
that can be set via both an option argument and a positional
argument. This leads to results which can violate the Principal
Of Least Astonishment. These changes refuse to use positional
arguments to override settings that have been made via positional
arguments. The changes are backpatched to all live branches.
This commit is contained in:
Andrew Dunstan 2012-04-17 18:36:22 -04:00
parent 67a48385b5
commit 7bebb85192
6 changed files with 75 additions and 38 deletions

View File

@ -2561,8 +2561,11 @@ main(int argc, char *argv[])
}
/* Non-option argument specifies data directory */
if (optind < argc)
/*
* Non-option argument specifies data directory
* as long as it wasn't already specified with -D / --pgdata
*/
if (optind < argc && strlen(pg_data) == 0)
{
pg_data = xstrdup(argv[optind]);
optind++;

View File

@ -97,18 +97,22 @@ main(int argc, char *argv[])
}
}
switch (argc - optind)
/*
* Non-option argument specifies database name
* as long as it wasn't already specified with -d / --dbname
*/
if (optind < argc && dbname == NULL)
{
case 0:
break;
case 1:
dbname = argv[optind];
break;
default:
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
progname, argv[optind + 1]);
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
dbname = argv[optind];
optind++;
}
if (optind < argc)
{
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
progname, argv[optind + 1]);
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
}
setup_cancel_handler();

View File

@ -87,14 +87,24 @@ main(int argc, char *argv[])
}
}
/*
* We set dbname from positional arguments if it is not
* already set by option arguments -d. If not doing
* listlangs, positional dbname must follow positional
* langname.
*/
if (argc - optind > 0)
{
if (listlangs)
dbname = argv[optind++];
{
if (dbname == NULL)
dbname = argv[optind++];
}
else
{
langname = argv[optind++];
if (argc - optind > 0)
if (argc - optind > 0 && dbname == NULL)
dbname = argv[optind++];
}
}

View File

@ -98,14 +98,24 @@ main(int argc, char *argv[])
}
}
/*
* We set dbname from positional arguments if it is not
* already set by option arguments -d. If not doing
* listlangs, positional dbname must follow positional
* langname.
*/
if (argc - optind > 0)
{
if (listlangs)
dbname = argv[optind++];
{
if (dbname == NULL)
dbname = argv[optind++];
}
else
{
langname = argv[optind++];
if (argc - optind > 0)
if (argc - optind > 0 && dbname == NULL)
dbname = argv[optind++];
}
}

View File

@ -112,17 +112,22 @@ main(int argc, char *argv[])
}
}
switch (argc - optind)
/*
* Non-option argument specifies database name
* as long as it wasn't already specified with -d / --dbname
*/
if (optind < argc && dbname == NULL)
{
case 0:
break;
case 1:
dbname = argv[optind];
break;
default:
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"), progname, argv[optind + 1]);
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
dbname = argv[optind];
optind++;
}
if (optind < argc)
{
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
progname, argv[optind + 1]);
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
}
setup_cancel_handler();

View File

@ -114,18 +114,23 @@ main(int argc, char *argv[])
}
}
switch (argc - optind)
/*
* Non-option argument specifies database name
* as long as it wasn't already specified with -d / --dbname
*/
if (optind < argc && dbname == NULL)
{
case 0:
break;
case 1:
dbname = argv[optind];
break;
default:
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
progname, argv[optind + 1]);
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
dbname = argv[optind];
optind++;
}
if (optind < argc)
{
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
progname, argv[optind + 1]);
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
}
setup_cancel_handler();