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

4301{
4302 if (!pset.quiet && !pset.notty)
4303 {
4304 int client_ver = PG_VERSION_NUM;
4305 char cverbuf[32];
4306 char sverbuf[32];
4307
4308 if (pset.sversion != client_ver)
4309 {
4310 const char *server_version;
4311
4312 /* Try to get full text form, might include "devel" etc */
4313 server_version = PQparameterStatus(pset.db, "server_version");
4314 /* Otherwise fall back on pset.sversion */
4315 if (!server_version)
4316 {
4318 sverbuf, sizeof(sverbuf));
4319 server_version = sverbuf;
4320 }
4321
4322 printf(_("%s (%s, server %s)\n"),
4323 pset.progname, PG_VERSION, server_version);
4324 }
4325 /* For version match, only print psql banner on startup. */
4326 else if (in_startup)
4327 printf("%s (%s)\n", pset.progname, PG_VERSION);
4328
4329 /*
4330 * Warn if server's major version is newer than ours, or if server
4331 * predates our support cutoff (currently 9.2).
4332 */
4333 if (pset.sversion / 100 > client_ver / 100 ||
4334 pset.sversion < 90200)
4335 printf(_("WARNING: %s major version %s, server major version %s.\n"
4336 " Some psql features might not work.\n"),
4337 pset.progname,
4338 formatPGVersionNumber(client_ver, false,
4339 cverbuf, sizeof(cverbuf)),
4341 sverbuf, sizeof(sverbuf)));
4342
4343#ifdef WIN32
4344 if (in_startup)
4345 checkWin32Codepage();
4346#endif
4347 printSSLInfo();
4348 printGSSInfo();
4349 }
4350}
static void printSSLInfo(void)
Definition: command.c:4359
static void printGSSInfo(void)
Definition: command.c:4387
#define _(x)
Definition: elog.c:90
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:7508
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:97
const char * progname
Definition: settings.h:136

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

4924{
4925 size_t vallen = 0;
4926
4927 Assert(param != NULL);
4928
4929 if (value)
4930 vallen = strlen(value);
4931
4932 /* set format */
4933 if (strcmp(param, "format") == 0)
4934 {
4935 static const struct fmt
4936 {
4937 const char *name;
4938 enum printFormat number;
4939 } formats[] =
4940 {
4941 /* remember to update error message below when adding more */
4942 {"aligned", PRINT_ALIGNED},
4943 {"asciidoc", PRINT_ASCIIDOC},
4944 {"csv", PRINT_CSV},
4945 {"html", PRINT_HTML},
4946 {"latex", PRINT_LATEX},
4947 {"troff-ms", PRINT_TROFF_MS},
4948 {"unaligned", PRINT_UNALIGNED},
4949 {"wrapped", PRINT_WRAPPED}
4950 };
4951
4952 if (!value)
4953 ;
4954 else
4955 {
4956 int match_pos = -1;
4957
4958 for (int i = 0; i < lengthof(formats); i++)
4959 {
4960 if (pg_strncasecmp(formats[i].name, value, vallen) == 0)
4961 {
4962 if (match_pos < 0)
4963 match_pos = i;
4964 else
4965 {
4966 pg_log_error("\\pset: ambiguous abbreviation \"%s\" matches both \"%s\" and \"%s\"",
4967 value,
4968 formats[match_pos].name, formats[i].name);
4969 return false;
4970 }
4971 }
4972 }
4973 if (match_pos >= 0)
4974 popt->topt.format = formats[match_pos].number;
4975 else if (pg_strncasecmp("latex-longtable", value, vallen) == 0)
4976 {
4977 /*
4978 * We must treat latex-longtable specially because latex is a
4979 * prefix of it; if both were in the table above, we'd think
4980 * "latex" is ambiguous.
4981 */
4983 }
4984 else
4985 {
4986 pg_log_error("\\pset: allowed formats are aligned, asciidoc, csv, html, latex, latex-longtable, troff-ms, unaligned, wrapped");
4987 return false;
4988 }
4989 }
4990 }
4991
4992 /* set table line style */
4993 else if (strcmp(param, "linestyle") == 0)
4994 {
4995 if (!value)
4996 ;
4997 else if (pg_strncasecmp("ascii", value, vallen) == 0)
4999 else if (pg_strncasecmp("old-ascii", value, vallen) == 0)
5001 else if (pg_strncasecmp("unicode", value, vallen) == 0)
5002 popt->topt.line_style = &pg_utf8format;
5003 else
5004 {
5005 pg_log_error("\\pset: allowed line styles are ascii, old-ascii, unicode");
5006 return false;
5007 }
5008 }
5009
5010 /* set unicode border line style */
5011 else if (strcmp(param, "unicode_border_linestyle") == 0)
5012 {
5013 if (!value)
5014 ;
5015 else if (set_unicode_line_style(value, vallen,
5017 refresh_utf8format(&(popt->topt));
5018 else
5019 {
5020 pg_log_error("\\pset: allowed Unicode border line styles are single, double");
5021 return false;
5022 }
5023 }
5024
5025 /* set unicode column line style */
5026 else if (strcmp(param, "unicode_column_linestyle") == 0)
5027 {
5028 if (!value)
5029 ;
5030 else if (set_unicode_line_style(value, vallen,
5032 refresh_utf8format(&(popt->topt));
5033 else
5034 {
5035 pg_log_error("\\pset: allowed Unicode column line styles are single, double");
5036 return false;
5037 }
5038 }
5039
5040 /* set unicode header line style */
5041 else if (strcmp(param, "unicode_header_linestyle") == 0)
5042 {
5043 if (!value)
5044 ;
5045 else if (set_unicode_line_style(value, vallen,
5047 refresh_utf8format(&(popt->topt));
5048 else
5049 {
5050 pg_log_error("\\pset: allowed Unicode header line styles are single, double");
5051 return false;
5052 }
5053 }
5054
5055 /* set border style/width */
5056 else if (strcmp(param, "border") == 0)
5057 {
5058 if (value)
5059 popt->topt.border = atoi(value);
5060 }
5061
5062 /* set expanded/vertical mode */
5063 else if (strcmp(param, "x") == 0 ||
5064 strcmp(param, "expanded") == 0 ||
5065 strcmp(param, "vertical") == 0)
5066 {
5067 if (value && pg_strcasecmp(value, "auto") == 0)
5068 popt->topt.expanded = 2;
5069 else if (value)
5070 {
5071 bool on_off;
5072
5073 if (ParseVariableBool(value, NULL, &on_off))
5074 popt->topt.expanded = on_off ? 1 : 0;
5075 else
5076 {
5077 PsqlVarEnumError(param, value, "on, off, auto");
5078 return false;
5079 }
5080 }
5081 else
5082 popt->topt.expanded = !popt->topt.expanded;
5083 }
5084
5085 /* header line width in expanded mode */
5086 else if (strcmp(param, "xheader_width") == 0)
5087 {
5088 if (!value)
5089 ;
5090 else if (pg_strcasecmp(value, "full") == 0)
5092 else if (pg_strcasecmp(value, "column") == 0)
5094 else if (pg_strcasecmp(value, "page") == 0)
5096 else
5097 {
5098 int intval = atoi(value);
5099
5100 if (intval == 0)
5101 {
5102 pg_log_error("\\pset: allowed xheader_width values are \"%s\" (default), \"%s\", \"%s\", or a number specifying the exact width", "full", "column", "page");
5103 return false;
5104 }
5105
5107 popt->topt.expanded_header_exact_width = intval;
5108 }
5109 }
5110
5111 /* field separator for CSV format */
5112 else if (strcmp(param, "csv_fieldsep") == 0)
5113 {
5114 if (value)
5115 {
5116 /* CSV separator has to be a one-byte character */
5117 if (strlen(value) != 1)
5118 {
5119 pg_log_error("\\pset: csv_fieldsep must be a single one-byte character");
5120 return false;
5121 }
5122 if (value[0] == '"' || value[0] == '\n' || value[0] == '\r')
5123 {
5124 pg_log_error("\\pset: csv_fieldsep cannot be a double quote, a newline, or a carriage return");
5125 return false;
5126 }
5127 popt->topt.csvFieldSep[0] = value[0];
5128 }
5129 }
5130
5131 /* locale-aware numeric output */
5132 else if (strcmp(param, "numericlocale") == 0)
5133 {
5134 if (value)
5135 return ParseVariableBool(value, param, &popt->topt.numericLocale);
5136 else
5137 popt->topt.numericLocale = !popt->topt.numericLocale;
5138 }
5139
5140 /* null display */
5141 else if (strcmp(param, "null") == 0)
5142 {
5143 if (value)
5144 {
5145 free(popt->nullPrint);
5146 popt->nullPrint = pg_strdup(value);
5147 }
5148 }
5149
5150 /* field separator for unaligned text */
5151 else if (strcmp(param, "fieldsep") == 0)
5152 {
5153 if (value)
5154 {
5155 free(popt->topt.fieldSep.separator);
5157 popt->topt.fieldSep.separator_zero = false;
5158 }
5159 }
5160
5161 else if (strcmp(param, "fieldsep_zero") == 0)
5162 {
5163 free(popt->topt.fieldSep.separator);
5164 popt->topt.fieldSep.separator = NULL;
5165 popt->topt.fieldSep.separator_zero = true;
5166 }
5167
5168 /* record separator for unaligned text */
5169 else if (strcmp(param, "recordsep") == 0)
5170 {
5171 if (value)
5172 {
5175 popt->topt.recordSep.separator_zero = false;
5176 }
5177 }
5178
5179 else if (strcmp(param, "recordsep_zero") == 0)
5180 {
5182 popt->topt.recordSep.separator = NULL;
5183 popt->topt.recordSep.separator_zero = true;
5184 }
5185
5186 /* toggle between full and tuples-only format */
5187 else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0)
5188 {
5189 if (value)
5190 return ParseVariableBool(value, param, &popt->topt.tuples_only);
5191 else
5192 popt->topt.tuples_only = !popt->topt.tuples_only;
5193 }
5194
5195 /* set title override */
5196 else if (strcmp(param, "C") == 0 || strcmp(param, "title") == 0)
5197 {
5198 free(popt->title);
5199 if (!value)
5200 popt->title = NULL;
5201 else
5202 popt->title = pg_strdup(value);
5203 }
5204
5205 /* set HTML table tag options */
5206 else if (strcmp(param, "T") == 0 || strcmp(param, "tableattr") == 0)
5207 {
5208 free(popt->topt.tableAttr);
5209 if (!value)
5210 popt->topt.tableAttr = NULL;
5211 else
5212 popt->topt.tableAttr = pg_strdup(value);
5213 }
5214
5215 /* toggle use of pager */
5216 else if (strcmp(param, "pager") == 0)
5217 {
5218 if (value && pg_strcasecmp(value, "always") == 0)
5219 popt->topt.pager = 2;
5220 else if (value)
5221 {
5222 bool on_off;
5223
5224 if (!ParseVariableBool(value, NULL, &on_off))
5225 {
5226 PsqlVarEnumError(param, value, "on, off, always");
5227 return false;
5228 }
5229 popt->topt.pager = on_off ? 1 : 0;
5230 }
5231 else if (popt->topt.pager == 1)
5232 popt->topt.pager = 0;
5233 else
5234 popt->topt.pager = 1;
5235 }
5236
5237 /* set minimum lines for pager use */
5238 else if (strcmp(param, "pager_min_lines") == 0)
5239 {
5240 if (value &&
5241 !ParseVariableNum(value, "pager_min_lines", &popt->topt.pager_min_lines))
5242 return false;
5243 }
5244
5245 /* disable "(x rows)" footer */
5246 else if (strcmp(param, "footer") == 0)
5247 {
5248 if (value)
5249 return ParseVariableBool(value, param, &popt->topt.default_footer);
5250 else
5251 popt->topt.default_footer = !popt->topt.default_footer;
5252 }
5253
5254 /* set border style/width */
5255 else if (strcmp(param, "columns") == 0)
5256 {
5257 if (value)
5258 popt->topt.columns = atoi(value);
5259 }
5260 else
5261 {
5262 pg_log_error("\\pset: unknown option: %s", param);
5263 return false;
5264 }
5265
5266 if (!quiet)
5267 printPsetInfo(param, &pset.popt);
5268
5269 return true;
5270}
#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:4880
static bool printPsetInfo(const char *param, printQueryOpt *popt)
Definition: command.c:5276
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 @162 value
int i
Definition: isn.c:72
#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:106
printTableOpt topt
Definition: print.h:185
char * nullPrint
Definition: print.h:186
char * title
Definition: print.h:187
unsigned short int expanded
Definition: print.h:114
unicode_linestyle unicode_border_linestyle
Definition: print.h:141
bool tuples_only
Definition: print.h:126
int columns
Definition: print.h:140
enum printFormat format
Definition: print.h:113
struct separator fieldSep
Definition: print.h:132
int expanded_header_exact_width
Definition: print.h:118
struct separator recordSep
Definition: print.h:133
printXheaderWidthType expanded_header_width_type
Definition: print.h:116
char csvFieldSep[2]
Definition: print.h:134
const printTextFormat * line_style
Definition: print.h:131
bool default_footer
Definition: print.h:129
int pager_min_lines
Definition: print.h:124
unsigned short int pager
Definition: print.h:122
char * tableAttr
Definition: print.h:137
bool numericLocale
Definition: print.h:135
unsigned short int border
Definition: print.h:120
unicode_linestyle unicode_header_linestyle
Definition: print.h:143
unicode_linestyle unicode_column_linestyle
Definition: print.h:142
bool separator_zero
Definition: print.h:108
char * separator
Definition: print.h:107
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition: variables.c:416
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:107
bool ParseVariableNum(const char *value, const char *name, int *result)
Definition: variables.c:156
const char * name

References Assert(), printTableOpt::border, printTableOpt::columns, printTableOpt::csvFieldSep, printTableOpt::default_footer, printTableOpt::expanded, printTableOpt::expanded_header_exact_width, printTableOpt::expanded_header_width_type, printTableOpt::fieldSep, 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 224 of file command.c.

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

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

4771{
4772 FILE *fd;
4773 int result;
4774 char *oldfilename;
4775 char relpath[MAXPGPATH];
4776
4777 if (!filename)
4778 {
4779 fd = stdin;
4780 filename = NULL;
4781 }
4782 else if (strcmp(filename, "-") != 0)
4783 {
4785
4786 /*
4787 * If we were asked to resolve the pathname relative to the location
4788 * of the currently executing script, and there is one, and this is a
4789 * relative pathname, then prepend all but the last pathname component
4790 * of the current script to this pathname.
4791 */
4792 if (use_relative_path && pset.inputfile &&
4794 {
4799
4800 filename = relpath;
4801 }
4802
4803 fd = fopen(filename, PG_BINARY_R);
4804
4805 if (!fd)
4806 {
4807 pg_log_error("%s: %m", filename);
4808 return EXIT_FAILURE;
4809 }
4810 }
4811 else
4812 {
4813 fd = stdin;
4814 filename = "<stdin>"; /* for future error messages */
4815 }
4816
4817 oldfilename = pset.inputfile;
4819
4821
4822 result = MainLoop(fd);
4823
4824 if (fd != stdin)
4825 fclose(fd);
4826
4827 pset.inputfile = oldfilename;
4828
4830
4831 return result;
4832}
#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:190
int encoding
Definition: settings.h:98
char * inputfile
Definition: settings.h:137

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

5513{
5514 /* Free all the old data we're about to overwrite the pointers to. */
5515
5516 /* topt.line_style points to const data that need not be duplicated */
5517 free(popt->topt.fieldSep.separator);
5519 free(popt->topt.tableAttr);
5520 free(popt->nullPrint);
5521 free(popt->title);
5522
5523 /*
5524 * footers and translate_columns are never set in psql's print settings,
5525 * so we needn't write code to duplicate them.
5526 */
5527 Assert(popt->footers == NULL);
5528 Assert(popt->translate_columns == NULL);
5529
5530 /* Now we may flat-copy all the fields, including pointers. */
5531 memcpy(popt, save, sizeof(printQueryOpt));
5532
5533 /* Lastly, free "save" ... but its sub-structures now belong to popt. */
5534 free(save);
5535}
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 5476 of file command.c.

5477{
5478 printQueryOpt *save;
5479
5480 save = (printQueryOpt *) pg_malloc(sizeof(printQueryOpt));
5481
5482 /* Flat-copy all the scalar fields, then duplicate sub-structures. */
5483 memcpy(save, popt, sizeof(printQueryOpt));
5484
5485 /* topt.line_style points to const data that need not be duplicated */
5486 if (popt->topt.fieldSep.separator)
5488 if (popt->topt.recordSep.separator)
5490 if (popt->topt.tableAttr)
5491 save->topt.tableAttr = pg_strdup(popt->topt.tableAttr);
5492 if (popt->nullPrint)
5493 save->nullPrint = pg_strdup(popt->nullPrint);
5494 if (popt->title)
5495 save->title = pg_strdup(popt->title);
5496
5497 /*
5498 * footers and translate_columns are never set in psql's print settings,
5499 * so we needn't write code to duplicate them.
5500 */
5501 Assert(popt->footers == NULL);
5502 Assert(popt->translate_columns == NULL);
5503
5504 return save;
5505}
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 4428 of file command.c.

4429{
4430 char vbuf[32];
4431 const char *server_version;
4432
4433 /* get stuff from connection */
4437
4439
4440 SetVariable(pset.vars, "DBNAME", PQdb(pset.db));
4441 SetVariable(pset.vars, "SERVICE", PQservice(pset.db));
4442 SetVariable(pset.vars, "USER", PQuser(pset.db));
4443 SetVariable(pset.vars, "HOST", PQhost(pset.db));
4444 SetVariable(pset.vars, "PORT", PQport(pset.db));
4446
4447 /* this bit should match connection_warnings(): */
4448 /* Try to get full text form of version, might include "devel" etc */
4449 server_version = PQparameterStatus(pset.db, "server_version");
4450 /* Otherwise fall back on pset.sversion */
4451 if (!server_version)
4452 {
4453 formatPGVersionNumber(pset.sversion, true, vbuf, sizeof(vbuf));
4454 server_version = vbuf;
4455 }
4456 SetVariable(pset.vars, "SERVER_VERSION_NAME", server_version);
4457
4458 snprintf(vbuf, sizeof(vbuf), "%d", pset.sversion);
4459 SetVariable(pset.vars, "SERVER_VERSION_NUM", vbuf);
4460
4461 /* send stuff to it, too */
4464}
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:7543
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:7381
char * PQport(const PGconn *conn)
Definition: fe-connect.c:7458
char * PQhost(const PGconn *conn)
Definition: fe-connect.c:7422
char * PQservice(const PGconn *conn)
Definition: fe-connect.c:7389
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:7643
PGContextVisibility PQsetErrorContextVisibility(PGconn *conn, PGContextVisibility show_context)
Definition: fe-connect.c:7705
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:7397
PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity)
Definition: fe-connect.c:7693
#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:145
PGVerbosity verbosity
Definition: settings.h:177
PGContextVisibility show_context
Definition: settings.h:179
int encoding
Definition: print.h:138
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211

References _psqlSettings::db, _psqlSettings::encoding, printTableOpt::encoding, formatPGVersionNumber(), pg_encoding_to_char, _psqlSettings::popt, PQclientEncoding(), PQdb(), PQhost(), PQparameterStatus(), PQport(), PQserverVersion(), 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 4472 of file command.c.

4473{
4474 SetVariable(pset.vars, "DBNAME", NULL);
4475 SetVariable(pset.vars, "SERVICE", NULL);
4476 SetVariable(pset.vars, "USER", NULL);
4477 SetVariable(pset.vars, "HOST", NULL);
4478 SetVariable(pset.vars, "PORT", NULL);
4479 SetVariable(pset.vars, "ENCODING", NULL);
4480 SetVariable(pset.vars, "SERVER_VERSION_NAME", NULL);
4481 SetVariable(pset.vars, "SERVER_VERSION_NUM", NULL);
4482}

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

Referenced by CheckConnection(), and do_connect().