2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 #include "git-compat-util.h"
9 void vreportf(const char *prefix, const char *err, va_list params)
14 vsnprintf(msg, sizeof(msg), err, params);
15 for (p = msg; *p; p++) {
16 if (iscntrl(*p) && *p != '\t' && *p != '\n')
19 fprintf(stderr, "%s%s\n", prefix, msg);
22 static NORETURN void usage_builtin(const char *err, va_list params)
24 vreportf("usage: ", err, params);
28 static NORETURN void die_builtin(const char *err, va_list params)
30 vreportf("fatal: ", err, params);
34 static void error_builtin(const char *err, va_list params)
36 vreportf("error: ", err, params);
39 static void warn_builtin(const char *warn, va_list params)
41 vreportf("warning: ", warn, params);
44 static int die_is_recursing_builtin(void)
50 /* If we are in a dlopen()ed .so write to a global variable would segfault
51 * (ugh), so keep things static. */
52 static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
53 static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
54 static void (*error_routine)(const char *err, va_list params) = error_builtin;
55 static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
56 static int (*die_is_recursing)(void) = die_is_recursing_builtin;
58 void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
60 die_routine = routine;
63 void set_error_routine(void (*routine)(const char *err, va_list params))
65 error_routine = routine;
68 void (*get_error_routine(void))(const char *err, va_list params)
73 void set_warn_routine(void (*routine)(const char *warn, va_list params))
75 warn_routine = routine;
78 void (*get_warn_routine(void))(const char *warn, va_list params)
83 void set_die_is_recursing_routine(int (*routine)(void))
85 die_is_recursing = routine;
88 void NORETURN usagef(const char *err, ...)
92 va_start(params, err);
93 usage_routine(err, params);
97 void NORETURN usage(const char *err)
102 void NORETURN die(const char *err, ...)
106 if (die_is_recursing()) {
107 fputs("fatal: recursion detected in die handler\n", stderr);
111 va_start(params, err);
112 die_routine(err, params);
116 static const char *fmt_with_err(char *buf, int n, const char *fmt)
118 char str_error[256], *err;
121 err = strerror(errno);
122 for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
123 if ((str_error[j++] = err[i++]) != '%')
125 if (j < sizeof(str_error) - 1) {
126 str_error[j++] = '%';
128 /* No room to double the '%', so we overwrite it with
135 snprintf(buf, n, "%s: %s", fmt, str_error);
139 void NORETURN die_errno(const char *fmt, ...)
144 if (die_is_recursing()) {
145 fputs("fatal: recursion detected in die_errno handler\n",
150 va_start(params, fmt);
151 die_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
156 int error_errno(const char *fmt, ...)
161 va_start(params, fmt);
162 error_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
168 int error(const char *err, ...)
172 va_start(params, err);
173 error_routine(err, params);
178 void warning_errno(const char *warn, ...)
183 va_start(params, warn);
184 warn_routine(fmt_with_err(buf, sizeof(buf), warn), params);
188 void warning(const char *warn, ...)
192 va_start(params, warn);
193 warn_routine(warn, params);