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)
48 * Just an arbitrary number X where "a < x < b" where "a" is
49 * "maximum number of pthreads we'll ever plausibly spawn" and
50 * "b" is "something less than Inf", since the point is to
51 * prevent infinite recursion.
53 static const int recursion_limit
= 1024;
56 if (dying
> recursion_limit
) {
58 } else if (dying
== 2) {
59 warning("die() called many times. Recursion error or racy threaded death!");
66 /* If we are in a dlopen()ed .so write to a global variable would segfault
67 * (ugh), so keep things static. */
68 static NORETURN_PTR
void (*usage_routine
)(const char *err
, va_list params
) = usage_builtin
;
69 static NORETURN_PTR
void (*die_routine
)(const char *err
, va_list params
) = die_builtin
;
70 static void (*error_routine
)(const char *err
, va_list params
) = error_builtin
;
71 static void (*warn_routine
)(const char *err
, va_list params
) = warn_builtin
;
72 static int (*die_is_recursing
)(void) = die_is_recursing_builtin
;
74 void set_die_routine(NORETURN_PTR
void (*routine
)(const char *err
, va_list params
))
76 die_routine
= routine
;
79 void set_error_routine(void (*routine
)(const char *err
, va_list params
))
81 error_routine
= routine
;
84 void (*get_error_routine(void))(const char *err
, va_list params
)
89 void set_warn_routine(void (*routine
)(const char *warn
, va_list params
))
91 warn_routine
= routine
;
94 void (*get_warn_routine(void))(const char *warn
, va_list params
)
99 void set_die_is_recursing_routine(int (*routine
)(void))
101 die_is_recursing
= routine
;
104 void NORETURN
usagef(const char *err
, ...)
108 va_start(params
, err
);
109 usage_routine(err
, params
);
113 void NORETURN
usage(const char *err
)
118 void NORETURN
die(const char *err
, ...)
122 if (die_is_recursing()) {
123 fputs("fatal: recursion detected in die handler\n", stderr
);
127 va_start(params
, err
);
128 die_routine(err
, params
);
132 static const char *fmt_with_err(char *buf
, int n
, const char *fmt
)
134 char str_error
[256], *err
;
137 err
= strerror(errno
);
138 for (i
= j
= 0; err
[i
] && j
< sizeof(str_error
) - 1; ) {
139 if ((str_error
[j
++] = err
[i
++]) != '%')
141 if (j
< sizeof(str_error
) - 1) {
142 str_error
[j
++] = '%';
144 /* No room to double the '%', so we overwrite it with
151 snprintf(buf
, n
, "%s: %s", fmt
, str_error
);
155 void NORETURN
die_errno(const char *fmt
, ...)
160 if (die_is_recursing()) {
161 fputs("fatal: recursion detected in die_errno handler\n",
166 va_start(params
, fmt
);
167 die_routine(fmt_with_err(buf
, sizeof(buf
), fmt
), params
);
172 int error_errno(const char *fmt
, ...)
177 va_start(params
, fmt
);
178 error_routine(fmt_with_err(buf
, sizeof(buf
), fmt
), params
);
184 int error(const char *err
, ...)
188 va_start(params
, err
);
189 error_routine(err
, params
);
194 void warning_errno(const char *warn
, ...)
199 va_start(params
, warn
);
200 warn_routine(fmt_with_err(buf
, sizeof(buf
), warn
), params
);
204 void warning(const char *warn
, ...)
208 va_start(params
, warn
);
209 warn_routine(warn
, params
);
213 static NORETURN
void BUG_vfl(const char *file
, int line
, const char *fmt
, va_list params
)
217 /* truncation via snprintf is OK here */
219 snprintf(prefix
, sizeof(prefix
), "BUG: %s:%d: ", file
, line
);
221 snprintf(prefix
, sizeof(prefix
), "BUG: ");
223 vreportf(prefix
, fmt
, params
);
227 #ifdef HAVE_VARIADIC_MACROS
228 NORETURN
void BUG_fl(const char *file
, int line
, const char *fmt
, ...)
232 BUG_vfl(file
, line
, fmt
, ap
);
236 NORETURN
void BUG(const char *fmt
, ...)
240 BUG_vfl(NULL
, 0, fmt
, ap
);