2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 #include "git-compat-util.h"
9 static FILE *error_handle
;
11 void vreportf(const char *prefix
, const char *err
, va_list params
)
14 FILE *fh
= error_handle
? error_handle
: stderr
;
17 vsnprintf(msg
, sizeof(msg
), err
, params
);
18 for (p
= msg
; *p
; p
++) {
19 if (iscntrl(*p
) && *p
!= '\t' && *p
!= '\n')
22 fprintf(fh
, "%s%s\n", prefix
, msg
);
25 static NORETURN
void usage_builtin(const char *err
, va_list params
)
27 vreportf("usage: ", err
, params
);
31 static NORETURN
void die_builtin(const char *err
, va_list params
)
33 vreportf("fatal: ", err
, params
);
37 static void error_builtin(const char *err
, va_list params
)
39 vreportf("error: ", err
, params
);
42 static void warn_builtin(const char *warn
, va_list params
)
44 vreportf("warning: ", warn
, params
);
47 static int die_is_recursing_builtin(void)
53 /* If we are in a dlopen()ed .so write to a global variable would segfault
54 * (ugh), so keep things static. */
55 static NORETURN_PTR
void (*usage_routine
)(const char *err
, va_list params
) = usage_builtin
;
56 static NORETURN_PTR
void (*die_routine
)(const char *err
, va_list params
) = die_builtin
;
57 static void (*error_routine
)(const char *err
, va_list params
) = error_builtin
;
58 static void (*warn_routine
)(const char *err
, va_list params
) = warn_builtin
;
59 static int (*die_is_recursing
)(void) = die_is_recursing_builtin
;
61 void set_die_routine(NORETURN_PTR
void (*routine
)(const char *err
, va_list params
))
63 die_routine
= routine
;
66 void set_error_routine(void (*routine
)(const char *err
, va_list params
))
68 error_routine
= routine
;
71 void (*get_error_routine(void))(const char *err
, va_list params
)
76 void set_warn_routine(void (*routine
)(const char *warn
, va_list params
))
78 warn_routine
= routine
;
81 void (*get_warn_routine(void))(const char *warn
, va_list params
)
86 void set_die_is_recursing_routine(int (*routine
)(void))
88 die_is_recursing
= routine
;
91 void set_error_handle(FILE *fh
)
96 void NORETURN
usagef(const char *err
, ...)
100 va_start(params
, err
);
101 usage_routine(err
, params
);
105 void NORETURN
usage(const char *err
)
110 void NORETURN
die(const char *err
, ...)
114 if (die_is_recursing()) {
115 fputs("fatal: recursion detected in die handler\n", stderr
);
119 va_start(params
, err
);
120 die_routine(err
, params
);
124 static const char *fmt_with_err(char *buf
, int n
, const char *fmt
)
126 char str_error
[256], *err
;
129 err
= strerror(errno
);
130 for (i
= j
= 0; err
[i
] && j
< sizeof(str_error
) - 1; ) {
131 if ((str_error
[j
++] = err
[i
++]) != '%')
133 if (j
< sizeof(str_error
) - 1) {
134 str_error
[j
++] = '%';
136 /* No room to double the '%', so we overwrite it with
143 snprintf(buf
, n
, "%s: %s", fmt
, str_error
);
147 void NORETURN
die_errno(const char *fmt
, ...)
152 if (die_is_recursing()) {
153 fputs("fatal: recursion detected in die_errno handler\n",
158 va_start(params
, fmt
);
159 die_routine(fmt_with_err(buf
, sizeof(buf
), fmt
), params
);
164 int error_errno(const char *fmt
, ...)
169 va_start(params
, fmt
);
170 error_routine(fmt_with_err(buf
, sizeof(buf
), fmt
), params
);
176 int error(const char *err
, ...)
180 va_start(params
, err
);
181 error_routine(err
, params
);
186 void warning_errno(const char *warn
, ...)
191 va_start(params
, warn
);
192 warn_routine(fmt_with_err(buf
, sizeof(buf
), warn
), params
);
196 void warning(const char *warn
, ...)
200 va_start(params
, warn
);
201 warn_routine(warn
, params
);
205 static NORETURN
void BUG_vfl(const char *file
, int line
, const char *fmt
, va_list params
)
209 /* truncation via snprintf is OK here */
211 snprintf(prefix
, sizeof(prefix
), "BUG: %s:%d: ", file
, line
);
213 snprintf(prefix
, sizeof(prefix
), "BUG: ");
215 vreportf(prefix
, fmt
, params
);
219 #ifdef HAVE_VARIADIC_MACROS
220 void BUG_fl(const char *file
, int line
, const char *fmt
, ...)
224 BUG_vfl(file
, line
, fmt
, ap
);
228 void BUG(const char *fmt
, ...)
232 BUG_vfl(NULL
, 0, fmt
, ap
);