2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
7 #include "git-compat-util.h"
11 static void vfreportf(FILE *f
, const char *prefix
, const char *err
, va_list params
)
14 char *p
, *pend
= msg
+ sizeof(msg
);
15 size_t prefix_len
= strlen(prefix
);
17 if (sizeof(msg
) <= prefix_len
) {
18 fprintf(stderr
, "BUG!!! too long a prefix '%s'\n", prefix
);
21 memcpy(msg
, prefix
, prefix_len
);
23 if (vsnprintf(p
, pend
- p
, err
, params
) < 0) {
24 fprintf(stderr
, _("error: unable to format message: %s\n"),
26 *p
= '\0'; /* vsnprintf() failed, clip at prefix */
29 for (; p
!= pend
- 1 && *p
; p
++) {
30 if (iscntrl(*p
) && *p
!= '\t' && *p
!= '\n')
34 *(p
++) = '\n'; /* we no longer need a NUL */
36 write_in_full(fileno(f
), msg
, p
- msg
);
39 static void vreportf(const char *prefix
, const char *err
, va_list params
)
41 vfreportf(stderr
, prefix
, err
, params
);
44 static NORETURN
void usage_builtin(const char *err
, va_list params
)
46 vreportf(_("usage: "), err
, params
);
49 * When we detect a usage error *before* the command dispatch in
50 * cmd_main(), we don't know what verb to report. Force it to this
51 * to facilitate post-processing.
53 trace2_cmd_name("_usage_");
56 * Currently, the (err, params) are usually just the static usage
57 * string which isn't very useful here. Usually, the call site
58 * manually calls fprintf(stderr,...) with the actual detailed
59 * syntax error before calling usage().
61 * TODO It would be nice to update the call sites to pass both
62 * the static usage string and the detailed error message.
68 static void die_message_builtin(const char *err
, va_list params
)
72 trace2_cmd_error_va(err
, params
);
73 vreportf(_("fatal: "), err
, params
);
77 * We call trace2_cmd_error_va() in the below functions first and
78 * expect it to va_copy 'params' before using it (because an 'ap' can
79 * only be walked once).
81 static NORETURN
void die_builtin(const char *err
, va_list params
)
83 report_fn die_message_fn
= get_die_message_routine();
85 die_message_fn(err
, params
);
89 static void error_builtin(const char *err
, va_list params
)
91 trace2_cmd_error_va(err
, params
);
93 vreportf(_("error: "), err
, params
);
96 static void warn_builtin(const char *warn
, va_list params
)
98 trace2_cmd_error_va(warn
, params
);
100 vreportf(_("warning: "), warn
, params
);
103 static int die_is_recursing_builtin(void)
107 * Just an arbitrary number X where "a < x < b" where "a" is
108 * "maximum number of pthreads we'll ever plausibly spawn" and
109 * "b" is "something less than Inf", since the point is to
110 * prevent infinite recursion.
112 static const int recursion_limit
= 1024;
115 if (dying
> recursion_limit
) {
117 } else if (dying
== 2) {
118 warning("die() called many times. Recursion error or racy threaded death!");
125 /* If we are in a dlopen()ed .so write to a global variable would segfault
126 * (ugh), so keep things static. */
127 static NORETURN_PTR report_fn usage_routine
= usage_builtin
;
128 static NORETURN_PTR report_fn die_routine
= die_builtin
;
129 static report_fn die_message_routine
= die_message_builtin
;
130 static report_fn error_routine
= error_builtin
;
131 static report_fn warn_routine
= warn_builtin
;
132 static int (*die_is_recursing
)(void) = die_is_recursing_builtin
;
134 void set_die_routine(NORETURN_PTR report_fn routine
)
136 die_routine
= routine
;
139 report_fn
get_die_message_routine(void)
141 return die_message_routine
;
144 void set_error_routine(report_fn routine
)
146 error_routine
= routine
;
149 report_fn
get_error_routine(void)
151 return error_routine
;
154 void set_warn_routine(report_fn routine
)
156 warn_routine
= routine
;
159 report_fn
get_warn_routine(void)
164 void set_die_is_recursing_routine(int (*routine
)(void))
166 die_is_recursing
= routine
;
169 void NORETURN
usagef(const char *err
, ...)
173 va_start(params
, err
);
174 usage_routine(err
, params
);
178 void NORETURN
usage(const char *err
)
183 static void show_usage_if_asked_helper(const char *err
, ...)
187 va_start(params
, err
);
188 vfreportf(stdout
, _("usage: "), err
, params
);
193 void show_usage_if_asked(int ac
, const char **av
, const char *err
)
195 if (ac
== 2 && !strcmp(av
[1], "-h"))
196 show_usage_if_asked_helper(err
);
199 void NORETURN
die(const char *err
, ...)
203 if (die_is_recursing()) {
204 fputs("fatal: recursion detected in die handler\n", stderr
);
208 va_start(params
, err
);
209 die_routine(err
, params
);
213 static const char *fmt_with_err(char *buf
, int n
, const char *fmt
)
215 char str_error
[256], *err
;
218 err
= strerror(errno
);
219 for (i
= j
= 0; err
[i
] && j
< sizeof(str_error
) - 1; ) {
220 if ((str_error
[j
++] = err
[i
++]) != '%')
222 if (j
< sizeof(str_error
) - 1) {
223 str_error
[j
++] = '%';
225 /* No room to double the '%', so we overwrite it with
232 /* Truncation is acceptable here */
233 snprintf(buf
, n
, "%s: %s", fmt
, str_error
);
237 void NORETURN
die_errno(const char *fmt
, ...)
242 if (die_is_recursing()) {
243 fputs("fatal: recursion detected in die_errno handler\n",
248 va_start(params
, fmt
);
249 die_routine(fmt_with_err(buf
, sizeof(buf
), fmt
), params
);
254 int die_message(const char *err
, ...)
258 va_start(params
, err
);
259 die_message_routine(err
, params
);
264 #undef die_message_errno
265 int die_message_errno(const char *fmt
, ...)
270 va_start(params
, fmt
);
271 die_message_routine(fmt_with_err(buf
, sizeof(buf
), fmt
), params
);
277 int error_errno(const char *fmt
, ...)
282 va_start(params
, fmt
);
283 error_routine(fmt_with_err(buf
, sizeof(buf
), fmt
), params
);
289 int error(const char *err
, ...)
293 va_start(params
, err
);
294 error_routine(err
, params
);
299 void warning_errno(const char *warn
, ...)
304 va_start(params
, warn
);
305 warn_routine(fmt_with_err(buf
, sizeof(buf
), warn
), params
);
309 void warning(const char *warn
, ...)
313 va_start(params
, warn
);
314 warn_routine(warn
, params
);
318 /* Only set this, ever, from t/helper/, when verifying that bugs are caught. */
321 static void BUG_vfl_common(const char *file
, int line
, const char *fmt
,
326 /* truncation via snprintf is OK here */
327 snprintf(prefix
, sizeof(prefix
), "BUG: %s:%d: ", file
, line
);
329 vreportf(prefix
, fmt
, params
);
332 static NORETURN
void BUG_vfl(const char *file
, int line
, const char *fmt
, va_list params
)
337 va_copy(params_copy
, params
);
338 BUG_vfl_common(file
, line
, fmt
, params
);
344 trace2_cmd_error_va(fmt
, params_copy
);
351 NORETURN
void BUG_fl(const char *file
, int line
, const char *fmt
, ...)
355 bug_called_must_BUG
= 0;
358 BUG_vfl(file
, line
, fmt
, ap
);
362 int bug_called_must_BUG
;
363 void bug_fl(const char *file
, int line
, const char *fmt
, ...)
367 bug_called_must_BUG
= 1;
370 BUG_vfl_common(file
, line
, fmt
, ap
);
374 trace2_cmd_error_va(fmt
, ap
);
378 NORETURN
void you_still_use_that(const char *command_name
)
381 _("'%s' is nominated for removal.\n"
382 "If you still use this command, please add an extra\n"
383 "option, '--i-still-use-this', on the command line\n"
384 "and let us know you still use it by sending an e-mail\n"
385 "to <git@vger.kernel.org>. Thanks.\n"),
387 die(_("refusing to run without --i-still-use-this"));