27#include "catalog/pg_class_d.h"
80 bool show_verbose,
bool show_system);
139 const char *cmd,
bool is_func);
174 int lineno,
bool discard_on_quit,
bool *edited);
175static bool do_shell(
const char *command);
192static void checkWin32Codepage(
void);
234 Assert(scan_state != NULL);
241 status =
exec_command(cmd, scan_state, cstack, query_buf, previous_buf);
317 pg_log_warning(
"\\%s command ignored; use \\endif or Ctrl-C to exit current \\if block",
321 if (strcmp(cmd,
"a") == 0)
323 else if (strcmp(cmd,
"bind") == 0)
325 else if (strcmp(cmd,
"bind_named") == 0)
327 else if (strcmp(cmd,
"C") == 0)
329 else if (strcmp(cmd,
"c") == 0 || strcmp(cmd,
"connect") == 0)
331 else if (strcmp(cmd,
"cd") == 0)
333 else if (strcmp(cmd,
"close") == 0)
335 else if (strcmp(cmd,
"conninfo") == 0)
339 else if (strcmp(cmd,
"copyright") == 0)
341 else if (strcmp(cmd,
"crosstabview") == 0)
343 else if (cmd[0] ==
'd')
345 else if (strcmp(cmd,
"e") == 0 || strcmp(cmd,
"edit") == 0)
347 query_buf, previous_buf);
348 else if (strcmp(cmd,
"ef") == 0)
350 else if (strcmp(cmd,
"ev") == 0)
352 else if (strcmp(cmd,
"echo") == 0 || strcmp(cmd,
"qecho") == 0 ||
353 strcmp(cmd,
"warn") == 0)
355 else if (strcmp(cmd,
"elif") == 0)
357 else if (strcmp(cmd,
"else") == 0)
359 else if (strcmp(cmd,
"endif") == 0)
361 else if (strcmp(cmd,
"endpipeline") == 0)
363 else if (strcmp(cmd,
"encoding") == 0)
365 else if (strcmp(cmd,
"errverbose") == 0)
367 else if (strcmp(cmd,
"f") == 0)
369 else if (strcmp(cmd,
"flush") == 0)
371 else if (strcmp(cmd,
"flushrequest") == 0)
373 else if (strcmp(cmd,
"g") == 0 || strcmp(cmd,
"gx") == 0)
375 else if (strcmp(cmd,
"gdesc") == 0)
377 else if (strcmp(cmd,
"getenv") == 0)
379 else if (strcmp(cmd,
"getresults") == 0)
381 else if (strcmp(cmd,
"gexec") == 0)
383 else if (strcmp(cmd,
"gset") == 0)
385 else if (strcmp(cmd,
"h") == 0 || strcmp(cmd,
"help") == 0)
387 else if (strcmp(cmd,
"H") == 0 || strcmp(cmd,
"html") == 0)
389 else if (strcmp(cmd,
"i") == 0 || strcmp(cmd,
"include") == 0 ||
390 strcmp(cmd,
"ir") == 0 || strcmp(cmd,
"include_relative") == 0)
392 else if (strcmp(cmd,
"if") == 0)
394 else if (strcmp(cmd,
"l") == 0 || strcmp(cmd,
"list") == 0 ||
395 strcmp(cmd,
"lx") == 0 || strcmp(cmd,
"listx") == 0 ||
396 strcmp(cmd,
"l+") == 0 || strcmp(cmd,
"list+") == 0 ||
397 strcmp(cmd,
"lx+") == 0 || strcmp(cmd,
"listx+") == 0 ||
398 strcmp(cmd,
"l+x") == 0 || strcmp(cmd,
"list+x") == 0)
400 else if (strncmp(cmd,
"lo_", 3) == 0)
402 else if (strcmp(cmd,
"o") == 0 || strcmp(cmd,
"out") == 0)
404 else if (strcmp(cmd,
"p") == 0 || strcmp(cmd,
"print") == 0)
406 query_buf, previous_buf);
407 else if (strcmp(cmd,
"parse") == 0)
409 else if (strcmp(cmd,
"password") == 0)
411 else if (strcmp(cmd,
"prompt") == 0)
413 else if (strcmp(cmd,
"pset") == 0)
415 else if (strcmp(cmd,
"q") == 0 || strcmp(cmd,
"quit") == 0)
417 else if (strcmp(cmd,
"r") == 0 || strcmp(cmd,
"reset") == 0)
419 else if (strcmp(cmd,
"s") == 0)
421 else if (strcmp(cmd,
"sendpipeline") == 0)
423 else if (strcmp(cmd,
"set") == 0)
425 else if (strcmp(cmd,
"setenv") == 0)
427 else if (strcmp(cmd,
"sf") == 0 || strcmp(cmd,
"sf+") == 0)
429 else if (strcmp(cmd,
"sv") == 0 || strcmp(cmd,
"sv+") == 0)
431 else if (strcmp(cmd,
"startpipeline") == 0)
433 else if (strcmp(cmd,
"syncpipeline") == 0)
435 else if (strcmp(cmd,
"t") == 0)
437 else if (strcmp(cmd,
"T") == 0)
439 else if (strcmp(cmd,
"timing") == 0)
441 else if (strcmp(cmd,
"unset") == 0)
443 else if (strcmp(cmd,
"w") == 0 || strcmp(cmd,
"write") == 0)
445 query_buf, previous_buf);
446 else if (strcmp(cmd,
"watch") == 0)
448 query_buf, previous_buf);
449 else if (strcmp(cmd,
"x") == 0)
451 else if (strcmp(cmd,
"z") == 0 ||
452 strcmp(cmd,
"zS") == 0 || strcmp(cmd,
"zx") == 0 ||
453 strcmp(cmd,
"zSx") == 0 || strcmp(cmd,
"zxS") == 0)
455 else if (strcmp(cmd,
"!") == 0)
457 else if (strcmp(cmd,
"?") == 0)
514 if (nparams > nalloc)
516 nalloc = nalloc ? nalloc * 2 : 1;
564 if (nparams > nalloc)
566 nalloc = nalloc ? nalloc * 2 : 1;
623 static const char prefix[] =
"-reuse-previous=";
631 if (opt1 != NULL && strncmp(opt1, prefix,
sizeof(prefix) - 1) == 0)
686 dir = getenv(
"HOME");
687 if (dir == NULL || dir[0] ==
'\0')
689 uid_t user_id = geteuid();
693 pw = getpwuid(user_id);
698 pg_log_error(
"could not get home directory for user ID %ld: %s",
700 errno ?
strerror(errno) :
_(
"user does not exist"));
717 pg_log_error(
"\\%s: could not change directory to \"%s\": %m",
776 char *protocol_version,
789 printf(
_(
"You are currently not connected to a database.\n"));
804 hostaddr && *hostaddr && strcmp(host, hostaddr) != 0);
831 if (hostaddr && *hostaddr)
912 _(
"true") :
_(
"false"),
false,
false);
929 pfree(protocol_version);
1007 unsigned short int save_expanded;
1013 show_verbose = strchr(cmd,
'+') ?
true :
false;
1014 show_system = strchr(cmd,
'S') ?
true :
false;
1023 if (cmd[1] !=
'\0' && strchr(&cmd[2],
'x'))
1039 char *pattern2 = NULL;
1041 if (pattern && cmd[2] !=
'\0' && cmd[2] !=
'+' && cmd[2] !=
'x')
1078 if (strncmp(cmd,
"dconfig", 7) == 0)
1091 if (strncmp(cmd,
"ddp", 3) == 0)
1112 show_verbose, show_system);
1134 show_verbose, show_system);
1172 if (cmd[2] ==
'd' && cmd[3] ==
's')
1174 char *pattern2 = NULL;
1183 else if (cmd[2] ==
'g')
1282 const char *pattern,
1283 bool show_verbose,
bool show_system)
1287 int num_arg_patterns = 0;
1297 arg_patterns[num_arg_patterns++] = ap;
1305 arg_patterns, num_arg_patterns,
1306 show_verbose, show_system);
1309 arg_patterns, num_arg_patterns,
1310 show_verbose, show_system);
1312 while (--num_arg_patterns >= 0)
1313 free(arg_patterns[num_arg_patterns]);
1352 strspn(fname,
"0123456789") == strlen(fname))
1371 bool discard_on_quit;
1378 discard_on_quit =
true;
1391 if (
do_edit(fname, query_buf, lineno, discard_on_quit, NULL))
1453 "CREATE FUNCTION ( )\n"
1456 " -- common options: IMMUTABLE STABLE STRICT SECURITY DEFINER\n"
1463 " -- something...\n");
1475 else if (is_func && lineno > 0)
1486 const char *lines = query_buf->
data;
1488 while (*lines !=
'\0')
1490 if (strncmp(lines,
"AS ", 3) == 0 ||
1491 strncmp(lines,
"BEGIN ", 6) == 0 ||
1492 strncmp(lines,
"RETURN ", 7) == 0)
1496 lines = strchr(lines,
'\n');
1506 bool edited =
false;
1508 if (!
do_edit(NULL, query_buf, lineno,
true, &edited))
1511 puts(
_(
"No changes"));
1541 bool no_newline =
false;
1545 if (strcmp(cmd,
"qecho") == 0)
1547 else if (strcmp(cmd,
"warn") == 0)
1555 if (first && !no_newline && !quoted && strcmp(
value,
"-n") == 0)
1636 puts(
_(
"out of memory"));
1639 puts(
_(
"There is no previous error."));
1727 if (fname && fname[0] ==
'(')
1731 active_branch, cmd);
1742 pg_log_error(
"\\%s not allowed in pipeline mode", cmd);
1755 if (strcmp(cmd,
"gx") == 0)
1777 bool active_branch,
const char *cmd)
1780 bool found_r_paren =
false;
1807 if (optlen > 0 &&
option[optlen - 1] ==
')')
1810 found_r_paren =
true;
1817 char *valptr = strchr(
option,
'=');
1832 first_option = NULL;
1835 }
while (!found_r_paren);
1880 if (!myvar || !envvar)
1887 char *envval = getenv(envvar);
1921 num_results = atoi(opt);
1922 if (num_results < 0)
1924 pg_log_error(
"\\getresults: invalid number of requested results");
2055 bool include_relative;
2057 include_relative = (strcmp(cmd,
"ir") == 0
2058 || strcmp(cmd,
"include_relative") == 0);
2315 unsigned short int save_expanded;
2320 show_verbose = strchr(cmd,
'+') ?
true :
false;
2324 if (strchr(cmd,
'x'))
2359 if (strcmp(cmd + 3,
"export") == 0)
2373 else if (strcmp(cmd + 3,
"import") == 0)
2387 else if (strncmp(cmd + 3,
"list", 4) == 0)
2390 unsigned short int save_expanded;
2392 show_verbose = strchr(cmd,
'+') ?
true :
false;
2396 if (strchr(cmd,
'x'))
2405 else if (strcmp(cmd + 3,
"unlink") == 0)
2468 if (query_buf && query_buf->
len > 0)
2469 puts(query_buf->
data);
2470 else if (previous_buf && previous_buf->
len > 0)
2471 puts(previous_buf->
data);
2473 puts(
_(
"Query buffer is empty."));
2536 res =
PSQLexec(
"SELECT CURRENT_USER");
2561 else if (strcmp(pw1, pw2) != 0)
2602 *prompt_text = NULL;
2640 fputs(prompt_text,
stdout);
2646 pg_log_error(
"\\%s: could not read value for variable",
2687 static const char *
const my_list[] = {
2688 "border",
"columns",
"csv_fieldsep",
"expanded",
"fieldsep",
2689 "fieldsep_zero",
"footer",
"format",
"linestyle",
"null",
2690 "numericlocale",
"pager",
"pager_min_lines",
2691 "recordsep",
"recordsep_zero",
2692 "tableattr",
"title",
"tuples_only",
2693 "unicode_border_linestyle",
2694 "unicode_column_linestyle",
2695 "unicode_header_linestyle",
2700 for (
i = 0; my_list[
i] != NULL;
i++)
2748 puts(
_(
"Query buffer reset (cleared)."));
2770 printf(
_(
"Wrote history to file \"%s\".\n"), fname);
2800 pg_log_error(
"\\sendpipeline must be used after \\bind or \\bind_named");
2807 pg_log_error(
"\\sendpipeline not allowed outside of pipeline mode");
2892 else if (strchr(envvar,
'=') != NULL)
2894 pg_log_error(
"\\%s: environment variable name must not contain \"=\"",
2907 setenv(envvar, envval, 1);
2924 const char *cmd,
bool is_func)
2930 bool show_linenumbers = (strchr(cmd,
'+') != NULL);
2978 if (show_linenumbers)