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

3947 {
3948  if (!pset.quiet && !pset.notty)
3949  {
3950  int client_ver = PG_VERSION_NUM;
3951  char cverbuf[32];
3952  char sverbuf[32];
3953 
3954  if (pset.sversion != client_ver)
3955  {
3956  const char *server_version;
3957 
3958  /* Try to get full text form, might include "devel" etc */
3959  server_version = PQparameterStatus(pset.db, "server_version");
3960  /* Otherwise fall back on pset.sversion */
3961  if (!server_version)
3962  {
3964  sverbuf, sizeof(sverbuf));
3965  server_version = sverbuf;
3966  }
3967 
3968  printf(_("%s (%s, server %s)\n"),
3969  pset.progname, PG_VERSION, server_version);
3970  }
3971  /* For version match, only print psql banner on startup. */
3972  else if (in_startup)
3973  printf("%s (%s)\n", pset.progname, PG_VERSION);
3974 
3975  /*
3976  * Warn if server's major version is newer than ours, or if server
3977  * predates our support cutoff (currently 9.2).
3978  */
3979  if (pset.sversion / 100 > client_ver / 100 ||
3980  pset.sversion < 90200)
3981  printf(_("WARNING: %s major version %s, server major version %s.\n"
3982  " Some psql features might not work.\n"),
3983  pset.progname,
3984  formatPGVersionNumber(client_ver, false,
3985  cverbuf, sizeof(cverbuf)),
3987  sverbuf, sizeof(sverbuf)));
3988 
3989 #ifdef WIN32
3990  if (in_startup)
3991  checkWin32Codepage();
3992 #endif
3993  printSSLInfo();
3994  printGSSInfo();
3995  }
3996 }
static void printSSLInfo(void)
Definition: command.c:4005
static void printGSSInfo(void)
Definition: command.c:4033
#define _(x)
Definition: elog.c:90
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:7155
static int server_version
Definition: pg_dumpall.c:110
#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:91
const char * progname
Definition: settings.h:124

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

4566 {
4567  size_t vallen = 0;
4568 
4569  Assert(param != NULL);
4570 
4571  if (value)
4572  vallen = strlen(value);
4573 
4574  /* set format */
4575  if (strcmp(param, "format") == 0)
4576  {
4577  static const struct fmt
4578  {
4579  const char *name;
4580  enum printFormat number;
4581  } formats[] =
4582  {
4583  /* remember to update error message below when adding more */
4584  {"aligned", PRINT_ALIGNED},
4585  {"asciidoc", PRINT_ASCIIDOC},
4586  {"csv", PRINT_CSV},
4587  {"html", PRINT_HTML},
4588  {"latex", PRINT_LATEX},
4589  {"troff-ms", PRINT_TROFF_MS},
4590  {"unaligned", PRINT_UNALIGNED},
4591  {"wrapped", PRINT_WRAPPED}
4592  };
4593 
4594  if (!value)
4595  ;
4596  else
4597  {
4598  int match_pos = -1;
4599 
4600  for (int i = 0; i < lengthof(formats); i++)
4601  {
4602  if (pg_strncasecmp(formats[i].name, value, vallen) == 0)
4603  {
4604  if (match_pos < 0)
4605  match_pos = i;
4606  else
4607  {
4608  pg_log_error("\\pset: ambiguous abbreviation \"%s\" matches both \"%s\" and \"%s\"",
4609  value,
4610  formats[match_pos].name, formats[i].name);
4611  return false;
4612  }
4613  }
4614  }
4615  if (match_pos >= 0)
4616  popt->topt.format = formats[match_pos].number;
4617  else if (pg_strncasecmp("latex-longtable", value, vallen) == 0)
4618  {
4619  /*
4620  * We must treat latex-longtable specially because latex is a
4621  * prefix of it; if both were in the table above, we'd think
4622  * "latex" is ambiguous.
4623  */
4625  }
4626  else
4627  {
4628  pg_log_error("\\pset: allowed formats are aligned, asciidoc, csv, html, latex, latex-longtable, troff-ms, unaligned, wrapped");
4629  return false;
4630  }
4631  }
4632  }
4633 
4634  /* set table line style */
4635  else if (strcmp(param, "linestyle") == 0)
4636  {
4637  if (!value)
4638  ;
4639  else if (pg_strncasecmp("ascii", value, vallen) == 0)
4640  popt->topt.line_style = &pg_asciiformat;
4641  else if (pg_strncasecmp("old-ascii", value, vallen) == 0)
4643  else if (pg_strncasecmp("unicode", value, vallen) == 0)
4644  popt->topt.line_style = &pg_utf8format;
4645  else
4646  {
4647  pg_log_error("\\pset: allowed line styles are ascii, old-ascii, unicode");
4648  return false;
4649  }
4650  }
4651 
4652  /* set unicode border line style */
4653  else if (strcmp(param, "unicode_border_linestyle") == 0)
4654  {
4655  if (!value)
4656  ;
4657  else if (set_unicode_line_style(value, vallen,
4659  refresh_utf8format(&(popt->topt));
4660  else
4661  {
4662  pg_log_error("\\pset: allowed Unicode border line styles are single, double");
4663  return false;
4664  }
4665  }
4666 
4667  /* set unicode column line style */
4668  else if (strcmp(param, "unicode_column_linestyle") == 0)
4669  {
4670  if (!value)
4671  ;
4672  else if (set_unicode_line_style(value, vallen,
4674  refresh_utf8format(&(popt->topt));
4675  else
4676  {
4677  pg_log_error("\\pset: allowed Unicode column line styles are single, double");
4678  return false;
4679  }
4680  }
4681 
4682  /* set unicode header line style */
4683  else if (strcmp(param, "unicode_header_linestyle") == 0)
4684  {
4685  if (!value)
4686  ;
4687  else if (set_unicode_line_style(value, vallen,
4689  refresh_utf8format(&(popt->topt));
4690  else
4691  {
4692  pg_log_error("\\pset: allowed Unicode header line styles are single, double");
4693  return false;
4694  }
4695  }
4696 
4697  /* set border style/width */
4698  else if (strcmp(param, "border") == 0)
4699  {
4700  if (value)
4701  popt->topt.border = atoi(value);
4702  }
4703 
4704  /* set expanded/vertical mode */
4705  else if (strcmp(param, "x") == 0 ||
4706  strcmp(param, "expanded") == 0 ||
4707  strcmp(param, "vertical") == 0)
4708  {
4709  if (value && pg_strcasecmp(value, "auto") == 0)
4710  popt->topt.expanded = 2;
4711  else if (value)
4712  {
4713  bool on_off;
4714 
4715  if (ParseVariableBool(value, NULL, &on_off))
4716  popt->topt.expanded = on_off ? 1 : 0;
4717  else
4718  {
4719  PsqlVarEnumError(param, value, "on, off, auto");
4720  return false;
4721  }
4722  }
4723  else
4724  popt->topt.expanded = !popt->topt.expanded;
4725  }
4726 
4727  /* header line width in expanded mode */
4728  else if (strcmp(param, "xheader_width") == 0)
4729  {
4730  if (!value)
4731  ;
4732  else if (pg_strcasecmp(value, "full") == 0)
4734  else if (pg_strcasecmp(value, "column") == 0)
4736  else if (pg_strcasecmp(value, "page") == 0)
4738  else
4739  {
4740  int intval = atoi(value);
4741 
4742  if (intval == 0)
4743  {
4744  pg_log_error("\\pset: allowed xheader_width values are \"%s\" (default), \"%s\", \"%s\", or a number specifying the exact width", "full", "column", "page");
4745  return false;
4746  }
4747 
4749  popt->topt.expanded_header_exact_width = intval;
4750  }
4751  }
4752 
4753  /* field separator for CSV format */
4754  else if (strcmp(param, "csv_fieldsep") == 0)
4755  {
4756  if (value)
4757  {
4758  /* CSV separator has to be a one-byte character */
4759  if (strlen(value) != 1)
4760  {
4761  pg_log_error("\\pset: csv_fieldsep must be a single one-byte character");
4762  return false;
4763  }
4764  if (value[0] == '"' || value[0] == '\n' || value[0] == '\r')
4765  {
4766  pg_log_error("\\pset: csv_fieldsep cannot be a double quote, a newline, or a carriage return");
4767  return false;
4768  }
4769  popt->topt.csvFieldSep[0] = value[0];
4770  }
4771  }
4772 
4773  /* locale-aware numeric output */
4774  else if (strcmp(param, "numericlocale") == 0)
4775  {
4776  if (value)
4777  return ParseVariableBool(value, param, &popt->topt.numericLocale);
4778  else
4779  popt->topt.numericLocale = !popt->topt.numericLocale;
4780  }
4781 
4782  /* null display */
4783  else if (strcmp(param, "null") == 0)
4784  {
4785  if (value)
4786  {
4787  free(popt->nullPrint);
4788  popt->nullPrint = pg_strdup(value);
4789  }
4790  }
4791 
4792  /* field separator for unaligned text */
4793  else if (strcmp(param, "fieldsep") == 0)
4794  {
4795  if (value)
4796  {
4797  free(popt->topt.fieldSep.separator);
4799  popt->topt.fieldSep.separator_zero = false;
4800  }
4801  }
4802 
4803  else if (strcmp(param, "fieldsep_zero") == 0)
4804  {
4805  free(popt->topt.fieldSep.separator);
4806  popt->topt.fieldSep.separator = NULL;
4807  popt->topt.fieldSep.separator_zero = true;
4808  }
4809 
4810  /* record separator for unaligned text */
4811  else if (strcmp(param, "recordsep") == 0)
4812  {
4813  if (value)
4814  {
4815  free(popt->topt.recordSep.separator);
4817  popt->topt.recordSep.separator_zero = false;
4818  }
4819  }
4820 
4821  else if (strcmp(param, "recordsep_zero") == 0)
4822  {
4823  free(popt->topt.recordSep.separator);
4824  popt->topt.recordSep.separator = NULL;
4825  popt->topt.recordSep.separator_zero = true;
4826  }
4827 
4828  /* toggle between full and tuples-only format */
4829  else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0)
4830  {
4831  if (value)
4832  return ParseVariableBool(value, param, &popt->topt.tuples_only);
4833  else
4834  popt->topt.tuples_only = !popt->topt.tuples_only;
4835  }
4836 
4837  /* set title override */
4838  else if (strcmp(param, "C") == 0 || strcmp(param, "title") == 0)
4839  {
4840  free(popt->title);
4841  if (!value)
4842  popt->title = NULL;
4843  else
4844  popt->title = pg_strdup(value);
4845  }
4846 
4847  /* set HTML table tag options */
4848  else if (strcmp(param, "T") == 0 || strcmp(param, "tableattr") == 0)
4849  {
4850  free(popt->topt.tableAttr);
4851  if (!value)
4852  popt->topt.tableAttr = NULL;
4853  else
4854  popt->topt.tableAttr = pg_strdup(value);
4855  }
4856 
4857  /* toggle use of pager */
4858  else if (strcmp(param, "pager") == 0)
4859  {
4860  if (value && pg_strcasecmp(value, "always") == 0)
4861  popt->topt.pager = 2;
4862  else if (value)
4863  {
4864  bool on_off;
4865 
4866  if (!ParseVariableBool(value, NULL, &on_off))
4867  {
4868  PsqlVarEnumError(param, value, "on, off, always");
4869  return false;
4870  }
4871  popt->topt.pager = on_off ? 1 : 0;
4872  }
4873  else if (popt->topt.pager == 1)
4874  popt->topt.pager = 0;
4875  else
4876  popt->topt.pager = 1;
4877  }
4878 
4879  /* set minimum lines for pager use */
4880  else if (strcmp(param, "pager_min_lines") == 0)
4881  {
4882  if (value &&
4883  !ParseVariableNum(value, "pager_min_lines", &popt->topt.pager_min_lines))
4884  return false;
4885  }
4886 
4887  /* disable "(x rows)" footer */
4888  else if (strcmp(param, "footer") == 0)
4889  {
4890  if (value)
4891  return ParseVariableBool(value, param, &popt->topt.default_footer);
4892  else
4893  popt->topt.default_footer = !popt->topt.default_footer;
4894  }
4895 
4896  /* set border style/width */
4897  else if (strcmp(param, "columns") == 0)
4898  {
4899  if (value)
4900  popt->topt.columns = atoi(value);
4901  }
4902  else
4903  {
4904  pg_log_error("\\pset: unknown option: %s", param);
4905  return false;
4906  }
4907 
4908  if (!quiet)
4909  printPsetInfo(param, &pset.popt);
4910 
4911  return true;
4912 }
#define Assert(condition)
Definition: c.h:812
#define lengthof(array)
Definition: c.h:742
static bool set_unicode_line_style(const char *value, size_t vallen, unicode_linestyle *linestyle)
Definition: command.c:4522
static bool printPsetInfo(const char *param, printQueryOpt *popt)
Definition: command.c:4918
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 @160 value
int i
Definition: isn.c:72
static void const char * fmt
#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:100
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 218 of file command.c.

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

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

4413 {
4414  FILE *fd;
4415  int result;
4416  char *oldfilename;
4417  char relpath[MAXPGPATH];
4418 
4419  if (!filename)
4420  {
4421  fd = stdin;
4422  filename = NULL;
4423  }
4424  else if (strcmp(filename, "-") != 0)
4425  {
4427 
4428  /*
4429  * If we were asked to resolve the pathname relative to the location
4430  * of the currently executing script, and there is one, and this is a
4431  * relative pathname, then prepend all but the last pathname component
4432  * of the current script to this pathname.
4433  */
4434  if (use_relative_path && pset.inputfile &&
4436  {
4437  strlcpy(relpath, pset.inputfile, sizeof(relpath));
4441 
4442  filename = relpath;
4443  }
4444 
4445  fd = fopen(filename, PG_BINARY_R);
4446 
4447  if (!fd)
4448  {
4449  pg_log_error("%s: %m", filename);
4450  return EXIT_FAILURE;
4451  }
4452  }
4453  else
4454  {
4455  fd = stdin;
4456  filename = "<stdin>"; /* for future error messages */
4457  }
4458 
4459  oldfilename = pset.inputfile;
4461 
4463 
4464  result = MainLoop(fd);
4465 
4466  if (fd != stdin)
4467  fclose(fd);
4468 
4469  pset.inputfile = oldfilename;
4470 
4472 
4473  return result;
4474 }
#define PG_BINARY_R
Definition: c.h:1229
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:119
void join_path_components(char *ret_path, const char *head, const char *tail)
Definition: path.c:220
#define is_absolute_path(filename)
Definition: port.h:103
void canonicalize_path(char *path)
Definition: path.c:265
void get_parent_directory(char *path)
Definition: path.c:991
bool has_drive_prefix(const char *path)
Definition: path.c:89
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:102
#define EXIT_FAILURE
Definition: settings.h:178
char * inputfile
Definition: settings.h:125

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

5155 {
5156  /* Free all the old data we're about to overwrite the pointers to. */
5157 
5158  /* topt.line_style points to const data that need not be duplicated */
5159  free(popt->topt.fieldSep.separator);
5160  free(popt->topt.recordSep.separator);
5161  free(popt->topt.tableAttr);
5162  free(popt->nullPrint);
5163  free(popt->title);
5164 
5165  /*
5166  * footers and translate_columns are never set in psql's print settings,
5167  * so we needn't write code to duplicate them.
5168  */
5169  Assert(popt->footers == NULL);
5170  Assert(popt->translate_columns == NULL);
5171 
5172  /* Now we may flat-copy all the fields, including pointers. */
5173  memcpy(popt, save, sizeof(printQueryOpt));
5174 
5175  /* Lastly, free "save" ... but its sub-structures now belong to popt. */
5176  free(save);
5177 }
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 5118 of file command.c.

5119 {
5120  printQueryOpt *save;
5121 
5122  save = (printQueryOpt *) pg_malloc(sizeof(printQueryOpt));
5123 
5124  /* Flat-copy all the scalar fields, then duplicate sub-structures. */
5125  memcpy(save, popt, sizeof(printQueryOpt));
5126 
5127  /* topt.line_style points to const data that need not be duplicated */
5128  if (popt->topt.fieldSep.separator)
5130  if (popt->topt.recordSep.separator)
5132  if (popt->topt.tableAttr)
5133  save->topt.tableAttr = pg_strdup(popt->topt.tableAttr);
5134  if (popt->nullPrint)
5135  save->nullPrint = pg_strdup(popt->nullPrint);
5136  if (popt->title)
5137  save->title = pg_strdup(popt->title);
5138 
5139  /*
5140  * footers and translate_columns are never set in psql's print settings,
5141  * so we needn't write code to duplicate them.
5142  */
5143  Assert(popt->footers == NULL);
5144  Assert(popt->translate_columns == NULL);
5145 
5146  return save;
5147 }
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 4074 of file command.c.

4075 {
4076  char vbuf[32];
4077  const char *server_version;
4078 
4079  /* get stuff from connection */
4083 
4084  SetVariable(pset.vars, "DBNAME", PQdb(pset.db));
4085  SetVariable(pset.vars, "USER", PQuser(pset.db));
4086  SetVariable(pset.vars, "HOST", PQhost(pset.db));
4087  SetVariable(pset.vars, "PORT", PQport(pset.db));
4089 
4090  /* this bit should match connection_warnings(): */
4091  /* Try to get full text form of version, might include "devel" etc */
4092  server_version = PQparameterStatus(pset.db, "server_version");
4093  /* Otherwise fall back on pset.sversion */
4094  if (!server_version)
4095  {
4096  formatPGVersionNumber(pset.sversion, true, vbuf, sizeof(vbuf));
4097  server_version = vbuf;
4098  }
4099  SetVariable(pset.vars, "SERVER_VERSION_NAME", server_version);
4100 
4101  snprintf(vbuf, sizeof(vbuf), "%d", pset.sversion);
4102  SetVariable(pset.vars, "SERVER_VERSION_NUM", vbuf);
4103 
4104  /* send stuff to it, too */
4107 }
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:7190
char * PQhost(const PGconn *conn)
Definition: fe-connect.c:7069
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:7036
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:7288
PGContextVisibility PQsetErrorContextVisibility(PGconn *conn, PGContextVisibility show_context)
Definition: fe-connect.c:7350
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:7044
char * PQport(const PGconn *conn)
Definition: fe-connect.c:7105
PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity)
Definition: fe-connect.c:7338
#define pg_encoding_to_char
Definition: pg_wchar.h:630
#define snprintf
Definition: port.h:238
VariableSpace vars
Definition: settings.h:133
int encoding
Definition: settings.h:92
PGVerbosity verbosity
Definition: settings.h:165
PGContextVisibility show_context
Definition: settings.h:167
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 4115 of file command.c.

4116 {
4117  SetVariable(pset.vars, "DBNAME", NULL);
4118  SetVariable(pset.vars, "USER", NULL);
4119  SetVariable(pset.vars, "HOST", NULL);
4120  SetVariable(pset.vars, "PORT", NULL);
4121  SetVariable(pset.vars, "ENCODING", NULL);
4122  SetVariable(pset.vars, "SERVER_VERSION_NAME", NULL);
4123  SetVariable(pset.vars, "SERVER_VERSION_NUM", NULL);
4124 }

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

Referenced by CheckConnection(), and do_connect().