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:30:34 -04:00
parent fe546f3da6
commit 1b37a8c3cc
6 changed files with 75 additions and 38 deletions

View File

@ -2760,8 +2760,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

@ -112,14 +112,18 @@ 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:
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);

View File

@ -91,14 +91,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)
{
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

@ -90,14 +90,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)
{
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

@ -122,15 +122,20 @@ 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]);
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);
}

View File

@ -135,14 +135,19 @@ 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:
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);