PostgreSQL Source Code  git master
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 3772 of file command.c.

3773 {
3774  if (!pset.quiet && !pset.notty)
3775  {
3776  int client_ver = PG_VERSION_NUM;
3777  char cverbuf[32];
3778  char sverbuf[32];
3779 
3780  if (pset.sversion != client_ver)
3781  {
3782  const char *server_version;
3783 
3784  /* Try to get full text form, might include "devel" etc */
3785  server_version = PQparameterStatus(pset.db, "server_version");
3786  /* Otherwise fall back on pset.sversion */
3787  if (!server_version)
3788  {
3790  sverbuf, sizeof(sverbuf));
3791  server_version = sverbuf;
3792  }
3793 
3794  printf(_("%s (%s, server %s)\n"),
3795  pset.progname, PG_VERSION, server_version);
3796  }
3797  /* For version match, only print psql banner on startup. */
3798  else if (in_startup)
3799  printf("%s (%s)\n", pset.progname, PG_VERSION);
3800 
3801  /*
3802  * Warn if server's major version is newer than ours, or if server
3803  * predates our support cutoff (currently 9.2).
3804  */
3805  if (pset.sversion / 100 > client_ver / 100 ||
3806  pset.sversion < 90200)
3807  printf(_("WARNING: %s major version %s, server major version %s.\n"
3808  " Some psql features might not work.\n"),
3809  pset.progname,
3810  formatPGVersionNumber(client_ver, false,
3811  cverbuf, sizeof(cverbuf)),
3813  sverbuf, sizeof(sverbuf)));
3814 
3815 #ifdef WIN32
3816  if (in_startup)
3817  checkWin32Codepage();
3818 #endif
3819  printSSLInfo();
3820  printGSSInfo();
3821  }
3822 }
static void printSSLInfo(void)
Definition: command.c:3831
static void printGSSInfo(void)
Definition: command.c:3856
#define _(x)
Definition: elog.c:91
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:7213
static int server_version
Definition: pg_dumpall.c:112
#define printf(...)
Definition: port.h:244
PsqlSettings pset
Definition: startup.c:32
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:177
PGconn * db
Definition: settings.h:82
const char * progname
Definition: settings.h:113

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 4388 of file command.c.

4389 {
4390  size_t vallen = 0;
4391 
4392  Assert(param != NULL);
4393 
4394  if (value)
4395  vallen = strlen(value);
4396 
4397  /* set format */
4398  if (strcmp(param, "format") == 0)
4399  {
4400  static const struct fmt
4401  {
4402  const char *name;
4403  enum printFormat number;
4404  } formats[] =
4405  {
4406  /* remember to update error message below when adding more */
4407  {"aligned", PRINT_ALIGNED},
4408  {"asciidoc", PRINT_ASCIIDOC},
4409  {"csv", PRINT_CSV},
4410  {"html", PRINT_HTML},
4411  {"latex", PRINT_LATEX},
4412  {"troff-ms", PRINT_TROFF_MS},
4413  {"unaligned", PRINT_UNALIGNED},
4414  {"wrapped", PRINT_WRAPPED}
4415  };
4416 
4417  if (!value)
4418  ;
4419  else
4420  {
4421  int match_pos = -1;
4422 
4423  for (int i = 0; i < lengthof(formats); i++)
4424  {
4425  if (pg_strncasecmp(formats[i].name, value, vallen) == 0)
4426  {
4427  if (match_pos < 0)
4428  match_pos = i;
4429  else
4430  {
4431  pg_log_error("\\pset: ambiguous abbreviation \"%s\" matches both \"%s\" and \"%s\"",
4432  value,
4433  formats[match_pos].name, formats[i].name);
4434  return false;
4435  }
4436  }
4437  }
4438  if (match_pos >= 0)
4439  popt->topt.format = formats[match_pos].number;
4440  else if (pg_strncasecmp("latex-longtable", value, vallen) == 0)
4441  {
4442  /*
4443  * We must treat latex-longtable specially because latex is a
4444  * prefix of it; if both were in the table above, we'd think
4445  * "latex" is ambiguous.
4446  */
4448  }
4449  else
4450  {
4451  pg_log_error("\\pset: allowed formats are aligned, asciidoc, csv, html, latex, latex-longtable, troff-ms, unaligned, wrapped");
4452  return false;
4453  }
4454  }
4455  }
4456 
4457  /* set table line style */
4458  else if (strcmp(param, "linestyle") == 0)
4459  {
4460  if (!value)
4461  ;
4462  else if (pg_strncasecmp("ascii", value, vallen) == 0)
4463  popt->topt.line_style = &pg_asciiformat;
4464  else if (pg_strncasecmp("old-ascii", value, vallen) == 0)
4466  else if (pg_strncasecmp("unicode", value, vallen) == 0)
4467  popt->topt.line_style = &pg_utf8format;
4468  else
4469  {
4470  pg_log_error("\\pset: allowed line styles are ascii, old-ascii, unicode");
4471  return false;
4472  }
4473  }
4474 
4475  /* set unicode border line style */
4476  else if (strcmp(param, "unicode_border_linestyle") == 0)
4477  {
4478  if (!value)
4479  ;
4480  else if (set_unicode_line_style(value, vallen,
4482  refresh_utf8format(&(popt->topt));
4483  else
4484  {
4485  pg_log_error("\\pset: allowed Unicode border line styles are single, double");
4486  return false;
4487  }
4488  }
4489 
4490  /* set unicode column line style */
4491  else if (strcmp(param, "unicode_column_linestyle") == 0)
4492  {
4493  if (!value)
4494  ;
4495  else if (set_unicode_line_style(value, vallen,
4497  refresh_utf8format(&(popt->topt));
4498  else
4499  {
4500  pg_log_error("\\pset: allowed Unicode column line styles are single, double");
4501  return false;
4502  }
4503  }
4504 
4505  /* set unicode header line style */
4506  else if (strcmp(param, "unicode_header_linestyle") == 0)
4507  {
4508  if (!value)
4509  ;
4510  else if (set_unicode_line_style(value, vallen,
4512  refresh_utf8format(&(popt->topt));
4513  else
4514  {
4515  pg_log_error("\\pset: allowed Unicode header line styles are single, double");
4516  return false;
4517  }
4518  }
4519 
4520  /* set border style/width */
4521  else if (strcmp(param, "border") == 0)
4522  {
4523  if (value)
4524  popt->topt.border = atoi(value);
4525  }
4526 
4527  /* set expanded/vertical mode */
4528  else if (strcmp(param, "x") == 0 ||
4529  strcmp(param, "expanded") == 0 ||
4530  strcmp(param, "vertical") == 0)
4531  {
4532  if (value && pg_strcasecmp(value, "auto") == 0)
4533  popt->topt.expanded = 2;
4534  else if (value)
4535  {
4536  bool on_off;
4537 
4538  if (ParseVariableBool(value, NULL, &on_off))
4539  popt->topt.expanded = on_off ? 1 : 0;
4540  else
4541  {
4542  PsqlVarEnumError(param, value, "on, off, auto");
4543  return false;
4544  }
4545  }
4546  else
4547  popt->topt.expanded = !popt->topt.expanded;
4548  }
4549 
4550  /* header line width in expanded mode */
4551  else if (strcmp(param, "xheader_width") == 0)
4552  {
4553  if (!value)
4554  ;
4555  else if (pg_strcasecmp(value, "full") == 0)
4557  else if (pg_strcasecmp(value, "column") == 0)
4559  else if (pg_strcasecmp(value, "page") == 0)
4561  else
4562  {
4563  int intval = atoi(value);
4564 
4565  if (intval == 0)
4566  {
4567  pg_log_error("\\pset: allowed xheader_width values are \"%s\" (default), \"%s\", \"%s\", or a number specifying the exact width", "full", "column", "page");
4568  return false;
4569  }
4570 
4572  popt->topt.expanded_header_exact_width = intval;
4573  }
4574  }
4575 
4576  /* field separator for CSV format */
4577  else if (strcmp(param, "csv_fieldsep") == 0)
4578  {
4579  if (value)
4580  {
4581  /* CSV separator has to be a one-byte character */
4582  if (strlen(value) != 1)
4583  {
4584  pg_log_error("\\pset: csv_fieldsep must be a single one-byte character");
4585  return false;
4586  }
4587  if (value[0] == '"' || value[0] == '\n' || value[0] == '\r')
4588  {
4589  pg_log_error("\\pset: csv_fieldsep cannot be a double quote, a newline, or a carriage return");
4590  return false;
4591  }
4592  popt->topt.csvFieldSep[0] = value[0];
4593  }
4594  }
4595 
4596  /* locale-aware numeric output */
4597  else if (strcmp(param, "numericlocale") == 0)
4598  {
4599  if (value)
4600  return ParseVariableBool(value, param, &popt->topt.numericLocale);
4601  else
4602  popt->topt.numericLocale = !popt->topt.numericLocale;
4603  }
4604 
4605  /* null display */
4606  else if (strcmp(param, "null") == 0)
4607  {
4608  if (value)
4609  {
4610  free(popt->nullPrint);
4611  popt->nullPrint = pg_strdup(value);
4612  }
4613  }
4614 
4615  /* field separator for unaligned text */
4616  else if (strcmp(param, "fieldsep") == 0)
4617  {
4618  if (value)
4619  {
4620  free(popt->topt.fieldSep.separator);
4622  popt->topt.fieldSep.separator_zero = false;
4623  }
4624  }
4625 
4626  else if (strcmp(param, "fieldsep_zero") == 0)
4627  {
4628  free(popt->topt.fieldSep.separator);
4629  popt->topt.fieldSep.separator = NULL;
4630  popt->topt.fieldSep.separator_zero = true;
4631  }
4632 
4633  /* record separator for unaligned text */
4634  else if (strcmp(param, "recordsep") == 0)
4635  {
4636  if (value)
4637  {
4638  free(popt->topt.recordSep.separator);
4640  popt->topt.recordSep.separator_zero = false;
4641  }
4642  }
4643 
4644  else if (strcmp(param, "recordsep_zero") == 0)
4645  {
4646  free(popt->topt.recordSep.separator);
4647  popt->topt.recordSep.separator = NULL;
4648  popt->topt.recordSep.separator_zero = true;
4649  }
4650 
4651  /* toggle between full and tuples-only format */
4652  else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0)
4653  {
4654  if (value)
4655  return ParseVariableBool(value, param, &popt->topt.tuples_only);
4656  else
4657  popt->topt.tuples_only = !popt->topt.tuples_only;
4658  }
4659 
4660  /* set title override */
4661  else if (strcmp(param, "C") == 0 || strcmp(param, "title") == 0)
4662  {
4663  free(popt->title);
4664  if (!value)
4665  popt->title = NULL;
4666  else
4667  popt->title = pg_strdup(value);
4668  }
4669 
4670  /* set HTML table tag options */
4671  else if (strcmp(param, "T") == 0 || strcmp(param, "tableattr") == 0)
4672  {
4673  free(popt->topt.tableAttr);
4674  if (!value)
4675  popt->topt.tableAttr = NULL;
4676  else
4677  popt->topt.tableAttr = pg_strdup(value);
4678  }
4679 
4680  /* toggle use of pager */
4681  else if (strcmp(param, "pager") == 0)
4682  {
4683  if (value && pg_strcasecmp(value, "always") == 0)
4684  popt->topt.pager = 2;
4685  else if (value)
4686  {
4687  bool on_off;
4688 
4689  if (!ParseVariableBool(value, NULL, &on_off))
4690  {
4691  PsqlVarEnumError(param, value, "on, off, always");
4692  return false;
4693  }
4694  popt->topt.pager = on_off ? 1 : 0;
4695  }
4696  else if (popt->topt.pager == 1)
4697  popt->topt.pager = 0;
4698  else
4699  popt->topt.pager = 1;
4700  }
4701 
4702  /* set minimum lines for pager use */
4703  else if (strcmp(param, "pager_min_lines") == 0)
4704  {
4705  if (value &&
4706  !ParseVariableNum(value, "pager_min_lines", &popt->topt.pager_min_lines))
4707  return false;
4708  }
4709 
4710  /* disable "(x rows)" footer */
4711  else if (strcmp(param, "footer") == 0)
4712  {
4713  if (value)
4714  return ParseVariableBool(value, param, &popt->topt.default_footer);
4715  else
4716  popt->topt.default_footer = !popt->topt.default_footer;
4717  }
4718 
4719  /* set border style/width */
4720  else if (strcmp(param, "columns") == 0)
4721  {
4722  if (value)
4723  popt->topt.columns = atoi(value);
4724  }
4725  else
4726  {
4727  pg_log_error("\\pset: unknown option: %s", param);
4728  return false;
4729  }
4730 
4731  if (!quiet)
4732  printPsetInfo(param, &pset.popt);
4733 
4734  return true;
4735 }
#define lengthof(array)
Definition: c.h:777
static bool set_unicode_line_style(const char *value, size_t vallen, unicode_linestyle *linestyle)
Definition: command.c:4345
static bool printPsetInfo(const char *param, printQueryOpt *popt)
Definition: command.c:4741
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void refresh_utf8format(const printTableOpt *opt)
Definition: print.c:3691
const printTextFormat pg_asciiformat
Definition: print.c:56
const printTextFormat pg_asciiformat_old
Definition: print.c:77
printTextFormat pg_utf8format
Definition: print.c:99
@ 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
#define free(a)
Definition: header.h:65
static struct @148 value
int i
Definition: isn.c:73
static void const char * fmt
Assert(fmt[strlen(fmt) - 1] !='\n')
#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:91
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:416
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:107
bool ParseVariableNum(const char *value, const char *name, int *result)
Definition: variables.c:156
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, fmt, 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 214 of file command.c.

218 {
219  backslashResult status;
220  char *cmd;
221  char *arg;
222 
223  Assert(scan_state != NULL);
224  Assert(cstack != NULL);
225 
226  /* Parse off the command name */
227  cmd = psql_scan_slash_command(scan_state);
228 
229  /* And try to execute it */
230  status = exec_command(cmd, scan_state, cstack, query_buf, previous_buf);
231 
232  if (status == PSQL_CMD_UNKNOWN)
233  {
234  pg_log_error("invalid command \\%s", cmd);
236  pg_log_error_hint("Try \\? for help.");
237  status = PSQL_CMD_ERROR;
238  }
239 
240  if (status != PSQL_CMD_ERROR)
241  {
242  /*
243  * Eat any remaining arguments after a valid command. We want to
244  * suppress evaluation of backticks in this situation, so transiently
245  * push an inactive conditional-stack entry.
246  */
247  bool active_branch = conditional_active(cstack);
248 
250  while ((arg = psql_scan_slash_option(scan_state,
251  OT_NORMAL, NULL, false)))
252  {
253  if (active_branch)
254  pg_log_warning("\\%s: extra argument \"%s\" ignored", cmd, arg);
255  free(arg);
256  }
257  conditional_stack_pop(cstack);
258  }
259  else
260  {
261  /* silently throw away rest of line after an erroneous command */
262  while ((arg = psql_scan_slash_option(scan_state,
263  OT_WHOLE_LINE, NULL, false)))
264  free(arg);
265  }
266 
267  /* if there is a trailing \\, swallow it */
268  psql_scan_slash_command_end(scan_state);
269 
270  free(cmd);
271 
272  /* some commands write to queryFout, so make sure output is sent */
274 
275  return status;
276 }
static backslashResult exec_command(const char *cmd, PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:287
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
static void const char fflush(stdout)
#define pg_log_error_hint(...)
Definition: logging.h:112
void * arg
#define pg_log_warning(...)
Definition: pgfnames.c:24
char * psql_scan_slash_command(PsqlScanState state)
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)
FILE * queryFout
Definition: settings.h:84
bool cur_cmd_interactive
Definition: settings.h:111

References arg, Assert(), conditional_active(), conditional_stack_pop(), conditional_stack_push(), _psqlSettings::cur_cmd_interactive, exec_command(), fflush(), 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(), and _psqlSettings::queryFout.

Referenced by main(), and MainLoop().

◆ process_file()

int process_file ( char *  filename,
bool  use_relative_path 
)

Definition at line 4235 of file command.c.

4236 {
4237  FILE *fd;
4238  int result;
4239  char *oldfilename;
4240  char relpath[MAXPGPATH];
4241 
4242  if (!filename)
4243  {
4244  fd = stdin;
4245  filename = NULL;
4246  }
4247  else if (strcmp(filename, "-") != 0)
4248  {
4250 
4251  /*
4252  * If we were asked to resolve the pathname relative to the location
4253  * of the currently executing script, and there is one, and this is a
4254  * relative pathname, then prepend all but the last pathname component
4255  * of the current script to this pathname.
4256  */
4257  if (use_relative_path && pset.inputfile &&
4259  {
4260  strlcpy(relpath, pset.inputfile, sizeof(relpath));
4264 
4265  filename = relpath;
4266  }
4267 
4268  fd = fopen(filename, PG_BINARY_R);
4269 
4270  if (!fd)
4271  {
4272  pg_log_error("%s: %m", filename);
4273  return EXIT_FAILURE;
4274  }
4275  }
4276  else
4277  {
4278  fd = stdin;
4279  filename = "<stdin>"; /* for future error messages */
4280  }
4281 
4282  oldfilename = pset.inputfile;
4284 
4286 
4287  result = MainLoop(fd);
4288 
4289  if (fd != stdin)
4290  fclose(fd);
4291 
4292  pset.inputfile = oldfilename;
4293 
4295 
4296  return result;
4297 }
#define PG_BINARY_R
Definition: c.h:1285
void pg_logging_config(int new_flags)
Definition: logging.c:163
#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:121
void join_path_components(char *ret_path, const char *head, const char *tail)
Definition: path.c:219
#define is_absolute_path(filename)
Definition: port.h:103
void canonicalize_path(char *path)
Definition: path.c:264
void get_parent_directory(char *path)
Definition: path.c:977
bool has_drive_prefix(const char *path)
Definition: path.c:88
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:94
#define EXIT_FAILURE
Definition: settings.h:167
char * inputfile
Definition: settings.h:114

References canonicalize_path(), 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 4977 of file command.c.

4978 {
4979  /* Free all the old data we're about to overwrite the pointers to. */
4980 
4981  /* topt.line_style points to const data that need not be duplicated */
4982  free(popt->topt.fieldSep.separator);
4983  free(popt->topt.recordSep.separator);
4984  free(popt->topt.tableAttr);
4985  free(popt->nullPrint);
4986  free(popt->title);
4987 
4988  /*
4989  * footers and translate_columns are never set in psql's print settings,
4990  * so we needn't write code to duplicate them.
4991  */
4992  Assert(popt->footers == NULL);
4993  Assert(popt->translate_columns == NULL);
4994 
4995  /* Now we may flat-copy all the fields, including pointers. */
4996  memcpy(popt, save, sizeof(printQueryOpt));
4997 
4998  /* Lastly, free "save" ... but its sub-structures now belong to popt. */
4999  free(save);
5000 }
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 4941 of file command.c.

4942 {
4943  printQueryOpt *save;
4944 
4945  save = (printQueryOpt *) pg_malloc(sizeof(printQueryOpt));
4946 
4947  /* Flat-copy all the scalar fields, then duplicate sub-structures. */
4948  memcpy(save, popt, sizeof(printQueryOpt));
4949 
4950  /* topt.line_style points to const data that need not be duplicated */
4951  if (popt->topt.fieldSep.separator)
4953  if (popt->topt.recordSep.separator)
4955  if (popt->topt.tableAttr)
4956  save->topt.tableAttr = pg_strdup(popt->topt.tableAttr);
4957  if (popt->nullPrint)
4958  save->nullPrint = pg_strdup(popt->nullPrint);
4959  if (popt->title)
4960  save->title = pg_strdup(popt->title);
4961 
4962  /*
4963  * footers and translate_columns are never set in psql's print settings,
4964  * so we needn't write code to duplicate them.
4965  */
4966  Assert(popt->footers == NULL);
4967  Assert(popt->translate_columns == NULL);
4968 
4969  return save;
4970 }
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 3897 of file command.c.

3898 {
3899  char vbuf[32];
3900  const char *server_version;
3901 
3902  /* get stuff from connection */
3906 
3907  SetVariable(pset.vars, "DBNAME", PQdb(pset.db));
3908  SetVariable(pset.vars, "USER", PQuser(pset.db));
3909  SetVariable(pset.vars, "HOST", PQhost(pset.db));
3910  SetVariable(pset.vars, "PORT", PQport(pset.db));
3912 
3913  /* this bit should match connection_warnings(): */
3914  /* Try to get full text form of version, might include "devel" etc */
3915  server_version = PQparameterStatus(pset.db, "server_version");
3916  /* Otherwise fall back on pset.sversion */
3917  if (!server_version)
3918  {
3919  formatPGVersionNumber(pset.sversion, true, vbuf, sizeof(vbuf));
3920  server_version = vbuf;
3921  }
3922  SetVariable(pset.vars, "SERVER_VERSION_NAME", server_version);
3923 
3924  snprintf(vbuf, sizeof(vbuf), "%d", pset.sversion);
3925  SetVariable(pset.vars, "SERVER_VERSION_NUM", vbuf);
3926 
3927  /* send stuff to it, too */
3930 }
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:7238
char * PQhost(const PGconn *conn)
Definition: fe-connect.c:7127
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:7094
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:7336
PGContextVisibility PQsetErrorContextVisibility(PGconn *conn, PGContextVisibility show_context)
Definition: fe-connect.c:7398
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:7102
char * PQport(const PGconn *conn)
Definition: fe-connect.c:7163
PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity)
Definition: fe-connect.c:7386
#define pg_encoding_to_char
Definition: pg_wchar.h:563
#define snprintf
Definition: port.h:238
VariableSpace vars
Definition: settings.h:122
int encoding
Definition: settings.h:83
PGVerbosity verbosity
Definition: settings.h:154
PGContextVisibility show_context
Definition: settings.h:156
int encoding
Definition: print.h:138
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211

References _psqlSettings::db, _psqlSettings::encoding, printTableOpt::encoding, formatPGVersionNumber(), pg_encoding_to_char, _psqlSettings::popt, PQclientEncoding(), PQdb(), PQhost(), PQparameterStatus(), PQport(), PQserverVersion(), PQsetErrorContextVisibility(), PQsetErrorVerbosity(), PQuser(), pset, server_version, 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 3938 of file command.c.

3939 {
3940  SetVariable(pset.vars, "DBNAME", NULL);
3941  SetVariable(pset.vars, "USER", NULL);
3942  SetVariable(pset.vars, "HOST", NULL);
3943  SetVariable(pset.vars, "PORT", NULL);
3944  SetVariable(pset.vars, "ENCODING", NULL);
3945  SetVariable(pset.vars, "SERVER_VERSION_NAME", NULL);
3946  SetVariable(pset.vars, "SERVER_VERSION_NUM", NULL);
3947 }

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

Referenced by CheckConnection(), and do_connect().