21 #include <sys/types.h>
30 #include <sys/types.h>
67 int lineno,
bool *edited);
69 static bool do_shell(
const char *command);
78 const char *header_keyword);
86 static void checkWin32Codepage(
void);
128 psql_error(
"Invalid command \\%s. Try \\? for help.\n", cmd);
141 psql_error(
"\\%s: extra argument \"%s\" ignored\n", cmd, arg);
190 if (*result ==
'\0' || strcmp(result,
"-") == 0)
213 if (strcmp(cmd,
"a") == 0)
222 else if (strcmp(cmd,
"C") == 0)
246 else if (strcmp(cmd,
"c") == 0 || strcmp(cmd,
"connect") == 0)
267 else if (strcmp(cmd,
"cd") == 0)
279 uid_t user_id = geteuid();
282 pw = getpwuid(user_id);
285 psql_error(
"could not get home directory for user ID %ld: %s\n",
287 errno ?
strerror(errno) :
_(
"user does not exist"));
301 if (chdir(dir) == -1)
303 psql_error(
"\\%s: could not change directory to \"%s\": %s\n",
313 else if (strcmp(cmd,
"conninfo") == 0)
318 printf(
_(
"You are currently not connected to a database.\n"));
328 if (connOptions ==
NULL)
333 for (option = connOptions; option && option->
keyword; option++)
334 if (strcmp(option->
keyword,
"hostaddr") == 0)
336 if (option->
val !=
NULL && option->
val[0] !=
'\0')
343 printf(
_(
"You are connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\".\n"),
346 printf(
_(
"You are connected to database \"%s\" as user \"%s\" on host \"%s\" at port \"%s\".\n"),
365 else if (strcmp(cmd,
"copyright") == 0)
369 else if (strcmp(cmd,
"crosstabview") == 0)
381 else if (cmd[0] ==
'd')
391 show_verbose = strchr(cmd,
'+') ?
true :
false;
392 show_system = strchr(cmd,
'S') ?
true :
false;
415 success =
listCasts(pattern, show_verbose);
418 if (strncmp(cmd,
"ddp", 3) == 0)
424 success =
listDomains(pattern, show_verbose, show_system);
454 success =
listSchemas(pattern, show_verbose, show_system);
474 success =
listTables(&cmd[1], pattern, show_verbose, show_system);
477 if (cmd[2] ==
'd' && cmd[3] ==
's')
479 char *pattern2 =
NULL;
555 else if (strcmp(cmd,
"e") == 0 || strcmp(cmd,
"edit") == 0)
579 strspn(fname,
"0123456789") == strlen(fname))
617 else if (strcmp(cmd,
"ef") == 0)
623 psql_error(
"The server (version %d.%d) does not support editing function source.\n",
649 "CREATE FUNCTION ( )\n"
652 " -- common options: IMMUTABLE STABLE STRICT SECURITY DEFINER\n"
677 const char *lines = query_buf->
data;
679 while (*lines !=
'\0')
681 if (strncmp(lines,
"AS ", 3) == 0)
685 lines = strchr(lines,
'\n');
703 puts(
_(
"No changes"));
713 else if (strcmp(cmd,
"ev") == 0)
719 psql_error(
"The server (version %d.%d) does not support editing view definitions.\n",
747 " -- something...\n");
771 puts(
_(
"No changes"));
778 else if (strcmp(cmd,
"echo") == 0 || strcmp(cmd,
"qecho") == 0)
782 bool no_newline =
false;
786 if (strcmp(cmd,
"qecho") == 0)
794 if (!quoted && strcmp(value,
"-n") == 0)
811 else if (strcmp(cmd,
"encoding") == 0)
825 psql_error(
"%s: invalid encoding name or conversion procedure not found\n", encoding);
839 else if (strcmp(cmd,
"errverbose") == 0)
854 puts(
_(
"out of memory"));
857 puts(
_(
"There is no previous error."));
861 else if (strcmp(cmd,
"f") == 0)
871 else if (strcmp(cmd,
"g") == 0)
888 else if (strcmp(cmd,
"gexec") == 0)
895 else if (strcmp(cmd,
"gset") == 0)
912 else if (strcmp(cmd,
"h") == 0 || strcmp(cmd,
"help") == 0)
923 (isspace((
unsigned char) opt[len - 1])
924 || opt[len - 1] ==
';'))
933 else if (strcmp(cmd,
"H") == 0 || strcmp(cmd,
"html") == 0)
943 else if (strcmp(cmd,
"i") == 0 || strcmp(cmd,
"include") == 0
944 || strcmp(cmd,
"ir") == 0 || strcmp(cmd,
"include_relative") == 0)
951 psql_error(
"\\%s: missing required argument\n", cmd);
956 bool include_relative;
958 include_relative = (strcmp(cmd,
"ir") == 0
959 || strcmp(cmd,
"include_relative") == 0);
967 else if (strcmp(cmd,
"l") == 0 || strcmp(cmd,
"list") == 0 ||
968 strcmp(cmd,
"l+") == 0 || strcmp(cmd,
"list+") == 0)
976 show_verbose = strchr(cmd,
'+') ?
true :
false;
987 else if (strncmp(cmd,
"lo_", 3) == 0)
997 if (strcmp(cmd + 3,
"export") == 0)
1001 psql_error(
"\\%s: missing required argument\n", cmd);
1011 else if (strcmp(cmd + 3,
"import") == 0)
1015 psql_error(
"\\%s: missing required argument\n", cmd);
1025 else if (strcmp(cmd + 3,
"list") == 0)
1028 else if (strcmp(cmd + 3,
"unlink") == 0)
1032 psql_error(
"\\%s: missing required argument\n", cmd);
1048 else if (strcmp(cmd,
"o") == 0 || strcmp(cmd,
"out") == 0)
1059 else if (strcmp(cmd,
"p") == 0 || strcmp(cmd,
"print") == 0)
1061 if (query_buf && query_buf->
len > 0)
1062 puts(query_buf->
data);
1064 puts(
_(
"Query buffer is empty."));
1069 else if (strcmp(cmd,
"password") == 0)
1077 if (strcmp(pw1, pw2) != 0)
1086 char *encrypted_password;
1095 if (!encrypted_password)
1127 else if (strcmp(cmd,
"prompt") == 0)
1130 *prompt_text =
NULL;
1139 psql_error(
"\\%s: missing required argument\n", cmd);
1160 fputs(prompt_text, stdout);
1168 psql_error(
"\\%s: error while setting variable\n", cmd);
1180 else if (strcmp(cmd,
"pset") == 0)
1192 static const char *
const my_list[] = {
1193 "border",
"columns",
"expanded",
"fieldsep",
"fieldsep_zero",
1194 "footer",
"format",
"linestyle",
"null",
1195 "numericlocale",
"pager",
"pager_min_lines",
1196 "recordsep",
"recordsep_zero",
1197 "tableattr",
"title",
"tuples_only",
1198 "unicode_border_linestyle",
1199 "unicode_column_linestyle",
1200 "unicode_header_linestyle",
1204 for (i = 0; my_list[
i] !=
NULL; i++)
1208 printf(
"%-24s %s\n", my_list[i], val);
1222 else if (strcmp(cmd,
"q") == 0 || strcmp(cmd,
"quit") == 0)
1226 else if (strcmp(cmd,
"r") == 0 || strcmp(cmd,
"reset") == 0)
1231 puts(
_(
"Query buffer reset (cleared)."));
1235 else if (strcmp(cmd,
"s") == 0)
1243 printf(
_(
"Wrote history to file \"%s\".\n"), fname);
1250 else if (strcmp(cmd,
"set") == 0)
1277 newval =
pg_realloc(newval, strlen(newval) + strlen(opt) + 1);
1278 strcat(newval, opt);
1284 psql_error(
"\\%s: error while setting variable\n", cmd);
1294 else if (strcmp(cmd,
"setenv") == 0)
1303 psql_error(
"\\%s: missing required argument\n", cmd);
1306 else if (strchr(envvar,
'=') !=
NULL)
1308 psql_error(
"\\%s: environment variable name must not contain \"=\"\n",
1323 newval =
psprintf(
"%s=%s", envvar, envval);
1338 else if (strcmp(cmd,
"sf") == 0 || strcmp(cmd,
"sf+") == 0)
1340 bool show_linenumbers = (strcmp(cmd,
"sf+") == 0);
1350 psql_error(
"The server (version %d.%d) does not support showing function source.\n",
1390 if (show_linenumbers)
1404 fputs(func_buf->
data, output);
1417 else if (strcmp(cmd,
"sv") == 0 || strcmp(cmd,
"sv+") == 0)
1419 bool show_linenumbers = (strcmp(cmd,
"sv+") == 0);
1429 psql_error(
"The server (version %d.%d) does not support showing view definitions.\n",
1469 if (show_linenumbers)
1477 fputs(view_buf->
data, output);
1490 else if (strcmp(cmd,
"t") == 0)
1500 else if (strcmp(cmd,
"T") == 0)
1510 else if (strcmp(cmd,
"timing") == 0)
1522 puts(
_(
"Timing is on."));
1524 puts(
_(
"Timing is off."));
1530 else if (strcmp(cmd,
"unset") == 0)
1537 psql_error(
"\\%s: missing required argument\n", cmd);
1542 psql_error(
"\\%s: error while setting variable\n", cmd);
1549 else if (strcmp(cmd,
"w") == 0 || strcmp(cmd,
"write") == 0)
1552 bool is_pipe =
false;
1568 psql_error(
"\\%s: missing required argument\n", cmd);
1573 if (fname[0] ==
'|')
1577 fd = popen(&fname[1],
"w");
1582 fd = fopen(fname,
"w");
1596 if (query_buf && query_buf->
len > 0)
1597 fprintf(fd,
"%s\n", query_buf->
data);
1600 result = pclose(fd);
1602 result = fclose(fd);
1618 else if (strcmp(cmd,
"watch") == 0)
1627 sleep = strtod(opt,
NULL);
1633 success =
do_watch(query_buf, sleep);
1641 else if (strcmp(cmd,
"x") == 0)
1651 else if (strcmp(cmd,
"z") == 0)
1662 else if (strcmp(cmd,
"!") == 0)
1672 else if (strcmp(cmd,
"?") == 0)
1677 if (!opt0 || strcmp(opt0,
"commands") == 0)
1679 else if (strcmp(opt0,
"options") == 0)
1681 else if (strcmp(opt0,
"variables") == 0)
1692 else if (strcmp(cmd,
"void") == 0 || strcmp(cmd,
"#") == 0)
1700 psql_error(
"+ opt(%d) = |%s|\n", i++, value);
1725 if (username ==
NULL)
1731 prompt_text =
psprintf(
_(
"Password for user %s: "), username);
1742 if (new_val ==
NULL)
1745 if (old_val ==
NULL || strcmp(old_val, new_val) != 0)
1767 char *password =
NULL;
1769 bool has_connection_string;
1771 if (!o_conn && (!dbname || !user || !host || !port))
1778 psql_error(
"All connection parameters must be supplied because no "
1779 "database connection exists\n");
1791 has_connection_string =
1799 if (has_connection_string)
1800 keep_password =
false;
1803 (user &&
PQuser(o_conn) && strcmp(user,
PQuser(o_conn)) == 0) &&
1804 (host &&
PQhost(o_conn) && strcmp(host,
PQhost(o_conn)) == 0) &&
1805 (port &&
PQport(o_conn) && strcmp(port,
PQport(o_conn)) == 0);
1812 dbname =
PQdb(o_conn);
1828 else if (o_conn && keep_password)
1830 password =
PQpass(o_conn);
1831 if (password && *password)
1839 #define PARAMS_ARRAY_SIZE 8
1844 keywords[0] =
"dbname";
1847 if (!has_connection_string)
1849 keywords[++paramnum] =
"host";
1850 values[paramnum] = host;
1851 keywords[++paramnum] =
"port";
1852 values[paramnum] =
port;
1853 keywords[++paramnum] =
"user";
1854 values[paramnum] =
user;
1856 keywords[++paramnum] =
"password";
1857 values[paramnum] = password;
1858 keywords[++paramnum] =
"fallback_application_name";
1860 keywords[++paramnum] =
"client_encoding";
1861 values[paramnum] = (
pset.
notty || getenv(
"PGCLIENTENCODING")) ?
NULL :
"auto";
1864 keywords[++paramnum] =
NULL;
1865 values[paramnum] =
NULL;
1937 printf(
_(
"You are now connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\".\n"),
1940 printf(
_(
"You are now connected to database \"%s\" as user \"%s\" on host \"%s\" at port \"%s\".\n"),
1944 printf(
_(
"You are now connected to database \"%s\" as user \"%s\".\n"),
1959 int client_ver = PG_VERSION_NUM;
1964 char server_ver_str[16];
1968 if (!server_version)
1970 snprintf(server_ver_str,
sizeof(server_ver_str),
1975 server_version = server_ver_str;
1978 printf(
_(
"%s (%s, server %s)\n"),
1982 else if (in_startup)
1986 printf(
_(
"WARNING: %s major version %d.%d, server major version %d.%d.\n"
1987 " Some psql features might not work.\n"),
1988 pset.
progname, client_ver / 10000, (client_ver / 100) % 100,
1992 checkWin32Codepage();
2007 const char *protocol;
2010 const char *compression;
2020 printf(
_(
"SSL connection (protocol: %s, cipher: %s, bits: %s, compression: %s)\n"),
2021 protocol ? protocol :
_(
"unknown"),
2022 cipher ? cipher :
_(
"unknown"),
2023 bits ? bits :
_(
"unknown"),
2024 (compression && strcmp(compression,
"off") != 0) ?
_(
"on") :
_(
"off"));
2035 checkWin32Codepage(
void)
2041 concp = GetConsoleCP();
2044 printf(
_(
"WARNING: Console code page (%u) differs from Windows code page (%u)\n"
2045 " 8-bit characters might not work correctly. See psql reference\n"
2046 " page \"Notes for Windows users\" for details.\n"),
2103 const char *editorName;
2104 const char *editor_lineno_arg =
NULL;
2111 editorName = getenv(
"PSQL_EDITOR");
2113 editorName = getenv(
"EDITOR");
2115 editorName = getenv(
"VISUAL");
2122 editor_lineno_arg = getenv(
"PSQL_EDITOR_LINENUMBER_ARG");
2123 #ifdef DEFAULT_EDITOR_LINENUMBER_ARG
2124 if (!editor_lineno_arg)
2127 if (!editor_lineno_arg)
2129 psql_error(
"environment variable PSQL_EDITOR_LINENUMBER_ARG must be set to specify a line number\n");
2143 sys =
psprintf(
"exec %s %s%d '%s'",
2144 editorName, editor_lineno_arg, lineno, fname);
2150 sys =
psprintf(
"\"%s\" %s%d \"%s\"",
2151 editorName, editor_lineno_arg, lineno, fname);
2158 psql_error(
"could not start editor \"%s\"\n", editorName);
2159 else if (result == 127)
2170 int lineno,
bool *edited)
2173 FILE *stream =
NULL;
2182 fname = filename_arg;
2187 const char *tmpdir = getenv(
"TMPDIR");
2198 psql_error(
"could not locate temporary directory: %s\n",
2210 snprintf(fnametmp,
sizeof(fnametmp),
"%s%spsql.edit.%d.sql", tmpdir,
2211 "/", (
int) getpid());
2213 snprintf(fnametmp,
sizeof(fnametmp),
"%s%spsql.edit.%d.sql", tmpdir,
2214 "" , (
int) getpid());
2217 fname = (
const char *) fnametmp;
2219 fd = open(fname, O_WRONLY | O_CREAT | O_EXCL, 0600);
2221 stream = fdopen(fd,
"w");
2223 if (fd == -1 || !stream)
2230 unsigned int ql = query_buf->
len;
2232 if (ql == 0 || query_buf->
data[ql - 1] !=
'\n')
2238 if (fwrite(query_buf->
data, 1, ql, stream) != ql)
2242 if (fclose(stream) != 0)
2245 if (
remove(fname) != 0)
2250 else if (fclose(stream) != 0)
2253 if (
remove(fname) != 0)
2260 if (!error && stat(fname, &before) != 0)
2270 if (!error && stat(fname, &after) != 0)
2276 if (!error && before.st_mtime != after.st_mtime)
2290 while (fgets(line,
sizeof(line), stream) !=
NULL)
2310 if (
remove(fname) == -1)
2345 else if (strcmp(filename,
"-") != 0)
2377 filename =
"<stdin>";
2421 return "latex-longtable";
2474 vallen = strlen(value);
2477 if (strcmp(param,
"format") == 0)
2499 psql_error(
"\\pset: allowed formats are unaligned, aligned, wrapped, html, asciidoc, latex, latex-longtable, troff-ms\n");
2506 else if (strcmp(param,
"linestyle") == 0)
2518 psql_error(
"\\pset: allowed line styles are ascii, old-ascii, unicode\n");
2525 else if (strcmp(param,
"unicode_border_linestyle") == 0)
2534 psql_error(
"\\pset: allowed Unicode border line styles are single, double\n");
2540 else if (strcmp(param,
"unicode_column_linestyle") == 0)
2549 psql_error(
"\\pset: allowed Unicode column line styles are single, double\n");
2555 else if (strcmp(param,
"unicode_header_linestyle") == 0)
2564 psql_error(
"\\pset: allowed Unicode header line styles are single, double\n");
2570 else if (strcmp(param,
"border") == 0)
2578 else if (strcmp(param,
"x") == 0 ||
2579 strcmp(param,
"expanded") == 0 ||
2580 strcmp(param,
"vertical") == 0)
2591 else if (strcmp(param,
"numericlocale") == 0)
2600 else if (strcmp(param,
"null") == 0)
2610 else if (strcmp(param,
"fieldsep") == 0)
2620 else if (strcmp(param,
"fieldsep_zero") == 0)
2628 else if (strcmp(param,
"recordsep") == 0)
2638 else if (strcmp(param,
"recordsep_zero") == 0)
2646 else if (strcmp(param,
"t") == 0 || strcmp(param,
"tuples_only") == 0)
2655 else if (strcmp(param,
"C") == 0 || strcmp(param,
"title") == 0)
2665 else if (strcmp(param,
"T") == 0 || strcmp(param,
"tableattr") == 0)
2675 else if (strcmp(param,
"pager") == 0)
2693 else if (strcmp(param,
"pager_min_lines") == 0)
2700 else if (strcmp(param,
"footer") == 0)
2709 else if (strcmp(param,
"columns") == 0)
2716 psql_error(
"\\pset: unknown option: %s\n", param);
2733 if (strcmp(param,
"border") == 0)
2734 printf(
_(
"Border style is %d.\n"), popt->
topt.
border);
2737 else if (strcmp(param,
"columns") == 0)
2740 printf(
_(
"Target width is unset.\n"));
2742 printf(
_(
"Target width is %d.\n"), popt->
topt.
columns);
2746 else if (strcmp(param,
"x") == 0 || strcmp(param,
"expanded") == 0 || strcmp(param,
"vertical") == 0)
2749 printf(
_(
"Expanded display is on.\n"));
2751 printf(
_(
"Expanded display is used automatically.\n"));
2753 printf(
_(
"Expanded display is off.\n"));
2757 else if (strcmp(param,
"fieldsep") == 0)
2760 printf(
_(
"Field separator is zero byte.\n"));
2762 printf(
_(
"Field separator is \"%s\".\n"),
2766 else if (strcmp(param,
"fieldsep_zero") == 0)
2768 printf(
_(
"Field separator is zero byte.\n"));
2772 else if (strcmp(param,
"footer") == 0)
2775 printf(
_(
"Default footer is on.\n"));
2777 printf(
_(
"Default footer is off.\n"));
2781 else if (strcmp(param,
"format") == 0)
2787 else if (strcmp(param,
"linestyle") == 0)
2789 printf(
_(
"Line style is %s.\n"),
2794 else if (strcmp(param,
"null") == 0)
2796 printf(
_(
"Null display is \"%s\".\n"),
2801 else if (strcmp(param,
"numericlocale") == 0)
2804 printf(
_(
"Locale-adjusted numeric output is on.\n"));
2806 printf(
_(
"Locale-adjusted numeric output is off.\n"));
2810 else if (strcmp(param,
"pager") == 0)
2813 printf(
_(
"Pager is used for long output.\n"));
2815 printf(
_(
"Pager is always used.\n"));
2817 printf(
_(
"Pager usage is off.\n"));
2821 else if (strcmp(param,
"pager_min_lines") == 0)
2823 printf(
ngettext(
"Pager won't be used for less than %d line.\n",
2824 "Pager won't be used for less than %d lines.\n",
2830 else if (strcmp(param,
"recordsep") == 0)
2833 printf(
_(
"Record separator is zero byte.\n"));
2835 printf(
_(
"Record separator is <newline>.\n"));
2837 printf(
_(
"Record separator is \"%s\".\n"),
2841 else if (strcmp(param,
"recordsep_zero") == 0)
2843 printf(
_(
"Record separator is zero byte.\n"));
2847 else if (strcmp(param,
"T") == 0 || strcmp(param,
"tableattr") == 0)
2850 printf(
_(
"Table attributes are \"%s\".\n"),
2853 printf(
_(
"Table attributes unset.\n"));
2857 else if (strcmp(param,
"C") == 0 || strcmp(param,
"title") == 0)
2860 printf(
_(
"Title is \"%s\".\n"), popt->
title);
2862 printf(
_(
"Title is unset.\n"));
2866 else if (strcmp(param,
"t") == 0 || strcmp(param,
"tuples_only") == 0)
2869 printf(
_(
"Tuples only is on.\n"));
2871 printf(
_(
"Tuples only is off.\n"));
2875 else if (strcmp(param,
"unicode_border_linestyle") == 0)
2877 printf(
_(
"Unicode border line style is \"%s\".\n"),
2881 else if (strcmp(param,
"unicode_column_linestyle") == 0)
2883 printf(
_(
"Unicode column line style is \"%s\".\n"),
2887 else if (strcmp(param,
"unicode_header_linestyle") == 0)
2889 printf(
_(
"Unicode header line style is \"%s\".\n"),
2895 psql_error(
"\\pset: unknown option: %s\n", param);
2906 return val ?
"on" :
"off";
2913 char *ret =
pg_malloc(strlen(str) * 2 + 3);
2925 else if (*str ==
'\'')
2953 if (strcmp(param,
"border") == 0)
2955 else if (strcmp(param,
"columns") == 0)
2957 else if (strcmp(param,
"expanded") == 0)
2961 else if (strcmp(param,
"fieldsep") == 0)
2965 else if (strcmp(param,
"fieldsep_zero") == 0)
2967 else if (strcmp(param,
"footer") == 0)
2969 else if (strcmp(param,
"format") == 0)
2971 else if (strcmp(param,
"linestyle") == 0)
2973 else if (strcmp(param,
"null") == 0)
2977 else if (strcmp(param,
"numericlocale") == 0)
2979 else if (strcmp(param,
"pager") == 0)
2981 else if (strcmp(param,
"pager_min_lines") == 0)
2983 else if (strcmp(param,
"recordsep") == 0)
2987 else if (strcmp(param,
"recordsep_zero") == 0)
2989 else if (strcmp(param,
"tableattr") == 0)
2991 else if (strcmp(param,
"title") == 0)
2993 else if (strcmp(param,
"tuples_only") == 0)
2995 else if (strcmp(param,
"unicode_border_linestyle") == 0)
2997 else if (strcmp(param,
"unicode_column_linestyle") == 0)
2999 else if (strcmp(param,
"unicode_header_linestyle") == 0)
3008 #define DEFAULT_SHELL "/bin/sh"
3014 #define DEFAULT_SHELL "cmd.exe"
3025 const char *shellName;
3027 shellName = getenv(
"SHELL");
3029 if (shellName ==
NULL)
3030 shellName = getenv(
"COMSPEC");
3032 if (shellName ==
NULL)
3037 sys =
psprintf(
"exec %s", shellName);
3039 sys =
psprintf(
"\"%s\"", shellName);
3045 result =
system(command);
3047 if (result == 127 || result == -1)
3064 long sleep_ms = (long) (sleep * 1000);
3066 const char *user_title;
3071 if (!query_buf || query_buf->
len <= 0)
3073 psql_error(
_(
"\\watch cannot be used with an empty query\n"));
3087 user_title = myopt.
title;
3088 title_len = (user_title ? strlen(user_title) : 0) + 100;
3094 char asctimebuf[64];
3103 strlcpy(asctimebuf, asctime(localtime(&timer)),
sizeof(asctimebuf));
3105 i = strlen(asctimebuf);
3106 while (i > 0 && asctimebuf[--i] ==
'\n')
3107 asctimebuf[
i] =
'\0';
3110 snprintf(title, title_len,
_(
"%s\t%s (every %gs)\n"),
3111 user_title, asctimebuf, sleep);
3113 snprintf(title, title_len,
_(
"%s (every %gs)\n"),
3115 myopt.
title = title;
3144 long s =
Min(i, 1000L);
3167 printf(
_(
"********* QUERY **********\n"
3169 "**************************\n\n"), query);
3174 _(
"********* QUERY **********\n"
3176 "**************************\n\n"), query);
3214 strchr(desc,
'(') ?
"regprocedure" :
"regproc");
3266 "SELECT pg_catalog.pg_get_functiondef(%u)",
3288 "SELECT nspname, relname, relkind, "
3289 "pg_catalog.pg_get_viewdef(c.oid, true), "
3290 "array_remove(array_remove(c.reloptions,'check_option=local'),'check_option=cascaded') AS reloptions, "
3291 "CASE WHEN 'check_option=local' = ANY (c.reloptions) THEN 'LOCAL'::text "
3292 "WHEN 'check_option=cascaded' = ANY (c.reloptions) THEN 'CASCADED'::text ELSE NULL END AS checkoption "
3293 "FROM pg_catalog.pg_class c "
3294 "LEFT JOIN pg_catalog.pg_namespace n "
3295 "ON c.relnamespace = n.oid WHERE c.oid = %u",
3301 "SELECT nspname, relname, relkind, "
3302 "pg_catalog.pg_get_viewdef(c.oid, true), "
3303 "c.reloptions AS reloptions, "
3304 "NULL AS checkoption "
3305 "FROM pg_catalog.pg_class c "
3306 "LEFT JOIN pg_catalog.pg_namespace n "
3307 "ON c.relnamespace = n.oid WHERE c.oid = %u",
3313 "SELECT nspname, relname, relkind, "
3314 "pg_catalog.pg_get_viewdef(c.oid, true), "
3315 "NULL AS reloptions, "
3316 "NULL AS checkoption "
3317 "FROM pg_catalog.pg_class c "
3318 "LEFT JOIN pg_catalog.pg_namespace n "
3319 "ON c.relnamespace = n.oid WHERE c.oid = %u",
3375 if (reloptions !=
NULL && strlen(reloptions) > 2)
3382 psql_error(
"could not parse reloptions array\n");
3392 if (buf->
len > 0 && buf->
data[buf->
len - 1] ==
';')
3393 buf->
data[--(buf->
len)] =
'\0';
3396 if (checkoption && checkoption[0] !=
'\0')
3403 if (buf->
len > 0 && buf->
data[buf->
len - 1] !=
'\n')
3433 if (!obj || obj[0] ==
'\0')
3436 c = obj + strlen(obj) - 1;
3449 while (c > obj && isascii((
unsigned char) *c) && isspace((
unsigned char) *c))
3453 if (c == obj || !isascii((
unsigned char) *c) || !isdigit((
unsigned char) *c))
3457 while (c > obj && isascii((
unsigned char) *c) && isdigit((
unsigned char) *c))
3462 if (c == obj || !isascii((
unsigned char) *c) ||
3463 !(isspace((
unsigned char) *c) || *c ==
')'))
3489 const char *lines = buf->
data;
3491 while (*lines !=
'\0')
3495 lines = strchr(lines,
'\n');
3514 const char *header_keyword)
3516 bool in_header = (header_keyword !=
NULL);
3517 size_t header_sz = in_header ? strlen(header_keyword) : 0;
3520 while (*lines !=
'\0')
3524 if (in_header && strncmp(lines, header_keyword, header_sz) == 0)
3532 eol = strchr(lines,
'\n');
3538 fprintf(output,
" %s\n", lines);
3540 fprintf(output,
"%-7d %s\n", lineno, lines);
bool do_lo_export(const char *loid_arg, const char *filename_arg)
static bool param_is_newly_set(const char *old_val, const char *new_val)
char * PQerrorMessage(const PGconn *conn)
void psql_scan_slash_command_end(PsqlScanState state)
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
const char * PQsslAttribute(PGconn *conn, const char *attribute_name)
volatile bool sigint_interrupt_enabled
void print_copyright(void)
#define PG_DIAG_MESSAGE_PRIMARY
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
int system(const char *command)
const printTextFormat * line_style
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
bool listTSConfigs(const char *pattern, bool verbose)
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
bool objectDescription(const char *pattern, bool showSystem)
void * pg_malloc(size_t size)
int PSQLexecWatch(const char *query, const printQueryOpt *opt)
bool listExtensionContents(const char *pattern)
const char * fmtId(const char *rawid)
void disable_sigpipe_trap(void)
static void output(uint64 loop_count)
void termPQExpBuffer(PQExpBuffer str)
printTextFormat pg_utf8format
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
char * psql_scan_slash_command(PsqlScanState state)
void ClosePager(FILE *pagerpipe)
const printTextFormat pg_asciiformat_old
bool do_lo_unlink(const char *loid_arg)
char * pstrdup(const char *in)
char * psprintf(const char *fmt,...)
bool recognized_connection_string(const char *connstr)
static void minimal_error_message(PGresult *res)
bool listEventTriggers(const char *pattern, bool verbose)
bool listCollations(const char *pattern, bool verbose, bool showSystem)
bool listLanguages(const char *pattern, bool verbose, bool showSystem)
static bool lookup_object_oid(EditableObjectType obj_type, const char *desc, Oid *obj_oid)
static void print_with_linenumbers(FILE *output, char *lines, const char *header_keyword)
static backslashResult exec_command(const char *cmd, PsqlScanState scan_state, PQExpBuffer query_buf)
void canonicalize_path(char *path)
unicode_linestyle unicode_header_linestyle
void PQfinish(PGconn *conn)
char * PQport(const PGconn *conn)
static const char * _unicode_linestyle2string(int linestyle)
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
bool do_copy(const char *args)
int pg_strcasecmp(const char *s1, const char *s2)
int PQserverVersion(const PGconn *conn)
bool listDomains(const char *pattern, bool verbose, bool showSystem)
void PrintVariables(VariableSpace space)
int PQntuples(const PGresult *res)
bool listSchemas(const char *pattern, bool verbose, bool showSystem)
static int fd(const char *x, int i)
unicode_linestyle unicode_border_linestyle
static char * read_connect_arg(PsqlScanState scan_state)
int PQclientEncoding(const PGconn *conn)
ExecStatusType PQresultStatus(const PGresult *res)
bool describeTablespaces(const char *pattern, bool verbose)
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
void UnsyncVariables(void)
char * PQuser(const PGconn *conn)
sigjmp_buf sigint_interrupt_jmp
static int count_lines_in_buf(PQExpBuffer buf)
volatile bool cancel_pressed
static char * relname(char const *dir, char const *base)
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
void pg_usleep(long microsec)
void destroyPQExpBuffer(PQExpBuffer str)
unsigned short int border
bool has_drive_prefix(const char *filename)
bool describeAggregates(const char *pattern, bool verbose, bool showSystem)
unsigned short int expanded
PSQL_ECHO_HIDDEN echo_hidden
FILE * PageOutput(int lines, const printTableOpt *topt)
static bool printPsetInfo(const char *param, struct printQueryOpt *popt)
void expand_tilde(char **filename)
static const char * pset_bool_string(bool val)
static bool do_watch(PQExpBuffer query_buf, double sleep)
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
unicode_linestyle unicode_column_linestyle
bool listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem)
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
bool describeTableDetails(const char *pattern, bool verbose, bool showSystem)
char * PQresultVerboseErrorMessage(const PGresult *res, PGVerbosity verbosity, PGContextVisibility show_context)
bool listConversions(const char *pattern, bool verbose, bool showSystem)
void psql_scan_reset(PsqlScanState state)
void PQconninfoFree(PQconninfoOption *connOptions)
#define is_absolute_path(filename)
bool appendReloptionsArray(PQExpBuffer buffer, const char *reloptions, const char *prefix, int encoding, bool std_strings)
void get_parent_directory(char *path)
char * pg_strdup(const char *in)
char * PQpass(const PGconn *conn)
PQconninfoOption * PQconninfo(PGconn *conn)
bool do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
bool ParseVariableBool(const char *value, const char *name)
static bool editFile(const char *fname, int lineno)
static char * pset_value_string(const char *param, struct printQueryOpt *popt)
void * pg_realloc(void *ptr, size_t size)
bool do_lo_import(const char *filename_arg, const char *comment_arg)
static char * prompt_for_password(const char *username)
void restore_sigpipe_trap(void)
bool listForeignTables(const char *pattern, bool verbose)
char * simple_prompt(const char *prompt, int maxlen, bool echo)
#define PARAMS_ARRAY_SIZE
#define ngettext(s, p, n)
PGContextVisibility show_context
static bool do_connect(char *dbname, char *user, char *host, char *port)
void psql_error(const char *fmt,...)
void helpVariables(unsigned short int pager)
bool listTSDictionaries(const char *pattern, bool verbose)
bool setQFout(const char *fname)
PQExpBuffer createPQExpBuffer(void)
void appendPQExpBufferChar(PQExpBuffer str, char ch)
int PQsetClientEncoding(PGconn *conn, const char *encoding)
PGresult * last_error_result
static bool do_edit(const char *filename_arg, PQExpBuffer query_buf, int lineno, bool *edited)
PGContextVisibility PQsetErrorContextVisibility(PGconn *conn, PGContextVisibility show_context)
bool listForeignServers(const char *pattern, bool verbose)
char * PQhost(const PGconn *conn)
int process_file(char *filename, bool use_relative_path)
void PQclear(PGresult *res)
bool standard_strings(void)
bool describeTypes(const char *pattern, bool verbose, bool showSystem)
struct separator fieldSep
void NoticeProcessor(void *arg, const char *message)
enum _backslashResult backslashResult
size_t strlcpy(char *dst, const char *src, size_t siz)
const char * pg_encoding_to_char(int encoding)
char * PQdb(const PGconn *conn)
const printTextFormat * get_line_style(const printTableOpt *opt)
char * PQresultErrorField(const PGresult *res, int fieldcode)
static int strip_lineno_from_objdesc(char *obj)
bool SetVariable(VariableSpace space, const char *name, const char *value)
#define Assert(condition)
bool listForeignDataWrappers(const char *pattern, bool verbose)
#define DEFAULT_EDITOR_LINENUMBER_ARG
int MainLoop(FILE *source)
bool listUserMappings(const char *pattern, bool verbose)
static int server_version
enum trivalue getPassword
static bool set_unicode_line_style(const char *value, size_t vallen, unicode_linestyle *linestyle)
void join_path_components(char *ret_path, const char *head, const char *tail)
bool describeFunctions(const char *functypes, const char *pattern, bool verbose, bool showSystem)
static bool do_shell(const char *command)
backslashResult HandleSlashCmds(PsqlScanState scan_state, PQExpBuffer query_buf)
static const char * _align2string(enum printFormat in)
bool listExtensions(const char *pattern)
static Datum values[MAXATTR]
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
int PQconnectionNeedsPassword(const PGconn *conn)
bool describeOperators(const char *pattern, bool verbose, bool showSystem)
static bool get_create_object_cmd(EditableObjectType obj_type, Oid oid, PQExpBuffer buf)
bool listCasts(const char *pattern, bool verbose)
void helpSQL(const char *topic, unsigned short int pager)
int PQsslInUse(PGconn *conn)
const char * strerror(int errnum)
void refresh_utf8format(const printTableOpt *opt)
#define relpath(rnode, forknum)
PGresult * PQexec(PGconn *conn, const char *query)
const printTextFormat pg_asciiformat
bool listDefaultACLs(const char *pattern)
void connection_warnings(bool in_startup)
bool permissionsList(const char *pattern)
struct separator recordSep
static void printSSLInfo(void)
void resetPQExpBuffer(PQExpBuffer str)
static void static void status(const char *fmt,...) pg_attribute_printf(1
static bool echo_hidden_command(const char *query)
ConnStatusType PQstatus(const PGconn *conn)
bool listDbRoleSettings(const char *pattern, const char *pattern2)
bool listTSTemplates(const char *pattern, bool verbose)
bool describeRoles(const char *pattern, bool verbose, bool showSystem)
PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity)
bool listTSParsers(const char *pattern, bool verbose)
void PQfreemem(void *ptr)
void slashUsage(unsigned short int pager)
bool listAllDbs(const char *pattern, bool verbose)
void initPQExpBuffer(PQExpBuffer str)
char * PQencryptPassword(const char *passwd, const char *user)
static char * pset_quoted_string(const char *str)
PGresult * PSQLexec(const char *query)