@ -3256,13 +3256,14 @@ get_stats_option_name(const char *arg)
* coming from the client , or PGC_SUSET for insecure options coming from
* coming from the client , or PGC_SUSET for insecure options coming from
* a superuser client .
* a superuser client .
*
*
* Returns the database name extracted from the command line , if any .
* If a database name is present in the command line arguments , it ' s
* returned into * dbname ( this is allowed only if * dbname is initially NULL ) .
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
*/
const char *
void
process_postgres_switches ( int argc , char * argv [ ] , GucContext ctx )
process_postgres_switches ( int argc , char * argv [ ] , GucContext ctx ,
const char * * dbname )
{
{
const char * dbname ;
bool secure = ( ctx = = PGC_POSTMASTER ) ;
bool secure = ( ctx = = PGC_POSTMASTER ) ;
int errs = 0 ;
int errs = 0 ;
GucSource gucsource ;
GucSource gucsource ;
@ -3303,7 +3304,8 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx)
case ' b ' :
case ' b ' :
/* Undocumented flag used for binary upgrades */
/* Undocumented flag used for binary upgrades */
IsBinaryUpgrade = true ;
if ( secure )
IsBinaryUpgrade = true ;
break ;
break ;
case ' D ' :
case ' D ' :
@ -3316,7 +3318,8 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx)
break ;
break ;
case ' E ' :
case ' E ' :
EchoQuery = true ;
if ( secure )
EchoQuery = true ;
break ;
break ;
case ' e ' :
case ' e ' :
@ -3341,7 +3344,8 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx)
break ;
break ;
case ' j ' :
case ' j ' :
UseNewLine = 0 ;
if ( secure )
UseNewLine = 0 ;
break ;
break ;
case ' k ' :
case ' k ' :
@ -3456,10 +3460,12 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx)
}
}
/*
/*
* Should be no more arguments except an optional database name , and
* Optional database name should be there only if * dbname is NULL .
* that ' s only in the secure case .
*/
*/
if ( errs | | argc - optind > 1 | | ( argc ! = optind & & ! secure ) )
if ( ! errs & & dbname & & * dbname = = NULL & & argc - optind > = 1 )
* dbname = strdup ( argv [ optind + + ] ) ;
if ( errs | | argc ! = optind )
{
{
/* spell the error message a bit differently depending on context */
/* spell the error message a bit differently depending on context */
if ( IsUnderPostmaster )
if ( IsUnderPostmaster )
@ -3475,11 +3481,6 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx)
errhint ( " Try \" %s --help \" for more information. " , progname ) ) ) ;
errhint ( " Try \" %s --help \" for more information. " , progname ) ) ) ;
}
}
if ( argc - optind = = 1 )
dbname = strdup ( argv [ optind ] ) ;
else
dbname = NULL ;
/*
/*
* Reset getopt ( 3 ) library so that it will work correctly in subprocesses
* Reset getopt ( 3 ) library so that it will work correctly in subprocesses
* or when this function is called a second time with another array .
* or when this function is called a second time with another array .
@ -3488,8 +3489,6 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx)
# ifdef HAVE_INT_OPTRESET
# ifdef HAVE_INT_OPTRESET
optreset = 1 ; /* some systems need this too */
optreset = 1 ; /* some systems need this too */
# endif
# endif
return dbname ;
}
}
@ -3499,14 +3498,16 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx)
*
*
* argc / argv are the command line arguments to be used . ( When being forked
* argc / argv are the command line arguments to be used . ( When being forked
* by the postmaster , these are not the original argv array of the process . )
* by the postmaster , these are not the original argv array of the process . )
* username is the ( possibly authenticated ) PostgreSQL user name to be used
* dbname is the name of the database to connect to , or NULL if the database
* for the session .
* name should be extracted from the command line arguments or defaulted .
* username is the PostgreSQL user name to be used for the session .
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
*/
int
int
PostgresMain ( int argc , char * argv [ ] , const char * username )
PostgresMain ( int argc , char * argv [ ] ,
const char * dbname ,
const char * username )
{
{
const char * dbname ;
int firstchar ;
int firstchar ;
StringInfoData input_message ;
StringInfoData input_message ;
sigjmp_buf local_sigjmp_buf ;
sigjmp_buf local_sigjmp_buf ;
@ -3553,7 +3554,7 @@ PostgresMain(int argc, char *argv[], const char *username)
/*
/*
* Parse command - line options .
* Parse command - line options .
*/
*/
dbname = process_postgres_switches ( argc , argv , PGC_POSTMASTER ) ;
process_postgres_switches ( argc , argv , PGC_POSTMASTER , & dbname ) ;
/* Must have gotten a database name, or have a default (the username) */
/* Must have gotten a database name, or have a default (the username) */
if ( dbname = = NULL )
if ( dbname = = NULL )