PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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)
 
void clean_extended_state (void)
 
bool recognized_connection_string (const char *connstr)
 

Variables

volatile sig_atomic_t sigint_interrupt_enabled
 
sigjmp_buf sigint_interrupt_jmp
 

Function Documentation

◆ clean_extended_state()

void clean_extended_state ( void  )

Definition at line 2279 of file common.c.

2280 {
2281  int i;
2282 
2283  switch (pset.send_mode)
2284  {
2285  case PSQL_SEND_EXTENDED_CLOSE: /* \close */
2286  free(pset.stmtName);
2287  break;
2288  case PSQL_SEND_EXTENDED_PARSE: /* \parse */
2289  free(pset.stmtName);
2290  break;
2291  case PSQL_SEND_EXTENDED_QUERY_PARAMS: /* \bind */
2292  case PSQL_SEND_EXTENDED_QUERY_PREPARED: /* \bind_named */
2293  for (i = 0; i < pset.bind_nparams; i++)
2294  free(pset.bind_params[i]);
2296  free(pset.stmtName);
2297  pset.bind_params = NULL;
2298  break;
2299  case PSQL_SEND_QUERY:
2300  break;
2301  }
2302 
2303  pset.stmtName = NULL;
2305 }
#define free(a)
Definition: header.h:65
int i
Definition: isn.c:72
PsqlSettings pset
Definition: startup.c:32
@ PSQL_SEND_QUERY
Definition: settings.h:67
@ PSQL_SEND_EXTENDED_QUERY_PARAMS
Definition: settings.h:70
@ PSQL_SEND_EXTENDED_PARSE
Definition: settings.h:69
@ PSQL_SEND_EXTENDED_CLOSE
Definition: settings.h:68
@ PSQL_SEND_EXTENDED_QUERY_PREPARED
Definition: settings.h:71
char * stmtName
Definition: settings.h:112
char ** bind_params
Definition: settings.h:111
PSQL_SEND_MODE send_mode
Definition: settings.h:108
int bind_nparams
Definition: settings.h:110

References _psqlSettings::bind_nparams, _psqlSettings::bind_params, free, i, pset, PSQL_SEND_EXTENDED_CLOSE, PSQL_SEND_EXTENDED_PARSE, PSQL_SEND_EXTENDED_QUERY_PARAMS, PSQL_SEND_EXTENDED_QUERY_PREPARED, PSQL_SEND_QUERY, _psqlSettings::send_mode, and _psqlSettings::stmtName.

Referenced by exec_command_bind(), exec_command_bind_named(), exec_command_close(), exec_command_parse(), and SendQuery().

◆ expand_tilde()

void expand_tilde ( char **  filename)

Definition at line 2195 of file common.c.

2196 {
2197  if (!filename || !(*filename))
2198  return;
2199 
2200  /*
2201  * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde
2202  * for short versions of long file names, though the tilde is usually
2203  * toward the end, not at the beginning.
2204  */
2205 #ifndef WIN32
2206 
2207  /* try tilde expansion */
2208  if (**filename == '~')
2209  {
2210  char *fn;
2211  char oldp,
2212  *p;
2213  struct passwd *pw;
2214  char home[MAXPGPATH];
2215 
2216  fn = *filename;
2217  *home = '\0';
2218 
2219  p = fn + 1;
2220  while (*p != '/' && *p != '\0')
2221  p++;
2222 
2223  oldp = *p;
2224  *p = '\0';
2225 
2226  if (*(fn + 1) == '\0')
2227  get_home_path(home); /* ~ or ~/ only */
2228  else if ((pw = getpwnam(fn + 1)) != NULL)
2229  strlcpy(home, pw->pw_dir, sizeof(home)); /* ~user */
2230 
2231  *p = oldp;
2232  if (strlen(home) != 0)
2233  {
2234  char *newfn;
2235 
2236  newfn = psprintf("%s%s", home, p);
2237  free(fn);
2238  *filename = newfn;
2239  }
2240  }
2241 #endif
2242 }
#define MAXPGPATH
static char * filename
Definition: pg_dumpall.c:119
bool get_home_path(char *ret_path)
Definition: path.c:928
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43
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 2134 of file common.c.

2135 {
2136  const char *val;
2137 
2138  if (!pset.db)
2139  return false;
2140 
2141  val = PQparameterStatus(pset.db, "is_superuser");
2142 
2143  if (val && strcmp(val, "on") == 0)
2144  return true;
2145 
2146  return false;
2147 }
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:7155
long val
Definition: informix.c:689
PGconn * db
Definition: settings.h:91

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:7200
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 @160 value
static char * buf
Definition: pg_test_fsync.c:72
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:133
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:131
PSQL_ECHO_HIDDEN echo_hidden
Definition: settings.h:158

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:1446
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 2317 of file common.c.

2318 {
2319  return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL;
2320 }
static int uri_prefix_length(const char *connstr)
Definition: common.c:2253
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 extended protocol queries */
1279 
1280  /* reset \gset trigger */
1281  if (pset.gset_prefix)
1282  {
1284  pset.gset_prefix = NULL;
1285  }
1286 
1287  /* reset \gdesc trigger */
1288  pset.gdesc_flag = false;
1289 
1290  /* reset \gexec trigger */
1291  pset.gexec_flag = false;
1292 
1293  /* reset \crosstabview trigger */
1294  pset.crosstab_flag = false;
1295  for (i = 0; i < lengthof(pset.ctv_args); i++)
1296  {
1297  pg_free(pset.ctv_args[i]);
1298  pset.ctv_args[i] = NULL;
1299  }
1300 
1301  return OK;
1302 }
static bool DescribeQuery(const char *query, double *elapsed_msec)
Definition: common.c:1314
static void PrintNotifications(void)
Definition: common.c:705
void clean_extended_state(void)
Definition: common.c:2279
static bool ConnectionUp(void)
Definition: common.c:324
static bool command_no_begin(const char *query)
Definition: common.c:1919
#define lengthof(array)
Definition: c.h:793
void restorePsetInfo(printQueryOpt *popt, printQueryOpt *save)
Definition: command.c:5154
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:7145
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:7288
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
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:120
PGTransactionStatusType
Definition: libpq-fe.h:141
@ PQTRANS_INTRANS
Definition: libpq-fe.h:144
@ PQTRANS_IDLE
Definition: libpq-fe.h:142
@ PQTRANS_ACTIVE
Definition: libpq-fe.h:143
@ PQTRANS_UNKNOWN
Definition: libpq-fe.h:146
@ PQTRANS_INERROR
Definition: libpq-fe.h:145
#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:100
char * gset_prefix
Definition: settings.h:105
PSQL_ERROR_ROLLBACK on_error_rollback
Definition: settings.h:159
int encoding
Definition: settings.h:92
bool autocommit
Definition: settings.h:147
char * ctv_args[4]
Definition: settings.h:115
PSQL_ECHO echo
Definition: settings.h:157
bool singlestep
Definition: settings.h:151
printQueryOpt * gsavepopt
Definition: settings.h:103
char * gfname
Definition: settings.h:102
bool cur_cmd_interactive
Definition: settings.h:122
bool gexec_flag
Definition: settings.h:107
bool crosstab_flag
Definition: settings.h:114
bool gdesc_flag
Definition: settings.h:106
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, buf, cancel_pressed, clean_extended_state(), 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 2174 of file common.c.

2175 {
2176  const char *val;
2177 
2178  if (!pset.db)
2179  return NULL;
2180 
2181  val = PQparameterStatus(pset.db, "session_authorization");
2182  if (val)
2183  return val;
2184  else
2185  return PQuser(pset.db);
2186 }
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:7044

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:93
bool queryFoutPipe
Definition: settings.h:94

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

2155 {
2156  const char *val;
2157 
2158  if (!pset.db)
2159  return false;
2160 
2161  val = PQparameterStatus(pset.db, "standard_conforming_strings");
2162 
2163  if (val && strcmp(val, "on") == 0)
2164  return true;
2165 
2166  return false;
2167 }

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