PostgreSQL Source Code  git master
util.c File Reference
#include "postgres_fe.h"
#include <signal.h>
#include "common/username.h"
#include "pg_upgrade.h"
Include dependency graph for util.c:

Go to the source code of this file.

Functions

static void pg_log_v (eLogType type, const char *fmt, va_list ap) pg_attribute_printf(2
 
static void void report_status (eLogType type, const char *fmt,...)
 
void end_progress_output (void)
 
void prep_status (const char *fmt,...)
 
void pg_log (eLogType type, const char *fmt,...)
 
void pg_fatal (const char *fmt,...)
 
void check_ok (void)
 
char * quote_identifier (const char *s)
 
int get_user_info (char **user_name_p)
 
unsigned int str2uint (const char *str)
 
void pg_putenv (const char *var, const char *val)
 

Variables

LogOpts log_opts
 

Function Documentation

◆ check_ok()

void check_ok ( void  )

Definition at line 171 of file util.c.

References PG_REPORT, report_status(), and generate_unaccent_rules::stdout.

172 {
173  /* all seems well */
174  report_status(PG_REPORT, "ok");
175  fflush(stdout);
176 }
static void void report_status(eLogType type, const char *fmt,...)
Definition: util.c:28

◆ end_progress_output()

void end_progress_output ( void  )

Definition at line 43 of file util.c.

References prep_status().

Referenced by create_new_objects(), generate_old_dump(), and transfer_all_new_tablespaces().

44 {
45  /*
46  * In case nothing printed; pass a space so gcc doesn't complain about
47  * empty format string.
48  */
49  prep_status(" ");
50 }
void prep_status(const char *fmt,...)
Definition: util.c:69

◆ get_user_info()

int get_user_info ( char **  user_name_p)

Definition at line 211 of file util.c.

References get_user_name(), pg_fatal(), and pg_strdup().

Referenced by parseCommandLine().

212 {
213  int user_id;
214  const char *user_name;
215  char *errstr;
216 
217 #ifndef WIN32
218  user_id = geteuid();
219 #else
220  user_id = 1;
221 #endif
222 
223  user_name = get_user_name(&errstr);
224  if (!user_name)
225  pg_fatal("%s\n", errstr);
226 
227  /* make a copy */
228  *user_name_p = pg_strdup(user_name);
229 
230  return user_id;
231 }
const char * get_user_name(char **errstr)
Definition: username.c:31
void pg_fatal(const char *fmt,...)
Definition: util.c:158
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85

◆ pg_fatal()

void pg_fatal ( const char *  fmt,
  ... 
)

Definition at line 158 of file util.c.

References _, generate_unaccent_rules::args, PG_FATAL, pg_log_v(), and printf.

Referenced by get_user_info().

159 {
160  va_list args;
161 
162  va_start(args, fmt);
163  pg_log_v(PG_FATAL, fmt, args);
164  va_end(args);
165  printf(_("Failure, exiting\n"));
166  exit(1);
167 }
static void pg_log_v(eLogType type, const char *fmt, va_list ap) pg_attribute_printf(2
Definition: util.c:87
#define printf(...)
Definition: port.h:198
#define _(x)
Definition: elog.c:87

◆ pg_log()

void pg_log ( eLogType  type,
const char *  fmt,
  ... 
)

Definition at line 147 of file util.c.

References generate_unaccent_rules::args, and pg_log_v().

Referenced by prep_status(), and report_status().

148 {
149  va_list args;
150 
151  va_start(args, fmt);
152  pg_log_v(type, fmt, args);
153  va_end(args);
154 }
static void pg_log_v(eLogType type, const char *fmt, va_list ap) pg_attribute_printf(2
Definition: util.c:87

◆ pg_log_v()

static void pg_log_v ( eLogType  type,
const char *  fmt,
va_list  ap 
)
static

Definition at line 87 of file util.c.

References _, fprintf, LogOpts::internal, MESSAGE_WIDTH, PG_FATAL, PG_REPORT, PG_STATUS, PG_VERBOSE, PG_WARNING, printf, QUERY_ALLOC, generate_unaccent_rules::stdout, LogOpts::verbose, and vsnprintf.

Referenced by pg_fatal(), and pg_log().

88 {
89  char message[QUERY_ALLOC];
90 
91  vsnprintf(message, sizeof(message), _(fmt), ap);
92 
93  /* PG_VERBOSE and PG_STATUS are only output in verbose mode */
94  /* fopen() on log_opts.internal might have failed, so check it */
95  if (((type != PG_VERBOSE && type != PG_STATUS) || log_opts.verbose) &&
96  log_opts.internal != NULL)
97  {
98  if (type == PG_STATUS)
99  /* status messages need two leading spaces and a newline */
100  fprintf(log_opts.internal, " %s\n", message);
101  else
102  fprintf(log_opts.internal, "%s", message);
103  fflush(log_opts.internal);
104  }
105 
106  switch (type)
107  {
108  case PG_VERBOSE:
109  if (log_opts.verbose)
110  printf("%s", message);
111  break;
112 
113  case PG_STATUS:
114  /* for output to a display, do leading truncation and append \r */
115  if (isatty(fileno(stdout)))
116  /* -2 because we use a 2-space indent */
117  printf(" %s%-*.*s\r",
118  /* prefix with "..." if we do leading truncation */
119  strlen(message) <= MESSAGE_WIDTH - 2 ? "" : "...",
120  MESSAGE_WIDTH - 2, MESSAGE_WIDTH - 2,
121  /* optional leading truncation */
122  strlen(message) <= MESSAGE_WIDTH - 2 ? message :
123  message + strlen(message) - MESSAGE_WIDTH + 3 + 2);
124  else
125  printf(" %s\n", message);
126  break;
127 
128  case PG_REPORT:
129  case PG_WARNING:
130  printf("%s", message);
131  break;
132 
133  case PG_FATAL:
134  printf("\n%s", message);
135  printf(_("Failure, exiting\n"));
136  exit(1);
137  break;
138 
139  default:
140  break;
141  }
142  fflush(stdout);
143 }
LogOpts log_opts
Definition: util.c:17
#define printf(...)
Definition: port.h:198
#define fprintf
Definition: port.h:196
#define vsnprintf
Definition: port.h:191
bool verbose
Definition: pg_upgrade.h:282
FILE * internal
Definition: pg_upgrade.h:281
#define MESSAGE_WIDTH
Definition: pg_upgrade.h:21
#define _(x)
Definition: elog.c:87
#define QUERY_ALLOC
Definition: pg_upgrade.h:19

◆ pg_putenv()

void pg_putenv ( const char *  var,
const char *  val 
)

Definition at line 253 of file util.c.

References psprintf(), putenv, and unsetenv.

Referenced by get_control_data(), and parseCommandLine().

254 {
255  if (val)
256  {
257 #ifndef WIN32
258  char *envstr;
259 
260  envstr = psprintf("%s=%s", var, val);
261  putenv(envstr);
262 
263  /*
264  * Do not free envstr because it becomes part of the environment on
265  * some operating systems. See port/unsetenv.c::unsetenv.
266  */
267 #else
268  SetEnvironmentVariableA(var, val);
269 #endif
270  }
271  else
272  {
273 #ifndef WIN32
274  unsetenv(var);
275 #else
276  SetEnvironmentVariableA(var, "");
277 #endif
278  }
279 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define putenv(x)
Definition: win32_port.h:474
long val
Definition: informix.c:664
#define unsetenv(x)
Definition: win32_port.h:475

◆ prep_status()

void prep_status ( const char *  fmt,
  ... 
)

Definition at line 69 of file util.c.

References generate_unaccent_rules::args, MAX_STRING, MESSAGE_WIDTH, pg_log(), PG_REPORT, and vsnprintf.

Referenced by end_progress_output().

70 {
71  va_list args;
72  char message[MAX_STRING];
73 
74  va_start(args, fmt);
75  vsnprintf(message, sizeof(message), fmt, args);
76  va_end(args);
77 
78  if (strlen(message) > 0 && message[strlen(message) - 1] == '\n')
79  pg_log(PG_REPORT, "%s", message);
80  else
81  /* trim strings that don't end in a newline */
82  pg_log(PG_REPORT, "%-*s", MESSAGE_WIDTH, message);
83 }
#define vsnprintf
Definition: port.h:191
#define MAX_STRING
Definition: pg_upgrade.h:18
#define MESSAGE_WIDTH
Definition: pg_upgrade.h:21
void pg_log(eLogType type, const char *fmt,...)
Definition: util.c:147

◆ quote_identifier()

char* quote_identifier ( const char *  s)

Definition at line 187 of file util.c.

References palloc(), pg_malloc(), quote_all_identifiers, ScanKeywordCategories, ScanKeywordLookup(), ScanKeywords, and UNRESERVED_KEYWORD.

188 {
189  char *result = pg_malloc(strlen(s) * 2 + 3);
190  char *r = result;
191 
192  *r++ = '"';
193  while (*s)
194  {
195  if (*s == '"')
196  *r++ = *s;
197  *r++ = *s;
198  s++;
199  }
200  *r++ = '"';
201  *r++ = '\0';
202 
203  return result;
204 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47

◆ report_status()

static void void report_status ( eLogType  type,
const char *  fmt,
  ... 
)

Definition at line 28 of file util.c.

References generate_unaccent_rules::args, MAX_STRING, pg_log(), and vsnprintf.

Referenced by check_ok().

29 {
30  va_list args;
31  char message[MAX_STRING];
32 
33  va_start(args, fmt);
34  vsnprintf(message, sizeof(message), fmt, args);
35  va_end(args);
36 
37  pg_log(type, "%s\n", message);
38 }
#define vsnprintf
Definition: port.h:191
#define MAX_STRING
Definition: pg_upgrade.h:18
void pg_log(eLogType type, const char *fmt,...)
Definition: util.c:147

◆ str2uint()

unsigned int str2uint ( const char *  str)

Definition at line 240 of file util.c.

Referenced by get_control_data().

241 {
242  return strtoul(str, NULL, 10);
243 }

Variable Documentation

◆ log_opts