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