PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
command.h File Reference
Include dependency graph for command.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef enum _backslashResult backslashResult
 

Enumerations

enum  _backslashResult {
  PSQL_CMD_UNKNOWN = 0 , PSQL_CMD_SEND , PSQL_CMD_SKIP_LINE , PSQL_CMD_TERMINATE ,
  PSQL_CMD_NEWEDIT , PSQL_CMD_ERROR
}
 

Functions

backslashResult HandleSlashCmds (PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf, PQExpBuffer previous_buf)
 
int process_file (char *filename, bool use_relative_path)
 
bool do_pset (const char *param, const char *value, printQueryOpt *popt, bool quiet)
 
printQueryOptsavePsetInfo (const printQueryOpt *popt)
 
void restorePsetInfo (printQueryOpt *popt, printQueryOpt *save)
 
void connection_warnings (bool in_startup)
 
void SyncVariables (void)
 
void UnsyncVariables (void)
 

Typedef Documentation

◆ backslashResult

Enumeration Type Documentation

◆ _backslashResult

Enumerator
PSQL_CMD_UNKNOWN 
PSQL_CMD_SEND 
PSQL_CMD_SKIP_LINE 
PSQL_CMD_TERMINATE 
PSQL_CMD_NEWEDIT 
PSQL_CMD_ERROR 

Definition at line 15 of file command.h.

16{
17 PSQL_CMD_UNKNOWN = 0, /* not done parsing yet (internal only) */
18 PSQL_CMD_SEND, /* query complete; send off */
19 PSQL_CMD_SKIP_LINE, /* keep building query */
20 PSQL_CMD_TERMINATE, /* quit program */
21 PSQL_CMD_NEWEDIT, /* query buffer was changed (e.g., via \e) */
22 PSQL_CMD_ERROR, /* the execution of the backslash command
23 * resulted in an error */
@ PSQL_CMD_TERMINATE
Definition: command.h:20
@ PSQL_CMD_UNKNOWN
Definition: command.h:17
@ PSQL_CMD_NEWEDIT
Definition: command.h:21
@ PSQL_CMD_ERROR
Definition: command.h:22
@ PSQL_CMD_SEND
Definition: command.h:18
@ PSQL_CMD_SKIP_LINE
Definition: command.h:19
enum _backslashResult backslashResult

Function Documentation

◆ connection_warnings()

void connection_warnings ( bool  in_startup)

Definition at line 4441 of file command.c.

4442{
4443 if (!pset.quiet && !pset.notty)
4444 {
4445 int client_ver = PG_VERSION_NUM;
4446 char cverbuf[32];
4447 char sverbuf[32];
4448
4449 if (pset.sversion != client_ver)
4450 {
4451 const char *server_version;
4452
4453 /* Try to get full text form, might include "devel" etc */
4454 server_version = PQparameterStatus(pset.db, "server_version");
4455 /* Otherwise fall back on pset.sversion */
4456 if (!server_version)
4457 {
4459 sverbuf, sizeof(sverbuf));
4460 server_version = sverbuf;
4461 }
4462
4463 printf(_("%s (%s, server %s)\n"),
4464 pset.progname, PG_VERSION, server_version);
4465 }
4466 /* For version match, only print psql banner on startup. */
4467 else if (in_startup)
4468 printf("%s (%s)\n", pset.progname, PG_VERSION);
4469
4470 /*
4471 * Warn if server's major version is newer than ours, or if server
4472 * predates our support cutoff (currently 9.2).
4473 */
4474 if (pset.sversion / 100 > client_ver / 100 ||
4475 pset.sversion < 90200)
4476 printf(_("WARNING: %s major version %s, server major version %s.\n"
4477 " Some psql features might not work.\n"),
4478 pset.progname,
4479 formatPGVersionNumber(client_ver, false,
4480 cverbuf, sizeof(cverbuf)),
4482 sverbuf, sizeof(sverbuf)));
4483
4484#ifdef WIN32
4485 if (in_startup)
4486 checkWin32Codepage();
4487#endif
4488 printSSLInfo();
4489 printGSSInfo();
4490 }
4491}
static void printSSLInfo(void)
Definition: command.c:4500
static void printGSSInfo(void)
Definition: command.c:4528
#define _(x)
Definition: elog.c:91
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:7634
static int server_version
Definition: pg_dumpall.c:109
#define printf(...)
Definition: port.h:245
PsqlSettings pset
Definition: startup.c:32
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:313
PGconn * db
Definition: settings.h:103
const char * progname
Definition: settings.h:142

References _, _psqlSettings::db, formatPGVersionNumber(), _psqlSettings::notty, PQparameterStatus(), printf, printGSSInfo(), printSSLInfo(), _psqlSettings::progname, pset, _psqlSettings::quiet, server_version, and _psqlSettings::sversion.

Referenced by CheckConnection(), do_connect(), and main().

◆ do_pset()

bool do_pset ( const char *  param,
const char *  value,
printQueryOpt popt,
bool  quiet 
)

Definition at line 5076 of file command.c.

5077{
5078 size_t vallen = 0;
5079
5080 Assert(param != NULL);
5081
5082 if (value)
5083 vallen = strlen(value);
5084
5085 /* set format */
5086 if (strcmp(param, "format") == 0)
5087 {
5088 static const struct fmt
5089 {
5090 const char *name;
5091 enum printFormat number;
5092 } formats[] =
5093 {
5094 /* remember to update error message below when adding more */
5095 {"aligned", PRINT_ALIGNED},
5096 {"asciidoc", PRINT_ASCIIDOC},
5097 {"csv", PRINT_CSV},
5098 {"html", PRINT_HTML},
5099 {"latex", PRINT_LATEX},
5100 {"troff-ms", PRINT_TROFF_MS},
5101 {"unaligned", PRINT_UNALIGNED},
5102 {"wrapped", PRINT_WRAPPED}
5103 };
5104
5105 if (!value)
5106 ;
5107 else
5108 {
5109 int match_pos = -1;
5110
5111 for (int i = 0; i < lengthof(formats); i++)
5112 {
5113 if (pg_strncasecmp(formats[i].name, value, vallen) == 0)
5114 {
5115 if (match_pos < 0)
5116 match_pos = i;
5117 else
5118 {
5119 pg_log_error("\\pset: ambiguous abbreviation \"%s\" matches both \"%s\" and \"%s\"",
5120 value,
5121 formats[match_pos].name, formats[i].name);
5122 return false;
5123 }
5124 }
5125 }
5126 if (match_pos >= 0)
5127 popt->topt.format = formats[match_pos].number;
5128 else if (pg_strncasecmp("latex-longtable", value, vallen) == 0)
5129 {
5130 /*
5131 * We must treat latex-longtable specially because latex is a
5132 * prefix of it; if both were in the table above, we'd think
5133 * "latex" is ambiguous.
5134 */
5136 }
5137 else
5138 {
5139 pg_log_error("\\pset: allowed formats are aligned, asciidoc, csv, html, latex, latex-longtable, troff-ms, unaligned, wrapped");
5140 return false;
5141 }
5142 }
5143 }
5144
5145 /* set table line style */
5146 else if (strcmp(param, "linestyle") == 0)
5147 {
5148 if (!value)
5149 ;
5150 else if (pg_strncasecmp("ascii", value, vallen) == 0)
5152 else if (pg_strncasecmp("old-ascii", value, vallen) == 0)
5154 else if (pg_strncasecmp("unicode", value, vallen) == 0)
5155 popt->topt.line_style = &pg_utf8format;
5156 else
5157 {
5158 pg_log_error("\\pset: allowed line styles are ascii, old-ascii, unicode");
5159 return false;
5160 }
5161 }
5162
5163 /* set unicode border line style */
5164 else if (strcmp(param, "unicode_border_linestyle") == 0)
5165 {
5166 if (!value)
5167 ;
5168 else if (set_unicode_line_style(value, vallen,
5170 refresh_utf8format(&(popt->topt));
5171 else
5172 {
5173 pg_log_error("\\pset: allowed Unicode border line styles are single, double");
5174 return false;
5175 }
5176 }
5177
5178 /* set unicode column line style */
5179 else if (strcmp(param, "unicode_column_linestyle") == 0)
5180 {
5181 if (!value)
5182 ;
5183 else if (set_unicode_line_style(value, vallen,
5185 refresh_utf8format(&(popt->topt));
5186 else
5187 {
5188 pg_log_error("\\pset: allowed Unicode column line styles are single, double");
5189 return false;
5190 }
5191 }
5192
5193 /* set unicode header line style */
5194 else if (strcmp(param, "unicode_header_linestyle") == 0)
5195 {
5196 if (!value)
5197 ;
5198 else if (set_unicode_line_style(value, vallen,
5200 refresh_utf8format(&(popt->topt));
5201 else
5202 {
5203 pg_log_error("\\pset: allowed Unicode header line styles are single, double");
5204 return false;
5205 }
5206 }
5207
5208 /* set border style/width */
5209 else if (strcmp(param, "border") == 0)
5210 {
5211 if (value)
5212 popt->topt.border = atoi(value);
5213 }
5214
5215 /* set expanded/vertical mode */
5216 else if (strcmp(param, "x") == 0 ||
5217 strcmp(param, "expanded") == 0 ||
5218 strcmp(param, "vertical") == 0)
5219 {
5220 if (value && pg_strcasecmp(value, "auto") == 0)
5221 popt->topt.expanded = 2;
5222 else if (value)
5223 {
5224 bool on_off;
5225
5226 if (ParseVariableBool(value, NULL, &on_off))
5227 popt->topt.expanded = on_off ? 1 : 0;
5228 else
5229 {
5230 PsqlVarEnumError(param, value, "on, off, auto");
5231 return false;
5232 }
5233 }
5234 else
5235 popt->topt.expanded = !popt->topt.expanded;
5236 }
5237
5238 /* header line width in expanded mode */
5239 else if (strcmp(param, "xheader_width") == 0)
5240 {
5241 if (!value)
5242 ;
5243 else if (pg_strcasecmp(value, "full") == 0)
5245 else if (pg_strcasecmp(value, "column") == 0)
5247 else if (pg_strcasecmp(value, "page") == 0)
5249 else
5250 {
5251 int intval = atoi(value);
5252
5253 if (intval == 0)
5254 {
5255 pg_log_error("\\pset: allowed xheader_width values are \"%s\" (default), \"%s\", \"%s\", or a number specifying the exact width", "full", "column", "page");
5256 return false;
5257 }
5258
5260 popt->topt.expanded_header_exact_width = intval;
5261 }
5262 }
5263
5264 /* field separator for CSV format */
5265 else if (strcmp(param, "csv_fieldsep") == 0)
5266 {
5267 if (value)
5268 {
5269 /* CSV separator has to be a one-byte character */
5270 if (strlen(value) != 1)
5271 {
5272 pg_log_error("\\pset: csv_fieldsep must be a single one-byte character");
5273 return false;
5274 }
5275 if (value[0] == '"' || value[0] == '\n' || value[0] == '\r')
5276 {
5277 pg_log_error("\\pset: csv_fieldsep cannot be a double quote, a newline, or a carriage return");
5278 return false;
5279 }
5280 popt->topt.csvFieldSep[0] = value[0];
5281 }
5282 }
5283
5284 /* locale-aware numeric output */
5285 else if (strcmp(param, "numericlocale") == 0)
5286 {
5287 if (value)
5288 return ParseVariableBool(value, param, &popt->topt.numericLocale);
5289 else
5290 popt->topt.numericLocale = !popt->topt.numericLocale;
5291 }
5292
5293 /* null display */
5294 else if (strcmp(param, "null") == 0)
5295 {
5296 if (value)
5297 {
5298 free(popt->nullPrint);
5299 popt->nullPrint = pg_strdup(value);
5300 }
5301 }
5302
5303 /* field separator for unaligned text */
5304 else if (strcmp(param, "fieldsep") == 0)
5305 {
5306 if (value)
5307 {
5308 free(popt->topt.fieldSep.separator);
5310 popt->topt.fieldSep.separator_zero = false;
5311 }
5312 }
5313
5314 else if (strcmp(param, "fieldsep_zero") == 0)
5315 {
5316 free(popt->topt.fieldSep.separator);
5317 popt->topt.fieldSep.separator = NULL;
5318 popt->topt.fieldSep.separator_zero = true;
5319 }
5320
5321 /* record separator for unaligned text */
5322 else if (strcmp(param, "recordsep") == 0)
5323 {
5324 if (value)
5325 {
5328 popt->topt.recordSep.separator_zero = false;
5329 }
5330 }
5331
5332 else if (strcmp(param, "recordsep_zero") == 0)
5333 {
5335 popt->topt.recordSep.separator = NULL;
5336 popt->topt.recordSep.separator_zero = true;
5337 }
5338
5339 /* toggle between full and tuples-only format */
5340 else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0)
5341 {
5342 if (value)
5343 return ParseVariableBool(value, param, &popt->topt.tuples_only);
5344 else
5345 popt->topt.tuples_only = !popt->topt.tuples_only;
5346 }
5347
5348 /* set title override */
5349 else if (strcmp(param, "C") == 0 || strcmp(param, "title") == 0)
5350 {
5351 free(popt->title);
5352 if (!value)
5353 popt->title = NULL;
5354 else
5355 popt->title = pg_strdup(value);
5356 }
5357
5358 /* set HTML table tag options */
5359 else if (strcmp(param, "T") == 0 || strcmp(param, "tableattr") == 0)
5360 {
5361 free(popt->topt.tableAttr);
5362 if (!value)
5363 popt->topt.tableAttr = NULL;
5364 else
5365 popt->topt.tableAttr = pg_strdup(value);
5366 }
5367
5368 /* toggle use of pager */
5369 else if (strcmp(param, "pager") == 0)
5370 {
5371 if (value && pg_strcasecmp(value, "always") == 0)
5372 popt->topt.pager = 2;
5373 else if (value)
5374 {
5375 bool on_off;
5376
5377 if (!ParseVariableBool(value, NULL, &on_off))
5378 {
5379 PsqlVarEnumError(param, value, "on, off, always");
5380 return false;
5381 }
5382 popt->topt.pager = on_off ? 1 : 0;
5383 }
5384 else if (popt->topt.pager == 1)
5385 popt->topt.pager = 0;
5386 else
5387 popt->topt.pager = 1;
5388 }
5389
5390 /* set minimum lines for pager use */
5391 else if (strcmp(param, "pager_min_lines") == 0)
5392 {
5393 if (value &&
5394 !ParseVariableNum(value, "pager_min_lines", &popt->topt.pager_min_lines))
5395 return false;
5396 }
5397
5398 /* disable "(x rows)" footer */
5399 else if (strcmp(param, "footer") == 0)
5400 {
5401 if (value)
5402 return ParseVariableBool(value, param, &popt->topt.default_footer);
5403 else
5404 popt->topt.default_footer = !popt->topt.default_footer;
5405 }
5406
5407 /* set border style/width */
5408 else if (strcmp(param, "columns") == 0)
5409 {
5410 if (value)
5411 popt->topt.columns = atoi(value);
5412 }
5413 else
5414 {
5415 pg_log_error("\\pset: unknown option: %s", param);
5416 return false;
5417 }
5418
5419 if (!quiet)
5420 printPsetInfo(param, &pset.popt);
5421
5422 return true;
5423}
#define lengthof(array)
Definition: c.h:787
static bool set_unicode_line_style(const char *value, size_t vallen, unicode_linestyle *linestyle)
Definition: command.c:5033
static bool printPsetInfo(const char *param, printQueryOpt *popt)
Definition: command.c:5429
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void refresh_utf8format(const printTableOpt *opt)
Definition: print.c:3884
const printTextFormat pg_asciiformat
Definition: print.c:61
const printTextFormat pg_asciiformat_old
Definition: print.c:82
printTextFormat pg_utf8format
Definition: print.c:104
@ PRINT_XHEADER_EXACT_WIDTH
Definition: print.h:78
@ PRINT_XHEADER_PAGE
Definition: print.h:76
@ PRINT_XHEADER_COLUMN
Definition: print.h:74
@ PRINT_XHEADER_FULL
Definition: print.h:72
printFormat
Definition: print.h:29
@ PRINT_LATEX_LONGTABLE
Definition: print.h:36
@ PRINT_CSV
Definition: print.h:33
@ PRINT_UNALIGNED
Definition: print.h:38
@ PRINT_ALIGNED
Definition: print.h:31
@ PRINT_TROFF_MS
Definition: print.h:37
@ PRINT_ASCIIDOC
Definition: print.h:32
@ PRINT_LATEX
Definition: print.h:35
@ PRINT_HTML
Definition: print.h:34
@ PRINT_WRAPPED
Definition: print.h:39
Assert(PointerIsAligned(start, uint64))
#define free(a)
Definition: header.h:65
static struct @169 value
int i
Definition: isn.c:77
#define pg_log_error(...)
Definition: logging.h:106
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69
printQueryOpt popt
Definition: settings.h:112
printTableOpt topt
Definition: print.h:185
char * nullPrint
Definition: print.h:186
char * title
Definition: print.h:187
unsigned short int expanded
Definition: print.h:114
unicode_linestyle unicode_border_linestyle
Definition: print.h:141
bool tuples_only
Definition: print.h:126
int columns
Definition: print.h:140
enum printFormat format
Definition: print.h:113
struct separator fieldSep
Definition: print.h:132
int expanded_header_exact_width
Definition: print.h:118
struct separator recordSep
Definition: print.h:133
printXheaderWidthType expanded_header_width_type
Definition: print.h:116
char csvFieldSep[2]
Definition: print.h:134
const printTextFormat * line_style
Definition: print.h:131
bool default_footer
Definition: print.h:129
int pager_min_lines
Definition: print.h:124
unsigned short int pager
Definition: print.h:122
char * tableAttr
Definition: print.h:137
bool numericLocale
Definition: print.h:135
unsigned short int border
Definition: print.h:120
unicode_linestyle unicode_header_linestyle
Definition: print.h:143
unicode_linestyle unicode_column_linestyle
Definition: print.h:142
bool separator_zero
Definition: print.h:108
char * separator
Definition: print.h:107
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition: variables.c:486
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:109
bool ParseVariableNum(const char *value, const char *name, int *result)
Definition: variables.c:158
const char * name

References Assert(), printTableOpt::border, printTableOpt::columns, printTableOpt::csvFieldSep, printTableOpt::default_footer, printTableOpt::expanded, printTableOpt::expanded_header_exact_width, printTableOpt::expanded_header_width_type, printTableOpt::fieldSep, printTableOpt::format, free, i, lengthof, printTableOpt::line_style, name, printQueryOpt::nullPrint, printTableOpt::numericLocale, printTableOpt::pager, printTableOpt::pager_min_lines, ParseVariableBool(), ParseVariableNum(), pg_asciiformat, pg_asciiformat_old, pg_log_error, pg_strcasecmp(), pg_strdup(), pg_strncasecmp(), pg_utf8format, _psqlSettings::popt, PRINT_ALIGNED, PRINT_ASCIIDOC, PRINT_CSV, PRINT_HTML, PRINT_LATEX, PRINT_LATEX_LONGTABLE, PRINT_TROFF_MS, PRINT_UNALIGNED, PRINT_WRAPPED, PRINT_XHEADER_COLUMN, PRINT_XHEADER_EXACT_WIDTH, PRINT_XHEADER_FULL, PRINT_XHEADER_PAGE, printPsetInfo(), pset, PsqlVarEnumError(), printTableOpt::recordSep, refresh_utf8format(), separator::separator, separator::separator_zero, set_unicode_line_style(), printTableOpt::tableAttr, printQueryOpt::title, printQueryOpt::topt, printTableOpt::tuples_only, printTableOpt::unicode_border_linestyle, printTableOpt::unicode_column_linestyle, printTableOpt::unicode_header_linestyle, and value.

Referenced by exec_command_a(), exec_command_C(), exec_command_f(), exec_command_html(), exec_command_pset(), exec_command_t(), exec_command_T(), exec_command_x(), parse_psql_options(), and process_command_g_options().

◆ HandleSlashCmds()

backslashResult HandleSlashCmds ( PsqlScanState  scan_state,
ConditionalStack  cstack,
PQExpBuffer  query_buf,
PQExpBuffer  previous_buf 
)

Definition at line 231 of file command.c.

235{
236 backslashResult status;
237 char *cmd;
238 char *arg;
239
240 Assert(scan_state != NULL);
241 Assert(cstack != NULL);
242
243 /* Parse off the command name */
244 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 */
252 if (restricted && strcmp(cmd, "unrestrict") != 0)
253 {
254 pg_log_error("backslash commands are restricted; only \\unrestrict is allowed");
255 status = PSQL_CMD_ERROR;
256 }
257 else
258 status = exec_command(cmd, scan_state, cstack, query_buf, previous_buf);
259
260 if (status == PSQL_CMD_UNKNOWN)
261 {
262 pg_log_error("invalid command \\%s", cmd);
264 pg_log_error_hint("Try \\? for help.");
265 status = PSQL_CMD_ERROR;
266 }
267
268 if (status != PSQL_CMD_ERROR)
269 {
270 /*
271 * Eat any remaining arguments after a valid command. We want to
272 * suppress evaluation of backticks in this situation, so transiently
273 * push an inactive conditional-stack entry.
274 */
275 bool active_branch = conditional_active(cstack);
276
278 while ((arg = psql_scan_slash_option(scan_state,
279 OT_NORMAL, NULL, false)))
280 {
281 if (active_branch)
282 pg_log_warning("\\%s: extra argument \"%s\" ignored", cmd, arg);
283 free(arg);
284 }
285 conditional_stack_pop(cstack);
286 }
287 else
288 {
289 /* silently throw away rest of line after an erroneous command */
290 while ((arg = psql_scan_slash_option(scan_state,
291 OT_WHOLE_LINE, NULL, false)))
292 free(arg);
293 }
294
295 /* if there is a trailing \\, swallow it */
296 psql_scan_slash_command_end(scan_state);
297
298 free(cmd);
299
300 /* some commands write to queryFout, so make sure output is sent */
301 fflush(pset.queryFout);
302
303 return status;
304}
static bool restricted
Definition: command.c:199
static backslashResult exec_command(const char *cmd, PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:315
void conditional_stack_push(ConditionalStack cstack, ifState new_state)
Definition: conditional.c:53
bool conditional_stack_pop(ConditionalStack cstack)
Definition: conditional.c:69
bool conditional_active(ConditionalStack cstack)
Definition: conditional.c:140
@ IFSTATE_IGNORED
Definition: conditional.h:37
#define pg_log_error_hint(...)
Definition: logging.h:112
void * arg
#define pg_log_warning(...)
Definition: pgfnames.c:24
void psql_scan_slash_command_end(PsqlScanState state)
@ OT_NORMAL
Definition: psqlscanslash.h:17
@ OT_WHOLE_LINE
Definition: psqlscanslash.h:21
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
char * psql_scan_slash_command(PsqlScanState state)
FILE * queryFout
Definition: settings.h:105
bool cur_cmd_interactive
Definition: settings.h:140

References arg, Assert(), conditional_active(), conditional_stack_pop(), conditional_stack_push(), _psqlSettings::cur_cmd_interactive, exec_command(), free, IFSTATE_IGNORED, OT_NORMAL, OT_WHOLE_LINE, pg_log_error, pg_log_error_hint, pg_log_warning, pset, PSQL_CMD_ERROR, PSQL_CMD_UNKNOWN, psql_scan_slash_command(), psql_scan_slash_command_end(), psql_scan_slash_option(), _psqlSettings::queryFout, and restricted.

Referenced by main(), and MainLoop().

◆ process_file()

int process_file ( char *  filename,
bool  use_relative_path 
)

Definition at line 4923 of file command.c.

4924{
4925 FILE *fd;
4926 int result;
4927 char *oldfilename;
4928 char relpath[MAXPGPATH];
4929
4930 if (!filename)
4931 {
4932 fd = stdin;
4933 filename = NULL;
4934 }
4935 else if (strcmp(filename, "-") != 0)
4936 {
4938
4939 /*
4940 * If we were asked to resolve the pathname relative to the location
4941 * of the currently executing script, and there is one, and this is a
4942 * relative pathname, then prepend all but the last pathname component
4943 * of the current script to this pathname.
4944 */
4945 if (use_relative_path && pset.inputfile &&
4947 {
4952
4953 filename = relpath;
4954 }
4955
4956 fd = fopen(filename, PG_BINARY_R);
4957
4958 if (!fd)
4959 {
4960 pg_log_error("%s: %m", filename);
4961 return EXIT_FAILURE;
4962 }
4963 }
4964 else
4965 {
4966 fd = stdin;
4967 filename = "<stdin>"; /* for future error messages */
4968 }
4969
4970 oldfilename = pset.inputfile;
4972
4974
4975 result = MainLoop(fd);
4976
4977 if (fd != stdin)
4978 fclose(fd);
4979
4980 pset.inputfile = oldfilename;
4981
4983
4984 return result;
4985}
#define PG_BINARY_R
Definition: c.h:1274
void pg_logging_config(int new_flags)
Definition: logging.c:166
#define PG_LOG_FLAG_TERSE
Definition: logging.h:86
int MainLoop(FILE *source)
Definition: mainloop.c:33
#define MAXPGPATH
static char * filename
Definition: pg_dumpall.c:120
void join_path_components(char *ret_path, const char *head, const char *tail)
Definition: path.c:286
#define is_absolute_path(filename)
Definition: port.h:104
void get_parent_directory(char *path)
Definition: path.c:1068
void canonicalize_path_enc(char *path, int encoding)
Definition: path.c:344
bool has_drive_prefix(const char *path)
Definition: path.c:94
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define relpath(rlocator, forknum)
Definition: relpath.h:150
#define EXIT_FAILURE
Definition: settings.h:197
char * inputfile
Definition: settings.h:143

References canonicalize_path_enc(), _psqlSettings::encoding, EXIT_FAILURE, fd(), filename, get_parent_directory(), has_drive_prefix(), _psqlSettings::inputfile, is_absolute_path, join_path_components(), MainLoop(), MAXPGPATH, PG_BINARY_R, pg_log_error, PG_LOG_FLAG_TERSE, pg_logging_config(), pset, relpath, and strlcpy().

Referenced by exec_command_include().

◆ restorePsetInfo()

void restorePsetInfo ( printQueryOpt popt,
printQueryOpt save 
)

Definition at line 5665 of file command.c.

5666{
5667 /* Free all the old data we're about to overwrite the pointers to. */
5668
5669 /* topt.line_style points to const data that need not be duplicated */
5670 free(popt->topt.fieldSep.separator);
5672 free(popt->topt.tableAttr);
5673 free(popt->nullPrint);
5674 free(popt->title);
5675
5676 /*
5677 * footers and translate_columns are never set in psql's print settings,
5678 * so we needn't write code to duplicate them.
5679 */
5680 Assert(popt->footers == NULL);
5681 Assert(popt->translate_columns == NULL);
5682
5683 /* Now we may flat-copy all the fields, including pointers. */
5684 memcpy(popt, save, sizeof(printQueryOpt));
5685
5686 /* Lastly, free "save" ... but its sub-structures now belong to popt. */
5687 free(save);
5688}
const bool * translate_columns
Definition: print.h:190
char ** footers
Definition: print.h:188

References Assert(), printTableOpt::fieldSep, printQueryOpt::footers, free, printQueryOpt::nullPrint, printTableOpt::recordSep, separator::separator, printTableOpt::tableAttr, printQueryOpt::title, printQueryOpt::topt, and printQueryOpt::translate_columns.

Referenced by process_command_g_options(), and SendQuery().

◆ savePsetInfo()

printQueryOpt * savePsetInfo ( const printQueryOpt popt)

Definition at line 5629 of file command.c.

5630{
5631 printQueryOpt *save;
5632
5633 save = (printQueryOpt *) pg_malloc(sizeof(printQueryOpt));
5634
5635 /* Flat-copy all the scalar fields, then duplicate sub-structures. */
5636 memcpy(save, popt, sizeof(printQueryOpt));
5637
5638 /* topt.line_style points to const data that need not be duplicated */
5639 if (popt->topt.fieldSep.separator)
5641 if (popt->topt.recordSep.separator)
5643 if (popt->topt.tableAttr)
5644 save->topt.tableAttr = pg_strdup(popt->topt.tableAttr);
5645 if (popt->nullPrint)
5646 save->nullPrint = pg_strdup(popt->nullPrint);
5647 if (popt->title)
5648 save->title = pg_strdup(popt->title);
5649
5650 /*
5651 * footers and translate_columns are never set in psql's print settings,
5652 * so we needn't write code to duplicate them.
5653 */
5654 Assert(popt->footers == NULL);
5655 Assert(popt->translate_columns == NULL);
5656
5657 return save;
5658}
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47

References Assert(), printTableOpt::fieldSep, printQueryOpt::footers, printQueryOpt::nullPrint, pg_malloc(), pg_strdup(), printTableOpt::recordSep, separator::separator, printTableOpt::tableAttr, printQueryOpt::title, printQueryOpt::topt, and printQueryOpt::translate_columns.

Referenced by exec_command_g(), and process_command_g_options().

◆ SyncVariables()

void SyncVariables ( void  )

Definition at line 4569 of file command.c.

4570{
4571 char vbuf[32];
4572 const char *server_version;
4573 char *service_name;
4574 char *service_file;
4575
4576 /* get stuff from connection */
4580
4582
4583 SetVariable(pset.vars, "DBNAME", PQdb(pset.db));
4584 SetVariable(pset.vars, "USER", PQuser(pset.db));
4585 SetVariable(pset.vars, "HOST", PQhost(pset.db));
4586 SetVariable(pset.vars, "PORT", PQport(pset.db));
4588
4589 service_name = get_conninfo_value("service");
4590 SetVariable(pset.vars, "SERVICE", service_name);
4591 if (service_name)
4592 pg_free(service_name);
4593
4594 service_file = get_conninfo_value("servicefile");
4595 SetVariable(pset.vars, "SERVICEFILE", service_file);
4596 if (service_file)
4597 pg_free(service_file);
4598
4599 /* this bit should match connection_warnings(): */
4600 /* Try to get full text form of version, might include "devel" etc */
4601 server_version = PQparameterStatus(pset.db, "server_version");
4602 /* Otherwise fall back on pset.sversion */
4603 if (!server_version)
4604 {
4605 formatPGVersionNumber(pset.sversion, true, vbuf, sizeof(vbuf));
4606 server_version = vbuf;
4607 }
4608 SetVariable(pset.vars, "SERVER_VERSION_NAME", server_version);
4609
4610 snprintf(vbuf, sizeof(vbuf), "%d", pset.sversion);
4611 SetVariable(pset.vars, "SERVER_VERSION_NUM", vbuf);
4612
4613 /* send stuff to it, too */
4616}
char * get_conninfo_value(const char *keyword)
Definition: common.c:2540
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:7669
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:7513
char * PQport(const PGconn *conn)
Definition: fe-connect.c:7582
char * PQhost(const PGconn *conn)
Definition: fe-connect.c:7546
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:7769
PGContextVisibility PQsetErrorContextVisibility(PGconn *conn, PGContextVisibility show_context)
Definition: fe-connect.c:7831
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:7521
PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity)
Definition: fe-connect.c:7819
void pg_free(void *ptr)
Definition: fe_memutils.c:105
#define pg_encoding_to_char
Definition: pg_wchar.h:630
#define snprintf
Definition: port.h:239
void setFmtEncoding(int encoding)
Definition: string_utils.c:69
VariableSpace vars
Definition: settings.h:151
PGVerbosity verbosity
Definition: settings.h:184
PGContextVisibility show_context
Definition: settings.h:186
int encoding
Definition: print.h:138
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:281

References _psqlSettings::db, _psqlSettings::encoding, printTableOpt::encoding, formatPGVersionNumber(), get_conninfo_value(), pg_encoding_to_char, pg_free(), _psqlSettings::popt, PQclientEncoding(), PQdb(), PQhost(), PQparameterStatus(), PQport(), PQserverVersion(), PQsetErrorContextVisibility(), PQsetErrorVerbosity(), PQuser(), pset, server_version, setFmtEncoding(), SetVariable(), _psqlSettings::show_context, snprintf, _psqlSettings::sversion, printQueryOpt::topt, _psqlSettings::vars, and _psqlSettings::verbosity.

Referenced by CheckConnection(), do_connect(), and main().

◆ UnsyncVariables()

void UnsyncVariables ( void  )

Definition at line 4624 of file command.c.

4625{
4626 SetVariable(pset.vars, "DBNAME", NULL);
4627 SetVariable(pset.vars, "SERVICE", NULL);
4628 SetVariable(pset.vars, "SERVICEFILE", NULL);
4629 SetVariable(pset.vars, "USER", NULL);
4630 SetVariable(pset.vars, "HOST", NULL);
4631 SetVariable(pset.vars, "PORT", NULL);
4632 SetVariable(pset.vars, "ENCODING", NULL);
4633 SetVariable(pset.vars, "SERVER_VERSION_NAME", NULL);
4634 SetVariable(pset.vars, "SERVER_VERSION_NUM", NULL);
4635}

References pset, SetVariable(), and _psqlSettings::vars.

Referenced by CheckConnection(), and do_connect().