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

2582{
2583 int i;
2584
2585 switch (pset.send_mode)
2586 {
2587 case PSQL_SEND_EXTENDED_CLOSE: /* \close */
2589 break;
2590 case PSQL_SEND_EXTENDED_PARSE: /* \parse */
2592 break;
2593 case PSQL_SEND_EXTENDED_QUERY_PARAMS: /* \bind */
2594 case PSQL_SEND_EXTENDED_QUERY_PREPARED: /* \bind_named */
2595 for (i = 0; i < pset.bind_nparams; i++)
2599 pset.bind_params = NULL;
2600 break;
2601 case PSQL_SEND_QUERY:
2602 case PSQL_SEND_START_PIPELINE_MODE: /* \startpipeline */
2603 case PSQL_SEND_END_PIPELINE_MODE: /* \endpipeline */
2604 case PSQL_SEND_PIPELINE_SYNC: /* \syncpipeline */
2605 case PSQL_SEND_FLUSH: /* \flush */
2606 case PSQL_SEND_GET_RESULTS: /* \getresults */
2607 case PSQL_SEND_FLUSH_REQUEST: /* \flushrequest */
2608 break;
2609 }
2610
2611 pset.stmtName = NULL;
2613}
#define free(a)
Definition: header.h:65
int i
Definition: isn.c:74
PsqlSettings pset
Definition: startup.c:32
@ PSQL_SEND_PIPELINE_SYNC
Definition: settings.h:78
@ PSQL_SEND_FLUSH
Definition: settings.h:81
@ PSQL_SEND_FLUSH_REQUEST
Definition: settings.h:82
@ PSQL_SEND_START_PIPELINE_MODE
Definition: settings.h:79
@ PSQL_SEND_QUERY
Definition: settings.h:73
@ PSQL_SEND_EXTENDED_QUERY_PARAMS
Definition: settings.h:76
@ PSQL_SEND_EXTENDED_PARSE
Definition: settings.h:75
@ PSQL_SEND_END_PIPELINE_MODE
Definition: settings.h:80
@ PSQL_SEND_GET_RESULTS
Definition: settings.h:83
@ PSQL_SEND_EXTENDED_CLOSE
Definition: settings.h:74
@ PSQL_SEND_EXTENDED_QUERY_PREPARED
Definition: settings.h:77
char * stmtName
Definition: settings.h:124
char ** bind_params
Definition: settings.h:123
PSQL_SEND_MODE send_mode
Definition: settings.h:120
int bind_nparams
Definition: settings.h:122

References _psqlSettings::bind_nparams, _psqlSettings::bind_params, free, i, pset, PSQL_SEND_END_PIPELINE_MODE, PSQL_SEND_EXTENDED_CLOSE, PSQL_SEND_EXTENDED_PARSE, PSQL_SEND_EXTENDED_QUERY_PARAMS, PSQL_SEND_EXTENDED_QUERY_PREPARED, PSQL_SEND_FLUSH, PSQL_SEND_FLUSH_REQUEST, PSQL_SEND_GET_RESULTS, PSQL_SEND_PIPELINE_SYNC, PSQL_SEND_QUERY, PSQL_SEND_START_PIPELINE_MODE, _psqlSettings::send_mode, and _psqlSettings::stmtName.

Referenced by exec_command_bind(), exec_command_bind_named(), exec_command_close(), exec_command_g(), exec_command_gexec(), exec_command_gset(), exec_command_parse(), exec_command_sendpipeline(), exec_command_watch(), and SendQuery().

◆ expand_tilde()

void expand_tilde ( char **  filename)

Definition at line 2497 of file common.c.

2498{
2499 if (!filename || !(*filename))
2500 return;
2501
2502 /*
2503 * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde
2504 * for short versions of long file names, though the tilde is usually
2505 * toward the end, not at the beginning.
2506 */
2507#ifndef WIN32
2508
2509 /* try tilde expansion */
2510 if (**filename == '~')
2511 {
2512 char *fn;
2513 char oldp,
2514 *p;
2515 struct passwd *pw;
2516 char home[MAXPGPATH];
2517
2518 fn = *filename;
2519 *home = '\0';
2520
2521 p = fn + 1;
2522 while (*p != '/' && *p != '\0')
2523 p++;
2524
2525 oldp = *p;
2526 *p = '\0';
2527
2528 if (*(fn + 1) == '\0')
2529 get_home_path(home); /* ~ or ~/ only */
2530 else if ((pw = getpwnam(fn + 1)) != NULL)
2531 strlcpy(home, pw->pw_dir, sizeof(home)); /* ~user */
2532
2533 *p = oldp;
2534 if (strlen(home) != 0)
2535 {
2536 char *newfn;
2537
2538 newfn = psprintf("%s%s", home, p);
2539 free(fn);
2540 *filename = newfn;
2541 }
2542 }
2543#endif
2544}
#define MAXPGPATH
static char * filename
Definition: pg_dumpall.c:127
bool get_home_path(char *ret_path)
Definition: path.c:1005
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 2436 of file common.c.

2437{
2438 const char *val;
2439
2440 if (!pset.db)
2441 return false;
2442
2443 val = PQparameterStatus(pset.db, "is_superuser");
2444
2445 if (val && strcmp(val, "on") == 0)
2446 return true;
2447
2448 return false;
2449}
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:7508
long val
Definition: informix.c:689
PGconn * db
Definition: settings.h:103

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

◆ NoticeProcessor()

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

Definition at line 279 of file common.c.

280{
281 (void) arg; /* not used */
282 pg_log_info("%s", message);
283}
#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}
#define pg_log_error(...)
Definition: logging.h:106

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

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

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

326{
328}
static void psql_cancel_callback(void)
Definition: common.c:309
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 655 of file common.c.

656{
657 PGresult *res;
658
659 if (!pset.db)
660 {
661 pg_log_error("You are currently not connected to a database.");
662 return NULL;
663 }
664
666 {
667 printf(_("/******** QUERY *********/\n"
668 "%s\n"
669 "/************************/\n\n"), query);
670 fflush(stdout);
671 if (pset.logfile)
672 {
674 _("/******** QUERY *********/\n"
675 "%s\n"
676 "/************************/\n\n"), query);
677 fflush(pset.logfile);
678 }
679
681 return NULL;
682 }
683
685
686 res = PQexec(pset.db, query);
687
689
690 if (!AcceptResult(res, true))
691 {
693 res = NULL;
694 }
695
696 return res;
697}
static void ClearOrSaveResult(PGresult *result)
Definition: common.c:558
static bool AcceptResult(const PGresult *result, bool show_error)
Definition: common.c:416
void ResetCancelConn(void)
Definition: cancel.c:107
void SetCancelConn(PGconn *conn)
Definition: cancel.c:77
#define fprintf(file, fmt, msg)
Definition: cubescan.l:21
#define _(x)
Definition: elog.c:90
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2262
#define printf(...)
Definition: port.h:245
@ PSQL_ECHO_HIDDEN_NOEXEC
Definition: settings.h:53
@ PSQL_ECHO_HIDDEN_OFF
Definition: settings.h:51
FILE * logfile
Definition: settings.h:149
PSQL_ECHO_HIDDEN echo_hidden
Definition: settings.h:177

References _, AcceptResult(), ClearOrSaveResult(), _psqlSettings::db, _psqlSettings::echo_hidden, fprintf, _psqlSettings::logfile, pg_log_error, PQexec(), printf, pset, PSQL_ECHO_HIDDEN_NOEXEC, PSQL_ECHO_HIDDEN_OFF, 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 710 of file common.c.

711{
712 bool timing = pset.timing;
713 double elapsed_msec = 0;
714 int res;
715
716 if (!pset.db)
717 {
718 pg_log_error("You are currently not connected to a database.");
719 return 0;
720 }
721
723
724 res = ExecQueryAndProcessResults(query, &elapsed_msec, NULL, true, min_rows, opt, printQueryFout);
725
727
728 /* Possible microtiming output */
729 if (timing)
730 PrintTiming(elapsed_msec);
731
732 return res;
733}
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:1539
static void PrintTiming(double elapsed_msec)
Definition: common.c:596

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

Referenced by do_watch().

◆ recognized_connection_string()

bool recognized_connection_string ( const char *  connstr)

Definition at line 2625 of file common.c.

2626{
2627 return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL;
2628}
static int uri_prefix_length(const char *connstr)
Definition: common.c:2555
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 1118 of file common.c.

1119{
1120 bool timing = pset.timing;
1121 PGTransactionStatusType transaction_status;
1122 double elapsed_msec = 0;
1123 bool OK = false;
1124 int i;
1125 bool on_error_rollback_savepoint = false;
1126 bool svpt_gone = false;
1127
1128 if (!pset.db)
1129 {
1130 pg_log_error("You are currently not connected to a database.");
1131 goto sendquery_cleanup;
1132 }
1133
1134 if (pset.singlestep)
1135 {
1136 char buf[3];
1137
1138 fflush(stderr);
1139 printf(_("/**(Single step mode: verify command)******************************************/\n"
1140 "%s\n"
1141 "/**(press return to proceed or enter x and return to cancel)*******************/\n"),
1142 query);
1143 fflush(stdout);
1144 if (fgets(buf, sizeof(buf), stdin) != NULL)
1145 if (buf[0] == 'x')
1146 goto sendquery_cleanup;
1147 if (cancel_pressed)
1148 goto sendquery_cleanup;
1149 }
1150 else if (pset.echo == PSQL_ECHO_QUERIES)
1151 {
1152 puts(query);
1153 fflush(stdout);
1154 }
1155
1156 if (pset.logfile)
1157 {
1159 _("/******** QUERY *********/\n"
1160 "%s\n"
1161 "/************************/\n\n"), query);
1162 fflush(pset.logfile);
1163 }
1164
1166
1167 transaction_status = PQtransactionStatus(pset.db);
1168
1169 if (transaction_status == PQTRANS_IDLE &&
1170 !pset.autocommit &&
1171 !command_no_begin(query))
1172 {
1173 PGresult *result;
1174
1175 result = PQexec(pset.db, "BEGIN");
1176 if (PQresultStatus(result) != PGRES_COMMAND_OK)
1177 {
1179 ClearOrSaveResult(result);
1180 goto sendquery_cleanup;
1181 }
1182 ClearOrSaveResult(result);
1183 transaction_status = PQtransactionStatus(pset.db);
1184 }
1185
1186 if (transaction_status == PQTRANS_INTRANS &&
1190 {
1191 PGresult *result;
1192
1193 result = PQexec(pset.db, "SAVEPOINT pg_psql_temporary_savepoint");
1194 if (PQresultStatus(result) != PGRES_COMMAND_OK)
1195 {
1197 ClearOrSaveResult(result);
1198 goto sendquery_cleanup;
1199 }
1200 ClearOrSaveResult(result);
1201 on_error_rollback_savepoint = true;
1202 }
1203
1204 if (pset.gdesc_flag)
1205 {
1206 /* Describe query's result columns, without executing it */
1207 OK = DescribeQuery(query, &elapsed_msec);
1208 }
1209 else
1210 {
1211 /* Default fetch-and-print mode */
1212 OK = (ExecQueryAndProcessResults(query, &elapsed_msec, &svpt_gone, false, 0, NULL, NULL) > 0);
1213 }
1214
1215 if (!OK && pset.echo == PSQL_ECHO_ERRORS)
1216 pg_log_info("STATEMENT: %s", query);
1217
1218 /* If we made a temporary savepoint, possibly release/rollback */
1219 if (on_error_rollback_savepoint)
1220 {
1221 const char *svptcmd = NULL;
1222
1223 transaction_status = PQtransactionStatus(pset.db);
1224
1225 switch (transaction_status)
1226 {
1227 case PQTRANS_INERROR:
1228 /* We always rollback on an error */
1229 svptcmd = "ROLLBACK TO pg_psql_temporary_savepoint";
1230 break;
1231
1232 case PQTRANS_IDLE:
1233 /* If they are no longer in a transaction, then do nothing */
1234 break;
1235
1236 case PQTRANS_INTRANS:
1237
1238 /*
1239 * Release our savepoint, but do nothing if they are messing
1240 * with savepoints themselves
1241 */
1242 if (!svpt_gone)
1243 svptcmd = "RELEASE pg_psql_temporary_savepoint";
1244 break;
1245
1246 case PQTRANS_ACTIVE:
1247 case PQTRANS_UNKNOWN:
1248 default:
1249 OK = false;
1250 /* PQTRANS_UNKNOWN is expected given a broken connection. */
1251 if (transaction_status != PQTRANS_UNKNOWN || ConnectionUp())
1252 pg_log_error("unexpected transaction status (%d)",
1253 transaction_status);
1254 break;
1255 }
1256
1257 if (svptcmd)
1258 {
1259 PGresult *svptres;
1260
1261 svptres = PQexec(pset.db, svptcmd);
1262 if (PQresultStatus(svptres) != PGRES_COMMAND_OK)
1263 {
1265 ClearOrSaveResult(svptres);
1266 OK = false;
1267
1268 goto sendquery_cleanup;
1269 }
1270 PQclear(svptres);
1271 }
1272 }
1273
1274 /* Possible microtiming output */
1275 if (timing)
1276 PrintTiming(elapsed_msec);
1277
1278 /* check for events that may occur during query execution */
1279
1281 PQclientEncoding(pset.db) >= 0)
1282 {
1283 /* track effects of SET CLIENT_ENCODING */
1286 SetVariable(pset.vars, "ENCODING",
1288 }
1289
1291
1292 /* perform cleanup that should occur after any attempted query */
1293
1294sendquery_cleanup:
1295
1296 /* global cancellation reset */
1298
1299 /* reset \g's output-to-filename trigger */
1300 if (pset.gfname)
1301 {
1302 free(pset.gfname);
1303 pset.gfname = NULL;
1304 }
1305
1306 /* restore print settings if \g changed them */
1307 if (pset.gsavepopt)
1308 {
1310 pset.gsavepopt = NULL;
1311 }
1312
1313 /* clean up after extended protocol queries */
1315
1316 /* reset \gset trigger */
1317 if (pset.gset_prefix)
1318 {
1320 pset.gset_prefix = NULL;
1321 }
1322
1323 /* reset \gdesc trigger */
1324 pset.gdesc_flag = false;
1325
1326 /* reset \gexec trigger */
1327 pset.gexec_flag = false;
1328
1329 /* reset \crosstabview trigger */
1330 pset.crosstab_flag = false;
1331 for (i = 0; i < lengthof(pset.ctv_args); i++)
1332 {
1334 pset.ctv_args[i] = NULL;
1335 }
1336
1337 return OK;
1338}
static bool DescribeQuery(const char *query, double *elapsed_msec)
Definition: common.c:1350
static void PrintNotifications(void)
Definition: common.c:740
void clean_extended_state(void)
Definition: common.c:2581
static bool ConnectionUp(void)
Definition: common.c:336
static bool command_no_begin(const char *query)
Definition: common.c:2221
#define lengthof(array)
Definition: c.h:759
void restorePsetInfo(printQueryOpt *popt, printQueryOpt *save)
Definition: command.c:5560
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:7498
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:7643
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3411
void PQclear(PGresult *res)
Definition: fe-exec.c:721
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:125
PGTransactionStatusType
Definition: libpq-fe.h:146
@ PQTRANS_INTRANS
Definition: libpq-fe.h:149
@ PQTRANS_IDLE
Definition: libpq-fe.h:147
@ PQTRANS_ACTIVE
Definition: libpq-fe.h:148
@ PQTRANS_UNKNOWN
Definition: libpq-fe.h:151
@ PQTRANS_INERROR
Definition: libpq-fe.h:150
#define pg_encoding_to_char
Definition: pg_wchar.h:630
@ PSQL_ERROR_ROLLBACK_ON
Definition: settings.h:60
@ PSQL_ERROR_ROLLBACK_OFF
Definition: settings.h:58
@ PSQL_ECHO_ERRORS
Definition: settings.h:45
@ PSQL_ECHO_QUERIES
Definition: settings.h:44
printQueryOpt popt
Definition: settings.h:112
char * gset_prefix
Definition: settings.h:117
PSQL_ERROR_ROLLBACK on_error_rollback
Definition: settings.h:178
bool autocommit
Definition: settings.h:165
char * ctv_args[4]
Definition: settings.h:133
PSQL_ECHO echo
Definition: settings.h:176
bool singlestep
Definition: settings.h:169
printQueryOpt * gsavepopt
Definition: settings.h:115
char * gfname
Definition: settings.h:114
bool cur_cmd_interactive
Definition: settings.h:140
bool gexec_flag
Definition: settings.h:119
bool crosstab_flag
Definition: settings.h:132
bool gdesc_flag
Definition: settings.h:118
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:281

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(), 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 2476 of file common.c.

2477{
2478 const char *val;
2479
2480 if (!pset.db)
2481 return NULL;
2482
2483 val = PQparameterStatus(pset.db, "session_authorization");
2484 if (val)
2485 return val;
2486 else
2487 return PQuser(pset.db);
2488}
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:7397

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

Referenced by get_prompt().

◆ setQFout()

bool setQFout ( const char *  fname)

Definition at line 144 of file common.c.

145{
146 FILE *fout;
147 bool is_pipe;
148
149 /* First make sure we can open the new output file/pipe */
150 if (!openQueryOutputFile(fname, &fout, &is_pipe))
151 return false;
152
153 /* Close old file/pipe */
154 if (pset.queryFout && pset.queryFout != stdout && pset.queryFout != stderr)
155 {
158 else
159 fclose(pset.queryFout);
160 }
161
162 pset.queryFout = fout;
163 pset.queryFoutPipe = is_pipe;
164
165 /* Adjust SIGPIPE handling appropriately: ignore signal if is_pipe */
166 set_sigpipe_trap_state(is_pipe);
168
169 return true;
170}
void SetShellResultVariables(int wait_result)
Definition: common.c:516
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:105
bool queryFoutPipe
Definition: settings.h:106

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

517{
518 char buf[32];
519
520 SetVariable(pset.vars, "SHELL_ERROR",
521 (wait_result == 0) ? "false" : "true");
522 snprintf(buf, sizeof(buf), "%d", wait_result_to_exit_code(wait_result));
523 SetVariable(pset.vars, "SHELL_EXIT_CODE", buf);
524}
#define snprintf
Definition: port.h:239
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(), evaluate_backtick(), exec_command_write(), and setQFout().

◆ standard_strings()

bool standard_strings ( void  )

Definition at line 2456 of file common.c.

2457{
2458 const char *val;
2459
2460 if (!pset.db)
2461 return false;
2462
2463 val = PQparameterStatus(pset.db, "standard_conforming_strings");
2464
2465 if (val && strcmp(val, "on") == 0)
2466 return true;
2467
2468 return false;
2469}

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