1212#include "postgres_fe.h"
1313#include "common.h"
1414
15+ #define DEFAULT_CONNECT_TIMEOUT "3"
16+
1517static void
1618help (const char * progname );
1719
1820int
1921main (int argc , char * * argv )
2022{
21- int c ,optindex ,opt_index = 0 ;
23+ int c ,optindex ,opt_index = 2 ;
2224
2325 const char * progname ;
2426
2527 const char * pghost = NULL ;
2628 const char * pgport = NULL ;
2729 const char * pguser = NULL ;
2830 const char * pgdbname = NULL ;
31+ const char * connect_timeout = DEFAULT_CONNECT_TIMEOUT ;
2932
30- const char * keywords [4 ], * values [4 ];
33+ const char * keywords [7 ] = { NULL };
34+ const char * values [7 ] = { NULL };
3135
3236 bool quiet = false;
3337
@@ -44,14 +48,16 @@ main(int argc, char **argv)
4448 {"host" , required_argument , NULL , 'h' },
4549 {"port" , required_argument , NULL , 'p' },
4650 {"quiet" , no_argument , NULL , 'q' },
51+ {"timeout" , required_argument , NULL , 't' },
4752 {"username" , required_argument , NULL , 'U' },
4853 {NULL , 0 , NULL , 0 }
4954 };
5055
5156 progname = get_progname (argv [0 ]);
57+ set_pglocale_pgservice (argv [0 ], PG_TEXTDOMAIN ("pgscripts" ));
5258 handle_help_version_opts (argc , argv , progname , help );
5359
54- while ((c = getopt_long (argc , argv , "d:h:p:qU :V" , long_options , & optindex )) != -1 )
60+ while ((c = getopt_long (argc , argv , "d:h:p:qt:U :V" , long_options , & optindex )) != -1 )
5561 {
5662 switch (c )
5763 {
@@ -67,10 +73,14 @@ main(int argc, char **argv)
6773 case 'q' :
6874 quiet = true;
6975 break ;
76+ case 't' :
77+ connect_timeout = pg_strdup (optarg );
78+ break ;
7079 case 'U' :
7180 pguser = pg_strdup (optarg );
7281 break ;
7382 default :
83+ fprintf (stderr , _ ("Try \"%s --help\" for more information.\n" ), progname );
7484 /*
7585 * We need to make sure we don't return 1 here because someone
7686 * checking the return code might infer unintended meaning
@@ -92,12 +102,31 @@ main(int argc, char **argv)
92102 }
93103
94104 /*
95- * Get the default options so we can display them in our output
105+ * Set connection options
96106 */
97107
108+ keywords [0 ] = "connect_timeout" ;
109+ values [0 ] = connect_timeout ;
110+ keywords [1 ] = "fallback_application_name" ;
111+ values [1 ] = progname ;
112+ if (pguser )
113+ {
114+ keywords [opt_index ] = "user" ;
115+ values [opt_index ] = pguser ;
116+ opt_index ++ ;
117+ }
118+ if (pgdbname )
119+ {
120+ keywords [opt_index ] = "dbname" ;
121+ values [opt_index ] = pgdbname ;
122+ opt_index ++ ;
123+ }
124+
125+ /*
126+ * Get the default host and port so we can display them in our output
127+ */
98128 connect_options = PQconndefaults ();
99129 conn_opt_ptr = connect_options ;
100-
101130 while (conn_opt_ptr -> keyword )
102131 {
103132 if (strncmp (conn_opt_ptr -> keyword , "host" , 5 ) == 0 )
@@ -124,34 +153,9 @@ main(int argc, char **argv)
124153 else if (conn_opt_ptr -> val )
125154 pgport = conn_opt_ptr -> val ;
126155 }
127- else if (strncmp (conn_opt_ptr -> keyword , "user" , 5 ) == 0 )
128- {
129- if (pguser )
130- {
131- keywords [opt_index ] = conn_opt_ptr -> keyword ;
132- values [opt_index ] = pguser ;
133- opt_index ++ ;
134- }
135- else if (conn_opt_ptr -> val )
136- pguser = conn_opt_ptr -> val ;
137- }
138- else if (strncmp (conn_opt_ptr -> keyword , "dbname" , 7 ) == 0 )
139- {
140- if (pgdbname )
141- {
142- keywords [opt_index ] = conn_opt_ptr -> keyword ;
143- values [opt_index ] = pgdbname ;
144- opt_index ++ ;
145- }
146- else if (conn_opt_ptr -> val )
147- pgdbname = conn_opt_ptr -> val ;
148- }
149156 conn_opt_ptr ++ ;
150157 }
151158
152- keywords [opt_index ] = NULL ;
153- values [opt_index ] = NULL ;
154-
155159 rv = PQpingParams (keywords , values , 1 );
156160
157161 if (!quiet )
@@ -198,5 +202,7 @@ help(const char *progname)
198202 printf (_ ("\nConnection options:\n" ));
199203 printf (_ (" -h, --host=HOSTNAME database server host or socket directory\n" ));
200204 printf (_ (" -p, --port=PORT database server port\n" ));
205+ printf (_ (" -t, --timeout=SECS seconds to wait when attempting connection, 0 disables (default: %s)\n" ), DEFAULT_CONNECT_TIMEOUT );
201206 printf (_ (" -U, --username=USERNAME database username\n" ));
207+ printf (_ ("\nReport bugs to <pgsql-bugs@postgresql.org>.\n" ));
202208}
0 commit comments