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