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 2180 of file common.c.

2181 {
2182  if (!filename || !(*filename))
2183  return;
2184 
2185  /*
2186  * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde
2187  * for short versions of long file names, though the tilde is usually
2188  * toward the end, not at the beginning.
2189  */
2190 #ifndef WIN32
2191 
2192  /* try tilde expansion */
2193  if (**filename == '~')
2194  {
2195  char *fn;
2196  char oldp,
2197  *p;
2198  struct passwd *pw;
2199  char home[MAXPGPATH];
2200 
2201  fn = *filename;
2202  *home = '\0';
2203 
2204  p = fn + 1;
2205  while (*p != '/' && *p != '\0')
2206  p++;
2207 
2208  oldp = *p;
2209  *p = '\0';
2210 
2211  if (*(fn + 1) == '\0')
2212  get_home_path(home); /* ~ or ~/ only */
2213  else if ((pw = getpwnam(fn + 1)) != NULL)
2214  strlcpy(home, pw->pw_dir, sizeof(home)); /* ~user */
2215 
2216  *p = oldp;
2217  if (strlen(home) != 0)
2218  {
2219  char *newfn;
2220 
2221  newfn = psprintf("%s%s", home, p);
2222  free(fn);
2223  *filename = newfn;
2224  }
2225  }
2226 #endif
2227 }
#define free(a)
Definition: header.h:65
#define MAXPGPATH
static char * filename
Definition: pg_dumpall.c:119
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 2119 of file common.c.

2120 {
2121  const char *val;
2122 
2123  if (!pset.db)
2124  return false;
2125 
2126  val = PQparameterStatus(pset.db, "is_superuser");
2127 
2128  if (val && strcmp(val, "on") == 0)
2129  return true;
2130 
2131  return false;
2132 }
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:7132
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 267 of file common.c.

268 {
269  (void) arg; /* not used */
270  pg_log_info("%s", message);
271 }
#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 56 of file common.c.

57 {
58  if (!fname || fname[0] == '\0')
59  {
60  *fout = stdout;
61  *is_pipe = false;
62  }
63  else if (*fname == '|')
64  {
65  fflush(NULL);
66  *fout = popen(fname + 1, "w");
67  *is_pipe = true;
68  }
69  else
70  {
71  *fout = fopen(fname, "w");
72  *is_pipe = false;
73  }
74 
75  if (*fout == NULL)
76  {
77  pg_log_error("%s: %m", fname);
78  return false;
79  }
80 
81  return true;
82 }
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 setQFout(), and SetupGOutput().

◆ psql_get_variable()

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

Definition at line 176 of file common.c.

178 {
179  char *result = NULL;
180  const char *value;
181 
182  /* In an inactive \if branch, suppress all variable substitutions */
183  if (passthrough && !conditional_active((ConditionalStack) passthrough))
184  return NULL;
185 
186  value = GetVariable(pset.vars, varname);
187  if (!value)
188  return NULL;
189 
190  switch (quote)
191  {
192  case PQUOTE_PLAIN:
193  result = pg_strdup(value);
194  break;
195  case PQUOTE_SQL_LITERAL:
196  case PQUOTE_SQL_IDENT:
197  {
198  /*
199  * For these cases, we use libpq's quoting functions, which
200  * assume the string is in the connection's client encoding.
201  */
202  char *escaped_value;
203 
204  if (!pset.db)
205  {
206  pg_log_error("cannot escape without active connection");
207  return NULL;
208  }
209 
210  if (quote == PQUOTE_SQL_LITERAL)
211  escaped_value =
212  PQescapeLiteral(pset.db, value, strlen(value));
213  else
214  escaped_value =
215  PQescapeIdentifier(pset.db, value, strlen(value));
216 
217  if (escaped_value == NULL)
218  {
219  const char *error = PQerrorMessage(pset.db);
220 
221  pg_log_info("%s", error);
222  return NULL;
223  }
224 
225  /*
226  * Rather than complicate the lexer's API with a notion of
227  * which free() routine to use, just pay the price of an extra
228  * strdup().
229  */
230  result = pg_strdup(escaped_value);
231  PQfreemem(escaped_value);
232  break;
233  }
234  case PQUOTE_SHELL_ARG:
235  {
236  /*
237  * For this we use appendShellStringNoError, which is
238  * encoding-agnostic, which is fine since the shell probably
239  * is too. In any case, the only special character is "'",
240  * which is not known to appear in valid multibyte characters.
241  */
243 
246  {
247  pg_log_error("shell command argument contains a newline or carriage return: \"%s\"",
248  value);
249  free(buf.data);
250  return NULL;
251  }
252  result = buf.data;
253  break;
254  }
255 
256  /* No default: we want a compiler warning for missing cases */
257  }
258 
259  return result;
260 }
bool conditional_active(ConditionalStack cstack)
Definition: conditional.c:140
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7167
void PQfreemem(void *ptr)
Definition: fe-exec.c:4032
char * PQescapeIdentifier(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:4310
char * PQescapeLiteral(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:4304
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static struct @155 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 313 of file common.c.

314 {
316 }
static void psql_cancel_callback(void)
Definition: common.c:297
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 620 of file common.c.

621 {
622  PGresult *res;
623 
624  if (!pset.db)
625  {
626  pg_log_error("You are currently not connected to a database.");
627  return NULL;
628  }
629 
631  {
632  printf(_("/******** QUERY *********/\n"
633  "%s\n"
634  "/************************/\n\n"), query);
635  fflush(stdout);
636  if (pset.logfile)
637  {
639  _("/******** QUERY *********/\n"
640  "%s\n"
641  "/************************/\n\n"), query);
643  }
644 
646  return NULL;
647  }
648 
650 
651  res = PQexec(pset.db, query);
652 
653  ResetCancelConn();
654 
655  if (!AcceptResult(res, true))
656  {
658  res = NULL;
659  }
660 
661  return res;
662 }
static void ClearOrSaveResult(PGresult *result)
Definition: common.c:523
static bool AcceptResult(const PGresult *result, bool show_error)
Definition: common.c:403
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:2262
#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 675 of file common.c.

676 {
677  bool timing = pset.timing;
678  double elapsed_msec = 0;
679  int res;
680 
681  if (!pset.db)
682  {
683  pg_log_error("You are currently not connected to a database.");
684  return 0;
685  }
686 
688 
689  res = ExecQueryAndProcessResults(query, &elapsed_msec, NULL, true, min_rows, opt, printQueryFout);
690 
691  ResetCancelConn();
692 
693  /* Possible microtiming output */
694  if (timing)
695  PrintTiming(elapsed_msec);
696 
697  return res;
698 }
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:1453
static void PrintTiming(double elapsed_msec)
Definition: common.c:561

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 2265 of file common.c.

2266 {
2267  return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL;
2268 }
static int uri_prefix_length(const char *connstr)
Definition: common.c:2238
static char * connstr
Definition: pg_dumpall.c:88

References connstr, and uri_prefix_length().

Referenced by do_connect().

◆ SendQuery()

bool SendQuery ( const char *  query)

Definition at line 1082 of file common.c.

1083 {
1084  bool timing = pset.timing;
1085  PGTransactionStatusType transaction_status;
1086  double elapsed_msec = 0;
1087  bool OK = false;
1088  int i;
1089  bool on_error_rollback_savepoint = false;
1090  bool svpt_gone = false;
1091 
1092  if (!pset.db)
1093  {
1094  pg_log_error("You are currently not connected to a database.");
1095  goto sendquery_cleanup;
1096  }
1097 
1098  if (pset.singlestep)
1099  {
1100  char buf[3];
1101 
1102  fflush(stderr);
1103  printf(_("/**(Single step mode: verify command)******************************************/\n"
1104  "%s\n"
1105  "/**(press return to proceed or enter x and return to cancel)*******************/\n"),
1106  query);
1107  fflush(stdout);
1108  if (fgets(buf, sizeof(buf), stdin) != NULL)
1109  if (buf[0] == 'x')
1110  goto sendquery_cleanup;
1111  if (cancel_pressed)
1112  goto sendquery_cleanup;
1113  }
1114  else if (pset.echo == PSQL_ECHO_QUERIES)
1115  {
1116  puts(query);
1117  fflush(stdout);
1118  }
1119 
1120  if (pset.logfile)
1121  {
1123  _("/******** QUERY *********/\n"
1124  "%s\n"
1125  "/************************/\n\n"), query);
1126  fflush(pset.logfile);
1127  }
1128 
1130 
1131  transaction_status = PQtransactionStatus(pset.db);
1132 
1133  if (transaction_status == PQTRANS_IDLE &&
1134  !pset.autocommit &&
1135  !command_no_begin(query))
1136  {
1137  PGresult *result;
1138 
1139  result = PQexec(pset.db, "BEGIN");
1140  if (PQresultStatus(result) != PGRES_COMMAND_OK)
1141  {
1143  ClearOrSaveResult(result);
1144  goto sendquery_cleanup;
1145  }
1146  ClearOrSaveResult(result);
1147  transaction_status = PQtransactionStatus(pset.db);
1148  }
1149 
1150  if (transaction_status == PQTRANS_INTRANS &&
1154  {
1155  PGresult *result;
1156 
1157  result = PQexec(pset.db, "SAVEPOINT pg_psql_temporary_savepoint");
1158  if (PQresultStatus(result) != PGRES_COMMAND_OK)
1159  {
1161  ClearOrSaveResult(result);
1162  goto sendquery_cleanup;
1163  }
1164  ClearOrSaveResult(result);
1165  on_error_rollback_savepoint = true;
1166  }
1167 
1168  if (pset.gdesc_flag)
1169  {
1170  /* Describe query's result columns, without executing it */
1171  OK = DescribeQuery(query, &elapsed_msec);
1172  }
1173  else
1174  {
1175  /* Default fetch-and-print mode */
1176  OK = (ExecQueryAndProcessResults(query, &elapsed_msec, &svpt_gone, false, 0, NULL, NULL) > 0);
1177  }
1178 
1179  if (!OK && pset.echo == PSQL_ECHO_ERRORS)
1180  pg_log_info("STATEMENT: %s", query);
1181 
1182  /* If we made a temporary savepoint, possibly release/rollback */
1183  if (on_error_rollback_savepoint)
1184  {
1185  const char *svptcmd = NULL;
1186 
1187  transaction_status = PQtransactionStatus(pset.db);
1188 
1189  switch (transaction_status)
1190  {
1191  case PQTRANS_INERROR:
1192  /* We always rollback on an error */
1193  svptcmd = "ROLLBACK TO pg_psql_temporary_savepoint";
1194  break;
1195 
1196  case PQTRANS_IDLE:
1197  /* If they are no longer in a transaction, then do nothing */
1198  break;
1199 
1200  case PQTRANS_INTRANS:
1201 
1202  /*
1203  * Release our savepoint, but do nothing if they are messing
1204  * with savepoints themselves
1205  */
1206  if (!svpt_gone)
1207  svptcmd = "RELEASE pg_psql_temporary_savepoint";
1208  break;
1209 
1210  case PQTRANS_ACTIVE:
1211  case PQTRANS_UNKNOWN:
1212  default:
1213  OK = false;
1214  /* PQTRANS_UNKNOWN is expected given a broken connection. */
1215  if (transaction_status != PQTRANS_UNKNOWN || ConnectionUp())
1216  pg_log_error("unexpected transaction status (%d)",
1217  transaction_status);
1218  break;
1219  }
1220 
1221  if (svptcmd)
1222  {
1223  PGresult *svptres;
1224 
1225  svptres = PQexec(pset.db, svptcmd);
1226  if (PQresultStatus(svptres) != PGRES_COMMAND_OK)
1227  {
1229  ClearOrSaveResult(svptres);
1230  OK = false;
1231 
1232  goto sendquery_cleanup;
1233  }
1234  PQclear(svptres);
1235  }
1236  }
1237 
1238  /* Possible microtiming output */
1239  if (timing)
1240  PrintTiming(elapsed_msec);
1241 
1242  /* check for events that may occur during query execution */
1243 
1244  if (pset.encoding != PQclientEncoding(pset.db) &&
1245  PQclientEncoding(pset.db) >= 0)
1246  {
1247  /* track effects of SET CLIENT_ENCODING */
1250  SetVariable(pset.vars, "ENCODING",
1252  }
1253 
1255 
1256  /* perform cleanup that should occur after any attempted query */
1257 
1258 sendquery_cleanup:
1259 
1260  /* global cancellation reset */
1261  ResetCancelConn();
1262 
1263  /* reset \g's output-to-filename trigger */
1264  if (pset.gfname)
1265  {
1266  free(pset.gfname);
1267  pset.gfname = NULL;
1268  }
1269 
1270  /* restore print settings if \g changed them */
1271  if (pset.gsavepopt)
1272  {
1274  pset.gsavepopt = NULL;
1275  }
1276 
1277  /* clean up after \bind */
1278  if (pset.bind_flag)
1279  {
1280  for (i = 0; i < pset.bind_nparams; i++)
1281  free(pset.bind_params[i]);
1283  pset.bind_params = NULL;
1284  pset.bind_flag = false;
1285  }
1286 
1287  /* reset \gset trigger */
1288  if (pset.gset_prefix)
1289  {
1291  pset.gset_prefix = NULL;
1292  }
1293 
1294  /* reset \gdesc trigger */
1295  pset.gdesc_flag = false;
1296 
1297  /* reset \gexec trigger */
1298  pset.gexec_flag = false;
1299 
1300  /* reset \crosstabview trigger */
1301  pset.crosstab_flag = false;
1302  for (i = 0; i < lengthof(pset.ctv_args); i++)
1303  {
1304  pg_free(pset.ctv_args[i]);
1305  pset.ctv_args[i] = NULL;
1306  }
1307 
1308  return OK;
1309 }
static bool DescribeQuery(const char *query, double *elapsed_msec)
Definition: common.c:1321
static void PrintNotifications(void)
Definition: common.c:705
static bool ConnectionUp(void)
Definition: common.c:324
static bool command_no_begin(const char *query)
Definition: common.c:1904
#define lengthof(array)
Definition: c.h:788
void restorePsetInfo(printQueryOpt *popt, printQueryOpt *save)
Definition: command.c:5029
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:7122
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:7255
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3411
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:121
@ PQTRANS_INTRANS
Definition: libpq-fe.h:124
@ PQTRANS_IDLE
Definition: libpq-fe.h:122
@ PQTRANS_ACTIVE
Definition: libpq-fe.h:123
@ PQTRANS_UNKNOWN
Definition: libpq-fe.h:126
@ PQTRANS_INERROR
Definition: libpq-fe.h:125
#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
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(), fflush(), fprintf, free, _psqlSettings::gdesc_flag, _psqlSettings::gexec_flag, _psqlSettings::gfname, _psqlSettings::gsavepopt, _psqlSettings::gset_prefix, i, 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 2159 of file common.c.

2160 {
2161  const char *val;
2162 
2163  if (!pset.db)
2164  return NULL;
2165 
2166  val = PQparameterStatus(pset.db, "session_authorization");
2167  if (val)
2168  return val;
2169  else
2170  return PQuser(pset.db);
2171 }
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:7021

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

Referenced by get_prompt().

◆ setQFout()

bool setQFout ( const char *  fname)

Definition at line 132 of file common.c.

133 {
134  FILE *fout;
135  bool is_pipe;
136 
137  /* First make sure we can open the new output file/pipe */
138  if (!openQueryOutputFile(fname, &fout, &is_pipe))
139  return false;
140 
141  /* Close old file/pipe */
142  if (pset.queryFout && pset.queryFout != stdout && pset.queryFout != stderr)
143  {
144  if (pset.queryFoutPipe)
146  else
147  fclose(pset.queryFout);
148  }
149 
150  pset.queryFout = fout;
151  pset.queryFoutPipe = is_pipe;
152 
153  /* Adjust SIGPIPE handling appropriately: ignore signal if is_pipe */
154  set_sigpipe_trap_state(is_pipe);
156 
157  return true;
158 }
void SetShellResultVariables(int wait_result)
Definition: common.c:501
bool openQueryOutputFile(const char *fname, FILE **fout, bool *is_pipe)
Definition: common.c:56
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 501 of file common.c.

502 {
503  char buf[32];
504 
505  SetVariable(pset.vars, "SHELL_ERROR",
506  (wait_result == 0) ? "false" : "true");
507  snprintf(buf, sizeof(buf), "%d", wait_result_to_exit_code(wait_result));
508  SetVariable(pset.vars, "SHELL_EXIT_CODE", buf);
509 }
#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 CloseGOutput(), do_copy(), do_shell(), exec_command_write(), and setQFout().

◆ standard_strings()

bool standard_strings ( void  )

Definition at line 2139 of file common.c.

2140 {
2141  const char *val;
2142 
2143  if (!pset.db)
2144  return false;
2145 
2146  val = PQparameterStatus(pset.db, "standard_conforming_strings");
2147 
2148  if (val && strcmp(val, "on") == 0)
2149  return true;
2150 
2151  return false;
2152 }

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