LCOV - differential code coverage report
Current view: top level - src/bin/psql - mainloop.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DUB DCB
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 64.0 % 261 167 6 88 3 164 6 3
Current Date: 2026-11-28 16:48:44 +0900 Functions: 100.0 % 1 1 1
Baseline: lcov-20261128-baseline Branches: 62.6 % 214 134 1 79 1 133
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 33.3 % 3 1 2 1
(30,360] days: 60.0 % 10 6 4 2 4
(360..) days: 64.5 % 248 160 88 160
Function coverage date bins:
(360..) days: 100.0 % 1 1 1
Branch coverage date bins:
(7,30] days: 50.0 % 2 1 1 1
(360..) days: 62.7 % 212 133 79 133

 Age         Owner                    Branch data    TLA  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
 9811 peter_e@gmx.net            34                 :CBC       11139 : 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;
 9768                            48                 :          11139 :     volatile int successResult = EXIT_SUCCESS;
 7650                            49                 :          11139 :     volatile backslashResult slashCmdStatus = PSQL_CMD_UNKNOWN;
 8318 tgl@sss.pgh.pa.us          50                 :          11139 :     volatile promptStatus_t prompt_status = PROMPT_READY;
 2563                            51                 :          11139 :     volatile bool need_redisplay = false;
 9726 bruce@momjian.us           52                 :          11139 :     volatile int count_eof = 0;
 8318 tgl@sss.pgh.pa.us          53                 :          11139 :     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 */
 9815 peter_e@gmx.net            59                 :          11139 :     prev_cmd_source = pset.cur_cmd_source;
                                 60                 :          11139 :     prev_cmd_interactive = pset.cur_cmd_interactive;
 8318 tgl@sss.pgh.pa.us          61                 :          11139 :     prev_lineno = pset.lineno;
                                 62                 :                :     /* pset.stmt_lineno does not need to be saved and restored */
                                 63                 :                : 
                                 64                 :                :     /* Establish new source */
 9815 peter_e@gmx.net            65                 :          11139 :     pset.cur_cmd_source = source;
                                 66   [ +  +  +  + ]:          11139 :     pset.cur_cmd_interactive = ((source == stdin) && !pset.notty);
 8318 tgl@sss.pgh.pa.us          67                 :          11139 :     pset.lineno = 0;
 4470 andres@anarazel.de         68                 :          11139 :     pset.stmt_lineno = 1;
                                 69                 :                : 
                                 70                 :                :     /* Create working state */
 3907 tgl@sss.pgh.pa.us          71                 :          11139 :     scan_state = psql_scan_create(&psqlscan_callbacks);
 3530                            72                 :          11139 :     cond_stack = conditional_stack_create();
  730 peter@eisentraut.org       73                 :          11139 :     psql_scan_set_passthrough(scan_state, cond_stack);
                                 74                 :                : 
 9886 bruce@momjian.us           75                 :          11139 :     query_buf = createPQExpBuffer();
 9726                            76                 :          11139 :     previous_buf = createPQExpBuffer();
 7595                            77                 :          11139 :     history_buf = createPQExpBuffer();
 6576 tgl@sss.pgh.pa.us          78   [ +  -  +  - ]:          11139 :     if (PQExpBufferBroken(query_buf) ||
                                 79   [ +  -  +  -  :          11139 :         PQExpBufferBroken(previous_buf) ||
                                              +  - ]
                                 80         [ -  + ]:          11139 :         PQExpBufferBroken(history_buf))
 1695 tgl@sss.pgh.pa.us          81                 :UBC           0 :         pg_fatal("out of memory");
                                 82                 :                : 
                                 83                 :                :     /* main loop to get queries and execute them */
 8654 bruce@momjian.us           84         [ +  + ]:CBC      657858 :     while (successResult == EXIT_SUCCESS)
                                 85                 :                :     {
                                 86                 :                :         /*
                                 87                 :                :          * Clean up after a previous Control-C
                                 88                 :                :          */
    9 peter@eisentraut.org       89         [ -  + ]:GNC      657763 :         if (CancelRequested)
                                 90                 :                :         {
 9726 bruce@momjian.us           91         [ #  # ]:UBC           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                 :                : 
    9 peter@eisentraut.org      100                 :UNC           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                 :                :          */
 7472 tgl@sss.pgh.pa.us         108         [ -  + ]:CBC      657763 :         if (sigsetjmp(sigint_interrupt_jmp, 1) != 0)
                                109                 :                :         {
                                110                 :                :             /* got here with longjmp */
                                111                 :                : 
                                112                 :                :             /* reset parsing state */
 8318 tgl@sss.pgh.pa.us         113                 :UBC           0 :             psql_scan_finish(scan_state);
                                114                 :              0 :             psql_scan_reset(scan_state);
 7472                           115                 :              0 :             resetPQExpBuffer(query_buf);
                                116                 :              0 :             resetPQExpBuffer(history_buf);
 8318                           117                 :              0 :             count_eof = 0;
 7650 peter_e@gmx.net           118                 :              0 :             slashCmdStatus = PSQL_CMD_UNKNOWN;
 8318 tgl@sss.pgh.pa.us         119                 :              0 :             prompt_status = PROMPT_READY;
 2563                           120                 :              0 :             need_redisplay = false;
 4470 andres@anarazel.de        121                 :              0 :             pset.stmt_lineno = 1;
    9 peter@eisentraut.org      122                 :UNC           0 :             CancelRequested = false;
                                123                 :                : 
 9726 bruce@momjian.us          124         [ #  # ]:UBC           0 :             if (pset.cur_cmd_interactive)
                                125                 :                :             {
 8654                           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                 :                :                  */
 3530 tgl@sss.pgh.pa.us         132         [ #  # ]:              0 :                 if (!conditional_stack_empty(cond_stack))
                                133                 :                :                 {
 2798 peter@eisentraut.org      134                 :              0 :                     pg_log_error("\\if: escaped");
 3530 tgl@sss.pgh.pa.us         135                 :              0 :                     conditional_stack_pop(cond_stack);
                                136                 :                :                 }
                                137                 :                :             }
                                138                 :                :             else
                                139                 :                :             {
 9726 bruce@momjian.us          140                 :              0 :                 successResult = EXIT_USER;
                                141                 :              0 :                 break;
                                142                 :                :             }
                                143                 :                :         }
                                144                 :                : 
 8347 tgl@sss.pgh.pa.us         145                 :CBC      657763 :         fflush(stdout);
                                146                 :                : 
                                147                 :                :         /*
                                148                 :                :          * get another line
                                149                 :                :          */
 7475                           150         [ +  + ]:         657763 :         if (pset.cur_cmd_interactive)
                                151                 :                :         {
                                152                 :                :             /* May need to reset prompt, eg after \r command */
 8318                           153         [ +  + ]:             70 :             if (query_buf->len == 0)
 8517 bruce@momjian.us          154                 :             68 :                 prompt_status = PROMPT_READY;
                                155                 :                :             /* If query buffer came from \e, redisplay it with a prompt */
 2563 tgl@sss.pgh.pa.us         156         [ -  + ]:             70 :             if (need_redisplay)
                                157                 :                :             {
 2563 tgl@sss.pgh.pa.us         158         [ #  # ]:UBC           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 */
 3530 tgl@sss.pgh.pa.us         167                 :CBC          70 :             line = gets_interactive(get_prompt(prompt_status, cond_stack),
                                168                 :                :                                     query_buf);
                                169                 :                :         }
                                170                 :                :         else
                                171                 :                :         {
 8517 bruce@momjian.us          172                 :         657693 :             line = gets_fromFile(source);
 6940 peter_e@gmx.net           173   [ +  +  -  + ]:         657693 :             if (!line && ferror(source))
 6940 peter_e@gmx.net           174                 :UBC           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 */
 9883 bruce@momjian.us          183         [ +  + ]:CBC      657763 :         if (line == NULL)
                                184                 :                :         {
 9815 peter_e@gmx.net           185         [ -  + ]:          10779 :             if (pset.cur_cmd_interactive)
                                186                 :                :             {
                                187                 :                :                 /* This tries to mimic bash's IGNOREEOF feature. */
 8654 bruce@momjian.us          188                 :UBC           0 :                 count_eof++;
                                189                 :                : 
 3586 tgl@sss.pgh.pa.us         190         [ #  # ]:              0 :                 if (count_eof < pset.ignoreeof)
                                191                 :                :                 {
 7396                           192         [ #  # ]:              0 :                     if (!pset.quiet)
 7949 bruce@momjian.us          193                 :              0 :                         printf(_("Use \"\\q\" to leave %s.\n"), pset.progname);
 9726                           194                 :              0 :                     continue;
                                195                 :                :                 }
                                196                 :                : 
 7396 tgl@sss.pgh.pa.us         197         [ #  # ]:              0 :                 puts(pset.quiet ? "" : "\\q");
                                198                 :                :             }
 8517 bruce@momjian.us          199                 :CBC       10779 :             break;
                                200                 :                :         }
                                201                 :                : 
                                202                 :         646984 :         count_eof = 0;
                                203                 :                : 
 9726                           204                 :         646984 :         pset.lineno++;
                                205                 :                : 
                                206                 :                :         /* ignore UTF-8 Unicode byte-order mark */
 6216 peter_e@gmx.net           207   [ +  +  +  +  :         646984 :         if (pset.lineno == 1 && pset.encoding == PG_UTF8 && strncmp(line, "\xef\xbb\xbf", 3) == 0)
                                              -  + ]
 6216 peter_e@gmx.net           208                 :UBC           0 :             memmove(line, line + 3, strlen(line + 3) + 1);
                                209                 :                : 
                                210                 :                :         /* Detect attempts to run custom-format dumps as SQL scripts */
 4418 alvherre@alvh.no-ip.      211   [ +  +  +  + ]:CBC      646984 :         if (pset.lineno == 1 && !pset.cur_cmd_interactive &&
                                212         [ -  + ]:          11102 :             strncmp(line, "PGDMP", 5) == 0)
                                213                 :                :         {
  150 peter@eisentraut.org      214                 :UNC           0 :             pg_free(line);
 4418 alvherre@alvh.no-ip.      215                 :UBC           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 */
 8318 tgl@sss.pgh.pa.us         223   [ +  +  +  + ]:CBC      646984 :         if (line[0] == '\0' && !psql_scan_in_quote(scan_state))
                                224                 :                :         {
  150 peter@eisentraut.org      225                 :GNC      102664 :             pg_free(line);
 9886 bruce@momjian.us          226                 :CBC      102664 :             continue;
                                227                 :                :         }
                                228                 :                : 
                                229                 :                :         /* Recognize "help", "quit", "exit" only in interactive mode */
 3222                           230         [ +  + ]:         544320 :         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;
 3211                           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                 :                :              */
 3222                           244         [ -  + ]:             64 :             if (pg_strncasecmp(first_word, "help", 4) == 0)
                                245                 :                :             {
 3222 bruce@momjian.us          246                 :UBC           0 :                 rest_of_line = first_word + 4;
                                247                 :              0 :                 found_help = true;
                                248                 :                :             }
 3222 bruce@momjian.us          249         [ +  - ]:CBC          64 :             else if (pg_strncasecmp(first_word, "exit", 4) == 0 ||
                                250         [ -  + ]:             64 :                      pg_strncasecmp(first_word, "quit", 4) == 0)
                                251                 :                :             {
 3222 bruce@momjian.us          252                 :UBC           0 :                 rest_of_line = first_word + 4;
                                253                 :              0 :                 found_exit_or_quit = true;
                                254                 :                :             }
 3211 bruce@momjian.us          255         [ +  + ]:CBC          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                 :                :              */
 3222                           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))
 3222 bruce@momjian.us          274                 :UBC           0 :                     ++rest_of_line;
 3222 bruce@momjian.us          275         [ -  + ]:CBC           3 :                 if (*rest_of_line == ';')
 3222 bruce@momjian.us          276                 :UBC           0 :                     ++rest_of_line;
 3222 bruce@momjian.us          277         [ -  + ]:CBC           3 :                 while (isspace((unsigned char) *rest_of_line))
 3222 bruce@momjian.us          278                 :UBC           0 :                     ++rest_of_line;
 3222 bruce@momjian.us          279         [ -  + ]:CBC           3 :                 if (*rest_of_line != '\0')
                                280                 :                :                 {
 3222 bruce@momjian.us          281                 :UBC           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                 :                :              */
 3222 bruce@momjian.us          292         [ -  + ]:CBC          64 :             if (found_help)
                                293                 :                :             {
 3222 bruce@momjian.us          294         [ #  # ]:UBC           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"));
  150 peter@eisentraut.org      308                 :UNC           0 :                     pg_free(line);
 3222 bruce@momjian.us          309                 :UBC           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                 :                :              */
 3222 bruce@momjian.us          320         [ -  + ]:CBC          64 :             if (found_exit_or_quit)
                                321                 :                :             {
 3222 bruce@momjian.us          322         [ #  # ]:UBC           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 */
  150 peter@eisentraut.org      338                 :UNC           0 :                     pg_free(line);
 3222 bruce@momjian.us          339                 :UBC           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                 :                :              */
 3211 bruce@momjian.us          349   [ +  +  -  + ]:CBC          64 :             if (found_q && query_buf->len != 0 &&
 3211 bruce@momjian.us          350         [ #  # ]:UBC           0 :                 prompt_status != PROMPT_READY &&
                                351         [ #  # ]:              0 :                 prompt_status != PROMPT_CONTINUE &&
                                352         [ #  # ]:              0 :                 prompt_status != PROMPT_PAREN)
                                353                 :                : #ifndef WIN32
 3138 tgl@sss.pgh.pa.us         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 */
 7396 tgl@sss.pgh.pa.us         361   [ +  +  +  - ]:CBC      544320 :         if (pset.echo == PSQL_ECHO_ALL && !pset.cur_cmd_interactive)
                                362                 :                :         {
 9726 bruce@momjian.us          363                 :         474376 :             puts(line);
 4319 tgl@sss.pgh.pa.us         364                 :         474376 :             fflush(stdout);
                                365                 :                :         }
                                366                 :                : 
                                367                 :                :         /* insert newlines into query buffer between source lines */
 8318                           368         [ +  + ]:         544320 :         if (query_buf->len > 0)
                                369                 :                :         {
                                370                 :         172968 :             appendPQExpBufferChar(query_buf, '\n');
                                371                 :         172968 :             added_nl_pos = query_buf->len;
                                372                 :                :         }
                                373                 :                :         else
                                374                 :         371352 :             added_nl_pos = -1;  /* flag we didn't add one */
                                375                 :                : 
                                376                 :                :         /* Setting this will not have effect until next line. */
 7396                           377                 :         544320 :         die_on_error = pset.on_error_stop;
                                378                 :                : 
                                379                 :                :         /*
                                380                 :                :          * Parse line, looking for command separators.
                                381                 :                :          */
 3907                           382                 :         544320 :         psql_scan_setup(scan_state, line, strlen(line),
                                383                 :         544320 :                         pset.encoding, standard_strings());
 9886 bruce@momjian.us          384                 :         544320 :         success = true;
 7475 tgl@sss.pgh.pa.us         385                 :         544320 :         line_saved_in_history = false;
                                386                 :                : 
 8318                           387   [ +  +  +  + ]:         830168 :         while (success || !die_on_error)
                                388                 :                :         {
                                389                 :                :             PsqlScanResult scan_result;
                                390                 :         830073 :             promptStatus_t prompt_tmp = prompt_status;
                                391                 :                :             size_t      pos_in_query;
                                392                 :                :             char       *tmp_line;
                                393                 :                : 
 4470 andres@anarazel.de        394                 :         830073 :             pos_in_query = query_buf->len;
 8318 tgl@sss.pgh.pa.us         395                 :         830073 :             scan_result = psql_scan(scan_state, query_buf, &prompt_tmp);
                                396                 :         830073 :             prompt_status = prompt_tmp;
                                397                 :                : 
 6576                           398   [ +  -  -  + ]:         830073 :             if (PQExpBufferBroken(query_buf))
 1695 tgl@sss.pgh.pa.us         399                 :UBC           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                 :                :              */
 4470 andres@anarazel.de        407                 :CBC      830073 :             tmp_line = query_buf->data + pos_in_query;
                                408         [ +  + ]:       19205874 :             while (*tmp_line != '\0')
                                409                 :                :             {
                                410         [ +  + ]:       18375801 :                 if (*(tmp_line++) == '\n')
                                411                 :            120 :                     pset.stmt_lineno++;
                                412                 :                :             }
                                413                 :                : 
                                414         [ +  + ]:         830073 :             if (scan_result == PSCAN_EOL)
                                415                 :          98094 :                 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                 :                :              */
 8318 tgl@sss.pgh.pa.us         421   [ +  +  +  + ]:         830073 :             if (scan_result == PSCAN_SEMICOLON ||
 7396                           422         [ -  + ]:          98094 :                 (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                 :                :                  */
 7475                           430   [ +  +  +  - ]:         253450 :                 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 */
 3530                           438         [ +  + ]:         506890 :                 if (conditional_active(cond_stack))
                                439                 :                :                 {
                                440                 :                :                     /* count_copy_from_stdin should be reliable here */
  110                           441                 :         253442 :                     success = SendQuery(query_buf->data,
                                442                 :                :                                         psql_scan_count_copy_from_stdin(scan_state));
 3530                           443         [ +  + ]:         253432 :                     slashCmdStatus = success ? PSQL_CMD_SEND : PSQL_CMD_ERROR;
                                444                 :         253432 :                     pset.stmt_lineno = 1;
                                445                 :                : 
                                446                 :                :                     /* transfer query to previous_buf by pointer-swapping */
                                447                 :                :                     {
                                448                 :         253432 :                         PQExpBuffer swap_buf = previous_buf;
                                449                 :                : 
                                450                 :         253432 :                         previous_buf = query_buf;
                                451                 :         253432 :                         query_buf = swap_buf;
                                452                 :                :                     }
                                453                 :         253432 :                     resetPQExpBuffer(query_buf);
                                454                 :                :                     /* reset parsing state, too */
  110                           455                 :         253432 :                     psql_scan_reset(scan_state);
                                456                 :                : 
 3530                           457                 :         253432 :                     added_nl_pos = -1;
                                458                 :                :                 }
                                459                 :                :                 else
                                460                 :                :                 {
                                461                 :                :                     /* if interactive, warn about non-executed query */
                                462         [ -  + ]:              8 :                     if (pset.cur_cmd_interactive)
 2798 peter@eisentraut.org      463                 :UBC           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 */
 3530 tgl@sss.pgh.pa.us         465                 :CBC           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                 :                :             }
 8318                           471         [ +  + ]:         576623 :             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         [ +  + ]:          32663 :                 if (query_buf->len == added_nl_pos)
                                485                 :                :                 {
                                486                 :            199 :                     query_buf->data[--query_buf->len] = '\0';
 7475                           487                 :            199 :                     pg_send_history(history_buf);
                                488                 :                :                 }
 8318                           489                 :          32663 :                 added_nl_pos = -1;
                                490                 :                : 
                                491                 :                :                 /* save backslash command in history */
 7475                           492   [ +  +  +  + ]:          32663 :                 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 */
 8318                           500                 :          32663 :                 slashCmdStatus = HandleSlashCmds(scan_state,
                                501                 :                :                                                  cond_stack,
                                502                 :                :                                                  query_buf,
                                503                 :                :                                                  previous_buf);
                                504                 :                : 
 7650 peter_e@gmx.net           505                 :          32661 :                 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                 :                :                  */
 3530 tgl@sss.pgh.pa.us         512                 :          32661 :                 pset.stmt_lineno = 1;
                                513                 :                : 
 7650 peter_e@gmx.net           514         [ +  + ]:          32661 :                 if (slashCmdStatus == PSQL_CMD_SEND)
                                515                 :                :                 {
                                516                 :                :                     /* should not see this in inactive branch */
 3530 tgl@sss.pgh.pa.us         517         [ -  + ]:           2364 :                     Assert(conditional_active(cond_stack));
                                518                 :                : 
  110                           519                 :           2364 :                     success = SendQuery(query_buf->data, -1);
                                520                 :                : 
                                521                 :                :                     /* transfer query to previous_buf by pointer-swapping */
                                522                 :                :                     {
 6576                           523                 :           2360 :                         PQExpBuffer swap_buf = previous_buf;
                                524                 :                : 
                                525                 :           2360 :                         previous_buf = query_buf;
                                526                 :           2360 :                         query_buf = swap_buf;
                                527                 :                :                     }
 9726 bruce@momjian.us          528                 :           2360 :                     resetPQExpBuffer(query_buf);
                                529                 :                :                     /* reset parsing state, too */
 8318 tgl@sss.pgh.pa.us         530                 :           2360 :                     psql_scan_reset(scan_state);
                                531                 :                :                 }
 7475                           532         [ -  + ]:          30297 :                 else if (slashCmdStatus == PSQL_CMD_NEWEDIT)
                                533                 :                :                 {
                                534                 :                :                     /* should not see this in inactive branch */
 3530 tgl@sss.pgh.pa.us         535         [ #  # ]:UBC           0 :                     Assert(conditional_active(cond_stack));
                                536                 :                :                     /* ensure what came back from editing ends in a newline */
 2563                           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 */
 7475                           541                 :              0 :                     psql_scan_finish(scan_state);
  150 peter@eisentraut.org      542                 :UNC           0 :                     pg_free(line);
 7475 tgl@sss.pgh.pa.us         543                 :UBC           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);
 3907                           547                 :              0 :                     psql_scan_setup(scan_state, line, strlen(line),
                                548                 :              0 :                                     pset.encoding, standard_strings());
 7475                           549                 :              0 :                     line_saved_in_history = false;
                                550                 :              0 :                     prompt_status = PROMPT_READY;
                                551                 :                :                     /* we'll want to redisplay after parsing what we have */
 2563                           552                 :              0 :                     need_redisplay = true;
                                553                 :                :                 }
 7475 tgl@sss.pgh.pa.us         554         [ +  + ]:CBC       30297 :                 else if (slashCmdStatus == PSQL_CMD_TERMINATE)
                                555                 :         544209 :                     break;
                                556                 :                :             }
                                557                 :                : 
                                558                 :                :             /* fall out of loop if lexer reached EOL */
                                559   [ +  +  +  + ]:         829808 :             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                 :                :          */
 1823                           571         [ +  + ]:         544304 :         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                 :                : 
 8318                           579                 :         544304 :         psql_scan_finish(scan_state);
  150 peter@eisentraut.org      580                 :GNC      544304 :         pg_free(line);
                                581                 :                : 
 7650 peter_e@gmx.net           582         [ +  + ]:CBC      544304 :         if (slashCmdStatus == PSQL_CMD_TERMINATE)
                                583                 :                :         {
 8318 tgl@sss.pgh.pa.us         584                 :            249 :             successResult = EXIT_SUCCESS;
                                585                 :            249 :             break;
                                586                 :                :         }
                                587                 :                : 
 8654 bruce@momjian.us          588         [ +  + ]:         544055 :         if (!pset.cur_cmd_interactive)
                                589                 :                :         {
                                590   [ +  +  +  + ]:         543994 :             if (!success && die_on_error)
 8517                           591                 :             95 :                 successResult = EXIT_USER;
                                592                 :                :             /* Have we lost the db connection? */
 8654                           593         [ -  + ]:         543899 :             else if (!pset.db)
 8517 bruce@momjian.us          594                 :UBC           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                 :                :      */
 9381 tgl@sss.pgh.pa.us         603   [ +  +  +  - ]:CBC       11123 :     if (query_buf->len > 0 && !pset.cur_cmd_interactive &&
                                604         [ +  - ]:           6102 :         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 */
 3530                           614         [ +  - ]:           6102 :         if (conditional_active(cond_stack))
                                615                 :                :         {
  110                           616                 :           6102 :             success = SendQuery(query_buf->data,
                                617                 :                :                                 psql_scan_count_copy_from_stdin(scan_state));
                                618                 :                :         }
                                619                 :                :         else
                                620                 :                :         {
 3530 tgl@sss.pgh.pa.us         621         [ #  # ]:UBC           0 :             if (pset.cur_cmd_interactive)
 2798 peter@eisentraut.org      622                 :              0 :                 pg_log_error("query ignored; use \\endif or Ctrl-C to exit current \\if block");
 3530 tgl@sss.pgh.pa.us         623                 :              0 :             success = true;
                                624                 :                :         }
                                625                 :                : 
 9726 bruce@momjian.us          626   [ +  +  +  - ]:CBC        6101 :         if (!success && die_on_error)
                                627                 :             78 :             successResult = EXIT_USER;
                                628         [ -  + ]:           6023 :         else if (pset.db == NULL)
 9726 bruce@momjian.us          629                 :UBC           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                 :                :      */
 3530 tgl@sss.pgh.pa.us         636         [ +  + ]:CBC       11122 :     if (slashCmdStatus != PSQL_CMD_TERMINATE &&
                                637         [ +  + ]:          10873 :         successResult != EXIT_USER &&
                                638         [ -  + ]:          10700 :         !conditional_stack_empty(cond_stack))
                                639                 :                :     {
 2798 peter@eisentraut.org      640                 :UBC           0 :         pg_log_error("reached EOF without finding closing \\endif(s)");
 3530 tgl@sss.pgh.pa.us         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                 :                :      */
 7472 tgl@sss.pgh.pa.us         652                 :CBC       11122 :     sigint_interrupt_enabled = false;
                                653                 :                : 
 9886 bruce@momjian.us          654                 :          11122 :     destroyPQExpBuffer(query_buf);
 9850                           655                 :          11122 :     destroyPQExpBuffer(previous_buf);
 7595                           656                 :          11122 :     destroyPQExpBuffer(history_buf);
                                657                 :                : 
 8318 tgl@sss.pgh.pa.us         658                 :          11122 :     psql_scan_destroy(scan_state);
 3530                           659                 :          11122 :     conditional_stack_destroy(cond_stack);
                                660                 :                : 
 9815 peter_e@gmx.net           661                 :          11122 :     pset.cur_cmd_source = prev_cmd_source;
                                662                 :          11122 :     pset.cur_cmd_interactive = prev_cmd_interactive;
 9726 bruce@momjian.us          663                 :          11122 :     pset.lineno = prev_lineno;
                                664                 :                : 
 9886                           665                 :          11122 :     return successResult;
                                666                 :                : }                               /* MainLoop() */
        

Generated by: LCOV version 2.0-1