Branch data Line data Source code
1 : : /*
2 : : * psql - the PostgreSQL interactive terminal
3 : : *
4 : : * Copyright (c) 2000-2026, PostgreSQL Global Development Group
5 : : *
6 : : * src/bin/psql/mainloop.c
7 : : */
8 : : #include "postgres_fe.h"
9 : :
10 : : #include "command.h"
11 : : #include "common.h"
12 : : #include "common/logging.h"
13 : : #include "input.h"
14 : : #include "mainloop.h"
15 : : #include "mb/pg_wchar.h"
16 : : #include "prompt.h"
17 : : #include "settings.h"
18 : :
19 : : /* callback functions for our flex lexer */
20 : : const PsqlScanCallbacks psqlscan_callbacks = {
21 : : psql_get_variable,
22 : : };
23 : :
24 : :
25 : : /*
26 : : * Main processing loop for reading lines of input
27 : : * and sending them to the backend.
28 : : *
29 : : * This loop is re-entrant. May be called by \i command
30 : : * which reads input from a file.
31 : : */
32 : : int
33 : 269 : MainLoop(FILE *source)
34 : : {
35 : 269 : PsqlScanState scan_state; /* lexer working state */
36 : 269 : ConditionalStack cond_stack; /* \if status stack */
37 : 269 : volatile PQExpBuffer query_buf; /* buffer for query being accumulated */
38 : 269 : volatile PQExpBuffer previous_buf; /* if there isn't anything in the new
39 : : * buffer yet, use this one for \e,
40 : : * etc. */
41 : 269 : PQExpBuffer history_buf; /* earlier lines of a multi-line command, not
42 : : * yet saved to readline history */
43 : 269 : char *line; /* current line of input */
44 : 269 : int added_nl_pos;
45 : 269 : bool success;
46 : 269 : bool line_saved_in_history;
47 : 269 : volatile int successResult = EXIT_SUCCESS;
48 : 269 : volatile backslashResult slashCmdStatus = PSQL_CMD_UNKNOWN;
49 : 269 : volatile promptStatus_t prompt_status = PROMPT_READY;
50 : 269 : volatile bool need_redisplay = false;
51 : 269 : volatile int count_eof = 0;
52 : 269 : volatile bool die_on_error = false;
53 : 269 : FILE *prev_cmd_source;
54 : 269 : bool prev_cmd_interactive;
55 : 269 : uint64 prev_lineno;
56 : :
57 : : /* Save the prior command source */
58 : 269 : prev_cmd_source = pset.cur_cmd_source;
59 : 269 : prev_cmd_interactive = pset.cur_cmd_interactive;
60 : 269 : prev_lineno = pset.lineno;
61 : : /* pset.stmt_lineno does not need to be saved and restored */
62 : :
63 : : /* Establish new source */
64 : 269 : pset.cur_cmd_source = source;
65 [ - + ]: 269 : pset.cur_cmd_interactive = ((source == stdin) && !pset.notty);
66 : 269 : pset.lineno = 0;
67 : 269 : pset.stmt_lineno = 1;
68 : :
69 : : /* Create working state */
70 : 269 : scan_state = psql_scan_create(&psqlscan_callbacks);
71 : 269 : cond_stack = conditional_stack_create();
72 : 269 : psql_scan_set_passthrough(scan_state, cond_stack);
73 : :
74 : 269 : query_buf = createPQExpBuffer();
75 : 269 : previous_buf = createPQExpBuffer();
76 : 269 : history_buf = createPQExpBuffer();
77 [ + - ]: 269 : if (PQExpBufferBroken(query_buf) ||
78 : 538 : PQExpBufferBroken(previous_buf) ||
79 : 269 : PQExpBufferBroken(history_buf))
80 : 0 : pg_fatal("out of memory");
81 : :
82 : : /* main loop to get queries and execute them */
83 [ - + ]: 120320 : while (successResult == EXIT_SUCCESS)
84 : : {
85 : : /*
86 : : * Clean up after a previous Control-C
87 : : */
88 [ + - ]: 120320 : if (cancel_pressed)
89 : : {
90 [ # # ]: 0 : if (!pset.cur_cmd_interactive)
91 : : {
92 : : /*
93 : : * You get here if you stopped a script with Ctrl-C.
94 : : */
95 : 0 : successResult = EXIT_USER;
96 : 0 : break;
97 : : }
98 : :
99 : 0 : cancel_pressed = false;
100 : 0 : }
101 : :
102 : : /*
103 : : * Establish longjmp destination for exiting from wait-for-input. We
104 : : * must re-do this each time through the loop for safety, since the
105 : : * jmpbuf might get changed during command execution.
106 : : */
107 [ + - ]: 120320 : if (sigsetjmp(sigint_interrupt_jmp, 1) != 0)
108 : : {
109 : : /* got here with longjmp */
110 : :
111 : : /* reset parsing state */
112 : 0 : psql_scan_finish(scan_state);
113 : 0 : psql_scan_reset(scan_state);
114 : 0 : resetPQExpBuffer(query_buf);
115 : 0 : resetPQExpBuffer(history_buf);
116 : 0 : count_eof = 0;
117 : 0 : slashCmdStatus = PSQL_CMD_UNKNOWN;
118 : 0 : prompt_status = PROMPT_READY;
119 : 0 : need_redisplay = false;
120 : 0 : pset.stmt_lineno = 1;
121 : 0 : cancel_pressed = false;
122 : :
123 [ # # ]: 0 : if (pset.cur_cmd_interactive)
124 : : {
125 : 0 : putc('\n', stdout);
126 : :
127 : : /*
128 : : * if interactive user is in an \if block, then Ctrl-C will
129 : : * exit from the innermost \if.
130 : : */
131 [ # # ]: 0 : if (!conditional_stack_empty(cond_stack))
132 : : {
133 : 0 : pg_log_error("\\if: escaped");
134 : 0 : conditional_stack_pop(cond_stack);
135 : 0 : }
136 : 0 : }
137 : : else
138 : : {
139 : 0 : successResult = EXIT_USER;
140 : 0 : break;
141 : : }
142 : 0 : }
143 : :
144 : 120320 : fflush(stdout);
145 : :
146 : : /*
147 : : * get another line
148 : : */
149 [ - + ]: 120320 : if (pset.cur_cmd_interactive)
150 : : {
151 : : /* May need to reset prompt, eg after \r command */
152 [ # # ]: 0 : if (query_buf->len == 0)
153 : 0 : prompt_status = PROMPT_READY;
154 : : /* If query buffer came from \e, redisplay it with a prompt */
155 [ # # ]: 0 : if (need_redisplay)
156 : : {
157 [ # # ]: 0 : if (query_buf->len > 0)
158 : : {
159 : 0 : fputs(get_prompt(PROMPT_READY, cond_stack), stdout);
160 : 0 : fputs(query_buf->data, stdout);
161 : 0 : fflush(stdout);
162 : 0 : }
163 : 0 : need_redisplay = false;
164 : 0 : }
165 : : /* Now we can fetch a line */
166 : 0 : line = gets_interactive(get_prompt(prompt_status, cond_stack),
167 : 0 : query_buf);
168 : 0 : }
169 : : else
170 : : {
171 : 120320 : line = gets_fromFile(source);
172 [ + + + - ]: 120320 : if (!line && ferror(source))
173 : 0 : successResult = EXIT_FAILURE;
174 : : }
175 : :
176 : : /*
177 : : * query_buf holds query already accumulated. line is the malloc'd
178 : : * new line of input (note it must be freed before looping around!)
179 : : */
180 : :
181 : : /* No more input. Time to quit, or \i done */
182 [ + + ]: 120320 : if (line == NULL)
183 : : {
184 [ + - ]: 266 : if (pset.cur_cmd_interactive)
185 : : {
186 : : /* This tries to mimic bash's IGNOREEOF feature. */
187 : 0 : count_eof++;
188 : :
189 [ # # ]: 0 : if (count_eof < pset.ignoreeof)
190 : : {
191 [ # # ]: 0 : if (!pset.quiet)
192 : 0 : printf(_("Use \"\\q\" to leave %s.\n"), pset.progname);
193 : 0 : continue;
194 : : }
195 : :
196 : 0 : puts(pset.quiet ? "" : "\\q");
197 : 0 : }
198 : 266 : break;
199 : : }
200 : :
201 : 120054 : count_eof = 0;
202 : :
203 : 120054 : pset.lineno++;
204 : :
205 : : /* ignore UTF-8 Unicode byte-order mark */
206 [ + + + - : 120054 : if (pset.lineno == 1 && pset.encoding == PG_UTF8 && strncmp(line, "\xef\xbb\xbf", 3) == 0)
+ - ]
207 : 0 : memmove(line, line + 3, strlen(line + 3) + 1);
208 : :
209 : : /* Detect attempts to run custom-format dumps as SQL scripts */
210 [ + + + - : 120054 : if (pset.lineno == 1 && !pset.cur_cmd_interactive &&
+ - ]
211 : 269 : strncmp(line, "PGDMP", 5) == 0)
212 : : {
213 : 0 : free(line);
214 : 0 : puts(_("The input is a PostgreSQL custom-format dump.\n"
215 : : "Use the pg_restore command-line client to restore this dump to a database.\n"));
216 : 0 : fflush(stdout);
217 : 0 : successResult = EXIT_FAILURE;
218 : 0 : break;
219 : : }
220 : :
221 : : /* no further processing of empty lines, unless within a literal */
222 [ + + + + ]: 120054 : if (line[0] == '\0' && !psql_scan_in_quote(scan_state))
223 : : {
224 : 20890 : free(line);
225 : 20890 : continue;
226 : : }
227 : :
228 : : /* Recognize "help", "quit", "exit" only in interactive mode */
229 [ + - ]: 99164 : if (pset.cur_cmd_interactive)
230 : : {
231 : 0 : char *first_word = line;
232 : 0 : char *rest_of_line = NULL;
233 : 0 : bool found_help = false;
234 : 0 : bool found_exit_or_quit = false;
235 : 0 : bool found_q = false;
236 : :
237 : : /*
238 : : * The assistance words, help/exit/quit, must have no whitespace
239 : : * before them, and only whitespace after, with an optional
240 : : * semicolon. This prevents indented use of these words, perhaps
241 : : * as identifiers, from invoking the assistance behavior.
242 : : */
243 [ # # ]: 0 : if (pg_strncasecmp(first_word, "help", 4) == 0)
244 : : {
245 : 0 : rest_of_line = first_word + 4;
246 : 0 : found_help = true;
247 : 0 : }
248 [ # # # # ]: 0 : else if (pg_strncasecmp(first_word, "exit", 4) == 0 ||
249 : 0 : pg_strncasecmp(first_word, "quit", 4) == 0)
250 : : {
251 : 0 : rest_of_line = first_word + 4;
252 : 0 : found_exit_or_quit = true;
253 : 0 : }
254 [ # # ]: 0 : else if (strncmp(first_word, "\\q", 2) == 0)
255 : : {
256 : 0 : rest_of_line = first_word + 2;
257 : 0 : found_q = true;
258 : 0 : }
259 : :
260 : : /*
261 : : * If we found a command word, check whether the rest of the line
262 : : * contains only whitespace plus maybe one semicolon. If not,
263 : : * ignore the command word after all. These commands are only for
264 : : * compatibility with other SQL clients and are not documented.
265 : : */
266 [ # # ]: 0 : if (rest_of_line != NULL)
267 : : {
268 : : /*
269 : : * Ignore unless rest of line is whitespace, plus maybe one
270 : : * semicolon
271 : : */
272 [ # # ]: 0 : while (isspace((unsigned char) *rest_of_line))
273 : 0 : ++rest_of_line;
274 [ # # ]: 0 : if (*rest_of_line == ';')
275 : 0 : ++rest_of_line;
276 [ # # ]: 0 : while (isspace((unsigned char) *rest_of_line))
277 : 0 : ++rest_of_line;
278 [ # # ]: 0 : if (*rest_of_line != '\0')
279 : : {
280 : 0 : found_help = false;
281 : 0 : found_exit_or_quit = false;
282 : 0 : }
283 : 0 : }
284 : :
285 : : /*
286 : : * "help" is only a command when the query buffer is empty, but we
287 : : * emit a one-line message even when it isn't to help confused
288 : : * users. The text is still added to the query buffer in that
289 : : * case.
290 : : */
291 [ # # ]: 0 : if (found_help)
292 : : {
293 [ # # ]: 0 : if (query_buf->len != 0)
294 : : #ifndef WIN32
295 : 0 : puts(_("Use \\? for help or press control-C to clear the input buffer."));
296 : : #else
297 : : puts(_("Use \\? for help."));
298 : : #endif
299 : : else
300 : : {
301 : 0 : puts(_("You are using psql, the command-line interface to PostgreSQL."));
302 : 0 : printf(_("Type: \\copyright for distribution terms\n"
303 : : " \\h for help with SQL commands\n"
304 : : " \\? for help with psql commands\n"
305 : : " \\g or terminate with semicolon to execute query\n"
306 : : " \\q to quit\n"));
307 : 0 : free(line);
308 : 0 : fflush(stdout);
309 : 0 : continue;
310 : : }
311 : 0 : }
312 : :
313 : : /*
314 : : * "quit" and "exit" are only commands when the query buffer is
315 : : * empty, but we emit a one-line message even when it isn't to
316 : : * help confused users. The text is still added to the query
317 : : * buffer in that case.
318 : : */
319 [ # # ]: 0 : if (found_exit_or_quit)
320 : : {
321 [ # # ]: 0 : if (query_buf->len != 0)
322 : : {
323 [ # # ]: 0 : if (prompt_status == PROMPT_READY ||
324 [ # # # # ]: 0 : prompt_status == PROMPT_CONTINUE ||
325 : 0 : prompt_status == PROMPT_PAREN)
326 : 0 : puts(_("Use \\q to quit."));
327 : : else
328 : : #ifndef WIN32
329 : 0 : puts(_("Use control-D to quit."));
330 : : #else
331 : : puts(_("Use control-C to quit."));
332 : : #endif
333 : 0 : }
334 : : else
335 : : {
336 : : /* exit app */
337 : 0 : free(line);
338 : 0 : fflush(stdout);
339 : 0 : successResult = EXIT_SUCCESS;
340 : 0 : break;
341 : : }
342 : 0 : }
343 : :
344 : : /*
345 : : * If they typed "\q" in a place where "\q" is not active, supply
346 : : * a hint. The text is still added to the query buffer.
347 : : */
348 [ # # # # ]: 0 : if (found_q && query_buf->len != 0 &&
349 [ # # ]: 0 : prompt_status != PROMPT_READY &&
350 [ # # # # ]: 0 : prompt_status != PROMPT_CONTINUE &&
351 : 0 : prompt_status != PROMPT_PAREN)
352 : : #ifndef WIN32
353 : 0 : puts(_("Use control-D to quit."));
354 : : #else
355 : : puts(_("Use control-C to quit."));
356 : : #endif
357 [ # # ]: 0 : }
358 : :
359 : : /* echo back if flag is set, unless interactive */
360 [ + + - + ]: 99164 : if (pset.echo == PSQL_ECHO_ALL && !pset.cur_cmd_interactive)
361 : : {
362 : 99112 : puts(line);
363 : 99112 : fflush(stdout);
364 : 99112 : }
365 : :
366 : : /* insert newlines into query buffer between source lines */
367 [ + + ]: 99164 : if (query_buf->len > 0)
368 : : {
369 : 30002 : appendPQExpBufferChar(query_buf, '\n');
370 : 30002 : added_nl_pos = query_buf->len;
371 : 30002 : }
372 : : else
373 : 69162 : added_nl_pos = -1; /* flag we didn't add one */
374 : :
375 : : /* Setting this will not have effect until next line. */
376 : 99164 : die_on_error = pset.on_error_stop;
377 : :
378 : : /*
379 : : * Parse line, looking for command separators.
380 : : */
381 : 198328 : psql_scan_setup(scan_state, line, strlen(line),
382 : 99164 : pset.encoding, standard_strings());
383 : 99164 : success = true;
384 : 99164 : line_saved_in_history = false;
385 : :
386 [ + + - + ]: 152319 : while (success || !die_on_error)
387 : : {
388 : 152319 : PsqlScanResult scan_result;
389 : 152319 : promptStatus_t prompt_tmp = prompt_status;
390 : 152319 : size_t pos_in_query;
391 : 152319 : char *tmp_line;
392 : :
393 : 152319 : pos_in_query = query_buf->len;
394 : 152319 : scan_result = psql_scan(scan_state, query_buf, &prompt_tmp);
395 : 152319 : prompt_status = prompt_tmp;
396 : :
397 [ + - ]: 152319 : if (PQExpBufferBroken(query_buf))
398 : 0 : pg_fatal("out of memory");
399 : :
400 : : /*
401 : : * Increase statement line number counter for each linebreak added
402 : : * to the query buffer by the last psql_scan() call. There only
403 : : * will be ones to add when navigating to a statement in
404 : : * readline's history containing newlines.
405 : : */
406 : 152319 : tmp_line = query_buf->data + pos_in_query;
407 [ + + ]: 3782165 : while (*tmp_line != '\0')
408 : : {
409 [ + + ]: 3629846 : if (*(tmp_line++) == '\n')
410 : 30 : pset.stmt_lineno++;
411 : : }
412 : :
413 [ + + ]: 152319 : if (scan_result == PSCAN_EOL)
414 : 16477 : pset.stmt_lineno++;
415 : :
416 : : /*
417 : : * Send command if semicolon found, or if end of line and we're in
418 : : * single-line mode.
419 : : */
420 [ + + - + ]: 168796 : if (scan_result == PSCAN_SEMICOLON ||
421 [ + + ]: 101825 : (scan_result == PSCAN_EOL && pset.singleline))
422 : : {
423 : : /*
424 : : * Save line in history. We use history_buf to accumulate
425 : : * multi-line queries into a single history entry. Note that
426 : : * history accumulation works on input lines, so it doesn't
427 : : * matter whether the query will be ignored due to \if.
428 : : */
429 [ - + # # ]: 50494 : if (pset.cur_cmd_interactive && !line_saved_in_history)
430 : : {
431 : 0 : pg_append_history(line, history_buf);
432 : 0 : pg_send_history(history_buf);
433 : 0 : line_saved_in_history = true;
434 : 0 : }
435 : :
436 : : /* execute query unless we're in an inactive \if branch */
437 [ + + ]: 50494 : if (conditional_active(cond_stack))
438 : : {
439 : 50492 : success = SendQuery(query_buf->data);
440 : 50492 : slashCmdStatus = success ? PSQL_CMD_SEND : PSQL_CMD_ERROR;
441 : 50492 : pset.stmt_lineno = 1;
442 : :
443 : : /* transfer query to previous_buf by pointer-swapping */
444 : : {
445 : 50492 : PQExpBuffer swap_buf = previous_buf;
446 : :
447 : 50492 : previous_buf = query_buf;
448 : 50492 : query_buf = swap_buf;
449 : 50492 : }
450 : 50492 : resetPQExpBuffer(query_buf);
451 : :
452 : 50492 : added_nl_pos = -1;
453 : : /* we need not do psql_scan_reset() here */
454 : 50492 : }
455 : : else
456 : : {
457 : : /* if interactive, warn about non-executed query */
458 [ + - ]: 2 : if (pset.cur_cmd_interactive)
459 : 0 : pg_log_error("query ignored; use \\endif or Ctrl-C to exit current \\if block");
460 : : /* fake an OK result for purposes of loop checks */
461 : 2 : success = true;
462 : 2 : slashCmdStatus = PSQL_CMD_SEND;
463 : 2 : pset.stmt_lineno = 1;
464 : : /* note that query_buf doesn't change state */
465 : : }
466 : 50494 : }
467 [ + + ]: 101825 : else if (scan_result == PSCAN_BACKSLASH)
468 : : {
469 : : /* handle backslash command */
470 : :
471 : : /*
472 : : * If we added a newline to query_buf, and nothing else has
473 : : * been inserted in query_buf by the lexer, then strip off the
474 : : * newline again. This avoids any change to query_buf when a
475 : : * line contains only a backslash command. Also, in this
476 : : * situation we force out any previous lines as a separate
477 : : * history entry; we don't want SQL and backslash commands
478 : : * intermixed in history if at all possible.
479 : : */
480 [ + + ]: 2664 : if (query_buf->len == added_nl_pos)
481 : : {
482 : 23 : query_buf->data[--query_buf->len] = '\0';
483 : 23 : pg_send_history(history_buf);
484 : 23 : }
485 : 2664 : added_nl_pos = -1;
486 : :
487 : : /* save backslash command in history */
488 [ - + # # ]: 2664 : if (pset.cur_cmd_interactive && !line_saved_in_history)
489 : : {
490 : 0 : pg_append_history(line, history_buf);
491 : 0 : pg_send_history(history_buf);
492 : 0 : line_saved_in_history = true;
493 : 0 : }
494 : :
495 : : /* execute backslash command */
496 : 5328 : slashCmdStatus = HandleSlashCmds(scan_state,
497 : 2664 : cond_stack,
498 : 2664 : query_buf,
499 : 2664 : previous_buf);
500 : :
501 : 2664 : success = slashCmdStatus != PSQL_CMD_ERROR;
502 : :
503 : : /*
504 : : * Resetting stmt_lineno after a backslash command isn't
505 : : * always appropriate, but it's what we've done historically
506 : : * and there have been few complaints.
507 : : */
508 : 2664 : pset.stmt_lineno = 1;
509 : :
510 [ + + ]: 2664 : if (slashCmdStatus == PSQL_CMD_SEND)
511 : : {
512 : : /* should not see this in inactive branch */
513 [ - + ]: 459 : Assert(conditional_active(cond_stack));
514 : :
515 : 459 : success = SendQuery(query_buf->data);
516 : :
517 : : /* transfer query to previous_buf by pointer-swapping */
518 : : {
519 : 459 : PQExpBuffer swap_buf = previous_buf;
520 : :
521 : 459 : previous_buf = query_buf;
522 : 459 : query_buf = swap_buf;
523 : 459 : }
524 : 459 : resetPQExpBuffer(query_buf);
525 : :
526 : : /* flush any paren nesting info after forced send */
527 : 459 : psql_scan_reset(scan_state);
528 : 459 : }
529 [ - + ]: 2205 : else if (slashCmdStatus == PSQL_CMD_NEWEDIT)
530 : : {
531 : : /* should not see this in inactive branch */
532 [ # # ]: 0 : Assert(conditional_active(cond_stack));
533 : : /* ensure what came back from editing ends in a newline */
534 [ # # # # ]: 0 : if (query_buf->len > 0 &&
535 : 0 : query_buf->data[query_buf->len - 1] != '\n')
536 : 0 : appendPQExpBufferChar(query_buf, '\n');
537 : : /* rescan query_buf as new input */
538 : 0 : psql_scan_finish(scan_state);
539 : 0 : free(line);
540 : 0 : line = pg_strdup(query_buf->data);
541 : 0 : resetPQExpBuffer(query_buf);
542 : : /* reset parsing state since we are rescanning whole line */
543 : 0 : psql_scan_reset(scan_state);
544 : 0 : psql_scan_setup(scan_state, line, strlen(line),
545 : 0 : pset.encoding, standard_strings());
546 : 0 : line_saved_in_history = false;
547 : 0 : prompt_status = PROMPT_READY;
548 : : /* we'll want to redisplay after parsing what we have */
549 : 0 : need_redisplay = true;
550 : 0 : }
551 [ + + ]: 2205 : else if (slashCmdStatus == PSQL_CMD_TERMINATE)
552 : 3 : break;
553 : 2661 : }
554 : :
555 : : /* fall out of loop if lexer reached EOL */
556 [ + + + + ]: 152316 : if (scan_result == PSCAN_INCOMPLETE ||
557 : 69632 : scan_result == PSCAN_EOL)
558 : 99161 : break;
559 [ + + ]: 152319 : }
560 : :
561 : : /*
562 : : * Add line to pending history if we didn't do so already. Then, if
563 : : * the query buffer is still empty, flush out any unsent history
564 : : * entry. This means that empty lines (containing only whitespace and
565 : : * perhaps a dash-dash comment) that precede a query will be recorded
566 : : * as separate history entries, not as part of that query.
567 : : */
568 [ + - ]: 99164 : if (pset.cur_cmd_interactive)
569 : : {
570 [ # # ]: 0 : if (!line_saved_in_history)
571 : 0 : pg_append_history(line, history_buf);
572 [ # # ]: 0 : if (query_buf->len == 0)
573 : 0 : pg_send_history(history_buf);
574 : 0 : }
575 : :
576 : 99164 : psql_scan_finish(scan_state);
577 : 99164 : free(line);
578 : :
579 [ + + ]: 99164 : if (slashCmdStatus == PSQL_CMD_TERMINATE)
580 : : {
581 : 3 : successResult = EXIT_SUCCESS;
582 : 3 : break;
583 : : }
584 : :
585 [ - + ]: 99161 : if (!pset.cur_cmd_interactive)
586 : : {
587 [ + + + - ]: 99161 : if (!success && die_on_error)
588 : 0 : successResult = EXIT_USER;
589 : : /* Have we lost the db connection? */
590 [ + - ]: 99161 : else if (!pset.db)
591 : 0 : successResult = EXIT_BADCONN;
592 : 99161 : }
593 : : } /* while !endoffile/session */
594 : :
595 : : /*
596 : : * If we have a non-semicolon-terminated query at the end of file, we
597 : : * process it unless the input source is interactive --- in that case it
598 : : * seems better to go ahead and quit. Also skip if this is an error exit.
599 : : */
600 [ + + + - : 269 : if (query_buf->len > 0 && !pset.cur_cmd_interactive &&
- + ]
601 : 21 : successResult == EXIT_SUCCESS)
602 : : {
603 : : /* save query in history */
604 : : /* currently unneeded since we don't use this block if interactive */
605 : : #ifdef NOT_USED
606 : : if (pset.cur_cmd_interactive)
607 : : pg_send_history(history_buf);
608 : : #endif
609 : :
610 : : /* execute query unless we're in an inactive \if branch */
611 [ + - ]: 21 : if (conditional_active(cond_stack))
612 : : {
613 : 21 : success = SendQuery(query_buf->data);
614 : 21 : }
615 : : else
616 : : {
617 [ # # ]: 0 : if (pset.cur_cmd_interactive)
618 : 0 : pg_log_error("query ignored; use \\endif or Ctrl-C to exit current \\if block");
619 : 0 : success = true;
620 : : }
621 : :
622 [ - + # # ]: 21 : if (!success && die_on_error)
623 : 0 : successResult = EXIT_USER;
624 [ + - ]: 21 : else if (pset.db == NULL)
625 : 0 : successResult = EXIT_BADCONN;
626 : 21 : }
627 : :
628 : : /*
629 : : * Check for unbalanced \if-\endifs unless user explicitly quit, or the
630 : : * script is erroring out
631 : : */
632 [ + + ]: 269 : if (slashCmdStatus != PSQL_CMD_TERMINATE &&
633 [ + - + - ]: 266 : successResult != EXIT_USER &&
634 : 266 : !conditional_stack_empty(cond_stack))
635 : : {
636 : 0 : pg_log_error("reached EOF without finding closing \\endif(s)");
637 [ # # # # ]: 0 : if (die_on_error && !pset.cur_cmd_interactive)
638 : 0 : successResult = EXIT_USER;
639 : 0 : }
640 : :
641 : : /*
642 : : * Let's just make real sure the SIGINT handler won't try to use
643 : : * sigint_interrupt_jmp after we exit this routine. If there is an outer
644 : : * MainLoop instance, it will reset sigint_interrupt_jmp to point to
645 : : * itself at the top of its loop, before any further interactive input
646 : : * happens.
647 : : */
648 : 269 : sigint_interrupt_enabled = false;
649 : :
650 : 269 : destroyPQExpBuffer(query_buf);
651 : 269 : destroyPQExpBuffer(previous_buf);
652 : 269 : destroyPQExpBuffer(history_buf);
653 : :
654 : 269 : psql_scan_destroy(scan_state);
655 : 269 : conditional_stack_destroy(cond_stack);
656 : :
657 : 269 : pset.cur_cmd_source = prev_cmd_source;
658 : 269 : pset.cur_cmd_interactive = prev_cmd_interactive;
659 : 269 : pset.lineno = prev_lineno;
660 : :
661 : 538 : return successResult;
662 : 269 : } /* MainLoop() */
|