46 bool line_saved_in_history;
50 volatile bool need_redisplay =
false;
51 volatile int count_eof = 0;
52 volatile bool die_on_error =
false;
53 FILE *prev_cmd_source;
54 bool prev_cmd_interactive;
119 need_redisplay =
false;
152 if (query_buf->
len == 0)
157 if (query_buf->
len > 0)
163 need_redisplay =
false;
172 if (!line && ferror(
source))
207 memmove(line, line + 3, strlen(line + 3) + 1);
211 strncmp(line,
"PGDMP", 5) == 0)
214 puts(
_(
"The input is a PostgreSQL custom-format dump.\n"
215 "Use the pg_restore command-line client to restore this dump to a database.\n"));
231 char *first_word = line;
232 char *rest_of_line = NULL;
233 bool found_help =
false;
234 bool found_exit_or_quit =
false;
235 bool found_q =
false;
245 rest_of_line = first_word + 4;
251 rest_of_line = first_word + 4;
252 found_exit_or_quit =
true;
254 else if (strncmp(first_word,
"\\q", 2) == 0)
256 rest_of_line = first_word + 2;
266 if (rest_of_line != NULL)
272 while (isspace((
unsigned char) *rest_of_line))
274 if (*rest_of_line ==
';')
276 while (isspace((
unsigned char) *rest_of_line))
278 if (*rest_of_line !=
'\0')
281 found_exit_or_quit =
false;
293 if (query_buf->
len != 0)
295 puts(
_(
"Use \\? for help or press control-C to clear the input buffer."));
297 puts(
_(
"Use \\? for help."));
301 puts(
_(
"You are using psql, the command-line interface to PostgreSQL."));
302 printf(
_(
"Type: \\copyright for distribution terms\n"
303 " \\h for help with SQL commands\n"
304 " \\? for help with psql commands\n"
305 " \\g or terminate with semicolon to execute query\n"
319 if (found_exit_or_quit)
321 if (query_buf->
len != 0)
326 puts(
_(
"Use \\q to quit."));
329 puts(
_(
"Use control-D to quit."));
331 puts(
_(
"Use control-C to quit."));
348 if (found_q && query_buf->
len != 0 &&
353 puts(
_(
"Use control-D to quit."));
355 puts(
_(
"Use control-C to quit."));
367 if (query_buf->
len > 0)
370 added_nl_pos = query_buf->
len;
384 line_saved_in_history =
false;
386 while (
success || !die_on_error)
393 pos_in_query = query_buf->
len;
394 scan_result =
psql_scan(scan_state, query_buf, &prompt_tmp);
395 prompt_status = prompt_tmp;
406 tmp_line = query_buf->
data + pos_in_query;
407 while (*tmp_line !=
'\0')
409 if (*(tmp_line++) ==
'\n')
433 line_saved_in_history =
true;
447 previous_buf = query_buf;
448 query_buf = swap_buf;
459 pg_log_error(
"query ignored; use \\endif or Ctrl-C to exit current \\if block");
480 if (query_buf->
len == added_nl_pos)
482 query_buf->
data[--query_buf->
len] =
'\0';
492 line_saved_in_history =
true;
521 previous_buf = query_buf;
522 query_buf = swap_buf;
534 if (query_buf->
len > 0 &&
535 query_buf->
data[query_buf->
len - 1] !=
'\n')
546 line_saved_in_history =
false;
549 need_redisplay =
true;
570 if (!line_saved_in_history)
572 if (query_buf->
len == 0)
618 pg_log_error(
"query ignored; use \\endif or Ctrl-C to exit current \\if block");
636 pg_log_error(
"reached EOF without finding closing \\endif(s)");
661 return successResult;
volatile sig_atomic_t sigint_interrupt_enabled
sigjmp_buf sigint_interrupt_jmp
bool standard_strings(void)
bool SendQuery(const char *query)
#define Assert(condition)
backslashResult HandleSlashCmds(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf, PQExpBuffer previous_buf)
enum _backslashResult backslashResult
ConditionalStack conditional_stack_create(void)
bool conditional_stack_pop(ConditionalStack cstack)
void conditional_stack_destroy(ConditionalStack cstack)
bool conditional_active(ConditionalStack cstack)
bool conditional_stack_empty(ConditionalStack cstack)
char * pg_strdup(const char *in)
volatile sig_atomic_t cancel_pressed
static void const char fflush(stdout)
#define pg_log_error(...)
const PsqlScanCallbacks psqlscan_callbacks
static rewind_source * source
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
PQExpBuffer createPQExpBuffer(void)
void resetPQExpBuffer(PQExpBuffer str)
void destroyPQExpBuffer(PQExpBuffer str)
void appendPQExpBufferChar(PQExpBuffer str, char ch)
#define PQExpBufferBroken(str)
char * get_prompt(promptStatus_t status, ConditionalStack cstack)
void psql_scan_reset(PsqlScanState state)
enum _promptStatus promptStatus_t
void psql_scan_destroy(PsqlScanState state)
PsqlScanResult psql_scan(PsqlScanState state, PQExpBuffer query_buf, promptStatus_t *prompt)
PsqlScanState psql_scan_create(const PsqlScanCallbacks *callbacks)
void psql_scan_set_passthrough(PsqlScanState state, void *passthrough)
void psql_scan_setup(PsqlScanState state, const char *line, int line_len, int encoding, bool std_strings)
bool psql_scan_in_quote(PsqlScanState state)
void psql_scan_finish(PsqlScanState state)