12
12
#include "postgres_fe.h"
13
13
#include "common.h"
14
14
15
+ #define DEFAULT_CONNECT_TIMEOUT "3"
16
+
15
17
static void
16
18
help (const char * progname );
17
19
18
20
int
19
21
main (int argc ,char * * argv )
20
22
{
21
- int c ,optindex ,opt_index = 0 ;
23
+ int c ,optindex ,opt_index = 2 ;
22
24
23
25
const char * progname ;
24
26
25
27
const char * pghost = NULL ;
26
28
const char * pgport = NULL ;
27
29
const char * pguser = NULL ;
28
30
const char * pgdbname = NULL ;
31
+ const char * connect_timeout = DEFAULT_CONNECT_TIMEOUT ;
29
32
30
- const char * keywords [4 ],* values [4 ];
33
+ const char * keywords [7 ]= {NULL };
34
+ const char * values [7 ]= {NULL };
31
35
32
36
bool quiet = false;
33
37
@@ -44,14 +48,16 @@ main(int argc, char **argv)
44
48
{"host" ,required_argument ,NULL ,'h' },
45
49
{"port" ,required_argument ,NULL ,'p' },
46
50
{"quiet" ,no_argument ,NULL ,'q' },
51
+ {"timeout" ,required_argument ,NULL ,'t' },
47
52
{"username" ,required_argument ,NULL ,'U' },
48
53
{NULL ,0 ,NULL ,0 }
49
54
};
50
55
51
56
progname = get_progname (argv [0 ]);
57
+ set_pglocale_pgservice (argv [0 ],PG_TEXTDOMAIN ("pgscripts" ));
52
58
handle_help_version_opts (argc ,argv ,progname ,help );
53
59
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 )
55
61
{
56
62
switch (c )
57
63
{
@@ -67,10 +73,14 @@ main(int argc, char **argv)
67
73
case 'q' :
68
74
quiet = true;
69
75
break ;
76
+ case 't' :
77
+ connect_timeout = pg_strdup (optarg );
78
+ break ;
70
79
case 'U' :
71
80
pguser = pg_strdup (optarg );
72
81
break ;
73
82
default :
83
+ fprintf (stderr ,_ ("Try \"%s --help\" for more information.\n" ),progname );
74
84
/*
75
85
* We need to make sure we don't return 1 here because someone
76
86
* checking the return code might infer unintended meaning
@@ -92,12 +102,31 @@ main(int argc, char **argv)
92
102
}
93
103
94
104
/*
95
- *Get the default options so we can display them in our output
105
+ *Set connection options
96
106
*/
97
107
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
+ */
98
128
connect_options = PQconndefaults ();
99
129
conn_opt_ptr = connect_options ;
100
-
101
130
while (conn_opt_ptr -> keyword )
102
131
{
103
132
if (strncmp (conn_opt_ptr -> keyword ,"host" ,5 )== 0 )
@@ -124,34 +153,9 @@ main(int argc, char **argv)
124
153
else if (conn_opt_ptr -> val )
125
154
pgport = conn_opt_ptr -> val ;
126
155
}
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
- }
149
156
conn_opt_ptr ++ ;
150
157
}
151
158
152
- keywords [opt_index ]= NULL ;
153
- values [opt_index ]= NULL ;
154
-
155
159
rv = PQpingParams (keywords ,values ,1 );
156
160
157
161
if (!quiet )
@@ -198,5 +202,7 @@ help(const char *progname)
198
202
printf (_ ("\nConnection options:\n" ));
199
203
printf (_ (" -h, --host=HOSTNAME database server host or socket directory\n" ));
200
204
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 );
201
206
printf (_ (" -U, --username=USERNAME database username\n" ));
207
+ printf (_ ("\nReport bugs to <pgsql-bugs@postgresql.org>.\n" ));
202
208
}