PostgreSQL Source Code  git master
common.h File Reference
#include <setjmp.h>
#include <signal.h>
#include "fe_utils/print.h"
#include "fe_utils/psqlscan.h"
#include "libpq-fe.h"
Include dependency graph for common.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

bool openQueryOutputFile (const char *fname, FILE **fout, bool *is_pipe)
 
bool setQFout (const char *fname)
 
char * psql_get_variable (const char *varname, PsqlScanQuoteType quote, void *passthrough)
 
void NoticeProcessor (void *arg, const char *message)
 
void psql_setup_cancel_handler (void)
 
void SetShellResultVariables (int wait_result)
 
PGresultPSQLexec (const char *query)
 
int PSQLexecWatch (const char *query, const printQueryOpt *opt, FILE *printQueryFout, int min_rows)
 
bool SendQuery (const char *query)
 
bool is_superuser (void)
 
bool standard_strings (void)
 
const char * session_username (void)
 
void expand_tilde (char **filename)
 
bool recognized_connection_string (const char *connstr)
 

Variables

volatile sig_atomic_t sigint_interrupt_enabled
 
sigjmp_buf sigint_interrupt_jmp
 

Function Documentation

◆ expand_tilde()

void expand_tilde ( char **  filename)

Definition at line 2352 of file common.c.

2353 {
2354  if (!filename || !(*filename))
2355  return;
2356 
2357  /*
2358  * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde
2359  * for short versions of long file names, though the tilde is usually
2360  * toward the end, not at the beginning.
2361  */
2362 #ifndef WIN32
2363 
2364  /* try tilde expansion */
2365  if (**filename == '~')
2366  {
2367  char *fn;
2368  char oldp,
2369  *p;
2370  struct passwd *pw;
2371  char home[MAXPGPATH];
2372 
2373  fn = *filename;
2374  *home = '\0';
2375 
2376  p = fn + 1;
2377  while (*p != '/' && *p != '\0')
2378  p++;
2379 
2380  oldp = *p;
2381  *p = '\0';
2382 
2383  if (*(fn + 1) == '\0')
2384  get_home_path(home); /* ~ or ~/ only */
2385  else if ((pw = getpwnam(fn + 1)) != NULL)
2386  strlcpy(home, pw->pw_dir, sizeof(home)); /* ~user */
2387 
2388  *p = oldp;
2389  if (strlen(home) != 0)
2390  {
2391  char *newfn;
2392 
2393  newfn = psprintf("%s%s", home, p);
2394  free(fn);
2395  *filename = newfn;
2396  }
2397  }
2398 #endif
2399 }
#define free(a)
Definition: header.h:65
#define MAXPGPATH
static char * filename
Definition: pg_dumpall.c:121
bool get_home_path(char *ret_path)
Definition: path.c:927
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static void * fn(void *arg)
Definition: thread-alloc.c:119

References filename, fn(), free, get_home_path(), MAXPGPATH, psprintf(), and strlcpy().

Referenced by exec_command_edit(), exec_command_g(), exec_command_include(), exec_command_lo(), exec_command_out(), exec_command_s(), exec_command_write(), initializeInput(), parse_slash_copy(), and process_psqlrc().

◆ is_superuser()

bool is_superuser ( void  )

Definition at line 2291 of file common.c.

2292 {
2293  const char *val;
2294 
2295  if (!pset.db)
2296  return false;
2297 
2298  val = PQparameterStatus(pset.db, "is_superuser");
2299 
2300  if (val && strcmp(val, "on") == 0)
2301  return true;
2302 
2303  return false;
2304 }
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6913
long val
Definition: informix.c:670
PsqlSettings pset
Definition: startup.c:32
PGconn * db
Definition: settings.h:82

References _psqlSettings::db, PQparameterStatus(), pset, and val.

◆ NoticeProcessor()

void NoticeProcessor ( void *  arg,
const char *  message 
)

Definition at line 229 of file common.c.

230 {
231  (void) arg; /* not used */
232  pg_log_info("%s", message);
233 }
#define pg_log_info(...)
Definition: logging.h:124
void * arg

References arg, and pg_log_info.

Referenced by do_connect(), and main().

◆ openQueryOutputFile()

bool openQueryOutputFile ( const char *  fname,
FILE **  fout,
bool is_pipe 
)

Definition at line 58 of file common.c.

59 {
60  if (!fname || fname[0] == '\0')
61  {
62  *fout = stdout;
63  *is_pipe = false;
64  }
65  else if (*fname == '|')
66  {
67  fflush(NULL);
68  *fout = popen(fname + 1, "w");
69  *is_pipe = true;
70  }
71  else
72  {
73  *fout = fopen(fname, "w");
74  *is_pipe = false;
75  }
76 
77  if (*fout == NULL)
78  {
79  pg_log_error("%s: %m", fname);
80  return false;
81  }
82 
83  return true;
84 }
static void const char fflush(stdout)
#define pg_log_error(...)
Definition: logging.h:106

References fflush(), pg_log_error, and generate_unaccent_rules::stdout.

Referenced by ExecQueryAndProcessResults(), ExecQueryUsingCursor(), and setQFout().

◆ psql_get_variable()

char* psql_get_variable ( const char *  varname,
PsqlScanQuoteType  quote,
void *  passthrough 
)

Definition at line 138 of file common.c.

140 {
141  char *result = NULL;
142  const char *value;
143 
144  /* In an inactive \if branch, suppress all variable substitutions */
145  if (passthrough && !conditional_active((ConditionalStack) passthrough))
146  return NULL;
147 
148  value = GetVariable(pset.vars, varname);
149  if (!value)
150  return NULL;
151 
152  switch (quote)
153  {
154  case PQUOTE_PLAIN:
155  result = pg_strdup(value);
156  break;
157  case PQUOTE_SQL_LITERAL:
158  case PQUOTE_SQL_IDENT:
159  {
160  /*
161  * For these cases, we use libpq's quoting functions, which
162  * assume the string is in the connection's client encoding.
163  */
164  char *escaped_value;
165 
166  if (!pset.db)
167  {
168  pg_log_error("cannot escape without active connection");
169  return NULL;
170  }
171 
172  if (quote == PQUOTE_SQL_LITERAL)
173  escaped_value =
174  PQescapeLiteral(pset.db, value, strlen(value));
175  else
176  escaped_value =
177  PQescapeIdentifier(pset.db, value, strlen(value));
178 
179  if (escaped_value == NULL)
180  {
181  const char *error = PQerrorMessage(pset.db);
182 
183  pg_log_info("%s", error);
184  return NULL;
185  }
186 
187  /*
188  * Rather than complicate the lexer's API with a notion of
189  * which free() routine to use, just pay the price of an extra
190  * strdup().
191  */
192  result = pg_strdup(escaped_value);
193  PQfreemem(escaped_value);
194  break;
195  }
196  case PQUOTE_SHELL_ARG:
197  {
198  /*
199  * For this we use appendShellStringNoError, which is
200  * encoding-agnostic, which is fine since the shell probably
201  * is too. In any case, the only special character is "'",
202  * which is not known to appear in valid multibyte characters.
203  */
205 
208  {
209  pg_log_error("shell command argument contains a newline or carriage return: \"%s\"",
210  value);
211  free(buf.data);
212  return NULL;
213  }
214  result = buf.data;
215  break;
216  }
217 
218  /* No default: we want a compiler warning for missing cases */
219  }
220 
221  return result;
222 }
bool conditional_active(ConditionalStack cstack)
Definition: conditional.c:140
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6948
void PQfreemem(void *ptr)
Definition: fe-exec.c:3992
char * PQescapeIdentifier(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:4270
char * PQescapeLiteral(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:4264
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static struct @154 value
static char * buf
Definition: pg_test_fsync.c:73
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:90
@ PQUOTE_SQL_LITERAL
Definition: psqlscan.h:55
@ PQUOTE_PLAIN
Definition: psqlscan.h:54
@ PQUOTE_SHELL_ARG
Definition: psqlscan.h:57
@ PQUOTE_SQL_IDENT
Definition: psqlscan.h:56
static void error(void)
Definition: sql-dyntest.c:147
bool appendShellStringNoError(PQExpBuffer buf, const char *str)
Definition: string_utils.c:441
VariableSpace vars
Definition: settings.h:122
const char * GetVariable(VariableSpace space, const char *name)
Definition: variables.c:71

References appendShellStringNoError(), buf, conditional_active(), _psqlSettings::db, error(), free, GetVariable(), initPQExpBuffer(), pg_log_error, pg_log_info, pg_strdup(), PQerrorMessage(), PQescapeIdentifier(), PQescapeLiteral(), PQfreemem(), PQUOTE_PLAIN, PQUOTE_SHELL_ARG, PQUOTE_SQL_IDENT, PQUOTE_SQL_LITERAL, pset, value, and _psqlSettings::vars.

◆ psql_setup_cancel_handler()

void psql_setup_cancel_handler ( void  )

Definition at line 275 of file common.c.

276 {
278 }
static void psql_cancel_callback(void)
Definition: common.c:259
void setup_cancel_handler(void(*query_cancel_callback)(void))
Definition: cancel.c:183

References psql_cancel_callback(), and setup_cancel_handler().

Referenced by main().

◆ PSQLexec()

PGresult* PSQLexec ( const char *  query)

Definition at line 581 of file common.c.

582 {
583  PGresult *res;
584 
585  if (!pset.db)
586  {
587  pg_log_error("You are currently not connected to a database.");
588  return NULL;
589  }
590 
592  {
593  printf(_("/******** QUERY *********/\n"
594  "%s\n"
595  "/************************/\n\n"), query);
596  fflush(stdout);
597  if (pset.logfile)
598  {
600  _("/******** QUERY *********/\n"
601  "%s\n"
602  "/************************/\n\n"), query);
604  }
605 
607  return NULL;
608  }
609 
611 
612  res = PQexec(pset.db, query);
613 
614  ResetCancelConn();
615 
616  if (!AcceptResult(res, true))
617  {
619  res = NULL;
620  }
621 
622  return res;
623 }
static void ClearOrSaveResult(PGresult *result)
Definition: common.c:484
static bool AcceptResult(const PGresult *result, bool show_error)
Definition: common.c:365
void ResetCancelConn(void)
Definition: cancel.c:107
void SetCancelConn(PGconn *conn)
Definition: cancel.c:77
#define _(x)
Definition: elog.c:90
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2224
#define fprintf
Definition: port.h:242
#define printf(...)
Definition: port.h:244
@ PSQL_ECHO_HIDDEN_NOEXEC
Definition: settings.h:47
@ PSQL_ECHO_HIDDEN_OFF
Definition: settings.h:45
FILE * logfile
Definition: settings.h:120
PSQL_ECHO_HIDDEN echo_hidden
Definition: settings.h:147

References _, AcceptResult(), ClearOrSaveResult(), _psqlSettings::db, _psqlSettings::echo_hidden, fflush(), fprintf, _psqlSettings::logfile, pg_log_error, PQexec(), printf, pset, PSQL_ECHO_HIDDEN_NOEXEC, PSQL_ECHO_HIDDEN_OFF, res, ResetCancelConn(), SetCancelConn(), and generate_unaccent_rules::stdout.

Referenced by add_tablespace_footer(), addFooterToPublicationDesc(), describeAccessMethods(), describeAggregates(), describeConfigurationParameters(), describeFunctions(), describeOneTableDetails(), describeOneTSConfig(), describeOneTSParser(), describeOperators(), describePublications(), describeRoleGrants(), describeRoles(), describeSubscriptions(), describeTableDetails(), describeTablespaces(), describeTypes(), do_lo_import(), exec_command_password(), fail_lo_xact(), finish_lo_xact(), listAllDbs(), listCasts(), listCollations(), listConversions(), listDbRoleSettings(), listDefaultACLs(), listDomains(), listEventTriggers(), listExtendedStats(), listExtensionContents(), listExtensions(), listForeignDataWrappers(), listForeignServers(), listForeignTables(), listLanguages(), listLargeObjects(), listOneExtensionContents(), listOperatorClasses(), listOperatorFamilies(), listOpFamilyFunctions(), listOpFamilyOperators(), listPartitionedTables(), listPublications(), listSchemas(), listTables(), listTSConfigs(), listTSConfigsVerbose(), listTSDictionaries(), listTSParsers(), listTSParsersVerbose(), listTSTemplates(), listUserMappings(), main(), objectDescription(), permissionsList(), and start_lo_xact().

◆ PSQLexecWatch()

int PSQLexecWatch ( const char *  query,
const printQueryOpt opt,
FILE *  printQueryFout,
int  min_rows 
)

Definition at line 636 of file common.c.

637 {
638  bool timing = pset.timing;
639  double elapsed_msec = 0;
640  int res;
641 
642  if (!pset.db)
643  {
644  pg_log_error("You are currently not connected to a database.");
645  return 0;
646  }
647 
649 
650  res = ExecQueryAndProcessResults(query, &elapsed_msec, NULL, true, min_rows, opt, printQueryFout);
651 
652  ResetCancelConn();
653 
654  /* Possible microtiming output */
655  if (timing)
656  PrintTiming(elapsed_msec);
657 
658  return res;
659 }
static int ExecQueryAndProcessResults(const char *query, double *elapsed_msec, bool *svpt_gone_p, bool is_watch, int min_rows, const printQueryOpt *opt, FILE *printQueryFout)
Definition: common.c:1421
static void PrintTiming(double elapsed_msec)
Definition: common.c:522

References _psqlSettings::db, ExecQueryAndProcessResults(), pg_log_error, PrintTiming(), pset, res, ResetCancelConn(), SetCancelConn(), and _psqlSettings::timing.

Referenced by do_watch().

◆ recognized_connection_string()

bool recognized_connection_string ( const char *  connstr)

Definition at line 2437 of file common.c.

2438 {
2439  return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL;
2440 }
static int uri_prefix_length(const char *connstr)
Definition: common.c:2410
static char * connstr
Definition: pg_dumpall.c:90

References connstr, and uri_prefix_length().

Referenced by do_connect().

◆ SendQuery()

bool SendQuery ( const char *  query)

Definition at line 1047 of file common.c.

1048 {
1049  bool timing = pset.timing;
1050  PGTransactionStatusType transaction_status;
1051  double elapsed_msec = 0;
1052  bool OK = false;
1053  int i;
1054  bool on_error_rollback_savepoint = false;
1055  bool svpt_gone = false;
1056 
1057  if (!pset.db)
1058  {
1059  pg_log_error("You are currently not connected to a database.");
1060  goto sendquery_cleanup;
1061  }
1062 
1063  if (pset.singlestep)
1064  {
1065  char buf[3];
1066 
1067  fflush(stderr);
1068  printf(_("/**(Single step mode: verify command)******************************************/\n"
1069  "%s\n"
1070  "/**(press return to proceed or enter x and return to cancel)*******************/\n"),
1071  query);
1072  fflush(stdout);
1073  if (fgets(buf, sizeof(buf), stdin) != NULL)
1074  if (buf[0] == 'x')
1075  goto sendquery_cleanup;
1076  if (cancel_pressed)
1077  goto sendquery_cleanup;
1078  }
1079  else if (pset.echo == PSQL_ECHO_QUERIES)
1080  {
1081  puts(query);
1082  fflush(stdout);
1083  }
1084 
1085  if (pset.logfile)
1086  {
1088  _("/******** QUERY *********/\n"
1089  "%s\n"
1090  "/************************/\n\n"), query);
1091  fflush(pset.logfile);
1092  }
1093 
1095 
1096  transaction_status = PQtransactionStatus(pset.db);
1097 
1098  if (transaction_status == PQTRANS_IDLE &&
1099  !pset.autocommit &&
1100  !command_no_begin(query))
1101  {
1102  PGresult *result;
1103 
1104  result = PQexec(pset.db, "BEGIN");
1105  if (PQresultStatus(result) != PGRES_COMMAND_OK)
1106  {
1108  ClearOrSaveResult(result);
1109  goto sendquery_cleanup;
1110  }
1111  ClearOrSaveResult(result);
1112  transaction_status = PQtransactionStatus(pset.db);
1113  }
1114 
1115  if (transaction_status == PQTRANS_INTRANS &&
1119  {
1120  PGresult *result;
1121 
1122  result = PQexec(pset.db, "SAVEPOINT pg_psql_temporary_savepoint");
1123  if (PQresultStatus(result) != PGRES_COMMAND_OK)
1124  {
1126  ClearOrSaveResult(result);
1127  goto sendquery_cleanup;
1128  }
1129  ClearOrSaveResult(result);
1130  on_error_rollback_savepoint = true;
1131  }
1132 
1133  if (pset.gdesc_flag)
1134  {
1135  /* Describe query's result columns, without executing it */
1136  OK = DescribeQuery(query, &elapsed_msec);
1137  }
1138  else if (pset.fetch_count <= 0 || pset.gexec_flag ||
1140  {
1141  /* Default fetch-it-all-and-print mode */
1142  OK = (ExecQueryAndProcessResults(query, &elapsed_msec, &svpt_gone, false, 0, NULL, NULL) > 0);
1143  }
1144  else
1145  {
1146  /* Fetch-in-segments mode */
1147  OK = ExecQueryUsingCursor(query, &elapsed_msec);
1148  }
1149 
1150  if (!OK && pset.echo == PSQL_ECHO_ERRORS)
1151  pg_log_info("STATEMENT: %s", query);
1152 
1153  /* If we made a temporary savepoint, possibly release/rollback */
1154  if (on_error_rollback_savepoint)
1155  {
1156  const char *svptcmd = NULL;
1157 
1158  transaction_status = PQtransactionStatus(pset.db);
1159 
1160  switch (transaction_status)
1161  {
1162  case PQTRANS_INERROR:
1163  /* We always rollback on an error */
1164  svptcmd = "ROLLBACK TO pg_psql_temporary_savepoint";
1165  break;
1166 
1167  case PQTRANS_IDLE:
1168  /* If they are no longer in a transaction, then do nothing */
1169  break;
1170 
1171  case PQTRANS_INTRANS:
1172 
1173  /*
1174  * Release our savepoint, but do nothing if they are messing
1175  * with savepoints themselves
1176  */
1177  if (!svpt_gone)
1178  svptcmd = "RELEASE pg_psql_temporary_savepoint";
1179  break;
1180 
1181  case PQTRANS_ACTIVE:
1182  case PQTRANS_UNKNOWN:
1183  default:
1184  OK = false;
1185  /* PQTRANS_UNKNOWN is expected given a broken connection. */
1186  if (transaction_status != PQTRANS_UNKNOWN || ConnectionUp())
1187  pg_log_error("unexpected transaction status (%d)",
1188  transaction_status);
1189  break;
1190  }
1191 
1192  if (svptcmd)
1193  {
1194  PGresult *svptres;
1195 
1196  svptres = PQexec(pset.db, svptcmd);
1197  if (PQresultStatus(svptres) != PGRES_COMMAND_OK)
1198  {
1200  ClearOrSaveResult(svptres);
1201  OK = false;
1202 
1203  goto sendquery_cleanup;
1204  }
1205  PQclear(svptres);
1206  }
1207  }
1208 
1209  /* Possible microtiming output */
1210  if (timing)
1211  PrintTiming(elapsed_msec);
1212 
1213  /* check for events that may occur during query execution */
1214 
1215  if (pset.encoding != PQclientEncoding(pset.db) &&
1216  PQclientEncoding(pset.db) >= 0)
1217  {
1218  /* track effects of SET CLIENT_ENCODING */
1221  SetVariable(pset.vars, "ENCODING",
1223  }
1224 
1226 
1227  /* perform cleanup that should occur after any attempted query */
1228 
1229 sendquery_cleanup:
1230 
1231  /* global cancellation reset */
1232  ResetCancelConn();
1233 
1234  /* reset \g's output-to-filename trigger */
1235  if (pset.gfname)
1236  {
1237  free(pset.gfname);
1238  pset.gfname = NULL;
1239  }
1240 
1241  /* restore print settings if \g changed them */
1242  if (pset.gsavepopt)
1243  {
1245  pset.gsavepopt = NULL;
1246  }
1247 
1248  /* clean up after \bind */
1249  if (pset.bind_flag)
1250  {
1251  for (i = 0; i < pset.bind_nparams; i++)
1252  free(pset.bind_params[i]);
1254  pset.bind_params = NULL;
1255  pset.bind_flag = false;
1256  }
1257 
1258  /* reset \gset trigger */
1259  if (pset.gset_prefix)
1260  {
1262  pset.gset_prefix = NULL;
1263  }
1264 
1265  /* reset \gdesc trigger */
1266  pset.gdesc_flag = false;
1267 
1268  /* reset \gexec trigger */
1269  pset.gexec_flag = false;
1270 
1271  /* reset \crosstabview trigger */
1272  pset.crosstab_flag = false;
1273  for (i = 0; i < lengthof(pset.ctv_args); i++)
1274  {
1275  pg_free(pset.ctv_args[i]);
1276  pset.ctv_args[i] = NULL;
1277  }
1278 
1279  return OK;
1280 }
static bool DescribeQuery(const char *query, double *elapsed_msec)
Definition: common.c:1292
static bool ExecQueryUsingCursor(const char *query, double *elapsed_msec)
Definition: common.c:1714
static void PrintNotifications(void)
Definition: common.c:666
static bool is_select_command(const char *query)
Definition: common.c:2254
static bool ConnectionUp(void)
Definition: common.c:286
static bool command_no_begin(const char *query)
Definition: common.c:2039
#define lengthof(array)
Definition: c.h:775
void restorePsetInfo(printQueryOpt *popt, printQueryOpt *save)
Definition: command.c:4958
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:6903
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:7036
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3371
void pg_free(void *ptr)
Definition: fe_memutils.c:105
volatile sig_atomic_t cancel_pressed
Definition: print.c:43
int i
Definition: isn.c:73
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:100
PGTransactionStatusType
Definition: libpq-fe.h:120
@ PQTRANS_INTRANS
Definition: libpq-fe.h:123
@ PQTRANS_IDLE
Definition: libpq-fe.h:121
@ PQTRANS_ACTIVE
Definition: libpq-fe.h:122
@ PQTRANS_UNKNOWN
Definition: libpq-fe.h:125
@ PQTRANS_INERROR
Definition: libpq-fe.h:124
#define pg_encoding_to_char
Definition: pg_wchar.h:630
@ PSQL_ERROR_ROLLBACK_ON
Definition: settings.h:54
@ PSQL_ERROR_ROLLBACK_OFF
Definition: settings.h:52
@ PSQL_ECHO_ERRORS
Definition: settings.h:39
@ PSQL_ECHO_QUERIES
Definition: settings.h:38
printQueryOpt popt
Definition: settings.h:91
bool bind_flag
Definition: settings.h:99
char * gset_prefix
Definition: settings.h:96
PSQL_ERROR_ROLLBACK on_error_rollback
Definition: settings.h:148
int encoding
Definition: settings.h:83
bool autocommit
Definition: settings.h:136
char * ctv_args[4]
Definition: settings.h:104
char ** bind_params
Definition: settings.h:102
PSQL_ECHO echo
Definition: settings.h:146
bool singlestep
Definition: settings.h:140
printQueryOpt * gsavepopt
Definition: settings.h:94
char * gfname
Definition: settings.h:93
int fetch_count
Definition: settings.h:143
bool cur_cmd_interactive
Definition: settings.h:111
bool gexec_flag
Definition: settings.h:98
bool crosstab_flag
Definition: settings.h:103
bool gdesc_flag
Definition: settings.h:97
int bind_nparams
Definition: settings.h:101
printTableOpt topt
Definition: print.h:185
int encoding
Definition: print.h:138
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211

References _, _psqlSettings::autocommit, _psqlSettings::bind_flag, _psqlSettings::bind_nparams, _psqlSettings::bind_params, buf, cancel_pressed, ClearOrSaveResult(), command_no_begin(), ConnectionUp(), _psqlSettings::crosstab_flag, _psqlSettings::ctv_args, _psqlSettings::cur_cmd_interactive, _psqlSettings::db, DescribeQuery(), _psqlSettings::echo, _psqlSettings::encoding, printTableOpt::encoding, ExecQueryAndProcessResults(), ExecQueryUsingCursor(), _psqlSettings::fetch_count, fflush(), fprintf, free, _psqlSettings::gdesc_flag, _psqlSettings::gexec_flag, _psqlSettings::gfname, _psqlSettings::gsavepopt, _psqlSettings::gset_prefix, i, is_select_command(), lengthof, _psqlSettings::logfile, _psqlSettings::on_error_rollback, pg_encoding_to_char, pg_free(), pg_log_error, pg_log_info, PGRES_COMMAND_OK, _psqlSettings::popt, PQclear(), PQclientEncoding(), PQerrorMessage(), PQexec(), PQresultStatus(), PQTRANS_ACTIVE, PQTRANS_IDLE, PQTRANS_INERROR, PQTRANS_INTRANS, PQTRANS_UNKNOWN, PQtransactionStatus(), printf, PrintNotifications(), PrintTiming(), pset, PSQL_ECHO_ERRORS, PSQL_ECHO_QUERIES, PSQL_ERROR_ROLLBACK_OFF, PSQL_ERROR_ROLLBACK_ON, ResetCancelConn(), restorePsetInfo(), SetCancelConn(), SetVariable(), _psqlSettings::singlestep, generate_unaccent_rules::stdout, _psqlSettings::timing, printQueryOpt::topt, and _psqlSettings::vars.

Referenced by do_copy(), ExecQueryTuples(), main(), and MainLoop().

◆ session_username()

const char* session_username ( void  )

Definition at line 2331 of file common.c.

2332 {
2333  const char *val;
2334 
2335  if (!pset.db)
2336  return NULL;
2337 
2338  val = PQparameterStatus(pset.db, "session_authorization");
2339  if (val)
2340  return val;
2341  else
2342  return PQuser(pset.db);
2343 }
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:6802

References _psqlSettings::db, PQparameterStatus(), PQuser(), pset, and val.

Referenced by get_prompt().

◆ setQFout()

bool setQFout ( const char *  fname)

Definition at line 94 of file common.c.

95 {
96  FILE *fout;
97  bool is_pipe;
98 
99  /* First make sure we can open the new output file/pipe */
100  if (!openQueryOutputFile(fname, &fout, &is_pipe))
101  return false;
102 
103  /* Close old file/pipe */
104  if (pset.queryFout && pset.queryFout != stdout && pset.queryFout != stderr)
105  {
106  if (pset.queryFoutPipe)
108  else
109  fclose(pset.queryFout);
110  }
111 
112  pset.queryFout = fout;
113  pset.queryFoutPipe = is_pipe;
114 
115  /* Adjust SIGPIPE handling appropriately: ignore signal if is_pipe */
116  set_sigpipe_trap_state(is_pipe);
118 
119  return true;
120 }
void SetShellResultVariables(int wait_result)
Definition: common.c:462
bool openQueryOutputFile(const char *fname, FILE **fout, bool *is_pipe)
Definition: common.c:58
void restore_sigpipe_trap(void)
Definition: print.c:3062
void set_sigpipe_trap_state(bool ignore)
Definition: print.c:3075
FILE * queryFout
Definition: settings.h:84
bool queryFoutPipe
Definition: settings.h:85

References openQueryOutputFile(), pset, _psqlSettings::queryFout, _psqlSettings::queryFoutPipe, restore_sigpipe_trap(), set_sigpipe_trap_state(), SetShellResultVariables(), and generate_unaccent_rules::stdout.

Referenced by exec_command_out(), main(), and parse_psql_options().

◆ SetShellResultVariables()

void SetShellResultVariables ( int  wait_result)

Definition at line 462 of file common.c.

463 {
464  char buf[32];
465 
466  SetVariable(pset.vars, "SHELL_ERROR",
467  (wait_result == 0) ? "false" : "true");
468  snprintf(buf, sizeof(buf), "%d", wait_result_to_exit_code(wait_result));
469  SetVariable(pset.vars, "SHELL_EXIT_CODE", buf);
470 }
#define snprintf
Definition: port.h:238
int wait_result_to_exit_code(int exit_status)
Definition: wait_error.c:138

References buf, pset, SetVariable(), snprintf, _psqlSettings::vars, and wait_result_to_exit_code().

Referenced by do_copy(), do_shell(), exec_command_write(), ExecQueryAndProcessResults(), ExecQueryUsingCursor(), and setQFout().

◆ standard_strings()

bool standard_strings ( void  )

Definition at line 2311 of file common.c.

2312 {
2313  const char *val;
2314 
2315  if (!pset.db)
2316  return false;
2317 
2318  val = PQparameterStatus(pset.db, "standard_conforming_strings");
2319 
2320  if (val && strcmp(val, "on") == 0)
2321  return true;
2322 
2323  return false;
2324 }

References _psqlSettings::db, PQparameterStatus(), pset, and val.

Referenced by get_create_object_cmd(), main(), MainLoop(), and parse_slash_copy().

Variable Documentation

◆ sigint_interrupt_enabled

volatile sig_atomic_t sigint_interrupt_enabled
extern

◆ sigint_interrupt_jmp

sigjmp_buf sigint_interrupt_jmp
extern