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

4443{
4444 if (!pset.quiet && !pset.notty)
4445 {
4446 int client_ver = PG_VERSION_NUM;
4447 char cverbuf[32];
4448 char sverbuf[32];
4449
4450 if (pset.sversion != client_ver)
4451 {
4452 const char *server_version;
4453
4454 /* Try to get full text form, might include "devel" etc */
4455 server_version = PQparameterStatus(pset.db, "server_version");
4456 /* Otherwise fall back on pset.sversion */
4457 if (!server_version)
4458 {
4460 sverbuf, sizeof(sverbuf));
4461 server_version = sverbuf;
4462 }
4463
4464 printf(_("%s (%s, server %s)\n"),
4465 pset.progname, PG_VERSION, server_version);
4466 }
4467 /* For version match, only print psql banner on startup. */
4468 else if (in_startup)
4469 printf("%s (%s)\n", pset.progname, PG_VERSION);
4470
4471 /*
4472 * Warn if server's major version is newer than ours, or if server
4473 * predates our support cutoff (currently 9.2).
4474 */
4475 if (pset.sversion / 100 > client_ver / 100 ||
4476 pset.sversion < 90200)
4477 printf(_("WARNING: %s major version %s, server major version %s.\n"
4478 " Some psql features might not work.\n"),
4479 pset.progname,
4480 formatPGVersionNumber(client_ver, false,
4481 cverbuf, sizeof(cverbuf)),
4483 sverbuf, sizeof(sverbuf)));
4484
4485#ifdef WIN32
4486 if (in_startup)
4487 checkWin32Codepage();
4488#endif
4489 printSSLInfo();
4490 printGSSInfo();
4491 }
4492}
static void printSSLInfo(void)
Definition: command.c:4501
static void printGSSInfo(void)
Definition: command.c:4529
#define _(x)
Definition: elog.c:91
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:7659
static int server_version
Definition: pg_dumpall.c:109
#define printf(...)
Definition: port.h:266
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 5077 of file command.c.

5078{
5079 size_t vallen = 0;
5080
5081 Assert(param != NULL);
5082
5083 if (value)
5084 vallen = strlen(value);
5085
5086 /* set format */
5087 if (strcmp(param, "format") == 0)
5088 {
5089 static const struct fmt
5090 {
5091 const char *name;
5092 enum printFormat number;
5093 } formats[] =
5094 {
5095 /* remember to update error message below when adding more */
5096 {"aligned", PRINT_ALIGNED},
5097 {"asciidoc", PRINT_ASCIIDOC},
5098 {"csv", PRINT_CSV},
5099 {"html", PRINT_HTML},
5100 {"latex", PRINT_LATEX},
5101 {"troff-ms", PRINT_TROFF_MS},
5102 {"unaligned", PRINT_UNALIGNED},
5103 {"wrapped", PRINT_WRAPPED}
5104 };
5105
5106 if (!value)
5107 ;
5108 else
5109 {
5110 int match_pos = -1;
5111
5112 for (int i = 0; i < lengthof(formats); i++)
5113 {
5114 if (pg_strncasecmp(formats[i].name, value, vallen) == 0)
5115 {
5116 if (match_pos < 0)
5117 match_pos = i;
5118 else
5119 {
5120 pg_log_error("\\pset: ambiguous abbreviation \"%s\" matches both \"%s\" and \"%s\"",
5121 value,
5122 formats[match_pos].name, formats[i].name);
5123 return false;
5124 }
5125 }
5126 }
5127 if (match_pos >= 0)
5128 popt->topt.format = formats[match_pos].number;
5129 else if (pg_strncasecmp("latex-longtable", value, vallen) == 0)
5130 {
5131 /*
5132 * We must treat latex-longtable specially because latex is a
5133 * prefix of it; if both were in the table above, we'd think
5134 * "latex" is ambiguous.
5135 */
5137 }
5138 else
5139 {
5140 pg_log_error("\\pset: allowed formats are aligned, asciidoc, csv, html, latex, latex-longtable, troff-ms, unaligned, wrapped");
5141 return false;
5142 }
5143 }
5144 }
5145
5146 /* set table line style */
5147 else if (strcmp(param, "linestyle") == 0)
5148 {
5149 if (!value)
5150 ;
5151 else if (pg_strncasecmp("ascii", value, vallen) == 0)
5153 else if (pg_strncasecmp("old-ascii", value, vallen) == 0)
5155 else if (pg_strncasecmp("unicode", value, vallen) == 0)
5156 popt->topt.line_style = &pg_utf8format;
5157 else
5158 {
5159 pg_log_error("\\pset: allowed line styles are ascii, old-ascii, unicode");
5160 return false;
5161 }
5162 }
5163
5164 /* set unicode border line style */
5165 else if (strcmp(param, "unicode_border_linestyle") == 0)
5166 {
5167 if (!value)
5168 ;
5169 else if (set_unicode_line_style(value, vallen,
5171 refresh_utf8format(&(popt->topt));
5172 else
5173 {
5174 pg_log_error("\\pset: allowed Unicode border line styles are single, double");
5175 return false;
5176 }
5177 }
5178
5179 /* set unicode column line style */
5180 else if (strcmp(param, "unicode_column_linestyle") == 0)
5181 {
5182 if (!value)
5183 ;
5184 else if (set_unicode_line_style(value, vallen,
5186 refresh_utf8format(&(popt->topt));
5187 else
5188 {
5189 pg_log_error("\\pset: allowed Unicode column line styles are single, double");
5190 return false;
5191 }
5192 }
5193
5194 /* set unicode header line style */
5195 else if (strcmp(param, "unicode_header_linestyle") == 0)
5196 {
5197 if (!value)
5198 ;
5199 else if (set_unicode_line_style(value, vallen,
5201 refresh_utf8format(&(popt->topt));
5202 else
5203 {
5204 pg_log_error("\\pset: allowed Unicode header line styles are single, double");
5205 return false;
5206 }
5207 }
5208
5209 /* set border style/width */
5210 else if (strcmp(param, "border") == 0)
5211 {
5212 if (value)
5213 popt->topt.border = atoi(value);
5214 }
5215
5216 /* set expanded/vertical mode */
5217 else if (strcmp(param, "x") == 0 ||
5218 strcmp(param, "expanded") == 0 ||
5219 strcmp(param, "vertical") == 0)
5220 {
5221 if (value && pg_strcasecmp(value, "auto") == 0)
5222 popt->topt.expanded = 2;
5223 else if (value)
5224 {
5225 bool on_off;
5226
5227 if (ParseVariableBool(value, NULL, &on_off))
5228 popt->topt.expanded = on_off ? 1 : 0;
5229 else
5230 {
5231 PsqlVarEnumError(param, value, "on, off, auto");
5232 return false;
5233 }
5234 }
5235 else
5236 popt->topt.expanded = !popt->topt.expanded;
5237 }
5238
5239 /* header line width in expanded mode */
5240 else if (strcmp(param, "xheader_width") == 0)
5241 {
5242 if (!value)
5243 ;
5244 else if (pg_strcasecmp(value, "full") == 0)
5246 else if (pg_strcasecmp(value, "column") == 0)
5248 else if (pg_strcasecmp(value, "page") == 0)
5250 else
5251 {
5252 int intval = atoi(value);
5253
5254 if (intval == 0)
5255 {
5256 pg_log_error("\\pset: allowed xheader_width values are \"%s\" (default), \"%s\", \"%s\", or a number specifying the exact width", "full", "column", "page");
5257 return false;
5258 }
5259
5261 popt->topt.expanded_header_exact_width = intval;
5262 }
5263 }
5264
5265 /* field separator for CSV format */
5266 else if (strcmp(param, "csv_fieldsep") == 0)
5267 {
5268 if (value)
5269 {
5270 /* CSV separator has to be a one-byte character */
5271 if (strlen(value) != 1)
5272 {
5273 pg_log_error("\\pset: csv_fieldsep must be a single one-byte character");
5274 return false;
5275 }
5276 if (value[0] == '"' || value[0] == '\n' || value[0] == '\r')
5277 {
5278 pg_log_error("\\pset: csv_fieldsep cannot be a double quote, a newline, or a carriage return");
5279 return false;
5280 }
5281 popt->topt.csvFieldSep[0] = value[0];
5282 }
5283 }
5284
5285 /* locale-aware numeric output */
5286 else if (strcmp(param, "numericlocale") == 0)
5287 {
5288 if (value)
5289 return ParseVariableBool(value, param, &popt->topt.numericLocale);
5290 else
5291 popt->topt.numericLocale = !popt->topt.numericLocale;
5292 }
5293
5294 /* null display */
5295 else if (strcmp(param, "null") == 0)
5296 {
5297 if (value)
5298 {
5299 free(popt->nullPrint);
5300 popt->nullPrint = pg_strdup(value);
5301 }
5302 }
5303
5304 /* 'false' display */
5305 else if (strcmp(param, "display_false") == 0)
5306 {
5307 if (value)
5308 {
5309 free(popt->falsePrint);
5310 popt->falsePrint = pg_strdup(value);
5311 }
5312 }
5313
5314 /* 'true' display */
5315 else if (strcmp(param, "display_true") == 0)
5316 {
5317 if (value)
5318 {
5319 free(popt->truePrint);
5320 popt->truePrint = pg_strdup(value);
5321 }
5322 }
5323
5324 /* field separator for unaligned text */
5325 else if (strcmp(param, "fieldsep") == 0)
5326 {
5327 if (value)
5328 {
5329 free(popt->topt.fieldSep.separator);
5331 popt->topt.fieldSep.separator_zero = false;
5332 }
5333 }
5334
5335 else if (strcmp(param, "fieldsep_zero") == 0)
5336 {
5337 free(popt->topt.fieldSep.separator);
5338 popt->topt.fieldSep.separator = NULL;
5339 popt->topt.fieldSep.separator_zero = true;
5340 }
5341
5342 /* record separator for unaligned text */
5343 else if (strcmp(param, "recordsep") == 0)
5344 {
5345 if (value)
5346 {
5349 popt->topt.recordSep.separator_zero = false;
5350 }
5351 }
5352
5353 else if (strcmp(param, "recordsep_zero") == 0)
5354 {
5356 popt->topt.recordSep.separator = NULL;
5357 popt->topt.recordSep.separator_zero = true;
5358 }
5359
5360 /* toggle between full and tuples-only format */
5361 else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0)
5362 {
5363 if (value)
5364 return ParseVariableBool(value, param, &popt->topt.tuples_only);
5365 else
5366 popt->topt.tuples_only = !popt->topt.tuples_only;
5367 }
5368
5369 /* set title override */
5370 else if (strcmp(param, "C") == 0 || strcmp(param, "title") == 0)
5371 {
5372 free(popt->title);
5373 if (!value)
5374 popt->title = NULL;
5375 else
5376 popt->title = pg_strdup(value);
5377 }
5378
5379 /* set HTML table tag options */
5380 else if (strcmp(param, "T") == 0 || strcmp(param, "tableattr") == 0)
5381 {
5382 free(popt->topt.tableAttr);
5383 if (!value)
5384 popt->topt.tableAttr = NULL;
5385 else
5386 popt->topt.tableAttr = pg_strdup(value);
5387 }
5388
5389 /* toggle use of pager */
5390 else if (strcmp(param, "pager") == 0)
5391 {
5392 if (value && pg_strcasecmp(value, "always") == 0)
5393 popt->topt.pager = 2;
5394 else if (value)
5395 {
5396 bool on_off;
5397
5398 if (!ParseVariableBool(value, NULL, &on_off))
5399 {
5400 PsqlVarEnumError(param, value, "on, off, always");
5401 return false;
5402 }
5403 popt->topt.pager = on_off ? 1 : 0;
5404 }
5405 else if (popt->topt.pager == 1)
5406 popt->topt.pager = 0;
5407 else
5408 popt->topt.pager = 1;
5409 }
5410
5411 /* set minimum lines for pager use */
5412 else if (strcmp(param, "pager_min_lines") == 0)
5413 {
5414 if (value &&
5415 !ParseVariableNum(value, "pager_min_lines", &popt->topt.pager_min_lines))
5416 return false;
5417 }
5418
5419 /* disable "(x rows)" footer */
5420 else if (strcmp(param, "footer") == 0)
5421 {
5422 if (value)
5423 return ParseVariableBool(value, param, &popt->topt.default_footer);
5424 else
5425 popt->topt.default_footer = !popt->topt.default_footer;
5426 }
5427
5428 /* set border style/width */
5429 else if (strcmp(param, "columns") == 0)
5430 {
5431 if (value)
5432 popt->topt.columns = atoi(value);
5433 }
5434 else
5435 {
5436 pg_log_error("\\pset: unknown option: %s", param);
5437 return false;
5438 }
5439
5440 if (!quiet)
5441 printPsetInfo(param, &pset.popt);
5442
5443 return true;
5444}
#define lengthof(array)
Definition: c.h:792
static bool set_unicode_line_style(const char *value, size_t vallen, unicode_linestyle *linestyle)
Definition: command.c:5034
static bool printPsetInfo(const char *param, printQueryOpt *popt)
Definition: command.c:5450
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void refresh_utf8format(const printTableOpt *opt)
Definition: print.c:3888
const printTextFormat pg_asciiformat
Definition: print.c:61
const printTextFormat pg_asciiformat_old
Definition: print.c:82
printTextFormat pg_utf8format
Definition: print.c:104
@ 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 @171 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:32
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:65
printQueryOpt popt
Definition: settings.h:112
printTableOpt topt
Definition: print.h:185
char * nullPrint
Definition: print.h:186
char * falsePrint
Definition: print.h:188
char * title
Definition: print.h:189
char * truePrint
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, printQueryOpt::falsePrint, 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, printQueryOpt::truePrint, 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 231 of file command.c.

235{
236 backslashResult status;
237 char *cmd;
238 char *arg;
239
240 Assert(scan_state != NULL);
241 Assert(cstack != NULL);
242
243 /* Parse off the command name */
244 cmd = psql_scan_slash_command(scan_state);
245
246 /*
247 * And try to execute it.
248 *
249 * If we are in "restricted" mode, the only allowable backslash command is
250 * \unrestrict (to exit restricted mode).
251 */
252 if (restricted && strcmp(cmd, "unrestrict") != 0)
253 {
254 pg_log_error("backslash commands are restricted; only \\unrestrict is allowed");
255 status = PSQL_CMD_ERROR;
256 }
257 else
258 status = exec_command(cmd, scan_state, cstack, query_buf, previous_buf);
259
260 if (status == PSQL_CMD_UNKNOWN)
261 {
262 pg_log_error("invalid command \\%s", cmd);
264 pg_log_error_hint("Try \\? for help.");
265 status = PSQL_CMD_ERROR;
266 }
267
268 if (status != PSQL_CMD_ERROR)
269 {
270 /*
271 * Eat any remaining arguments after a valid command. We want to
272 * suppress evaluation of backticks in this situation, so transiently
273 * push an inactive conditional-stack entry.
274 */
275 bool active_branch = conditional_active(cstack);
276
278 while ((arg = psql_scan_slash_option(scan_state,
279 OT_NORMAL, NULL, false)))
280 {
281 if (active_branch)
282 pg_log_warning("\\%s: extra argument \"%s\" ignored", cmd, arg);
283 free(arg);
284 }
285 conditional_stack_pop(cstack);
286 }
287 else
288 {
289 /* silently throw away rest of line after an erroneous command */
290 while ((arg = psql_scan_slash_option(scan_state,
291 OT_WHOLE_LINE, NULL, false)))
292 free(arg);
293 }
294
295 /* if there is a trailing \\, swallow it */
296 psql_scan_slash_command_end(scan_state);
297
298 free(cmd);
299
300 /* some commands write to queryFout, so make sure output is sent */
301 fflush(pset.queryFout);
302
303 return status;
304}
static bool restricted
Definition: command.c:199
static backslashResult exec_command(const char *cmd, PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:315
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(), _psqlSettings::queryFout, and restricted.

Referenced by main(), and MainLoop().

◆ process_file()

int process_file ( char *  filename,
bool  use_relative_path 
)

Definition at line 4924 of file command.c.

4925{
4926 FILE *fd;
4927 int result;
4928 char *oldfilename;
4929 char relpath[MAXPGPATH];
4930
4931 if (!filename)
4932 {
4933 fd = stdin;
4934 filename = NULL;
4935 }
4936 else if (strcmp(filename, "-") != 0)
4937 {
4939
4940 /*
4941 * If we were asked to resolve the pathname relative to the location
4942 * of the currently executing script, and there is one, and this is a
4943 * relative pathname, then prepend all but the last pathname component
4944 * of the current script to this pathname.
4945 */
4946 if (use_relative_path && pset.inputfile &&
4948 {
4953
4954 filename = relpath;
4955 }
4956
4957 fd = fopen(filename, PG_BINARY_R);
4958
4959 if (!fd)
4960 {
4961 pg_log_error("%s: %m", filename);
4962 return EXIT_FAILURE;
4963 }
4964 }
4965 else
4966 {
4967 fd = stdin;
4968 filename = "<stdin>"; /* for future error messages */
4969 }
4970
4971 oldfilename = pset.inputfile;
4973
4975
4976 result = MainLoop(fd);
4977
4978 if (fd != stdin)
4979 fclose(fd);
4980
4981 pset.inputfile = oldfilename;
4982
4984
4985 return result;
4986}
#define PG_BINARY_R
Definition: c.h:1265
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:120
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 5700 of file command.c.

5701{
5702 /* Free all the old data we're about to overwrite the pointers to. */
5703
5704 /* topt.line_style points to const data that need not be duplicated */
5705 free(popt->topt.fieldSep.separator);
5707 free(popt->topt.tableAttr);
5708 free(popt->nullPrint);
5709 free(popt->title);
5710
5711 /*
5712 * footers and translate_columns are never set in psql's print settings,
5713 * so we needn't write code to duplicate them.
5714 */
5715 Assert(popt->footers == NULL);
5716 Assert(popt->translate_columns == NULL);
5717
5718 /* Now we may flat-copy all the fields, including pointers. */
5719 memcpy(popt, save, sizeof(printQueryOpt));
5720
5721 /* Lastly, free "save" ... but its sub-structures now belong to popt. */
5722 free(save);
5723}
const bool * translate_columns
Definition: print.h:192
char ** footers
Definition: print.h:190

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

5665{
5666 printQueryOpt *save;
5667
5668 save = (printQueryOpt *) pg_malloc(sizeof(printQueryOpt));
5669
5670 /* Flat-copy all the scalar fields, then duplicate sub-structures. */
5671 memcpy(save, popt, sizeof(printQueryOpt));
5672
5673 /* topt.line_style points to const data that need not be duplicated */
5674 if (popt->topt.fieldSep.separator)
5676 if (popt->topt.recordSep.separator)
5678 if (popt->topt.tableAttr)
5679 save->topt.tableAttr = pg_strdup(popt->topt.tableAttr);
5680 if (popt->nullPrint)
5681 save->nullPrint = pg_strdup(popt->nullPrint);
5682 if (popt->title)
5683 save->title = pg_strdup(popt->title);
5684
5685 /*
5686 * footers and translate_columns are never set in psql's print settings,
5687 * so we needn't write code to duplicate them.
5688 */
5689 Assert(popt->footers == NULL);
5690 Assert(popt->translate_columns == NULL);
5691
5692 return save;
5693}
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 4570 of file command.c.

4571{
4572 char vbuf[32];
4573 const char *server_version;
4574 char *service_name;
4575 char *service_file;
4576
4577 /* get stuff from connection */
4581
4583
4584 SetVariable(pset.vars, "DBNAME", PQdb(pset.db));
4585 SetVariable(pset.vars, "USER", PQuser(pset.db));
4586 SetVariable(pset.vars, "HOST", PQhost(pset.db));
4587 SetVariable(pset.vars, "PORT", PQport(pset.db));
4589
4590 service_name = get_conninfo_value("service");
4591 SetVariable(pset.vars, "SERVICE", service_name);
4592 if (service_name)
4593 pg_free(service_name);
4594
4595 service_file = get_conninfo_value("servicefile");
4596 SetVariable(pset.vars, "SERVICEFILE", service_file);
4597 if (service_file)
4598 pg_free(service_file);
4599
4600 /* this bit should match connection_warnings(): */
4601 /* Try to get full text form of version, might include "devel" etc */
4602 server_version = PQparameterStatus(pset.db, "server_version");
4603 /* Otherwise fall back on pset.sversion */
4604 if (!server_version)
4605 {
4606 formatPGVersionNumber(pset.sversion, true, vbuf, sizeof(vbuf));
4607 server_version = vbuf;
4608 }
4609 SetVariable(pset.vars, "SERVER_VERSION_NAME", server_version);
4610
4611 snprintf(vbuf, sizeof(vbuf), "%d", pset.sversion);
4612 SetVariable(pset.vars, "SERVER_VERSION_NUM", vbuf);
4613
4614 /* send stuff to it, too */
4617}
char * get_conninfo_value(const char *keyword)
Definition: common.c:2540
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:7694
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:7538
char * PQport(const PGconn *conn)
Definition: fe-connect.c:7607
char * PQhost(const PGconn *conn)
Definition: fe-connect.c:7571
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:7794
PGContextVisibility PQsetErrorContextVisibility(PGconn *conn, PGContextVisibility show_context)
Definition: fe-connect.c:7856
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:7546
PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity)
Definition: fe-connect.c:7844
void pg_free(void *ptr)
Definition: fe_memutils.c:105
#define pg_encoding_to_char
Definition: pg_wchar.h:630
#define snprintf
Definition: port.h:260
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(), get_conninfo_value(), pg_encoding_to_char, pg_free(), _psqlSettings::popt, PQclientEncoding(), PQdb(), PQhost(), PQparameterStatus(), PQport(), PQserverVersion(), 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 4625 of file command.c.

4626{
4627 SetVariable(pset.vars, "DBNAME", NULL);
4628 SetVariable(pset.vars, "SERVICE", NULL);
4629 SetVariable(pset.vars, "SERVICEFILE", NULL);
4630 SetVariable(pset.vars, "USER", NULL);
4631 SetVariable(pset.vars, "HOST", NULL);
4632 SetVariable(pset.vars, "PORT", NULL);
4633 SetVariable(pset.vars, "ENCODING", NULL);
4634 SetVariable(pset.vars, "SERVER_VERSION_NAME", NULL);
4635 SetVariable(pset.vars, "SERVER_VERSION_NUM", NULL);
4636}

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

Referenced by CheckConnection(), and do_connect().