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