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

3339 {
3340  if (!pset.quiet && !pset.notty)
3341  {
3342  int client_ver = PG_VERSION_NUM;
3343  char cverbuf[32];
3344  char sverbuf[32];
3345 
3346  if (pset.sversion != client_ver)
3347  {
3348  const char *server_version;
3349 
3350  /* Try to get full text form, might include "devel" etc */
3351  server_version = PQparameterStatus(pset.db, "server_version");
3352  /* Otherwise fall back on pset.sversion */
3353  if (!server_version)
3354  {
3356  sverbuf, sizeof(sverbuf));
3357  server_version = sverbuf;
3358  }
3359 
3360  printf(_("%s (%s, server %s)\n"),
3361  pset.progname, PG_VERSION, server_version);
3362  }
3363  /* For version match, only print psql banner on startup. */
3364  else if (in_startup)
3365  printf("%s (%s)\n", pset.progname, PG_VERSION);
3366 
3367  if (pset.sversion / 100 > client_ver / 100)
3368  printf(_("WARNING: %s major version %s, server major version %s.\n"
3369  " Some psql features might not work.\n"),
3370  pset.progname,
3371  formatPGVersionNumber(client_ver, false,
3372  cverbuf, sizeof(cverbuf)),
3374  sverbuf, sizeof(sverbuf)));
3375 
3376 #ifdef WIN32
3377  if (in_startup)
3378  checkWin32Codepage();
3379 #endif
3380  printSSLInfo();
3381  printGSSInfo();
3382  }
3383 }
PGconn * db
Definition: settings.h:82
PsqlSettings pset
Definition: startup.c:31
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6656
#define printf(...)
Definition: port.h:199
static void printGSSInfo(void)
Definition: command.c:3420
const char * progname
Definition: settings.h:109
static int server_version
Definition: pg_dumpall.c:81
static void printSSLInfo(void)
Definition: command.c:3392
#define _(x)
Definition: elog.c:88
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 3907 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().

3908 {
3909  size_t vallen = 0;
3910 
3911  Assert(param != NULL);
3912 
3913  if (value)
3914  vallen = strlen(value);
3915 
3916  /* set format */
3917  if (strcmp(param, "format") == 0)
3918  {
3919  static const struct fmt
3920  {
3921  const char *name;
3922  enum printFormat number;
3923  } formats[] =
3924  {
3925  /* remember to update error message below when adding more */
3926  {"aligned", PRINT_ALIGNED},
3927  {"asciidoc", PRINT_ASCIIDOC},
3928  {"csv", PRINT_CSV},
3929  {"html", PRINT_HTML},
3930  {"latex", PRINT_LATEX},
3931  {"troff-ms", PRINT_TROFF_MS},
3932  {"unaligned", PRINT_UNALIGNED},
3933  {"wrapped", PRINT_WRAPPED}
3934  };
3935 
3936  if (!value)
3937  ;
3938  else
3939  {
3940  int match_pos = -1;
3941 
3942  for (int i = 0; i < lengthof(formats); i++)
3943  {
3944  if (pg_strncasecmp(formats[i].name, value, vallen) == 0)
3945  {
3946  if (match_pos < 0)
3947  match_pos = i;
3948  else
3949  {
3950  pg_log_error("\\pset: ambiguous abbreviation \"%s\" matches both \"%s\" and \"%s\"",
3951  value,
3952  formats[match_pos].name, formats[i].name);
3953  return false;
3954  }
3955  }
3956  }
3957  if (match_pos >= 0)
3958  popt->topt.format = formats[match_pos].number;
3959  else if (pg_strncasecmp("latex-longtable", value, vallen) == 0)
3960  {
3961  /*
3962  * We must treat latex-longtable specially because latex is a
3963  * prefix of it; if both were in the table above, we'd think
3964  * "latex" is ambiguous.
3965  */
3967  }
3968  else
3969  {
3970  pg_log_error("\\pset: allowed formats are aligned, asciidoc, csv, html, latex, latex-longtable, troff-ms, unaligned, wrapped");
3971  return false;
3972  }
3973  }
3974  }
3975 
3976  /* set table line style */
3977  else if (strcmp(param, "linestyle") == 0)
3978  {
3979  if (!value)
3980  ;
3981  else if (pg_strncasecmp("ascii", value, vallen) == 0)
3982  popt->topt.line_style = &pg_asciiformat;
3983  else if (pg_strncasecmp("old-ascii", value, vallen) == 0)
3985  else if (pg_strncasecmp("unicode", value, vallen) == 0)
3986  popt->topt.line_style = &pg_utf8format;
3987  else
3988  {
3989  pg_log_error("\\pset: allowed line styles are ascii, old-ascii, unicode");
3990  return false;
3991  }
3992  }
3993 
3994  /* set unicode border line style */
3995  else if (strcmp(param, "unicode_border_linestyle") == 0)
3996  {
3997  if (!value)
3998  ;
3999  else if (set_unicode_line_style(value, vallen,
4001  refresh_utf8format(&(popt->topt));
4002  else
4003  {
4004  pg_log_error("\\pset: allowed Unicode border line styles are single, double");
4005  return false;
4006  }
4007  }
4008 
4009  /* set unicode column line style */
4010  else if (strcmp(param, "unicode_column_linestyle") == 0)
4011  {
4012  if (!value)
4013  ;
4014  else if (set_unicode_line_style(value, vallen,
4016  refresh_utf8format(&(popt->topt));
4017  else
4018  {
4019  pg_log_error("\\pset: allowed Unicode column line styles are single, double");
4020  return false;
4021  }
4022  }
4023 
4024  /* set unicode header line style */
4025  else if (strcmp(param, "unicode_header_linestyle") == 0)
4026  {
4027  if (!value)
4028  ;
4029  else if (set_unicode_line_style(value, vallen,
4031  refresh_utf8format(&(popt->topt));
4032  else
4033  {
4034  pg_log_error("\\pset: allowed Unicode header line styles are single, double");
4035  return false;
4036  }
4037  }
4038 
4039  /* set border style/width */
4040  else if (strcmp(param, "border") == 0)
4041  {
4042  if (value)
4043  popt->topt.border = atoi(value);
4044  }
4045 
4046  /* set expanded/vertical mode */
4047  else if (strcmp(param, "x") == 0 ||
4048  strcmp(param, "expanded") == 0 ||
4049  strcmp(param, "vertical") == 0)
4050  {
4051  if (value && pg_strcasecmp(value, "auto") == 0)
4052  popt->topt.expanded = 2;
4053  else if (value)
4054  {
4055  bool on_off;
4056 
4057  if (ParseVariableBool(value, NULL, &on_off))
4058  popt->topt.expanded = on_off ? 1 : 0;
4059  else
4060  {
4061  PsqlVarEnumError(param, value, "on, off, auto");
4062  return false;
4063  }
4064  }
4065  else
4066  popt->topt.expanded = !popt->topt.expanded;
4067  }
4068 
4069  /* field separator for CSV format */
4070  else if (strcmp(param, "csv_fieldsep") == 0)
4071  {
4072  if (value)
4073  {
4074  /* CSV separator has to be a one-byte character */
4075  if (strlen(value) != 1)
4076  {
4077  pg_log_error("\\pset: csv_fieldsep must be a single one-byte character");
4078  return false;
4079  }
4080  if (value[0] == '"' || value[0] == '\n' || value[0] == '\r')
4081  {
4082  pg_log_error("\\pset: csv_fieldsep cannot be a double quote, a newline, or a carriage return");
4083  return false;
4084  }
4085  popt->topt.csvFieldSep[0] = value[0];
4086  }
4087  }
4088 
4089  /* locale-aware numeric output */
4090  else if (strcmp(param, "numericlocale") == 0)
4091  {
4092  if (value)
4093  return ParseVariableBool(value, param, &popt->topt.numericLocale);
4094  else
4095  popt->topt.numericLocale = !popt->topt.numericLocale;
4096  }
4097 
4098  /* null display */
4099  else if (strcmp(param, "null") == 0)
4100  {
4101  if (value)
4102  {
4103  free(popt->nullPrint);
4104  popt->nullPrint = pg_strdup(value);
4105  }
4106  }
4107 
4108  /* field separator for unaligned text */
4109  else if (strcmp(param, "fieldsep") == 0)
4110  {
4111  if (value)
4112  {
4113  free(popt->topt.fieldSep.separator);
4115  popt->topt.fieldSep.separator_zero = false;
4116  }
4117  }
4118 
4119  else if (strcmp(param, "fieldsep_zero") == 0)
4120  {
4121  free(popt->topt.fieldSep.separator);
4122  popt->topt.fieldSep.separator = NULL;
4123  popt->topt.fieldSep.separator_zero = true;
4124  }
4125 
4126  /* record separator for unaligned text */
4127  else if (strcmp(param, "recordsep") == 0)
4128  {
4129  if (value)
4130  {
4131  free(popt->topt.recordSep.separator);
4133  popt->topt.recordSep.separator_zero = false;
4134  }
4135  }
4136 
4137  else if (strcmp(param, "recordsep_zero") == 0)
4138  {
4139  free(popt->topt.recordSep.separator);
4140  popt->topt.recordSep.separator = NULL;
4141  popt->topt.recordSep.separator_zero = true;
4142  }
4143 
4144  /* toggle between full and tuples-only format */
4145  else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0)
4146  {
4147  if (value)
4148  return ParseVariableBool(value, param, &popt->topt.tuples_only);
4149  else
4150  popt->topt.tuples_only = !popt->topt.tuples_only;
4151  }
4152 
4153  /* set title override */
4154  else if (strcmp(param, "C") == 0 || strcmp(param, "title") == 0)
4155  {
4156  free(popt->title);
4157  if (!value)
4158  popt->title = NULL;
4159  else
4160  popt->title = pg_strdup(value);
4161  }
4162 
4163  /* set HTML table tag options */
4164  else if (strcmp(param, "T") == 0 || strcmp(param, "tableattr") == 0)
4165  {
4166  free(popt->topt.tableAttr);
4167  if (!value)
4168  popt->topt.tableAttr = NULL;
4169  else
4170  popt->topt.tableAttr = pg_strdup(value);
4171  }
4172 
4173  /* toggle use of pager */
4174  else if (strcmp(param, "pager") == 0)
4175  {
4176  if (value && pg_strcasecmp(value, "always") == 0)
4177  popt->topt.pager = 2;
4178  else if (value)
4179  {
4180  bool on_off;
4181 
4182  if (!ParseVariableBool(value, NULL, &on_off))
4183  {
4184  PsqlVarEnumError(param, value, "on, off, always");
4185  return false;
4186  }
4187  popt->topt.pager = on_off ? 1 : 0;
4188  }
4189  else if (popt->topt.pager == 1)
4190  popt->topt.pager = 0;
4191  else
4192  popt->topt.pager = 1;
4193  }
4194 
4195  /* set minimum lines for pager use */
4196  else if (strcmp(param, "pager_min_lines") == 0)
4197  {
4198  if (value)
4199  popt->topt.pager_min_lines = atoi(value);
4200  }
4201 
4202  /* disable "(x rows)" footer */
4203  else if (strcmp(param, "footer") == 0)
4204  {
4205  if (value)
4206  return ParseVariableBool(value, param, &popt->topt.default_footer);
4207  else
4208  popt->topt.default_footer = !popt->topt.default_footer;
4209  }
4210 
4211  /* set border style/width */
4212  else if (strcmp(param, "columns") == 0)
4213  {
4214  if (value)
4215  popt->topt.columns = atoi(value);
4216  }
4217  else
4218  {
4219  pg_log_error("\\pset: unknown option: %s", param);
4220  return false;
4221  }
4222 
4223  if (!quiet)
4224  printPsetInfo(param, &pset.popt);
4225 
4226  return true;
4227 }
char * nullPrint
Definition: print.h:170
PsqlSettings pset
Definition: startup.c:31
const printTextFormat * line_style
Definition: print.h:115
#define pg_log_error(...)
Definition: logging.h:79
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:391
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:668
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:4233
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:738
printQueryOpt popt
Definition: settings.h:91
static bool set_unicode_line_style(const char *value, size_t vallen, unicode_linestyle *linestyle)
Definition: command.c:3864
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:3559
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 204 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().

208 {
210  char *cmd;
211  char *arg;
212 
213  Assert(scan_state != NULL);
214  Assert(cstack != NULL);
215 
216  /* Parse off the command name */
217  cmd = psql_scan_slash_command(scan_state);
218 
219  /* And try to execute it */
220  status = exec_command(cmd, scan_state, cstack, query_buf, previous_buf);
221 
222  if (status == PSQL_CMD_UNKNOWN)
223  {
224  pg_log_error("invalid command \\%s", cmd);
226  pg_log_info("Try \\? for help.");
227  status = PSQL_CMD_ERROR;
228  }
229 
230  if (status != PSQL_CMD_ERROR)
231  {
232  /*
233  * Eat any remaining arguments after a valid command. We want to
234  * suppress evaluation of backticks in this situation, so transiently
235  * push an inactive conditional-stack entry.
236  */
237  bool active_branch = conditional_active(cstack);
238 
240  while ((arg = psql_scan_slash_option(scan_state,
241  OT_NORMAL, NULL, false)))
242  {
243  if (active_branch)
244  pg_log_warning("\\%s: extra argument \"%s\" ignored", cmd, arg);
245  free(arg);
246  }
247  conditional_stack_pop(cstack);
248  }
249  else
250  {
251  /* silently throw away rest of line after an erroneous command */
252  while ((arg = psql_scan_slash_option(scan_state,
253  OT_WHOLE_LINE, NULL, false)))
254  free(arg);
255  }
256 
257  /* if there is a trailing \\, swallow it */
258  psql_scan_slash_command_end(scan_state);
259 
260  free(cmd);
261 
262  /* some commands write to queryFout, so make sure output is sent */
263  fflush(pset.queryFout);
264 
265  return status;
266 }
void psql_scan_slash_command_end(PsqlScanState state)
bool conditional_active(ConditionalStack cstack)
Definition: conditional.c:128
PsqlSettings pset
Definition: startup.c:31
bool conditional_stack_pop(ConditionalStack cstack)
Definition: conditional.c:57
#define pg_log_error(...)
Definition: logging.h:79
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:277
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:738
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:225
#define pg_log_info(...)
Definition: logging.h:87

◆ process_file()

int process_file ( char *  filename,
bool  use_relative_path 
)

Definition at line 3754 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().

3755 {
3756  FILE *fd;
3757  int result;
3758  char *oldfilename;
3759  char relpath[MAXPGPATH];
3760 
3761  if (!filename)
3762  {
3763  fd = stdin;
3764  filename = NULL;
3765  }
3766  else if (strcmp(filename, "-") != 0)
3767  {
3769 
3770  /*
3771  * If we were asked to resolve the pathname relative to the location
3772  * of the currently executing script, and there is one, and this is a
3773  * relative pathname, then prepend all but the last pathname component
3774  * of the current script to this pathname.
3775  */
3776  if (use_relative_path && pset.inputfile &&
3778  {
3779  strlcpy(relpath, pset.inputfile, sizeof(relpath));
3780  get_parent_directory(relpath);
3781  join_path_components(relpath, relpath, filename);
3782  canonicalize_path(relpath);
3783 
3784  filename = relpath;
3785  }
3786 
3787  fd = fopen(filename, PG_BINARY_R);
3788 
3789  if (!fd)
3790  {
3791  pg_log_error("%s: %m", filename);
3792  return EXIT_FAILURE;
3793  }
3794  }
3795  else
3796  {
3797  fd = stdin;
3798  filename = "<stdin>"; /* for future error messages */
3799  }
3800 
3801  oldfilename = pset.inputfile;
3803 
3805 
3806  result = MainLoop(fd);
3807 
3808  if (fd != stdin)
3809  fclose(fd);
3810 
3811  pset.inputfile = oldfilename;
3812 
3814 
3815  return result;
3816 }
PsqlSettings pset
Definition: startup.c:31
#define pg_log_error(...)
Definition: logging.h:79
void canonicalize_path(char *path)
Definition: path.c:254
char * inputfile
Definition: settings.h:110
#define PG_BINARY_R
Definition: c.h:1235
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:90
#define PG_LOG_FLAG_TERSE
Definition: logging.h:64
#define relpath(rnode, forknum)
Definition: relpath.h:87
#define EXIT_FAILURE
Definition: settings.h:154

◆ restorePsetInfo()

void restorePsetInfo ( printQueryOpt popt,
printQueryOpt save 
)

Definition at line 4456 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().

4457 {
4458  /* Free all the old data we're about to overwrite the pointers to. */
4459 
4460  /* topt.line_style points to const data that need not be duplicated */
4461  if (popt->topt.fieldSep.separator)
4462  free(popt->topt.fieldSep.separator);
4463  if (popt->topt.recordSep.separator)
4464  free(popt->topt.recordSep.separator);
4465  if (popt->topt.tableAttr)
4466  free(popt->topt.tableAttr);
4467  if (popt->nullPrint)
4468  free(popt->nullPrint);
4469  if (popt->title)
4470  free(popt->title);
4471 
4472  /*
4473  * footers and translate_columns are never set in psql's print settings,
4474  * so we needn't write code to duplicate them.
4475  */
4476  Assert(popt->footers == NULL);
4477  Assert(popt->translate_columns == NULL);
4478 
4479  /* Now we may flat-copy all the fields, including pointers. */
4480  memcpy(popt, save, sizeof(printQueryOpt));
4481 
4482  /* Lastly, free "save" ... but its sub-structures now belong to popt. */
4483  free(save);
4484 }
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:738
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 4420 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().

4421 {
4422  printQueryOpt *save;
4423 
4424  save = (printQueryOpt *) pg_malloc(sizeof(printQueryOpt));
4425 
4426  /* Flat-copy all the scalar fields, then duplicate sub-structures. */
4427  memcpy(save, popt, sizeof(printQueryOpt));
4428 
4429  /* topt.line_style points to const data that need not be duplicated */
4430  if (popt->topt.fieldSep.separator)
4432  if (popt->topt.recordSep.separator)
4434  if (popt->topt.tableAttr)
4435  save->topt.tableAttr = pg_strdup(popt->topt.tableAttr);
4436  if (popt->nullPrint)
4437  save->nullPrint = pg_strdup(popt->nullPrint);
4438  if (popt->title)
4439  save->title = pg_strdup(popt->title);
4440 
4441  /*
4442  * footers and translate_columns are never set in psql's print settings,
4443  * so we needn't write code to duplicate them.
4444  */
4445  Assert(popt->footers == NULL);
4446  Assert(popt->translate_columns == NULL);
4447 
4448  return save;
4449 }
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:738
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 3461 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().

3462 {
3463  char vbuf[32];
3464  const char *server_version;
3465 
3466  /* get stuff from connection */
3470 
3471  SetVariable(pset.vars, "DBNAME", PQdb(pset.db));
3472  SetVariable(pset.vars, "USER", PQuser(pset.db));
3473  SetVariable(pset.vars, "HOST", PQhost(pset.db));
3474  SetVariable(pset.vars, "PORT", PQport(pset.db));
3476 
3477  /* this bit should match connection_warnings(): */
3478  /* Try to get full text form of version, might include "devel" etc */
3479  server_version = PQparameterStatus(pset.db, "server_version");
3480  /* Otherwise fall back on pset.sversion */
3481  if (!server_version)
3482  {
3483  formatPGVersionNumber(pset.sversion, true, vbuf, sizeof(vbuf));
3484  server_version = vbuf;
3485  }
3486  SetVariable(pset.vars, "SERVER_VERSION_NAME", server_version);
3487 
3488  snprintf(vbuf, sizeof(vbuf), "%d", pset.sversion);
3489  SetVariable(pset.vars, "SERVER_VERSION_NUM", vbuf);
3490 
3491  /* send stuff to it, too */
3494 }
PGconn * db
Definition: settings.h:82
int encoding
Definition: print.h:122
PsqlSettings pset
Definition: startup.c:31
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6656
printTableOpt topt
Definition: print.h:169
char * PQport(const PGconn *conn)
Definition: fe-connect.c:6610
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6681
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:6751
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:6549
PGContextVisibility show_context
Definition: settings.h:143
PGContextVisibility PQsetErrorContextVisibility(PGconn *conn, PGContextVisibility show_context)
Definition: fe-connect.c:6817
PGVerbosity verbosity
Definition: settings.h:142
char * PQhost(const PGconn *conn)
Definition: fe-connect.c:6574
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:588
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:6541
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:81
int encoding
Definition: settings.h:83
#define snprintf
Definition: port.h:193
PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity)
Definition: fe-connect.c:6805
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 3502 of file command.c.

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

Referenced by CheckConnection(), and do_connect().

3503 {
3504  SetVariable(pset.vars, "DBNAME", NULL);
3505  SetVariable(pset.vars, "USER", NULL);
3506  SetVariable(pset.vars, "HOST", NULL);
3507  SetVariable(pset.vars, "PORT", NULL);
3508  SetVariable(pset.vars, "ENCODING", NULL);
3509  SetVariable(pset.vars, "SERVER_VERSION_NAME", NULL);
3510  SetVariable(pset.vars, "SERVER_VERSION_NUM", NULL);
3511 }
PsqlSettings pset
Definition: startup.c:31
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211
VariableSpace vars
Definition: settings.h:118