PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
util.c File Reference
#include "postgres_fe.h"
#include "common/username.h"
#include "pg_upgrade.h"
#include <signal.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

void check_ok ( void  )

Definition at line 172 of file util.c.

References PG_REPORT, and report_status().

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

Definition at line 44 of file util.c.

References prep_status().

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

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

Definition at line 212 of file util.c.

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

Referenced by parseCommandLine().

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

Definition at line 159 of file util.c.

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

Referenced by get_user_info().

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

Definition at line 148 of file util.c.

References generate_unaccent_rules::args, and pg_log_v().

Referenced by prep_status(), and report_status().

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

Definition at line 88 of file util.c.

References _, LogOpts::internal, MESSAGE_WIDTH, NULL, PG_FATAL, PG_REPORT, PG_STATUS, PG_VERBOSE, PG_WARNING, QUERY_ALLOC, LogOpts::verbose, and vsnprintf().

Referenced by pg_fatal(), and pg_log().

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

Definition at line 254 of file util.c.

References psprintf(), putenv, and unsetenv.

Referenced by get_control_data(), and parseCommandLine().

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

Definition at line 70 of file util.c.

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

Referenced by end_progress_output().

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

Definition at line 188 of file util.c.

References ScanKeyword::category, NULL, NumScanKeywords, palloc(), pg_malloc(), quote_all_identifiers, result, ScanKeywordLookup(), ScanKeywords, and UNRESERVED_KEYWORD.

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

Definition at line 29 of file util.c.

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

Referenced by check_ok().

30 {
31  va_list args;
32  char message[MAX_STRING];
33 
34  va_start(args, fmt);
35  vsnprintf(message, sizeof(message), fmt, args);
36  va_end(args);
37 
38  pg_log(type, "%s\n", message);
39 }
int int vsnprintf(char *str, size_t count, const char *fmt, va_list args)
#define MAX_STRING
Definition: pg_upgrade.h:21
void pg_log(eLogType type, const char *fmt,...)
Definition: util.c:148
unsigned int str2uint ( const char *  str)

Definition at line 241 of file util.c.

References NULL.

Referenced by get_control_data().

242 {
243  return strtoul(str, NULL, 10);
244 }
#define NULL
Definition: c.h:229

Variable Documentation