2005-04-19 04:04:43 +08:00
|
|
|
/*
|
|
|
|
* GIT - The information manager from hell
|
|
|
|
*
|
|
|
|
* Copyright (C) Linus Torvalds, 2005
|
|
|
|
*/
|
2005-12-06 03:54:29 +08:00
|
|
|
#include "git-compat-util.h"
|
2005-04-19 04:04:43 +08:00
|
|
|
|
|
|
|
static void report(const char *prefix, const char *err, va_list params)
|
|
|
|
{
|
|
|
|
fputs(prefix, stderr);
|
|
|
|
vfprintf(stderr, err, params);
|
|
|
|
fputs("\n", stderr);
|
|
|
|
}
|
|
|
|
|
2006-06-24 13:44:33 +08:00
|
|
|
static NORETURN void usage_builtin(const char *err)
|
2005-04-19 04:04:43 +08:00
|
|
|
{
|
|
|
|
fprintf(stderr, "usage: %s\n", err);
|
2005-10-02 04:24:27 +08:00
|
|
|
exit(129);
|
2005-04-19 04:04:43 +08:00
|
|
|
}
|
|
|
|
|
2006-06-24 13:44:33 +08:00
|
|
|
static NORETURN void die_builtin(const char *err, va_list params)
|
2006-06-24 10:34:38 +08:00
|
|
|
{
|
|
|
|
report("fatal: ", err, params);
|
|
|
|
exit(128);
|
|
|
|
}
|
|
|
|
|
2006-06-24 13:44:33 +08:00
|
|
|
static void error_builtin(const char *err, va_list params)
|
2006-06-24 10:34:38 +08:00
|
|
|
{
|
|
|
|
report("error: ", err, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* If we are in a dlopen()ed .so write to a global variable would segfault
|
|
|
|
* (ugh), so keep things static. */
|
|
|
|
static void (*usage_routine)(const char *err) NORETURN = usage_builtin;
|
|
|
|
static void (*die_routine)(const char *err, va_list params) NORETURN = die_builtin;
|
|
|
|
static void (*error_routine)(const char *err, va_list params) = error_builtin;
|
|
|
|
|
|
|
|
void set_usage_routine(void (*routine)(const char *err) NORETURN)
|
|
|
|
{
|
|
|
|
usage_routine = routine;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN)
|
|
|
|
{
|
|
|
|
die_routine = routine;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_error_routine(void (*routine)(const char *err, va_list params))
|
|
|
|
{
|
|
|
|
error_routine = routine;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void usage(const char *err)
|
|
|
|
{
|
|
|
|
usage_routine(err);
|
|
|
|
}
|
|
|
|
|
2005-04-19 04:04:43 +08:00
|
|
|
void die(const char *err, ...)
|
|
|
|
{
|
|
|
|
va_list params;
|
|
|
|
|
|
|
|
va_start(params, err);
|
2006-06-24 10:34:38 +08:00
|
|
|
die_routine(err, params);
|
2005-04-19 04:04:43 +08:00
|
|
|
va_end(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
int error(const char *err, ...)
|
|
|
|
{
|
|
|
|
va_list params;
|
|
|
|
|
|
|
|
va_start(params, err);
|
2006-06-24 10:34:38 +08:00
|
|
|
error_routine(err, params);
|
2005-04-19 04:04:43 +08:00
|
|
|
va_end(params);
|
|
|
|
return -1;
|
|
|
|
}
|