@@ -2132,6 +2132,12 @@ exec_command_prompt(PsqlScanState scan_state, bool active_branch,
2132
2132
else
2133
2133
{
2134
2134
char * result ;
2135
+ PromptInterruptContext prompt_ctx ;
2136
+
2137
+ /* Set up to let SIGINT cancel simple_prompt_extended() */
2138
+ prompt_ctx .jmpbuf = sigint_interrupt_jmp ;
2139
+ prompt_ctx .enabled = & sigint_interrupt_enabled ;
2140
+ prompt_ctx .canceled = false;
2135
2141
2136
2142
if (arg2 )
2137
2143
{
@@ -2143,7 +2149,7 @@ exec_command_prompt(PsqlScanState scan_state, bool active_branch,
2143
2149
2144
2150
if (!pset .inputfile )
2145
2151
{
2146
- result = simple_prompt (prompt_text , true);
2152
+ result = simple_prompt_extended (prompt_text , true, & prompt_ctx );
2147
2153
}
2148
2154
else
2149
2155
{
@@ -2161,8 +2167,8 @@ exec_command_prompt(PsqlScanState scan_state, bool active_branch,
2161
2167
}
2162
2168
}
2163
2169
2164
- if (result &&
2165
- !SetVariable (pset .vars , opt , result ))
2170
+ if (prompt_ctx . canceled ||
2171
+ ( result && !SetVariable (pset .vars , opt , result ) ))
2166
2172
success = false;
2167
2173
2168
2174
if (result )
@@ -3058,24 +3064,36 @@ copy_previous_query(PQExpBuffer query_buf, PQExpBuffer previous_buf)
3058
3064
3059
3065
/*
3060
3066
* Ask the user for a password; 'username' is the username the
3061
- * password is for, if one has been explicitly specified. Returns a
3062
- * malloc'd string.
3067
+ * password is for, if one has been explicitly specified.
3068
+ * Returns a malloc'd string.
3069
+ * If 'canceled' is provided, *canceled will be set to true if the prompt
3070
+ * is canceled via SIGINT, and to false otherwise.
3063
3071
*/
3064
3072
static char *
3065
- prompt_for_password (const char * username )
3073
+ prompt_for_password (const char * username , bool * canceled )
3066
3074
{
3067
3075
char * result ;
3076
+ PromptInterruptContext prompt_ctx ;
3077
+
3078
+ /* Set up to let SIGINT cancel simple_prompt_extended() */
3079
+ prompt_ctx .jmpbuf = sigint_interrupt_jmp ;
3080
+ prompt_ctx .enabled = & sigint_interrupt_enabled ;
3081
+ prompt_ctx .canceled = false;
3068
3082
3069
3083
if (username == NULL || username [0 ] == '\0' )
3070
- result = simple_prompt ("Password: " , false);
3084
+ result = simple_prompt_extended ("Password: " , false, & prompt_ctx );
3071
3085
else
3072
3086
{
3073
3087
char * prompt_text ;
3074
3088
3075
3089
prompt_text = psprintf (_ ("Password for user %s: " ), username );
3076
- result = simple_prompt (prompt_text , false);
3090
+ result = simple_prompt_extended (prompt_text , false, & prompt_ctx );
3077
3091
free (prompt_text );
3078
3092
}
3093
+
3094
+ if (canceled )
3095
+ * canceled = prompt_ctx .canceled ;
3096
+
3079
3097
return result ;
3080
3098
}
3081
3099
@@ -3331,14 +3349,18 @@ do_connect(enum trivalue reuse_previous_specification,
3331
3349
*/
3332
3350
if (pset .getPassword == TRI_YES && success )
3333
3351
{
3352
+ bool canceled = false;
3353
+
3334
3354
/*
3335
3355
* If a connstring or URI is provided, we don't know which username
3336
3356
* will be used, since we haven't dug that out of the connstring.
3337
3357
* Don't risk issuing a misleading prompt. As in startup.c, it does
3338
3358
* not seem worth working harder, since this getPassword setting is
3339
3359
* normally only used in noninteractive cases.
3340
3360
*/
3341
- password = prompt_for_password (has_connection_string ? NULL : user );
3361
+ password = prompt_for_password (has_connection_string ? NULL : user ,
3362
+ & canceled );
3363
+ success = !canceled ;
3342
3364
}
3343
3365
3344
3366
/*
@@ -3417,13 +3439,16 @@ do_connect(enum trivalue reuse_previous_specification,
3417
3439
*/
3418
3440
if (!password && PQconnectionNeedsPassword (n_conn ) && pset .getPassword != TRI_NO )
3419
3441
{
3442
+ bool canceled = false;
3443
+
3420
3444
/*
3421
3445
* Prompt for password using the username we actually connected
3422
3446
* with --- it might've come out of "dbname" rather than "user".
3423
3447
*/
3424
- password = prompt_for_password (PQuser (n_conn ));
3448
+ password = prompt_for_password (PQuser (n_conn ), & canceled );
3425
3449
PQfinish (n_conn );
3426
3450
n_conn = NULL ;
3451
+ success = !canceled ;
3427
3452
continue ;
3428
3453
}
3429
3454
0 commit comments