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

Go to the source code of this file.

Typedefs

typedef enum _backslashResult backslashResult
 

Enumerations

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

Functions

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

Typedef Documentation

◆ backslashResult

Enumeration Type Documentation

◆ _backslashResult

Enumerator
PSQL_CMD_UNKNOWN 
PSQL_CMD_SEND 
PSQL_CMD_SKIP_LINE 
PSQL_CMD_TERMINATE 
PSQL_CMD_NEWEDIT 
PSQL_CMD_ERROR 

Definition at line 15 of file command.h.

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

Function Documentation

◆ connection_warnings()

void connection_warnings ( bool  in_startup)

Definition at line 4348 of file command.c.

4349{
4350 if (!pset.quiet && !pset.notty)
4351 {
4352 int client_ver = PG_VERSION_NUM;
4353 char cverbuf[32];
4354 char sverbuf[32];
4355
4356 if (pset.sversion != client_ver)
4357 {
4358 const char *server_version;
4359
4360 /* Try to get full text form, might include "devel" etc */
4361 server_version = PQparameterStatus(pset.db, "server_version");
4362 /* Otherwise fall back on pset.sversion */
4363 if (!server_version)
4364 {
4366 sverbuf, sizeof(sverbuf));
4367 server_version = sverbuf;
4368 }
4369
4370 printf(_("%s (%s, server %s)\n"),
4371 pset.progname, PG_VERSION, server_version);
4372 }
4373 /* For version match, only print psql banner on startup. */
4374 else if (in_startup)
4375 printf("%s (%s)\n", pset.progname, PG_VERSION);
4376
4377 /*
4378 * Warn if server's major version is newer than ours, or if server
4379 * predates our support cutoff (currently 9.2).
4380 */
4381 if (pset.sversion / 100 > client_ver / 100 ||
4382 pset.sversion < 90200)
4383 printf(_("WARNING: %s major version %s, server major version %s.\n"
4384 " Some psql features might not work.\n"),
4385 pset.progname,
4386 formatPGVersionNumber(client_ver, false,
4387 cverbuf, sizeof(cverbuf)),
4389 sverbuf, sizeof(sverbuf)));
4390
4391#ifdef WIN32
4392 if (in_startup)
4393 checkWin32Codepage();
4394#endif
4395 printSSLInfo();
4396 printGSSInfo();
4397 }
4398}
static void printSSLInfo(void)
Definition: command.c:4407
static void printGSSInfo(void)
Definition: command.c:4435
#define _(x)
Definition: elog.c:91
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:7574
static int server_version
Definition: pg_dumpall.c:113
#define printf(...)
Definition: port.h:245
PsqlSettings pset
Definition: startup.c:32
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:313
PGconn * db
Definition: settings.h:103
const char * progname
Definition: settings.h:142

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

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

◆ do_pset()

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

Definition at line 4971 of file command.c.

4972{
4973 size_t vallen = 0;
4974
4975 Assert(param != NULL);
4976
4977 if (value)
4978 vallen = strlen(value);
4979
4980 /* set format */
4981 if (strcmp(param, "format") == 0)
4982 {
4983 static const struct fmt
4984 {
4985 const char *name;
4986 enum printFormat number;
4987 } formats[] =
4988 {
4989 /* remember to update error message below when adding more */
4990 {"aligned", PRINT_ALIGNED},
4991 {"asciidoc", PRINT_ASCIIDOC},
4992 {"csv", PRINT_CSV},
4993 {"html", PRINT_HTML},
4994 {"latex", PRINT_LATEX},
4995 {"troff-ms", PRINT_TROFF_MS},
4996 {"unaligned", PRINT_UNALIGNED},
4997 {"wrapped", PRINT_WRAPPED}
4998 };
4999
5000 if (!value)
5001 ;
5002 else
5003 {
5004 int match_pos = -1;
5005
5006 for (int i = 0; i < lengthof(formats); i++)
5007 {
5008 if (pg_strncasecmp(formats[i].name, value, vallen) == 0)
5009 {
5010 if (match_pos < 0)
5011 match_pos = i;
5012 else
5013 {
5014 pg_log_error("\\pset: ambiguous abbreviation \"%s\" matches both \"%s\" and \"%s\"",
5015 value,
5016 formats[match_pos].name, formats[i].name);
5017 return false;
5018 }
5019 }
5020 }
5021 if (match_pos >= 0)
5022 popt->topt.format = formats[match_pos].number;
5023 else if (pg_strncasecmp("latex-longtable", value, vallen) == 0)
5024 {
5025 /*
5026 * We must treat latex-longtable specially because latex is a
5027 * prefix of it; if both were in the table above, we'd think
5028 * "latex" is ambiguous.
5029 */
5031 }
5032 else
5033 {
5034 pg_log_error("\\pset: allowed formats are aligned, asciidoc, csv, html, latex, latex-longtable, troff-ms, unaligned, wrapped");
5035 return false;
5036 }
5037 }
5038 }
5039
5040 /* set table line style */
5041 else if (strcmp(param, "linestyle") == 0)
5042 {
5043 if (!value)
5044 ;
5045 else if (pg_strncasecmp("ascii", value, vallen) == 0)
5047 else if (pg_strncasecmp("old-ascii", value, vallen) == 0)
5049 else if (pg_strncasecmp("unicode", value, vallen) == 0)
5050 popt->topt.line_style = &pg_utf8format;
5051 else
5052 {
5053 pg_log_error("\\pset: allowed line styles are ascii, old-ascii, unicode");
5054 return false;
5055 }
5056 }
5057
5058 /* set unicode border line style */
5059 else if (strcmp(param, "unicode_border_linestyle") == 0)
5060 {
5061 if (!value)
5062 ;
5063 else if (set_unicode_line_style(value, vallen,
5065 refresh_utf8format(&(popt->topt));
5066 else
5067 {
5068 pg_log_error("\\pset: allowed Unicode border line styles are single, double");
5069 return false;
5070 }
5071 }
5072
5073 /* set unicode column line style */
5074 else if (strcmp(param, "unicode_column_linestyle") == 0)
5075 {
5076 if (!value)
5077 ;
5078 else if (set_unicode_line_style(value, vallen,
5080 refresh_utf8format(&(popt->topt));
5081 else
5082 {
5083 pg_log_error("\\pset: allowed Unicode column line styles are single, double");
5084 return false;
5085 }
5086 }
5087
5088 /* set unicode header line style */
5089 else if (strcmp(param, "unicode_header_linestyle") == 0)
5090 {
5091 if (!value)
5092 ;
5093 else if (set_unicode_line_style(value, vallen,
5095 refresh_utf8format(&(popt->topt));
5096 else
5097 {
5098 pg_log_error("\\pset: allowed Unicode header line styles are single, double");
5099 return false;
5100 }
5101 }
5102
5103 /* set border style/width */
5104 else if (strcmp(param, "border") == 0)
5105 {
5106 if (value)
5107 popt->topt.border = atoi(value);
5108 }
5109
5110 /* set expanded/vertical mode */
5111 else if (strcmp(param, "x") == 0 ||
5112 strcmp(param, "expanded") == 0 ||
5113 strcmp(param, "vertical") == 0)
5114 {
5115 if (value && pg_strcasecmp(value, "auto") == 0)
5116 popt->topt.expanded = 2;
5117 else if (value)
5118 {
5119 bool on_off;
5120
5121 if (ParseVariableBool(value, NULL, &on_off))
5122 popt->topt.expanded = on_off ? 1 : 0;
5123 else
5124 {
5125 PsqlVarEnumError(param, value, "on, off, auto");
5126 return false;
5127 }
5128 }
5129 else
5130 popt->topt.expanded = !popt->topt.expanded;
5131 }
5132
5133 /* header line width in expanded mode */
5134 else if (strcmp(param, "xheader_width") == 0)
5135 {
5136 if (!value)
5137 ;
5138 else if (pg_strcasecmp(value, "full") == 0)
5140 else if (pg_strcasecmp(value, "column") == 0)
5142 else if (pg_strcasecmp(value, "page") == 0)
5144 else
5145 {
5146 int intval = atoi(value);
5147
5148 if (intval == 0)
5149 {
5150 pg_log_error("\\pset: allowed xheader_width values are \"%s\" (default), \"%s\", \"%s\", or a number specifying the exact width", "full", "column", "page");
5151 return false;
5152 }
5153
5155 popt->topt.expanded_header_exact_width = intval;
5156 }
5157 }
5158
5159 /* field separator for CSV format */
5160 else if (strcmp(param, "csv_fieldsep") == 0)
5161 {
5162 if (value)
5163 {
5164 /* CSV separator has to be a one-byte character */
5165 if (strlen(value) != 1)
5166 {
5167 pg_log_error("\\pset: csv_fieldsep must be a single one-byte character");
5168 return false;
5169 }
5170 if (value[0] == '"' || value[0] == '\n' || value[0] == '\r')
5171 {
5172 pg_log_error("\\pset: csv_fieldsep cannot be a double quote, a newline, or a carriage return");
5173 return false;
5174 }
5175 popt->topt.csvFieldSep[0] = value[0];
5176 }
5177 }
5178
5179 /* locale-aware numeric output */
5180 else if (strcmp(param, "numericlocale") == 0)
5181 {
5182 if (value)
5183 return ParseVariableBool(value, param, &popt->topt.numericLocale);
5184 else
5185 popt->topt.numericLocale = !popt->topt.numericLocale;
5186 }
5187
5188 /* null display */
5189 else if (strcmp(param, "null") == 0)
5190 {
5191 if (value)
5192 {
5193 free(popt->nullPrint);
5194 popt->nullPrint = pg_strdup(value);
5195 }
5196 }
5197
5198 /* field separator for unaligned text */
5199 else if (strcmp(param, "fieldsep") == 0)
5200 {
5201 if (value)
5202 {
5203 free(popt->topt.fieldSep.separator);
5205 popt->topt.fieldSep.separator_zero = false;
5206 }
5207 }
5208
5209 else if (strcmp(param, "fieldsep_zero") == 0)
5210 {
5211 free(popt->topt.fieldSep.separator);
5212 popt->topt.fieldSep.separator = NULL;
5213 popt->topt.fieldSep.separator_zero = true;
5214 }
5215
5216 /* record separator for unaligned text */
5217 else if (strcmp(param, "recordsep") == 0)
5218 {
5219 if (value)
5220 {
5223 popt->topt.recordSep.separator_zero = false;
5224 }
5225 }
5226
5227 else if (strcmp(param, "recordsep_zero") == 0)
5228 {
5230 popt->topt.recordSep.separator = NULL;
5231 popt->topt.recordSep.separator_zero = true;
5232 }
5233
5234 /* toggle between full and tuples-only format */
5235 else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0)
5236 {
5237 if (value)
5238 return ParseVariableBool(value, param, &popt->topt.tuples_only);
5239 else
5240 popt->topt.tuples_only = !popt->topt.tuples_only;
5241 }
5242
5243 /* set title override */
5244 else if (strcmp(param, "C") == 0 || strcmp(param, "title") == 0)
5245 {
5246 free(popt->title);
5247 if (!value)
5248 popt->title = NULL;
5249 else
5250 popt->title = pg_strdup(value);
5251 }
5252
5253 /* set HTML table tag options */
5254 else if (strcmp(param, "T") == 0 || strcmp(param, "tableattr") == 0)
5255 {
5256 free(popt->topt.tableAttr);
5257 if (!value)
5258 popt->topt.tableAttr = NULL;
5259 else
5260 popt->topt.tableAttr = pg_strdup(value);
5261 }
5262
5263 /* toggle use of pager */
5264 else if (strcmp(param, "pager") == 0)
5265 {
5266 if (value && pg_strcasecmp(value, "always") == 0)
5267 popt->topt.pager = 2;
5268 else if (value)
5269 {
5270 bool on_off;
5271
5272 if (!ParseVariableBool(value, NULL, &on_off))
5273 {
5274 PsqlVarEnumError(param, value, "on, off, always");
5275 return false;
5276 }
5277 popt->topt.pager = on_off ? 1 : 0;
5278 }
5279 else if (popt->topt.pager == 1)
5280 popt->topt.pager = 0;
5281 else
5282 popt->topt.pager = 1;
5283 }
5284
5285 /* set minimum lines for pager use */
5286 else if (strcmp(param, "pager_min_lines") == 0)
5287 {
5288 if (value &&
5289 !ParseVariableNum(value, "pager_min_lines", &popt->topt.pager_min_lines))
5290 return false;
5291 }
5292
5293 /* disable "(x rows)" footer */
5294 else if (strcmp(param, "footer") == 0)
5295 {
5296 if (value)
5297 return ParseVariableBool(value, param, &popt->topt.default_footer);
5298 else
5299 popt->topt.default_footer = !popt->topt.default_footer;
5300 }
5301
5302 /* set border style/width */
5303 else if (strcmp(param, "columns") == 0)
5304 {
5305 if (value)
5306 popt->topt.columns = atoi(value);
5307 }
5308 else
5309 {
5310 pg_log_error("\\pset: unknown option: %s", param);
5311 return false;
5312 }
5313
5314 if (!quiet)
5315 printPsetInfo(param, &pset.popt);
5316
5317 return true;
5318}
#define lengthof(array)
Definition: c.h:759
static bool set_unicode_line_style(const char *value, size_t vallen, unicode_linestyle *linestyle)
Definition: command.c:4928
static bool printPsetInfo(const char *param, printQueryOpt *popt)
Definition: command.c:5324
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
Assert(PointerIsAligned(start, uint64))
#define free(a)
Definition: header.h:65
static struct @165 value
int i
Definition: isn.c:77
#define pg_log_error(...)
Definition: logging.h:106
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69
printQueryOpt popt
Definition: settings.h:112
printTableOpt topt
Definition: print.h:185
char * nullPrint
Definition: print.h:186
char * title
Definition: print.h:187
unsigned short int expanded
Definition: print.h:114
unicode_linestyle unicode_border_linestyle
Definition: print.h:141
bool tuples_only
Definition: print.h:126
int columns
Definition: print.h:140
enum printFormat format
Definition: print.h:113
struct separator fieldSep
Definition: print.h:132
int expanded_header_exact_width
Definition: print.h:118
struct separator recordSep
Definition: print.h:133
printXheaderWidthType expanded_header_width_type
Definition: print.h:116
char csvFieldSep[2]
Definition: print.h:134
const printTextFormat * line_style
Definition: print.h:131
bool default_footer
Definition: print.h:129
int pager_min_lines
Definition: print.h:124
unsigned short int pager
Definition: print.h:122
char * tableAttr
Definition: print.h:137
bool numericLocale
Definition: print.h:135
unsigned short int border
Definition: print.h:120
unicode_linestyle unicode_header_linestyle
Definition: print.h:143
unicode_linestyle unicode_column_linestyle
Definition: print.h:142
bool separator_zero
Definition: print.h:108
char * separator
Definition: print.h:107
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition: variables.c:486
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:109
bool ParseVariableNum(const char *value, const char *name, int *result)
Definition: variables.c:158
const char * name

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

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

◆ HandleSlashCmds()

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

Definition at line 225 of file command.c.

229{
230 backslashResult status;
231 char *cmd;
232 char *arg;
233
234 Assert(scan_state != NULL);
235 Assert(cstack != NULL);
236
237 /* Parse off the command name */
238 cmd = psql_scan_slash_command(scan_state);
239
240 /* And try to execute it */
241 status = exec_command(cmd, scan_state, cstack, query_buf, previous_buf);
242
243 if (status == PSQL_CMD_UNKNOWN)
244 {
245 pg_log_error("invalid command \\%s", cmd);
247 pg_log_error_hint("Try \\? for help.");
248 status = PSQL_CMD_ERROR;
249 }
250
251 if (status != PSQL_CMD_ERROR)
252 {
253 /*
254 * Eat any remaining arguments after a valid command. We want to
255 * suppress evaluation of backticks in this situation, so transiently
256 * push an inactive conditional-stack entry.
257 */
258 bool active_branch = conditional_active(cstack);
259
261 while ((arg = psql_scan_slash_option(scan_state,
262 OT_NORMAL, NULL, false)))
263 {
264 if (active_branch)
265 pg_log_warning("\\%s: extra argument \"%s\" ignored", cmd, arg);
266 free(arg);
267 }
268 conditional_stack_pop(cstack);
269 }
270 else
271 {
272 /* silently throw away rest of line after an erroneous command */
273 while ((arg = psql_scan_slash_option(scan_state,
274 OT_WHOLE_LINE, NULL, false)))
275 free(arg);
276 }
277
278 /* if there is a trailing \\, swallow it */
279 psql_scan_slash_command_end(scan_state);
280
281 free(cmd);
282
283 /* some commands write to queryFout, so make sure output is sent */
284 fflush(pset.queryFout);
285
286 return status;
287}
static backslashResult exec_command(const char *cmd, PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:298
void conditional_stack_push(ConditionalStack cstack, ifState new_state)
Definition: conditional.c:53
bool conditional_stack_pop(ConditionalStack cstack)
Definition: conditional.c:69
bool conditional_active(ConditionalStack cstack)
Definition: conditional.c:140
@ IFSTATE_IGNORED
Definition: conditional.h:37
#define pg_log_error_hint(...)
Definition: logging.h:112
void * arg
#define pg_log_warning(...)
Definition: pgfnames.c:24
void psql_scan_slash_command_end(PsqlScanState state)
@ OT_NORMAL
Definition: psqlscanslash.h:17
@ OT_WHOLE_LINE
Definition: psqlscanslash.h:21
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
char * psql_scan_slash_command(PsqlScanState state)
FILE * queryFout
Definition: settings.h:105
bool cur_cmd_interactive
Definition: settings.h:140

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

Referenced by main(), and MainLoop().

◆ process_file()

int process_file ( char *  filename,
bool  use_relative_path 
)

Definition at line 4818 of file command.c.

4819{
4820 FILE *fd;
4821 int result;
4822 char *oldfilename;
4823 char relpath[MAXPGPATH];
4824
4825 if (!filename)
4826 {
4827 fd = stdin;
4828 filename = NULL;
4829 }
4830 else if (strcmp(filename, "-") != 0)
4831 {
4833
4834 /*
4835 * If we were asked to resolve the pathname relative to the location
4836 * of the currently executing script, and there is one, and this is a
4837 * relative pathname, then prepend all but the last pathname component
4838 * of the current script to this pathname.
4839 */
4840 if (use_relative_path && pset.inputfile &&
4842 {
4847
4848 filename = relpath;
4849 }
4850
4851 fd = fopen(filename, PG_BINARY_R);
4852
4853 if (!fd)
4854 {
4855 pg_log_error("%s: %m", filename);
4856 return EXIT_FAILURE;
4857 }
4858 }
4859 else
4860 {
4861 fd = stdin;
4862 filename = "<stdin>"; /* for future error messages */
4863 }
4864
4865 oldfilename = pset.inputfile;
4867
4869
4870 result = MainLoop(fd);
4871
4872 if (fd != stdin)
4873 fclose(fd);
4874
4875 pset.inputfile = oldfilename;
4876
4878
4879 return result;
4880}
#define PG_BINARY_R
Definition: c.h:1246
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:123
void join_path_components(char *ret_path, const char *head, const char *tail)
Definition: path.c:286
#define is_absolute_path(filename)
Definition: port.h:104
void get_parent_directory(char *path)
Definition: path.c:1068
void canonicalize_path_enc(char *path, int encoding)
Definition: path.c:344
bool has_drive_prefix(const char *path)
Definition: path.c:94
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define relpath(rlocator, forknum)
Definition: relpath.h:150
#define EXIT_FAILURE
Definition: settings.h:197
char * inputfile
Definition: settings.h:143

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

Referenced by exec_command_include().

◆ restorePsetInfo()

void restorePsetInfo ( printQueryOpt popt,
printQueryOpt save 
)

Definition at line 5560 of file command.c.

5561{
5562 /* Free all the old data we're about to overwrite the pointers to. */
5563
5564 /* topt.line_style points to const data that need not be duplicated */
5565 free(popt->topt.fieldSep.separator);
5567 free(popt->topt.tableAttr);
5568 free(popt->nullPrint);
5569 free(popt->title);
5570
5571 /*
5572 * footers and translate_columns are never set in psql's print settings,
5573 * so we needn't write code to duplicate them.
5574 */
5575 Assert(popt->footers == NULL);
5576 Assert(popt->translate_columns == NULL);
5577
5578 /* Now we may flat-copy all the fields, including pointers. */
5579 memcpy(popt, save, sizeof(printQueryOpt));
5580
5581 /* Lastly, free "save" ... but its sub-structures now belong to popt. */
5582 free(save);
5583}
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 5524 of file command.c.

5525{
5526 printQueryOpt *save;
5527
5528 save = (printQueryOpt *) pg_malloc(sizeof(printQueryOpt));
5529
5530 /* Flat-copy all the scalar fields, then duplicate sub-structures. */
5531 memcpy(save, popt, sizeof(printQueryOpt));
5532
5533 /* topt.line_style points to const data that need not be duplicated */
5534 if (popt->topt.fieldSep.separator)
5536 if (popt->topt.recordSep.separator)
5538 if (popt->topt.tableAttr)
5539 save->topt.tableAttr = pg_strdup(popt->topt.tableAttr);
5540 if (popt->nullPrint)
5541 save->nullPrint = pg_strdup(popt->nullPrint);
5542 if (popt->title)
5543 save->title = pg_strdup(popt->title);
5544
5545 /*
5546 * footers and translate_columns are never set in psql's print settings,
5547 * so we needn't write code to duplicate them.
5548 */
5549 Assert(popt->footers == NULL);
5550 Assert(popt->translate_columns == NULL);
5551
5552 return save;
5553}
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 4476 of file command.c.

4477{
4478 char vbuf[32];
4479 const char *server_version;
4480
4481 /* get stuff from connection */
4485
4487
4488 SetVariable(pset.vars, "DBNAME", PQdb(pset.db));
4489 SetVariable(pset.vars, "SERVICE", PQservice(pset.db));
4490 SetVariable(pset.vars, "USER", PQuser(pset.db));
4491 SetVariable(pset.vars, "HOST", PQhost(pset.db));
4492 SetVariable(pset.vars, "PORT", PQport(pset.db));
4494
4495 /* this bit should match connection_warnings(): */
4496 /* Try to get full text form of version, might include "devel" etc */
4497 server_version = PQparameterStatus(pset.db, "server_version");
4498 /* Otherwise fall back on pset.sversion */
4499 if (!server_version)
4500 {
4501 formatPGVersionNumber(pset.sversion, true, vbuf, sizeof(vbuf));
4502 server_version = vbuf;
4503 }
4504 SetVariable(pset.vars, "SERVER_VERSION_NAME", server_version);
4505
4506 snprintf(vbuf, sizeof(vbuf), "%d", pset.sversion);
4507 SetVariable(pset.vars, "SERVER_VERSION_NUM", vbuf);
4508
4509 /* send stuff to it, too */
4512}
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:7609
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:7447
char * PQport(const PGconn *conn)
Definition: fe-connect.c:7524
char * PQhost(const PGconn *conn)
Definition: fe-connect.c:7488
char * PQservice(const PGconn *conn)
Definition: fe-connect.c:7455
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:7709
PGContextVisibility PQsetErrorContextVisibility(PGconn *conn, PGContextVisibility show_context)
Definition: fe-connect.c:7771
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:7463
PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity)
Definition: fe-connect.c:7759
#define pg_encoding_to_char
Definition: pg_wchar.h:630
#define snprintf
Definition: port.h:239
void setFmtEncoding(int encoding)
Definition: string_utils.c:69
VariableSpace vars
Definition: settings.h:151
PGVerbosity verbosity
Definition: settings.h:184
PGContextVisibility show_context
Definition: settings.h:186
int encoding
Definition: print.h:138
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:281

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

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

◆ UnsyncVariables()

void UnsyncVariables ( void  )

Definition at line 4520 of file command.c.

4521{
4522 SetVariable(pset.vars, "DBNAME", NULL);
4523 SetVariable(pset.vars, "SERVICE", NULL);
4524 SetVariable(pset.vars, "USER", NULL);
4525 SetVariable(pset.vars, "HOST", NULL);
4526 SetVariable(pset.vars, "PORT", NULL);
4527 SetVariable(pset.vars, "ENCODING", NULL);
4528 SetVariable(pset.vars, "SERVER_VERSION_NAME", NULL);
4529 SetVariable(pset.vars, "SERVER_VERSION_NUM", NULL);
4530}

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

Referenced by CheckConnection(), and do_connect().