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

2340 {
2341  if (!filename || !(*filename))
2342  return;
2343 
2344  /*
2345  * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde
2346  * for short versions of long file names, though the tilde is usually
2347  * toward the end, not at the beginning.
2348  */
2349 #ifndef WIN32
2350 
2351  /* try tilde expansion */
2352  if (**filename == '~')
2353  {
2354  char *fn;
2355  char oldp,
2356  *p;
2357  struct passwd *pw;
2358  char home[MAXPGPATH];
2359 
2360  fn = *filename;
2361  *home = '\0';
2362 
2363  p = fn + 1;
2364  while (*p != '/' && *p != '\0')
2365  p++;
2366 
2367  oldp = *p;
2368  *p = '\0';
2369 
2370  if (*(fn + 1) == '\0')
2371  get_home_path(home); /* ~ or ~/ only */
2372  else if ((pw = getpwnam(fn + 1)) != NULL)
2373  strlcpy(home, pw->pw_dir, sizeof(home)); /* ~user */
2374 
2375  *p = oldp;
2376  if (strlen(home) != 0)
2377  {
2378  char *newfn;
2379 
2380  newfn = psprintf("%s%s", home, p);
2381  free(fn);
2382  *filename = newfn;
2383  }
2384  }
2385 #endif
2386 }
#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:928
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)

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

2279 {
2280  const char *val;
2281 
2282  if (!pset.db)
2283  return false;
2284 
2285  val = PQparameterStatus(pset.db, "is_superuser");
2286 
2287  if (val && strcmp(val, "on") == 0)
2288  return true;
2289 
2290  return false;
2291 }
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:7210
long val
Definition: informix.c:664
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 228 of file common.c.

229 {
230  (void) arg; /* not used */
231  pg_log_info("%s", message);
232 }
#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 57 of file common.c.

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

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

275 {
277 }
static void setup_cancel_handler(void)
Definition: parallel.c:608
static void psql_cancel_callback(void)
Definition: common.c:258

References psql_cancel_callback(), and setup_cancel_handler().

Referenced by main().

◆ PSQLexec()

PGresult* PSQLexec ( const char *  query)

Definition at line 580 of file common.c.

581 {
582  PGresult *res;
583 
584  if (!pset.db)
585  {
586  pg_log_error("You are currently not connected to a database.");
587  return NULL;
588  }
589 
591  {
592  printf(_("********* QUERY **********\n"
593  "%s\n"
594  "**************************\n\n"), query);
595  fflush(stdout);
596  if (pset.logfile)
597  {
599  _("********* QUERY **********\n"
600  "%s\n"
601  "**************************\n\n"), query);
603  }
604 
606  return NULL;
607  }
608 
610 
611  res = PQexec(pset.db, query);
612 
613  ResetCancelConn();
614 
615  if (!AcceptResult(res, true))
616  {
618  res = NULL;
619  }
620 
621  return res;
622 }
static void ClearOrSaveResult(PGresult *result)
Definition: common.c:483
static bool AcceptResult(const PGresult *result, bool show_error)
Definition: common.c:364
void ResetCancelConn(void)
Definition: cancel.c:107
void SetCancelConn(PGconn *conn)
Definition: cancel.c:77
#define _(x)
Definition: elog.c:91
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2228
#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(), 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 
)

Definition at line 635 of file common.c.

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

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

2425 {
2426  return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL;
2427 }
static int uri_prefix_length(const char *connstr)
Definition: common.c:2397
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 1042 of file common.c.

1043 {
1044  bool timing = pset.timing;
1045  PGTransactionStatusType transaction_status;
1046  double elapsed_msec = 0;
1047  bool OK = false;
1048  int i;
1049  bool on_error_rollback_savepoint = false;
1050  bool svpt_gone = false;
1051 
1052  if (!pset.db)
1053  {
1054  pg_log_error("You are currently not connected to a database.");
1055  goto sendquery_cleanup;
1056  }
1057 
1058  if (pset.singlestep)
1059  {
1060  char buf[3];
1061 
1062  fflush(stderr);
1063  printf(_("***(Single step mode: verify command)*******************************************\n"
1064  "%s\n"
1065  "***(press return to proceed or enter x and return to cancel)********************\n"),
1066  query);
1067  fflush(stdout);
1068  if (fgets(buf, sizeof(buf), stdin) != NULL)
1069  if (buf[0] == 'x')
1070  goto sendquery_cleanup;
1071  if (cancel_pressed)
1072  goto sendquery_cleanup;
1073  }
1074  else if (pset.echo == PSQL_ECHO_QUERIES)
1075  {
1076  puts(query);
1077  fflush(stdout);
1078  }
1079 
1080  if (pset.logfile)
1081  {
1083  _("********* QUERY **********\n"
1084  "%s\n"
1085  "**************************\n\n"), query);
1086  fflush(pset.logfile);
1087  }
1088 
1090 
1091  transaction_status = PQtransactionStatus(pset.db);
1092 
1093  if (transaction_status == PQTRANS_IDLE &&
1094  !pset.autocommit &&
1095  !command_no_begin(query))
1096  {
1097  PGresult *result;
1098 
1099  result = PQexec(pset.db, "BEGIN");
1100  if (PQresultStatus(result) != PGRES_COMMAND_OK)
1101  {
1103  ClearOrSaveResult(result);
1104  goto sendquery_cleanup;
1105  }
1106  ClearOrSaveResult(result);
1107  transaction_status = PQtransactionStatus(pset.db);
1108  }
1109 
1110  if (transaction_status == PQTRANS_INTRANS &&
1114  {
1115  PGresult *result;
1116 
1117  result = PQexec(pset.db, "SAVEPOINT pg_psql_temporary_savepoint");
1118  if (PQresultStatus(result) != PGRES_COMMAND_OK)
1119  {
1121  ClearOrSaveResult(result);
1122  goto sendquery_cleanup;
1123  }
1124  ClearOrSaveResult(result);
1125  on_error_rollback_savepoint = true;
1126  }
1127 
1128  if (pset.gdesc_flag)
1129  {
1130  /* Describe query's result columns, without executing it */
1131  OK = DescribeQuery(query, &elapsed_msec);
1132  }
1133  else if (pset.fetch_count <= 0 || pset.gexec_flag ||
1135  {
1136  /* Default fetch-it-all-and-print mode */
1137  OK = (ExecQueryAndProcessResults(query, &elapsed_msec, &svpt_gone, false, NULL, NULL) > 0);
1138  }
1139  else
1140  {
1141  /* Fetch-in-segments mode */
1142  OK = ExecQueryUsingCursor(query, &elapsed_msec);
1143  }
1144 
1145  if (!OK && pset.echo == PSQL_ECHO_ERRORS)
1146  pg_log_info("STATEMENT: %s", query);
1147 
1148  /* If we made a temporary savepoint, possibly release/rollback */
1149  if (on_error_rollback_savepoint)
1150  {
1151  const char *svptcmd = NULL;
1152 
1153  transaction_status = PQtransactionStatus(pset.db);
1154 
1155  switch (transaction_status)
1156  {
1157  case PQTRANS_INERROR:
1158  /* We always rollback on an error */
1159  svptcmd = "ROLLBACK TO pg_psql_temporary_savepoint";
1160  break;
1161 
1162  case PQTRANS_IDLE:
1163  /* If they are no longer in a transaction, then do nothing */
1164  break;
1165 
1166  case PQTRANS_INTRANS:
1167 
1168  /*
1169  * Release our savepoint, but do nothing if they are messing
1170  * with savepoints themselves
1171  */
1172  if (!svpt_gone)
1173  svptcmd = "RELEASE pg_psql_temporary_savepoint";
1174  break;
1175 
1176  case PQTRANS_ACTIVE:
1177  case PQTRANS_UNKNOWN:
1178  default:
1179  OK = false;
1180  /* PQTRANS_UNKNOWN is expected given a broken connection. */
1181  if (transaction_status != PQTRANS_UNKNOWN || ConnectionUp())
1182  pg_log_error("unexpected transaction status (%d)",
1183  transaction_status);
1184  break;
1185  }
1186 
1187  if (svptcmd)
1188  {
1189  PGresult *svptres;
1190 
1191  svptres = PQexec(pset.db, svptcmd);
1192  if (PQresultStatus(svptres) != PGRES_COMMAND_OK)
1193  {
1195  ClearOrSaveResult(svptres);
1196  OK = false;
1197 
1198  goto sendquery_cleanup;
1199  }
1200  PQclear(svptres);
1201  }
1202  }
1203 
1204  /* Possible microtiming output */
1205  if (timing)
1206  PrintTiming(elapsed_msec);
1207 
1208  /* check for events that may occur during query execution */
1209 
1210  if (pset.encoding != PQclientEncoding(pset.db) &&
1211  PQclientEncoding(pset.db) >= 0)
1212  {
1213  /* track effects of SET CLIENT_ENCODING */
1216  SetVariable(pset.vars, "ENCODING",
1218  }
1219 
1221 
1222  /* perform cleanup that should occur after any attempted query */
1223 
1224 sendquery_cleanup:
1225 
1226  /* global cancellation reset */
1227  ResetCancelConn();
1228 
1229  /* reset \g's output-to-filename trigger */
1230  if (pset.gfname)
1231  {
1232  free(pset.gfname);
1233  pset.gfname = NULL;
1234  }
1235 
1236  /* restore print settings if \g changed them */
1237  if (pset.gsavepopt)
1238  {
1240  pset.gsavepopt = NULL;
1241  }
1242 
1243  /* clean up after \bind */
1244  if (pset.bind_flag)
1245  {
1246  for (i = 0; i < pset.bind_nparams; i++)
1247  free(pset.bind_params[i]);
1249  pset.bind_params = NULL;
1250  pset.bind_flag = false;
1251  }
1252 
1253  /* reset \gset trigger */
1254  if (pset.gset_prefix)
1255  {
1257  pset.gset_prefix = NULL;
1258  }
1259 
1260  /* reset \gdesc trigger */
1261  pset.gdesc_flag = false;
1262 
1263  /* reset \gexec trigger */
1264  pset.gexec_flag = false;
1265 
1266  /* reset \crosstabview trigger */
1267  pset.crosstab_flag = false;
1268  for (i = 0; i < lengthof(pset.ctv_args); i++)
1269  {
1270  pg_free(pset.ctv_args[i]);
1271  pset.ctv_args[i] = NULL;
1272  }
1273 
1274  return OK;
1275 }
static bool DescribeQuery(const char *query, double *elapsed_msec)
Definition: common.c:1287
static bool ExecQueryUsingCursor(const char *query, double *elapsed_msec)
Definition: common.c:1701
static void PrintNotifications(void)
Definition: common.c:665
static bool is_select_command(const char *query)
Definition: common.c:2241
static bool ConnectionUp(void)
Definition: common.c:285
static bool command_no_begin(const char *query)
Definition: common.c:2026
#define lengthof(array)
Definition: c.h:772
void restorePsetInfo(printQueryOpt *popt, printQueryOpt *save)
Definition: command.c:4938
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:588
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:7200
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:7333
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3244
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:97
PGTransactionStatusType
Definition: libpq-fe.h:117
@ PQTRANS_INTRANS
Definition: libpq-fe.h:120
@ PQTRANS_IDLE
Definition: libpq-fe.h:118
@ PQTRANS_ACTIVE
Definition: libpq-fe.h:119
@ PQTRANS_UNKNOWN
Definition: libpq-fe.h:122
@ PQTRANS_INERROR
Definition: libpq-fe.h:121
@ 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 2318 of file common.c.

2319 {
2320  const char *val;
2321 
2322  if (!pset.db)
2323  return NULL;
2324 
2325  val = PQparameterStatus(pset.db, "session_authorization");
2326  if (val)
2327  return val;
2328  else
2329  return PQuser(pset.db);
2330 }
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:7099

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

Referenced by get_prompt().

◆ setQFout()

bool setQFout ( const char *  fname)

Definition at line 93 of file common.c.

94 {
95  FILE *fout;
96  bool is_pipe;
97 
98  /* First make sure we can open the new output file/pipe */
99  if (!openQueryOutputFile(fname, &fout, &is_pipe))
100  return false;
101 
102  /* Close old file/pipe */
103  if (pset.queryFout && pset.queryFout != stdout && pset.queryFout != stderr)
104  {
105  if (pset.queryFoutPipe)
107  else
108  fclose(pset.queryFout);
109  }
110 
111  pset.queryFout = fout;
112  pset.queryFoutPipe = is_pipe;
113 
114  /* Adjust SIGPIPE handling appropriately: ignore signal if is_pipe */
115  set_sigpipe_trap_state(is_pipe);
117 
118  return true;
119 }
void SetShellResultVariables(int wait_result)
Definition: common.c:461
bool openQueryOutputFile(const char *fname, FILE **fout, bool *is_pipe)
Definition: common.c:57
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 461 of file common.c.

462 {
463  char buf[32];
464 
465  SetVariable(pset.vars, "SHELL_ERROR",
466  (wait_result == 0) ? "false" : "true");
467  snprintf(buf, sizeof(buf), "%d", wait_result_to_exit_code(wait_result));
468  SetVariable(pset.vars, "SHELL_EXIT_CODE", buf);
469 }
#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 2298 of file common.c.

2299 {
2300  const char *val;
2301 
2302  if (!pset.db)
2303  return false;
2304 
2305  val = PQparameterStatus(pset.db, "standard_conforming_strings");
2306 
2307  if (val && strcmp(val, "on") == 0)
2308  return true;
2309 
2310  return false;
2311 }

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