2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 #include "git-compat-util.h"
9 static FILE *error_handle
;
10 static int tweaked_error_buffering
;
12 void vreportf(const char *prefix
, const char *err
, va_list params
)
14 FILE *fh
= error_handle
? error_handle
: stderr
;
17 if (!tweaked_error_buffering
) {
18 setvbuf(fh
, NULL
, _IOLBF
, 0);
19 tweaked_error_buffering
= 1;
23 vfprintf(fh
, err
, params
);
27 static NORETURN
void usage_builtin(const char *err
, va_list params
)
29 vreportf("usage: ", err
, params
);
33 static NORETURN
void die_builtin(const char *err
, va_list params
)
35 vreportf("fatal: ", err
, params
);
39 static void error_builtin(const char *err
, va_list params
)
41 vreportf("error: ", err
, params
);
44 static void warn_builtin(const char *warn
, va_list params
)
46 vreportf("warning: ", warn
, params
);
49 static int die_is_recursing_builtin(void)
55 /* If we are in a dlopen()ed .so write to a global variable would segfault
56 * (ugh), so keep things static. */
57 static NORETURN_PTR
void (*usage_routine
)(const char *err
, va_list params
) = usage_builtin
;
58 static NORETURN_PTR
void (*die_routine
)(const char *err
, va_list params
) = die_builtin
;
59 static void (*error_routine
)(const char *err
, va_list params
) = error_builtin
;
60 static void (*warn_routine
)(const char *err
, va_list params
) = warn_builtin
;
61 static int (*die_is_recursing
)(void) = die_is_recursing_builtin
;
63 void set_die_routine(NORETURN_PTR
void (*routine
)(const char *err
, va_list params
))
65 die_routine
= routine
;
68 void set_error_routine(void (*routine
)(const char *err
, va_list params
))
70 error_routine
= routine
;
73 void (*get_error_routine(void))(const char *err
, va_list params
)
78 void set_warn_routine(void (*routine
)(const char *warn
, va_list params
))
80 warn_routine
= routine
;
83 void (*get_warn_routine(void))(const char *warn
, va_list params
)
88 void set_die_is_recursing_routine(int (*routine
)(void))
90 die_is_recursing
= routine
;
93 void set_error_handle(FILE *fh
)
96 tweaked_error_buffering
= 0;
99 void NORETURN
usagef(const char *err
, ...)
103 va_start(params
, err
);
104 usage_routine(err
, params
);
108 void NORETURN
usage(const char *err
)
113 void NORETURN
die(const char *err
, ...)
117 if (die_is_recursing()) {
118 fputs("fatal: recursion detected in die handler\n", stderr
);
122 va_start(params
, err
);
123 die_routine(err
, params
);
127 static const char *fmt_with_err(char *buf
, int n
, const char *fmt
)
129 char str_error
[256], *err
;
132 err
= strerror(errno
);
133 for (i
= j
= 0; err
[i
] && j
< sizeof(str_error
) - 1; ) {
134 if ((str_error
[j
++] = err
[i
++]) != '%')
136 if (j
< sizeof(str_error
) - 1) {
137 str_error
[j
++] = '%';
139 /* No room to double the '%', so we overwrite it with
146 snprintf(buf
, n
, "%s: %s", fmt
, str_error
);
150 void NORETURN
die_errno(const char *fmt
, ...)
155 if (die_is_recursing()) {
156 fputs("fatal: recursion detected in die_errno handler\n",
161 va_start(params
, fmt
);
162 die_routine(fmt_with_err(buf
, sizeof(buf
), fmt
), params
);
167 int error_errno(const char *fmt
, ...)
172 va_start(params
, fmt
);
173 error_routine(fmt_with_err(buf
, sizeof(buf
), fmt
), params
);
179 int error(const char *err
, ...)
183 va_start(params
, err
);
184 error_routine(err
, params
);
189 void warning_errno(const char *warn
, ...)
194 va_start(params
, warn
);
195 warn_routine(fmt_with_err(buf
, sizeof(buf
), warn
), params
);
199 void warning(const char *warn
, ...)
203 va_start(params
, warn
);
204 warn_routine(warn
, params
);