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 */
enum _backslashResult backslashResult

Function Documentation

◆ connection_warnings()

void connection_warnings ( bool  in_startup)

Definition at line 3538 of file command.c.

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().

3539 {
3540  if (!pset.quiet && !pset.notty)
3541  {
3542  int client_ver = PG_VERSION_NUM;
3543  char cverbuf[32];
3544  char sverbuf[32];
3545 
3546  if (pset.sversion != client_ver)
3547  {
3548  const char *server_version;
3549 
3550  /* Try to get full text form, might include "devel" etc */
3551  server_version = PQparameterStatus(pset.db, "server_version");
3552  /* Otherwise fall back on pset.sversion */
3553  if (!server_version)
3554  {
3556  sverbuf, sizeof(sverbuf));
3557  server_version = sverbuf;
3558  }
3559 
3560  printf(_("%s (%s, server %s)\n"),
3561  pset.progname, PG_VERSION, server_version);
3562  }
3563  /* For version match, only print psql banner on startup. */
3564  else if (in_startup)
3565  printf("%s (%s)\n", pset.progname, PG_VERSION);
3566 
3567  if (pset.sversion / 100 > client_ver / 100)
3568  printf(_("WARNING: %s major version %s, server major version %s.\n"
3569  " Some psql features might not work.\n"),
3570  pset.progname,
3571  formatPGVersionNumber(client_ver, false,
3572  cverbuf, sizeof(cverbuf)),
3574  sverbuf, sizeof(sverbuf)));
3575 
3576 #ifdef WIN32
3577  if (in_startup)
3578  checkWin32Codepage();
3579 #endif
3580  printSSLInfo();
3581  printGSSInfo();
3582  }
3583 }
PGconn * db
Definition: settings.h:82
PsqlSettings pset
Definition: startup.c:32
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6709
#define printf(...)
Definition: port.h:223
static void printGSSInfo(void)
Definition: command.c:3620
const char * progname
Definition: settings.h:109
static int server_version
Definition: pg_dumpall.c:83
static void printSSLInfo(void)
Definition: command.c:3592
#define _(x)
Definition: elog.c:89
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:177

◆ do_pset()

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

Definition at line 4151 of file command.c.

References Assert, printTableOpt::border, printTableOpt::columns, printTableOpt::csvFieldSep, printTableOpt::default_footer, printTableOpt::expanded, printTableOpt::fieldSep, printTableOpt::format, free, i, lengthof, printTableOpt::line_style, name, printQueryOpt::nullPrint, printTableOpt::numericLocale, printTableOpt::pager, printTableOpt::pager_min_lines, ParseVariableBool(), 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, 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, and printTableOpt::unicode_header_linestyle.

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().

4152 {
4153  size_t vallen = 0;
4154 
4155  Assert(param != NULL);
4156 
4157  if (value)
4158  vallen = strlen(value);
4159 
4160  /* set format */
4161  if (strcmp(param, "format") == 0)
4162  {
4163  static const struct fmt
4164  {
4165  const char *name;
4166  enum printFormat number;
4167  } formats[] =
4168  {
4169  /* remember to update error message below when adding more */
4170  {"aligned", PRINT_ALIGNED},
4171  {"asciidoc", PRINT_ASCIIDOC},
4172  {"csv", PRINT_CSV},
4173  {"html", PRINT_HTML},
4174  {"latex", PRINT_LATEX},
4175  {"troff-ms", PRINT_TROFF_MS},
4176  {"unaligned", PRINT_UNALIGNED},
4177  {"wrapped", PRINT_WRAPPED}
4178  };
4179 
4180  if (!value)
4181  ;
4182  else
4183  {
4184  int match_pos = -1;
4185 
4186  for (int i = 0; i < lengthof(formats); i++)
4187  {
4188  if (pg_strncasecmp(formats[i].name, value, vallen) == 0)
4189  {
4190  if (match_pos < 0)
4191  match_pos = i;
4192  else
4193  {
4194  pg_log_error("\\pset: ambiguous abbreviation \"%s\" matches both \"%s\" and \"%s\"",
4195  value,
4196  formats[match_pos].name, formats[i].name);
4197  return false;
4198  }
4199  }
4200  }
4201  if (match_pos >= 0)
4202  popt->topt.format = formats[match_pos].number;
4203  else if (pg_strncasecmp("latex-longtable", value, vallen) == 0)
4204  {
4205  /*
4206  * We must treat latex-longtable specially because latex is a
4207  * prefix of it; if both were in the table above, we'd think
4208  * "latex" is ambiguous.
4209  */
4211  }
4212  else
4213  {
4214  pg_log_error("\\pset: allowed formats are aligned, asciidoc, csv, html, latex, latex-longtable, troff-ms, unaligned, wrapped");
4215  return false;
4216  }
4217  }
4218  }
4219 
4220  /* set table line style */
4221  else if (strcmp(param, "linestyle") == 0)
4222  {
4223  if (!value)
4224  ;
4225  else if (pg_strncasecmp("ascii", value, vallen) == 0)
4226  popt->topt.line_style = &pg_asciiformat;
4227  else if (pg_strncasecmp("old-ascii", value, vallen) == 0)
4229  else if (pg_strncasecmp("unicode", value, vallen) == 0)
4230  popt->topt.line_style = &pg_utf8format;
4231  else
4232  {
4233  pg_log_error("\\pset: allowed line styles are ascii, old-ascii, unicode");
4234  return false;
4235  }
4236  }
4237 
4238  /* set unicode border line style */
4239  else if (strcmp(param, "unicode_border_linestyle") == 0)
4240  {
4241  if (!value)
4242  ;
4243  else if (set_unicode_line_style(value, vallen,
4245  refresh_utf8format(&(popt->topt));
4246  else
4247  {
4248  pg_log_error("\\pset: allowed Unicode border line styles are single, double");
4249  return false;
4250  }
4251  }
4252 
4253  /* set unicode column line style */
4254  else if (strcmp(param, "unicode_column_linestyle") == 0)
4255  {
4256  if (!value)
4257  ;
4258  else if (set_unicode_line_style(value, vallen,
4260  refresh_utf8format(&(popt->topt));
4261  else
4262  {
4263  pg_log_error("\\pset: allowed Unicode column line styles are single, double");
4264  return false;
4265  }
4266  }
4267 
4268  /* set unicode header line style */
4269  else if (strcmp(param, "unicode_header_linestyle") == 0)
4270  {
4271  if (!value)
4272  ;
4273  else if (set_unicode_line_style(value, vallen,
4275  refresh_utf8format(&(popt->topt));
4276  else
4277  {
4278  pg_log_error("\\pset: allowed Unicode header line styles are single, double");
4279  return false;
4280  }
4281  }
4282 
4283  /* set border style/width */
4284  else if (strcmp(param, "border") == 0)
4285  {
4286  if (value)
4287  popt->topt.border = atoi(value);
4288  }
4289 
4290  /* set expanded/vertical mode */
4291  else if (strcmp(param, "x") == 0 ||
4292  strcmp(param, "expanded") == 0 ||
4293  strcmp(param, "vertical") == 0)
4294  {
4295  if (value && pg_strcasecmp(value, "auto") == 0)
4296  popt->topt.expanded = 2;
4297  else if (value)
4298  {
4299  bool on_off;
4300 
4301  if (ParseVariableBool(value, NULL, &on_off))
4302  popt->topt.expanded = on_off ? 1 : 0;
4303  else
4304  {
4305  PsqlVarEnumError(param, value, "on, off, auto");
4306  return false;
4307  }
4308  }
4309  else
4310  popt->topt.expanded = !popt->topt.expanded;
4311  }
4312 
4313  /* field separator for CSV format */
4314  else if (strcmp(param, "csv_fieldsep") == 0)
4315  {
4316  if (value)
4317  {
4318  /* CSV separator has to be a one-byte character */
4319  if (strlen(value) != 1)
4320  {
4321  pg_log_error("\\pset: csv_fieldsep must be a single one-byte character");
4322  return false;
4323  }
4324  if (value[0] == '"' || value[0] == '\n' || value[0] == '\r')
4325  {
4326  pg_log_error("\\pset: csv_fieldsep cannot be a double quote, a newline, or a carriage return");
4327  return false;
4328  }
4329  popt->topt.csvFieldSep[0] = value[0];
4330  }
4331  }
4332 
4333  /* locale-aware numeric output */
4334  else if (strcmp(param, "numericlocale") == 0)
4335  {
4336  if (value)
4337  return ParseVariableBool(value, param, &popt->topt.numericLocale);
4338  else
4339  popt->topt.numericLocale = !popt->topt.numericLocale;
4340  }
4341 
4342  /* null display */
4343  else if (strcmp(param, "null") == 0)
4344  {
4345  if (value)
4346  {
4347  free(popt->nullPrint);
4348  popt->nullPrint = pg_strdup(value);
4349  }
4350  }
4351 
4352  /* field separator for unaligned text */
4353  else if (strcmp(param, "fieldsep") == 0)
4354  {
4355  if (value)
4356  {
4357  free(popt->topt.fieldSep.separator);
4359  popt->topt.fieldSep.separator_zero = false;
4360  }
4361  }
4362 
4363  else if (strcmp(param, "fieldsep_zero") == 0)
4364  {
4365  free(popt->topt.fieldSep.separator);
4366  popt->topt.fieldSep.separator = NULL;
4367  popt->topt.fieldSep.separator_zero = true;
4368  }
4369 
4370  /* record separator for unaligned text */
4371  else if (strcmp(param, "recordsep") == 0)
4372  {
4373  if (value)
4374  {
4375  free(popt->topt.recordSep.separator);
4377  popt->topt.recordSep.separator_zero = false;
4378  }
4379  }
4380 
4381  else if (strcmp(param, "recordsep_zero") == 0)
4382  {
4383  free(popt->topt.recordSep.separator);
4384  popt->topt.recordSep.separator = NULL;
4385  popt->topt.recordSep.separator_zero = true;
4386  }
4387 
4388  /* toggle between full and tuples-only format */
4389  else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0)
4390  {
4391  if (value)
4392  return ParseVariableBool(value, param, &popt->topt.tuples_only);
4393  else
4394  popt->topt.tuples_only = !popt->topt.tuples_only;
4395  }
4396 
4397  /* set title override */
4398  else if (strcmp(param, "C") == 0 || strcmp(param, "title") == 0)
4399  {
4400  free(popt->title);
4401  if (!value)
4402  popt->title = NULL;
4403  else
4404  popt->title = pg_strdup(value);
4405  }
4406 
4407  /* set HTML table tag options */
4408  else if (strcmp(param, "T") == 0 || strcmp(param, "tableattr") == 0)
4409  {
4410  free(popt->topt.tableAttr);
4411  if (!value)
4412  popt->topt.tableAttr = NULL;
4413  else
4414  popt->topt.tableAttr = pg_strdup(value);
4415  }
4416 
4417  /* toggle use of pager */
4418  else if (strcmp(param, "pager") == 0)
4419  {
4420  if (value && pg_strcasecmp(value, "always") == 0)
4421  popt->topt.pager = 2;
4422  else if (value)
4423  {
4424  bool on_off;
4425 
4426  if (!ParseVariableBool(value, NULL, &on_off))
4427  {
4428  PsqlVarEnumError(param, value, "on, off, always");
4429  return false;
4430  }
4431  popt->topt.pager = on_off ? 1 : 0;
4432  }
4433  else if (popt->topt.pager == 1)
4434  popt->topt.pager = 0;
4435  else
4436  popt->topt.pager = 1;
4437  }
4438 
4439  /* set minimum lines for pager use */
4440  else if (strcmp(param, "pager_min_lines") == 0)
4441  {
4442  if (value)
4443  popt->topt.pager_min_lines = atoi(value);
4444  }
4445 
4446  /* disable "(x rows)" footer */
4447  else if (strcmp(param, "footer") == 0)
4448  {
4449  if (value)
4450  return ParseVariableBool(value, param, &popt->topt.default_footer);
4451  else
4452  popt->topt.default_footer = !popt->topt.default_footer;
4453  }
4454 
4455  /* set border style/width */
4456  else if (strcmp(param, "columns") == 0)
4457  {
4458  if (value)
4459  popt->topt.columns = atoi(value);
4460  }
4461  else
4462  {
4463  pg_log_error("\\pset: unknown option: %s", param);
4464  return false;
4465  }
4466 
4467  if (!quiet)
4468  printPsetInfo(param, &pset.popt);
4469 
4470  return true;
4471 }
char * nullPrint
Definition: print.h:170
PsqlSettings pset
Definition: startup.c:32
const printTextFormat * line_style
Definition: print.h:115
#define pg_log_error(...)
Definition: logging.h:80
printTextFormat pg_utf8format
Definition: print.c:99
const printTextFormat pg_asciiformat_old
Definition: print.c:77
enum printFormat format
Definition: print.h:101
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition: variables.c:417
printTableOpt topt
Definition: print.h:169
unicode_linestyle unicode_header_linestyle
Definition: print.h:127
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define lengthof(array)
Definition: c.h:734
bool separator_zero
Definition: print.h:96
unicode_linestyle unicode_border_linestyle
Definition: print.h:125
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69
unsigned short int border
Definition: print.h:104
unsigned short int expanded
Definition: print.h:102
unicode_linestyle unicode_column_linestyle
Definition: print.h:126
bool tuples_only
Definition: print.h:110
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
char * tableAttr
Definition: print.h:121
char csvFieldSep[2]
Definition: print.h:118
static bool printPsetInfo(const char *param, printQueryOpt *popt)
Definition: command.c:4477
unsigned short int pager
Definition: print.h:106
static struct @143 value
#define free(a)
Definition: header.h:65
struct separator fieldSep
Definition: print.h:116
char * title
Definition: print.h:171
int pager_min_lines
Definition: print.h:108
bool default_footer
Definition: print.h:113
#define Assert(condition)
Definition: c.h:804
printQueryOpt popt
Definition: settings.h:91
static bool set_unicode_line_style(const char *value, size_t vallen, unicode_linestyle *linestyle)
Definition: command.c:4108
const char * name
Definition: encode.c:561
bool numericLocale
Definition: print.h:119
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:107
int i
void refresh_utf8format(const printTableOpt *opt)
Definition: print.c:3562
const printTextFormat pg_asciiformat
Definition: print.c:56
char * separator
Definition: print.h:95
printFormat
Definition: print.h:28
struct separator recordSep
Definition: print.h:117
int columns
Definition: print.h:124

◆ HandleSlashCmds()

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

Definition at line 211 of file command.c.

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_info, 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 status().

Referenced by main(), and MainLoop().

215 {
217  char *cmd;
218  char *arg;
219 
220  Assert(scan_state != NULL);
221  Assert(cstack != NULL);
222 
223  /* Parse off the command name */
224  cmd = psql_scan_slash_command(scan_state);
225 
226  /* And try to execute it */
227  status = exec_command(cmd, scan_state, cstack, query_buf, previous_buf);
228 
229  if (status == PSQL_CMD_UNKNOWN)
230  {
231  pg_log_error("invalid command \\%s", cmd);
233  pg_log_info("Try \\? for help.");
234  status = PSQL_CMD_ERROR;
235  }
236 
237  if (status != PSQL_CMD_ERROR)
238  {
239  /*
240  * Eat any remaining arguments after a valid command. We want to
241  * suppress evaluation of backticks in this situation, so transiently
242  * push an inactive conditional-stack entry.
243  */
244  bool active_branch = conditional_active(cstack);
245 
247  while ((arg = psql_scan_slash_option(scan_state,
248  OT_NORMAL, NULL, false)))
249  {
250  if (active_branch)
251  pg_log_warning("\\%s: extra argument \"%s\" ignored", cmd, arg);
252  free(arg);
253  }
254  conditional_stack_pop(cstack);
255  }
256  else
257  {
258  /* silently throw away rest of line after an erroneous command */
259  while ((arg = psql_scan_slash_option(scan_state,
260  OT_WHOLE_LINE, NULL, false)))
261  free(arg);
262  }
263 
264  /* if there is a trailing \\, swallow it */
265  psql_scan_slash_command_end(scan_state);
266 
267  free(cmd);
268 
269  /* some commands write to queryFout, so make sure output is sent */
270  fflush(pset.queryFout);
271 
272  return status;
273 }
void psql_scan_slash_command_end(PsqlScanState state)
bool conditional_active(ConditionalStack cstack)
Definition: conditional.c:128
PsqlSettings pset
Definition: startup.c:32
bool conditional_stack_pop(ConditionalStack cstack)
Definition: conditional.c:57
#define pg_log_error(...)
Definition: logging.h:80
char * psql_scan_slash_command(PsqlScanState state)
void conditional_stack_push(ConditionalStack cstack, ifState new_state)
Definition: conditional.c:41
FILE * queryFout
Definition: settings.h:84
static backslashResult exec_command(const char *cmd, PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:284
bool cur_cmd_interactive
Definition: settings.h:107
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
#define free(a)
Definition: header.h:65
enum _backslashResult backslashResult
#define Assert(condition)
Definition: c.h:804
void * arg
#define pg_log_warning(...)
Definition: pgfnames.c:24
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:229
#define pg_log_info(...)
Definition: logging.h:88

◆ process_file()

int process_file ( char *  filename,
bool  use_relative_path 
)

Definition at line 3998 of file command.c.

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().

3999 {
4000  FILE *fd;
4001  int result;
4002  char *oldfilename;
4003  char relpath[MAXPGPATH];
4004 
4005  if (!filename)
4006  {
4007  fd = stdin;
4008  filename = NULL;
4009  }
4010  else if (strcmp(filename, "-") != 0)
4011  {
4013 
4014  /*
4015  * If we were asked to resolve the pathname relative to the location
4016  * of the currently executing script, and there is one, and this is a
4017  * relative pathname, then prepend all but the last pathname component
4018  * of the current script to this pathname.
4019  */
4020  if (use_relative_path && pset.inputfile &&
4022  {
4023  strlcpy(relpath, pset.inputfile, sizeof(relpath));
4024  get_parent_directory(relpath);
4025  join_path_components(relpath, relpath, filename);
4026  canonicalize_path(relpath);
4027 
4028  filename = relpath;
4029  }
4030 
4031  fd = fopen(filename, PG_BINARY_R);
4032 
4033  if (!fd)
4034  {
4035  pg_log_error("%s: %m", filename);
4036  return EXIT_FAILURE;
4037  }
4038  }
4039  else
4040  {
4041  fd = stdin;
4042  filename = "<stdin>"; /* for future error messages */
4043  }
4044 
4045  oldfilename = pset.inputfile;
4047 
4049 
4050  result = MainLoop(fd);
4051 
4052  if (fd != stdin)
4053  fclose(fd);
4054 
4055  pset.inputfile = oldfilename;
4056 
4058 
4059  return result;
4060 }
PsqlSettings pset
Definition: startup.c:32
#define pg_log_error(...)
Definition: logging.h:80
void canonicalize_path(char *path)
Definition: path.c:254
char * inputfile
Definition: settings.h:110
#define PG_BINARY_R
Definition: c.h:1273
static int fd(const char *x, int i)
Definition: preproc-init.c:105
bool has_drive_prefix(const char *filename)
Definition: path.c:87
#define MAXPGPATH
#define is_absolute_path(filename)
Definition: port.h:86
void get_parent_directory(char *path)
Definition: path.c:854
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
int MainLoop(FILE *source)
Definition: mainloop.c:33
void pg_logging_config(int new_flags)
Definition: logging.c:155
void join_path_components(char *ret_path, const char *head, const char *tail)
Definition: path.c:218
static char * filename
Definition: pg_dumpall.c:92
#define PG_LOG_FLAG_TERSE
Definition: logging.h:64
#define relpath(rnode, forknum)
Definition: relpath.h:87
#define EXIT_FAILURE
Definition: settings.h:162

◆ restorePsetInfo()

void restorePsetInfo ( printQueryOpt popt,
printQueryOpt save 
)

Definition at line 4700 of file command.c.

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().

4701 {
4702  /* Free all the old data we're about to overwrite the pointers to. */
4703 
4704  /* topt.line_style points to const data that need not be duplicated */
4705  if (popt->topt.fieldSep.separator)
4706  free(popt->topt.fieldSep.separator);
4707  if (popt->topt.recordSep.separator)
4708  free(popt->topt.recordSep.separator);
4709  if (popt->topt.tableAttr)
4710  free(popt->topt.tableAttr);
4711  if (popt->nullPrint)
4712  free(popt->nullPrint);
4713  if (popt->title)
4714  free(popt->title);
4715 
4716  /*
4717  * footers and translate_columns are never set in psql's print settings,
4718  * so we needn't write code to duplicate them.
4719  */
4720  Assert(popt->footers == NULL);
4721  Assert(popt->translate_columns == NULL);
4722 
4723  /* Now we may flat-copy all the fields, including pointers. */
4724  memcpy(popt, save, sizeof(printQueryOpt));
4725 
4726  /* Lastly, free "save" ... but its sub-structures now belong to popt. */
4727  free(save);
4728 }
char * nullPrint
Definition: print.h:170
printTableOpt topt
Definition: print.h:169
char * tableAttr
Definition: print.h:121
#define free(a)
Definition: header.h:65
struct separator fieldSep
Definition: print.h:116
char * title
Definition: print.h:171
char ** footers
Definition: print.h:172
#define Assert(condition)
Definition: c.h:804
char * separator
Definition: print.h:95
struct separator recordSep
Definition: print.h:117
const bool * translate_columns
Definition: print.h:174

◆ savePsetInfo()

printQueryOpt* savePsetInfo ( const printQueryOpt popt)

Definition at line 4664 of file command.c.

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().

4665 {
4666  printQueryOpt *save;
4667 
4668  save = (printQueryOpt *) pg_malloc(sizeof(printQueryOpt));
4669 
4670  /* Flat-copy all the scalar fields, then duplicate sub-structures. */
4671  memcpy(save, popt, sizeof(printQueryOpt));
4672 
4673  /* topt.line_style points to const data that need not be duplicated */
4674  if (popt->topt.fieldSep.separator)
4676  if (popt->topt.recordSep.separator)
4678  if (popt->topt.tableAttr)
4679  save->topt.tableAttr = pg_strdup(popt->topt.tableAttr);
4680  if (popt->nullPrint)
4681  save->nullPrint = pg_strdup(popt->nullPrint);
4682  if (popt->title)
4683  save->title = pg_strdup(popt->title);
4684 
4685  /*
4686  * footers and translate_columns are never set in psql's print settings,
4687  * so we needn't write code to duplicate them.
4688  */
4689  Assert(popt->footers == NULL);
4690  Assert(popt->translate_columns == NULL);
4691 
4692  return save;
4693 }
char * nullPrint
Definition: print.h:170
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
printTableOpt topt
Definition: print.h:169
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
char * tableAttr
Definition: print.h:121
struct separator fieldSep
Definition: print.h:116
char * title
Definition: print.h:171
char ** footers
Definition: print.h:172
#define Assert(condition)
Definition: c.h:804
char * separator
Definition: print.h:95
struct separator recordSep
Definition: print.h:117
const bool * translate_columns
Definition: print.h:174

◆ SyncVariables()

void SyncVariables ( void  )

Definition at line 3661 of file command.c.

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().

3662 {
3663  char vbuf[32];
3664  const char *server_version;
3665 
3666  /* get stuff from connection */
3670 
3671  SetVariable(pset.vars, "DBNAME", PQdb(pset.db));
3672  SetVariable(pset.vars, "USER", PQuser(pset.db));
3673  SetVariable(pset.vars, "HOST", PQhost(pset.db));
3674  SetVariable(pset.vars, "PORT", PQport(pset.db));
3676 
3677  /* this bit should match connection_warnings(): */
3678  /* Try to get full text form of version, might include "devel" etc */
3679  server_version = PQparameterStatus(pset.db, "server_version");
3680  /* Otherwise fall back on pset.sversion */
3681  if (!server_version)
3682  {
3683  formatPGVersionNumber(pset.sversion, true, vbuf, sizeof(vbuf));
3684  server_version = vbuf;
3685  }
3686  SetVariable(pset.vars, "SERVER_VERSION_NAME", server_version);
3687 
3688  snprintf(vbuf, sizeof(vbuf), "%d", pset.sversion);
3689  SetVariable(pset.vars, "SERVER_VERSION_NUM", vbuf);
3690 
3691  /* send stuff to it, too */
3694 }
PGconn * db
Definition: settings.h:82
int encoding
Definition: print.h:122
PsqlSettings pset
Definition: startup.c:32
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6709
printTableOpt topt
Definition: print.h:169
char * PQport(const PGconn *conn)
Definition: fe-connect.c:6659
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6734
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:6821
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:6598
PGContextVisibility show_context
Definition: settings.h:151
PGContextVisibility PQsetErrorContextVisibility(PGconn *conn, PGContextVisibility show_context)
Definition: fe-connect.c:6883
PGVerbosity verbosity
Definition: settings.h:150
char * PQhost(const PGconn *conn)
Definition: fe-connect.c:6623
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:588
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:6590
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211
printQueryOpt popt
Definition: settings.h:91
static int server_version
Definition: pg_dumpall.c:83
int encoding
Definition: settings.h:83
#define snprintf
Definition: port.h:217
PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity)
Definition: fe-connect.c:6871
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:177
VariableSpace vars
Definition: settings.h:118

◆ UnsyncVariables()

void UnsyncVariables ( void  )

Definition at line 3702 of file command.c.

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

Referenced by CheckConnection(), and do_connect().

3703 {
3704  SetVariable(pset.vars, "DBNAME", NULL);
3705  SetVariable(pset.vars, "USER", NULL);
3706  SetVariable(pset.vars, "HOST", NULL);
3707  SetVariable(pset.vars, "PORT", NULL);
3708  SetVariable(pset.vars, "ENCODING", NULL);
3709  SetVariable(pset.vars, "SERVER_VERSION_NAME", NULL);
3710  SetVariable(pset.vars, "SERVER_VERSION_NUM", NULL);
3711 }
PsqlSettings pset
Definition: startup.c:32
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211
VariableSpace vars
Definition: settings.h:118