2005-04-18 20:04:43 +00:00
|
|
|
/*
|
|
|
|
* GIT - The information manager from hell
|
|
|
|
*
|
|
|
|
* Copyright (C) Linus Torvalds, 2005
|
|
|
|
*/
|
2005-12-05 19:54:29 +00:00
|
|
|
#include "git-compat-util.h"
|
2005-04-18 20:04:43 +00:00
|
|
|
|
2010-03-06 15:40:39 +00:00
|
|
|
void vreportf(const char *prefix, const char *err, va_list params)
|
2005-04-18 20:04:43 +00:00
|
|
|
{
|
2009-11-22 21:19:53 +00:00
|
|
|
char msg[4096];
|
2007-11-09 19:33:54 +00:00
|
|
|
vsnprintf(msg, sizeof(msg), err, params);
|
|
|
|
fprintf(stderr, "%s%s\n", prefix, msg);
|
2005-04-18 20:04:43 +00:00
|
|
|
}
|
|
|
|
|
2009-11-09 15:05:02 +00:00
|
|
|
static NORETURN void usage_builtin(const char *err, va_list params)
|
2005-04-18 20:04:43 +00:00
|
|
|
{
|
2010-03-06 15:40:39 +00:00
|
|
|
vreportf("usage: ", err, params);
|
2005-10-01 20:24:27 +00:00
|
|
|
exit(129);
|
2005-04-18 20:04:43 +00:00
|
|
|
}
|
|
|
|
|
2006-06-24 05:44:33 +00:00
|
|
|
static NORETURN void die_builtin(const char *err, va_list params)
|
2006-06-24 02:34:38 +00:00
|
|
|
{
|
2010-03-06 15:40:39 +00:00
|
|
|
vreportf("fatal: ", err, params);
|
2006-06-24 02:34:38 +00:00
|
|
|
exit(128);
|
|
|
|
}
|
|
|
|
|
2006-06-24 05:44:33 +00:00
|
|
|
static void error_builtin(const char *err, va_list params)
|
2006-06-24 02:34:38 +00:00
|
|
|
{
|
2010-03-06 15:40:39 +00:00
|
|
|
vreportf("error: ", err, params);
|
2006-06-24 02:34:38 +00:00
|
|
|
}
|
|
|
|
|
2006-12-22 00:48:32 +00:00
|
|
|
static void warn_builtin(const char *warn, va_list params)
|
|
|
|
{
|
2010-03-06 15:40:39 +00:00
|
|
|
vreportf("warning: ", warn, params);
|
2006-12-22 00:48:32 +00:00
|
|
|
}
|
2006-06-24 02:34:38 +00:00
|
|
|
|
|
|
|
/* If we are in a dlopen()ed .so write to a global variable would segfault
|
|
|
|
* (ugh), so keep things static. */
|
2009-11-09 15:05:02 +00:00
|
|
|
static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
|
2009-09-30 18:05:50 +00:00
|
|
|
static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
|
2006-06-24 02:34:38 +00:00
|
|
|
static void (*error_routine)(const char *err, va_list params) = error_builtin;
|
2006-12-22 00:48:32 +00:00
|
|
|
static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
|
2006-06-24 02:34:38 +00:00
|
|
|
|
2009-09-30 18:05:50 +00:00
|
|
|
void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
|
2006-06-24 02:34:38 +00:00
|
|
|
{
|
|
|
|
die_routine = routine;
|
|
|
|
}
|
|
|
|
|
2009-11-09 15:05:02 +00:00
|
|
|
void usagef(const char *err, ...)
|
|
|
|
{
|
|
|
|
va_list params;
|
|
|
|
|
|
|
|
va_start(params, err);
|
|
|
|
usage_routine(err, params);
|
|
|
|
va_end(params);
|
|
|
|
}
|
|
|
|
|
2006-06-24 02:34:38 +00:00
|
|
|
void usage(const char *err)
|
|
|
|
{
|
2009-11-09 15:05:02 +00:00
|
|
|
usagef("%s", err);
|
2006-06-24 02:34:38 +00:00
|
|
|
}
|
|
|
|
|
2005-04-18 20:04:43 +00:00
|
|
|
void die(const char *err, ...)
|
|
|
|
{
|
|
|
|
va_list params;
|
|
|
|
|
|
|
|
va_start(params, err);
|
2006-06-24 02:34:38 +00:00
|
|
|
die_routine(err, params);
|
2005-04-18 20:04:43 +00:00
|
|
|
va_end(params);
|
|
|
|
}
|
|
|
|
|
2009-06-27 15:58:44 +00:00
|
|
|
void die_errno(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list params;
|
|
|
|
char fmt_with_err[1024];
|
2009-06-27 15:58:45 +00:00
|
|
|
char str_error[256], *err;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
err = strerror(errno);
|
|
|
|
for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
|
|
|
|
if ((str_error[j++] = err[i++]) != '%')
|
|
|
|
continue;
|
|
|
|
if (j < sizeof(str_error) - 1) {
|
|
|
|
str_error[j++] = '%';
|
|
|
|
} else {
|
|
|
|
/* No room to double the '%', so we overwrite it with
|
|
|
|
* '\0' below */
|
|
|
|
j--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
str_error[j] = 0;
|
|
|
|
snprintf(fmt_with_err, sizeof(fmt_with_err), "%s: %s", fmt, str_error);
|
2009-06-27 15:58:44 +00:00
|
|
|
|
|
|
|
va_start(params, fmt);
|
|
|
|
die_routine(fmt_with_err, params);
|
|
|
|
va_end(params);
|
|
|
|
}
|
|
|
|
|
2005-04-18 20:04:43 +00:00
|
|
|
int error(const char *err, ...)
|
|
|
|
{
|
|
|
|
va_list params;
|
|
|
|
|
|
|
|
va_start(params, err);
|
2006-06-24 02:34:38 +00:00
|
|
|
error_routine(err, params);
|
2005-04-18 20:04:43 +00:00
|
|
|
va_end(params);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-12-22 00:48:32 +00:00
|
|
|
|
2007-03-30 23:07:05 +00:00
|
|
|
void warning(const char *warn, ...)
|
2006-12-22 00:48:32 +00:00
|
|
|
{
|
|
|
|
va_list params;
|
|
|
|
|
|
|
|
va_start(params, warn);
|
|
|
|
warn_routine(warn, params);
|
|
|
|
va_end(params);
|
|
|
|
}
|