Line data Source code
1 : /*
2 : * psql - the PostgreSQL interactive terminal
3 : *
4 : * Copyright (c) 2000-2025, PostgreSQL Global Development Group
5 : *
6 : * src/bin/psql/command.c
7 : */
8 : #include "postgres_fe.h"
9 :
10 : #include <ctype.h>
11 : #include <time.h>
12 : #include <pwd.h>
13 : #include <utime.h>
14 : #ifndef WIN32
15 : #include <sys/stat.h> /* for stat() */
16 : #include <sys/time.h> /* for setitimer() */
17 : #include <fcntl.h> /* open() flags */
18 : #include <unistd.h> /* for geteuid(), getpid(), stat() */
19 : #else
20 : #include <win32.h>
21 : #include <io.h>
22 : #include <fcntl.h>
23 : #include <direct.h>
24 : #include <sys/stat.h> /* for stat() */
25 : #endif
26 :
27 : #include "catalog/pg_class_d.h"
28 : #include "command.h"
29 : #include "common.h"
30 : #include "common/logging.h"
31 : #include "common/string.h"
32 : #include "copy.h"
33 : #include "describe.h"
34 : #include "fe_utils/cancel.h"
35 : #include "fe_utils/print.h"
36 : #include "fe_utils/string_utils.h"
37 : #include "help.h"
38 : #include "input.h"
39 : #include "large_obj.h"
40 : #include "libpq/pqcomm.h"
41 : #include "mainloop.h"
42 : #include "pqexpbuffer.h"
43 : #include "psqlscanslash.h"
44 : #include "settings.h"
45 : #include "variables.h"
46 :
47 : /*
48 : * Editable database object types.
49 : */
50 : typedef enum EditableObjectType
51 : {
52 : EditableFunction,
53 : EditableView,
54 : } EditableObjectType;
55 :
56 : /* local function declarations */
57 : static backslashResult exec_command(const char *cmd,
58 : PsqlScanState scan_state,
59 : ConditionalStack cstack,
60 : PQExpBuffer query_buf,
61 : PQExpBuffer previous_buf);
62 : static backslashResult exec_command_a(PsqlScanState scan_state, bool active_branch);
63 : static backslashResult exec_command_bind(PsqlScanState scan_state, bool active_branch);
64 : static backslashResult exec_command_bind_named(PsqlScanState scan_state, bool active_branch,
65 : const char *cmd);
66 : static backslashResult exec_command_C(PsqlScanState scan_state, bool active_branch);
67 : static backslashResult exec_command_connect(PsqlScanState scan_state, bool active_branch);
68 : static backslashResult exec_command_cd(PsqlScanState scan_state, bool active_branch,
69 : const char *cmd);
70 : static backslashResult exec_command_close(PsqlScanState scan_state, bool active_branch,
71 : const char *cmd);
72 : static backslashResult exec_command_conninfo(PsqlScanState scan_state, bool active_branch);
73 : static backslashResult exec_command_copy(PsqlScanState scan_state, bool active_branch);
74 : static backslashResult exec_command_copyright(PsqlScanState scan_state, bool active_branch);
75 : static backslashResult exec_command_crosstabview(PsqlScanState scan_state, bool active_branch);
76 : static backslashResult exec_command_d(PsqlScanState scan_state, bool active_branch,
77 : const char *cmd);
78 : static bool exec_command_dfo(PsqlScanState scan_state, const char *cmd,
79 : const char *pattern,
80 : bool show_verbose, bool show_system);
81 : static backslashResult exec_command_edit(PsqlScanState scan_state, bool active_branch,
82 : PQExpBuffer query_buf, PQExpBuffer previous_buf);
83 : static backslashResult exec_command_ef_ev(PsqlScanState scan_state, bool active_branch,
84 : PQExpBuffer query_buf, bool is_func);
85 : static backslashResult exec_command_echo(PsqlScanState scan_state, bool active_branch,
86 : const char *cmd);
87 : static backslashResult exec_command_elif(PsqlScanState scan_state, ConditionalStack cstack,
88 : PQExpBuffer query_buf);
89 : static backslashResult exec_command_else(PsqlScanState scan_state, ConditionalStack cstack,
90 : PQExpBuffer query_buf);
91 : static backslashResult exec_command_endif(PsqlScanState scan_state, ConditionalStack cstack,
92 : PQExpBuffer query_buf);
93 : static backslashResult exec_command_endpipeline(PsqlScanState scan_state, bool active_branch);
94 : static backslashResult exec_command_encoding(PsqlScanState scan_state, bool active_branch);
95 : static backslashResult exec_command_errverbose(PsqlScanState scan_state, bool active_branch);
96 : static backslashResult exec_command_f(PsqlScanState scan_state, bool active_branch);
97 : static backslashResult exec_command_flush(PsqlScanState scan_state, bool active_branch);
98 : static backslashResult exec_command_flushrequest(PsqlScanState scan_state, bool active_branch);
99 : static backslashResult exec_command_g(PsqlScanState scan_state, bool active_branch,
100 : const char *cmd);
101 : static backslashResult process_command_g_options(char *first_option,
102 : PsqlScanState scan_state,
103 : bool active_branch,
104 : const char *cmd);
105 : static backslashResult exec_command_gdesc(PsqlScanState scan_state, bool active_branch);
106 : static backslashResult exec_command_getenv(PsqlScanState scan_state, bool active_branch,
107 : const char *cmd);
108 : static backslashResult exec_command_gexec(PsqlScanState scan_state, bool active_branch);
109 : static backslashResult exec_command_getresults(PsqlScanState scan_state, bool active_branch);
110 : static backslashResult exec_command_gset(PsqlScanState scan_state, bool active_branch);
111 : static backslashResult exec_command_help(PsqlScanState scan_state, bool active_branch);
112 : static backslashResult exec_command_html(PsqlScanState scan_state, bool active_branch);
113 : static backslashResult exec_command_include(PsqlScanState scan_state, bool active_branch,
114 : const char *cmd);
115 : static backslashResult exec_command_if(PsqlScanState scan_state, ConditionalStack cstack,
116 : PQExpBuffer query_buf);
117 : static backslashResult exec_command_list(PsqlScanState scan_state, bool active_branch,
118 : const char *cmd);
119 : static backslashResult exec_command_lo(PsqlScanState scan_state, bool active_branch,
120 : const char *cmd);
121 : static backslashResult exec_command_out(PsqlScanState scan_state, bool active_branch);
122 : static backslashResult exec_command_print(PsqlScanState scan_state, bool active_branch,
123 : PQExpBuffer query_buf, PQExpBuffer previous_buf);
124 : static backslashResult exec_command_parse(PsqlScanState scan_state, bool active_branch,
125 : const char *cmd);
126 : static backslashResult exec_command_password(PsqlScanState scan_state, bool active_branch);
127 : static backslashResult exec_command_prompt(PsqlScanState scan_state, bool active_branch,
128 : const char *cmd);
129 : static backslashResult exec_command_pset(PsqlScanState scan_state, bool active_branch);
130 : static backslashResult exec_command_quit(PsqlScanState scan_state, bool active_branch);
131 : static backslashResult exec_command_reset(PsqlScanState scan_state, bool active_branch,
132 : PQExpBuffer query_buf);
133 : static backslashResult exec_command_s(PsqlScanState scan_state, bool active_branch);
134 : static backslashResult exec_command_sendpipeline(PsqlScanState scan_state, bool active_branch);
135 : static backslashResult exec_command_set(PsqlScanState scan_state, bool active_branch);
136 : static backslashResult exec_command_setenv(PsqlScanState scan_state, bool active_branch,
137 : const char *cmd);
138 : static backslashResult exec_command_sf_sv(PsqlScanState scan_state, bool active_branch,
139 : const char *cmd, bool is_func);
140 : static backslashResult exec_command_startpipeline(PsqlScanState scan_state, bool active_branch);
141 : static backslashResult exec_command_syncpipeline(PsqlScanState scan_state, bool active_branch);
142 : static backslashResult exec_command_t(PsqlScanState scan_state, bool active_branch);
143 : static backslashResult exec_command_T(PsqlScanState scan_state, bool active_branch);
144 : static backslashResult exec_command_timing(PsqlScanState scan_state, bool active_branch);
145 : static backslashResult exec_command_unset(PsqlScanState scan_state, bool active_branch,
146 : const char *cmd);
147 : static backslashResult exec_command_write(PsqlScanState scan_state, bool active_branch,
148 : const char *cmd,
149 : PQExpBuffer query_buf, PQExpBuffer previous_buf);
150 : static backslashResult exec_command_watch(PsqlScanState scan_state, bool active_branch,
151 : PQExpBuffer query_buf, PQExpBuffer previous_buf);
152 : static backslashResult exec_command_x(PsqlScanState scan_state, bool active_branch);
153 : static backslashResult exec_command_z(PsqlScanState scan_state, bool active_branch,
154 : const char *cmd);
155 : static backslashResult exec_command_shell_escape(PsqlScanState scan_state, bool active_branch);
156 : static backslashResult exec_command_slash_command_help(PsqlScanState scan_state, bool active_branch);
157 : static char *read_connect_arg(PsqlScanState scan_state);
158 : static PQExpBuffer gather_boolean_expression(PsqlScanState scan_state);
159 : static bool is_true_boolean_expression(PsqlScanState scan_state, const char *name);
160 : static void ignore_boolean_expression(PsqlScanState scan_state);
161 : static void ignore_slash_options(PsqlScanState scan_state);
162 : static void ignore_slash_filepipe(PsqlScanState scan_state);
163 : static void ignore_slash_whole_line(PsqlScanState scan_state);
164 : static bool is_branching_command(const char *cmd);
165 : static void save_query_text_state(PsqlScanState scan_state, ConditionalStack cstack,
166 : PQExpBuffer query_buf);
167 : static void discard_query_text(PsqlScanState scan_state, ConditionalStack cstack,
168 : PQExpBuffer query_buf);
169 : static bool copy_previous_query(PQExpBuffer query_buf, PQExpBuffer previous_buf);
170 : static bool do_connect(enum trivalue reuse_previous_specification,
171 : char *dbname, char *user, char *host, char *port);
172 : static void wait_until_connected(PGconn *conn);
173 : static bool do_edit(const char *filename_arg, PQExpBuffer query_buf,
174 : int lineno, bool discard_on_quit, bool *edited);
175 : static bool do_shell(const char *command);
176 : static bool do_watch(PQExpBuffer query_buf, double sleep, int iter, int min_rows);
177 : static bool lookup_object_oid(EditableObjectType obj_type, const char *desc,
178 : Oid *obj_oid);
179 : static bool get_create_object_cmd(EditableObjectType obj_type, Oid oid,
180 : PQExpBuffer buf);
181 : static int strip_lineno_from_objdesc(char *obj);
182 : static int count_lines_in_buf(PQExpBuffer buf);
183 : static void print_with_linenumbers(FILE *output, char *lines, bool is_func);
184 : static void minimal_error_message(PGresult *res);
185 :
186 : static void printSSLInfo(void);
187 : static void printGSSInfo(void);
188 : static bool printPsetInfo(const char *param, printQueryOpt *popt);
189 : static char *pset_value_string(const char *param, printQueryOpt *popt);
190 :
191 : #ifdef WIN32
192 : static void checkWin32Codepage(void);
193 : #endif
194 :
195 :
196 :
197 : /*----------
198 : * HandleSlashCmds:
199 : *
200 : * Handles all the different commands that start with '\'.
201 : * Ordinarily called by MainLoop().
202 : *
203 : * scan_state is a lexer working state that is set to continue scanning
204 : * just after the '\'. The lexer is advanced past the command and all
205 : * arguments on return.
206 : *
207 : * cstack is the current \if stack state. This will be examined, and
208 : * possibly modified by conditional commands.
209 : *
210 : * query_buf contains the query-so-far, which may be modified by
211 : * execution of the backslash command (for example, \r clears it).
212 : *
213 : * previous_buf contains the query most recently sent to the server
214 : * (empty if none yet). This should not be modified here, but some
215 : * commands copy its content into query_buf.
216 : *
217 : * query_buf and previous_buf will be NULL when executing a "-c"
218 : * command-line option.
219 : *
220 : * Returns a status code indicating what action is desired, see command.h.
221 : *----------
222 : */
223 :
224 : backslashResult
225 17304 : HandleSlashCmds(PsqlScanState scan_state,
226 : ConditionalStack cstack,
227 : PQExpBuffer query_buf,
228 : PQExpBuffer previous_buf)
229 : {
230 : backslashResult status;
231 : char *cmd;
232 : char *arg;
233 :
234 : Assert(scan_state != NULL);
235 : Assert(cstack != NULL);
236 :
237 : /* Parse off the command name */
238 17304 : cmd = psql_scan_slash_command(scan_state);
239 :
240 : /* And try to execute it */
241 17304 : status = exec_command(cmd, scan_state, cstack, query_buf, previous_buf);
242 :
243 17302 : if (status == PSQL_CMD_UNKNOWN)
244 : {
245 18 : pg_log_error("invalid command \\%s", cmd);
246 18 : if (pset.cur_cmd_interactive)
247 0 : pg_log_error_hint("Try \\? for help.");
248 18 : status = PSQL_CMD_ERROR;
249 : }
250 :
251 17302 : if (status != PSQL_CMD_ERROR)
252 : {
253 : /*
254 : * Eat any remaining arguments after a valid command. We want to
255 : * suppress evaluation of backticks in this situation, so transiently
256 : * push an inactive conditional-stack entry.
257 : */
258 16132 : bool active_branch = conditional_active(cstack);
259 :
260 16132 : conditional_stack_push(cstack, IFSTATE_IGNORED);
261 16162 : while ((arg = psql_scan_slash_option(scan_state,
262 : OT_NORMAL, NULL, false)))
263 : {
264 30 : if (active_branch)
265 30 : pg_log_warning("\\%s: extra argument \"%s\" ignored", cmd, arg);
266 30 : free(arg);
267 : }
268 16132 : conditional_stack_pop(cstack);
269 : }
270 : else
271 : {
272 : /* silently throw away rest of line after an erroneous command */
273 1188 : while ((arg = psql_scan_slash_option(scan_state,
274 : OT_WHOLE_LINE, NULL, false)))
275 18 : free(arg);
276 : }
277 :
278 : /* if there is a trailing \\, swallow it */
279 17302 : psql_scan_slash_command_end(scan_state);
280 :
281 17302 : free(cmd);
282 :
283 : /* some commands write to queryFout, so make sure output is sent */
284 17302 : fflush(pset.queryFout);
285 :
286 17302 : return status;
287 : }
288 :
289 :
290 : /*
291 : * Subroutine to actually try to execute a backslash command.
292 : *
293 : * The typical "success" result code is PSQL_CMD_SKIP_LINE, although some
294 : * commands return something else. Failure result code is PSQL_CMD_ERROR,
295 : * unless PSQL_CMD_UNKNOWN is more appropriate.
296 : */
297 : static backslashResult
298 17304 : exec_command(const char *cmd,
299 : PsqlScanState scan_state,
300 : ConditionalStack cstack,
301 : PQExpBuffer query_buf,
302 : PQExpBuffer previous_buf)
303 : {
304 : backslashResult status;
305 17304 : bool active_branch = conditional_active(cstack);
306 :
307 : /*
308 : * In interactive mode, warn when we're ignoring a command within a false
309 : * \if-branch. But we continue on, so as to parse and discard the right
310 : * amount of parameter text. Each individual backslash command subroutine
311 : * is responsible for doing nothing after discarding appropriate
312 : * arguments, if !active_branch.
313 : */
314 17304 : if (pset.cur_cmd_interactive && !active_branch &&
315 0 : !is_branching_command(cmd))
316 : {
317 0 : pg_log_warning("\\%s command ignored; use \\endif or Ctrl-C to exit current \\if block",
318 : cmd);
319 : }
320 :
321 17304 : if (strcmp(cmd, "a") == 0)
322 54 : status = exec_command_a(scan_state, active_branch);
323 17250 : else if (strcmp(cmd, "bind") == 0)
324 706 : status = exec_command_bind(scan_state, active_branch);
325 16544 : else if (strcmp(cmd, "bind_named") == 0)
326 168 : status = exec_command_bind_named(scan_state, active_branch, cmd);
327 16376 : else if (strcmp(cmd, "C") == 0)
328 6 : status = exec_command_C(scan_state, active_branch);
329 16370 : else if (strcmp(cmd, "c") == 0 || strcmp(cmd, "connect") == 0)
330 336 : status = exec_command_connect(scan_state, active_branch);
331 16034 : else if (strcmp(cmd, "cd") == 0)
332 6 : status = exec_command_cd(scan_state, active_branch, cmd);
333 16028 : else if (strcmp(cmd, "close") == 0)
334 48 : status = exec_command_close(scan_state, active_branch, cmd);
335 15980 : else if (strcmp(cmd, "conninfo") == 0)
336 6 : status = exec_command_conninfo(scan_state, active_branch);
337 15974 : else if (pg_strcasecmp(cmd, "copy") == 0)
338 168 : status = exec_command_copy(scan_state, active_branch);
339 15806 : else if (strcmp(cmd, "copyright") == 0)
340 8 : status = exec_command_copyright(scan_state, active_branch);
341 15798 : else if (strcmp(cmd, "crosstabview") == 0)
342 138 : status = exec_command_crosstabview(scan_state, active_branch);
343 15660 : else if (cmd[0] == 'd')
344 6740 : status = exec_command_d(scan_state, active_branch, cmd);
345 8920 : else if (strcmp(cmd, "e") == 0 || strcmp(cmd, "edit") == 0)
346 6 : status = exec_command_edit(scan_state, active_branch,
347 : query_buf, previous_buf);
348 8914 : else if (strcmp(cmd, "ef") == 0)
349 6 : status = exec_command_ef_ev(scan_state, active_branch, query_buf, true);
350 8908 : else if (strcmp(cmd, "ev") == 0)
351 6 : status = exec_command_ef_ev(scan_state, active_branch, query_buf, false);
352 8902 : else if (strcmp(cmd, "echo") == 0 || strcmp(cmd, "qecho") == 0 ||
353 7932 : strcmp(cmd, "warn") == 0)
354 1162 : status = exec_command_echo(scan_state, active_branch, cmd);
355 7740 : else if (strcmp(cmd, "elif") == 0)
356 48 : status = exec_command_elif(scan_state, cstack, query_buf);
357 7692 : else if (strcmp(cmd, "else") == 0)
358 132 : status = exec_command_else(scan_state, cstack, query_buf);
359 7560 : else if (strcmp(cmd, "endif") == 0)
360 180 : status = exec_command_endif(scan_state, cstack, query_buf);
361 7380 : else if (strcmp(cmd, "endpipeline") == 0)
362 360 : status = exec_command_endpipeline(scan_state, active_branch);
363 7020 : else if (strcmp(cmd, "encoding") == 0)
364 22 : status = exec_command_encoding(scan_state, active_branch);
365 6998 : else if (strcmp(cmd, "errverbose") == 0)
366 14 : status = exec_command_errverbose(scan_state, active_branch);
367 6984 : else if (strcmp(cmd, "f") == 0)
368 6 : status = exec_command_f(scan_state, active_branch);
369 6978 : else if (strcmp(cmd, "flush") == 0)
370 24 : status = exec_command_flush(scan_state, active_branch);
371 6954 : else if (strcmp(cmd, "flushrequest") == 0)
372 84 : status = exec_command_flushrequest(scan_state, active_branch);
373 6870 : else if (strcmp(cmd, "g") == 0 || strcmp(cmd, "gx") == 0)
374 330 : status = exec_command_g(scan_state, active_branch, cmd);
375 6540 : else if (strcmp(cmd, "gdesc") == 0)
376 86 : status = exec_command_gdesc(scan_state, active_branch);
377 6454 : else if (strcmp(cmd, "getenv") == 0)
378 286 : status = exec_command_getenv(scan_state, active_branch, cmd);
379 6168 : else if (strcmp(cmd, "getresults") == 0)
380 204 : status = exec_command_getresults(scan_state, active_branch);
381 5964 : else if (strcmp(cmd, "gexec") == 0)
382 58 : status = exec_command_gexec(scan_state, active_branch);
383 5906 : else if (strcmp(cmd, "gset") == 0)
384 762 : status = exec_command_gset(scan_state, active_branch);
385 5144 : else if (strcmp(cmd, "h") == 0 || strcmp(cmd, "help") == 0)
386 10 : status = exec_command_help(scan_state, active_branch);
387 5134 : else if (strcmp(cmd, "H") == 0 || strcmp(cmd, "html") == 0)
388 6 : status = exec_command_html(scan_state, active_branch);
389 5128 : else if (strcmp(cmd, "i") == 0 || strcmp(cmd, "include") == 0 ||
390 5122 : strcmp(cmd, "ir") == 0 || strcmp(cmd, "include_relative") == 0)
391 12 : status = exec_command_include(scan_state, active_branch, cmd);
392 5116 : else if (strcmp(cmd, "if") == 0)
393 186 : status = exec_command_if(scan_state, cstack, query_buf);
394 4930 : else if (strcmp(cmd, "l") == 0 || strcmp(cmd, "list") == 0 ||
395 4924 : strcmp(cmd, "lx") == 0 || strcmp(cmd, "listx") == 0 ||
396 4924 : strcmp(cmd, "l+") == 0 || strcmp(cmd, "list+") == 0 ||
397 4924 : strcmp(cmd, "lx+") == 0 || strcmp(cmd, "listx+") == 0 ||
398 4924 : strcmp(cmd, "l+x") == 0 || strcmp(cmd, "list+x") == 0)
399 6 : status = exec_command_list(scan_state, active_branch, cmd);
400 4924 : else if (strncmp(cmd, "lo_", 3) == 0)
401 62 : status = exec_command_lo(scan_state, active_branch, cmd);
402 4862 : else if (strcmp(cmd, "o") == 0 || strcmp(cmd, "out") == 0)
403 42 : status = exec_command_out(scan_state, active_branch);
404 4820 : else if (strcmp(cmd, "p") == 0 || strcmp(cmd, "print") == 0)
405 42 : status = exec_command_print(scan_state, active_branch,
406 : query_buf, previous_buf);
407 4778 : else if (strcmp(cmd, "parse") == 0)
408 108 : status = exec_command_parse(scan_state, active_branch, cmd);
409 4670 : else if (strcmp(cmd, "password") == 0)
410 8 : status = exec_command_password(scan_state, active_branch);
411 4662 : else if (strcmp(cmd, "prompt") == 0)
412 6 : status = exec_command_prompt(scan_state, active_branch, cmd);
413 4656 : else if (strcmp(cmd, "pset") == 0)
414 1740 : status = exec_command_pset(scan_state, active_branch);
415 2916 : else if (strcmp(cmd, "q") == 0 || strcmp(cmd, "quit") == 0)
416 162 : status = exec_command_quit(scan_state, active_branch);
417 2754 : else if (strcmp(cmd, "r") == 0 || strcmp(cmd, "reset") == 0)
418 96 : status = exec_command_reset(scan_state, active_branch, query_buf);
419 2658 : else if (strcmp(cmd, "s") == 0)
420 6 : status = exec_command_s(scan_state, active_branch);
421 2652 : else if (strcmp(cmd, "sendpipeline") == 0)
422 660 : status = exec_command_sendpipeline(scan_state, active_branch);
423 1992 : else if (strcmp(cmd, "set") == 0)
424 962 : status = exec_command_set(scan_state, active_branch);
425 1030 : else if (strcmp(cmd, "setenv") == 0)
426 18 : status = exec_command_setenv(scan_state, active_branch, cmd);
427 1012 : else if (strcmp(cmd, "sf") == 0 || strcmp(cmd, "sf+") == 0)
428 66 : status = exec_command_sf_sv(scan_state, active_branch, cmd, true);
429 946 : else if (strcmp(cmd, "sv") == 0 || strcmp(cmd, "sv+") == 0)
430 132 : status = exec_command_sf_sv(scan_state, active_branch, cmd, false);
431 814 : else if (strcmp(cmd, "startpipeline") == 0)
432 360 : status = exec_command_startpipeline(scan_state, active_branch);
433 454 : else if (strcmp(cmd, "syncpipeline") == 0)
434 132 : status = exec_command_syncpipeline(scan_state, active_branch);
435 322 : else if (strcmp(cmd, "t") == 0)
436 70 : status = exec_command_t(scan_state, active_branch);
437 252 : else if (strcmp(cmd, "T") == 0)
438 6 : status = exec_command_T(scan_state, active_branch);
439 246 : else if (strcmp(cmd, "timing") == 0)
440 10 : status = exec_command_timing(scan_state, active_branch);
441 236 : else if (strcmp(cmd, "unset") == 0)
442 52 : status = exec_command_unset(scan_state, active_branch, cmd);
443 184 : else if (strcmp(cmd, "w") == 0 || strcmp(cmd, "write") == 0)
444 12 : status = exec_command_write(scan_state, active_branch, cmd,
445 : query_buf, previous_buf);
446 172 : else if (strcmp(cmd, "watch") == 0)
447 36 : status = exec_command_watch(scan_state, active_branch,
448 : query_buf, previous_buf);
449 136 : else if (strcmp(cmd, "x") == 0)
450 76 : status = exec_command_x(scan_state, active_branch);
451 60 : else if (strcmp(cmd, "z") == 0 ||
452 36 : strcmp(cmd, "zS") == 0 || strcmp(cmd, "zx") == 0 ||
453 30 : strcmp(cmd, "zSx") == 0 || strcmp(cmd, "zxS") == 0)
454 30 : status = exec_command_z(scan_state, active_branch, cmd);
455 30 : else if (strcmp(cmd, "!") == 0)
456 6 : status = exec_command_shell_escape(scan_state, active_branch);
457 24 : else if (strcmp(cmd, "?") == 0)
458 6 : status = exec_command_slash_command_help(scan_state, active_branch);
459 : else
460 18 : status = PSQL_CMD_UNKNOWN;
461 :
462 : /*
463 : * All the commands that return PSQL_CMD_SEND want to execute previous_buf
464 : * if query_buf is empty. For convenience we implement that here, not in
465 : * the individual command subroutines.
466 : */
467 17302 : if (status == PSQL_CMD_SEND)
468 3162 : (void) copy_previous_query(query_buf, previous_buf);
469 :
470 17302 : return status;
471 : }
472 :
473 :
474 : /*
475 : * \a -- toggle field alignment
476 : *
477 : * This makes little sense but we keep it around.
478 : */
479 : static backslashResult
480 54 : exec_command_a(PsqlScanState scan_state, bool active_branch)
481 : {
482 54 : bool success = true;
483 :
484 54 : if (active_branch)
485 : {
486 48 : if (pset.popt.topt.format != PRINT_ALIGNED)
487 24 : success = do_pset("format", "aligned", &pset.popt, pset.quiet);
488 : else
489 24 : success = do_pset("format", "unaligned", &pset.popt, pset.quiet);
490 : }
491 :
492 54 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
493 : }
494 :
495 : /*
496 : * \bind -- set query parameters
497 : */
498 : static backslashResult
499 706 : exec_command_bind(PsqlScanState scan_state, bool active_branch)
500 : {
501 706 : backslashResult status = PSQL_CMD_SKIP_LINE;
502 :
503 706 : if (active_branch)
504 : {
505 : char *opt;
506 700 : int nparams = 0;
507 700 : int nalloc = 0;
508 :
509 700 : clean_extended_state();
510 :
511 1188 : while ((opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false)))
512 : {
513 488 : nparams++;
514 488 : if (nparams > nalloc)
515 : {
516 488 : nalloc = nalloc ? nalloc * 2 : 1;
517 488 : pset.bind_params = pg_realloc_array(pset.bind_params, char *, nalloc);
518 : }
519 488 : pset.bind_params[nparams - 1] = opt;
520 : }
521 :
522 700 : pset.bind_nparams = nparams;
523 700 : pset.send_mode = PSQL_SEND_EXTENDED_QUERY_PARAMS;
524 : }
525 : else
526 6 : ignore_slash_options(scan_state);
527 :
528 706 : return status;
529 : }
530 :
531 : /*
532 : * \bind_named -- set query parameters for an existing prepared statement
533 : */
534 : static backslashResult
535 168 : exec_command_bind_named(PsqlScanState scan_state, bool active_branch,
536 : const char *cmd)
537 : {
538 168 : backslashResult status = PSQL_CMD_SKIP_LINE;
539 :
540 168 : if (active_branch)
541 : {
542 : char *opt;
543 162 : int nparams = 0;
544 162 : int nalloc = 0;
545 :
546 162 : clean_extended_state();
547 :
548 : /* get the mandatory prepared statement name */
549 162 : opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
550 162 : if (!opt)
551 : {
552 12 : pg_log_error("\\%s: missing required argument", cmd);
553 12 : status = PSQL_CMD_ERROR;
554 : }
555 : else
556 : {
557 150 : pset.stmtName = opt;
558 150 : pset.send_mode = PSQL_SEND_EXTENDED_QUERY_PREPARED;
559 :
560 : /* set of parameters */
561 318 : while ((opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false)))
562 : {
563 168 : nparams++;
564 168 : if (nparams > nalloc)
565 : {
566 168 : nalloc = nalloc ? nalloc * 2 : 1;
567 168 : pset.bind_params = pg_realloc_array(pset.bind_params, char *, nalloc);
568 : }
569 168 : pset.bind_params[nparams - 1] = opt;
570 : }
571 150 : pset.bind_nparams = nparams;
572 : }
573 : }
574 : else
575 6 : ignore_slash_options(scan_state);
576 :
577 168 : return status;
578 : }
579 :
580 : /*
581 : * \C -- override table title (formerly change HTML caption)
582 : */
583 : static backslashResult
584 6 : exec_command_C(PsqlScanState scan_state, bool active_branch)
585 : {
586 6 : bool success = true;
587 :
588 6 : if (active_branch)
589 : {
590 0 : char *opt = psql_scan_slash_option(scan_state,
591 : OT_NORMAL, NULL, true);
592 :
593 0 : success = do_pset("title", opt, &pset.popt, pset.quiet);
594 0 : free(opt);
595 : }
596 : else
597 6 : ignore_slash_options(scan_state);
598 :
599 6 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
600 : }
601 :
602 : /*
603 : * \c or \connect -- connect to database using the specified parameters.
604 : *
605 : * \c [-reuse-previous=BOOL] dbname user host port
606 : *
607 : * Specifying a parameter as '-' is equivalent to omitting it. Examples:
608 : *
609 : * \c - - hst Connect to current database on current port of
610 : * host "hst" as current user.
611 : * \c - usr - prt Connect to current database on port "prt" of current host
612 : * as user "usr".
613 : * \c dbs Connect to database "dbs" on current port of current host
614 : * as current user.
615 : */
616 : static backslashResult
617 336 : exec_command_connect(PsqlScanState scan_state, bool active_branch)
618 : {
619 336 : bool success = true;
620 :
621 336 : if (active_branch)
622 : {
623 : static const char prefix[] = "-reuse-previous=";
624 : char *opt1,
625 : *opt2,
626 : *opt3,
627 : *opt4;
628 330 : enum trivalue reuse_previous = TRI_DEFAULT;
629 :
630 330 : opt1 = read_connect_arg(scan_state);
631 330 : if (opt1 != NULL && strncmp(opt1, prefix, sizeof(prefix) - 1) == 0)
632 : {
633 : bool on_off;
634 :
635 22 : success = ParseVariableBool(opt1 + sizeof(prefix) - 1,
636 : "-reuse-previous",
637 : &on_off);
638 22 : if (success)
639 : {
640 22 : reuse_previous = on_off ? TRI_YES : TRI_NO;
641 22 : free(opt1);
642 22 : opt1 = read_connect_arg(scan_state);
643 : }
644 : }
645 :
646 330 : if (success) /* give up if reuse_previous was invalid */
647 : {
648 330 : opt2 = read_connect_arg(scan_state);
649 330 : opt3 = read_connect_arg(scan_state);
650 330 : opt4 = read_connect_arg(scan_state);
651 :
652 330 : success = do_connect(reuse_previous, opt1, opt2, opt3, opt4);
653 :
654 330 : free(opt2);
655 330 : free(opt3);
656 330 : free(opt4);
657 : }
658 330 : free(opt1);
659 : }
660 : else
661 6 : ignore_slash_options(scan_state);
662 :
663 336 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
664 : }
665 :
666 : /*
667 : * \cd -- change directory
668 : */
669 : static backslashResult
670 6 : exec_command_cd(PsqlScanState scan_state, bool active_branch, const char *cmd)
671 : {
672 6 : bool success = true;
673 :
674 6 : if (active_branch)
675 : {
676 0 : char *opt = psql_scan_slash_option(scan_state,
677 : OT_NORMAL, NULL, true);
678 : char *dir;
679 :
680 0 : if (opt)
681 0 : dir = opt;
682 : else
683 : {
684 : #ifndef WIN32
685 : /* This should match get_home_path() */
686 0 : dir = getenv("HOME");
687 0 : if (dir == NULL || dir[0] == '\0')
688 : {
689 0 : uid_t user_id = geteuid();
690 : struct passwd *pw;
691 :
692 0 : errno = 0; /* clear errno before call */
693 0 : pw = getpwuid(user_id);
694 0 : if (pw)
695 0 : dir = pw->pw_dir;
696 : else
697 : {
698 0 : pg_log_error("could not get home directory for user ID %ld: %s",
699 : (long) user_id,
700 : errno ? strerror(errno) : _("user does not exist"));
701 0 : success = false;
702 : }
703 : }
704 : #else /* WIN32 */
705 :
706 : /*
707 : * On Windows, 'cd' without arguments prints the current
708 : * directory, so if someone wants to code this here instead...
709 : */
710 : dir = "/";
711 : #endif /* WIN32 */
712 : }
713 :
714 0 : if (success &&
715 0 : chdir(dir) < 0)
716 : {
717 0 : pg_log_error("\\%s: could not change directory to \"%s\": %m",
718 : cmd, dir);
719 0 : success = false;
720 : }
721 :
722 0 : free(opt);
723 : }
724 : else
725 6 : ignore_slash_options(scan_state);
726 :
727 6 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
728 : }
729 :
730 : /*
731 : * \close -- close a previously prepared statement
732 : */
733 : static backslashResult
734 48 : exec_command_close(PsqlScanState scan_state, bool active_branch, const char *cmd)
735 : {
736 48 : backslashResult status = PSQL_CMD_SKIP_LINE;
737 :
738 48 : if (active_branch)
739 : {
740 42 : char *opt = psql_scan_slash_option(scan_state,
741 : OT_NORMAL, NULL, false);
742 :
743 42 : clean_extended_state();
744 :
745 42 : if (!opt)
746 : {
747 6 : pg_log_error("\\%s: missing required argument", cmd);
748 6 : status = PSQL_CMD_ERROR;
749 : }
750 : else
751 : {
752 36 : pset.stmtName = opt;
753 36 : pset.send_mode = PSQL_SEND_EXTENDED_CLOSE;
754 36 : status = PSQL_CMD_SEND;
755 : }
756 : }
757 : else
758 6 : ignore_slash_options(scan_state);
759 :
760 48 : return status;
761 : }
762 :
763 : /*
764 : * \conninfo -- display information about the current connection
765 : */
766 : static backslashResult
767 6 : exec_command_conninfo(PsqlScanState scan_state, bool active_branch)
768 : {
769 : printTableContent cont;
770 : int rows,
771 : cols;
772 : char *db;
773 : char *host;
774 : bool print_hostaddr;
775 : char *hostaddr;
776 : char *protocol_version,
777 : *backend_pid;
778 : int ssl_in_use,
779 : password_used,
780 : gssapi_used;
781 : char *paramval;
782 :
783 6 : if (!active_branch)
784 6 : return PSQL_CMD_SKIP_LINE;
785 :
786 0 : db = PQdb(pset.db);
787 0 : if (db == NULL)
788 : {
789 0 : printf(_("You are currently not connected to a database.\n"));
790 0 : return PSQL_CMD_SKIP_LINE;
791 : }
792 :
793 : /* Get values for the parameters */
794 0 : host = PQhost(pset.db);
795 0 : hostaddr = PQhostaddr(pset.db);
796 0 : protocol_version = psprintf("%d", PQprotocolVersion(pset.db));
797 0 : ssl_in_use = PQsslInUse(pset.db);
798 0 : password_used = PQconnectionUsedPassword(pset.db);
799 0 : gssapi_used = PQconnectionUsedGSSAPI(pset.db);
800 0 : backend_pid = psprintf("%d", PQbackendPID(pset.db));
801 :
802 : /* Only print hostaddr if it differs from host, and not if unixsock */
803 0 : print_hostaddr = (!is_unixsock_path(host) &&
804 0 : hostaddr && *hostaddr && strcmp(host, hostaddr) != 0);
805 :
806 : /* Determine the exact number of rows to print */
807 0 : rows = 12;
808 0 : cols = 2;
809 0 : if (ssl_in_use)
810 0 : rows += 6;
811 0 : if (print_hostaddr)
812 0 : rows++;
813 :
814 : /* Set it all up */
815 0 : printTableInit(&cont, &pset.popt.topt, _("Connection Information"), cols, rows);
816 0 : printTableAddHeader(&cont, _("Parameter"), true, 'l');
817 0 : printTableAddHeader(&cont, _("Value"), true, 'l');
818 :
819 : /* Database */
820 0 : printTableAddCell(&cont, _("Database"), false, false);
821 0 : printTableAddCell(&cont, db, false, false);
822 :
823 : /* Client User */
824 0 : printTableAddCell(&cont, _("Client User"), false, false);
825 0 : printTableAddCell(&cont, PQuser(pset.db), false, false);
826 :
827 : /* Host/hostaddr/socket */
828 0 : if (is_unixsock_path(host))
829 : {
830 : /* hostaddr if specified overrides socket, so suppress the latter */
831 0 : if (hostaddr && *hostaddr)
832 : {
833 0 : printTableAddCell(&cont, _("Host Address"), false, false);
834 0 : printTableAddCell(&cont, hostaddr, false, false);
835 : }
836 : else
837 : {
838 0 : printTableAddCell(&cont, _("Socket Directory"), false, false);
839 0 : printTableAddCell(&cont, host, false, false);
840 : }
841 : }
842 : else
843 : {
844 0 : printTableAddCell(&cont, _("Host"), false, false);
845 0 : printTableAddCell(&cont, host, false, false);
846 0 : if (print_hostaddr)
847 : {
848 0 : printTableAddCell(&cont, _("Host Address"), false, false);
849 0 : printTableAddCell(&cont, hostaddr, false, false);
850 : }
851 : }
852 :
853 : /* Server Port */
854 0 : printTableAddCell(&cont, _("Server Port"), false, false);
855 0 : printTableAddCell(&cont, PQport(pset.db), false, false);
856 :
857 : /* Options */
858 0 : printTableAddCell(&cont, _("Options"), false, false);
859 0 : printTableAddCell(&cont, PQoptions(pset.db), false, false);
860 :
861 : /* Protocol Version */
862 0 : printTableAddCell(&cont, _("Protocol Version"), false, false);
863 0 : printTableAddCell(&cont, protocol_version, false, false);
864 :
865 : /* Password Used */
866 0 : printTableAddCell(&cont, _("Password Used"), false, false);
867 0 : printTableAddCell(&cont, password_used ? _("true") : _("false"), false, false);
868 :
869 : /* GSSAPI Authenticated */
870 0 : printTableAddCell(&cont, _("GSSAPI Authenticated"), false, false);
871 0 : printTableAddCell(&cont, gssapi_used ? _("true") : _("false"), false, false);
872 :
873 : /* Backend PID */
874 0 : printTableAddCell(&cont, _("Backend PID"), false, false);
875 0 : printTableAddCell(&cont, backend_pid, false, false);
876 :
877 : /* TLS Connection */
878 0 : printTableAddCell(&cont, _("TLS Connection"), false, false);
879 0 : printTableAddCell(&cont, ssl_in_use ? _("true") : _("false"), false, false);
880 :
881 : /* TLS Information */
882 0 : if (ssl_in_use)
883 : {
884 : char *library,
885 : *protocol,
886 : *key_bits,
887 : *cipher,
888 : *compression,
889 : *alpn;
890 :
891 0 : library = (char *) PQsslAttribute(pset.db, "library");
892 0 : protocol = (char *) PQsslAttribute(pset.db, "protocol");
893 0 : key_bits = (char *) PQsslAttribute(pset.db, "key_bits");
894 0 : cipher = (char *) PQsslAttribute(pset.db, "cipher");
895 0 : compression = (char *) PQsslAttribute(pset.db, "compression");
896 0 : alpn = (char *) PQsslAttribute(pset.db, "alpn");
897 :
898 0 : printTableAddCell(&cont, _("TLS Library"), false, false);
899 0 : printTableAddCell(&cont, library ? library : _("unknown"), false, false);
900 :
901 0 : printTableAddCell(&cont, _("TLS Protocol"), false, false);
902 0 : printTableAddCell(&cont, protocol ? protocol : _("unknown"), false, false);
903 :
904 0 : printTableAddCell(&cont, _("TLS Key Bits"), false, false);
905 0 : printTableAddCell(&cont, key_bits ? key_bits : _("unknown"), false, false);
906 :
907 0 : printTableAddCell(&cont, _("TLS Cipher"), false, false);
908 0 : printTableAddCell(&cont, cipher ? cipher : _("unknown"), false, false);
909 :
910 0 : printTableAddCell(&cont, _("TLS Compression"), false, false);
911 0 : printTableAddCell(&cont, (compression && strcmp(compression, "off") != 0) ?
912 0 : _("true") : _("false"), false, false);
913 :
914 0 : printTableAddCell(&cont, _("ALPN"), false, false);
915 0 : printTableAddCell(&cont, (alpn && alpn[0] != '\0') ? alpn : _("none"), false, false);
916 : }
917 :
918 0 : paramval = (char *) PQparameterStatus(pset.db, "is_superuser");
919 0 : printTableAddCell(&cont, "Superuser", false, false);
920 0 : printTableAddCell(&cont, paramval ? paramval : _("unknown"), false, false);
921 :
922 0 : paramval = (char *) PQparameterStatus(pset.db, "in_hot_standby");
923 0 : printTableAddCell(&cont, "Hot Standby", false, false);
924 0 : printTableAddCell(&cont, paramval ? paramval : _("unknown"), false, false);
925 :
926 0 : printTable(&cont, pset.queryFout, false, pset.logfile);
927 0 : printTableCleanup(&cont);
928 :
929 0 : pfree(protocol_version);
930 0 : pfree(backend_pid);
931 :
932 0 : return PSQL_CMD_SKIP_LINE;
933 : }
934 :
935 : /*
936 : * \copy -- run a COPY command
937 : */
938 : static backslashResult
939 168 : exec_command_copy(PsqlScanState scan_state, bool active_branch)
940 : {
941 168 : bool success = true;
942 :
943 168 : if (active_branch)
944 : {
945 162 : char *opt = psql_scan_slash_option(scan_state,
946 : OT_WHOLE_LINE, NULL, false);
947 :
948 162 : success = do_copy(opt);
949 162 : free(opt);
950 : }
951 : else
952 6 : ignore_slash_whole_line(scan_state);
953 :
954 168 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
955 : }
956 :
957 : /*
958 : * \copyright -- print copyright notice
959 : */
960 : static backslashResult
961 8 : exec_command_copyright(PsqlScanState scan_state, bool active_branch)
962 : {
963 8 : if (active_branch)
964 2 : print_copyright();
965 :
966 8 : return PSQL_CMD_SKIP_LINE;
967 : }
968 :
969 : /*
970 : * \crosstabview -- execute a query and display result in crosstab
971 : */
972 : static backslashResult
973 138 : exec_command_crosstabview(PsqlScanState scan_state, bool active_branch)
974 : {
975 138 : backslashResult status = PSQL_CMD_SKIP_LINE;
976 :
977 138 : if (active_branch)
978 : {
979 : int i;
980 :
981 660 : for (i = 0; i < lengthof(pset.ctv_args); i++)
982 528 : pset.ctv_args[i] = psql_scan_slash_option(scan_state,
983 : OT_NORMAL, NULL, true);
984 132 : pset.crosstab_flag = true;
985 132 : status = PSQL_CMD_SEND;
986 : }
987 : else
988 6 : ignore_slash_options(scan_state);
989 :
990 138 : return status;
991 : }
992 :
993 : /*
994 : * \d* commands
995 : */
996 : static backslashResult
997 6740 : exec_command_d(PsqlScanState scan_state, bool active_branch, const char *cmd)
998 : {
999 6740 : backslashResult status = PSQL_CMD_SKIP_LINE;
1000 6740 : bool success = true;
1001 :
1002 6740 : if (active_branch)
1003 : {
1004 : char *pattern;
1005 : bool show_verbose,
1006 : show_system;
1007 : unsigned short int save_expanded;
1008 :
1009 : /* We don't do SQLID reduction on the pattern yet */
1010 6734 : pattern = psql_scan_slash_option(scan_state,
1011 : OT_NORMAL, NULL, true);
1012 :
1013 6734 : show_verbose = strchr(cmd, '+') ? true : false;
1014 6734 : show_system = strchr(cmd, 'S') ? true : false;
1015 :
1016 : /*
1017 : * The 'x' option turns expanded mode on for this command only. This
1018 : * is allowed in all \d* commands, except \d by itself, since \dx is a
1019 : * separate command. So the 'x' option cannot appear immediately after
1020 : * \d, but it can appear after \d followed by other options.
1021 : */
1022 6734 : save_expanded = pset.popt.topt.expanded;
1023 6734 : if (cmd[1] != '\0' && strchr(&cmd[2], 'x'))
1024 24 : pset.popt.topt.expanded = 1;
1025 :
1026 6734 : switch (cmd[1])
1027 : {
1028 3944 : case '\0':
1029 : case '+':
1030 : case 'S':
1031 3944 : if (pattern)
1032 3926 : success = describeTableDetails(pattern, show_verbose, show_system);
1033 : else
1034 : /* standard listing of interesting things */
1035 18 : success = listTables("tvmsE", NULL, show_verbose, show_system);
1036 3944 : break;
1037 222 : case 'A':
1038 : {
1039 222 : char *pattern2 = NULL;
1040 :
1041 222 : if (pattern && cmd[2] != '\0' && cmd[2] != '+' && cmd[2] != 'x')
1042 144 : pattern2 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
1043 :
1044 222 : switch (cmd[2])
1045 : {
1046 78 : case '\0':
1047 : case '+':
1048 : case 'x':
1049 78 : success = describeAccessMethods(pattern, show_verbose);
1050 78 : break;
1051 30 : case 'c':
1052 30 : success = listOperatorClasses(pattern, pattern2, show_verbose);
1053 30 : break;
1054 36 : case 'f':
1055 36 : success = listOperatorFamilies(pattern, pattern2, show_verbose);
1056 36 : break;
1057 36 : case 'o':
1058 36 : success = listOpFamilyOperators(pattern, pattern2, show_verbose);
1059 36 : break;
1060 42 : case 'p':
1061 42 : success = listOpFamilyFunctions(pattern, pattern2, show_verbose);
1062 42 : break;
1063 0 : default:
1064 0 : status = PSQL_CMD_UNKNOWN;
1065 0 : break;
1066 : }
1067 :
1068 222 : free(pattern2);
1069 : }
1070 222 : break;
1071 48 : case 'a':
1072 48 : success = describeAggregates(pattern, show_verbose, show_system);
1073 48 : break;
1074 24 : case 'b':
1075 24 : success = describeTablespaces(pattern, show_verbose);
1076 24 : break;
1077 54 : case 'c':
1078 54 : if (strncmp(cmd, "dconfig", 7) == 0)
1079 12 : success = describeConfigurationParameters(pattern,
1080 : show_verbose,
1081 : show_system);
1082 : else
1083 42 : success = listConversions(pattern,
1084 : show_verbose,
1085 : show_system);
1086 54 : break;
1087 42 : case 'C':
1088 42 : success = listCasts(pattern, show_verbose);
1089 42 : break;
1090 78 : case 'd':
1091 78 : if (strncmp(cmd, "ddp", 3) == 0)
1092 36 : success = listDefaultACLs(pattern);
1093 : else
1094 42 : success = objectDescription(pattern, show_system);
1095 78 : break;
1096 60 : case 'D':
1097 60 : success = listDomains(pattern, show_verbose, show_system);
1098 60 : break;
1099 298 : case 'f': /* function subsystem */
1100 298 : switch (cmd[2])
1101 : {
1102 298 : case '\0':
1103 : case '+':
1104 : case 'S':
1105 : case 'a':
1106 : case 'n':
1107 : case 'p':
1108 : case 't':
1109 : case 'w':
1110 : case 'x':
1111 298 : success = exec_command_dfo(scan_state, cmd, pattern,
1112 : show_verbose, show_system);
1113 298 : break;
1114 0 : default:
1115 0 : status = PSQL_CMD_UNKNOWN;
1116 0 : break;
1117 : }
1118 298 : break;
1119 24 : case 'g':
1120 : /* no longer distinct from \du */
1121 24 : success = describeRoles(pattern, show_verbose, show_system);
1122 24 : break;
1123 6 : case 'l':
1124 6 : success = listLargeObjects(show_verbose);
1125 6 : break;
1126 30 : case 'L':
1127 30 : success = listLanguages(pattern, show_verbose, show_system);
1128 30 : break;
1129 24 : case 'n':
1130 24 : success = listSchemas(pattern, show_verbose, show_system);
1131 24 : break;
1132 62 : case 'o':
1133 62 : success = exec_command_dfo(scan_state, cmd, pattern,
1134 : show_verbose, show_system);
1135 62 : break;
1136 42 : case 'O':
1137 42 : success = listCollations(pattern, show_verbose, show_system);
1138 42 : break;
1139 72 : case 'p':
1140 72 : success = permissionsList(pattern, show_system);
1141 72 : break;
1142 108 : case 'P':
1143 : {
1144 108 : switch (cmd[2])
1145 : {
1146 108 : case '\0':
1147 : case '+':
1148 : case 't':
1149 : case 'i':
1150 : case 'n':
1151 : case 'x':
1152 108 : success = listPartitionedTables(&cmd[2], pattern, show_verbose);
1153 108 : break;
1154 0 : default:
1155 0 : status = PSQL_CMD_UNKNOWN;
1156 0 : break;
1157 : }
1158 : }
1159 108 : break;
1160 68 : case 'T':
1161 68 : success = describeTypes(pattern, show_verbose, show_system);
1162 68 : break;
1163 294 : case 't':
1164 : case 'v':
1165 : case 'm':
1166 : case 'i':
1167 : case 's':
1168 : case 'E':
1169 294 : success = listTables(&cmd[1], pattern, show_verbose, show_system);
1170 294 : break;
1171 32 : case 'r':
1172 32 : if (cmd[2] == 'd' && cmd[3] == 's')
1173 26 : {
1174 26 : char *pattern2 = NULL;
1175 :
1176 26 : if (pattern)
1177 24 : pattern2 = psql_scan_slash_option(scan_state,
1178 : OT_NORMAL, NULL, true);
1179 26 : success = listDbRoleSettings(pattern, pattern2);
1180 :
1181 26 : free(pattern2);
1182 : }
1183 6 : else if (cmd[2] == 'g')
1184 6 : success = describeRoleGrants(pattern, show_system);
1185 : else
1186 0 : status = PSQL_CMD_UNKNOWN;
1187 32 : break;
1188 534 : case 'R':
1189 534 : switch (cmd[2])
1190 : {
1191 384 : case 'p':
1192 384 : if (show_verbose)
1193 336 : success = describePublications(pattern);
1194 : else
1195 48 : success = listPublications(pattern);
1196 384 : break;
1197 150 : case 's':
1198 150 : success = describeSubscriptions(pattern, show_verbose);
1199 150 : break;
1200 0 : default:
1201 0 : status = PSQL_CMD_UNKNOWN;
1202 : }
1203 534 : break;
1204 6 : case 'u':
1205 6 : success = describeRoles(pattern, show_verbose, show_system);
1206 6 : break;
1207 168 : case 'F': /* text search subsystem */
1208 168 : switch (cmd[2])
1209 : {
1210 42 : case '\0':
1211 : case '+':
1212 : case 'x':
1213 42 : success = listTSConfigs(pattern, show_verbose);
1214 42 : break;
1215 42 : case 'p':
1216 42 : success = listTSParsers(pattern, show_verbose);
1217 42 : break;
1218 42 : case 'd':
1219 42 : success = listTSDictionaries(pattern, show_verbose);
1220 42 : break;
1221 42 : case 't':
1222 42 : success = listTSTemplates(pattern, show_verbose);
1223 42 : break;
1224 0 : default:
1225 0 : status = PSQL_CMD_UNKNOWN;
1226 0 : break;
1227 : }
1228 168 : break;
1229 312 : case 'e': /* SQL/MED subsystem */
1230 312 : switch (cmd[2])
1231 : {
1232 120 : case 's':
1233 120 : success = listForeignServers(pattern, show_verbose);
1234 120 : break;
1235 60 : case 'u':
1236 60 : success = listUserMappings(pattern, show_verbose);
1237 60 : break;
1238 114 : case 'w':
1239 114 : success = listForeignDataWrappers(pattern, show_verbose);
1240 114 : break;
1241 18 : case 't':
1242 18 : success = listForeignTables(pattern, show_verbose);
1243 18 : break;
1244 0 : default:
1245 0 : status = PSQL_CMD_UNKNOWN;
1246 0 : break;
1247 : }
1248 312 : break;
1249 56 : case 'x': /* Extensions */
1250 56 : if (show_verbose)
1251 32 : success = listExtensionContents(pattern);
1252 : else
1253 24 : success = listExtensions(pattern);
1254 56 : break;
1255 102 : case 'X': /* Extended Statistics */
1256 102 : success = listExtendedStats(pattern);
1257 102 : break;
1258 24 : case 'y': /* Event Triggers */
1259 24 : success = listEventTriggers(pattern, show_verbose);
1260 24 : break;
1261 0 : default:
1262 0 : status = PSQL_CMD_UNKNOWN;
1263 : }
1264 :
1265 : /* Restore original expanded mode */
1266 6734 : pset.popt.topt.expanded = save_expanded;
1267 :
1268 6734 : free(pattern);
1269 : }
1270 : else
1271 6 : ignore_slash_options(scan_state);
1272 :
1273 6740 : if (!success)
1274 914 : status = PSQL_CMD_ERROR;
1275 :
1276 6740 : return status;
1277 : }
1278 :
1279 : /* \df and \do; messy enough to split out of exec_command_d */
1280 : static bool
1281 360 : exec_command_dfo(PsqlScanState scan_state, const char *cmd,
1282 : const char *pattern,
1283 : bool show_verbose, bool show_system)
1284 : {
1285 : bool success;
1286 : char *arg_patterns[FUNC_MAX_ARGS];
1287 360 : int num_arg_patterns = 0;
1288 :
1289 : /* Collect argument-type patterns too */
1290 360 : if (pattern) /* otherwise it was just \df or \do */
1291 : {
1292 : char *ap;
1293 :
1294 440 : while ((ap = psql_scan_slash_option(scan_state,
1295 : OT_NORMAL, NULL, true)) != NULL)
1296 : {
1297 84 : arg_patterns[num_arg_patterns++] = ap;
1298 84 : if (num_arg_patterns >= FUNC_MAX_ARGS)
1299 0 : break; /* protect limited-size array */
1300 : }
1301 : }
1302 :
1303 360 : if (cmd[1] == 'f')
1304 298 : success = describeFunctions(&cmd[2], pattern,
1305 : arg_patterns, num_arg_patterns,
1306 : show_verbose, show_system);
1307 : else
1308 62 : success = describeOperators(pattern,
1309 : arg_patterns, num_arg_patterns,
1310 : show_verbose, show_system);
1311 :
1312 444 : while (--num_arg_patterns >= 0)
1313 84 : free(arg_patterns[num_arg_patterns]);
1314 :
1315 360 : return success;
1316 : }
1317 :
1318 : /*
1319 : * \e or \edit -- edit the current query buffer, or edit a file and
1320 : * make it the query buffer
1321 : */
1322 : static backslashResult
1323 6 : exec_command_edit(PsqlScanState scan_state, bool active_branch,
1324 : PQExpBuffer query_buf, PQExpBuffer previous_buf)
1325 : {
1326 6 : backslashResult status = PSQL_CMD_SKIP_LINE;
1327 :
1328 6 : if (active_branch)
1329 : {
1330 0 : if (!query_buf)
1331 : {
1332 0 : pg_log_error("no query buffer");
1333 0 : status = PSQL_CMD_ERROR;
1334 : }
1335 : else
1336 : {
1337 : char *fname;
1338 0 : char *ln = NULL;
1339 0 : int lineno = -1;
1340 :
1341 0 : fname = psql_scan_slash_option(scan_state,
1342 : OT_NORMAL, NULL, true);
1343 0 : if (fname)
1344 : {
1345 : /* try to get separate lineno arg */
1346 0 : ln = psql_scan_slash_option(scan_state,
1347 : OT_NORMAL, NULL, true);
1348 0 : if (ln == NULL)
1349 : {
1350 : /* only one arg; maybe it is lineno not fname */
1351 0 : if (fname[0] &&
1352 0 : strspn(fname, "0123456789") == strlen(fname))
1353 : {
1354 : /* all digits, so assume it is lineno */
1355 0 : ln = fname;
1356 0 : fname = NULL;
1357 : }
1358 : }
1359 : }
1360 0 : if (ln)
1361 : {
1362 0 : lineno = atoi(ln);
1363 0 : if (lineno < 1)
1364 : {
1365 0 : pg_log_error("invalid line number: %s", ln);
1366 0 : status = PSQL_CMD_ERROR;
1367 : }
1368 : }
1369 0 : if (status != PSQL_CMD_ERROR)
1370 : {
1371 : bool discard_on_quit;
1372 :
1373 0 : expand_tilde(&fname);
1374 0 : if (fname)
1375 : {
1376 0 : canonicalize_path_enc(fname, pset.encoding);
1377 : /* Always clear buffer if the file isn't modified */
1378 0 : discard_on_quit = true;
1379 : }
1380 : else
1381 : {
1382 : /*
1383 : * If query_buf is empty, recall previous query for
1384 : * editing. But in that case, the query buffer should be
1385 : * emptied if editing doesn't modify the file.
1386 : */
1387 0 : discard_on_quit = copy_previous_query(query_buf,
1388 : previous_buf);
1389 : }
1390 :
1391 0 : if (do_edit(fname, query_buf, lineno, discard_on_quit, NULL))
1392 0 : status = PSQL_CMD_NEWEDIT;
1393 : else
1394 0 : status = PSQL_CMD_ERROR;
1395 : }
1396 :
1397 : /*
1398 : * On error while editing or if specifying an incorrect line
1399 : * number, reset the query buffer.
1400 : */
1401 0 : if (status == PSQL_CMD_ERROR)
1402 0 : resetPQExpBuffer(query_buf);
1403 :
1404 0 : free(fname);
1405 0 : free(ln);
1406 : }
1407 : }
1408 : else
1409 6 : ignore_slash_options(scan_state);
1410 :
1411 6 : return status;
1412 : }
1413 :
1414 : /*
1415 : * \ef/\ev -- edit the named function/view, or
1416 : * present a blank CREATE FUNCTION/VIEW template if no argument is given
1417 : */
1418 : static backslashResult
1419 12 : exec_command_ef_ev(PsqlScanState scan_state, bool active_branch,
1420 : PQExpBuffer query_buf, bool is_func)
1421 : {
1422 12 : backslashResult status = PSQL_CMD_SKIP_LINE;
1423 :
1424 12 : if (active_branch)
1425 : {
1426 0 : char *obj_desc = psql_scan_slash_option(scan_state,
1427 : OT_WHOLE_LINE,
1428 : NULL, true);
1429 0 : int lineno = -1;
1430 :
1431 0 : if (!query_buf)
1432 : {
1433 0 : pg_log_error("no query buffer");
1434 0 : status = PSQL_CMD_ERROR;
1435 : }
1436 : else
1437 : {
1438 0 : Oid obj_oid = InvalidOid;
1439 0 : EditableObjectType eot = is_func ? EditableFunction : EditableView;
1440 :
1441 0 : lineno = strip_lineno_from_objdesc(obj_desc);
1442 0 : if (lineno == 0)
1443 : {
1444 : /* error already reported */
1445 0 : status = PSQL_CMD_ERROR;
1446 : }
1447 0 : else if (!obj_desc)
1448 : {
1449 : /* set up an empty command to fill in */
1450 0 : resetPQExpBuffer(query_buf);
1451 0 : if (is_func)
1452 0 : appendPQExpBufferStr(query_buf,
1453 : "CREATE FUNCTION ( )\n"
1454 : " RETURNS \n"
1455 : " LANGUAGE \n"
1456 : " -- common options: IMMUTABLE STABLE STRICT SECURITY DEFINER\n"
1457 : "AS $function$\n"
1458 : "\n$function$\n");
1459 : else
1460 0 : appendPQExpBufferStr(query_buf,
1461 : "CREATE VIEW AS\n"
1462 : " SELECT \n"
1463 : " -- something...\n");
1464 : }
1465 0 : else if (!lookup_object_oid(eot, obj_desc, &obj_oid))
1466 : {
1467 : /* error already reported */
1468 0 : status = PSQL_CMD_ERROR;
1469 : }
1470 0 : else if (!get_create_object_cmd(eot, obj_oid, query_buf))
1471 : {
1472 : /* error already reported */
1473 0 : status = PSQL_CMD_ERROR;
1474 : }
1475 0 : else if (is_func && lineno > 0)
1476 : {
1477 : /*
1478 : * lineno "1" should correspond to the first line of the
1479 : * function body. We expect that pg_get_functiondef() will
1480 : * emit that on a line beginning with "AS ", "BEGIN ", or
1481 : * "RETURN ", and that there can be no such line before the
1482 : * real start of the function body. Increment lineno by the
1483 : * number of lines before that line, so that it becomes
1484 : * relative to the first line of the function definition.
1485 : */
1486 0 : const char *lines = query_buf->data;
1487 :
1488 0 : while (*lines != '\0')
1489 : {
1490 0 : if (strncmp(lines, "AS ", 3) == 0 ||
1491 0 : strncmp(lines, "BEGIN ", 6) == 0 ||
1492 0 : strncmp(lines, "RETURN ", 7) == 0)
1493 : break;
1494 0 : lineno++;
1495 : /* find start of next line */
1496 0 : lines = strchr(lines, '\n');
1497 0 : if (!lines)
1498 0 : break;
1499 0 : lines++;
1500 : }
1501 : }
1502 : }
1503 :
1504 0 : if (status != PSQL_CMD_ERROR)
1505 : {
1506 0 : bool edited = false;
1507 :
1508 0 : if (!do_edit(NULL, query_buf, lineno, true, &edited))
1509 0 : status = PSQL_CMD_ERROR;
1510 0 : else if (!edited)
1511 0 : puts(_("No changes"));
1512 : else
1513 0 : status = PSQL_CMD_NEWEDIT;
1514 : }
1515 :
1516 : /*
1517 : * On error while doing object lookup or while editing, or if
1518 : * specifying an incorrect line number, reset the query buffer.
1519 : */
1520 0 : if (status == PSQL_CMD_ERROR)
1521 0 : resetPQExpBuffer(query_buf);
1522 :
1523 0 : free(obj_desc);
1524 : }
1525 : else
1526 12 : ignore_slash_whole_line(scan_state);
1527 :
1528 12 : return status;
1529 : }
1530 :
1531 : /*
1532 : * \echo, \qecho, and \warn -- echo arguments to stdout, query output, or stderr
1533 : */
1534 : static backslashResult
1535 1162 : exec_command_echo(PsqlScanState scan_state, bool active_branch, const char *cmd)
1536 : {
1537 1162 : if (active_branch)
1538 : {
1539 : char *value;
1540 : char quoted;
1541 1024 : bool no_newline = false;
1542 1024 : bool first = true;
1543 : FILE *fout;
1544 :
1545 1024 : if (strcmp(cmd, "qecho") == 0)
1546 30 : fout = pset.queryFout;
1547 994 : else if (strcmp(cmd, "warn") == 0)
1548 192 : fout = stderr;
1549 : else
1550 802 : fout = stdout;
1551 :
1552 3066 : while ((value = psql_scan_slash_option(scan_state,
1553 : OT_NORMAL, "ed, false)))
1554 : {
1555 2042 : if (first && !no_newline && !quoted && strcmp(value, "-n") == 0)
1556 6 : no_newline = true;
1557 : else
1558 : {
1559 2036 : if (first)
1560 1024 : first = false;
1561 : else
1562 1012 : fputc(' ', fout);
1563 2036 : fputs(value, fout);
1564 : }
1565 2042 : free(value);
1566 : }
1567 1024 : if (!no_newline)
1568 1018 : fputs("\n", fout);
1569 : }
1570 : else
1571 138 : ignore_slash_options(scan_state);
1572 :
1573 1162 : return PSQL_CMD_SKIP_LINE;
1574 : }
1575 :
1576 : /*
1577 : * \encoding -- set/show client side encoding
1578 : */
1579 : static backslashResult
1580 22 : exec_command_encoding(PsqlScanState scan_state, bool active_branch)
1581 : {
1582 22 : if (active_branch)
1583 : {
1584 16 : char *encoding = psql_scan_slash_option(scan_state,
1585 : OT_NORMAL, NULL, false);
1586 :
1587 16 : if (!encoding)
1588 : {
1589 : /* show encoding */
1590 0 : puts(pg_encoding_to_char(pset.encoding));
1591 : }
1592 : else
1593 : {
1594 : /* set encoding */
1595 16 : if (PQsetClientEncoding(pset.db, encoding) == -1)
1596 0 : pg_log_error("%s: invalid encoding name or conversion procedure not found", encoding);
1597 : else
1598 : {
1599 : /* save encoding info into psql internal data */
1600 16 : pset.encoding = PQclientEncoding(pset.db);
1601 16 : pset.popt.topt.encoding = pset.encoding;
1602 16 : setFmtEncoding(pset.encoding);
1603 16 : SetVariable(pset.vars, "ENCODING",
1604 : pg_encoding_to_char(pset.encoding));
1605 : }
1606 16 : free(encoding);
1607 : }
1608 : }
1609 : else
1610 6 : ignore_slash_options(scan_state);
1611 :
1612 22 : return PSQL_CMD_SKIP_LINE;
1613 : }
1614 :
1615 : /*
1616 : * \errverbose -- display verbose message from last failed query
1617 : */
1618 : static backslashResult
1619 14 : exec_command_errverbose(PsqlScanState scan_state, bool active_branch)
1620 : {
1621 14 : if (active_branch)
1622 : {
1623 8 : if (pset.last_error_result)
1624 : {
1625 : char *msg;
1626 :
1627 6 : msg = PQresultVerboseErrorMessage(pset.last_error_result,
1628 : PQERRORS_VERBOSE,
1629 : PQSHOW_CONTEXT_ALWAYS);
1630 6 : if (msg)
1631 : {
1632 6 : pg_log_error("%s", msg);
1633 6 : PQfreemem(msg);
1634 : }
1635 : else
1636 0 : puts(_("out of memory"));
1637 : }
1638 : else
1639 2 : puts(_("There is no previous error."));
1640 : }
1641 :
1642 14 : return PSQL_CMD_SKIP_LINE;
1643 : }
1644 :
1645 : /*
1646 : * \f -- change field separator
1647 : */
1648 : static backslashResult
1649 6 : exec_command_f(PsqlScanState scan_state, bool active_branch)
1650 : {
1651 6 : bool success = true;
1652 :
1653 6 : if (active_branch)
1654 : {
1655 0 : char *fname = psql_scan_slash_option(scan_state,
1656 : OT_NORMAL, NULL, false);
1657 :
1658 0 : success = do_pset("fieldsep", fname, &pset.popt, pset.quiet);
1659 0 : free(fname);
1660 : }
1661 : else
1662 6 : ignore_slash_options(scan_state);
1663 :
1664 6 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
1665 : }
1666 :
1667 : /*
1668 : * \flush -- call PQflush() on the connection
1669 : */
1670 : static backslashResult
1671 24 : exec_command_flush(PsqlScanState scan_state, bool active_branch)
1672 : {
1673 24 : backslashResult status = PSQL_CMD_SKIP_LINE;
1674 :
1675 24 : if (active_branch)
1676 : {
1677 18 : pset.send_mode = PSQL_SEND_FLUSH;
1678 18 : status = PSQL_CMD_SEND;
1679 : }
1680 : else
1681 6 : ignore_slash_options(scan_state);
1682 :
1683 24 : return status;
1684 : }
1685 :
1686 : /*
1687 : * \flushrequest -- call PQsendFlushRequest() on the connection
1688 : */
1689 : static backslashResult
1690 84 : exec_command_flushrequest(PsqlScanState scan_state, bool active_branch)
1691 : {
1692 84 : backslashResult status = PSQL_CMD_SKIP_LINE;
1693 :
1694 84 : if (active_branch)
1695 : {
1696 78 : pset.send_mode = PSQL_SEND_FLUSH_REQUEST;
1697 78 : status = PSQL_CMD_SEND;
1698 : }
1699 : else
1700 6 : ignore_slash_options(scan_state);
1701 :
1702 84 : return status;
1703 : }
1704 :
1705 : /*
1706 : * \g [(pset-option[=pset-value] ...)] [filename/shell-command]
1707 : * \gx [(pset-option[=pset-value] ...)] [filename/shell-command]
1708 : *
1709 : * Send the current query. If pset options are specified, they are made
1710 : * active just for this query. If a filename or pipe command is given,
1711 : * the query output goes there. \gx implicitly sets "expanded=on" along
1712 : * with any other pset options that are specified.
1713 : */
1714 : static backslashResult
1715 330 : exec_command_g(PsqlScanState scan_state, bool active_branch, const char *cmd)
1716 : {
1717 330 : backslashResult status = PSQL_CMD_SKIP_LINE;
1718 : char *fname;
1719 :
1720 : /*
1721 : * Because the option processing for this is fairly complicated, we do it
1722 : * and then decide whether the branch is active.
1723 : */
1724 330 : fname = psql_scan_slash_option(scan_state,
1725 : OT_FILEPIPE, NULL, false);
1726 :
1727 330 : if (fname && fname[0] == '(')
1728 : {
1729 : /* Consume pset options through trailing ')' ... */
1730 24 : status = process_command_g_options(fname + 1, scan_state,
1731 : active_branch, cmd);
1732 24 : free(fname);
1733 : /* ... and again attempt to scan the filename. */
1734 24 : fname = psql_scan_slash_option(scan_state,
1735 : OT_FILEPIPE, NULL, false);
1736 : }
1737 :
1738 330 : if (status == PSQL_CMD_SKIP_LINE && active_branch)
1739 : {
1740 306 : if (PQpipelineStatus(pset.db) != PQ_PIPELINE_OFF)
1741 : {
1742 36 : pg_log_error("\\%s not allowed in pipeline mode", cmd);
1743 36 : clean_extended_state();
1744 36 : free(fname);
1745 36 : return PSQL_CMD_ERROR;
1746 : }
1747 :
1748 270 : if (!fname)
1749 238 : pset.gfname = NULL;
1750 : else
1751 : {
1752 32 : expand_tilde(&fname);
1753 32 : pset.gfname = pg_strdup(fname);
1754 : }
1755 270 : if (strcmp(cmd, "gx") == 0)
1756 : {
1757 : /* save settings if not done already, then force expanded=on */
1758 30 : if (pset.gsavepopt == NULL)
1759 24 : pset.gsavepopt = savePsetInfo(&pset.popt);
1760 30 : pset.popt.topt.expanded = 1;
1761 : }
1762 270 : status = PSQL_CMD_SEND;
1763 : }
1764 :
1765 294 : free(fname);
1766 :
1767 294 : return status;
1768 : }
1769 :
1770 : /*
1771 : * Process parenthesized pset options for \g
1772 : *
1773 : * Note: okay to modify first_option, but not to free it; caller does that
1774 : */
1775 : static backslashResult
1776 24 : process_command_g_options(char *first_option, PsqlScanState scan_state,
1777 : bool active_branch, const char *cmd)
1778 : {
1779 24 : bool success = true;
1780 24 : bool found_r_paren = false;
1781 :
1782 : do
1783 : {
1784 : char *option;
1785 : size_t optlen;
1786 :
1787 : /* If not first time through, collect a new option */
1788 42 : if (first_option)
1789 24 : option = first_option;
1790 : else
1791 : {
1792 18 : option = psql_scan_slash_option(scan_state,
1793 : OT_NORMAL, NULL, false);
1794 18 : if (!option)
1795 : {
1796 0 : if (active_branch)
1797 : {
1798 0 : pg_log_error("\\%s: missing right parenthesis", cmd);
1799 0 : success = false;
1800 : }
1801 0 : break;
1802 : }
1803 : }
1804 :
1805 : /* Check for terminating right paren, and remove it from string */
1806 42 : optlen = strlen(option);
1807 42 : if (optlen > 0 && option[optlen - 1] == ')')
1808 : {
1809 24 : option[--optlen] = '\0';
1810 24 : found_r_paren = true;
1811 : }
1812 :
1813 : /* If there was anything besides parentheses, parse/execute it */
1814 42 : if (optlen > 0)
1815 : {
1816 : /* We can have either "name" or "name=value" */
1817 42 : char *valptr = strchr(option, '=');
1818 :
1819 42 : if (valptr)
1820 42 : *valptr++ = '\0';
1821 42 : if (active_branch)
1822 : {
1823 : /* save settings if not done already, then apply option */
1824 42 : if (pset.gsavepopt == NULL)
1825 18 : pset.gsavepopt = savePsetInfo(&pset.popt);
1826 42 : success &= do_pset(option, valptr, &pset.popt, true);
1827 : }
1828 : }
1829 :
1830 : /* Clean up after this option. We should not free first_option. */
1831 42 : if (first_option)
1832 24 : first_option = NULL;
1833 : else
1834 18 : free(option);
1835 42 : } while (!found_r_paren);
1836 :
1837 : /* If we failed after already changing some options, undo side-effects */
1838 24 : if (!success && active_branch && pset.gsavepopt)
1839 : {
1840 0 : restorePsetInfo(&pset.popt, pset.gsavepopt);
1841 0 : pset.gsavepopt = NULL;
1842 : }
1843 :
1844 24 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
1845 : }
1846 :
1847 : /*
1848 : * \gdesc -- describe query result
1849 : */
1850 : static backslashResult
1851 86 : exec_command_gdesc(PsqlScanState scan_state, bool active_branch)
1852 : {
1853 86 : backslashResult status = PSQL_CMD_SKIP_LINE;
1854 :
1855 86 : if (active_branch)
1856 : {
1857 86 : pset.gdesc_flag = true;
1858 86 : status = PSQL_CMD_SEND;
1859 : }
1860 :
1861 86 : return status;
1862 : }
1863 :
1864 : /*
1865 : * \getenv -- set variable from environment variable
1866 : */
1867 : static backslashResult
1868 286 : exec_command_getenv(PsqlScanState scan_state, bool active_branch,
1869 : const char *cmd)
1870 : {
1871 286 : bool success = true;
1872 :
1873 286 : if (active_branch)
1874 : {
1875 286 : char *myvar = psql_scan_slash_option(scan_state,
1876 : OT_NORMAL, NULL, false);
1877 286 : char *envvar = psql_scan_slash_option(scan_state,
1878 : OT_NORMAL, NULL, false);
1879 :
1880 286 : if (!myvar || !envvar)
1881 : {
1882 0 : pg_log_error("\\%s: missing required argument", cmd);
1883 0 : success = false;
1884 : }
1885 : else
1886 : {
1887 286 : char *envval = getenv(envvar);
1888 :
1889 286 : if (envval && !SetVariable(pset.vars, myvar, envval))
1890 0 : success = false;
1891 : }
1892 286 : free(myvar);
1893 286 : free(envvar);
1894 : }
1895 : else
1896 0 : ignore_slash_options(scan_state);
1897 :
1898 286 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
1899 : }
1900 :
1901 : /*
1902 : * \getresults -- read results
1903 : */
1904 : static backslashResult
1905 204 : exec_command_getresults(PsqlScanState scan_state, bool active_branch)
1906 : {
1907 204 : backslashResult status = PSQL_CMD_SKIP_LINE;
1908 :
1909 204 : if (active_branch)
1910 : {
1911 : char *opt;
1912 : int num_results;
1913 :
1914 198 : pset.send_mode = PSQL_SEND_GET_RESULTS;
1915 198 : status = PSQL_CMD_SEND;
1916 198 : opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
1917 :
1918 198 : pset.requested_results = 0;
1919 198 : if (opt != NULL)
1920 : {
1921 72 : num_results = atoi(opt);
1922 72 : if (num_results < 0)
1923 : {
1924 6 : pg_log_error("\\getresults: invalid number of requested results");
1925 6 : return PSQL_CMD_SKIP_LINE;
1926 : }
1927 66 : pset.requested_results = num_results;
1928 : }
1929 : }
1930 : else
1931 6 : ignore_slash_options(scan_state);
1932 :
1933 198 : return status;
1934 : }
1935 :
1936 :
1937 : /*
1938 : * \gexec -- send query and execute each field of result
1939 : */
1940 : static backslashResult
1941 58 : exec_command_gexec(PsqlScanState scan_state, bool active_branch)
1942 : {
1943 58 : backslashResult status = PSQL_CMD_SKIP_LINE;
1944 :
1945 58 : if (active_branch)
1946 : {
1947 52 : if (PQpipelineStatus(pset.db) != PQ_PIPELINE_OFF)
1948 : {
1949 6 : pg_log_error("\\gexec not allowed in pipeline mode");
1950 6 : clean_extended_state();
1951 6 : return PSQL_CMD_ERROR;
1952 : }
1953 46 : pset.gexec_flag = true;
1954 46 : status = PSQL_CMD_SEND;
1955 : }
1956 :
1957 52 : return status;
1958 : }
1959 :
1960 : /*
1961 : * \gset [prefix] -- send query and store result into variables
1962 : */
1963 : static backslashResult
1964 762 : exec_command_gset(PsqlScanState scan_state, bool active_branch)
1965 : {
1966 762 : backslashResult status = PSQL_CMD_SKIP_LINE;
1967 :
1968 762 : if (active_branch)
1969 : {
1970 756 : char *prefix = psql_scan_slash_option(scan_state,
1971 : OT_NORMAL, NULL, false);
1972 :
1973 756 : if (PQpipelineStatus(pset.db) != PQ_PIPELINE_OFF)
1974 : {
1975 12 : pg_log_error("\\gset not allowed in pipeline mode");
1976 12 : clean_extended_state();
1977 12 : return PSQL_CMD_ERROR;
1978 : }
1979 :
1980 744 : if (prefix)
1981 108 : pset.gset_prefix = prefix;
1982 : else
1983 : {
1984 : /* we must set a non-NULL prefix to trigger storing */
1985 636 : pset.gset_prefix = pg_strdup("");
1986 : }
1987 : /* gset_prefix is freed later */
1988 744 : status = PSQL_CMD_SEND;
1989 : }
1990 : else
1991 6 : ignore_slash_options(scan_state);
1992 :
1993 750 : return status;
1994 : }
1995 :
1996 : /*
1997 : * \help [topic] -- print help about SQL commands
1998 : */
1999 : static backslashResult
2000 10 : exec_command_help(PsqlScanState scan_state, bool active_branch)
2001 : {
2002 10 : if (active_branch)
2003 : {
2004 4 : char *opt = psql_scan_slash_option(scan_state,
2005 : OT_WHOLE_LINE, NULL, true);
2006 :
2007 4 : helpSQL(opt, pset.popt.topt.pager);
2008 4 : free(opt);
2009 : }
2010 : else
2011 6 : ignore_slash_whole_line(scan_state);
2012 :
2013 10 : return PSQL_CMD_SKIP_LINE;
2014 : }
2015 :
2016 : /*
2017 : * \H and \html -- toggle HTML formatting
2018 : */
2019 : static backslashResult
2020 6 : exec_command_html(PsqlScanState scan_state, bool active_branch)
2021 : {
2022 6 : bool success = true;
2023 :
2024 6 : if (active_branch)
2025 : {
2026 0 : if (pset.popt.topt.format != PRINT_HTML)
2027 0 : success = do_pset("format", "html", &pset.popt, pset.quiet);
2028 : else
2029 0 : success = do_pset("format", "aligned", &pset.popt, pset.quiet);
2030 : }
2031 :
2032 6 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2033 : }
2034 :
2035 : /*
2036 : * \i and \ir -- include a file
2037 : */
2038 : static backslashResult
2039 12 : exec_command_include(PsqlScanState scan_state, bool active_branch, const char *cmd)
2040 : {
2041 12 : bool success = true;
2042 :
2043 12 : if (active_branch)
2044 : {
2045 0 : char *fname = psql_scan_slash_option(scan_state,
2046 : OT_NORMAL, NULL, true);
2047 :
2048 0 : if (!fname)
2049 : {
2050 0 : pg_log_error("\\%s: missing required argument", cmd);
2051 0 : success = false;
2052 : }
2053 : else
2054 : {
2055 : bool include_relative;
2056 :
2057 0 : include_relative = (strcmp(cmd, "ir") == 0
2058 0 : || strcmp(cmd, "include_relative") == 0);
2059 0 : expand_tilde(&fname);
2060 0 : success = (process_file(fname, include_relative) == EXIT_SUCCESS);
2061 0 : free(fname);
2062 : }
2063 : }
2064 : else
2065 12 : ignore_slash_options(scan_state);
2066 :
2067 12 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2068 : }
2069 :
2070 : /*
2071 : * \if <expr> -- beginning of an \if..\endif block
2072 : *
2073 : * <expr> is parsed as a boolean expression. Invalid expressions will emit a
2074 : * warning and be treated as false. Statements that follow a false expression
2075 : * will be parsed but ignored. Note that in the case where an \if statement
2076 : * is itself within an inactive section of a block, then the entire inner
2077 : * \if..\endif block will be parsed but ignored.
2078 : */
2079 : static backslashResult
2080 186 : exec_command_if(PsqlScanState scan_state, ConditionalStack cstack,
2081 : PQExpBuffer query_buf)
2082 : {
2083 186 : if (conditional_active(cstack))
2084 : {
2085 : /*
2086 : * First, push a new active stack entry; this ensures that the lexer
2087 : * will perform variable substitution and backtick evaluation while
2088 : * scanning the expression. (That should happen anyway, since we know
2089 : * we're in an active outer branch, but let's be sure.)
2090 : */
2091 180 : conditional_stack_push(cstack, IFSTATE_TRUE);
2092 :
2093 : /* Remember current query state in case we need to restore later */
2094 180 : save_query_text_state(scan_state, cstack, query_buf);
2095 :
2096 : /*
2097 : * Evaluate the expression; if it's false, change to inactive state.
2098 : */
2099 180 : if (!is_true_boolean_expression(scan_state, "\\if expression"))
2100 108 : conditional_stack_poke(cstack, IFSTATE_FALSE);
2101 : }
2102 : else
2103 : {
2104 : /*
2105 : * We're within an inactive outer branch, so this entire \if block
2106 : * will be ignored. We don't want to evaluate the expression, so push
2107 : * the "ignored" stack state before scanning it.
2108 : */
2109 6 : conditional_stack_push(cstack, IFSTATE_IGNORED);
2110 :
2111 : /* Remember current query state in case we need to restore later */
2112 6 : save_query_text_state(scan_state, cstack, query_buf);
2113 :
2114 6 : ignore_boolean_expression(scan_state);
2115 : }
2116 :
2117 186 : return PSQL_CMD_SKIP_LINE;
2118 : }
2119 :
2120 : /*
2121 : * \elif <expr> -- alternative branch in an \if..\endif block
2122 : *
2123 : * <expr> is evaluated the same as in \if <expr>.
2124 : */
2125 : static backslashResult
2126 48 : exec_command_elif(PsqlScanState scan_state, ConditionalStack cstack,
2127 : PQExpBuffer query_buf)
2128 : {
2129 48 : bool success = true;
2130 :
2131 48 : switch (conditional_stack_peek(cstack))
2132 : {
2133 6 : case IFSTATE_TRUE:
2134 :
2135 : /*
2136 : * Just finished active branch of this \if block. Update saved
2137 : * state so we will keep whatever data was put in query_buf by the
2138 : * active branch.
2139 : */
2140 6 : save_query_text_state(scan_state, cstack, query_buf);
2141 :
2142 : /*
2143 : * Discard \elif expression and ignore the rest until \endif.
2144 : * Switch state before reading expression to ensure proper lexer
2145 : * behavior.
2146 : */
2147 6 : conditional_stack_poke(cstack, IFSTATE_IGNORED);
2148 6 : ignore_boolean_expression(scan_state);
2149 6 : break;
2150 24 : case IFSTATE_FALSE:
2151 :
2152 : /*
2153 : * Discard any query text added by the just-skipped branch.
2154 : */
2155 24 : discard_query_text(scan_state, cstack, query_buf);
2156 :
2157 : /*
2158 : * Have not yet found a true expression in this \if block, so this
2159 : * might be the first. We have to change state before examining
2160 : * the expression, or the lexer won't do the right thing.
2161 : */
2162 24 : conditional_stack_poke(cstack, IFSTATE_TRUE);
2163 24 : if (!is_true_boolean_expression(scan_state, "\\elif expression"))
2164 18 : conditional_stack_poke(cstack, IFSTATE_FALSE);
2165 24 : break;
2166 6 : case IFSTATE_IGNORED:
2167 :
2168 : /*
2169 : * Discard any query text added by the just-skipped branch.
2170 : */
2171 6 : discard_query_text(scan_state, cstack, query_buf);
2172 :
2173 : /*
2174 : * Skip expression and move on. Either the \if block already had
2175 : * an active section, or whole block is being skipped.
2176 : */
2177 6 : ignore_boolean_expression(scan_state);
2178 6 : break;
2179 6 : case IFSTATE_ELSE_TRUE:
2180 : case IFSTATE_ELSE_FALSE:
2181 6 : pg_log_error("\\elif: cannot occur after \\else");
2182 6 : success = false;
2183 6 : break;
2184 6 : case IFSTATE_NONE:
2185 : /* no \if to elif from */
2186 6 : pg_log_error("\\elif: no matching \\if");
2187 6 : success = false;
2188 6 : break;
2189 : }
2190 :
2191 48 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2192 : }
2193 :
2194 : /*
2195 : * \else -- final alternative in an \if..\endif block
2196 : *
2197 : * Statements within an \else branch will only be executed if
2198 : * all previous \if and \elif expressions evaluated to false
2199 : * and the block was not itself being ignored.
2200 : */
2201 : static backslashResult
2202 132 : exec_command_else(PsqlScanState scan_state, ConditionalStack cstack,
2203 : PQExpBuffer query_buf)
2204 : {
2205 132 : bool success = true;
2206 :
2207 132 : switch (conditional_stack_peek(cstack))
2208 : {
2209 60 : case IFSTATE_TRUE:
2210 :
2211 : /*
2212 : * Just finished active branch of this \if block. Update saved
2213 : * state so we will keep whatever data was put in query_buf by the
2214 : * active branch.
2215 : */
2216 60 : save_query_text_state(scan_state, cstack, query_buf);
2217 :
2218 : /* Now skip the \else branch */
2219 60 : conditional_stack_poke(cstack, IFSTATE_ELSE_FALSE);
2220 60 : break;
2221 48 : case IFSTATE_FALSE:
2222 :
2223 : /*
2224 : * Discard any query text added by the just-skipped branch.
2225 : */
2226 48 : discard_query_text(scan_state, cstack, query_buf);
2227 :
2228 : /*
2229 : * We've not found any true \if or \elif expression, so execute
2230 : * the \else branch.
2231 : */
2232 48 : conditional_stack_poke(cstack, IFSTATE_ELSE_TRUE);
2233 48 : break;
2234 12 : case IFSTATE_IGNORED:
2235 :
2236 : /*
2237 : * Discard any query text added by the just-skipped branch.
2238 : */
2239 12 : discard_query_text(scan_state, cstack, query_buf);
2240 :
2241 : /*
2242 : * Either we previously processed the active branch of this \if,
2243 : * or the whole \if block is being skipped. Either way, skip the
2244 : * \else branch.
2245 : */
2246 12 : conditional_stack_poke(cstack, IFSTATE_ELSE_FALSE);
2247 12 : break;
2248 6 : case IFSTATE_ELSE_TRUE:
2249 : case IFSTATE_ELSE_FALSE:
2250 6 : pg_log_error("\\else: cannot occur after \\else");
2251 6 : success = false;
2252 6 : break;
2253 6 : case IFSTATE_NONE:
2254 : /* no \if to else from */
2255 6 : pg_log_error("\\else: no matching \\if");
2256 6 : success = false;
2257 6 : break;
2258 : }
2259 :
2260 132 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2261 : }
2262 :
2263 : /*
2264 : * \endif -- ends an \if...\endif block
2265 : */
2266 : static backslashResult
2267 180 : exec_command_endif(PsqlScanState scan_state, ConditionalStack cstack,
2268 : PQExpBuffer query_buf)
2269 : {
2270 180 : bool success = true;
2271 :
2272 180 : switch (conditional_stack_peek(cstack))
2273 : {
2274 48 : case IFSTATE_TRUE:
2275 : case IFSTATE_ELSE_TRUE:
2276 : /* Close the \if block, keeping the query text */
2277 48 : success = conditional_stack_pop(cstack);
2278 : Assert(success);
2279 48 : break;
2280 126 : case IFSTATE_FALSE:
2281 : case IFSTATE_IGNORED:
2282 : case IFSTATE_ELSE_FALSE:
2283 :
2284 : /*
2285 : * Discard any query text added by the just-skipped branch.
2286 : */
2287 126 : discard_query_text(scan_state, cstack, query_buf);
2288 :
2289 : /* Close the \if block */
2290 126 : success = conditional_stack_pop(cstack);
2291 : Assert(success);
2292 126 : break;
2293 6 : case IFSTATE_NONE:
2294 : /* no \if to end */
2295 6 : pg_log_error("\\endif: no matching \\if");
2296 6 : success = false;
2297 6 : break;
2298 : }
2299 :
2300 180 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2301 : }
2302 :
2303 : /*
2304 : * \l -- list databases
2305 : */
2306 : static backslashResult
2307 6 : exec_command_list(PsqlScanState scan_state, bool active_branch, const char *cmd)
2308 : {
2309 6 : bool success = true;
2310 :
2311 6 : if (active_branch)
2312 : {
2313 : char *pattern;
2314 : bool show_verbose;
2315 : unsigned short int save_expanded;
2316 :
2317 0 : pattern = psql_scan_slash_option(scan_state,
2318 : OT_NORMAL, NULL, true);
2319 :
2320 0 : show_verbose = strchr(cmd, '+') ? true : false;
2321 :
2322 : /* if 'x' option specified, force expanded mode */
2323 0 : save_expanded = pset.popt.topt.expanded;
2324 0 : if (strchr(cmd, 'x'))
2325 0 : pset.popt.topt.expanded = 1;
2326 :
2327 0 : success = listAllDbs(pattern, show_verbose);
2328 :
2329 : /* restore original expanded mode */
2330 0 : pset.popt.topt.expanded = save_expanded;
2331 :
2332 0 : free(pattern);
2333 : }
2334 : else
2335 6 : ignore_slash_options(scan_state);
2336 :
2337 6 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2338 : }
2339 :
2340 : /*
2341 : * \lo_* -- large object operations
2342 : */
2343 : static backslashResult
2344 62 : exec_command_lo(PsqlScanState scan_state, bool active_branch, const char *cmd)
2345 : {
2346 62 : backslashResult status = PSQL_CMD_SKIP_LINE;
2347 62 : bool success = true;
2348 :
2349 62 : if (active_branch)
2350 : {
2351 : char *opt1,
2352 : *opt2;
2353 :
2354 56 : opt1 = psql_scan_slash_option(scan_state,
2355 : OT_NORMAL, NULL, true);
2356 56 : opt2 = psql_scan_slash_option(scan_state,
2357 : OT_NORMAL, NULL, true);
2358 :
2359 56 : if (strcmp(cmd + 3, "export") == 0)
2360 : {
2361 6 : if (!opt2)
2362 : {
2363 0 : pg_log_error("\\%s: missing required argument", cmd);
2364 0 : success = false;
2365 : }
2366 : else
2367 : {
2368 6 : expand_tilde(&opt2);
2369 6 : success = do_lo_export(opt1, opt2);
2370 : }
2371 : }
2372 :
2373 50 : else if (strcmp(cmd + 3, "import") == 0)
2374 : {
2375 14 : if (!opt1)
2376 : {
2377 0 : pg_log_error("\\%s: missing required argument", cmd);
2378 0 : success = false;
2379 : }
2380 : else
2381 : {
2382 14 : expand_tilde(&opt1);
2383 14 : success = do_lo_import(opt1, opt2);
2384 : }
2385 : }
2386 :
2387 36 : else if (strncmp(cmd + 3, "list", 4) == 0)
2388 : {
2389 : bool show_verbose;
2390 : unsigned short int save_expanded;
2391 :
2392 12 : show_verbose = strchr(cmd, '+') ? true : false;
2393 :
2394 : /* if 'x' option specified, force expanded mode */
2395 12 : save_expanded = pset.popt.topt.expanded;
2396 12 : if (strchr(cmd, 'x'))
2397 0 : pset.popt.topt.expanded = 1;
2398 :
2399 12 : success = listLargeObjects(show_verbose);
2400 :
2401 : /* restore original expanded mode */
2402 12 : pset.popt.topt.expanded = save_expanded;
2403 : }
2404 :
2405 24 : else if (strcmp(cmd + 3, "unlink") == 0)
2406 : {
2407 24 : if (!opt1)
2408 : {
2409 0 : pg_log_error("\\%s: missing required argument", cmd);
2410 0 : success = false;
2411 : }
2412 : else
2413 24 : success = do_lo_unlink(opt1);
2414 : }
2415 :
2416 : else
2417 0 : status = PSQL_CMD_UNKNOWN;
2418 :
2419 56 : free(opt1);
2420 56 : free(opt2);
2421 : }
2422 : else
2423 6 : ignore_slash_options(scan_state);
2424 :
2425 62 : if (!success)
2426 0 : status = PSQL_CMD_ERROR;
2427 :
2428 62 : return status;
2429 : }
2430 :
2431 : /*
2432 : * \o -- set query output
2433 : */
2434 : static backslashResult
2435 42 : exec_command_out(PsqlScanState scan_state, bool active_branch)
2436 : {
2437 42 : bool success = true;
2438 :
2439 42 : if (active_branch)
2440 : {
2441 36 : char *fname = psql_scan_slash_option(scan_state,
2442 : OT_FILEPIPE, NULL, true);
2443 :
2444 36 : expand_tilde(&fname);
2445 36 : success = setQFout(fname);
2446 36 : free(fname);
2447 : }
2448 : else
2449 6 : ignore_slash_filepipe(scan_state);
2450 :
2451 42 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2452 : }
2453 :
2454 : /*
2455 : * \p -- print the current query buffer
2456 : */
2457 : static backslashResult
2458 42 : exec_command_print(PsqlScanState scan_state, bool active_branch,
2459 : PQExpBuffer query_buf, PQExpBuffer previous_buf)
2460 : {
2461 42 : if (active_branch)
2462 : {
2463 : /*
2464 : * We want to print the same thing \g would execute, but not to change
2465 : * the query buffer state; so we can't use copy_previous_query().
2466 : * Also, beware of possibility that buffer pointers are NULL.
2467 : */
2468 36 : if (query_buf && query_buf->len > 0)
2469 18 : puts(query_buf->data);
2470 18 : else if (previous_buf && previous_buf->len > 0)
2471 18 : puts(previous_buf->data);
2472 0 : else if (!pset.quiet)
2473 0 : puts(_("Query buffer is empty."));
2474 36 : fflush(stdout);
2475 : }
2476 :
2477 42 : return PSQL_CMD_SKIP_LINE;
2478 : }
2479 :
2480 : /*
2481 : * \parse -- parse query
2482 : */
2483 : static backslashResult
2484 108 : exec_command_parse(PsqlScanState scan_state, bool active_branch,
2485 : const char *cmd)
2486 : {
2487 108 : backslashResult status = PSQL_CMD_SKIP_LINE;
2488 :
2489 108 : if (active_branch)
2490 : {
2491 102 : char *opt = psql_scan_slash_option(scan_state,
2492 : OT_NORMAL, NULL, false);
2493 :
2494 102 : clean_extended_state();
2495 :
2496 102 : if (!opt)
2497 : {
2498 6 : pg_log_error("\\%s: missing required argument", cmd);
2499 6 : status = PSQL_CMD_ERROR;
2500 : }
2501 : else
2502 : {
2503 96 : pset.stmtName = opt;
2504 96 : pset.send_mode = PSQL_SEND_EXTENDED_PARSE;
2505 96 : status = PSQL_CMD_SEND;
2506 : }
2507 : }
2508 : else
2509 6 : ignore_slash_options(scan_state);
2510 :
2511 108 : return status;
2512 : }
2513 :
2514 : /*
2515 : * \password -- set user password
2516 : */
2517 : static backslashResult
2518 8 : exec_command_password(PsqlScanState scan_state, bool active_branch)
2519 : {
2520 8 : bool success = true;
2521 :
2522 8 : if (active_branch)
2523 : {
2524 2 : char *user = psql_scan_slash_option(scan_state,
2525 : OT_SQLID, NULL, true);
2526 2 : char *pw1 = NULL;
2527 2 : char *pw2 = NULL;
2528 : PQExpBufferData buf;
2529 : PromptInterruptContext prompt_ctx;
2530 :
2531 2 : if (user == NULL)
2532 : {
2533 : /* By default, the command applies to CURRENT_USER */
2534 : PGresult *res;
2535 :
2536 0 : res = PSQLexec("SELECT CURRENT_USER");
2537 0 : if (!res)
2538 0 : return PSQL_CMD_ERROR;
2539 :
2540 0 : user = pg_strdup(PQgetvalue(res, 0, 0));
2541 0 : PQclear(res);
2542 : }
2543 :
2544 : /* Set up to let SIGINT cancel simple_prompt_extended() */
2545 2 : prompt_ctx.jmpbuf = sigint_interrupt_jmp;
2546 2 : prompt_ctx.enabled = &sigint_interrupt_enabled;
2547 2 : prompt_ctx.canceled = false;
2548 :
2549 2 : initPQExpBuffer(&buf);
2550 2 : printfPQExpBuffer(&buf, _("Enter new password for user \"%s\": "), user);
2551 :
2552 2 : pw1 = simple_prompt_extended(buf.data, false, &prompt_ctx);
2553 2 : if (!prompt_ctx.canceled)
2554 2 : pw2 = simple_prompt_extended("Enter it again: ", false, &prompt_ctx);
2555 :
2556 2 : if (prompt_ctx.canceled)
2557 : {
2558 : /* fail silently */
2559 0 : success = false;
2560 : }
2561 2 : else if (strcmp(pw1, pw2) != 0)
2562 : {
2563 0 : pg_log_error("Passwords didn't match.");
2564 0 : success = false;
2565 : }
2566 : else
2567 : {
2568 2 : PGresult *res = PQchangePassword(pset.db, user, pw1);
2569 :
2570 2 : if (PQresultStatus(res) != PGRES_COMMAND_OK)
2571 : {
2572 0 : pg_log_info("%s", PQerrorMessage(pset.db));
2573 0 : success = false;
2574 : }
2575 :
2576 2 : PQclear(res);
2577 : }
2578 :
2579 2 : free(user);
2580 2 : free(pw1);
2581 2 : free(pw2);
2582 2 : termPQExpBuffer(&buf);
2583 : }
2584 : else
2585 6 : ignore_slash_options(scan_state);
2586 :
2587 8 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2588 : }
2589 :
2590 : /*
2591 : * \prompt -- prompt and set variable
2592 : */
2593 : static backslashResult
2594 6 : exec_command_prompt(PsqlScanState scan_state, bool active_branch,
2595 : const char *cmd)
2596 : {
2597 6 : bool success = true;
2598 :
2599 6 : if (active_branch)
2600 : {
2601 : char *opt,
2602 0 : *prompt_text = NULL;
2603 : char *arg1,
2604 : *arg2;
2605 :
2606 0 : arg1 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
2607 0 : arg2 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
2608 :
2609 0 : if (!arg1)
2610 : {
2611 0 : pg_log_error("\\%s: missing required argument", cmd);
2612 0 : success = false;
2613 : }
2614 : else
2615 : {
2616 : char *result;
2617 : PromptInterruptContext prompt_ctx;
2618 :
2619 : /* Set up to let SIGINT cancel simple_prompt_extended() */
2620 0 : prompt_ctx.jmpbuf = sigint_interrupt_jmp;
2621 0 : prompt_ctx.enabled = &sigint_interrupt_enabled;
2622 0 : prompt_ctx.canceled = false;
2623 :
2624 0 : if (arg2)
2625 : {
2626 0 : prompt_text = arg1;
2627 0 : opt = arg2;
2628 : }
2629 : else
2630 0 : opt = arg1;
2631 :
2632 0 : if (!pset.inputfile)
2633 : {
2634 0 : result = simple_prompt_extended(prompt_text, true, &prompt_ctx);
2635 : }
2636 : else
2637 : {
2638 0 : if (prompt_text)
2639 : {
2640 0 : fputs(prompt_text, stdout);
2641 0 : fflush(stdout);
2642 : }
2643 0 : result = gets_fromFile(stdin);
2644 0 : if (!result)
2645 : {
2646 0 : pg_log_error("\\%s: could not read value for variable",
2647 : cmd);
2648 0 : success = false;
2649 : }
2650 : }
2651 :
2652 0 : if (prompt_ctx.canceled ||
2653 0 : (result && !SetVariable(pset.vars, opt, result)))
2654 0 : success = false;
2655 :
2656 0 : free(result);
2657 0 : free(prompt_text);
2658 0 : free(opt);
2659 : }
2660 : }
2661 : else
2662 6 : ignore_slash_options(scan_state);
2663 :
2664 6 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2665 : }
2666 :
2667 : /*
2668 : * \pset -- set printing parameters
2669 : */
2670 : static backslashResult
2671 1740 : exec_command_pset(PsqlScanState scan_state, bool active_branch)
2672 : {
2673 1740 : bool success = true;
2674 :
2675 1740 : if (active_branch)
2676 : {
2677 1728 : char *opt0 = psql_scan_slash_option(scan_state,
2678 : OT_NORMAL, NULL, false);
2679 1728 : char *opt1 = psql_scan_slash_option(scan_state,
2680 : OT_NORMAL, NULL, false);
2681 :
2682 1728 : if (!opt0)
2683 : {
2684 : /* list all variables */
2685 :
2686 : int i;
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",
2696 : "xheader_width",
2697 : NULL
2698 : };
2699 :
2700 138 : for (i = 0; my_list[i] != NULL; i++)
2701 : {
2702 132 : char *val = pset_value_string(my_list[i], &pset.popt);
2703 :
2704 132 : printf("%-24s %s\n", my_list[i], val);
2705 132 : free(val);
2706 : }
2707 :
2708 6 : success = true;
2709 : }
2710 : else
2711 1722 : success = do_pset(opt0, opt1, &pset.popt, pset.quiet);
2712 :
2713 1728 : free(opt0);
2714 1728 : free(opt1);
2715 : }
2716 : else
2717 12 : ignore_slash_options(scan_state);
2718 :
2719 1740 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2720 : }
2721 :
2722 : /*
2723 : * \q or \quit -- exit psql
2724 : */
2725 : static backslashResult
2726 162 : exec_command_quit(PsqlScanState scan_state, bool active_branch)
2727 : {
2728 162 : backslashResult status = PSQL_CMD_SKIP_LINE;
2729 :
2730 162 : if (active_branch)
2731 102 : status = PSQL_CMD_TERMINATE;
2732 :
2733 162 : return status;
2734 : }
2735 :
2736 : /*
2737 : * \r -- reset (clear) the query buffer
2738 : */
2739 : static backslashResult
2740 96 : exec_command_reset(PsqlScanState scan_state, bool active_branch,
2741 : PQExpBuffer query_buf)
2742 : {
2743 96 : if (active_branch)
2744 : {
2745 90 : resetPQExpBuffer(query_buf);
2746 90 : psql_scan_reset(scan_state);
2747 90 : if (!pset.quiet)
2748 60 : puts(_("Query buffer reset (cleared)."));
2749 : }
2750 :
2751 96 : return PSQL_CMD_SKIP_LINE;
2752 : }
2753 :
2754 : /*
2755 : * \s -- save history in a file or show it on the screen
2756 : */
2757 : static backslashResult
2758 6 : exec_command_s(PsqlScanState scan_state, bool active_branch)
2759 : {
2760 6 : bool success = true;
2761 :
2762 6 : if (active_branch)
2763 : {
2764 0 : char *fname = psql_scan_slash_option(scan_state,
2765 : OT_NORMAL, NULL, true);
2766 :
2767 0 : expand_tilde(&fname);
2768 0 : success = printHistory(fname, pset.popt.topt.pager);
2769 0 : if (success && !pset.quiet && fname)
2770 0 : printf(_("Wrote history to file \"%s\".\n"), fname);
2771 0 : if (!fname)
2772 0 : putchar('\n');
2773 0 : free(fname);
2774 : }
2775 : else
2776 6 : ignore_slash_options(scan_state);
2777 :
2778 6 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2779 : }
2780 :
2781 : /*
2782 : * \sendpipeline -- send an extended query to an ongoing pipeline
2783 : */
2784 : static backslashResult
2785 660 : exec_command_sendpipeline(PsqlScanState scan_state, bool active_branch)
2786 : {
2787 660 : backslashResult status = PSQL_CMD_SKIP_LINE;
2788 :
2789 660 : if (active_branch)
2790 : {
2791 654 : if (PQpipelineStatus(pset.db) != PQ_PIPELINE_OFF)
2792 : {
2793 642 : if (pset.send_mode == PSQL_SEND_EXTENDED_QUERY_PREPARED ||
2794 594 : pset.send_mode == PSQL_SEND_EXTENDED_QUERY_PARAMS)
2795 : {
2796 630 : status = PSQL_CMD_SEND;
2797 : }
2798 : else
2799 : {
2800 12 : pg_log_error("\\sendpipeline must be used after \\bind or \\bind_named");
2801 12 : clean_extended_state();
2802 12 : return PSQL_CMD_ERROR;
2803 : }
2804 : }
2805 : else
2806 : {
2807 12 : pg_log_error("\\sendpipeline not allowed outside of pipeline mode");
2808 12 : clean_extended_state();
2809 12 : return PSQL_CMD_ERROR;
2810 : }
2811 : }
2812 : else
2813 6 : ignore_slash_options(scan_state);
2814 :
2815 636 : return status;
2816 : }
2817 :
2818 : /*
2819 : * \set -- set variable
2820 : */
2821 : static backslashResult
2822 962 : exec_command_set(PsqlScanState scan_state, bool active_branch)
2823 : {
2824 962 : bool success = true;
2825 :
2826 962 : if (active_branch)
2827 : {
2828 956 : char *opt0 = psql_scan_slash_option(scan_state,
2829 : OT_NORMAL, NULL, false);
2830 :
2831 956 : if (!opt0)
2832 : {
2833 : /* list all variables */
2834 0 : PrintVariables(pset.vars);
2835 0 : success = true;
2836 : }
2837 : else
2838 : {
2839 : /*
2840 : * Set variable to the concatenation of the arguments.
2841 : */
2842 : char *newval;
2843 : char *opt;
2844 :
2845 956 : opt = psql_scan_slash_option(scan_state,
2846 : OT_NORMAL, NULL, false);
2847 956 : newval = pg_strdup(opt ? opt : "");
2848 956 : free(opt);
2849 :
2850 1436 : while ((opt = psql_scan_slash_option(scan_state,
2851 : OT_NORMAL, NULL, false)))
2852 : {
2853 480 : newval = pg_realloc(newval, strlen(newval) + strlen(opt) + 1);
2854 480 : strcat(newval, opt);
2855 480 : free(opt);
2856 : }
2857 :
2858 956 : if (!SetVariable(pset.vars, opt0, newval))
2859 26 : success = false;
2860 :
2861 956 : free(newval);
2862 : }
2863 956 : free(opt0);
2864 : }
2865 : else
2866 6 : ignore_slash_options(scan_state);
2867 :
2868 962 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2869 : }
2870 :
2871 : /*
2872 : * \setenv -- set environment variable
2873 : */
2874 : static backslashResult
2875 18 : exec_command_setenv(PsqlScanState scan_state, bool active_branch,
2876 : const char *cmd)
2877 : {
2878 18 : bool success = true;
2879 :
2880 18 : if (active_branch)
2881 : {
2882 12 : char *envvar = psql_scan_slash_option(scan_state,
2883 : OT_NORMAL, NULL, false);
2884 12 : char *envval = psql_scan_slash_option(scan_state,
2885 : OT_NORMAL, NULL, false);
2886 :
2887 12 : if (!envvar)
2888 : {
2889 0 : pg_log_error("\\%s: missing required argument", cmd);
2890 0 : success = false;
2891 : }
2892 12 : else if (strchr(envvar, '=') != NULL)
2893 : {
2894 0 : pg_log_error("\\%s: environment variable name must not contain \"=\"",
2895 : cmd);
2896 0 : success = false;
2897 : }
2898 12 : else if (!envval)
2899 : {
2900 : /* No argument - unset the environment variable */
2901 6 : unsetenv(envvar);
2902 6 : success = true;
2903 : }
2904 : else
2905 : {
2906 : /* Set variable to the value of the next argument */
2907 6 : setenv(envvar, envval, 1);
2908 6 : success = true;
2909 : }
2910 12 : free(envvar);
2911 12 : free(envval);
2912 : }
2913 : else
2914 6 : ignore_slash_options(scan_state);
2915 :
2916 18 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2917 : }
2918 :
2919 : /*
2920 : * \sf/\sv -- show a function/view's source code
2921 : */
2922 : static backslashResult
2923 198 : exec_command_sf_sv(PsqlScanState scan_state, bool active_branch,
2924 : const char *cmd, bool is_func)
2925 : {
2926 198 : backslashResult status = PSQL_CMD_SKIP_LINE;
2927 :
2928 198 : if (active_branch)
2929 : {
2930 186 : bool show_linenumbers = (strchr(cmd, '+') != NULL);
2931 : PQExpBuffer buf;
2932 : char *obj_desc;
2933 186 : Oid obj_oid = InvalidOid;
2934 186 : EditableObjectType eot = is_func ? EditableFunction : EditableView;
2935 :
2936 186 : buf = createPQExpBuffer();
2937 186 : obj_desc = psql_scan_slash_option(scan_state,
2938 : OT_WHOLE_LINE, NULL, true);
2939 186 : if (!obj_desc)
2940 : {
2941 0 : if (is_func)
2942 0 : pg_log_error("function name is required");
2943 : else
2944 0 : pg_log_error("view name is required");
2945 0 : status = PSQL_CMD_ERROR;
2946 : }
2947 186 : else if (!lookup_object_oid(eot, obj_desc, &obj_oid))
2948 : {
2949 : /* error already reported */
2950 0 : status = PSQL_CMD_ERROR;
2951 : }
2952 186 : else if (!get_create_object_cmd(eot, obj_oid, buf))
2953 : {
2954 : /* error already reported */
2955 0 : status = PSQL_CMD_ERROR;
2956 : }
2957 : else
2958 : {
2959 : FILE *output;
2960 : bool is_pager;
2961 :
2962 : /* Select output stream: stdout, pager, or file */
2963 186 : if (pset.queryFout == stdout)
2964 : {
2965 : /* count lines in function to see if pager is needed */
2966 186 : int lineno = count_lines_in_buf(buf);
2967 :
2968 186 : output = PageOutput(lineno, &(pset.popt.topt));
2969 186 : is_pager = true;
2970 : }
2971 : else
2972 : {
2973 : /* use previously set output file, without pager */
2974 0 : output = pset.queryFout;
2975 0 : is_pager = false;
2976 : }
2977 :
2978 186 : if (show_linenumbers)
2979 : {
2980 : /* add line numbers */
2981 18 : print_with_linenumbers(output, buf->data, is_func);
2982 : }
2983 : else
2984 : {
2985 : /* just send the definition to output */
2986 168 : fputs(buf->data, output);
2987 : }
2988 :
2989 186 : if (is_pager)
2990 186 : ClosePager(output);
2991 : }
2992 :
2993 186 : free(obj_desc);
2994 186 : destroyPQExpBuffer(buf);
2995 : }
2996 : else
2997 12 : ignore_slash_whole_line(scan_state);
2998 :
2999 198 : return status;
3000 : }
3001 :
3002 : /*
3003 : * \startpipeline -- enter pipeline mode
3004 : */
3005 : static backslashResult
3006 360 : exec_command_startpipeline(PsqlScanState scan_state, bool active_branch)
3007 : {
3008 360 : backslashResult status = PSQL_CMD_SKIP_LINE;
3009 :
3010 360 : if (active_branch)
3011 : {
3012 354 : pset.send_mode = PSQL_SEND_START_PIPELINE_MODE;
3013 354 : status = PSQL_CMD_SEND;
3014 : }
3015 : else
3016 6 : ignore_slash_options(scan_state);
3017 :
3018 360 : return status;
3019 : }
3020 :
3021 : /*
3022 : * \syncpipeline -- send a sync message to an active pipeline
3023 : */
3024 : static backslashResult
3025 132 : exec_command_syncpipeline(PsqlScanState scan_state, bool active_branch)
3026 : {
3027 132 : backslashResult status = PSQL_CMD_SKIP_LINE;
3028 :
3029 132 : if (active_branch)
3030 : {
3031 126 : pset.send_mode = PSQL_SEND_PIPELINE_SYNC;
3032 126 : status = PSQL_CMD_SEND;
3033 : }
3034 : else
3035 6 : ignore_slash_options(scan_state);
3036 :
3037 132 : return status;
3038 : }
3039 :
3040 : /*
3041 : * \endpipeline -- end pipeline mode
3042 : */
3043 : static backslashResult
3044 360 : exec_command_endpipeline(PsqlScanState scan_state, bool active_branch)
3045 : {
3046 360 : backslashResult status = PSQL_CMD_SKIP_LINE;
3047 :
3048 360 : if (active_branch)
3049 : {
3050 354 : pset.send_mode = PSQL_SEND_END_PIPELINE_MODE;
3051 354 : status = PSQL_CMD_SEND;
3052 : }
3053 : else
3054 6 : ignore_slash_options(scan_state);
3055 :
3056 360 : return status;
3057 : }
3058 :
3059 : /*
3060 : * \t -- turn off table headers and row count
3061 : */
3062 : static backslashResult
3063 70 : exec_command_t(PsqlScanState scan_state, bool active_branch)
3064 : {
3065 70 : bool success = true;
3066 :
3067 70 : if (active_branch)
3068 : {
3069 64 : char *opt = psql_scan_slash_option(scan_state,
3070 : OT_NORMAL, NULL, true);
3071 :
3072 64 : success = do_pset("tuples_only", opt, &pset.popt, pset.quiet);
3073 64 : free(opt);
3074 : }
3075 : else
3076 6 : ignore_slash_options(scan_state);
3077 :
3078 70 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
3079 : }
3080 :
3081 : /*
3082 : * \T -- define html <table ...> attributes
3083 : */
3084 : static backslashResult
3085 6 : exec_command_T(PsqlScanState scan_state, bool active_branch)
3086 : {
3087 6 : bool success = true;
3088 :
3089 6 : if (active_branch)
3090 : {
3091 0 : char *value = psql_scan_slash_option(scan_state,
3092 : OT_NORMAL, NULL, false);
3093 :
3094 0 : success = do_pset("tableattr", value, &pset.popt, pset.quiet);
3095 0 : free(value);
3096 : }
3097 : else
3098 6 : ignore_slash_options(scan_state);
3099 :
3100 6 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
3101 : }
3102 :
3103 : /*
3104 : * \timing -- enable/disable timing of queries
3105 : */
3106 : static backslashResult
3107 10 : exec_command_timing(PsqlScanState scan_state, bool active_branch)
3108 : {
3109 10 : bool success = true;
3110 :
3111 10 : if (active_branch)
3112 : {
3113 4 : char *opt = psql_scan_slash_option(scan_state,
3114 : OT_NORMAL, NULL, false);
3115 :
3116 4 : if (opt)
3117 4 : success = ParseVariableBool(opt, "\\timing", &pset.timing);
3118 : else
3119 0 : pset.timing = !pset.timing;
3120 4 : if (!pset.quiet)
3121 : {
3122 0 : if (pset.timing)
3123 0 : puts(_("Timing is on."));
3124 : else
3125 0 : puts(_("Timing is off."));
3126 : }
3127 4 : free(opt);
3128 : }
3129 : else
3130 6 : ignore_slash_options(scan_state);
3131 :
3132 10 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
3133 : }
3134 :
3135 : /*
3136 : * \unset -- unset variable
3137 : */
3138 : static backslashResult
3139 52 : exec_command_unset(PsqlScanState scan_state, bool active_branch,
3140 : const char *cmd)
3141 : {
3142 52 : bool success = true;
3143 :
3144 52 : if (active_branch)
3145 : {
3146 46 : char *opt = psql_scan_slash_option(scan_state,
3147 : OT_NORMAL, NULL, false);
3148 :
3149 46 : if (!opt)
3150 : {
3151 0 : pg_log_error("\\%s: missing required argument", cmd);
3152 0 : success = false;
3153 : }
3154 46 : else if (!SetVariable(pset.vars, opt, NULL))
3155 0 : success = false;
3156 :
3157 46 : free(opt);
3158 : }
3159 : else
3160 6 : ignore_slash_options(scan_state);
3161 :
3162 52 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
3163 : }
3164 :
3165 : /*
3166 : * \w -- write query buffer to file
3167 : */
3168 : static backslashResult
3169 12 : exec_command_write(PsqlScanState scan_state, bool active_branch,
3170 : const char *cmd,
3171 : PQExpBuffer query_buf, PQExpBuffer previous_buf)
3172 : {
3173 12 : backslashResult status = PSQL_CMD_SKIP_LINE;
3174 :
3175 12 : if (active_branch)
3176 : {
3177 0 : char *fname = psql_scan_slash_option(scan_state,
3178 : OT_FILEPIPE, NULL, true);
3179 0 : FILE *fd = NULL;
3180 0 : bool is_pipe = false;
3181 :
3182 0 : if (!query_buf)
3183 : {
3184 0 : pg_log_error("no query buffer");
3185 0 : status = PSQL_CMD_ERROR;
3186 : }
3187 : else
3188 : {
3189 0 : if (!fname)
3190 : {
3191 0 : pg_log_error("\\%s: missing required argument", cmd);
3192 0 : status = PSQL_CMD_ERROR;
3193 : }
3194 : else
3195 : {
3196 0 : expand_tilde(&fname);
3197 0 : if (fname[0] == '|')
3198 : {
3199 0 : is_pipe = true;
3200 0 : fflush(NULL);
3201 0 : disable_sigpipe_trap();
3202 0 : fd = popen(&fname[1], "w");
3203 : }
3204 : else
3205 : {
3206 0 : canonicalize_path_enc(fname, pset.encoding);
3207 0 : fd = fopen(fname, "w");
3208 : }
3209 0 : if (!fd)
3210 : {
3211 0 : pg_log_error("%s: %m", fname);
3212 0 : status = PSQL_CMD_ERROR;
3213 : }
3214 : }
3215 : }
3216 :
3217 0 : if (fd)
3218 : {
3219 : int result;
3220 :
3221 : /*
3222 : * We want to print the same thing \g would execute, but not to
3223 : * change the query buffer state; so we can't use
3224 : * copy_previous_query(). Also, beware of possibility that buffer
3225 : * pointers are NULL.
3226 : */
3227 0 : if (query_buf && query_buf->len > 0)
3228 0 : fprintf(fd, "%s\n", query_buf->data);
3229 0 : else if (previous_buf && previous_buf->len > 0)
3230 0 : fprintf(fd, "%s\n", previous_buf->data);
3231 :
3232 0 : if (is_pipe)
3233 : {
3234 0 : result = pclose(fd);
3235 :
3236 0 : if (result != 0)
3237 : {
3238 0 : pg_log_error("%s: %s", fname, wait_result_to_str(result));
3239 0 : status = PSQL_CMD_ERROR;
3240 : }
3241 0 : SetShellResultVariables(result);
3242 : }
3243 : else
3244 : {
3245 0 : result = fclose(fd);
3246 :
3247 0 : if (result == EOF)
3248 : {
3249 0 : pg_log_error("%s: %m", fname);
3250 0 : status = PSQL_CMD_ERROR;
3251 : }
3252 : }
3253 : }
3254 :
3255 0 : if (is_pipe)
3256 0 : restore_sigpipe_trap();
3257 :
3258 0 : free(fname);
3259 : }
3260 : else
3261 12 : ignore_slash_filepipe(scan_state);
3262 :
3263 12 : return status;
3264 : }
3265 :
3266 : /*
3267 : * \watch -- execute a query every N seconds.
3268 : * Optionally, stop after M iterations.
3269 : */
3270 : static backslashResult
3271 36 : exec_command_watch(PsqlScanState scan_state, bool active_branch,
3272 : PQExpBuffer query_buf, PQExpBuffer previous_buf)
3273 : {
3274 36 : bool success = true;
3275 :
3276 36 : if (active_branch)
3277 : {
3278 30 : bool have_sleep = false;
3279 30 : bool have_iter = false;
3280 30 : bool have_min_rows = false;
3281 30 : double sleep = pset.watch_interval;
3282 30 : int iter = 0;
3283 30 : int min_rows = 0;
3284 :
3285 30 : if (PQpipelineStatus(pset.db) != PQ_PIPELINE_OFF)
3286 : {
3287 6 : pg_log_error("\\watch not allowed in pipeline mode");
3288 6 : clean_extended_state();
3289 6 : success = false;
3290 : }
3291 :
3292 : /*
3293 : * Parse arguments. We allow either an unlabeled interval or
3294 : * "name=value", where name is from the set ('i', 'interval', 'c',
3295 : * 'count', 'm', 'min_rows'). The parsing of interval value should be
3296 : * kept in sync with ParseVariableDouble which is used for setting the
3297 : * default interval value.
3298 : */
3299 66 : while (success)
3300 : {
3301 46 : char *opt = psql_scan_slash_option(scan_state,
3302 : OT_NORMAL, NULL, true);
3303 : char *valptr;
3304 : char *opt_end;
3305 :
3306 46 : if (!opt)
3307 10 : break; /* no more arguments */
3308 :
3309 36 : valptr = strchr(opt, '=');
3310 36 : if (valptr)
3311 : {
3312 : /* Labeled argument */
3313 24 : valptr++;
3314 24 : if (strncmp("i=", opt, strlen("i=")) == 0 ||
3315 18 : strncmp("interval=", opt, strlen("interval=")) == 0)
3316 : {
3317 6 : if (have_sleep)
3318 : {
3319 0 : pg_log_error("\\watch: interval value is specified more than once");
3320 0 : success = false;
3321 : }
3322 : else
3323 : {
3324 6 : have_sleep = true;
3325 6 : errno = 0;
3326 6 : sleep = strtod(valptr, &opt_end);
3327 6 : if (sleep < 0 || *opt_end || errno == ERANGE)
3328 : {
3329 0 : pg_log_error("\\watch: incorrect interval value \"%s\"", valptr);
3330 0 : success = false;
3331 : }
3332 : }
3333 : }
3334 18 : else if (strncmp("c=", opt, strlen("c=")) == 0 ||
3335 8 : strncmp("count=", opt, strlen("count=")) == 0)
3336 : {
3337 10 : if (have_iter)
3338 : {
3339 2 : pg_log_error("\\watch: iteration count is specified more than once");
3340 2 : success = false;
3341 : }
3342 : else
3343 : {
3344 8 : have_iter = true;
3345 8 : errno = 0;
3346 8 : iter = strtoint(valptr, &opt_end, 10);
3347 8 : if (iter <= 0 || *opt_end || errno == ERANGE)
3348 : {
3349 0 : pg_log_error("\\watch: incorrect iteration count \"%s\"", valptr);
3350 0 : success = false;
3351 : }
3352 : }
3353 : }
3354 8 : else if (strncmp("m=", opt, strlen("m=")) == 0 ||
3355 2 : strncmp("min_rows=", opt, strlen("min_rows=")) == 0)
3356 : {
3357 8 : if (have_min_rows)
3358 : {
3359 2 : pg_log_error("\\watch: minimum row count specified more than once");
3360 2 : success = false;
3361 : }
3362 : else
3363 : {
3364 6 : have_min_rows = true;
3365 6 : errno = 0;
3366 6 : min_rows = strtoint(valptr, &opt_end, 10);
3367 6 : if (min_rows <= 0 || *opt_end || errno == ERANGE)
3368 : {
3369 2 : pg_log_error("\\watch: incorrect minimum row count \"%s\"", valptr);
3370 2 : success = false;
3371 : }
3372 : }
3373 : }
3374 : else
3375 : {
3376 0 : pg_log_error("\\watch: unrecognized parameter \"%s\"", opt);
3377 0 : success = false;
3378 : }
3379 : }
3380 : else
3381 : {
3382 : /* Unlabeled argument: take it as interval */
3383 12 : if (have_sleep)
3384 : {
3385 2 : pg_log_error("\\watch: interval value is specified more than once");
3386 2 : success = false;
3387 : }
3388 : else
3389 : {
3390 10 : have_sleep = true;
3391 10 : errno = 0;
3392 10 : sleep = strtod(opt, &opt_end);
3393 10 : if (sleep < 0 || *opt_end || errno == ERANGE)
3394 : {
3395 6 : pg_log_error("\\watch: incorrect interval value \"%s\"", opt);
3396 6 : success = false;
3397 : }
3398 : }
3399 : }
3400 :
3401 36 : free(opt);
3402 : }
3403 :
3404 : /* If we parsed arguments successfully, do the command */
3405 30 : if (success)
3406 : {
3407 : /* If query_buf is empty, recall and execute previous query */
3408 10 : (void) copy_previous_query(query_buf, previous_buf);
3409 :
3410 10 : success = do_watch(query_buf, sleep, iter, min_rows);
3411 : }
3412 :
3413 : /* Reset the query buffer as though for \r */
3414 28 : resetPQExpBuffer(query_buf);
3415 28 : psql_scan_reset(scan_state);
3416 : }
3417 : else
3418 6 : ignore_slash_options(scan_state);
3419 :
3420 34 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
3421 : }
3422 :
3423 : /*
3424 : * \x -- set or toggle expanded table representation
3425 : */
3426 : static backslashResult
3427 76 : exec_command_x(PsqlScanState scan_state, bool active_branch)
3428 : {
3429 76 : bool success = true;
3430 :
3431 76 : if (active_branch)
3432 : {
3433 70 : char *opt = psql_scan_slash_option(scan_state,
3434 : OT_NORMAL, NULL, true);
3435 :
3436 70 : success = do_pset("expanded", opt, &pset.popt, pset.quiet);
3437 70 : free(opt);
3438 : }
3439 : else
3440 6 : ignore_slash_options(scan_state);
3441 :
3442 76 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
3443 : }
3444 :
3445 : /*
3446 : * \z -- list table privileges (equivalent to \dp)
3447 : */
3448 : static backslashResult
3449 30 : exec_command_z(PsqlScanState scan_state, bool active_branch, const char *cmd)
3450 : {
3451 30 : bool success = true;
3452 :
3453 30 : if (active_branch)
3454 : {
3455 : char *pattern;
3456 : bool show_system;
3457 : unsigned short int save_expanded;
3458 :
3459 24 : pattern = psql_scan_slash_option(scan_state,
3460 : OT_NORMAL, NULL, true);
3461 :
3462 24 : show_system = strchr(cmd, 'S') ? true : false;
3463 :
3464 : /* if 'x' option specified, force expanded mode */
3465 24 : save_expanded = pset.popt.topt.expanded;
3466 24 : if (strchr(cmd, 'x'))
3467 6 : pset.popt.topt.expanded = 1;
3468 :
3469 24 : success = permissionsList(pattern, show_system);
3470 :
3471 : /* restore original expanded mode */
3472 24 : pset.popt.topt.expanded = save_expanded;
3473 :
3474 24 : free(pattern);
3475 : }
3476 : else
3477 6 : ignore_slash_options(scan_state);
3478 :
3479 30 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
3480 : }
3481 :
3482 : /*
3483 : * \! -- execute shell command
3484 : */
3485 : static backslashResult
3486 6 : exec_command_shell_escape(PsqlScanState scan_state, bool active_branch)
3487 : {
3488 6 : bool success = true;
3489 :
3490 6 : if (active_branch)
3491 : {
3492 0 : char *opt = psql_scan_slash_option(scan_state,
3493 : OT_WHOLE_LINE, NULL, false);
3494 :
3495 0 : success = do_shell(opt);
3496 0 : free(opt);
3497 : }
3498 : else
3499 6 : ignore_slash_whole_line(scan_state);
3500 :
3501 6 : return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
3502 : }
3503 :
3504 : /*
3505 : * \? -- print help about backslash commands
3506 : */
3507 : static backslashResult
3508 6 : exec_command_slash_command_help(PsqlScanState scan_state, bool active_branch)
3509 : {
3510 6 : if (active_branch)
3511 : {
3512 0 : char *opt0 = psql_scan_slash_option(scan_state,
3513 : OT_NORMAL, NULL, false);
3514 :
3515 0 : if (!opt0 || strcmp(opt0, "commands") == 0)
3516 0 : slashUsage(pset.popt.topt.pager);
3517 0 : else if (strcmp(opt0, "options") == 0)
3518 0 : usage(pset.popt.topt.pager);
3519 0 : else if (strcmp(opt0, "variables") == 0)
3520 0 : helpVariables(pset.popt.topt.pager);
3521 : else
3522 0 : slashUsage(pset.popt.topt.pager);
3523 :
3524 0 : free(opt0);
3525 : }
3526 : else
3527 6 : ignore_slash_options(scan_state);
3528 :
3529 6 : return PSQL_CMD_SKIP_LINE;
3530 : }
3531 :
3532 :
3533 : /*
3534 : * Read and interpret an argument to the \connect slash command.
3535 : *
3536 : * Returns a malloc'd string, or NULL if no/empty argument.
3537 : */
3538 : static char *
3539 1342 : read_connect_arg(PsqlScanState scan_state)
3540 : {
3541 : char *result;
3542 : char quote;
3543 :
3544 : /*
3545 : * Ideally we should treat the arguments as SQL identifiers. But for
3546 : * backwards compatibility with 7.2 and older pg_dump files, we have to
3547 : * take unquoted arguments verbatim (don't downcase them). For now,
3548 : * double-quoted arguments may be stripped of double quotes (as if SQL
3549 : * identifiers). By 7.4 or so, pg_dump files can be expected to
3550 : * double-quote all mixed-case \connect arguments, and then we can get rid
3551 : * of OT_SQLIDHACK.
3552 : */
3553 1342 : result = psql_scan_slash_option(scan_state, OT_SQLIDHACK, "e, true);
3554 :
3555 1342 : if (!result)
3556 1078 : return NULL;
3557 :
3558 264 : if (quote)
3559 24 : return result;
3560 :
3561 240 : if (*result == '\0' || strcmp(result, "-") == 0)
3562 : {
3563 200 : free(result);
3564 200 : return NULL;
3565 : }
3566 :
3567 40 : return result;
3568 : }
3569 :
3570 : /*
3571 : * Read a boolean expression, return it as a PQExpBuffer string.
3572 : *
3573 : * Note: anything more or less than one token will certainly fail to be
3574 : * parsed by ParseVariableBool, so we don't worry about complaining here.
3575 : * This routine's return data structure will need to be rethought anyway
3576 : * to support likely future extensions such as "\if defined VARNAME".
3577 : */
3578 : static PQExpBuffer
3579 222 : gather_boolean_expression(PsqlScanState scan_state)
3580 : {
3581 222 : PQExpBuffer exp_buf = createPQExpBuffer();
3582 222 : int num_options = 0;
3583 : char *value;
3584 :
3585 : /* collect all arguments for the conditional command into exp_buf */
3586 456 : while ((value = psql_scan_slash_option(scan_state,
3587 : OT_NORMAL, NULL, false)) != NULL)
3588 : {
3589 : /* add spaces between tokens */
3590 234 : if (num_options > 0)
3591 12 : appendPQExpBufferChar(exp_buf, ' ');
3592 234 : appendPQExpBufferStr(exp_buf, value);
3593 234 : num_options++;
3594 234 : free(value);
3595 : }
3596 :
3597 222 : return exp_buf;
3598 : }
3599 :
3600 : /*
3601 : * Read a boolean expression, return true if the expression
3602 : * was a valid boolean expression that evaluated to true.
3603 : * Otherwise return false.
3604 : *
3605 : * Note: conditional stack's top state must be active, else lexer will
3606 : * fail to expand variables and backticks.
3607 : */
3608 : static bool
3609 204 : is_true_boolean_expression(PsqlScanState scan_state, const char *name)
3610 : {
3611 204 : PQExpBuffer buf = gather_boolean_expression(scan_state);
3612 204 : bool value = false;
3613 204 : bool success = ParseVariableBool(buf->data, name, &value);
3614 :
3615 204 : destroyPQExpBuffer(buf);
3616 204 : return success && value;
3617 : }
3618 :
3619 : /*
3620 : * Read a boolean expression, but do nothing with it.
3621 : *
3622 : * Note: conditional stack's top state must be INACTIVE, else lexer will
3623 : * expand variables and backticks, which we do not want here.
3624 : */
3625 : static void
3626 18 : ignore_boolean_expression(PsqlScanState scan_state)
3627 : {
3628 18 : PQExpBuffer buf = gather_boolean_expression(scan_state);
3629 :
3630 18 : destroyPQExpBuffer(buf);
3631 18 : }
3632 :
3633 : /*
3634 : * Read and discard "normal" slash command options.
3635 : *
3636 : * This should be used for inactive-branch processing of any slash command
3637 : * that eats one or more OT_NORMAL, OT_SQLID, or OT_SQLIDHACK parameters.
3638 : * We don't need to worry about exactly how many it would eat, since the
3639 : * cleanup logic in HandleSlashCmds would silently discard any extras anyway.
3640 : */
3641 : static void
3642 372 : ignore_slash_options(PsqlScanState scan_state)
3643 : {
3644 : char *arg;
3645 :
3646 834 : while ((arg = psql_scan_slash_option(scan_state,
3647 : OT_NORMAL, NULL, false)) != NULL)
3648 462 : free(arg);
3649 372 : }
3650 :
3651 : /*
3652 : * Read and discard FILEPIPE slash command argument.
3653 : *
3654 : * This *MUST* be used for inactive-branch processing of any slash command
3655 : * that takes an OT_FILEPIPE option. Otherwise we might consume a different
3656 : * amount of option text in active and inactive cases.
3657 : */
3658 : static void
3659 18 : ignore_slash_filepipe(PsqlScanState scan_state)
3660 : {
3661 18 : char *arg = psql_scan_slash_option(scan_state,
3662 : OT_FILEPIPE, NULL, false);
3663 :
3664 18 : free(arg);
3665 18 : }
3666 :
3667 : /*
3668 : * Read and discard whole-line slash command argument.
3669 : *
3670 : * This *MUST* be used for inactive-branch processing of any slash command
3671 : * that takes an OT_WHOLE_LINE option. Otherwise we might consume a different
3672 : * amount of option text in active and inactive cases.
3673 : *
3674 : * Note: although callers might pass "semicolon" as either true or false,
3675 : * we need not duplicate that here, since it doesn't affect the amount of
3676 : * input text consumed.
3677 : */
3678 : static void
3679 42 : ignore_slash_whole_line(PsqlScanState scan_state)
3680 : {
3681 42 : char *arg = psql_scan_slash_option(scan_state,
3682 : OT_WHOLE_LINE, NULL, false);
3683 :
3684 42 : free(arg);
3685 42 : }
3686 :
3687 : /*
3688 : * Return true if the command given is a branching command.
3689 : */
3690 : static bool
3691 0 : is_branching_command(const char *cmd)
3692 : {
3693 0 : return (strcmp(cmd, "if") == 0 ||
3694 0 : strcmp(cmd, "elif") == 0 ||
3695 0 : strcmp(cmd, "else") == 0 ||
3696 0 : strcmp(cmd, "endif") == 0);
3697 : }
3698 :
3699 : /*
3700 : * Prepare to possibly restore query buffer to its current state
3701 : * (cf. discard_query_text).
3702 : *
3703 : * We need to remember the length of the query buffer, and the lexer's
3704 : * notion of the parenthesis nesting depth.
3705 : */
3706 : static void
3707 252 : save_query_text_state(PsqlScanState scan_state, ConditionalStack cstack,
3708 : PQExpBuffer query_buf)
3709 : {
3710 252 : if (query_buf)
3711 252 : conditional_stack_set_query_len(cstack, query_buf->len);
3712 252 : conditional_stack_set_paren_depth(cstack,
3713 : psql_scan_get_paren_depth(scan_state));
3714 252 : }
3715 :
3716 : /*
3717 : * Discard any query text absorbed during an inactive conditional branch.
3718 : *
3719 : * We must discard data that was appended to query_buf during an inactive
3720 : * \if branch. We don't have to do anything there if there's no query_buf.
3721 : *
3722 : * Also, reset the lexer state to the same paren depth there was before.
3723 : * (The rest of its state doesn't need attention, since we could not be
3724 : * inside a comment or literal or partial token.)
3725 : */
3726 : static void
3727 216 : discard_query_text(PsqlScanState scan_state, ConditionalStack cstack,
3728 : PQExpBuffer query_buf)
3729 : {
3730 216 : if (query_buf)
3731 : {
3732 216 : int new_len = conditional_stack_get_query_len(cstack);
3733 :
3734 : Assert(new_len >= 0 && new_len <= query_buf->len);
3735 216 : query_buf->len = new_len;
3736 216 : query_buf->data[new_len] = '\0';
3737 : }
3738 216 : psql_scan_set_paren_depth(scan_state,
3739 : conditional_stack_get_paren_depth(cstack));
3740 216 : }
3741 :
3742 : /*
3743 : * If query_buf is empty, copy previous_buf into it.
3744 : *
3745 : * This is used by various slash commands for which re-execution of a
3746 : * previous query is a common usage. For convenience, we allow the
3747 : * case of query_buf == NULL (and do nothing).
3748 : *
3749 : * Returns "true" if the previous query was copied into the query
3750 : * buffer, else "false".
3751 : */
3752 : static bool
3753 3172 : copy_previous_query(PQExpBuffer query_buf, PQExpBuffer previous_buf)
3754 : {
3755 3172 : if (query_buf && query_buf->len == 0)
3756 : {
3757 1380 : appendPQExpBufferStr(query_buf, previous_buf->data);
3758 1380 : return true;
3759 : }
3760 1792 : return false;
3761 : }
3762 :
3763 : /*
3764 : * Ask the user for a password; 'username' is the username the
3765 : * password is for, if one has been explicitly specified.
3766 : * Returns a malloc'd string.
3767 : * If 'canceled' is provided, *canceled will be set to true if the prompt
3768 : * is canceled via SIGINT, and to false otherwise.
3769 : */
3770 : static char *
3771 0 : prompt_for_password(const char *username, bool *canceled)
3772 : {
3773 : char *result;
3774 : PromptInterruptContext prompt_ctx;
3775 :
3776 : /* Set up to let SIGINT cancel simple_prompt_extended() */
3777 0 : prompt_ctx.jmpbuf = sigint_interrupt_jmp;
3778 0 : prompt_ctx.enabled = &sigint_interrupt_enabled;
3779 0 : prompt_ctx.canceled = false;
3780 :
3781 0 : if (username == NULL || username[0] == '\0')
3782 0 : result = simple_prompt_extended("Password: ", false, &prompt_ctx);
3783 : else
3784 : {
3785 : char *prompt_text;
3786 :
3787 0 : prompt_text = psprintf(_("Password for user %s: "), username);
3788 0 : result = simple_prompt_extended(prompt_text, false, &prompt_ctx);
3789 0 : free(prompt_text);
3790 : }
3791 :
3792 0 : if (canceled)
3793 0 : *canceled = prompt_ctx.canceled;
3794 :
3795 0 : return result;
3796 : }
3797 :
3798 : static bool
3799 48 : param_is_newly_set(const char *old_val, const char *new_val)
3800 : {
3801 48 : if (new_val == NULL)
3802 0 : return false;
3803 :
3804 48 : if (old_val == NULL || strcmp(old_val, new_val) != 0)
3805 0 : return true;
3806 :
3807 48 : return false;
3808 : }
3809 :
3810 : /*
3811 : * do_connect -- handler for \connect
3812 : *
3813 : * Connects to a database with given parameters. If we are told to re-use
3814 : * parameters, parameters from the previous connection are used where the
3815 : * command's own options do not supply a value. Otherwise, libpq defaults
3816 : * are used.
3817 : *
3818 : * In interactive mode, if connection fails with the given parameters,
3819 : * the old connection will be kept.
3820 : */
3821 : static bool
3822 330 : do_connect(enum trivalue reuse_previous_specification,
3823 : char *dbname, char *user, char *host, char *port)
3824 : {
3825 330 : PGconn *o_conn = pset.db,
3826 330 : *n_conn = NULL;
3827 : PQconninfoOption *cinfo;
3828 330 : int nconnopts = 0;
3829 330 : bool same_host = false;
3830 330 : char *password = NULL;
3831 : char *client_encoding;
3832 330 : bool success = true;
3833 330 : bool keep_password = true;
3834 : bool has_connection_string;
3835 : bool reuse_previous;
3836 :
3837 330 : has_connection_string = dbname ?
3838 330 : recognized_connection_string(dbname) : false;
3839 :
3840 : /* Complain if we have additional arguments after a connection string. */
3841 330 : if (has_connection_string && (user || host || port))
3842 : {
3843 0 : pg_log_error("Do not give user, host, or port separately when using a connection string");
3844 0 : return false;
3845 : }
3846 :
3847 330 : switch (reuse_previous_specification)
3848 : {
3849 22 : case TRI_YES:
3850 22 : reuse_previous = true;
3851 22 : break;
3852 0 : case TRI_NO:
3853 0 : reuse_previous = false;
3854 0 : break;
3855 308 : default:
3856 308 : reuse_previous = !has_connection_string;
3857 308 : break;
3858 : }
3859 :
3860 : /*
3861 : * If we intend to re-use connection parameters, collect them out of the
3862 : * old connection, then replace individual values as necessary. (We may
3863 : * need to resort to looking at pset.dead_conn, if the connection died
3864 : * previously.) Otherwise, obtain a PQconninfoOption array containing
3865 : * libpq's defaults, and modify that. Note this function assumes that
3866 : * PQconninfo, PQconndefaults, and PQconninfoParse will all produce arrays
3867 : * containing the same options in the same order.
3868 : */
3869 330 : if (reuse_previous)
3870 : {
3871 330 : if (o_conn)
3872 330 : cinfo = PQconninfo(o_conn);
3873 0 : else if (pset.dead_conn)
3874 0 : cinfo = PQconninfo(pset.dead_conn);
3875 : else
3876 : {
3877 : /* This is reachable after a non-interactive \connect failure */
3878 0 : pg_log_error("No database connection exists to re-use parameters from");
3879 0 : return false;
3880 : }
3881 : }
3882 : else
3883 0 : cinfo = PQconndefaults();
3884 :
3885 330 : if (cinfo)
3886 : {
3887 330 : if (has_connection_string)
3888 : {
3889 : /* Parse the connstring and insert values into cinfo */
3890 : PQconninfoOption *replcinfo;
3891 : char *errmsg;
3892 :
3893 22 : replcinfo = PQconninfoParse(dbname, &errmsg);
3894 22 : if (replcinfo)
3895 : {
3896 : PQconninfoOption *ci;
3897 : PQconninfoOption *replci;
3898 22 : bool have_password = false;
3899 :
3900 22 : for (ci = cinfo, replci = replcinfo;
3901 1056 : ci->keyword && replci->keyword;
3902 1034 : ci++, replci++)
3903 : {
3904 : Assert(strcmp(ci->keyword, replci->keyword) == 0);
3905 : /* Insert value from connstring if one was provided */
3906 1034 : if (replci->val)
3907 : {
3908 : /*
3909 : * We know that both val strings were allocated by
3910 : * libpq, so the least messy way to avoid memory leaks
3911 : * is to swap them.
3912 : */
3913 28 : char *swap = replci->val;
3914 :
3915 28 : replci->val = ci->val;
3916 28 : ci->val = swap;
3917 :
3918 : /*
3919 : * Check whether connstring provides options affecting
3920 : * password re-use. While any change in user, host,
3921 : * hostaddr, or port causes us to ignore the old
3922 : * connection's password, we don't force that for
3923 : * dbname, since passwords aren't database-specific.
3924 : */
3925 28 : if (replci->val == NULL ||
3926 28 : strcmp(ci->val, replci->val) != 0)
3927 : {
3928 28 : if (strcmp(replci->keyword, "user") == 0 ||
3929 22 : strcmp(replci->keyword, "host") == 0 ||
3930 22 : strcmp(replci->keyword, "hostaddr") == 0 ||
3931 22 : strcmp(replci->keyword, "port") == 0)
3932 6 : keep_password = false;
3933 : }
3934 : /* Also note whether connstring contains a password. */
3935 28 : if (strcmp(replci->keyword, "password") == 0)
3936 0 : have_password = true;
3937 : }
3938 1006 : else if (!reuse_previous)
3939 : {
3940 : /*
3941 : * When we have a connstring and are not re-using
3942 : * parameters, swap *all* entries, even those not set
3943 : * by the connstring. This avoids absorbing
3944 : * environment-dependent defaults from the result of
3945 : * PQconndefaults(). We don't want to do that because
3946 : * they'd override service-file entries if the
3947 : * connstring specifies a service parameter, whereas
3948 : * the priority should be the other way around. libpq
3949 : * can certainly recompute any defaults we don't pass
3950 : * here. (In this situation, it's a bit wasteful to
3951 : * have called PQconndefaults() at all, but not doing
3952 : * so would require yet another major code path here.)
3953 : */
3954 0 : replci->val = ci->val;
3955 0 : ci->val = NULL;
3956 : }
3957 : }
3958 : Assert(ci->keyword == NULL && replci->keyword == NULL);
3959 :
3960 : /* While here, determine how many option slots there are */
3961 22 : nconnopts = ci - cinfo;
3962 :
3963 22 : PQconninfoFree(replcinfo);
3964 :
3965 : /*
3966 : * If the connstring contains a password, tell the loop below
3967 : * that we may use it, regardless of other settings (i.e.,
3968 : * cinfo's password is no longer an "old" password).
3969 : */
3970 22 : if (have_password)
3971 0 : keep_password = true;
3972 :
3973 : /* Don't let code below try to inject dbname into params. */
3974 22 : dbname = NULL;
3975 : }
3976 : else
3977 : {
3978 : /* PQconninfoParse failed */
3979 0 : if (errmsg)
3980 : {
3981 0 : pg_log_error("%s", errmsg);
3982 0 : PQfreemem(errmsg);
3983 : }
3984 : else
3985 0 : pg_log_error("out of memory");
3986 0 : success = false;
3987 : }
3988 : }
3989 : else
3990 : {
3991 : /*
3992 : * If dbname isn't a connection string, then we'll inject it and
3993 : * the other parameters into the keyword array below. (We can't
3994 : * easily insert them into the cinfo array because of memory
3995 : * management issues: PQconninfoFree would misbehave on Windows.)
3996 : * However, to avoid dependencies on the order in which parameters
3997 : * appear in the array, make a preliminary scan to set
3998 : * keep_password and same_host correctly.
3999 : *
4000 : * While any change in user, host, or port causes us to ignore the
4001 : * old connection's password, we don't force that for dbname,
4002 : * since passwords aren't database-specific.
4003 : */
4004 : PQconninfoOption *ci;
4005 :
4006 14784 : for (ci = cinfo; ci->keyword; ci++)
4007 : {
4008 14476 : if (user && strcmp(ci->keyword, "user") == 0)
4009 : {
4010 8 : if (!(ci->val && strcmp(user, ci->val) == 0))
4011 8 : keep_password = false;
4012 : }
4013 14468 : else if (host && strcmp(ci->keyword, "host") == 0)
4014 : {
4015 0 : if (ci->val && strcmp(host, ci->val) == 0)
4016 0 : same_host = true;
4017 : else
4018 0 : keep_password = false;
4019 : }
4020 14468 : else if (port && strcmp(ci->keyword, "port") == 0)
4021 : {
4022 0 : if (!(ci->val && strcmp(port, ci->val) == 0))
4023 0 : keep_password = false;
4024 : }
4025 : }
4026 :
4027 : /* While here, determine how many option slots there are */
4028 308 : nconnopts = ci - cinfo;
4029 : }
4030 : }
4031 : else
4032 : {
4033 : /* We failed to create the cinfo structure */
4034 0 : pg_log_error("out of memory");
4035 0 : success = false;
4036 : }
4037 :
4038 : /*
4039 : * If the user asked to be prompted for a password, ask for one now. If
4040 : * not, use the password from the old connection, provided the username
4041 : * etc have not changed. Otherwise, try to connect without a password
4042 : * first, and then ask for a password if needed.
4043 : *
4044 : * XXX: this behavior leads to spurious connection attempts recorded in
4045 : * the postmaster's log. But libpq offers no API that would let us obtain
4046 : * a password and then continue with the first connection attempt.
4047 : */
4048 330 : if (pset.getPassword == TRI_YES && success)
4049 : {
4050 0 : bool canceled = false;
4051 :
4052 : /*
4053 : * If a connstring or URI is provided, we don't know which username
4054 : * will be used, since we haven't dug that out of the connstring.
4055 : * Don't risk issuing a misleading prompt. As in startup.c, it does
4056 : * not seem worth working harder, since this getPassword setting is
4057 : * normally only used in noninteractive cases.
4058 : */
4059 0 : password = prompt_for_password(has_connection_string ? NULL : user,
4060 : &canceled);
4061 0 : success = !canceled;
4062 : }
4063 :
4064 : /*
4065 : * Consider whether to force client_encoding to "auto" (overriding
4066 : * anything in the connection string). We do so if we have a terminal
4067 : * connection and there is no PGCLIENTENCODING environment setting.
4068 : */
4069 330 : if (pset.notty || getenv("PGCLIENTENCODING"))
4070 330 : client_encoding = NULL;
4071 : else
4072 0 : client_encoding = "auto";
4073 :
4074 : /* Loop till we have a connection or fail, which we might've already */
4075 330 : while (success)
4076 : {
4077 330 : const char **keywords = pg_malloc((nconnopts + 1) * sizeof(*keywords));
4078 330 : const char **values = pg_malloc((nconnopts + 1) * sizeof(*values));
4079 330 : int paramnum = 0;
4080 : PQconninfoOption *ci;
4081 :
4082 : /*
4083 : * Copy non-default settings into the PQconnectdbParams parameter
4084 : * arrays; but inject any values specified old-style, as well as any
4085 : * interactively-obtained password, and a couple of fields we want to
4086 : * set forcibly.
4087 : *
4088 : * If you change this code, see also the initial-connection code in
4089 : * main().
4090 : */
4091 15840 : for (ci = cinfo; ci->keyword; ci++)
4092 : {
4093 15510 : keywords[paramnum] = ci->keyword;
4094 :
4095 15510 : if (dbname && strcmp(ci->keyword, "dbname") == 0)
4096 12 : values[paramnum++] = dbname;
4097 15498 : else if (user && strcmp(ci->keyword, "user") == 0)
4098 8 : values[paramnum++] = user;
4099 15490 : else if (host && strcmp(ci->keyword, "host") == 0)
4100 0 : values[paramnum++] = host;
4101 15490 : else if (host && !same_host && strcmp(ci->keyword, "hostaddr") == 0)
4102 : {
4103 : /* If we're changing the host value, drop any old hostaddr */
4104 0 : values[paramnum++] = NULL;
4105 : }
4106 15490 : else if (port && strcmp(ci->keyword, "port") == 0)
4107 0 : values[paramnum++] = port;
4108 : /* If !keep_password, we unconditionally drop old password */
4109 15490 : else if ((password || !keep_password) &&
4110 650 : strcmp(ci->keyword, "password") == 0)
4111 14 : values[paramnum++] = password;
4112 15476 : else if (strcmp(ci->keyword, "fallback_application_name") == 0)
4113 330 : values[paramnum++] = pset.progname;
4114 15146 : else if (client_encoding &&
4115 0 : strcmp(ci->keyword, "client_encoding") == 0)
4116 0 : values[paramnum++] = client_encoding;
4117 15146 : else if (ci->val)
4118 6274 : values[paramnum++] = ci->val;
4119 : /* else, don't bother making libpq parse this keyword */
4120 : }
4121 : /* add array terminator */
4122 330 : keywords[paramnum] = NULL;
4123 330 : values[paramnum] = NULL;
4124 :
4125 : /* Note we do not want libpq to re-expand the dbname parameter */
4126 330 : n_conn = PQconnectStartParams(keywords, values, false);
4127 :
4128 330 : pg_free(keywords);
4129 330 : pg_free(values);
4130 :
4131 330 : wait_until_connected(n_conn);
4132 330 : if (PQstatus(n_conn) == CONNECTION_OK)
4133 330 : break;
4134 :
4135 : /*
4136 : * Connection attempt failed; either retry the connection attempt with
4137 : * a new password, or give up.
4138 : */
4139 0 : if (!password && PQconnectionNeedsPassword(n_conn) && pset.getPassword != TRI_NO)
4140 : {
4141 0 : bool canceled = false;
4142 :
4143 : /*
4144 : * Prompt for password using the username we actually connected
4145 : * with --- it might've come out of "dbname" rather than "user".
4146 : */
4147 0 : password = prompt_for_password(PQuser(n_conn), &canceled);
4148 0 : PQfinish(n_conn);
4149 0 : n_conn = NULL;
4150 0 : success = !canceled;
4151 0 : continue;
4152 : }
4153 :
4154 : /*
4155 : * We'll report the error below ... unless n_conn is NULL, indicating
4156 : * that libpq didn't have enough memory to make a PGconn.
4157 : */
4158 0 : if (n_conn == NULL)
4159 0 : pg_log_error("out of memory");
4160 :
4161 0 : success = false;
4162 : } /* end retry loop */
4163 :
4164 : /* Release locally allocated data, whether we succeeded or not */
4165 330 : pg_free(password);
4166 330 : PQconninfoFree(cinfo);
4167 :
4168 330 : if (!success)
4169 : {
4170 : /*
4171 : * Failed to connect to the database. In interactive mode, keep the
4172 : * previous connection to the DB; in scripting mode, close our
4173 : * previous connection as well.
4174 : */
4175 0 : if (pset.cur_cmd_interactive)
4176 : {
4177 0 : if (n_conn)
4178 : {
4179 0 : pg_log_info("%s", PQerrorMessage(n_conn));
4180 0 : PQfinish(n_conn);
4181 : }
4182 :
4183 : /* pset.db is left unmodified */
4184 0 : if (o_conn)
4185 0 : pg_log_info("Previous connection kept");
4186 : }
4187 : else
4188 : {
4189 0 : if (n_conn)
4190 : {
4191 0 : pg_log_error("\\connect: %s", PQerrorMessage(n_conn));
4192 0 : PQfinish(n_conn);
4193 : }
4194 :
4195 0 : if (o_conn)
4196 : {
4197 : /*
4198 : * Transition to having no connection.
4199 : *
4200 : * Unlike CheckConnection(), we close the old connection
4201 : * immediately to prevent its parameters from being re-used.
4202 : * This is so that a script cannot accidentally reuse
4203 : * parameters it did not expect to. Otherwise, the state
4204 : * cleanup should be the same as in CheckConnection().
4205 : */
4206 0 : PQfinish(o_conn);
4207 0 : pset.db = NULL;
4208 0 : ResetCancelConn();
4209 0 : UnsyncVariables();
4210 : }
4211 :
4212 : /* On the same reasoning, release any dead_conn to prevent reuse */
4213 0 : if (pset.dead_conn)
4214 : {
4215 0 : PQfinish(pset.dead_conn);
4216 0 : pset.dead_conn = NULL;
4217 : }
4218 : }
4219 :
4220 0 : return false;
4221 : }
4222 :
4223 : /*
4224 : * Replace the old connection with the new one, and update
4225 : * connection-dependent variables. Keep the resynchronization logic in
4226 : * sync with CheckConnection().
4227 : */
4228 330 : PQsetNoticeProcessor(n_conn, NoticeProcessor, NULL);
4229 330 : pset.db = n_conn;
4230 330 : SyncVariables();
4231 330 : connection_warnings(false); /* Must be after SyncVariables */
4232 :
4233 : /* Tell the user about the new connection */
4234 330 : if (!pset.quiet)
4235 : {
4236 48 : if (!o_conn ||
4237 48 : param_is_newly_set(PQhost(o_conn), PQhost(pset.db)) ||
4238 24 : param_is_newly_set(PQport(o_conn), PQport(pset.db)))
4239 0 : {
4240 0 : char *connhost = PQhost(pset.db);
4241 0 : char *hostaddr = PQhostaddr(pset.db);
4242 :
4243 0 : if (is_unixsock_path(connhost))
4244 : {
4245 : /* hostaddr overrides connhost */
4246 0 : if (hostaddr && *hostaddr)
4247 0 : printf(_("You are now connected to database \"%s\" as user \"%s\" on address \"%s\" at port \"%s\".\n"),
4248 : PQdb(pset.db), PQuser(pset.db), hostaddr, PQport(pset.db));
4249 : else
4250 0 : printf(_("You are now connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\".\n"),
4251 : PQdb(pset.db), PQuser(pset.db), connhost, PQport(pset.db));
4252 : }
4253 : else
4254 : {
4255 0 : if (hostaddr && *hostaddr && strcmp(connhost, hostaddr) != 0)
4256 0 : printf(_("You are now connected to database \"%s\" as user \"%s\" on host \"%s\" (address \"%s\") at port \"%s\".\n"),
4257 : PQdb(pset.db), PQuser(pset.db), connhost, hostaddr, PQport(pset.db));
4258 : else
4259 0 : printf(_("You are now connected to database \"%s\" as user \"%s\" on host \"%s\" at port \"%s\".\n"),
4260 : PQdb(pset.db), PQuser(pset.db), connhost, PQport(pset.db));
4261 : }
4262 : }
4263 : else
4264 24 : printf(_("You are now connected to database \"%s\" as user \"%s\".\n"),
4265 : PQdb(pset.db), PQuser(pset.db));
4266 : }
4267 :
4268 : /* Drop no-longer-needed connection(s) */
4269 330 : if (o_conn)
4270 330 : PQfinish(o_conn);
4271 330 : if (pset.dead_conn)
4272 : {
4273 0 : PQfinish(pset.dead_conn);
4274 0 : pset.dead_conn = NULL;
4275 : }
4276 :
4277 330 : return true;
4278 : }
4279 :
4280 : /*
4281 : * Processes the connection sequence described by PQconnectStartParams(). Don't
4282 : * worry about reporting errors in this function. Our caller will check the
4283 : * connection's status, and report appropriately.
4284 : */
4285 : static void
4286 330 : wait_until_connected(PGconn *conn)
4287 : {
4288 330 : bool forRead = false;
4289 :
4290 : while (true)
4291 344 : {
4292 : int rc;
4293 : int sock;
4294 : pg_usec_time_t end_time;
4295 :
4296 : /*
4297 : * On every iteration of the connection sequence, let's check if the
4298 : * user has requested a cancellation.
4299 : */
4300 674 : if (cancel_pressed)
4301 0 : break;
4302 :
4303 : /*
4304 : * Do not assume that the socket remains the same across
4305 : * PQconnectPoll() calls.
4306 : */
4307 674 : sock = PQsocket(conn);
4308 674 : if (sock == -1)
4309 0 : break;
4310 :
4311 : /*
4312 : * If the user sends SIGINT between the cancel_pressed check, and
4313 : * polling of the socket, it will not be recognized. Instead, we will
4314 : * just wait until the next step in the connection sequence or
4315 : * forever, which might require users to send SIGTERM or SIGQUIT.
4316 : *
4317 : * Some solutions would include the "self-pipe trick," using
4318 : * pselect(2) and ppoll(2), or using a timeout.
4319 : *
4320 : * The self-pipe trick requires a bit of code to setup. pselect(2) and
4321 : * ppoll(2) are not on all the platforms we support. The simplest
4322 : * solution happens to just be adding a timeout, so let's wait for 1
4323 : * second and check cancel_pressed again.
4324 : */
4325 674 : end_time = PQgetCurrentTimeUSec() + 1000000;
4326 674 : rc = PQsocketPoll(sock, forRead, !forRead, end_time);
4327 674 : if (rc == -1)
4328 0 : return;
4329 :
4330 674 : switch (PQconnectPoll(conn))
4331 : {
4332 330 : case PGRES_POLLING_OK:
4333 : case PGRES_POLLING_FAILED:
4334 330 : return;
4335 344 : case PGRES_POLLING_READING:
4336 344 : forRead = true;
4337 344 : continue;
4338 0 : case PGRES_POLLING_WRITING:
4339 0 : forRead = false;
4340 0 : continue;
4341 : case PGRES_POLLING_ACTIVE:
4342 : pg_unreachable();
4343 : }
4344 : }
4345 : }
4346 :
4347 : void
4348 334 : connection_warnings(bool in_startup)
4349 : {
4350 334 : if (!pset.quiet && !pset.notty)
4351 : {
4352 4 : int client_ver = PG_VERSION_NUM;
4353 : char cverbuf[32];
4354 : char sverbuf[32];
4355 :
4356 4 : if (pset.sversion != client_ver)
4357 : {
4358 : const char *server_version;
4359 :
4360 : /* Try to get full text form, might include "devel" etc */
4361 0 : server_version = PQparameterStatus(pset.db, "server_version");
4362 : /* Otherwise fall back on pset.sversion */
4363 0 : if (!server_version)
4364 : {
4365 0 : formatPGVersionNumber(pset.sversion, true,
4366 : sverbuf, sizeof(sverbuf));
4367 0 : server_version = sverbuf;
4368 : }
4369 :
4370 0 : printf(_("%s (%s, server %s)\n"),
4371 : pset.progname, PG_VERSION, server_version);
4372 : }
4373 : /* For version match, only print psql banner on startup. */
4374 4 : else if (in_startup)
4375 4 : printf("%s (%s)\n", pset.progname, PG_VERSION);
4376 :
4377 : /*
4378 : * Warn if server's major version is newer than ours, or if server
4379 : * predates our support cutoff (currently 9.2).
4380 : */
4381 4 : if (pset.sversion / 100 > client_ver / 100 ||
4382 4 : pset.sversion < 90200)
4383 0 : printf(_("WARNING: %s major version %s, server major version %s.\n"
4384 : " Some psql features might not work.\n"),
4385 : pset.progname,
4386 : formatPGVersionNumber(client_ver, false,
4387 : cverbuf, sizeof(cverbuf)),
4388 : formatPGVersionNumber(pset.sversion, false,
4389 : sverbuf, sizeof(sverbuf)));
4390 :
4391 : #ifdef WIN32
4392 : if (in_startup)
4393 : checkWin32Codepage();
4394 : #endif
4395 4 : printSSLInfo();
4396 4 : printGSSInfo();
4397 : }
4398 334 : }
4399 :
4400 :
4401 : /*
4402 : * printSSLInfo
4403 : *
4404 : * Prints information about the current SSL connection, if SSL is in use
4405 : */
4406 : static void
4407 4 : printSSLInfo(void)
4408 : {
4409 : const char *protocol;
4410 : const char *cipher;
4411 : const char *compression;
4412 : const char *alpn;
4413 :
4414 4 : if (!PQsslInUse(pset.db))
4415 4 : return; /* no SSL */
4416 :
4417 0 : protocol = PQsslAttribute(pset.db, "protocol");
4418 0 : cipher = PQsslAttribute(pset.db, "cipher");
4419 0 : compression = PQsslAttribute(pset.db, "compression");
4420 0 : alpn = PQsslAttribute(pset.db, "alpn");
4421 :
4422 0 : printf(_("SSL connection (protocol: %s, cipher: %s, compression: %s, ALPN: %s)\n"),
4423 : protocol ? protocol : _("unknown"),
4424 : cipher ? cipher : _("unknown"),
4425 : (compression && strcmp(compression, "off") != 0) ? _("on") : _("off"),
4426 : (alpn && alpn[0] != '\0') ? alpn : _("none"));
4427 : }
4428 :
4429 : /*
4430 : * printGSSInfo
4431 : *
4432 : * Prints information about the current GSSAPI connection, if GSSAPI encryption is in use
4433 : */
4434 : static void
4435 4 : printGSSInfo(void)
4436 : {
4437 4 : if (!PQgssEncInUse(pset.db))
4438 4 : return; /* no GSSAPI encryption in use */
4439 :
4440 0 : printf(_("GSSAPI-encrypted connection\n"));
4441 : }
4442 :
4443 :
4444 : /*
4445 : * checkWin32Codepage
4446 : *
4447 : * Prints a warning when win32 console codepage differs from Windows codepage
4448 : */
4449 : #ifdef WIN32
4450 : static void
4451 : checkWin32Codepage(void)
4452 : {
4453 : unsigned int wincp,
4454 : concp;
4455 :
4456 : wincp = GetACP();
4457 : concp = GetConsoleCP();
4458 : if (wincp != concp)
4459 : {
4460 : printf(_("WARNING: Console code page (%u) differs from Windows code page (%u)\n"
4461 : " 8-bit characters might not work correctly. See psql reference\n"
4462 : " page \"Notes for Windows users\" for details.\n"),
4463 : concp, wincp);
4464 : }
4465 : }
4466 : #endif
4467 :
4468 :
4469 : /*
4470 : * SyncVariables
4471 : *
4472 : * Make psql's internal variables agree with connection state upon
4473 : * establishing a new connection.
4474 : */
4475 : void
4476 19520 : SyncVariables(void)
4477 : {
4478 : char vbuf[32];
4479 : const char *server_version;
4480 :
4481 : /* get stuff from connection */
4482 19520 : pset.encoding = PQclientEncoding(pset.db);
4483 19520 : pset.popt.topt.encoding = pset.encoding;
4484 19520 : pset.sversion = PQserverVersion(pset.db);
4485 :
4486 19520 : setFmtEncoding(pset.encoding);
4487 :
4488 19520 : SetVariable(pset.vars, "DBNAME", PQdb(pset.db));
4489 19520 : SetVariable(pset.vars, "SERVICE", PQservice(pset.db));
4490 19520 : SetVariable(pset.vars, "USER", PQuser(pset.db));
4491 19520 : SetVariable(pset.vars, "HOST", PQhost(pset.db));
4492 19520 : SetVariable(pset.vars, "PORT", PQport(pset.db));
4493 19520 : SetVariable(pset.vars, "ENCODING", pg_encoding_to_char(pset.encoding));
4494 :
4495 : /* this bit should match connection_warnings(): */
4496 : /* Try to get full text form of version, might include "devel" etc */
4497 19520 : server_version = PQparameterStatus(pset.db, "server_version");
4498 : /* Otherwise fall back on pset.sversion */
4499 19520 : if (!server_version)
4500 : {
4501 0 : formatPGVersionNumber(pset.sversion, true, vbuf, sizeof(vbuf));
4502 0 : server_version = vbuf;
4503 : }
4504 19520 : SetVariable(pset.vars, "SERVER_VERSION_NAME", server_version);
4505 :
4506 19520 : snprintf(vbuf, sizeof(vbuf), "%d", pset.sversion);
4507 19520 : SetVariable(pset.vars, "SERVER_VERSION_NUM", vbuf);
4508 :
4509 : /* send stuff to it, too */
4510 19520 : PQsetErrorVerbosity(pset.db, pset.verbosity);
4511 19520 : PQsetErrorContextVisibility(pset.db, pset.show_context);
4512 19520 : }
4513 :
4514 : /*
4515 : * UnsyncVariables
4516 : *
4517 : * Clear variables that should be not be set when there is no connection.
4518 : */
4519 : void
4520 0 : UnsyncVariables(void)
4521 : {
4522 0 : SetVariable(pset.vars, "DBNAME", NULL);
4523 0 : SetVariable(pset.vars, "SERVICE", NULL);
4524 0 : SetVariable(pset.vars, "USER", NULL);
4525 0 : SetVariable(pset.vars, "HOST", NULL);
4526 0 : SetVariable(pset.vars, "PORT", NULL);
4527 0 : SetVariable(pset.vars, "ENCODING", NULL);
4528 0 : SetVariable(pset.vars, "SERVER_VERSION_NAME", NULL);
4529 0 : SetVariable(pset.vars, "SERVER_VERSION_NUM", NULL);
4530 0 : }
4531 :
4532 :
4533 : /*
4534 : * helper for do_edit(): actually invoke the editor
4535 : *
4536 : * Returns true on success, false if we failed to invoke the editor or
4537 : * it returned nonzero status. (An error message is printed for failed-
4538 : * to-invoke cases, but not if the editor returns nonzero status.)
4539 : */
4540 : static bool
4541 0 : editFile(const char *fname, int lineno)
4542 : {
4543 : const char *editorName;
4544 0 : const char *editor_lineno_arg = NULL;
4545 : char *sys;
4546 : int result;
4547 :
4548 : Assert(fname != NULL);
4549 :
4550 : /* Find an editor to use */
4551 0 : editorName = getenv("PSQL_EDITOR");
4552 0 : if (!editorName)
4553 0 : editorName = getenv("EDITOR");
4554 0 : if (!editorName)
4555 0 : editorName = getenv("VISUAL");
4556 0 : if (!editorName)
4557 0 : editorName = DEFAULT_EDITOR;
4558 :
4559 : /* Get line number argument, if we need it. */
4560 0 : if (lineno > 0)
4561 : {
4562 0 : editor_lineno_arg = getenv("PSQL_EDITOR_LINENUMBER_ARG");
4563 : #ifdef DEFAULT_EDITOR_LINENUMBER_ARG
4564 0 : if (!editor_lineno_arg)
4565 0 : editor_lineno_arg = DEFAULT_EDITOR_LINENUMBER_ARG;
4566 : #endif
4567 0 : if (!editor_lineno_arg)
4568 : {
4569 0 : pg_log_error("environment variable PSQL_EDITOR_LINENUMBER_ARG must be set to specify a line number");
4570 0 : return false;
4571 : }
4572 : }
4573 :
4574 : /*
4575 : * On Unix the EDITOR value should *not* be quoted, since it might include
4576 : * switches, eg, EDITOR="pico -t"; it's up to the user to put quotes in it
4577 : * if necessary. But this policy is not very workable on Windows, due to
4578 : * severe brain damage in their command shell plus the fact that standard
4579 : * program paths include spaces.
4580 : */
4581 : #ifndef WIN32
4582 0 : if (lineno > 0)
4583 0 : sys = psprintf("exec %s %s%d '%s'",
4584 : editorName, editor_lineno_arg, lineno, fname);
4585 : else
4586 0 : sys = psprintf("exec %s '%s'",
4587 : editorName, fname);
4588 : #else
4589 : if (lineno > 0)
4590 : sys = psprintf("\"%s\" %s%d \"%s\"",
4591 : editorName, editor_lineno_arg, lineno, fname);
4592 : else
4593 : sys = psprintf("\"%s\" \"%s\"",
4594 : editorName, fname);
4595 : #endif
4596 0 : fflush(NULL);
4597 0 : result = system(sys);
4598 0 : if (result == -1)
4599 0 : pg_log_error("could not start editor \"%s\"", editorName);
4600 0 : else if (result == 127)
4601 0 : pg_log_error("could not start /bin/sh");
4602 0 : free(sys);
4603 :
4604 0 : return result == 0;
4605 : }
4606 :
4607 :
4608 : /*
4609 : * do_edit -- handler for \e
4610 : *
4611 : * If you do not specify a filename, the current query buffer will be copied
4612 : * into a temporary file.
4613 : *
4614 : * After this function is done, the resulting file will be copied back into the
4615 : * query buffer. As an exception to this, the query buffer will be emptied
4616 : * if the file was not modified (or the editor failed) and the caller passes
4617 : * "discard_on_quit" = true.
4618 : *
4619 : * If "edited" isn't NULL, *edited will be set to true if the query buffer
4620 : * is successfully replaced.
4621 : */
4622 : static bool
4623 0 : do_edit(const char *filename_arg, PQExpBuffer query_buf,
4624 : int lineno, bool discard_on_quit, bool *edited)
4625 : {
4626 : char fnametmp[MAXPGPATH];
4627 0 : FILE *stream = NULL;
4628 : const char *fname;
4629 0 : bool error = false;
4630 : int fd;
4631 : struct stat before,
4632 : after;
4633 :
4634 0 : if (filename_arg)
4635 0 : fname = filename_arg;
4636 : else
4637 : {
4638 : /* make a temp file to edit */
4639 : #ifndef WIN32
4640 0 : const char *tmpdir = getenv("TMPDIR");
4641 :
4642 0 : if (!tmpdir)
4643 0 : tmpdir = "/tmp";
4644 : #else
4645 : char tmpdir[MAXPGPATH];
4646 : int ret;
4647 :
4648 : ret = GetTempPath(MAXPGPATH, tmpdir);
4649 : if (ret == 0 || ret > MAXPGPATH)
4650 : {
4651 : pg_log_error("could not locate temporary directory: %s",
4652 : !ret ? strerror(errno) : "");
4653 : return false;
4654 : }
4655 : #endif
4656 :
4657 : /*
4658 : * No canonicalize_path() here. EDIT.EXE run from CMD.EXE prepends the
4659 : * current directory to the supplied path unless we use only
4660 : * backslashes, so we do that.
4661 : */
4662 : #ifndef WIN32
4663 0 : snprintf(fnametmp, sizeof(fnametmp), "%s%spsql.edit.%d.sql", tmpdir,
4664 0 : "/", (int) getpid());
4665 : #else
4666 : snprintf(fnametmp, sizeof(fnametmp), "%s%spsql.edit.%d.sql", tmpdir,
4667 : "" /* trailing separator already present */ , (int) getpid());
4668 : #endif
4669 :
4670 0 : fname = (const char *) fnametmp;
4671 :
4672 0 : fd = open(fname, O_WRONLY | O_CREAT | O_EXCL, 0600);
4673 0 : if (fd != -1)
4674 0 : stream = fdopen(fd, "w");
4675 :
4676 0 : if (fd == -1 || !stream)
4677 : {
4678 0 : pg_log_error("could not open temporary file \"%s\": %m", fname);
4679 0 : error = true;
4680 : }
4681 : else
4682 : {
4683 0 : unsigned int ql = query_buf->len;
4684 :
4685 : /* force newline-termination of what we send to editor */
4686 0 : if (ql > 0 && query_buf->data[ql - 1] != '\n')
4687 : {
4688 0 : appendPQExpBufferChar(query_buf, '\n');
4689 0 : ql++;
4690 : }
4691 :
4692 0 : if (fwrite(query_buf->data, 1, ql, stream) != ql)
4693 : {
4694 0 : pg_log_error("%s: %m", fname);
4695 :
4696 0 : if (fclose(stream) != 0)
4697 0 : pg_log_error("%s: %m", fname);
4698 :
4699 0 : if (remove(fname) != 0)
4700 0 : pg_log_error("%s: %m", fname);
4701 :
4702 0 : error = true;
4703 : }
4704 0 : else if (fclose(stream) != 0)
4705 : {
4706 0 : pg_log_error("%s: %m", fname);
4707 0 : if (remove(fname) != 0)
4708 0 : pg_log_error("%s: %m", fname);
4709 0 : error = true;
4710 : }
4711 : else
4712 : {
4713 : struct utimbuf ut;
4714 :
4715 : /*
4716 : * Try to set the file modification time of the temporary file
4717 : * a few seconds in the past. Otherwise, the low granularity
4718 : * (one second, or even worse on some filesystems) that we can
4719 : * portably measure with stat(2) could lead us to not
4720 : * recognize a modification, if the user typed very quickly.
4721 : *
4722 : * This is a rather unlikely race condition, so don't error
4723 : * out if the utime(2) call fails --- that would make the cure
4724 : * worse than the disease.
4725 : */
4726 0 : ut.modtime = ut.actime = time(NULL) - 2;
4727 0 : (void) utime(fname, &ut);
4728 : }
4729 : }
4730 : }
4731 :
4732 0 : if (!error && stat(fname, &before) != 0)
4733 : {
4734 0 : pg_log_error("%s: %m", fname);
4735 0 : error = true;
4736 : }
4737 :
4738 : /* call editor */
4739 0 : if (!error)
4740 0 : error = !editFile(fname, lineno);
4741 :
4742 0 : if (!error && stat(fname, &after) != 0)
4743 : {
4744 0 : pg_log_error("%s: %m", fname);
4745 0 : error = true;
4746 : }
4747 :
4748 : /* file was edited if the size or modification time has changed */
4749 0 : if (!error &&
4750 0 : (before.st_size != after.st_size ||
4751 0 : before.st_mtime != after.st_mtime))
4752 : {
4753 0 : stream = fopen(fname, PG_BINARY_R);
4754 0 : if (!stream)
4755 : {
4756 0 : pg_log_error("%s: %m", fname);
4757 0 : error = true;
4758 : }
4759 : else
4760 : {
4761 : /* read file back into query_buf */
4762 : char line[1024];
4763 :
4764 0 : resetPQExpBuffer(query_buf);
4765 0 : while (fgets(line, sizeof(line), stream) != NULL)
4766 0 : appendPQExpBufferStr(query_buf, line);
4767 :
4768 0 : if (ferror(stream))
4769 : {
4770 0 : pg_log_error("%s: %m", fname);
4771 0 : error = true;
4772 0 : resetPQExpBuffer(query_buf);
4773 : }
4774 0 : else if (edited)
4775 : {
4776 0 : *edited = true;
4777 : }
4778 :
4779 0 : fclose(stream);
4780 : }
4781 : }
4782 : else
4783 : {
4784 : /*
4785 : * If the file was not modified, and the caller requested it, discard
4786 : * the query buffer.
4787 : */
4788 0 : if (discard_on_quit)
4789 0 : resetPQExpBuffer(query_buf);
4790 : }
4791 :
4792 : /* remove temp file */
4793 0 : if (!filename_arg)
4794 : {
4795 0 : if (remove(fname) == -1)
4796 : {
4797 0 : pg_log_error("%s: %m", fname);
4798 0 : error = true;
4799 : }
4800 : }
4801 :
4802 0 : return !error;
4803 : }
4804 :
4805 :
4806 :
4807 : /*
4808 : * process_file
4809 : *
4810 : * Reads commands from filename and passes them to the main processing loop.
4811 : * Handler for \i and \ir, but can be used for other things as well. Returns
4812 : * MainLoop() error code.
4813 : *
4814 : * If use_relative_path is true and filename is not an absolute path, then open
4815 : * the file from where the currently processed file (if any) is located.
4816 : */
4817 : int
4818 18684 : process_file(char *filename, bool use_relative_path)
4819 : {
4820 : FILE *fd;
4821 : int result;
4822 : char *oldfilename;
4823 : char relpath[MAXPGPATH];
4824 :
4825 18684 : if (!filename)
4826 : {
4827 6942 : fd = stdin;
4828 6942 : filename = NULL;
4829 : }
4830 11742 : else if (strcmp(filename, "-") != 0)
4831 : {
4832 40 : canonicalize_path_enc(filename, pset.encoding);
4833 :
4834 : /*
4835 : * If we were asked to resolve the pathname relative to the location
4836 : * of the currently executing script, and there is one, and this is a
4837 : * relative pathname, then prepend all but the last pathname component
4838 : * of the current script to this pathname.
4839 : */
4840 40 : if (use_relative_path && pset.inputfile &&
4841 0 : !is_absolute_path(filename) && !has_drive_prefix(filename))
4842 : {
4843 0 : strlcpy(relpath, pset.inputfile, sizeof(relpath));
4844 0 : get_parent_directory(relpath);
4845 0 : join_path_components(relpath, relpath, filename);
4846 0 : canonicalize_path_enc(relpath, pset.encoding);
4847 :
4848 0 : filename = relpath;
4849 : }
4850 :
4851 40 : fd = fopen(filename, PG_BINARY_R);
4852 :
4853 40 : if (!fd)
4854 : {
4855 0 : pg_log_error("%s: %m", filename);
4856 0 : return EXIT_FAILURE;
4857 : }
4858 : }
4859 : else
4860 : {
4861 11702 : fd = stdin;
4862 11702 : filename = "<stdin>"; /* for future error messages */
4863 : }
4864 :
4865 18684 : oldfilename = pset.inputfile;
4866 18684 : pset.inputfile = filename;
4867 :
4868 18684 : pg_logging_config(pset.inputfile ? 0 : PG_LOG_FLAG_TERSE);
4869 :
4870 18684 : result = MainLoop(fd);
4871 :
4872 18666 : if (fd != stdin)
4873 40 : fclose(fd);
4874 :
4875 18666 : pset.inputfile = oldfilename;
4876 :
4877 18666 : pg_logging_config(pset.inputfile ? 0 : PG_LOG_FLAG_TERSE);
4878 :
4879 18666 : return result;
4880 : }
4881 :
4882 :
4883 :
4884 : static const char *
4885 6 : _align2string(enum printFormat in)
4886 : {
4887 6 : switch (in)
4888 : {
4889 0 : case PRINT_NOTHING:
4890 0 : return "nothing";
4891 : break;
4892 6 : case PRINT_ALIGNED:
4893 6 : return "aligned";
4894 : break;
4895 0 : case PRINT_ASCIIDOC:
4896 0 : return "asciidoc";
4897 : break;
4898 0 : case PRINT_CSV:
4899 0 : return "csv";
4900 : break;
4901 0 : case PRINT_HTML:
4902 0 : return "html";
4903 : break;
4904 0 : case PRINT_LATEX:
4905 0 : return "latex";
4906 : break;
4907 0 : case PRINT_LATEX_LONGTABLE:
4908 0 : return "latex-longtable";
4909 : break;
4910 0 : case PRINT_TROFF_MS:
4911 0 : return "troff-ms";
4912 : break;
4913 0 : case PRINT_UNALIGNED:
4914 0 : return "unaligned";
4915 : break;
4916 0 : case PRINT_WRAPPED:
4917 0 : return "wrapped";
4918 : break;
4919 : }
4920 0 : return "unknown";
4921 : }
4922 :
4923 : /*
4924 : * Parse entered Unicode linestyle. If ok, update *linestyle and return
4925 : * true, else return false.
4926 : */
4927 : static bool
4928 0 : set_unicode_line_style(const char *value, size_t vallen,
4929 : unicode_linestyle *linestyle)
4930 : {
4931 0 : if (pg_strncasecmp("single", value, vallen) == 0)
4932 0 : *linestyle = UNICODE_LINESTYLE_SINGLE;
4933 0 : else if (pg_strncasecmp("double", value, vallen) == 0)
4934 0 : *linestyle = UNICODE_LINESTYLE_DOUBLE;
4935 : else
4936 0 : return false;
4937 0 : return true;
4938 : }
4939 :
4940 : static const char *
4941 18 : _unicode_linestyle2string(int linestyle)
4942 : {
4943 18 : switch (linestyle)
4944 : {
4945 18 : case UNICODE_LINESTYLE_SINGLE:
4946 18 : return "single";
4947 : break;
4948 0 : case UNICODE_LINESTYLE_DOUBLE:
4949 0 : return "double";
4950 : break;
4951 : }
4952 0 : return "unknown";
4953 : }
4954 :
4955 : /*
4956 : * do_pset
4957 : *
4958 : * Performs the assignment "param = value", where value could be NULL;
4959 : * for some params that has an effect such as inversion, for others
4960 : * it does nothing.
4961 : *
4962 : * Adjusts the state of the formatting options at *popt. (In practice that
4963 : * is always pset.popt, but maybe someday it could be different.)
4964 : *
4965 : * If successful and quiet is false, then invokes printPsetInfo() to report
4966 : * the change.
4967 : *
4968 : * Returns true if successful, else false (eg for invalid param or value).
4969 : */
4970 : bool
4971 1950 : do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
4972 : {
4973 1950 : size_t vallen = 0;
4974 :
4975 : Assert(param != NULL);
4976 :
4977 1950 : if (value)
4978 1820 : vallen = strlen(value);
4979 :
4980 : /* set format */
4981 1950 : if (strcmp(param, "format") == 0)
4982 : {
4983 : static const struct fmt
4984 : {
4985 : const char *name;
4986 : enum printFormat number;
4987 : } formats[] =
4988 : {
4989 : /* remember to update error message below when adding more */
4990 : {"aligned", PRINT_ALIGNED},
4991 : {"asciidoc", PRINT_ASCIIDOC},
4992 : {"csv", PRINT_CSV},
4993 : {"html", PRINT_HTML},
4994 : {"latex", PRINT_LATEX},
4995 : {"troff-ms", PRINT_TROFF_MS},
4996 : {"unaligned", PRINT_UNALIGNED},
4997 : {"wrapped", PRINT_WRAPPED}
4998 : };
4999 :
5000 638 : if (!value)
5001 : ;
5002 : else
5003 : {
5004 638 : int match_pos = -1;
5005 :
5006 5700 : for (int i = 0; i < lengthof(formats); i++)
5007 : {
5008 5068 : if (pg_strncasecmp(formats[i].name, value, vallen) == 0)
5009 : {
5010 638 : if (match_pos < 0)
5011 632 : match_pos = i;
5012 : else
5013 : {
5014 6 : pg_log_error("\\pset: ambiguous abbreviation \"%s\" matches both \"%s\" and \"%s\"",
5015 : value,
5016 : formats[match_pos].name, formats[i].name);
5017 6 : return false;
5018 : }
5019 : }
5020 : }
5021 632 : if (match_pos >= 0)
5022 626 : popt->topt.format = formats[match_pos].number;
5023 6 : else if (pg_strncasecmp("latex-longtable", value, vallen) == 0)
5024 : {
5025 : /*
5026 : * We must treat latex-longtable specially because latex is a
5027 : * prefix of it; if both were in the table above, we'd think
5028 : * "latex" is ambiguous.
5029 : */
5030 6 : popt->topt.format = PRINT_LATEX_LONGTABLE;
5031 : }
5032 : else
5033 : {
5034 0 : pg_log_error("\\pset: allowed formats are aligned, asciidoc, csv, html, latex, latex-longtable, troff-ms, unaligned, wrapped");
5035 0 : return false;
5036 : }
5037 : }
5038 : }
5039 :
5040 : /* set table line style */
5041 1312 : else if (strcmp(param, "linestyle") == 0)
5042 : {
5043 30 : if (!value)
5044 : ;
5045 30 : else if (pg_strncasecmp("ascii", value, vallen) == 0)
5046 18 : popt->topt.line_style = &pg_asciiformat;
5047 12 : else if (pg_strncasecmp("old-ascii", value, vallen) == 0)
5048 12 : popt->topt.line_style = &pg_asciiformat_old;
5049 0 : else if (pg_strncasecmp("unicode", value, vallen) == 0)
5050 0 : popt->topt.line_style = &pg_utf8format;
5051 : else
5052 : {
5053 0 : pg_log_error("\\pset: allowed line styles are ascii, old-ascii, unicode");
5054 0 : return false;
5055 : }
5056 : }
5057 :
5058 : /* set unicode border line style */
5059 1282 : else if (strcmp(param, "unicode_border_linestyle") == 0)
5060 : {
5061 0 : if (!value)
5062 : ;
5063 0 : else if (set_unicode_line_style(value, vallen,
5064 : &popt->topt.unicode_border_linestyle))
5065 0 : refresh_utf8format(&(popt->topt));
5066 : else
5067 : {
5068 0 : pg_log_error("\\pset: allowed Unicode border line styles are single, double");
5069 0 : return false;
5070 : }
5071 : }
5072 :
5073 : /* set unicode column line style */
5074 1282 : else if (strcmp(param, "unicode_column_linestyle") == 0)
5075 : {
5076 0 : if (!value)
5077 : ;
5078 0 : else if (set_unicode_line_style(value, vallen,
5079 : &popt->topt.unicode_column_linestyle))
5080 0 : refresh_utf8format(&(popt->topt));
5081 : else
5082 : {
5083 0 : pg_log_error("\\pset: allowed Unicode column line styles are single, double");
5084 0 : return false;
5085 : }
5086 : }
5087 :
5088 : /* set unicode header line style */
5089 1282 : else if (strcmp(param, "unicode_header_linestyle") == 0)
5090 : {
5091 0 : if (!value)
5092 : ;
5093 0 : else if (set_unicode_line_style(value, vallen,
5094 : &popt->topt.unicode_header_linestyle))
5095 0 : refresh_utf8format(&(popt->topt));
5096 : else
5097 : {
5098 0 : pg_log_error("\\pset: allowed Unicode header line styles are single, double");
5099 0 : return false;
5100 : }
5101 : }
5102 :
5103 : /* set border style/width */
5104 1282 : else if (strcmp(param, "border") == 0)
5105 : {
5106 402 : if (value)
5107 402 : popt->topt.border = atoi(value);
5108 : }
5109 :
5110 : /* set expanded/vertical mode */
5111 880 : else if (strcmp(param, "x") == 0 ||
5112 880 : strcmp(param, "expanded") == 0 ||
5113 550 : strcmp(param, "vertical") == 0)
5114 : {
5115 330 : if (value && pg_strcasecmp(value, "auto") == 0)
5116 0 : popt->topt.expanded = 2;
5117 330 : else if (value)
5118 : {
5119 : bool on_off;
5120 :
5121 260 : if (ParseVariableBool(value, NULL, &on_off))
5122 260 : popt->topt.expanded = on_off ? 1 : 0;
5123 : else
5124 : {
5125 0 : PsqlVarEnumError(param, value, "on, off, auto");
5126 0 : return false;
5127 : }
5128 : }
5129 : else
5130 70 : popt->topt.expanded = !popt->topt.expanded;
5131 : }
5132 :
5133 : /* header line width in expanded mode */
5134 550 : else if (strcmp(param, "xheader_width") == 0)
5135 : {
5136 0 : if (!value)
5137 : ;
5138 0 : else if (pg_strcasecmp(value, "full") == 0)
5139 0 : popt->topt.expanded_header_width_type = PRINT_XHEADER_FULL;
5140 0 : else if (pg_strcasecmp(value, "column") == 0)
5141 0 : popt->topt.expanded_header_width_type = PRINT_XHEADER_COLUMN;
5142 0 : else if (pg_strcasecmp(value, "page") == 0)
5143 0 : popt->topt.expanded_header_width_type = PRINT_XHEADER_PAGE;
5144 : else
5145 : {
5146 0 : int intval = atoi(value);
5147 :
5148 0 : if (intval == 0)
5149 : {
5150 0 : pg_log_error("\\pset: allowed xheader_width values are \"%s\" (default), \"%s\", \"%s\", or a number specifying the exact width", "full", "column", "page");
5151 0 : return false;
5152 : }
5153 :
5154 0 : popt->topt.expanded_header_width_type = PRINT_XHEADER_EXACT_WIDTH;
5155 0 : popt->topt.expanded_header_exact_width = intval;
5156 : }
5157 : }
5158 :
5159 : /* field separator for CSV format */
5160 550 : else if (strcmp(param, "csv_fieldsep") == 0)
5161 : {
5162 60 : if (value)
5163 : {
5164 : /* CSV separator has to be a one-byte character */
5165 60 : if (strlen(value) != 1)
5166 : {
5167 18 : pg_log_error("\\pset: csv_fieldsep must be a single one-byte character");
5168 18 : return false;
5169 : }
5170 42 : if (value[0] == '"' || value[0] == '\n' || value[0] == '\r')
5171 : {
5172 18 : pg_log_error("\\pset: csv_fieldsep cannot be a double quote, a newline, or a carriage return");
5173 18 : return false;
5174 : }
5175 24 : popt->topt.csvFieldSep[0] = value[0];
5176 : }
5177 : }
5178 :
5179 : /* locale-aware numeric output */
5180 490 : else if (strcmp(param, "numericlocale") == 0)
5181 : {
5182 12 : if (value)
5183 12 : return ParseVariableBool(value, param, &popt->topt.numericLocale);
5184 : else
5185 0 : popt->topt.numericLocale = !popt->topt.numericLocale;
5186 : }
5187 :
5188 : /* null display */
5189 478 : else if (strcmp(param, "null") == 0)
5190 : {
5191 78 : if (value)
5192 : {
5193 78 : free(popt->nullPrint);
5194 78 : popt->nullPrint = pg_strdup(value);
5195 : }
5196 : }
5197 :
5198 : /* field separator for unaligned text */
5199 400 : else if (strcmp(param, "fieldsep") == 0)
5200 : {
5201 6 : if (value)
5202 : {
5203 6 : free(popt->topt.fieldSep.separator);
5204 6 : popt->topt.fieldSep.separator = pg_strdup(value);
5205 6 : popt->topt.fieldSep.separator_zero = false;
5206 : }
5207 : }
5208 :
5209 394 : else if (strcmp(param, "fieldsep_zero") == 0)
5210 : {
5211 0 : free(popt->topt.fieldSep.separator);
5212 0 : popt->topt.fieldSep.separator = NULL;
5213 0 : popt->topt.fieldSep.separator_zero = true;
5214 : }
5215 :
5216 : /* record separator for unaligned text */
5217 394 : else if (strcmp(param, "recordsep") == 0)
5218 : {
5219 0 : if (value)
5220 : {
5221 0 : free(popt->topt.recordSep.separator);
5222 0 : popt->topt.recordSep.separator = pg_strdup(value);
5223 0 : popt->topt.recordSep.separator_zero = false;
5224 : }
5225 : }
5226 :
5227 394 : else if (strcmp(param, "recordsep_zero") == 0)
5228 : {
5229 0 : free(popt->topt.recordSep.separator);
5230 0 : popt->topt.recordSep.separator = NULL;
5231 0 : popt->topt.recordSep.separator_zero = true;
5232 : }
5233 :
5234 : /* toggle between full and tuples-only format */
5235 394 : else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0)
5236 : {
5237 292 : if (value)
5238 256 : return ParseVariableBool(value, param, &popt->topt.tuples_only);
5239 : else
5240 36 : popt->topt.tuples_only = !popt->topt.tuples_only;
5241 : }
5242 :
5243 : /* set title override */
5244 102 : else if (strcmp(param, "C") == 0 || strcmp(param, "title") == 0)
5245 : {
5246 6 : free(popt->title);
5247 6 : if (!value)
5248 0 : popt->title = NULL;
5249 : else
5250 6 : popt->title = pg_strdup(value);
5251 : }
5252 :
5253 : /* set HTML table tag options */
5254 96 : else if (strcmp(param, "T") == 0 || strcmp(param, "tableattr") == 0)
5255 : {
5256 48 : free(popt->topt.tableAttr);
5257 48 : if (!value)
5258 24 : popt->topt.tableAttr = NULL;
5259 : else
5260 24 : popt->topt.tableAttr = pg_strdup(value);
5261 : }
5262 :
5263 : /* toggle use of pager */
5264 48 : else if (strcmp(param, "pager") == 0)
5265 : {
5266 0 : if (value && pg_strcasecmp(value, "always") == 0)
5267 0 : popt->topt.pager = 2;
5268 0 : else if (value)
5269 : {
5270 : bool on_off;
5271 :
5272 0 : if (!ParseVariableBool(value, NULL, &on_off))
5273 : {
5274 0 : PsqlVarEnumError(param, value, "on, off, always");
5275 0 : return false;
5276 : }
5277 0 : popt->topt.pager = on_off ? 1 : 0;
5278 : }
5279 0 : else if (popt->topt.pager == 1)
5280 0 : popt->topt.pager = 0;
5281 : else
5282 0 : popt->topt.pager = 1;
5283 : }
5284 :
5285 : /* set minimum lines for pager use */
5286 48 : else if (strcmp(param, "pager_min_lines") == 0)
5287 : {
5288 0 : if (value &&
5289 0 : !ParseVariableNum(value, "pager_min_lines", &popt->topt.pager_min_lines))
5290 0 : return false;
5291 : }
5292 :
5293 : /* disable "(x rows)" footer */
5294 48 : else if (strcmp(param, "footer") == 0)
5295 : {
5296 0 : if (value)
5297 0 : return ParseVariableBool(value, param, &popt->topt.default_footer);
5298 : else
5299 0 : popt->topt.default_footer = !popt->topt.default_footer;
5300 : }
5301 :
5302 : /* set border style/width */
5303 48 : else if (strcmp(param, "columns") == 0)
5304 : {
5305 48 : if (value)
5306 48 : popt->topt.columns = atoi(value);
5307 : }
5308 : else
5309 : {
5310 0 : pg_log_error("\\pset: unknown option: %s", param);
5311 0 : return false;
5312 : }
5313 :
5314 1640 : if (!quiet)
5315 0 : printPsetInfo(param, &pset.popt);
5316 :
5317 1640 : return true;
5318 : }
5319 :
5320 : /*
5321 : * printPsetInfo: print the state of the "param" formatting parameter in popt.
5322 : */
5323 : static bool
5324 0 : printPsetInfo(const char *param, printQueryOpt *popt)
5325 : {
5326 : Assert(param != NULL);
5327 :
5328 : /* show border style/width */
5329 0 : if (strcmp(param, "border") == 0)
5330 0 : printf(_("Border style is %d.\n"), popt->topt.border);
5331 :
5332 : /* show the target width for the wrapped format */
5333 0 : else if (strcmp(param, "columns") == 0)
5334 : {
5335 0 : if (!popt->topt.columns)
5336 0 : printf(_("Target width is unset.\n"));
5337 : else
5338 0 : printf(_("Target width is %d.\n"), popt->topt.columns);
5339 : }
5340 :
5341 : /* show expanded/vertical mode */
5342 0 : else if (strcmp(param, "x") == 0 || strcmp(param, "expanded") == 0 || strcmp(param, "vertical") == 0)
5343 : {
5344 0 : if (popt->topt.expanded == 1)
5345 0 : printf(_("Expanded display is on.\n"));
5346 0 : else if (popt->topt.expanded == 2)
5347 0 : printf(_("Expanded display is used automatically.\n"));
5348 : else
5349 0 : printf(_("Expanded display is off.\n"));
5350 : }
5351 :
5352 : /* show xheader width value */
5353 0 : else if (strcmp(param, "xheader_width") == 0)
5354 : {
5355 0 : if (popt->topt.expanded_header_width_type == PRINT_XHEADER_FULL)
5356 0 : printf(_("Expanded header width is \"%s\".\n"), "full");
5357 0 : else if (popt->topt.expanded_header_width_type == PRINT_XHEADER_COLUMN)
5358 0 : printf(_("Expanded header width is \"%s\".\n"), "column");
5359 0 : else if (popt->topt.expanded_header_width_type == PRINT_XHEADER_PAGE)
5360 0 : printf(_("Expanded header width is \"%s\".\n"), "page");
5361 0 : else if (popt->topt.expanded_header_width_type == PRINT_XHEADER_EXACT_WIDTH)
5362 0 : printf(_("Expanded header width is %d.\n"), popt->topt.expanded_header_exact_width);
5363 : }
5364 :
5365 : /* show field separator for CSV format */
5366 0 : else if (strcmp(param, "csv_fieldsep") == 0)
5367 : {
5368 0 : printf(_("Field separator for CSV is \"%s\".\n"),
5369 : popt->topt.csvFieldSep);
5370 : }
5371 :
5372 : /* show field separator for unaligned text */
5373 0 : else if (strcmp(param, "fieldsep") == 0)
5374 : {
5375 0 : if (popt->topt.fieldSep.separator_zero)
5376 0 : printf(_("Field separator is zero byte.\n"));
5377 : else
5378 0 : printf(_("Field separator is \"%s\".\n"),
5379 : popt->topt.fieldSep.separator);
5380 : }
5381 :
5382 0 : else if (strcmp(param, "fieldsep_zero") == 0)
5383 : {
5384 0 : printf(_("Field separator is zero byte.\n"));
5385 : }
5386 :
5387 : /* show disable "(x rows)" footer */
5388 0 : else if (strcmp(param, "footer") == 0)
5389 : {
5390 0 : if (popt->topt.default_footer)
5391 0 : printf(_("Default footer is on.\n"));
5392 : else
5393 0 : printf(_("Default footer is off.\n"));
5394 : }
5395 :
5396 : /* show format */
5397 0 : else if (strcmp(param, "format") == 0)
5398 : {
5399 0 : printf(_("Output format is %s.\n"), _align2string(popt->topt.format));
5400 : }
5401 :
5402 : /* show table line style */
5403 0 : else if (strcmp(param, "linestyle") == 0)
5404 : {
5405 0 : printf(_("Line style is %s.\n"),
5406 : get_line_style(&popt->topt)->name);
5407 : }
5408 :
5409 : /* show null display */
5410 0 : else if (strcmp(param, "null") == 0)
5411 : {
5412 0 : printf(_("Null display is \"%s\".\n"),
5413 : popt->nullPrint ? popt->nullPrint : "");
5414 : }
5415 :
5416 : /* show locale-aware numeric output */
5417 0 : else if (strcmp(param, "numericlocale") == 0)
5418 : {
5419 0 : if (popt->topt.numericLocale)
5420 0 : printf(_("Locale-adjusted numeric output is on.\n"));
5421 : else
5422 0 : printf(_("Locale-adjusted numeric output is off.\n"));
5423 : }
5424 :
5425 : /* show toggle use of pager */
5426 0 : else if (strcmp(param, "pager") == 0)
5427 : {
5428 0 : if (popt->topt.pager == 1)
5429 0 : printf(_("Pager is used for long output.\n"));
5430 0 : else if (popt->topt.pager == 2)
5431 0 : printf(_("Pager is always used.\n"));
5432 : else
5433 0 : printf(_("Pager usage is off.\n"));
5434 : }
5435 :
5436 : /* show minimum lines for pager use */
5437 0 : else if (strcmp(param, "pager_min_lines") == 0)
5438 : {
5439 0 : printf(ngettext("Pager won't be used for less than %d line.\n",
5440 : "Pager won't be used for less than %d lines.\n",
5441 : popt->topt.pager_min_lines),
5442 : popt->topt.pager_min_lines);
5443 : }
5444 :
5445 : /* show record separator for unaligned text */
5446 0 : else if (strcmp(param, "recordsep") == 0)
5447 : {
5448 0 : if (popt->topt.recordSep.separator_zero)
5449 0 : printf(_("Record separator is zero byte.\n"));
5450 0 : else if (strcmp(popt->topt.recordSep.separator, "\n") == 0)
5451 0 : printf(_("Record separator is <newline>.\n"));
5452 : else
5453 0 : printf(_("Record separator is \"%s\".\n"),
5454 : popt->topt.recordSep.separator);
5455 : }
5456 :
5457 0 : else if (strcmp(param, "recordsep_zero") == 0)
5458 : {
5459 0 : printf(_("Record separator is zero byte.\n"));
5460 : }
5461 :
5462 : /* show HTML table tag options */
5463 0 : else if (strcmp(param, "T") == 0 || strcmp(param, "tableattr") == 0)
5464 : {
5465 0 : if (popt->topt.tableAttr)
5466 0 : printf(_("Table attributes are \"%s\".\n"),
5467 : popt->topt.tableAttr);
5468 : else
5469 0 : printf(_("Table attributes unset.\n"));
5470 : }
5471 :
5472 : /* show title override */
5473 0 : else if (strcmp(param, "C") == 0 || strcmp(param, "title") == 0)
5474 : {
5475 0 : if (popt->title)
5476 0 : printf(_("Title is \"%s\".\n"), popt->title);
5477 : else
5478 0 : printf(_("Title is unset.\n"));
5479 : }
5480 :
5481 : /* show toggle between full and tuples-only format */
5482 0 : else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0)
5483 : {
5484 0 : if (popt->topt.tuples_only)
5485 0 : printf(_("Tuples only is on.\n"));
5486 : else
5487 0 : printf(_("Tuples only is off.\n"));
5488 : }
5489 :
5490 : /* Unicode style formatting */
5491 0 : else if (strcmp(param, "unicode_border_linestyle") == 0)
5492 : {
5493 0 : printf(_("Unicode border line style is \"%s\".\n"),
5494 : _unicode_linestyle2string(popt->topt.unicode_border_linestyle));
5495 : }
5496 :
5497 0 : else if (strcmp(param, "unicode_column_linestyle") == 0)
5498 : {
5499 0 : printf(_("Unicode column line style is \"%s\".\n"),
5500 : _unicode_linestyle2string(popt->topt.unicode_column_linestyle));
5501 : }
5502 :
5503 0 : else if (strcmp(param, "unicode_header_linestyle") == 0)
5504 : {
5505 0 : printf(_("Unicode header line style is \"%s\".\n"),
5506 : _unicode_linestyle2string(popt->topt.unicode_header_linestyle));
5507 : }
5508 :
5509 : else
5510 : {
5511 0 : pg_log_error("\\pset: unknown option: %s", param);
5512 0 : return false;
5513 : }
5514 :
5515 0 : return true;
5516 : }
5517 :
5518 : /*
5519 : * savePsetInfo: make a malloc'd copy of the data in *popt.
5520 : *
5521 : * Possibly this should be somewhere else, but it's a bit specific to psql.
5522 : */
5523 : printQueryOpt *
5524 42 : savePsetInfo(const printQueryOpt *popt)
5525 : {
5526 : printQueryOpt *save;
5527 :
5528 42 : save = (printQueryOpt *) pg_malloc(sizeof(printQueryOpt));
5529 :
5530 : /* Flat-copy all the scalar fields, then duplicate sub-structures. */
5531 42 : memcpy(save, popt, sizeof(printQueryOpt));
5532 :
5533 : /* topt.line_style points to const data that need not be duplicated */
5534 42 : if (popt->topt.fieldSep.separator)
5535 42 : save->topt.fieldSep.separator = pg_strdup(popt->topt.fieldSep.separator);
5536 42 : if (popt->topt.recordSep.separator)
5537 42 : save->topt.recordSep.separator = pg_strdup(popt->topt.recordSep.separator);
5538 42 : if (popt->topt.tableAttr)
5539 0 : save->topt.tableAttr = pg_strdup(popt->topt.tableAttr);
5540 42 : if (popt->nullPrint)
5541 0 : save->nullPrint = pg_strdup(popt->nullPrint);
5542 42 : if (popt->title)
5543 0 : save->title = pg_strdup(popt->title);
5544 :
5545 : /*
5546 : * footers and translate_columns are never set in psql's print settings,
5547 : * so we needn't write code to duplicate them.
5548 : */
5549 : Assert(popt->footers == NULL);
5550 : Assert(popt->translate_columns == NULL);
5551 :
5552 42 : return save;
5553 : }
5554 :
5555 : /*
5556 : * restorePsetInfo: restore *popt from the previously-saved copy *save,
5557 : * then free *save.
5558 : */
5559 : void
5560 42 : restorePsetInfo(printQueryOpt *popt, printQueryOpt *save)
5561 : {
5562 : /* Free all the old data we're about to overwrite the pointers to. */
5563 :
5564 : /* topt.line_style points to const data that need not be duplicated */
5565 42 : free(popt->topt.fieldSep.separator);
5566 42 : free(popt->topt.recordSep.separator);
5567 42 : free(popt->topt.tableAttr);
5568 42 : free(popt->nullPrint);
5569 42 : free(popt->title);
5570 :
5571 : /*
5572 : * footers and translate_columns are never set in psql's print settings,
5573 : * so we needn't write code to duplicate them.
5574 : */
5575 : Assert(popt->footers == NULL);
5576 : Assert(popt->translate_columns == NULL);
5577 :
5578 : /* Now we may flat-copy all the fields, including pointers. */
5579 42 : memcpy(popt, save, sizeof(printQueryOpt));
5580 :
5581 : /* Lastly, free "save" ... but its sub-structures now belong to popt. */
5582 42 : free(save);
5583 42 : }
5584 :
5585 : static const char *
5586 36 : pset_bool_string(bool val)
5587 : {
5588 36 : return val ? "on" : "off";
5589 : }
5590 :
5591 :
5592 : static char *
5593 24 : pset_quoted_string(const char *str)
5594 : {
5595 24 : char *ret = pg_malloc(strlen(str) * 2 + 3);
5596 24 : char *r = ret;
5597 :
5598 24 : *r++ = '\'';
5599 :
5600 42 : for (; *str; str++)
5601 : {
5602 18 : if (*str == '\n')
5603 : {
5604 6 : *r++ = '\\';
5605 6 : *r++ = 'n';
5606 : }
5607 12 : else if (*str == '\'')
5608 : {
5609 0 : *r++ = '\\';
5610 0 : *r++ = '\'';
5611 : }
5612 : else
5613 12 : *r++ = *str;
5614 : }
5615 :
5616 24 : *r++ = '\'';
5617 24 : *r = '\0';
5618 :
5619 24 : return ret;
5620 : }
5621 :
5622 :
5623 : /*
5624 : * Return a malloc'ed string for the \pset value.
5625 : *
5626 : * Note that for some string parameters, print.c distinguishes between unset
5627 : * and empty string, but for others it doesn't. This function should produce
5628 : * output that produces the correct setting when fed back into \pset.
5629 : */
5630 : static char *
5631 132 : pset_value_string(const char *param, printQueryOpt *popt)
5632 : {
5633 : Assert(param != NULL);
5634 :
5635 132 : if (strcmp(param, "border") == 0)
5636 6 : return psprintf("%d", popt->topt.border);
5637 126 : else if (strcmp(param, "columns") == 0)
5638 6 : return psprintf("%d", popt->topt.columns);
5639 120 : else if (strcmp(param, "csv_fieldsep") == 0)
5640 6 : return pset_quoted_string(popt->topt.csvFieldSep);
5641 114 : else if (strcmp(param, "expanded") == 0)
5642 12 : return pstrdup(popt->topt.expanded == 2
5643 : ? "auto"
5644 6 : : pset_bool_string(popt->topt.expanded));
5645 108 : else if (strcmp(param, "fieldsep") == 0)
5646 6 : return pset_quoted_string(popt->topt.fieldSep.separator
5647 : ? popt->topt.fieldSep.separator
5648 : : "");
5649 102 : else if (strcmp(param, "fieldsep_zero") == 0)
5650 6 : return pstrdup(pset_bool_string(popt->topt.fieldSep.separator_zero));
5651 96 : else if (strcmp(param, "footer") == 0)
5652 6 : return pstrdup(pset_bool_string(popt->topt.default_footer));
5653 90 : else if (strcmp(param, "format") == 0)
5654 6 : return pstrdup(_align2string(popt->topt.format));
5655 84 : else if (strcmp(param, "linestyle") == 0)
5656 6 : return pstrdup(get_line_style(&popt->topt)->name);
5657 78 : else if (strcmp(param, "null") == 0)
5658 6 : return pset_quoted_string(popt->nullPrint
5659 : ? popt->nullPrint
5660 : : "");
5661 72 : else if (strcmp(param, "numericlocale") == 0)
5662 6 : return pstrdup(pset_bool_string(popt->topt.numericLocale));
5663 66 : else if (strcmp(param, "pager") == 0)
5664 6 : return psprintf("%d", popt->topt.pager);
5665 60 : else if (strcmp(param, "pager_min_lines") == 0)
5666 6 : return psprintf("%d", popt->topt.pager_min_lines);
5667 54 : else if (strcmp(param, "recordsep") == 0)
5668 6 : return pset_quoted_string(popt->topt.recordSep.separator
5669 : ? popt->topt.recordSep.separator
5670 : : "");
5671 48 : else if (strcmp(param, "recordsep_zero") == 0)
5672 6 : return pstrdup(pset_bool_string(popt->topt.recordSep.separator_zero));
5673 42 : else if (strcmp(param, "tableattr") == 0)
5674 6 : return popt->topt.tableAttr ? pset_quoted_string(popt->topt.tableAttr) : pstrdup("");
5675 36 : else if (strcmp(param, "title") == 0)
5676 6 : return popt->title ? pset_quoted_string(popt->title) : pstrdup("");
5677 30 : else if (strcmp(param, "tuples_only") == 0)
5678 6 : return pstrdup(pset_bool_string(popt->topt.tuples_only));
5679 24 : else if (strcmp(param, "unicode_border_linestyle") == 0)
5680 6 : return pstrdup(_unicode_linestyle2string(popt->topt.unicode_border_linestyle));
5681 18 : else if (strcmp(param, "unicode_column_linestyle") == 0)
5682 6 : return pstrdup(_unicode_linestyle2string(popt->topt.unicode_column_linestyle));
5683 12 : else if (strcmp(param, "unicode_header_linestyle") == 0)
5684 6 : return pstrdup(_unicode_linestyle2string(popt->topt.unicode_header_linestyle));
5685 6 : else if (strcmp(param, "xheader_width") == 0)
5686 : {
5687 6 : if (popt->topt.expanded_header_width_type == PRINT_XHEADER_FULL)
5688 6 : return pstrdup("full");
5689 0 : else if (popt->topt.expanded_header_width_type == PRINT_XHEADER_COLUMN)
5690 0 : return pstrdup("column");
5691 0 : else if (popt->topt.expanded_header_width_type == PRINT_XHEADER_PAGE)
5692 0 : return pstrdup("page");
5693 : else
5694 : {
5695 : /* must be PRINT_XHEADER_EXACT_WIDTH */
5696 : char wbuff[32];
5697 :
5698 0 : snprintf(wbuff, sizeof(wbuff), "%d",
5699 : popt->topt.expanded_header_exact_width);
5700 0 : return pstrdup(wbuff);
5701 : }
5702 : }
5703 : else
5704 0 : return pstrdup("ERROR");
5705 : }
5706 :
5707 :
5708 :
5709 : #ifndef WIN32
5710 : #define DEFAULT_SHELL "/bin/sh"
5711 : #else
5712 : /*
5713 : * CMD.EXE is in different places in different Win32 releases so we
5714 : * have to rely on the path to find it.
5715 : */
5716 : #define DEFAULT_SHELL "cmd.exe"
5717 : #endif
5718 :
5719 : static bool
5720 0 : do_shell(const char *command)
5721 : {
5722 : int result;
5723 :
5724 0 : fflush(NULL);
5725 0 : if (!command)
5726 : {
5727 : char *sys;
5728 : const char *shellName;
5729 :
5730 0 : shellName = getenv("SHELL");
5731 : #ifdef WIN32
5732 : if (shellName == NULL)
5733 : shellName = getenv("COMSPEC");
5734 : #endif
5735 0 : if (shellName == NULL)
5736 0 : shellName = DEFAULT_SHELL;
5737 :
5738 : /* See EDITOR handling comment for an explanation */
5739 : #ifndef WIN32
5740 0 : sys = psprintf("exec %s", shellName);
5741 : #else
5742 : sys = psprintf("\"%s\"", shellName);
5743 : #endif
5744 0 : result = system(sys);
5745 0 : free(sys);
5746 : }
5747 : else
5748 0 : result = system(command);
5749 :
5750 0 : SetShellResultVariables(result);
5751 :
5752 0 : if (result == 127 || result == -1)
5753 : {
5754 0 : pg_log_error("\\!: failed");
5755 0 : return false;
5756 : }
5757 0 : return true;
5758 : }
5759 :
5760 : /*
5761 : * do_watch -- handler for \watch
5762 : *
5763 : * We break this out of exec_command to avoid having to plaster "volatile"
5764 : * onto a bunch of exec_command's variables to silence stupider compilers.
5765 : *
5766 : * "sleep" is the amount of time to sleep during each loop, measured in
5767 : * seconds. The internals of this function should use "sleep_ms" for
5768 : * precise sleep time calculations.
5769 : */
5770 : static bool
5771 10 : do_watch(PQExpBuffer query_buf, double sleep, int iter, int min_rows)
5772 : {
5773 10 : long sleep_ms = (long) (sleep * 1000);
5774 10 : printQueryOpt myopt = pset.popt;
5775 : const char *strftime_fmt;
5776 : const char *user_title;
5777 : char *title;
5778 10 : const char *pagerprog = NULL;
5779 10 : FILE *pagerpipe = NULL;
5780 : int title_len;
5781 10 : int res = 0;
5782 10 : bool done = false;
5783 : #ifndef WIN32
5784 : sigset_t sigalrm_sigchld_sigint;
5785 : sigset_t sigalrm_sigchld;
5786 : sigset_t sigint;
5787 : struct itimerval interval;
5788 : #endif
5789 :
5790 10 : if (!query_buf || query_buf->len <= 0)
5791 : {
5792 0 : pg_log_error("\\watch cannot be used with an empty query");
5793 0 : return false;
5794 : }
5795 :
5796 : #ifndef WIN32
5797 10 : sigemptyset(&sigalrm_sigchld_sigint);
5798 10 : sigaddset(&sigalrm_sigchld_sigint, SIGCHLD);
5799 10 : sigaddset(&sigalrm_sigchld_sigint, SIGALRM);
5800 10 : sigaddset(&sigalrm_sigchld_sigint, SIGINT);
5801 :
5802 10 : sigemptyset(&sigalrm_sigchld);
5803 10 : sigaddset(&sigalrm_sigchld, SIGCHLD);
5804 10 : sigaddset(&sigalrm_sigchld, SIGALRM);
5805 :
5806 10 : sigemptyset(&sigint);
5807 10 : sigaddset(&sigint, SIGINT);
5808 :
5809 : /*
5810 : * Block SIGALRM and SIGCHLD before we start the timer and the pager (if
5811 : * configured), to avoid races. sigwait() will receive them.
5812 : */
5813 10 : sigprocmask(SIG_BLOCK, &sigalrm_sigchld, NULL);
5814 :
5815 : /*
5816 : * Set a timer to interrupt sigwait() so we can run the query at the
5817 : * requested intervals.
5818 : */
5819 10 : interval.it_value.tv_sec = sleep_ms / 1000;
5820 10 : interval.it_value.tv_usec = (sleep_ms % 1000) * 1000;
5821 10 : interval.it_interval = interval.it_value;
5822 10 : if (setitimer(ITIMER_REAL, &interval, NULL) < 0)
5823 : {
5824 0 : pg_log_error("could not set timer: %m");
5825 0 : done = true;
5826 : }
5827 : #endif
5828 :
5829 : /*
5830 : * For \watch, we ignore the size of the result and always use the pager
5831 : * as long as we're talking to a terminal and "\pset pager" is enabled.
5832 : * However, we'll only use the pager identified by PSQL_WATCH_PAGER. We
5833 : * ignore the regular PSQL_PAGER or PAGER environment variables, because
5834 : * traditional pagers probably won't be very useful for showing a stream
5835 : * of results.
5836 : */
5837 : #ifndef WIN32
5838 10 : pagerprog = getenv("PSQL_WATCH_PAGER");
5839 : /* if variable is empty or all-white-space, don't use pager */
5840 10 : if (pagerprog && strspn(pagerprog, " \t\r\n") == strlen(pagerprog))
5841 0 : pagerprog = NULL;
5842 : #endif
5843 10 : if (pagerprog && myopt.topt.pager &&
5844 0 : isatty(fileno(stdin)) && isatty(fileno(stdout)))
5845 : {
5846 0 : fflush(NULL);
5847 0 : disable_sigpipe_trap();
5848 0 : pagerpipe = popen(pagerprog, "w");
5849 :
5850 0 : if (!pagerpipe)
5851 : /* silently proceed without pager */
5852 0 : restore_sigpipe_trap();
5853 : }
5854 :
5855 : /*
5856 : * Choose format for timestamps. We might eventually make this a \pset
5857 : * option. In the meantime, using a variable for the format suppresses
5858 : * overly-anal-retentive gcc warnings about %c being Y2K sensitive.
5859 : */
5860 10 : strftime_fmt = "%c";
5861 :
5862 : /*
5863 : * Set up rendering options, in particular, disable the pager unless
5864 : * PSQL_WATCH_PAGER was successfully launched.
5865 : */
5866 10 : if (!pagerpipe)
5867 10 : myopt.topt.pager = 0;
5868 :
5869 : /*
5870 : * If there's a title in the user configuration, make sure we have room
5871 : * for it in the title buffer. Allow 128 bytes for the timestamp plus 128
5872 : * bytes for the rest.
5873 : */
5874 10 : user_title = myopt.title;
5875 10 : title_len = (user_title ? strlen(user_title) : 0) + 256;
5876 10 : title = pg_malloc(title_len);
5877 :
5878 : /* Loop to run query and then sleep awhile */
5879 42 : while (!done)
5880 : {
5881 : time_t timer;
5882 : char timebuf[128];
5883 :
5884 : /*
5885 : * Prepare title for output. Note that we intentionally include a
5886 : * newline at the end of the title; this is somewhat historical but it
5887 : * makes for reasonably nicely formatted output in simple cases.
5888 : */
5889 42 : timer = time(NULL);
5890 42 : strftime(timebuf, sizeof(timebuf), strftime_fmt, localtime(&timer));
5891 :
5892 42 : if (user_title)
5893 0 : snprintf(title, title_len, _("%s\t%s (every %gs)\n"),
5894 : user_title, timebuf, sleep_ms / 1000.0);
5895 : else
5896 42 : snprintf(title, title_len, _("%s (every %gs)\n"),
5897 : timebuf, sleep_ms / 1000.0);
5898 42 : myopt.title = title;
5899 :
5900 : /* Run the query and print out the result */
5901 42 : res = PSQLexecWatch(query_buf->data, &myopt, pagerpipe, min_rows);
5902 :
5903 : /*
5904 : * PSQLexecWatch handles the case where we can no longer repeat the
5905 : * query, and returns 0 or -1.
5906 : */
5907 40 : if (res <= 0)
5908 8 : break;
5909 :
5910 : /* If we have iteration count, check that it's not exceeded yet */
5911 38 : if (iter && (--iter <= 0))
5912 6 : break;
5913 :
5914 : /* Quit if error on pager pipe (probably pager has quit) */
5915 32 : if (pagerpipe && ferror(pagerpipe))
5916 0 : break;
5917 :
5918 : /* Tight loop, no wait needed */
5919 32 : if (sleep_ms == 0)
5920 8 : continue;
5921 :
5922 : #ifdef WIN32
5923 :
5924 : /*
5925 : * Wait a while before running the query again. Break the sleep into
5926 : * short intervals (at most 1s); that's probably unnecessary since
5927 : * pg_usleep is interruptible on Windows, but it's cheap insurance.
5928 : */
5929 : for (long i = sleep_ms; i > 0;)
5930 : {
5931 : long s = Min(i, 1000L);
5932 :
5933 : pg_usleep(s * 1000L);
5934 : if (cancel_pressed)
5935 : {
5936 : done = true;
5937 : break;
5938 : }
5939 : i -= s;
5940 : }
5941 : #else
5942 : /* sigwait() will handle SIGINT. */
5943 24 : sigprocmask(SIG_BLOCK, &sigint, NULL);
5944 24 : if (cancel_pressed)
5945 0 : done = true;
5946 :
5947 : /* Wait for SIGINT, SIGCHLD or SIGALRM. */
5948 24 : while (!done)
5949 : {
5950 : int signal_received;
5951 :
5952 24 : errno = sigwait(&sigalrm_sigchld_sigint, &signal_received);
5953 24 : if (errno != 0)
5954 : {
5955 : /* Some other signal arrived? */
5956 0 : if (errno == EINTR)
5957 0 : continue;
5958 : else
5959 : {
5960 0 : pg_log_error("could not wait for signals: %m");
5961 0 : done = true;
5962 24 : break;
5963 : }
5964 : }
5965 : /* On ^C or pager exit, it's time to stop running the query. */
5966 24 : if (signal_received == SIGINT || signal_received == SIGCHLD)
5967 0 : done = true;
5968 : /* Otherwise, we must have SIGALRM. Time to run the query again. */
5969 24 : break;
5970 : }
5971 :
5972 : /* Unblock SIGINT so that slow queries can be interrupted. */
5973 24 : sigprocmask(SIG_UNBLOCK, &sigint, NULL);
5974 : #endif
5975 : }
5976 :
5977 8 : if (pagerpipe)
5978 : {
5979 0 : pclose(pagerpipe);
5980 0 : restore_sigpipe_trap();
5981 : }
5982 : else
5983 : {
5984 : /*
5985 : * If the terminal driver echoed "^C", libedit/libreadline might be
5986 : * confused about the cursor position. Therefore, inject a newline
5987 : * before the next prompt is displayed. We only do this when not
5988 : * using a pager, because pagers are expected to restore the screen to
5989 : * a sane state on exit.
5990 : */
5991 8 : fprintf(stdout, "\n");
5992 8 : fflush(stdout);
5993 : }
5994 :
5995 : #ifndef WIN32
5996 : /* Disable the interval timer. */
5997 8 : memset(&interval, 0, sizeof(interval));
5998 8 : setitimer(ITIMER_REAL, &interval, NULL);
5999 : /* Unblock SIGINT, SIGCHLD and SIGALRM. */
6000 8 : sigprocmask(SIG_UNBLOCK, &sigalrm_sigchld_sigint, NULL);
6001 : #endif
6002 :
6003 8 : pg_free(title);
6004 8 : return (res >= 0);
6005 : }
6006 :
6007 : /*
6008 : * a little code borrowed from PSQLexec() to manage ECHO_HIDDEN output.
6009 : * returns true unless we have ECHO_HIDDEN_NOEXEC.
6010 : */
6011 : static bool
6012 372 : echo_hidden_command(const char *query)
6013 : {
6014 372 : if (pset.echo_hidden != PSQL_ECHO_HIDDEN_OFF)
6015 : {
6016 0 : printf(_("/******** QUERY *********/\n"
6017 : "%s\n"
6018 : "/************************/\n\n"), query);
6019 0 : fflush(stdout);
6020 0 : if (pset.logfile)
6021 : {
6022 0 : fprintf(pset.logfile,
6023 0 : _("/******** QUERY *********/\n"
6024 : "%s\n"
6025 : "/************************/\n\n"), query);
6026 0 : fflush(pset.logfile);
6027 : }
6028 :
6029 0 : if (pset.echo_hidden == PSQL_ECHO_HIDDEN_NOEXEC)
6030 0 : return false;
6031 : }
6032 372 : return true;
6033 : }
6034 :
6035 : /*
6036 : * Look up the object identified by obj_type and desc. If successful,
6037 : * store its OID in *obj_oid and return true, else return false.
6038 : *
6039 : * Note that we'll fail if the object doesn't exist OR if there are multiple
6040 : * matching candidates OR if there's something syntactically wrong with the
6041 : * object description; unfortunately it can be hard to tell the difference.
6042 : */
6043 : static bool
6044 186 : lookup_object_oid(EditableObjectType obj_type, const char *desc,
6045 : Oid *obj_oid)
6046 : {
6047 186 : bool result = true;
6048 186 : PQExpBuffer query = createPQExpBuffer();
6049 : PGresult *res;
6050 :
6051 186 : switch (obj_type)
6052 : {
6053 60 : case EditableFunction:
6054 :
6055 : /*
6056 : * We have a function description, e.g. "x" or "x(int)". Issue a
6057 : * query to retrieve the function's OID using a cast to regproc or
6058 : * regprocedure (as appropriate).
6059 : */
6060 60 : appendPQExpBufferStr(query, "SELECT ");
6061 60 : appendStringLiteralConn(query, desc, pset.db);
6062 60 : appendPQExpBuffer(query, "::pg_catalog.%s::pg_catalog.oid",
6063 60 : strchr(desc, '(') ? "regprocedure" : "regproc");
6064 60 : break;
6065 :
6066 126 : case EditableView:
6067 :
6068 : /*
6069 : * Convert view name (possibly schema-qualified) to OID. Note:
6070 : * this code doesn't check if the relation is actually a view.
6071 : * We'll detect that in get_create_object_cmd().
6072 : */
6073 126 : appendPQExpBufferStr(query, "SELECT ");
6074 126 : appendStringLiteralConn(query, desc, pset.db);
6075 126 : appendPQExpBufferStr(query, "::pg_catalog.regclass::pg_catalog.oid");
6076 126 : break;
6077 : }
6078 :
6079 186 : if (!echo_hidden_command(query->data))
6080 : {
6081 0 : destroyPQExpBuffer(query);
6082 0 : return false;
6083 : }
6084 186 : res = PQexec(pset.db, query->data);
6085 186 : if (PQresultStatus(res) == PGRES_TUPLES_OK && PQntuples(res) == 1)
6086 186 : *obj_oid = atooid(PQgetvalue(res, 0, 0));
6087 : else
6088 : {
6089 0 : minimal_error_message(res);
6090 0 : result = false;
6091 : }
6092 :
6093 186 : PQclear(res);
6094 186 : destroyPQExpBuffer(query);
6095 :
6096 186 : return result;
6097 : }
6098 :
6099 : /*
6100 : * Construct a "CREATE OR REPLACE ..." command that describes the specified
6101 : * database object. If successful, the result is stored in buf.
6102 : */
6103 : static bool
6104 186 : get_create_object_cmd(EditableObjectType obj_type, Oid oid,
6105 : PQExpBuffer buf)
6106 : {
6107 186 : bool result = true;
6108 186 : PQExpBuffer query = createPQExpBuffer();
6109 : PGresult *res;
6110 :
6111 186 : switch (obj_type)
6112 : {
6113 60 : case EditableFunction:
6114 60 : printfPQExpBuffer(query,
6115 : "SELECT pg_catalog.pg_get_functiondef(%u)",
6116 : oid);
6117 60 : break;
6118 :
6119 126 : case EditableView:
6120 :
6121 : /*
6122 : * pg_get_viewdef() just prints the query, so we must prepend
6123 : * CREATE for ourselves. We must fully qualify the view name to
6124 : * ensure the right view gets replaced. Also, check relation kind
6125 : * to be sure it's a view.
6126 : *
6127 : * Starting with PG 9.4, views may have WITH [LOCAL|CASCADED]
6128 : * CHECK OPTION. These are not part of the view definition
6129 : * returned by pg_get_viewdef() and so need to be retrieved
6130 : * separately. Materialized views (introduced in 9.3) may have
6131 : * arbitrary storage parameter reloptions.
6132 : */
6133 126 : if (pset.sversion >= 90400)
6134 : {
6135 126 : printfPQExpBuffer(query,
6136 : "SELECT nspname, relname, relkind, "
6137 : "pg_catalog.pg_get_viewdef(c.oid, true), "
6138 : "pg_catalog.array_remove(pg_catalog.array_remove(c.reloptions,'check_option=local'),'check_option=cascaded') AS reloptions, "
6139 : "CASE WHEN 'check_option=local' = ANY (c.reloptions) THEN 'LOCAL'::text "
6140 : "WHEN 'check_option=cascaded' = ANY (c.reloptions) THEN 'CASCADED'::text ELSE NULL END AS checkoption "
6141 : "FROM pg_catalog.pg_class c "
6142 : "LEFT JOIN pg_catalog.pg_namespace n "
6143 : "ON c.relnamespace = n.oid WHERE c.oid = %u",
6144 : oid);
6145 : }
6146 : else
6147 : {
6148 0 : printfPQExpBuffer(query,
6149 : "SELECT nspname, relname, relkind, "
6150 : "pg_catalog.pg_get_viewdef(c.oid, true), "
6151 : "c.reloptions AS reloptions, "
6152 : "NULL AS checkoption "
6153 : "FROM pg_catalog.pg_class c "
6154 : "LEFT JOIN pg_catalog.pg_namespace n "
6155 : "ON c.relnamespace = n.oid WHERE c.oid = %u",
6156 : oid);
6157 : }
6158 126 : break;
6159 : }
6160 :
6161 186 : if (!echo_hidden_command(query->data))
6162 : {
6163 0 : destroyPQExpBuffer(query);
6164 0 : return false;
6165 : }
6166 186 : res = PQexec(pset.db, query->data);
6167 186 : if (PQresultStatus(res) == PGRES_TUPLES_OK && PQntuples(res) == 1)
6168 : {
6169 186 : resetPQExpBuffer(buf);
6170 186 : switch (obj_type)
6171 : {
6172 60 : case EditableFunction:
6173 60 : appendPQExpBufferStr(buf, PQgetvalue(res, 0, 0));
6174 60 : break;
6175 :
6176 126 : case EditableView:
6177 : {
6178 126 : char *nspname = PQgetvalue(res, 0, 0);
6179 126 : char *relname = PQgetvalue(res, 0, 1);
6180 126 : char *relkind = PQgetvalue(res, 0, 2);
6181 126 : char *viewdef = PQgetvalue(res, 0, 3);
6182 126 : char *reloptions = PQgetvalue(res, 0, 4);
6183 126 : char *checkoption = PQgetvalue(res, 0, 5);
6184 :
6185 : /*
6186 : * If the backend ever supports CREATE OR REPLACE
6187 : * MATERIALIZED VIEW, allow that here; but as of today it
6188 : * does not, so editing a matview definition in this way
6189 : * is impossible.
6190 : */
6191 126 : switch (relkind[0])
6192 : {
6193 : #ifdef NOT_USED
6194 : case RELKIND_MATVIEW:
6195 : appendPQExpBufferStr(buf, "CREATE OR REPLACE MATERIALIZED VIEW ");
6196 : break;
6197 : #endif
6198 126 : case RELKIND_VIEW:
6199 126 : appendPQExpBufferStr(buf, "CREATE OR REPLACE VIEW ");
6200 126 : break;
6201 0 : default:
6202 0 : pg_log_error("\"%s.%s\" is not a view",
6203 : nspname, relname);
6204 0 : result = false;
6205 0 : break;
6206 : }
6207 126 : appendPQExpBuffer(buf, "%s.", fmtId(nspname));
6208 126 : appendPQExpBufferStr(buf, fmtId(relname));
6209 :
6210 : /* reloptions, if not an empty array "{}" */
6211 126 : if (reloptions != NULL && strlen(reloptions) > 2)
6212 : {
6213 0 : appendPQExpBufferStr(buf, "\n WITH (");
6214 0 : if (!appendReloptionsArray(buf, reloptions, "",
6215 : pset.encoding,
6216 0 : standard_strings()))
6217 : {
6218 0 : pg_log_error("could not parse reloptions array");
6219 0 : result = false;
6220 : }
6221 0 : appendPQExpBufferChar(buf, ')');
6222 : }
6223 :
6224 : /* View definition from pg_get_viewdef (a SELECT query) */
6225 126 : appendPQExpBuffer(buf, " AS\n%s", viewdef);
6226 :
6227 : /* Get rid of the semicolon that pg_get_viewdef appends */
6228 126 : if (buf->len > 0 && buf->data[buf->len - 1] == ';')
6229 126 : buf->data[--(buf->len)] = '\0';
6230 :
6231 : /* WITH [LOCAL|CASCADED] CHECK OPTION */
6232 126 : if (checkoption && checkoption[0] != '\0')
6233 0 : appendPQExpBuffer(buf, "\n WITH %s CHECK OPTION",
6234 : checkoption);
6235 : }
6236 126 : break;
6237 : }
6238 : /* Make sure result ends with a newline */
6239 186 : if (buf->len > 0 && buf->data[buf->len - 1] != '\n')
6240 126 : appendPQExpBufferChar(buf, '\n');
6241 : }
6242 : else
6243 : {
6244 0 : minimal_error_message(res);
6245 0 : result = false;
6246 : }
6247 :
6248 186 : PQclear(res);
6249 186 : destroyPQExpBuffer(query);
6250 :
6251 186 : return result;
6252 : }
6253 :
6254 : /*
6255 : * If the given argument of \ef or \ev ends with a line number, delete the line
6256 : * number from the argument string and return it as an integer. (We need
6257 : * this kluge because we're too lazy to parse \ef's function or \ev's view
6258 : * argument carefully --- we just slop it up in OT_WHOLE_LINE mode.)
6259 : *
6260 : * Returns -1 if no line number is present, 0 on error, or a positive value
6261 : * on success.
6262 : */
6263 : static int
6264 0 : strip_lineno_from_objdesc(char *obj)
6265 : {
6266 : char *c;
6267 : int lineno;
6268 :
6269 0 : if (!obj || obj[0] == '\0')
6270 0 : return -1;
6271 :
6272 0 : c = obj + strlen(obj) - 1;
6273 :
6274 : /*
6275 : * This business of parsing backwards is dangerous as can be in a
6276 : * multibyte environment: there is no reason to believe that we are
6277 : * looking at the first byte of a character, nor are we necessarily
6278 : * working in a "safe" encoding. Fortunately the bitpatterns we are
6279 : * looking for are unlikely to occur as non-first bytes, but beware of
6280 : * trying to expand the set of cases that can be recognized. We must
6281 : * guard the <ctype.h> macros by using isascii() first, too.
6282 : */
6283 :
6284 : /* skip trailing whitespace */
6285 0 : while (c > obj && isascii((unsigned char) *c) && isspace((unsigned char) *c))
6286 0 : c--;
6287 :
6288 : /* must have a digit as last non-space char */
6289 0 : if (c == obj || !isascii((unsigned char) *c) || !isdigit((unsigned char) *c))
6290 0 : return -1;
6291 :
6292 : /* find start of digit string */
6293 0 : while (c > obj && isascii((unsigned char) *c) && isdigit((unsigned char) *c))
6294 0 : c--;
6295 :
6296 : /* digits must be separated from object name by space or closing paren */
6297 : /* notice also that we are not allowing an empty object name ... */
6298 0 : if (c == obj || !isascii((unsigned char) *c) ||
6299 0 : !(isspace((unsigned char) *c) || *c == ')'))
6300 0 : return -1;
6301 :
6302 : /* parse digit string */
6303 0 : c++;
6304 0 : lineno = atoi(c);
6305 0 : if (lineno < 1)
6306 : {
6307 0 : pg_log_error("invalid line number: %s", c);
6308 0 : return 0;
6309 : }
6310 :
6311 : /* strip digit string from object name */
6312 0 : *c = '\0';
6313 :
6314 0 : return lineno;
6315 : }
6316 :
6317 : /*
6318 : * Count number of lines in the buffer.
6319 : * This is used to test if pager is needed or not.
6320 : */
6321 : static int
6322 186 : count_lines_in_buf(PQExpBuffer buf)
6323 : {
6324 186 : int lineno = 0;
6325 186 : const char *lines = buf->data;
6326 :
6327 2896 : while (*lines != '\0')
6328 : {
6329 2710 : lineno++;
6330 : /* find start of next line */
6331 2710 : lines = strchr(lines, '\n');
6332 2710 : if (!lines)
6333 0 : break;
6334 2710 : lines++;
6335 : }
6336 :
6337 186 : return lineno;
6338 : }
6339 :
6340 : /*
6341 : * Write text at *lines to output with line numbers.
6342 : *
6343 : * For functions, lineno "1" should correspond to the first line of the
6344 : * function body; lines before that are unnumbered. We expect that
6345 : * pg_get_functiondef() will emit that on a line beginning with "AS ",
6346 : * "BEGIN ", or "RETURN ", and that there can be no such line before
6347 : * the real start of the function body.
6348 : *
6349 : * Caution: this scribbles on *lines.
6350 : */
6351 : static void
6352 18 : print_with_linenumbers(FILE *output, char *lines, bool is_func)
6353 : {
6354 18 : bool in_header = is_func;
6355 18 : int lineno = 0;
6356 :
6357 192 : while (*lines != '\0')
6358 : {
6359 : char *eol;
6360 :
6361 174 : if (in_header &&
6362 90 : (strncmp(lines, "AS ", 3) == 0 ||
6363 90 : strncmp(lines, "BEGIN ", 6) == 0 ||
6364 78 : strncmp(lines, "RETURN ", 7) == 0))
6365 18 : in_header = false;
6366 :
6367 : /* increment lineno only for body's lines */
6368 174 : if (!in_header)
6369 102 : lineno++;
6370 :
6371 : /* find and mark end of current line */
6372 174 : eol = strchr(lines, '\n');
6373 174 : if (eol != NULL)
6374 174 : *eol = '\0';
6375 :
6376 : /* show current line as appropriate */
6377 174 : if (in_header)
6378 72 : fprintf(output, " %s\n", lines);
6379 : else
6380 102 : fprintf(output, "%-7d %s\n", lineno, lines);
6381 :
6382 : /* advance to next line, if any */
6383 174 : if (eol == NULL)
6384 0 : break;
6385 174 : lines = ++eol;
6386 : }
6387 18 : }
6388 :
6389 : /*
6390 : * Report just the primary error; this is to avoid cluttering the output
6391 : * with, for instance, a redisplay of the internally generated query
6392 : */
6393 : static void
6394 0 : minimal_error_message(PGresult *res)
6395 : {
6396 : PQExpBuffer msg;
6397 : const char *fld;
6398 :
6399 0 : msg = createPQExpBuffer();
6400 :
6401 0 : fld = PQresultErrorField(res, PG_DIAG_SEVERITY);
6402 0 : if (fld)
6403 0 : printfPQExpBuffer(msg, "%s: ", fld);
6404 : else
6405 0 : printfPQExpBuffer(msg, "ERROR: ");
6406 0 : fld = PQresultErrorField(res, PG_DIAG_MESSAGE_PRIMARY);
6407 0 : if (fld)
6408 0 : appendPQExpBufferStr(msg, fld);
6409 : else
6410 0 : appendPQExpBufferStr(msg, "(not available)");
6411 0 : appendPQExpBufferChar(msg, '\n');
6412 :
6413 0 : pg_log_error("%s", msg->data);
6414 :
6415 0 : destroyPQExpBuffer(msg);
6416 0 : }
|